setup-bus.c 15 KB

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