remoteproc_virtio.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Remote processor messaging transport (OMAP platform-specific bits)
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. * Copyright (C) 2011 Google, Inc.
  6. *
  7. * Ohad Ben-Cohen <ohad@wizery.com>
  8. * Brian Swetland <swetland@google.com>
  9. *
  10. * This software is licensed under the terms of the GNU General Public
  11. * License version 2, as published by the Free Software Foundation, and
  12. * may be copied, distributed, and modified under those terms.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/export.h>
  20. #include <linux/remoteproc.h>
  21. #include <linux/virtio.h>
  22. #include <linux/virtio_config.h>
  23. #include <linux/virtio_ids.h>
  24. #include <linux/virtio_ring.h>
  25. #include <linux/err.h>
  26. #include <linux/kref.h>
  27. #include <linux/slab.h>
  28. #include "remoteproc_internal.h"
  29. /* kick the remote processor, and let it know which virtqueue to poke at */
  30. static void rproc_virtio_notify(struct virtqueue *vq)
  31. {
  32. struct rproc_vring *rvring = vq->priv;
  33. struct rproc *rproc = rvring->rvdev->rproc;
  34. int notifyid = rvring->notifyid;
  35. dev_dbg(rproc->dev, "kicking vq index: %d\n", notifyid);
  36. rproc->ops->kick(rproc, notifyid);
  37. }
  38. /**
  39. * rproc_vq_interrupt() - tell remoteproc that a virtqueue is interrupted
  40. * @rproc: handle to the remote processor
  41. * @notifyid: index of the signalled virtqueue (unique per this @rproc)
  42. *
  43. * This function should be called by the platform-specific rproc driver,
  44. * when the remote processor signals that a specific virtqueue has pending
  45. * messages available.
  46. *
  47. * Returns IRQ_NONE if no message was found in the @notifyid virtqueue,
  48. * and otherwise returns IRQ_HANDLED.
  49. */
  50. irqreturn_t rproc_vq_interrupt(struct rproc *rproc, int notifyid)
  51. {
  52. struct rproc_vring *rvring;
  53. dev_dbg(rproc->dev, "vq index %d is interrupted\n", notifyid);
  54. rvring = idr_find(&rproc->notifyids, notifyid);
  55. if (!rvring || !rvring->vq)
  56. return IRQ_NONE;
  57. return vring_interrupt(0, rvring->vq);
  58. }
  59. EXPORT_SYMBOL(rproc_vq_interrupt);
  60. static struct virtqueue *rp_find_vq(struct virtio_device *vdev,
  61. unsigned id,
  62. void (*callback)(struct virtqueue *vq),
  63. const char *name)
  64. {
  65. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  66. struct rproc *rproc = vdev_to_rproc(vdev);
  67. struct rproc_vring *rvring;
  68. struct virtqueue *vq;
  69. void *addr;
  70. int len, size;
  71. /* we're temporarily limited to two virtqueues per rvdev */
  72. if (id >= ARRAY_SIZE(rvdev->vring))
  73. return ERR_PTR(-EINVAL);
  74. rvring = &rvdev->vring[id];
  75. addr = rvring->va;
  76. len = rvring->len;
  77. /* zero vring */
  78. size = vring_size(len, rvring->align);
  79. memset(addr, 0, size);
  80. dev_dbg(rproc->dev, "vring%d: va %p qsz %d notifyid %d\n",
  81. id, addr, len, rvring->notifyid);
  82. /*
  83. * Create the new vq, and tell virtio we're not interested in
  84. * the 'weak' smp barriers, since we're talking with a real device.
  85. */
  86. vq = vring_new_virtqueue(len, rvring->align, vdev, false, addr,
  87. rproc_virtio_notify, callback, name);
  88. if (!vq) {
  89. dev_err(rproc->dev, "vring_new_virtqueue %s failed\n", name);
  90. return ERR_PTR(-ENOMEM);
  91. }
  92. rvring->vq = vq;
  93. vq->priv = rvring;
  94. return vq;
  95. }
  96. static void rproc_virtio_del_vqs(struct virtio_device *vdev)
  97. {
  98. struct virtqueue *vq, *n;
  99. struct rproc *rproc = vdev_to_rproc(vdev);
  100. struct rproc_vring *rvring;
  101. /* power down the remote processor before deleting vqs */
  102. rproc_shutdown(rproc);
  103. list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
  104. rvring = vq->priv;
  105. rvring->vq = NULL;
  106. vring_del_virtqueue(vq);
  107. }
  108. }
  109. static int rproc_virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  110. struct virtqueue *vqs[],
  111. vq_callback_t *callbacks[],
  112. const char *names[])
  113. {
  114. struct rproc *rproc = vdev_to_rproc(vdev);
  115. int i, ret;
  116. for (i = 0; i < nvqs; ++i) {
  117. vqs[i] = rp_find_vq(vdev, i, callbacks[i], names[i]);
  118. if (IS_ERR(vqs[i])) {
  119. ret = PTR_ERR(vqs[i]);
  120. goto error;
  121. }
  122. }
  123. /* now that the vqs are all set, boot the remote processor */
  124. ret = rproc_boot(rproc);
  125. if (ret) {
  126. dev_err(rproc->dev, "rproc_boot() failed %d\n", ret);
  127. goto error;
  128. }
  129. return 0;
  130. error:
  131. rproc_virtio_del_vqs(vdev);
  132. return ret;
  133. }
  134. /*
  135. * We don't support yet real virtio status semantics.
  136. *
  137. * The plan is to provide this via the VDEV resource entry
  138. * which is part of the firmware: this way the remote processor
  139. * will be able to access the status values as set by us.
  140. */
  141. static u8 rproc_virtio_get_status(struct virtio_device *vdev)
  142. {
  143. return 0;
  144. }
  145. static void rproc_virtio_set_status(struct virtio_device *vdev, u8 status)
  146. {
  147. dev_dbg(&vdev->dev, "status: %d\n", status);
  148. }
  149. static void rproc_virtio_reset(struct virtio_device *vdev)
  150. {
  151. dev_dbg(&vdev->dev, "reset !\n");
  152. }
  153. /* provide the vdev features as retrieved from the firmware */
  154. static u32 rproc_virtio_get_features(struct virtio_device *vdev)
  155. {
  156. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  157. return rvdev->dfeatures;
  158. }
  159. static void rproc_virtio_finalize_features(struct virtio_device *vdev)
  160. {
  161. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  162. /* Give virtio_ring a chance to accept features */
  163. vring_transport_features(vdev);
  164. /*
  165. * Remember the finalized features of our vdev, and provide it
  166. * to the remote processor once it is powered on.
  167. *
  168. * Similarly to the status field, we don't expose yet the negotiated
  169. * features to the remote processors at this point. This will be
  170. * fixed as part of a small resource table overhaul and then an
  171. * extension of the virtio resource entries.
  172. */
  173. rvdev->gfeatures = vdev->features[0];
  174. }
  175. static struct virtio_config_ops rproc_virtio_config_ops = {
  176. .get_features = rproc_virtio_get_features,
  177. .finalize_features = rproc_virtio_finalize_features,
  178. .find_vqs = rproc_virtio_find_vqs,
  179. .del_vqs = rproc_virtio_del_vqs,
  180. .reset = rproc_virtio_reset,
  181. .set_status = rproc_virtio_set_status,
  182. .get_status = rproc_virtio_get_status,
  183. };
  184. /*
  185. * This function is called whenever vdev is released, and is responsible
  186. * to decrement the remote processor's refcount taken when vdev was
  187. * added.
  188. *
  189. * Never call this function directly; it will be called by the driver
  190. * core when needed.
  191. */
  192. static void rproc_vdev_release(struct device *dev)
  193. {
  194. struct virtio_device *vdev = dev_to_virtio(dev);
  195. struct rproc *rproc = vdev_to_rproc(vdev);
  196. kref_put(&rproc->refcount, rproc_release);
  197. }
  198. /**
  199. * rproc_add_virtio_dev() - register an rproc-induced virtio device
  200. * @rvdev: the remote vdev
  201. *
  202. * This function registers a virtio device. This vdev's partent is
  203. * the rproc device.
  204. *
  205. * Returns 0 on success or an appropriate error value otherwise.
  206. */
  207. int rproc_add_virtio_dev(struct rproc_vdev *rvdev, int id)
  208. {
  209. struct rproc *rproc = rvdev->rproc;
  210. struct device *dev = rproc->dev;
  211. struct virtio_device *vdev = &rvdev->vdev;
  212. int ret;
  213. vdev->id.device = id,
  214. vdev->config = &rproc_virtio_config_ops,
  215. vdev->dev.parent = dev;
  216. vdev->dev.release = rproc_vdev_release;
  217. /*
  218. * We're indirectly making a non-temporary copy of the rproc pointer
  219. * here, because drivers probed with this vdev will indirectly
  220. * access the wrapping rproc.
  221. *
  222. * Therefore we must increment the rproc refcount here, and decrement
  223. * it _only_ when the vdev is released.
  224. */
  225. kref_get(&rproc->refcount);
  226. ret = register_virtio_device(vdev);
  227. if (ret) {
  228. kref_put(&rproc->refcount, rproc_release);
  229. dev_err(dev, "failed to register vdev: %d\n", ret);
  230. goto out;
  231. }
  232. dev_info(dev, "registered %s (type %d)\n", dev_name(&vdev->dev), id);
  233. out:
  234. return ret;
  235. }
  236. /**
  237. * rproc_remove_virtio_dev() - remove an rproc-induced virtio device
  238. * @rvdev: the remote vdev
  239. *
  240. * This function unregisters an existing virtio device.
  241. */
  242. void rproc_remove_virtio_dev(struct rproc_vdev *rvdev)
  243. {
  244. unregister_virtio_device(&rvdev->vdev);
  245. }