Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
Elyes Haouas | 04c3b5a | 2022-10-07 10:08:05 +0200 | [diff] [blame] | 3 | #include <commonlib/bsd/helpers.h> |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 4 | #include <console/console.h> |
| 5 | #include <device/device.h> |
| 6 | #include <memrange.h> |
| 7 | #include <post.h> |
Elyes Haouas | 04c3b5a | 2022-10-07 10:08:05 +0200 | [diff] [blame] | 8 | #include <types.h> |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 9 | |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 10 | static const char *resource2str(const struct resource *res) |
| 11 | { |
| 12 | if (res->flags & IORESOURCE_IO) |
| 13 | return "io"; |
| 14 | if (res->flags & IORESOURCE_PREFETCH) |
| 15 | return "prefmem"; |
| 16 | if (res->flags & IORESOURCE_MEM) |
| 17 | return "mem"; |
| 18 | return "undefined"; |
| 19 | } |
| 20 | |
| 21 | static bool dev_has_children(const struct device *dev) |
| 22 | { |
| 23 | const struct bus *bus = dev->link_list; |
| 24 | return bus && bus->children; |
| 25 | } |
| 26 | |
Nico Huber | 5226301 | 2020-05-23 19:15:36 +0200 | [diff] [blame] | 27 | static resource_t effective_limit(const struct resource *const res) |
| 28 | { |
| 29 | /* Always allow bridge resources above 4G. */ |
| 30 | if (res->flags & IORESOURCE_BRIDGE) |
| 31 | return res->limit; |
| 32 | |
| 33 | const resource_t quirk_4g_limit = |
| 34 | res->flags & IORESOURCE_ABOVE_4G ? UINT64_MAX : UINT32_MAX; |
| 35 | return MIN(res->limit, quirk_4g_limit); |
| 36 | } |
| 37 | |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 38 | #define res_printk(depth, str, ...) printk(BIOS_DEBUG, "%*c"str, depth, ' ', __VA_ARGS__) |
| 39 | |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 40 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 41 | * During pass 1, once all the requirements for downstream devices of a |
| 42 | * bridge are gathered, this function calculates the overall resource |
| 43 | * requirement for the bridge. It starts by picking the largest resource |
| 44 | * requirement downstream for the given resource type and works by |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 45 | * adding requirements in descending order. |
| 46 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 47 | * Additionally, it takes alignment and limits of the downstream devices |
| 48 | * into consideration and ensures that they get propagated to the bridge |
| 49 | * resource. This is required to guarantee that the upstream bridge/ |
| 50 | * domain honors the limit and alignment requirements for this bridge |
| 51 | * based on the tightest constraints downstream. |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 52 | * |
| 53 | * Last but not least, it stores the offset inside the bridge resource |
| 54 | * for each child resource in its base field. This simplifies pass 2 |
| 55 | * for resources behind a bridge, as we only have to add offsets to the |
| 56 | * allocated base of the bridge resource. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 57 | */ |
| 58 | static void update_bridge_resource(const struct device *bridge, struct resource *bridge_res, |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 59 | unsigned long type_match, int print_depth) |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 60 | { |
| 61 | const struct device *child; |
| 62 | struct resource *child_res; |
| 63 | resource_t base; |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 64 | const unsigned long type_mask = IORESOURCE_TYPE_MASK | IORESOURCE_PREFETCH; |
| 65 | struct bus *bus = bridge->link_list; |
| 66 | |
| 67 | child_res = NULL; |
| 68 | |
| 69 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 70 | * `base` keeps track of where the next allocation for child resources |
| 71 | * can take place from within the bridge resource window. Since the |
| 72 | * bridge resource window allocation is not performed yet, it can start |
| 73 | * at 0. Base gets updated every time a resource requirement is |
| 74 | * accounted for in the loop below. After scanning all these resources, |
| 75 | * base will indicate the total size requirement for the current bridge |
| 76 | * resource window. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 77 | */ |
| 78 | base = 0; |
| 79 | |
Arthur Heymans | d436b16 | 2023-05-23 22:47:05 +0200 | [diff] [blame] | 80 | res_printk(print_depth, "%s %s: size: %llx align: %u gran: %u limit: %llx\n", |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 81 | dev_path(bridge), resource2str(bridge_res), bridge_res->size, |
| 82 | bridge_res->align, bridge_res->gran, bridge_res->limit); |
| 83 | |
| 84 | while ((child = largest_resource(bus, &child_res, type_mask, type_match))) { |
| 85 | |
| 86 | /* Size 0 resources can be skipped. */ |
| 87 | if (!child_res->size) |
| 88 | continue; |
| 89 | |
Nico Huber | ec7b313 | 2020-05-23 18:20:47 +0200 | [diff] [blame] | 90 | /* Resources with 0 limit can't be assigned anything. */ |
| 91 | if (!child_res->limit) |
| 92 | continue; |
| 93 | |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 94 | /* |
Nico Huber | 74169c1 | 2020-05-23 18:15:34 +0200 | [diff] [blame] | 95 | * Propagate the resource alignment to the bridge resource. The |
| 96 | * condition can only be true for the first (largest) resource. For all |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 97 | * other child resources, alignment is taken care of by rounding their |
| 98 | * base up. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 99 | */ |
Nico Huber | 74169c1 | 2020-05-23 18:15:34 +0200 | [diff] [blame] | 100 | if (child_res->align > bridge_res->align) |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 101 | bridge_res->align = child_res->align; |
| 102 | |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 103 | /* |
Nico Huber | ec7b313 | 2020-05-23 18:20:47 +0200 | [diff] [blame] | 104 | * Propagate the resource limit to the bridge resource. If a downstream |
| 105 | * device has stricter requirements w.r.t. limits for any resource, that |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 106 | * constraint needs to be propagated back up to the bridges downstream |
| 107 | * of the domain. This way, the whole bridge resource fulfills the limit. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 108 | */ |
Nico Huber | 5226301 | 2020-05-23 19:15:36 +0200 | [diff] [blame] | 109 | if (effective_limit(child_res) < bridge_res->limit) |
| 110 | bridge_res->limit = effective_limit(child_res); |
Furquan Shaikh | 1bb05ef30 | 2020-05-15 17:33:52 -0700 | [diff] [blame] | 111 | |
| 112 | /* |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 113 | * Alignment value of 0 means that the child resource has no alignment |
| 114 | * requirements and so the base value remains unchanged here. |
| 115 | */ |
Nico Huber | b327704 | 2020-05-23 18:08:50 +0200 | [diff] [blame] | 116 | base = ALIGN_UP(base, POWER_OF_2(child_res->align)); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 117 | |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 118 | /* |
| 119 | * Store the relative offset inside the bridge resource for later |
| 120 | * consumption in allocate_bridge_resources(), and invalidate flags |
| 121 | * related to the base. |
| 122 | */ |
| 123 | child_res->base = base; |
| 124 | child_res->flags &= ~(IORESOURCE_ASSIGNED | IORESOURCE_STORED); |
| 125 | |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 126 | res_printk(print_depth + 1, "%s %02lx * [0x%llx - 0x%llx] %s\n", |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 127 | dev_path(child), child_res->index, base, base + child_res->size - 1, |
| 128 | resource2str(child_res)); |
| 129 | |
| 130 | base += child_res->size; |
| 131 | } |
| 132 | |
| 133 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 134 | * After all downstream device resources are scanned, `base` represents |
| 135 | * the total size requirement for the current bridge resource window. |
| 136 | * This size needs to be rounded up to the granularity requirement of |
| 137 | * the bridge to ensure that the upstream bridge/domain allocates big |
| 138 | * enough window. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 139 | */ |
Nico Huber | b327704 | 2020-05-23 18:08:50 +0200 | [diff] [blame] | 140 | bridge_res->size = ALIGN_UP(base, POWER_OF_2(bridge_res->gran)); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 141 | |
Arthur Heymans | d436b16 | 2023-05-23 22:47:05 +0200 | [diff] [blame] | 142 | res_printk(print_depth, "%s %s: size: %llx align: %u gran: %u limit: %llx done\n", |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 143 | dev_path(bridge), resource2str(bridge_res), bridge_res->size, |
| 144 | bridge_res->align, bridge_res->gran, bridge_res->limit); |
| 145 | } |
| 146 | |
| 147 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 148 | * During pass 1, at the bridge level, the resource allocator gathers |
| 149 | * requirements from downstream devices and updates its own resource |
| 150 | * windows for the provided resource type. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 151 | */ |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 152 | static void compute_bridge_resources(const struct device *bridge, unsigned long type_match, |
| 153 | int print_depth) |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 154 | { |
| 155 | const struct device *child; |
| 156 | struct resource *res; |
| 157 | struct bus *bus = bridge->link_list; |
| 158 | const unsigned long type_mask = IORESOURCE_TYPE_MASK | IORESOURCE_PREFETCH; |
| 159 | |
| 160 | for (res = bridge->resource_list; res; res = res->next) { |
| 161 | if (!(res->flags & IORESOURCE_BRIDGE)) |
| 162 | continue; |
| 163 | |
| 164 | if ((res->flags & type_mask) != type_match) |
| 165 | continue; |
| 166 | |
| 167 | /* |
| 168 | * Ensure that the resource requirements for all downstream bridges are |
| 169 | * gathered before updating the window for current bridge resource. |
| 170 | */ |
| 171 | for (child = bus->children; child; child = child->sibling) { |
| 172 | if (!dev_has_children(child)) |
| 173 | continue; |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 174 | compute_bridge_resources(child, type_match, print_depth + 1); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Update the window for current bridge resource now that all downstream |
| 179 | * requirements are gathered. |
| 180 | */ |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 181 | update_bridge_resource(bridge, res, type_match, print_depth); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 186 | * During pass 1, the resource allocator walks down the entire sub-tree |
| 187 | * of a domain. It gathers resource requirements for every downstream |
| 188 | * bridge by looking at the resource requests of its children. Thus, the |
| 189 | * requirement gathering begins at the leaf devices and is propagated |
| 190 | * back up to the downstream bridges of the domain. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 191 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 192 | * At the domain level, it identifies every downstream bridge and walks |
| 193 | * down that bridge to gather requirements for each resource type i.e. |
| 194 | * i/o, mem and prefmem. Since bridges have separate windows for mem and |
| 195 | * prefmem, requirements for each need to be collected separately. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 196 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 197 | * Domain resource windows are fixed ranges and hence requirement |
| 198 | * gathering does not result in any changes to these fixed ranges. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 199 | */ |
| 200 | static void compute_domain_resources(const struct device *domain) |
| 201 | { |
| 202 | const struct device *child; |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 203 | const int print_depth = 1; |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 204 | |
| 205 | if (domain->link_list == NULL) |
| 206 | return; |
| 207 | |
| 208 | for (child = domain->link_list->children; child; child = child->sibling) { |
| 209 | |
| 210 | /* Skip if this is not a bridge or has no children under it. */ |
| 211 | if (!dev_has_children(child)) |
| 212 | continue; |
| 213 | |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 214 | compute_bridge_resources(child, IORESOURCE_IO, print_depth); |
| 215 | compute_bridge_resources(child, IORESOURCE_MEM, print_depth); |
| 216 | compute_bridge_resources(child, IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 217 | print_depth); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 221 | static unsigned char get_alignment_by_resource_type(const unsigned long type) |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 222 | { |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 223 | if (type & IORESOURCE_MEM) |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 224 | return 12; /* Page-aligned --> log2(4KiB) */ |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 225 | else if (type & IORESOURCE_IO) |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 226 | return 0; /* No special alignment required --> log2(1) */ |
| 227 | |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 228 | die("Unexpected resource type: flags(%lu)!\n", type); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 231 | static void print_resource_ranges(const struct device *dev, const struct memranges *ranges) |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 232 | { |
| 233 | const struct range_entry *r; |
| 234 | |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 235 | printk(BIOS_INFO, " %s: Resource ranges:\n", dev_path(dev)); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 236 | |
| 237 | if (memranges_is_empty(ranges)) |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 238 | printk(BIOS_INFO, " * EMPTY!!\n"); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 239 | |
| 240 | memranges_each_entry(r, ranges) { |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 241 | printk(BIOS_INFO, " * Base: %llx, Size: %llx, Tag: %lx\n", |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 242 | range_entry_base(r), range_entry_size(r), range_entry_tag(r)); |
| 243 | } |
| 244 | } |
| 245 | |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 246 | static void update_constraints(struct memranges *ranges, const struct device *dev, |
| 247 | const struct resource *res) |
| 248 | { |
| 249 | if (!res->size) |
| 250 | return; |
| 251 | |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 252 | printk(BIOS_DEBUG, " %s: %s %02lx base %08llx limit %08llx %s (fixed)\n", |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 253 | __func__, dev_path(dev), res->index, res->base, |
| 254 | res->base + res->size - 1, resource2str(res)); |
| 255 | |
| 256 | memranges_create_hole(ranges, res->base, res->size); |
| 257 | } |
| 258 | |
| 259 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 260 | * Scan the entire tree to identify any fixed resources allocated by |
| 261 | * any device to ensure that the address map for domain resources are |
| 262 | * appropriately updated. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 263 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 264 | * Domains can typically provide a memrange for entire address space. |
| 265 | * So, this function punches holes in the address space for all fixed |
| 266 | * resources that are already defined. Both I/O and normal memory |
| 267 | * resources are added as fixed. Both need to be removed from address |
| 268 | * space where dynamic resource allocations are sourced. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 269 | */ |
| 270 | static void avoid_fixed_resources(struct memranges *ranges, const struct device *dev, |
| 271 | unsigned long mask_match) |
| 272 | { |
| 273 | const struct resource *res; |
| 274 | const struct device *child; |
| 275 | const struct bus *bus; |
| 276 | |
| 277 | for (res = dev->resource_list; res != NULL; res = res->next) { |
| 278 | if ((res->flags & mask_match) != mask_match) |
| 279 | continue; |
| 280 | update_constraints(ranges, dev, res); |
| 281 | } |
| 282 | |
| 283 | bus = dev->link_list; |
| 284 | if (bus == NULL) |
| 285 | return; |
| 286 | |
| 287 | for (child = bus->children; child != NULL; child = child->sibling) |
| 288 | avoid_fixed_resources(ranges, child, mask_match); |
| 289 | } |
| 290 | |
| 291 | static void constrain_domain_resources(const struct device *domain, struct memranges *ranges, |
| 292 | unsigned long type) |
| 293 | { |
| 294 | unsigned long mask_match = type | IORESOURCE_FIXED; |
| 295 | |
| 296 | if (type == IORESOURCE_IO) { |
| 297 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 298 | * Don't allow allocations in the VGA I/O range. PCI has special |
| 299 | * cases for that. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 300 | */ |
Furquan Shaikh | 563e614 | 2020-05-26 12:04:35 -0700 | [diff] [blame] | 301 | memranges_create_hole(ranges, 0x3b0, 0x3df - 0x3b0 + 1); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 302 | |
| 303 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 304 | * Resource allocator no longer supports the legacy behavior where |
| 305 | * I/O resource allocation is guaranteed to avoid aliases over legacy |
| 306 | * PCI expansion card addresses. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 307 | */ |
| 308 | } |
| 309 | |
| 310 | avoid_fixed_resources(ranges, domain, mask_match); |
| 311 | } |
| 312 | |
| 313 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 314 | * This function creates a list of memranges of given type using the |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 315 | * resource that is provided. It applies additional constraints to |
| 316 | * ensure that the memranges do not overlap any of the fixed resources |
| 317 | * under the domain. The domain typically provides a memrange for the |
| 318 | * entire address space. Thus, it is up to the chipset to add DRAM and |
| 319 | * all other windows which cannot be used for resource allocation as |
| 320 | * fixed resources. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 321 | */ |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 322 | static void setup_resource_ranges(const struct device *const domain, |
| 323 | const unsigned long type, |
| 324 | struct memranges *const ranges) |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 325 | { |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 326 | const unsigned long type_mask = IORESOURCE_TYPE_MASK | IORESOURCE_FIXED; |
Nico Huber | 5226301 | 2020-05-23 19:15:36 +0200 | [diff] [blame] | 327 | const unsigned char alignment = get_alignment_by_resource_type(type); |
| 328 | |
| 329 | memranges_init_empty_with_alignment(ranges, NULL, 0, alignment); |
| 330 | |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 331 | for (struct resource *res = domain->resource_list; res != NULL; res = res->next) { |
| 332 | if ((res->flags & type_mask) != type) |
Nico Huber | 5226301 | 2020-05-23 19:15:36 +0200 | [diff] [blame] | 333 | continue; |
| 334 | |
| 335 | printk(BIOS_DEBUG, "%s %s: base: %llx size: %llx align: %u gran: %u limit: %llx\n", |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 336 | dev_path(domain), resource2str(res), res->base, res->size, res->align, |
Nico Huber | 5226301 | 2020-05-23 19:15:36 +0200 | [diff] [blame] | 337 | res->gran, res->limit); |
| 338 | |
| 339 | memranges_insert(ranges, res->base, res->limit - res->base + 1, type); |
Nico Huber | 38aafa3 | 2022-09-04 22:20:21 +0200 | [diff] [blame] | 340 | } |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 341 | |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 342 | constrain_domain_resources(domain, ranges, type); |
Nico Huber | 5226301 | 2020-05-23 19:15:36 +0200 | [diff] [blame] | 343 | |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 344 | print_resource_ranges(domain, ranges); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 347 | static void print_resource_done(const struct device *dev, const struct resource *res) |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 348 | { |
Arthur Heymans | d436b16 | 2023-05-23 22:47:05 +0200 | [diff] [blame] | 349 | printk(BIOS_DEBUG, "%s %s: base: %llx size: %llx align: %u gran: %u limit: %llx done\n", |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 350 | dev_path(dev), resource2str(res), res->base, res->size, res->align, |
| 351 | res->gran, res->limit); |
| 352 | } |
| 353 | |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 354 | static void cleanup_domain_resource_ranges(const struct device *dev, struct memranges *ranges, |
| 355 | unsigned long type) |
| 356 | { |
| 357 | memranges_teardown(ranges); |
| 358 | for (struct resource *res = dev->resource_list; res != NULL; res = res->next) { |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 359 | if (res->flags & IORESOURCE_FIXED) |
| 360 | continue; |
| 361 | if ((res->flags & IORESOURCE_TYPE_MASK) != type) |
| 362 | continue; |
| 363 | print_resource_done(dev, res); |
| 364 | } |
| 365 | } |
| 366 | |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 367 | static void assign_resource(struct resource *const res, const resource_t base, |
| 368 | const struct device *const dev) |
| 369 | { |
| 370 | res->base = base; |
| 371 | res->limit = res->base + res->size - 1; |
| 372 | res->flags |= IORESOURCE_ASSIGNED; |
| 373 | res->flags &= ~IORESOURCE_STORED; |
| 374 | |
| 375 | printk(BIOS_DEBUG, " %s %02lx * [0x%llx - 0x%llx] limit: %llx %s\n", |
| 376 | dev_path(dev), res->index, res->base, res->limit, res->limit, resource2str(res)); |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | * This is where the actual allocation of resources happens during |
| 381 | * pass 2. We construct a list of memory ranges corresponding to the |
| 382 | * resource of a given type, then look for the biggest unallocated |
| 383 | * resource on the downstream bus. This continues in a descending order |
| 384 | * until all resources of a given type have space allocated within the |
| 385 | * domain's resource window. |
| 386 | */ |
| 387 | static void allocate_toplevel_resources(const struct device *const domain, |
| 388 | const unsigned long type) |
| 389 | { |
| 390 | const unsigned long type_mask = IORESOURCE_TYPE_MASK; |
| 391 | struct resource *res = NULL; |
| 392 | const struct device *dev; |
| 393 | struct memranges ranges; |
| 394 | resource_t base; |
| 395 | |
| 396 | if (!dev_has_children(domain)) |
| 397 | return; |
| 398 | |
| 399 | setup_resource_ranges(domain, type, &ranges); |
| 400 | |
| 401 | while ((dev = largest_resource(domain->link_list, &res, type_mask, type))) { |
| 402 | |
| 403 | if (!res->size) |
| 404 | continue; |
| 405 | |
| 406 | if (!memranges_steal(&ranges, res->limit, res->size, res->align, type, &base, |
| 407 | CONFIG(RESOURCE_ALLOCATION_TOP_DOWN))) { |
| 408 | printk(BIOS_ERR, "Resource didn't fit!!! "); |
| 409 | printk(BIOS_DEBUG, " %s %02lx * size: 0x%llx limit: %llx %s\n", |
| 410 | dev_path(dev), res->index, res->size, |
| 411 | res->limit, resource2str(res)); |
| 412 | continue; |
| 413 | } |
| 414 | |
| 415 | assign_resource(res, base, dev); |
| 416 | } |
| 417 | |
| 418 | cleanup_domain_resource_ranges(domain, &ranges, type); |
| 419 | } |
| 420 | |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 421 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 422 | * Pass 2 of the resource allocator at the bridge level loops through |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 423 | * all the resources for the bridge and assigns all the base addresses |
| 424 | * of its children's resources of the same type. update_bridge_resource() |
| 425 | * of pass 1 pre-calculated the offsets of these bases inside the bridge |
| 426 | * resource. Now that the bridge resource is allocated, all we have to |
| 427 | * do is to add its final base to these offsets. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 428 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 429 | * Once allocation at the current bridge is complete, resource allocator |
| 430 | * continues walking down the downstream bridges until it hits the leaf |
| 431 | * devices. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 432 | */ |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 433 | static void assign_resource_cb(void *param, struct device *dev, struct resource *res) |
| 434 | { |
| 435 | /* We have to filter the same resources as update_bridge_resource(). */ |
| 436 | if (!res->size || !res->limit) |
| 437 | return; |
| 438 | |
| 439 | assign_resource(res, *(const resource_t *)param + res->base, dev); |
| 440 | } |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 441 | static void allocate_bridge_resources(const struct device *bridge) |
| 442 | { |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 443 | const unsigned long type_mask = |
| 444 | IORESOURCE_TYPE_MASK | IORESOURCE_PREFETCH | IORESOURCE_FIXED; |
| 445 | struct bus *const bus = bridge->link_list; |
| 446 | struct resource *res; |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 447 | struct device *child; |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 448 | |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 449 | for (res = bridge->resource_list; res != NULL; res = res->next) { |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 450 | if (!res->size) |
| 451 | continue; |
| 452 | |
| 453 | if (!(res->flags & IORESOURCE_BRIDGE)) |
| 454 | continue; |
| 455 | |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 456 | if (!(res->flags & IORESOURCE_ASSIGNED)) |
| 457 | continue; |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 458 | |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 459 | /* Run assign_resource_cb() for all downstream resources of the same type. */ |
| 460 | search_bus_resources(bus, type_mask, res->flags & type_mask, |
| 461 | assign_resource_cb, &res->base); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 464 | for (child = bus->children; child != NULL; child = child->sibling) { |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 465 | if (!dev_has_children(child)) |
| 466 | continue; |
| 467 | |
| 468 | allocate_bridge_resources(child); |
| 469 | } |
| 470 | } |
| 471 | |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 472 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 473 | * Pass 2 of resource allocator begins at the domain level. Every domain |
| 474 | * has two types of resources - io and mem. For each of these resources, |
| 475 | * this function creates a list of memory ranges that can be used for |
| 476 | * downstream resource allocation. This list is constrained to remove |
| 477 | * any fixed resources in the domain sub-tree of the given resource |
| 478 | * type. It then uses the memory ranges to apply best fit on the |
| 479 | * resource requirements of the downstream devices. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 480 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 481 | * Once resources are allocated to all downstream devices of the domain, |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 482 | * it walks down each downstream bridge to finish resource assignment |
| 483 | * of its children resources within its own window. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 484 | */ |
| 485 | static void allocate_domain_resources(const struct device *domain) |
| 486 | { |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 487 | /* Resource type I/O */ |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 488 | allocate_toplevel_resources(domain, IORESOURCE_IO); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 489 | |
| 490 | /* |
| 491 | * Resource type Mem: |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 492 | * Domain does not distinguish between mem and prefmem resources. Thus, |
| 493 | * the resource allocation at domain level considers mem and prefmem |
| 494 | * together when finding the best fit based on the biggest resource |
| 495 | * requirement. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 496 | */ |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 497 | allocate_toplevel_resources(domain, IORESOURCE_MEM); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 498 | |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 499 | struct device *child; |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 500 | for (child = domain->link_list->children; child; child = child->sibling) { |
| 501 | if (!dev_has_children(child)) |
| 502 | continue; |
| 503 | |
| 504 | /* Continue allocation for all downstream bridges. */ |
| 505 | allocate_bridge_resources(child); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 510 | * This function forms the guts of the resource allocator. It walks |
| 511 | * through the entire device tree for each domain two times. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 512 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 513 | * Every domain has a fixed set of ranges. These ranges cannot be |
| 514 | * relaxed based on the requirements of the downstream devices. They |
| 515 | * represent the available windows from which resources can be allocated |
| 516 | * to the different devices under the domain. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 517 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 518 | * In order to identify the requirements of downstream devices, resource |
| 519 | * allocator walks in a DFS fashion. It gathers the requirements from |
| 520 | * leaf devices and propagates those back up to their upstream bridges |
| 521 | * until the requirements for all the downstream devices of the domain |
| 522 | * are gathered. This is referred to as pass 1 of the resource allocator. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 523 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 524 | * Once the requirements for all the devices under the domain are |
| 525 | * gathered, the resource allocator walks a second time to allocate |
| 526 | * resources to downstream devices as per the requirements. It always |
| 527 | * picks the biggest resource request as per the type (i/o and mem) to |
| 528 | * allocate space from its fixed window to the immediate downstream |
| 529 | * device of the domain. In order to accomplish best fit for the |
| 530 | * resources, a list of ranges is maintained by each resource type (i/o |
| 531 | * and mem). At the domain level we don't differentiate between mem and |
| 532 | * prefmem. Since they are allocated space from the same window, the |
| 533 | * resource allocator at the domain level ensures that the biggest |
| 534 | * requirement is selected independent of the prefetch type. Once the |
| 535 | * resource allocation for all immediate downstream devices is complete |
| 536 | * at the domain level, the resource allocator walks down the subtree |
| 537 | * for each downstream bridge to continue the allocation process at the |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 538 | * bridge level. Since bridges have either their whole window allocated |
| 539 | * or nothing, we only need to place downstream resources inside these |
| 540 | * windows by re-using offsets that were pre-calculated in pass 1. This |
| 541 | * continues until resource allocation is realized for all downstream |
| 542 | * bridges in the domain sub-tree. This is referred to as pass 2 of the |
| 543 | * resource allocator. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 544 | * |
| 545 | * Some rules that are followed by the resource allocator: |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 546 | * - Allocate resource locations for every device as long as |
| 547 | * the requirements can be satisfied. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 548 | * - Don't overlap with resources in fixed locations. |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 549 | * - Don't overlap and follow the rules of bridges -- downstream |
| 550 | * devices of bridges should use parts of the address space |
| 551 | * allocated to the bridge. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 552 | */ |
| 553 | void allocate_resources(const struct device *root) |
| 554 | { |
| 555 | const struct device *child; |
| 556 | |
| 557 | if ((root == NULL) || (root->link_list == NULL)) |
| 558 | return; |
| 559 | |
| 560 | for (child = root->link_list->children; child; child = child->sibling) { |
| 561 | |
| 562 | if (child->path.type != DEVICE_PATH_DOMAIN) |
| 563 | continue; |
| 564 | |
| 565 | post_log_path(child); |
| 566 | |
Nico Huber | 9260ea6 | 2020-05-23 23:20:13 +0200 | [diff] [blame^] | 567 | /* Pass 1 - Relative placement. */ |
| 568 | printk(BIOS_INFO, "=== Resource allocator: %s - Pass 1 (relative placement) ===\n", |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 569 | dev_path(child)); |
| 570 | compute_domain_resources(child); |
| 571 | |
| 572 | /* Pass 2 - Allocate resources as per gathered requirements. */ |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 573 | printk(BIOS_INFO, "=== Resource allocator: %s - Pass 2 (allocating resources) ===\n", |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 574 | dev_path(child)); |
| 575 | allocate_domain_resources(child); |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 576 | |
| 577 | printk(BIOS_INFO, "=== Resource allocator: %s - resource allocation complete ===\n", |
| 578 | dev_path(child)); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 579 | } |
| 580 | } |