blob: 685dc1bf00833b4fd1efcae33a6426b5fcfdb5fd [file] [log] [blame]
Russell Kingd111e8f2006-09-27 15:27:33 +01001/*
2 * linux/arch/arm/mm/mmu.c
3 *
4 * Copyright (C) 1995-2005 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
Russell Kingae8f1542006-09-27 15:38:34 +010010#include <linux/module.h>
Russell Kingd111e8f2006-09-27 15:27:33 +010011#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/init.h>
14#include <linux/bootmem.h>
15#include <linux/mman.h>
16#include <linux/nodemask.h>
17
18#include <asm/mach-types.h>
19#include <asm/setup.h>
20#include <asm/sizes.h>
21#include <asm/tlb.h>
22
23#include <asm/mach/arch.h>
24#include <asm/mach/map.h>
25
26#include "mm.h"
27
28DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
29
Russell King6ae5a6e2006-09-30 10:50:05 +010030extern void _stext, _etext, __data_start, _end;
Russell Kingd111e8f2006-09-27 15:27:33 +010031extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
32
33/*
34 * empty_zero_page is a special page that is used for
35 * zero-initialized data and COW.
36 */
37struct page *empty_zero_page;
Aneesh Kumar K.V3653f3a2008-04-29 08:11:12 -040038EXPORT_SYMBOL(empty_zero_page);
Russell Kingd111e8f2006-09-27 15:27:33 +010039
40/*
41 * The pmd table for the upper-most set of pages.
42 */
43pmd_t *top_pmd;
44
Russell Kingae8f1542006-09-27 15:38:34 +010045#define CPOLICY_UNCACHED 0
46#define CPOLICY_BUFFERED 1
47#define CPOLICY_WRITETHROUGH 2
48#define CPOLICY_WRITEBACK 3
49#define CPOLICY_WRITEALLOC 4
50
51static unsigned int cachepolicy __initdata = CPOLICY_WRITEBACK;
52static unsigned int ecc_mask __initdata = 0;
Imre_Deak44b18692007-02-11 13:45:13 +010053pgprot_t pgprot_user;
Russell Kingae8f1542006-09-27 15:38:34 +010054pgprot_t pgprot_kernel;
55
Imre_Deak44b18692007-02-11 13:45:13 +010056EXPORT_SYMBOL(pgprot_user);
Russell Kingae8f1542006-09-27 15:38:34 +010057EXPORT_SYMBOL(pgprot_kernel);
58
59struct cachepolicy {
60 const char policy[16];
61 unsigned int cr_mask;
62 unsigned int pmd;
63 unsigned int pte;
64};
65
66static struct cachepolicy cache_policies[] __initdata = {
67 {
68 .policy = "uncached",
69 .cr_mask = CR_W|CR_C,
70 .pmd = PMD_SECT_UNCACHED,
Russell Kingbb30f362008-09-06 20:04:59 +010071 .pte = L_PTE_MT_UNCACHED,
Russell Kingae8f1542006-09-27 15:38:34 +010072 }, {
73 .policy = "buffered",
74 .cr_mask = CR_C,
75 .pmd = PMD_SECT_BUFFERED,
Russell Kingbb30f362008-09-06 20:04:59 +010076 .pte = L_PTE_MT_BUFFERABLE,
Russell Kingae8f1542006-09-27 15:38:34 +010077 }, {
78 .policy = "writethrough",
79 .cr_mask = 0,
80 .pmd = PMD_SECT_WT,
Russell Kingbb30f362008-09-06 20:04:59 +010081 .pte = L_PTE_MT_WRITETHROUGH,
Russell Kingae8f1542006-09-27 15:38:34 +010082 }, {
83 .policy = "writeback",
84 .cr_mask = 0,
85 .pmd = PMD_SECT_WB,
Russell Kingbb30f362008-09-06 20:04:59 +010086 .pte = L_PTE_MT_WRITEBACK,
Russell Kingae8f1542006-09-27 15:38:34 +010087 }, {
88 .policy = "writealloc",
89 .cr_mask = 0,
90 .pmd = PMD_SECT_WBWA,
Russell Kingbb30f362008-09-06 20:04:59 +010091 .pte = L_PTE_MT_WRITEALLOC,
Russell Kingae8f1542006-09-27 15:38:34 +010092 }
93};
94
95/*
Simon Arlott6cbdc8c2007-05-11 20:40:30 +010096 * These are useful for identifying cache coherency
Russell Kingae8f1542006-09-27 15:38:34 +010097 * problems by allowing the cache or the cache and
98 * writebuffer to be turned off. (Note: the write
99 * buffer should not be on and the cache off).
100 */
101static void __init early_cachepolicy(char **p)
102{
103 int i;
104
105 for (i = 0; i < ARRAY_SIZE(cache_policies); i++) {
106 int len = strlen(cache_policies[i].policy);
107
108 if (memcmp(*p, cache_policies[i].policy, len) == 0) {
109 cachepolicy = i;
110 cr_alignment &= ~cache_policies[i].cr_mask;
111 cr_no_alignment &= ~cache_policies[i].cr_mask;
112 *p += len;
113 break;
114 }
115 }
116 if (i == ARRAY_SIZE(cache_policies))
117 printk(KERN_ERR "ERROR: unknown or unsupported cache policy\n");
Catalin Marinas11179d82007-07-20 11:42:24 +0100118 if (cpu_architecture() >= CPU_ARCH_ARMv6) {
119 printk(KERN_WARNING "Only cachepolicy=writeback supported on ARMv6 and later\n");
120 cachepolicy = CPOLICY_WRITEBACK;
121 }
Russell Kingae8f1542006-09-27 15:38:34 +0100122 flush_cache_all();
123 set_cr(cr_alignment);
124}
125__early_param("cachepolicy=", early_cachepolicy);
126
127static void __init early_nocache(char **__unused)
128{
129 char *p = "buffered";
130 printk(KERN_WARNING "nocache is deprecated; use cachepolicy=%s\n", p);
131 early_cachepolicy(&p);
132}
133__early_param("nocache", early_nocache);
134
135static void __init early_nowrite(char **__unused)
136{
137 char *p = "uncached";
138 printk(KERN_WARNING "nowb is deprecated; use cachepolicy=%s\n", p);
139 early_cachepolicy(&p);
140}
141__early_param("nowb", early_nowrite);
142
143static void __init early_ecc(char **p)
144{
145 if (memcmp(*p, "on", 2) == 0) {
146 ecc_mask = PMD_PROTECTION;
147 *p += 2;
148 } else if (memcmp(*p, "off", 3) == 0) {
149 ecc_mask = 0;
150 *p += 3;
151 }
152}
153__early_param("ecc=", early_ecc);
154
155static int __init noalign_setup(char *__unused)
156{
157 cr_alignment &= ~CR_A;
158 cr_no_alignment &= ~CR_A;
159 set_cr(cr_alignment);
160 return 1;
161}
162__setup("noalign", noalign_setup);
163
Russell King255d1f82006-12-18 00:12:47 +0000164#ifndef CONFIG_SMP
165void adjust_cr(unsigned long mask, unsigned long set)
166{
167 unsigned long flags;
168
169 mask &= ~CR_A;
170
171 set &= mask;
172
173 local_irq_save(flags);
174
175 cr_no_alignment = (cr_no_alignment & ~mask) | set;
176 cr_alignment = (cr_alignment & ~mask) | set;
177
178 set_cr((get_cr() & ~mask) | set);
179
180 local_irq_restore(flags);
181}
182#endif
183
Russell King0af92be2007-05-05 20:28:16 +0100184#define PROT_PTE_DEVICE L_PTE_PRESENT|L_PTE_YOUNG|L_PTE_DIRTY|L_PTE_WRITE
185#define PROT_SECT_DEVICE PMD_TYPE_SECT|PMD_SECT_XN|PMD_SECT_AP_WRITE
186
Russell Kingb29e9f52007-04-21 10:47:29 +0100187static struct mem_type mem_types[] = {
Russell King0af92be2007-05-05 20:28:16 +0100188 [MT_DEVICE] = { /* Strongly ordered / ARMv6 shared device */
Russell Kingbb30f362008-09-06 20:04:59 +0100189 .prot_pte = PROT_PTE_DEVICE | L_PTE_MT_DEV_SHARED |
190 L_PTE_SHARED,
Russell King0af92be2007-05-05 20:28:16 +0100191 .prot_l1 = PMD_TYPE_TABLE,
192 .prot_sect = PROT_SECT_DEVICE | PMD_SECT_UNCACHED,
193 .domain = DOMAIN_IO,
194 },
195 [MT_DEVICE_NONSHARED] = { /* ARMv6 non-shared device */
Russell Kingbb30f362008-09-06 20:04:59 +0100196 .prot_pte = PROT_PTE_DEVICE | L_PTE_MT_DEV_NONSHARED,
Russell King0af92be2007-05-05 20:28:16 +0100197 .prot_l1 = PMD_TYPE_TABLE,
198 .prot_sect = PROT_SECT_DEVICE | PMD_SECT_TEX(2),
199 .domain = DOMAIN_IO,
200 },
201 [MT_DEVICE_CACHED] = { /* ioremap_cached */
Russell Kingbb30f362008-09-06 20:04:59 +0100202 .prot_pte = PROT_PTE_DEVICE | L_PTE_MT_DEV_CACHED,
Russell King0af92be2007-05-05 20:28:16 +0100203 .prot_l1 = PMD_TYPE_TABLE,
204 .prot_sect = PROT_SECT_DEVICE | PMD_SECT_WB,
205 .domain = DOMAIN_IO,
206 },
207 [MT_DEVICE_IXP2000] = { /* IXP2400 requires XCB=101 for on-chip I/O */
Russell Kingbb30f362008-09-06 20:04:59 +0100208 .prot_pte = PROT_PTE_DEVICE | L_PTE_MT_DEV_IXP2000,
Russell King0af92be2007-05-05 20:28:16 +0100209 .prot_l1 = PMD_TYPE_TABLE,
210 .prot_sect = PROT_SECT_DEVICE | PMD_SECT_BUFFERABLE |
211 PMD_SECT_TEX(1),
212 .domain = DOMAIN_IO,
Russell Kingae8f1542006-09-27 15:38:34 +0100213 },
Lennert Buytenhek1ad77a82008-09-05 13:17:11 +0100214 [MT_DEVICE_WC] = { /* ioremap_wc */
Russell Kingbb30f362008-09-06 20:04:59 +0100215 .prot_pte = PROT_PTE_DEVICE | L_PTE_MT_DEV_WC,
Lennert Buytenhek1ad77a82008-09-05 13:17:11 +0100216 .prot_l1 = PMD_TYPE_TABLE,
Russell Kingbb30f362008-09-06 20:04:59 +0100217 .prot_sect = PROT_SECT_DEVICE | PMD_SECT_BUFFERABLE,
Lennert Buytenhek1ad77a82008-09-05 13:17:11 +0100218 .domain = DOMAIN_IO,
219 },
Russell Kingae8f1542006-09-27 15:38:34 +0100220 [MT_CACHECLEAN] = {
Russell King9ef79632007-05-05 20:03:35 +0100221 .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN,
Russell Kingae8f1542006-09-27 15:38:34 +0100222 .domain = DOMAIN_KERNEL,
223 },
224 [MT_MINICLEAN] = {
Russell King9ef79632007-05-05 20:03:35 +0100225 .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN | PMD_SECT_MINICACHE,
Russell Kingae8f1542006-09-27 15:38:34 +0100226 .domain = DOMAIN_KERNEL,
227 },
228 [MT_LOW_VECTORS] = {
229 .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
230 L_PTE_EXEC,
231 .prot_l1 = PMD_TYPE_TABLE,
232 .domain = DOMAIN_USER,
233 },
234 [MT_HIGH_VECTORS] = {
235 .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
236 L_PTE_USER | L_PTE_EXEC,
237 .prot_l1 = PMD_TYPE_TABLE,
238 .domain = DOMAIN_USER,
239 },
240 [MT_MEMORY] = {
Russell King9ef79632007-05-05 20:03:35 +0100241 .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
Russell Kingae8f1542006-09-27 15:38:34 +0100242 .domain = DOMAIN_KERNEL,
243 },
244 [MT_ROM] = {
Russell King9ef79632007-05-05 20:03:35 +0100245 .prot_sect = PMD_TYPE_SECT,
Russell Kingae8f1542006-09-27 15:38:34 +0100246 .domain = DOMAIN_KERNEL,
247 },
Russell Kingae8f1542006-09-27 15:38:34 +0100248};
249
Russell Kingb29e9f52007-04-21 10:47:29 +0100250const struct mem_type *get_mem_type(unsigned int type)
251{
252 return type < ARRAY_SIZE(mem_types) ? &mem_types[type] : NULL;
253}
254
Russell Kingae8f1542006-09-27 15:38:34 +0100255/*
256 * Adjust the PMD section entries according to the CPU in use.
257 */
258static void __init build_mem_type_table(void)
259{
260 struct cachepolicy *cp;
261 unsigned int cr = get_cr();
Russell Kingbb30f362008-09-06 20:04:59 +0100262 unsigned int user_pgprot, kern_pgprot, vecs_pgprot;
Russell Kingae8f1542006-09-27 15:38:34 +0100263 int cpu_arch = cpu_architecture();
264 int i;
265
Catalin Marinas11179d82007-07-20 11:42:24 +0100266 if (cpu_arch < CPU_ARCH_ARMv6) {
Russell Kingae8f1542006-09-27 15:38:34 +0100267#if defined(CONFIG_CPU_DCACHE_DISABLE)
Catalin Marinas11179d82007-07-20 11:42:24 +0100268 if (cachepolicy > CPOLICY_BUFFERED)
269 cachepolicy = CPOLICY_BUFFERED;
Russell Kingae8f1542006-09-27 15:38:34 +0100270#elif defined(CONFIG_CPU_DCACHE_WRITETHROUGH)
Catalin Marinas11179d82007-07-20 11:42:24 +0100271 if (cachepolicy > CPOLICY_WRITETHROUGH)
272 cachepolicy = CPOLICY_WRITETHROUGH;
Russell Kingae8f1542006-09-27 15:38:34 +0100273#endif
Catalin Marinas11179d82007-07-20 11:42:24 +0100274 }
Russell Kingae8f1542006-09-27 15:38:34 +0100275 if (cpu_arch < CPU_ARCH_ARMv5) {
276 if (cachepolicy >= CPOLICY_WRITEALLOC)
277 cachepolicy = CPOLICY_WRITEBACK;
278 ecc_mask = 0;
279 }
Russell Kingbb30f362008-09-06 20:04:59 +0100280#ifdef CONFIG_SMP
281 cachepolicy = CPOLICY_WRITEALLOC;
282#endif
Russell Kingae8f1542006-09-27 15:38:34 +0100283
284 /*
Lennert Buytenhek1ad77a82008-09-05 13:17:11 +0100285 * On non-Xscale3 ARMv5-and-older systems, use CB=01
286 * (Uncached/Buffered) for ioremap_wc() mappings. On XScale3
287 * and ARMv6+, use TEXCB=00100 mappings (Inner/Outer Uncacheable
288 * in xsc3 parlance, Uncached Normal in ARMv6 parlance).
289 */
290 if (cpu_is_xsc3() || cpu_arch >= CPU_ARCH_ARMv6) {
Lennert Buytenhek1ad77a82008-09-05 13:17:11 +0100291 mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_TEX(1);
Russell Kingbb30f362008-09-06 20:04:59 +0100292 mem_types[MT_DEVICE_WC].prot_sect &= ~PMD_SECT_BUFFERABLE;
Lennert Buytenhek1ad77a82008-09-05 13:17:11 +0100293 }
294
295 /*
Russell King9ef79632007-05-05 20:03:35 +0100296 * ARMv5 and lower, bit 4 must be set for page tables.
297 * (was: cache "update-able on write" bit on ARM610)
298 * However, Xscale cores require this bit to be cleared.
Russell Kingae8f1542006-09-27 15:38:34 +0100299 */
Russell King9ef79632007-05-05 20:03:35 +0100300 if (cpu_is_xscale()) {
301 for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
Russell Kingae8f1542006-09-27 15:38:34 +0100302 mem_types[i].prot_sect &= ~PMD_BIT4;
Russell King9ef79632007-05-05 20:03:35 +0100303 mem_types[i].prot_l1 &= ~PMD_BIT4;
304 }
305 } else if (cpu_arch < CPU_ARCH_ARMv6) {
306 for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
Russell Kingae8f1542006-09-27 15:38:34 +0100307 if (mem_types[i].prot_l1)
308 mem_types[i].prot_l1 |= PMD_BIT4;
Russell King9ef79632007-05-05 20:03:35 +0100309 if (mem_types[i].prot_sect)
310 mem_types[i].prot_sect |= PMD_BIT4;
311 }
312 }
Russell Kingae8f1542006-09-27 15:38:34 +0100313
314 cp = &cache_policies[cachepolicy];
Russell Kingbb30f362008-09-06 20:04:59 +0100315 vecs_pgprot = kern_pgprot = user_pgprot = cp->pte;
316
317#ifndef CONFIG_SMP
318 /*
319 * Only use write-through for non-SMP systems
320 */
321 if (cpu_arch >= CPU_ARCH_ARMv5 && cachepolicy > CPOLICY_WRITETHROUGH)
322 vecs_pgprot = cache_policies[CPOLICY_WRITETHROUGH].pte;
323#endif
Russell Kingae8f1542006-09-27 15:38:34 +0100324
325 /*
326 * Enable CPU-specific coherency if supported.
327 * (Only available on XSC3 at the moment.)
328 */
329 if (arch_is_coherent()) {
330 if (cpu_is_xsc3()) {
331 mem_types[MT_MEMORY].prot_sect |= PMD_SECT_S;
Lennert Buytenhek0e5fdca2006-12-02 00:03:47 +0100332 mem_types[MT_MEMORY].prot_pte |= L_PTE_SHARED;
Russell Kingae8f1542006-09-27 15:38:34 +0100333 }
334 }
335
336 /*
337 * ARMv6 and above have extended page tables.
338 */
339 if (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP)) {
340 /*
Russell Kingae8f1542006-09-27 15:38:34 +0100341 * Mark cache clean areas and XIP ROM read only
342 * from SVC mode and no access from userspace.
343 */
344 mem_types[MT_ROM].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
345 mem_types[MT_MINICLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
346 mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
347
348 /*
349 * Mark the device area as "shared device"
350 */
Russell Kingae8f1542006-09-27 15:38:34 +0100351 mem_types[MT_DEVICE].prot_sect |= PMD_SECT_BUFFERED;
352
Russell Kingae8f1542006-09-27 15:38:34 +0100353#ifdef CONFIG_SMP
354 /*
355 * Mark memory with the "shared" attribute for SMP systems
356 */
357 user_pgprot |= L_PTE_SHARED;
358 kern_pgprot |= L_PTE_SHARED;
Russell Kingbb30f362008-09-06 20:04:59 +0100359 vecs_pgprot |= L_PTE_SHARED;
Russell Kingae8f1542006-09-27 15:38:34 +0100360 mem_types[MT_MEMORY].prot_sect |= PMD_SECT_S;
361#endif
362 }
363
364 for (i = 0; i < 16; i++) {
365 unsigned long v = pgprot_val(protection_map[i]);
Russell Kingbb30f362008-09-06 20:04:59 +0100366 protection_map[i] = __pgprot(v | user_pgprot);
Russell Kingae8f1542006-09-27 15:38:34 +0100367 }
368
Russell Kingbb30f362008-09-06 20:04:59 +0100369 mem_types[MT_LOW_VECTORS].prot_pte |= vecs_pgprot;
370 mem_types[MT_HIGH_VECTORS].prot_pte |= vecs_pgprot;
Russell Kingae8f1542006-09-27 15:38:34 +0100371
Russell Kingbb30f362008-09-06 20:04:59 +0100372 if (cpu_arch < CPU_ARCH_ARMv5)
Russell Kingae8f1542006-09-27 15:38:34 +0100373 mem_types[MT_MINICLEAN].prot_sect &= ~PMD_SECT_TEX(1);
Russell Kingae8f1542006-09-27 15:38:34 +0100374
Imre_Deak44b18692007-02-11 13:45:13 +0100375 pgprot_user = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | user_pgprot);
Russell Kingae8f1542006-09-27 15:38:34 +0100376 pgprot_kernel = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG |
377 L_PTE_DIRTY | L_PTE_WRITE |
378 L_PTE_EXEC | kern_pgprot);
379
380 mem_types[MT_LOW_VECTORS].prot_l1 |= ecc_mask;
381 mem_types[MT_HIGH_VECTORS].prot_l1 |= ecc_mask;
382 mem_types[MT_MEMORY].prot_sect |= ecc_mask | cp->pmd;
383 mem_types[MT_ROM].prot_sect |= cp->pmd;
384
385 switch (cp->pmd) {
386 case PMD_SECT_WT:
387 mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WT;
388 break;
389 case PMD_SECT_WB:
390 case PMD_SECT_WBWA:
391 mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WB;
392 break;
393 }
394 printk("Memory policy: ECC %sabled, Data cache %s\n",
395 ecc_mask ? "en" : "dis", cp->policy);
Russell King2497f0a2007-04-21 09:59:44 +0100396
397 for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
398 struct mem_type *t = &mem_types[i];
399 if (t->prot_l1)
400 t->prot_l1 |= PMD_DOMAIN(t->domain);
401 if (t->prot_sect)
402 t->prot_sect |= PMD_DOMAIN(t->domain);
403 }
Russell Kingae8f1542006-09-27 15:38:34 +0100404}
405
406#define vectors_base() (vectors_high() ? 0xffff0000 : 0)
407
Russell King24e6c692007-04-21 10:21:28 +0100408static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
409 unsigned long end, unsigned long pfn,
410 const struct mem_type *type)
Russell Kingae8f1542006-09-27 15:38:34 +0100411{
Russell King24e6c692007-04-21 10:21:28 +0100412 pte_t *pte;
Russell Kingae8f1542006-09-27 15:38:34 +0100413
Russell King24e6c692007-04-21 10:21:28 +0100414 if (pmd_none(*pmd)) {
415 pte = alloc_bootmem_low_pages(2 * PTRS_PER_PTE * sizeof(pte_t));
416 __pmd_populate(pmd, __pa(pte) | type->prot_l1);
417 }
Russell Kingae8f1542006-09-27 15:38:34 +0100418
Russell King24e6c692007-04-21 10:21:28 +0100419 pte = pte_offset_kernel(pmd, addr);
420 do {
Russell King40d192b2008-09-06 21:15:56 +0100421 set_pte_ext(pte, pfn_pte(pfn, __pgprot(type->prot_pte)), 0);
Russell King24e6c692007-04-21 10:21:28 +0100422 pfn++;
423 } while (pte++, addr += PAGE_SIZE, addr != end);
Russell Kingae8f1542006-09-27 15:38:34 +0100424}
425
Russell King24e6c692007-04-21 10:21:28 +0100426static void __init alloc_init_section(pgd_t *pgd, unsigned long addr,
427 unsigned long end, unsigned long phys,
428 const struct mem_type *type)
Russell Kingae8f1542006-09-27 15:38:34 +0100429{
Russell King24e6c692007-04-21 10:21:28 +0100430 pmd_t *pmd = pmd_offset(pgd, addr);
Russell Kingae8f1542006-09-27 15:38:34 +0100431
Russell King24e6c692007-04-21 10:21:28 +0100432 /*
433 * Try a section mapping - end, addr and phys must all be aligned
434 * to a section boundary. Note that PMDs refer to the individual
435 * L1 entries, whereas PGDs refer to a group of L1 entries making
436 * up one logical pointer to an L2 table.
437 */
438 if (((addr | end | phys) & ~SECTION_MASK) == 0) {
439 pmd_t *p = pmd;
Russell Kingae8f1542006-09-27 15:38:34 +0100440
Russell King24e6c692007-04-21 10:21:28 +0100441 if (addr & SECTION_SIZE)
442 pmd++;
443
444 do {
445 *pmd = __pmd(phys | type->prot_sect);
446 phys += SECTION_SIZE;
447 } while (pmd++, addr += SECTION_SIZE, addr != end);
448
449 flush_pmd_entry(p);
450 } else {
451 /*
452 * No need to loop; pte's aren't interested in the
453 * individual L1 entries.
454 */
455 alloc_init_pte(pmd, addr, end, __phys_to_pfn(phys), type);
Russell Kingae8f1542006-09-27 15:38:34 +0100456 }
Russell Kingae8f1542006-09-27 15:38:34 +0100457}
458
Russell King4a56c1e2007-04-21 10:16:48 +0100459static void __init create_36bit_mapping(struct map_desc *md,
460 const struct mem_type *type)
461{
462 unsigned long phys, addr, length, end;
463 pgd_t *pgd;
464
465 addr = md->virtual;
466 phys = (unsigned long)__pfn_to_phys(md->pfn);
467 length = PAGE_ALIGN(md->length);
468
469 if (!(cpu_architecture() >= CPU_ARCH_ARMv6 || cpu_is_xsc3())) {
470 printk(KERN_ERR "MM: CPU does not support supersection "
471 "mapping for 0x%08llx at 0x%08lx\n",
472 __pfn_to_phys((u64)md->pfn), addr);
473 return;
474 }
475
476 /* N.B. ARMv6 supersections are only defined to work with domain 0.
477 * Since domain assignments can in fact be arbitrary, the
478 * 'domain == 0' check below is required to insure that ARMv6
479 * supersections are only allocated for domain 0 regardless
480 * of the actual domain assignments in use.
481 */
482 if (type->domain) {
483 printk(KERN_ERR "MM: invalid domain in supersection "
484 "mapping for 0x%08llx at 0x%08lx\n",
485 __pfn_to_phys((u64)md->pfn), addr);
486 return;
487 }
488
489 if ((addr | length | __pfn_to_phys(md->pfn)) & ~SUPERSECTION_MASK) {
490 printk(KERN_ERR "MM: cannot create mapping for "
491 "0x%08llx at 0x%08lx invalid alignment\n",
492 __pfn_to_phys((u64)md->pfn), addr);
493 return;
494 }
495
496 /*
497 * Shift bits [35:32] of address into bits [23:20] of PMD
498 * (See ARMv6 spec).
499 */
500 phys |= (((md->pfn >> (32 - PAGE_SHIFT)) & 0xF) << 20);
501
502 pgd = pgd_offset_k(addr);
503 end = addr + length;
504 do {
505 pmd_t *pmd = pmd_offset(pgd, addr);
506 int i;
507
508 for (i = 0; i < 16; i++)
509 *pmd++ = __pmd(phys | type->prot_sect | PMD_SECT_SUPER);
510
511 addr += SUPERSECTION_SIZE;
512 phys += SUPERSECTION_SIZE;
513 pgd += SUPERSECTION_SIZE >> PGDIR_SHIFT;
514 } while (addr != end);
515}
516
Russell Kingae8f1542006-09-27 15:38:34 +0100517/*
518 * Create the page directory entries and any necessary
519 * page tables for the mapping specified by `md'. We
520 * are able to cope here with varying sizes and address
521 * offsets, and we take full advantage of sections and
522 * supersections.
523 */
524void __init create_mapping(struct map_desc *md)
525{
Russell King24e6c692007-04-21 10:21:28 +0100526 unsigned long phys, addr, length, end;
Russell Kingd5c98172007-04-21 10:05:32 +0100527 const struct mem_type *type;
Russell King24e6c692007-04-21 10:21:28 +0100528 pgd_t *pgd;
Russell Kingae8f1542006-09-27 15:38:34 +0100529
530 if (md->virtual != vectors_base() && md->virtual < TASK_SIZE) {
531 printk(KERN_WARNING "BUG: not creating mapping for "
532 "0x%08llx at 0x%08lx in user region\n",
533 __pfn_to_phys((u64)md->pfn), md->virtual);
534 return;
535 }
536
537 if ((md->type == MT_DEVICE || md->type == MT_ROM) &&
538 md->virtual >= PAGE_OFFSET && md->virtual < VMALLOC_END) {
539 printk(KERN_WARNING "BUG: mapping for 0x%08llx at 0x%08lx "
540 "overlaps vmalloc space\n",
541 __pfn_to_phys((u64)md->pfn), md->virtual);
542 }
543
Russell Kingd5c98172007-04-21 10:05:32 +0100544 type = &mem_types[md->type];
Russell Kingae8f1542006-09-27 15:38:34 +0100545
546 /*
547 * Catch 36-bit addresses
548 */
Russell King4a56c1e2007-04-21 10:16:48 +0100549 if (md->pfn >= 0x100000) {
550 create_36bit_mapping(md, type);
551 return;
Russell Kingae8f1542006-09-27 15:38:34 +0100552 }
553
Russell King7b9c7b42007-07-04 21:16:33 +0100554 addr = md->virtual & PAGE_MASK;
Russell King24e6c692007-04-21 10:21:28 +0100555 phys = (unsigned long)__pfn_to_phys(md->pfn);
Russell King7b9c7b42007-07-04 21:16:33 +0100556 length = PAGE_ALIGN(md->length + (md->virtual & ~PAGE_MASK));
Russell Kingae8f1542006-09-27 15:38:34 +0100557
Russell King24e6c692007-04-21 10:21:28 +0100558 if (type->prot_l1 == 0 && ((addr | phys | length) & ~SECTION_MASK)) {
Russell Kingae8f1542006-09-27 15:38:34 +0100559 printk(KERN_WARNING "BUG: map for 0x%08lx at 0x%08lx can not "
560 "be mapped using pages, ignoring.\n",
Russell King24e6c692007-04-21 10:21:28 +0100561 __pfn_to_phys(md->pfn), addr);
Russell Kingae8f1542006-09-27 15:38:34 +0100562 return;
563 }
564
Russell King24e6c692007-04-21 10:21:28 +0100565 pgd = pgd_offset_k(addr);
566 end = addr + length;
567 do {
568 unsigned long next = pgd_addr_end(addr, end);
Russell Kingae8f1542006-09-27 15:38:34 +0100569
Russell King24e6c692007-04-21 10:21:28 +0100570 alloc_init_section(pgd, addr, next, phys, type);
Russell Kingae8f1542006-09-27 15:38:34 +0100571
Russell King24e6c692007-04-21 10:21:28 +0100572 phys += next - addr;
573 addr = next;
574 } while (pgd++, addr != end);
Russell Kingae8f1542006-09-27 15:38:34 +0100575}
576
577/*
578 * Create the architecture specific mappings
579 */
580void __init iotable_init(struct map_desc *io_desc, int nr)
581{
582 int i;
583
584 for (i = 0; i < nr; i++)
585 create_mapping(io_desc + i);
586}
587
Lennert Buytenhek60296c72008-08-05 01:56:13 +0200588static int __init check_membank_valid(struct membank *mb)
589{
590 /*
591 * Check whether this memory region has non-zero size.
592 */
593 if (mb->size == 0)
594 return 0;
595
596 /*
597 * Check whether this memory region would entirely overlap
598 * the vmalloc area.
599 */
600 if (phys_to_virt(mb->start) >= VMALLOC_MIN) {
601 printk(KERN_NOTICE "Ignoring RAM at %.8lx-%.8lx "
602 "(vmalloc region overlap).\n",
603 mb->start, mb->start + mb->size - 1);
604 return 0;
605 }
606
607 /*
608 * Check whether this memory region would partially overlap
609 * the vmalloc area.
610 */
611 if (phys_to_virt(mb->start + mb->size) < phys_to_virt(mb->start) ||
612 phys_to_virt(mb->start + mb->size) > VMALLOC_MIN) {
613 unsigned long newsize = VMALLOC_MIN - phys_to_virt(mb->start);
614
615 printk(KERN_NOTICE "Truncating RAM at %.8lx-%.8lx "
616 "to -%.8lx (vmalloc region overlap).\n",
617 mb->start, mb->start + mb->size - 1,
618 mb->start + newsize - 1);
619 mb->size = newsize;
620 }
621
622 return 1;
623}
624
625static void __init sanity_check_meminfo(struct meminfo *mi)
626{
627 int i;
628 int j;
629
630 for (i = 0, j = 0; i < mi->nr_banks; i++) {
631 if (check_membank_valid(&mi->bank[i]))
632 mi->bank[j++] = mi->bank[i];
633 }
634 mi->nr_banks = j;
635}
636
Russell Kingd111e8f2006-09-27 15:27:33 +0100637static inline void prepare_page_table(struct meminfo *mi)
638{
639 unsigned long addr;
640
641 /*
642 * Clear out all the mappings below the kernel image.
643 */
644 for (addr = 0; addr < MODULE_START; addr += PGDIR_SIZE)
645 pmd_clear(pmd_off_k(addr));
646
647#ifdef CONFIG_XIP_KERNEL
648 /* The XIP kernel is mapped in the module area -- skip over it */
649 addr = ((unsigned long)&_etext + PGDIR_SIZE - 1) & PGDIR_MASK;
650#endif
651 for ( ; addr < PAGE_OFFSET; addr += PGDIR_SIZE)
652 pmd_clear(pmd_off_k(addr));
653
654 /*
655 * Clear out all the kernel space mappings, except for the first
656 * memory bank, up to the end of the vmalloc region.
657 */
658 for (addr = __phys_to_virt(mi->bank[0].start + mi->bank[0].size);
659 addr < VMALLOC_END; addr += PGDIR_SIZE)
660 pmd_clear(pmd_off_k(addr));
661}
662
663/*
664 * Reserve the various regions of node 0
665 */
666void __init reserve_node_zero(pg_data_t *pgdat)
667{
668 unsigned long res_size = 0;
669
670 /*
671 * Register the kernel text and data with bootmem.
672 * Note that this can only be in node 0.
673 */
674#ifdef CONFIG_XIP_KERNEL
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800675 reserve_bootmem_node(pgdat, __pa(&__data_start), &_end - &__data_start,
676 BOOTMEM_DEFAULT);
Russell Kingd111e8f2006-09-27 15:27:33 +0100677#else
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800678 reserve_bootmem_node(pgdat, __pa(&_stext), &_end - &_stext,
679 BOOTMEM_DEFAULT);
Russell Kingd111e8f2006-09-27 15:27:33 +0100680#endif
681
682 /*
683 * Reserve the page tables. These are already in use,
684 * and can only be in node 0.
685 */
686 reserve_bootmem_node(pgdat, __pa(swapper_pg_dir),
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800687 PTRS_PER_PGD * sizeof(pgd_t), BOOTMEM_DEFAULT);
Russell Kingd111e8f2006-09-27 15:27:33 +0100688
689 /*
690 * Hmm... This should go elsewhere, but we really really need to
691 * stop things allocating the low memory; ideally we need a better
692 * implementation of GFP_DMA which does not assume that DMA-able
693 * memory starts at zero.
694 */
695 if (machine_is_integrator() || machine_is_cintegrator())
696 res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
697
698 /*
699 * These should likewise go elsewhere. They pre-reserve the
700 * screen memory region at the start of main system memory.
701 */
702 if (machine_is_edb7211())
703 res_size = 0x00020000;
704 if (machine_is_p720t())
705 res_size = 0x00014000;
706
Ben Dooksbbf6f282006-12-07 20:47:58 +0100707 /* H1940 and RX3715 need to reserve this for suspend */
708
709 if (machine_is_h1940() || machine_is_rx3715()) {
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800710 reserve_bootmem_node(pgdat, 0x30003000, 0x1000,
711 BOOTMEM_DEFAULT);
712 reserve_bootmem_node(pgdat, 0x30081000, 0x1000,
713 BOOTMEM_DEFAULT);
Ben Dooks90733412006-12-06 01:50:24 +0100714 }
715
Russell Kingd111e8f2006-09-27 15:27:33 +0100716#ifdef CONFIG_SA1111
717 /*
718 * Because of the SA1111 DMA bug, we want to preserve our
719 * precious DMA-able memory...
720 */
721 res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
722#endif
723 if (res_size)
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800724 reserve_bootmem_node(pgdat, PHYS_OFFSET, res_size,
725 BOOTMEM_DEFAULT);
Russell Kingd111e8f2006-09-27 15:27:33 +0100726}
727
728/*
729 * Set up device the mappings. Since we clear out the page tables for all
730 * mappings above VMALLOC_END, we will remove any debug device mappings.
731 * This means you have to be careful how you debug this function, or any
732 * called function. This means you can't use any function or debugging
733 * method which may touch any device, otherwise the kernel _will_ crash.
734 */
735static void __init devicemaps_init(struct machine_desc *mdesc)
736{
737 struct map_desc map;
738 unsigned long addr;
739 void *vectors;
740
741 /*
742 * Allocate the vector page early.
743 */
744 vectors = alloc_bootmem_low_pages(PAGE_SIZE);
745 BUG_ON(!vectors);
746
747 for (addr = VMALLOC_END; addr; addr += PGDIR_SIZE)
748 pmd_clear(pmd_off_k(addr));
749
750 /*
751 * Map the kernel if it is XIP.
752 * It is always first in the modulearea.
753 */
754#ifdef CONFIG_XIP_KERNEL
755 map.pfn = __phys_to_pfn(CONFIG_XIP_PHYS_ADDR & SECTION_MASK);
756 map.virtual = MODULE_START;
757 map.length = ((unsigned long)&_etext - map.virtual + ~SECTION_MASK) & SECTION_MASK;
758 map.type = MT_ROM;
759 create_mapping(&map);
760#endif
761
762 /*
763 * Map the cache flushing regions.
764 */
765#ifdef FLUSH_BASE
766 map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS);
767 map.virtual = FLUSH_BASE;
768 map.length = SZ_1M;
769 map.type = MT_CACHECLEAN;
770 create_mapping(&map);
771#endif
772#ifdef FLUSH_BASE_MINICACHE
773 map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS + SZ_1M);
774 map.virtual = FLUSH_BASE_MINICACHE;
775 map.length = SZ_1M;
776 map.type = MT_MINICLEAN;
777 create_mapping(&map);
778#endif
779
780 /*
781 * Create a mapping for the machine vectors at the high-vectors
782 * location (0xffff0000). If we aren't using high-vectors, also
783 * create a mapping at the low-vectors virtual address.
784 */
785 map.pfn = __phys_to_pfn(virt_to_phys(vectors));
786 map.virtual = 0xffff0000;
787 map.length = PAGE_SIZE;
788 map.type = MT_HIGH_VECTORS;
789 create_mapping(&map);
790
791 if (!vectors_high()) {
792 map.virtual = 0;
793 map.type = MT_LOW_VECTORS;
794 create_mapping(&map);
795 }
796
797 /*
798 * Ask the machine support to map in the statically mapped devices.
799 */
800 if (mdesc->map_io)
801 mdesc->map_io();
802
803 /*
804 * Finally flush the caches and tlb to ensure that we're in a
805 * consistent state wrt the writebuffer. This also ensures that
806 * any write-allocated cache lines in the vector page are written
807 * back. After this point, we can start to touch devices again.
808 */
809 local_flush_tlb_all();
810 flush_cache_all();
811}
812
813/*
814 * paging_init() sets up the page tables, initialises the zone memory
815 * maps, and sets up the zero page, bad page and bad page tables.
816 */
817void __init paging_init(struct meminfo *mi, struct machine_desc *mdesc)
818{
819 void *zero_page;
820
821 build_mem_type_table();
Lennert Buytenhek60296c72008-08-05 01:56:13 +0200822 sanity_check_meminfo(mi);
Russell Kingd111e8f2006-09-27 15:27:33 +0100823 prepare_page_table(mi);
824 bootmem_init(mi);
825 devicemaps_init(mdesc);
826
827 top_pmd = pmd_off_k(0xffff0000);
828
829 /*
830 * allocate the zero page. Note that we count on this going ok.
831 */
832 zero_page = alloc_bootmem_low_pages(PAGE_SIZE);
833 memzero(zero_page, PAGE_SIZE);
834 empty_zero_page = virt_to_page(zero_page);
835 flush_dcache_page(empty_zero_page);
836}
Russell Kingae8f1542006-09-27 15:38:34 +0100837
838/*
839 * In order to soft-boot, we need to insert a 1:1 mapping in place of
840 * the user-mode pages. This will then ensure that we have predictable
841 * results when turning the mmu off
842 */
843void setup_mm_for_reboot(char mode)
844{
845 unsigned long base_pmdval;
846 pgd_t *pgd;
847 int i;
848
849 if (current->mm && current->mm->pgd)
850 pgd = current->mm->pgd;
851 else
852 pgd = init_mm.pgd;
853
854 base_pmdval = PMD_SECT_AP_WRITE | PMD_SECT_AP_READ | PMD_TYPE_SECT;
855 if (cpu_architecture() <= CPU_ARCH_ARMv5TEJ && !cpu_is_xscale())
856 base_pmdval |= PMD_BIT4;
857
858 for (i = 0; i < FIRST_USER_PGD_NR + USER_PTRS_PER_PGD; i++, pgd++) {
859 unsigned long pmdval = (i << PGDIR_SHIFT) | base_pmdval;
860 pmd_t *pmd;
861
862 pmd = pmd_off(pgd, i << PGDIR_SHIFT);
863 pmd[0] = __pmd(pmdval);
864 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
865 flush_pmd_entry(pmd);
866 }
867}