setup-bus.c 16 KB

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