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 | |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 27 | #define res_printk(depth, str, ...) printk(BIOS_DEBUG, "%*c"str, depth, ' ', __VA_ARGS__) |
| 28 | |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 29 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 30 | * During pass 1, once all the requirements for downstream devices of a |
| 31 | * bridge are gathered, this function calculates the overall resource |
| 32 | * requirement for the bridge. It starts by picking the largest resource |
| 33 | * requirement downstream for the given resource type and works by |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 34 | * adding requirements in descending order. |
| 35 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 36 | * Additionally, it takes alignment and limits of the downstream devices |
| 37 | * into consideration and ensures that they get propagated to the bridge |
| 38 | * resource. This is required to guarantee that the upstream bridge/ |
| 39 | * domain honors the limit and alignment requirements for this bridge |
| 40 | * based on the tightest constraints downstream. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 41 | */ |
| 42 | 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] | 43 | unsigned long type_match, int print_depth) |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 44 | { |
| 45 | const struct device *child; |
| 46 | struct resource *child_res; |
| 47 | resource_t base; |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 48 | const unsigned long type_mask = IORESOURCE_TYPE_MASK | IORESOURCE_PREFETCH; |
| 49 | struct bus *bus = bridge->link_list; |
| 50 | |
| 51 | child_res = NULL; |
| 52 | |
| 53 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 54 | * `base` keeps track of where the next allocation for child resources |
| 55 | * can take place from within the bridge resource window. Since the |
| 56 | * bridge resource window allocation is not performed yet, it can start |
| 57 | * at 0. Base gets updated every time a resource requirement is |
| 58 | * accounted for in the loop below. After scanning all these resources, |
| 59 | * base will indicate the total size requirement for the current bridge |
| 60 | * resource window. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 61 | */ |
| 62 | base = 0; |
| 63 | |
Arthur Heymans | d436b16 | 2023-05-23 22:47:05 +0200 | [diff] [blame] | 64 | 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] | 65 | dev_path(bridge), resource2str(bridge_res), bridge_res->size, |
| 66 | bridge_res->align, bridge_res->gran, bridge_res->limit); |
| 67 | |
| 68 | while ((child = largest_resource(bus, &child_res, type_mask, type_match))) { |
| 69 | |
| 70 | /* Size 0 resources can be skipped. */ |
| 71 | if (!child_res->size) |
| 72 | continue; |
| 73 | |
Nico Huber | ec7b313 | 2020-05-23 18:20:47 +0200 | [diff] [blame] | 74 | /* Resources with 0 limit can't be assigned anything. */ |
| 75 | if (!child_res->limit) |
| 76 | continue; |
| 77 | |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 78 | /* |
Nico Huber | 74169c1 | 2020-05-23 18:15:34 +0200 | [diff] [blame] | 79 | * Propagate the resource alignment to the bridge resource. The |
| 80 | * condition can only be true for the first (largest) resource. For all |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 81 | * other children resources, alignment is taken care of by updating the |
| 82 | * base to round up as per the child resource alignment. It is |
| 83 | * guaranteed that pass 2 follows the exact same method of picking the |
| 84 | * resource for allocation using largest_resource(). Thus, as long as |
Nico Huber | 74169c1 | 2020-05-23 18:15:34 +0200 | [diff] [blame] | 85 | * the alignment for the largest child resource is propagated up to the |
| 86 | * bridge resource, it can be guaranteed that the alignment for all |
| 87 | * resources is appropriately met. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 88 | */ |
Nico Huber | 74169c1 | 2020-05-23 18:15:34 +0200 | [diff] [blame] | 89 | if (child_res->align > bridge_res->align) |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 90 | bridge_res->align = child_res->align; |
| 91 | |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 92 | /* |
Nico Huber | ec7b313 | 2020-05-23 18:20:47 +0200 | [diff] [blame] | 93 | * Propagate the resource limit to the bridge resource. If a downstream |
| 94 | * device has stricter requirements w.r.t. limits for any resource, that |
| 95 | * constraint needs to be propagated back up to the downstream bridges |
| 96 | * of the domain. This guarantees that the resource allocation which |
| 97 | * starts at the domain level takes into account all these constraints |
| 98 | * thus working on a global view. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 99 | */ |
Nico Huber | 38aafa3 | 2022-09-04 22:20:21 +0200 | [diff] [blame] | 100 | if (child_res->limit < bridge_res->limit) |
| 101 | bridge_res->limit = child_res->limit; |
| 102 | |
| 103 | /* |
| 104 | * Propagate the downstream resource request to allocate above 4G |
| 105 | * boundary to upstream bridge resource. This ensures that during |
| 106 | * pass 2, the resource allocator at domain level has a global view |
| 107 | * of all the downstream device requirements and thus address space |
| 108 | * is allocated as per updated flags in the bridge resource. |
| 109 | * |
| 110 | * Since the bridge resource is a single window, all the downstream |
| 111 | * resources of this bridge resource will be allocated in space above |
| 112 | * the 4G boundary. |
| 113 | */ |
| 114 | if (child_res->flags & IORESOURCE_ABOVE_4G) |
| 115 | bridge_res->flags |= IORESOURCE_ABOVE_4G; |
Furquan Shaikh | 1bb05ef30 | 2020-05-15 17:33:52 -0700 | [diff] [blame] | 116 | |
| 117 | /* |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 118 | * Alignment value of 0 means that the child resource has no alignment |
| 119 | * requirements and so the base value remains unchanged here. |
| 120 | */ |
Nico Huber | b327704 | 2020-05-23 18:08:50 +0200 | [diff] [blame] | 121 | base = ALIGN_UP(base, POWER_OF_2(child_res->align)); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 122 | |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 123 | 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] | 124 | dev_path(child), child_res->index, base, base + child_res->size - 1, |
| 125 | resource2str(child_res)); |
| 126 | |
| 127 | base += child_res->size; |
| 128 | } |
| 129 | |
| 130 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 131 | * After all downstream device resources are scanned, `base` represents |
| 132 | * the total size requirement for the current bridge resource window. |
| 133 | * This size needs to be rounded up to the granularity requirement of |
| 134 | * the bridge to ensure that the upstream bridge/domain allocates big |
| 135 | * enough window. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 136 | */ |
Nico Huber | b327704 | 2020-05-23 18:08:50 +0200 | [diff] [blame] | 137 | bridge_res->size = ALIGN_UP(base, POWER_OF_2(bridge_res->gran)); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 138 | |
Arthur Heymans | d436b16 | 2023-05-23 22:47:05 +0200 | [diff] [blame] | 139 | 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] | 140 | dev_path(bridge), resource2str(bridge_res), bridge_res->size, |
| 141 | bridge_res->align, bridge_res->gran, bridge_res->limit); |
| 142 | } |
| 143 | |
| 144 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 145 | * During pass 1, at the bridge level, the resource allocator gathers |
| 146 | * requirements from downstream devices and updates its own resource |
| 147 | * windows for the provided resource type. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 148 | */ |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 149 | static void compute_bridge_resources(const struct device *bridge, unsigned long type_match, |
| 150 | int print_depth) |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 151 | { |
| 152 | const struct device *child; |
| 153 | struct resource *res; |
| 154 | struct bus *bus = bridge->link_list; |
| 155 | const unsigned long type_mask = IORESOURCE_TYPE_MASK | IORESOURCE_PREFETCH; |
| 156 | |
| 157 | for (res = bridge->resource_list; res; res = res->next) { |
| 158 | if (!(res->flags & IORESOURCE_BRIDGE)) |
| 159 | continue; |
| 160 | |
| 161 | if ((res->flags & type_mask) != type_match) |
| 162 | continue; |
| 163 | |
| 164 | /* |
| 165 | * Ensure that the resource requirements for all downstream bridges are |
| 166 | * gathered before updating the window for current bridge resource. |
| 167 | */ |
| 168 | for (child = bus->children; child; child = child->sibling) { |
| 169 | if (!dev_has_children(child)) |
| 170 | continue; |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 171 | compute_bridge_resources(child, type_match, print_depth + 1); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Update the window for current bridge resource now that all downstream |
| 176 | * requirements are gathered. |
| 177 | */ |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 178 | update_bridge_resource(bridge, res, type_match, print_depth); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 183 | * During pass 1, the resource allocator walks down the entire sub-tree |
| 184 | * of a domain. It gathers resource requirements for every downstream |
| 185 | * bridge by looking at the resource requests of its children. Thus, the |
| 186 | * requirement gathering begins at the leaf devices and is propagated |
| 187 | * back up to the downstream bridges of the domain. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 188 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 189 | * At the domain level, it identifies every downstream bridge and walks |
| 190 | * down that bridge to gather requirements for each resource type i.e. |
| 191 | * i/o, mem and prefmem. Since bridges have separate windows for mem and |
| 192 | * prefmem, requirements for each need to be collected separately. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 193 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 194 | * Domain resource windows are fixed ranges and hence requirement |
| 195 | * gathering does not result in any changes to these fixed ranges. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 196 | */ |
| 197 | static void compute_domain_resources(const struct device *domain) |
| 198 | { |
| 199 | const struct device *child; |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 200 | const int print_depth = 1; |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 201 | |
| 202 | if (domain->link_list == NULL) |
| 203 | return; |
| 204 | |
| 205 | for (child = domain->link_list->children; child; child = child->sibling) { |
| 206 | |
| 207 | /* Skip if this is not a bridge or has no children under it. */ |
| 208 | if (!dev_has_children(child)) |
| 209 | continue; |
| 210 | |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 211 | compute_bridge_resources(child, IORESOURCE_IO, print_depth); |
| 212 | compute_bridge_resources(child, IORESOURCE_MEM, print_depth); |
| 213 | compute_bridge_resources(child, IORESOURCE_MEM | IORESOURCE_PREFETCH, |
| 214 | print_depth); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 218 | static unsigned char get_alignment_by_resource_type(const unsigned long type) |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 219 | { |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 220 | if (type & IORESOURCE_MEM) |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 221 | return 12; /* Page-aligned --> log2(4KiB) */ |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 222 | else if (type & IORESOURCE_IO) |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 223 | return 0; /* No special alignment required --> log2(1) */ |
| 224 | |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 225 | die("Unexpected resource type: flags(%lu)!\n", type); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Nico Huber | 38aafa3 | 2022-09-04 22:20:21 +0200 | [diff] [blame] | 228 | /* |
| 229 | * If the resource is NULL or if the resource is not assigned, then it |
| 230 | * cannot be used for allocation for downstream devices. |
| 231 | */ |
| 232 | static bool is_resource_invalid(const struct resource *res) |
| 233 | { |
| 234 | return (res == NULL) || !(res->flags & IORESOURCE_ASSIGNED); |
| 235 | } |
| 236 | |
| 237 | static void initialize_domain_io_resource_memranges(struct memranges *ranges, |
| 238 | const struct resource *res, |
| 239 | unsigned long memrange_type) |
| 240 | { |
| 241 | memranges_insert(ranges, res->base, res->limit - res->base + 1, memrange_type); |
| 242 | } |
| 243 | |
| 244 | static void initialize_domain_mem_resource_memranges(struct memranges *ranges, |
| 245 | const struct resource *res, |
| 246 | unsigned long memrange_type) |
| 247 | { |
| 248 | resource_t res_base; |
| 249 | resource_t res_limit; |
| 250 | |
| 251 | const resource_t limit_4g = 0xffffffff; |
| 252 | |
| 253 | res_base = res->base; |
| 254 | res_limit = res->limit; |
| 255 | |
| 256 | /* |
| 257 | * Split the resource into two separate ranges if it crosses the 4G |
| 258 | * boundary. Memrange type is set differently to ensure that memrange |
| 259 | * does not merge these two ranges. For the range above 4G boundary, |
| 260 | * given memrange type is ORed with IORESOURCE_ABOVE_4G. |
| 261 | */ |
| 262 | if (res_base <= limit_4g) { |
| 263 | |
| 264 | resource_t range_limit; |
| 265 | |
| 266 | /* Clip the resource limit at 4G boundary if necessary. */ |
| 267 | range_limit = MIN(res_limit, limit_4g); |
| 268 | memranges_insert(ranges, res_base, range_limit - res_base + 1, memrange_type); |
| 269 | |
| 270 | /* |
| 271 | * If the resource lies completely below the 4G boundary, nothing more |
| 272 | * needs to be done. |
| 273 | */ |
| 274 | if (res_limit <= limit_4g) |
| 275 | return; |
| 276 | |
| 277 | /* |
| 278 | * If the resource window crosses the 4G boundary, then update res_base |
| 279 | * to add another entry for the range above the boundary. |
| 280 | */ |
| 281 | res_base = limit_4g + 1; |
| 282 | } |
| 283 | |
| 284 | if (res_base > res_limit) |
| 285 | return; |
| 286 | |
| 287 | /* |
| 288 | * If resource lies completely above the 4G boundary or if the resource |
| 289 | * was clipped to add two separate ranges, the range above 4G boundary |
| 290 | * has the resource flag IORESOURCE_ABOVE_4G set. This allows domain to |
| 291 | * handle any downstream requests for resource allocation above 4G |
| 292 | * differently. |
| 293 | */ |
| 294 | memranges_insert(ranges, res_base, res_limit - res_base + 1, |
| 295 | memrange_type | IORESOURCE_ABOVE_4G); |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * This function initializes memranges for domain device. If the |
| 300 | * resource crosses 4G boundary, then this function splits it into two |
| 301 | * ranges -- one for the window below 4G and the other for the window |
| 302 | * above 4G. The latter range has IORESOURCE_ABOVE_4G flag set to |
| 303 | * satisfy resource requests from downstream devices for allocations |
| 304 | * above 4G. |
| 305 | */ |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 306 | static void initialize_domain_memranges(const struct device *dev, struct memranges *ranges, |
Nico Huber | 38aafa3 | 2022-09-04 22:20:21 +0200 | [diff] [blame] | 307 | unsigned long memrange_type) |
| 308 | { |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 309 | unsigned char align = get_alignment_by_resource_type(memrange_type); |
Nico Huber | 38aafa3 | 2022-09-04 22:20:21 +0200 | [diff] [blame] | 310 | |
| 311 | memranges_init_empty_with_alignment(ranges, NULL, 0, align); |
| 312 | |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 313 | struct resource *res; |
| 314 | for (res = dev->resource_list; res != NULL; res = res->next) { |
| 315 | if (is_resource_invalid(res)) |
| 316 | continue; |
| 317 | if (res->flags & IORESOURCE_FIXED) |
| 318 | continue; |
| 319 | if ((res->flags & IORESOURCE_TYPE_MASK) != memrange_type) |
| 320 | continue; |
Nico Huber | 38aafa3 | 2022-09-04 22:20:21 +0200 | [diff] [blame] | 321 | |
Arthur Heymans | d436b16 | 2023-05-23 22:47:05 +0200 | [diff] [blame] | 322 | printk(BIOS_DEBUG, "%s %s: base: %llx size: %llx align: %u gran: %u limit: %llx\n", |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 323 | dev_path(dev), resource2str(res), res->base, res->size, res->align, |
| 324 | res->gran, res->limit); |
| 325 | |
| 326 | if (res->flags & IORESOURCE_IO) |
| 327 | initialize_domain_io_resource_memranges(ranges, res, memrange_type); |
| 328 | else |
| 329 | initialize_domain_mem_resource_memranges(ranges, res, memrange_type); |
| 330 | } |
Nico Huber | 38aafa3 | 2022-09-04 22:20:21 +0200 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /* |
| 334 | * This function initializes memranges for bridge device. Unlike domain, |
| 335 | * bridge does not need to care about resource window crossing 4G |
| 336 | * boundary. This is handled by the resource allocator at domain level |
| 337 | * to ensure that all downstream bridges are allocated space either |
| 338 | * above or below 4G boundary as per the state of IORESOURCE_ABOVE_4G |
| 339 | * for the respective bridge resource. |
| 340 | * |
| 341 | * So, this function creates a single range of the entire resource |
| 342 | * window available for the bridge resource. Thus all downstream |
| 343 | * resources of the bridge for the given resource type get allocated |
| 344 | * space from the same window. If there is any downstream resource of |
| 345 | * the bridge which requests allocation above 4G, then all other |
| 346 | * downstream resources of the same type under the bridge get allocated |
| 347 | * above 4G. |
| 348 | */ |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 349 | static void initialize_bridge_memranges(const struct device *dev, struct memranges *ranges, |
Nico Huber | 38aafa3 | 2022-09-04 22:20:21 +0200 | [diff] [blame] | 350 | unsigned long memrange_type) |
| 351 | { |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 352 | unsigned char align = get_alignment_by_resource_type(memrange_type); |
Nico Huber | 38aafa3 | 2022-09-04 22:20:21 +0200 | [diff] [blame] | 353 | |
| 354 | memranges_init_empty_with_alignment(ranges, NULL, 0, align); |
| 355 | |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 356 | struct resource *res; |
| 357 | for (res = dev->resource_list; res != NULL; res = res->next) { |
| 358 | if (is_resource_invalid(res)) |
| 359 | continue; |
| 360 | if (res->flags & IORESOURCE_FIXED) |
| 361 | continue; |
| 362 | if ((res->flags & (IORESOURCE_TYPE_MASK | IORESOURCE_PREFETCH)) == memrange_type) |
| 363 | break; |
| 364 | } |
Nico Huber | 38aafa3 | 2022-09-04 22:20:21 +0200 | [diff] [blame] | 365 | |
| 366 | memranges_insert(ranges, res->base, res->limit - res->base + 1, memrange_type); |
| 367 | } |
| 368 | |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 369 | 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] | 370 | { |
| 371 | const struct range_entry *r; |
| 372 | |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 373 | printk(BIOS_INFO, " %s: Resource ranges:\n", dev_path(dev)); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 374 | |
| 375 | if (memranges_is_empty(ranges)) |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 376 | printk(BIOS_INFO, " * EMPTY!!\n"); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 377 | |
| 378 | memranges_each_entry(r, ranges) { |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 379 | printk(BIOS_INFO, " * Base: %llx, Size: %llx, Tag: %lx\n", |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 380 | range_entry_base(r), range_entry_size(r), range_entry_tag(r)); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 385 | * This is where the actual allocation of resources happens during |
| 386 | * pass 2. Given the list of memory ranges corresponding to the |
| 387 | * resource of given type, it finds the biggest unallocated resource |
| 388 | * using the type mask on the downstream bus. This continues in a |
| 389 | * descending order until all resources of given type are allocated |
| 390 | * address space within the current resource window. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 391 | */ |
| 392 | static void allocate_child_resources(struct bus *bus, struct memranges *ranges, |
| 393 | unsigned long type_mask, unsigned long type_match) |
| 394 | { |
Nico Huber | 526c642 | 2020-05-25 00:03:14 +0200 | [diff] [blame] | 395 | const bool allocate_top_down = |
| 396 | bus->dev->path.type == DEVICE_PATH_DOMAIN && |
| 397 | CONFIG(RESOURCE_ALLOCATION_TOP_DOWN); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 398 | struct resource *resource = NULL; |
| 399 | const struct device *dev; |
| 400 | |
| 401 | while ((dev = largest_resource(bus, &resource, type_mask, type_match))) { |
| 402 | |
| 403 | if (!resource->size) |
| 404 | continue; |
| 405 | |
Nico Huber | 38aafa3 | 2022-09-04 22:20:21 +0200 | [diff] [blame] | 406 | if (memranges_steal(ranges, resource->limit, resource->size, resource->align, |
| 407 | type_match, &resource->base, allocate_top_down) == false) { |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 408 | printk(BIOS_ERR, " ERROR: Resource didn't fit!!! "); |
| 409 | printk(BIOS_DEBUG, " %s %02lx * size: 0x%llx limit: %llx %s\n", |
Nico Huber | 38aafa3 | 2022-09-04 22:20:21 +0200 | [diff] [blame] | 410 | dev_path(dev), resource->index, |
| 411 | resource->size, resource->limit, resource2str(resource)); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 412 | continue; |
| 413 | } |
| 414 | |
| 415 | resource->limit = resource->base + resource->size - 1; |
| 416 | resource->flags |= IORESOURCE_ASSIGNED; |
| 417 | |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 418 | printk(BIOS_DEBUG, " %s %02lx * [0x%llx - 0x%llx] limit: %llx %s\n", |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 419 | dev_path(dev), resource->index, resource->base, |
| 420 | resource->size ? resource->base + resource->size - 1 : |
| 421 | resource->base, resource->limit, resource2str(resource)); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | static void update_constraints(struct memranges *ranges, const struct device *dev, |
| 426 | const struct resource *res) |
| 427 | { |
| 428 | if (!res->size) |
| 429 | return; |
| 430 | |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 431 | 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] | 432 | __func__, dev_path(dev), res->index, res->base, |
| 433 | res->base + res->size - 1, resource2str(res)); |
| 434 | |
| 435 | memranges_create_hole(ranges, res->base, res->size); |
| 436 | } |
| 437 | |
| 438 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 439 | * Scan the entire tree to identify any fixed resources allocated by |
| 440 | * any device to ensure that the address map for domain resources are |
| 441 | * appropriately updated. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 442 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 443 | * Domains can typically provide a memrange for entire address space. |
| 444 | * So, this function punches holes in the address space for all fixed |
| 445 | * resources that are already defined. Both I/O and normal memory |
| 446 | * resources are added as fixed. Both need to be removed from address |
| 447 | * space where dynamic resource allocations are sourced. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 448 | */ |
| 449 | static void avoid_fixed_resources(struct memranges *ranges, const struct device *dev, |
| 450 | unsigned long mask_match) |
| 451 | { |
| 452 | const struct resource *res; |
| 453 | const struct device *child; |
| 454 | const struct bus *bus; |
| 455 | |
| 456 | for (res = dev->resource_list; res != NULL; res = res->next) { |
| 457 | if ((res->flags & mask_match) != mask_match) |
| 458 | continue; |
| 459 | update_constraints(ranges, dev, res); |
| 460 | } |
| 461 | |
| 462 | bus = dev->link_list; |
| 463 | if (bus == NULL) |
| 464 | return; |
| 465 | |
| 466 | for (child = bus->children; child != NULL; child = child->sibling) |
| 467 | avoid_fixed_resources(ranges, child, mask_match); |
| 468 | } |
| 469 | |
| 470 | static void constrain_domain_resources(const struct device *domain, struct memranges *ranges, |
| 471 | unsigned long type) |
| 472 | { |
| 473 | unsigned long mask_match = type | IORESOURCE_FIXED; |
| 474 | |
| 475 | if (type == IORESOURCE_IO) { |
| 476 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 477 | * Don't allow allocations in the VGA I/O range. PCI has special |
| 478 | * cases for that. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 479 | */ |
Furquan Shaikh | 563e614 | 2020-05-26 12:04:35 -0700 | [diff] [blame] | 480 | memranges_create_hole(ranges, 0x3b0, 0x3df - 0x3b0 + 1); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 481 | |
| 482 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 483 | * Resource allocator no longer supports the legacy behavior where |
| 484 | * I/O resource allocation is guaranteed to avoid aliases over legacy |
| 485 | * PCI expansion card addresses. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 486 | */ |
| 487 | } |
| 488 | |
| 489 | avoid_fixed_resources(ranges, domain, mask_match); |
| 490 | } |
| 491 | |
| 492 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 493 | * This function creates a list of memranges of given type using the |
Nico Huber | 38aafa3 | 2022-09-04 22:20:21 +0200 | [diff] [blame] | 494 | * resource that is provided. If the given resource is NULL or if the |
| 495 | * resource window size is 0, then it creates an empty list. This |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 496 | * results in resource allocation for that resource type failing for |
| 497 | * all downstream devices since there is nothing to allocate from. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 498 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 499 | * In case of domain, it applies additional constraints to ensure that |
| 500 | * the memranges do not overlap any of the fixed resources under that |
| 501 | * domain. Domain typically seems to provide memrange for entire address |
| 502 | * space. Thus, it is up to the chipset to add DRAM and all other |
| 503 | * windows which cannot be used for resource allocation as fixed |
| 504 | * resources. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 505 | */ |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 506 | static void setup_resource_ranges(const struct device *dev, unsigned long type, |
| 507 | struct memranges *ranges) |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 508 | { |
Nico Huber | 38aafa3 | 2022-09-04 22:20:21 +0200 | [diff] [blame] | 509 | if (dev->path.type == DEVICE_PATH_DOMAIN) { |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 510 | initialize_domain_memranges(dev, ranges, type); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 511 | constrain_domain_resources(dev, ranges, type); |
Nico Huber | 38aafa3 | 2022-09-04 22:20:21 +0200 | [diff] [blame] | 512 | } else { |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 513 | initialize_bridge_memranges(dev, ranges, type); |
Nico Huber | 38aafa3 | 2022-09-04 22:20:21 +0200 | [diff] [blame] | 514 | } |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 515 | |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 516 | print_resource_ranges(dev, ranges); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 519 | 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] | 520 | { |
Arthur Heymans | d436b16 | 2023-05-23 22:47:05 +0200 | [diff] [blame] | 521 | 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] | 522 | dev_path(dev), resource2str(res), res->base, res->size, res->align, |
| 523 | res->gran, res->limit); |
| 524 | } |
| 525 | |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 526 | static void cleanup_domain_resource_ranges(const struct device *dev, struct memranges *ranges, |
| 527 | unsigned long type) |
| 528 | { |
| 529 | memranges_teardown(ranges); |
| 530 | for (struct resource *res = dev->resource_list; res != NULL; res = res->next) { |
| 531 | if (is_resource_invalid(res)) |
| 532 | continue; |
| 533 | if (res->flags & IORESOURCE_FIXED) |
| 534 | continue; |
| 535 | if ((res->flags & IORESOURCE_TYPE_MASK) != type) |
| 536 | continue; |
| 537 | print_resource_done(dev, res); |
| 538 | } |
| 539 | } |
| 540 | |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 541 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 542 | * Pass 2 of the resource allocator at the bridge level loops through |
| 543 | * all the resources for the bridge and generates a list of memory |
| 544 | * ranges similar to that at the domain level. However, there is no need |
| 545 | * to apply any additional constraints since the window allocated to the |
| 546 | * bridge is guaranteed to be non-overlapping by the allocator at domain |
| 547 | * level. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 548 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 549 | * Allocation at the bridge level works the same as at domain level |
| 550 | * (starts with the biggest resource requirement from downstream devices |
| 551 | * and continues in descending order). One major difference at the |
| 552 | * bridge level is that it considers prefmem resources separately from |
| 553 | * mem resources. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 554 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 555 | * Once allocation at the current bridge is complete, resource allocator |
| 556 | * continues walking down the downstream bridges until it hits the leaf |
| 557 | * devices. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 558 | */ |
| 559 | static void allocate_bridge_resources(const struct device *bridge) |
| 560 | { |
| 561 | struct memranges ranges; |
| 562 | const struct resource *res; |
| 563 | struct bus *bus = bridge->link_list; |
| 564 | unsigned long type_match; |
| 565 | struct device *child; |
| 566 | const unsigned long type_mask = IORESOURCE_TYPE_MASK | IORESOURCE_PREFETCH; |
| 567 | |
| 568 | for (res = bridge->resource_list; res; res = res->next) { |
| 569 | if (!res->size) |
| 570 | continue; |
| 571 | |
| 572 | if (!(res->flags & IORESOURCE_BRIDGE)) |
| 573 | continue; |
| 574 | |
| 575 | type_match = res->flags & type_mask; |
| 576 | |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 577 | setup_resource_ranges(bridge, type_match, &ranges); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 578 | allocate_child_resources(bus, &ranges, type_mask, type_match); |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 579 | print_resource_done(bridge, res); |
| 580 | memranges_teardown(&ranges); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | for (child = bus->children; child; child = child->sibling) { |
| 584 | if (!dev_has_children(child)) |
| 585 | continue; |
| 586 | |
| 587 | allocate_bridge_resources(child); |
| 588 | } |
| 589 | } |
| 590 | |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 591 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 592 | * Pass 2 of resource allocator begins at the domain level. Every domain |
| 593 | * has two types of resources - io and mem. For each of these resources, |
| 594 | * this function creates a list of memory ranges that can be used for |
| 595 | * downstream resource allocation. This list is constrained to remove |
| 596 | * any fixed resources in the domain sub-tree of the given resource |
| 597 | * type. It then uses the memory ranges to apply best fit on the |
| 598 | * resource requirements of the downstream devices. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 599 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 600 | * Once resources are allocated to all downstream devices of the domain, |
| 601 | * it walks down each downstream bridge to continue the same process |
| 602 | * until resources are allocated to all devices under the domain. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 603 | */ |
| 604 | static void allocate_domain_resources(const struct device *domain) |
| 605 | { |
| 606 | struct memranges ranges; |
| 607 | struct device *child; |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 608 | |
| 609 | /* Resource type I/O */ |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 610 | setup_resource_ranges(domain, IORESOURCE_IO, &ranges); |
| 611 | allocate_child_resources(domain->link_list, &ranges, IORESOURCE_TYPE_MASK, |
| 612 | IORESOURCE_IO); |
| 613 | cleanup_domain_resource_ranges(domain, &ranges, IORESOURCE_IO); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 614 | |
| 615 | /* |
| 616 | * Resource type Mem: |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 617 | * Domain does not distinguish between mem and prefmem resources. Thus, |
| 618 | * the resource allocation at domain level considers mem and prefmem |
| 619 | * together when finding the best fit based on the biggest resource |
| 620 | * requirement. |
Nico Huber | 38aafa3 | 2022-09-04 22:20:21 +0200 | [diff] [blame] | 621 | * |
| 622 | * However, resource requests for allocation above 4G boundary need to |
| 623 | * be handled separately if the domain resource window crosses this |
| 624 | * boundary. There is a single window for resource of type |
| 625 | * IORESOURCE_MEM. When creating memranges, this resource is split into |
| 626 | * two separate ranges -- one for the window below 4G boundary and other |
| 627 | * for the window above 4G boundary (with IORESOURCE_ABOVE_4G flag set). |
| 628 | * Thus, when allocating child resources, requests for below and above |
| 629 | * the 4G boundary are handled separately by setting the type_mask and |
| 630 | * type_match to allocate_child_resources() accordingly. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 631 | */ |
Arthur Heymans | 68b2b8f | 2022-12-19 15:04:50 +0100 | [diff] [blame] | 632 | setup_resource_ranges(domain, IORESOURCE_MEM, &ranges); |
| 633 | allocate_child_resources(domain->link_list, &ranges, |
| 634 | IORESOURCE_TYPE_MASK | IORESOURCE_ABOVE_4G, |
| 635 | IORESOURCE_MEM); |
| 636 | allocate_child_resources(domain->link_list, &ranges, |
| 637 | IORESOURCE_TYPE_MASK | IORESOURCE_ABOVE_4G, |
| 638 | IORESOURCE_MEM | IORESOURCE_ABOVE_4G); |
| 639 | cleanup_domain_resource_ranges(domain, &ranges, IORESOURCE_MEM); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 640 | |
| 641 | for (child = domain->link_list->children; child; child = child->sibling) { |
| 642 | if (!dev_has_children(child)) |
| 643 | continue; |
| 644 | |
| 645 | /* Continue allocation for all downstream bridges. */ |
| 646 | allocate_bridge_resources(child); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | /* |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 651 | * This function forms the guts of the resource allocator. It walks |
| 652 | * through the entire device tree for each domain two times. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 653 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 654 | * Every domain has a fixed set of ranges. These ranges cannot be |
| 655 | * relaxed based on the requirements of the downstream devices. They |
| 656 | * represent the available windows from which resources can be allocated |
| 657 | * to the different devices under the domain. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 658 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 659 | * In order to identify the requirements of downstream devices, resource |
| 660 | * allocator walks in a DFS fashion. It gathers the requirements from |
| 661 | * leaf devices and propagates those back up to their upstream bridges |
| 662 | * until the requirements for all the downstream devices of the domain |
| 663 | * 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] | 664 | * |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 665 | * Once the requirements for all the devices under the domain are |
| 666 | * gathered, the resource allocator walks a second time to allocate |
| 667 | * resources to downstream devices as per the requirements. It always |
| 668 | * picks the biggest resource request as per the type (i/o and mem) to |
| 669 | * allocate space from its fixed window to the immediate downstream |
| 670 | * device of the domain. In order to accomplish best fit for the |
| 671 | * resources, a list of ranges is maintained by each resource type (i/o |
| 672 | * and mem). At the domain level we don't differentiate between mem and |
| 673 | * prefmem. Since they are allocated space from the same window, the |
| 674 | * resource allocator at the domain level ensures that the biggest |
| 675 | * requirement is selected independent of the prefetch type. Once the |
| 676 | * resource allocation for all immediate downstream devices is complete |
| 677 | * at the domain level, the resource allocator walks down the subtree |
| 678 | * for each downstream bridge to continue the allocation process at the |
| 679 | * bridge level. Since bridges have separate windows for i/o, mem and |
| 680 | * prefmem, best fit algorithm at bridge level looks for the biggest |
| 681 | * requirement considering prefmem resources separately from non-prefmem |
| 682 | * resources. This continues until resource allocation is performed for |
| 683 | * all downstream bridges in the domain sub-tree. This is referred to as |
| 684 | * pass 2 of the resource allocator. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 685 | * |
| 686 | * Some rules that are followed by the resource allocator: |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 687 | * - Allocate resource locations for every device as long as |
| 688 | * the requirements can be satisfied. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 689 | * - Don't overlap with resources in fixed locations. |
Nico Huber | 9d7728a | 2020-05-23 18:00:10 +0200 | [diff] [blame] | 690 | * - Don't overlap and follow the rules of bridges -- downstream |
| 691 | * devices of bridges should use parts of the address space |
| 692 | * allocated to the bridge. |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 693 | */ |
| 694 | void allocate_resources(const struct device *root) |
| 695 | { |
| 696 | const struct device *child; |
| 697 | |
| 698 | if ((root == NULL) || (root->link_list == NULL)) |
| 699 | return; |
| 700 | |
| 701 | for (child = root->link_list->children; child; child = child->sibling) { |
| 702 | |
| 703 | if (child->path.type != DEVICE_PATH_DOMAIN) |
| 704 | continue; |
| 705 | |
| 706 | post_log_path(child); |
| 707 | |
| 708 | /* Pass 1 - Gather requirements. */ |
Paul Menzel | 2efcafa | 2021-07-02 17:39:45 +0200 | [diff] [blame] | 709 | printk(BIOS_INFO, "=== Resource allocator: %s - Pass 1 (gathering requirements) ===\n", |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 710 | dev_path(child)); |
| 711 | compute_domain_resources(child); |
| 712 | |
| 713 | /* Pass 2 - Allocate resources as per gathered requirements. */ |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 714 | printk(BIOS_INFO, "=== Resource allocator: %s - Pass 2 (allocating resources) ===\n", |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 715 | dev_path(child)); |
| 716 | allocate_domain_resources(child); |
Furquan Shaikh | c356861 | 2020-05-16 15:18:23 -0700 | [diff] [blame] | 717 | |
| 718 | printk(BIOS_INFO, "=== Resource allocator: %s - resource allocation complete ===\n", |
| 719 | dev_path(child)); |
Furquan Shaikh | f4bc9eb | 2020-05-15 16:04:28 -0700 | [diff] [blame] | 720 | } |
| 721 | } |