setup-bus.c 16 KB

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