1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/arch/arm/mm/ioremap.c 4 * 5 * Re-map IO memory to kernel address space so that we can access it. 6 * 7 * (C) Copyright 1995 1996 Linus Torvalds 8 * 9 * Hacked for ARM by Phil Blundell <philb@gnu.org> 10 * Hacked to allow all architectures to build, and various cleanups 11 * by Russell King 12 * 13 * This allows a driver to remap an arbitrary region of bus memory into 14 * virtual space. One should *only* use readl, writel, memcpy_toio and 15 * so on with such remapped areas. 16 * 17 * Because the ARM only has a 32-bit address space we can't address the 18 * whole of the (physical) PCI space at once. PCI huge-mode addressing 19 * allows us to circumvent this restriction by splitting PCI space into 20 * two 2GB chunks and mapping only one at a time into processor memory. 21 * We use MMU protection domains to trap any attempt to access the bank 22 * that is not currently mapped. (This isn't fully implemented yet.) 23 */ 24 #include <linux/module.h> 25 #include <linux/errno.h> 26 #include <linux/kasan.h> 27 #include <linux/mm.h> 28 #include <linux/vmalloc.h> 29 #include <linux/io.h> 30 #include <linux/sizes.h> 31 #include <linux/memblock.h> 32 33 #include <asm/cacheflush.h> 34 #include <asm/early_ioremap.h> 35 #include <asm/mmu_context.h> 36 #include <asm/pgalloc.h> 37 #include <asm/tlbflush.h> 38 #include <asm/set_memory.h> 39 #include <asm/system_info.h> 40 41 #include <asm/mach/map.h> 42 #include <asm/mach/pci.h> 43 #include "mm.h" 44 45 46 LIST_HEAD(static_vmlist); 47 48 static struct static_vm *find_static_vm_paddr(phys_addr_t paddr, 49 size_t size, unsigned int mtype) 50 { 51 struct static_vm *svm; 52 struct vm_struct *vm; 53 54 list_for_each_entry(svm, &static_vmlist, list) { 55 vm = &svm->vm; 56 if (!(vm->flags & VM_ARM_STATIC_MAPPING)) 57 continue; 58 if ((vm->flags & VM_ARM_MTYPE_MASK) != VM_ARM_MTYPE(mtype)) 59 continue; 60 61 if (vm->phys_addr > paddr || 62 paddr + size - 1 > vm->phys_addr + vm->size - 1) 63 continue; 64 65 return svm; 66 } 67 68 return NULL; 69 } 70 71 struct static_vm *find_static_vm_vaddr(void *vaddr) 72 { 73 struct static_vm *svm; 74 struct vm_struct *vm; 75 76 list_for_each_entry(svm, &static_vmlist, list) { 77 vm = &svm->vm; 78 79 /* static_vmlist is ascending order */ 80 if (vm->addr > vaddr) 81 break; 82 83 if (vm->addr <= vaddr && vm->addr + vm->size > vaddr) 84 return svm; 85 } 86 87 return NULL; 88 } 89 90 void __init add_static_vm_early(struct static_vm *svm) 91 { 92 struct static_vm *curr_svm; 93 struct vm_struct *vm; 94 void *vaddr; 95 96 vm = &svm->vm; 97 vm_area_add_early(vm); 98 vaddr = vm->addr; 99 100 list_for_each_entry(curr_svm, &static_vmlist, list) { 101 vm = &curr_svm->vm; 102 103 if (vm->addr > vaddr) 104 break; 105 } 106 list_add_tail(&svm->list, &curr_svm->list); 107 } 108 109 int ioremap_page(unsigned long virt, unsigned long phys, 110 const struct mem_type *mtype) 111 { 112 return vmap_page_range(virt, virt + PAGE_SIZE, phys, 113 __pgprot(mtype->prot_pte)); 114 } 115 EXPORT_SYMBOL(ioremap_page); 116 117 #ifdef CONFIG_KASAN 118 static unsigned long arm_kasan_mem_to_shadow(unsigned long addr) 119 { 120 return (unsigned long)kasan_mem_to_shadow((void *)addr); 121 } 122 #else 123 static unsigned long arm_kasan_mem_to_shadow(unsigned long addr) 124 { 125 return 0; 126 } 127 #endif 128 129 static void memcpy_pgd(struct mm_struct *mm, unsigned long start, 130 unsigned long end) 131 { 132 end = ALIGN(end, PGDIR_SIZE); 133 memcpy(pgd_offset(mm, start), pgd_offset_k(start), 134 sizeof(pgd_t) * (pgd_index(end) - pgd_index(start))); 135 } 136 137 void __check_vmalloc_seq(struct mm_struct *mm) 138 { 139 int seq; 140 141 do { 142 seq = atomic_read_acquire(&init_mm.context.vmalloc_seq); 143 memcpy_pgd(mm, VMALLOC_START, VMALLOC_END); 144 if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) { 145 unsigned long start = 146 arm_kasan_mem_to_shadow(VMALLOC_START); 147 unsigned long end = 148 arm_kasan_mem_to_shadow(VMALLOC_END); 149 memcpy_pgd(mm, start, end); 150 } 151 /* 152 * Use a store-release so that other CPUs that observe the 153 * counter's new value are guaranteed to see the results of the 154 * memcpy as well. 155 */ 156 atomic_set_release(&mm->context.vmalloc_seq, seq); 157 } while (seq != atomic_read(&init_mm.context.vmalloc_seq)); 158 } 159 160 #if !defined(CONFIG_SMP) && !defined(CONFIG_ARM_LPAE) 161 /* 162 * Section support is unsafe on SMP - If you iounmap and ioremap a region, 163 * the other CPUs will not see this change until their next context switch. 164 * Meanwhile, (eg) if an interrupt comes in on one of those other CPUs 165 * which requires the new ioremap'd region to be referenced, the CPU will 166 * reference the _old_ region. 167 * 168 * Note that get_vm_area_caller() allocates a guard 4K page, so we need to 169 * mask the size back to 1MB aligned or we will overflow in the loop below. 170 */ 171 static void unmap_area_sections(unsigned long virt, unsigned long size) 172 { 173 unsigned long addr = virt, end = virt + (size & ~(SZ_1M - 1)); 174 pmd_t *pmdp = pmd_off_k(addr); 175 176 do { 177 pmd_t pmd = *pmdp; 178 179 if (!pmd_none(pmd)) { 180 /* 181 * Clear the PMD from the page table, and 182 * increment the vmalloc sequence so others 183 * notice this change. 184 * 185 * Note: this is still racy on SMP machines. 186 */ 187 pmd_clear(pmdp); 188 atomic_inc_return_release(&init_mm.context.vmalloc_seq); 189 190 /* 191 * Free the page table, if there was one. 192 */ 193 if ((pmd_val(pmd) & PMD_TYPE_MASK) == PMD_TYPE_TABLE) 194 pte_free_kernel(&init_mm, pmd_page_vaddr(pmd)); 195 } 196 197 addr += PMD_SIZE; 198 pmdp += 2; 199 } while (addr < end); 200 201 /* 202 * Ensure that the active_mm is up to date - we want to 203 * catch any use-after-iounmap cases. 204 */ 205 check_vmalloc_seq(current->active_mm); 206 207 flush_tlb_kernel_range(virt, end); 208 } 209 210 static int 211 remap_area_sections(unsigned long virt, unsigned long pfn, 212 size_t size, const struct mem_type *type) 213 { 214 unsigned long addr = virt, end = virt + size; 215 pmd_t *pmd = pmd_off_k(addr); 216 217 /* 218 * Remove and free any PTE-based mapping, and 219 * sync the current kernel mapping. 220 */ 221 unmap_area_sections(virt, size); 222 223 do { 224 pmd[0] = __pmd(__pfn_to_phys(pfn) | type->prot_sect); 225 pfn += SZ_1M >> PAGE_SHIFT; 226 pmd[1] = __pmd(__pfn_to_phys(pfn) | type->prot_sect); 227 pfn += SZ_1M >> PAGE_SHIFT; 228 flush_pmd_entry(pmd); 229 230 addr += PMD_SIZE; 231 pmd += 2; 232 } while (addr < end); 233 234 return 0; 235 } 236 237 #endif 238 239 static void __iomem * __arm_ioremap_pfn_caller(unsigned long pfn, 240 unsigned long offset, size_t size, unsigned int mtype, void *caller) 241 { 242 const struct mem_type *type; 243 int err; 244 unsigned long addr; 245 struct vm_struct *area; 246 phys_addr_t paddr; 247 248 #ifndef CONFIG_ARM_LPAE 249 /* 250 * The physical range must not extend beyond the 32-bit 251 * phys_addr_t 252 */ 253 if (((u64)pfn << PAGE_SHIFT) + offset + size > SZ_4G) 254 return NULL; 255 #endif 256 paddr = __pfn_to_phys(pfn); 257 258 type = get_mem_type(mtype); 259 if (!type) 260 return NULL; 261 262 /* 263 * Page align the mapping size, taking account of any offset. 264 */ 265 size = PAGE_ALIGN(offset + size); 266 267 /* 268 * Try to reuse one of the static mapping whenever possible. 269 */ 270 if (size) { 271 struct static_vm *svm; 272 273 svm = find_static_vm_paddr(paddr, size, mtype); 274 if (svm) { 275 addr = (unsigned long)svm->vm.addr; 276 addr += paddr - svm->vm.phys_addr; 277 return (void __iomem *) (offset + addr); 278 } 279 } 280 281 /* 282 * Don't allow RAM to be mapped with mismatched attributes - this 283 * causes problems with ARMv6+ 284 */ 285 if (WARN_ON(memblock_is_map_memory(PFN_PHYS(pfn)) && 286 mtype != MT_MEMORY_RW)) 287 return NULL; 288 289 area = get_vm_area_caller(size, VM_IOREMAP, caller); 290 if (!area) 291 return NULL; 292 addr = (unsigned long)area->addr; 293 area->phys_addr = paddr; 294 295 #if !defined(CONFIG_SMP) && !defined(CONFIG_ARM_LPAE) 296 if (!((paddr | size | addr) & ~PMD_MASK)) { 297 area->flags |= VM_ARM_SECTION_MAPPING; 298 err = remap_area_sections(addr, pfn, size, type); 299 } else 300 #endif 301 err = ioremap_page_range(addr, addr + size, paddr, 302 __pgprot(type->prot_pte)); 303 304 if (err) { 305 vunmap((void *)addr); 306 return NULL; 307 } 308 309 flush_cache_vmap(addr, addr + size); 310 return (void __iomem *) (offset + addr); 311 } 312 313 void __iomem *__arm_ioremap_caller(phys_addr_t phys_addr, size_t size, 314 unsigned int mtype, void *caller) 315 { 316 phys_addr_t last_addr; 317 unsigned long offset = phys_addr & ~PAGE_MASK; 318 unsigned long pfn = __phys_to_pfn(phys_addr); 319 320 /* 321 * Don't allow wraparound or zero size 322 */ 323 last_addr = phys_addr + size - 1; 324 if (!size || last_addr < phys_addr) 325 return NULL; 326 327 return __arm_ioremap_pfn_caller(pfn, offset, size, mtype, 328 caller); 329 } 330 331 /* 332 * Remap an arbitrary physical address space into the kernel virtual 333 * address space. Needed when the kernel wants to access high addresses 334 * directly. 335 * 336 * NOTE! We need to allow non-page-aligned mappings too: we will obviously 337 * have to convert them into an offset in a page-aligned mapping, but the 338 * caller shouldn't need to know that small detail. 339 */ 340 void __iomem * 341 __arm_ioremap_pfn(unsigned long pfn, unsigned long offset, size_t size, 342 unsigned int mtype) 343 { 344 return __arm_ioremap_pfn_caller(pfn, offset, size, mtype, 345 __builtin_return_address(0)); 346 } 347 EXPORT_SYMBOL(__arm_ioremap_pfn); 348 349 void __iomem * (*arch_ioremap_caller)(phys_addr_t, size_t, 350 unsigned int, void *) = 351 __arm_ioremap_caller; 352 353 void __iomem *ioremap(resource_size_t res_cookie, size_t size) 354 { 355 return arch_ioremap_caller(res_cookie, size, MT_DEVICE, 356 __builtin_return_address(0)); 357 } 358 EXPORT_SYMBOL(ioremap); 359 360 void __iomem *ioremap_cache(resource_size_t res_cookie, size_t size) 361 { 362 return arch_ioremap_caller(res_cookie, size, MT_DEVICE_CACHED, 363 __builtin_return_address(0)); 364 } 365 EXPORT_SYMBOL(ioremap_cache); 366 367 void __iomem *ioremap_wc(resource_size_t res_cookie, size_t size) 368 { 369 return arch_ioremap_caller(res_cookie, size, MT_DEVICE_WC, 370 __builtin_return_address(0)); 371 } 372 EXPORT_SYMBOL(ioremap_wc); 373 374 /* 375 * Remap an arbitrary physical address space into the kernel virtual 376 * address space as memory. Needed when the kernel wants to execute 377 * code in external memory. This is needed for reprogramming source 378 * clocks that would affect normal memory for example. Please see 379 * CONFIG_GENERIC_ALLOCATOR for allocating external memory. 380 */ 381 void __iomem * 382 __arm_ioremap_exec(phys_addr_t phys_addr, size_t size, bool cached) 383 { 384 unsigned int mtype; 385 386 if (cached) 387 mtype = MT_MEMORY_RWX; 388 else 389 mtype = MT_MEMORY_RWX_NONCACHED; 390 391 return __arm_ioremap_caller(phys_addr, size, mtype, 392 __builtin_return_address(0)); 393 } 394 395 void __arm_iomem_set_ro(void __iomem *ptr, size_t size) 396 { 397 set_memory_ro((unsigned long)ptr, PAGE_ALIGN(size) / PAGE_SIZE); 398 } 399 400 void *arch_memremap_wb(phys_addr_t phys_addr, size_t size, unsigned long flags) 401 { 402 return (__force void *)arch_ioremap_caller(phys_addr, size, 403 MT_MEMORY_RW, 404 __builtin_return_address(0)); 405 } 406 407 void iounmap(volatile void __iomem *io_addr) 408 { 409 void *addr = (void *)(PAGE_MASK & (unsigned long)io_addr); 410 struct static_vm *svm; 411 412 /* If this is a static mapping, we must leave it alone */ 413 svm = find_static_vm_vaddr(addr); 414 if (svm) 415 return; 416 417 #if !defined(CONFIG_SMP) && !defined(CONFIG_ARM_LPAE) 418 { 419 struct vm_struct *vm; 420 421 vm = find_vm_area(addr); 422 423 /* 424 * If this is a section based mapping we need to handle it 425 * specially as the VM subsystem does not know how to handle 426 * such a beast. 427 */ 428 if (vm && (vm->flags & VM_ARM_SECTION_MAPPING)) 429 unmap_area_sections((unsigned long)vm->addr, vm->size); 430 } 431 #endif 432 433 vunmap(addr); 434 } 435 EXPORT_SYMBOL(iounmap); 436 437 #if defined(CONFIG_PCI) || IS_ENABLED(CONFIG_PCMCIA) 438 static int pci_ioremap_mem_type = MT_DEVICE; 439 440 void pci_ioremap_set_mem_type(int mem_type) 441 { 442 pci_ioremap_mem_type = mem_type; 443 } 444 445 int pci_remap_iospace(const struct resource *res, phys_addr_t phys_addr) 446 { 447 unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start; 448 449 if (!(res->flags & IORESOURCE_IO)) 450 return -EINVAL; 451 452 if (res->end > IO_SPACE_LIMIT) 453 return -EINVAL; 454 455 return vmap_page_range(vaddr, vaddr + resource_size(res), phys_addr, 456 __pgprot(get_mem_type(pci_ioremap_mem_type)->prot_pte)); 457 } 458 EXPORT_SYMBOL(pci_remap_iospace); 459 460 void __iomem *pci_remap_cfgspace(resource_size_t res_cookie, size_t size) 461 { 462 return arch_ioremap_caller(res_cookie, size, MT_UNCACHED, 463 __builtin_return_address(0)); 464 } 465 EXPORT_SYMBOL_GPL(pci_remap_cfgspace); 466 #endif 467 468 /* 469 * Must be called after early_fixmap_init 470 */ 471 void __init early_ioremap_init(void) 472 { 473 early_ioremap_setup(); 474 } 475 476 bool arch_memremap_can_ram_remap(resource_size_t offset, size_t size, 477 unsigned long flags) 478 { 479 return memblock_is_map_memory(offset); 480 } 481