xref: /linux/kernel/iomem.c (revision ab52c59103002b49f2455371e4b9c56ba3ef1781)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <linux/device.h>
3 #include <linux/types.h>
4 #include <linux/io.h>
5 #include <linux/mm.h>
6 #include <linux/ioremap.h>
7 
8 #ifndef arch_memremap_wb
9 static void *arch_memremap_wb(resource_size_t offset, unsigned long size)
10 {
11 #ifdef ioremap_cache
12 	return (__force void *)ioremap_cache(offset, size);
13 #else
14 	return (__force void *)ioremap(offset, size);
15 #endif
16 }
17 #endif
18 
19 #ifndef arch_memremap_can_ram_remap
20 static bool arch_memremap_can_ram_remap(resource_size_t offset, size_t size,
21 					unsigned long flags)
22 {
23 	return true;
24 }
25 #endif
26 
27 static void *try_ram_remap(resource_size_t offset, size_t size,
28 			   unsigned long flags)
29 {
30 	unsigned long pfn = PHYS_PFN(offset);
31 
32 	/* In the simple case just return the existing linear address */
33 	if (pfn_valid(pfn) && !PageHighMem(pfn_to_page(pfn)) &&
34 	    arch_memremap_can_ram_remap(offset, size, flags))
35 		return __va(offset);
36 
37 	return NULL; /* fallback to arch_memremap_wb */
38 }
39 
40 /**
41  * memremap() - remap an iomem_resource as cacheable memory
42  * @offset: iomem resource start address
43  * @size: size of remap
44  * @flags: any of MEMREMAP_WB, MEMREMAP_WT, MEMREMAP_WC,
45  *		  MEMREMAP_ENC, MEMREMAP_DEC
46  *
47  * memremap() is "ioremap" for cases where it is known that the resource
48  * being mapped does not have i/o side effects and the __iomem
49  * annotation is not applicable. In the case of multiple flags, the different
50  * mapping types will be attempted in the order listed below until one of
51  * them succeeds.
52  *
53  * MEMREMAP_WB - matches the default mapping for System RAM on
54  * the architecture.  This is usually a read-allocate write-back cache.
55  * Moreover, if MEMREMAP_WB is specified and the requested remap region is RAM
56  * memremap() will bypass establishing a new mapping and instead return
57  * a pointer into the direct map.
58  *
59  * MEMREMAP_WT - establish a mapping whereby writes either bypass the
60  * cache or are written through to memory and never exist in a
61  * cache-dirty state with respect to program visibility.  Attempts to
62  * map System RAM with this mapping type will fail.
63  *
64  * MEMREMAP_WC - establish a writecombine mapping, whereby writes may
65  * be coalesced together (e.g. in the CPU's write buffers), but is otherwise
66  * uncached. Attempts to map System RAM with this mapping type will fail.
67  */
68 void *memremap(resource_size_t offset, size_t size, unsigned long flags)
69 {
70 	int is_ram = region_intersects(offset, size,
71 				       IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
72 	void *addr = NULL;
73 
74 	if (!flags)
75 		return NULL;
76 
77 	if (is_ram == REGION_MIXED) {
78 		WARN_ONCE(1, "memremap attempted on mixed range %pa size: %#lx\n",
79 				&offset, (unsigned long) size);
80 		return NULL;
81 	}
82 
83 	/* Try all mapping types requested until one returns non-NULL */
84 	if (flags & MEMREMAP_WB) {
85 		/*
86 		 * MEMREMAP_WB is special in that it can be satisfied
87 		 * from the direct map.  Some archs depend on the
88 		 * capability of memremap() to autodetect cases where
89 		 * the requested range is potentially in System RAM.
90 		 */
91 		if (is_ram == REGION_INTERSECTS)
92 			addr = try_ram_remap(offset, size, flags);
93 		if (!addr)
94 			addr = arch_memremap_wb(offset, size);
95 	}
96 
97 	/*
98 	 * If we don't have a mapping yet and other request flags are
99 	 * present then we will be attempting to establish a new virtual
100 	 * address mapping.  Enforce that this mapping is not aliasing
101 	 * System RAM.
102 	 */
103 	if (!addr && is_ram == REGION_INTERSECTS && flags != MEMREMAP_WB) {
104 		WARN_ONCE(1, "memremap attempted on ram %pa size: %#lx\n",
105 				&offset, (unsigned long) size);
106 		return NULL;
107 	}
108 
109 	if (!addr && (flags & MEMREMAP_WT))
110 		addr = ioremap_wt(offset, size);
111 
112 	if (!addr && (flags & MEMREMAP_WC))
113 		addr = ioremap_wc(offset, size);
114 
115 	return addr;
116 }
117 EXPORT_SYMBOL(memremap);
118 
119 void memunmap(void *addr)
120 {
121 	if (is_ioremap_addr(addr))
122 		iounmap((void __iomem *) addr);
123 }
124 EXPORT_SYMBOL(memunmap);
125 
126 static void devm_memremap_release(struct device *dev, void *res)
127 {
128 	memunmap(*(void **)res);
129 }
130 
131 static int devm_memremap_match(struct device *dev, void *res, void *match_data)
132 {
133 	return *(void **)res == match_data;
134 }
135 
136 void *devm_memremap(struct device *dev, resource_size_t offset,
137 		size_t size, unsigned long flags)
138 {
139 	void **ptr, *addr;
140 
141 	ptr = devres_alloc_node(devm_memremap_release, sizeof(*ptr), GFP_KERNEL,
142 			dev_to_node(dev));
143 	if (!ptr)
144 		return ERR_PTR(-ENOMEM);
145 
146 	addr = memremap(offset, size, flags);
147 	if (addr) {
148 		*ptr = addr;
149 		devres_add(dev, ptr);
150 	} else {
151 		devres_free(ptr);
152 		return ERR_PTR(-ENXIO);
153 	}
154 
155 	return addr;
156 }
157 EXPORT_SYMBOL(devm_memremap);
158 
159 void devm_memunmap(struct device *dev, void *addr)
160 {
161 	WARN_ON(devres_release(dev, devm_memremap_release,
162 				devm_memremap_match, addr));
163 }
164 EXPORT_SYMBOL(devm_memunmap);
165