From 4904802efc8eee37f95163f60823509e2a208af4 Mon Sep 17 00:00:00 2001 From: Aaron Durbin Date: Tue, 18 Feb 2014 21:55:02 -0600 Subject: coreboot: introduce notion of bootmem for memory map at boot The write_coreboot_table() in coreboot_table.c was already using struct memrange for managing and building up the entries that eventually go into the lb_memory table. Abstract that concept out to a bootmem memory map. The bootmem concept can then be used as a basis for loading payloads, for example. Change-Id: I7edbbca6bbd0568f658fde39ca93b126cab88367 Signed-off-by: Aaron Durbin Reviewed-on: http://review.coreboot.org/5302 Tested-by: build bot (Jenkins) Reviewed-by: Edward O'Callaghan Reviewed-by: Alexandru Gagniuc --- src/lib/Makefile.inc | 1 + src/lib/bootmem.c | 177 +++++++++++++++++++++++++++++++++++++++++++++++ src/lib/cbmem.c | 6 +- src/lib/coreboot_table.c | 90 +++--------------------- src/lib/dynamic_cbmem.c | 6 +- 5 files changed, 195 insertions(+), 85 deletions(-) create mode 100644 src/lib/bootmem.c (limited to 'src/lib') diff --git a/src/lib/Makefile.inc b/src/lib/Makefile.inc index 5c37329..cb75dbf 100644 --- a/src/lib/Makefile.inc +++ b/src/lib/Makefile.inc @@ -63,6 +63,7 @@ romstage-$(CONFIG_ARCH_X86) += gcc.c ramstage-y += hardwaremain.c ramstage-y += selfboot.c ramstage-y += coreboot_table.c +ramstage-y += bootmem.c ifneq ($(CONFIG_HAVE_ARCH_MEMSET),y) ramstage-y += memset.c endif diff --git a/src/lib/bootmem.c b/src/lib/bootmem.c new file mode 100644 index 0000000..f1bd7cc --- /dev/null +++ b/src/lib/bootmem.c @@ -0,0 +1,177 @@ +/* + * This file is part of the coreboot project. + * + * Copyright (C) 2003-2004 Eric Biederman + * Copyright (C) 2005-2010 coresystems GmbH + * Copyright (C) 2014 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. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include +#include + +static struct memranges bootmem; + +void bootmem_init(void) +{ + const unsigned long cacheable = IORESOURCE_CACHEABLE; + const unsigned long reserved = IORESOURCE_RESERVE; + struct memranges *bm = &bootmem; + + /* + * Fill the memory map out. The order of operations is important in + * that each overlapping range will take over the next. Therefore, + * add cacheable resources as RAM then add the reserved resources. + */ + memranges_init(bm, cacheable, cacheable, LB_MEM_RAM); + memranges_add_resources(bm, reserved, reserved, LB_MEM_RESERVED); + + /* Add memory used by CBMEM. */ + cbmem_add_bootmem(); +} + +void bootmem_add_range(uint64_t start, uint64_t size, uint32_t type) +{ + memranges_insert(&bootmem, start, size, type); +} + +void bootmem_write_memory_table(struct lb_memory *mem) +{ + const struct range_entry *r; + struct lb_memory_range *lb_r; + + lb_r = &mem->map[0]; + + bootmem_dump_ranges(); + + memranges_each_entry(r, &bootmem) { + lb_r->start = pack_lb64(range_entry_base(r)); + lb_r->size = pack_lb64(range_entry_size(r)); + lb_r->type = range_entry_tag(r); + + lb_r++; + mem->size += sizeof(struct lb_memory_range); + } +} + +struct range_strings { + unsigned long tag; + const char *str; +}; + +static const struct range_strings type_strings[] = { + { LB_MEM_RAM, "RAM" }, + { LB_MEM_RESERVED, "RESERVED" }, + { LB_MEM_ACPI, "ACPI" }, + { LB_MEM_NVS, "NVS" }, + { LB_MEM_UNUSABLE, "UNUSABLE" }, + { LB_MEM_VENDOR_RSVD, "VENDOR RESERVED" }, + { LB_MEM_TABLE, "CONFIGURATION TABLES" }, +}; + +static const char *bootmem_range_string(unsigned long tag) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(type_strings); i++) { + if (type_strings[i].tag == tag) + return type_strings[i].str; + } + + return "UNKNOWN!"; +} + +void bootmem_dump_ranges(void) +{ + int i; + const struct range_entry *r; + + i = 0; + memranges_each_entry(r, &bootmem) { + printk(BIOS_DEBUG, "%2d. %016llx-%016llx: %s\n", + i, range_entry_base(r), range_entry_end(r) - 1, + bootmem_range_string(range_entry_tag(r))); + i++; + } +} + +int bootmem_region_targets_usable_ram(uint64_t start, uint64_t size) +{ + const struct range_entry *r; + uint64_t end = start + size; + + memranges_each_entry(r, &bootmem) { + /* All further bootmem entries are beyond this range. */ + if (end <= range_entry_base(r)) + break; + + if (start >= range_entry_base(r) && end <= range_entry_end(r)) { + if (range_entry_tag(r) == LB_MEM_RAM) + return 1; + } + } + return 0; +} + +void *bootmem_allocate_buffer(size_t size) +{ + const struct range_entry *r; + const struct range_entry *region; + /* All allocated buffers fall below the 32-bit boundary. */ + const resource_t max_addr = 1ULL << 32; + resource_t begin; + resource_t end; + + /* 4KiB alignment. */ + size = ALIGN(size, 4096); + region = NULL; + memranges_each_entry(r, &bootmem) { + if (range_entry_size(r) < size) + continue; + + if (range_entry_tag(r) != LB_MEM_RAM) + continue; + + if (range_entry_base(r) >= max_addr) + continue; + + end = range_entry_end(r); + if (end > max_addr) + end = max_addr; + + if ((end - range_entry_base(r)) < size) + continue; + + region = r; + } + + if (region == NULL) + return NULL; + + /* region now points to the highest usable region for the given size. */ + begin = range_entry_base(region); + end = range_entry_end(region); + if (end > max_addr) + end = max_addr; + begin = end - size; + + /* Mark buffer as unusuable for future buffer use. */ + bootmem_add_range(begin, size, LB_MEM_UNUSABLE); + + return (void *)(uintptr_t)begin; +} diff --git a/src/lib/cbmem.c b/src/lib/cbmem.c index d97f6af..33fa721 100644 --- a/src/lib/cbmem.c +++ b/src/lib/cbmem.c @@ -19,9 +19,9 @@ #include #include +#include #include #include -#include #include #include #if CONFIG_HAVE_ACPI_RESUME && !defined(__PRE_RAM__) @@ -265,9 +265,9 @@ BOOT_STATE_INIT_ENTRIES(cbmem_bscb) = { init_cbmem_post_device, NULL), }; -void cbmem_add_lb_mem(struct lb_memory *mem) +void cbmem_add_bootmem(void) { - lb_add_memory_range(mem, LB_MEM_TABLE, cbmem_base, cbmem_size); + bootmem_add_range(cbmem_base, cbmem_size, LB_MEM_TABLE); } void cbmem_list(void) diff --git a/src/lib/coreboot_table.c b/src/lib/coreboot_table.c index 904185a..cb7179d 100644 --- a/src/lib/coreboot_table.c +++ b/src/lib/coreboot_table.c @@ -29,7 +29,7 @@ #include #include #include -#include +#include #if CONFIG_CHROMEOS #if CONFIG_GENERATE_ACPI_TABLES #include @@ -366,78 +366,11 @@ struct lb_memory *get_lb_mem(void) return mem_ranges; } -/* This structure keeps track of the coreboot table memory ranges. */ -static struct memranges lb_ranges; - -static struct lb_memory *build_lb_mem(struct lb_header *head) -{ - struct lb_memory *mem; - - /* Record where the lb memory ranges will live */ - mem = lb_memory(head); - mem_ranges = mem; - - /* Fill the memory map out. The order of operations is important in - * that each overlapping range will take over the next. Therefore, - * add cacheable resources as RAM then add the reserved resources. */ - memranges_init(&lb_ranges, IORESOURCE_CACHEABLE, - IORESOURCE_CACHEABLE, LB_MEM_RAM); - memranges_add_resources(&lb_ranges, IORESOURCE_RESERVE, - IORESOURCE_RESERVE, LB_MEM_RESERVED); - - return mem; -} - -static void commit_lb_memory(struct lb_memory *mem) -{ - struct range_entry *r; - struct lb_memory_range *lb_r; - int i; - - lb_r = &mem->map[0]; - i = 0; - - memranges_each_entry(r, &lb_ranges) { - const char *entry_type; - - switch (range_entry_tag(r)) { - case LB_MEM_RAM: entry_type="RAM"; break; - case LB_MEM_RESERVED: entry_type="RESERVED"; break; - case LB_MEM_ACPI: entry_type="ACPI"; break; - case LB_MEM_NVS: entry_type="NVS"; break; - case LB_MEM_UNUSABLE: entry_type="UNUSABLE"; break; - case LB_MEM_VENDOR_RSVD: entry_type="VENDOR RESERVED"; break; - case LB_MEM_TABLE: entry_type="CONFIGURATION TABLES"; break; - default: entry_type="UNKNOWN!"; break; - } - - printk(BIOS_DEBUG, "%2d. %016llx-%016llx: %s\n", - i, range_entry_base(r), range_entry_end(r)-1, - entry_type); - - lb_r->start = pack_lb64(range_entry_base(r)); - lb_r->size = pack_lb64(range_entry_size(r)); - lb_r->type = range_entry_tag(r); - - i++; - lb_r++; - mem->size += sizeof(struct lb_memory_range); - } -} - -void lb_add_memory_range(struct lb_memory *mem, - uint32_t type, uint64_t start, uint64_t size) -{ - memranges_insert(&lb_ranges, start, size, type); -} - - unsigned long write_coreboot_table( unsigned long low_table_start, unsigned long low_table_end, unsigned long rom_table_start, unsigned long rom_table_end) { struct lb_header *head; - struct lb_memory *mem; if (low_table_start || low_table_end) { printk(BIOS_DEBUG, "Writing table forward entry at 0x%08lx\n", @@ -476,30 +409,29 @@ unsigned long write_coreboot_table( } #endif - /* The Linux kernel assumes this region is reserved */ - /* Record where RAM is located */ - mem = build_lb_mem(head); + /* Initialize the memory map at boot time. */ + bootmem_init(); if (low_table_start || low_table_end) { + uint64_t size = low_table_end - low_table_start; /* Record the mptable and the the lb_table. * (This will be adjusted later) */ - lb_add_memory_range(mem, LB_MEM_TABLE, - low_table_start, low_table_end - low_table_start); + bootmem_add_range(low_table_start, size, LB_MEM_TABLE); } /* Record the pirq table, acpi tables, and maybe the mptable. However, * these only need to be added when the rom_table is sitting below * 1MiB. If it isn't that means high tables are being written. * The code below handles high tables correctly. */ - if (rom_table_end <= (1 << 20)) - lb_add_memory_range(mem, LB_MEM_TABLE, - rom_table_start, rom_table_end - rom_table_start); - - cbmem_add_lb_mem(mem); + if (rom_table_end <= (1 << 20)) { + uint64_t size = rom_table_end - rom_table_start; + bootmem_add_range(rom_table_start, size, LB_MEM_TABLE); + } /* No other memory areas can be added after the memory table has been * committed as the entries won't show up in the serialize mem table. */ - commit_lb_memory(mem); + mem_ranges = lb_memory(head); + bootmem_write_memory_table(mem_ranges); /* Record our motherboard */ lb_mainboard(head); diff --git a/src/lib/dynamic_cbmem.c b/src/lib/dynamic_cbmem.c index 0cc6295..322ead0 100644 --- a/src/lib/dynamic_cbmem.c +++ b/src/lib/dynamic_cbmem.c @@ -18,7 +18,7 @@ */ #include -#include +#include #include #include #include @@ -431,14 +431,14 @@ BOOT_STATE_INIT_ENTRIES(cbmem_bscb) = { init_cbmem_pre_device, NULL), }; -void cbmem_add_lb_mem(struct lb_memory *mem) +void cbmem_add_bootmem(void) { unsigned long base; unsigned long top; base = (unsigned long)cbmem_base(); top = (unsigned long)get_top_aligned(); - lb_add_memory_range(mem, LB_MEM_TABLE, base, top - base); + bootmem_add_range(base, top - base, LB_MEM_TABLE); } void cbmem_list(void) -- cgit v1.1