setup-bus.c 21 KB

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