probe.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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 = 3; i < PCI_BUS_NUM_RESOURCES; i++)
  218. child->resource[i] = child->parent->resource[i - 3];
  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. spin_lock(&pci_bus_lock);
  334. list_add_tail(&child->node, &parent->children);
  335. spin_unlock(&pci_bus_lock);
  336. }
  337. return child;
  338. }
  339. static void pci_enable_crs(struct pci_dev *dev)
  340. {
  341. u16 cap, rpctl;
  342. int rpcap = pci_find_capability(dev, PCI_CAP_ID_EXP);
  343. if (!rpcap)
  344. return;
  345. pci_read_config_word(dev, rpcap + PCI_CAP_FLAGS, &cap);
  346. if (((cap & PCI_EXP_FLAGS_TYPE) >> 4) != PCI_EXP_TYPE_ROOT_PORT)
  347. return;
  348. pci_read_config_word(dev, rpcap + PCI_EXP_RTCTL, &rpctl);
  349. rpctl |= PCI_EXP_RTCTL_CRSSVE;
  350. pci_write_config_word(dev, rpcap + PCI_EXP_RTCTL, rpctl);
  351. }
  352. static void __devinit pci_fixup_parent_subordinate_busnr(struct pci_bus *child, int max)
  353. {
  354. struct pci_bus *parent = child->parent;
  355. while (parent->parent && parent->subordinate < max) {
  356. parent->subordinate = max;
  357. pci_write_config_byte(parent->self, PCI_SUBORDINATE_BUS, max);
  358. parent = parent->parent;
  359. }
  360. }
  361. unsigned int __devinit pci_scan_child_bus(struct pci_bus *bus);
  362. /*
  363. * If it's a bridge, configure it and scan the bus behind it.
  364. * For CardBus bridges, we don't scan behind as the devices will
  365. * be handled by the bridge driver itself.
  366. *
  367. * We need to process bridges in two passes -- first we scan those
  368. * already configured by the BIOS and after we are done with all of
  369. * them, we proceed to assigning numbers to the remaining buses in
  370. * order to avoid overlaps between old and new bus numbers.
  371. */
  372. int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev * dev, int max, int pass)
  373. {
  374. struct pci_bus *child;
  375. int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
  376. u32 buses, i;
  377. u16 bctl;
  378. pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
  379. pr_debug("PCI: Scanning behind PCI bridge %s, config %06x, pass %d\n",
  380. pci_name(dev), buses & 0xffffff, pass);
  381. /* Disable MasterAbortMode during probing to avoid reporting
  382. of bus errors (in some architectures) */
  383. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
  384. pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
  385. bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
  386. pci_enable_crs(dev);
  387. if ((buses & 0xffff00) && !pcibios_assign_all_busses() && !is_cardbus) {
  388. unsigned int cmax, busnr;
  389. /*
  390. * Bus already configured by firmware, process it in the first
  391. * pass and just note the configuration.
  392. */
  393. if (pass)
  394. return max;
  395. busnr = (buses >> 8) & 0xFF;
  396. /*
  397. * If we already got to this bus through a different bridge,
  398. * ignore it. This can happen with the i450NX chipset.
  399. */
  400. if (pci_find_bus(pci_domain_nr(bus), busnr)) {
  401. printk(KERN_INFO "PCI: Bus %04x:%02x already known\n",
  402. pci_domain_nr(bus), busnr);
  403. return max;
  404. }
  405. child = pci_add_new_bus(bus, dev, busnr);
  406. if (!child)
  407. return max;
  408. child->primary = buses & 0xFF;
  409. child->subordinate = (buses >> 16) & 0xFF;
  410. child->bridge_ctl = bctl;
  411. cmax = pci_scan_child_bus(child);
  412. if (cmax > max)
  413. max = cmax;
  414. if (child->subordinate > max)
  415. max = child->subordinate;
  416. } else {
  417. /*
  418. * We need to assign a number to this bus which we always
  419. * do in the second pass.
  420. */
  421. if (!pass)
  422. return max;
  423. /* Clear errors */
  424. pci_write_config_word(dev, PCI_STATUS, 0xffff);
  425. /* Prevent assigning a bus number that already exists.
  426. * This can happen when a bridge is hot-plugged */
  427. if (pci_find_bus(pci_domain_nr(bus), max+1))
  428. return max;
  429. child = pci_add_new_bus(bus, dev, ++max);
  430. buses = (buses & 0xff000000)
  431. | ((unsigned int)(child->primary) << 0)
  432. | ((unsigned int)(child->secondary) << 8)
  433. | ((unsigned int)(child->subordinate) << 16);
  434. /*
  435. * yenta.c forces a secondary latency timer of 176.
  436. * Copy that behaviour here.
  437. */
  438. if (is_cardbus) {
  439. buses &= ~0xff000000;
  440. buses |= CARDBUS_LATENCY_TIMER << 24;
  441. }
  442. /*
  443. * We need to blast all three values with a single write.
  444. */
  445. pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
  446. if (!is_cardbus) {
  447. child->bridge_ctl = bctl | PCI_BRIDGE_CTL_NO_ISA;
  448. /*
  449. * Adjust subordinate busnr in parent buses.
  450. * We do this before scanning for children because
  451. * some devices may not be detected if the bios
  452. * was lazy.
  453. */
  454. pci_fixup_parent_subordinate_busnr(child, max);
  455. /* Now we can scan all subordinate buses... */
  456. max = pci_scan_child_bus(child);
  457. } else {
  458. /*
  459. * For CardBus bridges, we leave 4 bus numbers
  460. * as cards with a PCI-to-PCI bridge can be
  461. * inserted later.
  462. */
  463. for (i=0; i<CARDBUS_RESERVE_BUSNR; i++)
  464. if (pci_find_bus(pci_domain_nr(bus),
  465. max+i+1))
  466. break;
  467. max += i;
  468. pci_fixup_parent_subordinate_busnr(child, max);
  469. }
  470. /*
  471. * Set the subordinate bus number to its real value.
  472. */
  473. child->subordinate = max;
  474. pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
  475. }
  476. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
  477. sprintf(child->name, (is_cardbus ? "PCI CardBus #%02x" : "PCI Bus #%02x"), child->number);
  478. return max;
  479. }
  480. /*
  481. * Read interrupt line and base address registers.
  482. * The architecture-dependent code can tweak these, of course.
  483. */
  484. static void pci_read_irq(struct pci_dev *dev)
  485. {
  486. unsigned char irq;
  487. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
  488. if (irq)
  489. pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
  490. dev->irq = irq;
  491. }
  492. /**
  493. * pci_setup_device - fill in class and map information of a device
  494. * @dev: the device structure to fill
  495. *
  496. * Initialize the device structure with information about the device's
  497. * vendor,class,memory and IO-space addresses,IRQ lines etc.
  498. * Called at initialisation of the PCI subsystem and by CardBus services.
  499. * Returns 0 on success and -1 if unknown type of device (not normal, bridge
  500. * or CardBus).
  501. */
  502. static int pci_setup_device(struct pci_dev * dev)
  503. {
  504. u32 class;
  505. sprintf(pci_name(dev), "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
  506. dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
  507. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
  508. class >>= 8; /* upper 3 bytes */
  509. dev->class = class;
  510. class >>= 8;
  511. pr_debug("PCI: Found %s [%04x/%04x] %06x %02x\n", pci_name(dev),
  512. dev->vendor, dev->device, class, dev->hdr_type);
  513. /* "Unknown power state" */
  514. dev->current_state = PCI_UNKNOWN;
  515. /* Early fixups, before probing the BARs */
  516. pci_fixup_device(pci_fixup_early, dev);
  517. class = dev->class >> 8;
  518. switch (dev->hdr_type) { /* header type */
  519. case PCI_HEADER_TYPE_NORMAL: /* standard header */
  520. if (class == PCI_CLASS_BRIDGE_PCI)
  521. goto bad;
  522. pci_read_irq(dev);
  523. pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
  524. pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
  525. pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
  526. break;
  527. case PCI_HEADER_TYPE_BRIDGE: /* bridge header */
  528. if (class != PCI_CLASS_BRIDGE_PCI)
  529. goto bad;
  530. /* The PCI-to-PCI bridge spec requires that subtractive
  531. decoding (i.e. transparent) bridge must have programming
  532. interface code of 0x01. */
  533. dev->transparent = ((dev->class & 0xff) == 1);
  534. pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
  535. break;
  536. case PCI_HEADER_TYPE_CARDBUS: /* CardBus bridge header */
  537. if (class != PCI_CLASS_BRIDGE_CARDBUS)
  538. goto bad;
  539. pci_read_irq(dev);
  540. pci_read_bases(dev, 1, 0);
  541. pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
  542. pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
  543. break;
  544. default: /* unknown header */
  545. printk(KERN_ERR "PCI: device %s has unknown header type %02x, ignoring.\n",
  546. pci_name(dev), dev->hdr_type);
  547. return -1;
  548. bad:
  549. printk(KERN_ERR "PCI: %s: class %x doesn't match header type %02x. Ignoring class.\n",
  550. pci_name(dev), class, dev->hdr_type);
  551. dev->class = PCI_CLASS_NOT_DEFINED;
  552. }
  553. /* We found a fine healthy device, go go go... */
  554. return 0;
  555. }
  556. /**
  557. * pci_release_dev - free a pci device structure when all users of it are finished.
  558. * @dev: device that's been disconnected
  559. *
  560. * Will be called only by the device core when all users of this pci device are
  561. * done.
  562. */
  563. static void pci_release_dev(struct device *dev)
  564. {
  565. struct pci_dev *pci_dev;
  566. pci_dev = to_pci_dev(dev);
  567. kfree(pci_dev);
  568. }
  569. /**
  570. * pci_cfg_space_size - get the configuration space size of the PCI device.
  571. *
  572. * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
  573. * have 4096 bytes. Even if the device is capable, that doesn't mean we can
  574. * access it. Maybe we don't have a way to generate extended config space
  575. * accesses, or the device is behind a reverse Express bridge. So we try
  576. * reading the dword at 0x100 which must either be 0 or a valid extended
  577. * capability header.
  578. */
  579. static int pci_cfg_space_size(struct pci_dev *dev)
  580. {
  581. int pos;
  582. u32 status;
  583. pos = pci_find_capability(dev, PCI_CAP_ID_EXP);
  584. if (!pos) {
  585. pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  586. if (!pos)
  587. goto fail;
  588. pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
  589. if (!(status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ)))
  590. goto fail;
  591. }
  592. if (pci_read_config_dword(dev, 256, &status) != PCIBIOS_SUCCESSFUL)
  593. goto fail;
  594. if (status == 0xffffffff)
  595. goto fail;
  596. return PCI_CFG_SPACE_EXP_SIZE;
  597. fail:
  598. return PCI_CFG_SPACE_SIZE;
  599. }
  600. static void pci_release_bus_bridge_dev(struct device *dev)
  601. {
  602. kfree(dev);
  603. }
  604. /*
  605. * Read the config data for a PCI device, sanity-check it
  606. * and fill in the dev structure...
  607. */
  608. static struct pci_dev * __devinit
  609. pci_scan_device(struct pci_bus *bus, int devfn)
  610. {
  611. struct pci_dev *dev;
  612. u32 l;
  613. u8 hdr_type;
  614. int delay = 1;
  615. if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l))
  616. return NULL;
  617. /* some broken boards return 0 or ~0 if a slot is empty: */
  618. if (l == 0xffffffff || l == 0x00000000 ||
  619. l == 0x0000ffff || l == 0xffff0000)
  620. return NULL;
  621. /* Configuration request Retry Status */
  622. while (l == 0xffff0001) {
  623. msleep(delay);
  624. delay *= 2;
  625. if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l))
  626. return NULL;
  627. /* Card hasn't responded in 60 seconds? Must be stuck. */
  628. if (delay > 60 * 1000) {
  629. printk(KERN_WARNING "Device %04x:%02x:%02x.%d not "
  630. "responding\n", pci_domain_nr(bus),
  631. bus->number, PCI_SLOT(devfn),
  632. PCI_FUNC(devfn));
  633. return NULL;
  634. }
  635. }
  636. if (pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type))
  637. return NULL;
  638. dev = kmalloc(sizeof(struct pci_dev), GFP_KERNEL);
  639. if (!dev)
  640. return NULL;
  641. memset(dev, 0, sizeof(struct pci_dev));
  642. dev->bus = bus;
  643. dev->sysdata = bus->sysdata;
  644. dev->dev.parent = bus->bridge;
  645. dev->dev.bus = &pci_bus_type;
  646. dev->devfn = devfn;
  647. dev->hdr_type = hdr_type & 0x7f;
  648. dev->multifunction = !!(hdr_type & 0x80);
  649. dev->vendor = l & 0xffff;
  650. dev->device = (l >> 16) & 0xffff;
  651. dev->cfg_size = pci_cfg_space_size(dev);
  652. /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
  653. set this higher, assuming the system even supports it. */
  654. dev->dma_mask = 0xffffffff;
  655. if (pci_setup_device(dev) < 0) {
  656. kfree(dev);
  657. return NULL;
  658. }
  659. device_initialize(&dev->dev);
  660. dev->dev.release = pci_release_dev;
  661. pci_dev_get(dev);
  662. dev->dev.dma_mask = &dev->dma_mask;
  663. dev->dev.coherent_dma_mask = 0xffffffffull;
  664. return dev;
  665. }
  666. struct pci_dev * __devinit
  667. pci_scan_single_device(struct pci_bus *bus, int devfn)
  668. {
  669. struct pci_dev *dev;
  670. dev = pci_scan_device(bus, devfn);
  671. pci_scan_msi_device(dev);
  672. if (!dev)
  673. return NULL;
  674. /* Fix up broken headers */
  675. pci_fixup_device(pci_fixup_header, dev);
  676. /*
  677. * Add the device to our list of discovered devices
  678. * and the bus list for fixup functions, etc.
  679. */
  680. INIT_LIST_HEAD(&dev->global_list);
  681. spin_lock(&pci_bus_lock);
  682. list_add_tail(&dev->bus_list, &bus->devices);
  683. spin_unlock(&pci_bus_lock);
  684. return dev;
  685. }
  686. /**
  687. * pci_scan_slot - scan a PCI slot on a bus for devices.
  688. * @bus: PCI bus to scan
  689. * @devfn: slot number to scan (must have zero function.)
  690. *
  691. * Scan a PCI slot on the specified PCI bus for devices, adding
  692. * discovered devices to the @bus->devices list. New devices
  693. * will have an empty dev->global_list head.
  694. */
  695. int __devinit pci_scan_slot(struct pci_bus *bus, int devfn)
  696. {
  697. int func, nr = 0;
  698. int scan_all_fns;
  699. scan_all_fns = pcibios_scan_all_fns(bus, devfn);
  700. for (func = 0; func < 8; func++, devfn++) {
  701. struct pci_dev *dev;
  702. dev = pci_scan_single_device(bus, devfn);
  703. if (dev) {
  704. nr++;
  705. /*
  706. * If this is a single function device,
  707. * don't scan past the first function.
  708. */
  709. if (!dev->multifunction) {
  710. if (func > 0) {
  711. dev->multifunction = 1;
  712. } else {
  713. break;
  714. }
  715. }
  716. } else {
  717. if (func == 0 && !scan_all_fns)
  718. break;
  719. }
  720. }
  721. return nr;
  722. }
  723. unsigned int __devinit pci_scan_child_bus(struct pci_bus *bus)
  724. {
  725. unsigned int devfn, pass, max = bus->secondary;
  726. struct pci_dev *dev;
  727. pr_debug("PCI: Scanning bus %04x:%02x\n", pci_domain_nr(bus), bus->number);
  728. /* Go find them, Rover! */
  729. for (devfn = 0; devfn < 0x100; devfn += 8)
  730. pci_scan_slot(bus, devfn);
  731. /*
  732. * After performing arch-dependent fixup of the bus, look behind
  733. * all PCI-to-PCI bridges on this bus.
  734. */
  735. pr_debug("PCI: Fixups for bus %04x:%02x\n", pci_domain_nr(bus), bus->number);
  736. pcibios_fixup_bus(bus);
  737. for (pass=0; pass < 2; pass++)
  738. list_for_each_entry(dev, &bus->devices, bus_list) {
  739. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  740. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  741. max = pci_scan_bridge(bus, dev, max, pass);
  742. }
  743. /*
  744. * We've scanned the bus and so we know all about what's on
  745. * the other side of any bridges that may be on this bus plus
  746. * any devices.
  747. *
  748. * Return how far we've got finding sub-buses.
  749. */
  750. pr_debug("PCI: Bus scan for %04x:%02x returning with max=%02x\n",
  751. pci_domain_nr(bus), bus->number, max);
  752. return max;
  753. }
  754. unsigned int __devinit pci_do_scan_bus(struct pci_bus *bus)
  755. {
  756. unsigned int max;
  757. max = pci_scan_child_bus(bus);
  758. /*
  759. * Make the discovered devices available.
  760. */
  761. pci_bus_add_devices(bus);
  762. return max;
  763. }
  764. struct pci_bus * __devinit pci_scan_bus_parented(struct device *parent, int bus, struct pci_ops *ops, void *sysdata)
  765. {
  766. int error;
  767. struct pci_bus *b;
  768. struct device *dev;
  769. b = pci_alloc_bus();
  770. if (!b)
  771. return NULL;
  772. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  773. if (!dev){
  774. kfree(b);
  775. return NULL;
  776. }
  777. b->sysdata = sysdata;
  778. b->ops = ops;
  779. if (pci_find_bus(pci_domain_nr(b), bus)) {
  780. /* If we already got to this bus through a different bridge, ignore it */
  781. pr_debug("PCI: Bus %04x:%02x already known\n", pci_domain_nr(b), bus);
  782. goto err_out;
  783. }
  784. spin_lock(&pci_bus_lock);
  785. list_add_tail(&b->node, &pci_root_buses);
  786. spin_unlock(&pci_bus_lock);
  787. memset(dev, 0, sizeof(*dev));
  788. dev->parent = parent;
  789. dev->release = pci_release_bus_bridge_dev;
  790. sprintf(dev->bus_id, "pci%04x:%02x", pci_domain_nr(b), bus);
  791. error = device_register(dev);
  792. if (error)
  793. goto dev_reg_err;
  794. b->bridge = get_device(dev);
  795. b->class_dev.class = &pcibus_class;
  796. sprintf(b->class_dev.class_id, "%04x:%02x", pci_domain_nr(b), bus);
  797. error = class_device_register(&b->class_dev);
  798. if (error)
  799. goto class_dev_reg_err;
  800. error = class_device_create_file(&b->class_dev, &class_device_attr_cpuaffinity);
  801. if (error)
  802. goto class_dev_create_file_err;
  803. /* Create legacy_io and legacy_mem files for this bus */
  804. pci_create_legacy_files(b);
  805. error = sysfs_create_link(&b->class_dev.kobj, &b->bridge->kobj, "bridge");
  806. if (error)
  807. goto sys_create_link_err;
  808. b->number = b->secondary = bus;
  809. b->resource[0] = &ioport_resource;
  810. b->resource[1] = &iomem_resource;
  811. b->subordinate = pci_scan_child_bus(b);
  812. return b;
  813. sys_create_link_err:
  814. class_device_remove_file(&b->class_dev, &class_device_attr_cpuaffinity);
  815. class_dev_create_file_err:
  816. class_device_unregister(&b->class_dev);
  817. class_dev_reg_err:
  818. device_unregister(dev);
  819. dev_reg_err:
  820. spin_lock(&pci_bus_lock);
  821. list_del(&b->node);
  822. spin_unlock(&pci_bus_lock);
  823. err_out:
  824. kfree(dev);
  825. kfree(b);
  826. return NULL;
  827. }
  828. EXPORT_SYMBOL(pci_scan_bus_parented);
  829. #ifdef CONFIG_HOTPLUG
  830. EXPORT_SYMBOL(pci_add_new_bus);
  831. EXPORT_SYMBOL(pci_do_scan_bus);
  832. EXPORT_SYMBOL(pci_scan_slot);
  833. EXPORT_SYMBOL(pci_scan_bridge);
  834. EXPORT_SYMBOL(pci_scan_single_device);
  835. EXPORT_SYMBOL_GPL(pci_scan_child_bus);
  836. #endif