diff options
author | Antonello Dettori <dev@dettori.io> | 2016-06-22 21:09:08 +0200 |
---|---|---|
committer | Martin Roth <martinroth@google.com> | 2016-06-24 20:48:12 +0200 |
commit | e5f48d20e7d66471f182afe305fe073177722224 (patch) | |
tree | f4a28f1a43ca733aca74d79ee7cff4787dd1b9c1 /src | |
parent | 5992afa57db4921f341bfdeb4bc796dfa6d0ae0e (diff) |
region: Add writeat and eraseat support
Implement writeat and eraseat support into the region_device_ops struct.
Change-Id: Iac2cf32e523d2f19ee9e5feefe1fba8c68982f3d
Signed-off-by: Antonello Dettori <dev@dettori.io>
Reviewed-on: https://review.coreboot.org/15318
Tested-by: build bot (Jenkins)
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Reviewed-by: Furquan Shaikh <furquan@google.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/arch/power8/rom_media.c | 2 | ||||
-rw-r--r-- | src/arch/riscv/rom_media.c | 2 | ||||
-rw-r--r-- | src/arch/x86/mmap_boot.c | 2 | ||||
-rw-r--r-- | src/commonlib/include/commonlib/region.h | 71 | ||||
-rw-r--r-- | src/commonlib/region.c | 171 | ||||
-rw-r--r-- | src/cpu/ti/am335x/bootblock_media.c | 2 | ||||
-rw-r--r-- | src/lib/prog_loaders.c | 3 | ||||
-rw-r--r-- | src/mainboard/emulation/qemu-armv7/media.c | 2 | ||||
-rw-r--r-- | src/soc/intel/apollolake/mmap_boot.c | 4 | ||||
-rw-r--r-- | src/soc/samsung/exynos5250/alternate_cbfs.c | 6 | ||||
-rw-r--r-- | src/soc/samsung/exynos5420/alternate_cbfs.c | 6 |
11 files changed, 237 insertions, 34 deletions
diff --git a/src/arch/power8/rom_media.c b/src/arch/power8/rom_media.c index 0c54e7abfd..c1713074f9 100644 --- a/src/arch/power8/rom_media.c +++ b/src/arch/power8/rom_media.c @@ -18,7 +18,7 @@ /* This assumes that the CBFS resides at 0x0, which is true for the default * configuration. */ static const struct mem_region_device boot_dev = - MEM_REGION_DEV_INIT(NULL, CONFIG_ROM_SIZE); + MEM_REGION_DEV_RO_INIT(NULL, CONFIG_ROM_SIZE); const struct region_device *boot_device_ro(void) { diff --git a/src/arch/riscv/rom_media.c b/src/arch/riscv/rom_media.c index 0c54e7abfd..c1713074f9 100644 --- a/src/arch/riscv/rom_media.c +++ b/src/arch/riscv/rom_media.c @@ -18,7 +18,7 @@ /* This assumes that the CBFS resides at 0x0, which is true for the default * configuration. */ static const struct mem_region_device boot_dev = - MEM_REGION_DEV_INIT(NULL, CONFIG_ROM_SIZE); + MEM_REGION_DEV_RO_INIT(NULL, CONFIG_ROM_SIZE); const struct region_device *boot_device_ro(void) { diff --git a/src/arch/x86/mmap_boot.c b/src/arch/x86/mmap_boot.c index ae35451b3c..35a7544c30 100644 --- a/src/arch/x86/mmap_boot.c +++ b/src/arch/x86/mmap_boot.c @@ -22,7 +22,7 @@ #define rom_base ((void *)(uintptr_t)(0x100000000ULL-CONFIG_ROM_SIZE)) static const struct mem_region_device boot_dev = - MEM_REGION_DEV_INIT(rom_base, CONFIG_ROM_SIZE); + MEM_REGION_DEV_RO_INIT(rom_base, CONFIG_ROM_SIZE); const struct region_device *boot_device_ro(void) { diff --git a/src/commonlib/include/commonlib/region.h b/src/commonlib/include/commonlib/region.h index 634132a695..ffc507beae 100644 --- a/src/commonlib/include/commonlib/region.h +++ b/src/commonlib/include/commonlib/region.h @@ -48,6 +48,20 @@ int rdev_munmap(const struct region_device *rd, void *mapping); ssize_t rdev_readat(const struct region_device *rd, void *b, size_t offset, size_t size); +/* + * Returns < 0 on error otherwise returns size of data wrote at provided + * offset from the buffer passed. + */ +ssize_t rdev_writeat(const struct region_device *rd, void *b, size_t offset, + size_t size); + +/* + * Returns < 0 on error otherwise returns size of data erased. + * If eraseat ops is not defined it returns size which indicates + * that operation was successful. + */ +ssize_t rdev_eraseat(const struct region_device *rd, size_t offset, + size_t size); /**************************************** * Implementation of a region device * @@ -67,6 +81,9 @@ struct region_device_ops { void *(*mmap)(const struct region_device *, size_t, size_t); int (*munmap)(const struct region_device *, void *); ssize_t (*readat)(const struct region_device *, void *, size_t, size_t); + ssize_t (*writeat)(const struct region_device *, void *, size_t, + size_t); + ssize_t (*eraseat)(const struct region_device *, size_t, size_t); }; struct region { @@ -139,26 +156,38 @@ struct mem_region_device { struct region_device rdev; }; -/* Iniitalize at runtime a mem_region_device. This would be used when - * the base and size are dynamic or can't be known during linking. */ -void mem_region_device_init(struct mem_region_device *mdev, void *base, +/* Inititalize 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. */ +void mem_region_device_ro_init(struct mem_region_device *mdev, void *base, size_t size); -extern const struct region_device_ops mem_rdev_ops; +void mem_region_device_rw_init(struct mem_region_device *mdev, void *base, + size_t size); + +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. */ -#define MEM_REGION_DEV_INIT(base_, size_) \ +#define MEM_REGION_DEV_INIT(base_, size_, ops_) \ { \ .base = (void *)(base_), \ - .rdev = REGION_DEV_INIT(&mem_rdev_ops, 0, (size_)), \ + .rdev = REGION_DEV_INIT((ops_), 0, (size_)), \ } +#define MEM_REGION_DEV_RO_INIT(base_, size_) \ + MEM_REGION_DEV_INIT(base_, size_, &mem_rdev_ro_ops) \ + +#define MEM_REGION_DEV_RW_INIT(base_, size_) \ + MEM_REGION_DEV_INIT(base_, size_, &mem_rdev_rw_ops) \ + struct mmap_helper_region_device { struct mem_pool pool; struct region_device rdev; }; -#define MMAP_HELPER_REGION_INIT(ops_, offset_, size_) \ +#define MMAP_HELPER_REGION_INIT(ops_, offset_, size_) \ { \ .rdev = REGION_DEV_INIT((ops_), (offset_), (size_)), \ } @@ -170,7 +199,7 @@ void *mmap_helper_rdev_mmap(const struct region_device *, size_t, size_t); int mmap_helper_rdev_munmap(const struct region_device *, void *); /* A translated region device provides the ability to publish a region device - * in one address space and use an access mechansim within another address + * in one address space and use an access mechanism within another address * space. The sub region is the window within the 1st address space and * the request is modified prior to accessing the second address space * provided by access_dev. */ @@ -180,20 +209,38 @@ struct xlate_region_device { struct region_device rdev; }; -extern const struct region_device_ops xlate_rdev_ops; +extern const struct region_device_ops xlate_rdev_ro_ops; + +extern const struct region_device_ops xlate_rdev_rw_ops; -#define XLATE_REGION_DEV_INIT(access_dev_, sub_offset_, sub_size_, parent_sz_) \ +#define XLATE_REGION_DEV_INIT(access_dev_, sub_offset_, sub_size_, \ + parent_sz_, ops_) \ { \ .access_dev = access_dev_, \ .sub_region = { \ .offset = (sub_offset_), \ .size = (sub_size_), \ }, \ - .rdev = REGION_DEV_INIT(&xlate_rdev_ops, 0, (parent_sz_)),\ + .rdev = REGION_DEV_INIT((ops_), 0, (parent_sz_)), \ } +#define XLATE_REGION_DEV_RO_INIT(access_dev_, sub_offset_, sub_size_, \ + parent_sz_) \ + XLATE_REGION_DEV_INIT(access_dev_, sub_offset_, \ + sub_size_, parent_sz_, &xlate_rdev_ro_ops), \ + +#define XLATE_REGION_DEV_RW_INIT(access_dev_, sub_offset_, sub_size_, \ + parent_sz_) \ + XLATE_REGION_DEV_INIT(access_dev_, sub_offset_, \ + sub_size_, parent_sz_, &xlate_rdev_rw_ops), \ + /* Helper to dynamically initialize xlate region device. */ -void xlate_region_device_init(struct xlate_region_device *xdev, +void xlate_region_device_ro_init(struct xlate_region_device *xdev, + const struct region_device *access_dev, + size_t sub_offset, size_t sub_size, + size_t parent_size); + +void xlate_region_device_rw_init(struct xlate_region_device *xdev, const struct region_device *access_dev, size_t sub_offset, size_t sub_size, size_t parent_size); diff --git a/src/commonlib/region.c b/src/commonlib/region.c index 6f710746ca..d7bea03ae9 100644 --- a/src/commonlib/region.c +++ b/src/commonlib/region.c @@ -103,6 +103,48 @@ ssize_t rdev_readat(const struct region_device *rd, void *b, size_t offset, return rdev->ops->readat(rdev, b, req.offset, req.size); } +ssize_t rdev_writeat(const struct region_device *rd, void *b, size_t offset, + size_t size) +{ + const struct region_device *rdev; + struct region req = { + .offset = offset, + .size = size, + }; + + if (!normalize_and_ok(&rd->region, &req)) + return -1; + + rdev = rdev_root(rd); + + if (rdev->ops->writeat == NULL) + return -1; + + return rdev->ops->writeat(rdev, b, req.offset, req.size); +} + +ssize_t rdev_eraseat(const struct region_device *rd, size_t offset, + size_t size) +{ + const struct region_device *rdev; + struct region req = { + .offset = offset, + .size = size, + }; + + if (!normalize_and_ok(&rd->region, &req)) + return -1; + + rdev = rdev_root(rd); + + /* If the eraseat ptr is NULL we assume that the erase + * function was completed successfully. */ + if (rdev->ops->eraseat == NULL) + return size; + + return rdev->ops->eraseat(rdev, req.offset, req.size); +} + int rdev_chain(struct region_device *child, const struct region_device *parent, size_t offset, size_t size) { @@ -124,15 +166,27 @@ int rdev_chain(struct region_device *child, const struct region_device *parent, return 0; } -void mem_region_device_init(struct mem_region_device *mdev, void *base, - size_t size) +static void mem_region_device_init(struct mem_region_device *mdev, + const struct region_device_ops *ops, void *base, size_t size) { memset(mdev, 0, sizeof(*mdev)); mdev->base = base; - mdev->rdev.ops = &mem_rdev_ops; + mdev->rdev.ops = ops; mdev->rdev.region.size = size; } +void mem_region_device_ro_init(struct mem_region_device *mdev, void *base, + size_t size) +{ + return mem_region_device_init(mdev, &mem_rdev_ro_ops, base, size); +} + +void mem_region_device_rw_init(struct mem_region_device *mdev, void *base, + size_t size) +{ + return mem_region_device_init(mdev, &mem_rdev_rw_ops, base, size); +} + void region_device_init(struct region_device *rdev, const struct region_device_ops *ops, size_t offset, size_t size) @@ -144,16 +198,35 @@ void region_device_init(struct region_device *rdev, rdev->region.size = size; } -void xlate_region_device_init(struct xlate_region_device *xdev, - const struct region_device *access_dev, - size_t sub_offset, size_t sub_size, - size_t parent_size) +static void xlate_region_device_init(struct xlate_region_device *xdev, + const struct region_device_ops *ops, + const struct region_device *access_dev, + size_t sub_offset, size_t sub_size, + size_t parent_size) { memset(xdev, 0, sizeof(*xdev)); xdev->access_dev = access_dev; xdev->sub_region.offset = sub_offset; xdev->sub_region.size = sub_size; - region_device_init(&xdev->rdev, &xlate_rdev_ops, 0, parent_size); + region_device_init(&xdev->rdev, ops, 0, parent_size); +} + +void xlate_region_device_ro_init(struct xlate_region_device *xdev, + const struct region_device *access_dev, + size_t sub_offset, size_t sub_size, + size_t parent_size) +{ + xlate_region_device_init(xdev, &xlate_rdev_ro_ops, access_dev, + sub_offset, sub_size, parent_size); +} + +void xlate_region_device_rw_init(struct xlate_region_device *xdev, + const struct region_device *access_dev, + size_t sub_offset, size_t sub_size, + size_t parent_size) +{ + xlate_region_device_init(xdev, &xlate_rdev_rw_ops, access_dev, + sub_offset, sub_size, parent_size); } static void *mdev_mmap(const struct region_device *rd, size_t offset, @@ -184,10 +257,42 @@ static ssize_t mdev_readat(const struct region_device *rd, void *b, return size; } -const struct region_device_ops mem_rdev_ops = { +static ssize_t mdev_writeat(const struct region_device *rd, void *b, + size_t offset, size_t size) +{ + const struct mem_region_device *mdev; + + mdev = container_of(rd, __typeof__(*mdev), rdev); + + memcpy(&mdev->base[offset], b, size); + + return size; +} + +static ssize_t mdev_eraseat(const struct region_device *rd, size_t offset, + size_t size) +{ + const struct mem_region_device *mdev; + + mdev = container_of(rd, __typeof__(*mdev), rdev); + + memset(&mdev->base[offset], 0, size); + + return size; +} + +const struct region_device_ops mem_rdev_ro_ops = { + .mmap = mdev_mmap, + .munmap = mdev_munmap, + .readat = mdev_readat, +}; + +const struct region_device_ops mem_rdev_rw_ops = { .mmap = mdev_mmap, .munmap = mdev_munmap, .readat = mdev_readat, + .writeat = mdev_writeat, + .eraseat = mdev_eraseat, }; void mmap_helper_device_init(struct mmap_helper_region_device *mdev, @@ -275,8 +380,54 @@ static ssize_t xlate_readat(const struct region_device *rd, void *b, return rdev_readat(xldev->access_dev, b, offset, size); } -const struct region_device_ops xlate_rdev_ops = { +static ssize_t xlate_writeat(const struct region_device *rd, void *b, + size_t offset, size_t size) +{ + struct region req = { + .offset = offset, + .size = size, + }; + const struct xlate_region_device *xldev; + + xldev = container_of(rd, __typeof__(*xldev), rdev); + + if (!is_subregion(&xldev->sub_region, &req)) + return -1; + + offset -= region_offset(&xldev->sub_region); + + return rdev_writeat(xldev->access_dev, b, offset, size); +} + +static ssize_t xlate_eraseat(const struct region_device *rd, + size_t offset, size_t size) +{ + struct region req = { + .offset = offset, + .size = size, + }; + const struct xlate_region_device *xldev; + + xldev = container_of(rd, __typeof__(*xldev), rdev); + + if (!is_subregion(&xldev->sub_region, &req)) + return -1; + + offset -= region_offset(&xldev->sub_region); + + return rdev_eraseat(xldev->access_dev, offset, size); +} + +const struct region_device_ops xlate_rdev_ro_ops = { + .mmap = xlate_mmap, + .munmap = xlate_munmap, + .readat = xlate_readat, +}; + +const struct region_device_ops xlate_rdev_rw_ops = { .mmap = xlate_mmap, .munmap = xlate_munmap, .readat = xlate_readat, + .writeat = xlate_writeat, + .eraseat = xlate_eraseat, }; diff --git a/src/cpu/ti/am335x/bootblock_media.c b/src/cpu/ti/am335x/bootblock_media.c index a1fc818d21..2c208b98a1 100644 --- a/src/cpu/ti/am335x/bootblock_media.c +++ b/src/cpu/ti/am335x/bootblock_media.c @@ -18,7 +18,7 @@ /* FIXME: No idea how big the internal SRAM actually is. */ static const struct mem_region_device boot_dev = - MEM_REGION_DEV_INIT(_dram, CONFIG_ROM_SIZE); + MEM_REGION_DEV_RO_INIT(_dram, CONFIG_ROM_SIZE); const struct region_device *boot_device_ro(void) { diff --git a/src/lib/prog_loaders.c b/src/lib/prog_loaders.c index 38dfc2b5df..ecbc679f2a 100644 --- a/src/lib/prog_loaders.c +++ b/src/lib/prog_loaders.c @@ -30,7 +30,8 @@ #include <timestamp.h> /* Only can represent up to 1 byte less than size_t. */ -const struct mem_region_device addrspace_32bit = MEM_REGION_DEV_INIT(0, ~0UL); +const struct mem_region_device addrspace_32bit = + MEM_REGION_DEV_RO_INIT(0, ~0UL); int prog_locate(struct prog *prog) { diff --git a/src/mainboard/emulation/qemu-armv7/media.c b/src/mainboard/emulation/qemu-armv7/media.c index e9feaf43c7..71f3c75ef3 100644 --- a/src/mainboard/emulation/qemu-armv7/media.c +++ b/src/mainboard/emulation/qemu-armv7/media.c @@ -16,7 +16,7 @@ /* Maps directly to NOR flash up to rom size. */ static const struct mem_region_device boot_dev = - MEM_REGION_DEV_INIT((void *)0x0, CONFIG_ROM_SIZE); + MEM_REGION_DEV_RO_INIT((void *)0x0, CONFIG_ROM_SIZE); const struct region_device *boot_device_ro(void) { diff --git a/src/soc/intel/apollolake/mmap_boot.c b/src/soc/intel/apollolake/mmap_boot.c index d434cfacda..bf2e5b95b4 100644 --- a/src/soc/intel/apollolake/mmap_boot.c +++ b/src/soc/intel/apollolake/mmap_boot.c @@ -102,10 +102,10 @@ static void bios_mmap_init(void) shadow_dev_ptr = car_get_var_ptr(&shadow_dev); real_dev_ptr = car_get_var_ptr(&real_dev); - mem_region_device_init(shadow_dev_ptr, (void *)base, + mem_region_device_ro_init(shadow_dev_ptr, (void *)base, bios_mapped_size); - xlate_region_device_init(real_dev_ptr, &shadow_dev_ptr->rdev, + xlate_region_device_ro_init(real_dev_ptr, &shadow_dev_ptr->rdev, start, bios_mapped_size, CONFIG_ROM_SIZE); diff --git a/src/soc/samsung/exynos5250/alternate_cbfs.c b/src/soc/samsung/exynos5250/alternate_cbfs.c index 2e290c82c4..a3431e28fd 100644 --- a/src/soc/samsung/exynos5250/alternate_cbfs.c +++ b/src/soc/samsung/exynos5250/alternate_cbfs.c @@ -108,7 +108,8 @@ static int sdmmc_cbfs_open(void) return 0; } -static struct mem_region_device alternate_rdev = MEM_REGION_DEV_INIT(NULL, 0); +static struct mem_region_device alternate_rdev = + MEM_REGION_DEV_RO_INIT(NULL, 0); const struct region_device *boot_device_ro(void) { @@ -129,7 +130,8 @@ const struct region_device *boot_device_ro(void) void boot_device_init(void) { - mem_region_device_init(&alternate_rdev, _cbfs_cache, _cbfs_cache_size); + mem_region_device_ro_init(&alternate_rdev, _cbfs_cache, + _cbfs_cache_size); if (*iram_secondary_base == SECONDARY_BASE_BOOT_USB) { printk(BIOS_DEBUG, "Using Exynos alternate boot mode USB A-A\n"); diff --git a/src/soc/samsung/exynos5420/alternate_cbfs.c b/src/soc/samsung/exynos5420/alternate_cbfs.c index 20fc317fce..3a40154c11 100644 --- a/src/soc/samsung/exynos5420/alternate_cbfs.c +++ b/src/soc/samsung/exynos5420/alternate_cbfs.c @@ -115,7 +115,8 @@ static int sdmmc_cbfs_open(void) return 0; } -static struct mem_region_device alternate_rdev = MEM_REGION_DEV_INIT(NULL, 0); +static struct mem_region_device alternate_rdev = + MEM_REGION_DEV_RO_INIT(NULL, 0); const struct region_device *boot_device_ro(void) { @@ -136,7 +137,8 @@ const struct region_device *boot_device_ro(void) void boot_device_init(void) { - mem_region_device_init(&alternate_rdev, _cbfs_cache, _cbfs_cache_size); + mem_region_device_ro_init(&alternate_rdev, _cbfs_cache, + _cbfs_cache_size); if (*iram_secondary_base == SECONDARY_BASE_BOOT_USB) { printk(BIOS_DEBUG, "Using Exynos alternate boot mode USB A-A\n"); |