remoteproc_virtio.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 bool 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. return true;
  38. }
  39. /**
  40. * rproc_vq_interrupt() - tell remoteproc that a virtqueue is interrupted
  41. * @rproc: handle to the remote processor
  42. * @notifyid: index of the signalled virtqueue (unique per this @rproc)
  43. *
  44. * This function should be called by the platform-specific rproc driver,
  45. * when the remote processor signals that a specific virtqueue has pending
  46. * messages available.
  47. *
  48. * Returns IRQ_NONE if no message was found in the @notifyid virtqueue,
  49. * and otherwise returns IRQ_HANDLED.
  50. */
  51. irqreturn_t rproc_vq_interrupt(struct rproc *rproc, int notifyid)
  52. {
  53. struct rproc_vring *rvring;
  54. dev_dbg(&rproc->dev, "vq index %d is interrupted\n", notifyid);
  55. rvring = idr_find(&rproc->notifyids, notifyid);
  56. if (!rvring || !rvring->vq)
  57. return IRQ_NONE;
  58. return vring_interrupt(0, rvring->vq);
  59. }
  60. EXPORT_SYMBOL(rproc_vq_interrupt);
  61. static struct virtqueue *rp_find_vq(struct virtio_device *vdev,
  62. unsigned int id,
  63. void (*callback)(struct virtqueue *vq),
  64. const char *name, bool ctx)
  65. {
  66. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  67. struct rproc *rproc = vdev_to_rproc(vdev);
  68. struct device *dev = &rproc->dev;
  69. struct rproc_vring *rvring;
  70. struct virtqueue *vq;
  71. void *addr;
  72. int len, size;
  73. /* we're temporarily limited to two virtqueues per rvdev */
  74. if (id >= ARRAY_SIZE(rvdev->vring))
  75. return ERR_PTR(-EINVAL);
  76. if (!name)
  77. return NULL;
  78. rvring = &rvdev->vring[id];
  79. addr = rvring->va;
  80. len = rvring->len;
  81. /* zero vring */
  82. size = vring_size(len, rvring->align);
  83. memset(addr, 0, size);
  84. dev_dbg(dev, "vring%d: va %pK qsz %d notifyid %d\n",
  85. id, addr, len, rvring->notifyid);
  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(id, len, rvring->align, vdev, false, ctx,
  91. addr, rproc_virtio_notify, callback, name);
  92. if (!vq) {
  93. dev_err(dev, "vring_new_virtqueue %s failed\n", name);
  94. rproc_free_vring(rvring);
  95. return ERR_PTR(-ENOMEM);
  96. }
  97. rvring->vq = vq;
  98. vq->priv = rvring;
  99. return vq;
  100. }
  101. static void __rproc_virtio_del_vqs(struct virtio_device *vdev)
  102. {
  103. struct virtqueue *vq, *n;
  104. struct rproc_vring *rvring;
  105. list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
  106. rvring = vq->priv;
  107. rvring->vq = NULL;
  108. vring_del_virtqueue(vq);
  109. }
  110. }
  111. static void rproc_virtio_del_vqs(struct virtio_device *vdev)
  112. {
  113. __rproc_virtio_del_vqs(vdev);
  114. }
  115. static int rproc_virtio_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
  116. struct virtqueue *vqs[],
  117. vq_callback_t *callbacks[],
  118. const char * const names[],
  119. const bool * ctx,
  120. struct irq_affinity *desc)
  121. {
  122. int i, ret;
  123. for (i = 0; i < nvqs; ++i) {
  124. vqs[i] = rp_find_vq(vdev, i, callbacks[i], names[i],
  125. ctx ? ctx[i] : false);
  126. if (IS_ERR(vqs[i])) {
  127. ret = PTR_ERR(vqs[i]);
  128. goto error;
  129. }
  130. }
  131. return 0;
  132. error:
  133. __rproc_virtio_del_vqs(vdev);
  134. return ret;
  135. }
  136. static u8 rproc_virtio_get_status(struct virtio_device *vdev)
  137. {
  138. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  139. struct fw_rsc_vdev *rsc;
  140. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  141. return rsc->status;
  142. }
  143. static void rproc_virtio_set_status(struct virtio_device *vdev, u8 status)
  144. {
  145. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  146. struct fw_rsc_vdev *rsc;
  147. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  148. rsc->status = status;
  149. dev_dbg(&vdev->dev, "status: %d\n", status);
  150. }
  151. static void rproc_virtio_reset(struct virtio_device *vdev)
  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 = 0;
  157. dev_dbg(&vdev->dev, "reset !\n");
  158. }
  159. /* provide the vdev features as retrieved from the firmware */
  160. static u64 rproc_virtio_get_features(struct virtio_device *vdev)
  161. {
  162. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  163. struct fw_rsc_vdev *rsc;
  164. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  165. return rsc->dfeatures;
  166. }
  167. static int rproc_virtio_finalize_features(struct virtio_device *vdev)
  168. {
  169. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  170. struct fw_rsc_vdev *rsc;
  171. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  172. /* Give virtio_ring a chance to accept features */
  173. vring_transport_features(vdev);
  174. /* Make sure we don't have any features > 32 bits! */
  175. BUG_ON((u32)vdev->features != vdev->features);
  176. /*
  177. * Remember the finalized features of our vdev, and provide it
  178. * to the remote processor once it is powered on.
  179. */
  180. rsc->gfeatures = vdev->features;
  181. return 0;
  182. }
  183. static void rproc_virtio_get(struct virtio_device *vdev, unsigned int offset,
  184. void *buf, unsigned int len)
  185. {
  186. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  187. struct fw_rsc_vdev *rsc;
  188. void *cfg;
  189. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  190. cfg = &rsc->vring[rsc->num_of_vrings];
  191. if (offset + len > rsc->config_len || offset + len < len) {
  192. dev_err(&vdev->dev, "rproc_virtio_get: access out of bounds\n");
  193. return;
  194. }
  195. memcpy(buf, cfg + offset, len);
  196. }
  197. static void rproc_virtio_set(struct virtio_device *vdev, unsigned int offset,
  198. const void *buf, unsigned int len)
  199. {
  200. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  201. struct fw_rsc_vdev *rsc;
  202. void *cfg;
  203. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  204. cfg = &rsc->vring[rsc->num_of_vrings];
  205. if (offset + len > rsc->config_len || offset + len < len) {
  206. dev_err(&vdev->dev, "rproc_virtio_set: access out of bounds\n");
  207. return;
  208. }
  209. memcpy(cfg + offset, buf, len);
  210. }
  211. static const struct virtio_config_ops rproc_virtio_config_ops = {
  212. .get_features = rproc_virtio_get_features,
  213. .finalize_features = rproc_virtio_finalize_features,
  214. .find_vqs = rproc_virtio_find_vqs,
  215. .del_vqs = rproc_virtio_del_vqs,
  216. .reset = rproc_virtio_reset,
  217. .set_status = rproc_virtio_set_status,
  218. .get_status = rproc_virtio_get_status,
  219. .get = rproc_virtio_get,
  220. .set = rproc_virtio_set,
  221. };
  222. /*
  223. * This function is called whenever vdev is released, and is responsible
  224. * to decrement the remote processor's refcount which was taken when vdev was
  225. * added.
  226. *
  227. * Never call this function directly; it will be called by the driver
  228. * core when needed.
  229. */
  230. static void rproc_virtio_dev_release(struct device *dev)
  231. {
  232. struct virtio_device *vdev = dev_to_virtio(dev);
  233. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  234. struct rproc *rproc = vdev_to_rproc(vdev);
  235. kref_put(&rvdev->refcount, rproc_vdev_release);
  236. put_device(&rproc->dev);
  237. }
  238. /**
  239. * rproc_add_virtio_dev() - register an rproc-induced virtio device
  240. * @rvdev: the remote vdev
  241. *
  242. * This function registers a virtio device. This vdev's partent is
  243. * the rproc device.
  244. *
  245. * Returns 0 on success or an appropriate error value otherwise.
  246. */
  247. int rproc_add_virtio_dev(struct rproc_vdev *rvdev, int id)
  248. {
  249. struct rproc *rproc = rvdev->rproc;
  250. struct device *dev = &rproc->dev;
  251. struct virtio_device *vdev = &rvdev->vdev;
  252. int ret;
  253. vdev->id.device = id,
  254. vdev->config = &rproc_virtio_config_ops,
  255. vdev->dev.parent = dev;
  256. vdev->dev.release = rproc_virtio_dev_release;
  257. /*
  258. * We're indirectly making a non-temporary copy of the rproc pointer
  259. * here, because drivers probed with this vdev will indirectly
  260. * access the wrapping rproc.
  261. *
  262. * Therefore we must increment the rproc refcount here, and decrement
  263. * it _only_ when the vdev is released.
  264. */
  265. get_device(&rproc->dev);
  266. /* Reference the vdev and vring allocations */
  267. kref_get(&rvdev->refcount);
  268. ret = register_virtio_device(vdev);
  269. if (ret) {
  270. put_device(&vdev->dev);
  271. dev_err(dev, "failed to register vdev: %d\n", ret);
  272. goto out;
  273. }
  274. dev_info(dev, "registered %s (type %d)\n", dev_name(&vdev->dev), id);
  275. out:
  276. return ret;
  277. }
  278. /**
  279. * rproc_remove_virtio_dev() - remove an rproc-induced virtio device
  280. * @rvdev: the remote vdev
  281. *
  282. * This function unregisters an existing virtio device.
  283. */
  284. void rproc_remove_virtio_dev(struct rproc_vdev *rvdev)
  285. {
  286. unregister_virtio_device(&rvdev->vdev);
  287. }