Furquan Shaikh has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/39484 )
Change subject: memrange: Add support for stealing required memory from given ranges ......................................................................
memrange: Add support for stealing required memory from given ranges
This change adds support for 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 --- M src/include/memrange.h M src/lib/memrange.c 2 files changed, 60 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/84/39484/1
diff --git a/src/include/memrange.h b/src/include/memrange.h index fba77af..9b1d1d2 100644 --- a/src/include/memrange.h +++ b/src/include/memrange.h @@ -16,6 +16,7 @@ #define MEMRANGE_H_
#include <device/resource.h> +#include <stdbool.h>
/* A memranges structure consists of a list of range_entry(s). The structure * is exposed so that a memranges can be used on the stack if needed. */ @@ -166,4 +167,17 @@ /* Returns next entry after the provided entry. NULL if r is last. */ struct range_entry *memranges_next_entry(struct memranges *ranges, const struct range_entry *r); + +/* Steals memory from the available list in given ranges as per the constraints: + * limit = Limit beyond which the stolen memory cannot grow. + * size = Requested size for the stolen memory. + * align = Alignment requirements for the starting address of the stolen memory. + * tag = Use a range that matches the given tag. + * + * If it is not possible to fulfill the request as per given constraints, this function returns + * false. Else, it sets the base address of stolen memory in stolen_base, creates a hole of + * required size at the stolen_base and returns true to indicate success. */ +bool memranges_steal(struct memranges *ranges, resource_t limit, resource_t size, size_t align, + unsigned long tag, resource_t *stolen_base); + #endif /* MEMRANGE_H_ */ diff --git a/src/lib/memrange.c b/src/lib/memrange.c index a76f24c..b57c6cb 100644 --- a/src/lib/memrange.c +++ b/src/lib/memrange.c @@ -385,3 +385,49 @@ { 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; + + 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; +}