blob: 03cec3e673ba10e83f1e7483680f5a7e4867a32c [file] [log] [blame]
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -07001/* SPDX-License-Identifier: GPL-2.0-only */
2
Nico Huber117e4362020-05-23 19:15:36 +02003#include <stdint.h>
4#include <commonlib/helpers.h>
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -07005#include <console/console.h>
6#include <device/device.h>
7#include <memrange.h>
8#include <post.h>
9
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
Nico Huber117e4362020-05-23 19:15:36 +020027static 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 Shaikhc3568612020-05-16 15:18:23 -070038#define res_printk(depth, str, ...) printk(BIOS_DEBUG, "%*c"str, depth, ' ', __VA_ARGS__)
39
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070040/*
Nico Huber9d7728a2020-05-23 18:00:10 +020041 * 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 Shaikhf4bc9eb2020-05-15 16:04:28 -070045 * adding requirements in descending order.
46 *
Nico Huber9d7728a2020-05-23 18:00:10 +020047 * 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.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070052 */
53static void update_bridge_resource(const struct device *bridge, struct resource *bridge_res,
Furquan Shaikhc3568612020-05-16 15:18:23 -070054 unsigned long type_match, int print_depth)
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070055{
56 const struct device *child;
57 struct resource *child_res;
58 resource_t base;
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070059 const unsigned long type_mask = IORESOURCE_TYPE_MASK | IORESOURCE_PREFETCH;
60 struct bus *bus = bridge->link_list;
61
62 child_res = NULL;
63
64 /*
Nico Huber9d7728a2020-05-23 18:00:10 +020065 * `base` keeps track of where the next allocation for child resources
66 * can take place from within the bridge resource window. Since the
67 * bridge resource window allocation is not performed yet, it can start
68 * at 0. Base gets updated every time a resource requirement is
69 * accounted for in the loop below. After scanning all these resources,
70 * base will indicate the total size requirement for the current bridge
71 * resource window.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070072 */
73 base = 0;
74
Furquan Shaikhc3568612020-05-16 15:18:23 -070075 res_printk(print_depth, "%s %s: size: %llx align: %d gran: %d limit: %llx\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070076 dev_path(bridge), resource2str(bridge_res), bridge_res->size,
77 bridge_res->align, bridge_res->gran, bridge_res->limit);
78
79 while ((child = largest_resource(bus, &child_res, type_mask, type_match))) {
80
81 /* Size 0 resources can be skipped. */
82 if (!child_res->size)
83 continue;
84
Nico Huberec7b3132020-05-23 18:20:47 +020085 /* Resources with 0 limit can't be assigned anything. */
86 if (!child_res->limit)
87 continue;
88
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070089 /*
Nico Huber74169c12020-05-23 18:15:34 +020090 * Propagate the resource alignment to the bridge resource. The
91 * condition can only be true for the first (largest) resource. For all
Nico Huber9d7728a2020-05-23 18:00:10 +020092 * other children resources, alignment is taken care of by updating the
93 * base to round up as per the child resource alignment. It is
94 * guaranteed that pass 2 follows the exact same method of picking the
95 * resource for allocation using largest_resource(). Thus, as long as
Nico Huber74169c12020-05-23 18:15:34 +020096 * the alignment for the largest child resource is propagated up to the
97 * bridge resource, it can be guaranteed that the alignment for all
98 * resources is appropriately met.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070099 */
Nico Huber74169c12020-05-23 18:15:34 +0200100 if (child_res->align > bridge_res->align)
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700101 bridge_res->align = child_res->align;
102
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700103 /*
Nico Huberec7b3132020-05-23 18:20:47 +0200104 * Propagate the resource limit to the bridge resource. If a downstream
105 * device has stricter requirements w.r.t. limits for any resource, that
106 * constraint needs to be propagated back up to the downstream bridges
107 * of the domain. This guarantees that the resource allocation which
108 * starts at the domain level takes into account all these constraints
109 * thus working on a global view.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700110 */
Nico Huber117e4362020-05-23 19:15:36 +0200111 if (effective_limit(child_res) < bridge_res->limit)
112 bridge_res->limit = effective_limit(child_res);
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700113
114 /*
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700115 * Alignment value of 0 means that the child resource has no alignment
116 * requirements and so the base value remains unchanged here.
117 */
Nico Huberb3277042020-05-23 18:08:50 +0200118 base = ALIGN_UP(base, POWER_OF_2(child_res->align));
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700119
Furquan Shaikhc3568612020-05-16 15:18:23 -0700120 res_printk(print_depth + 1, "%s %02lx * [0x%llx - 0x%llx] %s\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700121 dev_path(child), child_res->index, base, base + child_res->size - 1,
122 resource2str(child_res));
123
124 base += child_res->size;
125 }
126
127 /*
Nico Huber9d7728a2020-05-23 18:00:10 +0200128 * After all downstream device resources are scanned, `base` represents
129 * the total size requirement for the current bridge resource window.
130 * This size needs to be rounded up to the granularity requirement of
131 * the bridge to ensure that the upstream bridge/domain allocates big
132 * enough window.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700133 */
Nico Huberb3277042020-05-23 18:08:50 +0200134 bridge_res->size = ALIGN_UP(base, POWER_OF_2(bridge_res->gran));
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700135
Furquan Shaikhc3568612020-05-16 15:18:23 -0700136 res_printk(print_depth, "%s %s: size: %llx align: %d gran: %d limit: %llx done\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700137 dev_path(bridge), resource2str(bridge_res), bridge_res->size,
138 bridge_res->align, bridge_res->gran, bridge_res->limit);
139}
140
141/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200142 * During pass 1, at the bridge level, the resource allocator gathers
143 * requirements from downstream devices and updates its own resource
144 * windows for the provided resource type.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700145 */
Furquan Shaikhc3568612020-05-16 15:18:23 -0700146static void compute_bridge_resources(const struct device *bridge, unsigned long type_match,
147 int print_depth)
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700148{
149 const struct device *child;
150 struct resource *res;
151 struct bus *bus = bridge->link_list;
152 const unsigned long type_mask = IORESOURCE_TYPE_MASK | IORESOURCE_PREFETCH;
153
154 for (res = bridge->resource_list; res; res = res->next) {
155 if (!(res->flags & IORESOURCE_BRIDGE))
156 continue;
157
158 if ((res->flags & type_mask) != type_match)
159 continue;
160
161 /*
162 * Ensure that the resource requirements for all downstream bridges are
163 * gathered before updating the window for current bridge resource.
164 */
165 for (child = bus->children; child; child = child->sibling) {
166 if (!dev_has_children(child))
167 continue;
Furquan Shaikhc3568612020-05-16 15:18:23 -0700168 compute_bridge_resources(child, type_match, print_depth + 1);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700169 }
170
171 /*
172 * Update the window for current bridge resource now that all downstream
173 * requirements are gathered.
174 */
Furquan Shaikhc3568612020-05-16 15:18:23 -0700175 update_bridge_resource(bridge, res, type_match, print_depth);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700176 }
177}
178
179/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200180 * During pass 1, the resource allocator walks down the entire sub-tree
181 * of a domain. It gathers resource requirements for every downstream
182 * bridge by looking at the resource requests of its children. Thus, the
183 * requirement gathering begins at the leaf devices and is propagated
184 * back up to the downstream bridges of the domain.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700185 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200186 * At the domain level, it identifies every downstream bridge and walks
187 * down that bridge to gather requirements for each resource type i.e.
188 * i/o, mem and prefmem. Since bridges have separate windows for mem and
189 * prefmem, requirements for each need to be collected separately.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700190 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200191 * Domain resource windows are fixed ranges and hence requirement
192 * gathering does not result in any changes to these fixed ranges.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700193 */
194static void compute_domain_resources(const struct device *domain)
195{
196 const struct device *child;
Furquan Shaikhc3568612020-05-16 15:18:23 -0700197 const int print_depth = 1;
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700198
199 if (domain->link_list == NULL)
200 return;
201
202 for (child = domain->link_list->children; child; child = child->sibling) {
203
204 /* Skip if this is not a bridge or has no children under it. */
205 if (!dev_has_children(child))
206 continue;
207
Furquan Shaikhc3568612020-05-16 15:18:23 -0700208 compute_bridge_resources(child, IORESOURCE_IO, print_depth);
209 compute_bridge_resources(child, IORESOURCE_MEM, print_depth);
210 compute_bridge_resources(child, IORESOURCE_MEM | IORESOURCE_PREFETCH,
211 print_depth);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700212 }
213}
214
215static unsigned char get_alignment_by_resource_type(const struct resource *res)
216{
217 if (res->flags & IORESOURCE_MEM)
218 return 12; /* Page-aligned --> log2(4KiB) */
219 else if (res->flags & IORESOURCE_IO)
220 return 0; /* No special alignment required --> log2(1) */
221
222 die("Unexpected resource type: flags(%d)!\n", res->flags);
223}
224
Furquan Shaikhc3568612020-05-16 15:18:23 -0700225static void print_resource_ranges(const struct device *dev, const struct memranges *ranges)
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700226{
227 const struct range_entry *r;
228
Furquan Shaikhc3568612020-05-16 15:18:23 -0700229 printk(BIOS_INFO, " %s: Resource ranges:\n", dev_path(dev));
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700230
231 if (memranges_is_empty(ranges))
Furquan Shaikhc3568612020-05-16 15:18:23 -0700232 printk(BIOS_INFO, " * EMPTY!!\n");
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700233
234 memranges_each_entry(r, ranges) {
Furquan Shaikhc3568612020-05-16 15:18:23 -0700235 printk(BIOS_INFO, " * Base: %llx, Size: %llx, Tag: %lx\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700236 range_entry_base(r), range_entry_size(r), range_entry_tag(r));
237 }
238}
239
240/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200241 * This is where the actual allocation of resources happens during
242 * pass 2. Given the list of memory ranges corresponding to the
243 * resource of given type, it finds the biggest unallocated resource
244 * using the type mask on the downstream bus. This continues in a
245 * descending order until all resources of given type are allocated
246 * address space within the current resource window.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700247 */
248static void allocate_child_resources(struct bus *bus, struct memranges *ranges,
249 unsigned long type_mask, unsigned long type_match)
250{
Nico Huber526c6422020-05-25 00:03:14 +0200251 const bool allocate_top_down =
252 bus->dev->path.type == DEVICE_PATH_DOMAIN &&
253 CONFIG(RESOURCE_ALLOCATION_TOP_DOWN);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700254 struct resource *resource = NULL;
255 const struct device *dev;
256
257 while ((dev = largest_resource(bus, &resource, type_mask, type_match))) {
258
259 if (!resource->size)
260 continue;
261
Nico Huber117e4362020-05-23 19:15:36 +0200262 if (memranges_steal(ranges, effective_limit(resource), resource->size,
263 resource->align, type_match, &resource->base,
264 allocate_top_down) == false) {
Furquan Shaikhc3568612020-05-16 15:18:23 -0700265 printk(BIOS_ERR, " ERROR: Resource didn't fit!!! ");
266 printk(BIOS_DEBUG, " %s %02lx * size: 0x%llx limit: %llx %s\n",
Nico Huber117e4362020-05-23 19:15:36 +0200267 dev_path(dev), resource->index, resource->size,
268 effective_limit(resource), resource2str(resource));
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700269 continue;
270 }
271
272 resource->limit = resource->base + resource->size - 1;
273 resource->flags |= IORESOURCE_ASSIGNED;
274
Furquan Shaikhc3568612020-05-16 15:18:23 -0700275 printk(BIOS_DEBUG, " %s %02lx * [0x%llx - 0x%llx] limit: %llx %s\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700276 dev_path(dev), resource->index, resource->base,
277 resource->size ? resource->base + resource->size - 1 :
278 resource->base, resource->limit, resource2str(resource));
279 }
280}
281
282static void update_constraints(struct memranges *ranges, const struct device *dev,
283 const struct resource *res)
284{
285 if (!res->size)
286 return;
287
Furquan Shaikhc3568612020-05-16 15:18:23 -0700288 printk(BIOS_DEBUG, " %s: %s %02lx base %08llx limit %08llx %s (fixed)\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700289 __func__, dev_path(dev), res->index, res->base,
290 res->base + res->size - 1, resource2str(res));
291
292 memranges_create_hole(ranges, res->base, res->size);
293}
294
295/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200296 * Scan the entire tree to identify any fixed resources allocated by
297 * any device to ensure that the address map for domain resources are
298 * appropriately updated.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700299 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200300 * Domains can typically provide a memrange for entire address space.
301 * So, this function punches holes in the address space for all fixed
302 * resources that are already defined. Both I/O and normal memory
303 * resources are added as fixed. Both need to be removed from address
304 * space where dynamic resource allocations are sourced.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700305 */
306static void avoid_fixed_resources(struct memranges *ranges, const struct device *dev,
307 unsigned long mask_match)
308{
309 const struct resource *res;
310 const struct device *child;
311 const struct bus *bus;
312
313 for (res = dev->resource_list; res != NULL; res = res->next) {
314 if ((res->flags & mask_match) != mask_match)
315 continue;
316 update_constraints(ranges, dev, res);
317 }
318
319 bus = dev->link_list;
320 if (bus == NULL)
321 return;
322
323 for (child = bus->children; child != NULL; child = child->sibling)
324 avoid_fixed_resources(ranges, child, mask_match);
325}
326
327static void constrain_domain_resources(const struct device *domain, struct memranges *ranges,
328 unsigned long type)
329{
330 unsigned long mask_match = type | IORESOURCE_FIXED;
331
332 if (type == IORESOURCE_IO) {
333 /*
Nico Huber9d7728a2020-05-23 18:00:10 +0200334 * Don't allow allocations in the VGA I/O range. PCI has special
335 * cases for that.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700336 */
Furquan Shaikh563e6142020-05-26 12:04:35 -0700337 memranges_create_hole(ranges, 0x3b0, 0x3df - 0x3b0 + 1);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700338
339 /*
Nico Huber9d7728a2020-05-23 18:00:10 +0200340 * Resource allocator no longer supports the legacy behavior where
341 * I/O resource allocation is guaranteed to avoid aliases over legacy
342 * PCI expansion card addresses.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700343 */
344 }
345
346 avoid_fixed_resources(ranges, domain, mask_match);
347}
348
349/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200350 * This function creates a list of memranges of given type using the
Nico Huber117e4362020-05-23 19:15:36 +0200351 * resource that is provided. If the given resource is unassigned or if
352 * the resource window size is 0, then it creates an empty list. This
Nico Huber9d7728a2020-05-23 18:00:10 +0200353 * results in resource allocation for that resource type failing for
354 * all downstream devices since there is nothing to allocate from.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700355 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200356 * In case of domain, it applies additional constraints to ensure that
357 * the memranges do not overlap any of the fixed resources under that
358 * domain. Domain typically seems to provide memrange for entire address
359 * space. Thus, it is up to the chipset to add DRAM and all other
360 * windows which cannot be used for resource allocation as fixed
361 * resources.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700362 */
363static void setup_resource_ranges(const struct device *dev, const struct resource *res,
364 unsigned long type, struct memranges *ranges)
365{
Nico Huber117e4362020-05-23 19:15:36 +0200366 const unsigned char alignment = get_alignment_by_resource_type(res);
367
Furquan Shaikhc0dc1e12020-05-16 13:54:37 -0700368 printk(BIOS_DEBUG, "%s %s: base: %llx size: %llx align: %d gran: %d limit: %llx\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700369 dev_path(dev), resource2str(res), res->base, res->size, res->align,
370 res->gran, res->limit);
371
Nico Huber117e4362020-05-23 19:15:36 +0200372 memranges_init_empty_with_alignment(ranges, NULL, 0, alignment);
373 if (res->flags & IORESOURCE_ASSIGNED)
374 memranges_insert(ranges, res->base, res->limit - res->base + 1, type);
375 if (dev->path.type == DEVICE_PATH_DOMAIN)
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700376 constrain_domain_resources(dev, ranges, type);
377
Furquan Shaikhc3568612020-05-16 15:18:23 -0700378 print_resource_ranges(dev, ranges);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700379}
380
381static void cleanup_resource_ranges(const struct device *dev, struct memranges *ranges,
382 const struct resource *res)
383{
384 memranges_teardown(ranges);
Furquan Shaikhc0dc1e12020-05-16 13:54:37 -0700385 printk(BIOS_DEBUG, "%s %s: base: %llx size: %llx align: %d gran: %d limit: %llx done\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700386 dev_path(dev), resource2str(res), res->base, res->size, res->align,
387 res->gran, res->limit);
388}
389
390/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200391 * Pass 2 of the resource allocator at the bridge level loops through
392 * all the resources for the bridge and generates a list of memory
393 * ranges similar to that at the domain level. However, there is no need
394 * to apply any additional constraints since the window allocated to the
395 * bridge is guaranteed to be non-overlapping by the allocator at domain
396 * level.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700397 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200398 * Allocation at the bridge level works the same as at domain level
399 * (starts with the biggest resource requirement from downstream devices
400 * and continues in descending order). One major difference at the
401 * bridge level is that it considers prefmem resources separately from
402 * mem resources.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700403 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200404 * Once allocation at the current bridge is complete, resource allocator
405 * continues walking down the downstream bridges until it hits the leaf
406 * devices.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700407 */
408static void allocate_bridge_resources(const struct device *bridge)
409{
410 struct memranges ranges;
411 const struct resource *res;
412 struct bus *bus = bridge->link_list;
413 unsigned long type_match;
414 struct device *child;
415 const unsigned long type_mask = IORESOURCE_TYPE_MASK | IORESOURCE_PREFETCH;
416
417 for (res = bridge->resource_list; res; res = res->next) {
418 if (!res->size)
419 continue;
420
421 if (!(res->flags & IORESOURCE_BRIDGE))
422 continue;
423
424 type_match = res->flags & type_mask;
425
426 setup_resource_ranges(bridge, res, type_match, &ranges);
427 allocate_child_resources(bus, &ranges, type_mask, type_match);
428 cleanup_resource_ranges(bridge, &ranges, res);
429 }
430
431 for (child = bus->children; child; child = child->sibling) {
432 if (!dev_has_children(child))
433 continue;
434
435 allocate_bridge_resources(child);
436 }
437}
438
439static const struct resource *find_domain_resource(const struct device *domain,
440 unsigned long type)
441{
442 const struct resource *res;
443
444 for (res = domain->resource_list; res; res = res->next) {
445 if (res->flags & IORESOURCE_FIXED)
446 continue;
447
448 if ((res->flags & IORESOURCE_TYPE_MASK) == type)
449 return res;
450 }
451
452 return NULL;
453}
454
455/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200456 * Pass 2 of resource allocator begins at the domain level. Every domain
457 * has two types of resources - io and mem. For each of these resources,
458 * this function creates a list of memory ranges that can be used for
459 * downstream resource allocation. This list is constrained to remove
460 * any fixed resources in the domain sub-tree of the given resource
461 * type. It then uses the memory ranges to apply best fit on the
462 * resource requirements of the downstream devices.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700463 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200464 * Once resources are allocated to all downstream devices of the domain,
465 * it walks down each downstream bridge to continue the same process
466 * until resources are allocated to all devices under the domain.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700467 */
468static void allocate_domain_resources(const struct device *domain)
469{
470 struct memranges ranges;
471 struct device *child;
472 const struct resource *res;
473
474 /* Resource type I/O */
475 res = find_domain_resource(domain, IORESOURCE_IO);
476 if (res) {
477 setup_resource_ranges(domain, res, IORESOURCE_IO, &ranges);
478 allocate_child_resources(domain->link_list, &ranges, IORESOURCE_TYPE_MASK,
479 IORESOURCE_IO);
480 cleanup_resource_ranges(domain, &ranges, res);
481 }
482
483 /*
484 * Resource type Mem:
Nico Huber9d7728a2020-05-23 18:00:10 +0200485 * Domain does not distinguish between mem and prefmem resources. Thus,
486 * the resource allocation at domain level considers mem and prefmem
487 * together when finding the best fit based on the biggest resource
488 * requirement.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700489 */
490 res = find_domain_resource(domain, IORESOURCE_MEM);
491 if (res) {
492 setup_resource_ranges(domain, res, IORESOURCE_MEM, &ranges);
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700493 allocate_child_resources(domain->link_list, &ranges,
Nico Huber117e4362020-05-23 19:15:36 +0200494 IORESOURCE_TYPE_MASK, IORESOURCE_MEM);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700495 cleanup_resource_ranges(domain, &ranges, res);
496 }
497
498 for (child = domain->link_list->children; child; child = child->sibling) {
499 if (!dev_has_children(child))
500 continue;
501
502 /* Continue allocation for all downstream bridges. */
503 allocate_bridge_resources(child);
504 }
505}
506
507/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200508 * This function forms the guts of the resource allocator. It walks
509 * through the entire device tree for each domain two times.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700510 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200511 * Every domain has a fixed set of ranges. These ranges cannot be
512 * relaxed based on the requirements of the downstream devices. They
513 * represent the available windows from which resources can be allocated
514 * to the different devices under the domain.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700515 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200516 * In order to identify the requirements of downstream devices, resource
517 * allocator walks in a DFS fashion. It gathers the requirements from
518 * leaf devices and propagates those back up to their upstream bridges
519 * until the requirements for all the downstream devices of the domain
520 * are gathered. This is referred to as pass 1 of the resource allocator.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700521 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200522 * Once the requirements for all the devices under the domain are
523 * gathered, the resource allocator walks a second time to allocate
524 * resources to downstream devices as per the requirements. It always
525 * picks the biggest resource request as per the type (i/o and mem) to
526 * allocate space from its fixed window to the immediate downstream
527 * device of the domain. In order to accomplish best fit for the
528 * resources, a list of ranges is maintained by each resource type (i/o
529 * and mem). At the domain level we don't differentiate between mem and
530 * prefmem. Since they are allocated space from the same window, the
531 * resource allocator at the domain level ensures that the biggest
532 * requirement is selected independent of the prefetch type. Once the
533 * resource allocation for all immediate downstream devices is complete
534 * at the domain level, the resource allocator walks down the subtree
535 * for each downstream bridge to continue the allocation process at the
536 * bridge level. Since bridges have separate windows for i/o, mem and
537 * prefmem, best fit algorithm at bridge level looks for the biggest
538 * requirement considering prefmem resources separately from non-prefmem
539 * resources. This continues until resource allocation is performed for
540 * all downstream bridges in the domain sub-tree. This is referred to as
541 * pass 2 of the resource allocator.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700542 *
543 * Some rules that are followed by the resource allocator:
Nico Huber9d7728a2020-05-23 18:00:10 +0200544 * - Allocate resource locations for every device as long as
545 * the requirements can be satisfied.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700546 * - Don't overlap with resources in fixed locations.
Nico Huber9d7728a2020-05-23 18:00:10 +0200547 * - Don't overlap and follow the rules of bridges -- downstream
548 * devices of bridges should use parts of the address space
549 * allocated to the bridge.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700550 */
551void allocate_resources(const struct device *root)
552{
553 const struct device *child;
554
555 if ((root == NULL) || (root->link_list == NULL))
556 return;
557
558 for (child = root->link_list->children; child; child = child->sibling) {
559
560 if (child->path.type != DEVICE_PATH_DOMAIN)
561 continue;
562
563 post_log_path(child);
564
565 /* Pass 1 - Gather requirements. */
Paul Menzel2efcafa2021-07-02 17:39:45 +0200566 printk(BIOS_INFO, "=== Resource allocator: %s - Pass 1 (gathering requirements) ===\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700567 dev_path(child));
568 compute_domain_resources(child);
569
570 /* Pass 2 - Allocate resources as per gathered requirements. */
Furquan Shaikhc3568612020-05-16 15:18:23 -0700571 printk(BIOS_INFO, "=== Resource allocator: %s - Pass 2 (allocating resources) ===\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700572 dev_path(child));
573 allocate_domain_resources(child);
Furquan Shaikhc3568612020-05-16 15:18:23 -0700574
575 printk(BIOS_INFO, "=== Resource allocator: %s - resource allocation complete ===\n",
576 dev_path(child));
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700577 }
578}