2013-03-23 02:44:46 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the coreboot project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Google, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <console/console.h>
|
|
|
|
#include <memrange.h>
|
|
|
|
|
|
|
|
static inline void range_entry_link(struct range_entry **prev_ptr,
|
2017-03-10 01:21:34 +01:00
|
|
|
struct range_entry *r)
|
2013-03-23 02:44:46 +01:00
|
|
|
{
|
|
|
|
r->next = *prev_ptr;
|
|
|
|
*prev_ptr = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void range_entry_unlink(struct range_entry **prev_ptr,
|
2017-03-10 01:21:34 +01:00
|
|
|
struct range_entry *r)
|
2013-03-23 02:44:46 +01:00
|
|
|
{
|
|
|
|
*prev_ptr = r->next;
|
|
|
|
r->next = NULL;
|
|
|
|
}
|
|
|
|
|
2016-01-16 04:59:37 +01:00
|
|
|
static inline void range_entry_unlink_and_free(struct memranges *ranges,
|
2017-03-10 01:21:34 +01:00
|
|
|
struct range_entry **prev_ptr,
|
|
|
|
struct range_entry *r)
|
2013-03-23 02:44:46 +01:00
|
|
|
{
|
|
|
|
range_entry_unlink(prev_ptr, r);
|
2016-01-16 04:59:37 +01:00
|
|
|
range_entry_link(&ranges->free_list, r);
|
2013-03-23 02:44:46 +01:00
|
|
|
}
|
|
|
|
|
2016-01-16 04:59:37 +01:00
|
|
|
static struct range_entry *alloc_range(struct memranges *ranges)
|
2013-03-23 02:44:46 +01:00
|
|
|
{
|
2016-01-16 04:59:37 +01:00
|
|
|
if (ranges->free_list != NULL) {
|
2013-03-23 02:44:46 +01:00
|
|
|
struct range_entry *r;
|
|
|
|
|
2016-01-16 04:59:37 +01:00
|
|
|
r = ranges->free_list;
|
|
|
|
range_entry_unlink(&ranges->free_list, r);
|
2013-03-23 02:44:46 +01:00
|
|
|
return r;
|
|
|
|
}
|
2016-01-16 04:59:37 +01:00
|
|
|
if (ENV_RAMSTAGE)
|
|
|
|
return malloc(sizeof(struct range_entry));
|
|
|
|
return NULL;
|
2013-03-23 02:44:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct range_entry *
|
2016-01-16 04:59:37 +01:00
|
|
|
range_list_add(struct memranges *ranges, struct range_entry **prev_ptr,
|
2017-03-10 01:21:34 +01:00
|
|
|
resource_t begin, resource_t end, unsigned long tag)
|
2013-03-23 02:44:46 +01:00
|
|
|
{
|
|
|
|
struct range_entry *new_entry;
|
|
|
|
|
2016-01-16 04:59:37 +01:00
|
|
|
new_entry = alloc_range(ranges);
|
2013-03-23 02:44:46 +01:00
|
|
|
if (new_entry == NULL) {
|
|
|
|
printk(BIOS_ERR, "Could not allocate range_entry!\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
new_entry->begin = begin;
|
|
|
|
new_entry->end = end;
|
|
|
|
new_entry->tag = tag;
|
|
|
|
range_entry_link(prev_ptr, new_entry);
|
|
|
|
|
|
|
|
return new_entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void merge_neighbor_entries(struct memranges *ranges)
|
|
|
|
{
|
|
|
|
struct range_entry *cur;
|
|
|
|
struct range_entry *prev;
|
|
|
|
|
|
|
|
prev = NULL;
|
|
|
|
/* Merge all neighbors and delete/free the leftover entry. */
|
|
|
|
for (cur = ranges->entries; cur != NULL; cur = cur->next) {
|
|
|
|
/* First entry. Just set prev. */
|
|
|
|
if (prev == NULL) {
|
|
|
|
prev = cur;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the previous entry merges with the current update the
|
|
|
|
* previous entry to cover full range and delete current from
|
|
|
|
* the list. */
|
|
|
|
if (prev->end + 1 >= cur->begin && prev->tag == cur->tag) {
|
|
|
|
prev->end = cur->end;
|
2016-01-16 04:59:37 +01:00
|
|
|
range_entry_unlink_and_free(ranges, &prev->next, cur);
|
2013-03-23 02:44:46 +01:00
|
|
|
/* Set cur to prev so cur->next is valid since cur
|
|
|
|
* was just unlinked and free. */
|
|
|
|
cur = prev;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
prev = cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_memranges(struct memranges *ranges,
|
2017-03-10 01:21:34 +01:00
|
|
|
resource_t begin, resource_t end,
|
|
|
|
unsigned long unused)
|
2013-03-23 02:44:46 +01:00
|
|
|
{
|
|
|
|
struct range_entry *cur;
|
|
|
|
struct range_entry *next;
|
|
|
|
struct range_entry **prev_ptr;
|
|
|
|
|
|
|
|
prev_ptr = &ranges->entries;
|
|
|
|
for (cur = ranges->entries; cur != NULL; cur = next) {
|
|
|
|
resource_t tmp_end;
|
|
|
|
|
|
|
|
/* Cache the next value to handle unlinks. */
|
|
|
|
next = cur->next;
|
|
|
|
|
|
|
|
/* No other ranges are affected. */
|
|
|
|
if (end < cur->begin)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* The removal range starts after this one. */
|
|
|
|
if (begin > cur->end) {
|
|
|
|
prev_ptr = &cur->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The removal range overlaps with the current entry either
|
|
|
|
* partially or fully. However, we need to adjust the removal
|
|
|
|
* range for any holes. */
|
|
|
|
if (begin <= cur->begin) {
|
|
|
|
begin = cur->begin;
|
|
|
|
|
|
|
|
/* Full removal. */
|
|
|
|
if (end >= cur->end) {
|
|
|
|
begin = cur->end + 1;
|
2016-01-16 04:59:37 +01:00
|
|
|
range_entry_unlink_and_free(ranges, prev_ptr,
|
2017-03-10 01:21:34 +01:00
|
|
|
cur);
|
2013-03-23 02:44:46 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* prev_ptr can be set now that the unlink path wasn't taken. */
|
|
|
|
prev_ptr = &cur->next;
|
|
|
|
|
|
|
|
/* Clip the end fragment to do proper splitting. */
|
|
|
|
tmp_end = end;
|
|
|
|
if (end > cur->end)
|
|
|
|
tmp_end = cur->end;
|
|
|
|
|
|
|
|
/* Hole punched in middle of entry. */
|
|
|
|
if (begin > cur->begin && tmp_end < cur->end) {
|
2016-01-16 04:59:37 +01:00
|
|
|
range_list_add(ranges, &cur->next, end + 1, cur->end,
|
2017-03-10 01:21:34 +01:00
|
|
|
cur->tag);
|
2013-03-23 02:44:46 +01:00
|
|
|
cur->end = begin - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Removal at beginning. */
|
|
|
|
if (begin == cur->begin)
|
|
|
|
cur->begin = tmp_end + 1;
|
|
|
|
|
|
|
|
/* Removal at end. */
|
|
|
|
if (tmp_end == cur->end)
|
|
|
|
cur->end = begin - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void merge_add_memranges(struct memranges *ranges,
|
2017-03-10 01:21:34 +01:00
|
|
|
resource_t begin, resource_t end,
|
|
|
|
unsigned long tag)
|
2013-03-23 02:44:46 +01:00
|
|
|
{
|
|
|
|
struct range_entry *cur;
|
|
|
|
struct range_entry **prev_ptr;
|
|
|
|
|
|
|
|
prev_ptr = &ranges->entries;
|
|
|
|
|
|
|
|
/* Remove all existing entries covered by the range. */
|
|
|
|
remove_memranges(ranges, begin, end, -1);
|
|
|
|
|
|
|
|
/* Find the entry to place the new entry after. Since
|
2013-07-10 05:51:14 +02:00
|
|
|
* remove_memranges() was called above there is a guaranteed
|
2013-03-23 02:44:46 +01:00
|
|
|
* spot for this new entry. */
|
|
|
|
for (cur = ranges->entries; cur != NULL; cur = cur->next) {
|
|
|
|
/* Found insertion spot before current entry. */
|
|
|
|
if (end < cur->begin)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Keep track of previous entry to insert new entry after it. */
|
|
|
|
prev_ptr = &cur->next;
|
|
|
|
|
|
|
|
/* The new entry starts after this one. */
|
|
|
|
if (begin > cur->end)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add new entry and merge with neighbors. */
|
2016-01-16 04:59:37 +01:00
|
|
|
range_list_add(ranges, prev_ptr, begin, end, tag);
|
2013-03-23 02:44:46 +01:00
|
|
|
merge_neighbor_entries(ranges);
|
|
|
|
}
|
|
|
|
|
2014-02-05 22:44:30 +01:00
|
|
|
void memranges_update_tag(struct memranges *ranges, unsigned long old_tag,
|
2017-03-10 01:21:34 +01:00
|
|
|
unsigned long new_tag)
|
2014-02-05 22:44:30 +01:00
|
|
|
{
|
|
|
|
struct range_entry *r;
|
|
|
|
|
|
|
|
memranges_each_entry(r, ranges) {
|
|
|
|
if (range_entry_tag(r) == old_tag)
|
|
|
|
range_entry_update_tag(r, new_tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
merge_neighbor_entries(ranges);
|
|
|
|
}
|
|
|
|
|
2013-03-23 02:44:46 +01:00
|
|
|
typedef void (*range_action_t)(struct memranges *ranges,
|
2017-03-10 01:21:34 +01:00
|
|
|
resource_t begin, resource_t end,
|
|
|
|
unsigned long tag);
|
2013-03-23 02:44:46 +01:00
|
|
|
|
|
|
|
static void do_action(struct memranges *ranges,
|
2017-03-10 01:21:34 +01:00
|
|
|
resource_t base, resource_t size, unsigned long tag,
|
|
|
|
range_action_t action)
|
2013-03-23 02:44:46 +01:00
|
|
|
{
|
|
|
|
resource_t end;
|
|
|
|
resource_t begin;
|
|
|
|
|
2014-07-18 19:25:54 +02:00
|
|
|
if (size == 0)
|
|
|
|
return;
|
|
|
|
|
2013-03-23 02:44:46 +01:00
|
|
|
/* The addresses are aligned to 4096 bytes: the begin address is
|
|
|
|
* aligned down while the end address is aligned up to be conservative
|
|
|
|
* about the full range covered. */
|
|
|
|
begin = ALIGN_DOWN(base, 4096);
|
|
|
|
end = begin + size + (base - begin);
|
|
|
|
end = ALIGN_UP(end, 4096) - 1;
|
|
|
|
action(ranges, begin, end, tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void memranges_create_hole(struct memranges *ranges,
|
2017-03-10 01:21:34 +01:00
|
|
|
resource_t base, resource_t size)
|
2013-03-23 02:44:46 +01:00
|
|
|
{
|
|
|
|
do_action(ranges, base, size, -1, remove_memranges);
|
|
|
|
}
|
|
|
|
|
|
|
|
void memranges_insert(struct memranges *ranges,
|
2017-03-10 01:21:34 +01:00
|
|
|
resource_t base, resource_t size, unsigned long tag)
|
2013-03-23 02:44:46 +01:00
|
|
|
{
|
|
|
|
do_action(ranges, base, size, tag, merge_add_memranges);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct collect_context {
|
|
|
|
struct memranges *ranges;
|
|
|
|
unsigned long tag;
|
2014-02-08 22:41:52 +01:00
|
|
|
memrange_filter_t filter;
|
2013-03-23 02:44:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void collect_ranges(void *gp, struct device *dev, struct resource *res)
|
|
|
|
{
|
|
|
|
struct collect_context *ctx = gp;
|
|
|
|
|
2014-02-05 23:38:29 +01:00
|
|
|
if (res->size == 0)
|
|
|
|
return;
|
|
|
|
|
2014-02-08 22:41:52 +01:00
|
|
|
if (ctx->filter == NULL || ctx->filter(dev, res))
|
|
|
|
memranges_insert(ctx->ranges, res->base, res->size, ctx->tag);
|
2013-03-23 02:44:46 +01:00
|
|
|
}
|
|
|
|
|
2014-02-08 22:41:52 +01:00
|
|
|
void memranges_add_resources_filter(struct memranges *ranges,
|
2017-03-10 01:21:34 +01:00
|
|
|
unsigned long mask, unsigned long match,
|
|
|
|
unsigned long tag,
|
|
|
|
memrange_filter_t filter)
|
2013-03-23 02:44:46 +01:00
|
|
|
{
|
|
|
|
struct collect_context context;
|
|
|
|
|
|
|
|
/* Only deal with MEM resources. */
|
|
|
|
mask |= IORESOURCE_MEM;
|
|
|
|
match |= IORESOURCE_MEM;
|
|
|
|
|
|
|
|
context.ranges = ranges;
|
|
|
|
context.tag = tag;
|
2014-02-08 22:41:52 +01:00
|
|
|
context.filter = filter;
|
2013-03-23 02:44:46 +01:00
|
|
|
search_global_resources(mask, match, collect_ranges, &context);
|
|
|
|
}
|
|
|
|
|
2014-02-08 22:41:52 +01:00
|
|
|
void memranges_add_resources(struct memranges *ranges,
|
2017-03-10 01:21:34 +01:00
|
|
|
unsigned long mask, unsigned long match,
|
|
|
|
unsigned long tag)
|
2014-02-08 22:41:52 +01:00
|
|
|
{
|
|
|
|
memranges_add_resources_filter(ranges, mask, match, tag, NULL);
|
|
|
|
}
|
|
|
|
|
2016-02-25 01:49:25 +01:00
|
|
|
void memranges_init_empty(struct memranges *ranges, struct range_entry *to_free,
|
2017-03-10 01:21:34 +01:00
|
|
|
size_t num_free)
|
2014-07-18 19:25:54 +02:00
|
|
|
{
|
2016-01-16 04:59:37 +01:00
|
|
|
size_t i;
|
|
|
|
|
2014-07-18 19:25:54 +02:00
|
|
|
ranges->entries = NULL;
|
2016-01-16 04:59:37 +01:00
|
|
|
ranges->free_list = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < num_free; i++)
|
2016-02-25 01:49:25 +01:00
|
|
|
range_entry_link(&ranges->free_list, &to_free[i]);
|
2014-07-18 19:25:54 +02:00
|
|
|
}
|
|
|
|
|
2013-03-23 02:44:46 +01:00
|
|
|
void memranges_init(struct memranges *ranges,
|
2017-03-10 01:21:34 +01:00
|
|
|
unsigned long mask, unsigned long match,
|
|
|
|
unsigned long tag)
|
2013-03-23 02:44:46 +01:00
|
|
|
{
|
2016-01-16 04:59:37 +01:00
|
|
|
memranges_init_empty(ranges, NULL, 0);
|
2013-03-23 02:44:46 +01:00
|
|
|
memranges_add_resources(ranges, mask, match, tag);
|
|
|
|
}
|
|
|
|
|
2018-04-10 09:31:10 +02:00
|
|
|
/* Clone a memrange. The new memrange has the same entries as the old one. */
|
|
|
|
void memranges_clone(struct memranges *newranges, struct memranges *oldranges)
|
|
|
|
{
|
|
|
|
struct range_entry *r, *cur;
|
|
|
|
struct range_entry **prev_ptr;
|
|
|
|
|
|
|
|
memranges_init_empty(newranges, NULL, 0);
|
|
|
|
|
|
|
|
prev_ptr = &newranges->entries;
|
|
|
|
memranges_each_entry(r, oldranges) {
|
|
|
|
cur = range_list_add(newranges, prev_ptr, r->begin, r->end,
|
|
|
|
r->tag);
|
|
|
|
prev_ptr = &cur->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-23 02:44:46 +01:00
|
|
|
void memranges_teardown(struct memranges *ranges)
|
|
|
|
{
|
|
|
|
while (ranges->entries != NULL) {
|
2016-01-16 04:59:37 +01:00
|
|
|
range_entry_unlink_and_free(ranges, &ranges->entries,
|
2017-03-10 01:21:34 +01:00
|
|
|
ranges->entries);
|
2013-03-23 02:44:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void memranges_fill_holes_up_to(struct memranges *ranges,
|
2017-03-10 01:21:34 +01:00
|
|
|
resource_t limit, unsigned long tag)
|
2013-03-23 02:44:46 +01:00
|
|
|
{
|
|
|
|
struct range_entry *cur;
|
|
|
|
struct range_entry *prev;
|
|
|
|
|
|
|
|
prev = NULL;
|
|
|
|
for (cur = ranges->entries; cur != NULL; cur = cur->next) {
|
|
|
|
/* First entry. Just set prev. */
|
|
|
|
if (prev == NULL) {
|
|
|
|
prev = cur;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-07-10 05:51:14 +02:00
|
|
|
/* If the previous entry does not directly precede the current
|
2013-03-23 02:44:46 +01:00
|
|
|
* entry then add a new entry just after the previous one. */
|
|
|
|
if (range_entry_end(prev) != cur->begin) {
|
|
|
|
resource_t end;
|
|
|
|
|
|
|
|
end = cur->begin - 1;
|
|
|
|
if (end >= limit)
|
|
|
|
end = limit - 1;
|
2016-01-16 04:59:37 +01:00
|
|
|
range_list_add(ranges, &prev->next,
|
2017-03-10 01:21:34 +01:00
|
|
|
range_entry_end(prev), end, tag);
|
2013-03-23 02:44:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
prev = cur;
|
|
|
|
|
|
|
|
/* Hit the requested range limit. No other entries after this
|
|
|
|
* are affected. */
|
|
|
|
if (cur->begin >= limit)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle the case where the limit was never reached. A new entry needs
|
|
|
|
* to be added to cover the range up to the limit. */
|
|
|
|
if (prev != NULL && range_entry_end(prev) < limit)
|
2016-01-16 04:59:37 +01:00
|
|
|
range_list_add(ranges, &prev->next, range_entry_end(prev),
|
2017-03-10 01:21:34 +01:00
|
|
|
limit - 1, tag);
|
2013-03-23 02:44:46 +01:00
|
|
|
|
|
|
|
/* Merge all entries that were newly added. */
|
|
|
|
merge_neighbor_entries(ranges);
|
|
|
|
}
|
2013-03-27 03:22:42 +01:00
|
|
|
|
|
|
|
struct range_entry *memranges_next_entry(struct memranges *ranges,
|
2017-03-10 01:21:34 +01:00
|
|
|
const struct range_entry *r)
|
2013-03-27 03:22:42 +01:00
|
|
|
{
|
|
|
|
return r->next;
|
|
|
|
}
|