virtio_pcidev.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Intel Corporation
  4. * Author: Johannes Berg <johannes@sipsolutions.net>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/pci.h>
  8. #include <linux/virtio.h>
  9. #include <linux/virtio_config.h>
  10. #include <linux/logic_iomem.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/irqdomain.h>
  13. #include <linux/virtio_pcidev.h>
  14. #include <linux/virtio-uml.h>
  15. #include <linux/delay.h>
  16. #include <linux/msi.h>
  17. #include <linux/unaligned.h>
  18. #include <irq_kern.h>
  19. #include "virt-pci.h"
  20. #define to_virtio_pcidev(_pdev) \
  21. container_of(_pdev, struct virtio_pcidev_device, pdev)
  22. /* for MSI-X we have a 32-bit payload */
  23. #define MAX_IRQ_MSG_SIZE (sizeof(struct virtio_pcidev_msg) + sizeof(u32))
  24. #define NUM_IRQ_MSGS 10
  25. struct virtio_pcidev_message_buffer {
  26. struct virtio_pcidev_msg hdr;
  27. u8 data[8];
  28. };
  29. struct virtio_pcidev_device {
  30. struct um_pci_device pdev;
  31. struct virtio_device *vdev;
  32. struct virtqueue *cmd_vq, *irq_vq;
  33. #define VIRTIO_PCIDEV_WRITE_BUFS 20
  34. struct virtio_pcidev_message_buffer bufs[VIRTIO_PCIDEV_WRITE_BUFS + 1];
  35. void *extra_ptrs[VIRTIO_PCIDEV_WRITE_BUFS + 1];
  36. DECLARE_BITMAP(used_bufs, VIRTIO_PCIDEV_WRITE_BUFS);
  37. #define VIRTIO_PCIDEV_STAT_WAITING 0
  38. unsigned long status;
  39. bool platform;
  40. };
  41. static unsigned int virtio_pcidev_max_delay_us = 40000;
  42. module_param_named(max_delay_us, virtio_pcidev_max_delay_us, uint, 0644);
  43. static int virtio_pcidev_get_buf(struct virtio_pcidev_device *dev, bool *posted)
  44. {
  45. int i;
  46. for (i = 0; i < VIRTIO_PCIDEV_WRITE_BUFS; i++) {
  47. if (!test_and_set_bit(i, dev->used_bufs))
  48. return i;
  49. }
  50. *posted = false;
  51. return VIRTIO_PCIDEV_WRITE_BUFS;
  52. }
  53. static void virtio_pcidev_free_buf(struct virtio_pcidev_device *dev, void *buf)
  54. {
  55. int i;
  56. if (buf == &dev->bufs[VIRTIO_PCIDEV_WRITE_BUFS]) {
  57. kfree(dev->extra_ptrs[VIRTIO_PCIDEV_WRITE_BUFS]);
  58. dev->extra_ptrs[VIRTIO_PCIDEV_WRITE_BUFS] = NULL;
  59. return;
  60. }
  61. for (i = 0; i < VIRTIO_PCIDEV_WRITE_BUFS; i++) {
  62. if (buf == &dev->bufs[i]) {
  63. kfree(dev->extra_ptrs[i]);
  64. dev->extra_ptrs[i] = NULL;
  65. WARN_ON(!test_and_clear_bit(i, dev->used_bufs));
  66. return;
  67. }
  68. }
  69. WARN_ON(1);
  70. }
  71. static int virtio_pcidev_send_cmd(struct virtio_pcidev_device *dev,
  72. struct virtio_pcidev_msg *cmd,
  73. unsigned int cmd_size,
  74. const void *extra, unsigned int extra_size,
  75. void *out, unsigned int out_size)
  76. {
  77. struct scatterlist out_sg, extra_sg, in_sg;
  78. struct scatterlist *sgs_list[] = {
  79. [0] = &out_sg,
  80. [1] = extra ? &extra_sg : &in_sg,
  81. [2] = extra ? &in_sg : NULL,
  82. };
  83. struct virtio_pcidev_message_buffer *buf;
  84. int delay_count = 0;
  85. bool bounce_out;
  86. int ret, len;
  87. int buf_idx;
  88. bool posted;
  89. if (WARN_ON(cmd_size < sizeof(*cmd) || cmd_size > sizeof(*buf)))
  90. return -EINVAL;
  91. switch (cmd->op) {
  92. case VIRTIO_PCIDEV_OP_CFG_WRITE:
  93. case VIRTIO_PCIDEV_OP_MMIO_WRITE:
  94. case VIRTIO_PCIDEV_OP_MMIO_MEMSET:
  95. /* in PCI, writes are posted, so don't wait */
  96. posted = !out;
  97. WARN_ON(!posted);
  98. break;
  99. default:
  100. posted = false;
  101. break;
  102. }
  103. bounce_out = !posted && cmd_size <= sizeof(*cmd) &&
  104. out && out_size <= sizeof(buf->data);
  105. buf_idx = virtio_pcidev_get_buf(dev, &posted);
  106. buf = &dev->bufs[buf_idx];
  107. memcpy(buf, cmd, cmd_size);
  108. if (posted && extra && extra_size > sizeof(buf) - cmd_size) {
  109. dev->extra_ptrs[buf_idx] = kmemdup(extra, extra_size,
  110. GFP_ATOMIC);
  111. if (!dev->extra_ptrs[buf_idx]) {
  112. virtio_pcidev_free_buf(dev, buf);
  113. return -ENOMEM;
  114. }
  115. extra = dev->extra_ptrs[buf_idx];
  116. } else if (extra && extra_size <= sizeof(buf) - cmd_size) {
  117. memcpy((u8 *)buf + cmd_size, extra, extra_size);
  118. cmd_size += extra_size;
  119. extra_size = 0;
  120. extra = NULL;
  121. cmd = (void *)buf;
  122. } else {
  123. cmd = (void *)buf;
  124. }
  125. sg_init_one(&out_sg, cmd, cmd_size);
  126. if (extra)
  127. sg_init_one(&extra_sg, extra, extra_size);
  128. /* allow stack for small buffers */
  129. if (bounce_out)
  130. sg_init_one(&in_sg, buf->data, out_size);
  131. else if (out)
  132. sg_init_one(&in_sg, out, out_size);
  133. /* add to internal virtio queue */
  134. ret = virtqueue_add_sgs(dev->cmd_vq, sgs_list,
  135. extra ? 2 : 1,
  136. out ? 1 : 0,
  137. cmd, GFP_ATOMIC);
  138. if (ret) {
  139. virtio_pcidev_free_buf(dev, buf);
  140. return ret;
  141. }
  142. if (posted) {
  143. virtqueue_kick(dev->cmd_vq);
  144. return 0;
  145. }
  146. /* kick and poll for getting a response on the queue */
  147. set_bit(VIRTIO_PCIDEV_STAT_WAITING, &dev->status);
  148. virtqueue_kick(dev->cmd_vq);
  149. ret = 0;
  150. while (1) {
  151. void *completed = virtqueue_get_buf(dev->cmd_vq, &len);
  152. if (completed == buf)
  153. break;
  154. if (completed)
  155. virtio_pcidev_free_buf(dev, completed);
  156. if (WARN_ONCE(virtqueue_is_broken(dev->cmd_vq) ||
  157. ++delay_count > virtio_pcidev_max_delay_us,
  158. "um virt-pci delay: %d", delay_count)) {
  159. ret = -EIO;
  160. break;
  161. }
  162. udelay(1);
  163. }
  164. clear_bit(VIRTIO_PCIDEV_STAT_WAITING, &dev->status);
  165. if (bounce_out)
  166. memcpy(out, buf->data, out_size);
  167. virtio_pcidev_free_buf(dev, buf);
  168. return ret;
  169. }
  170. static unsigned long virtio_pcidev_cfgspace_read(struct um_pci_device *pdev,
  171. unsigned int offset, int size)
  172. {
  173. struct virtio_pcidev_device *dev = to_virtio_pcidev(pdev);
  174. struct virtio_pcidev_msg hdr = {
  175. .op = VIRTIO_PCIDEV_OP_CFG_READ,
  176. .size = size,
  177. .addr = offset,
  178. };
  179. /* max 8, we might not use it all */
  180. u8 data[8];
  181. memset(data, 0xff, sizeof(data));
  182. /* size has been checked in um_pci_cfgspace_read() */
  183. if (virtio_pcidev_send_cmd(dev, &hdr, sizeof(hdr), NULL, 0, data, size))
  184. return ULONG_MAX;
  185. switch (size) {
  186. case 1:
  187. return data[0];
  188. case 2:
  189. return le16_to_cpup((void *)data);
  190. case 4:
  191. return le32_to_cpup((void *)data);
  192. #ifdef CONFIG_64BIT
  193. case 8:
  194. return le64_to_cpup((void *)data);
  195. #endif
  196. default:
  197. return ULONG_MAX;
  198. }
  199. }
  200. static void virtio_pcidev_cfgspace_write(struct um_pci_device *pdev,
  201. unsigned int offset, int size,
  202. unsigned long val)
  203. {
  204. struct virtio_pcidev_device *dev = to_virtio_pcidev(pdev);
  205. struct {
  206. struct virtio_pcidev_msg hdr;
  207. /* maximum size - we may only use parts of it */
  208. u8 data[8];
  209. } msg = {
  210. .hdr = {
  211. .op = VIRTIO_PCIDEV_OP_CFG_WRITE,
  212. .size = size,
  213. .addr = offset,
  214. },
  215. };
  216. /* size has been checked in um_pci_cfgspace_write() */
  217. switch (size) {
  218. case 1:
  219. msg.data[0] = (u8)val;
  220. break;
  221. case 2:
  222. put_unaligned_le16(val, (void *)msg.data);
  223. break;
  224. case 4:
  225. put_unaligned_le32(val, (void *)msg.data);
  226. break;
  227. #ifdef CONFIG_64BIT
  228. case 8:
  229. put_unaligned_le64(val, (void *)msg.data);
  230. break;
  231. #endif
  232. }
  233. WARN_ON(virtio_pcidev_send_cmd(dev, &msg.hdr, sizeof(msg), NULL, 0, NULL, 0));
  234. }
  235. static void virtio_pcidev_bar_copy_from(struct um_pci_device *pdev,
  236. int bar, void *buffer,
  237. unsigned int offset, int size)
  238. {
  239. struct virtio_pcidev_device *dev = to_virtio_pcidev(pdev);
  240. struct virtio_pcidev_msg hdr = {
  241. .op = VIRTIO_PCIDEV_OP_MMIO_READ,
  242. .bar = bar,
  243. .size = size,
  244. .addr = offset,
  245. };
  246. memset(buffer, 0xff, size);
  247. virtio_pcidev_send_cmd(dev, &hdr, sizeof(hdr), NULL, 0, buffer, size);
  248. }
  249. static unsigned long virtio_pcidev_bar_read(struct um_pci_device *pdev, int bar,
  250. unsigned int offset, int size)
  251. {
  252. /* 8 is maximum size - we may only use parts of it */
  253. u8 data[8];
  254. /* size has been checked in um_pci_bar_read() */
  255. virtio_pcidev_bar_copy_from(pdev, bar, data, offset, size);
  256. switch (size) {
  257. case 1:
  258. return data[0];
  259. case 2:
  260. return le16_to_cpup((void *)data);
  261. case 4:
  262. return le32_to_cpup((void *)data);
  263. #ifdef CONFIG_64BIT
  264. case 8:
  265. return le64_to_cpup((void *)data);
  266. #endif
  267. default:
  268. return ULONG_MAX;
  269. }
  270. }
  271. static void virtio_pcidev_bar_copy_to(struct um_pci_device *pdev,
  272. int bar, unsigned int offset,
  273. const void *buffer, int size)
  274. {
  275. struct virtio_pcidev_device *dev = to_virtio_pcidev(pdev);
  276. struct virtio_pcidev_msg hdr = {
  277. .op = VIRTIO_PCIDEV_OP_MMIO_WRITE,
  278. .bar = bar,
  279. .size = size,
  280. .addr = offset,
  281. };
  282. virtio_pcidev_send_cmd(dev, &hdr, sizeof(hdr), buffer, size, NULL, 0);
  283. }
  284. static void virtio_pcidev_bar_write(struct um_pci_device *pdev, int bar,
  285. unsigned int offset, int size,
  286. unsigned long val)
  287. {
  288. /* maximum size - we may only use parts of it */
  289. u8 data[8];
  290. /* size has been checked in um_pci_bar_write() */
  291. switch (size) {
  292. case 1:
  293. data[0] = (u8)val;
  294. break;
  295. case 2:
  296. put_unaligned_le16(val, (void *)data);
  297. break;
  298. case 4:
  299. put_unaligned_le32(val, (void *)data);
  300. break;
  301. #ifdef CONFIG_64BIT
  302. case 8:
  303. put_unaligned_le64(val, (void *)data);
  304. break;
  305. #endif
  306. }
  307. virtio_pcidev_bar_copy_to(pdev, bar, offset, data, size);
  308. }
  309. static void virtio_pcidev_bar_set(struct um_pci_device *pdev, int bar,
  310. unsigned int offset, u8 value, int size)
  311. {
  312. struct virtio_pcidev_device *dev = to_virtio_pcidev(pdev);
  313. struct {
  314. struct virtio_pcidev_msg hdr;
  315. u8 data;
  316. } msg = {
  317. .hdr = {
  318. .op = VIRTIO_PCIDEV_OP_CFG_WRITE,
  319. .bar = bar,
  320. .size = size,
  321. .addr = offset,
  322. },
  323. .data = value,
  324. };
  325. virtio_pcidev_send_cmd(dev, &msg.hdr, sizeof(msg), NULL, 0, NULL, 0);
  326. }
  327. static const struct um_pci_ops virtio_pcidev_um_pci_ops = {
  328. .cfgspace_read = virtio_pcidev_cfgspace_read,
  329. .cfgspace_write = virtio_pcidev_cfgspace_write,
  330. .bar_read = virtio_pcidev_bar_read,
  331. .bar_write = virtio_pcidev_bar_write,
  332. .bar_copy_from = virtio_pcidev_bar_copy_from,
  333. .bar_copy_to = virtio_pcidev_bar_copy_to,
  334. .bar_set = virtio_pcidev_bar_set,
  335. };
  336. static void virtio_pcidev_irq_vq_addbuf(struct virtqueue *vq, void *buf, bool kick)
  337. {
  338. struct scatterlist sg[1];
  339. sg_init_one(sg, buf, MAX_IRQ_MSG_SIZE);
  340. if (virtqueue_add_inbuf(vq, sg, 1, buf, GFP_ATOMIC))
  341. kfree(buf);
  342. else if (kick)
  343. virtqueue_kick(vq);
  344. }
  345. static void virtio_pcidev_handle_irq_message(struct virtqueue *vq,
  346. struct virtio_pcidev_msg *msg)
  347. {
  348. struct virtio_device *vdev = vq->vdev;
  349. struct virtio_pcidev_device *dev = vdev->priv;
  350. if (!dev->pdev.irq)
  351. return;
  352. /* we should properly chain interrupts, but on ARCH=um we don't care */
  353. switch (msg->op) {
  354. case VIRTIO_PCIDEV_OP_INT:
  355. generic_handle_irq(dev->pdev.irq);
  356. break;
  357. case VIRTIO_PCIDEV_OP_MSI:
  358. /* our MSI message is just the interrupt number */
  359. if (msg->size == sizeof(u32))
  360. generic_handle_irq(le32_to_cpup((void *)msg->data));
  361. else
  362. generic_handle_irq(le16_to_cpup((void *)msg->data));
  363. break;
  364. case VIRTIO_PCIDEV_OP_PME:
  365. /* nothing to do - we already woke up due to the message */
  366. break;
  367. default:
  368. dev_err(&vdev->dev, "unexpected virt-pci message %d\n", msg->op);
  369. break;
  370. }
  371. }
  372. static void virtio_pcidev_cmd_vq_cb(struct virtqueue *vq)
  373. {
  374. struct virtio_device *vdev = vq->vdev;
  375. struct virtio_pcidev_device *dev = vdev->priv;
  376. void *cmd;
  377. int len;
  378. if (test_bit(VIRTIO_PCIDEV_STAT_WAITING, &dev->status))
  379. return;
  380. while ((cmd = virtqueue_get_buf(vq, &len)))
  381. virtio_pcidev_free_buf(dev, cmd);
  382. }
  383. static void virtio_pcidev_irq_vq_cb(struct virtqueue *vq)
  384. {
  385. struct virtio_pcidev_msg *msg;
  386. int len;
  387. while ((msg = virtqueue_get_buf(vq, &len))) {
  388. if (len >= sizeof(*msg))
  389. virtio_pcidev_handle_irq_message(vq, msg);
  390. /* recycle the message buffer */
  391. virtio_pcidev_irq_vq_addbuf(vq, msg, true);
  392. }
  393. }
  394. static int virtio_pcidev_init_vqs(struct virtio_pcidev_device *dev)
  395. {
  396. struct virtqueue_info vqs_info[] = {
  397. { "cmd", virtio_pcidev_cmd_vq_cb },
  398. { "irq", virtio_pcidev_irq_vq_cb },
  399. };
  400. struct virtqueue *vqs[2];
  401. int err, i;
  402. err = virtio_find_vqs(dev->vdev, 2, vqs, vqs_info, NULL);
  403. if (err)
  404. return err;
  405. dev->cmd_vq = vqs[0];
  406. dev->irq_vq = vqs[1];
  407. virtio_device_ready(dev->vdev);
  408. for (i = 0; i < NUM_IRQ_MSGS; i++) {
  409. void *msg = kzalloc(MAX_IRQ_MSG_SIZE, GFP_KERNEL);
  410. if (msg)
  411. virtio_pcidev_irq_vq_addbuf(dev->irq_vq, msg, false);
  412. }
  413. virtqueue_kick(dev->irq_vq);
  414. return 0;
  415. }
  416. static void __virtio_pcidev_virtio_platform_remove(struct virtio_device *vdev,
  417. struct virtio_pcidev_device *dev)
  418. {
  419. um_pci_platform_device_unregister(&dev->pdev);
  420. virtio_reset_device(vdev);
  421. vdev->config->del_vqs(vdev);
  422. kfree(dev);
  423. }
  424. static int virtio_pcidev_virtio_platform_probe(struct virtio_device *vdev,
  425. struct virtio_pcidev_device *dev)
  426. {
  427. int err;
  428. dev->platform = true;
  429. err = virtio_pcidev_init_vqs(dev);
  430. if (err)
  431. goto err_free;
  432. err = um_pci_platform_device_register(&dev->pdev);
  433. if (err)
  434. goto err_reset;
  435. err = of_platform_default_populate(vdev->dev.of_node, NULL, &vdev->dev);
  436. if (err)
  437. goto err_unregister;
  438. return 0;
  439. err_unregister:
  440. um_pci_platform_device_unregister(&dev->pdev);
  441. err_reset:
  442. virtio_reset_device(vdev);
  443. vdev->config->del_vqs(vdev);
  444. err_free:
  445. kfree(dev);
  446. return err;
  447. }
  448. static int virtio_pcidev_virtio_probe(struct virtio_device *vdev)
  449. {
  450. struct virtio_pcidev_device *dev;
  451. int err;
  452. dev = kzalloc_obj(*dev);
  453. if (!dev)
  454. return -ENOMEM;
  455. dev->vdev = vdev;
  456. vdev->priv = dev;
  457. dev->pdev.ops = &virtio_pcidev_um_pci_ops;
  458. if (of_device_is_compatible(vdev->dev.of_node, "simple-bus"))
  459. return virtio_pcidev_virtio_platform_probe(vdev, dev);
  460. err = virtio_pcidev_init_vqs(dev);
  461. if (err)
  462. goto err_free;
  463. err = um_pci_device_register(&dev->pdev);
  464. if (err)
  465. goto err_reset;
  466. device_set_wakeup_enable(&vdev->dev, true);
  467. /*
  468. * In order to do suspend-resume properly, don't allow VQs
  469. * to be suspended.
  470. */
  471. virtio_uml_set_no_vq_suspend(vdev, true);
  472. return 0;
  473. err_reset:
  474. virtio_reset_device(vdev);
  475. vdev->config->del_vqs(vdev);
  476. err_free:
  477. kfree(dev);
  478. return err;
  479. }
  480. static void virtio_pcidev_virtio_remove(struct virtio_device *vdev)
  481. {
  482. struct virtio_pcidev_device *dev = vdev->priv;
  483. if (dev->platform) {
  484. of_platform_depopulate(&vdev->dev);
  485. __virtio_pcidev_virtio_platform_remove(vdev, dev);
  486. return;
  487. }
  488. device_set_wakeup_enable(&vdev->dev, false);
  489. um_pci_device_unregister(&dev->pdev);
  490. /* Stop all virtqueues */
  491. virtio_reset_device(vdev);
  492. dev->cmd_vq = NULL;
  493. dev->irq_vq = NULL;
  494. vdev->config->del_vqs(vdev);
  495. kfree(dev);
  496. }
  497. static void virtio_pcidev_virtio_shutdown(struct virtio_device *vdev)
  498. {
  499. /* nothing to do, we just don't want queue shutdown */
  500. }
  501. static struct virtio_device_id id_table[] = {
  502. { CONFIG_UML_PCI_OVER_VIRTIO_DEVICE_ID, VIRTIO_DEV_ANY_ID },
  503. { 0 },
  504. };
  505. MODULE_DEVICE_TABLE(virtio, id_table);
  506. static struct virtio_driver virtio_pcidev_virtio_driver = {
  507. .driver.name = "virtio-pci",
  508. .id_table = id_table,
  509. .probe = virtio_pcidev_virtio_probe,
  510. .remove = virtio_pcidev_virtio_remove,
  511. .shutdown = virtio_pcidev_virtio_shutdown,
  512. };
  513. static int __init virtio_pcidev_init(void)
  514. {
  515. if (WARN(CONFIG_UML_PCI_OVER_VIRTIO_DEVICE_ID < 0,
  516. "No virtio device ID configured for PCI - no PCI support\n"))
  517. return 0;
  518. return register_virtio_driver(&virtio_pcidev_virtio_driver);
  519. }
  520. late_initcall(virtio_pcidev_init);
  521. static void __exit virtio_pcidev_exit(void)
  522. {
  523. unregister_virtio_driver(&virtio_pcidev_virtio_driver);
  524. }
  525. module_exit(virtio_pcidev_exit);