- Fix poor resource allocation estimate.
[coreboot.git] / src / devices / device.c
1 /*
2  *      (c) 1999--2000 Martin Mares <mj@suse.cz>
3  *      (c) 2003 Eric Biederman <ebiederm@xmission.com>
4  */
5 /* lots of mods by ron minnich (rminnich@lanl.gov), with 
6  * the final architecture guidance from Tom Merritt (tjm@codegen.com)
7  * In particular, we changed from the one-pass original version to 
8  * Tom's recommended multiple-pass version. I wasn't sure about doing 
9  * it with multiple passes, until I actually started doing it and saw
10  * the wisdom of Tom's recommendations ...
11  *
12  * Lots of cleanups by Eric Biederman to handle bridges, and to
13  * handle resource allocation for non-pci devices.
14  */
15
16 #include <console/console.h>
17 #include <bitops.h>
18 #include <arch/io.h>
19 #include <device/device.h>
20 #include <device/pci.h>
21
22 /**
23  * This is the root of the device tree. A PCI tree always has 
24  * one bus, bus 0. Bus 0 contains devices and bridges. 
25  */
26 struct device dev_root;
27 /* Linked list of ALL devices */
28 struct device *all_devices = 0;
29 /* pointer to the last device */
30 static struct device **last_dev_p = &all_devices;
31
32 #define DEVICE_MEM_HIGH  0xFEC00000UL /* Reserve 20M for the system */
33 #define DEVICE_IO_START 0x1000
34
35
36 /* Append a new device to the global device chain.
37  * The chain is used to find devices once everything is set up.
38  */
39 void append_device(struct device *dev)
40 {
41         *last_dev_p = dev;
42         last_dev_p = &dev->next;
43 }
44
45
46 /** round a number to an alignment. 
47  * @param val the starting value
48  * @param roundup Alignment as a power of two
49  * @returns rounded up number
50  */
51 static unsigned long round(unsigned long val, unsigned long roundup)
52 {
53         /* ROUNDUP MUST BE A POWER OF TWO. */
54         unsigned long inverse;
55         inverse = ~(roundup - 1);
56         val += (roundup - 1);
57         val &= inverse;
58         return val;
59 }
60
61 static unsigned long round_down(unsigned long val, unsigned long round_down)
62 {
63         /* ROUND_DOWN MUST BE A POWER OF TWO. */
64         unsigned long inverse;
65         inverse = ~(round_down - 1);
66         val &= inverse;
67         return val;
68 }
69
70
71 /** Read the resources on all devices of a given bus.
72  * @param bus bus to read the resources on.
73  */
74 static void read_resources(struct device *bus)
75 {
76         struct device *curdev;
77
78         
79         /* Walk through all of the devices and find which resources they need. */
80         for(curdev = bus->children; curdev; curdev = curdev->sibling) {
81                 if (curdev->resources > 0) {
82                         continue;
83                 }
84                 curdev->ops->read_resources(curdev);
85         }
86 }
87
88 static struct device *largest_resource(struct device *bus, struct resource **result_res,
89         unsigned long type_mask, unsigned long type)
90 {
91         struct device *curdev;
92         struct device *result_dev = 0;
93         struct resource *last = *result_res;
94         struct resource *result = 0;
95         int seen_last = 0;
96         for(curdev = bus->children; curdev; curdev = curdev->sibling) {
97                 int i;
98                 for(i = 0; i < curdev->resources; i++) {
99                         struct resource *resource = &curdev->resource[i];
100                         /* If it isn't the right kind of resource ignore it */
101                         if ((resource->flags & type_mask) != type) {
102                                 continue;
103                         }
104                         /* Be certain to pick the successor to last */
105                         if (resource == last) {
106                                 seen_last = 1;
107                                 continue;
108                         }
109                         if (last && (
110                                 (last->align < resource->align) ||
111                                 ((last->align == resource->align) &&
112                                         (last->size < resource->size)) ||
113                                 ((last->align == resource->align) &&
114                                         (last->size == resource->size) &&
115                                         (!seen_last)))) {
116                                 continue;
117                         }
118                         if (!result || 
119                                 (result->align < resource->align) ||
120                                 ((result->align == resource->align) &&
121                                         (result->size < resource->size))) {
122                                 result_dev = curdev;
123                                 result = resource;
124                         }
125                 }
126         }
127         *result_res = result;
128         return result_dev;
129 }
130
131 /* Compute allocate resources is the guts of the resource allocator.
132  * 
133  * The problem.
134  *  - Allocate resources locations for every device.
135  *  - Don't overlap, and follow the rules of bridges.
136  *  - Don't overlap with resources in fixed locations.
137  *  - Be efficient so we don't have ugly strategies.
138  *
139  * The strategy.
140  * - Devices that have fixed addresses are the minority so don't
141  *   worry about them too much.  Instead only use part of the address
142  *   space for devices with programmable addresses.  This easily handles
143  *   everything except bridges.
144  *
145  * - PCI devices are required to have thier sizes and their alignments
146  *   equal.  In this case an optimal solution to the packing problem
147  *   exists.  Allocate all devices from highest alignment to least
148  *   alignment or vice versa.  Use this.
149  *
150  * - So we can handle more than PCI run two allocation passes on
151  *   bridges.  The first to see how large the resources are behind
152  *   the bridge, and what their alignment requirements are.  The
153  *   second to assign a safe address to the devices behind the
154  *   bridge.  This allows me to treat a bridge as just a device with 
155  *   a couple of resources, and not need to special case it in the
156  *   allocator.  Also this allows handling of other types of bridges.
157  *
158  */
159
160 void compute_allocate_resource(
161         struct device *bus,
162         struct resource *bridge,
163         unsigned long type_mask,
164         unsigned long type)
165 {
166         struct device *dev;
167         struct resource *resource;
168         unsigned long base;
169         unsigned long align, min_align;
170         min_align = 0;
171         base = bridge->base;
172
173         /* We want different minimum alignments for different kinds of
174          * resources.  These minimums are not device type specific
175          * but resource type specific.
176          */
177         if (bridge->flags & IORESOURCE_IO) {
178                 min_align = log2(DEVICE_IO_ALIGN);
179         }
180         if (bridge->flags & IORESOURCE_MEM) {
181                 min_align = log2(DEVICE_MEM_ALIGN);
182         }
183
184         printk_spew("DEV: %02x:%02x.%01x compute_allocate_%s: base: %08lx size: %08lx align: %d gran: %d\n", 
185                 bus->bus->secondary,
186                 PCI_SLOT(bus->devfn), PCI_FUNC(bus->devfn),
187                 (bridge->flags & IORESOURCE_IO)? "io":
188                 (bridge->flags & IORESOURCE_PREFETCH)? "prefmem" : "mem",
189                 base, bridge->size, bridge->align, bridge->gran);
190
191         /* Make certain I have read in all of the resources */
192         read_resources(bus);
193
194         /* Remember I haven't found anything yet. */
195         resource = 0;
196
197         /* Walk through all the devices on the current bus and compute the addresses */
198         while((dev = largest_resource(bus, &resource, type_mask, type))) {
199                 unsigned long size;
200                 /* Do NOT I repeat do not ignore resources which have zero size.
201                  * If they need to be ignored dev->read_resources should not even
202                  * return them.   Some resources must be set even when they have
203                  * no size.  PCI bridge resources are a good example of this.
204                  */
205
206                 /* Propogate the resource alignment to the bridge register  */
207                 if (resource->align > bridge->align) {
208                         bridge->align = resource->align;
209                 }
210
211                 /* Make certain we are dealing with a good minimum size */
212                 size = resource->size;
213                 align = resource->align;
214                 if (align < min_align) {
215                         align = min_align;
216                 }
217                 if (resource->flags & IORESOURCE_IO) {
218                         /* Don't allow potential aliases over the
219                          * legacy pci expansion card addresses.
220                          * The legacy pci decodes only 10 bits,
221                          * uses 100h - 3ffh. Therefor, only 0 - ff
222                          * can be used out of each 400h block of io
223                          * space.
224                          */
225                         if ((base & 0x300) != 0) {
226                                 base = (base & ~0x3ff) + 0x400;
227                         }
228                         /* Don't allow allocations in the VGA IO range.
229                          * PCI has special cases for that.
230                          */
231                         else if ((base >= 0x3b0) && (base <= 0x3df)) {
232                                 base = 0x3e0;
233                         }
234                 }
235                 if (((round(base, 1UL << align) + size) -1) <= resource->limit) {
236                         /* base must be aligned to size */
237                         base = round(base, 1UL << align);
238                         resource->base = base;
239                         resource->flags |= IORESOURCE_SET;
240                         base += size;
241                         
242                         printk_spew(
243                                 "DEV: %02x:%02x.%01x %02x *  [0x%08lx - 0x%08lx] %s\n",
244                                 dev->bus->secondary, 
245                                 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), 
246                                 resource->index, 
247                                 resource->base, resource->base + resource->size -1,
248                                 (resource->flags & IORESOURCE_IO)? "io":
249                                 (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem");
250                 }
251
252         }
253         /* A pci bridge resource does not need to be a power
254          * of two size, but it does have a minimum granularity.
255          * Round the size up to that minimum granularity so we
256          * know not to place something else at an address postitively
257          * decoded by the bridge.
258          */
259         bridge->size = round(base, 1UL << bridge->gran) - bridge->base;
260
261         printk_spew("DEV: %02x:%02x.%01x compute_allocate_%s: base: %08lx size: %08lx align: %d gran: %d done\n", 
262                 bus->bus->secondary,
263                 PCI_SLOT(bus->devfn), PCI_FUNC(bus->devfn),
264                 (bridge->flags & IORESOURCE_IO)? "io":
265                 (bridge->flags & IORESOURCE_PREFETCH)? "prefmem" : "mem",
266                 base, bridge->size, bridge->align, bridge->gran);
267
268
269 }
270
271 static void allocate_vga_resource(void)
272 {
273         /* FIXME handle the VGA pallette snooping */
274         struct device *dev, *vga, *bus;
275         bus = vga = 0;
276         for(dev = all_devices; dev; dev = dev->next) {
277                 uint32_t class_revision;
278                 class_revision = pci_read_config32(dev, PCI_CLASS_REVISION);
279                 if (((class_revision >> 24) == 0x03) && 
280                     ((class_revision >> 16) != 0x380)) {
281                         if (!vga) {
282                                 printk_debug("Allocating VGA resource\n");
283                                 vga = dev;
284                         }
285                         if (vga == dev) {
286                                 /* All legacy VGA cards have MEM & I/O space registers */
287                                 dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
288                         } else {
289                                 /* It isn't safe to enable other VGA cards */
290                                 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
291                         }
292                 }
293         }
294         if (vga) {
295                 bus = vga->bus;
296         }
297         /* Now walk up the bridges setting the VGA enable */
298         while(bus) {
299                 uint16_t ctrl;
300                 ctrl = pci_read_config16(bus, PCI_BRIDGE_CONTROL);
301                 ctrl |= PCI_BRIDGE_CTL_VGA;
302                 pci_write_config16(bus, PCI_BRIDGE_CONTROL, ctrl);
303                 bus = (bus == bus->bus)? 0 : bus->bus;
304         } 
305 }
306
307
308 /** Assign the computed resources to the bridges and devices on the bus.
309  * Recurse to any bridges found on this bus first. Then do the devices
310  * on this bus. 
311  * @param bus Pointer to the structure for this bus
312  */ 
313 void assign_resources(struct device *bus)
314 {
315         struct device *curdev;
316
317         printk_debug("ASSIGN RESOURCES, bus %d\n", bus->secondary);
318
319         for (curdev = bus->children; curdev; curdev = curdev->sibling) {
320                 curdev->ops->set_resources(curdev);
321         }
322         printk_debug("ASSIGNED RESOURCES, bus %d\n", bus->secondary);
323 }
324
325 static void enable_resources(struct device *bus)
326 {
327         struct device *curdev;
328
329         /* Walk through the chain of all pci devices and enable them.
330          * This is effectively a breadth first traversal so we should
331          * not have enalbing ordering problems.
332          */
333         for (curdev = all_devices; curdev; curdev = curdev->next) {
334                 uint16_t command;
335                 command = pci_read_config16(curdev, PCI_COMMAND);
336                 command |= curdev->command;
337                 printk_debug("DEV: %02x:%02x.%01x cmd <- %02x\n",
338                         curdev->bus->secondary,
339                         PCI_SLOT(curdev->devfn), PCI_FUNC(curdev->devfn),
340                         command);
341                 pci_write_config16(curdev, PCI_COMMAND, command);
342         }
343 }
344
345 /** Enumerate the resources on the PCI by calling pci_init
346  */
347 void dev_enumerate(void)
348 {
349         struct device *root;
350         printk_info("Enumerating buses...");
351         root = &dev_root;
352         if (!root->ops) {
353                 root->ops = &default_pci_ops_root;
354         }
355         root->subordinate = root->ops->scan_bus(root, 0);
356         printk_info("done\n");
357 }
358
359 /** Starting at the root, compute what resources are needed and allocate them. 
360  * I/O starts at PCI_IO_START. Since the assignment is hierarchical we
361  * set the values into the dev_root struct. 
362  */
363 void dev_configure(void)
364 {
365         struct device *root = &dev_root;
366         printk_info("Allocating resources...");
367         printk_debug("\n");
368
369
370         root->ops->read_resources(root);
371
372         /* Make certain the io devices are allocated somewhere
373          * safe.
374          */
375         root->resource[0].base = DEVICE_IO_START;
376         root->resource[0].flags |= IORESOURCE_SET;
377         /* Now reallocate the pci resources memory with the
378          * highest addresses I can manage.
379          */
380         root->resource[1].base = 
381                 round_down(DEVICE_MEM_HIGH - root->resource[1].size,
382                         1UL << root->resource[1].align);
383         root->resource[1].flags |= IORESOURCE_SET;
384         // now just set things into registers ... we hope ...
385         root->ops->set_resources(root);
386
387         allocate_vga_resource();
388
389         printk_info("done.\n");
390 }
391
392 /** Starting at the root, walk the tree and enable all devices/bridges. 
393  * What really happens is computed COMMAND bits get set in register 4
394  */
395 void dev_enable(void)
396 {
397         printk_info("Enabling resourcess...");
398
399         /* now enable everything. */
400         enable_resources(&dev_root);
401         printk_info("done.\n");
402 }
403
404 /** Starting at the root, walk the tree and call a driver to
405  *  do device specific setup.
406  */
407 void dev_initialize(void)
408 {
409         struct device *dev;
410
411         printk_info("Initializing devices...\n");
412         for (dev = all_devices; dev; dev = dev->next) {
413                 if (dev->ops->init) {
414                         printk_debug("PCI: %02x:%02x.%01x init\n",
415                                 dev->bus->secondary,
416                                 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
417                         dev->ops->init(dev);
418                 }
419         }
420         printk_info("Devices initialized\n");
421 }
422
423