remoteproc_virtio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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-direct.h>
  12. #include <linux/dma-map-ops.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/export.h>
  15. #include <linux/of_reserved_mem.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/remoteproc.h>
  18. #include <linux/virtio.h>
  19. #include <linux/virtio_config.h>
  20. #include <linux/virtio_ids.h>
  21. #include <linux/virtio_ring.h>
  22. #include <linux/err.h>
  23. #include <linux/kref.h>
  24. #include <linux/slab.h>
  25. #include "remoteproc_internal.h"
  26. static int copy_dma_range_map(struct device *to, struct device *from)
  27. {
  28. const struct bus_dma_region *map = from->dma_range_map, *new_map, *r;
  29. int num_ranges = 0;
  30. if (!map)
  31. return 0;
  32. for (r = map; r->size; r++)
  33. num_ranges++;
  34. new_map = kmemdup(map, array_size(num_ranges + 1, sizeof(*map)),
  35. GFP_KERNEL);
  36. if (!new_map)
  37. return -ENOMEM;
  38. to->dma_range_map = new_map;
  39. return 0;
  40. }
  41. static struct rproc_vdev *vdev_to_rvdev(struct virtio_device *vdev)
  42. {
  43. struct platform_device *pdev;
  44. pdev = container_of(vdev->dev.parent, struct platform_device, dev);
  45. return platform_get_drvdata(pdev);
  46. }
  47. static struct rproc *vdev_to_rproc(struct virtio_device *vdev)
  48. {
  49. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  50. return rvdev->rproc;
  51. }
  52. /* kick the remote processor, and let it know which virtqueue to poke at */
  53. static bool rproc_virtio_notify(struct virtqueue *vq)
  54. {
  55. struct rproc_vring *rvring = vq->priv;
  56. struct rproc *rproc = rvring->rvdev->rproc;
  57. int notifyid = rvring->notifyid;
  58. dev_dbg(&rproc->dev, "kicking vq index: %d\n", notifyid);
  59. rproc->ops->kick(rproc, notifyid);
  60. return true;
  61. }
  62. /**
  63. * rproc_vq_interrupt() - tell remoteproc that a virtqueue is interrupted
  64. * @rproc: handle to the remote processor
  65. * @notifyid: index of the signalled virtqueue (unique per this @rproc)
  66. *
  67. * This function should be called by the platform-specific rproc driver,
  68. * when the remote processor signals that a specific virtqueue has pending
  69. * messages available.
  70. *
  71. * Return: IRQ_NONE if no message was found in the @notifyid virtqueue,
  72. * and otherwise returns IRQ_HANDLED.
  73. */
  74. irqreturn_t rproc_vq_interrupt(struct rproc *rproc, int notifyid)
  75. {
  76. struct rproc_vring *rvring;
  77. dev_dbg(&rproc->dev, "vq index %d is interrupted\n", notifyid);
  78. rvring = idr_find(&rproc->notifyids, notifyid);
  79. if (!rvring || !rvring->vq)
  80. return IRQ_NONE;
  81. return vring_interrupt(0, rvring->vq);
  82. }
  83. EXPORT_SYMBOL(rproc_vq_interrupt);
  84. static struct virtqueue *rp_find_vq(struct virtio_device *vdev,
  85. unsigned int id,
  86. void (*callback)(struct virtqueue *vq),
  87. const char *name, bool ctx)
  88. {
  89. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  90. struct rproc *rproc = vdev_to_rproc(vdev);
  91. struct device *dev = &rproc->dev;
  92. struct rproc_mem_entry *mem;
  93. struct rproc_vring *rvring;
  94. struct fw_rsc_vdev *rsc;
  95. struct virtqueue *vq;
  96. void *addr;
  97. int num, size;
  98. /* we're temporarily limited to two virtqueues per rvdev */
  99. if (id >= ARRAY_SIZE(rvdev->vring))
  100. return ERR_PTR(-EINVAL);
  101. if (!name)
  102. return NULL;
  103. /* Search allocated memory region by name */
  104. mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index,
  105. id);
  106. if (!mem || !mem->va)
  107. return ERR_PTR(-ENOMEM);
  108. rvring = &rvdev->vring[id];
  109. addr = mem->va;
  110. num = rvring->num;
  111. /* zero vring */
  112. size = vring_size(num, rvring->align);
  113. memset(addr, 0, size);
  114. dev_dbg(dev, "vring%d: va %p qsz %d notifyid %d\n",
  115. id, addr, num, rvring->notifyid);
  116. /*
  117. * Create the new vq, and tell virtio we're not interested in
  118. * the 'weak' smp barriers, since we're talking with a real device.
  119. */
  120. vq = vring_new_virtqueue(id, num, rvring->align, vdev, false, ctx,
  121. addr, rproc_virtio_notify, callback, name);
  122. if (!vq) {
  123. dev_err(dev, "vring_new_virtqueue %s failed\n", name);
  124. rproc_free_vring(rvring);
  125. return ERR_PTR(-ENOMEM);
  126. }
  127. vq->num_max = num;
  128. rvring->vq = vq;
  129. vq->priv = rvring;
  130. /* Update vring in resource table */
  131. rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
  132. rsc->vring[id].da = mem->da;
  133. return vq;
  134. }
  135. static void __rproc_virtio_del_vqs(struct virtio_device *vdev)
  136. {
  137. struct virtqueue *vq, *n;
  138. struct rproc_vring *rvring;
  139. list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
  140. rvring = vq->priv;
  141. rvring->vq = NULL;
  142. vring_del_virtqueue(vq);
  143. }
  144. }
  145. static void rproc_virtio_del_vqs(struct virtio_device *vdev)
  146. {
  147. __rproc_virtio_del_vqs(vdev);
  148. }
  149. static int rproc_virtio_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
  150. struct virtqueue *vqs[],
  151. struct virtqueue_info vqs_info[],
  152. struct irq_affinity *desc)
  153. {
  154. int i, ret, queue_idx = 0;
  155. for (i = 0; i < nvqs; ++i) {
  156. struct virtqueue_info *vqi = &vqs_info[i];
  157. if (!vqi->name) {
  158. vqs[i] = NULL;
  159. continue;
  160. }
  161. vqs[i] = rp_find_vq(vdev, queue_idx++, vqi->callback,
  162. vqi->name, vqi->ctx);
  163. if (IS_ERR(vqs[i])) {
  164. ret = PTR_ERR(vqs[i]);
  165. goto error;
  166. }
  167. }
  168. return 0;
  169. error:
  170. __rproc_virtio_del_vqs(vdev);
  171. return ret;
  172. }
  173. static u8 rproc_virtio_get_status(struct virtio_device *vdev)
  174. {
  175. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  176. struct fw_rsc_vdev *rsc;
  177. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  178. return rsc->status;
  179. }
  180. static void rproc_virtio_set_status(struct virtio_device *vdev, u8 status)
  181. {
  182. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  183. struct fw_rsc_vdev *rsc;
  184. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  185. rsc->status = status;
  186. dev_dbg(&vdev->dev, "status: %d\n", status);
  187. }
  188. static void rproc_virtio_reset(struct virtio_device *vdev)
  189. {
  190. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  191. struct fw_rsc_vdev *rsc;
  192. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  193. rsc->status = 0;
  194. dev_dbg(&vdev->dev, "reset !\n");
  195. }
  196. /* provide the vdev features as retrieved from the firmware */
  197. static u64 rproc_virtio_get_features(struct virtio_device *vdev)
  198. {
  199. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  200. struct fw_rsc_vdev *rsc;
  201. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  202. return rsc->dfeatures;
  203. }
  204. static void rproc_transport_features(struct virtio_device *vdev)
  205. {
  206. /*
  207. * Packed ring isn't enabled on remoteproc for now,
  208. * because remoteproc uses vring_new_virtqueue() which
  209. * creates virtio rings on preallocated memory.
  210. */
  211. __virtio_clear_bit(vdev, VIRTIO_F_RING_PACKED);
  212. }
  213. static int rproc_virtio_finalize_features(struct virtio_device *vdev)
  214. {
  215. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  216. struct fw_rsc_vdev *rsc;
  217. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  218. /* Give virtio_ring a chance to accept features */
  219. vring_transport_features(vdev);
  220. /* Give virtio_rproc a chance to accept features. */
  221. rproc_transport_features(vdev);
  222. /* Make sure we don't have any features > 32 bits! */
  223. BUG_ON((u32)vdev->features != vdev->features);
  224. /*
  225. * Remember the finalized features of our vdev, and provide it
  226. * to the remote processor once it is powered on.
  227. */
  228. rsc->gfeatures = vdev->features;
  229. return 0;
  230. }
  231. static void rproc_virtio_get(struct virtio_device *vdev, unsigned int offset,
  232. void *buf, unsigned int len)
  233. {
  234. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  235. struct fw_rsc_vdev *rsc;
  236. void *cfg;
  237. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  238. cfg = &rsc->vring[rsc->num_of_vrings];
  239. if (offset + len > rsc->config_len || offset + len < len) {
  240. dev_err(&vdev->dev, "rproc_virtio_get: access out of bounds\n");
  241. return;
  242. }
  243. memcpy(buf, cfg + offset, len);
  244. }
  245. static void rproc_virtio_set(struct virtio_device *vdev, unsigned int offset,
  246. const void *buf, unsigned int len)
  247. {
  248. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  249. struct fw_rsc_vdev *rsc;
  250. void *cfg;
  251. rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
  252. cfg = &rsc->vring[rsc->num_of_vrings];
  253. if (offset + len > rsc->config_len || offset + len < len) {
  254. dev_err(&vdev->dev, "rproc_virtio_set: access out of bounds\n");
  255. return;
  256. }
  257. memcpy(cfg + offset, buf, len);
  258. }
  259. static const struct virtio_config_ops rproc_virtio_config_ops = {
  260. .get_features = rproc_virtio_get_features,
  261. .finalize_features = rproc_virtio_finalize_features,
  262. .find_vqs = rproc_virtio_find_vqs,
  263. .del_vqs = rproc_virtio_del_vqs,
  264. .reset = rproc_virtio_reset,
  265. .set_status = rproc_virtio_set_status,
  266. .get_status = rproc_virtio_get_status,
  267. .get = rproc_virtio_get,
  268. .set = rproc_virtio_set,
  269. };
  270. /*
  271. * This function is called whenever vdev is released, and is responsible
  272. * to decrement the remote processor's refcount which was taken when vdev was
  273. * added.
  274. *
  275. * Never call this function directly; it will be called by the driver
  276. * core when needed.
  277. */
  278. static void rproc_virtio_dev_release(struct device *dev)
  279. {
  280. struct virtio_device *vdev = dev_to_virtio(dev);
  281. struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
  282. kfree(vdev);
  283. of_reserved_mem_device_release(&rvdev->pdev->dev);
  284. dma_release_coherent_memory(&rvdev->pdev->dev);
  285. put_device(&rvdev->pdev->dev);
  286. }
  287. /**
  288. * rproc_add_virtio_dev() - register an rproc-induced virtio device
  289. * @rvdev: the remote vdev
  290. * @id: the device type identification (used to match it with a driver).
  291. *
  292. * This function registers a virtio device. This vdev's partent is
  293. * the rproc device.
  294. *
  295. * Return: 0 on success or an appropriate error value otherwise
  296. */
  297. static int rproc_add_virtio_dev(struct rproc_vdev *rvdev, int id)
  298. {
  299. struct rproc *rproc = rvdev->rproc;
  300. struct device *dev = &rvdev->pdev->dev;
  301. struct virtio_device *vdev;
  302. struct rproc_mem_entry *mem;
  303. int ret;
  304. if (rproc->ops->kick == NULL) {
  305. ret = -EINVAL;
  306. dev_err(dev, ".kick method not defined for %s\n", rproc->name);
  307. goto out;
  308. }
  309. /* Try to find dedicated vdev buffer carveout */
  310. mem = rproc_find_carveout_by_name(rproc, "vdev%dbuffer", rvdev->index);
  311. if (mem) {
  312. phys_addr_t pa;
  313. if (mem->of_resm_idx != -1) {
  314. struct device_node *np = rproc->dev.parent->of_node;
  315. /* Associate reserved memory to vdev device */
  316. ret = of_reserved_mem_device_init_by_idx(dev, np,
  317. mem->of_resm_idx);
  318. if (ret) {
  319. dev_err(dev, "Can't associate reserved memory\n");
  320. goto out;
  321. }
  322. } else {
  323. if (mem->va) {
  324. dev_warn(dev, "vdev %d buffer already mapped\n",
  325. rvdev->index);
  326. pa = rproc_va_to_pa(mem->va);
  327. } else {
  328. /* Use dma address as carveout no memmapped yet */
  329. pa = (phys_addr_t)mem->dma;
  330. }
  331. /* Associate vdev buffer memory pool to vdev subdev */
  332. ret = dma_declare_coherent_memory(dev, pa,
  333. mem->da,
  334. mem->len);
  335. if (ret < 0) {
  336. dev_err(dev, "Failed to associate buffer\n");
  337. goto out;
  338. }
  339. }
  340. } else {
  341. struct device_node *np = rproc->dev.parent->of_node;
  342. /*
  343. * If we don't have dedicated buffer, just attempt to re-assign
  344. * the reserved memory from our parent. A default memory-region
  345. * at index 0 from the parent's memory-regions is assigned for
  346. * the rvdev dev to allocate from. Failure is non-critical and
  347. * the allocations will fall back to global pools, so don't
  348. * check return value either.
  349. */
  350. of_reserved_mem_device_init_by_idx(dev, np, 0);
  351. }
  352. /* Allocate virtio device */
  353. vdev = kzalloc_obj(*vdev);
  354. if (!vdev) {
  355. ret = -ENOMEM;
  356. goto out;
  357. }
  358. vdev->id.device = id,
  359. vdev->config = &rproc_virtio_config_ops,
  360. vdev->dev.parent = dev;
  361. vdev->dev.release = rproc_virtio_dev_release;
  362. /* Reference the vdev and vring allocations */
  363. get_device(dev);
  364. ret = register_virtio_device(vdev);
  365. if (ret) {
  366. put_device(&vdev->dev);
  367. dev_err(dev, "failed to register vdev: %d\n", ret);
  368. goto out;
  369. }
  370. dev_info(dev, "registered %s (type %d)\n", dev_name(&vdev->dev), id);
  371. out:
  372. return ret;
  373. }
  374. /**
  375. * rproc_remove_virtio_dev() - remove an rproc-induced virtio device
  376. * @dev: the virtio device
  377. * @data: must be null
  378. *
  379. * This function unregisters an existing virtio device.
  380. *
  381. * Return: 0
  382. */
  383. static int rproc_remove_virtio_dev(struct device *dev, void *data)
  384. {
  385. struct virtio_device *vdev = dev_to_virtio(dev);
  386. unregister_virtio_device(vdev);
  387. return 0;
  388. }
  389. static int rproc_vdev_do_start(struct rproc_subdev *subdev)
  390. {
  391. struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
  392. return rproc_add_virtio_dev(rvdev, rvdev->id);
  393. }
  394. static void rproc_vdev_do_stop(struct rproc_subdev *subdev, bool crashed)
  395. {
  396. struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
  397. struct device *dev = &rvdev->pdev->dev;
  398. int ret;
  399. ret = device_for_each_child(dev, NULL, rproc_remove_virtio_dev);
  400. if (ret)
  401. dev_warn(dev, "can't remove vdev child device: %d\n", ret);
  402. }
  403. static int rproc_virtio_probe(struct platform_device *pdev)
  404. {
  405. struct device *dev = &pdev->dev;
  406. struct rproc_vdev_data *rvdev_data = dev->platform_data;
  407. struct rproc_vdev *rvdev;
  408. struct rproc *rproc = container_of(dev->parent, struct rproc, dev);
  409. struct fw_rsc_vdev *rsc;
  410. int i, ret;
  411. if (!rvdev_data)
  412. return -EINVAL;
  413. rvdev = devm_kzalloc(dev, sizeof(*rvdev), GFP_KERNEL);
  414. if (!rvdev)
  415. return -ENOMEM;
  416. rvdev->id = rvdev_data->id;
  417. rvdev->rproc = rproc;
  418. rvdev->index = rvdev_data->index;
  419. ret = copy_dma_range_map(dev, rproc->dev.parent);
  420. if (ret)
  421. return ret;
  422. /* Make device dma capable by inheriting from parent's capabilities */
  423. set_dma_ops(dev, get_dma_ops(rproc->dev.parent));
  424. ret = dma_coerce_mask_and_coherent(dev, dma_get_mask(rproc->dev.parent));
  425. if (ret) {
  426. dev_warn(dev, "Failed to set DMA mask %llx. Trying to continue... (%pe)\n",
  427. dma_get_mask(rproc->dev.parent), ERR_PTR(ret));
  428. }
  429. platform_set_drvdata(pdev, rvdev);
  430. rvdev->pdev = pdev;
  431. rsc = rvdev_data->rsc;
  432. /* parse the vrings */
  433. for (i = 0; i < rsc->num_of_vrings; i++) {
  434. ret = rproc_parse_vring(rvdev, rsc, i);
  435. if (ret)
  436. return ret;
  437. }
  438. /* remember the resource offset*/
  439. rvdev->rsc_offset = rvdev_data->rsc_offset;
  440. /* allocate the vring resources */
  441. for (i = 0; i < rsc->num_of_vrings; i++) {
  442. ret = rproc_alloc_vring(rvdev, i);
  443. if (ret)
  444. goto unwind_vring_allocations;
  445. }
  446. rproc_add_rvdev(rproc, rvdev);
  447. rvdev->subdev.start = rproc_vdev_do_start;
  448. rvdev->subdev.stop = rproc_vdev_do_stop;
  449. rproc_add_subdev(rproc, &rvdev->subdev);
  450. /*
  451. * We're indirectly making a non-temporary copy of the rproc pointer
  452. * here, because the platform device or the vdev device will indirectly
  453. * access the wrapping rproc.
  454. *
  455. * Therefore we must increment the rproc refcount here, and decrement
  456. * it _only_ on platform remove.
  457. */
  458. get_device(&rproc->dev);
  459. return 0;
  460. unwind_vring_allocations:
  461. for (i--; i >= 0; i--)
  462. rproc_free_vring(&rvdev->vring[i]);
  463. return ret;
  464. }
  465. static void rproc_virtio_remove(struct platform_device *pdev)
  466. {
  467. struct rproc_vdev *rvdev = dev_get_drvdata(&pdev->dev);
  468. struct rproc *rproc = rvdev->rproc;
  469. struct rproc_vring *rvring;
  470. int id;
  471. for (id = 0; id < ARRAY_SIZE(rvdev->vring); id++) {
  472. rvring = &rvdev->vring[id];
  473. rproc_free_vring(rvring);
  474. }
  475. rproc_remove_subdev(rproc, &rvdev->subdev);
  476. rproc_remove_rvdev(rvdev);
  477. put_device(&rproc->dev);
  478. }
  479. /* Platform driver */
  480. static struct platform_driver rproc_virtio_driver = {
  481. .probe = rproc_virtio_probe,
  482. .remove = rproc_virtio_remove,
  483. .driver = {
  484. .name = "rproc-virtio",
  485. },
  486. };
  487. builtin_platform_driver(rproc_virtio_driver);