diff options
Diffstat (limited to 'src/commonlib')
-rw-r--r-- | src/commonlib/include/commonlib/region.h | 13 | ||||
-rw-r--r-- | src/commonlib/region.c | 13 |
2 files changed, 22 insertions, 4 deletions
diff --git a/src/commonlib/include/commonlib/region.h b/src/commonlib/include/commonlib/region.h index 0080c441a5..764870f46c 100644 --- a/src/commonlib/include/commonlib/region.h +++ b/src/commonlib/include/commonlib/region.h @@ -164,14 +164,18 @@ static inline int rdev_chain_full(struct region_device *child, 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_ro_ops; 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 55c0679033..04c3180754 100644 --- a/src/commonlib/region.c +++ b/src/commonlib/region.c @@ -287,6 +287,19 @@ const struct region_device_ops mem_rdev_rw_ops = { .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) { |