Skip to contentSkip to conversation

New of_io_release_and_unmap()

RFC New Joshua Clayton 2 participants 4 messages Feb 25, 2015 Device tree

lore
Share & tools
lore
#0 lore raw

Hello folks,
I notice that io_request_and_map has no inverse. I would like to add a new function to undo what it does.

Joshua Clayton (1):
of/address: Add of_io_release_and_unmap()

 drivers/of/address.c       | 29 +++++++++++++++++++++++++++++
 include/linux/of_address.h |  2 ++
 2 files changed, 31 insertions(+)
Signature · 1 line
2.1.0
#1 lore raw

PATCH 1/1 of/address: Add of_io_release_and_unmap()

New Fumction of_io_release_an_unmap frees resources and io memory allocated by io_request_and_map()

---
 drivers/of/address.c       | 29 +++++++++++++++++++++++++++++
 include/linux/of_address.h |  2 ++
 2 files changed, 31 insertions(+)
drivers/of/address.c +29 -0
index ad29069..d0f3b81 100644
@@ -872,6 +872,7 @@ EXPORT_SYMBOL(of_iomap);
 /*
  * of_io_request_and_map - Requests a resource and maps the memory mapped IO
  *			   for a given device_node
+ *			   Call of_io_release_and_unmap() to free the memory
  * @device:	the device whose io range will be mapped
  * @index:	index of the io range
  * @name:	name of the resource
@@ -882,6 +883,10 @@ EXPORT_SYMBOL(of_iomap);
  *	base = of_io_request_and_map(node, 0, "foo");
  *	if (IS_ERR(base))
  *		return PTR_ERR(base);
+ *
+ * ...
+ *
+ *	return of_io_request_and_map(node, 0, base);
  */
 void __iomem *of_io_request_and_map(struct device_node *np, int index,
 					const char *name)
@@ -906,6 +911,30 @@ void __iomem *of_io_request_and_map(struct device_node *np, int index,
 EXPORT_SYMBOL(of_io_request_and_map);
 
 /**
+ * of_io_release_and_unmap - Unmap memory and release resource
+ *			   associated with a device node
+ * Use this function to free memory requested by of_io_request_and_map()
+ * See the Usage example for of_io_request_and_map
+ *
+ */
+int of_io_release_and_unmap(struct device_node *np, int index,
+				void __iomem *addr)
+{
+	struct resource res;
+	int ret = 0;
+
+	ret = of_address_to_resource(np, index, &res);
+	if (ret)
+		return ret;
+
+	iounmap(addr);
+	release_mem_region(res.start, resource_size(&res));
+
+	return ret;
+}
+EXPORT_SYMBOL(of_io_release_and_unmap);
+
+/**
  * of_dma_get_range - Get DMA range info
  * @np:		device node to get DMA range info
  * @dma_addr:	pointer to store initial DMA address of DMA range
include/linux/of_address.h +2 -0
index d88e81b..f62eb9e 100644
@@ -107,6 +107,8 @@ extern int of_address_to_resource(struct device_node *dev, int index,
 void __iomem *of_iomap(struct device_node *node, int index);
 void __iomem *of_io_request_and_map(struct device_node *device,
 					int index, const char *name);
+int of_io_release_and_unmap(struct device_node *np, int index,
+					void __iomem *addr);
 #else
 
 #include <linux/io.h>
Signature · 1 line
2.1.0
#2 lore raw
In reply to Joshua Clayton
Quoted 4 lines · On Wednesday 25 February 2015 11:20:41 Joshua Clayton wrote:

Hello folks,
I notice that io_request_and_map has no inverse. I would like to add a new function to undo what it does.

What do you want to use this for? So far all users are in essential drivers (clk and clocksource) that result in a boot failure if this goes wrong.

Normal drivers can probably always use devm_ioremap_resource(), which has an automatic cleanup.

Arnd

#3 lore raw
Quoted 9 lines · Arnd Bergmann
Quoted 4 lines · On Wednesday 25 February 2015 11:20:41 Joshua Clayton wrote:

Hello folks,
I notice that io_request_and_map has no inverse. I would like to add a new function to undo what it does.

What do you want to use this for? So far all users are in essential drivers (clk and clocksource) that result in a boot failure if this goes wrong.

That explains a lot. It looks (to my n00b eye) like this function leaks some references, which is more ok if the ref counters should never be decremented to 0.

Quoted 3 lines

Normal drivers can probably always use devm_ioremap_resource(), which has an automatic cleanup.

Hmmm.
My use case is indeed a normal driver, which could definitely use devm_ioremap_resource()

Quoted 1 line

Arnd

That would leave only "completeness" as a reason. Probably not needed. --
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html