blob: 6e4aa5ee7d9b68eeb897319405498430b0da972d [file] [log] [blame]
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -07001/* SPDX-License-Identifier: GPL-2.0-only */
2
Elyes Haouas04c3b5a2022-10-07 10:08:05 +02003#include <commonlib/bsd/helpers.h>
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -07004#include <console/console.h>
5#include <device/device.h>
6#include <memrange.h>
7#include <post.h>
Elyes Haouas04c3b5a2022-10-07 10:08:05 +02008#include <types.h>
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -07009
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070010static 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
21static 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 Shaikhc3568612020-05-16 15:18:23 -070027#define res_printk(depth, str, ...) printk(BIOS_DEBUG, "%*c"str, depth, ' ', __VA_ARGS__)
28
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070029/*
Nico Huber9d7728a2020-05-23 18:00:10 +020030 * 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 Shaikhf4bc9eb2020-05-15 16:04:28 -070034 * adding requirements in descending order.
35 *
Nico Huber9d7728a2020-05-23 18:00:10 +020036 * 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 Shaikhf4bc9eb2020-05-15 16:04:28 -070041 */
42static void update_bridge_resource(const struct device *bridge, struct resource *bridge_res,
Furquan Shaikhc3568612020-05-16 15:18:23 -070043 unsigned long type_match, int print_depth)
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070044{
45 const struct device *child;
46 struct resource *child_res;
47 resource_t base;
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070048 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 Huber9d7728a2020-05-23 18:00:10 +020054 * `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 Shaikhf4bc9eb2020-05-15 16:04:28 -070061 */
62 base = 0;
63
Furquan Shaikhc3568612020-05-16 15:18:23 -070064 res_printk(print_depth, "%s %s: size: %llx align: %d gran: %d limit: %llx\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070065 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 Huberec7b3132020-05-23 18:20:47 +020074 /* Resources with 0 limit can't be assigned anything. */
75 if (!child_res->limit)
76 continue;
77
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070078 /*
Nico Huber74169c12020-05-23 18:15:34 +020079 * Propagate the resource alignment to the bridge resource. The
80 * condition can only be true for the first (largest) resource. For all
Nico Huber9d7728a2020-05-23 18:00:10 +020081 * 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 Huber74169c12020-05-23 18:15:34 +020085 * 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 Shaikhf4bc9eb2020-05-15 16:04:28 -070088 */
Nico Huber74169c12020-05-23 18:15:34 +020089 if (child_res->align > bridge_res->align)
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070090 bridge_res->align = child_res->align;
91
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070092 /*
Nico Huberec7b3132020-05-23 18:20:47 +020093 * 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 Shaikhf4bc9eb2020-05-15 16:04:28 -070099 */
Nico Huber38aafa32022-09-04 22:20:21 +0200100 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 Shaikh1bb05ef302020-05-15 17:33:52 -0700116
117 /*
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700118 * Alignment value of 0 means that the child resource has no alignment
119 * requirements and so the base value remains unchanged here.
120 */
Nico Huberb3277042020-05-23 18:08:50 +0200121 base = ALIGN_UP(base, POWER_OF_2(child_res->align));
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700122
Furquan Shaikhc3568612020-05-16 15:18:23 -0700123 res_printk(print_depth + 1, "%s %02lx * [0x%llx - 0x%llx] %s\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700124 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 Huber9d7728a2020-05-23 18:00:10 +0200131 * 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 Shaikhf4bc9eb2020-05-15 16:04:28 -0700136 */
Nico Huberb3277042020-05-23 18:08:50 +0200137 bridge_res->size = ALIGN_UP(base, POWER_OF_2(bridge_res->gran));
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700138
Furquan Shaikhc3568612020-05-16 15:18:23 -0700139 res_printk(print_depth, "%s %s: size: %llx align: %d gran: %d limit: %llx done\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700140 dev_path(bridge), resource2str(bridge_res), bridge_res->size,
141 bridge_res->align, bridge_res->gran, bridge_res->limit);
142}
143
144/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200145 * 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 Shaikhf4bc9eb2020-05-15 16:04:28 -0700148 */
Furquan Shaikhc3568612020-05-16 15:18:23 -0700149static void compute_bridge_resources(const struct device *bridge, unsigned long type_match,
150 int print_depth)
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700151{
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 Shaikhc3568612020-05-16 15:18:23 -0700171 compute_bridge_resources(child, type_match, print_depth + 1);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700172 }
173
174 /*
175 * Update the window for current bridge resource now that all downstream
176 * requirements are gathered.
177 */
Furquan Shaikhc3568612020-05-16 15:18:23 -0700178 update_bridge_resource(bridge, res, type_match, print_depth);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700179 }
180}
181
182/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200183 * 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 Shaikhf4bc9eb2020-05-15 16:04:28 -0700188 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200189 * 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 Shaikhf4bc9eb2020-05-15 16:04:28 -0700193 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200194 * Domain resource windows are fixed ranges and hence requirement
195 * gathering does not result in any changes to these fixed ranges.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700196 */
197static void compute_domain_resources(const struct device *domain)
198{
199 const struct device *child;
Furquan Shaikhc3568612020-05-16 15:18:23 -0700200 const int print_depth = 1;
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700201
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 Shaikhc3568612020-05-16 15:18:23 -0700211 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 Shaikhf4bc9eb2020-05-15 16:04:28 -0700215 }
216}
217
218static unsigned char get_alignment_by_resource_type(const struct resource *res)
219{
220 if (res->flags & IORESOURCE_MEM)
221 return 12; /* Page-aligned --> log2(4KiB) */
222 else if (res->flags & IORESOURCE_IO)
223 return 0; /* No special alignment required --> log2(1) */
224
225 die("Unexpected resource type: flags(%d)!\n", res->flags);
226}
227
Nico Huber38aafa32022-09-04 22:20:21 +0200228/*
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 */
232static bool is_resource_invalid(const struct resource *res)
233{
234 return (res == NULL) || !(res->flags & IORESOURCE_ASSIGNED);
235}
236
237static 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
244static 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 */
306static void initialize_domain_memranges(struct memranges *ranges, const struct resource *res,
307 unsigned long memrange_type)
308{
309 unsigned char align = get_alignment_by_resource_type(res);
310
311 memranges_init_empty_with_alignment(ranges, NULL, 0, align);
312
313 if (is_resource_invalid(res))
314 return;
315
316 if (res->flags & IORESOURCE_IO)
317 initialize_domain_io_resource_memranges(ranges, res, memrange_type);
318 else
319 initialize_domain_mem_resource_memranges(ranges, res, memrange_type);
320}
321
322/*
323 * This function initializes memranges for bridge device. Unlike domain,
324 * bridge does not need to care about resource window crossing 4G
325 * boundary. This is handled by the resource allocator at domain level
326 * to ensure that all downstream bridges are allocated space either
327 * above or below 4G boundary as per the state of IORESOURCE_ABOVE_4G
328 * for the respective bridge resource.
329 *
330 * So, this function creates a single range of the entire resource
331 * window available for the bridge resource. Thus all downstream
332 * resources of the bridge for the given resource type get allocated
333 * space from the same window. If there is any downstream resource of
334 * the bridge which requests allocation above 4G, then all other
335 * downstream resources of the same type under the bridge get allocated
336 * above 4G.
337 */
338static void initialize_bridge_memranges(struct memranges *ranges, const struct resource *res,
339 unsigned long memrange_type)
340{
341 unsigned char align = get_alignment_by_resource_type(res);
342
343 memranges_init_empty_with_alignment(ranges, NULL, 0, align);
344
345 if (is_resource_invalid(res))
346 return;
347
348 memranges_insert(ranges, res->base, res->limit - res->base + 1, memrange_type);
349}
350
Furquan Shaikhc3568612020-05-16 15:18:23 -0700351static void print_resource_ranges(const struct device *dev, const struct memranges *ranges)
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700352{
353 const struct range_entry *r;
354
Furquan Shaikhc3568612020-05-16 15:18:23 -0700355 printk(BIOS_INFO, " %s: Resource ranges:\n", dev_path(dev));
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700356
357 if (memranges_is_empty(ranges))
Furquan Shaikhc3568612020-05-16 15:18:23 -0700358 printk(BIOS_INFO, " * EMPTY!!\n");
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700359
360 memranges_each_entry(r, ranges) {
Furquan Shaikhc3568612020-05-16 15:18:23 -0700361 printk(BIOS_INFO, " * Base: %llx, Size: %llx, Tag: %lx\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700362 range_entry_base(r), range_entry_size(r), range_entry_tag(r));
363 }
364}
365
366/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200367 * This is where the actual allocation of resources happens during
368 * pass 2. Given the list of memory ranges corresponding to the
369 * resource of given type, it finds the biggest unallocated resource
370 * using the type mask on the downstream bus. This continues in a
371 * descending order until all resources of given type are allocated
372 * address space within the current resource window.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700373 */
374static void allocate_child_resources(struct bus *bus, struct memranges *ranges,
375 unsigned long type_mask, unsigned long type_match)
376{
Nico Huber526c6422020-05-25 00:03:14 +0200377 const bool allocate_top_down =
378 bus->dev->path.type == DEVICE_PATH_DOMAIN &&
379 CONFIG(RESOURCE_ALLOCATION_TOP_DOWN);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700380 struct resource *resource = NULL;
381 const struct device *dev;
382
383 while ((dev = largest_resource(bus, &resource, type_mask, type_match))) {
384
385 if (!resource->size)
386 continue;
387
Nico Huber38aafa32022-09-04 22:20:21 +0200388 if (memranges_steal(ranges, resource->limit, resource->size, resource->align,
389 type_match, &resource->base, allocate_top_down) == false) {
Furquan Shaikhc3568612020-05-16 15:18:23 -0700390 printk(BIOS_ERR, " ERROR: Resource didn't fit!!! ");
391 printk(BIOS_DEBUG, " %s %02lx * size: 0x%llx limit: %llx %s\n",
Nico Huber38aafa32022-09-04 22:20:21 +0200392 dev_path(dev), resource->index,
393 resource->size, resource->limit, resource2str(resource));
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700394 continue;
395 }
396
397 resource->limit = resource->base + resource->size - 1;
398 resource->flags |= IORESOURCE_ASSIGNED;
399
Furquan Shaikhc3568612020-05-16 15:18:23 -0700400 printk(BIOS_DEBUG, " %s %02lx * [0x%llx - 0x%llx] limit: %llx %s\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700401 dev_path(dev), resource->index, resource->base,
402 resource->size ? resource->base + resource->size - 1 :
403 resource->base, resource->limit, resource2str(resource));
404 }
405}
406
407static void update_constraints(struct memranges *ranges, const struct device *dev,
408 const struct resource *res)
409{
410 if (!res->size)
411 return;
412
Furquan Shaikhc3568612020-05-16 15:18:23 -0700413 printk(BIOS_DEBUG, " %s: %s %02lx base %08llx limit %08llx %s (fixed)\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700414 __func__, dev_path(dev), res->index, res->base,
415 res->base + res->size - 1, resource2str(res));
416
417 memranges_create_hole(ranges, res->base, res->size);
418}
419
420/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200421 * Scan the entire tree to identify any fixed resources allocated by
422 * any device to ensure that the address map for domain resources are
423 * appropriately updated.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700424 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200425 * Domains can typically provide a memrange for entire address space.
426 * So, this function punches holes in the address space for all fixed
427 * resources that are already defined. Both I/O and normal memory
428 * resources are added as fixed. Both need to be removed from address
429 * space where dynamic resource allocations are sourced.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700430 */
431static void avoid_fixed_resources(struct memranges *ranges, const struct device *dev,
432 unsigned long mask_match)
433{
434 const struct resource *res;
435 const struct device *child;
436 const struct bus *bus;
437
438 for (res = dev->resource_list; res != NULL; res = res->next) {
439 if ((res->flags & mask_match) != mask_match)
440 continue;
441 update_constraints(ranges, dev, res);
442 }
443
444 bus = dev->link_list;
445 if (bus == NULL)
446 return;
447
448 for (child = bus->children; child != NULL; child = child->sibling)
449 avoid_fixed_resources(ranges, child, mask_match);
450}
451
452static void constrain_domain_resources(const struct device *domain, struct memranges *ranges,
453 unsigned long type)
454{
455 unsigned long mask_match = type | IORESOURCE_FIXED;
456
457 if (type == IORESOURCE_IO) {
458 /*
Nico Huber9d7728a2020-05-23 18:00:10 +0200459 * Don't allow allocations in the VGA I/O range. PCI has special
460 * cases for that.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700461 */
Furquan Shaikh563e6142020-05-26 12:04:35 -0700462 memranges_create_hole(ranges, 0x3b0, 0x3df - 0x3b0 + 1);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700463
464 /*
Nico Huber9d7728a2020-05-23 18:00:10 +0200465 * Resource allocator no longer supports the legacy behavior where
466 * I/O resource allocation is guaranteed to avoid aliases over legacy
467 * PCI expansion card addresses.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700468 */
469 }
470
471 avoid_fixed_resources(ranges, domain, mask_match);
472}
473
474/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200475 * This function creates a list of memranges of given type using the
Nico Huber38aafa32022-09-04 22:20:21 +0200476 * resource that is provided. If the given resource is NULL or if the
477 * resource window size is 0, then it creates an empty list. This
Nico Huber9d7728a2020-05-23 18:00:10 +0200478 * results in resource allocation for that resource type failing for
479 * all downstream devices since there is nothing to allocate from.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700480 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200481 * In case of domain, it applies additional constraints to ensure that
482 * the memranges do not overlap any of the fixed resources under that
483 * domain. Domain typically seems to provide memrange for entire address
484 * space. Thus, it is up to the chipset to add DRAM and all other
485 * windows which cannot be used for resource allocation as fixed
486 * resources.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700487 */
488static void setup_resource_ranges(const struct device *dev, const struct resource *res,
489 unsigned long type, struct memranges *ranges)
490{
Furquan Shaikhc0dc1e12020-05-16 13:54:37 -0700491 printk(BIOS_DEBUG, "%s %s: base: %llx size: %llx align: %d gran: %d limit: %llx\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700492 dev_path(dev), resource2str(res), res->base, res->size, res->align,
493 res->gran, res->limit);
494
Nico Huber38aafa32022-09-04 22:20:21 +0200495 if (dev->path.type == DEVICE_PATH_DOMAIN) {
496 initialize_domain_memranges(ranges, res, type);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700497 constrain_domain_resources(dev, ranges, type);
Nico Huber38aafa32022-09-04 22:20:21 +0200498 } else {
499 initialize_bridge_memranges(ranges, res, type);
500 }
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700501
Furquan Shaikhc3568612020-05-16 15:18:23 -0700502 print_resource_ranges(dev, ranges);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700503}
504
505static void cleanup_resource_ranges(const struct device *dev, struct memranges *ranges,
506 const struct resource *res)
507{
508 memranges_teardown(ranges);
Furquan Shaikhc0dc1e12020-05-16 13:54:37 -0700509 printk(BIOS_DEBUG, "%s %s: base: %llx size: %llx align: %d gran: %d limit: %llx done\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700510 dev_path(dev), resource2str(res), res->base, res->size, res->align,
511 res->gran, res->limit);
512}
513
514/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200515 * Pass 2 of the resource allocator at the bridge level loops through
516 * all the resources for the bridge and generates a list of memory
517 * ranges similar to that at the domain level. However, there is no need
518 * to apply any additional constraints since the window allocated to the
519 * bridge is guaranteed to be non-overlapping by the allocator at domain
520 * level.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700521 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200522 * Allocation at the bridge level works the same as at domain level
523 * (starts with the biggest resource requirement from downstream devices
524 * and continues in descending order). One major difference at the
525 * bridge level is that it considers prefmem resources separately from
526 * mem resources.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700527 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200528 * Once allocation at the current bridge is complete, resource allocator
529 * continues walking down the downstream bridges until it hits the leaf
530 * devices.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700531 */
532static void allocate_bridge_resources(const struct device *bridge)
533{
534 struct memranges ranges;
535 const struct resource *res;
536 struct bus *bus = bridge->link_list;
537 unsigned long type_match;
538 struct device *child;
539 const unsigned long type_mask = IORESOURCE_TYPE_MASK | IORESOURCE_PREFETCH;
540
541 for (res = bridge->resource_list; res; res = res->next) {
542 if (!res->size)
543 continue;
544
545 if (!(res->flags & IORESOURCE_BRIDGE))
546 continue;
547
548 type_match = res->flags & type_mask;
549
550 setup_resource_ranges(bridge, res, type_match, &ranges);
551 allocate_child_resources(bus, &ranges, type_mask, type_match);
552 cleanup_resource_ranges(bridge, &ranges, res);
553 }
554
555 for (child = bus->children; child; child = child->sibling) {
556 if (!dev_has_children(child))
557 continue;
558
559 allocate_bridge_resources(child);
560 }
561}
562
563static const struct resource *find_domain_resource(const struct device *domain,
564 unsigned long type)
565{
566 const struct resource *res;
567
568 for (res = domain->resource_list; res; res = res->next) {
569 if (res->flags & IORESOURCE_FIXED)
570 continue;
571
572 if ((res->flags & IORESOURCE_TYPE_MASK) == type)
573 return res;
574 }
575
576 return NULL;
577}
578
579/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200580 * Pass 2 of resource allocator begins at the domain level. Every domain
581 * has two types of resources - io and mem. For each of these resources,
582 * this function creates a list of memory ranges that can be used for
583 * downstream resource allocation. This list is constrained to remove
584 * any fixed resources in the domain sub-tree of the given resource
585 * type. It then uses the memory ranges to apply best fit on the
586 * resource requirements of the downstream devices.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700587 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200588 * Once resources are allocated to all downstream devices of the domain,
589 * it walks down each downstream bridge to continue the same process
590 * until resources are allocated to all devices under the domain.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700591 */
592static void allocate_domain_resources(const struct device *domain)
593{
594 struct memranges ranges;
595 struct device *child;
596 const struct resource *res;
597
598 /* Resource type I/O */
599 res = find_domain_resource(domain, IORESOURCE_IO);
600 if (res) {
601 setup_resource_ranges(domain, res, IORESOURCE_IO, &ranges);
602 allocate_child_resources(domain->link_list, &ranges, IORESOURCE_TYPE_MASK,
603 IORESOURCE_IO);
604 cleanup_resource_ranges(domain, &ranges, res);
605 }
606
607 /*
608 * Resource type Mem:
Nico Huber9d7728a2020-05-23 18:00:10 +0200609 * Domain does not distinguish between mem and prefmem resources. Thus,
610 * the resource allocation at domain level considers mem and prefmem
611 * together when finding the best fit based on the biggest resource
612 * requirement.
Nico Huber38aafa32022-09-04 22:20:21 +0200613 *
614 * However, resource requests for allocation above 4G boundary need to
615 * be handled separately if the domain resource window crosses this
616 * boundary. There is a single window for resource of type
617 * IORESOURCE_MEM. When creating memranges, this resource is split into
618 * two separate ranges -- one for the window below 4G boundary and other
619 * for the window above 4G boundary (with IORESOURCE_ABOVE_4G flag set).
620 * Thus, when allocating child resources, requests for below and above
621 * the 4G boundary are handled separately by setting the type_mask and
622 * type_match to allocate_child_resources() accordingly.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700623 */
624 res = find_domain_resource(domain, IORESOURCE_MEM);
625 if (res) {
626 setup_resource_ranges(domain, res, IORESOURCE_MEM, &ranges);
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700627 allocate_child_resources(domain->link_list, &ranges,
Nico Huber38aafa32022-09-04 22:20:21 +0200628 IORESOURCE_TYPE_MASK | IORESOURCE_ABOVE_4G,
629 IORESOURCE_MEM);
630 allocate_child_resources(domain->link_list, &ranges,
631 IORESOURCE_TYPE_MASK | IORESOURCE_ABOVE_4G,
632 IORESOURCE_MEM | IORESOURCE_ABOVE_4G);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700633 cleanup_resource_ranges(domain, &ranges, res);
634 }
635
636 for (child = domain->link_list->children; child; child = child->sibling) {
637 if (!dev_has_children(child))
638 continue;
639
640 /* Continue allocation for all downstream bridges. */
641 allocate_bridge_resources(child);
642 }
643}
644
645/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200646 * This function forms the guts of the resource allocator. It walks
647 * through the entire device tree for each domain two times.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700648 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200649 * Every domain has a fixed set of ranges. These ranges cannot be
650 * relaxed based on the requirements of the downstream devices. They
651 * represent the available windows from which resources can be allocated
652 * to the different devices under the domain.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700653 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200654 * In order to identify the requirements of downstream devices, resource
655 * allocator walks in a DFS fashion. It gathers the requirements from
656 * leaf devices and propagates those back up to their upstream bridges
657 * until the requirements for all the downstream devices of the domain
658 * are gathered. This is referred to as pass 1 of the resource allocator.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700659 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200660 * Once the requirements for all the devices under the domain are
661 * gathered, the resource allocator walks a second time to allocate
662 * resources to downstream devices as per the requirements. It always
663 * picks the biggest resource request as per the type (i/o and mem) to
664 * allocate space from its fixed window to the immediate downstream
665 * device of the domain. In order to accomplish best fit for the
666 * resources, a list of ranges is maintained by each resource type (i/o
667 * and mem). At the domain level we don't differentiate between mem and
668 * prefmem. Since they are allocated space from the same window, the
669 * resource allocator at the domain level ensures that the biggest
670 * requirement is selected independent of the prefetch type. Once the
671 * resource allocation for all immediate downstream devices is complete
672 * at the domain level, the resource allocator walks down the subtree
673 * for each downstream bridge to continue the allocation process at the
674 * bridge level. Since bridges have separate windows for i/o, mem and
675 * prefmem, best fit algorithm at bridge level looks for the biggest
676 * requirement considering prefmem resources separately from non-prefmem
677 * resources. This continues until resource allocation is performed for
678 * all downstream bridges in the domain sub-tree. This is referred to as
679 * pass 2 of the resource allocator.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700680 *
681 * Some rules that are followed by the resource allocator:
Nico Huber9d7728a2020-05-23 18:00:10 +0200682 * - Allocate resource locations for every device as long as
683 * the requirements can be satisfied.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700684 * - Don't overlap with resources in fixed locations.
Nico Huber9d7728a2020-05-23 18:00:10 +0200685 * - Don't overlap and follow the rules of bridges -- downstream
686 * devices of bridges should use parts of the address space
687 * allocated to the bridge.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700688 */
689void allocate_resources(const struct device *root)
690{
691 const struct device *child;
692
693 if ((root == NULL) || (root->link_list == NULL))
694 return;
695
696 for (child = root->link_list->children; child; child = child->sibling) {
697
698 if (child->path.type != DEVICE_PATH_DOMAIN)
699 continue;
700
701 post_log_path(child);
702
703 /* Pass 1 - Gather requirements. */
Paul Menzel2efcafa2021-07-02 17:39:45 +0200704 printk(BIOS_INFO, "=== Resource allocator: %s - Pass 1 (gathering requirements) ===\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700705 dev_path(child));
706 compute_domain_resources(child);
707
708 /* Pass 2 - Allocate resources as per gathered requirements. */
Furquan Shaikhc3568612020-05-16 15:18:23 -0700709 printk(BIOS_INFO, "=== Resource allocator: %s - Pass 2 (allocating resources) ===\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700710 dev_path(child));
711 allocate_domain_resources(child);
Furquan Shaikhc3568612020-05-16 15:18:23 -0700712
713 printk(BIOS_INFO, "=== Resource allocator: %s - resource allocation complete ===\n",
714 dev_path(child));
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700715 }
716}