pci_of_scan.c 11 KB

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