remoteproc_rpmsg.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/rpmsg.h>
  22. #include <linux/virtio.h>
  23. #include <linux/virtio_config.h>
  24. #include <linux/virtio_ids.h>
  25. #include <linux/virtio_ring.h>
  26. #include <linux/err.h>
  27. #include <linux/kref.h>
  28. #include <linux/slab.h>
  29. #include "remoteproc_internal.h"
  30. /**
  31. * struct rproc_virtio_vq_info - virtqueue state
  32. * @vq_id: a unique index of this virtqueue (unique for this @rproc)
  33. * @rproc: handle to the remote processor
  34. *
  35. * Such a struct will be maintained for every virtqueue we're
  36. * using to communicate with the remote processor
  37. */
  38. struct rproc_virtio_vq_info {
  39. __u16 vq_id;
  40. struct rproc *rproc;
  41. };
  42. /* kick the remote processor, and let it know which virtqueue to poke at */
  43. static void rproc_virtio_notify(struct virtqueue *vq)
  44. {
  45. struct rproc_virtio_vq_info *rpvq = vq->priv;
  46. struct rproc *rproc = rpvq->rproc;
  47. dev_dbg(rproc->dev, "kicking vq id: %d\n", rpvq->vq_id);
  48. rproc->ops->kick(rproc, rpvq->vq_id);
  49. }
  50. /**
  51. * rproc_vq_interrupt() - tell remoteproc that a virtqueue is interrupted
  52. * @rproc: handle to the remote processor
  53. * @vq_id: index of the signalled virtqueue
  54. *
  55. * This function should be called by the platform-specific rproc driver,
  56. * when the remote processor signals that a specific virtqueue has pending
  57. * messages available.
  58. *
  59. * Returns IRQ_NONE if no message was found in the @vq_id virtqueue,
  60. * and otherwise returns IRQ_HANDLED.
  61. */
  62. irqreturn_t rproc_vq_interrupt(struct rproc *rproc, int vq_id)
  63. {
  64. return vring_interrupt(0, rproc->rvdev->vq[vq_id]);
  65. }
  66. EXPORT_SYMBOL(rproc_vq_interrupt);
  67. static struct virtqueue *rp_find_vq(struct virtio_device *vdev,
  68. unsigned id,
  69. void (*callback)(struct virtqueue *vq),
  70. const char *name)
  71. {
  72. struct rproc *rproc = vdev_to_rproc(vdev);
  73. struct rproc_vdev *rvdev = rproc->rvdev;
  74. struct rproc_virtio_vq_info *rpvq;
  75. struct virtqueue *vq;
  76. void *addr;
  77. int ret, len;
  78. rpvq = kmalloc(sizeof(*rpvq), GFP_KERNEL);
  79. if (!rpvq)
  80. return ERR_PTR(-ENOMEM);
  81. rpvq->rproc = rproc;
  82. rpvq->vq_id = id;
  83. addr = rvdev->vring[id].va;
  84. len = rvdev->vring[id].len;
  85. dev_dbg(rproc->dev, "vring%d: va %p qsz %d\n", id, addr, len);
  86. /*
  87. * Create the new vq, and tell virtio we're not interested in
  88. * the 'weak' smp barriers, since we're talking with a real device.
  89. */
  90. vq = vring_new_virtqueue(len, AMP_VRING_ALIGN, vdev, false, addr,
  91. rproc_virtio_notify, callback, name);
  92. if (!vq) {
  93. dev_err(rproc->dev, "vring_new_virtqueue %s failed\n", name);
  94. ret = -ENOMEM;
  95. goto free_rpvq;
  96. }
  97. rvdev->vq[id] = vq;
  98. vq->priv = rpvq;
  99. return vq;
  100. free_rpvq:
  101. kfree(rpvq);
  102. return ERR_PTR(ret);
  103. }
  104. static void rproc_virtio_del_vqs(struct virtio_device *vdev)
  105. {
  106. struct virtqueue *vq, *n;
  107. struct rproc *rproc = vdev_to_rproc(vdev);
  108. list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
  109. struct rproc_virtio_vq_info *rpvq = vq->priv;
  110. vring_del_virtqueue(vq);
  111. kfree(rpvq);
  112. }
  113. /* power down the remote processor */
  114. rproc_shutdown(rproc);
  115. }
  116. static int rproc_virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  117. struct virtqueue *vqs[],
  118. vq_callback_t *callbacks[],
  119. const char *names[])
  120. {
  121. struct rproc *rproc = vdev_to_rproc(vdev);
  122. int i, ret;
  123. /* we maintain two virtqueues per remote processor (for RX and TX) */
  124. if (nvqs != 2)
  125. return -EINVAL;
  126. /* boot the remote processor */
  127. ret = rproc_boot(rproc);
  128. if (ret) {
  129. dev_err(rproc->dev, "rproc_boot() failed %d\n", ret);
  130. goto error;
  131. }
  132. for (i = 0; i < nvqs; ++i) {
  133. vqs[i] = rp_find_vq(vdev, i, callbacks[i], names[i]);
  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. /*
  145. * We don't support yet real virtio status semantics.
  146. *
  147. * The plan is to provide this via the VIRTIO HDR resource entry
  148. * which is part of the firmware: this way the remote processor
  149. * will be able to access the status values as set by us.
  150. */
  151. static u8 rproc_virtio_get_status(struct virtio_device *vdev)
  152. {
  153. return 0;
  154. }
  155. static void rproc_virtio_set_status(struct virtio_device *vdev, u8 status)
  156. {
  157. dev_dbg(&vdev->dev, "new status: %d\n", status);
  158. }
  159. static void rproc_virtio_reset(struct virtio_device *vdev)
  160. {
  161. dev_dbg(&vdev->dev, "reset !\n");
  162. }
  163. /* provide the vdev features as retrieved from the firmware */
  164. static u32 rproc_virtio_get_features(struct virtio_device *vdev)
  165. {
  166. struct rproc *rproc = vdev_to_rproc(vdev);
  167. /* we only support a single vdev device for now */
  168. return rproc->rvdev->dfeatures;
  169. }
  170. static void rproc_virtio_finalize_features(struct virtio_device *vdev)
  171. {
  172. struct rproc *rproc = vdev_to_rproc(vdev);
  173. /* Give virtio_ring a chance to accept features */
  174. vring_transport_features(vdev);
  175. /*
  176. * Remember the finalized features of our vdev, and provide it
  177. * to the remote processor once it is powered on.
  178. *
  179. * Similarly to the status field, we don't expose yet the negotiated
  180. * features to the remote processors at this point. This will be
  181. * fixed as part of a small resource table overhaul and then an
  182. * extension of the virtio resource entries.
  183. */
  184. rproc->rvdev->gfeatures = vdev->features[0];
  185. }
  186. static struct virtio_config_ops rproc_virtio_config_ops = {
  187. .get_features = rproc_virtio_get_features,
  188. .finalize_features = rproc_virtio_finalize_features,
  189. .find_vqs = rproc_virtio_find_vqs,
  190. .del_vqs = rproc_virtio_del_vqs,
  191. .reset = rproc_virtio_reset,
  192. .set_status = rproc_virtio_set_status,
  193. .get_status = rproc_virtio_get_status,
  194. };
  195. /*
  196. * This function is called whenever vdev is released, and is responsible
  197. * to decrement the remote processor's refcount taken when vdev was
  198. * added.
  199. *
  200. * Never call this function directly; it will be called by the driver
  201. * core when needed.
  202. */
  203. static void rproc_vdev_release(struct device *dev)
  204. {
  205. struct virtio_device *vdev = dev_to_virtio(dev);
  206. struct rproc *rproc = vdev_to_rproc(vdev);
  207. kref_put(&rproc->refcount, rproc_release);
  208. }
  209. /**
  210. * rproc_add_rpmsg_vdev() - create an rpmsg virtio device
  211. * @rproc: the rproc handle
  212. *
  213. * This function is called if virtio rpmsg support was found in the
  214. * firmware of the remote processor.
  215. *
  216. * Today we only support creating a single rpmsg vdev (virtio device),
  217. * but the plan is to remove this limitation. At that point this interface
  218. * will be revised/extended.
  219. */
  220. int rproc_add_rpmsg_vdev(struct rproc *rproc)
  221. {
  222. struct device *dev = rproc->dev;
  223. struct rproc_vdev *rvdev = rproc->rvdev;
  224. int ret;
  225. rvdev->vdev.id.device = VIRTIO_ID_RPMSG,
  226. rvdev->vdev.config = &rproc_virtio_config_ops,
  227. rvdev->vdev.dev.parent = dev;
  228. rvdev->vdev.dev.release = rproc_vdev_release;
  229. /*
  230. * We're indirectly making a non-temporary copy of the rproc pointer
  231. * here, because drivers probed with this vdev will indirectly
  232. * access the wrapping rproc.
  233. *
  234. * Therefore we must increment the rproc refcount here, and decrement
  235. * it _only_ when the vdev is released.
  236. */
  237. kref_get(&rproc->refcount);
  238. ret = register_virtio_device(&rvdev->vdev);
  239. if (ret) {
  240. kref_put(&rproc->refcount, rproc_release);
  241. dev_err(dev, "failed to register vdev: %d\n", ret);
  242. }
  243. return ret;
  244. }
  245. /**
  246. * rproc_remove_rpmsg_vdev() - remove an rpmsg vdev device
  247. * @rproc: the rproc handle
  248. *
  249. * This function is called whenever @rproc is removed _iff_ an rpmsg
  250. * vdev was created beforehand.
  251. */
  252. void rproc_remove_rpmsg_vdev(struct rproc *rproc)
  253. {
  254. struct rproc_vdev *rvdev = rproc->rvdev;
  255. unregister_virtio_device(&rvdev->vdev);
  256. }