We have a few drivers that need to get a reserved memory region, request the region, and map the reserved memory with memremap(). Add an API to do this all in one function call. Cc: Evan Green <evgreen@xxxxxxxxxxxx> Cc: Rob Herring <robh+dt@xxxxxxxxxx> Cc: Bjorn Andersson <bjorn.andersson@xxxxxxxxxx> Cc: Andy Gross <agross@xxxxxxxxxx> Cc: Will Deacon <will.deacon@xxxxxxx> Cc: Catalin Marinas <catalin.marinas@xxxxxxx> Cc: Dan Williams <dan.j.williams@xxxxxxxxx> Signed-off-by: Stephen Boyd <swboyd@xxxxxxxxxxxx> --- drivers/of/of_reserved_mem.c | 45 +++++++++++++++++++++++++++++++++ include/linux/of_reserved_mem.h | 6 +++++ 2 files changed, 51 insertions(+) diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c index 6a36bc0b3d64..39ab2bfd40d7 100644 --- a/drivers/of/of_reserved_mem.c +++ b/drivers/of/of_reserved_mem.c @@ -12,6 +12,7 @@ #define pr_fmt(fmt) "OF: reserved mem: " fmt #include <linux/err.h> +#include <linux/io.h> #include <linux/of.h> #include <linux/of_fdt.h> #include <linux/of_platform.h> @@ -395,3 +396,47 @@ struct reserved_mem *of_reserved_mem_lookup(struct device_node *np) return NULL; } EXPORT_SYMBOL_GPL(of_reserved_mem_lookup); + +/** + * devm_memremap_reserved_mem() - acquire reserved_mem from a device node, + * request and memremap it + * @dev: device with node pointer of the desired reserved-memory region + * @flags: flags to pass to memremap() + * + * This function allows drivers to acquire a reference to the reserved_mem + * struct based on the device's device_node handle, request it and then + * memremap() it. + * + * Returns: A remapped reserved memory region, or an error pointer on failure. + */ +void *devm_memremap_reserved_mem(struct device *dev, unsigned long flags) +{ + void *dest_ptr; + struct reserved_mem *rmem; + struct resource *res; + const char *name; + + rmem = of_reserved_mem_lookup(dev->of_node); + if (!rmem) { + dev_err(dev, "failed to acquire memory region\n"); + return ERR_PTR(-ENODEV); + } + + name = rmem->name ? : dev_name(dev); + + res = devm_request_mem_region(dev, rmem->base, rmem->size, name); + if (!res) { + dev_err(dev, "can't request region for reserved memory\n"); + return ERR_PTR(-EBUSY); + } + + dest_ptr = devm_memremap(dev, rmem->base, rmem->size, flags); + if (!dest_ptr) { + dev_err(dev, "memremap failed for reserved memory\n"); + devm_release_mem_region(dev, rmem->base, rmem->size); + dest_ptr = ERR_PTR(-ENOMEM); + } + + return dest_ptr; +} +EXPORT_SYMBOL_GPL(devm_memremap_reserved_mem); diff --git a/include/linux/of_reserved_mem.h b/include/linux/of_reserved_mem.h index 60f541912ccf..a36be60ef67c 100644 --- a/include/linux/of_reserved_mem.h +++ b/include/linux/of_reserved_mem.h @@ -39,6 +39,7 @@ void fdt_init_reserved_mem(void); void fdt_reserved_mem_save_node(unsigned long node, const char *uname, phys_addr_t base, phys_addr_t size); struct reserved_mem *of_reserved_mem_lookup(struct device_node *np); +void *devm_memremap_reserved_mem(struct device *dev, unsigned long flags); #else static inline int of_reserved_mem_device_init_by_idx(struct device *dev, struct device_node *np, int idx) @@ -54,6 +55,11 @@ static inline struct reserved_mem *of_reserved_mem_lookup(struct device_node *np { return NULL; } +static inline void *devm_memremap_reserved_mem(struct device *dev, + unsigned long flags) +{ + return NULL; +} #endif /** -- Sent by a computer through tubes