blob: 33eeab0ce3d167dffd4bead06d72dd31538d07d3 [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>
27
28#include <asm/cacheflush.h>
29#include <asm/io.h>
Russell Kingff0daca2006-06-29 20:17:15 +010030#include <asm/mmu_context.h>
31#include <asm/pgalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/tlbflush.h>
Russell Kingff0daca2006-06-29 20:17:15 +010033#include <asm/sizes.h>
34
Russell Kingb29e9f52007-04-21 10:47:29 +010035#include <asm/mach/map.h>
36#include "mm.h"
37
Russell Kingff0daca2006-06-29 20:17:15 +010038/*
Lennert Buytenheka069c892006-07-01 19:58:20 +010039 * Used by ioremap() and iounmap() code to mark (super)section-mapped
40 * I/O regions in vm_struct->flags field.
Russell Kingff0daca2006-06-29 20:17:15 +010041 */
42#define VM_ARM_SECTION_MAPPING 0x80000000
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Russell Kingda2c12a2006-12-13 14:35:58 +000044static int remap_area_pte(pmd_t *pmd, unsigned long addr, unsigned long end,
Russell Kingb29e9f52007-04-21 10:47:29 +010045 unsigned long phys_addr, const struct mem_type *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Russell Kingb29e9f52007-04-21 10:47:29 +010047 pgprot_t prot = __pgprot(type->prot_pte);
Russell Kingda2c12a2006-12-13 14:35:58 +000048 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Russell Kingda2c12a2006-12-13 14:35:58 +000050 pte = pte_alloc_kernel(pmd, addr);
51 if (!pte)
52 return -ENOMEM;
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 do {
55 if (!pte_none(*pte))
56 goto bad;
57
Russell King40d192b2008-09-06 21:15:56 +010058 set_pte_ext(pte, pfn_pte(phys_addr >> PAGE_SHIFT, prot), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 phys_addr += PAGE_SIZE;
Russell Kingda2c12a2006-12-13 14:35:58 +000060 } while (pte++, addr += PAGE_SIZE, addr != end);
61 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 bad:
Russell Kingda2c12a2006-12-13 14:35:58 +000064 printk(KERN_CRIT "remap_area_pte: page already exists\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 BUG();
66}
67
Russell Kingda2c12a2006-12-13 14:35:58 +000068static inline int remap_area_pmd(pgd_t *pgd, unsigned long addr,
69 unsigned long end, unsigned long phys_addr,
Russell Kingb29e9f52007-04-21 10:47:29 +010070 const struct mem_type *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Russell Kingda2c12a2006-12-13 14:35:58 +000072 unsigned long next;
73 pmd_t *pmd;
74 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Russell Kingda2c12a2006-12-13 14:35:58 +000076 pmd = pmd_alloc(&init_mm, pgd, addr);
77 if (!pmd)
78 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 do {
Russell Kingda2c12a2006-12-13 14:35:58 +000081 next = pmd_addr_end(addr, end);
Russell Kingb29e9f52007-04-21 10:47:29 +010082 ret = remap_area_pte(pmd, addr, next, phys_addr, type);
Russell Kingda2c12a2006-12-13 14:35:58 +000083 if (ret)
84 return ret;
85 phys_addr += next - addr;
86 } while (pmd++, addr = next, addr != end);
87 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Russell Kingda2c12a2006-12-13 14:35:58 +000090static int remap_area_pages(unsigned long start, unsigned long pfn,
Russell Kingb29e9f52007-04-21 10:47:29 +010091 size_t size, const struct mem_type *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Russell Kingda2c12a2006-12-13 14:35:58 +000093 unsigned long addr = start;
94 unsigned long next, end = start + size;
Deepak Saxena9d4ae722006-01-09 19:23:11 +000095 unsigned long phys_addr = __pfn_to_phys(pfn);
Russell Kingda2c12a2006-12-13 14:35:58 +000096 pgd_t *pgd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Russell Kingda2c12a2006-12-13 14:35:58 +000099 BUG_ON(addr >= end);
100 pgd = pgd_offset_k(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 do {
Russell Kingda2c12a2006-12-13 14:35:58 +0000102 next = pgd_addr_end(addr, end);
Russell Kingb29e9f52007-04-21 10:47:29 +0100103 err = remap_area_pmd(pgd, addr, next, phys_addr, type);
Russell Kingda2c12a2006-12-13 14:35:58 +0000104 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 break;
Russell Kingda2c12a2006-12-13 14:35:58 +0000106 phys_addr += next - addr;
107 } while (pgd++, addr = next, addr != end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return err;
110}
111
Russell Kingff0daca2006-06-29 20:17:15 +0100112
113void __check_kvm_seq(struct mm_struct *mm)
114{
115 unsigned int seq;
116
117 do {
118 seq = init_mm.context.kvm_seq;
119 memcpy(pgd_offset(mm, VMALLOC_START),
120 pgd_offset_k(VMALLOC_START),
121 sizeof(pgd_t) * (pgd_index(VMALLOC_END) -
122 pgd_index(VMALLOC_START)));
123 mm->context.kvm_seq = seq;
124 } while (seq != init_mm.context.kvm_seq);
125}
126
127#ifndef CONFIG_SMP
128/*
129 * Section support is unsafe on SMP - If you iounmap and ioremap a region,
130 * the other CPUs will not see this change until their next context switch.
131 * Meanwhile, (eg) if an interrupt comes in on one of those other CPUs
132 * which requires the new ioremap'd region to be referenced, the CPU will
133 * reference the _old_ region.
134 *
135 * Note that get_vm_area() allocates a guard 4K page, so we need to mask
136 * the size back to 1MB aligned or we will overflow in the loop below.
137 */
138static void unmap_area_sections(unsigned long virt, unsigned long size)
139{
140 unsigned long addr = virt, end = virt + (size & ~SZ_1M);
141 pgd_t *pgd;
142
143 flush_cache_vunmap(addr, end);
144 pgd = pgd_offset_k(addr);
145 do {
146 pmd_t pmd, *pmdp = pmd_offset(pgd, addr);
147
148 pmd = *pmdp;
149 if (!pmd_none(pmd)) {
150 /*
151 * Clear the PMD from the page table, and
152 * increment the kvm sequence so others
153 * notice this change.
154 *
155 * Note: this is still racy on SMP machines.
156 */
157 pmd_clear(pmdp);
158 init_mm.context.kvm_seq++;
159
160 /*
161 * Free the page table, if there was one.
162 */
163 if ((pmd_val(pmd) & PMD_TYPE_MASK) == PMD_TYPE_TABLE)
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -0800164 pte_free_kernel(&init_mm, pmd_page_vaddr(pmd));
Russell Kingff0daca2006-06-29 20:17:15 +0100165 }
166
167 addr += PGDIR_SIZE;
168 pgd++;
169 } while (addr < end);
170
171 /*
172 * Ensure that the active_mm is up to date - we want to
173 * catch any use-after-iounmap cases.
174 */
175 if (current->active_mm->context.kvm_seq != init_mm.context.kvm_seq)
176 __check_kvm_seq(current->active_mm);
177
178 flush_tlb_kernel_range(virt, end);
179}
180
181static int
182remap_area_sections(unsigned long virt, unsigned long pfn,
Russell Kingb29e9f52007-04-21 10:47:29 +0100183 size_t size, const struct mem_type *type)
Russell Kingff0daca2006-06-29 20:17:15 +0100184{
Russell Kingb29e9f52007-04-21 10:47:29 +0100185 unsigned long addr = virt, end = virt + size;
Russell Kingff0daca2006-06-29 20:17:15 +0100186 pgd_t *pgd;
187
188 /*
189 * Remove and free any PTE-based mapping, and
190 * sync the current kernel mapping.
191 */
192 unmap_area_sections(virt, size);
193
Russell Kingff0daca2006-06-29 20:17:15 +0100194 pgd = pgd_offset_k(addr);
195 do {
196 pmd_t *pmd = pmd_offset(pgd, addr);
197
Russell Kingb29e9f52007-04-21 10:47:29 +0100198 pmd[0] = __pmd(__pfn_to_phys(pfn) | type->prot_sect);
Russell Kingff0daca2006-06-29 20:17:15 +0100199 pfn += SZ_1M >> PAGE_SHIFT;
Russell Kingb29e9f52007-04-21 10:47:29 +0100200 pmd[1] = __pmd(__pfn_to_phys(pfn) | type->prot_sect);
Russell Kingff0daca2006-06-29 20:17:15 +0100201 pfn += SZ_1M >> PAGE_SHIFT;
202 flush_pmd_entry(pmd);
203
204 addr += PGDIR_SIZE;
205 pgd++;
206 } while (addr < end);
207
208 return 0;
209}
Lennert Buytenheka069c892006-07-01 19:58:20 +0100210
211static int
212remap_area_supersections(unsigned long virt, unsigned long pfn,
Russell Kingb29e9f52007-04-21 10:47:29 +0100213 size_t size, const struct mem_type *type)
Lennert Buytenheka069c892006-07-01 19:58:20 +0100214{
Russell Kingb29e9f52007-04-21 10:47:29 +0100215 unsigned long addr = virt, end = virt + size;
Lennert Buytenheka069c892006-07-01 19:58:20 +0100216 pgd_t *pgd;
217
218 /*
219 * Remove and free any PTE-based mapping, and
220 * sync the current kernel mapping.
221 */
222 unmap_area_sections(virt, size);
223
Lennert Buytenheka069c892006-07-01 19:58:20 +0100224 pgd = pgd_offset_k(virt);
225 do {
226 unsigned long super_pmd_val, i;
227
Russell Kingb29e9f52007-04-21 10:47:29 +0100228 super_pmd_val = __pfn_to_phys(pfn) | type->prot_sect |
229 PMD_SECT_SUPER;
Lennert Buytenheka069c892006-07-01 19:58:20 +0100230 super_pmd_val |= ((pfn >> (32 - PAGE_SHIFT)) & 0xf) << 20;
231
232 for (i = 0; i < 8; i++) {
233 pmd_t *pmd = pmd_offset(pgd, addr);
234
235 pmd[0] = __pmd(super_pmd_val);
236 pmd[1] = __pmd(super_pmd_val);
237 flush_pmd_entry(pmd);
238
239 addr += PGDIR_SIZE;
240 pgd++;
241 }
242
243 pfn += SUPERSECTION_SIZE >> PAGE_SHIFT;
244 } while (addr < end);
245
246 return 0;
247}
Russell Kingff0daca2006-06-29 20:17:15 +0100248#endif
249
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*
252 * Remap an arbitrary physical address space into the kernel virtual
253 * address space. Needed when the kernel wants to access high addresses
254 * directly.
255 *
256 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
257 * have to convert them into an offset in a page-aligned mapping, but the
258 * caller shouldn't need to know that small detail.
259 *
260 * 'flags' are the extra L_PTE_ flags that you want to specify for this
Russell King4baa9922008-08-02 10:55:55 +0100261 * mapping. See <asm/pgtable.h> for more information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 */
263void __iomem *
Russell King3603ab22007-05-05 20:59:27 +0100264__arm_ioremap_pfn(unsigned long pfn, unsigned long offset, size_t size,
265 unsigned int mtype)
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000266{
Russell Kingb29e9f52007-04-21 10:47:29 +0100267 const struct mem_type *type;
Russell Kingff0daca2006-06-29 20:17:15 +0100268 int err;
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000269 unsigned long addr;
270 struct vm_struct * area;
Lennert Buytenheka069c892006-07-01 19:58:20 +0100271
272 /*
273 * High mappings must be supersection aligned
274 */
275 if (pfn >= 0x100000 && (__pfn_to_phys(pfn) & ~SUPERSECTION_MASK))
276 return NULL;
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000277
Russell King3603ab22007-05-05 20:59:27 +0100278 type = get_mem_type(mtype);
279 if (!type)
280 return NULL;
Russell Kingb29e9f52007-04-21 10:47:29 +0100281
Russell King6d78b5f2007-06-03 19:26:04 +0100282 /*
283 * Page align the mapping size, taking account of any offset.
284 */
285 size = PAGE_ALIGN(offset + size);
Russell Kingc924aff2006-12-17 23:29:57 +0000286
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000287 area = get_vm_area(size, VM_IOREMAP);
288 if (!area)
289 return NULL;
290 addr = (unsigned long)area->addr;
Russell Kingff0daca2006-06-29 20:17:15 +0100291
292#ifndef CONFIG_SMP
Catalin Marinas412489a2007-01-25 14:16:47 +0100293 if (DOMAIN_IO == 0 &&
294 (((cpu_architecture() >= CPU_ARCH_ARMv6) && (get_cr() & CR_XP)) ||
Russell King4a56c1e2007-04-21 10:16:48 +0100295 cpu_is_xsc3()) && pfn >= 0x100000 &&
Lennert Buytenheka069c892006-07-01 19:58:20 +0100296 !((__pfn_to_phys(pfn) | size | addr) & ~SUPERSECTION_MASK)) {
297 area->flags |= VM_ARM_SECTION_MAPPING;
Russell Kingb29e9f52007-04-21 10:47:29 +0100298 err = remap_area_supersections(addr, pfn, size, type);
Lennert Buytenheka069c892006-07-01 19:58:20 +0100299 } else if (!((__pfn_to_phys(pfn) | size | addr) & ~PMD_MASK)) {
Russell Kingff0daca2006-06-29 20:17:15 +0100300 area->flags |= VM_ARM_SECTION_MAPPING;
Russell Kingb29e9f52007-04-21 10:47:29 +0100301 err = remap_area_sections(addr, pfn, size, type);
Russell Kingff0daca2006-06-29 20:17:15 +0100302 } else
303#endif
Russell Kingb29e9f52007-04-21 10:47:29 +0100304 err = remap_area_pages(addr, pfn, size, type);
Russell Kingff0daca2006-06-29 20:17:15 +0100305
306 if (err) {
Catalin Marinas478922c2006-05-16 11:30:26 +0100307 vunmap((void *)addr);
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000308 return NULL;
309 }
Russell Kingff0daca2006-06-29 20:17:15 +0100310
311 flush_cache_vmap(addr, addr + size);
312 return (void __iomem *) (offset + addr);
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000313}
Russell King3603ab22007-05-05 20:59:27 +0100314EXPORT_SYMBOL(__arm_ioremap_pfn);
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000315
316void __iomem *
Russell King3603ab22007-05-05 20:59:27 +0100317__arm_ioremap(unsigned long phys_addr, size_t size, unsigned int mtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000319 unsigned long last_addr;
320 unsigned long offset = phys_addr & ~PAGE_MASK;
321 unsigned long pfn = __phys_to_pfn(phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000323 /*
324 * Don't allow wraparound or zero size
325 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 last_addr = phys_addr + size - 1;
327 if (!size || last_addr < phys_addr)
328 return NULL;
329
Russell King3603ab22007-05-05 20:59:27 +0100330 return __arm_ioremap_pfn(pfn, offset, size, mtype);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
Russell King3603ab22007-05-05 20:59:27 +0100332EXPORT_SYMBOL(__arm_ioremap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Al Viro16226052006-10-09 02:09:49 +0100334void __iounmap(volatile void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Catalin Marinasceaccbd2006-07-29 08:29:30 +0100336#ifndef CONFIG_SMP
Russell Kingff0daca2006-06-29 20:17:15 +0100337 struct vm_struct **p, *tmp;
Catalin Marinasceaccbd2006-07-29 08:29:30 +0100338#endif
Russell Kingff0daca2006-06-29 20:17:15 +0100339 unsigned int section_mapping = 0;
340
Al Viro16226052006-10-09 02:09:49 +0100341 addr = (volatile void __iomem *)(PAGE_MASK & (unsigned long)addr);
Russell Kingff0daca2006-06-29 20:17:15 +0100342
Lennert Buytenhek7cddc392006-07-03 12:26:02 +0100343#ifndef CONFIG_SMP
Russell Kingff0daca2006-06-29 20:17:15 +0100344 /*
345 * If this is a section based mapping we need to handle it
Simon Arlott6cbdc8c2007-05-11 20:40:30 +0100346 * specially as the VM subsystem does not know how to handle
Russell Kingff0daca2006-06-29 20:17:15 +0100347 * such a beast. We need the lock here b/c we need to clear
348 * all the mappings before the area can be reclaimed
349 * by someone else.
350 */
351 write_lock(&vmlist_lock);
352 for (p = &vmlist ; (tmp = *p) ; p = &tmp->next) {
353 if((tmp->flags & VM_IOREMAP) && (tmp->addr == addr)) {
354 if (tmp->flags & VM_ARM_SECTION_MAPPING) {
355 *p = tmp->next;
356 unmap_area_sections((unsigned long)tmp->addr,
357 tmp->size);
358 kfree(tmp);
359 section_mapping = 1;
360 }
361 break;
362 }
363 }
364 write_unlock(&vmlist_lock);
Lennert Buytenhek7cddc392006-07-03 12:26:02 +0100365#endif
Russell Kingff0daca2006-06-29 20:17:15 +0100366
367 if (!section_mapping)
Al Viro16226052006-10-09 02:09:49 +0100368 vunmap((void __force *)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370EXPORT_SYMBOL(__iounmap);