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>
22 #include <device/pci_ids.h>
25 #include <smp/spinlock.h>
27 /** Linked list of ALL devices */
28 struct device *all_devices = &dev_root;
29 /** Pointer to the last device */
30 extern struct device **last_dev_p;
32 /** The upper limit of MEM resource of the devices.
33 * Reserve 20M for the system */
34 #define DEVICE_MEM_HIGH 0xFEBFFFFFUL
35 /** The lower limit of IO resource of the devices.
36 * Reserve 4k for ISA/Legacy devices */
37 #define DEVICE_IO_START 0x1000
40 * @brief Allocate a new device structure.
42 * Allocte a new device structure and attached it to the device tree as a
43 * child of the parent bus.
45 * @param parent parent bus the newly created device attached to.
46 * @param path path to the device to be created.
48 * @return pointer to the newly created device structure.
52 static spinlock_t dev_lock = SPIN_LOCK_UNLOCKED;
53 device_t alloc_dev(struct bus *parent, struct device_path *path)
59 /* Find the last child of our parent */
60 for(child = parent->children; child && child->sibling; ) {
61 child = child->sibling;
63 dev = malloc(sizeof(*dev));
65 die("DEV: out of memory.\n");
67 memset(dev, 0, sizeof(*dev));
68 memcpy(&dev->path, path, sizeof(*path));
71 /* Initialize the back pointers in the link fields */
72 for(link = 0; link < MAX_LINKS; link++) {
73 dev->link[link].dev = dev;
74 dev->link[link].link = link;
77 /* By default devices are enabled */
80 /* Add the new device to the list of children of the bus. */
85 parent->children = dev;
88 /* Append a new device to the global device list.
89 * The list is used to find devices once everything is set up.
92 last_dev_p = &dev->next;
94 spin_unlock(&dev_lock);
99 * @brief round a number up to an alignment.
100 * @param val the starting value
101 * @param roundup Alignment as a power of two
102 * @returns rounded up number
104 static resource_t round(resource_t val, unsigned long pow)
107 mask = (1ULL << pow) - 1ULL;
113 /** Read the resources on all devices of a given bus.
114 * @param bus bus to read the resources on.
116 static void read_resources(struct bus *bus)
118 struct device *curdev;
120 printk_spew("%s read_resources bus %d link: %d\n",
121 dev_path(bus->dev), bus->secondary, bus->link);
123 /* Walk through all of the devices and find which resources they need. */
124 for(curdev = bus->children; curdev; curdev = curdev->sibling) {
127 if (curdev->have_resources) {
130 if (!curdev->enabled) {
133 if (!curdev->ops || !curdev->ops->read_resources) {
134 printk_err("%s missing read_resources\n",
138 curdev->ops->read_resources(curdev);
139 curdev->have_resources = 1;
140 /* Read in subtractive resources behind the current device */
142 for(i = 0; i < curdev->resources; i++) {
143 struct resource *resource;
145 resource = &curdev->resource[i];
146 if (!(resource->flags & IORESOURCE_SUBTRACTIVE))
148 link = IOINDEX_SUBTRACTIVE_LINK(resource->index);
149 if (link > MAX_LINKS) {
150 printk_err("%s subtractive index on link: %d\n",
151 dev_path(curdev), link);
154 if (!(links & (1 << link))) {
155 links |= (1 << link);
156 read_resources(&curdev->link[resource->index]);
161 printk_spew("%s read_resources bus %d link: %d done\n",
162 dev_path(bus->dev), bus->secondary, bus->link);
165 struct pick_largest_state {
166 struct resource *last;
167 struct device *result_dev;
168 struct resource *result;
172 static void pick_largest_resource(void *gp,
173 struct device *dev, struct resource *resource)
175 struct pick_largest_state *state = gp;
176 struct resource *last;
178 /* Be certain to pick the successor to last */
179 if (resource == last) {
180 state->seen_last = 1;
184 (last->align < resource->align) ||
185 ((last->align == resource->align) &&
186 (last->size < resource->size)) ||
187 ((last->align == resource->align) &&
188 (last->size == resource->size) &&
189 (!state->seen_last)))) {
192 if (!state->result ||
193 (state->result->align < resource->align) ||
194 ((state->result->align == resource->align) &&
195 (state->result->size < resource->size))) {
196 state->result_dev = dev;
197 state->result = resource;
201 static struct device *largest_resource(struct bus *bus, struct resource **result_res,
202 unsigned long type_mask, unsigned long type)
204 struct pick_largest_state state;
206 state.last = *result_res;
207 state.result_dev = 0;
211 search_bus_resources(bus, type_mask, type, pick_largest_resource, &state);
213 *result_res = state.result;
214 return state.result_dev;
217 /* Compute allocate resources is the guts of the resource allocator.
220 * - Allocate resources locations for every device.
221 * - Don't overlap, and follow the rules of bridges.
222 * - Don't overlap with resources in fixed locations.
223 * - Be efficient so we don't have ugly strategies.
226 * - Devices that have fixed addresses are the minority so don't
227 * worry about them too much. Instead only use part of the address
228 * space for devices with programmable addresses. This easily handles
229 * everything except bridges.
231 * - PCI devices are required to have thier sizes and their alignments
232 * equal. In this case an optimal solution to the packing problem
233 * exists. Allocate all devices from highest alignment to least
234 * alignment or vice versa. Use this.
236 * - So we can handle more than PCI run two allocation passes on
237 * bridges. The first to see how large the resources are behind
238 * the bridge, and what their alignment requirements are. The
239 * second to assign a safe address to the devices behind the
240 * bridge. This allows me to treat a bridge as just a device with
241 * a couple of resources, and not need to special case it in the
242 * allocator. Also this allows handling of other types of bridges.
246 void compute_allocate_resource(
248 struct resource *bridge,
249 unsigned long type_mask,
253 struct resource *resource;
255 unsigned long align, min_align;
259 printk_spew("%s compute_allocate_%s: base: %08Lx size: %08Lx align: %d gran: %d\n",
261 (bridge->flags & IORESOURCE_IO)? "io":
262 (bridge->flags & IORESOURCE_PREFETCH)? "prefmem" : "mem",
263 base, bridge->size, bridge->align, bridge->gran);
266 /* We want different minimum alignments for different kinds of
267 * resources. These minimums are not device type specific
268 * but resource type specific.
270 if (bridge->flags & IORESOURCE_IO) {
271 min_align = log2(DEVICE_IO_ALIGN);
273 if (bridge->flags & IORESOURCE_MEM) {
274 min_align = log2(DEVICE_MEM_ALIGN);
277 /* Make certain I have read in all of the resources */
280 /* Remember I haven't found anything yet. */
283 /* Walk through all the devices on the current bus and
284 * compute the addresses.
286 while((dev = largest_resource(bus, &resource, type_mask, type))) {
288 /* Do NOT I repeat do not ignore resources which have zero size.
289 * If they need to be ignored dev->read_resources should not even
290 * return them. Some resources must be set even when they have
291 * no size. PCI bridge resources are a good example of this.
294 /* Propogate the resource alignment to the bridge register */
295 if (resource->align > bridge->align) {
296 bridge->align = resource->align;
299 /* Make certain we are dealing with a good minimum size */
300 size = resource->size;
301 align = resource->align;
302 if (align < min_align) {
305 if (resource->flags & IORESOURCE_FIXED) {
308 /* Propogate the resource limit to the bridge register */
309 if (bridge->limit > resource->limit) {
310 bridge->limit = resource->limit;
312 /* Artificially deny limits between DEVICE_MEM_HIGH and 0xffffffff */
313 if ((bridge->limit > DEVICE_MEM_HIGH) && (bridge->limit <= 0xffffffff)) {
314 bridge->limit = DEVICE_MEM_HIGH;
316 if (resource->flags & IORESOURCE_IO) {
317 /* Don't allow potential aliases over the
318 * legacy pci expansion card addresses.
319 * The legacy pci decodes only 10 bits,
320 * uses 100h - 3ffh. Therefor, only 0 - ff
321 * can be used out of each 400h block of io
324 if ((base & 0x300) != 0) {
325 base = (base & ~0x3ff) + 0x400;
327 /* Don't allow allocations in the VGA IO range.
328 * PCI has special cases for that.
330 else if ((base >= 0x3b0) && (base <= 0x3df)) {
334 if (((round(base, align) + size) -1) <= resource->limit) {
335 /* base must be aligned to size */
336 base = round(base, align);
337 resource->base = base;
338 resource->flags |= IORESOURCE_ASSIGNED;
339 resource->flags &= ~IORESOURCE_STORED;
343 "%s %02x * [0x%08Lx - 0x%08Lx] %s\n",
347 resource->base + resource->size - 1,
348 (resource->flags & IORESOURCE_IO)? "io":
349 (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem");
352 /* A pci bridge resource does not need to be a power
353 * of two size, but it does have a minimum granularity.
354 * Round the size up to that minimum granularity so we
355 * know not to place something else at an address postitively
356 * decoded by the bridge.
358 bridge->size = round(base, bridge->gran) - bridge->base;
360 printk_spew("%s compute_allocate_%s: base: %08Lx size: %08Lx align: %d gran: %d done\n",
362 (bridge->flags & IORESOURCE_IO)? "io":
363 (bridge->flags & IORESOURCE_PREFETCH)? "prefmem" : "mem",
364 base, bridge->size, bridge->align, bridge->gran);
369 static void allocate_vga_resource(void)
371 #warning "FIXME modify allocate_vga_resource so it is less pci centric!"
372 #warning "This function knows to much about PCI stuff, it should be just a ietrator/visitor."
374 /* FIXME handle the VGA pallette snooping */
375 struct device *dev, *vga;
379 for (dev = all_devices; dev; dev = dev->next) {
380 if (((dev->class >> 16) == PCI_BASE_CLASS_DISPLAY) &&
381 ((dev->class >> 8) != PCI_CLASS_DISPLAY_OTHER)) {
383 printk_debug("Allocating VGA resource %s\n",
385 printk_debug("parent of the vga device %s\n",
386 dev_path(dev->bus->dev));
390 /* All legacy VGA cards have MEM & I/O space registers */
391 dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
393 /* It isn't safe to enable other VGA cards */
394 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
401 /* Now walk up the bridges setting the VGA enable */
403 printk_info("Enabling VGA forward on bus connect to %s\n",
405 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA;
406 bus = (bus == bus->dev->bus)? 0 : bus->dev->bus;
412 * @brief Assign the computed resources to the devices on the bus.
414 * @param bus Pointer to the structure for this bus
416 * Use the device specific set_resources method to store the computed
417 * resources to hardware. For bridge devices, the set_resources() method
418 * has to recurse into every down stream buses.
421 * assign_resources() -> device_operation::set_resources()
422 * device_operation::set_resources() -> assign_resources()
424 void assign_resources(struct bus *bus)
426 struct device *curdev;
428 printk_spew("%s assign_resources, bus %d link: %d\n",
429 dev_path(bus->dev), bus->secondary, bus->link);
431 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
432 if (!curdev->enabled || !curdev->resources) {
435 if (!curdev->ops || !curdev->ops->set_resources) {
436 printk_err("%s missing set_resources\n",
440 curdev->ops->set_resources(curdev);
442 printk_spew("%s assign_resources, bus %d link: %d\n",
443 dev_path(bus->dev), bus->secondary, bus->link);
447 * @brief Enable the resources for a specific device
449 * @param dev the device whose resources are to be enabled
451 * Enable resources of the device by calling the device specific
452 * enable_resources() method.
454 * The parent's resources should be enabled first to avoid having enabling
455 * order problem. This is done by calling the parent's enable_resources()
456 * method and let that method to call it's children's enable_resoruces()
457 * method via the (global) enable_childrens_resources().
459 * Indirect mutual recursion:
460 * enable_resources() -> device_operations::enable_resource()
461 * device_operations::enable_resource() -> enable_children_resources()
462 * enable_children_resources() -> enable_resources()
464 void enable_resources(struct device *dev)
469 if (!dev->ops || !dev->ops->enable_resources) {
470 printk_err("%s missing enable_resources\n", dev_path(dev));
473 dev->ops->enable_resources(dev);
477 * @brief Determine the existence of devices and extend the device tree.
479 * Most of the devices in the system are listed in the mainboard Config.lb
480 * file. The device structures for these devices are generated at compile
481 * time by the config tool and are organized into the device tree. This
482 * function determines if the devices created at compile time actually exist
483 * in the physical system.
485 * For devices in the physical system but not listed in the Config.lb file,
486 * the device structures have to be created at run time and attached to the
489 * This function starts from the root device 'dev_root', scan the buses in
490 * the system recursively, modify the device tree according to the result of
493 * This function has no idea how to scan and probe buses and devices at all.
494 * It depends on the bus/device specific scan_bus() method to do it. The
495 * scan_bus() method also has to create the device structure and attach
496 * it to the device tree.
498 void dev_enumerate(void)
501 unsigned subordinate;
502 printk_info("Enumerating buses...\n");
504 if (root->chip_ops && root->chip_ops->enable_dev) {
505 root->chip_ops->enable_dev(root);
507 if (!root->ops || !root->ops->scan_bus) {
508 printk_err("dev_root missing scan_bus operation");
511 subordinate = root->ops->scan_bus(root, 0);
512 printk_info("done\n");
516 * @brief Configure devices on the devices tree.
518 * Starting at the root of the device tree, travel it recursively in two
519 * passes. In the first pass, we compute and allocate resources (ranges)
520 * requried by each device. In the second pass, the resources ranges are
521 * relocated to their final position and stored to the hardware.
523 * I/O resources start at DEVICE_IO_START and grow upward. MEM resources start
524 * at DEVICE_MEM_START and grow downward.
526 * Since the assignment is hierarchical we set the values into the dev_root
529 void dev_configure(void)
531 struct resource *io, *mem;
534 printk_info("Allocating resources...\n");
537 if (!root->ops || !root->ops->read_resources) {
538 printk_err("dev_root missing read_resources\n");
541 if (!root->ops || !root->ops->set_resources) {
542 printk_err("dev_root missing set_resources\n");
546 printk_info("Reading resources...\n");
547 root->ops->read_resources(root);
548 printk_info("Done\n");
550 /* Get the resources */
551 io = &root->resource[0];
552 mem = &root->resource[1];
553 /* Make certain the io devices are allocated somewhere safe. */
554 io->base = DEVICE_IO_START;
555 io->flags |= IORESOURCE_ASSIGNED;
556 io->flags &= ~IORESOURCE_STORED;
557 /* Now reallocate the pci resources memory with the
558 * highest addresses I can manage.
560 mem->base = resource_max(&root->resource[1]);
561 mem->flags |= IORESOURCE_ASSIGNED;
562 mem->flags &= ~IORESOURCE_STORED;
564 /* Allocate the VGA I/O resource.. */
565 allocate_vga_resource();
567 /* Store the computed resource allocations into device registers ... */
568 printk_info("Setting resources...\n");
569 root->ops->set_resources(root);
570 printk_info("Done\n");
572 mem->flags |= IORESOURCE_STORED;
573 report_resource_stored(root, mem, "");
576 printk_info("done.\n");
580 * @brief Enable devices on the device tree.
582 * Starting at the root, walk the tree and enable all devices/bridges by
583 * calling the device's enable_resources() method.
585 void dev_enable(void)
587 printk_info("Enabling resourcess...\n");
589 /* now enable everything. */
590 enable_resources(&dev_root);
592 printk_info("done.\n");
596 * @brief Initialize all devices in the global device list.
598 * Starting at the first device on the global device link list,
599 * walk the list and call the device's init() method to do deivce
602 void dev_initialize(void)
606 printk_info("Initializing devices...\n");
607 for (dev = all_devices; dev; dev = dev->next) {
608 if (dev->enabled && !dev->initialized &&
609 dev->ops && dev->ops->init)
611 printk_debug("%s init\n", dev_path(dev));
612 dev->initialized = 1;
616 printk_info("Devices initialized\n");