probe.c 25 KB

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