From: 黄涛 Date: Mon, 18 Nov 2013 02:05:43 +0000 (+0800) Subject: lib: devres: Add exec versions of devm_ioremap_resource and friends X-Git-Tag: firefly_0821_release~6493 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=336430a16bbf240ba9305723c38384caa1f785a5;p=firefly-linux-kernel-4.4.55.git lib: devres: Add exec versions of devm_ioremap_resource and friends Now that there is an _exec version of ioremap, add devm support for it. Signed-off-by: Russ Dill --- diff --git a/include/linux/device.h b/include/linux/device.h index c0a126125325..286139e77675 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -576,6 +576,11 @@ void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res); void __iomem *devm_request_and_ioremap(struct device *dev, struct resource *res); +void __iomem *devm_ioremap_exec_resource(struct device *dev, + struct resource *res); +void __iomem *devm_request_and_ioremap_exec(struct device *dev, + struct resource *res); + /* allows to add/remove a custom action to devres stack */ int devm_add_action(struct device *dev, void (*action)(void *), void *data); void devm_remove_action(struct device *dev, void (*action)(void *), void *data); diff --git a/include/linux/io.h b/include/linux/io.h index 069e4075f872..5849899f7bfe 100644 --- a/include/linux/io.h +++ b/include/linux/io.h @@ -62,6 +62,10 @@ void __iomem *devm_ioremap(struct device *dev, resource_size_t offset, unsigned long size); void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset, unsigned long size); +void __iomem *devm_ioremap_exec(struct device *dev, resource_size_t offset, + unsigned long size); +void __iomem *devm_ioremap_exec_nocache(struct device *dev, resource_size_t offset, + unsigned long size); void devm_iounmap(struct device *dev, void __iomem *addr); int check_signature(const volatile void __iomem *io_addr, const unsigned char *signature, int length); diff --git a/lib/devres.c b/lib/devres.c index 823533138fa0..6eb6c3b703d8 100644 --- a/lib/devres.c +++ b/lib/devres.c @@ -71,6 +71,64 @@ void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset, } EXPORT_SYMBOL(devm_ioremap_nocache); +/** + * devm_ioremap_exec - Managed ioremap_exec() + * @dev: Generic device to remap IO address for + * @offset: BUS offset to map + * @size: Size of map + * + * Managed ioremap_exec(). Map is automatically unmapped on driver detach. + */ +void __iomem *devm_ioremap_exec(struct device *dev, resource_size_t offset, + unsigned long size) +{ + void __iomem **ptr, *addr; + + ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return NULL; + + addr = ioremap_exec(offset, size); + if (addr) { + *ptr = addr; + devres_add(dev, ptr); + } else + devres_free(ptr); + + return addr; +} +EXPORT_SYMBOL(devm_ioremap_exec); + +/** + * devm_ioremap_exec_nocache - Managed ioremap_exec_nocache() + * @dev: Generic device to remap IO address for + * @offset: BUS offset to map + * @size: Size of map + * + * Managed ioremap_exec_nocache(). Map is automatically unmapped on driver + * detach. + */ +void __iomem *devm_ioremap_exec_nocache(struct device *dev, + resource_size_t offset, + unsigned long size) +{ + void __iomem **ptr, *addr; + + ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL); + if (!ptr) + return NULL; + + addr = ioremap_exec_nocache(offset, size); + if (addr) { + *ptr = addr; + devres_add(dev, ptr); + } else + devres_free(ptr); + + return addr; +} +EXPORT_SYMBOL(devm_ioremap_exec_nocache); + /** * devm_iounmap - Managed iounmap() * @dev: Generic device to unmap for @@ -140,6 +198,43 @@ void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res) } EXPORT_SYMBOL(devm_ioremap_resource); +void __iomem *devm_ioremap_exec_resource(struct device *dev, + struct resource *res) +{ + resource_size_t size; + const char *name; + void __iomem *dest_ptr; + + BUG_ON(!dev); + + if (!res || resource_type(res) != IORESOURCE_MEM) { + dev_err(dev, "invalid resource\n"); + return ERR_PTR(-EINVAL); + } + + size = resource_size(res); + name = res->name ?: dev_name(dev); + + if (!devm_request_mem_region(dev, res->start, size, name)) { + dev_err(dev, "can't request region for resource %pR\n", res); + return ERR_PTR(-EBUSY); + } + + if (res->flags & IORESOURCE_CACHEABLE) + dest_ptr = devm_ioremap_exec(dev, res->start, size); + else + dest_ptr = devm_ioremap_exec_nocache(dev, res->start, size); + + if (!dest_ptr) { + dev_err(dev, "ioremap failed for resource %pR\n", res); + devm_release_mem_region(dev, res->start, size); + dest_ptr = ERR_PTR(-ENOMEM); + } + + return dest_ptr; +} +EXPORT_SYMBOL(devm_ioremap_exec_resource); + /** * devm_request_and_ioremap() - Check, request region, and ioremap resource * @dev: Generic device to handle the resource for @@ -168,6 +263,34 @@ void __iomem *devm_request_and_ioremap(struct device *device, } EXPORT_SYMBOL(devm_request_and_ioremap); +/** + * devm_request_and_ioremap_exec() - Check, request region, and ioremap resource + * @dev: Generic device to handle the resource for + * @res: resource to be handled + * + * Takes all necessary steps to ioremap a mem resource. Uses managed device, so + * everything is undone on driver detach. Checks arguments, so you can feed + * it the result from e.g. platform_get_resource() directly. Returns the + * remapped pointer or NULL on error. Usage example: + * + * res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + * base = devm_request_and_ioremap_exec(&pdev->dev, res); + * if (!base) + * return -EADDRNOTAVAIL; + */ +void __iomem *devm_request_and_ioremap_exec(struct device *device, + struct resource *res) +{ + void __iomem *dest_ptr; + + dest_ptr = devm_ioremap_exec_resource(device, res); + if (IS_ERR(dest_ptr)) + return NULL; + + return dest_ptr; +} +EXPORT_SYMBOL(devm_request_and_ioremap_exec); + #ifdef CONFIG_HAS_IOPORT /* * Generic iomap devres