blob: 5cf84568c9e46264a72f12220ffa5be0700d66d7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/*
13 * Nov 2000, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
14 * PCI-PCI bridges cleanup, sorted resource allocation.
15 * Feb 2002, Ivan Kokshaysky <ink@jurassic.park.msu.ru>
16 * Converted to allocation in 3 passes, which gives
17 * tighter packing. Prefetchable range support.
18 */
19
20#include <linux/init.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/pci.h>
24#include <linux/errno.h>
25#include <linux/ioport.h>
26#include <linux/cache.h>
27#include <linux/slab.h>
28
29
30#define DEBUG_CONFIG 1
31#if DEBUG_CONFIG
32#define DBG(x...) printk(x)
33#else
34#define DBG(x...)
35#endif
36
Sam Ravnborg96bde062007-03-26 21:53:30 -080037static void pbus_assign_resources_sorted(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 struct pci_dev *dev;
40 struct resource *res;
41 struct resource_list head, *list, *tmp;
42 int idx;
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 head.next = NULL;
45 list_for_each_entry(dev, &bus->devices, bus_list) {
46 u16 class = dev->class >> 8;
47
Kenji Kaneshige9bded002006-10-04 02:15:34 -070048 /* Don't touch classless devices or host bridges or ioapics. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 if (class == PCI_CLASS_NOT_DEFINED ||
Satoru Takeuchi23186272006-09-12 10:21:44 -070050 class == PCI_CLASS_BRIDGE_HOST)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 continue;
52
Kenji Kaneshige9bded002006-10-04 02:15:34 -070053 /* Don't touch ioapic devices already enabled by firmware */
Satoru Takeuchi23186272006-09-12 10:21:44 -070054 if (class == PCI_CLASS_SYSTEM_PIC) {
Kenji Kaneshige9bded002006-10-04 02:15:34 -070055 u16 command;
56 pci_read_config_word(dev, PCI_COMMAND, &command);
57 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
Satoru Takeuchi23186272006-09-12 10:21:44 -070058 continue;
59 }
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 pdev_sort_resources(dev, &head);
62 }
63
64 for (list = head.next; list;) {
65 res = list->res;
66 idx = res - &list->dev->resource[0];
Rajesh Shah542df5d2005-04-28 00:25:50 -070067 if (pci_assign_resource(list->dev, idx)) {
Ivan Kokshaysky88452562008-03-30 19:50:14 +040068 /* FIXME: get rid of this */
Rajesh Shah542df5d2005-04-28 00:25:50 -070069 res->start = 0;
Ivan Kokshaysky960b8462005-07-07 03:07:56 +040070 res->end = 0;
Rajesh Shah542df5d2005-04-28 00:25:50 -070071 res->flags = 0;
72 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 tmp = list;
74 list = list->next;
75 kfree(tmp);
76 }
77}
78
Dominik Brodowskib3743fa2005-09-09 13:03:23 -070079void pci_setup_cardbus(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 struct pci_dev *bridge = bus->self;
82 struct pci_bus_region region;
83
84 printk("PCI: Bus %d, cardbus bridge: %s\n",
85 bus->number, pci_name(bridge));
86
87 pcibios_resource_to_bus(bridge, &region, bus->resource[0]);
88 if (bus->resource[0]->flags & IORESOURCE_IO) {
89 /*
90 * The IO resource is allocated a range twice as large as it
91 * would normally need. This allows us to set both IO regs.
92 */
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +110093 printk(KERN_INFO " IO window: 0x%08lx-0x%08lx\n",
94 (unsigned long)region.start,
95 (unsigned long)region.end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
97 region.start);
98 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
99 region.end);
100 }
101
102 pcibios_resource_to_bus(bridge, &region, bus->resource[1]);
103 if (bus->resource[1]->flags & IORESOURCE_IO) {
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100104 printk(KERN_INFO " IO window: 0x%08lx-0x%08lx\n",
105 (unsigned long)region.start,
106 (unsigned long)region.end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
108 region.start);
109 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
110 region.end);
111 }
112
113 pcibios_resource_to_bus(bridge, &region, bus->resource[2]);
114 if (bus->resource[2]->flags & IORESOURCE_MEM) {
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100115 printk(KERN_INFO " PREFETCH window: 0x%08lx-0x%08lx\n",
116 (unsigned long)region.start,
117 (unsigned long)region.end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
119 region.start);
120 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
121 region.end);
122 }
123
124 pcibios_resource_to_bus(bridge, &region, bus->resource[3]);
125 if (bus->resource[3]->flags & IORESOURCE_MEM) {
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100126 printk(KERN_INFO " MEM window: 0x%08lx-0x%08lx\n",
127 (unsigned long)region.start,
128 (unsigned long)region.end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
130 region.start);
131 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
132 region.end);
133 }
134}
Dominik Brodowskib3743fa2005-09-09 13:03:23 -0700135EXPORT_SYMBOL(pci_setup_cardbus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/* Initialize bridges with base/limit values we have collected.
138 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
139 requires that if there is no I/O ports or memory behind the
140 bridge, corresponding range must be turned off by writing base
141 value greater than limit to the bridge's base/limit registers.
142
143 Note: care must be taken when updating I/O base/limit registers
144 of bridges which support 32-bit I/O. This update requires two
145 config space writes, so it's quite possible that an I/O window of
146 the bridge will have some undesirable address (e.g. 0) after the
147 first write. Ditto 64-bit prefetchable MMIO. */
148static void __devinit
149pci_setup_bridge(struct pci_bus *bus)
150{
151 struct pci_dev *bridge = bus->self;
152 struct pci_bus_region region;
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100153 u32 l, bu, lu, io_upper16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 DBG(KERN_INFO "PCI: Bridge: %s\n", pci_name(bridge));
156
157 /* Set up the top and bottom of the PCI I/O segment for this bus. */
158 pcibios_resource_to_bus(bridge, &region, bus->resource[0]);
159 if (bus->resource[0]->flags & IORESOURCE_IO) {
160 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
161 l &= 0xffff0000;
162 l |= (region.start >> 8) & 0x00f0;
163 l |= region.end & 0xf000;
164 /* Set up upper 16 bits of I/O base/limit. */
165 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
166 DBG(KERN_INFO " IO window: %04lx-%04lx\n",
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100167 (unsigned long)region.start,
168 (unsigned long)region.end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170 else {
171 /* Clear upper 16 bits of I/O base/limit. */
172 io_upper16 = 0;
173 l = 0x00f0;
174 DBG(KERN_INFO " IO window: disabled.\n");
175 }
176 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
177 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
178 /* Update lower 16 bits of I/O base/limit. */
179 pci_write_config_dword(bridge, PCI_IO_BASE, l);
180 /* Update upper 16 bits of I/O base/limit. */
181 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
182
183 /* Set up the top and bottom of the PCI Memory segment
184 for this bus. */
185 pcibios_resource_to_bus(bridge, &region, bus->resource[1]);
186 if (bus->resource[1]->flags & IORESOURCE_MEM) {
187 l = (region.start >> 16) & 0xfff0;
188 l |= region.end & 0xfff00000;
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100189 DBG(KERN_INFO " MEM window: 0x%08lx-0x%08lx\n",
190 (unsigned long)region.start,
191 (unsigned long)region.end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193 else {
194 l = 0x0000fff0;
195 DBG(KERN_INFO " MEM window: disabled.\n");
196 }
197 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
198
199 /* Clear out the upper 32 bits of PREF limit.
200 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
201 disables PREF range, which is ok. */
202 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
203
204 /* Set up PREF base/limit. */
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100205 bu = lu = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 pcibios_resource_to_bus(bridge, &region, bus->resource[2]);
207 if (bus->resource[2]->flags & IORESOURCE_PREFETCH) {
208 l = (region.start >> 16) & 0xfff0;
209 l |= region.end & 0xfff00000;
Andrew Morton13d36c22008-02-04 23:50:12 -0800210 bu = upper_32_bits(region.start);
211 lu = upper_32_bits(region.end);
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100212 DBG(KERN_INFO " PREFETCH window: 0x%016llx-0x%016llx\n",
213 (unsigned long long)region.start,
214 (unsigned long long)region.end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216 else {
217 l = 0x0000fff0;
218 DBG(KERN_INFO " PREFETCH window: disabled.\n");
219 }
220 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
221
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100222 /* Set the upper 32 bits of PREF base & limit. */
223 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
224 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
227}
228
229/* Check whether the bridge supports optional I/O and
230 prefetchable memory ranges. If not, the respective
231 base/limit registers must be read-only and read as 0. */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800232static void pci_bridge_check_ranges(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 u16 io;
235 u32 pmem;
236 struct pci_dev *bridge = bus->self;
237 struct resource *b_res;
238
239 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
240 b_res[1].flags |= IORESOURCE_MEM;
241
242 pci_read_config_word(bridge, PCI_IO_BASE, &io);
243 if (!io) {
244 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
245 pci_read_config_word(bridge, PCI_IO_BASE, &io);
246 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
247 }
248 if (io)
249 b_res[0].flags |= IORESOURCE_IO;
250 /* DECchip 21050 pass 2 errata: the bridge may miss an address
251 disconnect boundary by one PCI data phase.
252 Workaround: do not use prefetching on this device. */
253 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
254 return;
255 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
256 if (!pmem) {
257 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
258 0xfff0fff0);
259 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
260 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
261 }
262 if (pmem)
263 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
264}
265
266/* Helper function for sizing routines: find first available
267 bus resource of a given type. Note: we intentionally skip
268 the bus resources which have already been assigned (that is,
269 have non-NULL parent resource). */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800270static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 int i;
273 struct resource *r;
274 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
275 IORESOURCE_PREFETCH;
276
277 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
278 r = bus->resource[i];
Ivan Kokshaysky299de032005-06-15 18:59:27 +0400279 if (r == &ioport_resource || r == &iomem_resource)
280 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (r && (r->flags & type_mask) == type && !r->parent)
282 return r;
283 }
284 return NULL;
285}
286
287/* Sizing the IO windows of the PCI-PCI bridge is trivial,
288 since these windows have 4K granularity and the IO ranges
289 of non-bridge PCI devices are limited to 256 bytes.
290 We must be careful with the ISA aliasing though. */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800291static void pbus_size_io(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct pci_dev *dev;
294 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
295 unsigned long size = 0, size1 = 0;
296
297 if (!b_res)
298 return;
299
300 list_for_each_entry(dev, &bus->devices, bus_list) {
301 int i;
302
303 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
304 struct resource *r = &dev->resource[i];
305 unsigned long r_size;
306
307 if (r->parent || !(r->flags & IORESOURCE_IO))
308 continue;
309 r_size = r->end - r->start + 1;
310
311 if (r_size < 0x400)
312 /* Might be re-aligned for ISA */
313 size += r_size;
314 else
315 size1 += r_size;
316 }
317 }
318/* To be fixed in 2.5: we should have sort of HAVE_ISA
319 flag in the struct pci_bus. */
320#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
321 size = (size & 0xff) + ((size & ~0xffUL) << 2);
322#endif
Milind Arun Choudhary6f6f8c22007-07-09 11:55:51 -0700323 size = ALIGN(size + size1, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (!size) {
325 b_res->flags = 0;
326 return;
327 }
328 /* Alignment of the IO window is always 4K */
329 b_res->start = 4096;
330 b_res->end = b_res->start + size - 1;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400331 b_res->flags |= IORESOURCE_STARTALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
334/* Calculate the size of the bus and minimal alignment which
335 guarantees that all child resources fit in this size. */
Sam Ravnborg96bde062007-03-26 21:53:30 -0800336static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, unsigned long type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct pci_dev *dev;
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100339 resource_size_t min_align, align, size;
340 resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int order, max_order;
342 struct resource *b_res = find_free_bus_resource(bus, type);
343
344 if (!b_res)
345 return 0;
346
347 memset(aligns, 0, sizeof(aligns));
348 max_order = 0;
349 size = 0;
350
351 list_for_each_entry(dev, &bus->devices, bus_list) {
352 int i;
353
354 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
355 struct resource *r = &dev->resource[i];
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100356 resource_size_t r_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 if (r->parent || (r->flags & mask) != type)
359 continue;
360 r_size = r->end - r->start + 1;
361 /* For bridges size != alignment */
362 align = (i < PCI_BRIDGE_RESOURCES) ? r_size : r->start;
363 order = __ffs(align) - 20;
364 if (order > 11) {
365 printk(KERN_WARNING "PCI: region %s/%d "
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100366 "too large: 0x%016llx-0x%016llx\n",
Greg Kroah-Hartman1396a8c2006-06-12 15:14:29 -0700367 pci_name(dev), i,
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100368 (unsigned long long)r->start,
369 (unsigned long long)r->end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 r->flags = 0;
371 continue;
372 }
373 size += r_size;
374 if (order < 0)
375 order = 0;
376 /* Exclude ranges with size > align from
377 calculation of the alignment. */
378 if (r_size == align)
379 aligns[order] += align;
380 if (order > max_order)
381 max_order = order;
382 }
383 }
384
385 align = 0;
386 min_align = 0;
387 for (order = 0; order <= max_order; order++) {
Benjamin Herrenschmidtc40a22e2007-12-10 17:32:15 +1100388#ifdef CONFIG_RESOURCES_64BIT
389 resource_size_t align1 = 1ULL << (order + 20);
390#else
391 resource_size_t align1 = 1U << (order + 20);
392#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (!align)
394 min_align = align1;
Milind Arun Choudhary6f6f8c22007-07-09 11:55:51 -0700395 else if (ALIGN(align + min_align, min_align) < align1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 min_align = align1 >> 1;
397 align += aligns[order];
398 }
Milind Arun Choudhary6f6f8c22007-07-09 11:55:51 -0700399 size = ALIGN(size, min_align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (!size) {
401 b_res->flags = 0;
402 return 1;
403 }
404 b_res->start = min_align;
405 b_res->end = size + min_align - 1;
Ivan Kokshaysky88452562008-03-30 19:50:14 +0400406 b_res->flags |= IORESOURCE_STARTALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return 1;
408}
409
410static void __devinit
411pci_bus_size_cardbus(struct pci_bus *bus)
412{
413 struct pci_dev *bridge = bus->self;
414 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
415 u16 ctrl;
416
417 /*
418 * Reserve some resources for CardBus. We reserve
419 * a fixed amount of bus space for CardBus bridges.
420 */
Atsushi Nemoto4516a612007-02-05 16:36:06 -0800421 b_res[0].start = pci_cardbus_io_size;
422 b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 b_res[0].flags |= IORESOURCE_IO;
424
Atsushi Nemoto4516a612007-02-05 16:36:06 -0800425 b_res[1].start = pci_cardbus_io_size;
426 b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 b_res[1].flags |= IORESOURCE_IO;
428
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 /*
441 * If we have prefetchable memory support, allocate
442 * two regions. Otherwise, allocate one region of
443 * twice the size.
444 */
445 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
Atsushi Nemoto4516a612007-02-05 16:36:06 -0800446 b_res[2].start = pci_cardbus_mem_size;
447 b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
449
Atsushi Nemoto4516a612007-02-05 16:36:06 -0800450 b_res[3].start = pci_cardbus_mem_size;
451 b_res[3].end = b_res[3].start + pci_cardbus_mem_size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 b_res[3].flags |= IORESOURCE_MEM;
453 } else {
Atsushi Nemoto4516a612007-02-05 16:36:06 -0800454 b_res[3].start = pci_cardbus_mem_size * 2;
455 b_res[3].end = b_res[3].start + pci_cardbus_mem_size * 2 - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 b_res[3].flags |= IORESOURCE_MEM;
457 }
458}
459
Sam Ravnborg451124a2008-02-02 22:33:43 +0100460void __ref pci_bus_size_bridges(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 struct pci_dev *dev;
463 unsigned long mask, prefmask;
464
465 list_for_each_entry(dev, &bus->devices, bus_list) {
466 struct pci_bus *b = dev->subordinate;
467 if (!b)
468 continue;
469
470 switch (dev->class >> 8) {
471 case PCI_CLASS_BRIDGE_CARDBUS:
472 pci_bus_size_cardbus(b);
473 break;
474
475 case PCI_CLASS_BRIDGE_PCI:
476 default:
477 pci_bus_size_bridges(b);
478 break;
479 }
480 }
481
482 /* The root bus? */
483 if (!bus->self)
484 return;
485
486 switch (bus->self->class >> 8) {
487 case PCI_CLASS_BRIDGE_CARDBUS:
488 /* don't size cardbuses yet. */
489 break;
490
491 case PCI_CLASS_BRIDGE_PCI:
492 pci_bridge_check_ranges(bus);
493 default:
494 pbus_size_io(bus);
495 /* If the bridge supports prefetchable range, size it
496 separately. If it doesn't, or its prefetchable window
497 has already been allocated by arch code, try
498 non-prefetchable range for both types of PCI memory
499 resources. */
500 mask = IORESOURCE_MEM;
501 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
502 if (pbus_size_mem(bus, prefmask, prefmask))
503 mask = prefmask; /* Success, size non-prefetch only. */
504 pbus_size_mem(bus, mask, IORESOURCE_MEM);
505 break;
506 }
507}
508EXPORT_SYMBOL(pci_bus_size_bridges);
509
Sam Ravnborg451124a2008-02-02 22:33:43 +0100510void __ref pci_bus_assign_resources(struct pci_bus *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 struct pci_bus *b;
513 struct pci_dev *dev;
514
515 pbus_assign_resources_sorted(bus);
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 list_for_each_entry(dev, &bus->devices, bus_list) {
518 b = dev->subordinate;
519 if (!b)
520 continue;
521
522 pci_bus_assign_resources(b);
523
524 switch (dev->class >> 8) {
525 case PCI_CLASS_BRIDGE_PCI:
526 pci_setup_bridge(b);
527 break;
528
529 case PCI_CLASS_BRIDGE_CARDBUS:
530 pci_setup_cardbus(b);
531 break;
532
533 default:
534 printk(KERN_INFO "PCI: not setting up bridge %s "
535 "for bus %d\n", pci_name(dev), b->number);
536 break;
537 }
538 }
539}
540EXPORT_SYMBOL(pci_bus_assign_resources);
541
542void __init
543pci_assign_unassigned_resources(void)
544{
545 struct pci_bus *bus;
546
547 /* Depth first, calculate sizes and alignments of all
548 subordinate buses. */
549 list_for_each_entry(bus, &pci_root_buses, node) {
550 pci_bus_size_bridges(bus);
551 }
552 /* Depth last, allocate resources and update the hardware. */
553 list_for_each_entry(bus, &pci_root_buses, node) {
554 pci_bus_assign_resources(bus);
555 pci_enable_bridges(bus);
556 }
557}