remoteproc_virtio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Remote processor messaging transport (OMAP platform-specific bits)
  4. *
  5. * Copyright (C) 2011 Texas Instruments, Inc.
  6. * Copyright (C) 2011 Google, Inc.
  7. *
  8. * Ohad Ben-Cohen <ohad@wizery.com>
  9. * Brian Swetland <swetland@google.com>
  10. */
  11. #include <linux/dma-map-ops.h>
  12. #include <linux/export.h>
  13. #include <linux/of_reserved_mem.h>
  14. #include <linux/remoteproc.h>
  15. #include <linux/virtio.h>
  16. #include <linux/virtio_config.h>
  17. #include <linux/virtio_ids.h>
  18. #include <linux/virtio_ring.h>
  19. #include <linux/err.h>
  20. #include <linux/kref.h>
  21. #include <linux/slab.h>
  22. #include "remoteproc_internal.h"
  23. /* kick the remote processor, and let it know which virtqueue to poke at */
  24. static bool rproc_virtio_notify(struct virtqueue *vq)
  25. {
  26. struct rproc_vring *rvring = vq->priv;
  27. struct rproc *rproc = rvring->rvdev->rproc;
  28. int notifyid = rvring->notifyid;
  29. dev_dbg(&rproc->dev, "kicking vq index: %d\n", notifyid);
  30. rproc->ops->kick(rproc, notifyid);
  31. return true;
  32. }
  33. /**
  34. * rproc_vq_interrupt() - tell remoteproc that a virtqueue is interrupted
  35. * @rproc: handle to the remote processor
  36. * @notifyid: index of the signalled virtqueue (unique per this @rproc)
  37. *
  38. * This function should be called by the platform-specific rproc driver,
  39. * when the remote processor signals that a specific virtqueue has pending
  40. * messages available.
  41. *
  42. * Returns IRQ_NONE if no message was found in the @notifyid virtqueue,
  43. * and otherwise returns IRQ_HANDLED.
  44. */
  45. irqreturn_t rproc_vq_interrupt(struct rproc *rproc, int notifyid)
  46. {
  47. struct rproc_vring *rvring;
  48. dev_dbg(&rproc->dev, "vq index %d is interrupted\n", notifyid);
  49. rvring = idr_find(&rproc->notifyids, notifyid);
  50. if (!rvring || !rvring->vq)
  51. return IRQ_NONE;
  52. return vring_interrupt(0, rvring->vq);
  53. }
  54. EXPORT_SYMBOL(rproc_vq_interrupt);
  55. static struct virtqueue *rp_find_vq(struct virtio_device *vdev,
  56. unsigned int id,
  57. void (*callback)(struct virtqueue *vq),
  58. const char *name, bool ctx)
  59. {
  60. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  61. struct rproc *rproc = vdev_to_rproc(vdev);
  62. struct device *dev = &rproc->dev;
  63. struct rproc_mem_entry *mem;
  64. struct rproc_vring *rvring;
  65. struct fw_rsc_vdev *rsc;
  66. struct virtqueue *vq;
  67. void *addr;
  68. int len, size;
  69. /* we're temporarily limited to two virtqueues per rvdev */
  70. if (id >= ARRAY_SIZE(rvdev->vring))
  71. return ERR_PTR(-EINVAL);
  72. if (!name)
  73. return NULL;
  74. /* Search allocated memory region by name */
  75. mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index,
  76. id);
  77. if (!mem || !mem->va)
  78. return ERR_PTR(-ENOMEM);
  79. rvring = &rvdev->vring[id];
  80. addr = mem->va;
  81. len = rvring->len;
  82. /* zero vring */
  83. size = vring_size(len, rvring->align);
  84. memset(addr, 0, size);
  85. dev_dbg(dev, "vring%d: va %pK qsz %d notifyid %d\n",
  86. id, addr, len, rvring->notifyid);
  87. /*
  88. * Create the new vq, and tell virtio we're not interested in
  89. * the 'weak' smp barriers, since we're talking with a real device.
  90. */
  91. vq = vring_new_virtqueue(id, len, rvring->align, vdev, false, ctx,
  92. addr, rproc_virtio_notify, callback, name);
  93. if (!vq) {
  94. dev_err(dev, "vring_new_virtqueue %s failed\n", name);
  95. rproc_free_vring(rvring);
  96. return ERR_PTR(-ENOMEM);
  97. }
  98. rvring->vq = vq;
  99. vq->priv = rvring;
  100. /* Update vring in resource table */
  101. rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
  102. rsc->vring[id].da = mem->da;
  103. return vq;
  104. }
  105. static void __rproc_virtio_del_vqs(struct virtio_device *vdev)
  106. {
  107. struct virtqueue *vq, *n;
  108. struct rproc_vring *rvring;
  109. list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
  110. rvring = vq->priv;
  111. rvring->vq = NULL;
  112. vring_del_virtqueue(vq);
  113. }
  114. }
  115. static void rproc_virtio_del_vqs(struct virtio_device *vdev)
  116. {
  117. __rproc_virtio_del_vqs(vdev);
  118. }
  119. static int rproc_virtio_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
  120. struct virtqueue *vqs[],
  121. vq_callback_t *callbacks[],
  122. const char * const names[],
  123. const bool * ctx,
  124. struct irq_affinity *desc)
  125. {
  126. int i, ret, queue_idx = 0;
  127. for (i = 0; i < nvqs; ++i) {
  128. if (!names[i]) {
  129. vqs[i] = NULL;
  130. continue;
  131. }
  132. vqs[i] = rp_find_vq(vdev, queue_idx++, callbacks[i], names[i],
  133. ctx ? ctx[i] : false);
  134. if (IS_ERR(vqs[i])) {
  135. ret = PTR_ERR(vqs[i]);
  136. goto error;
  137. }
  138. }
  139. return 0;
  140. error:
  141. __rproc_virtio_del_vqs(vdev);
  142. return ret;
  143. }
  144. static u8 rproc_virtio_get_status(struct virtio_device *vdev)
  145. {
  146. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  147. struct fw_rsc_vdev *rsc;
  148. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  149. return rsc->status;
  150. }
  151. static void rproc_virtio_set_status(struct virtio_device *vdev, u8 status)
  152. {
  153. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  154. struct fw_rsc_vdev *rsc;
  155. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  156. rsc->status = status;
  157. dev_dbg(&vdev->dev, "status: %d\n", status);
  158. }
  159. static void rproc_virtio_reset(struct virtio_device *vdev)
  160. {
  161. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  162. struct fw_rsc_vdev *rsc;
  163. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  164. rsc->status = 0;
  165. dev_dbg(&vdev->dev, "reset !\n");
  166. }
  167. /* provide the vdev features as retrieved from the firmware */
  168. static u64 rproc_virtio_get_features(struct virtio_device *vdev)
  169. {
  170. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  171. struct fw_rsc_vdev *rsc;
  172. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  173. return rsc->dfeatures;
  174. }
  175. static void rproc_transport_features(struct virtio_device *vdev)
  176. {
  177. /*
  178. * Packed ring isn't enabled on remoteproc for now,
  179. * because remoteproc uses vring_new_virtqueue() which
  180. * creates virtio rings on preallocated memory.
  181. */
  182. __virtio_clear_bit(vdev, VIRTIO_F_RING_PACKED);
  183. }
  184. static int rproc_virtio_finalize_features(struct virtio_device *vdev)
  185. {
  186. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  187. struct fw_rsc_vdev *rsc;
  188. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  189. /* Give virtio_ring a chance to accept features */
  190. vring_transport_features(vdev);
  191. /* Give virtio_rproc a chance to accept features. */
  192. rproc_transport_features(vdev);
  193. /* Make sure we don't have any features > 32 bits! */
  194. BUG_ON((u32)vdev->features != vdev->features);
  195. /*
  196. * Remember the finalized features of our vdev, and provide it
  197. * to the remote processor once it is powered on.
  198. */
  199. rsc->gfeatures = vdev->features;
  200. return 0;
  201. }
  202. static void rproc_virtio_get(struct virtio_device *vdev, unsigned int offset,
  203. void *buf, unsigned int len)
  204. {
  205. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  206. struct fw_rsc_vdev *rsc;
  207. void *cfg;
  208. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  209. cfg = &rsc->vring[rsc->num_of_vrings];
  210. if (offset + len > rsc->config_len || offset + len < len) {
  211. dev_err(&vdev->dev, "rproc_virtio_get: access out of bounds\n");
  212. return;
  213. }
  214. memcpy(buf, cfg + offset, len);
  215. }
  216. static void rproc_virtio_set(struct virtio_device *vdev, unsigned int offset,
  217. const void *buf, unsigned int len)
  218. {
  219. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  220. struct fw_rsc_vdev *rsc;
  221. void *cfg;
  222. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  223. cfg = &rsc->vring[rsc->num_of_vrings];
  224. if (offset + len > rsc->config_len || offset + len < len) {
  225. dev_err(&vdev->dev, "rproc_virtio_set: access out of bounds\n");
  226. return;
  227. }
  228. memcpy(cfg + offset, buf, len);
  229. }
  230. static const struct virtio_config_ops rproc_virtio_config_ops = {
  231. .get_features = rproc_virtio_get_features,
  232. .finalize_features = rproc_virtio_finalize_features,
  233. .find_vqs = rproc_virtio_find_vqs,
  234. .del_vqs = rproc_virtio_del_vqs,
  235. .reset = rproc_virtio_reset,
  236. .set_status = rproc_virtio_set_status,
  237. .get_status = rproc_virtio_get_status,
  238. .get = rproc_virtio_get,
  239. .set = rproc_virtio_set,
  240. };
  241. /*
  242. * This function is called whenever vdev is released, and is responsible
  243. * to decrement the remote processor's refcount which was taken when vdev was
  244. * added.
  245. *
  246. * Never call this function directly; it will be called by the driver
  247. * core when needed.
  248. */
  249. static void rproc_virtio_dev_release(struct device *dev)
  250. {
  251. struct virtio_device *vdev = dev_to_virtio(dev);
  252. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  253. struct rproc *rproc = vdev_to_rproc(vdev);
  254. kfree(vdev);
  255. kref_put(&rvdev->refcount, rproc_vdev_release);
  256. put_device(&rproc->dev);
  257. }
  258. /**
  259. * rproc_add_virtio_dev() - register an rproc-induced virtio device
  260. * @rvdev: the remote vdev
  261. * @id: the device type identification (used to match it with a driver).
  262. *
  263. * This function registers a virtio device. This vdev's partent is
  264. * the rproc device.
  265. *
  266. * Returns 0 on success or an appropriate error value otherwise.
  267. */
  268. int rproc_add_virtio_dev(struct rproc_vdev *rvdev, int id)
  269. {
  270. struct rproc *rproc = rvdev->rproc;
  271. struct device *dev = &rvdev->dev;
  272. struct virtio_device *vdev;
  273. struct rproc_mem_entry *mem;
  274. int ret;
  275. if (rproc->ops->kick == NULL) {
  276. ret = -EINVAL;
  277. dev_err(dev, ".kick method not defined for %s\n", rproc->name);
  278. goto out;
  279. }
  280. /* Try to find dedicated vdev buffer carveout */
  281. mem = rproc_find_carveout_by_name(rproc, "vdev%dbuffer", rvdev->index);
  282. if (mem) {
  283. phys_addr_t pa;
  284. if (mem->of_resm_idx != -1) {
  285. struct device_node *np = rproc->dev.parent->of_node;
  286. /* Associate reserved memory to vdev device */
  287. ret = of_reserved_mem_device_init_by_idx(dev, np,
  288. mem->of_resm_idx);
  289. if (ret) {
  290. dev_err(dev, "Can't associate reserved memory\n");
  291. goto out;
  292. }
  293. } else {
  294. if (mem->va) {
  295. dev_warn(dev, "vdev %d buffer already mapped\n",
  296. rvdev->index);
  297. pa = rproc_va_to_pa(mem->va);
  298. } else {
  299. /* Use dma address as carveout no memmapped yet */
  300. pa = (phys_addr_t)mem->dma;
  301. }
  302. /* Associate vdev buffer memory pool to vdev subdev */
  303. ret = dma_declare_coherent_memory(dev, pa,
  304. mem->da,
  305. mem->len);
  306. if (ret < 0) {
  307. dev_err(dev, "Failed to associate buffer\n");
  308. goto out;
  309. }
  310. }
  311. } else {
  312. struct device_node *np = rproc->dev.parent->of_node;
  313. /*
  314. * If we don't have dedicated buffer, just attempt to re-assign
  315. * the reserved memory from our parent. A default memory-region
  316. * at index 0 from the parent's memory-regions is assigned for
  317. * the rvdev dev to allocate from. Failure is non-critical and
  318. * the allocations will fall back to global pools, so don't
  319. * check return value either.
  320. */
  321. of_reserved_mem_device_init_by_idx(dev, np, 0);
  322. }
  323. /* Allocate virtio device */
  324. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  325. if (!vdev) {
  326. ret = -ENOMEM;
  327. goto out;
  328. }
  329. vdev->id.device = id,
  330. vdev->config = &rproc_virtio_config_ops,
  331. vdev->dev.parent = dev;
  332. vdev->dev.release = rproc_virtio_dev_release;
  333. /*
  334. * We're indirectly making a non-temporary copy of the rproc pointer
  335. * here, because drivers probed with this vdev will indirectly
  336. * access the wrapping rproc.
  337. *
  338. * Therefore we must increment the rproc refcount here, and decrement
  339. * it _only_ when the vdev is released.
  340. */
  341. get_device(&rproc->dev);
  342. /* Reference the vdev and vring allocations */
  343. kref_get(&rvdev->refcount);
  344. ret = register_virtio_device(vdev);
  345. if (ret) {
  346. put_device(&vdev->dev);
  347. dev_err(dev, "failed to register vdev: %d\n", ret);
  348. goto out;
  349. }
  350. dev_info(dev, "registered %s (type %d)\n", dev_name(&vdev->dev), id);
  351. out:
  352. return ret;
  353. }
  354. /**
  355. * rproc_remove_virtio_dev() - remove an rproc-induced virtio device
  356. * @dev: the virtio device
  357. * @data: must be null
  358. *
  359. * This function unregisters an existing virtio device.
  360. */
  361. int rproc_remove_virtio_dev(struct device *dev, void *data)
  362. {
  363. struct virtio_device *vdev = dev_to_virtio(dev);
  364. unregister_virtio_device(vdev);
  365. return 0;
  366. }