of.c 16 KB

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