blob: 88fd86cf3d9a536d63ba4b51589a9e04adea6d27 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/mm/ioremap.c
3 *
4 * Re-map IO memory to kernel address space so that we can access it.
5 *
6 * (C) Copyright 1995 1996 Linus Torvalds
7 *
8 * Hacked for ARM by Phil Blundell <philb@gnu.org>
9 * Hacked to allow all architectures to build, and various cleanups
10 * by Russell King
11 *
12 * This allows a driver to remap an arbitrary region of bus memory into
13 * virtual space. One should *only* use readl, writel, memcpy_toio and
14 * so on with such remapped areas.
15 *
16 * Because the ARM only has a 32-bit address space we can't address the
17 * whole of the (physical) PCI space at once. PCI huge-mode addressing
18 * allows us to circumvent this restriction by splitting PCI space into
19 * two 2GB chunks and mapping only one at a time into processor memory.
20 * We use MMU protection domains to trap any attempt to access the bank
21 * that is not currently mapped. (This isn't fully implemented yet.)
22 */
23#include <linux/module.h>
24#include <linux/errno.h>
25#include <linux/mm.h>
26#include <linux/vmalloc.h>
Russell Kingfced80c2008-09-06 12:10:45 +010027#include <linux/io.h>
Alessandro Rubini158e8bf2012-06-24 12:46:26 +010028#include <linux/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Russell King15d07dc2012-03-28 18:30:01 +010030#include <asm/cp15.h>
Russell King0ba8b9b2008-08-10 18:08:10 +010031#include <asm/cputype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/cacheflush.h>
Russell Kingff0daca2006-06-29 20:17:15 +010033#include <asm/mmu_context.h>
34#include <asm/pgalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/tlbflush.h>
David Howells9f97da72012-03-28 18:30:01 +010036#include <asm/system_info.h>
Russell Kingff0daca2006-06-29 20:17:15 +010037
Russell Kingb29e9f52007-04-21 10:47:29 +010038#include <asm/mach/map.h>
Rob Herringc2794432012-02-29 18:10:58 -060039#include <asm/mach/pci.h>
Russell Kingb29e9f52007-04-21 10:47:29 +010040#include "mm.h"
41
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020042int ioremap_page(unsigned long virt, unsigned long phys,
43 const struct mem_type *mtype)
44{
Russell Kingd7461962010-07-26 10:29:13 +010045 return ioremap_page_range(virt, virt + PAGE_SIZE, phys,
46 __pgprot(mtype->prot_pte));
Hiroshi DOYU69d3a842009-01-28 21:32:08 +020047}
48EXPORT_SYMBOL(ioremap_page);
Russell Kingff0daca2006-06-29 20:17:15 +010049
Nicolas Pitre3e996752012-11-25 03:24:32 +010050void __check_vmalloc_seq(struct mm_struct *mm)
Russell Kingff0daca2006-06-29 20:17:15 +010051{
52 unsigned int seq;
53
54 do {
Nicolas Pitre3e996752012-11-25 03:24:32 +010055 seq = init_mm.context.vmalloc_seq;
Russell Kingff0daca2006-06-29 20:17:15 +010056 memcpy(pgd_offset(mm, VMALLOC_START),
57 pgd_offset_k(VMALLOC_START),
58 sizeof(pgd_t) * (pgd_index(VMALLOC_END) -
59 pgd_index(VMALLOC_START)));
Nicolas Pitre3e996752012-11-25 03:24:32 +010060 mm->context.vmalloc_seq = seq;
61 } while (seq != init_mm.context.vmalloc_seq);
Russell Kingff0daca2006-06-29 20:17:15 +010062}
63
Catalin Marinasda028772011-11-22 17:30:29 +000064#if !defined(CONFIG_SMP) && !defined(CONFIG_ARM_LPAE)
Russell Kingff0daca2006-06-29 20:17:15 +010065/*
66 * Section support is unsafe on SMP - If you iounmap and ioremap a region,
67 * the other CPUs will not see this change until their next context switch.
68 * Meanwhile, (eg) if an interrupt comes in on one of those other CPUs
69 * which requires the new ioremap'd region to be referenced, the CPU will
70 * reference the _old_ region.
71 *
Russell King31aa8fd2009-12-18 11:10:03 +000072 * Note that get_vm_area_caller() allocates a guard 4K page, so we need to
73 * mask the size back to 1MB aligned or we will overflow in the loop below.
Russell Kingff0daca2006-06-29 20:17:15 +010074 */
75static void unmap_area_sections(unsigned long virt, unsigned long size)
76{
Russell King24f11ec2009-01-25 17:36:34 +000077 unsigned long addr = virt, end = virt + (size & ~(SZ_1M - 1));
Russell Kingff0daca2006-06-29 20:17:15 +010078 pgd_t *pgd;
Catalin Marinas03a6b822011-11-22 17:30:27 +000079 pud_t *pud;
80 pmd_t *pmdp;
Russell Kingff0daca2006-06-29 20:17:15 +010081
82 flush_cache_vunmap(addr, end);
83 pgd = pgd_offset_k(addr);
Catalin Marinas03a6b822011-11-22 17:30:27 +000084 pud = pud_offset(pgd, addr);
85 pmdp = pmd_offset(pud, addr);
Russell Kingff0daca2006-06-29 20:17:15 +010086 do {
Catalin Marinas03a6b822011-11-22 17:30:27 +000087 pmd_t pmd = *pmdp;
Russell Kingff0daca2006-06-29 20:17:15 +010088
Russell Kingff0daca2006-06-29 20:17:15 +010089 if (!pmd_none(pmd)) {
90 /*
91 * Clear the PMD from the page table, and
Nicolas Pitre3e996752012-11-25 03:24:32 +010092 * increment the vmalloc sequence so others
Russell Kingff0daca2006-06-29 20:17:15 +010093 * notice this change.
94 *
95 * Note: this is still racy on SMP machines.
96 */
97 pmd_clear(pmdp);
Nicolas Pitre3e996752012-11-25 03:24:32 +010098 init_mm.context.vmalloc_seq++;
Russell Kingff0daca2006-06-29 20:17:15 +010099
100 /*
101 * Free the page table, if there was one.
102 */
103 if ((pmd_val(pmd) & PMD_TYPE_MASK) == PMD_TYPE_TABLE)
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -0800104 pte_free_kernel(&init_mm, pmd_page_vaddr(pmd));
Russell Kingff0daca2006-06-29 20:17:15 +0100105 }
106
Catalin Marinas03a6b822011-11-22 17:30:27 +0000107 addr += PMD_SIZE;
108 pmdp += 2;
Russell Kingff0daca2006-06-29 20:17:15 +0100109 } while (addr < end);
110
111 /*
112 * Ensure that the active_mm is up to date - we want to
113 * catch any use-after-iounmap cases.
114 */
Nicolas Pitre3e996752012-11-25 03:24:32 +0100115 if (current->active_mm->context.vmalloc_seq != init_mm.context.vmalloc_seq)
116 __check_vmalloc_seq(current->active_mm);
Russell Kingff0daca2006-06-29 20:17:15 +0100117
118 flush_tlb_kernel_range(virt, end);
119}
120
121static int
122remap_area_sections(unsigned long virt, unsigned long pfn,
Russell Kingb29e9f52007-04-21 10:47:29 +0100123 size_t size, const struct mem_type *type)
Russell Kingff0daca2006-06-29 20:17:15 +0100124{
Russell Kingb29e9f52007-04-21 10:47:29 +0100125 unsigned long addr = virt, end = virt + size;
Russell Kingff0daca2006-06-29 20:17:15 +0100126 pgd_t *pgd;
Catalin Marinas03a6b822011-11-22 17:30:27 +0000127 pud_t *pud;
128 pmd_t *pmd;
Russell Kingff0daca2006-06-29 20:17:15 +0100129
130 /*
131 * Remove and free any PTE-based mapping, and
132 * sync the current kernel mapping.
133 */
134 unmap_area_sections(virt, size);
135
Russell Kingff0daca2006-06-29 20:17:15 +0100136 pgd = pgd_offset_k(addr);
Catalin Marinas03a6b822011-11-22 17:30:27 +0000137 pud = pud_offset(pgd, addr);
138 pmd = pmd_offset(pud, addr);
Russell Kingff0daca2006-06-29 20:17:15 +0100139 do {
Russell Kingb29e9f52007-04-21 10:47:29 +0100140 pmd[0] = __pmd(__pfn_to_phys(pfn) | type->prot_sect);
Russell Kingff0daca2006-06-29 20:17:15 +0100141 pfn += SZ_1M >> PAGE_SHIFT;
Russell Kingb29e9f52007-04-21 10:47:29 +0100142 pmd[1] = __pmd(__pfn_to_phys(pfn) | type->prot_sect);
Russell Kingff0daca2006-06-29 20:17:15 +0100143 pfn += SZ_1M >> PAGE_SHIFT;
144 flush_pmd_entry(pmd);
145
Catalin Marinas03a6b822011-11-22 17:30:27 +0000146 addr += PMD_SIZE;
147 pmd += 2;
Russell Kingff0daca2006-06-29 20:17:15 +0100148 } while (addr < end);
149
150 return 0;
151}
Lennert Buytenheka069c892006-07-01 19:58:20 +0100152
153static int
154remap_area_supersections(unsigned long virt, unsigned long pfn,
Russell Kingb29e9f52007-04-21 10:47:29 +0100155 size_t size, const struct mem_type *type)
Lennert Buytenheka069c892006-07-01 19:58:20 +0100156{
Russell Kingb29e9f52007-04-21 10:47:29 +0100157 unsigned long addr = virt, end = virt + size;
Lennert Buytenheka069c892006-07-01 19:58:20 +0100158 pgd_t *pgd;
Catalin Marinas03a6b822011-11-22 17:30:27 +0000159 pud_t *pud;
160 pmd_t *pmd;
Lennert Buytenheka069c892006-07-01 19:58:20 +0100161
162 /*
163 * Remove and free any PTE-based mapping, and
164 * sync the current kernel mapping.
165 */
166 unmap_area_sections(virt, size);
167
Lennert Buytenheka069c892006-07-01 19:58:20 +0100168 pgd = pgd_offset_k(virt);
Catalin Marinas03a6b822011-11-22 17:30:27 +0000169 pud = pud_offset(pgd, addr);
170 pmd = pmd_offset(pud, addr);
Lennert Buytenheka069c892006-07-01 19:58:20 +0100171 do {
172 unsigned long super_pmd_val, i;
173
Russell Kingb29e9f52007-04-21 10:47:29 +0100174 super_pmd_val = __pfn_to_phys(pfn) | type->prot_sect |
175 PMD_SECT_SUPER;
Lennert Buytenheka069c892006-07-01 19:58:20 +0100176 super_pmd_val |= ((pfn >> (32 - PAGE_SHIFT)) & 0xf) << 20;
177
178 for (i = 0; i < 8; i++) {
Lennert Buytenheka069c892006-07-01 19:58:20 +0100179 pmd[0] = __pmd(super_pmd_val);
180 pmd[1] = __pmd(super_pmd_val);
181 flush_pmd_entry(pmd);
182
Catalin Marinas03a6b822011-11-22 17:30:27 +0000183 addr += PMD_SIZE;
184 pmd += 2;
Lennert Buytenheka069c892006-07-01 19:58:20 +0100185 }
186
187 pfn += SUPERSECTION_SIZE >> PAGE_SHIFT;
188 } while (addr < end);
189
190 return 0;
191}
Russell Kingff0daca2006-06-29 20:17:15 +0100192#endif
193
Russell King31aa8fd2009-12-18 11:10:03 +0000194void __iomem * __arm_ioremap_pfn_caller(unsigned long pfn,
195 unsigned long offset, size_t size, unsigned int mtype, void *caller)
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000196{
Russell Kingb29e9f52007-04-21 10:47:29 +0100197 const struct mem_type *type;
Russell Kingff0daca2006-06-29 20:17:15 +0100198 int err;
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000199 unsigned long addr;
200 struct vm_struct * area;
Lennert Buytenheka069c892006-07-01 19:58:20 +0100201
Catalin Marinasda028772011-11-22 17:30:29 +0000202#ifndef CONFIG_ARM_LPAE
Lennert Buytenheka069c892006-07-01 19:58:20 +0100203 /*
204 * High mappings must be supersection aligned
205 */
206 if (pfn >= 0x100000 && (__pfn_to_phys(pfn) & ~SUPERSECTION_MASK))
207 return NULL;
Catalin Marinasda028772011-11-22 17:30:29 +0000208#endif
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000209
Russell King3603ab22007-05-05 20:59:27 +0100210 type = get_mem_type(mtype);
211 if (!type)
212 return NULL;
Russell Kingb29e9f52007-04-21 10:47:29 +0100213
Russell King6d78b5f2007-06-03 19:26:04 +0100214 /*
215 * Page align the mapping size, taking account of any offset.
216 */
217 size = PAGE_ALIGN(offset + size);
Russell Kingc924aff2006-12-17 23:29:57 +0000218
Nicolas Pitre576d2f22011-09-16 01:14:23 -0400219 /*
220 * Try to reuse one of the static mapping whenever possible.
221 */
222 read_lock(&vmlist_lock);
223 for (area = vmlist; area; area = area->next) {
224 if (!size || (sizeof(phys_addr_t) == 4 && pfn >= 0x100000))
225 break;
226 if (!(area->flags & VM_ARM_STATIC_MAPPING))
227 continue;
228 if ((area->flags & VM_ARM_MTYPE_MASK) != VM_ARM_MTYPE(mtype))
229 continue;
230 if (__phys_to_pfn(area->phys_addr) > pfn ||
Russell King97f10402012-01-29 14:55:21 +0000231 __pfn_to_phys(pfn) + size-1 > area->phys_addr + area->size-1)
Nicolas Pitre576d2f22011-09-16 01:14:23 -0400232 continue;
233 /* we can drop the lock here as we know *area is static */
234 read_unlock(&vmlist_lock);
235 addr = (unsigned long)area->addr;
236 addr += __pfn_to_phys(pfn) - area->phys_addr;
237 return (void __iomem *) (offset + addr);
238 }
239 read_unlock(&vmlist_lock);
240
241 /*
242 * Don't allow RAM to be mapped - this causes problems with ARMv6+
243 */
244 if (WARN_ON(pfn_valid(pfn)))
245 return NULL;
246
Russell King31aa8fd2009-12-18 11:10:03 +0000247 area = get_vm_area_caller(size, VM_IOREMAP, caller);
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000248 if (!area)
249 return NULL;
250 addr = (unsigned long)area->addr;
Russell Kinga3d71932012-09-28 13:48:48 +0100251 area->phys_addr = __pfn_to_phys(pfn);
Russell Kingff0daca2006-06-29 20:17:15 +0100252
Catalin Marinasda028772011-11-22 17:30:29 +0000253#if !defined(CONFIG_SMP) && !defined(CONFIG_ARM_LPAE)
Catalin Marinas412489a2007-01-25 14:16:47 +0100254 if (DOMAIN_IO == 0 &&
255 (((cpu_architecture() >= CPU_ARCH_ARMv6) && (get_cr() & CR_XP)) ||
Russell King4a56c1e2007-04-21 10:16:48 +0100256 cpu_is_xsc3()) && pfn >= 0x100000 &&
Lennert Buytenheka069c892006-07-01 19:58:20 +0100257 !((__pfn_to_phys(pfn) | size | addr) & ~SUPERSECTION_MASK)) {
258 area->flags |= VM_ARM_SECTION_MAPPING;
Russell Kingb29e9f52007-04-21 10:47:29 +0100259 err = remap_area_supersections(addr, pfn, size, type);
Lennert Buytenheka069c892006-07-01 19:58:20 +0100260 } else if (!((__pfn_to_phys(pfn) | size | addr) & ~PMD_MASK)) {
Russell Kingff0daca2006-06-29 20:17:15 +0100261 area->flags |= VM_ARM_SECTION_MAPPING;
Russell Kingb29e9f52007-04-21 10:47:29 +0100262 err = remap_area_sections(addr, pfn, size, type);
Russell Kingff0daca2006-06-29 20:17:15 +0100263 } else
264#endif
Russell Kingd7461962010-07-26 10:29:13 +0100265 err = ioremap_page_range(addr, addr + size, __pfn_to_phys(pfn),
266 __pgprot(type->prot_pte));
Russell Kingff0daca2006-06-29 20:17:15 +0100267
268 if (err) {
Catalin Marinas478922c2006-05-16 11:30:26 +0100269 vunmap((void *)addr);
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000270 return NULL;
271 }
Russell Kingff0daca2006-06-29 20:17:15 +0100272
273 flush_cache_vmap(addr, addr + size);
274 return (void __iomem *) (offset + addr);
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000275}
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000276
Russell King31aa8fd2009-12-18 11:10:03 +0000277void __iomem *__arm_ioremap_caller(unsigned long phys_addr, size_t size,
278 unsigned int mtype, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000280 unsigned long last_addr;
281 unsigned long offset = phys_addr & ~PAGE_MASK;
282 unsigned long pfn = __phys_to_pfn(phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000284 /*
285 * Don't allow wraparound or zero size
286 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 last_addr = phys_addr + size - 1;
288 if (!size || last_addr < phys_addr)
289 return NULL;
290
Russell King31aa8fd2009-12-18 11:10:03 +0000291 return __arm_ioremap_pfn_caller(pfn, offset, size, mtype,
292 caller);
293}
294
295/*
296 * Remap an arbitrary physical address space into the kernel virtual
297 * address space. Needed when the kernel wants to access high addresses
298 * directly.
299 *
300 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
301 * have to convert them into an offset in a page-aligned mapping, but the
302 * caller shouldn't need to know that small detail.
303 */
304void __iomem *
305__arm_ioremap_pfn(unsigned long pfn, unsigned long offset, size_t size,
306 unsigned int mtype)
307{
308 return __arm_ioremap_pfn_caller(pfn, offset, size, mtype,
309 __builtin_return_address(0));
310}
311EXPORT_SYMBOL(__arm_ioremap_pfn);
312
Rob Herring4fe7ef32012-02-10 17:05:13 -0600313void __iomem * (*arch_ioremap_caller)(unsigned long, size_t,
314 unsigned int, void *) =
315 __arm_ioremap_caller;
316
Russell King31aa8fd2009-12-18 11:10:03 +0000317void __iomem *
318__arm_ioremap(unsigned long phys_addr, size_t size, unsigned int mtype)
319{
Rob Herring4fe7ef32012-02-10 17:05:13 -0600320 return arch_ioremap_caller(phys_addr, size, mtype,
321 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
Russell King3603ab22007-05-05 20:59:27 +0100323EXPORT_SYMBOL(__arm_ioremap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Tony Lindgren6c5482d2011-10-12 01:02:50 +0100325/*
326 * Remap an arbitrary physical address space into the kernel virtual
327 * address space as memory. Needed when the kernel wants to execute
328 * code in external memory. This is needed for reprogramming source
329 * clocks that would affect normal memory for example. Please see
330 * CONFIG_GENERIC_ALLOCATOR for allocating external memory.
331 */
332void __iomem *
333__arm_ioremap_exec(unsigned long phys_addr, size_t size, bool cached)
334{
335 unsigned int mtype;
336
337 if (cached)
338 mtype = MT_MEMORY;
339 else
340 mtype = MT_MEMORY_NONCACHED;
341
342 return __arm_ioremap_caller(phys_addr, size, mtype,
343 __builtin_return_address(0));
344}
345
Russell King09d9bae2008-09-05 14:08:44 +0100346void __iounmap(volatile void __iomem *io_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Russell King09d9bae2008-09-05 14:08:44 +0100348 void *addr = (void *)(PAGE_MASK & (unsigned long)io_addr);
Nicolas Pitre6ee723a2011-09-15 22:12:19 -0400349 struct vm_struct *vm;
Russell Kingff0daca2006-06-29 20:17:15 +0100350
Nicolas Pitre6ee723a2011-09-15 22:12:19 -0400351 read_lock(&vmlist_lock);
352 for (vm = vmlist; vm; vm = vm->next) {
Nicolas Pitre576d2f22011-09-16 01:14:23 -0400353 if (vm->addr > addr)
354 break;
355 if (!(vm->flags & VM_IOREMAP))
356 continue;
357 /* If this is a static mapping we must leave it alone */
358 if ((vm->flags & VM_ARM_STATIC_MAPPING) &&
359 (vm->addr <= addr) && (vm->addr + vm->size > addr)) {
360 read_unlock(&vmlist_lock);
361 return;
362 }
Russell King6ae25a52011-12-08 18:02:04 +0000363#if !defined(CONFIG_SMP) && !defined(CONFIG_ARM_LPAE)
Nicolas Pitre576d2f22011-09-16 01:14:23 -0400364 /*
365 * If this is a section based mapping we need to handle it
366 * specially as the VM subsystem does not know how to handle
367 * such a beast.
368 */
369 if ((vm->addr == addr) &&
370 (vm->flags & VM_ARM_SECTION_MAPPING)) {
371 unmap_area_sections((unsigned long)vm->addr, vm->size);
Russell Kingff0daca2006-06-29 20:17:15 +0100372 break;
373 }
Nicolas Pitre576d2f22011-09-16 01:14:23 -0400374#endif
Russell Kingff0daca2006-06-29 20:17:15 +0100375 }
Nicolas Pitre6ee723a2011-09-15 22:12:19 -0400376 read_unlock(&vmlist_lock);
Russell Kingff0daca2006-06-29 20:17:15 +0100377
Russell King24f11ec2009-01-25 17:36:34 +0000378 vunmap(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
Rob Herring4fe7ef32012-02-10 17:05:13 -0600380
381void (*arch_iounmap)(volatile void __iomem *) = __iounmap;
382
383void __arm_iounmap(volatile void __iomem *io_addr)
384{
385 arch_iounmap(io_addr);
386}
387EXPORT_SYMBOL(__arm_iounmap);
Rob Herringc2794432012-02-29 18:10:58 -0600388
389#ifdef CONFIG_PCI
390int pci_ioremap_io(unsigned int offset, phys_addr_t phys_addr)
391{
392 BUG_ON(offset + SZ_64K > IO_SPACE_LIMIT);
393
394 return ioremap_page_range(PCI_IO_VIRT_BASE + offset,
395 PCI_IO_VIRT_BASE + offset + SZ_64K,
396 phys_addr,
397 __pgprot(get_mem_type(MT_DEVICE)->prot_pte));
398}
399EXPORT_SYMBOL_GPL(pci_ioremap_io);
400#endif