probe.c 33 KB

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