remoteproc_virtio.c 9.6 KB

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