Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1 | /* |
Stefan Reinauer | 7e61e45 | 2008-01-18 10:35:56 +0000 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 3 | * |
| 4 | * It was originally based on the Linux kernel (arch/i386/kernel/pci-pc.c). |
| 5 | * |
| 6 | * Modifications are: |
| 7 | * Copyright (C) 2003 Eric Biederman <ebiederm@xmission.com> |
| 8 | * Copyright (C) 2003-2004 Linux Networx |
| 9 | * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx) |
| 10 | * Copyright (C) 2003 Ronald G. Minnich <rminnich@gmail.com> |
| 11 | * Copyright (C) 2004-2005 Li-Ta Lo <ollie@lanl.gov> |
| 12 | * Copyright (C) 2005-2006 Tyan |
| 13 | * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan) |
| 14 | * Copyright (C) 2005-2006 Stefan Reinauer <stepan@openbios.org> |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 15 | * Copyright (C) 2009 Myles Watson <mylesgw@gmail.com> |
Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | /* |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 19 | * (c) 1999--2000 Martin Mares <mj@suse.cz> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 20 | */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * Lots of mods by Ron Minnich <rminnich@lanl.gov>, with |
| 24 | * the final architecture guidance from Tom Merritt <tjm@codegen.com>. |
| 25 | * |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 26 | * In particular, we changed from the one-pass original version to |
| 27 | * Tom's recommended multiple-pass version. I wasn't sure about doing |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 28 | * it with multiple passes, until I actually started doing it and saw |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 29 | * the wisdom of Tom's recommendations... |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 30 | * |
| 31 | * Lots of cleanups by Eric Biederman to handle bridges, and to |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 32 | * handle resource allocation for non-PCI devices. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | #include <console/console.h> |
| 36 | #include <bitops.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 37 | #include <arch/io.h> |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 38 | #include <device/device.h> |
| 39 | #include <device/pci.h> |
Li-Ta Lo | 54f05f6 | 2004-05-14 17:20:29 +0000 | [diff] [blame] | 40 | #include <device/pci_ids.h> |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 43 | #include <smp/spinlock.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 44 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 45 | /** Linked list of ALL devices */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 46 | struct device *all_devices = &dev_root; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 47 | /** Pointer to the last device */ |
Myles Watson | 70679a0 | 2010-09-01 21:03:03 +0000 | [diff] [blame] | 48 | extern struct device *last_dev; |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 49 | /** Linked list of free resources */ |
| 50 | struct resource *free_resources = NULL; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 51 | |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 52 | DECLARE_SPIN_LOCK(dev_lock) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 53 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 54 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 55 | * Allocate a new device structure. |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 56 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 57 | * Allocte a new device structure and attach it to the device tree as a |
Li-Ta Lo | 9f0d0f9 | 2004-05-10 16:05:16 +0000 | [diff] [blame] | 58 | * child of the parent bus. |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 59 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 60 | * @param parent Parent bus the newly created device should be attached to. |
| 61 | * @param path Path to the device to be created. |
| 62 | * @return Pointer to the newly created device structure. |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 63 | * |
| 64 | * @see device_path |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 65 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 66 | device_t alloc_dev(struct bus *parent, struct device_path *path) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 67 | { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 68 | device_t dev, child; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 69 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 70 | spin_lock(&dev_lock); |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame] | 71 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 72 | /* Find the last child of our parent. */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 73 | for (child = parent->children; child && child->sibling; /* */ ) |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 74 | child = child->sibling; |
Li-Ta Lo | 3a81285 | 2004-12-03 22:39:34 +0000 | [diff] [blame] | 75 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 76 | dev = malloc(sizeof(*dev)); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 77 | if (dev == 0) |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 78 | die("alloc_dev(): out of memory.\n"); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 79 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 80 | memset(dev, 0, sizeof(*dev)); |
| 81 | memcpy(&dev->path, path, sizeof(*path)); |
| 82 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 83 | /* By default devices are enabled. */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 84 | dev->enabled = 1; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 85 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 86 | /* Add the new device to the list of children of the bus. */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 87 | dev->bus = parent; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 88 | if (child) |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 89 | child->sibling = dev; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 90 | else |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 91 | parent->children = dev; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 92 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 93 | /* Append a new device to the global device list. |
| 94 | * The list is used to find devices once everything is set up. |
| 95 | */ |
Myles Watson | 70679a0 | 2010-09-01 21:03:03 +0000 | [diff] [blame] | 96 | last_dev->next = dev; |
| 97 | last_dev = dev; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 98 | |
| 99 | spin_unlock(&dev_lock); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 100 | return dev; |
| 101 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 102 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 103 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 104 | * Round a number up to an alignment. |
| 105 | * |
| 106 | * @param val The starting value. |
| 107 | * @param roundup Alignment as a power of two. |
| 108 | * @return Rounded up number. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 109 | */ |
Eric Biederman | 448bd63 | 2004-10-14 22:52:15 +0000 | [diff] [blame] | 110 | static resource_t round(resource_t val, unsigned long pow) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 111 | { |
Eric Biederman | 448bd63 | 2004-10-14 22:52:15 +0000 | [diff] [blame] | 112 | resource_t mask; |
| 113 | mask = (1ULL << pow) - 1ULL; |
| 114 | val += mask; |
| 115 | val &= ~mask; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 116 | return val; |
| 117 | } |
| 118 | |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 119 | /** |
| 120 | * Read the resources on all devices of a given bus. |
| 121 | * |
| 122 | * @param bus Bus to read the resources on. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 123 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 124 | static void read_resources(struct bus *bus) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 125 | { |
| 126 | struct device *curdev; |
| 127 | |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 128 | printk(BIOS_SPEW, "%s %s bus %x link: %d\n", dev_path(bus->dev), |
| 129 | __func__, bus->secondary, bus->link_num); |
Eric Biederman | 448bd63 | 2004-10-14 22:52:15 +0000 | [diff] [blame] | 130 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 131 | /* Walk through all devices and find which resources they need. */ |
| 132 | for (curdev = bus->children; curdev; curdev = curdev->sibling) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 133 | struct bus *link; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 134 | |
| 135 | if (!curdev->enabled) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 136 | continue; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 137 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 138 | if (!curdev->ops || !curdev->ops->read_resources) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 139 | printk(BIOS_ERR, "%s missing read_resources\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 140 | dev_path(curdev)); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 141 | continue; |
| 142 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 143 | curdev->ops->read_resources(curdev); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 144 | |
| 145 | /* Read in the resources behind the current device's links. */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 146 | for (link = curdev->link_list; link; link = link->next) |
| 147 | read_resources(link); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 148 | } |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 149 | printk(BIOS_SPEW, "%s read_resources bus %d link: %d done\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 150 | dev_path(bus->dev), bus->secondary, bus->link_num); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 153 | struct pick_largest_state { |
| 154 | struct resource *last; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 155 | struct device *result_dev; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 156 | struct resource *result; |
| 157 | int seen_last; |
| 158 | }; |
| 159 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 160 | static void pick_largest_resource(void *gp, struct device *dev, |
| 161 | struct resource *resource) |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 162 | { |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 163 | struct pick_largest_state *state = gp; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 164 | struct resource *last; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 165 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 166 | last = state->last; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 167 | |
| 168 | /* Be certain to pick the successor to last. */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 169 | if (resource == last) { |
| 170 | state->seen_last = 1; |
| 171 | return; |
| 172 | } |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 173 | if (resource->flags & IORESOURCE_FIXED) |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 174 | return; /* Skip it. */ |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 175 | if (last && ((last->align < resource->align) || |
| 176 | ((last->align == resource->align) && |
| 177 | (last->size < resource->size)) || |
| 178 | ((last->align == resource->align) && |
| 179 | (last->size == resource->size) && (!state->seen_last)))) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 180 | return; |
| 181 | } |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 182 | if (!state->result || |
| 183 | (state->result->align < resource->align) || |
| 184 | ((state->result->align == resource->align) && |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 185 | (state->result->size < resource->size))) { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 186 | state->result_dev = dev; |
| 187 | state->result = resource; |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 188 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 191 | static struct device *largest_resource(struct bus *bus, |
| 192 | struct resource **result_res, |
| 193 | unsigned long type_mask, |
| 194 | unsigned long type) |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 195 | { |
| 196 | struct pick_largest_state state; |
| 197 | |
| 198 | state.last = *result_res; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 199 | state.result_dev = NULL; |
| 200 | state.result = NULL; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 201 | state.seen_last = 0; |
| 202 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 203 | search_bus_resources(bus, type_mask, type, pick_largest_resource, |
| 204 | &state); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 205 | |
| 206 | *result_res = state.result; |
| 207 | return state.result_dev; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 210 | /** |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 211 | * This function is the guts of the resource allocator. |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 212 | * |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 213 | * The problem. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 214 | * - Allocate resource locations for every device. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 215 | * - Don't overlap, and follow the rules of bridges. |
| 216 | * - Don't overlap with resources in fixed locations. |
| 217 | * - Be efficient so we don't have ugly strategies. |
| 218 | * |
| 219 | * The strategy. |
| 220 | * - Devices that have fixed addresses are the minority so don't |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 221 | * worry about them too much. Instead only use part of the address |
| 222 | * space for devices with programmable addresses. This easily handles |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 223 | * everything except bridges. |
| 224 | * |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 225 | * - PCI devices are required to have their sizes and their alignments |
| 226 | * equal. In this case an optimal solution to the packing problem |
| 227 | * exists. Allocate all devices from highest alignment to least |
| 228 | * alignment or vice versa. Use this. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 229 | * |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 230 | * - So we can handle more than PCI run two allocation passes on bridges. The |
| 231 | * first to see how large the resources are behind the bridge, and what |
| 232 | * their alignment requirements are. The second to assign a safe address to |
| 233 | * the devices behind the bridge. This allows us to treat a bridge as just |
| 234 | * a device with a couple of resources, and not need to special case it in |
| 235 | * the allocator. Also this allows handling of other types of bridges. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 236 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 237 | * @param bus The bus we are traversing. |
| 238 | * @param bridge The bridge resource which must contain the bus' resources. |
| 239 | * @param type_mask This value gets ANDed with the resource type. |
| 240 | * @param type This value must match the result of the AND. |
| 241 | * @return TODO |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 242 | */ |
Myles Watson | 54913b9 | 2009-10-13 20:00:09 +0000 | [diff] [blame] | 243 | static void compute_resources(struct bus *bus, struct resource *bridge, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 244 | unsigned long type_mask, unsigned long type) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 245 | { |
| 246 | struct device *dev; |
| 247 | struct resource *resource; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 248 | resource_t base; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 249 | base = round(bridge->base, bridge->align); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 250 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 251 | printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d" |
| 252 | " limit: %llx\n", dev_path(bus->dev), __func__, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 253 | (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ? |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 254 | "prefmem" : "mem", base, bridge->size, bridge->align, |
| 255 | bridge->gran, bridge->limit); |
Ronald G. Minnich | 99dcf23 | 2003-09-30 02:16:47 +0000 | [diff] [blame] | 256 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 257 | /* For each child which is a bridge, compute the resource needs. */ |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 258 | for (dev = bus->children; dev; dev = dev->sibling) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 259 | struct resource *child_bridge; |
| 260 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 261 | if (!dev->link_list) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 262 | continue; |
| 263 | |
| 264 | /* Find the resources with matching type flags. */ |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 265 | for (child_bridge = dev->resource_list; child_bridge; |
| 266 | child_bridge = child_bridge->next) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 267 | struct bus* link; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 268 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 269 | if (!(child_bridge->flags & IORESOURCE_BRIDGE) |
| 270 | || (child_bridge->flags & type_mask) != type) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 271 | continue; |
| 272 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 273 | /* |
| 274 | * Split prefetchable memory if combined. Many domains |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 275 | * use the same address space for prefetchable memory |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 276 | * and non-prefetchable memory. Bridges below them need |
| 277 | * it separated. Add the PREFETCH flag to the type_mask |
| 278 | * and type. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 279 | */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 280 | link = dev->link_list; |
| 281 | while (link && link->link_num != |
| 282 | IOINDEX_LINK(child_bridge->index)) |
| 283 | link = link->next; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 284 | |
| 285 | if (link == NULL) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 286 | printk(BIOS_ERR, "link %ld not found on %s\n", |
| 287 | IOINDEX_LINK(child_bridge->index), |
| 288 | dev_path(dev)); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 291 | compute_resources(link, child_bridge, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 292 | type_mask | IORESOURCE_PREFETCH, |
| 293 | type | (child_bridge->flags & |
| 294 | IORESOURCE_PREFETCH)); |
| 295 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 298 | /* Remember we haven't found anything yet. */ |
| 299 | resource = NULL; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 300 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 301 | /* |
| 302 | * Walk through all the resources on the current bus and compute the |
| 303 | * amount of address space taken by them. Take granularity and |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 304 | * alignment into account. |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 305 | */ |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 306 | while ((dev = largest_resource(bus, &resource, type_mask, type))) { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 307 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 308 | /* Size 0 resources can be skipped. */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 309 | if (!resource->size) |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 310 | continue; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 311 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 312 | /* Propagate the resource alignment to the bridge resource. */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 313 | if (resource->align > bridge->align) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 314 | bridge->align = resource->align; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 315 | |
| 316 | /* Propagate the resource limit to the bridge register. */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 317 | if (bridge->limit > resource->limit) |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 318 | bridge->limit = resource->limit; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 319 | |
| 320 | /* Warn if it looks like APICs aren't declared. */ |
| 321 | if ((resource->limit == 0xffffffff) && |
| 322 | (resource->flags & IORESOURCE_ASSIGNED)) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 323 | printk(BIOS_ERR, |
| 324 | "Resource limit looks wrong! (no APIC?)\n"); |
| 325 | printk(BIOS_ERR, "%s %02lx limit %08Lx\n", |
| 326 | dev_path(dev), resource->index, resource->limit); |
Eric Biederman | dbec2d4 | 2004-10-21 10:44:08 +0000 | [diff] [blame] | 327 | } |
Stefan Reinauer | 51754a3 | 2008-08-01 12:28:38 +0000 | [diff] [blame] | 328 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 329 | if (resource->flags & IORESOURCE_IO) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 330 | /* |
| 331 | * Don't allow potential aliases over the legacy PCI |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 332 | * expansion card addresses. The legacy PCI decodes |
| 333 | * only 10 bits, uses 0x100 - 0x3ff. Therefore, only |
| 334 | * 0x00 - 0xff can be used out of each 0x400 block of |
| 335 | * I/O space. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 336 | */ |
Eric Biederman | bbb6d10 | 2003-08-04 19:54:48 +0000 | [diff] [blame] | 337 | if ((base & 0x300) != 0) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 338 | base = (base & ~0x3ff) + 0x400; |
| 339 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 340 | /* |
| 341 | * Don't allow allocations in the VGA I/O range. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 342 | * PCI has special cases for that. |
| 343 | */ |
| 344 | else if ((base >= 0x3b0) && (base <= 0x3df)) { |
| 345 | base = 0x3e0; |
| 346 | } |
| 347 | } |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 348 | /* Base must be aligned. */ |
| 349 | base = round(base, resource->align); |
| 350 | resource->base = base; |
| 351 | base += resource->size; |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 352 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 353 | printk(BIOS_SPEW, "%s %02lx * [0x%llx - 0x%llx] %s\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 354 | dev_path(dev), resource->index, resource->base, |
| 355 | resource->base + resource->size - 1, |
| 356 | (resource->flags & IORESOURCE_IO) ? "io" : |
| 357 | (resource->flags & IORESOURCE_PREFETCH) ? |
| 358 | "prefmem" : "mem"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 359 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 360 | |
| 361 | /* |
| 362 | * A PCI bridge resource does not need to be a power of two size, but |
| 363 | * it does have a minimum granularity. Round the size up to that |
| 364 | * minimum granularity so we know not to place something else at an |
| 365 | * address postitively decoded by the bridge. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 366 | */ |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 367 | bridge->size = round(base, bridge->gran) - |
| 368 | round(bridge->base, bridge->align); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 369 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 370 | printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d" |
| 371 | " limit: %llx done\n", dev_path(bus->dev), __func__, |
| 372 | (bridge->flags & IORESOURCE_IO) ? "io" : |
| 373 | (bridge->flags & IORESOURCE_PREFETCH) ? "prefmem" : "mem", |
| 374 | base, bridge->size, bridge->align, bridge->gran, bridge->limit); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /** |
| 378 | * This function is the second part of the resource allocator. |
| 379 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 380 | * See the compute_resources function for a more detailed explanation. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 381 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 382 | * This function assigns the resources a value. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 383 | * |
| 384 | * @param bus The bus we are traversing. |
| 385 | * @param bridge The bridge resource which must contain the bus' resources. |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 386 | * @param type_mask This value gets ANDed with the resource type. |
| 387 | * @param type This value must match the result of the AND. |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 388 | * |
| 389 | * @see compute_resources |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 390 | */ |
Myles Watson | 54913b9 | 2009-10-13 20:00:09 +0000 | [diff] [blame] | 391 | static void allocate_resources(struct bus *bus, struct resource *bridge, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 392 | unsigned long type_mask, unsigned long type) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 393 | { |
| 394 | struct device *dev; |
| 395 | struct resource *resource; |
| 396 | resource_t base; |
| 397 | base = bridge->base; |
| 398 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 399 | printk(BIOS_SPEW, "%s %s_%s: base:%llx size:%llx align:%d gran:%d " |
| 400 | "limit:%llx\n", dev_path(bus->dev), __func__, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 401 | (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ? |
| 402 | "prefmem" : "mem", |
| 403 | base, bridge->size, bridge->align, bridge->gran, bridge->limit); |
| 404 | |
| 405 | /* Remember we haven't found anything yet. */ |
| 406 | resource = NULL; |
| 407 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 408 | /* |
| 409 | * Walk through all the resources on the current bus and allocate them |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 410 | * address space. |
| 411 | */ |
| 412 | while ((dev = largest_resource(bus, &resource, type_mask, type))) { |
| 413 | |
| 414 | /* Propagate the bridge limit to the resource register. */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 415 | if (resource->limit > bridge->limit) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 416 | resource->limit = bridge->limit; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 417 | |
| 418 | /* Size 0 resources can be skipped. */ |
| 419 | if (!resource->size) { |
| 420 | /* Set the base to limit so it doesn't confuse tolm. */ |
| 421 | resource->base = resource->limit; |
| 422 | resource->flags |= IORESOURCE_ASSIGNED; |
| 423 | continue; |
| 424 | } |
| 425 | |
| 426 | if (resource->flags & IORESOURCE_IO) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 427 | /* |
| 428 | * Don't allow potential aliases over the legacy PCI |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 429 | * expansion card addresses. The legacy PCI decodes |
| 430 | * only 10 bits, uses 0x100 - 0x3ff. Therefore, only |
| 431 | * 0x00 - 0xff can be used out of each 0x400 block of |
| 432 | * I/O space. |
| 433 | */ |
| 434 | if ((base & 0x300) != 0) { |
| 435 | base = (base & ~0x3ff) + 0x400; |
| 436 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 437 | /* |
| 438 | * Don't allow allocations in the VGA I/O range. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 439 | * PCI has special cases for that. |
| 440 | */ |
| 441 | else if ((base >= 0x3b0) && (base <= 0x3df)) { |
| 442 | base = 0x3e0; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | if ((round(base, resource->align) + resource->size - 1) <= |
| 447 | resource->limit) { |
| 448 | /* Base must be aligned. */ |
| 449 | base = round(base, resource->align); |
| 450 | resource->base = base; |
| 451 | resource->flags |= IORESOURCE_ASSIGNED; |
| 452 | resource->flags &= ~IORESOURCE_STORED; |
| 453 | base += resource->size; |
| 454 | } else { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 455 | printk(BIOS_ERR, "!! Resource didn't fit !!\n"); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 456 | printk(BIOS_ERR, " aligned base %llx size %llx " |
| 457 | "limit %llx\n", round(base, resource->align), |
| 458 | resource->size, resource->limit); |
| 459 | printk(BIOS_ERR, " %llx needs to be <= %llx " |
| 460 | "(limit)\n", (round(base, resource->align) + |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 461 | resource->size) - 1, resource->limit); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 462 | printk(BIOS_ERR, " %s%s %02lx * [0x%llx - 0x%llx]" |
| 463 | " %s\n", (resource->flags & IORESOURCE_ASSIGNED) |
| 464 | ? "Assigned: " : "", dev_path(dev), |
| 465 | resource->index, resource->base, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 466 | resource->base + resource->size - 1, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 467 | (resource->flags & IORESOURCE_IO) ? "io" |
| 468 | : (resource->flags & IORESOURCE_PREFETCH) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 469 | ? "prefmem" : "mem"); |
| 470 | } |
| 471 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 472 | printk(BIOS_SPEW, "%s%s %02lx * [0x%llx - 0x%llx] %s\n", |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 473 | (resource->flags & IORESOURCE_ASSIGNED) ? "Assigned: " |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 474 | : "", dev_path(dev), resource->index, resource->base, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 475 | resource->size ? resource->base + resource->size - 1 : |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 476 | resource->base, (resource->flags & IORESOURCE_IO) |
| 477 | ? "io" : (resource->flags & IORESOURCE_PREFETCH) |
| 478 | ? "prefmem" : "mem"); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 479 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 480 | |
| 481 | /* |
| 482 | * A PCI bridge resource does not need to be a power of two size, but |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 483 | * it does have a minimum granularity. Round the size up to that |
| 484 | * minimum granularity so we know not to place something else at an |
| 485 | * address positively decoded by the bridge. |
| 486 | */ |
| 487 | |
| 488 | bridge->flags |= IORESOURCE_ASSIGNED; |
| 489 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 490 | printk(BIOS_SPEW, "%s %s_%s: next_base: %llx size: %llx align: %d " |
| 491 | "gran: %d done\n", dev_path(bus->dev), __func__, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 492 | (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ? |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 493 | "prefmem" : "mem", base, bridge->size, bridge->align, |
| 494 | bridge->gran); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 495 | |
| 496 | /* For each child which is a bridge, allocate_resources. */ |
| 497 | for (dev = bus->children; dev; dev = dev->sibling) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 498 | struct resource *child_bridge; |
| 499 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 500 | if (!dev->link_list) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 501 | continue; |
| 502 | |
| 503 | /* Find the resources with matching type flags. */ |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 504 | for (child_bridge = dev->resource_list; child_bridge; |
| 505 | child_bridge = child_bridge->next) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 506 | struct bus* link; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 507 | |
| 508 | if (!(child_bridge->flags & IORESOURCE_BRIDGE) || |
| 509 | (child_bridge->flags & type_mask) != type) |
| 510 | continue; |
| 511 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 512 | /* |
| 513 | * Split prefetchable memory if combined. Many domains |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 514 | * use the same address space for prefetchable memory |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 515 | * and non-prefetchable memory. Bridges below them need |
| 516 | * it separated. Add the PREFETCH flag to the type_mask |
| 517 | * and type. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 518 | */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 519 | link = dev->link_list; |
| 520 | while (link && link->link_num != |
| 521 | IOINDEX_LINK(child_bridge->index)) |
| 522 | link = link->next; |
| 523 | if (link == NULL) |
| 524 | printk(BIOS_ERR, "link %ld not found on %s\n", |
| 525 | IOINDEX_LINK(child_bridge->index), |
| 526 | dev_path(dev)); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 527 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 528 | allocate_resources(link, child_bridge, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 529 | type_mask | IORESOURCE_PREFETCH, |
| 530 | type | (child_bridge->flags & |
| 531 | IORESOURCE_PREFETCH)); |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | #if CONFIG_PCI_64BIT_PREF_MEM == 1 |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 537 | #define MEM_MASK (IORESOURCE_PREFETCH | IORESOURCE_MEM) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 538 | #else |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 539 | #define MEM_MASK (IORESOURCE_MEM) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 540 | #endif |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 541 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 542 | #define IO_MASK (IORESOURCE_IO) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 543 | #define PREF_TYPE (IORESOURCE_PREFETCH | IORESOURCE_MEM) |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 544 | #define MEM_TYPE (IORESOURCE_MEM) |
| 545 | #define IO_TYPE (IORESOURCE_IO) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 546 | |
| 547 | struct constraints { |
| 548 | struct resource pref, io, mem; |
| 549 | }; |
| 550 | |
| 551 | static void constrain_resources(struct device *dev, struct constraints* limits) |
| 552 | { |
| 553 | struct device *child; |
| 554 | struct resource *res; |
| 555 | struct resource *lim; |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 556 | struct bus *link; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 557 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 558 | printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev)); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 559 | |
| 560 | /* Constrain limits based on the fixed resources of this device. */ |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 561 | for (res = dev->resource_list; res; res = res->next) { |
Patrick Georgi | 18c585b | 2009-08-28 12:48:02 +0000 | [diff] [blame] | 562 | if (!(res->flags & IORESOURCE_FIXED)) |
| 563 | continue; |
Myles Watson | ce9d864 | 2009-08-19 19:12:39 +0000 | [diff] [blame] | 564 | if (!res->size) { |
| 565 | /* It makes no sense to have 0-sized, fixed resources.*/ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 566 | printk(BIOS_ERR, "skipping %s@%lx fixed resource, " |
| 567 | "size=0!\n", dev_path(dev), res->index); |
Patrick Georgi | 6bd93f4 | 2009-08-19 17:29:41 +0000 | [diff] [blame] | 568 | continue; |
Myles Watson | ce9d864 | 2009-08-19 19:12:39 +0000 | [diff] [blame] | 569 | } |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 570 | |
| 571 | /* PREFETCH, MEM, or I/O - skip any others. */ |
| 572 | if ((res->flags & MEM_MASK) == PREF_TYPE) |
| 573 | lim = &limits->pref; |
| 574 | else if ((res->flags & MEM_MASK) == MEM_TYPE) |
| 575 | lim = &limits->mem; |
| 576 | else if ((res->flags & IO_MASK) == IO_TYPE) |
| 577 | lim = &limits->io; |
| 578 | else |
| 579 | continue; |
| 580 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 581 | /* |
| 582 | * Is it a fixed resource outside the current known region? |
| 583 | * If so, we don't have to consider it - it will be handled |
| 584 | * correctly and doesn't affect current region's limits. |
| 585 | */ |
| 586 | if (((res->base + res->size -1) < lim->base) |
| 587 | || (res->base > lim->limit)) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 588 | continue; |
| 589 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 590 | /* |
| 591 | * Choose to be above or below fixed resources. This check is |
| 592 | * signed so that "negative" amounts of space are handled |
| 593 | * correctly. |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 594 | */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 595 | if ((signed long long)(lim->limit - (res->base + res->size -1)) |
| 596 | > (signed long long)(res->base - lim->base)) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 597 | lim->base = res->base + res->size; |
| 598 | else |
| 599 | lim->limit = res->base -1; |
| 600 | } |
| 601 | |
| 602 | /* Descend into every enabled child and look for fixed resources. */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 603 | for (link = dev->link_list; link; link = link->next) { |
| 604 | for (child = link->children; child; child = child->sibling) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 605 | if (child->enabled) |
| 606 | constrain_resources(child, limits); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | static void avoid_fixed_resources(struct device *dev) |
| 612 | { |
| 613 | struct constraints limits; |
| 614 | struct resource *res; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 615 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 616 | printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev)); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 617 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 618 | /* Initialize constraints to maximum size. */ |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 619 | limits.pref.base = 0; |
| 620 | limits.pref.limit = 0xffffffffffffffffULL; |
| 621 | limits.io.base = 0; |
| 622 | limits.io.limit = 0xffffffffffffffffULL; |
| 623 | limits.mem.base = 0; |
| 624 | limits.mem.limit = 0xffffffffffffffffULL; |
| 625 | |
| 626 | /* Constrain the limits to dev's initial resources. */ |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 627 | for (res = dev->resource_list; res; res = res->next) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 628 | if ((res->flags & IORESOURCE_FIXED)) |
| 629 | continue; |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 630 | printk(BIOS_SPEW, "%s:@%s %02lx limit %08Lx\n", __func__, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 631 | dev_path(dev), res->index, res->limit); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 632 | if ((res->flags & MEM_MASK) == PREF_TYPE && |
| 633 | (res->limit < limits.pref.limit)) |
| 634 | limits.pref.limit = res->limit; |
| 635 | if ((res->flags & MEM_MASK) == MEM_TYPE && |
| 636 | (res->limit < limits.mem.limit)) |
| 637 | limits.mem.limit = res->limit; |
| 638 | if ((res->flags & IO_MASK) == IO_TYPE && |
| 639 | (res->limit < limits.io.limit)) |
| 640 | limits.io.limit = res->limit; |
| 641 | } |
| 642 | |
| 643 | /* Look through the tree for fixed resources and update the limits. */ |
| 644 | constrain_resources(dev, &limits); |
| 645 | |
| 646 | /* Update dev's resources with new limits. */ |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 647 | for (res = dev->resource_list; res; res = res->next) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 648 | struct resource *lim; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 649 | |
| 650 | if ((res->flags & IORESOURCE_FIXED)) |
| 651 | continue; |
| 652 | |
| 653 | /* PREFETCH, MEM, or I/O - skip any others. */ |
| 654 | if ((res->flags & MEM_MASK) == PREF_TYPE) |
| 655 | lim = &limits.pref; |
| 656 | else if ((res->flags & MEM_MASK) == MEM_TYPE) |
| 657 | lim = &limits.mem; |
| 658 | else if ((res->flags & IO_MASK) == IO_TYPE) |
| 659 | lim = &limits.io; |
| 660 | else |
| 661 | continue; |
| 662 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 663 | printk(BIOS_SPEW, "%s2: %s@%02lx limit %08Lx\n", __func__, |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 664 | dev_path(dev), res->index, res->limit); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 665 | printk(BIOS_SPEW, "\tlim->base %08Lx lim->limit %08Lx\n", |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 666 | lim->base, lim->limit); |
| 667 | |
| 668 | /* Is the resource outside the limits? */ |
| 669 | if (lim->base > res->base) |
| 670 | res->base = lim->base; |
| 671 | if (res->limit > lim->limit) |
| 672 | res->limit = lim->limit; |
| 673 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 674 | } |
arch import user (historical) | dc81118 | 2005-07-06 17:16:09 +0000 | [diff] [blame] | 675 | |
Myles Watson | 28412f5 | 2009-09-17 16:54:46 +0000 | [diff] [blame] | 676 | #if CONFIG_VGA_BRIDGE_SETUP == 1 |
Yinghai Lu | 1f1085b | 2005-01-17 21:37:12 +0000 | [diff] [blame] | 677 | device_t vga_pri = 0; |
Myles Watson | c7233e0 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 678 | static void set_vga_bridge_bits(void) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 679 | { |
Uwe Hermann | 312673c | 2009-10-27 21:49:33 +0000 | [diff] [blame] | 680 | /* |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 681 | * FIXME: Modify set_vga_bridge() so it is less PCI centric! |
Uwe Hermann | 312673c | 2009-10-27 21:49:33 +0000 | [diff] [blame] | 682 | * This function knows too much about PCI stuff, it should be just |
| 683 | * an iterator/visitor. |
| 684 | */ |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 685 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 686 | /* FIXME: Handle the VGA palette snooping. */ |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame] | 687 | struct device *dev, *vga, *vga_onboard, *vga_first, *vga_last; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 688 | struct bus *bus; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 689 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 690 | bus = 0; |
| 691 | vga = 0; |
Yinghai Lu | 1f1085b | 2005-01-17 21:37:12 +0000 | [diff] [blame] | 692 | vga_onboard = 0; |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame] | 693 | vga_first = 0; |
| 694 | vga_last = 0; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 695 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 696 | for (dev = all_devices; dev; dev = dev->next) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 697 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 698 | if (!dev->enabled) |
| 699 | continue; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 700 | |
Li-Ta Lo | 54f05f6 | 2004-05-14 17:20:29 +0000 | [diff] [blame] | 701 | if (((dev->class >> 16) == PCI_BASE_CLASS_DISPLAY) && |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 702 | ((dev->class >> 8) != PCI_CLASS_DISPLAY_OTHER)) { |
| 703 | if (!vga_first) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 704 | if (dev->on_mainboard) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 705 | vga_onboard = dev; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 706 | else |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 707 | vga_first = dev; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 708 | } else { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 709 | if (dev->on_mainboard) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 710 | vga_onboard = dev; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 711 | else |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 712 | vga_last = dev; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 713 | } |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame] | 714 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 715 | /* It isn't safe to enable other VGA cards. */ |
Yinghai Lu | 1f1085b | 2005-01-17 21:37:12 +0000 | [diff] [blame] | 716 | dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 719 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 720 | vga = vga_last; |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame] | 721 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 722 | if (!vga) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 723 | vga = vga_first; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 724 | |
Stefan Reinauer | abc0c85 | 2010-11-22 08:09:50 +0000 | [diff] [blame] | 725 | #if CONFIG_ONBOARD_VGA_IS_PRIMARY == 1 |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 726 | if (vga_onboard) /* Will use onboard VGA as primary. */ |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame] | 727 | #else |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 728 | if (!vga) /* Will use last add-on adapter as primary. */ |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame] | 729 | #endif |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 730 | { |
| 731 | vga = vga_onboard; |
| 732 | } |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 733 | |
arch import user (historical) | dc81118 | 2005-07-06 17:16:09 +0000 | [diff] [blame] | 734 | if (vga) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 735 | /* VGA is first add-on card or the only onboard VGA. */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 736 | printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga)); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 737 | /* All legacy VGA cards have MEM & I/O space registers. */ |
Yinghai Lu | 1f1085b | 2005-01-17 21:37:12 +0000 | [diff] [blame] | 738 | vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO); |
| 739 | vga_pri = vga; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 740 | bus = vga->bus; |
| 741 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 742 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 743 | /* Now walk up the bridges setting the VGA enable. */ |
| 744 | while (bus) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 745 | printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 746 | dev_path(bus->dev)); |
Li-Ta Lo | db7f47c | 2004-01-08 21:15:49 +0000 | [diff] [blame] | 747 | bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 748 | bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus; |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 749 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 750 | } |
Stefan Reinauer | 7ce8c54 | 2005-12-02 21:52:30 +0000 | [diff] [blame] | 751 | |
Yinghai Lu | 9e4faef | 2005-01-14 22:04:49 +0000 | [diff] [blame] | 752 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 753 | |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 754 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 755 | * Assign the computed resources to the devices on the bus. |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 756 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 757 | * Use the device specific set_resources() method to store the computed |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 758 | * resources to hardware. For bridge devices, the set_resources() method |
| 759 | * has to recurse into every down stream buses. |
| 760 | * |
| 761 | * Mutual recursion: |
| 762 | * assign_resources() -> device_operation::set_resources() |
| 763 | * device_operation::set_resources() -> assign_resources() |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 764 | * |
| 765 | * @param bus Pointer to the structure for this bus. |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 766 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 767 | void assign_resources(struct bus *bus) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 768 | { |
| 769 | struct device *curdev; |
| 770 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 771 | printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 772 | dev_path(bus->dev), bus->secondary, bus->link_num); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 773 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 774 | for (curdev = bus->children; curdev; curdev = curdev->sibling) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 775 | if (!curdev->enabled || !curdev->resource_list) |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 776 | continue; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 777 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 778 | if (!curdev->ops || !curdev->ops->set_resources) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 779 | printk(BIOS_ERR, "%s missing set_resources\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 780 | dev_path(curdev)); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 781 | continue; |
| 782 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 783 | curdev->ops->set_resources(curdev); |
| 784 | } |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 785 | printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 786 | dev_path(bus->dev), bus->secondary, bus->link_num); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Li-Ta Lo | 5782d27 | 2004-04-26 17:51:20 +0000 | [diff] [blame] | 789 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 790 | * Enable the resources for devices on a link. |
Li-Ta Lo | 5782d27 | 2004-04-26 17:51:20 +0000 | [diff] [blame] | 791 | * |
| 792 | * Enable resources of the device by calling the device specific |
| 793 | * enable_resources() method. |
| 794 | * |
| 795 | * The parent's resources should be enabled first to avoid having enabling |
| 796 | * order problem. This is done by calling the parent's enable_resources() |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 797 | * method before its childrens' enable_resources() methods. |
Li-Ta Lo | 9f0d0f9 | 2004-05-10 16:05:16 +0000 | [diff] [blame] | 798 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 799 | * @param link The link whose devices' resources are to be enabled. |
Li-Ta Lo | 5782d27 | 2004-04-26 17:51:20 +0000 | [diff] [blame] | 800 | */ |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 801 | static void enable_resources(struct bus *link) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 802 | { |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 803 | struct device *dev; |
| 804 | struct bus *c_link; |
| 805 | |
| 806 | for (dev = link->children; dev; dev = dev->sibling) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 807 | if (dev->enabled && dev->ops && dev->ops->enable_resources) |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 808 | dev->ops->enable_resources(dev); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 809 | } |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 810 | |
| 811 | for (dev = link->children; dev; dev = dev->sibling) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 812 | for (c_link = dev->link_list; c_link; c_link = c_link->next) |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 813 | enable_resources(c_link); |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 814 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 817 | /** |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 818 | * Reset all of the devices on a bus and clear the bus's reset_needed flag. |
| 819 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 820 | * @param bus Pointer to the bus structure. |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 821 | * @return 1 if the bus was successfully reset, 0 otherwise. |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 822 | */ |
| 823 | int reset_bus(struct bus *bus) |
| 824 | { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 825 | if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 826 | bus->dev->ops->reset_bus(bus); |
| 827 | bus->reset_needed = 0; |
| 828 | return 1; |
| 829 | } |
| 830 | return 0; |
| 831 | } |
| 832 | |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 833 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 834 | * Scan for devices on a bus. |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 835 | * |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 836 | * If there are bridges on the bus, recursively scan the buses behind the |
| 837 | * bridges. If the setting up and tuning of the bus causes a reset to be |
| 838 | * required, reset the bus and scan it again. |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 839 | * |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 840 | * @param busdev Pointer to the bus device. |
| 841 | * @param max Current bus number. |
| 842 | * @return The maximum bus number found, after scanning all subordinate buses. |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 843 | */ |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 844 | unsigned int scan_bus(struct device *busdev, unsigned int max) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 845 | { |
| 846 | unsigned int new_max; |
| 847 | int do_scan_bus; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 848 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 849 | if (!busdev || !busdev->enabled || !busdev->ops || |
| 850 | !busdev->ops->scan_bus) { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 851 | return max; |
| 852 | } |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 853 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 854 | do_scan_bus = 1; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 855 | while (do_scan_bus) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 856 | struct bus *link; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 857 | new_max = busdev->ops->scan_bus(busdev, max); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 858 | do_scan_bus = 0; |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 859 | for (link = busdev->link_list; link; link = link->next) { |
| 860 | if (link->reset_needed) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 861 | if (reset_bus(link)) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 862 | do_scan_bus = 1; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 863 | else |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 864 | busdev->bus->reset_needed = 1; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | } |
| 868 | return new_max; |
| 869 | } |
| 870 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 871 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 872 | * Determine the existence of devices and extend the device tree. |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 873 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 874 | * Most of the devices in the system are listed in the mainboard devicetree.cb |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 875 | * file. The device structures for these devices are generated at compile |
| 876 | * time by the config tool and are organized into the device tree. This |
| 877 | * function determines if the devices created at compile time actually exist |
| 878 | * in the physical system. |
| 879 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 880 | * For devices in the physical system but not listed in devicetree.cb, |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 881 | * the device structures have to be created at run time and attached to the |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 882 | * device tree. |
| 883 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 884 | * This function starts from the root device 'dev_root', scans the buses in |
| 885 | * the system recursively, and modifies the device tree according to the |
| 886 | * result of the probe. |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 887 | * |
Li-Ta Lo | 5782d27 | 2004-04-26 17:51:20 +0000 | [diff] [blame] | 888 | * This function has no idea how to scan and probe buses and devices at all. |
| 889 | * It depends on the bus/device specific scan_bus() method to do it. The |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 890 | * scan_bus() method also has to create the device structure and attach |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 891 | * it to the device tree. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 892 | */ |
| 893 | void dev_enumerate(void) |
| 894 | { |
| 895 | struct device *root; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 896 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 897 | printk(BIOS_INFO, "Enumerating buses...\n"); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 898 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 899 | root = &dev_root; |
Myles Watson | cd5d756 | 2009-05-12 13:43:34 +0000 | [diff] [blame] | 900 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 901 | show_all_devs(BIOS_SPEW, "Before device enumeration."); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 902 | printk(BIOS_SPEW, "Compare with tree...\n"); |
Stefan Reinauer | 39e7229 | 2009-10-26 16:47:05 +0000 | [diff] [blame] | 903 | show_devs_tree(root, BIOS_SPEW, 0, 0); |
Myles Watson | cd5d756 | 2009-05-12 13:43:34 +0000 | [diff] [blame] | 904 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 905 | if (root->chip_ops && root->chip_ops->enable_dev) |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 906 | root->chip_ops->enable_dev(root); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 907 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 908 | if (!root->ops || !root->ops->scan_bus) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 909 | printk(BIOS_ERR, "dev_root missing scan_bus operation"); |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 910 | return; |
| 911 | } |
Stefan Reinauer | 6afcea8 | 2009-07-18 17:58:44 +0000 | [diff] [blame] | 912 | scan_bus(root, 0); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 913 | printk(BIOS_INFO, "done\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 916 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 917 | * Configure devices on the devices tree. |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 918 | * |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 919 | * Starting at the root of the device tree, travel it recursively in two |
| 920 | * passes. In the first pass, we compute and allocate resources (ranges) |
| 921 | * requried by each device. In the second pass, the resources ranges are |
| 922 | * relocated to their final position and stored to the hardware. |
Li-Ta Lo | 5782d27 | 2004-04-26 17:51:20 +0000 | [diff] [blame] | 923 | * |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 924 | * I/O resources grow upward. MEM resources grow downward. |
Li-Ta Lo | 5782d27 | 2004-04-26 17:51:20 +0000 | [diff] [blame] | 925 | * |
| 926 | * Since the assignment is hierarchical we set the values into the dev_root |
Myles Watson | 032a965 | 2009-05-11 22:24:53 +0000 | [diff] [blame] | 927 | * struct. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 928 | */ |
| 929 | void dev_configure(void) |
| 930 | { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 931 | struct resource *res; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 932 | struct device *root; |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 933 | struct device *child; |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 934 | |
Myles Watson | 28412f5 | 2009-09-17 16:54:46 +0000 | [diff] [blame] | 935 | #if CONFIG_VGA_BRIDGE_SETUP == 1 |
Myles Watson | c7233e0 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 936 | set_vga_bridge_bits(); |
| 937 | #endif |
| 938 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 939 | printk(BIOS_INFO, "Allocating resources...\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 940 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 941 | root = &dev_root; |
Myles Watson | cd5d756 | 2009-05-12 13:43:34 +0000 | [diff] [blame] | 942 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 943 | /* |
| 944 | * Each domain should create resources which contain the entire address |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 945 | * space for IO, MEM, and PREFMEM resources in the domain. The |
| 946 | * allocation of device resources will be done from this address space. |
| 947 | */ |
Myles Watson | cd5d756 | 2009-05-12 13:43:34 +0000 | [diff] [blame] | 948 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 949 | /* Read the resources for the entire tree. */ |
Li-Ta Lo | 0493069 | 2004-11-25 17:37:19 +0000 | [diff] [blame] | 950 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 951 | printk(BIOS_INFO, "Reading resources...\n"); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 952 | read_resources(root->link_list); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 953 | printk(BIOS_INFO, "Done reading resources.\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 954 | |
Stefan Reinauer | 39e7229 | 2009-10-26 16:47:05 +0000 | [diff] [blame] | 955 | print_resource_tree(root, BIOS_SPEW, "After reading."); |
Myles Watson | cd5d756 | 2009-05-12 13:43:34 +0000 | [diff] [blame] | 956 | |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 957 | /* Compute resources for all domains. */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 958 | for (child = root->link_list->children; child; child = child->sibling) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 959 | if (!(child->path.type == DEVICE_PATH_PCI_DOMAIN)) |
| 960 | continue; |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 961 | for (res = child->resource_list; res; res = res->next) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 962 | if (res->flags & IORESOURCE_FIXED) |
| 963 | continue; |
| 964 | if (res->flags & IORESOURCE_PREFETCH) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 965 | compute_resources(child->link_list, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 966 | res, MEM_MASK, PREF_TYPE); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 967 | continue; |
| 968 | } |
| 969 | if (res->flags & IORESOURCE_MEM) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 970 | compute_resources(child->link_list, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 971 | res, MEM_MASK, MEM_TYPE); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 972 | continue; |
| 973 | } |
| 974 | if (res->flags & IORESOURCE_IO) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 975 | compute_resources(child->link_list, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 976 | res, IO_MASK, IO_TYPE); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 977 | continue; |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | /* For all domains. */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 983 | for (child = root->link_list->children; child; child=child->sibling) |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 984 | if (child->path.type == DEVICE_PATH_PCI_DOMAIN) |
| 985 | avoid_fixed_resources(child); |
| 986 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 987 | /* |
| 988 | * Now we need to adjust the resources. MEM resources need to start at |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 989 | * the highest address managable. |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 990 | */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 991 | for (child = root->link_list->children; child; child = child->sibling) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 992 | if (child->path.type != DEVICE_PATH_PCI_DOMAIN) |
| 993 | continue; |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 994 | for (res = child->resource_list; res; res = res->next) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 995 | if (!(res->flags & IORESOURCE_MEM) || |
| 996 | res->flags & IORESOURCE_FIXED) |
| 997 | continue; |
| 998 | res->base = resource_max(res); |
| 999 | } |
| 1000 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 1001 | |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 1002 | /* Store the computed resource allocations into device registers ... */ |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1003 | printk(BIOS_INFO, "Setting resources...\n"); |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1004 | for (child = root->link_list->children; child; child = child->sibling) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1005 | if (!(child->path.type == DEVICE_PATH_PCI_DOMAIN)) |
| 1006 | continue; |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 1007 | for (res = child->resource_list; res; res = res->next) { |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1008 | if (res->flags & IORESOURCE_FIXED) |
| 1009 | continue; |
| 1010 | if (res->flags & IORESOURCE_PREFETCH) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1011 | allocate_resources(child->link_list, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1012 | res, MEM_MASK, PREF_TYPE); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1013 | continue; |
| 1014 | } |
| 1015 | if (res->flags & IORESOURCE_MEM) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1016 | allocate_resources(child->link_list, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1017 | res, MEM_MASK, MEM_TYPE); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1018 | continue; |
| 1019 | } |
| 1020 | if (res->flags & IORESOURCE_IO) { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1021 | allocate_resources(child->link_list, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1022 | res, IO_MASK, IO_TYPE); |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 1023 | continue; |
| 1024 | } |
| 1025 | } |
| 1026 | } |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 1027 | assign_resources(root->link_list); |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1028 | printk(BIOS_INFO, "Done setting resources.\n"); |
Stefan Reinauer | 39e7229 | 2009-10-26 16:47:05 +0000 | [diff] [blame] | 1029 | print_resource_tree(root, BIOS_SPEW, "After assigning values."); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 1030 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1031 | printk(BIOS_INFO, "Done allocating resources.\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1034 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 1035 | * Enable devices on the device tree. |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1036 | * |
| 1037 | * Starting at the root, walk the tree and enable all devices/bridges by |
| 1038 | * calling the device's enable_resources() method. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1039 | */ |
| 1040 | void dev_enable(void) |
| 1041 | { |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1042 | struct bus *link; |
| 1043 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1044 | printk(BIOS_INFO, "Enabling resources...\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1045 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1046 | /* Now enable everything. */ |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1047 | for (link = dev_root.link_list; link; link = link->next) |
| 1048 | enable_resources(link); |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1049 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1050 | printk(BIOS_INFO, "done.\n"); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 1053 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 1054 | * Initialize a specific device. |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1055 | * |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1056 | * The parent should be initialized first to avoid having an ordering problem. |
| 1057 | * This is done by calling the parent's init() method before its childrens' |
| 1058 | * init() methods. |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1059 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 1060 | * @param dev The device to be initialized. |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1061 | */ |
| 1062 | static void init_dev(struct device *dev) |
| 1063 | { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1064 | if (!dev->enabled) |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1065 | return; |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1066 | |
| 1067 | if (!dev->initialized && dev->ops && dev->ops->init) { |
| 1068 | if (dev->path.type == DEVICE_PATH_I2C) { |
| 1069 | printk(BIOS_DEBUG, "smbus: %s[%d]->", |
| 1070 | dev_path(dev->bus->dev), dev->bus->link_num); |
| 1071 | } |
| 1072 | |
| 1073 | printk(BIOS_DEBUG, "%s init\n", dev_path(dev)); |
| 1074 | dev->initialized = 1; |
| 1075 | dev->ops->init(dev); |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | static void init_link(struct bus *link) |
| 1080 | { |
| 1081 | struct device *dev; |
| 1082 | struct bus *c_link; |
| 1083 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1084 | for (dev = link->children; dev; dev = dev->sibling) |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1085 | init_dev(dev); |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1086 | |
| 1087 | for (dev = link->children; dev; dev = dev->sibling) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1088 | for (c_link = dev->link_list; c_link; c_link = c_link->next) |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1089 | init_link(c_link); |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 1094 | * Initialize all devices in the global device tree. |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1095 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 1096 | * Starting at the root device, call the device's init() method to do |
| 1097 | * device-specific setup, then call each child's init() method. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1098 | */ |
| 1099 | void dev_initialize(void) |
| 1100 | { |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1101 | struct bus *link; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1102 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1103 | printk(BIOS_INFO, "Initializing devices...\n"); |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1104 | |
Myles Watson | 1bd3fb7 | 2010-08-16 16:25:23 +0000 | [diff] [blame] | 1105 | /* First call the mainboard init. */ |
| 1106 | init_dev(&dev_root); |
| 1107 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 1108 | /* Now initialize everything. */ |
Myles Watson | 7eac445 | 2010-06-17 16:16:56 +0000 | [diff] [blame] | 1109 | for (link = dev_root.link_list; link; link = link->next) |
| 1110 | init_link(link); |
| 1111 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 1112 | printk(BIOS_INFO, "Devices initialized\n"); |
Stefan Reinauer | 39e7229 | 2009-10-26 16:47:05 +0000 | [diff] [blame] | 1113 | show_all_devs(BIOS_SPEW, "After init."); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 1114 | } |