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