[coreboot-gerrit] Patch set updated for coreboot: region: Add writeat and eraseat support

Antonello Dettori (dev@dettori.io) gerrit at coreboot.org
Thu Jun 23 21:36:20 CEST 2016


Antonello Dettori (dev at dettori.io) just uploaded a new patch set to gerrit, which you can find at https://review.coreboot.org/15318

-gerrit

commit 8e0a517b474964e991589171269aea9a01812e40
Author: Antonello Dettori <dev at dettori.io>
Date:   Wed Jun 22 21:09:08 2016 +0200

    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 at dettori.io>
---
 src/arch/power8/rom_media.c                 |   2 +-
 src/arch/riscv/rom_media.c                  |   2 +-
 src/arch/x86/mmap_boot.c                    |   2 +-
 src/commonlib/include/commonlib/region.h    |  73 ++++++++++--
 src/commonlib/region.c                      | 169 ++++++++++++++++++++++++++--
 src/cpu/ti/am335x/bootblock_media.c         |   2 +-
 src/lib/prog_loaders.c                      |   3 +-
 src/mainboard/emulation/qemu-armv7/media.c  |   2 +-
 src/soc/intel/apollolake/mmap_boot.c        |   4 +-
 src/soc/samsung/exynos5250/alternate_cbfs.c |   6 +-
 src/soc/samsung/exynos5420/alternate_cbfs.c |   6 +-
 11 files changed, 240 insertions(+), 31 deletions(-)

diff --git a/src/arch/power8/rom_media.c b/src/arch/power8/rom_media.c
index 0c54e7a..c171307 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 0c54e7a..c171307 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 ae35451..35a7544 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 634132a..5339259 100644
--- a/src/commonlib/include/commonlib/region.h
+++ b/src/commonlib/include/commonlib/region.h
@@ -48,6 +48,19 @@ 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 wrote at provided
+ * offset from the buffer passed.
+ */
+ssize_t rdev_eraseat(const struct region_device *rd, size_t offset,
+			size_t size);
 
 /****************************************
  *  Implementation of a region device   *
@@ -67,6 +80,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,18 +155,30 @@ 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_RO_INIT(base_, size_)				\
+	{								\
+		.base = (void *)(base_),				\
+		.rdev = REGION_DEV_INIT(&mem_rdev_ro_ops, 0, (size_)),	\
+	}
+
+#define MEM_REGION_DEV_RW_INIT(base_, size_)				\
 	{								\
 		.base = (void *)(base_),				\
-		.rdev = REGION_DEV_INIT(&mem_rdev_ops, 0, (size_)),	\
+		.rdev = REGION_DEV_INIT(&mem_rdev_rw_ops, 0, (size_)),	\
 	}
 
 struct mmap_helper_region_device {
@@ -170,7 +198,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 +208,45 @@ 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;
 
-#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_RO_INIT(access_dev_, sub_offset_, sub_size_, \
+		parent_sz_) \
+	{								\
+		.access_dev = access_dev_,				\
+		.sub_region = {						\
+			.offset = (sub_offset_),			\
+			.size = (sub_size_),				\
+		},							\
+		.rdev = REGION_DEV_INIT(&xlate_rdev_ro_ops, 0,  (parent_sz_)),\
+	}
+
+#define XLATE_REGION_DEV_RW_INIT(access_dev_, sub_offset_, sub_size_, \
+		parent_sz_) \
 	{								\
 		.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(&xlate_rdev_rw_ops, 0,  (parent_sz_)),\
 	}
 
 /* Helper to dynamically initialize xlate region device. */
 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);
+
+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 6f71074..10df05e 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)
@@ -145,15 +199,34 @@ void region_device_init(struct region_device *rdev,
 }
 
 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)
+			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 a1fc818..2c208b9 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 38dfc2b..ecbc679 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 e9feaf4..71f3c75 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 d3c39ec..b6e33a2 100644
--- a/src/soc/intel/apollolake/mmap_boot.c
+++ b/src/soc/intel/apollolake/mmap_boot.c
@@ -106,10 +106,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 2e290c8..a3431e2 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 20fc317..3a40154 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");



More information about the coreboot-gerrit mailing list