virt-pci.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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. #define MAX_DEVICES 8
  20. #define MAX_MSI_VECTORS 32
  21. #define CFG_SPACE_SIZE 4096
  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. #define HANDLE_NO_FREE(ptr) ((void *)((unsigned long)(ptr) | 1))
  26. #define HANDLE_IS_NO_FREE(ptr) ((unsigned long)(ptr) & 1)
  27. struct um_pci_device {
  28. struct virtio_device *vdev;
  29. /* for now just standard BARs */
  30. u8 resptr[PCI_STD_NUM_BARS];
  31. struct virtqueue *cmd_vq, *irq_vq;
  32. #define UM_PCI_STAT_WAITING 0
  33. unsigned long status;
  34. int irq;
  35. bool platform;
  36. };
  37. struct um_pci_device_reg {
  38. struct um_pci_device *dev;
  39. void __iomem *iomem;
  40. };
  41. static struct pci_host_bridge *bridge;
  42. static DEFINE_MUTEX(um_pci_mtx);
  43. static struct um_pci_device *um_pci_platform_device;
  44. static struct um_pci_device_reg um_pci_devices[MAX_DEVICES];
  45. static struct fwnode_handle *um_pci_fwnode;
  46. static struct irq_domain *um_pci_inner_domain;
  47. static struct irq_domain *um_pci_msi_domain;
  48. static unsigned long um_pci_msi_used[BITS_TO_LONGS(MAX_MSI_VECTORS)];
  49. static unsigned int um_pci_max_delay_us = 40000;
  50. module_param_named(max_delay_us, um_pci_max_delay_us, uint, 0644);
  51. struct um_pci_message_buffer {
  52. struct virtio_pcidev_msg hdr;
  53. u8 data[8];
  54. };
  55. static struct um_pci_message_buffer __percpu *um_pci_msg_bufs;
  56. static int um_pci_send_cmd(struct um_pci_device *dev,
  57. struct virtio_pcidev_msg *cmd,
  58. unsigned int cmd_size,
  59. const void *extra, unsigned int extra_size,
  60. void *out, unsigned int out_size)
  61. {
  62. struct scatterlist out_sg, extra_sg, in_sg;
  63. struct scatterlist *sgs_list[] = {
  64. [0] = &out_sg,
  65. [1] = extra ? &extra_sg : &in_sg,
  66. [2] = extra ? &in_sg : NULL,
  67. };
  68. struct um_pci_message_buffer *buf;
  69. int delay_count = 0;
  70. int ret, len;
  71. bool posted;
  72. if (WARN_ON(cmd_size < sizeof(*cmd) || cmd_size > sizeof(*buf)))
  73. return -EINVAL;
  74. switch (cmd->op) {
  75. case VIRTIO_PCIDEV_OP_CFG_WRITE:
  76. case VIRTIO_PCIDEV_OP_MMIO_WRITE:
  77. case VIRTIO_PCIDEV_OP_MMIO_MEMSET:
  78. /* in PCI, writes are posted, so don't wait */
  79. posted = !out;
  80. WARN_ON(!posted);
  81. break;
  82. default:
  83. posted = false;
  84. break;
  85. }
  86. buf = get_cpu_var(um_pci_msg_bufs);
  87. if (buf)
  88. memcpy(buf, cmd, cmd_size);
  89. if (posted) {
  90. u8 *ncmd = kmalloc(cmd_size + extra_size, GFP_ATOMIC);
  91. if (ncmd) {
  92. memcpy(ncmd, cmd, cmd_size);
  93. if (extra)
  94. memcpy(ncmd + cmd_size, extra, extra_size);
  95. cmd = (void *)ncmd;
  96. cmd_size += extra_size;
  97. extra = NULL;
  98. extra_size = 0;
  99. } else {
  100. /* try without allocating memory */
  101. posted = false;
  102. cmd = (void *)buf;
  103. }
  104. } else {
  105. cmd = (void *)buf;
  106. }
  107. sg_init_one(&out_sg, cmd, cmd_size);
  108. if (extra)
  109. sg_init_one(&extra_sg, extra, extra_size);
  110. if (out)
  111. sg_init_one(&in_sg, out, out_size);
  112. /* add to internal virtio queue */
  113. ret = virtqueue_add_sgs(dev->cmd_vq, sgs_list,
  114. extra ? 2 : 1,
  115. out ? 1 : 0,
  116. posted ? cmd : HANDLE_NO_FREE(cmd),
  117. GFP_ATOMIC);
  118. if (ret) {
  119. if (posted)
  120. kfree(cmd);
  121. goto out;
  122. }
  123. if (posted) {
  124. virtqueue_kick(dev->cmd_vq);
  125. ret = 0;
  126. goto out;
  127. }
  128. /* kick and poll for getting a response on the queue */
  129. set_bit(UM_PCI_STAT_WAITING, &dev->status);
  130. virtqueue_kick(dev->cmd_vq);
  131. while (1) {
  132. void *completed = virtqueue_get_buf(dev->cmd_vq, &len);
  133. if (completed == HANDLE_NO_FREE(cmd))
  134. break;
  135. if (completed && !HANDLE_IS_NO_FREE(completed))
  136. kfree(completed);
  137. if (WARN_ONCE(virtqueue_is_broken(dev->cmd_vq) ||
  138. ++delay_count > um_pci_max_delay_us,
  139. "um virt-pci delay: %d", delay_count)) {
  140. ret = -EIO;
  141. break;
  142. }
  143. udelay(1);
  144. }
  145. clear_bit(UM_PCI_STAT_WAITING, &dev->status);
  146. out:
  147. put_cpu_var(um_pci_msg_bufs);
  148. return ret;
  149. }
  150. static unsigned long um_pci_cfgspace_read(void *priv, unsigned int offset,
  151. int size)
  152. {
  153. struct um_pci_device_reg *reg = priv;
  154. struct um_pci_device *dev = reg->dev;
  155. struct virtio_pcidev_msg hdr = {
  156. .op = VIRTIO_PCIDEV_OP_CFG_READ,
  157. .size = size,
  158. .addr = offset,
  159. };
  160. /* buf->data is maximum size - we may only use parts of it */
  161. struct um_pci_message_buffer *buf;
  162. u8 *data;
  163. unsigned long ret = ULONG_MAX;
  164. size_t bytes = sizeof(buf->data);
  165. if (!dev)
  166. return ULONG_MAX;
  167. buf = get_cpu_var(um_pci_msg_bufs);
  168. data = buf->data;
  169. if (buf)
  170. memset(data, 0xff, bytes);
  171. switch (size) {
  172. case 1:
  173. case 2:
  174. case 4:
  175. #ifdef CONFIG_64BIT
  176. case 8:
  177. #endif
  178. break;
  179. default:
  180. WARN(1, "invalid config space read size %d\n", size);
  181. goto out;
  182. }
  183. if (um_pci_send_cmd(dev, &hdr, sizeof(hdr), NULL, 0, data, bytes))
  184. goto out;
  185. switch (size) {
  186. case 1:
  187. ret = data[0];
  188. break;
  189. case 2:
  190. ret = le16_to_cpup((void *)data);
  191. break;
  192. case 4:
  193. ret = le32_to_cpup((void *)data);
  194. break;
  195. #ifdef CONFIG_64BIT
  196. case 8:
  197. ret = le64_to_cpup((void *)data);
  198. break;
  199. #endif
  200. default:
  201. break;
  202. }
  203. out:
  204. put_cpu_var(um_pci_msg_bufs);
  205. return ret;
  206. }
  207. static void um_pci_cfgspace_write(void *priv, unsigned int offset, int size,
  208. unsigned long val)
  209. {
  210. struct um_pci_device_reg *reg = priv;
  211. struct um_pci_device *dev = reg->dev;
  212. struct {
  213. struct virtio_pcidev_msg hdr;
  214. /* maximum size - we may only use parts of it */
  215. u8 data[8];
  216. } msg = {
  217. .hdr = {
  218. .op = VIRTIO_PCIDEV_OP_CFG_WRITE,
  219. .size = size,
  220. .addr = offset,
  221. },
  222. };
  223. if (!dev)
  224. return;
  225. switch (size) {
  226. case 1:
  227. msg.data[0] = (u8)val;
  228. break;
  229. case 2:
  230. put_unaligned_le16(val, (void *)msg.data);
  231. break;
  232. case 4:
  233. put_unaligned_le32(val, (void *)msg.data);
  234. break;
  235. #ifdef CONFIG_64BIT
  236. case 8:
  237. put_unaligned_le64(val, (void *)msg.data);
  238. break;
  239. #endif
  240. default:
  241. WARN(1, "invalid config space write size %d\n", size);
  242. return;
  243. }
  244. WARN_ON(um_pci_send_cmd(dev, &msg.hdr, sizeof(msg), NULL, 0, NULL, 0));
  245. }
  246. static const struct logic_iomem_ops um_pci_device_cfgspace_ops = {
  247. .read = um_pci_cfgspace_read,
  248. .write = um_pci_cfgspace_write,
  249. };
  250. static void um_pci_bar_copy_from(void *priv, void *buffer,
  251. unsigned int offset, int size)
  252. {
  253. u8 *resptr = priv;
  254. struct um_pci_device *dev = container_of(resptr - *resptr,
  255. struct um_pci_device,
  256. resptr[0]);
  257. struct virtio_pcidev_msg hdr = {
  258. .op = VIRTIO_PCIDEV_OP_MMIO_READ,
  259. .bar = *resptr,
  260. .size = size,
  261. .addr = offset,
  262. };
  263. memset(buffer, 0xff, size);
  264. um_pci_send_cmd(dev, &hdr, sizeof(hdr), NULL, 0, buffer, size);
  265. }
  266. static unsigned long um_pci_bar_read(void *priv, unsigned int offset,
  267. int size)
  268. {
  269. /* buf->data is maximum size - we may only use parts of it */
  270. struct um_pci_message_buffer *buf;
  271. u8 *data;
  272. unsigned long ret = ULONG_MAX;
  273. buf = get_cpu_var(um_pci_msg_bufs);
  274. data = buf->data;
  275. switch (size) {
  276. case 1:
  277. case 2:
  278. case 4:
  279. #ifdef CONFIG_64BIT
  280. case 8:
  281. #endif
  282. break;
  283. default:
  284. WARN(1, "invalid config space read size %d\n", size);
  285. goto out;
  286. }
  287. um_pci_bar_copy_from(priv, data, offset, size);
  288. switch (size) {
  289. case 1:
  290. ret = data[0];
  291. break;
  292. case 2:
  293. ret = le16_to_cpup((void *)data);
  294. break;
  295. case 4:
  296. ret = le32_to_cpup((void *)data);
  297. break;
  298. #ifdef CONFIG_64BIT
  299. case 8:
  300. ret = le64_to_cpup((void *)data);
  301. break;
  302. #endif
  303. default:
  304. break;
  305. }
  306. out:
  307. put_cpu_var(um_pci_msg_bufs);
  308. return ret;
  309. }
  310. static void um_pci_bar_copy_to(void *priv, unsigned int offset,
  311. const void *buffer, int size)
  312. {
  313. u8 *resptr = priv;
  314. struct um_pci_device *dev = container_of(resptr - *resptr,
  315. struct um_pci_device,
  316. resptr[0]);
  317. struct virtio_pcidev_msg hdr = {
  318. .op = VIRTIO_PCIDEV_OP_MMIO_WRITE,
  319. .bar = *resptr,
  320. .size = size,
  321. .addr = offset,
  322. };
  323. um_pci_send_cmd(dev, &hdr, sizeof(hdr), buffer, size, NULL, 0);
  324. }
  325. static void um_pci_bar_write(void *priv, unsigned int offset, int size,
  326. unsigned long val)
  327. {
  328. /* maximum size - we may only use parts of it */
  329. u8 data[8];
  330. switch (size) {
  331. case 1:
  332. data[0] = (u8)val;
  333. break;
  334. case 2:
  335. put_unaligned_le16(val, (void *)data);
  336. break;
  337. case 4:
  338. put_unaligned_le32(val, (void *)data);
  339. break;
  340. #ifdef CONFIG_64BIT
  341. case 8:
  342. put_unaligned_le64(val, (void *)data);
  343. break;
  344. #endif
  345. default:
  346. WARN(1, "invalid config space write size %d\n", size);
  347. return;
  348. }
  349. um_pci_bar_copy_to(priv, offset, data, size);
  350. }
  351. static void um_pci_bar_set(void *priv, unsigned int offset, u8 value, int size)
  352. {
  353. u8 *resptr = priv;
  354. struct um_pci_device *dev = container_of(resptr - *resptr,
  355. struct um_pci_device,
  356. resptr[0]);
  357. struct {
  358. struct virtio_pcidev_msg hdr;
  359. u8 data;
  360. } msg = {
  361. .hdr = {
  362. .op = VIRTIO_PCIDEV_OP_CFG_WRITE,
  363. .bar = *resptr,
  364. .size = size,
  365. .addr = offset,
  366. },
  367. .data = value,
  368. };
  369. um_pci_send_cmd(dev, &msg.hdr, sizeof(msg), NULL, 0, NULL, 0);
  370. }
  371. static const struct logic_iomem_ops um_pci_device_bar_ops = {
  372. .read = um_pci_bar_read,
  373. .write = um_pci_bar_write,
  374. .set = um_pci_bar_set,
  375. .copy_from = um_pci_bar_copy_from,
  376. .copy_to = um_pci_bar_copy_to,
  377. };
  378. static void __iomem *um_pci_map_bus(struct pci_bus *bus, unsigned int devfn,
  379. int where)
  380. {
  381. struct um_pci_device_reg *dev;
  382. unsigned int busn = bus->number;
  383. if (busn > 0)
  384. return NULL;
  385. /* not allowing functions for now ... */
  386. if (devfn % 8)
  387. return NULL;
  388. if (devfn / 8 >= ARRAY_SIZE(um_pci_devices))
  389. return NULL;
  390. dev = &um_pci_devices[devfn / 8];
  391. if (!dev)
  392. return NULL;
  393. return (void __iomem *)((unsigned long)dev->iomem + where);
  394. }
  395. static struct pci_ops um_pci_ops = {
  396. .map_bus = um_pci_map_bus,
  397. .read = pci_generic_config_read,
  398. .write = pci_generic_config_write,
  399. };
  400. static void um_pci_rescan(void)
  401. {
  402. pci_lock_rescan_remove();
  403. pci_rescan_bus(bridge->bus);
  404. pci_unlock_rescan_remove();
  405. }
  406. static void um_pci_irq_vq_addbuf(struct virtqueue *vq, void *buf, bool kick)
  407. {
  408. struct scatterlist sg[1];
  409. sg_init_one(sg, buf, MAX_IRQ_MSG_SIZE);
  410. if (virtqueue_add_inbuf(vq, sg, 1, buf, GFP_ATOMIC))
  411. kfree(buf);
  412. else if (kick)
  413. virtqueue_kick(vq);
  414. }
  415. static void um_pci_handle_irq_message(struct virtqueue *vq,
  416. struct virtio_pcidev_msg *msg)
  417. {
  418. struct virtio_device *vdev = vq->vdev;
  419. struct um_pci_device *dev = vdev->priv;
  420. if (!dev->irq)
  421. return;
  422. /* we should properly chain interrupts, but on ARCH=um we don't care */
  423. switch (msg->op) {
  424. case VIRTIO_PCIDEV_OP_INT:
  425. generic_handle_irq(dev->irq);
  426. break;
  427. case VIRTIO_PCIDEV_OP_MSI:
  428. /* our MSI message is just the interrupt number */
  429. if (msg->size == sizeof(u32))
  430. generic_handle_irq(le32_to_cpup((void *)msg->data));
  431. else
  432. generic_handle_irq(le16_to_cpup((void *)msg->data));
  433. break;
  434. case VIRTIO_PCIDEV_OP_PME:
  435. /* nothing to do - we already woke up due to the message */
  436. break;
  437. default:
  438. dev_err(&vdev->dev, "unexpected virt-pci message %d\n", msg->op);
  439. break;
  440. }
  441. }
  442. static void um_pci_cmd_vq_cb(struct virtqueue *vq)
  443. {
  444. struct virtio_device *vdev = vq->vdev;
  445. struct um_pci_device *dev = vdev->priv;
  446. void *cmd;
  447. int len;
  448. if (test_bit(UM_PCI_STAT_WAITING, &dev->status))
  449. return;
  450. while ((cmd = virtqueue_get_buf(vq, &len))) {
  451. if (WARN_ON(HANDLE_IS_NO_FREE(cmd)))
  452. continue;
  453. kfree(cmd);
  454. }
  455. }
  456. static void um_pci_irq_vq_cb(struct virtqueue *vq)
  457. {
  458. struct virtio_pcidev_msg *msg;
  459. int len;
  460. while ((msg = virtqueue_get_buf(vq, &len))) {
  461. if (len >= sizeof(*msg))
  462. um_pci_handle_irq_message(vq, msg);
  463. /* recycle the message buffer */
  464. um_pci_irq_vq_addbuf(vq, msg, true);
  465. }
  466. }
  467. #ifdef CONFIG_OF
  468. /* Copied from arch/x86/kernel/devicetree.c */
  469. struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
  470. {
  471. struct device_node *np;
  472. for_each_node_by_type(np, "pci") {
  473. const void *prop;
  474. unsigned int bus_min;
  475. prop = of_get_property(np, "bus-range", NULL);
  476. if (!prop)
  477. continue;
  478. bus_min = be32_to_cpup(prop);
  479. if (bus->number == bus_min)
  480. return np;
  481. }
  482. return NULL;
  483. }
  484. #endif
  485. static int um_pci_init_vqs(struct um_pci_device *dev)
  486. {
  487. struct virtqueue_info vqs_info[] = {
  488. { "cmd", um_pci_cmd_vq_cb },
  489. { "irq", um_pci_irq_vq_cb },
  490. };
  491. struct virtqueue *vqs[2];
  492. int err, i;
  493. err = virtio_find_vqs(dev->vdev, 2, vqs, vqs_info, NULL);
  494. if (err)
  495. return err;
  496. dev->cmd_vq = vqs[0];
  497. dev->irq_vq = vqs[1];
  498. virtio_device_ready(dev->vdev);
  499. for (i = 0; i < NUM_IRQ_MSGS; i++) {
  500. void *msg = kzalloc(MAX_IRQ_MSG_SIZE, GFP_KERNEL);
  501. if (msg)
  502. um_pci_irq_vq_addbuf(dev->irq_vq, msg, false);
  503. }
  504. virtqueue_kick(dev->irq_vq);
  505. return 0;
  506. }
  507. static void __um_pci_virtio_platform_remove(struct virtio_device *vdev,
  508. struct um_pci_device *dev)
  509. {
  510. virtio_reset_device(vdev);
  511. vdev->config->del_vqs(vdev);
  512. mutex_lock(&um_pci_mtx);
  513. um_pci_platform_device = NULL;
  514. mutex_unlock(&um_pci_mtx);
  515. kfree(dev);
  516. }
  517. static int um_pci_virtio_platform_probe(struct virtio_device *vdev,
  518. struct um_pci_device *dev)
  519. {
  520. int ret;
  521. dev->platform = true;
  522. mutex_lock(&um_pci_mtx);
  523. if (um_pci_platform_device) {
  524. mutex_unlock(&um_pci_mtx);
  525. ret = -EBUSY;
  526. goto out_free;
  527. }
  528. ret = um_pci_init_vqs(dev);
  529. if (ret) {
  530. mutex_unlock(&um_pci_mtx);
  531. goto out_free;
  532. }
  533. um_pci_platform_device = dev;
  534. mutex_unlock(&um_pci_mtx);
  535. ret = of_platform_default_populate(vdev->dev.of_node, NULL, &vdev->dev);
  536. if (ret)
  537. __um_pci_virtio_platform_remove(vdev, dev);
  538. return ret;
  539. out_free:
  540. kfree(dev);
  541. return ret;
  542. }
  543. static int um_pci_virtio_probe(struct virtio_device *vdev)
  544. {
  545. struct um_pci_device *dev;
  546. int i, free = -1;
  547. int err = -ENOSPC;
  548. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  549. if (!dev)
  550. return -ENOMEM;
  551. dev->vdev = vdev;
  552. vdev->priv = dev;
  553. if (of_device_is_compatible(vdev->dev.of_node, "simple-bus"))
  554. return um_pci_virtio_platform_probe(vdev, dev);
  555. mutex_lock(&um_pci_mtx);
  556. for (i = 0; i < MAX_DEVICES; i++) {
  557. if (um_pci_devices[i].dev)
  558. continue;
  559. free = i;
  560. break;
  561. }
  562. if (free < 0)
  563. goto error;
  564. err = um_pci_init_vqs(dev);
  565. if (err)
  566. goto error;
  567. dev->irq = irq_alloc_desc(numa_node_id());
  568. if (dev->irq < 0) {
  569. err = dev->irq;
  570. goto err_reset;
  571. }
  572. um_pci_devices[free].dev = dev;
  573. vdev->priv = dev;
  574. mutex_unlock(&um_pci_mtx);
  575. device_set_wakeup_enable(&vdev->dev, true);
  576. /*
  577. * In order to do suspend-resume properly, don't allow VQs
  578. * to be suspended.
  579. */
  580. virtio_uml_set_no_vq_suspend(vdev, true);
  581. um_pci_rescan();
  582. return 0;
  583. err_reset:
  584. virtio_reset_device(vdev);
  585. vdev->config->del_vqs(vdev);
  586. error:
  587. mutex_unlock(&um_pci_mtx);
  588. kfree(dev);
  589. return err;
  590. }
  591. static void um_pci_virtio_remove(struct virtio_device *vdev)
  592. {
  593. struct um_pci_device *dev = vdev->priv;
  594. int i;
  595. if (dev->platform) {
  596. of_platform_depopulate(&vdev->dev);
  597. __um_pci_virtio_platform_remove(vdev, dev);
  598. return;
  599. }
  600. device_set_wakeup_enable(&vdev->dev, false);
  601. mutex_lock(&um_pci_mtx);
  602. for (i = 0; i < MAX_DEVICES; i++) {
  603. if (um_pci_devices[i].dev != dev)
  604. continue;
  605. um_pci_devices[i].dev = NULL;
  606. irq_free_desc(dev->irq);
  607. break;
  608. }
  609. mutex_unlock(&um_pci_mtx);
  610. if (i < MAX_DEVICES) {
  611. struct pci_dev *pci_dev;
  612. pci_dev = pci_get_slot(bridge->bus, i);
  613. if (pci_dev)
  614. pci_stop_and_remove_bus_device_locked(pci_dev);
  615. }
  616. /* Stop all virtqueues */
  617. virtio_reset_device(vdev);
  618. dev->cmd_vq = NULL;
  619. dev->irq_vq = NULL;
  620. vdev->config->del_vqs(vdev);
  621. kfree(dev);
  622. }
  623. static struct virtio_device_id id_table[] = {
  624. { CONFIG_UML_PCI_OVER_VIRTIO_DEVICE_ID, VIRTIO_DEV_ANY_ID },
  625. { 0 },
  626. };
  627. MODULE_DEVICE_TABLE(virtio, id_table);
  628. static struct virtio_driver um_pci_virtio_driver = {
  629. .driver.name = "virtio-pci",
  630. .id_table = id_table,
  631. .probe = um_pci_virtio_probe,
  632. .remove = um_pci_virtio_remove,
  633. };
  634. static struct resource virt_cfgspace_resource = {
  635. .name = "PCI config space",
  636. .start = 0xf0000000 - MAX_DEVICES * CFG_SPACE_SIZE,
  637. .end = 0xf0000000 - 1,
  638. .flags = IORESOURCE_MEM,
  639. };
  640. static long um_pci_map_cfgspace(unsigned long offset, size_t size,
  641. const struct logic_iomem_ops **ops,
  642. void **priv)
  643. {
  644. if (WARN_ON(size > CFG_SPACE_SIZE || offset % CFG_SPACE_SIZE))
  645. return -EINVAL;
  646. if (offset / CFG_SPACE_SIZE < MAX_DEVICES) {
  647. *ops = &um_pci_device_cfgspace_ops;
  648. *priv = &um_pci_devices[offset / CFG_SPACE_SIZE];
  649. return 0;
  650. }
  651. WARN(1, "cannot map offset 0x%lx/0x%zx\n", offset, size);
  652. return -ENOENT;
  653. }
  654. static const struct logic_iomem_region_ops um_pci_cfgspace_ops = {
  655. .map = um_pci_map_cfgspace,
  656. };
  657. static struct resource virt_iomem_resource = {
  658. .name = "PCI iomem",
  659. .start = 0xf0000000,
  660. .end = 0xffffffff,
  661. .flags = IORESOURCE_MEM,
  662. };
  663. struct um_pci_map_iomem_data {
  664. unsigned long offset;
  665. size_t size;
  666. const struct logic_iomem_ops **ops;
  667. void **priv;
  668. long ret;
  669. };
  670. static int um_pci_map_iomem_walk(struct pci_dev *pdev, void *_data)
  671. {
  672. struct um_pci_map_iomem_data *data = _data;
  673. struct um_pci_device_reg *reg = &um_pci_devices[pdev->devfn / 8];
  674. struct um_pci_device *dev;
  675. int i;
  676. if (!reg->dev)
  677. return 0;
  678. for (i = 0; i < ARRAY_SIZE(dev->resptr); i++) {
  679. struct resource *r = &pdev->resource[i];
  680. if ((r->flags & IORESOURCE_TYPE_BITS) != IORESOURCE_MEM)
  681. continue;
  682. /*
  683. * must be the whole or part of the resource,
  684. * not allowed to only overlap
  685. */
  686. if (data->offset < r->start || data->offset > r->end)
  687. continue;
  688. if (data->offset + data->size - 1 > r->end)
  689. continue;
  690. dev = reg->dev;
  691. *data->ops = &um_pci_device_bar_ops;
  692. dev->resptr[i] = i;
  693. *data->priv = &dev->resptr[i];
  694. data->ret = data->offset - r->start;
  695. /* no need to continue */
  696. return 1;
  697. }
  698. return 0;
  699. }
  700. static long um_pci_map_iomem(unsigned long offset, size_t size,
  701. const struct logic_iomem_ops **ops,
  702. void **priv)
  703. {
  704. struct um_pci_map_iomem_data data = {
  705. /* we want the full address here */
  706. .offset = offset + virt_iomem_resource.start,
  707. .size = size,
  708. .ops = ops,
  709. .priv = priv,
  710. .ret = -ENOENT,
  711. };
  712. pci_walk_bus(bridge->bus, um_pci_map_iomem_walk, &data);
  713. return data.ret;
  714. }
  715. static const struct logic_iomem_region_ops um_pci_iomem_ops = {
  716. .map = um_pci_map_iomem,
  717. };
  718. static void um_pci_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  719. {
  720. /*
  721. * This is a very low address and not actually valid 'physical' memory
  722. * in UML, so we can simply map MSI(-X) vectors to there, it cannot be
  723. * legitimately written to by the device in any other way.
  724. * We use the (virtual) IRQ number here as the message to simplify the
  725. * code that receives the message, where for now we simply trust the
  726. * device to send the correct message.
  727. */
  728. msg->address_hi = 0;
  729. msg->address_lo = 0xa0000;
  730. msg->data = data->irq;
  731. }
  732. static struct irq_chip um_pci_msi_bottom_irq_chip = {
  733. .name = "UM virtio MSI",
  734. .irq_compose_msi_msg = um_pci_compose_msi_msg,
  735. };
  736. static int um_pci_inner_domain_alloc(struct irq_domain *domain,
  737. unsigned int virq, unsigned int nr_irqs,
  738. void *args)
  739. {
  740. unsigned long bit;
  741. WARN_ON(nr_irqs != 1);
  742. mutex_lock(&um_pci_mtx);
  743. bit = find_first_zero_bit(um_pci_msi_used, MAX_MSI_VECTORS);
  744. if (bit >= MAX_MSI_VECTORS) {
  745. mutex_unlock(&um_pci_mtx);
  746. return -ENOSPC;
  747. }
  748. set_bit(bit, um_pci_msi_used);
  749. mutex_unlock(&um_pci_mtx);
  750. irq_domain_set_info(domain, virq, bit, &um_pci_msi_bottom_irq_chip,
  751. domain->host_data, handle_simple_irq,
  752. NULL, NULL);
  753. return 0;
  754. }
  755. static void um_pci_inner_domain_free(struct irq_domain *domain,
  756. unsigned int virq, unsigned int nr_irqs)
  757. {
  758. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  759. mutex_lock(&um_pci_mtx);
  760. if (!test_bit(d->hwirq, um_pci_msi_used))
  761. pr_err("trying to free unused MSI#%lu\n", d->hwirq);
  762. else
  763. __clear_bit(d->hwirq, um_pci_msi_used);
  764. mutex_unlock(&um_pci_mtx);
  765. }
  766. static const struct irq_domain_ops um_pci_inner_domain_ops = {
  767. .alloc = um_pci_inner_domain_alloc,
  768. .free = um_pci_inner_domain_free,
  769. };
  770. static struct irq_chip um_pci_msi_irq_chip = {
  771. .name = "UM virtio PCIe MSI",
  772. .irq_mask = pci_msi_mask_irq,
  773. .irq_unmask = pci_msi_unmask_irq,
  774. };
  775. static struct msi_domain_info um_pci_msi_domain_info = {
  776. .flags = MSI_FLAG_USE_DEF_DOM_OPS |
  777. MSI_FLAG_USE_DEF_CHIP_OPS |
  778. MSI_FLAG_PCI_MSIX,
  779. .chip = &um_pci_msi_irq_chip,
  780. };
  781. static struct resource busn_resource = {
  782. .name = "PCI busn",
  783. .start = 0,
  784. .end = 0,
  785. .flags = IORESOURCE_BUS,
  786. };
  787. static int um_pci_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
  788. {
  789. struct um_pci_device_reg *reg = &um_pci_devices[pdev->devfn / 8];
  790. if (WARN_ON(!reg->dev))
  791. return -EINVAL;
  792. /* Yes, we map all pins to the same IRQ ... doesn't matter for now. */
  793. return reg->dev->irq;
  794. }
  795. void *pci_root_bus_fwnode(struct pci_bus *bus)
  796. {
  797. return um_pci_fwnode;
  798. }
  799. static long um_pci_map_platform(unsigned long offset, size_t size,
  800. const struct logic_iomem_ops **ops,
  801. void **priv)
  802. {
  803. if (!um_pci_platform_device)
  804. return -ENOENT;
  805. *ops = &um_pci_device_bar_ops;
  806. *priv = &um_pci_platform_device->resptr[0];
  807. return offset;
  808. }
  809. static const struct logic_iomem_region_ops um_pci_platform_ops = {
  810. .map = um_pci_map_platform,
  811. };
  812. static struct resource virt_platform_resource = {
  813. .name = "platform",
  814. .start = 0x10000000,
  815. .end = 0x1fffffff,
  816. .flags = IORESOURCE_MEM,
  817. };
  818. static int __init um_pci_init(void)
  819. {
  820. struct irq_domain_info inner_domain_info = {
  821. .size = MAX_MSI_VECTORS,
  822. .hwirq_max = MAX_MSI_VECTORS,
  823. .ops = &um_pci_inner_domain_ops,
  824. };
  825. int err, i;
  826. WARN_ON(logic_iomem_add_region(&virt_cfgspace_resource,
  827. &um_pci_cfgspace_ops));
  828. WARN_ON(logic_iomem_add_region(&virt_iomem_resource,
  829. &um_pci_iomem_ops));
  830. WARN_ON(logic_iomem_add_region(&virt_platform_resource,
  831. &um_pci_platform_ops));
  832. if (WARN(CONFIG_UML_PCI_OVER_VIRTIO_DEVICE_ID < 0,
  833. "No virtio device ID configured for PCI - no PCI support\n"))
  834. return 0;
  835. um_pci_msg_bufs = alloc_percpu(struct um_pci_message_buffer);
  836. if (!um_pci_msg_bufs)
  837. return -ENOMEM;
  838. bridge = pci_alloc_host_bridge(0);
  839. if (!bridge) {
  840. err = -ENOMEM;
  841. goto free;
  842. }
  843. um_pci_fwnode = irq_domain_alloc_named_fwnode("um-pci");
  844. if (!um_pci_fwnode) {
  845. err = -ENOMEM;
  846. goto free;
  847. }
  848. inner_domain_info.fwnode = um_pci_fwnode;
  849. um_pci_inner_domain = irq_domain_instantiate(&inner_domain_info);
  850. if (IS_ERR(um_pci_inner_domain)) {
  851. err = PTR_ERR(um_pci_inner_domain);
  852. goto free;
  853. }
  854. um_pci_msi_domain = pci_msi_create_irq_domain(um_pci_fwnode,
  855. &um_pci_msi_domain_info,
  856. um_pci_inner_domain);
  857. if (!um_pci_msi_domain) {
  858. err = -ENOMEM;
  859. goto free;
  860. }
  861. pci_add_resource(&bridge->windows, &virt_iomem_resource);
  862. pci_add_resource(&bridge->windows, &busn_resource);
  863. bridge->ops = &um_pci_ops;
  864. bridge->map_irq = um_pci_map_irq;
  865. for (i = 0; i < MAX_DEVICES; i++) {
  866. resource_size_t start;
  867. start = virt_cfgspace_resource.start + i * CFG_SPACE_SIZE;
  868. um_pci_devices[i].iomem = ioremap(start, CFG_SPACE_SIZE);
  869. if (WARN(!um_pci_devices[i].iomem, "failed to map %d\n", i)) {
  870. err = -ENOMEM;
  871. goto free;
  872. }
  873. }
  874. err = pci_host_probe(bridge);
  875. if (err)
  876. goto free;
  877. err = register_virtio_driver(&um_pci_virtio_driver);
  878. if (err)
  879. goto free;
  880. return 0;
  881. free:
  882. if (!IS_ERR_OR_NULL(um_pci_inner_domain))
  883. irq_domain_remove(um_pci_inner_domain);
  884. if (um_pci_fwnode)
  885. irq_domain_free_fwnode(um_pci_fwnode);
  886. if (bridge) {
  887. pci_free_resource_list(&bridge->windows);
  888. pci_free_host_bridge(bridge);
  889. }
  890. free_percpu(um_pci_msg_bufs);
  891. return err;
  892. }
  893. module_init(um_pci_init);
  894. static void __exit um_pci_exit(void)
  895. {
  896. unregister_virtio_driver(&um_pci_virtio_driver);
  897. irq_domain_remove(um_pci_msi_domain);
  898. irq_domain_remove(um_pci_inner_domain);
  899. pci_free_resource_list(&bridge->windows);
  900. pci_free_host_bridge(bridge);
  901. free_percpu(um_pci_msg_bufs);
  902. }
  903. module_exit(um_pci_exit);