summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/include/region.h19
-rw-r--r--src/lib/region.c42
2 files changed, 61 insertions, 0 deletions
diff --git a/src/include/region.h b/src/include/region.h
index cb911023b3..ed0ad57a28 100644
--- a/src/include/region.h
+++ b/src/include/region.h
@@ -97,4 +97,23 @@ static inline size_t region_sz(const struct region *r)
return r->size;
}
+struct mem_region_device {
+ char *base;
+ 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,
+ size_t size);
+
+extern const struct region_device_ops mem_rdev_ops;
+
+/* Statically initialize mem_region_device. */
+#define MEM_REGION_DEV_INIT(base_, size_) \
+ { \
+ .base = (void *)(base_), \
+ .rdev = REGION_DEV_INIT(&mem_rdev_ops, 0, (size_)), \
+ }
+
#endif /* _REGION_H_ */
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,
+};