setup-bus.c 16 KB

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