[coreboot-gerrit] Patch set updated for coreboot: ce67fb3 coreboot: introduce notion of bootmem for memory map at boot

Aaron Durbin (adurbin@google.com) gerrit at coreboot.org
Wed Feb 26 04:44:15 CET 2014


Aaron Durbin (adurbin at google.com) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/5302

-gerrit

commit ce67fb3a94157c70ada12493946a6280fdcb6fd8
Author: Aaron Durbin <adurbin at chromium.org>
Date:   Tue Feb 18 21:55:02 2014 -0600

    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 at chromium.org>
---
 src/include/boot/coreboot_tables.h |   3 -
 src/include/bootmem.h              |  48 ++++++++++
 src/include/cbmem.h                |   5 +-
 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 +-
 8 files changed, 245 insertions(+), 91 deletions(-)

diff --git a/src/include/boot/coreboot_tables.h b/src/include/boot/coreboot_tables.h
index 6243d45..9b87f3d 100644
--- a/src/include/boot/coreboot_tables.h
+++ b/src/include/boot/coreboot_tables.h
@@ -338,9 +338,6 @@ 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);
 
-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
  * from the coreboot table.
  */
diff --git a/src/include/bootmem.h b/src/include/bootmem.h
new file mode 100644
index 0000000..656769f
--- /dev/null
+++ b/src/include/bootmem.h
@@ -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 */
diff --git a/src/include/cbmem.h b/src/include/cbmem.h
index eb307d4..7cb12b2 100644
--- a/src/include/cbmem.h
+++ b/src/include/cbmem.h
@@ -174,9 +174,8 @@ void *cbmem_find(u32 id);
 
 #ifndef __PRE_RAM__
 /* Ramstage only functions. */
-/* Add the cbmem memory used to the memory tables. */
-struct lb_memory;
-void cbmem_add_lb_mem(struct lb_memory *mem);
+/* Add the cbmem memory used to the memory map at boot. */
+void cbmem_add_bootmem(void);
 void cbmem_list(void);
 void cbmem_arch_init(void);
 void cbmem_print_entry(int n, u32 id, u64 start, u64 size);
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 <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;
+}
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 <types.h>
 #include <string.h>
+#include <bootmem.h>
 #include <bootstate.h>
 #include <cbmem.h>
-#include <boot/coreboot_tables.h>
 #include <console/console.h>
 #include <arch/early_variables.h>
 #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 <stdlib.h>
 #include <cbfs.h>
 #include <cbmem.h>
-#include <memrange.h>
+#include <bootmem.h>
 #if CONFIG_CHROMEOS
 #if CONFIG_GENERATE_ACPI_TABLES
 #include <arch/acpi.h>
@@ -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 <bootstate.h>
-#include <boot/tables.h>
+#include <bootmem.h>
 #include <console/console.h>
 #include <cbmem.h>
 #include <string.h>
@@ -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)



More information about the coreboot-gerrit mailing list