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