diff options
author | Furquan Shaikh <furquan@google.com> | 2020-03-11 19:06:24 -0700 |
---|---|---|
committer | Patrick Georgi <pgeorgi@google.com> | 2020-03-17 08:18:40 +0000 |
commit | 9c6274cd8fb1c9ee0eb674ce5945a05f818cb32e (patch) | |
tree | a117e3b70d4694c6f7149160b3aa944ea2392e28 /src/lib | |
parent | 1429092d02e8157c8e6c26849e0c8aa096da9c0c (diff) |
memrange: Add support for stealing required memory from given ranges
This change adds memranges_steal() which allows the user
to steal memory from the list of available ranges by providing a set
of constraints (limit, size, alignment, tag). It tries to find the
first big enough range that can satisfy the constraints, creates a
hole as per the request and returns base of the stolen memory.
BUG=b:149186922
Signed-off-by: Furquan Shaikh <furquan@google.com>
Change-Id: Ibe9cfae18fc6101ab2e7e27233e45324c8117708
Reviewed-on: https://review.coreboot.org/c/coreboot/+/39484
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Patrick Georgi <pgeorgi@google.com>
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/memrange.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/lib/memrange.c b/src/lib/memrange.c index 21fff003a1..b9c09e8782 100644 --- a/src/lib/memrange.c +++ b/src/lib/memrange.c @@ -391,3 +391,58 @@ struct range_entry *memranges_next_entry(struct memranges *ranges, { return r->next; } + +/* Find a range entry that satisfies the given constraints to fit a hole that matches the + * required alignment, is big enough, does not exceed the limit and has a matching tag. */ +static const struct range_entry *memranges_find_entry(struct memranges *ranges, + resource_t limit, resource_t size, + size_t align, unsigned long tag) +{ + const struct range_entry *r; + resource_t base, end; + + if (size == 0) + return NULL; + + if (!IS_POWER_OF_2(align)) + return NULL; + + if (!IS_ALIGNED(align, ranges->align)) + return NULL; + + memranges_each_entry(r, ranges) { + + if (r->tag != tag) + continue; + + base = ALIGN_UP(r->begin, align); + end = base + size - 1; + + if (end > r->end) + continue; + + if (end > limit) + continue; + + return r; + } + + return NULL; +} + +bool memranges_steal(struct memranges *ranges, resource_t limit, resource_t size, size_t align, + unsigned long tag, resource_t *stolen_base) +{ + resource_t base; + const struct range_entry *r = memranges_find_entry(ranges, limit, size, align, tag); + + if (r == NULL) + return false; + + base = ALIGN_UP(r->begin, align); + + memranges_create_hole(ranges, base, size); + *stolen_base = base; + + return true; +} |