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 <adurbin@chromium.org>
Reviewed-on: http://review.coreboot.org/5302
Tested-by: build bot (Jenkins)
Reviewed-by: Edward O'Callaghan <eocallaghan@alterapraxis.com>
Reviewed-by: Alexandru Gagniuc <mr.nuke.me@gmail.com>
This commit is contained in:
Aaron Durbin 2014-02-18 21:55:02 -06:00 committed by Aaron Durbin
parent c7db28c580
commit 4904802efc
8 changed files with 245 additions and 91 deletions

View File

@ -338,9 +338,6 @@ unsigned long write_coreboot_table(
unsigned long low_table_start, unsigned long low_table_end, unsigned long low_table_start, unsigned long low_table_end,
unsigned long rom_table_start, unsigned long rom_table_end); unsigned long rom_table_start, unsigned long rom_table_end);
void lb_add_memory_range(struct lb_memory *mem,
uint32_t type, uint64_t start, uint64_t size);
/* Routines to extract part so the coreboot table or information /* Routines to extract part so the coreboot table or information
* from the coreboot table. * from the coreboot table.
*/ */

48
src/include/bootmem.h Normal file
View File

@ -0,0 +1,48 @@
/*
* This file is part of the coreboot project.
*
* 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
*/
#ifndef BOOTMEM_H
#define BOOTMEM_H
#include <memrange.h>
#include <stdint.h>
#include <boot/coreboot_tables.h>
/*
* Initialize the memory address space prior to payload loading. The bootmem
* serves as the source for the lb_mem table.
*/
void bootmem_init(void);
/* Add a range of a given type to the bootmem address space. */
void bootmem_add_range(uint64_t start, uint64_t size, uint32_t type);
/* Write memory coreboot table. */
void bootmem_write_memory_table(struct lb_memory *mem);
/* Print current range map of boot memory. */
void bootmem_dump_ranges(void);
/* Return 1 if region targets usable RAM, 0 otherwise. */
int bootmem_region_targets_usable_ram(uint64_t start, uint64_t size);
/* Allocate a temporary buffer from the unused RAM areas. */
void *bootmem_allocate_buffer(size_t size);
#endif /* BOOTMEM_H */

View File

@ -174,9 +174,8 @@ void *cbmem_find(u32 id);
#ifndef __PRE_RAM__ #ifndef __PRE_RAM__
/* Ramstage only functions. */ /* Ramstage only functions. */
/* Add the cbmem memory used to the memory tables. */ /* Add the cbmem memory used to the memory map at boot. */
struct lb_memory; void cbmem_add_bootmem(void);
void cbmem_add_lb_mem(struct lb_memory *mem);
void cbmem_list(void); void cbmem_list(void);
void cbmem_arch_init(void); void cbmem_arch_init(void);
void cbmem_print_entry(int n, u32 id, u64 start, u64 size); void cbmem_print_entry(int n, u32 id, u64 start, u64 size);

View File

@ -63,6 +63,7 @@ romstage-$(CONFIG_ARCH_X86) += gcc.c
ramstage-y += hardwaremain.c ramstage-y += hardwaremain.c
ramstage-y += selfboot.c ramstage-y += selfboot.c
ramstage-y += coreboot_table.c ramstage-y += coreboot_table.c
ramstage-y += bootmem.c
ifneq ($(CONFIG_HAVE_ARCH_MEMSET),y) ifneq ($(CONFIG_HAVE_ARCH_MEMSET),y)
ramstage-y += memset.c ramstage-y += memset.c
endif endif

177
src/lib/bootmem.c Normal file
View File

@ -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 <console/console.h>
#include <bootmem.h>
#include <cbmem.h>
#include <device/resource.h>
#include <stdlib.h>
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;
}

View File

@ -19,9 +19,9 @@
#include <types.h> #include <types.h>
#include <string.h> #include <string.h>
#include <bootmem.h>
#include <bootstate.h> #include <bootstate.h>
#include <cbmem.h> #include <cbmem.h>
#include <boot/coreboot_tables.h>
#include <console/console.h> #include <console/console.h>
#include <arch/early_variables.h> #include <arch/early_variables.h>
#if CONFIG_HAVE_ACPI_RESUME && !defined(__PRE_RAM__) #if CONFIG_HAVE_ACPI_RESUME && !defined(__PRE_RAM__)
@ -265,9 +265,9 @@ BOOT_STATE_INIT_ENTRIES(cbmem_bscb) = {
init_cbmem_post_device, NULL), 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) void cbmem_list(void)

View File

@ -29,7 +29,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <cbfs.h> #include <cbfs.h>
#include <cbmem.h> #include <cbmem.h>
#include <memrange.h> #include <bootmem.h>
#if CONFIG_CHROMEOS #if CONFIG_CHROMEOS
#if CONFIG_GENERATE_ACPI_TABLES #if CONFIG_GENERATE_ACPI_TABLES
#include <arch/acpi.h> #include <arch/acpi.h>
@ -366,78 +366,11 @@ struct lb_memory *get_lb_mem(void)
return mem_ranges; 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 write_coreboot_table(
unsigned long low_table_start, unsigned long low_table_end, unsigned long low_table_start, unsigned long low_table_end,
unsigned long rom_table_start, unsigned long rom_table_end) unsigned long rom_table_start, unsigned long rom_table_end)
{ {
struct lb_header *head; struct lb_header *head;
struct lb_memory *mem;
if (low_table_start || low_table_end) { if (low_table_start || low_table_end) {
printk(BIOS_DEBUG, "Writing table forward entry at 0x%08lx\n", printk(BIOS_DEBUG, "Writing table forward entry at 0x%08lx\n",
@ -476,30 +409,29 @@ unsigned long write_coreboot_table(
} }
#endif #endif
/* The Linux kernel assumes this region is reserved */ /* Initialize the memory map at boot time. */
/* Record where RAM is located */ bootmem_init();
mem = build_lb_mem(head);
if (low_table_start || low_table_end) { if (low_table_start || low_table_end) {
uint64_t size = low_table_end - low_table_start;
/* Record the mptable and the the lb_table. /* Record the mptable and the the lb_table.
* (This will be adjusted later) */ * (This will be adjusted later) */
lb_add_memory_range(mem, LB_MEM_TABLE, bootmem_add_range(low_table_start, size, LB_MEM_TABLE);
low_table_start, low_table_end - low_table_start);
} }
/* Record the pirq table, acpi tables, and maybe the mptable. However, /* Record the pirq table, acpi tables, and maybe the mptable. However,
* these only need to be added when the rom_table is sitting below * 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. * 1MiB. If it isn't that means high tables are being written.
* The code below handles high tables correctly. */ * The code below handles high tables correctly. */
if (rom_table_end <= (1 << 20)) if (rom_table_end <= (1 << 20)) {
lb_add_memory_range(mem, LB_MEM_TABLE, uint64_t size = rom_table_end - rom_table_start;
rom_table_start, rom_table_end - rom_table_start); bootmem_add_range(rom_table_start, size, LB_MEM_TABLE);
}
cbmem_add_lb_mem(mem);
/* No other memory areas can be added after the memory table has been /* 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. */ * 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 */ /* Record our motherboard */
lb_mainboard(head); lb_mainboard(head);

View File

@ -18,7 +18,7 @@
*/ */
#include <bootstate.h> #include <bootstate.h>
#include <boot/tables.h> #include <bootmem.h>
#include <console/console.h> #include <console/console.h>
#include <cbmem.h> #include <cbmem.h>
#include <string.h> #include <string.h>
@ -431,14 +431,14 @@ BOOT_STATE_INIT_ENTRIES(cbmem_bscb) = {
init_cbmem_pre_device, NULL), init_cbmem_pre_device, NULL),
}; };
void cbmem_add_lb_mem(struct lb_memory *mem) void cbmem_add_bootmem(void)
{ {
unsigned long base; unsigned long base;
unsigned long top; unsigned long top;
base = (unsigned long)cbmem_base(); base = (unsigned long)cbmem_base();
top = (unsigned long)get_top_aligned(); 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) void cbmem_list(void)