setup-bus.c 15 KB

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