summaryrefslogtreecommitdiff
path: root/src/lib/region.c
diff options
context:
space:
mode:
authorAaron Durbin <adurbin@chromium.org>2015-03-27 01:03:45 -0500
committerAaron Durbin <adurbin@chromium.org>2015-05-14 17:27:25 +0200
commitb419c1a87ccb2037a88ffb533b5b4a11cfa387c0 (patch)
tree35fc1d38aa3827a9e154a650353a702cf2c8e907 /src/lib/region.c
parent127525c772f82a214a13899a344c5fa24f4f10a8 (diff)
regions: add memory region device support
Provide common code for using memory-backed region devices. This allows in-memory buffers to act as a region device. Change-Id: I266cd07bbfa16a427c2b31c512e7c87b77f47718 Signed-off-by: Aaron Durbin <adurbin@chromium.org> Reviewed-on: http://review.coreboot.org/9131 Tested-by: build bot (Jenkins) Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
Diffstat (limited to 'src/lib/region.c')
-rw-r--r--src/lib/region.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/lib/region.c b/src/lib/region.c
index 948addd2ca..ca8cd7bcf7 100644
--- a/src/lib/region.c
+++ b/src/lib/region.c
@@ -119,3 +119,45 @@ 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)
+{
+ memset(mdev, 0, sizeof(*mdev));
+ mdev->base = base;
+ mdev->rdev.ops = &mem_rdev_ops;
+ mdev->rdev.region.size = size;
+}
+
+static void *mdev_mmap(const struct region_device *rd, size_t offset,
+ size_t size)
+{
+ const struct mem_region_device *mdev;
+
+ mdev = container_of(rd, typeof(*mdev), rdev);
+
+ return &mdev->base[offset];
+}
+
+static int mdev_munmap(const struct region_device *rd, void *mapping)
+{
+ return 0;
+}
+
+static ssize_t mdev_readat(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(b, &mdev->base[offset], size);
+
+ return size;
+}
+
+const struct region_device_ops mem_rdev_ops = {
+ .mmap = mdev_mmap,
+ .munmap = mdev_munmap,
+ .readat = mdev_readat,
+};