probe.c 28 KB

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