2 * (c) 1999--2000 Martin Mares <mj@suse.cz>
3 * (c) 2003 Eric Biederman <ebiederm@xmission.com>
4 * (c) 2003 Linux Networx
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 ...
13 * Lots of cleanups by Eric Biederman to handle bridges, and to
14 * handle resource allocation for non-pci devices.
17 #include <console/console.h>
20 #include <device/device.h>
21 #include <device/pci.h>
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;
30 #define DEVICE_MEM_HIGH 0xFEC00000UL /* Reserve 20M for the system */
31 #define DEVICE_IO_START 0x1000
33 /** Allocate a new device structure
35 device_t alloc_dev(struct bus *parent, struct device_path *path)
39 /* Find the last child of our parent */
40 for(child = parent->children; child && child->sibling; ) {
41 child = child->sibling;
43 dev = malloc(sizeof(*dev));
45 die("DEV: out of memory.\n");
47 memset(dev, 0, sizeof(*dev));
48 memcpy(&dev->path, path, sizeof(*path));
50 /* Append a new device to the global device chain.
51 * The chain is used to find devices once everything is set up.
54 last_dev_p = &dev->next;
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;
62 /* Add the new device to the children of the bus. */
67 parent->children = dev;
69 /* If we don't have any other information about a device enable it */
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
79 static unsigned long round(unsigned long val, unsigned long roundup)
81 /* ROUNDUP MUST BE A POWER OF TWO. */
82 unsigned long inverse;
83 inverse = ~(roundup - 1);
89 static unsigned long round_down(unsigned long val, unsigned long round_down)
91 /* ROUND_DOWN MUST BE A POWER OF TWO. */
92 unsigned long inverse;
93 inverse = ~(round_down - 1);
99 /** Read the resources on all devices of a given bus.
100 * @param bus bus to read the resources on.
102 static void read_resources(struct bus *bus)
104 struct device *curdev;
106 /* Walk through all of the devices and find which resources they need. */
107 for(curdev = bus->children; curdev; curdev = curdev->sibling) {
110 if (curdev->resources > 0) {
113 if (!curdev->ops || !curdev->ops->read_resources) {
114 printk_err("%s missing read_resources\n",
118 if (!curdev->enable) {
121 curdev->ops->read_resources(curdev);
122 /* Read in subtractive resources behind the current device */
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))))
130 links |= (1 << resource->index);
131 read_resources(&curdev->link[resource->index]);
138 struct pick_largest_state {
139 struct resource *last;
140 struct device *result_dev;
141 struct resource *result;
145 static void pick_largest_resource(
146 struct pick_largest_state *state, struct device *dev, struct resource *resource)
148 struct resource *last;
150 /* Be certain to pick the successor to last */
151 if (resource == last) {
152 state->seen_last = 1;
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)))) {
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;
174 static void find_largest_resource(struct pick_largest_state *state,
175 struct bus *bus, unsigned long type_mask, unsigned long type)
177 struct device *curdev;
178 for(curdev = bus->children; curdev; curdev = curdev->sibling) {
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) {
186 /* If it is a subtractive resource recurse */
187 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
189 subbus = &curdev->link[resource->index];
190 find_largest_resource(state, subbus, type_mask, type);
193 /* See if this is the largest resource */
194 pick_largest_resource(state, curdev, resource);
199 static struct device *largest_resource(struct bus *bus, struct resource **result_res,
200 unsigned long type_mask, unsigned long type)
202 struct pick_largest_state state;
204 state.last = *result_res;
205 state.result_dev = 0;
209 find_largest_resource(&state, bus, type_mask, type);
211 *result_res = state.result;
212 return state.result_dev;
215 /* Compute allocate resources is the guts of the resource allocator.
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.
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.
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.
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.
244 void compute_allocate_resource(
246 struct resource *bridge,
247 unsigned long type_mask,
251 struct resource *resource;
253 unsigned long align, min_align;
257 printk_spew("%s compute_allocate_%s: base: %08lx size: %08lx align: %d gran: %d\n",
259 (bridge->flags & IORESOURCE_IO)? "io":
260 (bridge->flags & IORESOURCE_PREFETCH)? "prefmem" : "mem",
261 base, bridge->size, bridge->align, bridge->gran);
264 /* We want different minimum alignments for different kinds of
265 * resources. These minimums are not device type specific
266 * but resource type specific.
268 if (bridge->flags & IORESOURCE_IO) {
269 min_align = log2(DEVICE_IO_ALIGN);
271 if (bridge->flags & IORESOURCE_MEM) {
272 min_align = log2(DEVICE_MEM_ALIGN);
275 /* Make certain I have read in all of the resources */
278 /* Remember I haven't found anything yet. */
281 /* Walk through all the devices on the current bus and compute the addresses */
282 while((dev = largest_resource(bus, &resource, type_mask, type))) {
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.
290 /* Propogate the resource alignment to the bridge register */
291 if (resource->align > bridge->align) {
292 bridge->align = resource->align;
295 /* Make certain we are dealing with a good minimum size */
296 size = resource->size;
297 align = resource->align;
298 if (align < min_align) {
301 if (resource->flags & IORESOURCE_FIXED) {
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
312 if ((base & 0x300) != 0) {
313 base = (base & ~0x3ff) + 0x400;
315 /* Don't allow allocations in the VGA IO range.
316 * PCI has special cases for that.
318 else if ((base >= 0x3b0) && (base <= 0x3df)) {
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;
330 "%s %02x * [0x%08lx - 0x%08lx] %s\n",
333 resource->base, resource->base + resource->size -1,
334 (resource->flags & IORESOURCE_IO)? "io":
335 (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem");
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.
345 bridge->size = round(base, 1UL << bridge->gran) - bridge->base;
347 printk_spew("%s compute_allocate_%s: base: %08lx size: %08lx align: %d gran: %d done\n",
349 (bridge->flags & IORESOURCE_IO)? "io":
350 (bridge->flags & IORESOURCE_PREFETCH)? "prefmem" : "mem",
351 base, bridge->size, bridge->align, bridge->gran);
356 static void allocate_vga_resource(void)
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;
364 for(dev = all_devices; dev; dev = dev->next) {
365 if (((dev->class >> 16) == 0x03) &&
366 ((dev->class >> 8) != 0x380)) {
368 printk_debug("Allocating VGA resource\n");
372 /* All legacy VGA cards have MEM & I/O space registers */
373 dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
375 /* It isn't safe to enable other VGA cards */
376 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
383 /* Now walk up the bridges setting the VGA enable */
385 bus->bridge_ctrl |= PCI_BRIDGE_CONTROL;
386 bus = (bus == bus->dev->bus)? 0 : bus->dev->bus;
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
394 * @param bus Pointer to the structure for this bus
396 void assign_resources(struct bus *bus)
398 struct device *curdev;
400 printk_debug("ASSIGN RESOURCES, bus %d\n", bus->secondary);
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",
408 if (!curdev->enable) {
411 curdev->ops->set_resources(curdev);
413 printk_debug("ASSIGNED RESOURCES, bus %d\n", bus->secondary);
416 void enable_resources(struct device *dev)
418 /* Enable the resources for a specific device.
419 * The parents resources should be enabled first to avoid
420 * having enabling ordering problems.
422 if (!dev->ops || !dev->ops->enable_resources) {
423 printk_err("%s missing enable_resources\n",
430 dev->ops->enable_resources(dev);
433 /** Enumerate the resources on the PCI by calling pci_init
435 void dev_enumerate(void)
438 unsigned subordinate;
439 printk_info("Enumerating buses...");
441 subordinate = root->ops->scan_bus(root, 0);
442 printk_info("done\n");
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.
449 void dev_configure(void)
451 struct device *root = &dev_root;
452 printk_info("Allocating resources...");
456 root->ops->read_resources(root);
458 /* Make certain the io devices are allocated somewhere
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.
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);
473 allocate_vga_resource();
475 printk_info("done.\n");
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
481 void dev_enable(void)
483 printk_info("Enabling resourcess...\n");
485 /* now enable everything. */
486 enable_resources(&dev_root);
487 printk_info("done.\n");
490 /** Starting at the root, walk the tree and call a driver to
491 * do device specific setup.
493 void dev_initialize(void)
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));
504 printk_info("Devices initialized\n");