probe.c 25 KB

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