probe.c 28 KB

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