Aaron Durbin (adurbin@google.com) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/9132
-gerrit
commit ce8690bbbf255c9ffc562a39e7cca150a2052286 Author: Aaron Durbin adurbin@chromium.org Date: Fri Mar 27 01:58:06 2015 -0500
regions: add mmap helper device
In order to facilitate platforms which need a buffer cache for performing boot device operations provide infrastructure to share the logic in managing the buffer and operations.
Change-Id: I45dd9f213029706ff92a3e5a2c9edd5e8b541e27 Signed-off-by: Aaron Durbin adurbin@chromium.org --- src/include/region.h | 15 +++++++++++++++ src/lib/region.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+)
diff --git a/src/include/region.h b/src/include/region.h index 45fd074..c4e74f2 100644 --- a/src/include/region.h +++ b/src/include/region.h @@ -22,6 +22,7 @@
#include <stdint.h> #include <stddef.h> +#include <mem_pool.h>
/* * Region support. @@ -113,4 +114,18 @@ extern const struct region_device_ops mem_rdev_ops; .rdev = REGION_DEV_INIT(&mem_rdev_ops, 0, (size_)), \ }
+struct mmap_helper_region_device { + struct mem_pool pool; + struct region_device rdev; +}; + +#define MMAP_HELPER_REGION_INIT(ops_, offset_, size_, cache_, cache_size_) \ + { \ + .pool = MEM_POOL_INIT((cache_), (cache_size_)), \ + .rdev = REGION_DEV_INIT((ops_), (offset_), (size_)), \ + } + +void *mmap_helper_rdev_mmap(const struct region_device *, size_t, size_t); +int mmap_helper_rdev_munmap(const struct region_device *, void *); + #endif /* _REGION_H_ */ diff --git a/src/lib/region.c b/src/lib/region.c index 848139d..6abf7c1 100644 --- a/src/lib/region.c +++ b/src/lib/region.c @@ -161,3 +161,35 @@ const struct region_device_ops mem_rdev_ops = { .munmap = mdev_munmap, .readat = mdev_readat, }; + +void *mmap_helper_rdev_mmap(const struct region_device *rd, size_t offset, + size_t size) +{ + struct mmap_helper_region_device *mdev; + void *mapping; + + mdev = container_of((void *)rd, typeof(*mdev), rdev); + + mapping = mem_pool_alloc(&mdev->pool, size); + + if (mapping == NULL) + return NULL; + + if (rd->ops->readat(rd, mapping, offset, size) != size) { + mem_pool_free(&mdev->pool, mapping); + return NULL; + } + + return mapping; +} + +int mmap_helper_rdev_munmap(const struct region_device *rd, void *mapping) +{ + struct mmap_helper_region_device *mdev; + + mdev = container_of((void *)rd, typeof(*mdev), rdev); + + mem_pool_free(&mdev->pool, mapping); + + return 0; +}