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