blob: 707ccf0a960e507dbbf7291408838a053cd3524c [file] [log] [blame]
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -07001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <console/console.h>
4#include <device/device.h>
5#include <memrange.h>
6#include <post.h>
7
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -07008static const char *resource2str(const struct resource *res)
9{
10 if (res->flags & IORESOURCE_IO)
11 return "io";
12 if (res->flags & IORESOURCE_PREFETCH)
13 return "prefmem";
14 if (res->flags & IORESOURCE_MEM)
15 return "mem";
16 return "undefined";
17}
18
19static bool dev_has_children(const struct device *dev)
20{
21 const struct bus *bus = dev->link_list;
22 return bus && bus->children;
23}
24
Furquan Shaikhc3568612020-05-16 15:18:23 -070025#define res_printk(depth, str, ...) printk(BIOS_DEBUG, "%*c"str, depth, ' ', __VA_ARGS__)
26
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070027/*
Nico Huber9d7728a2020-05-23 18:00:10 +020028 * During pass 1, once all the requirements for downstream devices of a
29 * bridge are gathered, this function calculates the overall resource
30 * requirement for the bridge. It starts by picking the largest resource
31 * requirement downstream for the given resource type and works by
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070032 * adding requirements in descending order.
33 *
Nico Huber9d7728a2020-05-23 18:00:10 +020034 * Additionally, it takes alignment and limits of the downstream devices
35 * into consideration and ensures that they get propagated to the bridge
36 * resource. This is required to guarantee that the upstream bridge/
37 * domain honors the limit and alignment requirements for this bridge
38 * based on the tightest constraints downstream.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070039 */
40static void update_bridge_resource(const struct device *bridge, struct resource *bridge_res,
Furquan Shaikhc3568612020-05-16 15:18:23 -070041 unsigned long type_match, int print_depth)
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070042{
43 const struct device *child;
44 struct resource *child_res;
45 resource_t base;
46 bool first_child_res = true;
47 const unsigned long type_mask = IORESOURCE_TYPE_MASK | IORESOURCE_PREFETCH;
48 struct bus *bus = bridge->link_list;
49
50 child_res = NULL;
51
52 /*
Nico Huber9d7728a2020-05-23 18:00:10 +020053 * `base` keeps track of where the next allocation for child resources
54 * can take place from within the bridge resource window. Since the
55 * bridge resource window allocation is not performed yet, it can start
56 * at 0. Base gets updated every time a resource requirement is
57 * accounted for in the loop below. After scanning all these resources,
58 * base will indicate the total size requirement for the current bridge
59 * resource window.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070060 */
61 base = 0;
62
Furquan Shaikhc3568612020-05-16 15:18:23 -070063 res_printk(print_depth, "%s %s: size: %llx align: %d gran: %d limit: %llx\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070064 dev_path(bridge), resource2str(bridge_res), bridge_res->size,
65 bridge_res->align, bridge_res->gran, bridge_res->limit);
66
67 while ((child = largest_resource(bus, &child_res, type_mask, type_match))) {
68
69 /* Size 0 resources can be skipped. */
70 if (!child_res->size)
71 continue;
72
73 /*
Nico Huber9d7728a2020-05-23 18:00:10 +020074 * Propagate the resource alignment to the bridge resource if this is
75 * the first child resource with non-zero size being considered. For all
76 * other children resources, alignment is taken care of by updating the
77 * base to round up as per the child resource alignment. It is
78 * guaranteed that pass 2 follows the exact same method of picking the
79 * resource for allocation using largest_resource(). Thus, as long as
80 * the alignment for first child resource is propagated up to the bridge
81 * resource, it can be guaranteed that the alignment for all resources
82 * is appropriately met.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070083 */
84 if (first_child_res && (child_res->align > bridge_res->align))
85 bridge_res->align = child_res->align;
86
87 first_child_res = false;
88
89 /*
Nico Huber9d7728a2020-05-23 18:00:10 +020090 * Propagate the resource limit to the bridge resource only if child
91 * resource limit is non-zero. If a downstream device has stricter
92 * requirements w.r.t. limits for any resource, that constraint needs to
93 * be propagated back up to the downstream bridges of the domain. This
94 * guarantees that the resource allocation which starts at the domain
95 * level takes into account all these constraints thus working on a
96 * global view.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -070097 */
98 if (child_res->limit && (child_res->limit < bridge_res->limit))
99 bridge_res->limit = child_res->limit;
100
101 /*
Nico Huber9d7728a2020-05-23 18:00:10 +0200102 * Propagate the downstream resource request to allocate above 4G
103 * boundary to upstream bridge resource. This ensures that during
104 * pass 2, the resource allocator at domain level has a global view
105 * of all the downstream device requirements and thus address space
106 * is allocated as per updated flags in the bridge resource.
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700107 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200108 * Since the bridge resource is a single window, all the downstream
109 * resources of this bridge resource will be allocated in space above
110 * the 4G boundary.
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700111 */
112 if (child_res->flags & IORESOURCE_ABOVE_4G)
113 bridge_res->flags |= IORESOURCE_ABOVE_4G;
114
115 /*
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700116 * Alignment value of 0 means that the child resource has no alignment
117 * requirements and so the base value remains unchanged here.
118 */
Nico Huberb3277042020-05-23 18:08:50 +0200119 base = ALIGN_UP(base, POWER_OF_2(child_res->align));
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700120
Furquan Shaikhc3568612020-05-16 15:18:23 -0700121 res_printk(print_depth + 1, "%s %02lx * [0x%llx - 0x%llx] %s\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700122 dev_path(child), child_res->index, base, base + child_res->size - 1,
123 resource2str(child_res));
124
125 base += child_res->size;
126 }
127
128 /*
Nico Huber9d7728a2020-05-23 18:00:10 +0200129 * After all downstream device resources are scanned, `base` represents
130 * the total size requirement for the current bridge resource window.
131 * This size needs to be rounded up to the granularity requirement of
132 * the bridge to ensure that the upstream bridge/domain allocates big
133 * enough window.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700134 */
Nico Huberb3277042020-05-23 18:08:50 +0200135 bridge_res->size = ALIGN_UP(base, POWER_OF_2(bridge_res->gran));
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700136
Furquan Shaikhc3568612020-05-16 15:18:23 -0700137 res_printk(print_depth, "%s %s: size: %llx align: %d gran: %d limit: %llx done\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700138 dev_path(bridge), resource2str(bridge_res), bridge_res->size,
139 bridge_res->align, bridge_res->gran, bridge_res->limit);
140}
141
142/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200143 * During pass 1, at the bridge level, the resource allocator gathers
144 * requirements from downstream devices and updates its own resource
145 * windows for the provided resource type.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700146 */
Furquan Shaikhc3568612020-05-16 15:18:23 -0700147static void compute_bridge_resources(const struct device *bridge, unsigned long type_match,
148 int print_depth)
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700149{
150 const struct device *child;
151 struct resource *res;
152 struct bus *bus = bridge->link_list;
153 const unsigned long type_mask = IORESOURCE_TYPE_MASK | IORESOURCE_PREFETCH;
154
155 for (res = bridge->resource_list; res; res = res->next) {
156 if (!(res->flags & IORESOURCE_BRIDGE))
157 continue;
158
159 if ((res->flags & type_mask) != type_match)
160 continue;
161
162 /*
163 * Ensure that the resource requirements for all downstream bridges are
164 * gathered before updating the window for current bridge resource.
165 */
166 for (child = bus->children; child; child = child->sibling) {
167 if (!dev_has_children(child))
168 continue;
Furquan Shaikhc3568612020-05-16 15:18:23 -0700169 compute_bridge_resources(child, type_match, print_depth + 1);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700170 }
171
172 /*
173 * Update the window for current bridge resource now that all downstream
174 * requirements are gathered.
175 */
Furquan Shaikhc3568612020-05-16 15:18:23 -0700176 update_bridge_resource(bridge, res, type_match, print_depth);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700177 }
178}
179
180/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200181 * During pass 1, the resource allocator walks down the entire sub-tree
182 * of a domain. It gathers resource requirements for every downstream
183 * bridge by looking at the resource requests of its children. Thus, the
184 * requirement gathering begins at the leaf devices and is propagated
185 * back up to the downstream bridges of the domain.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700186 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200187 * At the domain level, it identifies every downstream bridge and walks
188 * down that bridge to gather requirements for each resource type i.e.
189 * i/o, mem and prefmem. Since bridges have separate windows for mem and
190 * prefmem, requirements for each need to be collected separately.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700191 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200192 * Domain resource windows are fixed ranges and hence requirement
193 * gathering does not result in any changes to these fixed ranges.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700194 */
195static void compute_domain_resources(const struct device *domain)
196{
197 const struct device *child;
Furquan Shaikhc3568612020-05-16 15:18:23 -0700198 const int print_depth = 1;
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700199
200 if (domain->link_list == NULL)
201 return;
202
203 for (child = domain->link_list->children; child; child = child->sibling) {
204
205 /* Skip if this is not a bridge or has no children under it. */
206 if (!dev_has_children(child))
207 continue;
208
Furquan Shaikhc3568612020-05-16 15:18:23 -0700209 compute_bridge_resources(child, IORESOURCE_IO, print_depth);
210 compute_bridge_resources(child, IORESOURCE_MEM, print_depth);
211 compute_bridge_resources(child, IORESOURCE_MEM | IORESOURCE_PREFETCH,
212 print_depth);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700213 }
214}
215
216static unsigned char get_alignment_by_resource_type(const struct resource *res)
217{
218 if (res->flags & IORESOURCE_MEM)
219 return 12; /* Page-aligned --> log2(4KiB) */
220 else if (res->flags & IORESOURCE_IO)
221 return 0; /* No special alignment required --> log2(1) */
222
223 die("Unexpected resource type: flags(%d)!\n", res->flags);
224}
225
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700226/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200227 * If the resource is NULL or if the resource is not assigned, then it
228 * cannot be used for allocation for downstream devices.
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700229 */
230static bool is_resource_invalid(const struct resource *res)
231{
232 return (res == NULL) || !(res->flags & IORESOURCE_ASSIGNED);
233}
234
235static void initialize_domain_io_resource_memranges(struct memranges *ranges,
236 const struct resource *res,
237 unsigned long memrange_type)
238{
239 memranges_insert(ranges, res->base, res->limit - res->base + 1, memrange_type);
240}
241
242static void initialize_domain_mem_resource_memranges(struct memranges *ranges,
243 const struct resource *res,
244 unsigned long memrange_type)
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700245{
246 resource_t res_base;
247 resource_t res_limit;
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700248
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700249 const resource_t limit_4g = 0xffffffff;
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700250
251 res_base = res->base;
252 res_limit = res->limit;
253
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700254 /*
Nico Huber9d7728a2020-05-23 18:00:10 +0200255 * Split the resource into two separate ranges if it crosses the 4G
256 * boundary. Memrange type is set differently to ensure that memrange
257 * does not merge these two ranges. For the range above 4G boundary,
258 * given memrange type is ORed with IORESOURCE_ABOVE_4G.
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700259 */
260 if (res_base <= limit_4g) {
261
262 resource_t range_limit;
263
264 /* Clip the resource limit at 4G boundary if necessary. */
265 range_limit = MIN(res_limit, limit_4g);
266 memranges_insert(ranges, res_base, range_limit - res_base + 1, memrange_type);
267
268 /*
Nico Huber9d7728a2020-05-23 18:00:10 +0200269 * If the resource lies completely below the 4G boundary, nothing more
270 * needs to be done.
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700271 */
272 if (res_limit <= limit_4g)
273 return;
274
275 /*
Nico Huber9d7728a2020-05-23 18:00:10 +0200276 * If the resource window crosses the 4G boundary, then update res_base
277 * to add another entry for the range above the boundary.
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700278 */
279 res_base = limit_4g + 1;
280 }
281
282 if (res_base > res_limit)
283 return;
284
285 /*
Nico Huber9d7728a2020-05-23 18:00:10 +0200286 * If resource lies completely above the 4G boundary or if the resource
287 * was clipped to add two separate ranges, the range above 4G boundary
288 * has the resource flag IORESOURCE_ABOVE_4G set. This allows domain to
289 * handle any downstream requests for resource allocation above 4G
290 * differently.
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700291 */
292 memranges_insert(ranges, res_base, res_limit - res_base + 1,
293 memrange_type | IORESOURCE_ABOVE_4G);
294}
295
296/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200297 * This function initializes memranges for domain device. If the
298 * resource crosses 4G boundary, then this function splits it into two
299 * ranges -- one for the window below 4G and the other for the window
300 * above 4G. The latter range has IORESOURCE_ABOVE_4G flag set to
301 * satisfy resource requests from downstream devices for allocations
302 * above 4G.
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700303 */
304static void initialize_domain_memranges(struct memranges *ranges, const struct resource *res,
305 unsigned long memrange_type)
306{
307 unsigned char align = get_alignment_by_resource_type(res);
308
309 memranges_init_empty_with_alignment(ranges, NULL, 0, align);
310
311 if (is_resource_invalid(res))
312 return;
313
314 if (res->flags & IORESOURCE_IO)
315 initialize_domain_io_resource_memranges(ranges, res, memrange_type);
316 else
317 initialize_domain_mem_resource_memranges(ranges, res, memrange_type);
318}
319
320/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200321 * This function initializes memranges for bridge device. Unlike domain,
322 * bridge does not need to care about resource window crossing 4G
323 * boundary. This is handled by the resource allocator at domain level
324 * to ensure that all downstream bridges are allocated space either
325 * above or below 4G boundary as per the state of IORESOURCE_ABOVE_4G
326 * for the respective bridge resource.
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700327 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200328 * So, this function creates a single range of the entire resource
329 * window available for the bridge resource. Thus all downstream
330 * resources of the bridge for the given resource type get allocated
331 * space from the same window. If there is any downstream resource of
332 * the bridge which requests allocation above 4G, then all other
333 * downstream resources of the same type under the bridge get allocated
334 * above 4G.
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700335 */
336static void initialize_bridge_memranges(struct memranges *ranges, const struct resource *res,
337 unsigned long memrange_type)
338{
339 unsigned char align = get_alignment_by_resource_type(res);
340
341 memranges_init_empty_with_alignment(ranges, NULL, 0, align);
342
343 if (is_resource_invalid(res))
344 return;
345
346 memranges_insert(ranges, res->base, res->limit - res->base + 1, memrange_type);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700347}
348
Furquan Shaikhc3568612020-05-16 15:18:23 -0700349static void print_resource_ranges(const struct device *dev, const struct memranges *ranges)
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700350{
351 const struct range_entry *r;
352
Furquan Shaikhc3568612020-05-16 15:18:23 -0700353 printk(BIOS_INFO, " %s: Resource ranges:\n", dev_path(dev));
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700354
355 if (memranges_is_empty(ranges))
Furquan Shaikhc3568612020-05-16 15:18:23 -0700356 printk(BIOS_INFO, " * EMPTY!!\n");
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700357
358 memranges_each_entry(r, ranges) {
Furquan Shaikhc3568612020-05-16 15:18:23 -0700359 printk(BIOS_INFO, " * Base: %llx, Size: %llx, Tag: %lx\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700360 range_entry_base(r), range_entry_size(r), range_entry_tag(r));
361 }
362}
363
364/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200365 * This is where the actual allocation of resources happens during
366 * pass 2. Given the list of memory ranges corresponding to the
367 * resource of given type, it finds the biggest unallocated resource
368 * using the type mask on the downstream bus. This continues in a
369 * descending order until all resources of given type are allocated
370 * address space within the current resource window.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700371 */
372static void allocate_child_resources(struct bus *bus, struct memranges *ranges,
373 unsigned long type_mask, unsigned long type_match)
374{
375 struct resource *resource = NULL;
376 const struct device *dev;
377
378 while ((dev = largest_resource(bus, &resource, type_mask, type_match))) {
379
380 if (!resource->size)
381 continue;
382
383 if (memranges_steal(ranges, resource->limit, resource->size, resource->align,
384 type_match, &resource->base) == false) {
Furquan Shaikhc3568612020-05-16 15:18:23 -0700385 printk(BIOS_ERR, " ERROR: Resource didn't fit!!! ");
386 printk(BIOS_DEBUG, " %s %02lx * size: 0x%llx limit: %llx %s\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700387 dev_path(dev), resource->index,
388 resource->size, resource->limit, resource2str(resource));
389 continue;
390 }
391
392 resource->limit = resource->base + resource->size - 1;
393 resource->flags |= IORESOURCE_ASSIGNED;
394
Furquan Shaikhc3568612020-05-16 15:18:23 -0700395 printk(BIOS_DEBUG, " %s %02lx * [0x%llx - 0x%llx] limit: %llx %s\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700396 dev_path(dev), resource->index, resource->base,
397 resource->size ? resource->base + resource->size - 1 :
398 resource->base, resource->limit, resource2str(resource));
399 }
400}
401
402static void update_constraints(struct memranges *ranges, const struct device *dev,
403 const struct resource *res)
404{
405 if (!res->size)
406 return;
407
Furquan Shaikhc3568612020-05-16 15:18:23 -0700408 printk(BIOS_DEBUG, " %s: %s %02lx base %08llx limit %08llx %s (fixed)\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700409 __func__, dev_path(dev), res->index, res->base,
410 res->base + res->size - 1, resource2str(res));
411
412 memranges_create_hole(ranges, res->base, res->size);
413}
414
415/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200416 * Scan the entire tree to identify any fixed resources allocated by
417 * any device to ensure that the address map for domain resources are
418 * appropriately updated.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700419 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200420 * Domains can typically provide a memrange for entire address space.
421 * So, this function punches holes in the address space for all fixed
422 * resources that are already defined. Both I/O and normal memory
423 * resources are added as fixed. Both need to be removed from address
424 * space where dynamic resource allocations are sourced.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700425 */
426static void avoid_fixed_resources(struct memranges *ranges, const struct device *dev,
427 unsigned long mask_match)
428{
429 const struct resource *res;
430 const struct device *child;
431 const struct bus *bus;
432
433 for (res = dev->resource_list; res != NULL; res = res->next) {
434 if ((res->flags & mask_match) != mask_match)
435 continue;
436 update_constraints(ranges, dev, res);
437 }
438
439 bus = dev->link_list;
440 if (bus == NULL)
441 return;
442
443 for (child = bus->children; child != NULL; child = child->sibling)
444 avoid_fixed_resources(ranges, child, mask_match);
445}
446
447static void constrain_domain_resources(const struct device *domain, struct memranges *ranges,
448 unsigned long type)
449{
450 unsigned long mask_match = type | IORESOURCE_FIXED;
451
452 if (type == IORESOURCE_IO) {
453 /*
Nico Huber9d7728a2020-05-23 18:00:10 +0200454 * Don't allow allocations in the VGA I/O range. PCI has special
455 * cases for that.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700456 */
Furquan Shaikh563e6142020-05-26 12:04:35 -0700457 memranges_create_hole(ranges, 0x3b0, 0x3df - 0x3b0 + 1);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700458
459 /*
Nico Huber9d7728a2020-05-23 18:00:10 +0200460 * Resource allocator no longer supports the legacy behavior where
461 * I/O resource allocation is guaranteed to avoid aliases over legacy
462 * PCI expansion card addresses.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700463 */
464 }
465
466 avoid_fixed_resources(ranges, domain, mask_match);
467}
468
469/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200470 * This function creates a list of memranges of given type using the
471 * resource that is provided. If the given resource is NULL or if the
472 * resource window size is 0, then it creates an empty list. This
473 * results in resource allocation for that resource type failing for
474 * all downstream devices since there is nothing to allocate from.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700475 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200476 * In case of domain, it applies additional constraints to ensure that
477 * the memranges do not overlap any of the fixed resources under that
478 * domain. Domain typically seems to provide memrange for entire address
479 * space. Thus, it is up to the chipset to add DRAM and all other
480 * windows which cannot be used for resource allocation as fixed
481 * resources.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700482 */
483static void setup_resource_ranges(const struct device *dev, const struct resource *res,
484 unsigned long type, struct memranges *ranges)
485{
Furquan Shaikhc0dc1e12020-05-16 13:54:37 -0700486 printk(BIOS_DEBUG, "%s %s: base: %llx size: %llx align: %d gran: %d limit: %llx\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700487 dev_path(dev), resource2str(res), res->base, res->size, res->align,
488 res->gran, res->limit);
489
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700490 if (dev->path.type == DEVICE_PATH_DOMAIN) {
491 initialize_domain_memranges(ranges, res, type);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700492 constrain_domain_resources(dev, ranges, type);
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700493 } else {
494 initialize_bridge_memranges(ranges, res, type);
495 }
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700496
Furquan Shaikhc3568612020-05-16 15:18:23 -0700497 print_resource_ranges(dev, ranges);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700498}
499
500static void cleanup_resource_ranges(const struct device *dev, struct memranges *ranges,
501 const struct resource *res)
502{
503 memranges_teardown(ranges);
Furquan Shaikhc0dc1e12020-05-16 13:54:37 -0700504 printk(BIOS_DEBUG, "%s %s: base: %llx size: %llx align: %d gran: %d limit: %llx done\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700505 dev_path(dev), resource2str(res), res->base, res->size, res->align,
506 res->gran, res->limit);
507}
508
509/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200510 * Pass 2 of the resource allocator at the bridge level loops through
511 * all the resources for the bridge and generates a list of memory
512 * ranges similar to that at the domain level. However, there is no need
513 * to apply any additional constraints since the window allocated to the
514 * bridge is guaranteed to be non-overlapping by the allocator at domain
515 * level.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700516 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200517 * Allocation at the bridge level works the same as at domain level
518 * (starts with the biggest resource requirement from downstream devices
519 * and continues in descending order). One major difference at the
520 * bridge level is that it considers prefmem resources separately from
521 * mem resources.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700522 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200523 * Once allocation at the current bridge is complete, resource allocator
524 * continues walking down the downstream bridges until it hits the leaf
525 * devices.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700526 */
527static void allocate_bridge_resources(const struct device *bridge)
528{
529 struct memranges ranges;
530 const struct resource *res;
531 struct bus *bus = bridge->link_list;
532 unsigned long type_match;
533 struct device *child;
534 const unsigned long type_mask = IORESOURCE_TYPE_MASK | IORESOURCE_PREFETCH;
535
536 for (res = bridge->resource_list; res; res = res->next) {
537 if (!res->size)
538 continue;
539
540 if (!(res->flags & IORESOURCE_BRIDGE))
541 continue;
542
543 type_match = res->flags & type_mask;
544
545 setup_resource_ranges(bridge, res, type_match, &ranges);
546 allocate_child_resources(bus, &ranges, type_mask, type_match);
547 cleanup_resource_ranges(bridge, &ranges, res);
548 }
549
550 for (child = bus->children; child; child = child->sibling) {
551 if (!dev_has_children(child))
552 continue;
553
554 allocate_bridge_resources(child);
555 }
556}
557
558static const struct resource *find_domain_resource(const struct device *domain,
559 unsigned long type)
560{
561 const struct resource *res;
562
563 for (res = domain->resource_list; res; res = res->next) {
564 if (res->flags & IORESOURCE_FIXED)
565 continue;
566
567 if ((res->flags & IORESOURCE_TYPE_MASK) == type)
568 return res;
569 }
570
571 return NULL;
572}
573
574/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200575 * Pass 2 of resource allocator begins at the domain level. Every domain
576 * has two types of resources - io and mem. For each of these resources,
577 * this function creates a list of memory ranges that can be used for
578 * downstream resource allocation. This list is constrained to remove
579 * any fixed resources in the domain sub-tree of the given resource
580 * type. It then uses the memory ranges to apply best fit on the
581 * resource requirements of the downstream devices.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700582 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200583 * Once resources are allocated to all downstream devices of the domain,
584 * it walks down each downstream bridge to continue the same process
585 * until resources are allocated to all devices under the domain.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700586 */
587static void allocate_domain_resources(const struct device *domain)
588{
589 struct memranges ranges;
590 struct device *child;
591 const struct resource *res;
592
593 /* Resource type I/O */
594 res = find_domain_resource(domain, IORESOURCE_IO);
595 if (res) {
596 setup_resource_ranges(domain, res, IORESOURCE_IO, &ranges);
597 allocate_child_resources(domain->link_list, &ranges, IORESOURCE_TYPE_MASK,
598 IORESOURCE_IO);
599 cleanup_resource_ranges(domain, &ranges, res);
600 }
601
602 /*
603 * Resource type Mem:
Nico Huber9d7728a2020-05-23 18:00:10 +0200604 * Domain does not distinguish between mem and prefmem resources. Thus,
605 * the resource allocation at domain level considers mem and prefmem
606 * together when finding the best fit based on the biggest resource
607 * requirement.
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700608 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200609 * However, resource requests for allocation above 4G boundary need to
610 * be handled separately if the domain resource window crosses this
611 * boundary. There is a single window for resource of type
612 * IORESOURCE_MEM. When creating memranges, this resource is split into
613 * two separate ranges -- one for the window below 4G boundary and other
614 * for the window above 4G boundary (with IORESOURCE_ABOVE_4G flag set).
615 * Thus, when allocating child resources, requests for below and above
616 * the 4G boundary are handled separately by setting the type_mask and
617 * type_match to allocate_child_resources() accordingly.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700618 */
619 res = find_domain_resource(domain, IORESOURCE_MEM);
620 if (res) {
621 setup_resource_ranges(domain, res, IORESOURCE_MEM, &ranges);
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700622 allocate_child_resources(domain->link_list, &ranges,
623 IORESOURCE_TYPE_MASK | IORESOURCE_ABOVE_4G,
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700624 IORESOURCE_MEM);
Furquan Shaikh1bb05ef302020-05-15 17:33:52 -0700625 allocate_child_resources(domain->link_list, &ranges,
626 IORESOURCE_TYPE_MASK | IORESOURCE_ABOVE_4G,
627 IORESOURCE_MEM | IORESOURCE_ABOVE_4G);
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700628 cleanup_resource_ranges(domain, &ranges, res);
629 }
630
631 for (child = domain->link_list->children; child; child = child->sibling) {
632 if (!dev_has_children(child))
633 continue;
634
635 /* Continue allocation for all downstream bridges. */
636 allocate_bridge_resources(child);
637 }
638}
639
640/*
Nico Huber9d7728a2020-05-23 18:00:10 +0200641 * This function forms the guts of the resource allocator. It walks
642 * through the entire device tree for each domain two times.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700643 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200644 * Every domain has a fixed set of ranges. These ranges cannot be
645 * relaxed based on the requirements of the downstream devices. They
646 * represent the available windows from which resources can be allocated
647 * to the different devices under the domain.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700648 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200649 * In order to identify the requirements of downstream devices, resource
650 * allocator walks in a DFS fashion. It gathers the requirements from
651 * leaf devices and propagates those back up to their upstream bridges
652 * until the requirements for all the downstream devices of the domain
653 * are gathered. This is referred to as pass 1 of the resource allocator.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700654 *
Nico Huber9d7728a2020-05-23 18:00:10 +0200655 * Once the requirements for all the devices under the domain are
656 * gathered, the resource allocator walks a second time to allocate
657 * resources to downstream devices as per the requirements. It always
658 * picks the biggest resource request as per the type (i/o and mem) to
659 * allocate space from its fixed window to the immediate downstream
660 * device of the domain. In order to accomplish best fit for the
661 * resources, a list of ranges is maintained by each resource type (i/o
662 * and mem). At the domain level we don't differentiate between mem and
663 * prefmem. Since they are allocated space from the same window, the
664 * resource allocator at the domain level ensures that the biggest
665 * requirement is selected independent of the prefetch type. Once the
666 * resource allocation for all immediate downstream devices is complete
667 * at the domain level, the resource allocator walks down the subtree
668 * for each downstream bridge to continue the allocation process at the
669 * bridge level. Since bridges have separate windows for i/o, mem and
670 * prefmem, best fit algorithm at bridge level looks for the biggest
671 * requirement considering prefmem resources separately from non-prefmem
672 * resources. This continues until resource allocation is performed for
673 * all downstream bridges in the domain sub-tree. This is referred to as
674 * pass 2 of the resource allocator.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700675 *
676 * Some rules that are followed by the resource allocator:
Nico Huber9d7728a2020-05-23 18:00:10 +0200677 * - Allocate resource locations for every device as long as
678 * the requirements can be satisfied.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700679 * - Don't overlap with resources in fixed locations.
Nico Huber9d7728a2020-05-23 18:00:10 +0200680 * - Don't overlap and follow the rules of bridges -- downstream
681 * devices of bridges should use parts of the address space
682 * allocated to the bridge.
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700683 */
684void allocate_resources(const struct device *root)
685{
686 const struct device *child;
687
688 if ((root == NULL) || (root->link_list == NULL))
689 return;
690
691 for (child = root->link_list->children; child; child = child->sibling) {
692
693 if (child->path.type != DEVICE_PATH_DOMAIN)
694 continue;
695
696 post_log_path(child);
697
698 /* Pass 1 - Gather requirements. */
Paul Menzel2efcafa2021-07-02 17:39:45 +0200699 printk(BIOS_INFO, "=== Resource allocator: %s - Pass 1 (gathering requirements) ===\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700700 dev_path(child));
701 compute_domain_resources(child);
702
703 /* Pass 2 - Allocate resources as per gathered requirements. */
Furquan Shaikhc3568612020-05-16 15:18:23 -0700704 printk(BIOS_INFO, "=== Resource allocator: %s - Pass 2 (allocating resources) ===\n",
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700705 dev_path(child));
706 allocate_domain_resources(child);
Furquan Shaikhc3568612020-05-16 15:18:23 -0700707
708 printk(BIOS_INFO, "=== Resource allocator: %s - resource allocation complete ===\n",
709 dev_path(child));
Furquan Shaikhf4bc9eb2020-05-15 16:04:28 -0700710 }
711}