probe.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. /*
  2. * probe.c - PCI detection and setup code
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/delay.h>
  6. #include <linux/init.h>
  7. #include <linux/pci.h>
  8. #include <linux/slab.h>
  9. #include <linux/module.h>
  10. #include <linux/cpumask.h>
  11. #include "pci.h"
  12. #define CARDBUS_LATENCY_TIMER 176 /* secondary latency timer */
  13. #define CARDBUS_RESERVE_BUSNR 3
  14. #define PCI_CFG_SPACE_SIZE 256
  15. #define PCI_CFG_SPACE_EXP_SIZE 4096
  16. /* Ugh. Need to stop exporting this to modules. */
  17. LIST_HEAD(pci_root_buses);
  18. EXPORT_SYMBOL(pci_root_buses);
  19. LIST_HEAD(pci_devices);
  20. #ifdef HAVE_PCI_LEGACY
  21. /**
  22. * pci_create_legacy_files - create legacy I/O port and memory files
  23. * @b: bus to create files under
  24. *
  25. * Some platforms allow access to legacy I/O port and ISA memory space on
  26. * a per-bus basis. This routine creates the files and ties them into
  27. * their associated read, write and mmap files from pci-sysfs.c
  28. */
  29. static void pci_create_legacy_files(struct pci_bus *b)
  30. {
  31. b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
  32. GFP_ATOMIC);
  33. if (b->legacy_io) {
  34. b->legacy_io->attr.name = "legacy_io";
  35. b->legacy_io->size = 0xffff;
  36. b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
  37. b->legacy_io->attr.owner = THIS_MODULE;
  38. b->legacy_io->read = pci_read_legacy_io;
  39. b->legacy_io->write = pci_write_legacy_io;
  40. class_device_create_bin_file(&b->class_dev, b->legacy_io);
  41. /* Allocated above after the legacy_io struct */
  42. b->legacy_mem = b->legacy_io + 1;
  43. b->legacy_mem->attr.name = "legacy_mem";
  44. b->legacy_mem->size = 1024*1024;
  45. b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
  46. b->legacy_mem->attr.owner = THIS_MODULE;
  47. b->legacy_mem->mmap = pci_mmap_legacy_mem;
  48. class_device_create_bin_file(&b->class_dev, b->legacy_mem);
  49. }
  50. }
  51. void pci_remove_legacy_files(struct pci_bus *b)
  52. {
  53. if (b->legacy_io) {
  54. class_device_remove_bin_file(&b->class_dev, b->legacy_io);
  55. class_device_remove_bin_file(&b->class_dev, b->legacy_mem);
  56. kfree(b->legacy_io); /* both are allocated here */
  57. }
  58. }
  59. #else /* !HAVE_PCI_LEGACY */
  60. static inline void pci_create_legacy_files(struct pci_bus *bus) { return; }
  61. void pci_remove_legacy_files(struct pci_bus *bus) { return; }
  62. #endif /* HAVE_PCI_LEGACY */
  63. /*
  64. * PCI Bus Class Devices
  65. */
  66. static ssize_t pci_bus_show_cpuaffinity(struct class_device *class_dev,
  67. char *buf)
  68. {
  69. int ret;
  70. cpumask_t cpumask;
  71. cpumask = pcibus_to_cpumask(to_pci_bus(class_dev));
  72. ret = cpumask_scnprintf(buf, PAGE_SIZE, cpumask);
  73. if (ret < PAGE_SIZE)
  74. buf[ret++] = '\n';
  75. return ret;
  76. }
  77. CLASS_DEVICE_ATTR(cpuaffinity, S_IRUGO, pci_bus_show_cpuaffinity, NULL);
  78. /*
  79. * PCI Bus Class
  80. */
  81. static void release_pcibus_dev(struct class_device *class_dev)
  82. {
  83. struct pci_bus *pci_bus = to_pci_bus(class_dev);
  84. if (pci_bus->bridge)
  85. put_device(pci_bus->bridge);
  86. kfree(pci_bus);
  87. }
  88. static struct class pcibus_class = {
  89. .name = "pci_bus",
  90. .release = &release_pcibus_dev,
  91. };
  92. static int __init pcibus_class_init(void)
  93. {
  94. return class_register(&pcibus_class);
  95. }
  96. postcore_initcall(pcibus_class_init);
  97. /*
  98. * Translate the low bits of the PCI base
  99. * to the resource type
  100. */
  101. static inline unsigned int pci_calc_resource_flags(unsigned int flags)
  102. {
  103. if (flags & PCI_BASE_ADDRESS_SPACE_IO)
  104. return IORESOURCE_IO;
  105. if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
  106. return IORESOURCE_MEM | IORESOURCE_PREFETCH;
  107. return IORESOURCE_MEM;
  108. }
  109. /*
  110. * Find the extent of a PCI decode..
  111. */
  112. static u32 pci_size(u32 base, u32 maxbase, u32 mask)
  113. {
  114. u32 size = mask & maxbase; /* Find the significant bits */
  115. if (!size)
  116. return 0;
  117. /* Get the lowest of them to find the decode size, and
  118. from that the extent. */
  119. size = (size & ~(size-1)) - 1;
  120. /* base == maxbase can be valid only if the BAR has
  121. already been programmed with all 1s. */
  122. if (base == maxbase && ((base | size) & mask) != mask)
  123. return 0;
  124. return size;
  125. }
  126. static u64 pci_size64(u64 base, u64 maxbase, u64 mask)
  127. {
  128. u64 size = mask & maxbase; /* Find the significant bits */
  129. if (!size)
  130. return 0;
  131. /* Get the lowest of them to find the decode size, and
  132. from that the extent. */
  133. size = (size & ~(size-1)) - 1;
  134. /* base == maxbase can be valid only if the BAR has
  135. already been programmed with all 1s. */
  136. if (base == maxbase && ((base | size) & mask) != mask)
  137. return 0;
  138. return size;
  139. }
  140. static inline int is_64bit_memory(u32 mask)
  141. {
  142. if ((mask & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
  143. (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64))
  144. return 1;
  145. return 0;
  146. }
  147. static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
  148. {
  149. unsigned int pos, reg, next;
  150. u32 l, sz;
  151. struct resource *res;
  152. for(pos=0; pos<howmany; pos = next) {
  153. u64 l64;
  154. u64 sz64;
  155. u32 raw_sz;
  156. next = pos+1;
  157. res = &dev->resource[pos];
  158. res->name = pci_name(dev);
  159. reg = PCI_BASE_ADDRESS_0 + (pos << 2);
  160. pci_read_config_dword(dev, reg, &l);
  161. pci_write_config_dword(dev, reg, ~0);
  162. pci_read_config_dword(dev, reg, &sz);
  163. pci_write_config_dword(dev, reg, l);
  164. if (!sz || sz == 0xffffffff)
  165. continue;
  166. if (l == 0xffffffff)
  167. l = 0;
  168. raw_sz = sz;
  169. if ((l & PCI_BASE_ADDRESS_SPACE) ==
  170. PCI_BASE_ADDRESS_SPACE_MEMORY) {
  171. sz = pci_size(l, sz, (u32)PCI_BASE_ADDRESS_MEM_MASK);
  172. /*
  173. * For 64bit prefetchable memory sz could be 0, if the
  174. * real size is bigger than 4G, so we need to check
  175. * szhi for that.
  176. */
  177. if (!is_64bit_memory(l) && !sz)
  178. continue;
  179. res->start = l & PCI_BASE_ADDRESS_MEM_MASK;
  180. res->flags |= l & ~PCI_BASE_ADDRESS_MEM_MASK;
  181. } else {
  182. sz = pci_size(l, sz, PCI_BASE_ADDRESS_IO_MASK & 0xffff);
  183. if (!sz)
  184. continue;
  185. res->start = l & PCI_BASE_ADDRESS_IO_MASK;
  186. res->flags |= l & ~PCI_BASE_ADDRESS_IO_MASK;
  187. }
  188. res->end = res->start + (unsigned long) sz;
  189. res->flags |= pci_calc_resource_flags(l);
  190. if (is_64bit_memory(l)) {
  191. u32 szhi, lhi;
  192. pci_read_config_dword(dev, reg+4, &lhi);
  193. pci_write_config_dword(dev, reg+4, ~0);
  194. pci_read_config_dword(dev, reg+4, &szhi);
  195. pci_write_config_dword(dev, reg+4, lhi);
  196. sz64 = ((u64)szhi << 32) | raw_sz;
  197. l64 = ((u64)lhi << 32) | l;
  198. sz64 = pci_size64(l64, sz64, PCI_BASE_ADDRESS_MEM_MASK);
  199. next++;
  200. #if BITS_PER_LONG == 64
  201. if (!sz64) {
  202. res->start = 0;
  203. res->end = 0;
  204. res->flags = 0;
  205. continue;
  206. }
  207. res->start = l64 & PCI_BASE_ADDRESS_MEM_MASK;
  208. res->end = res->start + sz64;
  209. #else
  210. if (sz64 > 0x100000000ULL) {
  211. printk(KERN_ERR "PCI: Unable to handle 64-bit "
  212. "BAR for device %s\n", pci_name(dev));
  213. res->start = 0;
  214. res->flags = 0;
  215. } else if (lhi) {
  216. /* 64-bit wide address, treat as disabled */
  217. pci_write_config_dword(dev, reg,
  218. l & ~(u32)PCI_BASE_ADDRESS_MEM_MASK);
  219. pci_write_config_dword(dev, reg+4, 0);
  220. res->start = 0;
  221. res->end = sz;
  222. }
  223. #endif
  224. }
  225. }
  226. if (rom) {
  227. dev->rom_base_reg = rom;
  228. res = &dev->resource[PCI_ROM_RESOURCE];
  229. res->name = pci_name(dev);
  230. pci_read_config_dword(dev, rom, &l);
  231. pci_write_config_dword(dev, rom, ~PCI_ROM_ADDRESS_ENABLE);
  232. pci_read_config_dword(dev, rom, &sz);
  233. pci_write_config_dword(dev, rom, l);
  234. if (l == 0xffffffff)
  235. l = 0;
  236. if (sz && sz != 0xffffffff) {
  237. sz = pci_size(l, sz, (u32)PCI_ROM_ADDRESS_MASK);
  238. if (sz) {
  239. res->flags = (l & IORESOURCE_ROM_ENABLE) |
  240. IORESOURCE_MEM | IORESOURCE_PREFETCH |
  241. IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
  242. res->start = l & PCI_ROM_ADDRESS_MASK;
  243. res->end = res->start + (unsigned long) sz;
  244. }
  245. }
  246. }
  247. }
  248. void __devinit pci_read_bridge_bases(struct pci_bus *child)
  249. {
  250. struct pci_dev *dev = child->self;
  251. u8 io_base_lo, io_limit_lo;
  252. u16 mem_base_lo, mem_limit_lo;
  253. unsigned long base, limit;
  254. struct resource *res;
  255. int i;
  256. if (!dev) /* It's a host bus, nothing to read */
  257. return;
  258. if (dev->transparent) {
  259. printk(KERN_INFO "PCI: Transparent bridge - %s\n", pci_name(dev));
  260. for(i = 3; i < PCI_BUS_NUM_RESOURCES; i++)
  261. child->resource[i] = child->parent->resource[i - 3];
  262. }
  263. for(i=0; i<3; i++)
  264. child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
  265. res = child->resource[0];
  266. pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
  267. pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
  268. base = (io_base_lo & PCI_IO_RANGE_MASK) << 8;
  269. limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8;
  270. if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
  271. u16 io_base_hi, io_limit_hi;
  272. pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
  273. pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
  274. base |= (io_base_hi << 16);
  275. limit |= (io_limit_hi << 16);
  276. }
  277. if (base <= limit) {
  278. res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
  279. if (!res->start)
  280. res->start = base;
  281. if (!res->end)
  282. res->end = limit + 0xfff;
  283. }
  284. res = child->resource[1];
  285. pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
  286. pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
  287. base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
  288. limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
  289. if (base <= limit) {
  290. res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
  291. res->start = base;
  292. res->end = limit + 0xfffff;
  293. }
  294. res = child->resource[2];
  295. pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
  296. pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
  297. base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
  298. limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
  299. if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
  300. u32 mem_base_hi, mem_limit_hi;
  301. pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
  302. pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
  303. /*
  304. * Some bridges set the base > limit by default, and some
  305. * (broken) BIOSes do not initialize them. If we find
  306. * this, just assume they are not being used.
  307. */
  308. if (mem_base_hi <= mem_limit_hi) {
  309. #if BITS_PER_LONG == 64
  310. base |= ((long) mem_base_hi) << 32;
  311. limit |= ((long) mem_limit_hi) << 32;
  312. #else
  313. if (mem_base_hi || mem_limit_hi) {
  314. printk(KERN_ERR "PCI: Unable to handle 64-bit address space for bridge %s\n", pci_name(dev));
  315. return;
  316. }
  317. #endif
  318. }
  319. }
  320. if (base <= limit) {
  321. res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM | IORESOURCE_PREFETCH;
  322. res->start = base;
  323. res->end = limit + 0xfffff;
  324. }
  325. }
  326. static struct pci_bus * __devinit pci_alloc_bus(void)
  327. {
  328. struct pci_bus *b;
  329. b = kzalloc(sizeof(*b), GFP_KERNEL);
  330. if (b) {
  331. INIT_LIST_HEAD(&b->node);
  332. INIT_LIST_HEAD(&b->children);
  333. INIT_LIST_HEAD(&b->devices);
  334. }
  335. return b;
  336. }
  337. static struct pci_bus * __devinit
  338. pci_alloc_child_bus(struct pci_bus *parent, struct pci_dev *bridge, int busnr)
  339. {
  340. struct pci_bus *child;
  341. int i;
  342. int retval;
  343. /*
  344. * Allocate a new bus, and inherit stuff from the parent..
  345. */
  346. child = pci_alloc_bus();
  347. if (!child)
  348. return NULL;
  349. child->self = bridge;
  350. child->parent = parent;
  351. child->ops = parent->ops;
  352. child->sysdata = parent->sysdata;
  353. child->bus_flags = parent->bus_flags;
  354. child->bridge = get_device(&bridge->dev);
  355. child->class_dev.class = &pcibus_class;
  356. sprintf(child->class_dev.class_id, "%04x:%02x", pci_domain_nr(child), busnr);
  357. retval = class_device_register(&child->class_dev);
  358. if (retval)
  359. goto error_register;
  360. retval = class_device_create_file(&child->class_dev,
  361. &class_device_attr_cpuaffinity);
  362. if (retval)
  363. goto error_file_create;
  364. /*
  365. * Set up the primary, secondary and subordinate
  366. * bus numbers.
  367. */
  368. child->number = child->secondary = busnr;
  369. child->primary = parent->secondary;
  370. child->subordinate = 0xff;
  371. /* Set up default resource pointers and names.. */
  372. for (i = 0; i < 4; i++) {
  373. child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
  374. child->resource[i]->name = child->name;
  375. }
  376. bridge->subordinate = child;
  377. return child;
  378. error_file_create:
  379. class_device_unregister(&child->class_dev);
  380. error_register:
  381. kfree(child);
  382. return NULL;
  383. }
  384. struct pci_bus * __devinit pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, int busnr)
  385. {
  386. struct pci_bus *child;
  387. child = pci_alloc_child_bus(parent, dev, busnr);
  388. if (child) {
  389. down_write(&pci_bus_sem);
  390. list_add_tail(&child->node, &parent->children);
  391. up_write(&pci_bus_sem);
  392. }
  393. return child;
  394. }
  395. static void pci_enable_crs(struct pci_dev *dev)
  396. {
  397. u16 cap, rpctl;
  398. int rpcap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  399. if (!rpcap)
  400. return;
  401. pci_read_config_word(dev, rpcap + PCI_CAP_FLAGS, &cap);
  402. if (((cap & PCI_EXP_FLAGS_TYPE) >> 4) != PCI_EXP_TYPE_ROOT_PORT)
  403. return;
  404. pci_read_config_word(dev, rpcap + PCI_EXP_RTCTL, &rpctl);
  405. rpctl |= PCI_EXP_RTCTL_CRSSVE;
  406. pci_write_config_word(dev, rpcap + PCI_EXP_RTCTL, rpctl);
  407. }
  408. static void __devinit pci_fixup_parent_subordinate_busnr(struct pci_bus *child, int max)
  409. {
  410. struct pci_bus *parent = child->parent;
  411. /* Attempts to fix that up are really dangerous unless
  412. we're going to re-assign all bus numbers. */
  413. if (!pcibios_assign_all_busses())
  414. return;
  415. while (parent->parent && parent->subordinate < max) {
  416. parent->subordinate = max;
  417. pci_write_config_byte(parent->self, PCI_SUBORDINATE_BUS, max);
  418. parent = parent->parent;
  419. }
  420. }
  421. unsigned int __devinit pci_scan_child_bus(struct pci_bus *bus);
  422. /*
  423. * If it's a bridge, configure it and scan the bus behind it.
  424. * For CardBus bridges, we don't scan behind as the devices will
  425. * be handled by the bridge driver itself.
  426. *
  427. * We need to process bridges in two passes -- first we scan those
  428. * already configured by the BIOS and after we are done with all of
  429. * them, we proceed to assigning numbers to the remaining buses in
  430. * order to avoid overlaps between old and new bus numbers.
  431. */
  432. int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev * dev, int max, int pass)
  433. {
  434. struct pci_bus *child;
  435. int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
  436. u32 buses, i, j = 0;
  437. u16 bctl;
  438. pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
  439. pr_debug("PCI: Scanning behind PCI bridge %s, config %06x, pass %d\n",
  440. pci_name(dev), buses & 0xffffff, pass);
  441. /* Disable MasterAbortMode during probing to avoid reporting
  442. of bus errors (in some architectures) */
  443. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
  444. pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
  445. bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
  446. pci_enable_crs(dev);
  447. if ((buses & 0xffff00) && !pcibios_assign_all_busses() && !is_cardbus) {
  448. unsigned int cmax, busnr;
  449. /*
  450. * Bus already configured by firmware, process it in the first
  451. * pass and just note the configuration.
  452. */
  453. if (pass)
  454. goto out;
  455. busnr = (buses >> 8) & 0xFF;
  456. /*
  457. * If we already got to this bus through a different bridge,
  458. * ignore it. This can happen with the i450NX chipset.
  459. */
  460. if (pci_find_bus(pci_domain_nr(bus), busnr)) {
  461. printk(KERN_INFO "PCI: Bus %04x:%02x already known\n",
  462. pci_domain_nr(bus), busnr);
  463. goto out;
  464. }
  465. child = pci_add_new_bus(bus, dev, busnr);
  466. if (!child)
  467. goto out;
  468. child->primary = buses & 0xFF;
  469. child->subordinate = (buses >> 16) & 0xFF;
  470. child->bridge_ctl = bctl;
  471. cmax = pci_scan_child_bus(child);
  472. if (cmax > max)
  473. max = cmax;
  474. if (child->subordinate > max)
  475. max = child->subordinate;
  476. } else {
  477. /*
  478. * We need to assign a number to this bus which we always
  479. * do in the second pass.
  480. */
  481. if (!pass) {
  482. if (pcibios_assign_all_busses())
  483. /* Temporarily disable forwarding of the
  484. configuration cycles on all bridges in
  485. this bus segment to avoid possible
  486. conflicts in the second pass between two
  487. bridges programmed with overlapping
  488. bus ranges. */
  489. pci_write_config_dword(dev, PCI_PRIMARY_BUS,
  490. buses & ~0xffffff);
  491. goto out;
  492. }
  493. /* Clear errors */
  494. pci_write_config_word(dev, PCI_STATUS, 0xffff);
  495. /* Prevent assigning a bus number that already exists.
  496. * This can happen when a bridge is hot-plugged */
  497. if (pci_find_bus(pci_domain_nr(bus), max+1))
  498. goto out;
  499. child = pci_add_new_bus(bus, dev, ++max);
  500. buses = (buses & 0xff000000)
  501. | ((unsigned int)(child->primary) << 0)
  502. | ((unsigned int)(child->secondary) << 8)
  503. | ((unsigned int)(child->subordinate) << 16);
  504. /*
  505. * yenta.c forces a secondary latency timer of 176.
  506. * Copy that behaviour here.
  507. */
  508. if (is_cardbus) {
  509. buses &= ~0xff000000;
  510. buses |= CARDBUS_LATENCY_TIMER << 24;
  511. }
  512. /*
  513. * We need to blast all three values with a single write.
  514. */
  515. pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
  516. if (!is_cardbus) {
  517. child->bridge_ctl = bctl | PCI_BRIDGE_CTL_NO_ISA;
  518. /*
  519. * Adjust subordinate busnr in parent buses.
  520. * We do this before scanning for children because
  521. * some devices may not be detected if the bios
  522. * was lazy.
  523. */
  524. pci_fixup_parent_subordinate_busnr(child, max);
  525. /* Now we can scan all subordinate buses... */
  526. max = pci_scan_child_bus(child);
  527. /*
  528. * now fix it up again since we have found
  529. * the real value of max.
  530. */
  531. pci_fixup_parent_subordinate_busnr(child, max);
  532. } else {
  533. /*
  534. * For CardBus bridges, we leave 4 bus numbers
  535. * as cards with a PCI-to-PCI bridge can be
  536. * inserted later.
  537. */
  538. for (i=0; i<CARDBUS_RESERVE_BUSNR; i++) {
  539. struct pci_bus *parent = bus;
  540. if (pci_find_bus(pci_domain_nr(bus),
  541. max+i+1))
  542. break;
  543. while (parent->parent) {
  544. if ((!pcibios_assign_all_busses()) &&
  545. (parent->subordinate > max) &&
  546. (parent->subordinate <= max+i)) {
  547. j = 1;
  548. }
  549. parent = parent->parent;
  550. }
  551. if (j) {
  552. /*
  553. * Often, there are two cardbus bridges
  554. * -- try to leave one valid bus number
  555. * for each one.
  556. */
  557. i /= 2;
  558. break;
  559. }
  560. }
  561. max += i;
  562. pci_fixup_parent_subordinate_busnr(child, max);
  563. }
  564. /*
  565. * Set the subordinate bus number to its real value.
  566. */
  567. child->subordinate = max;
  568. pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
  569. }
  570. sprintf(child->name, (is_cardbus ? "PCI CardBus #%02x" : "PCI Bus #%02x"), child->number);
  571. while (bus->parent) {
  572. if ((child->subordinate > bus->subordinate) ||
  573. (child->number > bus->subordinate) ||
  574. (child->number < bus->number) ||
  575. (child->subordinate < bus->number)) {
  576. printk(KERN_WARNING "PCI: Bus #%02x (-#%02x) is "
  577. "hidden behind%s bridge #%02x (-#%02x)%s\n",
  578. child->number, child->subordinate,
  579. bus->self->transparent ? " transparent" : " ",
  580. bus->number, bus->subordinate,
  581. pcibios_assign_all_busses() ? " " :
  582. " (try 'pci=assign-busses')");
  583. printk(KERN_WARNING "Please report the result to "
  584. "linux-kernel to fix this permanently\n");
  585. }
  586. bus = bus->parent;
  587. }
  588. out:
  589. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
  590. return max;
  591. }
  592. /*
  593. * Read interrupt line and base address registers.
  594. * The architecture-dependent code can tweak these, of course.
  595. */
  596. static void pci_read_irq(struct pci_dev *dev)
  597. {
  598. unsigned char irq;
  599. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
  600. dev->pin = irq;
  601. if (irq)
  602. pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
  603. dev->irq = irq;
  604. }
  605. #define LEGACY_IO_RESOURCE (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
  606. /**
  607. * pci_setup_device - fill in class and map information of a device
  608. * @dev: the device structure to fill
  609. *
  610. * Initialize the device structure with information about the device's
  611. * vendor,class,memory and IO-space addresses,IRQ lines etc.
  612. * Called at initialisation of the PCI subsystem and by CardBus services.
  613. * Returns 0 on success and -1 if unknown type of device (not normal, bridge
  614. * or CardBus).
  615. */
  616. static int pci_setup_device(struct pci_dev * dev)
  617. {
  618. u32 class;
  619. sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
  620. dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
  621. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
  622. class >>= 8; /* upper 3 bytes */
  623. dev->class = class;
  624. class >>= 8;
  625. pr_debug("PCI: Found %s [%04x/%04x] %06x %02x\n", pci_name(dev),
  626. dev->vendor, dev->device, class, dev->hdr_type);
  627. /* "Unknown power state" */
  628. dev->current_state = PCI_UNKNOWN;
  629. /* Early fixups, before probing the BARs */
  630. pci_fixup_device(pci_fixup_early, dev);
  631. class = dev->class >> 8;
  632. switch (dev->hdr_type) { /* header type */
  633. case PCI_HEADER_TYPE_NORMAL: /* standard header */
  634. if (class == PCI_CLASS_BRIDGE_PCI)
  635. goto bad;
  636. pci_read_irq(dev);
  637. pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
  638. pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
  639. pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
  640. /*
  641. * Do the ugly legacy mode stuff here rather than broken chip
  642. * quirk code. Legacy mode ATA controllers have fixed
  643. * addresses. These are not always echoed in BAR0-3, and
  644. * BAR0-3 in a few cases contain junk!
  645. */
  646. if (class == PCI_CLASS_STORAGE_IDE) {
  647. u8 progif;
  648. pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
  649. if ((progif & 1) == 0) {
  650. dev->resource[0].start = 0x1F0;
  651. dev->resource[0].end = 0x1F7;
  652. dev->resource[0].flags = LEGACY_IO_RESOURCE;
  653. dev->resource[1].start = 0x3F6;
  654. dev->resource[1].end = 0x3F6;
  655. dev->resource[1].flags = LEGACY_IO_RESOURCE;
  656. }
  657. if ((progif & 4) == 0) {
  658. dev->resource[2].start = 0x170;
  659. dev->resource[2].end = 0x177;
  660. dev->resource[2].flags = LEGACY_IO_RESOURCE;
  661. dev->resource[3].start = 0x376;
  662. dev->resource[3].end = 0x376;
  663. dev->resource[3].flags = LEGACY_IO_RESOURCE;
  664. }
  665. }
  666. break;
  667. case PCI_HEADER_TYPE_BRIDGE: /* bridge header */
  668. if (class != PCI_CLASS_BRIDGE_PCI)
  669. goto bad;
  670. /* The PCI-to-PCI bridge spec requires that subtractive
  671. decoding (i.e. transparent) bridge must have programming
  672. interface code of 0x01. */
  673. pci_read_irq(dev);
  674. dev->transparent = ((dev->class & 0xff) == 1);
  675. pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
  676. break;
  677. case PCI_HEADER_TYPE_CARDBUS: /* CardBus bridge header */
  678. if (class != PCI_CLASS_BRIDGE_CARDBUS)
  679. goto bad;
  680. pci_read_irq(dev);
  681. pci_read_bases(dev, 1, 0);
  682. pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
  683. pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
  684. break;
  685. default: /* unknown header */
  686. printk(KERN_ERR "PCI: device %s has unknown header type %02x, ignoring.\n",
  687. pci_name(dev), dev->hdr_type);
  688. return -1;
  689. bad:
  690. printk(KERN_ERR "PCI: %s: class %x doesn't match header type %02x. Ignoring class.\n",
  691. pci_name(dev), class, dev->hdr_type);
  692. dev->class = PCI_CLASS_NOT_DEFINED;
  693. }
  694. /* We found a fine healthy device, go go go... */
  695. return 0;
  696. }
  697. /**
  698. * pci_release_dev - free a pci device structure when all users of it are finished.
  699. * @dev: device that's been disconnected
  700. *
  701. * Will be called only by the device core when all users of this pci device are
  702. * done.
  703. */
  704. static void pci_release_dev(struct device *dev)
  705. {
  706. struct pci_dev *pci_dev;
  707. pci_dev = to_pci_dev(dev);
  708. kfree(pci_dev);
  709. }
  710. /**
  711. * pci_cfg_space_size - get the configuration space size of the PCI device.
  712. * @dev: PCI device
  713. *
  714. * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
  715. * have 4096 bytes. Even if the device is capable, that doesn't mean we can
  716. * access it. Maybe we don't have a way to generate extended config space
  717. * accesses, or the device is behind a reverse Express bridge. So we try
  718. * reading the dword at 0x100 which must either be 0 or a valid extended
  719. * capability header.
  720. */
  721. int pci_cfg_space_size(struct pci_dev *dev)
  722. {
  723. int pos;
  724. u32 status;
  725. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  726. if (!pos) {
  727. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  728. if (!pos)
  729. goto fail;
  730. pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
  731. if (!(status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ)))
  732. goto fail;
  733. }
  734. if (pci_read_config_dword(dev, 256, &status) != PCIBIOS_SUCCESSFUL)
  735. goto fail;
  736. if (status == 0xffffffff)
  737. goto fail;
  738. return PCI_CFG_SPACE_EXP_SIZE;
  739. fail:
  740. return PCI_CFG_SPACE_SIZE;
  741. }
  742. static void pci_release_bus_bridge_dev(struct device *dev)
  743. {
  744. kfree(dev);
  745. }
  746. /*
  747. * Read the config data for a PCI device, sanity-check it
  748. * and fill in the dev structure...
  749. */
  750. static struct pci_dev * __devinit
  751. pci_scan_device(struct pci_bus *bus, int devfn)
  752. {
  753. struct pci_dev *dev;
  754. u32 l;
  755. u8 hdr_type;
  756. int delay = 1;
  757. if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l))
  758. return NULL;
  759. /* some broken boards return 0 or ~0 if a slot is empty: */
  760. if (l == 0xffffffff || l == 0x00000000 ||
  761. l == 0x0000ffff || l == 0xffff0000)
  762. return NULL;
  763. /* Configuration request Retry Status */
  764. while (l == 0xffff0001) {
  765. msleep(delay);
  766. delay *= 2;
  767. if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l))
  768. return NULL;
  769. /* Card hasn't responded in 60 seconds? Must be stuck. */
  770. if (delay > 60 * 1000) {
  771. printk(KERN_WARNING "Device %04x:%02x:%02x.%d not "
  772. "responding\n", pci_domain_nr(bus),
  773. bus->number, PCI_SLOT(devfn),
  774. PCI_FUNC(devfn));
  775. return NULL;
  776. }
  777. }
  778. if (pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type))
  779. return NULL;
  780. dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
  781. if (!dev)
  782. return NULL;
  783. dev->bus = bus;
  784. dev->sysdata = bus->sysdata;
  785. dev->dev.parent = bus->bridge;
  786. dev->dev.bus = &pci_bus_type;
  787. dev->devfn = devfn;
  788. dev->hdr_type = hdr_type & 0x7f;
  789. dev->multifunction = !!(hdr_type & 0x80);
  790. dev->vendor = l & 0xffff;
  791. dev->device = (l >> 16) & 0xffff;
  792. dev->cfg_size = pci_cfg_space_size(dev);
  793. dev->error_state = pci_channel_io_normal;
  794. /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
  795. set this higher, assuming the system even supports it. */
  796. dev->dma_mask = 0xffffffff;
  797. if (pci_setup_device(dev) < 0) {
  798. kfree(dev);
  799. return NULL;
  800. }
  801. return dev;
  802. }
  803. void __devinit pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
  804. {
  805. device_initialize(&dev->dev);
  806. dev->dev.release = pci_release_dev;
  807. pci_dev_get(dev);
  808. set_dev_node(&dev->dev, pcibus_to_node(bus));
  809. dev->dev.dma_mask = &dev->dma_mask;
  810. dev->dev.coherent_dma_mask = 0xffffffffull;
  811. /* Fix up broken headers */
  812. pci_fixup_device(pci_fixup_header, dev);
  813. /*
  814. * Add the device to our list of discovered devices
  815. * and the bus list for fixup functions, etc.
  816. */
  817. INIT_LIST_HEAD(&dev->global_list);
  818. down_write(&pci_bus_sem);
  819. list_add_tail(&dev->bus_list, &bus->devices);
  820. up_write(&pci_bus_sem);
  821. }
  822. struct pci_dev * __devinit
  823. pci_scan_single_device(struct pci_bus *bus, int devfn)
  824. {
  825. struct pci_dev *dev;
  826. dev = pci_scan_device(bus, devfn);
  827. if (!dev)
  828. return NULL;
  829. pci_device_add(dev, bus);
  830. return dev;
  831. }
  832. /**
  833. * pci_scan_slot - scan a PCI slot on a bus for devices.
  834. * @bus: PCI bus to scan
  835. * @devfn: slot number to scan (must have zero function.)
  836. *
  837. * Scan a PCI slot on the specified PCI bus for devices, adding
  838. * discovered devices to the @bus->devices list. New devices
  839. * will have an empty dev->global_list head.
  840. */
  841. int __devinit pci_scan_slot(struct pci_bus *bus, int devfn)
  842. {
  843. int func, nr = 0;
  844. int scan_all_fns;
  845. scan_all_fns = pcibios_scan_all_fns(bus, devfn);
  846. for (func = 0; func < 8; func++, devfn++) {
  847. struct pci_dev *dev;
  848. dev = pci_scan_single_device(bus, devfn);
  849. if (dev) {
  850. nr++;
  851. /*
  852. * If this is a single function device,
  853. * don't scan past the first function.
  854. */
  855. if (!dev->multifunction) {
  856. if (func > 0) {
  857. dev->multifunction = 1;
  858. } else {
  859. break;
  860. }
  861. }
  862. } else {
  863. if (func == 0 && !scan_all_fns)
  864. break;
  865. }
  866. }
  867. return nr;
  868. }
  869. unsigned int __devinit pci_scan_child_bus(struct pci_bus *bus)
  870. {
  871. unsigned int devfn, pass, max = bus->secondary;
  872. struct pci_dev *dev;
  873. pr_debug("PCI: Scanning bus %04x:%02x\n", pci_domain_nr(bus), bus->number);
  874. /* Go find them, Rover! */
  875. for (devfn = 0; devfn < 0x100; devfn += 8)
  876. pci_scan_slot(bus, devfn);
  877. /*
  878. * After performing arch-dependent fixup of the bus, look behind
  879. * all PCI-to-PCI bridges on this bus.
  880. */
  881. pr_debug("PCI: Fixups for bus %04x:%02x\n", pci_domain_nr(bus), bus->number);
  882. pcibios_fixup_bus(bus);
  883. for (pass=0; pass < 2; pass++)
  884. list_for_each_entry(dev, &bus->devices, bus_list) {
  885. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  886. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  887. max = pci_scan_bridge(bus, dev, max, pass);
  888. }
  889. /*
  890. * We've scanned the bus and so we know all about what's on
  891. * the other side of any bridges that may be on this bus plus
  892. * any devices.
  893. *
  894. * Return how far we've got finding sub-buses.
  895. */
  896. pr_debug("PCI: Bus scan for %04x:%02x returning with max=%02x\n",
  897. pci_domain_nr(bus), bus->number, max);
  898. return max;
  899. }
  900. unsigned int __devinit pci_do_scan_bus(struct pci_bus *bus)
  901. {
  902. unsigned int max;
  903. max = pci_scan_child_bus(bus);
  904. /*
  905. * Make the discovered devices available.
  906. */
  907. pci_bus_add_devices(bus);
  908. return max;
  909. }
  910. struct pci_bus * __devinit pci_create_bus(struct device *parent,
  911. int bus, struct pci_ops *ops, void *sysdata)
  912. {
  913. int error;
  914. struct pci_bus *b;
  915. struct device *dev;
  916. b = pci_alloc_bus();
  917. if (!b)
  918. return NULL;
  919. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  920. if (!dev){
  921. kfree(b);
  922. return NULL;
  923. }
  924. b->sysdata = sysdata;
  925. b->ops = ops;
  926. if (pci_find_bus(pci_domain_nr(b), bus)) {
  927. /* If we already got to this bus through a different bridge, ignore it */
  928. pr_debug("PCI: Bus %04x:%02x already known\n", pci_domain_nr(b), bus);
  929. goto err_out;
  930. }
  931. down_write(&pci_bus_sem);
  932. list_add_tail(&b->node, &pci_root_buses);
  933. up_write(&pci_bus_sem);
  934. memset(dev, 0, sizeof(*dev));
  935. dev->parent = parent;
  936. dev->release = pci_release_bus_bridge_dev;
  937. sprintf(dev->bus_id, "pci%04x:%02x", pci_domain_nr(b), bus);
  938. error = device_register(dev);
  939. if (error)
  940. goto dev_reg_err;
  941. b->bridge = get_device(dev);
  942. b->class_dev.class = &pcibus_class;
  943. sprintf(b->class_dev.class_id, "%04x:%02x", pci_domain_nr(b), bus);
  944. error = class_device_register(&b->class_dev);
  945. if (error)
  946. goto class_dev_reg_err;
  947. error = class_device_create_file(&b->class_dev, &class_device_attr_cpuaffinity);
  948. if (error)
  949. goto class_dev_create_file_err;
  950. /* Create legacy_io and legacy_mem files for this bus */
  951. pci_create_legacy_files(b);
  952. error = sysfs_create_link(&b->class_dev.kobj, &b->bridge->kobj, "bridge");
  953. if (error)
  954. goto sys_create_link_err;
  955. b->number = b->secondary = bus;
  956. b->resource[0] = &ioport_resource;
  957. b->resource[1] = &iomem_resource;
  958. return b;
  959. sys_create_link_err:
  960. class_device_remove_file(&b->class_dev, &class_device_attr_cpuaffinity);
  961. class_dev_create_file_err:
  962. class_device_unregister(&b->class_dev);
  963. class_dev_reg_err:
  964. device_unregister(dev);
  965. dev_reg_err:
  966. down_write(&pci_bus_sem);
  967. list_del(&b->node);
  968. up_write(&pci_bus_sem);
  969. err_out:
  970. kfree(dev);
  971. kfree(b);
  972. return NULL;
  973. }
  974. EXPORT_SYMBOL_GPL(pci_create_bus);
  975. struct pci_bus * __devinit pci_scan_bus_parented(struct device *parent,
  976. int bus, struct pci_ops *ops, void *sysdata)
  977. {
  978. struct pci_bus *b;
  979. b = pci_create_bus(parent, bus, ops, sysdata);
  980. if (b)
  981. b->subordinate = pci_scan_child_bus(b);
  982. return b;
  983. }
  984. EXPORT_SYMBOL(pci_scan_bus_parented);
  985. #ifdef CONFIG_HOTPLUG
  986. EXPORT_SYMBOL(pci_add_new_bus);
  987. EXPORT_SYMBOL(pci_do_scan_bus);
  988. EXPORT_SYMBOL(pci_scan_slot);
  989. EXPORT_SYMBOL(pci_scan_bridge);
  990. EXPORT_SYMBOL(pci_scan_single_device);
  991. EXPORT_SYMBOL_GPL(pci_scan_child_bus);
  992. #endif
  993. static int __init pci_sort_bf_cmp(const struct pci_dev *a, const struct pci_dev *b)
  994. {
  995. if (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1;
  996. else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return 1;
  997. if (a->bus->number < b->bus->number) return -1;
  998. else if (a->bus->number > b->bus->number) return 1;
  999. if (a->devfn < b->devfn) return -1;
  1000. else if (a->devfn > b->devfn) return 1;
  1001. return 0;
  1002. }
  1003. /*
  1004. * Yes, this forcably breaks the klist abstraction temporarily. It
  1005. * just wants to sort the klist, not change reference counts and
  1006. * take/drop locks rapidly in the process. It does all this while
  1007. * holding the lock for the list, so objects can't otherwise be
  1008. * added/removed while we're swizzling.
  1009. */
  1010. static void __init pci_insertion_sort_klist(struct pci_dev *a, struct list_head *list)
  1011. {
  1012. struct list_head *pos;
  1013. struct klist_node *n;
  1014. struct device *dev;
  1015. struct pci_dev *b;
  1016. list_for_each(pos, list) {
  1017. n = container_of(pos, struct klist_node, n_node);
  1018. dev = container_of(n, struct device, knode_bus);
  1019. b = to_pci_dev(dev);
  1020. if (pci_sort_bf_cmp(a, b) <= 0) {
  1021. list_move_tail(&a->dev.knode_bus.n_node, &b->dev.knode_bus.n_node);
  1022. return;
  1023. }
  1024. }
  1025. list_move_tail(&a->dev.knode_bus.n_node, list);
  1026. }
  1027. static void __init pci_sort_breadthfirst_klist(void)
  1028. {
  1029. LIST_HEAD(sorted_devices);
  1030. struct list_head *pos, *tmp;
  1031. struct klist_node *n;
  1032. struct device *dev;
  1033. struct pci_dev *pdev;
  1034. spin_lock(&pci_bus_type.klist_devices.k_lock);
  1035. list_for_each_safe(pos, tmp, &pci_bus_type.klist_devices.k_list) {
  1036. n = container_of(pos, struct klist_node, n_node);
  1037. dev = container_of(n, struct device, knode_bus);
  1038. pdev = to_pci_dev(dev);
  1039. pci_insertion_sort_klist(pdev, &sorted_devices);
  1040. }
  1041. list_splice(&sorted_devices, &pci_bus_type.klist_devices.k_list);
  1042. spin_unlock(&pci_bus_type.klist_devices.k_lock);
  1043. }
  1044. static void __init pci_insertion_sort_devices(struct pci_dev *a, struct list_head *list)
  1045. {
  1046. struct pci_dev *b;
  1047. list_for_each_entry(b, list, global_list) {
  1048. if (pci_sort_bf_cmp(a, b) <= 0) {
  1049. list_move_tail(&a->global_list, &b->global_list);
  1050. return;
  1051. }
  1052. }
  1053. list_move_tail(&a->global_list, list);
  1054. }
  1055. static void __init pci_sort_breadthfirst_devices(void)
  1056. {
  1057. LIST_HEAD(sorted_devices);
  1058. struct pci_dev *dev, *tmp;
  1059. down_write(&pci_bus_sem);
  1060. list_for_each_entry_safe(dev, tmp, &pci_devices, global_list) {
  1061. pci_insertion_sort_devices(dev, &sorted_devices);
  1062. }
  1063. list_splice(&sorted_devices, &pci_devices);
  1064. up_write(&pci_bus_sem);
  1065. }
  1066. void __init pci_sort_breadthfirst(void)
  1067. {
  1068. pci_sort_breadthfirst_devices();
  1069. pci_sort_breadthfirst_klist();
  1070. }