pci_of_scan.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Helper routines to scan the device tree for PCI devices and busses
  4. *
  5. * Migrated out of PowerPC architecture pci_64.c file by Grant Likely
  6. * <[email protected]> so that these routines are available for
  7. * 32 bit also.
  8. *
  9. * Copyright (C) 2003 Anton Blanchard <[email protected]>, IBM
  10. * Rework, based on alpha PCI code.
  11. * Copyright (c) 2009 Secret Lab Technologies Ltd.
  12. */
  13. #include <linux/pci.h>
  14. #include <linux/export.h>
  15. #include <linux/of.h>
  16. #include <asm/pci-bridge.h>
  17. /**
  18. * get_int_prop - Decode a u32 from a device tree property
  19. */
  20. static u32 get_int_prop(struct device_node *np, const char *name, u32 def)
  21. {
  22. const __be32 *prop;
  23. int len;
  24. prop = of_get_property(np, name, &len);
  25. if (prop && len >= 4)
  26. return of_read_number(prop, 1);
  27. return def;
  28. }
  29. /**
  30. * pci_parse_of_flags - Parse the flags cell of a device tree PCI address
  31. * @addr0: value of 1st cell of a device tree PCI address.
  32. * @bridge: Set this flag if the address is from a bridge 'ranges' property
  33. *
  34. * PCI Bus Binding to IEEE Std 1275-1994
  35. *
  36. * Bit# 33222222 22221111 11111100 00000000
  37. * 10987654 32109876 54321098 76543210
  38. * phys.hi cell: npt000ss bbbbbbbb dddddfff rrrrrrrr
  39. * phys.mid cell: hhhhhhhh hhhhhhhh hhhhhhhh hhhhhhhh
  40. * phys.lo cell: llllllll llllllll llllllll llllllll
  41. *
  42. * where:
  43. * n is 0 if the address is relocatable, 1 otherwise
  44. * p is 1 if the addressable region is "prefetchable", 0 otherwise
  45. * t is 1 if the address is aliased (for non-relocatable I/O),
  46. * below 1 MB (for Memory),or below 64 KB (for relocatable I/O).
  47. * ss is the space code, denoting the address space:
  48. * 00 denotes Configuration Space
  49. * 01 denotes I/O Space
  50. * 10 denotes 32-bit-address Memory Space
  51. * 11 denotes 64-bit-address Memory Space
  52. * bbbbbbbb is the 8-bit Bus Number
  53. * ddddd is the 5-bit Device Number
  54. * fff is the 3-bit Function Number
  55. * rrrrrrrr is the 8-bit Register Number
  56. */
  57. #define OF_PCI_ADDR0_SPACE(ss) (((ss)&3)<<24)
  58. #define OF_PCI_ADDR0_SPACE_CFG OF_PCI_ADDR0_SPACE(0)
  59. #define OF_PCI_ADDR0_SPACE_IO OF_PCI_ADDR0_SPACE(1)
  60. #define OF_PCI_ADDR0_SPACE_MMIO32 OF_PCI_ADDR0_SPACE(2)
  61. #define OF_PCI_ADDR0_SPACE_MMIO64 OF_PCI_ADDR0_SPACE(3)
  62. #define OF_PCI_ADDR0_SPACE_MASK OF_PCI_ADDR0_SPACE(3)
  63. #define OF_PCI_ADDR0_RELOC (1UL<<31)
  64. #define OF_PCI_ADDR0_PREFETCH (1UL<<30)
  65. #define OF_PCI_ADDR0_ALIAS (1UL<<29)
  66. #define OF_PCI_ADDR0_BUS 0x00FF0000UL
  67. #define OF_PCI_ADDR0_DEV 0x0000F800UL
  68. #define OF_PCI_ADDR0_FN 0x00000700UL
  69. #define OF_PCI_ADDR0_BARREG 0x000000FFUL
  70. unsigned int pci_parse_of_flags(u32 addr0, int bridge)
  71. {
  72. unsigned int flags = 0, as = addr0 & OF_PCI_ADDR0_SPACE_MASK;
  73. if (as == OF_PCI_ADDR0_SPACE_MMIO32 || as == OF_PCI_ADDR0_SPACE_MMIO64) {
  74. flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
  75. if (as == OF_PCI_ADDR0_SPACE_MMIO64)
  76. flags |= PCI_BASE_ADDRESS_MEM_TYPE_64 | IORESOURCE_MEM_64;
  77. if (addr0 & OF_PCI_ADDR0_ALIAS)
  78. flags |= PCI_BASE_ADDRESS_MEM_TYPE_1M;
  79. if (addr0 & OF_PCI_ADDR0_PREFETCH)
  80. flags |= IORESOURCE_PREFETCH |
  81. PCI_BASE_ADDRESS_MEM_PREFETCH;
  82. /* Note: We don't know whether the ROM has been left enabled
  83. * by the firmware or not. We mark it as disabled (ie, we do
  84. * not set the IORESOURCE_ROM_ENABLE flag) for now rather than
  85. * do a config space read, it will be force-enabled if needed
  86. */
  87. if (!bridge && (addr0 & OF_PCI_ADDR0_BARREG) == PCI_ROM_ADDRESS)
  88. flags |= IORESOURCE_READONLY;
  89. } else if (as == OF_PCI_ADDR0_SPACE_IO)
  90. flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
  91. if (flags)
  92. flags |= IORESOURCE_SIZEALIGN;
  93. return flags;
  94. }
  95. /**
  96. * of_pci_parse_addrs - Parse PCI addresses assigned in the device tree node
  97. * @node: device tree node for the PCI device
  98. * @dev: pci_dev structure for the device
  99. *
  100. * This function parses the 'assigned-addresses' property of a PCI devices'
  101. * device tree node and writes them into the associated pci_dev structure.
  102. */
  103. static void of_pci_parse_addrs(struct device_node *node, struct pci_dev *dev)
  104. {
  105. u64 base, size;
  106. unsigned int flags;
  107. struct pci_bus_region region;
  108. struct resource *res;
  109. const __be32 *addrs;
  110. u32 i;
  111. int proplen;
  112. bool mark_unset = false;
  113. addrs = of_get_property(node, "assigned-addresses", &proplen);
  114. if (!addrs || !proplen) {
  115. addrs = of_get_property(node, "reg", &proplen);
  116. if (!addrs || !proplen)
  117. return;
  118. mark_unset = true;
  119. }
  120. pr_debug(" parse addresses (%d bytes) @ %p\n", proplen, addrs);
  121. for (; proplen >= 20; proplen -= 20, addrs += 5) {
  122. flags = pci_parse_of_flags(of_read_number(addrs, 1), 0);
  123. if (!flags)
  124. continue;
  125. base = of_read_number(&addrs[1], 2);
  126. size = of_read_number(&addrs[3], 2);
  127. if (!size)
  128. continue;
  129. i = of_read_number(addrs, 1) & 0xff;
  130. pr_debug(" base: %llx, size: %llx, i: %x\n",
  131. (unsigned long long)base,
  132. (unsigned long long)size, i);
  133. if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
  134. res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
  135. } else if (i == dev->rom_base_reg) {
  136. res = &dev->resource[PCI_ROM_RESOURCE];
  137. flags |= IORESOURCE_READONLY;
  138. } else {
  139. printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
  140. continue;
  141. }
  142. res->flags = flags;
  143. if (mark_unset)
  144. res->flags |= IORESOURCE_UNSET;
  145. res->name = pci_name(dev);
  146. region.start = base;
  147. region.end = base + size - 1;
  148. pcibios_bus_to_resource(dev->bus, res, &region);
  149. }
  150. }
  151. /**
  152. * of_create_pci_dev - Given a device tree node on a pci bus, create a pci_dev
  153. * @node: device tree node pointer
  154. * @bus: bus the device is sitting on
  155. * @devfn: PCI function number, extracted from device tree by caller.
  156. */
  157. struct pci_dev *of_create_pci_dev(struct device_node *node,
  158. struct pci_bus *bus, int devfn)
  159. {
  160. struct pci_dev *dev;
  161. dev = pci_alloc_dev(bus);
  162. if (!dev)
  163. return NULL;
  164. pr_debug(" create device, devfn: %x, type: %s\n", devfn,
  165. of_node_get_device_type(node));
  166. dev->dev.of_node = of_node_get(node);
  167. dev->dev.parent = bus->bridge;
  168. dev->dev.bus = &pci_bus_type;
  169. dev->devfn = devfn;
  170. dev->multifunction = 0; /* maybe a lie? */
  171. dev->needs_freset = 0; /* pcie fundamental reset required */
  172. set_pcie_port_type(dev);
  173. pci_dev_assign_slot(dev);
  174. dev->vendor = get_int_prop(node, "vendor-id", 0xffff);
  175. dev->device = get_int_prop(node, "device-id", 0xffff);
  176. dev->subsystem_vendor = get_int_prop(node, "subsystem-vendor-id", 0);
  177. dev->subsystem_device = get_int_prop(node, "subsystem-id", 0);
  178. dev->cfg_size = pci_cfg_space_size(dev);
  179. dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
  180. dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
  181. dev->class = get_int_prop(node, "class-code", 0);
  182. dev->revision = get_int_prop(node, "revision-id", 0);
  183. pr_debug(" class: 0x%x\n", dev->class);
  184. pr_debug(" revision: 0x%x\n", dev->revision);
  185. dev->current_state = PCI_UNKNOWN; /* unknown power state */
  186. dev->error_state = pci_channel_io_normal;
  187. dev->dma_mask = 0xffffffff;
  188. /* Early fixups, before probing the BARs */
  189. pci_fixup_device(pci_fixup_early, dev);
  190. if (of_node_is_type(node, "pci") || of_node_is_type(node, "pciex")) {
  191. /* a PCI-PCI bridge */
  192. dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
  193. dev->rom_base_reg = PCI_ROM_ADDRESS1;
  194. set_pcie_hotplug_bridge(dev);
  195. } else if (of_node_is_type(node, "cardbus")) {
  196. dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
  197. } else {
  198. dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
  199. dev->rom_base_reg = PCI_ROM_ADDRESS;
  200. /* Maybe do a default OF mapping here */
  201. dev->irq = 0;
  202. }
  203. of_pci_parse_addrs(node, dev);
  204. pr_debug(" adding to system ...\n");
  205. pci_device_add(dev, bus);
  206. return dev;
  207. }
  208. EXPORT_SYMBOL(of_create_pci_dev);
  209. /**
  210. * of_scan_pci_bridge - Set up a PCI bridge and scan for child nodes
  211. * @dev: pci_dev structure for the bridge
  212. *
  213. * of_scan_bus() calls this routine for each PCI bridge that it finds, and
  214. * this routine in turn call of_scan_bus() recursively to scan for more child
  215. * devices.
  216. */
  217. void of_scan_pci_bridge(struct pci_dev *dev)
  218. {
  219. struct device_node *node = dev->dev.of_node;
  220. struct pci_bus *bus;
  221. struct pci_controller *phb;
  222. const __be32 *busrange, *ranges;
  223. int len, i, mode;
  224. struct pci_bus_region region;
  225. struct resource *res;
  226. unsigned int flags;
  227. u64 size;
  228. pr_debug("of_scan_pci_bridge(%pOF)\n", node);
  229. /* parse bus-range property */
  230. busrange = of_get_property(node, "bus-range", &len);
  231. if (busrange == NULL || len != 8) {
  232. printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %pOF\n",
  233. node);
  234. return;
  235. }
  236. ranges = of_get_property(node, "ranges", &len);
  237. if (ranges == NULL) {
  238. printk(KERN_DEBUG "Can't get ranges for PCI-PCI bridge %pOF\n",
  239. node);
  240. return;
  241. }
  242. bus = pci_find_bus(pci_domain_nr(dev->bus),
  243. of_read_number(busrange, 1));
  244. if (!bus) {
  245. bus = pci_add_new_bus(dev->bus, dev,
  246. of_read_number(busrange, 1));
  247. if (!bus) {
  248. printk(KERN_ERR "Failed to create pci bus for %pOF\n",
  249. node);
  250. return;
  251. }
  252. }
  253. bus->primary = dev->bus->number;
  254. pci_bus_insert_busn_res(bus, of_read_number(busrange, 1),
  255. of_read_number(busrange+1, 1));
  256. bus->bridge_ctl = 0;
  257. /* parse ranges property */
  258. /* PCI #address-cells == 3 and #size-cells == 2 always */
  259. res = &dev->resource[PCI_BRIDGE_RESOURCES];
  260. for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
  261. res->flags = 0;
  262. bus->resource[i] = res;
  263. ++res;
  264. }
  265. i = 1;
  266. for (; len >= 32; len -= 32, ranges += 8) {
  267. flags = pci_parse_of_flags(of_read_number(ranges, 1), 1);
  268. size = of_read_number(&ranges[6], 2);
  269. if (flags == 0 || size == 0)
  270. continue;
  271. if (flags & IORESOURCE_IO) {
  272. res = bus->resource[0];
  273. if (res->flags) {
  274. printk(KERN_ERR "PCI: ignoring extra I/O range"
  275. " for bridge %pOF\n", node);
  276. continue;
  277. }
  278. } else {
  279. if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
  280. printk(KERN_ERR "PCI: too many memory ranges"
  281. " for bridge %pOF\n", node);
  282. continue;
  283. }
  284. res = bus->resource[i];
  285. ++i;
  286. }
  287. res->flags = flags;
  288. region.start = of_read_number(&ranges[1], 2);
  289. region.end = region.start + size - 1;
  290. pcibios_bus_to_resource(dev->bus, res, &region);
  291. }
  292. sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
  293. bus->number);
  294. pr_debug(" bus name: %s\n", bus->name);
  295. phb = pci_bus_to_host(bus);
  296. mode = PCI_PROBE_NORMAL;
  297. if (phb->controller_ops.probe_mode)
  298. mode = phb->controller_ops.probe_mode(bus);
  299. pr_debug(" probe mode: %d\n", mode);
  300. if (mode == PCI_PROBE_DEVTREE)
  301. of_scan_bus(node, bus);
  302. else if (mode == PCI_PROBE_NORMAL)
  303. pci_scan_child_bus(bus);
  304. }
  305. EXPORT_SYMBOL(of_scan_pci_bridge);
  306. static struct pci_dev *of_scan_pci_dev(struct pci_bus *bus,
  307. struct device_node *dn)
  308. {
  309. struct pci_dev *dev = NULL;
  310. const __be32 *reg;
  311. int reglen, devfn;
  312. #ifdef CONFIG_EEH
  313. struct eeh_dev *edev = pdn_to_eeh_dev(PCI_DN(dn));
  314. #endif
  315. pr_debug(" * %pOF\n", dn);
  316. if (!of_device_is_available(dn))
  317. return NULL;
  318. reg = of_get_property(dn, "reg", &reglen);
  319. if (reg == NULL || reglen < 20)
  320. return NULL;
  321. devfn = (of_read_number(reg, 1) >> 8) & 0xff;
  322. /* Check if the PCI device is already there */
  323. dev = pci_get_slot(bus, devfn);
  324. if (dev) {
  325. pci_dev_put(dev);
  326. return dev;
  327. }
  328. /* Device removed permanently ? */
  329. #ifdef CONFIG_EEH
  330. if (edev && (edev->mode & EEH_DEV_REMOVED))
  331. return NULL;
  332. #endif
  333. /* create a new pci_dev for this device */
  334. dev = of_create_pci_dev(dn, bus, devfn);
  335. if (!dev)
  336. return NULL;
  337. pr_debug(" dev header type: %x\n", dev->hdr_type);
  338. return dev;
  339. }
  340. /**
  341. * __of_scan_bus - given a PCI bus node, setup bus and scan for child devices
  342. * @node: device tree node for the PCI bus
  343. * @bus: pci_bus structure for the PCI bus
  344. * @rescan_existing: Flag indicating bus has already been set up
  345. */
  346. static void __of_scan_bus(struct device_node *node, struct pci_bus *bus,
  347. int rescan_existing)
  348. {
  349. struct device_node *child;
  350. struct pci_dev *dev;
  351. pr_debug("of_scan_bus(%pOF) bus no %d...\n",
  352. node, bus->number);
  353. /* Scan direct children */
  354. for_each_child_of_node(node, child) {
  355. dev = of_scan_pci_dev(bus, child);
  356. if (!dev)
  357. continue;
  358. pr_debug(" dev header type: %x\n", dev->hdr_type);
  359. }
  360. /* Apply all fixups necessary. We don't fixup the bus "self"
  361. * for an existing bridge that is being rescanned
  362. */
  363. if (!rescan_existing)
  364. pcibios_setup_bus_self(bus);
  365. /* Now scan child busses */
  366. for_each_pci_bridge(dev, bus)
  367. of_scan_pci_bridge(dev);
  368. }
  369. /**
  370. * of_scan_bus - given a PCI bus node, setup bus and scan for child devices
  371. * @node: device tree node for the PCI bus
  372. * @bus: pci_bus structure for the PCI bus
  373. */
  374. void of_scan_bus(struct device_node *node, struct pci_bus *bus)
  375. {
  376. __of_scan_bus(node, bus, 0);
  377. }
  378. EXPORT_SYMBOL_GPL(of_scan_bus);
  379. /**
  380. * of_rescan_bus - given a PCI bus node, scan for child devices
  381. * @node: device tree node for the PCI bus
  382. * @bus: pci_bus structure for the PCI bus
  383. *
  384. * Same as of_scan_bus, but for a pci_bus structure that has already been
  385. * setup.
  386. */
  387. void of_rescan_bus(struct device_node *node, struct pci_bus *bus)
  388. {
  389. __of_scan_bus(node, bus, 1);
  390. }
  391. EXPORT_SYMBOL_GPL(of_rescan_bus);