of.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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/cleanup.h>
  9. #include <linux/irqdomain.h>
  10. #include <linux/kernel.h>
  11. #include <linux/pci.h>
  12. #include <linux/of.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_pci.h>
  16. #include <linux/platform_device.h>
  17. #include "pci.h"
  18. #ifdef CONFIG_PCI
  19. /**
  20. * pci_set_of_node - Find and set device's DT device_node
  21. * @dev: the PCI device structure to fill
  22. *
  23. * Returns 0 on success with of_node set or when no device is described in the
  24. * DT. Returns -ENODEV if the device is present, but disabled in the DT.
  25. */
  26. int pci_set_of_node(struct pci_dev *dev)
  27. {
  28. if (!dev->bus->dev.of_node)
  29. return 0;
  30. struct device_node *node __free(device_node) =
  31. of_pci_find_child_device(dev->bus->dev.of_node, dev->devfn);
  32. if (!node)
  33. return 0;
  34. struct device *pdev __free(put_device) =
  35. bus_find_device_by_of_node(&platform_bus_type, node);
  36. if (pdev)
  37. dev->bus->dev.of_node_reused = true;
  38. device_set_node(&dev->dev, of_fwnode_handle(no_free_ptr(node)));
  39. return 0;
  40. }
  41. void pci_release_of_node(struct pci_dev *dev)
  42. {
  43. of_node_put(dev->dev.of_node);
  44. device_set_node(&dev->dev, NULL);
  45. }
  46. void pci_set_bus_of_node(struct pci_bus *bus)
  47. {
  48. struct device_node *node;
  49. if (bus->self == NULL) {
  50. node = pcibios_get_phb_of_node(bus);
  51. } else {
  52. node = of_node_get(bus->self->dev.of_node);
  53. if (node && of_property_read_bool(node, "external-facing"))
  54. bus->self->external_facing = true;
  55. }
  56. device_set_node(&bus->dev, of_fwnode_handle(node));
  57. }
  58. void pci_release_bus_of_node(struct pci_bus *bus)
  59. {
  60. of_node_put(bus->dev.of_node);
  61. device_set_node(&bus->dev, NULL);
  62. }
  63. struct device_node * __weak pcibios_get_phb_of_node(struct pci_bus *bus)
  64. {
  65. /* This should only be called for PHBs */
  66. if (WARN_ON(bus->self || bus->parent))
  67. return NULL;
  68. /*
  69. * Look for a node pointer in either the intermediary device we
  70. * create above the root bus or its own parent. Normally only
  71. * the later is populated.
  72. */
  73. if (bus->bridge->of_node)
  74. return of_node_get(bus->bridge->of_node);
  75. if (bus->bridge->parent && bus->bridge->parent->of_node)
  76. return of_node_get(bus->bridge->parent->of_node);
  77. return NULL;
  78. }
  79. struct irq_domain *pci_host_bridge_of_msi_domain(struct pci_bus *bus)
  80. {
  81. #ifdef CONFIG_IRQ_DOMAIN
  82. struct irq_domain *d;
  83. if (!bus->dev.of_node)
  84. return NULL;
  85. /* Start looking for a phandle to an MSI controller. */
  86. d = of_msi_get_domain(&bus->dev, bus->dev.of_node, DOMAIN_BUS_PCI_MSI);
  87. if (d)
  88. return d;
  89. /*
  90. * If we don't have an msi-parent property, look for a domain
  91. * directly attached to the host bridge.
  92. */
  93. d = irq_find_matching_host(bus->dev.of_node, DOMAIN_BUS_PCI_MSI);
  94. if (d)
  95. return d;
  96. return irq_find_host(bus->dev.of_node);
  97. #else
  98. return NULL;
  99. #endif
  100. }
  101. bool pci_host_of_has_msi_map(struct device *dev)
  102. {
  103. if (dev && dev->of_node)
  104. return of_get_property(dev->of_node, "msi-map", NULL);
  105. return false;
  106. }
  107. static inline int __of_pci_pci_compare(struct device_node *node,
  108. unsigned int data)
  109. {
  110. int devfn;
  111. devfn = of_pci_get_devfn(node);
  112. if (devfn < 0)
  113. return 0;
  114. return devfn == data;
  115. }
  116. struct device_node *of_pci_find_child_device(struct device_node *parent,
  117. unsigned int devfn)
  118. {
  119. struct device_node *node, *node2;
  120. for_each_child_of_node(parent, node) {
  121. if (__of_pci_pci_compare(node, devfn))
  122. return node;
  123. /*
  124. * Some OFs create a parent node "multifunc-device" as
  125. * a fake root for all functions of a multi-function
  126. * device we go down them as well.
  127. */
  128. if (of_node_name_eq(node, "multifunc-device")) {
  129. for_each_child_of_node(node, node2) {
  130. if (__of_pci_pci_compare(node2, devfn)) {
  131. of_node_put(node);
  132. return node2;
  133. }
  134. }
  135. }
  136. }
  137. return NULL;
  138. }
  139. EXPORT_SYMBOL_GPL(of_pci_find_child_device);
  140. /**
  141. * of_pci_get_devfn() - Get device and function numbers for a device node
  142. * @np: device node
  143. *
  144. * Parses a standard 5-cell PCI resource and returns an 8-bit value that can
  145. * be passed to the PCI_SLOT() and PCI_FUNC() macros to extract the device
  146. * and function numbers respectively. On error a negative error code is
  147. * returned.
  148. */
  149. int of_pci_get_devfn(struct device_node *np)
  150. {
  151. u32 reg[5];
  152. int error;
  153. error = of_property_read_u32_array(np, "reg", reg, ARRAY_SIZE(reg));
  154. if (error)
  155. return error;
  156. return (reg[0] >> 8) & 0xff;
  157. }
  158. EXPORT_SYMBOL_GPL(of_pci_get_devfn);
  159. /**
  160. * of_pci_parse_bus_range() - parse the bus-range property of a PCI device
  161. * @node: device node
  162. * @res: address to a struct resource to return the bus-range
  163. *
  164. * Returns 0 on success or a negative error-code on failure.
  165. */
  166. static int of_pci_parse_bus_range(struct device_node *node,
  167. struct resource *res)
  168. {
  169. u32 bus_range[2];
  170. int error;
  171. error = of_property_read_u32_array(node, "bus-range", bus_range,
  172. ARRAY_SIZE(bus_range));
  173. if (error)
  174. return error;
  175. res->name = node->name;
  176. res->start = bus_range[0];
  177. res->end = bus_range[1];
  178. res->flags = IORESOURCE_BUS;
  179. return 0;
  180. }
  181. /**
  182. * of_get_pci_domain_nr - Find the host bridge domain number
  183. * of the given device node.
  184. * @node: Device tree node with the domain information.
  185. *
  186. * This function will try to obtain the host bridge domain number by finding
  187. * a property called "linux,pci-domain" of the given device node.
  188. *
  189. * Return:
  190. * * > 0 - On success, an associated domain number.
  191. * * -EINVAL - The property "linux,pci-domain" does not exist.
  192. * * -ENODATA - The linux,pci-domain" property does not have value.
  193. * * -EOVERFLOW - Invalid "linux,pci-domain" property value.
  194. *
  195. * Returns the associated domain number from DT in the range [0-0xffff], or
  196. * a negative value if the required property is not found.
  197. */
  198. int of_get_pci_domain_nr(struct device_node *node)
  199. {
  200. u32 domain;
  201. int error;
  202. error = of_property_read_u32(node, "linux,pci-domain", &domain);
  203. if (error)
  204. return error;
  205. return (u16)domain;
  206. }
  207. EXPORT_SYMBOL_GPL(of_get_pci_domain_nr);
  208. /**
  209. * of_pci_preserve_config - Return true if the boot configuration needs to
  210. * be preserved
  211. * @node: Device tree node.
  212. *
  213. * Look for "linux,pci-probe-only" property for a given PCI controller's
  214. * node and return true if found. Also look in the chosen node if the
  215. * property is not found in the given controller's node. Having this
  216. * property ensures that the kernel doesn't reconfigure the BARs and bridge
  217. * windows that are already done by the platform firmware.
  218. *
  219. * Return: true if the property exists; false otherwise.
  220. */
  221. bool of_pci_preserve_config(struct device_node *node)
  222. {
  223. u32 val = 0;
  224. int ret;
  225. if (!node) {
  226. pr_warn("device node is NULL, trying with of_chosen\n");
  227. node = of_chosen;
  228. }
  229. retry:
  230. ret = of_property_read_u32(node, "linux,pci-probe-only", &val);
  231. if (ret) {
  232. if (ret == -ENODATA || ret == -EOVERFLOW) {
  233. pr_warn("Incorrect value for linux,pci-probe-only in %pOF, ignoring\n",
  234. node);
  235. return false;
  236. }
  237. if (ret == -EINVAL) {
  238. if (node == of_chosen)
  239. return false;
  240. node = of_chosen;
  241. goto retry;
  242. }
  243. }
  244. if (val)
  245. return true;
  246. else
  247. return false;
  248. }
  249. /**
  250. * of_pci_check_probe_only - Setup probe only mode if linux,pci-probe-only
  251. * is present and valid
  252. */
  253. void of_pci_check_probe_only(void)
  254. {
  255. if (of_pci_preserve_config(of_chosen))
  256. pci_add_flags(PCI_PROBE_ONLY);
  257. else
  258. pci_clear_flags(PCI_PROBE_ONLY);
  259. }
  260. EXPORT_SYMBOL_GPL(of_pci_check_probe_only);
  261. /**
  262. * devm_of_pci_get_host_bridge_resources() - Resource-managed parsing of PCI
  263. * host bridge resources from DT
  264. * @dev: host bridge device
  265. * @resources: list where the range of resources will be added after DT parsing
  266. * @ib_resources: list where the range of inbound resources (with addresses
  267. * from 'dma-ranges') will be added after DT parsing
  268. * @io_base: pointer to a variable that will contain on return the physical
  269. * address for the start of the I/O range. Can be NULL if the caller doesn't
  270. * expect I/O ranges to be present in the device tree.
  271. *
  272. * This function will parse the "ranges" property of a PCI host bridge device
  273. * node and setup the resource mapping based on its content. It is expected
  274. * that the property conforms with the Power ePAPR document.
  275. *
  276. * It returns zero if the range parsing has been successful or a standard error
  277. * value if it failed.
  278. */
  279. static int devm_of_pci_get_host_bridge_resources(struct device *dev,
  280. struct list_head *resources,
  281. struct list_head *ib_resources,
  282. resource_size_t *io_base)
  283. {
  284. struct device_node *dev_node = dev->of_node;
  285. struct resource *res, tmp_res;
  286. struct resource *bus_range;
  287. struct of_pci_range range;
  288. struct of_pci_range_parser parser;
  289. const char *range_type;
  290. int err;
  291. if (io_base)
  292. *io_base = (resource_size_t)OF_BAD_ADDR;
  293. bus_range = devm_kzalloc(dev, sizeof(*bus_range), GFP_KERNEL);
  294. if (!bus_range)
  295. return -ENOMEM;
  296. dev_info(dev, "host bridge %pOF ranges:\n", dev_node);
  297. err = of_pci_parse_bus_range(dev_node, bus_range);
  298. if (err) {
  299. bus_range->start = 0;
  300. bus_range->end = 0xff;
  301. bus_range->flags = IORESOURCE_BUS;
  302. } else {
  303. if (bus_range->end > 0xff) {
  304. dev_warn(dev, " Invalid end bus number in %pR, defaulting to 0xff\n",
  305. bus_range);
  306. bus_range->end = 0xff;
  307. }
  308. }
  309. pci_add_resource(resources, bus_range);
  310. /* Check for ranges property */
  311. err = of_pci_range_parser_init(&parser, dev_node);
  312. if (err)
  313. return 0;
  314. dev_dbg(dev, "Parsing ranges property...\n");
  315. for_each_of_pci_range(&parser, &range) {
  316. /* Read next ranges element */
  317. if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_IO)
  318. range_type = "IO";
  319. else if ((range.flags & IORESOURCE_TYPE_BITS) == IORESOURCE_MEM)
  320. range_type = "MEM";
  321. else
  322. range_type = "err";
  323. dev_info(dev, " %6s %#012llx..%#012llx -> %#012llx\n",
  324. range_type, range.cpu_addr,
  325. range.cpu_addr + range.size - 1, range.pci_addr);
  326. /*
  327. * If we failed translation or got a zero-sized region
  328. * then skip this range
  329. */
  330. if (range.cpu_addr == OF_BAD_ADDR || range.size == 0)
  331. continue;
  332. err = of_pci_range_to_resource(&range, dev_node, &tmp_res);
  333. if (err)
  334. continue;
  335. res = devm_kmemdup(dev, &tmp_res, sizeof(tmp_res), GFP_KERNEL);
  336. if (!res) {
  337. err = -ENOMEM;
  338. goto failed;
  339. }
  340. if (resource_type(res) == IORESOURCE_IO) {
  341. if (!io_base) {
  342. dev_err(dev, "I/O range found for %pOF. Please provide an io_base pointer to save CPU base address\n",
  343. dev_node);
  344. err = -EINVAL;
  345. goto failed;
  346. }
  347. if (*io_base != (resource_size_t)OF_BAD_ADDR)
  348. dev_warn(dev, "More than one I/O resource converted for %pOF. CPU base address for old range lost!\n",
  349. dev_node);
  350. *io_base = range.cpu_addr;
  351. } else if (resource_type(res) == IORESOURCE_MEM) {
  352. res->flags &= ~IORESOURCE_MEM_64;
  353. }
  354. pci_add_resource_offset(resources, res, res->start - range.pci_addr);
  355. }
  356. /* Check for dma-ranges property */
  357. if (!ib_resources)
  358. return 0;
  359. err = of_pci_dma_range_parser_init(&parser, dev_node);
  360. if (err)
  361. return 0;
  362. dev_dbg(dev, "Parsing dma-ranges property...\n");
  363. for_each_of_pci_range(&parser, &range) {
  364. /*
  365. * If we failed translation or got a zero-sized region
  366. * then skip this range
  367. */
  368. if (((range.flags & IORESOURCE_TYPE_BITS) != IORESOURCE_MEM) ||
  369. range.cpu_addr == OF_BAD_ADDR || range.size == 0)
  370. continue;
  371. dev_info(dev, " %6s %#012llx..%#012llx -> %#012llx\n",
  372. "IB MEM", range.cpu_addr,
  373. range.cpu_addr + range.size - 1, range.pci_addr);
  374. err = of_pci_range_to_resource(&range, dev_node, &tmp_res);
  375. if (err)
  376. continue;
  377. res = devm_kmemdup(dev, &tmp_res, sizeof(tmp_res), GFP_KERNEL);
  378. if (!res) {
  379. err = -ENOMEM;
  380. goto failed;
  381. }
  382. pci_add_resource_offset(ib_resources, res,
  383. res->start - range.pci_addr);
  384. }
  385. return 0;
  386. failed:
  387. pci_free_resource_list(resources);
  388. return err;
  389. }
  390. #if IS_ENABLED(CONFIG_OF_IRQ)
  391. /**
  392. * of_irq_parse_pci - Resolve the interrupt for a PCI device
  393. * @pdev: the device whose interrupt is to be resolved
  394. * @out_irq: structure of_phandle_args filled by this function
  395. *
  396. * This function resolves the PCI interrupt for a given PCI device. If a
  397. * device node exists for a given pci_dev, it will use normal OF tree
  398. * walking. If not, it will implement standard swizzling and walk up the
  399. * PCI tree until a device node is found, at which point it will finish
  400. * resolving using the OF tree walking.
  401. */
  402. static int of_irq_parse_pci(const struct pci_dev *pdev, struct of_phandle_args *out_irq)
  403. {
  404. struct device_node *dn, *ppnode = NULL;
  405. struct pci_dev *ppdev;
  406. __be32 laddr[3];
  407. u8 pin;
  408. int rc;
  409. /*
  410. * Check if we have a device node, if yes, fallback to standard
  411. * device tree parsing
  412. */
  413. dn = pci_device_to_OF_node(pdev);
  414. if (dn) {
  415. rc = of_irq_parse_one(dn, 0, out_irq);
  416. if (!rc)
  417. return rc;
  418. }
  419. /*
  420. * Ok, we don't, time to have fun. Let's start by building up an
  421. * interrupt spec. we assume #interrupt-cells is 1, which is standard
  422. * for PCI. If you do different, then don't use that routine.
  423. */
  424. rc = pci_read_config_byte(pdev, PCI_INTERRUPT_PIN, &pin);
  425. if (rc != 0)
  426. goto err;
  427. /* No pin, exit with no error message. */
  428. if (pin == 0)
  429. return -ENODEV;
  430. /* Local interrupt-map in the device node? Use it! */
  431. if (of_property_present(dn, "interrupt-map")) {
  432. pin = pci_swizzle_interrupt_pin(pdev, pin);
  433. ppnode = dn;
  434. }
  435. /* Now we walk up the PCI tree */
  436. while (!ppnode) {
  437. /* Get the pci_dev of our parent */
  438. ppdev = pdev->bus->self;
  439. /* Ouch, it's a host bridge... */
  440. if (ppdev == NULL) {
  441. ppnode = pci_bus_to_OF_node(pdev->bus);
  442. /* No node for host bridge ? give up */
  443. if (ppnode == NULL) {
  444. rc = -EINVAL;
  445. goto err;
  446. }
  447. } else {
  448. /* We found a P2P bridge, check if it has a node */
  449. ppnode = pci_device_to_OF_node(ppdev);
  450. }
  451. /*
  452. * Ok, we have found a parent with a device node, hand over to
  453. * the OF parsing code.
  454. *
  455. * We build a unit address from the linux device to be used for
  456. * resolution. Note that we use the linux bus number which may
  457. * not match your firmware bus numbering.
  458. *
  459. * Fortunately, in most cases, interrupt-map-mask doesn't
  460. * include the bus number as part of the matching.
  461. *
  462. * You should still be careful about that though if you intend
  463. * to rely on this function (you ship a firmware that doesn't
  464. * create device nodes for all PCI devices).
  465. */
  466. if (ppnode)
  467. break;
  468. /*
  469. * We can only get here if we hit a P2P bridge with no node;
  470. * let's do standard swizzling and try again
  471. */
  472. pin = pci_swizzle_interrupt_pin(pdev, pin);
  473. pdev = ppdev;
  474. }
  475. out_irq->np = ppnode;
  476. out_irq->args_count = 1;
  477. out_irq->args[0] = pin;
  478. laddr[0] = cpu_to_be32((pdev->bus->number << 16) | (pdev->devfn << 8));
  479. laddr[1] = laddr[2] = cpu_to_be32(0);
  480. rc = of_irq_parse_raw(laddr, out_irq);
  481. if (rc)
  482. goto err;
  483. return 0;
  484. err:
  485. if (rc == -ENOENT) {
  486. dev_warn(&pdev->dev,
  487. "%s: no interrupt-map found, INTx interrupts not available\n",
  488. __func__);
  489. pr_warn_once("%s: possibly some PCI slots don't have level triggered interrupts capability\n",
  490. __func__);
  491. } else {
  492. dev_err(&pdev->dev, "%s: failed with rc=%d\n", __func__, rc);
  493. }
  494. return rc;
  495. }
  496. /**
  497. * of_irq_parse_and_map_pci() - Decode a PCI IRQ from the device tree and map to a VIRQ
  498. * @dev: The PCI device needing an IRQ
  499. * @slot: PCI slot number; passed when used as map_irq callback. Unused
  500. * @pin: PCI IRQ pin number; passed when used as map_irq callback. Unused
  501. *
  502. * @slot and @pin are unused, but included in the function so that this
  503. * function can be used directly as the map_irq callback to
  504. * pci_assign_irq() and struct pci_host_bridge.map_irq pointer
  505. */
  506. int of_irq_parse_and_map_pci(const struct pci_dev *dev, u8 slot, u8 pin)
  507. {
  508. struct of_phandle_args oirq;
  509. int ret;
  510. ret = of_irq_parse_pci(dev, &oirq);
  511. if (ret)
  512. return 0; /* Proper return code 0 == NO_IRQ */
  513. return irq_create_of_mapping(&oirq);
  514. }
  515. EXPORT_SYMBOL_GPL(of_irq_parse_and_map_pci);
  516. #endif /* CONFIG_OF_IRQ */
  517. static int pci_parse_request_of_pci_ranges(struct device *dev,
  518. struct pci_host_bridge *bridge)
  519. {
  520. int err, res_valid = 0;
  521. resource_size_t iobase;
  522. struct resource_entry *win, *tmp;
  523. INIT_LIST_HEAD(&bridge->windows);
  524. INIT_LIST_HEAD(&bridge->dma_ranges);
  525. err = devm_of_pci_get_host_bridge_resources(dev, &bridge->windows,
  526. &bridge->dma_ranges, &iobase);
  527. if (err)
  528. return err;
  529. err = devm_request_pci_bus_resources(dev, &bridge->windows);
  530. if (err)
  531. return err;
  532. resource_list_for_each_entry_safe(win, tmp, &bridge->windows) {
  533. struct resource *res = win->res;
  534. switch (resource_type(res)) {
  535. case IORESOURCE_IO:
  536. err = devm_pci_remap_iospace(dev, res, iobase);
  537. if (err) {
  538. dev_warn(dev, "error %d: failed to map resource %pR\n",
  539. err, res);
  540. resource_list_destroy_entry(win);
  541. }
  542. break;
  543. case IORESOURCE_MEM:
  544. res_valid |= !(res->flags & IORESOURCE_PREFETCH);
  545. if (!(res->flags & IORESOURCE_PREFETCH))
  546. if (upper_32_bits(resource_size(res)))
  547. dev_warn(dev, "Memory resource size exceeds max for 32 bits\n");
  548. break;
  549. }
  550. }
  551. if (!res_valid)
  552. dev_warn(dev, "non-prefetchable memory resource required\n");
  553. return 0;
  554. }
  555. int devm_of_pci_bridge_init(struct device *dev, struct pci_host_bridge *bridge)
  556. {
  557. if (!dev->of_node)
  558. return 0;
  559. bridge->swizzle_irq = pci_common_swizzle;
  560. bridge->map_irq = of_irq_parse_and_map_pci;
  561. return pci_parse_request_of_pci_ranges(dev, bridge);
  562. }
  563. #ifdef CONFIG_PCI_DYNAMIC_OF_NODES
  564. void of_pci_remove_node(struct pci_dev *pdev)
  565. {
  566. struct device_node *np;
  567. np = pci_device_to_OF_node(pdev);
  568. if (!np || !of_node_check_flag(np, OF_DYNAMIC))
  569. return;
  570. device_remove_of_node(&pdev->dev);
  571. of_changeset_revert(np->data);
  572. of_changeset_destroy(np->data);
  573. of_node_put(np);
  574. }
  575. void of_pci_make_dev_node(struct pci_dev *pdev)
  576. {
  577. struct device_node *ppnode, *np = NULL;
  578. const char *pci_type;
  579. struct of_changeset *cset;
  580. const char *name;
  581. int ret;
  582. /*
  583. * If there is already a device tree node linked to this device,
  584. * return immediately.
  585. */
  586. if (pci_device_to_OF_node(pdev))
  587. return;
  588. /* Check if there is device tree node for parent device */
  589. if (!pdev->bus->self)
  590. ppnode = pdev->bus->dev.of_node;
  591. else
  592. ppnode = pdev->bus->self->dev.of_node;
  593. if (!ppnode)
  594. return;
  595. if (pci_is_bridge(pdev))
  596. pci_type = "pci";
  597. else
  598. pci_type = "dev";
  599. name = kasprintf(GFP_KERNEL, "%s@%x,%x", pci_type,
  600. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  601. if (!name)
  602. return;
  603. cset = kmalloc_obj(*cset);
  604. if (!cset)
  605. goto out_free_name;
  606. of_changeset_init(cset);
  607. np = of_changeset_create_node(cset, ppnode, name);
  608. if (!np)
  609. goto out_destroy_cset;
  610. ret = of_pci_add_properties(pdev, cset, np);
  611. if (ret)
  612. goto out_free_node;
  613. ret = of_changeset_apply(cset);
  614. if (ret)
  615. goto out_free_node;
  616. np->data = cset;
  617. ret = device_add_of_node(&pdev->dev, np);
  618. if (ret)
  619. goto out_revert_cset;
  620. kfree(name);
  621. return;
  622. out_revert_cset:
  623. np->data = NULL;
  624. of_changeset_revert(cset);
  625. out_free_node:
  626. of_node_put(np);
  627. out_destroy_cset:
  628. of_changeset_destroy(cset);
  629. kfree(cset);
  630. out_free_name:
  631. kfree(name);
  632. }
  633. void of_pci_remove_host_bridge_node(struct pci_host_bridge *bridge)
  634. {
  635. struct device_node *np;
  636. np = pci_bus_to_OF_node(bridge->bus);
  637. if (!np || !of_node_check_flag(np, OF_DYNAMIC))
  638. return;
  639. device_remove_of_node(&bridge->bus->dev);
  640. device_remove_of_node(&bridge->dev);
  641. of_changeset_revert(np->data);
  642. of_changeset_destroy(np->data);
  643. of_node_put(np);
  644. }
  645. void of_pci_make_host_bridge_node(struct pci_host_bridge *bridge)
  646. {
  647. struct device_node *np = NULL;
  648. struct of_changeset *cset;
  649. const char *name;
  650. int ret;
  651. /*
  652. * If there is already a device tree node linked to the PCI bus handled
  653. * by this bridge (i.e. the PCI root bus), nothing to do.
  654. */
  655. if (pci_bus_to_OF_node(bridge->bus))
  656. return;
  657. /*
  658. * The root bus has no node. Check that the host bridge has no node
  659. * too
  660. */
  661. if (bridge->dev.of_node) {
  662. dev_err(&bridge->dev, "PCI host bridge of_node already set");
  663. return;
  664. }
  665. /* Check if there is a DT root node to attach the created node */
  666. if (!of_root) {
  667. pr_err("of_root node is NULL, cannot create PCI host bridge node\n");
  668. return;
  669. }
  670. name = kasprintf(GFP_KERNEL, "pci@%x,%x", pci_domain_nr(bridge->bus),
  671. bridge->bus->number);
  672. if (!name)
  673. return;
  674. cset = kmalloc_obj(*cset);
  675. if (!cset)
  676. goto out_free_name;
  677. of_changeset_init(cset);
  678. np = of_changeset_create_node(cset, of_root, name);
  679. if (!np)
  680. goto out_destroy_cset;
  681. ret = of_pci_add_host_bridge_properties(bridge, cset, np);
  682. if (ret)
  683. goto out_free_node;
  684. /*
  685. * This of_node will be added to an existing device. The of_node parent
  686. * is the root OF node and so this node will be handled by the platform
  687. * bus. Avoid any new device creation.
  688. */
  689. of_node_set_flag(np, OF_POPULATED);
  690. np->fwnode.dev = &bridge->dev;
  691. fwnode_dev_initialized(&np->fwnode, true);
  692. ret = of_changeset_apply(cset);
  693. if (ret)
  694. goto out_free_node;
  695. np->data = cset;
  696. /* Add the of_node to host bridge and the root bus */
  697. ret = device_add_of_node(&bridge->dev, np);
  698. if (ret)
  699. goto out_revert_cset;
  700. ret = device_add_of_node(&bridge->bus->dev, np);
  701. if (ret)
  702. goto out_remove_bridge_dev_of_node;
  703. kfree(name);
  704. return;
  705. out_remove_bridge_dev_of_node:
  706. device_remove_of_node(&bridge->dev);
  707. out_revert_cset:
  708. np->data = NULL;
  709. of_changeset_revert(cset);
  710. out_free_node:
  711. of_node_put(np);
  712. out_destroy_cset:
  713. of_changeset_destroy(cset);
  714. kfree(cset);
  715. out_free_name:
  716. kfree(name);
  717. }
  718. #endif /* CONFIG_PCI_DYNAMIC_OF_NODES */
  719. /**
  720. * of_pci_supply_present() - Check if the power supply is present for the PCI
  721. * device
  722. * @np: Device tree node
  723. *
  724. * Check if the power supply for the PCI device is present in the device tree
  725. * node or not.
  726. *
  727. * Return: true if at least one power supply exists; false otherwise.
  728. */
  729. bool of_pci_supply_present(struct device_node *np)
  730. {
  731. struct property *prop;
  732. char *supply;
  733. if (!np)
  734. return false;
  735. for_each_property_of_node(np, prop) {
  736. supply = strrchr(prop->name, '-');
  737. if (supply && !strcmp(supply, "-supply"))
  738. return true;
  739. }
  740. return false;
  741. }
  742. EXPORT_SYMBOL_GPL(of_pci_supply_present);
  743. #endif /* CONFIG_PCI */
  744. /**
  745. * of_pci_get_max_link_speed - Find the maximum link speed of the given device node.
  746. * @node: Device tree node with the maximum link speed information.
  747. *
  748. * This function will try to find the limitation of link speed by finding
  749. * a property called "max-link-speed" of the given device node.
  750. *
  751. * Return:
  752. * * > 0 - On success, a maximum link speed.
  753. * * -EINVAL - Invalid "max-link-speed" property value, or failure to access
  754. * the property of the device tree node.
  755. *
  756. * Returns the associated max link speed from DT, or a negative value if the
  757. * required property is not found or is invalid.
  758. */
  759. int of_pci_get_max_link_speed(struct device_node *node)
  760. {
  761. u32 max_link_speed;
  762. if (of_property_read_u32(node, "max-link-speed", &max_link_speed) ||
  763. max_link_speed == 0 || max_link_speed > 4)
  764. return -EINVAL;
  765. return max_link_speed;
  766. }
  767. EXPORT_SYMBOL_GPL(of_pci_get_max_link_speed);
  768. /**
  769. * of_pci_get_slot_power_limit - Parses the "slot-power-limit-milliwatt"
  770. * property.
  771. *
  772. * @node: device tree node with the slot power limit information
  773. * @slot_power_limit_value: pointer where the value should be stored in PCIe
  774. * Slot Capabilities Register format
  775. * @slot_power_limit_scale: pointer where the scale should be stored in PCIe
  776. * Slot Capabilities Register format
  777. *
  778. * Returns the slot power limit in milliwatts and if @slot_power_limit_value
  779. * and @slot_power_limit_scale pointers are non-NULL, fills in the value and
  780. * scale in format used by PCIe Slot Capabilities Register.
  781. *
  782. * If the property is not found or is invalid, returns 0.
  783. */
  784. u32 of_pci_get_slot_power_limit(struct device_node *node,
  785. u8 *slot_power_limit_value,
  786. u8 *slot_power_limit_scale)
  787. {
  788. u32 slot_power_limit_mw;
  789. u8 value, scale;
  790. if (of_property_read_u32(node, "slot-power-limit-milliwatt",
  791. &slot_power_limit_mw))
  792. slot_power_limit_mw = 0;
  793. /* Calculate Slot Power Limit Value and Slot Power Limit Scale */
  794. if (slot_power_limit_mw == 0) {
  795. value = 0x00;
  796. scale = 0;
  797. } else if (slot_power_limit_mw <= 255) {
  798. value = slot_power_limit_mw;
  799. scale = 3;
  800. } else if (slot_power_limit_mw <= 255*10) {
  801. value = slot_power_limit_mw / 10;
  802. scale = 2;
  803. slot_power_limit_mw = slot_power_limit_mw / 10 * 10;
  804. } else if (slot_power_limit_mw <= 255*100) {
  805. value = slot_power_limit_mw / 100;
  806. scale = 1;
  807. slot_power_limit_mw = slot_power_limit_mw / 100 * 100;
  808. } else if (slot_power_limit_mw <= 239*1000) {
  809. value = slot_power_limit_mw / 1000;
  810. scale = 0;
  811. slot_power_limit_mw = slot_power_limit_mw / 1000 * 1000;
  812. } else if (slot_power_limit_mw < 250*1000) {
  813. value = 0xEF;
  814. scale = 0;
  815. slot_power_limit_mw = 239*1000;
  816. } else if (slot_power_limit_mw <= 600*1000) {
  817. value = 0xF0 + (slot_power_limit_mw / 1000 - 250) / 25;
  818. scale = 0;
  819. slot_power_limit_mw = slot_power_limit_mw / (1000*25) * (1000*25);
  820. } else {
  821. value = 0xFE;
  822. scale = 0;
  823. slot_power_limit_mw = 600*1000;
  824. }
  825. if (slot_power_limit_value)
  826. *slot_power_limit_value = value;
  827. if (slot_power_limit_scale)
  828. *slot_power_limit_scale = scale;
  829. return slot_power_limit_mw;
  830. }
  831. EXPORT_SYMBOL_GPL(of_pci_get_slot_power_limit);
  832. /**
  833. * of_pci_get_equalization_presets - Parses the "eq-presets-Ngts" property.
  834. *
  835. * @dev: Device containing the properties.
  836. * @presets: Pointer to store the parsed data.
  837. * @num_lanes: Maximum number of lanes supported.
  838. *
  839. * If the property is present, read and store the data in the @presets structure.
  840. * Else, assign a default value of PCI_EQ_RESV.
  841. *
  842. * Return: 0 if the property is not available or successfully parsed else
  843. * errno otherwise.
  844. */
  845. int of_pci_get_equalization_presets(struct device *dev,
  846. struct pci_eq_presets *presets,
  847. int num_lanes)
  848. {
  849. char name[20];
  850. int ret;
  851. presets->eq_presets_8gts[0] = PCI_EQ_RESV;
  852. ret = of_property_read_u16_array(dev->of_node, "eq-presets-8gts",
  853. presets->eq_presets_8gts, num_lanes);
  854. if (ret && ret != -EINVAL) {
  855. dev_err(dev, "Error reading eq-presets-8gts: %d\n", ret);
  856. return ret;
  857. }
  858. for (int i = 0; i < EQ_PRESET_TYPE_MAX - 1; i++) {
  859. presets->eq_presets_Ngts[i][0] = PCI_EQ_RESV;
  860. snprintf(name, sizeof(name), "eq-presets-%dgts", 8 << (i + 1));
  861. ret = of_property_read_u8_array(dev->of_node, name,
  862. presets->eq_presets_Ngts[i],
  863. num_lanes);
  864. if (ret && ret != -EINVAL) {
  865. dev_err(dev, "Error reading %s: %d\n", name, ret);
  866. return ret;
  867. }
  868. }
  869. return 0;
  870. }
  871. EXPORT_SYMBOL_GPL(of_pci_get_equalization_presets);