setup-bus.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * drivers/pci/setup-bus.c
  3. *
  4. * Extruded from code written by
  5. * Dave Rusling (david.rusling@reo.mts.dec.com)
  6. * David Mosberger (davidm@cs.arizona.edu)
  7. * David Miller (davem@redhat.com)
  8. *
  9. * Support routines for initializing a PCI subsystem.
  10. */
  11. /*
  12. * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
  13. * PCI-PCI bridges cleanup, sorted resource allocation.
  14. * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
  15. * Converted to allocation in 3 passes, which gives
  16. * tighter packing. Prefetchable range support.
  17. */
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/pci.h>
  22. #include <linux/errno.h>
  23. #include <linux/ioport.h>
  24. #include <linux/cache.h>
  25. #include <linux/slab.h>
  26. #define DEBUG_CONFIG 1
  27. #if DEBUG_CONFIG
  28. #define DBG(x...) printk(x)
  29. #else
  30. #define DBG(x...)
  31. #endif
  32. #define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
  33. static void __devinit
  34. pbus_assign_resources_sorted(struct pci_bus *bus)
  35. {
  36. struct pci_dev *dev;
  37. struct resource *res;
  38. struct resource_list head, *list, *tmp;
  39. int idx;
  40. head.next = NULL;
  41. list_for_each_entry(dev, &bus->devices, bus_list) {
  42. u16 class = dev->class >> 8;
  43. /* Don't touch classless devices or host bridges or ioapics. */
  44. if (class == PCI_CLASS_NOT_DEFINED ||
  45. class == PCI_CLASS_BRIDGE_HOST)
  46. continue;
  47. /* Don't touch ioapic devices already enabled by firmware */
  48. if (class == PCI_CLASS_SYSTEM_PIC) {
  49. u16 command;
  50. pci_read_config_word(dev, PCI_COMMAND, &command);
  51. if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
  52. continue;
  53. }
  54. pdev_sort_resources(dev, &head);
  55. }
  56. for (list = head.next; list;) {
  57. res = list->res;
  58. idx = res - &list->dev->resource[0];
  59. if (pci_assign_resource(list->dev, idx)) {
  60. res->start = 0;
  61. res->end = 0;
  62. res->flags = 0;
  63. }
  64. tmp = list;
  65. list = list->next;
  66. kfree(tmp);
  67. }
  68. }
  69. void pci_setup_cardbus(struct pci_bus *bus)
  70. {
  71. struct pci_dev *bridge = bus->self;
  72. struct pci_bus_region region;
  73. printk("PCI: Bus %d, cardbus bridge: %s\n",
  74. bus->number, pci_name(bridge));
  75. pcibios_resource_to_bus(bridge, &region, bus->resource[0]);
  76. if (bus->resource[0]->flags & IORESOURCE_IO) {
  77. /*
  78. * The IO resource is allocated a range twice as large as it
  79. * would normally need. This allows us to set both IO regs.
  80. */
  81. printk(" IO window: %08lx-%08lx\n",
  82. region.start, region.end);
  83. pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
  84. region.start);
  85. pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
  86. region.end);
  87. }
  88. pcibios_resource_to_bus(bridge, &region, bus->resource[1]);
  89. if (bus->resource[1]->flags & IORESOURCE_IO) {
  90. printk(" IO window: %08lx-%08lx\n",
  91. region.start, region.end);
  92. pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
  93. region.start);
  94. pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
  95. region.end);
  96. }
  97. pcibios_resource_to_bus(bridge, &region, bus->resource[2]);
  98. if (bus->resource[2]->flags & IORESOURCE_MEM) {
  99. printk(" PREFETCH window: %08lx-%08lx\n",
  100. region.start, region.end);
  101. pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
  102. region.start);
  103. pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
  104. region.end);
  105. }
  106. pcibios_resource_to_bus(bridge, &region, bus->resource[3]);
  107. if (bus->resource[3]->flags & IORESOURCE_MEM) {
  108. printk(" MEM window: %08lx-%08lx\n",
  109. region.start, region.end);
  110. pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
  111. region.start);
  112. pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
  113. region.end);
  114. }
  115. }
  116. EXPORT_SYMBOL(pci_setup_cardbus);
  117. /* Initialize bridges with base/limit values we have collected.
  118. PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
  119. requires that if there is no I/O ports or memory behind the
  120. bridge, corresponding range must be turned off by writing base
  121. value greater than limit to the bridge's base/limit registers.
  122. Note: care must be taken when updating I/O base/limit registers
  123. of bridges which support 32-bit I/O. This update requires two
  124. config space writes, so it's quite possible that an I/O window of
  125. the bridge will have some undesirable address (e.g. 0) after the
  126. first write. Ditto 64-bit prefetchable MMIO. */
  127. static void __devinit
  128. pci_setup_bridge(struct pci_bus *bus)
  129. {
  130. struct pci_dev *bridge = bus->self;
  131. struct pci_bus_region region;
  132. u32 l, io_upper16;
  133. DBG(KERN_INFO "PCI: Bridge: %s\n", pci_name(bridge));
  134. /* Set up the top and bottom of the PCI I/O segment for this bus. */
  135. pcibios_resource_to_bus(bridge, &region, bus->resource[0]);
  136. if (bus->resource[0]->flags & IORESOURCE_IO) {
  137. pci_read_config_dword(bridge, PCI_IO_BASE, &l);
  138. l &= 0xffff0000;
  139. l |= (region.start >> 8) & 0x00f0;
  140. l |= region.end & 0xf000;
  141. /* Set up upper 16 bits of I/O base/limit. */
  142. io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
  143. DBG(KERN_INFO " IO window: %04lx-%04lx\n",
  144. region.start, region.end);
  145. }
  146. else {
  147. /* Clear upper 16 bits of I/O base/limit. */
  148. io_upper16 = 0;
  149. l = 0x00f0;
  150. DBG(KERN_INFO " IO window: disabled.\n");
  151. }
  152. /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
  153. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
  154. /* Update lower 16 bits of I/O base/limit. */
  155. pci_write_config_dword(bridge, PCI_IO_BASE, l);
  156. /* Update upper 16 bits of I/O base/limit. */
  157. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
  158. /* Set up the top and bottom of the PCI Memory segment
  159. for this bus. */
  160. pcibios_resource_to_bus(bridge, &region, bus->resource[1]);
  161. if (bus->resource[1]->flags & IORESOURCE_MEM) {
  162. l = (region.start >> 16) & 0xfff0;
  163. l |= region.end & 0xfff00000;
  164. DBG(KERN_INFO " MEM window: %08lx-%08lx\n",
  165. region.start, region.end);
  166. }
  167. else {
  168. l = 0x0000fff0;
  169. DBG(KERN_INFO " MEM window: disabled.\n");
  170. }
  171. pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
  172. /* Clear out the upper 32 bits of PREF limit.
  173. If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
  174. disables PREF range, which is ok. */
  175. pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
  176. /* Set up PREF base/limit. */
  177. pcibios_resource_to_bus(bridge, &region, bus->resource[2]);
  178. if (bus->resource[2]->flags & IORESOURCE_PREFETCH) {
  179. l = (region.start >> 16) & 0xfff0;
  180. l |= region.end & 0xfff00000;
  181. DBG(KERN_INFO " PREFETCH window: %08lx-%08lx\n",
  182. region.start, region.end);
  183. }
  184. else {
  185. l = 0x0000fff0;
  186. DBG(KERN_INFO " PREFETCH window: disabled.\n");
  187. }
  188. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
  189. /* Clear out the upper 32 bits of PREF base. */
  190. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 0);
  191. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
  192. }
  193. /* Check whether the bridge supports optional I/O and
  194. prefetchable memory ranges. If not, the respective
  195. base/limit registers must be read-only and read as 0. */
  196. static void __devinit
  197. pci_bridge_check_ranges(struct pci_bus *bus)
  198. {
  199. u16 io;
  200. u32 pmem;
  201. struct pci_dev *bridge = bus->self;
  202. struct resource *b_res;
  203. b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
  204. b_res[1].flags |= IORESOURCE_MEM;
  205. pci_read_config_word(bridge, PCI_IO_BASE, &io);
  206. if (!io) {
  207. pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
  208. pci_read_config_word(bridge, PCI_IO_BASE, &io);
  209. pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
  210. }
  211. if (io)
  212. b_res[0].flags |= IORESOURCE_IO;
  213. /* DECchip 21050 pass 2 errata: the bridge may miss an address
  214. disconnect boundary by one PCI data phase.
  215. Workaround: do not use prefetching on this device. */
  216. if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
  217. return;
  218. pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
  219. if (!pmem) {
  220. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
  221. 0xfff0fff0);
  222. pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
  223. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
  224. }
  225. if (pmem)
  226. b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
  227. }
  228. /* Helper function for sizing routines: find first available
  229. bus resource of a given type. Note: we intentionally skip
  230. the bus resources which have already been assigned (that is,
  231. have non-NULL parent resource). */
  232. static struct resource * __devinit
  233. find_free_bus_resource(struct pci_bus *bus, unsigned long type)
  234. {
  235. int i;
  236. struct resource *r;
  237. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  238. IORESOURCE_PREFETCH;
  239. for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
  240. r = bus->resource[i];
  241. if (r == &ioport_resource || r == &iomem_resource)
  242. continue;
  243. if (r && (r->flags & type_mask) == type && !r->parent)
  244. return r;
  245. }
  246. return NULL;
  247. }
  248. /* Sizing the IO windows of the PCI-PCI bridge is trivial,
  249. since these windows have 4K granularity and the IO ranges
  250. of non-bridge PCI devices are limited to 256 bytes.
  251. We must be careful with the ISA aliasing though. */
  252. static void __devinit
  253. pbus_size_io(struct pci_bus *bus)
  254. {
  255. struct pci_dev *dev;
  256. struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
  257. unsigned long size = 0, size1 = 0;
  258. if (!b_res)
  259. return;
  260. list_for_each_entry(dev, &bus->devices, bus_list) {
  261. int i;
  262. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  263. struct resource *r = &dev->resource[i];
  264. unsigned long r_size;
  265. if (r->parent || !(r->flags & IORESOURCE_IO))
  266. continue;
  267. r_size = r->end - r->start + 1;
  268. if (r_size < 0x400)
  269. /* Might be re-aligned for ISA */
  270. size += r_size;
  271. else
  272. size1 += r_size;
  273. }
  274. }
  275. /* To be fixed in 2.5: we should have sort of HAVE_ISA
  276. flag in the struct pci_bus. */
  277. #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
  278. size = (size & 0xff) + ((size & ~0xffUL) << 2);
  279. #endif
  280. size = ROUND_UP(size + size1, 4096);
  281. if (!size) {
  282. b_res->flags = 0;
  283. return;
  284. }
  285. /* Alignment of the IO window is always 4K */
  286. b_res->start = 4096;
  287. b_res->end = b_res->start + size - 1;
  288. }
  289. /* Calculate the size of the bus and minimal alignment which
  290. guarantees that all child resources fit in this size. */
  291. static int __devinit
  292. pbus_size_mem(struct pci_bus *bus, unsigned long mask, unsigned long type)
  293. {
  294. struct pci_dev *dev;
  295. unsigned long min_align, align, size;
  296. unsigned long aligns[12]; /* Alignments from 1Mb to 2Gb */
  297. int order, max_order;
  298. struct resource *b_res = find_free_bus_resource(bus, type);
  299. if (!b_res)
  300. return 0;
  301. memset(aligns, 0, sizeof(aligns));
  302. max_order = 0;
  303. size = 0;
  304. list_for_each_entry(dev, &bus->devices, bus_list) {
  305. int i;
  306. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  307. struct resource *r = &dev->resource[i];
  308. unsigned long r_size;
  309. if (r->parent || (r->flags & mask) != type)
  310. continue;
  311. r_size = r->end - r->start + 1;
  312. /* For bridges size != alignment */
  313. align = (i < PCI_BRIDGE_RESOURCES) ? r_size : r->start;
  314. order = __ffs(align) - 20;
  315. if (order > 11) {
  316. printk(KERN_WARNING "PCI: region %s/%d "
  317. "too large: %llx-%llx\n",
  318. pci_name(dev), i,
  319. (unsigned long long)r->start,
  320. (unsigned long long)r->end);
  321. r->flags = 0;
  322. continue;
  323. }
  324. size += r_size;
  325. if (order < 0)
  326. order = 0;
  327. /* Exclude ranges with size > align from
  328. calculation of the alignment. */
  329. if (r_size == align)
  330. aligns[order] += align;
  331. if (order > max_order)
  332. max_order = order;
  333. }
  334. }
  335. align = 0;
  336. min_align = 0;
  337. for (order = 0; order <= max_order; order++) {
  338. unsigned long align1 = 1UL << (order + 20);
  339. if (!align)
  340. min_align = align1;
  341. else if (ROUND_UP(align + min_align, min_align) < align1)
  342. min_align = align1 >> 1;
  343. align += aligns[order];
  344. }
  345. size = ROUND_UP(size, min_align);
  346. if (!size) {
  347. b_res->flags = 0;
  348. return 1;
  349. }
  350. b_res->start = min_align;
  351. b_res->end = size + min_align - 1;
  352. return 1;
  353. }
  354. static void __devinit
  355. pci_bus_size_cardbus(struct pci_bus *bus)
  356. {
  357. struct pci_dev *bridge = bus->self;
  358. struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
  359. u16 ctrl;
  360. /*
  361. * Reserve some resources for CardBus. We reserve
  362. * a fixed amount of bus space for CardBus bridges.
  363. */
  364. b_res[0].start = pci_cardbus_io_size;
  365. b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1;
  366. b_res[0].flags |= IORESOURCE_IO;
  367. b_res[1].start = pci_cardbus_io_size;
  368. b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1;
  369. b_res[1].flags |= IORESOURCE_IO;
  370. /*
  371. * Check whether prefetchable memory is supported
  372. * by this bridge.
  373. */
  374. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  375. if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
  376. ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
  377. pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
  378. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  379. }
  380. /*
  381. * If we have prefetchable memory support, allocate
  382. * two regions. Otherwise, allocate one region of
  383. * twice the size.
  384. */
  385. if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
  386. b_res[2].start = pci_cardbus_mem_size;
  387. b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1;
  388. b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
  389. b_res[3].start = pci_cardbus_mem_size;
  390. b_res[3].end = b_res[3].start + pci_cardbus_mem_size - 1;
  391. b_res[3].flags |= IORESOURCE_MEM;
  392. } else {
  393. b_res[3].start = pci_cardbus_mem_size * 2;
  394. b_res[3].end = b_res[3].start + pci_cardbus_mem_size * 2 - 1;
  395. b_res[3].flags |= IORESOURCE_MEM;
  396. }
  397. }
  398. void __devinit
  399. pci_bus_size_bridges(struct pci_bus *bus)
  400. {
  401. struct pci_dev *dev;
  402. unsigned long mask, prefmask;
  403. list_for_each_entry(dev, &bus->devices, bus_list) {
  404. struct pci_bus *b = dev->subordinate;
  405. if (!b)
  406. continue;
  407. switch (dev->class >> 8) {
  408. case PCI_CLASS_BRIDGE_CARDBUS:
  409. pci_bus_size_cardbus(b);
  410. break;
  411. case PCI_CLASS_BRIDGE_PCI:
  412. default:
  413. pci_bus_size_bridges(b);
  414. break;
  415. }
  416. }
  417. /* The root bus? */
  418. if (!bus->self)
  419. return;
  420. switch (bus->self->class >> 8) {
  421. case PCI_CLASS_BRIDGE_CARDBUS:
  422. /* don't size cardbuses yet. */
  423. break;
  424. case PCI_CLASS_BRIDGE_PCI:
  425. pci_bridge_check_ranges(bus);
  426. default:
  427. pbus_size_io(bus);
  428. /* If the bridge supports prefetchable range, size it
  429. separately. If it doesn't, or its prefetchable window
  430. has already been allocated by arch code, try
  431. non-prefetchable range for both types of PCI memory
  432. resources. */
  433. mask = IORESOURCE_MEM;
  434. prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
  435. if (pbus_size_mem(bus, prefmask, prefmask))
  436. mask = prefmask; /* Success, size non-prefetch only. */
  437. pbus_size_mem(bus, mask, IORESOURCE_MEM);
  438. break;
  439. }
  440. }
  441. EXPORT_SYMBOL(pci_bus_size_bridges);
  442. void __devinit
  443. pci_bus_assign_resources(struct pci_bus *bus)
  444. {
  445. struct pci_bus *b;
  446. struct pci_dev *dev;
  447. pbus_assign_resources_sorted(bus);
  448. list_for_each_entry(dev, &bus->devices, bus_list) {
  449. b = dev->subordinate;
  450. if (!b)
  451. continue;
  452. pci_bus_assign_resources(b);
  453. switch (dev->class >> 8) {
  454. case PCI_CLASS_BRIDGE_PCI:
  455. pci_setup_bridge(b);
  456. break;
  457. case PCI_CLASS_BRIDGE_CARDBUS:
  458. pci_setup_cardbus(b);
  459. break;
  460. default:
  461. printk(KERN_INFO "PCI: not setting up bridge %s "
  462. "for bus %d\n", pci_name(dev), b->number);
  463. break;
  464. }
  465. }
  466. }
  467. EXPORT_SYMBOL(pci_bus_assign_resources);
  468. void __init
  469. pci_assign_unassigned_resources(void)
  470. {
  471. struct pci_bus *bus;
  472. /* Depth first, calculate sizes and alignments of all
  473. subordinate buses. */
  474. list_for_each_entry(bus, &pci_root_buses, node) {
  475. pci_bus_size_bridges(bus);
  476. }
  477. /* Depth last, allocate resources and update the hardware. */
  478. list_for_each_entry(bus, &pci_root_buses, node) {
  479. pci_bus_assign_resources(bus);
  480. pci_enable_bridges(bus);
  481. }
  482. }