setup-bus.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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. #include "pci.h"
  27. static void pbus_assign_resources_sorted(const struct pci_bus *bus)
  28. {
  29. struct pci_dev *dev;
  30. struct resource *res;
  31. struct resource_list head, *list, *tmp;
  32. int idx;
  33. head.next = NULL;
  34. list_for_each_entry(dev, &bus->devices, bus_list) {
  35. u16 class = dev->class >> 8;
  36. /* Don't touch classless devices or host bridges or ioapics. */
  37. if (class == PCI_CLASS_NOT_DEFINED ||
  38. class == PCI_CLASS_BRIDGE_HOST)
  39. continue;
  40. /* Don't touch ioapic devices already enabled by firmware */
  41. if (class == PCI_CLASS_SYSTEM_PIC) {
  42. u16 command;
  43. pci_read_config_word(dev, PCI_COMMAND, &command);
  44. if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
  45. continue;
  46. }
  47. pdev_sort_resources(dev, &head);
  48. }
  49. for (list = head.next; list;) {
  50. res = list->res;
  51. idx = res - &list->dev->resource[0];
  52. if (pci_assign_resource(list->dev, idx)) {
  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 resource *res;
  66. struct pci_bus_region region;
  67. dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
  68. bus->secondary, bus->subordinate);
  69. res = bus->resource[0];
  70. pcibios_resource_to_bus(bridge, &region, res);
  71. if (res->flags & IORESOURCE_IO) {
  72. /*
  73. * The IO resource is allocated a range twice as large as it
  74. * would normally need. This allows us to set both IO regs.
  75. */
  76. dev_info(&bridge->dev, " bridge window %pR\n", res);
  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. res = bus->resource[1];
  83. pcibios_resource_to_bus(bridge, &region, res);
  84. if (res->flags & IORESOURCE_IO) {
  85. dev_info(&bridge->dev, " bridge window %pR\n", res);
  86. pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
  87. region.start);
  88. pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
  89. region.end);
  90. }
  91. res = bus->resource[2];
  92. pcibios_resource_to_bus(bridge, &region, res);
  93. if (res->flags & IORESOURCE_MEM) {
  94. dev_info(&bridge->dev, " bridge window %pR\n", res);
  95. pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
  96. region.start);
  97. pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
  98. region.end);
  99. }
  100. res = bus->resource[3];
  101. pcibios_resource_to_bus(bridge, &region, res);
  102. if (res->flags & IORESOURCE_MEM) {
  103. dev_info(&bridge->dev, " bridge window %pR\n", res);
  104. pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
  105. region.start);
  106. pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
  107. region.end);
  108. }
  109. }
  110. EXPORT_SYMBOL(pci_setup_cardbus);
  111. /* Initialize bridges with base/limit values we have collected.
  112. PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
  113. requires that if there is no I/O ports or memory behind the
  114. bridge, corresponding range must be turned off by writing base
  115. value greater than limit to the bridge's base/limit registers.
  116. Note: care must be taken when updating I/O base/limit registers
  117. of bridges which support 32-bit I/O. This update requires two
  118. config space writes, so it's quite possible that an I/O window of
  119. the bridge will have some undesirable address (e.g. 0) after the
  120. first write. Ditto 64-bit prefetchable MMIO. */
  121. static void pci_setup_bridge_io(struct pci_bus *bus)
  122. {
  123. struct pci_dev *bridge = bus->self;
  124. struct resource *res;
  125. struct pci_bus_region region;
  126. u32 l, io_upper16;
  127. /* Set up the top and bottom of the PCI I/O segment for this bus. */
  128. res = bus->resource[0];
  129. pcibios_resource_to_bus(bridge, &region, res);
  130. if (res->flags & IORESOURCE_IO) {
  131. pci_read_config_dword(bridge, PCI_IO_BASE, &l);
  132. l &= 0xffff0000;
  133. l |= (region.start >> 8) & 0x00f0;
  134. l |= region.end & 0xf000;
  135. /* Set up upper 16 bits of I/O base/limit. */
  136. io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
  137. dev_info(&bridge->dev, " bridge window %pR\n", res);
  138. } else {
  139. /* Clear upper 16 bits of I/O base/limit. */
  140. io_upper16 = 0;
  141. l = 0x00f0;
  142. dev_info(&bridge->dev, " bridge window [io disabled]\n");
  143. }
  144. /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
  145. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
  146. /* Update lower 16 bits of I/O base/limit. */
  147. pci_write_config_dword(bridge, PCI_IO_BASE, l);
  148. /* Update upper 16 bits of I/O base/limit. */
  149. pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
  150. }
  151. static void pci_setup_bridge_mmio(struct pci_bus *bus)
  152. {
  153. struct pci_dev *bridge = bus->self;
  154. struct resource *res;
  155. struct pci_bus_region region;
  156. u32 l;
  157. /* Set up the top and bottom of the PCI Memory segment for this bus. */
  158. res = bus->resource[1];
  159. pcibios_resource_to_bus(bridge, &region, res);
  160. if (res->flags & IORESOURCE_MEM) {
  161. l = (region.start >> 16) & 0xfff0;
  162. l |= region.end & 0xfff00000;
  163. dev_info(&bridge->dev, " bridge window %pR\n", res);
  164. } else {
  165. l = 0x0000fff0;
  166. dev_info(&bridge->dev, " bridge window [mem disabled]\n");
  167. }
  168. pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
  169. }
  170. static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
  171. {
  172. struct pci_dev *bridge = bus->self;
  173. struct resource *res;
  174. struct pci_bus_region region;
  175. u32 l, bu, lu;
  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. res = bus->resource[2];
  183. pcibios_resource_to_bus(bridge, &region, res);
  184. if (res->flags & IORESOURCE_PREFETCH) {
  185. l = (region.start >> 16) & 0xfff0;
  186. l |= region.end & 0xfff00000;
  187. if (res->flags & IORESOURCE_MEM_64) {
  188. bu = upper_32_bits(region.start);
  189. lu = upper_32_bits(region.end);
  190. }
  191. dev_info(&bridge->dev, " bridge window %pR\n", res);
  192. } else {
  193. l = 0x0000fff0;
  194. dev_info(&bridge->dev, " bridge window [mem pref 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. }
  201. static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
  202. {
  203. struct pci_dev *bridge = bus->self;
  204. if (pci_is_enabled(bridge))
  205. return;
  206. dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
  207. bus->secondary, bus->subordinate);
  208. if (type & IORESOURCE_IO)
  209. pci_setup_bridge_io(bus);
  210. if (type & IORESOURCE_MEM)
  211. pci_setup_bridge_mmio(bus);
  212. if (type & IORESOURCE_PREFETCH)
  213. pci_setup_bridge_mmio_pref(bus);
  214. pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
  215. }
  216. static void pci_setup_bridge(struct pci_bus *bus)
  217. {
  218. unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
  219. IORESOURCE_PREFETCH;
  220. __pci_setup_bridge(bus, type);
  221. }
  222. /* Check whether the bridge supports optional I/O and
  223. prefetchable memory ranges. If not, the respective
  224. base/limit registers must be read-only and read as 0. */
  225. static void pci_bridge_check_ranges(struct pci_bus *bus)
  226. {
  227. u16 io;
  228. u32 pmem;
  229. struct pci_dev *bridge = bus->self;
  230. struct resource *b_res;
  231. b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
  232. b_res[1].flags |= IORESOURCE_MEM;
  233. pci_read_config_word(bridge, PCI_IO_BASE, &io);
  234. if (!io) {
  235. pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
  236. pci_read_config_word(bridge, PCI_IO_BASE, &io);
  237. pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
  238. }
  239. if (io)
  240. b_res[0].flags |= IORESOURCE_IO;
  241. /* DECchip 21050 pass 2 errata: the bridge may miss an address
  242. disconnect boundary by one PCI data phase.
  243. Workaround: do not use prefetching on this device. */
  244. if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
  245. return;
  246. pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
  247. if (!pmem) {
  248. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
  249. 0xfff0fff0);
  250. pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
  251. pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
  252. }
  253. if (pmem) {
  254. b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
  255. if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64)
  256. b_res[2].flags |= IORESOURCE_MEM_64;
  257. }
  258. /* double check if bridge does support 64 bit pref */
  259. if (b_res[2].flags & IORESOURCE_MEM_64) {
  260. u32 mem_base_hi, tmp;
  261. pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  262. &mem_base_hi);
  263. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  264. 0xffffffff);
  265. pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
  266. if (!tmp)
  267. b_res[2].flags &= ~IORESOURCE_MEM_64;
  268. pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
  269. mem_base_hi);
  270. }
  271. }
  272. /* Helper function for sizing routines: find first available
  273. bus resource of a given type. Note: we intentionally skip
  274. the bus resources which have already been assigned (that is,
  275. have non-NULL parent resource). */
  276. static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
  277. {
  278. int i;
  279. struct resource *r;
  280. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  281. IORESOURCE_PREFETCH;
  282. for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
  283. r = bus->resource[i];
  284. if (r == &ioport_resource || r == &iomem_resource)
  285. continue;
  286. if (r && (r->flags & type_mask) == type && !r->parent)
  287. return r;
  288. }
  289. return NULL;
  290. }
  291. /* Sizing the IO windows of the PCI-PCI bridge is trivial,
  292. since these windows have 4K granularity and the IO ranges
  293. of non-bridge PCI devices are limited to 256 bytes.
  294. We must be careful with the ISA aliasing though. */
  295. static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size)
  296. {
  297. struct pci_dev *dev;
  298. struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
  299. unsigned long size = 0, size1 = 0;
  300. if (!b_res)
  301. return;
  302. list_for_each_entry(dev, &bus->devices, bus_list) {
  303. int i;
  304. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  305. struct resource *r = &dev->resource[i];
  306. unsigned long r_size;
  307. if (r->parent || !(r->flags & IORESOURCE_IO))
  308. continue;
  309. r_size = resource_size(r);
  310. if (r_size < 0x400)
  311. /* Might be re-aligned for ISA */
  312. size += r_size;
  313. else
  314. size1 += r_size;
  315. }
  316. }
  317. if (size < min_size)
  318. size = min_size;
  319. /* To be fixed in 2.5: we should have sort of HAVE_ISA
  320. flag in the struct pci_bus. */
  321. #if defined(CONFIG_ISA) || defined(CONFIG_EISA)
  322. size = (size & 0xff) + ((size & ~0xffUL) << 2);
  323. #endif
  324. size = ALIGN(size + size1, 4096);
  325. if (!size) {
  326. if (b_res->start || b_res->end)
  327. dev_info(&bus->self->dev, "disabling bridge window "
  328. "%pR to [bus %02x-%02x] (unused)\n", b_res,
  329. bus->secondary, bus->subordinate);
  330. b_res->flags = 0;
  331. return;
  332. }
  333. /* Alignment of the IO window is always 4K */
  334. b_res->start = 4096;
  335. b_res->end = b_res->start + size - 1;
  336. b_res->flags |= IORESOURCE_STARTALIGN;
  337. }
  338. /* Calculate the size of the bus and minimal alignment which
  339. guarantees that all child resources fit in this size. */
  340. static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
  341. unsigned long type, resource_size_t min_size)
  342. {
  343. struct pci_dev *dev;
  344. resource_size_t min_align, align, size;
  345. resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
  346. int order, max_order;
  347. struct resource *b_res = find_free_bus_resource(bus, type);
  348. unsigned int mem64_mask = 0;
  349. if (!b_res)
  350. return 0;
  351. memset(aligns, 0, sizeof(aligns));
  352. max_order = 0;
  353. size = 0;
  354. mem64_mask = b_res->flags & IORESOURCE_MEM_64;
  355. b_res->flags &= ~IORESOURCE_MEM_64;
  356. list_for_each_entry(dev, &bus->devices, bus_list) {
  357. int i;
  358. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  359. struct resource *r = &dev->resource[i];
  360. resource_size_t r_size;
  361. if (r->parent || (r->flags & mask) != type)
  362. continue;
  363. r_size = resource_size(r);
  364. /* For bridges size != alignment */
  365. align = pci_resource_alignment(dev, r);
  366. order = __ffs(align) - 20;
  367. if (order > 11) {
  368. dev_warn(&dev->dev, "disabling BAR %d: %pR "
  369. "(bad alignment %#llx)\n", i, r,
  370. (unsigned long long) align);
  371. r->flags = 0;
  372. continue;
  373. }
  374. size += r_size;
  375. if (order < 0)
  376. order = 0;
  377. /* Exclude ranges with size > align from
  378. calculation of the alignment. */
  379. if (r_size == align)
  380. aligns[order] += align;
  381. if (order > max_order)
  382. max_order = order;
  383. mem64_mask &= r->flags & IORESOURCE_MEM_64;
  384. }
  385. }
  386. if (size < min_size)
  387. size = min_size;
  388. align = 0;
  389. min_align = 0;
  390. for (order = 0; order <= max_order; order++) {
  391. resource_size_t align1 = 1;
  392. align1 <<= (order + 20);
  393. if (!align)
  394. min_align = align1;
  395. else if (ALIGN(align + min_align, min_align) < align1)
  396. min_align = align1 >> 1;
  397. align += aligns[order];
  398. }
  399. size = ALIGN(size, min_align);
  400. if (!size) {
  401. if (b_res->start || b_res->end)
  402. dev_info(&bus->self->dev, "disabling bridge window "
  403. "%pR to [bus %02x-%02x] (unused)\n", b_res,
  404. bus->secondary, bus->subordinate);
  405. b_res->flags = 0;
  406. return 1;
  407. }
  408. b_res->start = min_align;
  409. b_res->end = size + min_align - 1;
  410. b_res->flags |= IORESOURCE_STARTALIGN;
  411. b_res->flags |= mem64_mask;
  412. return 1;
  413. }
  414. static void pci_bus_size_cardbus(struct pci_bus *bus)
  415. {
  416. struct pci_dev *bridge = bus->self;
  417. struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
  418. u16 ctrl;
  419. /*
  420. * Reserve some resources for CardBus. We reserve
  421. * a fixed amount of bus space for CardBus bridges.
  422. */
  423. b_res[0].start = 0;
  424. b_res[0].end = pci_cardbus_io_size - 1;
  425. b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
  426. b_res[1].start = 0;
  427. b_res[1].end = pci_cardbus_io_size - 1;
  428. b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
  429. /*
  430. * Check whether prefetchable memory is supported
  431. * by this bridge.
  432. */
  433. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  434. if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
  435. ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
  436. pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
  437. pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
  438. }
  439. /*
  440. * If we have prefetchable memory support, allocate
  441. * two regions. Otherwise, allocate one region of
  442. * twice the size.
  443. */
  444. if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
  445. b_res[2].start = 0;
  446. b_res[2].end = pci_cardbus_mem_size - 1;
  447. b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
  448. b_res[3].start = 0;
  449. b_res[3].end = pci_cardbus_mem_size - 1;
  450. b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
  451. } else {
  452. b_res[3].start = 0;
  453. b_res[3].end = pci_cardbus_mem_size * 2 - 1;
  454. b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
  455. }
  456. }
  457. void __ref pci_bus_size_bridges(struct pci_bus *bus)
  458. {
  459. struct pci_dev *dev;
  460. unsigned long mask, prefmask;
  461. resource_size_t min_mem_size = 0, min_io_size = 0;
  462. list_for_each_entry(dev, &bus->devices, bus_list) {
  463. struct pci_bus *b = dev->subordinate;
  464. if (!b)
  465. continue;
  466. switch (dev->class >> 8) {
  467. case PCI_CLASS_BRIDGE_CARDBUS:
  468. pci_bus_size_cardbus(b);
  469. break;
  470. case PCI_CLASS_BRIDGE_PCI:
  471. default:
  472. pci_bus_size_bridges(b);
  473. break;
  474. }
  475. }
  476. /* The root bus? */
  477. if (!bus->self)
  478. return;
  479. switch (bus->self->class >> 8) {
  480. case PCI_CLASS_BRIDGE_CARDBUS:
  481. /* don't size cardbuses yet. */
  482. break;
  483. case PCI_CLASS_BRIDGE_PCI:
  484. pci_bridge_check_ranges(bus);
  485. if (bus->self->is_hotplug_bridge) {
  486. min_io_size = pci_hotplug_io_size;
  487. min_mem_size = pci_hotplug_mem_size;
  488. }
  489. default:
  490. pbus_size_io(bus, min_io_size);
  491. /* If the bridge supports prefetchable range, size it
  492. separately. If it doesn't, or its prefetchable window
  493. has already been allocated by arch code, try
  494. non-prefetchable range for both types of PCI memory
  495. resources. */
  496. mask = IORESOURCE_MEM;
  497. prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
  498. if (pbus_size_mem(bus, prefmask, prefmask, min_mem_size))
  499. mask = prefmask; /* Success, size non-prefetch only. */
  500. else
  501. min_mem_size += min_mem_size;
  502. pbus_size_mem(bus, mask, IORESOURCE_MEM, min_mem_size);
  503. break;
  504. }
  505. }
  506. EXPORT_SYMBOL(pci_bus_size_bridges);
  507. void __ref pci_bus_assign_resources(const struct pci_bus *bus)
  508. {
  509. struct pci_bus *b;
  510. struct pci_dev *dev;
  511. pbus_assign_resources_sorted(bus);
  512. list_for_each_entry(dev, &bus->devices, bus_list) {
  513. b = dev->subordinate;
  514. if (!b)
  515. continue;
  516. pci_bus_assign_resources(b);
  517. switch (dev->class >> 8) {
  518. case PCI_CLASS_BRIDGE_PCI:
  519. pci_setup_bridge(b);
  520. break;
  521. case PCI_CLASS_BRIDGE_CARDBUS:
  522. pci_setup_cardbus(b);
  523. break;
  524. default:
  525. dev_info(&dev->dev, "not setting up bridge for bus "
  526. "%04x:%02x\n", pci_domain_nr(b), b->number);
  527. break;
  528. }
  529. }
  530. }
  531. EXPORT_SYMBOL(pci_bus_assign_resources);
  532. static void pci_bridge_release_resources(struct pci_bus *bus,
  533. unsigned long type)
  534. {
  535. int idx;
  536. bool changed = false;
  537. struct pci_dev *dev;
  538. struct resource *r;
  539. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
  540. IORESOURCE_PREFETCH;
  541. dev = bus->self;
  542. for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
  543. idx++) {
  544. r = &dev->resource[idx];
  545. if ((r->flags & type_mask) != type)
  546. continue;
  547. if (!r->parent)
  548. continue;
  549. /*
  550. * if there are children under that, we should release them
  551. * all
  552. */
  553. release_child_resources(r);
  554. if (!release_resource(r)) {
  555. dev_printk(KERN_DEBUG, &dev->dev,
  556. "resource %d %pR released\n", idx, r);
  557. /* keep the old size */
  558. r->end = resource_size(r) - 1;
  559. r->start = 0;
  560. r->flags = 0;
  561. changed = true;
  562. }
  563. }
  564. if (changed) {
  565. /* avoiding touch the one without PREF */
  566. if (type & IORESOURCE_PREFETCH)
  567. type = IORESOURCE_PREFETCH;
  568. __pci_setup_bridge(bus, type);
  569. }
  570. }
  571. enum release_type {
  572. leaf_only,
  573. whole_subtree,
  574. };
  575. /*
  576. * try to release pci bridge resources that is from leaf bridge,
  577. * so we can allocate big new one later
  578. */
  579. static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
  580. unsigned long type,
  581. enum release_type rel_type)
  582. {
  583. struct pci_dev *dev;
  584. bool is_leaf_bridge = true;
  585. list_for_each_entry(dev, &bus->devices, bus_list) {
  586. struct pci_bus *b = dev->subordinate;
  587. if (!b)
  588. continue;
  589. is_leaf_bridge = false;
  590. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  591. continue;
  592. if (rel_type == whole_subtree)
  593. pci_bus_release_bridge_resources(b, type,
  594. whole_subtree);
  595. }
  596. if (pci_is_root_bus(bus))
  597. return;
  598. if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  599. return;
  600. if ((rel_type == whole_subtree) || is_leaf_bridge)
  601. pci_bridge_release_resources(bus, type);
  602. }
  603. static void pci_bus_dump_res(struct pci_bus *bus)
  604. {
  605. int i;
  606. for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
  607. struct resource *res = bus->resource[i];
  608. if (!res || !res->end || !res->flags)
  609. continue;
  610. dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
  611. }
  612. }
  613. static void pci_bus_dump_resources(struct pci_bus *bus)
  614. {
  615. struct pci_bus *b;
  616. struct pci_dev *dev;
  617. pci_bus_dump_res(bus);
  618. list_for_each_entry(dev, &bus->devices, bus_list) {
  619. b = dev->subordinate;
  620. if (!b)
  621. continue;
  622. pci_bus_dump_resources(b);
  623. }
  624. }
  625. void __init
  626. pci_assign_unassigned_resources(void)
  627. {
  628. struct pci_bus *bus;
  629. /* Depth first, calculate sizes and alignments of all
  630. subordinate buses. */
  631. list_for_each_entry(bus, &pci_root_buses, node) {
  632. pci_bus_size_bridges(bus);
  633. }
  634. /* Depth last, allocate resources and update the hardware. */
  635. list_for_each_entry(bus, &pci_root_buses, node) {
  636. pci_bus_assign_resources(bus);
  637. pci_enable_bridges(bus);
  638. }
  639. /* dump the resource on buses */
  640. list_for_each_entry(bus, &pci_root_buses, node) {
  641. pci_bus_dump_resources(bus);
  642. }
  643. }