probe.c 26 KB

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