Subversion Repositories shark

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
428 giacomo 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>
28
 
29
 
30
#define DEBUG_CONFIG 1
31
#if DEBUG_CONFIG
32
# define DBGC(args)     printk args
33
#else
34
# define DBGC(args)
35
#endif
36
 
37
#define ROUND_UP(x, a)          (((x) + (a) - 1) & ~((a) - 1))
38
 
39
/*
40
 * FIXME: IO should be max 256 bytes.  However, since we may
41
 * have a P2P bridge below a cardbus bridge, we need 4K.
42
 */
43
#define CARDBUS_IO_SIZE         (4096)
44
#define CARDBUS_MEM_SIZE        (32*1024*1024)
45
 
46
static int __devinit
47
pbus_assign_resources_sorted(struct pci_bus *bus)
48
{
49
        struct pci_dev *dev;
50
        struct resource *res;
51
        struct resource_list head, *list, *tmp;
52
        int idx, found_vga = 0;
53
 
54
        head.next = NULL;
55
        list_for_each_entry(dev, &bus->devices, bus_list) {
56
                u16 class = dev->class >> 8;
57
 
58
                if (class == PCI_CLASS_DISPLAY_VGA
59
                                || class == PCI_CLASS_NOT_DEFINED_VGA)
60
                        found_vga = 1;
61
 
62
                pdev_sort_resources(dev, &head);
63
        }
64
 
65
        for (list = head.next; list;) {
66
                res = list->res;
67
                idx = res - &list->dev->resource[0];
68
                pci_assign_resource(list->dev, idx);
69
                tmp = list;
70
                list = list->next;
71
                kfree(tmp);
72
        }
73
 
74
        return found_vga;
75
}
76
 
77
static void __devinit
78
pci_setup_cardbus(struct pci_bus *bus)
79
{
80
        struct pci_dev *bridge = bus->self;
81
        struct pci_bus_region region;
82
 
83
        printk("PCI: Bus %d, cardbus bridge: %s\n",
84
                bus->number, pci_name(bridge));
85
 
86
        pcibios_resource_to_bus(bridge, &region, bus->resource[0]);
87
        if (bus->resource[0]->flags & IORESOURCE_IO) {
88
                /*
89
                 * The IO resource is allocated a range twice as large as it
90
                 * would normally need.  This allows us to set both IO regs.
91
                 */
92
                printk("  IO window: %08lx-%08lx\n",
93
                        region.start, region.end);
94
                pci_write_config_dword(bridge, PCI_CB_IO_BASE_0,
95
                                        region.start);
96
                pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_0,
97
                                        region.end);
98
        }
99
 
100
        pcibios_resource_to_bus(bridge, &region, bus->resource[1]);
101
        if (bus->resource[1]->flags & IORESOURCE_IO) {
102
                printk("  IO window: %08lx-%08lx\n",
103
                        region.start, region.end);
104
                pci_write_config_dword(bridge, PCI_CB_IO_BASE_1,
105
                                        region.start);
106
                pci_write_config_dword(bridge, PCI_CB_IO_LIMIT_1,
107
                                        region.end);
108
        }
109
 
110
        pcibios_resource_to_bus(bridge, &region, bus->resource[2]);
111
        if (bus->resource[2]->flags & IORESOURCE_MEM) {
112
                printk("  PREFETCH window: %08lx-%08lx\n",
113
                        region.start, region.end);
114
                pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_0,
115
                                        region.start);
116
                pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_0,
117
                                        region.end);
118
        }
119
 
120
        pcibios_resource_to_bus(bridge, &region, bus->resource[3]);
121
        if (bus->resource[3]->flags & IORESOURCE_MEM) {
122
                printk("  MEM window: %08lx-%08lx\n",
123
                        region.start, region.end);
124
                pci_write_config_dword(bridge, PCI_CB_MEMORY_BASE_1,
125
                                        region.start);
126
                pci_write_config_dword(bridge, PCI_CB_MEMORY_LIMIT_1,
127
                                        region.end);
128
        }
129
}
130
 
131
/* Initialize bridges with base/limit values we have collected.
132
   PCI-to-PCI Bridge Architecture Specification rev. 1.1 (1998)
133
   requires that if there is no I/O ports or memory behind the
134
   bridge, corresponding range must be turned off by writing base
135
   value greater than limit to the bridge's base/limit registers.
136
 
137
   Note: care must be taken when updating I/O base/limit registers
138
   of bridges which support 32-bit I/O. This update requires two
139
   config space writes, so it's quite possible that an I/O window of
140
   the bridge will have some undesirable address (e.g. 0) after the
141
   first write. Ditto 64-bit prefetchable MMIO.  */
142
static void __devinit
143
pci_setup_bridge(struct pci_bus *bus)
144
{
145
        struct pci_dev *bridge = bus->self;
146
        struct pci_bus_region region;
147
        u32 l, io_upper16;
148
 
149
        DBGC((KERN_INFO "PCI: Bus %d, bridge: %s\n",
150
                        bus->number, pci_name(bridge)));
151
 
152
        /* Set up the top and bottom of the PCI I/O segment for this bus. */
153
        pcibios_resource_to_bus(bridge, &region, bus->resource[0]);
154
        if (bus->resource[0]->flags & IORESOURCE_IO) {
155
                pci_read_config_dword(bridge, PCI_IO_BASE, &l);
156
                l &= 0xffff0000;
157
                l |= (region.start >> 8) & 0x00f0;
158
                l |= region.end & 0xf000;
159
                /* Set up upper 16 bits of I/O base/limit. */
160
                io_upper16 = (region.end & 0xffff0000) | (region.start >> 16);
161
                DBGC((KERN_INFO "  IO window: %04lx-%04lx\n",
162
                                region.start, region.end));
163
        }
164
        else {
165
                /* Clear upper 16 bits of I/O base/limit. */
166
                io_upper16 = 0;
167
                l = 0x00f0;
168
                DBGC((KERN_INFO "  IO window: disabled.\n"));
169
        }
170
        /* Temporarily disable the I/O range before updating PCI_IO_BASE. */
171
        pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, 0x0000ffff);
172
        /* Update lower 16 bits of I/O base/limit. */
173
        pci_write_config_dword(bridge, PCI_IO_BASE, l);
174
        /* Update upper 16 bits of I/O base/limit. */
175
        pci_write_config_dword(bridge, PCI_IO_BASE_UPPER16, io_upper16);
176
 
177
        /* Set up the top and bottom of the PCI Memory segment
178
           for this bus. */
179
        pcibios_resource_to_bus(bridge, &region, bus->resource[1]);
180
        if (bus->resource[1]->flags & IORESOURCE_MEM) {
181
                l = (region.start >> 16) & 0xfff0;
182
                l |= region.end & 0xfff00000;
183
                DBGC((KERN_INFO "  MEM window: %08lx-%08lx\n",
184
                                region.start, region.end));
185
        }
186
        else {
187
                l = 0x0000fff0;
188
                DBGC((KERN_INFO "  MEM window: disabled.\n"));
189
        }
190
        pci_write_config_dword(bridge, PCI_MEMORY_BASE, l);
191
 
192
        /* Clear out the upper 32 bits of PREF limit.
193
           If PCI_PREF_BASE_UPPER32 was non-zero, this temporarily
194
           disables PREF range, which is ok. */
195
        pci_write_config_dword(bridge, PCI_PREF_LIMIT_UPPER32, 0);
196
 
197
        /* Set up PREF base/limit. */
198
        pcibios_resource_to_bus(bridge, &region, bus->resource[2]);
199
        if (bus->resource[2]->flags & IORESOURCE_PREFETCH) {
200
                l = (region.start >> 16) & 0xfff0;
201
                l |= region.end & 0xfff00000;
202
                DBGC((KERN_INFO "  PREFETCH window: %08lx-%08lx\n",
203
                                region.start, region.end));
204
        }
205
        else {
206
                l = 0x0000fff0;
207
                DBGC((KERN_INFO "  PREFETCH window: disabled.\n"));
208
        }
209
        pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, l);
210
 
211
        /* Clear out the upper 32 bits of PREF base. */
212
        pci_write_config_dword(bridge, PCI_PREF_BASE_UPPER32, 0);
213
 
214
        /* Check if we have VGA behind the bridge.
215
           Enable ISA in either case (FIXME!). */
216
        l = (bus->resource[0]->flags & IORESOURCE_BUS_HAS_VGA) ? 0x0c : 0x04;
217
        pci_write_config_word(bridge, PCI_BRIDGE_CONTROL, l);
218
}
219
 
220
/* Check whether the bridge supports optional I/O and
221
   prefetchable memory ranges. If not, the respective
222
   base/limit registers must be read-only and read as 0. */
223
static void __devinit
224
pci_bridge_check_ranges(struct pci_bus *bus)
225
{
226
        u16 io;
227
        u32 pmem;
228
        struct pci_dev *bridge = bus->self;
229
        struct resource *b_res;
230
 
231
        b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
232
        b_res[1].flags |= IORESOURCE_MEM;
233
 
234
        pci_read_config_word(bridge, PCI_IO_BASE, &io);
235
        if (!io) {
236
                pci_write_config_word(bridge, PCI_IO_BASE, 0xf0f0);
237
                pci_read_config_word(bridge, PCI_IO_BASE, &io);
238
                pci_write_config_word(bridge, PCI_IO_BASE, 0x0);
239
        }
240
        if (io)
241
                b_res[0].flags |= IORESOURCE_IO;
242
        /*  DECchip 21050 pass 2 errata: the bridge may miss an address
243
            disconnect boundary by one PCI data phase.
244
            Workaround: do not use prefetching on this device. */
245
        if (bridge->vendor == PCI_VENDOR_ID_DEC && bridge->device == 0x0001)
246
                return;
247
        pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
248
        if (!pmem) {
249
                pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE,
250
                                               0xfff0fff0);
251
                pci_read_config_dword(bridge, PCI_PREF_MEMORY_BASE, &pmem);
252
                pci_write_config_dword(bridge, PCI_PREF_MEMORY_BASE, 0x0);
253
        }
254
        if (pmem)
255
                b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
256
}
257
 
258
/* Helper function for sizing routines: find first available
259
   bus resource of a given type. Note: we intentionally skip
260
   the bus resources which have already been assigned (that is,
261
   have non-NULL parent resource). */
262
static struct resource * __devinit
263
find_free_bus_resource(struct pci_bus *bus, unsigned long type)
264
{
265
        int i;
266
        struct resource *r;
267
        unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
268
                                  IORESOURCE_PREFETCH;
269
 
270
        for (i = 0; i < PCI_BUS_NUM_RESOURCES; i++) {
271
                r = bus->resource[i];
272
                if (r && (r->flags & type_mask) == type && !r->parent)
273
                        return r;
274
        }
275
        return NULL;
276
}
277
 
278
/* Sizing the IO windows of the PCI-PCI bridge is trivial,
279
   since these windows have 4K granularity and the IO ranges
280
   of non-bridge PCI devices are limited to 256 bytes.
281
   We must be careful with the ISA aliasing though. */
282
static void __devinit
283
pbus_size_io(struct pci_bus *bus)
284
{
285
        struct pci_dev *dev;
286
        struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
287
        unsigned long size = 0, size1 = 0;
288
 
289
        if (!b_res)
290
                return;
291
 
292
        list_for_each_entry(dev, &bus->devices, bus_list) {
293
                int i;
294
 
295
                for (i = 0; i < PCI_NUM_RESOURCES; i++) {
296
                        struct resource *r = &dev->resource[i];
297
                        unsigned long r_size;
298
 
299
                        if (r->parent || !(r->flags & IORESOURCE_IO))
300
                                continue;
301
                        r_size = r->end - r->start + 1;
302
 
303
                        if (r_size < 0x400)
304
                                /* Might be re-aligned for ISA */
305
                                size += r_size;
306
                        else
307
                                size1 += r_size;
308
                }
309
        }
310
/* To be fixed in 2.5: we should have sort of HAVE_ISA
311
   flag in the struct pci_bus. */
312
#if defined(CONFIG_ISA) || defined(CONFIG_EISA)
313
        size = (size & 0xff) + ((size & ~0xffUL) << 2);
314
#endif
315
        size = ROUND_UP(size + size1, 4096);
316
        if (!size) {
317
                b_res->flags = 0;
318
                return;
319
        }
320
        /* Alignment of the IO window is always 4K */
321
        b_res->start = 4096;
322
        b_res->end = b_res->start + size - 1;
323
}
324
 
325
/* Calculate the size of the bus and minimal alignment which
326
   guarantees that all child resources fit in this size. */
327
static int __devinit
328
pbus_size_mem(struct pci_bus *bus, unsigned long mask, unsigned long type)
329
{
330
        struct pci_dev *dev;
331
        unsigned long min_align, align, size;
332
        unsigned long aligns[12];       /* Alignments from 1Mb to 2Gb */
333
        int order, max_order;
334
        struct resource *b_res = find_free_bus_resource(bus, type);
335
 
336
        if (!b_res)
337
                return 0;
338
 
339
        memset(aligns, 0, sizeof(aligns));
340
        max_order = 0;
341
        size = 0;
342
 
343
        list_for_each_entry(dev, &bus->devices, bus_list) {
344
                int i;
345
 
346
                for (i = 0; i < PCI_NUM_RESOURCES; i++) {
347
                        struct resource *r = &dev->resource[i];
348
                        unsigned long r_size;
349
 
350
                        if (r->parent || (r->flags & mask) != type)
351
                                continue;
352
                        r_size = r->end - r->start + 1;
353
                        /* For bridges size != alignment */
354
                        align = (i < PCI_BRIDGE_RESOURCES) ? r_size : r->start;
355
                        order = __ffs(align) - 20;
356
                        if (order > 11) {
357
                                printk(KERN_WARNING "PCI: region %s/%d "
358
                                       "too large: %lx-%lx\n",
359
                                       pci_name(dev), i, r->start, r->end);
360
                                r->flags = 0;
361
                                continue;
362
                        }
363
                        size += r_size;
364
                        if (order < 0)
365
                                order = 0;
366
                        /* Exclude ranges with size > align from
367
                           calculation of the alignment. */
368
                        if (r_size == align)
369
                                aligns[order] += align;
370
                        if (order > max_order)
371
                                max_order = order;
372
                }
373
        }
374
 
375
        align = 0;
376
        min_align = 0;
377
        for (order = 0; order <= max_order; order++) {
378
                unsigned long align1 = 1UL << (order + 20);
379
 
380
                if (!align)
381
                        min_align = align1;
382
                else if (ROUND_UP(align + min_align, min_align) < align1)
383
                        min_align = align1 >> 1;
384
                align += aligns[order];
385
        }
386
        size = ROUND_UP(size, min_align);
387
        if (!size) {
388
                b_res->flags = 0;
389
                return 1;
390
        }
391
        b_res->start = min_align;
392
        b_res->end = size + min_align - 1;
393
        return 1;
394
}
395
 
396
static void __devinit
397
pci_bus_size_cardbus(struct pci_bus *bus)
398
{
399
        struct pci_dev *bridge = bus->self;
400
        struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
401
        u16 ctrl;
402
 
403
        /*
404
         * Reserve some resources for CardBus.  We reserve
405
         * a fixed amount of bus space for CardBus bridges.
406
         */
407
        b_res[0].start = CARDBUS_IO_SIZE;
408
        b_res[0].end = b_res[0].start + CARDBUS_IO_SIZE - 1;
409
        b_res[0].flags |= IORESOURCE_IO;
410
 
411
        b_res[1].start = CARDBUS_IO_SIZE;
412
        b_res[1].end = b_res[1].start + CARDBUS_IO_SIZE - 1;
413
        b_res[1].flags |= IORESOURCE_IO;
414
 
415
        /*
416
         * Check whether prefetchable memory is supported
417
         * by this bridge.
418
         */
419
        pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
420
        if (!(ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0)) {
421
                ctrl |= PCI_CB_BRIDGE_CTL_PREFETCH_MEM0;
422
                pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
423
                pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
424
        }
425
 
426
        /*
427
         * If we have prefetchable memory support, allocate
428
         * two regions.  Otherwise, allocate one region of
429
         * twice the size.
430
         */
431
        if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
432
                b_res[2].start = CARDBUS_MEM_SIZE;
433
                b_res[2].end = b_res[2].start + CARDBUS_MEM_SIZE - 1;
434
                b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
435
 
436
                b_res[3].start = CARDBUS_MEM_SIZE;
437
                b_res[3].end = b_res[3].start + CARDBUS_MEM_SIZE - 1;
438
                b_res[3].flags |= IORESOURCE_MEM;
439
        } else {
440
                b_res[3].start = CARDBUS_MEM_SIZE * 2;
441
                b_res[3].end = b_res[3].start + CARDBUS_MEM_SIZE * 2 - 1;
442
                b_res[3].flags |= IORESOURCE_MEM;
443
        }
444
}
445
 
446
void __devinit
447
pci_bus_size_bridges(struct pci_bus *bus)
448
{
449
        struct pci_dev *dev;
450
        unsigned long mask, prefmask;
451
 
452
        list_for_each_entry(dev, &bus->devices, bus_list) {
453
                struct pci_bus *b = dev->subordinate;
454
                if (!b)
455
                        continue;
456
 
457
                switch (dev->class >> 8) {
458
                case PCI_CLASS_BRIDGE_CARDBUS:
459
                        pci_bus_size_cardbus(b);
460
                        break;
461
 
462
                case PCI_CLASS_BRIDGE_PCI:
463
                default:
464
                        pci_bus_size_bridges(b);
465
                        break;
466
                }
467
        }
468
 
469
        /* The root bus? */
470
        if (!bus->self)
471
                return;
472
 
473
        switch (bus->self->class >> 8) {
474
        case PCI_CLASS_BRIDGE_CARDBUS:
475
                /* don't size cardbuses yet. */
476
                break;
477
 
478
        case PCI_CLASS_BRIDGE_PCI:
479
                pci_bridge_check_ranges(bus);
480
        default:
481
                pbus_size_io(bus);
482
                /* If the bridge supports prefetchable range, size it
483
                   separately. If it doesn't, or its prefetchable window
484
                   has already been allocated by arch code, try
485
                   non-prefetchable range for both types of PCI memory
486
                   resources. */
487
                mask = IORESOURCE_MEM;
488
                prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
489
                if (pbus_size_mem(bus, prefmask, prefmask))
490
                        mask = prefmask; /* Success, size non-prefetch only. */
491
                pbus_size_mem(bus, mask, IORESOURCE_MEM);
492
                break;
493
        }
494
}
495
EXPORT_SYMBOL(pci_bus_size_bridges);
496
 
497
void __devinit
498
pci_bus_assign_resources(struct pci_bus *bus)
499
{
500
        struct pci_bus *b;
501
        int found_vga = pbus_assign_resources_sorted(bus);
502
        struct pci_dev *dev;
503
 
504
        if (found_vga) {
505
                /* Propagate presence of the VGA to upstream bridges */
506
                for (b = bus; b->parent; b = b->parent) {
507
                        b->resource[0]->flags |= IORESOURCE_BUS_HAS_VGA;
508
                }
509
        }
510
        list_for_each_entry(dev, &bus->devices, bus_list) {
511
                b = dev->subordinate;
512
                if (!b)
513
                        continue;
514
 
515
                pci_bus_assign_resources(b);
516
 
517
                switch (dev->class >> 8) {
518
                case PCI_CLASS_BRIDGE_PCI:
519
                        pci_setup_bridge(b);
520
                        break;
521
 
522
                case PCI_CLASS_BRIDGE_CARDBUS:
523
                        pci_setup_cardbus(b);
524
                        break;
525
 
526
                default:
527
                        printk(KERN_INFO "PCI: not setting up bridge %s "
528
                               "for bus %d\n", pci_name(dev), b->number);
529
                        break;
530
                }
531
        }
532
}
533
EXPORT_SYMBOL(pci_bus_assign_resources);
534
 
535
void __init
536
pci_assign_unassigned_resources(void)
537
{
538
        struct list_head *ln;
539
 
540
        /* Depth first, calculate sizes and alignments of all
541
           subordinate buses. */
542
        for(ln=pci_root_buses.next; ln != &pci_root_buses; ln=ln->next)
543
                pci_bus_size_bridges(pci_bus_b(ln));
544
        /* Depth last, allocate resources and update the hardware. */
545
        for(ln=pci_root_buses.next; ln != &pci_root_buses; ln=ln->next) {
546
                pci_bus_assign_resources(pci_bus_b(ln));
547
                pci_enable_bridges(pci_bus_b(ln));
548
        }
549
}