1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <linux/device.h>
3 #include <linux/types.h>
6 #include <linux/ioremap.h>
8 #ifndef arch_memremap_wb
9 static void *arch_memremap_wb(resource_size_t offset, unsigned long size)
12 return (__force void *)ioremap_cache(offset, size);
14 return (__force void *)ioremap(offset, size);
19 #ifndef arch_memremap_can_ram_remap
20 static bool arch_memremap_can_ram_remap(resource_size_t offset, size_t size,
27 static void *try_ram_remap(resource_size_t offset, size_t size,
30 unsigned long pfn = PHYS_PFN(offset);
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))
37 return NULL; /* fallback to arch_memremap_wb */
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
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
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.
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.
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.
68 void *memremap(resource_size_t offset, size_t size, unsigned long flags)
70 int is_ram = region_intersects(offset, size,
71 IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
77 if (is_ram == REGION_MIXED) {
78 WARN_ONCE(1, "memremap attempted on mixed range %pa size: %#lx\n",
79 &offset, (unsigned long) size);
83 /* Try all mapping types requested until one returns non-NULL */
84 if (flags & MEMREMAP_WB) {
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.
91 if (is_ram == REGION_INTERSECTS)
92 addr = try_ram_remap(offset, size, flags);
94 addr = arch_memremap_wb(offset, size);
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
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);
109 if (!addr && (flags & MEMREMAP_WT))
110 addr = ioremap_wt(offset, size);
112 if (!addr && (flags & MEMREMAP_WC))
113 addr = ioremap_wc(offset, size);
117 EXPORT_SYMBOL(memremap);
119 void memunmap(void *addr)
121 if (is_ioremap_addr(addr))
122 iounmap((void __iomem *) addr);
124 EXPORT_SYMBOL(memunmap);
126 static void devm_memremap_release(struct device *dev, void *res)
128 memunmap(*(void **)res);
131 static int devm_memremap_match(struct device *dev, void *res, void *match_data)
133 return *(void **)res == match_data;
136 void *devm_memremap(struct device *dev, resource_size_t offset,
137 size_t size, unsigned long flags)
141 ptr = devres_alloc_node(devm_memremap_release, sizeof(*ptr), GFP_KERNEL,
144 return ERR_PTR(-ENOMEM);
146 addr = memremap(offset, size, flags);
149 devres_add(dev, ptr);
152 return ERR_PTR(-ENXIO);
157 EXPORT_SYMBOL(devm_memremap);
159 void devm_memunmap(struct device *dev, void *addr)
161 WARN_ON(devres_release(dev, devm_memremap_release,
162 devm_memremap_match, addr));
164 EXPORT_SYMBOL(devm_memunmap);