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