2 * (c) 1999--2000 Martin Mares <mj@suse.cz>
3 * (c) 2003 Eric Biederman <ebiederm@xmission.com>
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 ...
12 * Lots of cleanups by Eric Biederman to handle bridges, and to
13 * handle resource allocation for non-pci devices.
16 #include <console/console.h>
19 #include <device/device.h>
20 #include <device/pci.h>
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.
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;
32 #define DEVICE_MEM_HIGH 0xFEC00000UL /* Reserve 20M for the system */
33 #define DEVICE_IO_START 0x1000
36 /* Append a new device to the global device chain.
37 * The chain is used to find devices once everything is set up.
39 void append_device(struct device *dev)
42 last_dev_p = &dev->next;
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
51 static unsigned long round(unsigned long val, unsigned long roundup)
53 /* ROUNDUP MUST BE A POWER OF TWO. */
54 unsigned long inverse;
55 inverse = ~(roundup - 1);
61 static unsigned long round_down(unsigned long val, unsigned long round_down)
63 /* ROUND_DOWN MUST BE A POWER OF TWO. */
64 unsigned long inverse;
65 inverse = ~(round_down - 1);
71 /** Read the resources on all devices of a given bus.
72 * @param bus bus to read the resources on.
74 static void read_resources(struct device *bus)
76 struct device *curdev;
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) {
84 curdev->ops->read_resources(curdev);
88 static struct device *largest_resource(struct device *bus, struct resource **result_res,
89 unsigned long type_mask, unsigned long type)
91 struct device *curdev;
92 struct device *result_dev = 0;
93 struct resource *last = *result_res;
94 struct resource *result = 0;
96 for(curdev = bus->children; curdev; curdev = curdev->sibling) {
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) {
104 /* Be certain to pick the successor to last */
105 if (resource == 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) &&
119 (result->align < resource->align) ||
120 ((result->align == resource->align) &&
121 (result->size < resource->size))) {
127 *result_res = result;
131 /* Compute allocate resources is the guts of the resource allocator.
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.
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.
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.
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.
160 void compute_allocate_resource(
162 struct resource *bridge,
163 unsigned long type_mask,
167 struct resource *resource;
169 unsigned long align, min_align;
173 /* We want different minimum alignments for different kinds of
174 * resources. These minimums are not device type specific
175 * but resource type specific.
177 if (bridge->flags & IORESOURCE_IO) {
178 min_align = log2(DEVICE_IO_ALIGN);
180 if (bridge->flags & IORESOURCE_MEM) {
181 min_align = log2(DEVICE_MEM_ALIGN);
184 printk_spew("DEV: %02x:%02x.%01x compute_allocate_%s: base: %08lx size: %08lx align: %d gran: %d\n",
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);
191 /* Make certain I have read in all of the resources */
194 /* Remember I haven't found anything yet. */
197 /* Walk through all the devices on the current bus and compute the addresses */
198 while((dev = largest_resource(bus, &resource, type_mask, type))) {
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.
206 /* Propogate the resource alignment to the bridge register */
207 if (resource->align > bridge->align) {
208 bridge->align = resource->align;
211 /* Make certain we are dealing with a good minimum size */
212 size = resource->size;
213 align = resource->align;
214 if (align < min_align) {
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
225 if ((base & 0x300) != 0) {
226 base = (base & ~0x3ff) + 0x400;
228 /* Don't allow allocations in the VGA IO range.
229 * PCI has special cases for that.
231 else if ((base >= 0x3b0) && (base <= 0x3df)) {
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;
243 "DEV: %02x:%02x.%01x %02x * [0x%08lx - 0x%08lx] %s\n",
245 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
247 resource->base, resource->base + resource->size -1,
248 (resource->flags & IORESOURCE_IO)? "io":
249 (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem");
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.
259 bridge->size = round(base, 1UL << bridge->gran) - bridge->base;
261 printk_spew("DEV: %02x:%02x.%01x compute_allocate_%s: base: %08lx size: %08lx align: %d gran: %d done\n",
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);
271 static void allocate_vga_resource(void)
273 /* FIXME handle the VGA pallette snooping */
274 struct device *dev, *vga, *bus;
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)) {
282 printk_debug("Allocating VGA resource\n");
286 /* All legacy VGA cards have MEM & I/O space registers */
287 dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
289 /* It isn't safe to enable other VGA cards */
290 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
297 /* Now walk up the bridges setting the VGA enable */
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;
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
311 * @param bus Pointer to the structure for this bus
313 void assign_resources(struct device *bus)
315 struct device *curdev;
317 printk_debug("ASSIGN RESOURCES, bus %d\n", bus->secondary);
319 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
320 curdev->ops->set_resources(curdev);
322 printk_debug("ASSIGNED RESOURCES, bus %d\n", bus->secondary);
325 static void enable_resources(struct device *bus)
327 struct device *curdev;
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.
333 for (curdev = all_devices; curdev; curdev = curdev->next) {
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),
341 pci_write_config16(curdev, PCI_COMMAND, command);
345 /** Enumerate the resources on the PCI by calling pci_init
347 void dev_enumerate(void)
350 printk_info("Enumerating buses...");
353 root->ops = &default_pci_ops_root;
355 root->subordinate = root->ops->scan_bus(root, 0);
356 printk_info("done\n");
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.
363 void dev_configure(void)
365 struct device *root = &dev_root;
366 printk_info("Allocating resources...");
370 root->ops->read_resources(root);
372 /* Make certain the io devices are allocated somewhere
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.
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);
387 allocate_vga_resource();
389 printk_info("done.\n");
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
395 void dev_enable(void)
397 printk_info("Enabling resourcess...");
399 /* now enable everything. */
400 enable_resources(&dev_root);
401 printk_info("done.\n");
404 /** Starting at the root, walk the tree and call a driver to
405 * do device specific setup.
407 void dev_initialize(void)
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",
416 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
420 printk_info("Devices initialized\n");