of.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PCI <-> OF mapping helpers
  4. *
  5. * Copyright 2011 IBM Corp.
  6. */
  7. #define pr_fmt(fmt) "PCI: OF: " fmt
  8. #include <linux/irqdomain.h>
  9. #include <linux/kernel.h>
  10. #include <linux/pci.h>
  11. #include <linux/of.h>
  12. #include <linux/of_irq.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_pci.h>
  15. #include "pci.h"
  16. void pci_set_of_node(struct pci_dev *dev)
  17. {
  18. if (!dev->bus->dev.of_node)
  19. return;
  20. dev->dev.of_node = of_pci_find_child_device(dev->bus->dev.of_node,
  21. dev->devfn);
  22. }
  23. void pci_release_of_node(struct pci_dev *dev)
  24. {
  25. of_node_put(dev->dev.of_node);
  26. dev->dev.of_node = NULL;
  27. }
  28. void pci_set_bus_of_node(struct pci_bus *bus)
  29. {
  30. if (bus->self == NULL)
  31. bus->dev.of_node = pcibios_get_phb_of_node(bus);
  32. else
  33. bus->dev.of_node = of_node_get(bus->self->dev.of_node);
  34. }
  35. void pci_release_bus_of_node(struct pci_bus *bus)
  36. {
  37. of_node_put(bus->dev.of_node);
  38. bus->dev.of_node = NULL;
  39. }
  40. struct device_node * __weak pcibios_get_phb_of_node(struct pci_bus *bus)
  41. {
  42. /* This should only be called for PHBs */
  43. if (WARN_ON(bus->self || bus->parent))
  44. return NULL;
  45. /*
  46. * Look for a node pointer in either the intermediary device we
  47. * create above the root bus or its own parent. Normally only
  48. * the later is populated.
  49. */
  50. if (bus->bridge->of_node)
  51. return of_node_get(bus->bridge->of_node);
  52. if (bus->bridge->parent && bus->bridge->parent->of_node)
  53. return of_node_get(bus->bridge->parent->of_node);
  54. return NULL;
  55. }
  56. struct irq_domain *pci_host_bridge_of_msi_domain(struct pci_bus *bus)
  57. {
  58. #ifdef CONFIG_IRQ_DOMAIN
  59. struct irq_domain *d;
  60. if (!bus->dev.of_node)
  61. return NULL;
  62. /* Start looking for a phandle to an MSI controller. */
  63. d = of_msi_get_domain(&bus->dev, bus->dev.of_node, DOMAIN_BUS_PCI_MSI);
  64. if (d)
  65. return d;
  66. /*
  67. * If we don't have an msi-parent property, look for a domain
  68. * directly attached to the host bridge.
  69. */
  70. d = irq_find_matching_host(bus->dev.of_node, DOMAIN_BUS_PCI_MSI);
  71. if (d)
  72. return d;
  73. return irq_find_host(bus->dev.of_node);
  74. #else
  75. return NULL;
  76. #endif
  77. }
  78. static inline int __of_pci_pci_compare(struct device_node *node,
  79. unsigned int data)
  80. {
  81. int devfn;
  82. devfn = of_pci_get_devfn(node);
  83. if (devfn < 0)
  84. return 0;
  85. return devfn == data;
  86. }
  87. struct device_node *of_pci_find_child_device(struct device_node *parent,
  88. unsigned int devfn)
  89. {
  90. struct device_node *node, *node2;
  91. for_each_child_of_node(parent, node) {
  92. if (__of_pci_pci_compare(node, devfn))
  93. return node;
  94. /*
  95. * Some OFs create a parent node "multifunc-device" as
  96. * a fake root for all functions of a multi-function
  97. * device we go down them as well.
  98. */
  99. if (!strcmp(node->name, "multifunc-device")) {
  100. for_each_child_of_node(node, node2) {
  101. if (__of_pci_pci_compare(node2, devfn)) {
  102. of_node_put(node);
  103. return node2;
  104. }
  105. }
  106. }
  107. }
  108. return NULL;
  109. }
  110. EXPORT_SYMBOL_GPL(of_pci_find_child_device);
  111. /**
  112. * of_pci_get_devfn() - Get device and function numbers for a device node
  113. * @np: device node
  114. *
  115. * Parses a standard 5-cell PCI resource and returns an 8-bit value that can
  116. * be passed to the PCI_SLOT() and PCI_FUNC() macros to extract the device
  117. * and function numbers respectively. On error a negative error code is
  118. * returned.
  119. */
  120. int of_pci_get_devfn(struct device_node *np)
  121. {
  122. u32 reg[5];
  123. int error;
  124. error = of_property_read_u32_array(np, "reg", reg, ARRAY_SIZE(reg));
  125. if (error)
  126. return error;
  127. return (reg[0] >> 8) & 0xff;
  128. }
  129. EXPORT_SYMBOL_GPL(of_pci_get_devfn);
  130. /**
  131. * of_pci_parse_bus_range() - parse the bus-range property of a PCI device
  132. * @node: device node
  133. * @res: address to a struct resource to return the bus-range
  134. *
  135. * Returns 0 on success or a negative error-code on failure.
  136. */
  137. int of_pci_parse_bus_range(struct device_node *node, struct resource *res)
  138. {
  139. u32 bus_range[2];
  140. int error;
  141. error = of_property_read_u32_array(node, "bus-range", bus_range,
  142. ARRAY_SIZE(bus_range));
  143. if (error)
  144. return error;
  145. res->name = node->name;
  146. res->start = bus_range[0];
  147. res->end = bus_range[1];
  148. res->flags = IORESOURCE_BUS;
  149. return 0;
  150. }
  151. EXPORT_SYMBOL_GPL(of_pci_parse_bus_range);
  152. /**
  153. * This function will try to obtain the host bridge domain number by
  154. * finding a property called "linux,pci-domain" of the given device node.
  155. *
  156. * @node: device tree node with the domain information
  157. *
  158. * Returns the associated domain number from DT in the range [0-0xffff], or
  159. * a negative value if the required property is not found.
  160. */
  161. int of_get_pci_domain_nr(struct device_node *node)
  162. {
  163. u32 domain;
  164. int error;
  165. error = of_property_read_u32(node, "linux,pci-domain", &domain);
  166. if (error)
  167. return error;
  168. return (u16)domain;
  169. }
  170. EXPORT_SYMBOL_GPL(of_get_pci_domain_nr);
  171. /**
  172. * This function will try to find the limitation of link speed by finding
  173. * a property called "max-link-speed" of the given device node.
  174. *
  175. * @node: device tree node with the max link speed information
  176. *
  177. * Returns the associated max link speed from DT, or a negative value if the
  178. * required property is not found or is invalid.
  179. */
  180. int of_pci_get_max_link_speed(struct device_node *node)
  181. {
  182. u32 max_link_speed;
  183. if (of_property_read_u32(node, "max-link-speed", &max_link_speed) ||
  184. max_link_speed > 4)
  185. return -EINVAL;
  186. return max_link_speed;
  187. }
  188. EXPORT_SYMBOL_GPL(of_pci_get_max_link_speed);
  189. /**
  190. * of_pci_check_probe_only - Setup probe only mode if linux,pci-probe-only
  191. * is present and valid
  192. */
  193. void of_pci_check_probe_only(void)
  194. {
  195. u32 val;
  196. int ret;
  197. ret = of_property_read_u32(of_chosen, "linux,pci-probe-only", &val);
  198. if (ret) {
  199. if (ret == -ENODATA || ret == -EOVERFLOW)
  200. pr_warn("linux,pci-probe-only without valid value, ignoring\n");
  201. return;
  202. }
  203. if (val)
  204. pci_add_flags(PCI_PROBE_ONLY);
  205. else
  206. pci_clear_flags(PCI_PROBE_ONLY);
  207. pr_info("PROBE_ONLY %sabled\n", val ? "en" : "dis");
  208. }
  209. EXPORT_SYMBOL_GPL(of_pci_check_probe_only);
  210. #if defined(CONFIG_OF_ADDRESS)
  211. /**
  212. * devm_of_pci_get_host_bridge_resources() - Resource-managed parsing of PCI
  213. * host bridge resources from DT
  214. * @dev: host bridge device
  215. * @busno: bus number associated with the bridge root bus
  216. * @bus_max: maximum number of buses for this bridge
  217. * @resources: list where the range of resources will be added after DT parsing
  218. * @io_base: pointer to a variable that will contain on return the physical
  219. * address for the start of the I/O range. Can be NULL if the caller doesn't
  220. * expect I/O ranges to be present in the device tree.
  221. *
  222. * This function will parse the "ranges" property of a PCI host bridge device
  223. * node and setup the resource mapping based on its content. It is expected
  224. * that the property conforms with the Power ePAPR document.
  225. *
  226. * It returns zero if the range parsing has been successful or a standard error
  227. * value if it failed.
  228. */
  229. int devm_of_pci_get_host_bridge_resources(struct device *dev,
  230. unsigned char busno, unsigned char bus_max,
  231. struct list_head *resources, resource_size_t *io_base)
  232. {
  233. struct device_node *dev_node = dev->of_node;
  234. struct resource *res, tmp_res;
  235. struct resource *bus_range;
  236. struct of_pci_range range;
  237. struct of_pci_range_parser parser;
  238. char range_type[4];
  239. int err;
  240. if (io_base)
  241. *io_base = (resource_size_t)OF_BAD_ADDR;
  242. bus_range = devm_kzalloc(dev, sizeof(*bus_range), GFP_KERNEL);
  243. if (!bus_range)
  244. return -ENOMEM;
  245. dev_info(dev, "host bridge %pOF ranges:\n", dev_node);
  246. err = of_pci_parse_bus_range(dev_node, bus_range);
  247. if (err) {
  248. bus_range->start = busno;
  249. bus_range->end = bus_max;
  250. bus_range->flags = IORESOURCE_BUS;
  251. dev_info(dev, " No bus range found for %pOF, using %pR\n",
  252. dev_node, bus_range);
  253. } else {
  254. if (bus_range->end > bus_range->start + bus_max)
  255. bus_range->end = bus_range->start + bus_max;
  256. }
  257. pci_add_resource(resources, bus_range);
  258. /* Check for ranges property */
  259. err = of_pci_range_parser_init(&parser, dev_node);
  260. if (err)
  261. goto failed;
  262. dev_dbg(dev, "Parsing ranges property...\n");
  263. for_each_of_pci_range(&parser, &range) {
  264. /* Read next ranges element */
  265. if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO)
  266. snprintf(range_type, 4, " IO");
  267. else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM)
  268. snprintf(range_type, 4, "MEM");
  269. else
  270. snprintf(range_type, 4, "err");
  271. dev_info(dev, " %s %#010llx..%#010llx -> %#010llx\n",
  272. range_type, range.cpu_addr,
  273. range.cpu_addr + range.size - 1, range.pci_addr);
  274. /*
  275. * If we failed translation or got a zero-sized region
  276. * then skip this range
  277. */
  278. if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
  279. continue;
  280. err = of_pci_range_to_resource(&range, dev_node, &tmp_res);
  281. if (err)
  282. continue;
  283. res = devm_kmemdup(dev, &tmp_res, sizeof(tmp_res), GFP_KERNEL);
  284. if (!res) {
  285. err = -ENOMEM;
  286. goto failed;
  287. }
  288. if (resource_type(res) == IORESOURCE_IO) {
  289. if (!io_base) {
  290. dev_err(dev, "I/O range found for %pOF. Please provide an io_base pointer to save CPU base address\n",
  291. dev_node);
  292. err = -EINVAL;
  293. goto failed;
  294. }
  295. if (*io_base != (resource_size_t)OF_BAD_ADDR)
  296. dev_warn(dev, "More than one I/O resource converted for %pOF. CPU base address for old range lost!\n",
  297. dev_node);
  298. *io_base = range.cpu_addr;
  299. }
  300. pci_add_resource_offset(resources, res, res->start - range.pci_addr);
  301. }
  302. return 0;
  303. failed:
  304. pci_free_resource_list(resources);
  305. return err;
  306. }
  307. EXPORT_SYMBOL_GPL(devm_of_pci_get_host_bridge_resources);
  308. #endif /* CONFIG_OF_ADDRESS */
  309. #if IS_ENABLED(CONFIG_OF_IRQ)
  310. /**
  311. * of_irq_parse_pci - Resolve the interrupt for a PCI device
  312. * @pdev: the device whose interrupt is to be resolved
  313. * @out_irq: structure of_irq filled by this function
  314. *
  315. * This function resolves the PCI interrupt for a given PCI device. If a
  316. * device-node exists for a given pci_dev, it will use normal OF tree
  317. * walking. If not, it will implement standard swizzling and walk up the
  318. * PCI tree until an device-node is found, at which point it will finish
  319. * resolving using the OF tree walking.
  320. */
  321. static int of_irq_parse_pci(const struct pci_dev *pdev, struct of_phandle_args *out_irq)
  322. {
  323. struct device_node *dn, *ppnode;
  324. struct pci_dev *ppdev;
  325. __be32 laddr[3];
  326. u8 pin;
  327. int rc;
  328. /*
  329. * Check if we have a device node, if yes, fallback to standard
  330. * device tree parsing
  331. */
  332. dn = pci_device_to_OF_node(pdev);
  333. if (dn) {
  334. rc = of_irq_parse_one(dn, 0, out_irq);
  335. if (!rc)
  336. return rc;
  337. }
  338. /*
  339. * Ok, we don't, time to have fun. Let's start by building up an
  340. * interrupt spec. we assume #interrupt-cells is 1, which is standard
  341. * for PCI. If you do different, then don't use that routine.
  342. */
  343. rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
  344. if (rc != 0)
  345. goto err;
  346. /* No pin, exit with no error message. */
  347. if (pin == 0)
  348. return -ENODEV;
  349. /* Now we walk up the PCI tree */
  350. for (;;) {
  351. /* Get the pci_dev of our parent */
  352. ppdev = pdev->bus->self;
  353. /* Ouch, it's a host bridge... */
  354. if (ppdev == NULL) {
  355. ppnode = pci_bus_to_OF_node(pdev->bus);
  356. /* No node for host bridge ? give up */
  357. if (ppnode == NULL) {
  358. rc = -EINVAL;
  359. goto err;
  360. }
  361. } else {
  362. /* We found a P2P bridge, check if it has a node */
  363. ppnode = pci_device_to_OF_node(ppdev);
  364. }
  365. /*
  366. * Ok, we have found a parent with a device-node, hand over to
  367. * the OF parsing code.
  368. * We build a unit address from the linux device to be used for
  369. * resolution. Note that we use the linux bus number which may
  370. * not match your firmware bus numbering.
  371. * Fortunately, in most cases, interrupt-map-mask doesn't
  372. * include the bus number as part of the matching.
  373. * You should still be careful about that though if you intend
  374. * to rely on this function (you ship a firmware that doesn't
  375. * create device nodes for all PCI devices).
  376. */
  377. if (ppnode)
  378. break;
  379. /*
  380. * We can only get here if we hit a P2P bridge with no node;
  381. * let's do standard swizzling and try again
  382. */
  383. pin = pci_swizzle_interrupt_pin(pdev, pin);
  384. pdev = ppdev;
  385. }
  386. out_irq->np = ppnode;
  387. out_irq->args_count = 1;
  388. out_irq->args[0] = pin;
  389. laddr[0] = cpu_to_be32((pdev->bus->number << 16) | (pdev->devfn << 8));
  390. laddr[1] = laddr[2] = cpu_to_be32(0);
  391. rc = of_irq_parse_raw(laddr, out_irq);
  392. if (rc)
  393. goto err;
  394. return 0;
  395. err:
  396. if (rc == -ENOENT) {
  397. dev_warn(&pdev->dev,
  398. "%s: no interrupt-map found, INTx interrupts not available\n",
  399. __func__);
  400. pr_warn_once("%s: possibly some PCI slots don't have level triggered interrupts capability\n",
  401. __func__);
  402. } else {
  403. dev_err(&pdev->dev, "%s: failed with rc=%d\n", __func__, rc);
  404. }
  405. return rc;
  406. }
  407. /**
  408. * of_irq_parse_and_map_pci() - Decode a PCI IRQ from the device tree and map to a VIRQ
  409. * @dev: The PCI device needing an IRQ
  410. * @slot: PCI slot number; passed when used as map_irq callback. Unused
  411. * @pin: PCI IRQ pin number; passed when used as map_irq callback. Unused
  412. *
  413. * @slot and @pin are unused, but included in the function so that this
  414. * function can be used directly as the map_irq callback to
  415. * pci_assign_irq() and struct pci_host_bridge.map_irq pointer
  416. */
  417. int of_irq_parse_and_map_pci(const struct pci_dev *dev, u8 slot, u8 pin)
  418. {
  419. struct of_phandle_args oirq;
  420. int ret;
  421. ret = of_irq_parse_pci(dev, &oirq);
  422. if (ret)
  423. return 0; /* Proper return code 0 == NO_IRQ */
  424. return irq_create_of_mapping(&oirq);
  425. }
  426. EXPORT_SYMBOL_GPL(of_irq_parse_and_map_pci);
  427. #endif /* CONFIG_OF_IRQ */
  428. int pci_parse_request_of_pci_ranges(struct device *dev,
  429. struct list_head *resources,
  430. struct resource **bus_range)
  431. {
  432. int err, res_valid = 0;
  433. resource_size_t iobase;
  434. struct resource_entry *win, *tmp;
  435. INIT_LIST_HEAD(resources);
  436. err = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff, resources,
  437. &iobase);
  438. if (err)
  439. return err;
  440. err = devm_request_pci_bus_resources(dev, resources);
  441. if (err)
  442. goto out_release_res;
  443. resource_list_for_each_entry_safe(win, tmp, resources) {
  444. struct resource *res = win->res;
  445. switch (resource_type(res)) {
  446. case IORESOURCE_IO:
  447. err = devm_pci_remap_iospace(dev, res, iobase);
  448. if (err) {
  449. dev_warn(dev, "error %d: failed to map resource %pR\n",
  450. err, res);
  451. resource_list_destroy_entry(win);
  452. }
  453. break;
  454. case IORESOURCE_MEM:
  455. res_valid |= !(res->flags & IORESOURCE_PREFETCH);
  456. break;
  457. case IORESOURCE_BUS:
  458. if (bus_range)
  459. *bus_range = res;
  460. break;
  461. }
  462. }
  463. if (res_valid)
  464. return 0;
  465. dev_err(dev, "non-prefetchable memory resource required\n");
  466. err = -EINVAL;
  467. out_release_res:
  468. pci_free_resource_list(resources);
  469. return err;
  470. }