PCI: pciehp: second try to get big range for pcie devices
[deliverable/linux.git] / drivers / pci / setup-bus.c
CommitLineData
1da177e4
LT
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/*
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>
6faf17f6 28#include "pci.h"
1da177e4 29
568ddef8
YL
30struct resource_list_x {
31 struct resource_list_x *next;
32 struct resource *res;
33 struct pci_dev *dev;
34 resource_size_t start;
35 resource_size_t end;
36 unsigned long flags;
37};
38
39static void add_to_failed_list(struct resource_list_x *head,
40 struct pci_dev *dev, struct resource *res)
41{
42 struct resource_list_x *list = head;
43 struct resource_list_x *ln = list->next;
44 struct resource_list_x *tmp;
45
46 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
47 if (!tmp) {
48 pr_warning("add_to_failed_list: kmalloc() failed!\n");
49 return;
50 }
51
52 tmp->next = ln;
53 tmp->res = res;
54 tmp->dev = dev;
55 tmp->start = res->start;
56 tmp->end = res->end;
57 tmp->flags = res->flags;
58 list->next = tmp;
59}
60
61static void free_failed_list(struct resource_list_x *head)
62{
63 struct resource_list_x *list, *tmp;
64
65 for (list = head->next; list;) {
66 tmp = list;
67 list = list->next;
68 kfree(tmp);
69 }
70
71 head->next = NULL;
72}
73
6841ec68
YL
74static void __dev_sort_resources(struct pci_dev *dev,
75 struct resource_list *head)
1da177e4 76{
6841ec68 77 u16 class = dev->class >> 8;
1da177e4 78
6841ec68
YL
79 /* Don't touch classless devices or host bridges or ioapics. */
80 if (class == PCI_CLASS_NOT_DEFINED || class == PCI_CLASS_BRIDGE_HOST)
81 return;
1da177e4 82
6841ec68
YL
83 /* Don't touch ioapic devices already enabled by firmware */
84 if (class == PCI_CLASS_SYSTEM_PIC) {
85 u16 command;
86 pci_read_config_word(dev, PCI_COMMAND, &command);
87 if (command & (PCI_COMMAND_IO | PCI_COMMAND_MEMORY))
88 return;
89 }
1da177e4 90
6841ec68
YL
91 pdev_sort_resources(dev, head);
92}
23186279 93
6841ec68
YL
94static void __assign_resources_sorted(struct resource_list *head,
95 struct resource_list_x *fail_head)
96{
97 struct resource *res;
98 struct resource_list *list, *tmp;
99 int idx;
1da177e4 100
6841ec68 101 for (list = head->next; list;) {
1da177e4
LT
102 res = list->res;
103 idx = res - &list->dev->resource[0];
542df5de 104 if (pci_assign_resource(list->dev, idx)) {
568ddef8
YL
105 if (fail_head && !pci_is_root_bus(list->dev->bus))
106 add_to_failed_list(fail_head, list->dev, res);
542df5de 107 res->start = 0;
960b8466 108 res->end = 0;
542df5de
RS
109 res->flags = 0;
110 }
1da177e4
LT
111 tmp = list;
112 list = list->next;
113 kfree(tmp);
114 }
115}
116
6841ec68
YL
117static void pdev_assign_resources_sorted(struct pci_dev *dev,
118 struct resource_list_x *fail_head)
119{
120 struct resource_list head;
121
122 head.next = NULL;
123 __dev_sort_resources(dev, &head);
124 __assign_resources_sorted(&head, fail_head);
125
126}
127
128static void pbus_assign_resources_sorted(const struct pci_bus *bus,
129 struct resource_list_x *fail_head)
130{
131 struct pci_dev *dev;
132 struct resource_list head;
133
134 head.next = NULL;
135 list_for_each_entry(dev, &bus->devices, bus_list)
136 __dev_sort_resources(dev, &head);
137
138 __assign_resources_sorted(&head, fail_head);
139}
140
b3743fa4 141void pci_setup_cardbus(struct pci_bus *bus)
1da177e4
LT
142{
143 struct pci_dev *bridge = bus->self;
c7dabef8 144 struct resource *res;
1da177e4
LT
145 struct pci_bus_region region;
146
865df576
BH
147 dev_info(&bridge->dev, "CardBus bridge to [bus %02x-%02x]\n",
148 bus->secondary, bus->subordinate);
1da177e4 149
c7dabef8
BH
150 res = bus->resource[0];
151 pcibios_resource_to_bus(bridge, &region, res);
152 if (res->flags & IORESOURCE_IO) {
1da177e4
LT
153 /*
154 * The IO resource is allocated a range twice as large as it
155 * would normally need. This allows us to set both IO regs.
156 */
c7dabef8 157 dev_info(&bridge->dev, " bridge window %pR\n", res);
1da177e4
LT
158 pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
159 region.start);
160 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
161 region.end);
162 }
163
c7dabef8
BH
164 res = bus->resource[1];
165 pcibios_resource_to_bus(bridge, &region, res);
166 if (res->flags & IORESOURCE_IO) {
167 dev_info(&bridge->dev, " bridge window %pR\n", res);
1da177e4
LT
168 pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
169 region.start);
170 pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
171 region.end);
172 }
173
c7dabef8
BH
174 res = bus->resource[2];
175 pcibios_resource_to_bus(bridge, &region, res);
176 if (res->flags & IORESOURCE_MEM) {
177 dev_info(&bridge->dev, " bridge window %pR\n", res);
1da177e4
LT
178 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
179 region.start);
180 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
181 region.end);
182 }
183
c7dabef8
BH
184 res = bus->resource[3];
185 pcibios_resource_to_bus(bridge, &region, res);
186 if (res->flags & IORESOURCE_MEM) {
187 dev_info(&bridge->dev, " bridge window %pR\n", res);
1da177e4
LT
188 pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
189 region.start);
190 pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
191 region.end);
192 }
193}
b3743fa4 194EXPORT_SYMBOL(pci_setup_cardbus);
1da177e4
LT
195
196/* Initialize bridges with base/limit values we have collected.
197 PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
198 requires that if there is no I/O ports or memory behind the
199 bridge, corresponding range must be turned off by writing base
200 value greater than limit to the bridge's base/limit registers.
201
202 Note: care must be taken when updating I/O base/limit registers
203 of bridges which support 32-bit I/O. This update requires two
204 config space writes, so it's quite possible that an I/O window of
205 the bridge will have some undesirable address (e.g. 0) after the
206 first write. Ditto 64-bit prefetchable MMIO. */
7cc5997d 207static void pci_setup_bridge_io(struct pci_bus *bus)
1da177e4
LT
208{
209 struct pci_dev *bridge = bus->self;
c7dabef8 210 struct resource *res;
1da177e4 211 struct pci_bus_region region;
7cc5997d 212 u32 l, io_upper16;
1da177e4
LT
213
214 /* Set up the top and bottom of the PCI I/O segment for this bus. */
c7dabef8
BH
215 res = bus->resource[0];
216 pcibios_resource_to_bus(bridge, &region, res);
217 if (res->flags & IORESOURCE_IO) {
1da177e4
LT
218 pci_read_config_dword(bridge, PCI_IO_BASE, &l);
219 l &= 0xffff0000;
220 l |= (region.start >> 8) & 0x00f0;
221 l |= region.end & 0xf000;
222 /* Set up upper 16 bits of I/O base/limit. */
223 io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
c7dabef8 224 dev_info(&bridge->dev, " bridge window %pR\n", res);
7cc5997d 225 } else {
1da177e4
LT
226 /* Clear upper 16 bits of I/O base/limit. */
227 io_upper16 = 0;
228 l = 0x00f0;
c7dabef8 229 dev_info(&bridge->dev, " bridge window [io disabled]\n");
1da177e4
LT
230 }
231 /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
232 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
233 /* Update lower 16 bits of I/O base/limit. */
234 pci_write_config_dword(bridge, PCI_IO_BASE, l);
235 /* Update upper 16 bits of I/O base/limit. */
236 pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
7cc5997d
YL
237}
238
239static void pci_setup_bridge_mmio(struct pci_bus *bus)
240{
241 struct pci_dev *bridge = bus->self;
242 struct resource *res;
243 struct pci_bus_region region;
244 u32 l;
1da177e4 245
7cc5997d 246 /* Set up the top and bottom of the PCI Memory segment for this bus. */
c7dabef8
BH
247 res = bus->resource[1];
248 pcibios_resource_to_bus(bridge, &region, res);
249 if (res->flags & IORESOURCE_MEM) {
1da177e4
LT
250 l = (region.start >> 16) & 0xfff0;
251 l |= region.end & 0xfff00000;
c7dabef8 252 dev_info(&bridge->dev, " bridge window %pR\n", res);
7cc5997d 253 } else {
1da177e4 254 l = 0x0000fff0;
c7dabef8 255 dev_info(&bridge->dev, " bridge window [mem disabled]\n");
1da177e4
LT
256 }
257 pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
7cc5997d
YL
258}
259
260static void pci_setup_bridge_mmio_pref(struct pci_bus *bus)
261{
262 struct pci_dev *bridge = bus->self;
263 struct resource *res;
264 struct pci_bus_region region;
265 u32 l, bu, lu;
1da177e4
LT
266
267 /* Clear out the upper 32 bits of PREF limit.
268 If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
269 disables PREF range, which is ok. */
270 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
271
272 /* Set up PREF base/limit. */
c40a22e0 273 bu = lu = 0;
c7dabef8
BH
274 res = bus->resource[2];
275 pcibios_resource_to_bus(bridge, &region, res);
276 if (res->flags & IORESOURCE_PREFETCH) {
1da177e4
LT
277 l = (region.start >> 16) & 0xfff0;
278 l |= region.end & 0xfff00000;
c7dabef8 279 if (res->flags & IORESOURCE_MEM_64) {
1f82de10
YL
280 bu = upper_32_bits(region.start);
281 lu = upper_32_bits(region.end);
1f82de10 282 }
c7dabef8 283 dev_info(&bridge->dev, " bridge window %pR\n", res);
7cc5997d 284 } else {
1da177e4 285 l = 0x0000fff0;
c7dabef8 286 dev_info(&bridge->dev, " bridge window [mem pref disabled]\n");
1da177e4
LT
287 }
288 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
289
59353ea3
AW
290 /* Set the upper 32 bits of PREF base & limit. */
291 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, bu);
292 pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, lu);
7cc5997d
YL
293}
294
295static void __pci_setup_bridge(struct pci_bus *bus, unsigned long type)
296{
297 struct pci_dev *bridge = bus->self;
298
7cc5997d
YL
299 dev_info(&bridge->dev, "PCI bridge to [bus %02x-%02x]\n",
300 bus->secondary, bus->subordinate);
301
302 if (type & IORESOURCE_IO)
303 pci_setup_bridge_io(bus);
304
305 if (type & IORESOURCE_MEM)
306 pci_setup_bridge_mmio(bus);
307
308 if (type & IORESOURCE_PREFETCH)
309 pci_setup_bridge_mmio_pref(bus);
1da177e4
LT
310
311 pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, bus->bridge_ctl);
312}
313
7cc5997d
YL
314static void pci_setup_bridge(struct pci_bus *bus)
315{
316 unsigned long type = IORESOURCE_IO | IORESOURCE_MEM |
317 IORESOURCE_PREFETCH;
318
319 __pci_setup_bridge(bus, type);
320}
321
1da177e4
LT
322/* Check whether the bridge supports optional I/O and
323 prefetchable memory ranges. If not, the respective
324 base/limit registers must be read-only and read as 0. */
96bde06a 325static void pci_bridge_check_ranges(struct pci_bus *bus)
1da177e4
LT
326{
327 u16 io;
328 u32 pmem;
329 struct pci_dev *bridge = bus->self;
330 struct resource *b_res;
331
332 b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
333 b_res[1].flags |= IORESOURCE_MEM;
334
335 pci_read_config_word(bridge, PCI_IO_BASE, &io);
336 if (!io) {
337 pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
338 pci_read_config_word(bridge, PCI_IO_BASE, &io);
339 pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
340 }
341 if (io)
342 b_res[0].flags |= IORESOURCE_IO;
343 /* DECchip 21050 pass 2 errata: the bridge may miss an address
344 disconnect boundary by one PCI data phase.
345 Workaround: do not use prefetching on this device. */
346 if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
347 return;
348 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
349 if (!pmem) {
350 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
351 0xfff0fff0);
352 pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
353 pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
354 }
1f82de10 355 if (pmem) {
1da177e4 356 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
1f82de10
YL
357 if ((pmem & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64)
358 b_res[2].flags |= IORESOURCE_MEM_64;
359 }
360
361 /* double check if bridge does support 64 bit pref */
362 if (b_res[2].flags & IORESOURCE_MEM_64) {
363 u32 mem_base_hi, tmp;
364 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32,
365 &mem_base_hi);
366 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
367 0xffffffff);
368 pci_read_config_dword(bridge, PCI_PREF_BASE_UPPER32, &tmp);
369 if (!tmp)
370 b_res[2].flags &= ~IORESOURCE_MEM_64;
371 pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32,
372 mem_base_hi);
373 }
1da177e4
LT
374}
375
376/* Helper function for sizing routines: find first available
377 bus resource of a given type. Note: we intentionally skip
378 the bus resources which have already been assigned (that is,
379 have non-NULL parent resource). */
96bde06a 380static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type)
1da177e4
LT
381{
382 int i;
383 struct resource *r;
384 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
385 IORESOURCE_PREFETCH;
386
387 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
388 r = bus->resource[i];
299de034
IK
389 if (r == &ioport_resource || r == &iomem_resource)
390 continue;
55a10984
JB
391 if (r && (r->flags & type_mask) == type && !r->parent)
392 return r;
1da177e4
LT
393 }
394 return NULL;
395}
396
397/* Sizing the IO windows of the PCI-PCI bridge is trivial,
398 since these windows have 4K granularity and the IO ranges
399 of non-bridge PCI devices are limited to 256 bytes.
400 We must be careful with the ISA aliasing though. */
28760489 401static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size)
1da177e4
LT
402{
403 struct pci_dev *dev;
404 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
d65245c3 405 unsigned long size = 0, size1 = 0, old_size;
1da177e4
LT
406
407 if (!b_res)
408 return;
409
410 list_for_each_entry(dev, &bus->devices, bus_list) {
411 int i;
412
413 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
414 struct resource *r = &dev->resource[i];
415 unsigned long r_size;
416
417 if (r->parent || !(r->flags & IORESOURCE_IO))
418 continue;
022edd86 419 r_size = resource_size(r);
1da177e4
LT
420
421 if (r_size < 0x400)
422 /* Might be re-aligned for ISA */
423 size += r_size;
424 else
425 size1 += r_size;
426 }
427 }
28760489
EB
428 if (size < min_size)
429 size = min_size;
d65245c3
YL
430 old_size = resource_size(b_res);
431 if (old_size == 1)
432 old_size = 0;
1da177e4
LT
433/* To be fixed in 2.5: we should have sort of HAVE_ISA
434 flag in the struct pci_bus. */
435#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
436 size = (size & 0xff) + ((size & ~0xffUL) << 2);
437#endif
6f6f8c2f 438 size = ALIGN(size + size1, 4096);
d65245c3
YL
439 if (size < old_size)
440 size = old_size;
1da177e4 441 if (!size) {
865df576
BH
442 if (b_res->start || b_res->end)
443 dev_info(&bus->self->dev, "disabling bridge window "
444 "%pR to [bus %02x-%02x] (unused)\n", b_res,
445 bus->secondary, bus->subordinate);
1da177e4
LT
446 b_res->flags = 0;
447 return;
448 }
449 /* Alignment of the IO window is always 4K */
450 b_res->start = 4096;
451 b_res->end = b_res->start + size - 1;
88452565 452 b_res->flags |= IORESOURCE_STARTALIGN;
1da177e4
LT
453}
454
455/* Calculate the size of the bus and minimal alignment which
456 guarantees that all child resources fit in this size. */
28760489
EB
457static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
458 unsigned long type, resource_size_t min_size)
1da177e4
LT
459{
460 struct pci_dev *dev;
d65245c3 461 resource_size_t min_align, align, size, old_size;
c40a22e0 462 resource_size_t aligns[12]; /* Alignments from 1Mb to 2Gb */
1da177e4
LT
463 int order, max_order;
464 struct resource *b_res = find_free_bus_resource(bus, type);
1f82de10 465 unsigned int mem64_mask = 0;
1da177e4
LT
466
467 if (!b_res)
468 return 0;
469
470 memset(aligns, 0, sizeof(aligns));
471 max_order = 0;
472 size = 0;
473
1f82de10
YL
474 mem64_mask = b_res->flags & IORESOURCE_MEM_64;
475 b_res->flags &= ~IORESOURCE_MEM_64;
476
1da177e4
LT
477 list_for_each_entry(dev, &bus->devices, bus_list) {
478 int i;
1f82de10 479
1da177e4
LT
480 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
481 struct resource *r = &dev->resource[i];
c40a22e0 482 resource_size_t r_size;
1da177e4
LT
483
484 if (r->parent || (r->flags & mask) != type)
485 continue;
022edd86 486 r_size = resource_size(r);
1da177e4 487 /* For bridges size != alignment */
6faf17f6 488 align = pci_resource_alignment(dev, r);
1da177e4
LT
489 order = __ffs(align) - 20;
490 if (order > 11) {
865df576
BH
491 dev_warn(&dev->dev, "disabling BAR %d: %pR "
492 "(bad alignment %#llx)\n", i, r,
493 (unsigned long long) align);
1da177e4
LT
494 r->flags = 0;
495 continue;
496 }
497 size += r_size;
498 if (order < 0)
499 order = 0;
500 /* Exclude ranges with size > align from
501 calculation of the alignment. */
502 if (r_size == align)
503 aligns[order] += align;
504 if (order > max_order)
505 max_order = order;
1f82de10 506 mem64_mask &= r->flags & IORESOURCE_MEM_64;
1da177e4
LT
507 }
508 }
28760489
EB
509 if (size < min_size)
510 size = min_size;
d65245c3
YL
511 old_size = resource_size(b_res);
512 if (old_size == 1)
513 old_size = 0;
514 if (size < old_size)
515 size = old_size;
1da177e4
LT
516
517 align = 0;
518 min_align = 0;
519 for (order = 0; order <= max_order; order++) {
8308c54d
JF
520 resource_size_t align1 = 1;
521
522 align1 <<= (order + 20);
523
1da177e4
LT
524 if (!align)
525 min_align = align1;
6f6f8c2f 526 else if (ALIGN(align + min_align, min_align) < align1)
1da177e4
LT
527 min_align = align1 >> 1;
528 align += aligns[order];
529 }
6f6f8c2f 530 size = ALIGN(size, min_align);
1da177e4 531 if (!size) {
865df576
BH
532 if (b_res->start || b_res->end)
533 dev_info(&bus->self->dev, "disabling bridge window "
534 "%pR to [bus %02x-%02x] (unused)\n", b_res,
535 bus->secondary, bus->subordinate);
1da177e4
LT
536 b_res->flags = 0;
537 return 1;
538 }
539 b_res->start = min_align;
540 b_res->end = size + min_align - 1;
88452565 541 b_res->flags |= IORESOURCE_STARTALIGN;
1f82de10 542 b_res->flags |= mem64_mask;
1da177e4
LT
543 return 1;
544}
545
5468ae61 546static void pci_bus_size_cardbus(struct pci_bus *bus)
1da177e4
LT
547{
548 struct pci_dev *bridge = bus->self;
549 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
550 u16 ctrl;
551
552 /*
553 * Reserve some resources for CardBus. We reserve
554 * a fixed amount of bus space for CardBus bridges.
555 */
934b7024
LT
556 b_res[0].start = 0;
557 b_res[0].end = pci_cardbus_io_size - 1;
558 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
1da177e4 559
934b7024
LT
560 b_res[1].start = 0;
561 b_res[1].end = pci_cardbus_io_size - 1;
562 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN;
1da177e4
LT
563
564 /*
565 * Check whether prefetchable memory is supported
566 * by this bridge.
567 */
568 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
569 if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
570 ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
571 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
572 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
573 }
574
575 /*
576 * If we have prefetchable memory support, allocate
577 * two regions. Otherwise, allocate one region of
578 * twice the size.
579 */
580 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
934b7024
LT
581 b_res[2].start = 0;
582 b_res[2].end = pci_cardbus_mem_size - 1;
583 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN;
1da177e4 584
934b7024
LT
585 b_res[3].start = 0;
586 b_res[3].end = pci_cardbus_mem_size - 1;
587 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
1da177e4 588 } else {
934b7024
LT
589 b_res[3].start = 0;
590 b_res[3].end = pci_cardbus_mem_size * 2 - 1;
591 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN;
1da177e4
LT
592 }
593}
594
451124a7 595void __ref pci_bus_size_bridges(struct pci_bus *bus)
1da177e4
LT
596{
597 struct pci_dev *dev;
598 unsigned long mask, prefmask;
28760489 599 resource_size_t min_mem_size = 0, min_io_size = 0;
1da177e4
LT
600
601 list_for_each_entry(dev, &bus->devices, bus_list) {
602 struct pci_bus *b = dev->subordinate;
603 if (!b)
604 continue;
605
606 switch (dev->class >> 8) {
607 case PCI_CLASS_BRIDGE_CARDBUS:
608 pci_bus_size_cardbus(b);
609 break;
610
611 case PCI_CLASS_BRIDGE_PCI:
612 default:
613 pci_bus_size_bridges(b);
614 break;
615 }
616 }
617
618 /* The root bus? */
619 if (!bus->self)
620 return;
621
622 switch (bus->self->class >> 8) {
623 case PCI_CLASS_BRIDGE_CARDBUS:
624 /* don't size cardbuses yet. */
625 break;
626
627 case PCI_CLASS_BRIDGE_PCI:
628 pci_bridge_check_ranges(bus);
28760489
EB
629 if (bus->self->is_hotplug_bridge) {
630 min_io_size = pci_hotplug_io_size;
631 min_mem_size = pci_hotplug_mem_size;
632 }
1da177e4 633 default:
28760489 634 pbus_size_io(bus, min_io_size);
1da177e4
LT
635 /* If the bridge supports prefetchable range, size it
636 separately. If it doesn't, or its prefetchable window
637 has already been allocated by arch code, try
638 non-prefetchable range for both types of PCI memory
639 resources. */
640 mask = IORESOURCE_MEM;
641 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
28760489 642 if (pbus_size_mem(bus, prefmask, prefmask, min_mem_size))
1da177e4 643 mask = prefmask; /* Success, size non-prefetch only. */
28760489
EB
644 else
645 min_mem_size += min_mem_size;
646 pbus_size_mem(bus, mask, IORESOURCE_MEM, min_mem_size);
1da177e4
LT
647 break;
648 }
649}
650EXPORT_SYMBOL(pci_bus_size_bridges);
651
568ddef8
YL
652static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
653 struct resource_list_x *fail_head)
1da177e4
LT
654{
655 struct pci_bus *b;
656 struct pci_dev *dev;
657
568ddef8 658 pbus_assign_resources_sorted(bus, fail_head);
1da177e4 659
1da177e4
LT
660 list_for_each_entry(dev, &bus->devices, bus_list) {
661 b = dev->subordinate;
662 if (!b)
663 continue;
664
568ddef8 665 __pci_bus_assign_resources(b, fail_head);
1da177e4
LT
666
667 switch (dev->class >> 8) {
668 case PCI_CLASS_BRIDGE_PCI:
6841ec68
YL
669 if (!pci_is_enabled(dev))
670 pci_setup_bridge(b);
1da177e4
LT
671 break;
672
673 case PCI_CLASS_BRIDGE_CARDBUS:
674 pci_setup_cardbus(b);
675 break;
676
677 default:
80ccba11
BH
678 dev_info(&dev->dev, "not setting up bridge for bus "
679 "%04x:%02x\n", pci_domain_nr(b), b->number);
1da177e4
LT
680 break;
681 }
682 }
683}
568ddef8
YL
684
685void __ref pci_bus_assign_resources(const struct pci_bus *bus)
686{
687 __pci_bus_assign_resources(bus, NULL);
688}
1da177e4
LT
689EXPORT_SYMBOL(pci_bus_assign_resources);
690
6841ec68
YL
691static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
692 struct resource_list_x *fail_head)
693{
694 struct pci_bus *b;
695
696 pdev_assign_resources_sorted((struct pci_dev *)bridge, fail_head);
697
698 b = bridge->subordinate;
699 if (!b)
700 return;
701
702 __pci_bus_assign_resources(b, fail_head);
703
704 switch (bridge->class >> 8) {
705 case PCI_CLASS_BRIDGE_PCI:
706 pci_setup_bridge(b);
707 break;
708
709 case PCI_CLASS_BRIDGE_CARDBUS:
710 pci_setup_cardbus(b);
711 break;
712
713 default:
714 dev_info(&bridge->dev, "not setting up bridge for bus "
715 "%04x:%02x\n", pci_domain_nr(b), b->number);
716 break;
717 }
718}
5009b460
YL
719static void pci_bridge_release_resources(struct pci_bus *bus,
720 unsigned long type)
721{
722 int idx;
723 bool changed = false;
724 struct pci_dev *dev;
725 struct resource *r;
726 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
727 IORESOURCE_PREFETCH;
728
729 dev = bus->self;
730 for (idx = PCI_BRIDGE_RESOURCES; idx <= PCI_BRIDGE_RESOURCE_END;
731 idx++) {
732 r = &dev->resource[idx];
733 if ((r->flags & type_mask) != type)
734 continue;
735 if (!r->parent)
736 continue;
737 /*
738 * if there are children under that, we should release them
739 * all
740 */
741 release_child_resources(r);
742 if (!release_resource(r)) {
743 dev_printk(KERN_DEBUG, &dev->dev,
744 "resource %d %pR released\n", idx, r);
745 /* keep the old size */
746 r->end = resource_size(r) - 1;
747 r->start = 0;
748 r->flags = 0;
749 changed = true;
750 }
751 }
752
753 if (changed) {
754 /* avoiding touch the one without PREF */
755 if (type & IORESOURCE_PREFETCH)
756 type = IORESOURCE_PREFETCH;
757 __pci_setup_bridge(bus, type);
758 }
759}
760
761enum release_type {
762 leaf_only,
763 whole_subtree,
764};
765/*
766 * try to release pci bridge resources that is from leaf bridge,
767 * so we can allocate big new one later
768 */
769static void __ref pci_bus_release_bridge_resources(struct pci_bus *bus,
770 unsigned long type,
771 enum release_type rel_type)
772{
773 struct pci_dev *dev;
774 bool is_leaf_bridge = true;
775
776 list_for_each_entry(dev, &bus->devices, bus_list) {
777 struct pci_bus *b = dev->subordinate;
778 if (!b)
779 continue;
780
781 is_leaf_bridge = false;
782
783 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
784 continue;
785
786 if (rel_type == whole_subtree)
787 pci_bus_release_bridge_resources(b, type,
788 whole_subtree);
789 }
790
791 if (pci_is_root_bus(bus))
792 return;
793
794 if ((bus->self->class >> 8) != PCI_CLASS_BRIDGE_PCI)
795 return;
796
797 if ((rel_type == whole_subtree) || is_leaf_bridge)
798 pci_bridge_release_resources(bus, type);
799}
800
76fbc263
YL
801static void pci_bus_dump_res(struct pci_bus *bus)
802{
803 int i;
804
805 for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
806 struct resource *res = bus->resource[i];
7c9342b8
YL
807
808 if (!res || !res->end || !res->flags)
76fbc263
YL
809 continue;
810
c7dabef8 811 dev_printk(KERN_DEBUG, &bus->dev, "resource %d %pR\n", i, res);
76fbc263
YL
812 }
813}
814
815static void pci_bus_dump_resources(struct pci_bus *bus)
816{
817 struct pci_bus *b;
818 struct pci_dev *dev;
819
820
821 pci_bus_dump_res(bus);
822
823 list_for_each_entry(dev, &bus->devices, bus_list) {
824 b = dev->subordinate;
825 if (!b)
826 continue;
827
828 pci_bus_dump_resources(b);
829 }
830}
831
977d17bb
YL
832static int __init pci_bus_get_depth(struct pci_bus *bus)
833{
834 int depth = 0;
835 struct pci_dev *dev;
836
837 list_for_each_entry(dev, &bus->devices, bus_list) {
838 int ret;
839 struct pci_bus *b = dev->subordinate;
840 if (!b)
841 continue;
842
843 ret = pci_bus_get_depth(b);
844 if (ret + 1 > depth)
845 depth = ret + 1;
846 }
847
848 return depth;
849}
850static int __init pci_get_max_depth(void)
851{
852 int depth = 0;
853 struct pci_bus *bus;
854
855 list_for_each_entry(bus, &pci_root_buses, node) {
856 int ret;
857
858 ret = pci_bus_get_depth(bus);
859 if (ret > depth)
860 depth = ret;
861 }
862
863 return depth;
864}
865
866/*
867 * first try will not touch pci bridge res
868 * second and later try will clear small leaf bridge res
869 * will stop till to the max deepth if can not find good one
870 */
1da177e4
LT
871void __init
872pci_assign_unassigned_resources(void)
873{
874 struct pci_bus *bus;
977d17bb
YL
875 int tried_times = 0;
876 enum release_type rel_type = leaf_only;
877 struct resource_list_x head, *list;
878 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
879 IORESOURCE_PREFETCH;
880 unsigned long failed_type;
881 int max_depth = pci_get_max_depth();
882 int pci_try_num;
883
884 head.next = NULL;
1da177e4 885
977d17bb
YL
886 pci_try_num = max_depth + 1;
887 printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
888 max_depth, pci_try_num);
889
890again:
1da177e4
LT
891 /* Depth first, calculate sizes and alignments of all
892 subordinate buses. */
893 list_for_each_entry(bus, &pci_root_buses, node) {
894 pci_bus_size_bridges(bus);
895 }
896 /* Depth last, allocate resources and update the hardware. */
897 list_for_each_entry(bus, &pci_root_buses, node) {
977d17bb
YL
898 __pci_bus_assign_resources(bus, &head);
899 }
900 tried_times++;
901
902 /* any device complain? */
903 if (!head.next)
904 goto enable_and_dump;
905 failed_type = 0;
906 for (list = head.next; list;) {
907 failed_type |= list->flags;
908 list = list->next;
909 }
910 /*
911 * io port are tight, don't try extra
912 * or if reach the limit, don't want to try more
913 */
914 failed_type &= type_mask;
915 if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) {
916 free_failed_list(&head);
917 goto enable_and_dump;
918 }
919
920 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
921 tried_times + 1);
922
923 /* third times and later will not check if it is leaf */
924 if ((tried_times + 1) > 2)
925 rel_type = whole_subtree;
926
927 /*
928 * Try to release leaf bridge's resources that doesn't fit resource of
929 * child device under that bridge
930 */
931 for (list = head.next; list;) {
932 bus = list->dev->bus;
933 pci_bus_release_bridge_resources(bus, list->flags & type_mask,
934 rel_type);
935 list = list->next;
1da177e4 936 }
977d17bb
YL
937 /* restore size and flags */
938 for (list = head.next; list;) {
939 struct resource *res = list->res;
940
941 res->start = list->start;
942 res->end = list->end;
943 res->flags = list->flags;
944 if (list->dev->subordinate)
945 res->flags = 0;
946
947 list = list->next;
948 }
949 free_failed_list(&head);
950
951 goto again;
952
953enable_and_dump:
954 /* Depth last, update the hardware. */
955 list_for_each_entry(bus, &pci_root_buses, node)
956 pci_enable_bridges(bus);
76fbc263
YL
957
958 /* dump the resource on buses */
959 list_for_each_entry(bus, &pci_root_buses, node) {
960 pci_bus_dump_resources(bus);
961 }
1da177e4 962}
6841ec68
YL
963
964void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
965{
966 struct pci_bus *parent = bridge->subordinate;
32180e40
YL
967 int tried_times = 0;
968 struct resource_list_x head, *list;
6841ec68 969 int retval;
32180e40
YL
970 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
971 IORESOURCE_PREFETCH;
972
973 head.next = NULL;
6841ec68 974
32180e40 975again:
6841ec68 976 pci_bus_size_bridges(parent);
32180e40 977 __pci_bridge_assign_resources(bridge, &head);
6841ec68
YL
978 retval = pci_reenable_device(bridge);
979 pci_set_master(bridge);
980 pci_enable_bridges(parent);
32180e40
YL
981
982 tried_times++;
983
984 if (!head.next)
985 return;
986
987 if (tried_times >= 2) {
988 /* still fail, don't need to try more */
989 free_failed_list(&head);
990 return;
991 }
992
993 printk(KERN_DEBUG "PCI: No. %d try to assign unassigned res\n",
994 tried_times + 1);
995
996 /*
997 * Try to release leaf bridge's resources that doesn't fit resource of
998 * child device under that bridge
999 */
1000 for (list = head.next; list;) {
1001 struct pci_bus *bus = list->dev->bus;
1002 unsigned long flags = list->flags;
1003
1004 pci_bus_release_bridge_resources(bus, flags & type_mask,
1005 whole_subtree);
1006 list = list->next;
1007 }
1008 /* restore size and flags */
1009 for (list = head.next; list;) {
1010 struct resource *res = list->res;
1011
1012 res->start = list->start;
1013 res->end = list->end;
1014 res->flags = list->flags;
1015 if (list->dev->subordinate)
1016 res->flags = 0;
1017
1018 list = list->next;
1019 }
1020 free_failed_list(&head);
1021
1022 goto again;
6841ec68
YL
1023}
1024EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
This page took 0.660714 seconds and 5 git commands to generate.