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>
This commit is contained in:
Antonello Dettori 2016-06-22 21:09:08 +02:00 committed by Martin Roth
parent 5992afa57d
commit e5f48d20e7
11 changed files with 237 additions and 34 deletions

View File

@ -18,7 +18,7 @@
/* This assumes that the CBFS resides at 0x0, which is true for the default /* This assumes that the CBFS resides at 0x0, which is true for the default
* configuration. */ * configuration. */
static const struct mem_region_device boot_dev = 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) const struct region_device *boot_device_ro(void)
{ {

View File

@ -18,7 +18,7 @@
/* This assumes that the CBFS resides at 0x0, which is true for the default /* This assumes that the CBFS resides at 0x0, which is true for the default
* configuration. */ * configuration. */
static const struct mem_region_device boot_dev = 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) const struct region_device *boot_device_ro(void)
{ {

View File

@ -22,7 +22,7 @@
#define rom_base ((void *)(uintptr_t)(0x100000000ULL-CONFIG_ROM_SIZE)) #define rom_base ((void *)(uintptr_t)(0x100000000ULL-CONFIG_ROM_SIZE))
static const struct mem_region_device boot_dev = 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) const struct region_device *boot_device_ro(void)
{ {

View File

@ -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, ssize_t rdev_readat(const struct region_device *rd, void *b, size_t offset,
size_t size); 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 * * Implementation of a region device *
@ -67,6 +81,9 @@ struct region_device_ops {
void *(*mmap)(const struct region_device *, size_t, size_t); void *(*mmap)(const struct region_device *, size_t, size_t);
int (*munmap)(const struct region_device *, void *); int (*munmap)(const struct region_device *, void *);
ssize_t (*readat)(const struct region_device *, void *, size_t, size_t); 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 { struct region {
@ -139,26 +156,38 @@ struct mem_region_device {
struct region_device rdev; struct region_device rdev;
}; };
/* Iniitalize at runtime a mem_region_device. This would be used when /* 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. */ * 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, * 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); 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. */ /* Statically initialize mem_region_device. */
#define MEM_REGION_DEV_INIT(base_, size_) \ #define MEM_REGION_DEV_INIT(base_, size_, ops_) \
{ \ { \
.base = (void *)(base_), \ .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 mmap_helper_region_device {
struct mem_pool pool; struct mem_pool pool;
struct region_device rdev; 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_)), \ .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 *); int mmap_helper_rdev_munmap(const struct region_device *, void *);
/* A translated region device provides the ability to publish a region device /* 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 * space. The sub region is the window within the 1st address space and
* the request is modified prior to accessing the second address space * the request is modified prior to accessing the second address space
* provided by access_dev. */ * provided by access_dev. */
@ -180,20 +209,38 @@ struct xlate_region_device {
struct region_device rdev; struct region_device rdev;
}; };
extern const struct region_device_ops xlate_rdev_ops; extern const struct region_device_ops xlate_rdev_ro_ops;
#define XLATE_REGION_DEV_INIT(access_dev_, sub_offset_, sub_size_, parent_sz_) \ extern const struct region_device_ops xlate_rdev_rw_ops;
#define XLATE_REGION_DEV_INIT(access_dev_, sub_offset_, sub_size_, \
parent_sz_, ops_) \
{ \ { \
.access_dev = access_dev_, \ .access_dev = access_dev_, \
.sub_region = { \ .sub_region = { \
.offset = (sub_offset_), \ .offset = (sub_offset_), \
.size = (sub_size_), \ .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. */ /* 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, const struct region_device *access_dev,
size_t sub_offset, size_t sub_size, size_t sub_offset, size_t sub_size,
size_t parent_size); size_t parent_size);

View File

@ -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); 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, int rdev_chain(struct region_device *child, const struct region_device *parent,
size_t offset, size_t size) size_t offset, size_t size)
{ {
@ -124,15 +166,27 @@ int rdev_chain(struct region_device *child, const struct region_device *parent,
return 0; return 0;
} }
void mem_region_device_init(struct mem_region_device *mdev, void *base, static void mem_region_device_init(struct mem_region_device *mdev,
size_t size) const struct region_device_ops *ops, void *base, size_t size)
{ {
memset(mdev, 0, sizeof(*mdev)); memset(mdev, 0, sizeof(*mdev));
mdev->base = base; mdev->base = base;
mdev->rdev.ops = &mem_rdev_ops; mdev->rdev.ops = ops;
mdev->rdev.region.size = size; 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, void region_device_init(struct region_device *rdev,
const struct region_device_ops *ops, size_t offset, const struct region_device_ops *ops, size_t offset,
size_t size) size_t size)
@ -144,16 +198,35 @@ void region_device_init(struct region_device *rdev,
rdev->region.size = size; rdev->region.size = size;
} }
void xlate_region_device_init(struct xlate_region_device *xdev, static void xlate_region_device_init(struct xlate_region_device *xdev,
const struct region_device *access_dev, const struct region_device_ops *ops,
size_t sub_offset, size_t sub_size, const struct region_device *access_dev,
size_t parent_size) size_t sub_offset, size_t sub_size,
size_t parent_size)
{ {
memset(xdev, 0, sizeof(*xdev)); memset(xdev, 0, sizeof(*xdev));
xdev->access_dev = access_dev; xdev->access_dev = access_dev;
xdev->sub_region.offset = sub_offset; xdev->sub_region.offset = sub_offset;
xdev->sub_region.size = sub_size; 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, static void *mdev_mmap(const struct region_device *rd, size_t offset,
@ -184,12 +257,44 @@ static ssize_t mdev_readat(const struct region_device *rd, void *b,
return size; 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, .mmap = mdev_mmap,
.munmap = mdev_munmap, .munmap = mdev_munmap,
.readat = mdev_readat, .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, void mmap_helper_device_init(struct mmap_helper_region_device *mdev,
void *cache, size_t cache_size) void *cache, size_t cache_size)
{ {
@ -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); 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, .mmap = xlate_mmap,
.munmap = xlate_munmap, .munmap = xlate_munmap,
.readat = xlate_readat, .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,
};

View File

@ -18,7 +18,7 @@
/* FIXME: No idea how big the internal SRAM actually is. */ /* FIXME: No idea how big the internal SRAM actually is. */
static const struct mem_region_device boot_dev = 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) const struct region_device *boot_device_ro(void)
{ {

View File

@ -30,7 +30,8 @@
#include <timestamp.h> #include <timestamp.h>
/* Only can represent up to 1 byte less than size_t. */ /* 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) int prog_locate(struct prog *prog)
{ {

View File

@ -16,7 +16,7 @@
/* Maps directly to NOR flash up to rom size. */ /* Maps directly to NOR flash up to rom size. */
static const struct mem_region_device boot_dev = 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) const struct region_device *boot_device_ro(void)
{ {

View File

@ -102,10 +102,10 @@ static void bios_mmap_init(void)
shadow_dev_ptr = car_get_var_ptr(&shadow_dev); shadow_dev_ptr = car_get_var_ptr(&shadow_dev);
real_dev_ptr = car_get_var_ptr(&real_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); 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, start, bios_mapped_size,
CONFIG_ROM_SIZE); CONFIG_ROM_SIZE);

View File

@ -108,7 +108,8 @@ static int sdmmc_cbfs_open(void)
return 0; 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) 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) 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) { if (*iram_secondary_base == SECONDARY_BASE_BOOT_USB) {
printk(BIOS_DEBUG, "Using Exynos alternate boot mode USB A-A\n"); printk(BIOS_DEBUG, "Using Exynos alternate boot mode USB A-A\n");

View File

@ -115,7 +115,8 @@ static int sdmmc_cbfs_open(void)
return 0; 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) 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) 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) { if (*iram_secondary_base == SECONDARY_BASE_BOOT_USB) {
printk(BIOS_DEBUG, "Using Exynos alternate boot mode USB A-A\n"); printk(BIOS_DEBUG, "Using Exynos alternate boot mode USB A-A\n");