setup-bus.c 15 KB

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