1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/arch/arm/mm/ioremap.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Re-map IO memory to kernel address space so that we can access it.
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * (C) Copyright 1995 1996 Linus Torvalds
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * Hacked for ARM by Phil Blundell <philb@gnu.org>
101da177e4SLinus Torvalds * Hacked to allow all architectures to build, and various cleanups
111da177e4SLinus Torvalds * by Russell King
121da177e4SLinus Torvalds *
131da177e4SLinus Torvalds * This allows a driver to remap an arbitrary region of bus memory into
141da177e4SLinus Torvalds * virtual space. One should *only* use readl, writel, memcpy_toio and
151da177e4SLinus Torvalds * so on with such remapped areas.
161da177e4SLinus Torvalds *
171da177e4SLinus Torvalds * Because the ARM only has a 32-bit address space we can't address the
181da177e4SLinus Torvalds * whole of the (physical) PCI space at once. PCI huge-mode addressing
191da177e4SLinus Torvalds * allows us to circumvent this restriction by splitting PCI space into
201da177e4SLinus Torvalds * two 2GB chunks and mapping only one at a time into processor memory.
211da177e4SLinus Torvalds * We use MMU protection domains to trap any attempt to access the bank
221da177e4SLinus Torvalds * that is not currently mapped. (This isn't fully implemented yet.)
231da177e4SLinus Torvalds */
241da177e4SLinus Torvalds #include <linux/module.h>
251da177e4SLinus Torvalds #include <linux/errno.h>
261da177e4SLinus Torvalds #include <linux/mm.h>
271da177e4SLinus Torvalds #include <linux/vmalloc.h>
28fced80c7SRussell King #include <linux/io.h>
29158e8bfeSAlessandro Rubini #include <linux/sizes.h>
30024591f9SMike Rapoport #include <linux/memblock.h>
311da177e4SLinus Torvalds
3215d07dc9SRussell King #include <asm/cp15.h>
330ba8b9b2SRussell King #include <asm/cputype.h>
341da177e4SLinus Torvalds #include <asm/cacheflush.h>
352937367bSArd Biesheuvel #include <asm/early_ioremap.h>
36ff0daca5SRussell King #include <asm/mmu_context.h>
37ff0daca5SRussell King #include <asm/pgalloc.h>
381da177e4SLinus Torvalds #include <asm/tlbflush.h>
39b8bc0e50SRussell King (Oracle) #include <asm/set_memory.h>
409f97da78SDavid Howells #include <asm/system_info.h>
41ff0daca5SRussell King
42b29e9f5eSRussell King #include <asm/mach/map.h>
43c2794437SRob Herring #include <asm/mach/pci.h>
44b29e9f5eSRussell King #include "mm.h"
45b29e9f5eSRussell King
46ed8fd218SJoonsoo Kim
47ed8fd218SJoonsoo Kim LIST_HEAD(static_vmlist);
48ed8fd218SJoonsoo Kim
find_static_vm_paddr(phys_addr_t paddr,size_t size,unsigned int mtype)49ed8fd218SJoonsoo Kim static struct static_vm *find_static_vm_paddr(phys_addr_t paddr,
50ed8fd218SJoonsoo Kim size_t size, unsigned int mtype)
51ed8fd218SJoonsoo Kim {
52ed8fd218SJoonsoo Kim struct static_vm *svm;
53ed8fd218SJoonsoo Kim struct vm_struct *vm;
54ed8fd218SJoonsoo Kim
55ed8fd218SJoonsoo Kim list_for_each_entry(svm, &static_vmlist, list) {
56ed8fd218SJoonsoo Kim vm = &svm->vm;
57ed8fd218SJoonsoo Kim if (!(vm->flags & VM_ARM_STATIC_MAPPING))
58ed8fd218SJoonsoo Kim continue;
59ed8fd218SJoonsoo Kim if ((vm->flags & VM_ARM_MTYPE_MASK) != VM_ARM_MTYPE(mtype))
60ed8fd218SJoonsoo Kim continue;
61ed8fd218SJoonsoo Kim
62ed8fd218SJoonsoo Kim if (vm->phys_addr > paddr ||
63ed8fd218SJoonsoo Kim paddr + size - 1 > vm->phys_addr + vm->size - 1)
64ed8fd218SJoonsoo Kim continue;
65ed8fd218SJoonsoo Kim
66ed8fd218SJoonsoo Kim return svm;
67ed8fd218SJoonsoo Kim }
68ed8fd218SJoonsoo Kim
69ed8fd218SJoonsoo Kim return NULL;
70ed8fd218SJoonsoo Kim }
71ed8fd218SJoonsoo Kim
find_static_vm_vaddr(void * vaddr)72ed8fd218SJoonsoo Kim struct static_vm *find_static_vm_vaddr(void *vaddr)
73ed8fd218SJoonsoo Kim {
74ed8fd218SJoonsoo Kim struct static_vm *svm;
75ed8fd218SJoonsoo Kim struct vm_struct *vm;
76ed8fd218SJoonsoo Kim
77ed8fd218SJoonsoo Kim list_for_each_entry(svm, &static_vmlist, list) {
78ed8fd218SJoonsoo Kim vm = &svm->vm;
79ed8fd218SJoonsoo Kim
80ed8fd218SJoonsoo Kim /* static_vmlist is ascending order */
81ed8fd218SJoonsoo Kim if (vm->addr > vaddr)
82ed8fd218SJoonsoo Kim break;
83ed8fd218SJoonsoo Kim
84ed8fd218SJoonsoo Kim if (vm->addr <= vaddr && vm->addr + vm->size > vaddr)
85ed8fd218SJoonsoo Kim return svm;
86ed8fd218SJoonsoo Kim }
87ed8fd218SJoonsoo Kim
88ed8fd218SJoonsoo Kim return NULL;
89ed8fd218SJoonsoo Kim }
90ed8fd218SJoonsoo Kim
add_static_vm_early(struct static_vm * svm)91ed8fd218SJoonsoo Kim void __init add_static_vm_early(struct static_vm *svm)
92ed8fd218SJoonsoo Kim {
93ed8fd218SJoonsoo Kim struct static_vm *curr_svm;
94ed8fd218SJoonsoo Kim struct vm_struct *vm;
95ed8fd218SJoonsoo Kim void *vaddr;
96ed8fd218SJoonsoo Kim
97ed8fd218SJoonsoo Kim vm = &svm->vm;
98ed8fd218SJoonsoo Kim vm_area_add_early(vm);
99ed8fd218SJoonsoo Kim vaddr = vm->addr;
100ed8fd218SJoonsoo Kim
101ed8fd218SJoonsoo Kim list_for_each_entry(curr_svm, &static_vmlist, list) {
102ed8fd218SJoonsoo Kim vm = &curr_svm->vm;
103ed8fd218SJoonsoo Kim
104ed8fd218SJoonsoo Kim if (vm->addr > vaddr)
105ed8fd218SJoonsoo Kim break;
106ed8fd218SJoonsoo Kim }
107ed8fd218SJoonsoo Kim list_add_tail(&svm->list, &curr_svm->list);
108ed8fd218SJoonsoo Kim }
109ed8fd218SJoonsoo Kim
ioremap_page(unsigned long virt,unsigned long phys,const struct mem_type * mtype)11069d3a84aSHiroshi DOYU int ioremap_page(unsigned long virt, unsigned long phys,
11169d3a84aSHiroshi DOYU const struct mem_type *mtype)
11269d3a84aSHiroshi DOYU {
113d7461963SRussell King return ioremap_page_range(virt, virt + PAGE_SIZE, phys,
114d7461963SRussell King __pgprot(mtype->prot_pte));
11569d3a84aSHiroshi DOYU }
11669d3a84aSHiroshi DOYU EXPORT_SYMBOL(ioremap_page);
117ff0daca5SRussell King
__check_vmalloc_seq(struct mm_struct * mm)1183e99675aSNicolas Pitre void __check_vmalloc_seq(struct mm_struct *mm)
119ff0daca5SRussell King {
120d31e23afSArd Biesheuvel int seq;
121ff0daca5SRussell King
122ff0daca5SRussell King do {
123d31e23afSArd Biesheuvel seq = atomic_read(&init_mm.context.vmalloc_seq);
124ff0daca5SRussell King memcpy(pgd_offset(mm, VMALLOC_START),
125ff0daca5SRussell King pgd_offset_k(VMALLOC_START),
126ff0daca5SRussell King sizeof(pgd_t) * (pgd_index(VMALLOC_END) -
127ff0daca5SRussell King pgd_index(VMALLOC_START)));
128d31e23afSArd Biesheuvel /*
129d31e23afSArd Biesheuvel * Use a store-release so that other CPUs that observe the
130d31e23afSArd Biesheuvel * counter's new value are guaranteed to see the results of the
131d31e23afSArd Biesheuvel * memcpy as well.
132d31e23afSArd Biesheuvel */
133d31e23afSArd Biesheuvel atomic_set_release(&mm->context.vmalloc_seq, seq);
134d31e23afSArd Biesheuvel } while (seq != atomic_read(&init_mm.context.vmalloc_seq));
135ff0daca5SRussell King }
136ff0daca5SRussell King
137da028779SCatalin Marinas #if !defined(CONFIG_SMP) && !defined(CONFIG_ARM_LPAE)
138ff0daca5SRussell King /*
139ff0daca5SRussell King * Section support is unsafe on SMP - If you iounmap and ioremap a region,
140ff0daca5SRussell King * the other CPUs will not see this change until their next context switch.
141ff0daca5SRussell King * Meanwhile, (eg) if an interrupt comes in on one of those other CPUs
142ff0daca5SRussell King * which requires the new ioremap'd region to be referenced, the CPU will
143ff0daca5SRussell King * reference the _old_ region.
144ff0daca5SRussell King *
14531aa8fd6SRussell King * Note that get_vm_area_caller() allocates a guard 4K page, so we need to
14631aa8fd6SRussell King * mask the size back to 1MB aligned or we will overflow in the loop below.
147ff0daca5SRussell King */
unmap_area_sections(unsigned long virt,unsigned long size)148ff0daca5SRussell King static void unmap_area_sections(unsigned long virt, unsigned long size)
149ff0daca5SRussell King {
15024f11ec0SRussell King unsigned long addr = virt, end = virt + (size & ~(SZ_1M - 1));
151e05c7b1fSMike Rapoport pmd_t *pmdp = pmd_off_k(addr);
152ff0daca5SRussell King
153ff0daca5SRussell King do {
15403a6b827SCatalin Marinas pmd_t pmd = *pmdp;
155ff0daca5SRussell King
156ff0daca5SRussell King if (!pmd_none(pmd)) {
157ff0daca5SRussell King /*
158ff0daca5SRussell King * Clear the PMD from the page table, and
1593e99675aSNicolas Pitre * increment the vmalloc sequence so others
160ff0daca5SRussell King * notice this change.
161ff0daca5SRussell King *
162ff0daca5SRussell King * Note: this is still racy on SMP machines.
163ff0daca5SRussell King */
164ff0daca5SRussell King pmd_clear(pmdp);
165d31e23afSArd Biesheuvel atomic_inc_return_release(&init_mm.context.vmalloc_seq);
166ff0daca5SRussell King
167ff0daca5SRussell King /*
168ff0daca5SRussell King * Free the page table, if there was one.
169ff0daca5SRussell King */
170ff0daca5SRussell King if ((pmd_val(pmd) & PMD_TYPE_MASK) == PMD_TYPE_TABLE)
1715e541973SBenjamin Herrenschmidt pte_free_kernel(&init_mm, pmd_page_vaddr(pmd));
172ff0daca5SRussell King }
173ff0daca5SRussell King
17403a6b827SCatalin Marinas addr += PMD_SIZE;
17503a6b827SCatalin Marinas pmdp += 2;
176ff0daca5SRussell King } while (addr < end);
177ff0daca5SRussell King
178ff0daca5SRussell King /*
179ff0daca5SRussell King * Ensure that the active_mm is up to date - we want to
180ff0daca5SRussell King * catch any use-after-iounmap cases.
181ff0daca5SRussell King */
182d31e23afSArd Biesheuvel check_vmalloc_seq(current->active_mm);
183ff0daca5SRussell King
184ff0daca5SRussell King flush_tlb_kernel_range(virt, end);
185ff0daca5SRussell King }
186ff0daca5SRussell King
187ff0daca5SRussell King static int
remap_area_sections(unsigned long virt,unsigned long pfn,size_t size,const struct mem_type * type)188ff0daca5SRussell King remap_area_sections(unsigned long virt, unsigned long pfn,
189b29e9f5eSRussell King size_t size, const struct mem_type *type)
190ff0daca5SRussell King {
191b29e9f5eSRussell King unsigned long addr = virt, end = virt + size;
192e05c7b1fSMike Rapoport pmd_t *pmd = pmd_off_k(addr);
193ff0daca5SRussell King
194ff0daca5SRussell King /*
195ff0daca5SRussell King * Remove and free any PTE-based mapping, and
196ff0daca5SRussell King * sync the current kernel mapping.
197ff0daca5SRussell King */
198ff0daca5SRussell King unmap_area_sections(virt, size);
199ff0daca5SRussell King
200ff0daca5SRussell King do {
201b29e9f5eSRussell King pmd[0] = __pmd(__pfn_to_phys(pfn) | type->prot_sect);
202ff0daca5SRussell King pfn += SZ_1M >> PAGE_SHIFT;
203b29e9f5eSRussell King pmd[1] = __pmd(__pfn_to_phys(pfn) | type->prot_sect);
204ff0daca5SRussell King pfn += SZ_1M >> PAGE_SHIFT;
205ff0daca5SRussell King flush_pmd_entry(pmd);
206ff0daca5SRussell King
20703a6b827SCatalin Marinas addr += PMD_SIZE;
20803a6b827SCatalin Marinas pmd += 2;
209ff0daca5SRussell King } while (addr < end);
210ff0daca5SRussell King
211ff0daca5SRussell King return 0;
212ff0daca5SRussell King }
213a069c896SLennert Buytenhek
214a069c896SLennert Buytenhek static int
remap_area_supersections(unsigned long virt,unsigned long pfn,size_t size,const struct mem_type * type)215a069c896SLennert Buytenhek remap_area_supersections(unsigned long virt, unsigned long pfn,
216b29e9f5eSRussell King size_t size, const struct mem_type *type)
217a069c896SLennert Buytenhek {
218b29e9f5eSRussell King unsigned long addr = virt, end = virt + size;
219e05c7b1fSMike Rapoport pmd_t *pmd = pmd_off_k(addr);
220a069c896SLennert Buytenhek
221a069c896SLennert Buytenhek /*
222a069c896SLennert Buytenhek * Remove and free any PTE-based mapping, and
223a069c896SLennert Buytenhek * sync the current kernel mapping.
224a069c896SLennert Buytenhek */
225a069c896SLennert Buytenhek unmap_area_sections(virt, size);
226a069c896SLennert Buytenhek do {
227a069c896SLennert Buytenhek unsigned long super_pmd_val, i;
228a069c896SLennert Buytenhek
229b29e9f5eSRussell King super_pmd_val = __pfn_to_phys(pfn) | type->prot_sect |
230b29e9f5eSRussell King PMD_SECT_SUPER;
231a069c896SLennert Buytenhek super_pmd_val |= ((pfn >> (32 - PAGE_SHIFT)) & 0xf) << 20;
232a069c896SLennert Buytenhek
233a069c896SLennert Buytenhek for (i = 0; i < 8; i++) {
234a069c896SLennert Buytenhek pmd[0] = __pmd(super_pmd_val);
235a069c896SLennert Buytenhek pmd[1] = __pmd(super_pmd_val);
236a069c896SLennert Buytenhek flush_pmd_entry(pmd);
237a069c896SLennert Buytenhek
23803a6b827SCatalin Marinas addr += PMD_SIZE;
23903a6b827SCatalin Marinas pmd += 2;
240a069c896SLennert Buytenhek }
241a069c896SLennert Buytenhek
242a069c896SLennert Buytenhek pfn += SUPERSECTION_SIZE >> PAGE_SHIFT;
243a069c896SLennert Buytenhek } while (addr < end);
244a069c896SLennert Buytenhek
245a069c896SLennert Buytenhek return 0;
246a069c896SLennert Buytenhek }
247ff0daca5SRussell King #endif
248ff0daca5SRussell King
__arm_ioremap_pfn_caller(unsigned long pfn,unsigned long offset,size_t size,unsigned int mtype,void * caller)24920a1080dSRussell King static void __iomem * __arm_ioremap_pfn_caller(unsigned long pfn,
25031aa8fd6SRussell King unsigned long offset, size_t size, unsigned int mtype, void *caller)
2519d4ae727SDeepak Saxena {
252b29e9f5eSRussell King const struct mem_type *type;
253ff0daca5SRussell King int err;
2549d4ae727SDeepak Saxena unsigned long addr;
2559d4ae727SDeepak Saxena struct vm_struct *area;
256101eeda3SJoonsoo Kim phys_addr_t paddr = __pfn_to_phys(pfn);
257a069c896SLennert Buytenhek
258da028779SCatalin Marinas #ifndef CONFIG_ARM_LPAE
259a069c896SLennert Buytenhek /*
260a069c896SLennert Buytenhek * High mappings must be supersection aligned
261a069c896SLennert Buytenhek */
262101eeda3SJoonsoo Kim if (pfn >= 0x100000 && (paddr & ~SUPERSECTION_MASK))
263a069c896SLennert Buytenhek return NULL;
264da028779SCatalin Marinas #endif
2659d4ae727SDeepak Saxena
2663603ab2bSRussell King type = get_mem_type(mtype);
2673603ab2bSRussell King if (!type)
2683603ab2bSRussell King return NULL;
269b29e9f5eSRussell King
2706d78b5f9SRussell King /*
2716d78b5f9SRussell King * Page align the mapping size, taking account of any offset.
2726d78b5f9SRussell King */
2736d78b5f9SRussell King size = PAGE_ALIGN(offset + size);
274c924aff8SRussell King
275576d2f25SNicolas Pitre /*
276576d2f25SNicolas Pitre * Try to reuse one of the static mapping whenever possible.
277576d2f25SNicolas Pitre */
278101eeda3SJoonsoo Kim if (size && !(sizeof(phys_addr_t) == 4 && pfn >= 0x100000)) {
279101eeda3SJoonsoo Kim struct static_vm *svm;
280101eeda3SJoonsoo Kim
281101eeda3SJoonsoo Kim svm = find_static_vm_paddr(paddr, size, mtype);
282101eeda3SJoonsoo Kim if (svm) {
283101eeda3SJoonsoo Kim addr = (unsigned long)svm->vm.addr;
284101eeda3SJoonsoo Kim addr += paddr - svm->vm.phys_addr;
285576d2f25SNicolas Pitre return (void __iomem *) (offset + addr);
286576d2f25SNicolas Pitre }
287101eeda3SJoonsoo Kim }
288576d2f25SNicolas Pitre
289576d2f25SNicolas Pitre /*
2909ab9e4fcSArd Biesheuvel * Don't allow RAM to be mapped with mismatched attributes - this
2919ab9e4fcSArd Biesheuvel * causes problems with ARMv6+
292576d2f25SNicolas Pitre */
293024591f9SMike Rapoport if (WARN_ON(memblock_is_map_memory(PFN_PHYS(pfn)) &&
294024591f9SMike Rapoport mtype != MT_MEMORY_RW))
295576d2f25SNicolas Pitre return NULL;
296576d2f25SNicolas Pitre
29731aa8fd6SRussell King area = get_vm_area_caller(size, VM_IOREMAP, caller);
2989d4ae727SDeepak Saxena if (!area)
2999d4ae727SDeepak Saxena return NULL;
3009d4ae727SDeepak Saxena addr = (unsigned long)area->addr;
301101eeda3SJoonsoo Kim area->phys_addr = paddr;
302ff0daca5SRussell King
303da028779SCatalin Marinas #if !defined(CONFIG_SMP) && !defined(CONFIG_ARM_LPAE)
304412489afSCatalin Marinas if (DOMAIN_IO == 0 &&
305412489afSCatalin Marinas (((cpu_architecture() >= CPU_ARCH_ARMv6) && (get_cr() & CR_XP)) ||
3064a56c1e4SRussell King cpu_is_xsc3()) && pfn >= 0x100000 &&
307101eeda3SJoonsoo Kim !((paddr | size | addr) & ~SUPERSECTION_MASK)) {
308a069c896SLennert Buytenhek area->flags |= VM_ARM_SECTION_MAPPING;
309b29e9f5eSRussell King err = remap_area_supersections(addr, pfn, size, type);
310101eeda3SJoonsoo Kim } else if (!((paddr | size | addr) & ~PMD_MASK)) {
311ff0daca5SRussell King area->flags |= VM_ARM_SECTION_MAPPING;
312b29e9f5eSRussell King err = remap_area_sections(addr, pfn, size, type);
313ff0daca5SRussell King } else
314ff0daca5SRussell King #endif
315101eeda3SJoonsoo Kim err = ioremap_page_range(addr, addr + size, paddr,
316d7461963SRussell King __pgprot(type->prot_pte));
317ff0daca5SRussell King
318ff0daca5SRussell King if (err) {
319478922c2SCatalin Marinas vunmap((void *)addr);
3209d4ae727SDeepak Saxena return NULL;
3219d4ae727SDeepak Saxena }
322ff0daca5SRussell King
323ff0daca5SRussell King flush_cache_vmap(addr, addr + size);
324ff0daca5SRussell King return (void __iomem *) (offset + addr);
3259d4ae727SDeepak Saxena }
3269d4ae727SDeepak Saxena
__arm_ioremap_caller(phys_addr_t phys_addr,size_t size,unsigned int mtype,void * caller)3279b97173eSLaura Abbott void __iomem *__arm_ioremap_caller(phys_addr_t phys_addr, size_t size,
32831aa8fd6SRussell King unsigned int mtype, void *caller)
3291da177e4SLinus Torvalds {
3309b97173eSLaura Abbott phys_addr_t last_addr;
3319d4ae727SDeepak Saxena unsigned long offset = phys_addr & ~PAGE_MASK;
3329d4ae727SDeepak Saxena unsigned long pfn = __phys_to_pfn(phys_addr);
3331da177e4SLinus Torvalds
3349d4ae727SDeepak Saxena /*
3359d4ae727SDeepak Saxena * Don't allow wraparound or zero size
3369d4ae727SDeepak Saxena */
3371da177e4SLinus Torvalds last_addr = phys_addr + size - 1;
3381da177e4SLinus Torvalds if (!size || last_addr < phys_addr)
3391da177e4SLinus Torvalds return NULL;
3401da177e4SLinus Torvalds
34131aa8fd6SRussell King return __arm_ioremap_pfn_caller(pfn, offset, size, mtype,
34231aa8fd6SRussell King caller);
34331aa8fd6SRussell King }
34431aa8fd6SRussell King
34531aa8fd6SRussell King /*
34631aa8fd6SRussell King * Remap an arbitrary physical address space into the kernel virtual
34731aa8fd6SRussell King * address space. Needed when the kernel wants to access high addresses
34831aa8fd6SRussell King * directly.
34931aa8fd6SRussell King *
35031aa8fd6SRussell King * NOTE! We need to allow non-page-aligned mappings too: we will obviously
35131aa8fd6SRussell King * have to convert them into an offset in a page-aligned mapping, but the
35231aa8fd6SRussell King * caller shouldn't need to know that small detail.
35331aa8fd6SRussell King */
35431aa8fd6SRussell King void __iomem *
__arm_ioremap_pfn(unsigned long pfn,unsigned long offset,size_t size,unsigned int mtype)35531aa8fd6SRussell King __arm_ioremap_pfn(unsigned long pfn, unsigned long offset, size_t size,
35631aa8fd6SRussell King unsigned int mtype)
35731aa8fd6SRussell King {
35831aa8fd6SRussell King return __arm_ioremap_pfn_caller(pfn, offset, size, mtype,
35931aa8fd6SRussell King __builtin_return_address(0));
36031aa8fd6SRussell King }
36131aa8fd6SRussell King EXPORT_SYMBOL(__arm_ioremap_pfn);
36231aa8fd6SRussell King
3639b97173eSLaura Abbott void __iomem * (*arch_ioremap_caller)(phys_addr_t, size_t,
3644fe7ef3aSRob Herring unsigned int, void *) =
3654fe7ef3aSRob Herring __arm_ioremap_caller;
3664fe7ef3aSRob Herring
ioremap(resource_size_t res_cookie,size_t size)36720a1080dSRussell King void __iomem *ioremap(resource_size_t res_cookie, size_t size)
36831aa8fd6SRussell King {
36920a1080dSRussell King return arch_ioremap_caller(res_cookie, size, MT_DEVICE,
37031aa8fd6SRussell King __builtin_return_address(0));
3711da177e4SLinus Torvalds }
37220a1080dSRussell King EXPORT_SYMBOL(ioremap);
37320a1080dSRussell King
ioremap_cache(resource_size_t res_cookie,size_t size)37420a1080dSRussell King void __iomem *ioremap_cache(resource_size_t res_cookie, size_t size)
37520a1080dSRussell King {
37620a1080dSRussell King return arch_ioremap_caller(res_cookie, size, MT_DEVICE_CACHED,
37720a1080dSRussell King __builtin_return_address(0));
37820a1080dSRussell King }
37920a1080dSRussell King EXPORT_SYMBOL(ioremap_cache);
38020a1080dSRussell King
ioremap_wc(resource_size_t res_cookie,size_t size)38120a1080dSRussell King void __iomem *ioremap_wc(resource_size_t res_cookie, size_t size)
38220a1080dSRussell King {
38320a1080dSRussell King return arch_ioremap_caller(res_cookie, size, MT_DEVICE_WC,
38420a1080dSRussell King __builtin_return_address(0));
38520a1080dSRussell King }
38620a1080dSRussell King EXPORT_SYMBOL(ioremap_wc);
3871da177e4SLinus Torvalds
3886c5482d5STony Lindgren /*
3896c5482d5STony Lindgren * Remap an arbitrary physical address space into the kernel virtual
3906c5482d5STony Lindgren * address space as memory. Needed when the kernel wants to execute
3916c5482d5STony Lindgren * code in external memory. This is needed for reprogramming source
3926c5482d5STony Lindgren * clocks that would affect normal memory for example. Please see
3936c5482d5STony Lindgren * CONFIG_GENERIC_ALLOCATOR for allocating external memory.
3946c5482d5STony Lindgren */
3956c5482d5STony Lindgren void __iomem *
__arm_ioremap_exec(phys_addr_t phys_addr,size_t size,bool cached)3969b97173eSLaura Abbott __arm_ioremap_exec(phys_addr_t phys_addr, size_t size, bool cached)
3976c5482d5STony Lindgren {
3986c5482d5STony Lindgren unsigned int mtype;
3996c5482d5STony Lindgren
4006c5482d5STony Lindgren if (cached)
4012e2c9de2SRussell King mtype = MT_MEMORY_RWX;
4026c5482d5STony Lindgren else
4032e2c9de2SRussell King mtype = MT_MEMORY_RWX_NONCACHED;
4046c5482d5STony Lindgren
4056c5482d5STony Lindgren return __arm_ioremap_caller(phys_addr, size, mtype,
4066c5482d5STony Lindgren __builtin_return_address(0));
4076c5482d5STony Lindgren }
4086c5482d5STony Lindgren
__arm_iomem_set_ro(void __iomem * ptr,size_t size)409b8bc0e50SRussell King (Oracle) void __arm_iomem_set_ro(void __iomem *ptr, size_t size)
410b8bc0e50SRussell King (Oracle) {
411b8bc0e50SRussell King (Oracle) set_memory_ro((unsigned long)ptr, PAGE_ALIGN(size) / PAGE_SIZE);
412b8bc0e50SRussell King (Oracle) }
413b8bc0e50SRussell King (Oracle)
arch_memremap_wb(phys_addr_t phys_addr,size_t size)4149ab9e4fcSArd Biesheuvel void *arch_memremap_wb(phys_addr_t phys_addr, size_t size)
4159ab9e4fcSArd Biesheuvel {
4169ab9e4fcSArd Biesheuvel return (__force void *)arch_ioremap_caller(phys_addr, size,
4179ab9e4fcSArd Biesheuvel MT_MEMORY_RW,
4189ab9e4fcSArd Biesheuvel __builtin_return_address(0));
4199ab9e4fcSArd Biesheuvel }
4209ab9e4fcSArd Biesheuvel
iounmap(volatile void __iomem * io_addr)421*d803336aSKefeng Wang void iounmap(volatile void __iomem *io_addr)
4221da177e4SLinus Torvalds {
42309d9bae0SRussell King void *addr = (void *)(PAGE_MASK & (unsigned long)io_addr);
424101eeda3SJoonsoo Kim struct static_vm *svm;
425101eeda3SJoonsoo Kim
426101eeda3SJoonsoo Kim /* If this is a static mapping, we must leave it alone */
427101eeda3SJoonsoo Kim svm = find_static_vm_vaddr(addr);
428101eeda3SJoonsoo Kim if (svm)
429101eeda3SJoonsoo Kim return;
430101eeda3SJoonsoo Kim
431101eeda3SJoonsoo Kim #if !defined(CONFIG_SMP) && !defined(CONFIG_ARM_LPAE)
432101eeda3SJoonsoo Kim {
4336ee723a6SNicolas Pitre struct vm_struct *vm;
434ff0daca5SRussell King
435101eeda3SJoonsoo Kim vm = find_vm_area(addr);
436101eeda3SJoonsoo Kim
437ff0daca5SRussell King /*
438ff0daca5SRussell King * If this is a section based mapping we need to handle it
4396cbdc8c5SSimon Arlott * specially as the VM subsystem does not know how to handle
4406ee723a6SNicolas Pitre * such a beast.
441ff0daca5SRussell King */
442101eeda3SJoonsoo Kim if (vm && (vm->flags & VM_ARM_SECTION_MAPPING))
443576d2f25SNicolas Pitre unmap_area_sections((unsigned long)vm->addr, vm->size);
444ff0daca5SRussell King }
445576d2f25SNicolas Pitre #endif
446ff0daca5SRussell King
44709d9bae0SRussell King vunmap(addr);
4481da177e4SLinus Torvalds }
44920a1080dSRussell King EXPORT_SYMBOL(iounmap);
450c2794437SRob Herring
451645b3026SArnd Bergmann #if defined(CONFIG_PCI) || IS_ENABLED(CONFIG_PCMCIA)
4521c8c3cf0SThomas Petazzoni static int pci_ioremap_mem_type = MT_DEVICE;
4531c8c3cf0SThomas Petazzoni
pci_ioremap_set_mem_type(int mem_type)4541c8c3cf0SThomas Petazzoni void pci_ioremap_set_mem_type(int mem_type)
4551c8c3cf0SThomas Petazzoni {
4561c8c3cf0SThomas Petazzoni pci_ioremap_mem_type = mem_type;
4571c8c3cf0SThomas Petazzoni }
4581c8c3cf0SThomas Petazzoni
pci_remap_iospace(const struct resource * res,phys_addr_t phys_addr)459bc02973aSPali Rohár int pci_remap_iospace(const struct resource *res, phys_addr_t phys_addr)
460bc02973aSPali Rohár {
461bc02973aSPali Rohár unsigned long vaddr = (unsigned long)PCI_IOBASE + res->start;
462bc02973aSPali Rohár
463bc02973aSPali Rohár if (!(res->flags & IORESOURCE_IO))
464bc02973aSPali Rohár return -EINVAL;
465bc02973aSPali Rohár
466bc02973aSPali Rohár if (res->end > IO_SPACE_LIMIT)
467bc02973aSPali Rohár return -EINVAL;
468bc02973aSPali Rohár
469bc02973aSPali Rohár return ioremap_page_range(vaddr, vaddr + resource_size(res), phys_addr,
470bc02973aSPali Rohár __pgprot(get_mem_type(pci_ioremap_mem_type)->prot_pte));
471bc02973aSPali Rohár }
472bc02973aSPali Rohár EXPORT_SYMBOL(pci_remap_iospace);
473bc02973aSPali Rohár
pci_remap_cfgspace(resource_size_t res_cookie,size_t size)474b9cdbe6eSLorenzo Pieralisi void __iomem *pci_remap_cfgspace(resource_size_t res_cookie, size_t size)
475b9cdbe6eSLorenzo Pieralisi {
476b9cdbe6eSLorenzo Pieralisi return arch_ioremap_caller(res_cookie, size, MT_UNCACHED,
477b9cdbe6eSLorenzo Pieralisi __builtin_return_address(0));
478b9cdbe6eSLorenzo Pieralisi }
479b9cdbe6eSLorenzo Pieralisi EXPORT_SYMBOL_GPL(pci_remap_cfgspace);
480c2794437SRob Herring #endif
4812937367bSArd Biesheuvel
4822937367bSArd Biesheuvel /*
4832937367bSArd Biesheuvel * Must be called after early_fixmap_init
4842937367bSArd Biesheuvel */
early_ioremap_init(void)4852937367bSArd Biesheuvel void __init early_ioremap_init(void)
4862937367bSArd Biesheuvel {
4872937367bSArd Biesheuvel early_ioremap_setup();
4882937367bSArd Biesheuvel }
489260364d1SMike Rapoport
arch_memremap_can_ram_remap(resource_size_t offset,size_t size,unsigned long flags)490260364d1SMike Rapoport bool arch_memremap_can_ram_remap(resource_size_t offset, size_t size,
491260364d1SMike Rapoport unsigned long flags)
492260364d1SMike Rapoport {
493260364d1SMike Rapoport unsigned long pfn = PHYS_PFN(offset);
494260364d1SMike Rapoport
495260364d1SMike Rapoport return memblock_is_map_memory(pfn);
496260364d1SMike Rapoport }
497