1 /* SPDX-License-Identifier: GPL-2.0 */ 1 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 10 { 11 #ifdef ioremap_cache 12 return (__force void *)ioremap_cache(o 13 #else 14 return (__force void *)ioremap(offset, 15 #endif 16 } 17 #endif 18 19 #ifndef arch_memremap_can_ram_remap 20 static bool arch_memremap_can_ram_remap(resour 21 unsign 22 { 23 return true; 24 } 25 #endif 26 27 static void *try_ram_remap(resource_size_t off 28 unsigned long flags 29 { 30 unsigned long pfn = PHYS_PFN(offset); 31 32 /* In the simple case just return the 33 if (pfn_valid(pfn) && !PageHighMem(pfn 34 arch_memremap_can_ram_remap(offset 35 return __va(offset); 36 37 return NULL; /* fallback to arch_memre 38 } 39 40 /** 41 * memremap() - remap an iomem_resource as cac 42 * @offset: iomem resource start address 43 * @size: size of remap 44 * @flags: any of MEMREMAP_WB, MEMREMAP_WT, ME 45 * MEMREMAP_ENC, MEMREMAP_DEC 46 * 47 * memremap() is "ioremap" for cases where it 48 * being mapped does not have i/o side effects 49 * annotation is not applicable. In the case o 50 * mapping types will be attempted in the orde 51 * them succeeds. 52 * 53 * MEMREMAP_WB - matches the default mapping f 54 * the architecture. This is usually a read-a 55 * Moreover, if MEMREMAP_WB is specified and t 56 * memremap() will bypass establishing a new m 57 * a pointer into the direct map. 58 * 59 * MEMREMAP_WT - establish a mapping whereby w 60 * cache or are written through to memory and 61 * cache-dirty state with respect to program v 62 * map System RAM with this mapping type will 63 * 64 * MEMREMAP_WC - establish a writecombine mapp 65 * be coalesced together (e.g. in the CPU's wr 66 * uncached. Attempts to map System RAM with t 67 */ 68 void *memremap(resource_size_t offset, size_t 69 { 70 int is_ram = region_intersects(offset, 71 IORESOU 72 void *addr = NULL; 73 74 if (!flags) 75 return NULL; 76 77 if (is_ram == REGION_MIXED) { 78 WARN_ONCE(1, "memremap attempt 79 &offset, (unsi 80 return NULL; 81 } 82 83 /* Try all mapping types requested unt 84 if (flags & MEMREMAP_WB) { 85 /* 86 * MEMREMAP_WB is special in t 87 * from the direct map. Some 88 * capability of memremap() to 89 * the requested range is pote 90 */ 91 if (is_ram == REGION_INTERSECT 92 addr = try_ram_remap(o 93 if (!addr) 94 addr = arch_memremap_w 95 } 96 97 /* 98 * If we don't have a mapping yet and 99 * present then we will be attempting 100 * address mapping. Enforce that this 101 * System RAM. 102 */ 103 if (!addr && is_ram == REGION_INTERSEC 104 WARN_ONCE(1, "memremap attempt 105 &offset, (unsi 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 devic 127 { 128 memunmap(*(void **)res); 129 } 130 131 static int devm_memremap_match(struct device * 132 { 133 return *(void **)res == match_data; 134 } 135 136 void *devm_memremap(struct device *dev, resour 137 size_t size, unsigned long fla 138 { 139 void **ptr, *addr; 140 141 ptr = devres_alloc_node(devm_memremap_ 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 *a 160 { 161 WARN_ON(devres_release(dev, devm_memre 162 devm_memremap_ 163 } 164 EXPORT_SYMBOL(devm_memunmap); 165
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.