setup-bus.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:11k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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/pci.h>
  21. #include <linux/errno.h>
  22. #include <linux/ioport.h>
  23. #include <linux/cache.h>
  24. #include <linux/slab.h>
  25. #define DEBUG_CONFIG 1
  26. #if DEBUG_CONFIG
  27. # define DBGC(args)     printk args
  28. #else
  29. # define DBGC(args)
  30. #endif
  31. #define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
  32. static int __init
  33. pbus_assign_resources_sorted(struct pci_bus *bus)
  34. {
  35. struct list_head *ln;
  36. struct resource *res;
  37. struct resource_list head, *list, *tmp;
  38. int idx, found_vga = 0;
  39. head.next = NULL;
  40. for (ln=bus->devices.next; ln != &bus->devices; ln=ln->next) {
  41. struct pci_dev *dev = pci_dev_b(ln);
  42. u16 class = dev->class >> 8;
  43. u16 cmd;
  44. /* First, disable the device to avoid side
  45.    effects of possibly overlapping I/O and
  46.    memory ranges.
  47.    Leave VGA enabled - for obvious reason. :-)
  48.    Same with all sorts of bridges - they may
  49.    have VGA behind them.  */
  50. if (class == PCI_CLASS_DISPLAY_VGA
  51. || class == PCI_CLASS_NOT_DEFINED_VGA)
  52. found_vga = 1;
  53. else if (class >> 8 != PCI_BASE_CLASS_BRIDGE) {
  54. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  55. cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY
  56. | PCI_COMMAND_MASTER);
  57. pci_write_config_word(dev, PCI_COMMAND, cmd);
  58. }
  59. pdev_sort_resources(dev, &head);
  60. }
  61. for (list = head.next; list;) {
  62. res = list->res;
  63. idx = res - &list->dev->resource[0];
  64. pci_assign_resource(list->dev, idx);
  65. tmp = list;
  66. list = list->next;
  67. kfree(tmp);
  68. }
  69. return found_vga;
  70. }
  71. /* Initialize bridges with base/limit values we have collected.
  72.    PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
  73.    requires that if there is no I/O ports or memory behind the
  74.    bridge, corresponding range must be turned off by writing base
  75.    value greater than limit to the bridge's base/limit registers.  */
  76. static void __init
  77. pci_setup_bridge(struct pci_bus *bus)
  78. {
  79. struct pbus_set_ranges_data ranges;
  80. struct pci_dev *bridge = bus->self;
  81. u32 l;
  82. if (!bridge || (bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  83. return;
  84. ranges.io_start = bus->resource[0]->start;
  85. ranges.io_end = bus->resource[0]->end;
  86. ranges.mem_start = bus->resource[1]->start;
  87. ranges.mem_end = bus->resource[1]->end;
  88. ranges.prefetch_start = bus->resource[2]->start;
  89. ranges.prefetch_end = bus->resource[2]->end;
  90. pcibios_fixup_pbus_ranges(bus, &ranges);
  91. DBGC((KERN_INFO "PCI: Bus %d, bridge: %sn",
  92. bus->number, bridge->name));
  93. /* Set up the top and bottom of the PCI I/O segment for this bus. */
  94. if (bus->resource[0]->flags & IORESOURCE_IO) {
  95. pci_read_config_dword(bridge, PCI_IO_BASE, &l);
  96. l &= 0xffff0000;
  97. l |= (ranges.io_start >> 8) & 0x00f0;
  98. l |= ranges.io_end & 0xf000;
  99. /* Set up upper 16 bits of I/O base/limit. */
  100. pci_write_config_word(bridge, PCI_IO_BASE_UPPER16,
  101.       ranges.io_start >> 16);
  102. pci_write_config_word(bridge, PCI_IO_LIMIT_UPPER16,
  103.       ranges.io_end >> 16);
  104. DBGC((KERN_INFO "  IO window: %04lx-%04lxn",
  105. ranges.io_start, ranges.io_end));
  106. }
  107. else {
  108. /* Clear upper 16 bits of I/O base/limit. */
  109. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0);
  110. l = 0x00f0;
  111. DBGC((KERN_INFO "  IO window: disabled.n"));
  112. }
  113. pci_write_config_dword(bridge, PCI_IO_BASE, l);
  114. /* Set up the top and bottom of the PCI Memory segment
  115.    for this bus. */
  116. if (bus->resource[1]->flags & IORESOURCE_MEM) {
  117. l = (ranges.mem_start >> 16) & 0xfff0;
  118. l |= ranges.mem_end & 0xfff00000;
  119. DBGC((KERN_INFO "  MEM window: %08lx-%08lxn",
  120. ranges.mem_start, ranges.mem_end));
  121. }
  122. else {
  123. l = 0x0000fff0;
  124. DBGC((KERN_INFO "  MEM window: disabled.n"));
  125. }
  126. pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
  127. /* Clear out the upper 32 bits of PREF base/limit. */
  128. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 0);
  129. pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
  130. /* Set up PREF base/limit. */
  131. if (bus->resource[2]->flags & IORESOURCE_PREFETCH) {
  132. l = (ranges.prefetch_start >> 16) & 0xfff0;
  133. l |= ranges.prefetch_end & 0xfff00000;
  134. DBGC((KERN_INFO "  PREFETCH window: %08lx-%08lxn",
  135. ranges.prefetch_start, ranges.prefetch_end));
  136. }
  137. else {
  138. l = 0x0000fff0;
  139. DBGC((KERN_INFO "  PREFETCH window: disabled.n"));
  140. }
  141. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
  142. /* Check if we have VGA behind the bridge.
  143.    Enable ISA in either case (FIXME!). */
  144. l = (bus->resource[0]->flags & IORESOURCE_BUS_HAS_VGA) ? 0x0c : 0x04;
  145. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, l);
  146. }
  147. /* Check whether the bridge supports optional I/O and
  148.    prefetchable memory ranges. If not, the respective
  149.    base/limit registers must be read-only and read as 0. */
  150. static void __init
  151. pci_bridge_check_ranges(struct pci_bus *bus)
  152. {
  153. u16 io;
  154. u32 pmem;
  155. struct pci_dev *bridge = bus->self;
  156. struct resource *b_res;
  157. if (!bridge || (bridge->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  158. return;
  159. b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
  160. b_res[1].flags |= IORESOURCE_MEM;
  161. pci_read_config_word(bridge, PCI_IO_BASE, &io);
  162. if (!io) {
  163. pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
  164. pci_read_config_word(bridge, PCI_IO_BASE, &io);
  165.   pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
  166.   }
  167.   if (io)
  168. b_res[0].flags |= IORESOURCE_IO;
  169. /*  DECchip 21050 pass 2 errata: the bridge may miss an address
  170.     disconnect boundary by one PCI data phase.
  171.     Workaround: do not use prefetching on this device. */
  172. if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
  173. return;
  174. pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
  175. if (!pmem) {
  176. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
  177.        0xfff0fff0);
  178. pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
  179. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
  180. }
  181. if (pmem)
  182. b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
  183. }
  184. /* Sizing the IO windows of the PCI-PCI bridge is trivial,
  185.    since these windows have 4K granularity and the IO ranges
  186.    of non-bridge PCI devices are limited to 256 bytes.
  187.    We must be careful with the ISA aliasing though. */
  188. static void __init
  189. pbus_size_io(struct pci_bus *bus)
  190. {
  191. struct list_head *ln;
  192. struct resource *b_res = bus->resource[0];
  193. unsigned long size = 0, size1 = 0;
  194. if (!(b_res->flags & IORESOURCE_IO))
  195. return;
  196. for (ln=bus->devices.next; ln != &bus->devices; ln=ln->next) {
  197. struct pci_dev *dev = pci_dev_b(ln);
  198. int i;
  199. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  200. struct resource *r = &dev->resource[i];
  201. unsigned long r_size;
  202. if (r->parent || !(r->flags & IORESOURCE_IO))
  203. continue;
  204. r_size = r->end - r->start + 1;
  205. if (r_size < 0x400)
  206. /* Might be re-aligned for ISA */
  207. size += r_size;
  208. else
  209. size1 += r_size;
  210. }
  211. /* ??? Reserve some resources for CardBus. */
  212. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS)
  213. size1 += 4*1024;
  214. }
  215. /* To be fixed in 2.5: we should have sort of HAVE_ISA
  216.    flag in the struct pci_bus. */
  217. #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
  218. size = (size & 0xff) + ((size & ~0xffUL) << 2);
  219. #endif
  220. size = ROUND_UP(size + size1, 4096);
  221. if (!size) {
  222. b_res->flags = 0;
  223. return;
  224. }
  225. /* Alignment of the IO window is always 4K */
  226. b_res->start = 4096;
  227. b_res->end = b_res->start + size - 1;
  228. }
  229. /* Calculate the size of the bus and minimal alignment which
  230.    guarantees that all child resources fit in this size. */
  231. static void __init
  232. pbus_size_mem(struct pci_bus *bus, unsigned long mask, unsigned long type)
  233. {
  234. struct list_head *ln;
  235. unsigned long min_align, align, size;
  236. unsigned long aligns[12]; /* Alignments from 1Mb to 2Gb */
  237. int order, max_order;
  238. struct resource *b_res = (type & IORESOURCE_PREFETCH) ?
  239.  bus->resource[2] : bus->resource[1];
  240. memset(aligns, 0, sizeof(aligns));
  241. max_order = 0;
  242. size = 0;
  243. for (ln=bus->devices.next; ln != &bus->devices; ln=ln->next) {
  244. struct pci_dev *dev = pci_dev_b(ln);
  245. int i;
  246. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  247. struct resource *r = &dev->resource[i];
  248. unsigned long r_size;
  249. if (r->parent || (r->flags & mask) != type)
  250. continue;
  251. r_size = r->end - r->start + 1;
  252. /* For bridges size != alignment */
  253. align = (i < PCI_BRIDGE_RESOURCES) ? r_size : r->start;
  254. order = ffz(~align) - 20;
  255. if (order > 11) {
  256. printk(KERN_WARNING "PCI: region %s/%d "
  257.        "too large: %lx-%lxn",
  258.        dev->slot_name, i, r->start, r->end);
  259. r->flags = 0;
  260. continue;
  261. }
  262. size += r_size;
  263. if (order < 0)
  264. order = 0;
  265. /* Exclude ranges with size > align from
  266.    calculation of the alignment. */
  267. if (size == align)
  268. aligns[order] += align;
  269. if (order > max_order)
  270. max_order = order;
  271. }
  272. /* ??? Reserve some resources for CardBus. */
  273. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS) {
  274. size += 1UL << 24; /* 16 Mb */
  275. aligns[24 - 20] += 1UL << 24;
  276. }
  277. }
  278. align = 0;
  279. min_align = 0;
  280. for (order = 0; order <= max_order; order++) {
  281. unsigned long align1 = 1UL << (order + 20);
  282. if (!align)
  283. min_align = align1;
  284. else if (ROUND_UP(align + min_align, min_align) < align1)
  285. min_align = align1 >> 1;
  286. align += aligns[order];
  287. }
  288. size = ROUND_UP(size, min_align);
  289. if (!size) {
  290. b_res->flags = 0;
  291. return;
  292. }
  293. b_res->start = min_align;
  294. b_res->end = size + min_align - 1;
  295. }
  296. void __init
  297. pbus_size_bridges(struct pci_bus *bus)
  298. {
  299. struct list_head *ln;
  300. unsigned long mask, type;
  301. for (ln=bus->children.next; ln != &bus->children; ln=ln->next)
  302. pbus_size_bridges(pci_bus_b(ln));
  303. /* The root bus? */
  304. if (!bus->self)
  305. return;
  306. pci_bridge_check_ranges(bus);
  307. pbus_size_io(bus);
  308. mask = type = IORESOURCE_MEM;
  309. /* If the bridge supports prefetchable range, size it separately. */
  310. if (bus->resource[2] &&
  311.     bus->resource[2]->flags & IORESOURCE_PREFETCH) {
  312. pbus_size_mem(bus, IORESOURCE_PREFETCH, IORESOURCE_PREFETCH);
  313. mask |= IORESOURCE_PREFETCH; /* Size non-prefetch only. */
  314. }
  315. pbus_size_mem(bus, mask, type);
  316. }
  317. void __init
  318. pbus_assign_resources(struct pci_bus *bus)
  319. {
  320. struct list_head *ln;
  321. int found_vga = pbus_assign_resources_sorted(bus);
  322. if (found_vga) {
  323. struct pci_bus *b;
  324. /* Propagate presence of the VGA to upstream bridges */
  325. for (b = bus; b->parent; b = b->parent) {
  326. b->resource[0]->flags |= IORESOURCE_BUS_HAS_VGA;
  327. }
  328. }
  329. for (ln=bus->children.next; ln != &bus->children; ln=ln->next) {
  330. struct pci_bus *b = pci_bus_b(ln);
  331. pbus_assign_resources(b);
  332. pci_setup_bridge(b);
  333. }
  334. }
  335. void __init
  336. pci_assign_unassigned_resources(void)
  337. {
  338. struct list_head *ln;
  339. struct pci_dev *dev;
  340. /* Depth first, calculate sizes and alignments of all
  341.    subordinate buses. */
  342. for(ln=pci_root_buses.next; ln != &pci_root_buses; ln=ln->next)
  343. pbus_size_bridges(pci_bus_b(ln));
  344. /* Depth last, allocate resources and update the hardware. */
  345. for(ln=pci_root_buses.next; ln != &pci_root_buses; ln=ln->next)
  346. pbus_assign_resources(pci_bus_b(ln));
  347. pci_for_each_dev(dev) {
  348. pdev_enable_device(dev);
  349. }
  350. }