commonlib/region: Turn addrspace_32bit into a more official API
We had the addrspace_32bit rdev in prog_loaders.c for a while to help
represent memory ranges as an rdev, and we've found it useful for a
couple of things that have nothing to do with program loading. This
patch moves the concept straight into commonlib/region.c so it is no
longer anchored in such a weird place, and easier to use in unit tests.
Also expand the concept to the whole address space (there's no real need
to restrict it to 32 bits in 64-bit environments) and introduce an
rdev_chain_mem() helper function to make it a bit easier to use. Replace
some direct uses of struct mem_region_device with this new API where it
seems to make sense.
Signed-off-by: Julius Werner <jwerner@chromium.org>
Change-Id: Ie4c763b77f77d227768556a9528681d771a08dca
Reviewed-on: https://review.coreboot.org/c/coreboot/+/52533
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
diff --git a/src/commonlib/include/commonlib/region.h b/src/commonlib/include/commonlib/region.h
index 0080c44..764870f 100644
--- a/src/commonlib/include/commonlib/region.h
+++ b/src/commonlib/include/commonlib/region.h
@@ -164,14 +164,18 @@
ssize_t rdev_relative_offset(const struct region_device *p,
const struct region_device *c);
+/* Helper functions to create an rdev that represents memory. */
+int rdev_chain_mem(struct region_device *child, const void *base, size_t size);
+int rdev_chain_mem_rw(struct region_device *child, void *base, size_t size);
+
struct mem_region_device {
char *base;
struct region_device rdev;
};
-/* Initialize at runtime a mem_region_device. This would be used when
- * the base and size are dynamic or can't be known during linking.
- * There are two variants: read-only and read-write. */
+/* Initialize at runtime a mem_region_device. Should only be used for mappings
+ that need to fit right up to the edge of the physical address space. Most use
+ cases will want to use rdev_chain_mem() instead. */
void mem_region_device_ro_init(struct mem_region_device *mdev, void *base,
size_t size);
@@ -182,7 +186,8 @@
extern const struct region_device_ops mem_rdev_rw_ops;
-/* Statically initialize mem_region_device. */
+/* Statically initialize mem_region_device. Should normally only be used for
+ const globals. Most use cases will want to use rdev_chain_mem() instead. */
#define MEM_REGION_DEV_INIT(base_, size_, ops_) \
{ \
.base = (void *)(base_), \
diff --git a/src/commonlib/region.c b/src/commonlib/region.c
index 55c0679..04c3180 100644
--- a/src/commonlib/region.c
+++ b/src/commonlib/region.c
@@ -287,6 +287,19 @@
.eraseat = mdev_eraseat,
};
+static const struct mem_region_device mem_rdev = MEM_REGION_DEV_RO_INIT(0, ~(size_t)0);
+static const struct mem_region_device mem_rdev_rw = MEM_REGION_DEV_RW_INIT(0, ~(size_t)0);
+
+int rdev_chain_mem(struct region_device *child, const void *base, size_t size)
+{
+ return rdev_chain(child, &mem_rdev.rdev, (uintptr_t)base, size);
+}
+
+int rdev_chain_mem_rw(struct region_device *child, void *base, size_t size)
+{
+ return rdev_chain(child, &mem_rdev_rw.rdev, (uintptr_t)base, size);
+}
+
void *mmap_helper_rdev_mmap(const struct region_device *rd, size_t offset,
size_t size)
{
diff --git a/src/drivers/elog/elog.c b/src/drivers/elog/elog.c
index 82df989..4769559 100644
--- a/src/drivers/elog/elog.c
+++ b/src/drivers/elog/elog.c
@@ -44,7 +44,7 @@
struct region_device nv_dev;
/* Device that mirrors the eventlog in memory. */
- struct mem_region_device mirror_dev;
+ struct region_device mirror_dev;
enum elog_init_state elog_initialized;
};
@@ -56,7 +56,7 @@
static inline struct region_device *mirror_dev_get(void)
{
- return &elog_state.mirror_dev.rdev;
+ return &elog_state.mirror_dev;
}
static size_t elog_events_start(void)
@@ -798,8 +798,7 @@
printk(BIOS_ERR, "ELOG: Unable to allocate backing store\n");
return -1;
}
- mem_region_device_rw_init(&elog_state.mirror_dev, mirror_buffer,
- elog_size);
+ rdev_chain_mem_rw(&elog_state.mirror_dev, mirror_buffer, elog_size);
/*
* Mark as initialized to allow elog_init() to be called and deemed
diff --git a/src/drivers/intel/gma/opregion.c b/src/drivers/intel/gma/opregion.c
index e1ed6c8..cf2c5d9 100644
--- a/src/drivers/intel/gma/opregion.c
+++ b/src/drivers/intel/gma/opregion.c
@@ -128,16 +128,14 @@
size_t offset;
// FIXME: caller should supply a region_device instead of vbios pointer
- if (rdev_chain(&rd, &addrspace_32bit.rdev, (uintptr_t)vbios,
- sizeof(*oprom)))
+ if (rdev_chain_mem(&rd, vbios, sizeof(*oprom)))
return CB_ERR;
if (rdev_readat(&rd, &opromsize, offsetof(optionrom_header_t, size),
sizeof(opromsize)) != sizeof(opromsize) || !opromsize)
return CB_ERR;
- if (rdev_chain(&rd, &addrspace_32bit.rdev, (uintptr_t)vbios,
- opromsize * 512))
+ if (rdev_chain_mem(&rd, vbios, opromsize * 512))
return CB_ERR;
oprom = rdev_mmap(&rd, 0, sizeof(*oprom));
@@ -200,8 +198,7 @@
if (vbt == NULL)
return CB_ERR;
- if (rdev_chain(rdev, &addrspace_32bit.rdev, (uintptr_t)vbt,
- vbt_data_size))
+ if (rdev_chain_mem(rdev, vbt, vbt_data_size))
return CB_ERR;
printk(BIOS_INFO, "GMA: Found VBT in CBFS\n");
diff --git a/src/drivers/smmstore/store.c b/src/drivers/smmstore/store.c
index 9f9ab01..a12cd58 100644
--- a/src/drivers/smmstore/store.c
+++ b/src/drivers/smmstore/store.c
@@ -267,14 +267,14 @@
/* Implementation of Version 2 */
static bool store_initialized;
-static struct mem_region_device mdev_com_buf;
+static struct region_device mdev_com_buf;
static int smmstore_rdev_chain(struct region_device *rdev)
{
if (!store_initialized)
return -1;
- return rdev_chain_full(rdev, &mdev_com_buf.rdev);
+ return rdev_chain_full(rdev, &mdev_com_buf);
}
/**
@@ -289,7 +289,7 @@
if (store_initialized)
return -1;
- mem_region_device_rw_init(&mdev_com_buf, buf, len);
+ rdev_chain_mem_rw(&mdev_com_buf, buf, len);
store_initialized = true;
diff --git a/src/drivers/vpd/vpd.c b/src/drivers/vpd/vpd.c
index d3ff370..a099b3b 100644
--- a/src/drivers/vpd/vpd.c
+++ b/src/drivers/vpd/vpd.c
@@ -101,10 +101,8 @@
if (!cbmem)
return -1;
- rdev_chain(&ro_vpd, &addrspace_32bit.rdev,
- (uintptr_t)cbmem->blob, cbmem->ro_size);
- rdev_chain(&rw_vpd, &addrspace_32bit.rdev,
- (uintptr_t)cbmem->blob + cbmem->ro_size, cbmem->rw_size);
+ rdev_chain_mem(&ro_vpd, cbmem->blob, cbmem->ro_size);
+ rdev_chain_mem(&rw_vpd, cbmem->blob + cbmem->ro_size, cbmem->rw_size);
return 0;
}
diff --git a/src/include/program_loading.h b/src/include/program_loading.h
index d01eff6..65a36b9 100644
--- a/src/include/program_loading.h
+++ b/src/include/program_loading.h
@@ -91,15 +91,11 @@
return prog->arg;
}
-/* region_device representing the 32-bit flat address space. */
-extern const struct mem_region_device addrspace_32bit;
-
/* Can be used to get an rdev representation of program area in memory. */
static inline void prog_chain_rdev(const struct prog *prog,
struct region_device *rdev_out)
{
- rdev_chain(rdev_out, &addrspace_32bit.rdev,
- (uintptr_t)prog->start, prog->size);
+ rdev_chain_mem(rdev_out, prog->start, prog->size);
}
static inline void prog_set_area(struct prog *prog, void *start, size_t size)
diff --git a/src/lib/cbfs.c b/src/lib/cbfs.c
index 23e8f41..32ebfaf 100644
--- a/src/lib/cbfs.c
+++ b/src/lib/cbfs.c
@@ -89,7 +89,7 @@
return -1;
size_t msize = be32toh(fh->mdata.h.offset);
- if (rdev_chain(&fh->metadata, &addrspace_32bit.rdev, (uintptr_t)&fh->mdata, msize))
+ if (rdev_chain_mem(&fh->metadata, &fh->mdata, msize))
return -1;
if (type) {
@@ -436,7 +436,7 @@
void *compr_start = prog_start(pstage) + prog_size(pstage) - in_size;
if (rdev_readat(&rdev, compr_start, 0, in_size) != in_size)
return CB_ERR;
- rdev_chain(&rdev, &addrspace_32bit.rdev, (uintptr_t)compr_start, in_size);
+ rdev_chain_mem(&rdev, compr_start, in_size);
}
size_t fsize = cbfs_load_and_decompress(&rdev, prog_start(pstage), prog_size(pstage),
diff --git a/src/lib/fmap.c b/src/lib/fmap.c
index 418e715..254d787 100644
--- a/src/lib/fmap.c
+++ b/src/lib/fmap.c
@@ -16,7 +16,7 @@
*/
static int fmap_print_once;
-static struct mem_region_device fmap_cache;
+static struct region_device fmap_cache;
#define print_once(...) do { \
if (!fmap_print_once) \
@@ -53,7 +53,7 @@
fmap_print_once = 1;
}
-static void setup_preram_cache(struct mem_region_device *cache_mrdev)
+static void setup_preram_cache(struct region_device *cache_rdev)
{
if (CONFIG(NO_FMAP_CACHE))
return;
@@ -99,7 +99,7 @@
report(fmap);
register_cache:
- mem_region_device_ro_init(cache_mrdev, fmap, FMAP_SIZE);
+ rdev_chain_mem(cache_rdev, fmap, FMAP_SIZE);
}
static int find_fmap_directory(struct region_device *fmrd)
@@ -109,10 +109,10 @@
size_t offset = FMAP_OFFSET;
/* Try FMAP cache first */
- if (!region_device_sz(&fmap_cache.rdev))
+ if (!region_device_sz(&fmap_cache))
setup_preram_cache(&fmap_cache);
- if (region_device_sz(&fmap_cache.rdev))
- return rdev_chain_full(fmrd, &fmap_cache.rdev);
+ if (region_device_sz(&fmap_cache))
+ return rdev_chain_full(fmrd, &fmap_cache);
boot_device_init();
boot = boot_device_ro();
@@ -281,7 +281,7 @@
if (!e)
return;
- mem_region_device_ro_init(&fmap_cache, cbmem_entry_start(e), cbmem_entry_size(e));
+ rdev_chain_mem(&fmap_cache, cbmem_entry_start(e), cbmem_entry_size(e));
}
/*
diff --git a/src/lib/prog_loaders.c b/src/lib/prog_loaders.c
index 28c6bf7..40f51eb 100644
--- a/src/lib/prog_loaders.c
+++ b/src/lib/prog_loaders.c
@@ -16,10 +16,6 @@
#include <timestamp.h>
#include <security/vboot/vboot_common.h>
-/* Only can represent up to 1 byte less than size_t. */
-const struct mem_region_device addrspace_32bit =
- MEM_REGION_DEV_RO_INIT(0, ~0UL);
-
void run_romstage(void)
{
struct prog romstage =
diff --git a/src/soc/intel/apollolake/mmap_boot.c b/src/soc/intel/apollolake/mmap_boot.c
index 23c819e..6e0800c 100644
--- a/src/soc/intel/apollolake/mmap_boot.c
+++ b/src/soc/intel/apollolake/mmap_boot.c
@@ -42,7 +42,7 @@
static size_t bios_size;
-static struct mem_region_device shadow_dev;
+static struct region_device shadow_dev;
static struct xlate_region_device real_dev;
static struct xlate_window real_dev_window;
@@ -67,10 +67,9 @@
*/
bios_mapped_size = size - 256 * KiB;
- mem_region_device_ro_init(&shadow_dev, (void *)base,
- bios_mapped_size);
+ rdev_chain_mem(&shadow_dev, (void *)base, bios_mapped_size);
- xlate_window_init(&real_dev_window, &shadow_dev.rdev, start, bios_mapped_size);
+ xlate_window_init(&real_dev_window, &shadow_dev, start, bios_mapped_size);
xlate_region_device_ro_init(&real_dev, 1, &real_dev_window, CONFIG_ROM_SIZE);
bios_size = size;
@@ -98,7 +97,7 @@
bios_mmap_init();
table->flash_base = region_offset(&real_dev_window.sub_region);
- table->host_base = (uintptr_t)rdev_mmap_full(&shadow_dev.rdev);
+ table->host_base = (uintptr_t)rdev_mmap_full(&shadow_dev);
table->size = region_sz(&real_dev_window.sub_region);
return 1;
diff --git a/src/soc/samsung/exynos5250/alternate_cbfs.c b/src/soc/samsung/exynos5250/alternate_cbfs.c
index a4ae71a..a540c3c 100644
--- a/src/soc/samsung/exynos5250/alternate_cbfs.c
+++ b/src/soc/samsung/exynos5250/alternate_cbfs.c
@@ -92,8 +92,8 @@
return 0;
}
-static struct mem_region_device alternate_rdev =
- MEM_REGION_DEV_RO_INIT(NULL, 0);
+const static struct mem_region_device alternate_rdev =
+ MEM_REGION_DEV_RO_INIT(_cbfs_cache, REGION_SIZE(cbfs_cache));
const struct region_device *boot_device_ro(void)
{
@@ -114,9 +114,6 @@
void boot_device_init(void)
{
- mem_region_device_ro_init(&alternate_rdev, _cbfs_cache,
- REGION_SIZE(cbfs_cache));
-
if (*iram_secondary_base == SECONDARY_BASE_BOOT_USB) {
printk(BIOS_DEBUG, "Using Exynos alternate boot mode USB A-A\n");
usb_cbfs_open();
diff --git a/src/soc/samsung/exynos5420/alternate_cbfs.c b/src/soc/samsung/exynos5420/alternate_cbfs.c
index 0eb8c08..99f14a1 100644
--- a/src/soc/samsung/exynos5420/alternate_cbfs.c
+++ b/src/soc/samsung/exynos5420/alternate_cbfs.c
@@ -100,7 +100,7 @@
}
static struct mem_region_device alternate_rdev =
- MEM_REGION_DEV_RO_INIT(NULL, 0);
+ MEM_REGION_DEV_RO_INIT(_cbfs_cache, REGION_SIZE(cbfs_cache));
const struct region_device *boot_device_ro(void)
{
@@ -121,9 +121,6 @@
void boot_device_init(void)
{
- mem_region_device_ro_init(&alternate_rdev, _cbfs_cache,
- REGION_SIZE(cbfs_cache));
-
if (*iram_secondary_base == SECONDARY_BASE_BOOT_USB) {
printk(BIOS_DEBUG, "Using Exynos alternate boot mode USB A-A\n");
usb_cbfs_open();