Aaron Durbin (adurbin@google.com) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/9131
-gerrit
commit 226ffebfa78720dd3fbb286bdad2a9ab6c7f1d34 Author: Aaron Durbin adurbin@chromium.org Date: Fri Mar 27 01:03:45 2015 -0500
regions: add memory region device support
In order to make it easy and convenient for memory regions to be used provide an implementation that can be reused.
Change-Id: I266cd07bbfa16a427c2b31c512e7c87b77f47718 Signed-off-by: Aaron Durbin adurbin@chromium.org --- src/include/region.h | 16 ++++++++++++++++ src/lib/region.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+)
diff --git a/src/include/region.h b/src/include/region.h index 4f8cda1..45fd074 100644 --- a/src/include/region.h +++ b/src/include/region.h @@ -97,4 +97,20 @@ static inline size_t region_sz(const struct region *r) return r->size; }
+struct mem_region_device { + char *base; + struct region_device rdev; +}; + +void mem_region_device_init(struct mem_region_device *mdev, void *base, + size_t size); + +extern const struct region_device_ops mem_rdev_ops; + +#define MEM_REGION_DEV_INIT(base_, size_) \ + { \ + .base = (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 cf74784..848139d 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, +};