1
2#include <linux/device.h>
3#include <linux/types.h>
4#include <linux/io.h>
5#include <linux/mm.h>
6
7#ifndef ioremap_cache
8
9__weak void __iomem *ioremap_cache(resource_size_t offset, unsigned long size)
10{
11 return ioremap(offset, size);
12}
13#endif
14
15#ifndef arch_memremap_wb
16static void *arch_memremap_wb(resource_size_t offset, unsigned long size)
17{
18 return (__force void *)ioremap_cache(offset, size);
19}
20#endif
21
22#ifndef arch_memremap_can_ram_remap
23static bool arch_memremap_can_ram_remap(resource_size_t offset, size_t size,
24 unsigned long flags)
25{
26 return true;
27}
28#endif
29
30static void *try_ram_remap(resource_size_t offset, size_t size,
31 unsigned long flags)
32{
33 unsigned long pfn = PHYS_PFN(offset);
34
35
36 if (pfn_valid(pfn) && !PageHighMem(pfn_to_page(pfn)) &&
37 arch_memremap_can_ram_remap(offset, size, flags))
38 return __va(offset);
39
40 return NULL;
41}
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71void *memremap(resource_size_t offset, size_t size, unsigned long flags)
72{
73 int is_ram = region_intersects(offset, size,
74 IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
75 void *addr = NULL;
76
77 if (!flags)
78 return NULL;
79
80 if (is_ram == REGION_MIXED) {
81 WARN_ONCE(1, "memremap attempted on mixed range %pa size: %#lx\n",
82 &offset, (unsigned long) size);
83 return NULL;
84 }
85
86
87 if (flags & MEMREMAP_WB) {
88
89
90
91
92
93
94 if (is_ram == REGION_INTERSECTS)
95 addr = try_ram_remap(offset, size, flags);
96 if (!addr)
97 addr = arch_memremap_wb(offset, size);
98 }
99
100
101
102
103
104
105
106 if (!addr && is_ram == REGION_INTERSECTS && flags != MEMREMAP_WB) {
107 WARN_ONCE(1, "memremap attempted on ram %pa size: %#lx\n",
108 &offset, (unsigned long) size);
109 return NULL;
110 }
111
112 if (!addr && (flags & MEMREMAP_WT))
113 addr = ioremap_wt(offset, size);
114
115 if (!addr && (flags & MEMREMAP_WC))
116 addr = ioremap_wc(offset, size);
117
118 return addr;
119}
120EXPORT_SYMBOL(memremap);
121
122void memunmap(void *addr)
123{
124 if (is_ioremap_addr(addr))
125 iounmap((void __iomem *) addr);
126}
127EXPORT_SYMBOL(memunmap);
128
129static void devm_memremap_release(struct device *dev, void *res)
130{
131 memunmap(*(void **)res);
132}
133
134static int devm_memremap_match(struct device *dev, void *res, void *match_data)
135{
136 return *(void **)res == match_data;
137}
138
139void *devm_memremap(struct device *dev, resource_size_t offset,
140 size_t size, unsigned long flags)
141{
142 void **ptr, *addr;
143
144 ptr = devres_alloc_node(devm_memremap_release, sizeof(*ptr), GFP_KERNEL,
145 dev_to_node(dev));
146 if (!ptr)
147 return ERR_PTR(-ENOMEM);
148
149 addr = memremap(offset, size, flags);
150 if (addr) {
151 *ptr = addr;
152 devres_add(dev, ptr);
153 } else {
154 devres_free(ptr);
155 return ERR_PTR(-ENXIO);
156 }
157
158 return addr;
159}
160EXPORT_SYMBOL(devm_memremap);
161
162void devm_memunmap(struct device *dev, void *addr)
163{
164 WARN_ON(devres_release(dev, devm_memremap_release,
165 devm_memremap_match, addr));
166}
167EXPORT_SYMBOL(devm_memunmap);
168