diff options
Diffstat (limited to 'common/memory.c')
-rw-r--r-- | common/memory.c | 49 |
1 files changed, 45 insertions, 4 deletions
diff --git a/common/memory.c b/common/memory.c index 612ed87168..95995bb6e3 100644 --- a/common/memory.c +++ b/common/memory.c @@ -111,15 +111,56 @@ void *sbrk(ptrdiff_t increment) LIST_HEAD(memory_banks); +static int barebox_grow_memory_bank(struct memory_bank *bank, const char *name, + const struct resource *newres) +{ + struct resource *res; + resource_size_t bank_end = bank->res->end; + + if (newres->start < bank->start) { + res = request_iomem_region(name, newres->start, bank->start - 1); + if (IS_ERR(res)) + return PTR_ERR(res); + __merge_regions(name, bank->res, res); + } + + if (bank_end < newres->end) { + res = request_iomem_region(name, bank_end + 1, newres->end); + if (IS_ERR(res)) + return PTR_ERR(res); + __merge_regions(name, bank->res, res); + } + + bank->start = newres->start; + bank->size = resource_size(bank->res); + + return 0; +} + int barebox_add_memory_bank(const char *name, resource_size_t start, resource_size_t size) { - struct memory_bank *bank = xzalloc(sizeof(*bank)); + struct memory_bank *bank; + struct resource *res; + struct resource newres = { + .start = start, + .end = start + size - 1, + }; + + for_each_memory_bank(bank) { + if (resource_contains(bank->res, &newres)) + return 0; + if (resource_contains(&newres, bank->res)) + return barebox_grow_memory_bank(bank, name, &newres); + } + + res = request_iomem_region(name, start, start + size - 1); + if (IS_ERR(res)) + return PTR_ERR(res); - bank->res = request_iomem_region(name, start, start + size - 1); - if (IS_ERR(bank->res)) - return PTR_ERR(bank->res); + bank = xzalloc(sizeof(*bank)); + bank->res = res; bank->start = start; bank->size = size; |