virt-pci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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/logic_iomem.h>
  9. #include <linux/of_platform.h>
  10. #include <linux/irqchip/irq-msi-lib.h>
  11. #include <linux/irqdomain.h>
  12. #include <linux/msi.h>
  13. #include <linux/unaligned.h>
  14. #include <irq_kern.h>
  15. #include "virt-pci.h"
  16. #define MAX_DEVICES 8
  17. #define MAX_MSI_VECTORS 32
  18. #define CFG_SPACE_SIZE 4096
  19. struct um_pci_device_reg {
  20. struct um_pci_device *dev;
  21. void __iomem *iomem;
  22. };
  23. static struct pci_host_bridge *bridge;
  24. static DEFINE_MUTEX(um_pci_mtx);
  25. static struct um_pci_device *um_pci_platform_device;
  26. static struct um_pci_device_reg um_pci_devices[MAX_DEVICES];
  27. static struct fwnode_handle *um_pci_fwnode;
  28. static struct irq_domain *um_pci_inner_domain;
  29. static unsigned long um_pci_msi_used[BITS_TO_LONGS(MAX_MSI_VECTORS)];
  30. static unsigned long um_pci_cfgspace_read(void *priv, unsigned int offset,
  31. int size)
  32. {
  33. struct um_pci_device_reg *reg = priv;
  34. struct um_pci_device *dev = reg->dev;
  35. if (!dev)
  36. return ULONG_MAX;
  37. switch (size) {
  38. case 1:
  39. case 2:
  40. case 4:
  41. #ifdef CONFIG_64BIT
  42. case 8:
  43. #endif
  44. break;
  45. default:
  46. WARN(1, "invalid config space read size %d\n", size);
  47. return ULONG_MAX;
  48. }
  49. return dev->ops->cfgspace_read(dev, offset, size);
  50. }
  51. static void um_pci_cfgspace_write(void *priv, unsigned int offset, int size,
  52. unsigned long val)
  53. {
  54. struct um_pci_device_reg *reg = priv;
  55. struct um_pci_device *dev = reg->dev;
  56. if (!dev)
  57. return;
  58. switch (size) {
  59. case 1:
  60. case 2:
  61. case 4:
  62. #ifdef CONFIG_64BIT
  63. case 8:
  64. #endif
  65. break;
  66. default:
  67. WARN(1, "invalid config space write size %d\n", size);
  68. return;
  69. }
  70. dev->ops->cfgspace_write(dev, offset, size, val);
  71. }
  72. static const struct logic_iomem_ops um_pci_device_cfgspace_ops = {
  73. .read = um_pci_cfgspace_read,
  74. .write = um_pci_cfgspace_write,
  75. };
  76. static unsigned long um_pci_bar_read(void *priv, unsigned int offset,
  77. int size)
  78. {
  79. u8 *resptr = priv;
  80. struct um_pci_device *dev = container_of(resptr - *resptr,
  81. struct um_pci_device,
  82. resptr[0]);
  83. u8 bar = *resptr;
  84. switch (size) {
  85. case 1:
  86. case 2:
  87. case 4:
  88. #ifdef CONFIG_64BIT
  89. case 8:
  90. #endif
  91. break;
  92. default:
  93. WARN(1, "invalid bar read size %d\n", size);
  94. return ULONG_MAX;
  95. }
  96. return dev->ops->bar_read(dev, bar, offset, size);
  97. }
  98. static void um_pci_bar_write(void *priv, unsigned int offset, int size,
  99. unsigned long val)
  100. {
  101. u8 *resptr = priv;
  102. struct um_pci_device *dev = container_of(resptr - *resptr,
  103. struct um_pci_device,
  104. resptr[0]);
  105. u8 bar = *resptr;
  106. switch (size) {
  107. case 1:
  108. case 2:
  109. case 4:
  110. #ifdef CONFIG_64BIT
  111. case 8:
  112. #endif
  113. break;
  114. default:
  115. WARN(1, "invalid bar write size %d\n", size);
  116. return;
  117. }
  118. dev->ops->bar_write(dev, bar, offset, size, val);
  119. }
  120. static void um_pci_bar_copy_from(void *priv, void *buffer,
  121. unsigned int offset, int size)
  122. {
  123. u8 *resptr = priv;
  124. struct um_pci_device *dev = container_of(resptr - *resptr,
  125. struct um_pci_device,
  126. resptr[0]);
  127. u8 bar = *resptr;
  128. dev->ops->bar_copy_from(dev, bar, buffer, offset, size);
  129. }
  130. static void um_pci_bar_copy_to(void *priv, unsigned int offset,
  131. const void *buffer, int size)
  132. {
  133. u8 *resptr = priv;
  134. struct um_pci_device *dev = container_of(resptr - *resptr,
  135. struct um_pci_device,
  136. resptr[0]);
  137. u8 bar = *resptr;
  138. dev->ops->bar_copy_to(dev, bar, offset, buffer, size);
  139. }
  140. static void um_pci_bar_set(void *priv, unsigned int offset, u8 value, int size)
  141. {
  142. u8 *resptr = priv;
  143. struct um_pci_device *dev = container_of(resptr - *resptr,
  144. struct um_pci_device,
  145. resptr[0]);
  146. u8 bar = *resptr;
  147. dev->ops->bar_set(dev, bar, offset, value, size);
  148. }
  149. static const struct logic_iomem_ops um_pci_device_bar_ops = {
  150. .read = um_pci_bar_read,
  151. .write = um_pci_bar_write,
  152. .set = um_pci_bar_set,
  153. .copy_from = um_pci_bar_copy_from,
  154. .copy_to = um_pci_bar_copy_to,
  155. };
  156. static void __iomem *um_pci_map_bus(struct pci_bus *bus, unsigned int devfn,
  157. int where)
  158. {
  159. struct um_pci_device_reg *dev;
  160. unsigned int busn = bus->number;
  161. if (busn > 0)
  162. return NULL;
  163. /* not allowing functions for now ... */
  164. if (devfn % 8)
  165. return NULL;
  166. if (devfn / 8 >= ARRAY_SIZE(um_pci_devices))
  167. return NULL;
  168. dev = &um_pci_devices[devfn / 8];
  169. if (!dev)
  170. return NULL;
  171. return (void __iomem *)((unsigned long)dev->iomem + where);
  172. }
  173. static struct pci_ops um_pci_ops = {
  174. .map_bus = um_pci_map_bus,
  175. .read = pci_generic_config_read,
  176. .write = pci_generic_config_write,
  177. };
  178. static void um_pci_rescan(void)
  179. {
  180. pci_lock_rescan_remove();
  181. pci_rescan_bus(bridge->bus);
  182. pci_unlock_rescan_remove();
  183. }
  184. #ifdef CONFIG_OF
  185. /* Copied from arch/x86/kernel/devicetree.c */
  186. struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
  187. {
  188. struct device_node *np;
  189. for_each_node_by_type(np, "pci") {
  190. const void *prop;
  191. unsigned int bus_min;
  192. prop = of_get_property(np, "bus-range", NULL);
  193. if (!prop)
  194. continue;
  195. bus_min = be32_to_cpup(prop);
  196. if (bus->number == bus_min)
  197. return np;
  198. }
  199. return NULL;
  200. }
  201. #endif
  202. static struct resource virt_cfgspace_resource = {
  203. .name = "PCI config space",
  204. .start = 0xf0000000 - MAX_DEVICES * CFG_SPACE_SIZE,
  205. .end = 0xf0000000 - 1,
  206. .flags = IORESOURCE_MEM,
  207. };
  208. static long um_pci_map_cfgspace(unsigned long offset, size_t size,
  209. const struct logic_iomem_ops **ops,
  210. void **priv)
  211. {
  212. if (WARN_ON(size > CFG_SPACE_SIZE || offset % CFG_SPACE_SIZE))
  213. return -EINVAL;
  214. if (offset / CFG_SPACE_SIZE < MAX_DEVICES) {
  215. *ops = &um_pci_device_cfgspace_ops;
  216. *priv = &um_pci_devices[offset / CFG_SPACE_SIZE];
  217. return 0;
  218. }
  219. WARN(1, "cannot map offset 0x%lx/0x%zx\n", offset, size);
  220. return -ENOENT;
  221. }
  222. static const struct logic_iomem_region_ops um_pci_cfgspace_ops = {
  223. .map = um_pci_map_cfgspace,
  224. };
  225. static struct resource virt_iomem_resource = {
  226. .name = "PCI iomem",
  227. .start = 0xf0000000,
  228. .end = 0xffffffff,
  229. .flags = IORESOURCE_MEM,
  230. };
  231. struct um_pci_map_iomem_data {
  232. unsigned long offset;
  233. size_t size;
  234. const struct logic_iomem_ops **ops;
  235. void **priv;
  236. long ret;
  237. };
  238. static int um_pci_map_iomem_walk(struct pci_dev *pdev, void *_data)
  239. {
  240. struct um_pci_map_iomem_data *data = _data;
  241. struct um_pci_device_reg *reg = &um_pci_devices[pdev->devfn / 8];
  242. struct um_pci_device *dev;
  243. int i;
  244. if (!reg->dev)
  245. return 0;
  246. for (i = 0; i < ARRAY_SIZE(dev->resptr); i++) {
  247. struct resource *r = &pdev->resource[i];
  248. if ((r->flags & IORESOURCE_TYPE_BITS) != IORESOURCE_MEM)
  249. continue;
  250. /*
  251. * must be the whole or part of the resource,
  252. * not allowed to only overlap
  253. */
  254. if (data->offset < r->start || data->offset > r->end)
  255. continue;
  256. if (data->offset + data->size - 1 > r->end)
  257. continue;
  258. dev = reg->dev;
  259. *data->ops = &um_pci_device_bar_ops;
  260. dev->resptr[i] = i;
  261. *data->priv = &dev->resptr[i];
  262. data->ret = data->offset - r->start;
  263. /* no need to continue */
  264. return 1;
  265. }
  266. return 0;
  267. }
  268. static long um_pci_map_iomem(unsigned long offset, size_t size,
  269. const struct logic_iomem_ops **ops,
  270. void **priv)
  271. {
  272. struct um_pci_map_iomem_data data = {
  273. /* we want the full address here */
  274. .offset = offset + virt_iomem_resource.start,
  275. .size = size,
  276. .ops = ops,
  277. .priv = priv,
  278. .ret = -ENOENT,
  279. };
  280. pci_walk_bus(bridge->bus, um_pci_map_iomem_walk, &data);
  281. return data.ret;
  282. }
  283. static const struct logic_iomem_region_ops um_pci_iomem_ops = {
  284. .map = um_pci_map_iomem,
  285. };
  286. static void um_pci_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  287. {
  288. /*
  289. * This is a very low address and not actually valid 'physical' memory
  290. * in UML, so we can simply map MSI(-X) vectors to there, it cannot be
  291. * legitimately written to by the device in any other way.
  292. * We use the (virtual) IRQ number here as the message to simplify the
  293. * code that receives the message, where for now we simply trust the
  294. * device to send the correct message.
  295. */
  296. msg->address_hi = 0;
  297. msg->address_lo = 0xa0000;
  298. msg->data = data->irq;
  299. }
  300. static struct irq_chip um_pci_msi_bottom_irq_chip = {
  301. .name = "UM virtual MSI",
  302. .irq_compose_msi_msg = um_pci_compose_msi_msg,
  303. };
  304. static int um_pci_inner_domain_alloc(struct irq_domain *domain,
  305. unsigned int virq, unsigned int nr_irqs,
  306. void *args)
  307. {
  308. unsigned long bit;
  309. WARN_ON(nr_irqs != 1);
  310. mutex_lock(&um_pci_mtx);
  311. bit = find_first_zero_bit(um_pci_msi_used, MAX_MSI_VECTORS);
  312. if (bit >= MAX_MSI_VECTORS) {
  313. mutex_unlock(&um_pci_mtx);
  314. return -ENOSPC;
  315. }
  316. set_bit(bit, um_pci_msi_used);
  317. mutex_unlock(&um_pci_mtx);
  318. irq_domain_set_info(domain, virq, bit, &um_pci_msi_bottom_irq_chip,
  319. domain->host_data, handle_simple_irq,
  320. NULL, NULL);
  321. return 0;
  322. }
  323. static void um_pci_inner_domain_free(struct irq_domain *domain,
  324. unsigned int virq, unsigned int nr_irqs)
  325. {
  326. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  327. mutex_lock(&um_pci_mtx);
  328. if (!test_bit(d->hwirq, um_pci_msi_used))
  329. pr_err("trying to free unused MSI#%lu\n", d->hwirq);
  330. else
  331. __clear_bit(d->hwirq, um_pci_msi_used);
  332. mutex_unlock(&um_pci_mtx);
  333. }
  334. static const struct irq_domain_ops um_pci_inner_domain_ops = {
  335. .select = msi_lib_irq_domain_select,
  336. .alloc = um_pci_inner_domain_alloc,
  337. .free = um_pci_inner_domain_free,
  338. };
  339. #define UM_PCI_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS | \
  340. MSI_FLAG_USE_DEF_CHIP_OPS | \
  341. MSI_FLAG_NO_AFFINITY)
  342. #define UM_PCI_MSI_FLAGS_SUPPORTED (MSI_GENERIC_FLAGS_MASK | \
  343. MSI_FLAG_PCI_MSIX)
  344. static const struct msi_parent_ops um_pci_msi_parent_ops = {
  345. .required_flags = UM_PCI_MSI_FLAGS_REQUIRED,
  346. .supported_flags = UM_PCI_MSI_FLAGS_SUPPORTED,
  347. .bus_select_token = DOMAIN_BUS_NEXUS,
  348. .bus_select_mask = MATCH_PCI_MSI,
  349. .prefix = "UM-virtual-",
  350. .init_dev_msi_info = msi_lib_init_dev_msi_info,
  351. };
  352. static struct resource busn_resource = {
  353. .name = "PCI busn",
  354. .start = 0,
  355. .end = 0,
  356. .flags = IORESOURCE_BUS,
  357. };
  358. static int um_pci_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
  359. {
  360. struct um_pci_device_reg *reg = &um_pci_devices[pdev->devfn / 8];
  361. if (WARN_ON(!reg->dev))
  362. return -EINVAL;
  363. /* Yes, we map all pins to the same IRQ ... doesn't matter for now. */
  364. return reg->dev->irq;
  365. }
  366. void *pci_root_bus_fwnode(struct pci_bus *bus)
  367. {
  368. return um_pci_fwnode;
  369. }
  370. static long um_pci_map_platform(unsigned long offset, size_t size,
  371. const struct logic_iomem_ops **ops,
  372. void **priv)
  373. {
  374. if (!um_pci_platform_device)
  375. return -ENOENT;
  376. *ops = &um_pci_device_bar_ops;
  377. *priv = &um_pci_platform_device->resptr[0];
  378. return offset;
  379. }
  380. static const struct logic_iomem_region_ops um_pci_platform_ops = {
  381. .map = um_pci_map_platform,
  382. };
  383. static struct resource virt_platform_resource = {
  384. .name = "platform",
  385. .start = 0x10000000,
  386. .end = 0x1fffffff,
  387. .flags = IORESOURCE_MEM,
  388. };
  389. int um_pci_device_register(struct um_pci_device *dev)
  390. {
  391. int i, free = -1;
  392. int err = 0;
  393. mutex_lock(&um_pci_mtx);
  394. for (i = 0; i < MAX_DEVICES; i++) {
  395. if (um_pci_devices[i].dev)
  396. continue;
  397. free = i;
  398. break;
  399. }
  400. if (free < 0) {
  401. err = -ENOSPC;
  402. goto out;
  403. }
  404. dev->irq = irq_alloc_desc(numa_node_id());
  405. if (dev->irq < 0) {
  406. err = dev->irq;
  407. goto out;
  408. }
  409. um_pci_devices[free].dev = dev;
  410. out:
  411. mutex_unlock(&um_pci_mtx);
  412. if (!err)
  413. um_pci_rescan();
  414. return err;
  415. }
  416. void um_pci_device_unregister(struct um_pci_device *dev)
  417. {
  418. int i;
  419. mutex_lock(&um_pci_mtx);
  420. for (i = 0; i < MAX_DEVICES; i++) {
  421. if (um_pci_devices[i].dev != dev)
  422. continue;
  423. um_pci_devices[i].dev = NULL;
  424. irq_free_desc(dev->irq);
  425. break;
  426. }
  427. mutex_unlock(&um_pci_mtx);
  428. if (i < MAX_DEVICES) {
  429. struct pci_dev *pci_dev;
  430. pci_dev = pci_get_slot(bridge->bus, i);
  431. if (pci_dev)
  432. pci_stop_and_remove_bus_device_locked(pci_dev);
  433. }
  434. }
  435. int um_pci_platform_device_register(struct um_pci_device *dev)
  436. {
  437. guard(mutex)(&um_pci_mtx);
  438. if (um_pci_platform_device)
  439. return -EBUSY;
  440. um_pci_platform_device = dev;
  441. return 0;
  442. }
  443. void um_pci_platform_device_unregister(struct um_pci_device *dev)
  444. {
  445. guard(mutex)(&um_pci_mtx);
  446. if (um_pci_platform_device == dev)
  447. um_pci_platform_device = NULL;
  448. }
  449. static int __init um_pci_init(void)
  450. {
  451. int err, i;
  452. WARN_ON(logic_iomem_add_region(&virt_cfgspace_resource,
  453. &um_pci_cfgspace_ops));
  454. WARN_ON(logic_iomem_add_region(&virt_iomem_resource,
  455. &um_pci_iomem_ops));
  456. WARN_ON(logic_iomem_add_region(&virt_platform_resource,
  457. &um_pci_platform_ops));
  458. bridge = pci_alloc_host_bridge(0);
  459. if (!bridge) {
  460. err = -ENOMEM;
  461. goto free;
  462. }
  463. um_pci_fwnode = irq_domain_alloc_named_fwnode("um-pci");
  464. if (!um_pci_fwnode) {
  465. err = -ENOMEM;
  466. goto free;
  467. }
  468. struct irq_domain_info info = {
  469. .fwnode = um_pci_fwnode,
  470. .ops = &um_pci_inner_domain_ops,
  471. .size = MAX_MSI_VECTORS,
  472. };
  473. um_pci_inner_domain = msi_create_parent_irq_domain(&info, &um_pci_msi_parent_ops);
  474. if (!um_pci_inner_domain) {
  475. err = -ENOMEM;
  476. goto free;
  477. }
  478. pci_add_resource(&bridge->windows, &virt_iomem_resource);
  479. pci_add_resource(&bridge->windows, &busn_resource);
  480. bridge->ops = &um_pci_ops;
  481. bridge->map_irq = um_pci_map_irq;
  482. for (i = 0; i < MAX_DEVICES; i++) {
  483. resource_size_t start;
  484. start = virt_cfgspace_resource.start + i * CFG_SPACE_SIZE;
  485. um_pci_devices[i].iomem = ioremap(start, CFG_SPACE_SIZE);
  486. if (WARN(!um_pci_devices[i].iomem, "failed to map %d\n", i)) {
  487. err = -ENOMEM;
  488. goto free;
  489. }
  490. }
  491. err = pci_host_probe(bridge);
  492. if (err)
  493. goto free;
  494. return 0;
  495. free:
  496. if (um_pci_inner_domain)
  497. irq_domain_remove(um_pci_inner_domain);
  498. if (um_pci_fwnode)
  499. irq_domain_free_fwnode(um_pci_fwnode);
  500. if (bridge) {
  501. pci_free_resource_list(&bridge->windows);
  502. pci_free_host_bridge(bridge);
  503. }
  504. return err;
  505. }
  506. device_initcall(um_pci_init);
  507. static void __exit um_pci_exit(void)
  508. {
  509. irq_domain_remove(um_pci_inner_domain);
  510. pci_free_resource_list(&bridge->windows);
  511. pci_free_host_bridge(bridge);
  512. }
  513. module_exit(um_pci_exit);