probe.c 27 KB

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