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.
221 if ((base > 0x3ff) && ((base & 0x300) != 0)) {
222 base = (base & ~0x3ff) + 0x400;
224 /* Don't allow allocations in the VGA IO range.
225 * PCI has special cases for that.
227 else if ((base >= 0x3b0) && (base <= 0x3df)) {
231 if (((round(base, 1UL << align) + size) -1) <= resource->limit) {
232 /* base must be aligned to size */
233 base = round(base, 1UL << align);
234 resource->base = base;
235 resource->flags |= IORESOURCE_SET;
239 "DEV: %02x:%02x.%01x %02x * [0x%08lx - 0x%08lx] %s\n",
241 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
243 resource->base, resource->base + resource->size -1,
244 (resource->flags & IORESOURCE_IO)? "io":
245 (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem");
249 /* A pci bridge resource does not need to be a power
250 * of two size, but it does have a minimum granularity.
251 * Round the size up to that minimum granularity so we
252 * know not to place something else at an address postitively
253 * decoded by the bridge.
255 bridge->size = round(base, 1UL << bridge->gran) - bridge->base;
257 printk_spew("DEV: %02x:%02x.%01x compute_allocate_%s: base: %08lx size: %08lx align: %d gran: %d done\n",
259 PCI_SLOT(bus->devfn), PCI_FUNC(bus->devfn),
260 (bridge->flags & IORESOURCE_IO)? "io":
261 (bridge->flags & IORESOURCE_PREFETCH)? "prefmem" : "mem",
262 base, bridge->size, bridge->align, bridge->gran);
267 static void allocate_vga_resource(void)
269 /* FIXME handle the VGA pallette snooping */
270 struct device *dev, *vga, *bus;
272 for(dev = all_devices; dev; dev = dev->next) {
273 uint32_t class_revision;
274 class_revision = pci_read_config32(dev, PCI_CLASS_REVISION);
275 if (((class_revision >> 24) == 0x03) &&
276 ((class_revision >> 16) != 0x380)) {
278 printk_debug("Allocating VGA resource\n");
282 /* All legacy VGA cards have MEM & I/O space registers */
283 dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
285 /* It isn't safe to enable other VGA cards */
286 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
293 /* Now walk up the bridges setting the VGA enable */
296 ctrl = pci_read_config16(bus, PCI_BRIDGE_CONTROL);
297 ctrl |= PCI_BRIDGE_CTL_VGA;
298 pci_write_config16(bus, PCI_BRIDGE_CONTROL, ctrl);
299 bus = (bus == bus->bus)? 0 : bus->bus;
304 /** Assign the computed resources to the bridges and devices on the bus.
305 * Recurse to any bridges found on this bus first. Then do the devices
307 * @param bus Pointer to the structure for this bus
309 void assign_resources(struct device *bus)
311 struct device *curdev;
313 printk_debug("ASSIGN RESOURCES, bus %d\n", bus->secondary);
315 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
316 curdev->ops->set_resources(curdev);
318 printk_debug("ASSIGNED RESOURCES, bus %d\n", bus->secondary);
321 static void enable_resources(struct device *bus)
323 struct device *curdev;
325 /* Walk through the chain of all pci devices and enable them.
326 * This is effectively a breadth first traversal so we should
327 * not have enalbing ordering problems.
329 for (curdev = all_devices; curdev; curdev = curdev->next) {
331 command = pci_read_config16(curdev, PCI_COMMAND);
332 command |= curdev->command;
333 printk_debug("DEV: %02x:%02x.%01x cmd <- %02x\n",
334 curdev->bus->secondary,
335 PCI_SLOT(curdev->devfn), PCI_FUNC(curdev->devfn),
337 pci_write_config16(curdev, PCI_COMMAND, command);
341 /** Enumerate the resources on the PCI by calling pci_init
343 void dev_enumerate(void)
346 printk_info("Enumerating buses...");
349 root->ops = &default_pci_ops_root;
351 root->subordinate = root->ops->scan_bus(root, 0);
352 printk_info("done\n");
355 /** Starting at the root, compute what resources are needed and allocate them.
356 * I/O starts at PCI_IO_START. Since the assignment is hierarchical we
357 * set the values into the dev_root struct.
359 void dev_configure(void)
361 struct device *root = &dev_root;
362 printk_info("Allocating resources...");
366 root->ops->read_resources(root);
368 /* Make certain the io devices are allocated somewhere
371 root->resource[0].base = DEVICE_IO_START;
372 root->resource[0].flags |= IORESOURCE_SET;
373 /* Now reallocate the pci resources memory with the
374 * highest addresses I can manage.
376 root->resource[1].base =
377 round_down(DEVICE_MEM_HIGH - root->resource[1].size,
378 1UL << root->resource[1].align);
379 root->resource[1].flags |= IORESOURCE_SET;
380 // now just set things into registers ... we hope ...
381 root->ops->set_resources(root);
383 allocate_vga_resource();
385 printk_info("done.\n");
388 /** Starting at the root, walk the tree and enable all devices/bridges.
389 * What really happens is computed COMMAND bits get set in register 4
391 void dev_enable(void)
393 printk_info("Enabling resourcess...");
395 /* now enable everything. */
396 enable_resources(&dev_root);
397 printk_info("done.\n");
400 /** Starting at the root, walk the tree and call a driver to
401 * do device specific setup.
403 void dev_initialize(void)
407 printk_info("Initializing devices...\n");
408 for (dev = all_devices; dev; dev = dev->next) {
409 if (dev->ops->init) {
410 printk_debug("PCI: %02x:%02x.%01x init\n",
412 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
416 printk_info("Devices initialized\n");