blob: c2bbe3f311f0b91cfe881b639f274fde9d50a471 [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
Russell King0ba8b9b22008-08-10 18:08:10 +010018#include <asm/cputype.h>
Russell Kingd111e8f2006-09-27 15:27:33 +010019#include <asm/mach-types.h>
20#include <asm/setup.h>
21#include <asm/sizes.h>
22#include <asm/tlb.h>
23
24#include <asm/mach/arch.h>
25#include <asm/mach/map.h>
26
27#include "mm.h"
28
29DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
30
Russell King6ae5a6e2006-09-30 10:50:05 +010031extern void _stext, _etext, __data_start, _end;
Russell Kingd111e8f2006-09-27 15:27:33 +010032extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
33
34/*
35 * empty_zero_page is a special page that is used for
36 * zero-initialized data and COW.
37 */
38struct page *empty_zero_page;
Aneesh Kumar K.V3653f3a2008-04-29 08:11:12 -040039EXPORT_SYMBOL(empty_zero_page);
Russell Kingd111e8f2006-09-27 15:27:33 +010040
41/*
42 * The pmd table for the upper-most set of pages.
43 */
44pmd_t *top_pmd;
45
Russell Kingae8f1542006-09-27 15:38:34 +010046#define CPOLICY_UNCACHED 0
47#define CPOLICY_BUFFERED 1
48#define CPOLICY_WRITETHROUGH 2
49#define CPOLICY_WRITEBACK 3
50#define CPOLICY_WRITEALLOC 4
51
52static unsigned int cachepolicy __initdata = CPOLICY_WRITEBACK;
53static unsigned int ecc_mask __initdata = 0;
Imre_Deak44b18692007-02-11 13:45:13 +010054pgprot_t pgprot_user;
Russell Kingae8f1542006-09-27 15:38:34 +010055pgprot_t pgprot_kernel;
56
Imre_Deak44b18692007-02-11 13:45:13 +010057EXPORT_SYMBOL(pgprot_user);
Russell Kingae8f1542006-09-27 15:38:34 +010058EXPORT_SYMBOL(pgprot_kernel);
59
60struct cachepolicy {
61 const char policy[16];
62 unsigned int cr_mask;
63 unsigned int pmd;
64 unsigned int pte;
65};
66
67static struct cachepolicy cache_policies[] __initdata = {
68 {
69 .policy = "uncached",
70 .cr_mask = CR_W|CR_C,
71 .pmd = PMD_SECT_UNCACHED,
72 .pte = 0,
73 }, {
74 .policy = "buffered",
75 .cr_mask = CR_C,
76 .pmd = PMD_SECT_BUFFERED,
77 .pte = PTE_BUFFERABLE,
78 }, {
79 .policy = "writethrough",
80 .cr_mask = 0,
81 .pmd = PMD_SECT_WT,
82 .pte = PTE_CACHEABLE,
83 }, {
84 .policy = "writeback",
85 .cr_mask = 0,
86 .pmd = PMD_SECT_WB,
87 .pte = PTE_BUFFERABLE|PTE_CACHEABLE,
88 }, {
89 .policy = "writealloc",
90 .cr_mask = 0,
91 .pmd = PMD_SECT_WBWA,
92 .pte = PTE_BUFFERABLE|PTE_CACHEABLE,
93 }
94};
95
96/*
Simon Arlott6cbdc8c2007-05-11 20:40:30 +010097 * These are useful for identifying cache coherency
Russell Kingae8f1542006-09-27 15:38:34 +010098 * problems by allowing the cache or the cache and
99 * writebuffer to be turned off. (Note: the write
100 * buffer should not be on and the cache off).
101 */
102static void __init early_cachepolicy(char **p)
103{
104 int i;
105
106 for (i = 0; i < ARRAY_SIZE(cache_policies); i++) {
107 int len = strlen(cache_policies[i].policy);
108
109 if (memcmp(*p, cache_policies[i].policy, len) == 0) {
110 cachepolicy = i;
111 cr_alignment &= ~cache_policies[i].cr_mask;
112 cr_no_alignment &= ~cache_policies[i].cr_mask;
113 *p += len;
114 break;
115 }
116 }
117 if (i == ARRAY_SIZE(cache_policies))
118 printk(KERN_ERR "ERROR: unknown or unsupported cache policy\n");
Catalin Marinas11179d82007-07-20 11:42:24 +0100119 if (cpu_architecture() >= CPU_ARCH_ARMv6) {
120 printk(KERN_WARNING "Only cachepolicy=writeback supported on ARMv6 and later\n");
121 cachepolicy = CPOLICY_WRITEBACK;
122 }
Russell Kingae8f1542006-09-27 15:38:34 +0100123 flush_cache_all();
124 set_cr(cr_alignment);
125}
126__early_param("cachepolicy=", early_cachepolicy);
127
128static void __init early_nocache(char **__unused)
129{
130 char *p = "buffered";
131 printk(KERN_WARNING "nocache is deprecated; use cachepolicy=%s\n", p);
132 early_cachepolicy(&p);
133}
134__early_param("nocache", early_nocache);
135
136static void __init early_nowrite(char **__unused)
137{
138 char *p = "uncached";
139 printk(KERN_WARNING "nowb is deprecated; use cachepolicy=%s\n", p);
140 early_cachepolicy(&p);
141}
142__early_param("nowb", early_nowrite);
143
144static void __init early_ecc(char **p)
145{
146 if (memcmp(*p, "on", 2) == 0) {
147 ecc_mask = PMD_PROTECTION;
148 *p += 2;
149 } else if (memcmp(*p, "off", 3) == 0) {
150 ecc_mask = 0;
151 *p += 3;
152 }
153}
154__early_param("ecc=", early_ecc);
155
156static int __init noalign_setup(char *__unused)
157{
158 cr_alignment &= ~CR_A;
159 cr_no_alignment &= ~CR_A;
160 set_cr(cr_alignment);
161 return 1;
162}
163__setup("noalign", noalign_setup);
164
Russell King255d1f82006-12-18 00:12:47 +0000165#ifndef CONFIG_SMP
166void adjust_cr(unsigned long mask, unsigned long set)
167{
168 unsigned long flags;
169
170 mask &= ~CR_A;
171
172 set &= mask;
173
174 local_irq_save(flags);
175
176 cr_no_alignment = (cr_no_alignment & ~mask) | set;
177 cr_alignment = (cr_alignment & ~mask) | set;
178
179 set_cr((get_cr() & ~mask) | set);
180
181 local_irq_restore(flags);
182}
183#endif
184
Russell King0af92be2007-05-05 20:28:16 +0100185#define PROT_PTE_DEVICE L_PTE_PRESENT|L_PTE_YOUNG|L_PTE_DIRTY|L_PTE_WRITE
186#define PROT_SECT_DEVICE PMD_TYPE_SECT|PMD_SECT_XN|PMD_SECT_AP_WRITE
187
Russell Kingb29e9f52007-04-21 10:47:29 +0100188static struct mem_type mem_types[] = {
Russell King0af92be2007-05-05 20:28:16 +0100189 [MT_DEVICE] = { /* Strongly ordered / ARMv6 shared device */
190 .prot_pte = PROT_PTE_DEVICE,
191 .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 */
196 .prot_pte = PROT_PTE_DEVICE,
197 .prot_pte_ext = PTE_EXT_TEX(2),
198 .prot_l1 = PMD_TYPE_TABLE,
199 .prot_sect = PROT_SECT_DEVICE | PMD_SECT_TEX(2),
200 .domain = DOMAIN_IO,
201 },
202 [MT_DEVICE_CACHED] = { /* ioremap_cached */
203 .prot_pte = PROT_PTE_DEVICE | L_PTE_CACHEABLE | L_PTE_BUFFERABLE,
204 .prot_l1 = PMD_TYPE_TABLE,
205 .prot_sect = PROT_SECT_DEVICE | PMD_SECT_WB,
206 .domain = DOMAIN_IO,
207 },
208 [MT_DEVICE_IXP2000] = { /* IXP2400 requires XCB=101 for on-chip I/O */
209 .prot_pte = PROT_PTE_DEVICE,
210 .prot_l1 = PMD_TYPE_TABLE,
211 .prot_sect = PROT_SECT_DEVICE | PMD_SECT_BUFFERABLE |
212 PMD_SECT_TEX(1),
213 .domain = DOMAIN_IO,
Russell Kingae8f1542006-09-27 15:38:34 +0100214 },
215 [MT_CACHECLEAN] = {
Russell King9ef79632007-05-05 20:03:35 +0100216 .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN,
Russell Kingae8f1542006-09-27 15:38:34 +0100217 .domain = DOMAIN_KERNEL,
218 },
219 [MT_MINICLEAN] = {
Russell King9ef79632007-05-05 20:03:35 +0100220 .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN | PMD_SECT_MINICACHE,
Russell Kingae8f1542006-09-27 15:38:34 +0100221 .domain = DOMAIN_KERNEL,
222 },
223 [MT_LOW_VECTORS] = {
224 .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
225 L_PTE_EXEC,
226 .prot_l1 = PMD_TYPE_TABLE,
227 .domain = DOMAIN_USER,
228 },
229 [MT_HIGH_VECTORS] = {
230 .prot_pte = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
231 L_PTE_USER | L_PTE_EXEC,
232 .prot_l1 = PMD_TYPE_TABLE,
233 .domain = DOMAIN_USER,
234 },
235 [MT_MEMORY] = {
Russell King9ef79632007-05-05 20:03:35 +0100236 .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
Russell Kingae8f1542006-09-27 15:38:34 +0100237 .domain = DOMAIN_KERNEL,
238 },
239 [MT_ROM] = {
Russell King9ef79632007-05-05 20:03:35 +0100240 .prot_sect = PMD_TYPE_SECT,
Russell Kingae8f1542006-09-27 15:38:34 +0100241 .domain = DOMAIN_KERNEL,
242 },
Russell Kingae8f1542006-09-27 15:38:34 +0100243};
244
Russell Kingb29e9f52007-04-21 10:47:29 +0100245const struct mem_type *get_mem_type(unsigned int type)
246{
247 return type < ARRAY_SIZE(mem_types) ? &mem_types[type] : NULL;
248}
249
Russell Kingae8f1542006-09-27 15:38:34 +0100250/*
251 * Adjust the PMD section entries according to the CPU in use.
252 */
253static void __init build_mem_type_table(void)
254{
255 struct cachepolicy *cp;
256 unsigned int cr = get_cr();
257 unsigned int user_pgprot, kern_pgprot;
258 int cpu_arch = cpu_architecture();
259 int i;
260
Catalin Marinas11179d82007-07-20 11:42:24 +0100261 if (cpu_arch < CPU_ARCH_ARMv6) {
Russell Kingae8f1542006-09-27 15:38:34 +0100262#if defined(CONFIG_CPU_DCACHE_DISABLE)
Catalin Marinas11179d82007-07-20 11:42:24 +0100263 if (cachepolicy > CPOLICY_BUFFERED)
264 cachepolicy = CPOLICY_BUFFERED;
Russell Kingae8f1542006-09-27 15:38:34 +0100265#elif defined(CONFIG_CPU_DCACHE_WRITETHROUGH)
Catalin Marinas11179d82007-07-20 11:42:24 +0100266 if (cachepolicy > CPOLICY_WRITETHROUGH)
267 cachepolicy = CPOLICY_WRITETHROUGH;
Russell Kingae8f1542006-09-27 15:38:34 +0100268#endif
Catalin Marinas11179d82007-07-20 11:42:24 +0100269 }
Russell Kingae8f1542006-09-27 15:38:34 +0100270 if (cpu_arch < CPU_ARCH_ARMv5) {
271 if (cachepolicy >= CPOLICY_WRITEALLOC)
272 cachepolicy = CPOLICY_WRITEBACK;
273 ecc_mask = 0;
274 }
275
276 /*
Russell King9ef79632007-05-05 20:03:35 +0100277 * ARMv5 and lower, bit 4 must be set for page tables.
278 * (was: cache "update-able on write" bit on ARM610)
279 * However, Xscale cores require this bit to be cleared.
Russell Kingae8f1542006-09-27 15:38:34 +0100280 */
Russell King9ef79632007-05-05 20:03:35 +0100281 if (cpu_is_xscale()) {
282 for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
Russell Kingae8f1542006-09-27 15:38:34 +0100283 mem_types[i].prot_sect &= ~PMD_BIT4;
Russell King9ef79632007-05-05 20:03:35 +0100284 mem_types[i].prot_l1 &= ~PMD_BIT4;
285 }
286 } else if (cpu_arch < CPU_ARCH_ARMv6) {
287 for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
Russell Kingae8f1542006-09-27 15:38:34 +0100288 if (mem_types[i].prot_l1)
289 mem_types[i].prot_l1 |= PMD_BIT4;
Russell King9ef79632007-05-05 20:03:35 +0100290 if (mem_types[i].prot_sect)
291 mem_types[i].prot_sect |= PMD_BIT4;
292 }
293 }
Russell Kingae8f1542006-09-27 15:38:34 +0100294
295 cp = &cache_policies[cachepolicy];
296 kern_pgprot = user_pgprot = cp->pte;
297
298 /*
299 * Enable CPU-specific coherency if supported.
300 * (Only available on XSC3 at the moment.)
301 */
302 if (arch_is_coherent()) {
303 if (cpu_is_xsc3()) {
304 mem_types[MT_MEMORY].prot_sect |= PMD_SECT_S;
Lennert Buytenhek0e5fdca72006-12-02 00:03:47 +0100305 mem_types[MT_MEMORY].prot_pte |= L_PTE_SHARED;
Russell Kingae8f1542006-09-27 15:38:34 +0100306 }
307 }
308
309 /*
310 * ARMv6 and above have extended page tables.
311 */
312 if (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP)) {
313 /*
Russell Kingae8f1542006-09-27 15:38:34 +0100314 * Mark cache clean areas and XIP ROM read only
315 * from SVC mode and no access from userspace.
316 */
317 mem_types[MT_ROM].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
318 mem_types[MT_MINICLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
319 mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
320
321 /*
322 * Mark the device area as "shared device"
323 */
324 mem_types[MT_DEVICE].prot_pte |= L_PTE_BUFFERABLE;
325 mem_types[MT_DEVICE].prot_sect |= PMD_SECT_BUFFERED;
326
Russell Kingae8f1542006-09-27 15:38:34 +0100327#ifdef CONFIG_SMP
328 /*
329 * Mark memory with the "shared" attribute for SMP systems
330 */
331 user_pgprot |= L_PTE_SHARED;
332 kern_pgprot |= L_PTE_SHARED;
333 mem_types[MT_MEMORY].prot_sect |= PMD_SECT_S;
334#endif
335 }
336
337 for (i = 0; i < 16; i++) {
338 unsigned long v = pgprot_val(protection_map[i]);
339 v = (v & ~(L_PTE_BUFFERABLE|L_PTE_CACHEABLE)) | user_pgprot;
340 protection_map[i] = __pgprot(v);
341 }
342
343 mem_types[MT_LOW_VECTORS].prot_pte |= kern_pgprot;
344 mem_types[MT_HIGH_VECTORS].prot_pte |= kern_pgprot;
345
346 if (cpu_arch >= CPU_ARCH_ARMv5) {
347#ifndef CONFIG_SMP
348 /*
349 * Only use write-through for non-SMP systems
350 */
351 mem_types[MT_LOW_VECTORS].prot_pte &= ~L_PTE_BUFFERABLE;
352 mem_types[MT_HIGH_VECTORS].prot_pte &= ~L_PTE_BUFFERABLE;
353#endif
354 } else {
355 mem_types[MT_MINICLEAN].prot_sect &= ~PMD_SECT_TEX(1);
356 }
357
Imre_Deak44b18692007-02-11 13:45:13 +0100358 pgprot_user = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | user_pgprot);
Russell Kingae8f1542006-09-27 15:38:34 +0100359 pgprot_kernel = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG |
360 L_PTE_DIRTY | L_PTE_WRITE |
361 L_PTE_EXEC | kern_pgprot);
362
363 mem_types[MT_LOW_VECTORS].prot_l1 |= ecc_mask;
364 mem_types[MT_HIGH_VECTORS].prot_l1 |= ecc_mask;
365 mem_types[MT_MEMORY].prot_sect |= ecc_mask | cp->pmd;
366 mem_types[MT_ROM].prot_sect |= cp->pmd;
367
368 switch (cp->pmd) {
369 case PMD_SECT_WT:
370 mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WT;
371 break;
372 case PMD_SECT_WB:
373 case PMD_SECT_WBWA:
374 mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WB;
375 break;
376 }
377 printk("Memory policy: ECC %sabled, Data cache %s\n",
378 ecc_mask ? "en" : "dis", cp->policy);
Russell King2497f0a2007-04-21 09:59:44 +0100379
380 for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
381 struct mem_type *t = &mem_types[i];
382 if (t->prot_l1)
383 t->prot_l1 |= PMD_DOMAIN(t->domain);
384 if (t->prot_sect)
385 t->prot_sect |= PMD_DOMAIN(t->domain);
386 }
Russell Kingae8f1542006-09-27 15:38:34 +0100387}
388
389#define vectors_base() (vectors_high() ? 0xffff0000 : 0)
390
Russell King24e6c692007-04-21 10:21:28 +0100391static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
392 unsigned long end, unsigned long pfn,
393 const struct mem_type *type)
Russell Kingae8f1542006-09-27 15:38:34 +0100394{
Russell King24e6c692007-04-21 10:21:28 +0100395 pte_t *pte;
Russell Kingae8f1542006-09-27 15:38:34 +0100396
Russell King24e6c692007-04-21 10:21:28 +0100397 if (pmd_none(*pmd)) {
398 pte = alloc_bootmem_low_pages(2 * PTRS_PER_PTE * sizeof(pte_t));
399 __pmd_populate(pmd, __pa(pte) | type->prot_l1);
400 }
Russell Kingae8f1542006-09-27 15:38:34 +0100401
Russell King24e6c692007-04-21 10:21:28 +0100402 pte = pte_offset_kernel(pmd, addr);
403 do {
Russell Kingc172cc92007-04-21 10:52:32 +0100404 set_pte_ext(pte, pfn_pte(pfn, __pgprot(type->prot_pte)),
405 type->prot_pte_ext);
Russell King24e6c692007-04-21 10:21:28 +0100406 pfn++;
407 } while (pte++, addr += PAGE_SIZE, addr != end);
Russell Kingae8f1542006-09-27 15:38:34 +0100408}
409
Russell King24e6c692007-04-21 10:21:28 +0100410static void __init alloc_init_section(pgd_t *pgd, unsigned long addr,
411 unsigned long end, unsigned long phys,
412 const struct mem_type *type)
Russell Kingae8f1542006-09-27 15:38:34 +0100413{
Russell King24e6c692007-04-21 10:21:28 +0100414 pmd_t *pmd = pmd_offset(pgd, addr);
Russell Kingae8f1542006-09-27 15:38:34 +0100415
Russell King24e6c692007-04-21 10:21:28 +0100416 /*
417 * Try a section mapping - end, addr and phys must all be aligned
418 * to a section boundary. Note that PMDs refer to the individual
419 * L1 entries, whereas PGDs refer to a group of L1 entries making
420 * up one logical pointer to an L2 table.
421 */
422 if (((addr | end | phys) & ~SECTION_MASK) == 0) {
423 pmd_t *p = pmd;
Russell Kingae8f1542006-09-27 15:38:34 +0100424
Russell King24e6c692007-04-21 10:21:28 +0100425 if (addr & SECTION_SIZE)
426 pmd++;
427
428 do {
429 *pmd = __pmd(phys | type->prot_sect);
430 phys += SECTION_SIZE;
431 } while (pmd++, addr += SECTION_SIZE, addr != end);
432
433 flush_pmd_entry(p);
434 } else {
435 /*
436 * No need to loop; pte's aren't interested in the
437 * individual L1 entries.
438 */
439 alloc_init_pte(pmd, addr, end, __phys_to_pfn(phys), type);
Russell Kingae8f1542006-09-27 15:38:34 +0100440 }
Russell Kingae8f1542006-09-27 15:38:34 +0100441}
442
Russell King4a56c1e2007-04-21 10:16:48 +0100443static void __init create_36bit_mapping(struct map_desc *md,
444 const struct mem_type *type)
445{
446 unsigned long phys, addr, length, end;
447 pgd_t *pgd;
448
449 addr = md->virtual;
450 phys = (unsigned long)__pfn_to_phys(md->pfn);
451 length = PAGE_ALIGN(md->length);
452
453 if (!(cpu_architecture() >= CPU_ARCH_ARMv6 || cpu_is_xsc3())) {
454 printk(KERN_ERR "MM: CPU does not support supersection "
455 "mapping for 0x%08llx at 0x%08lx\n",
456 __pfn_to_phys((u64)md->pfn), addr);
457 return;
458 }
459
460 /* N.B. ARMv6 supersections are only defined to work with domain 0.
461 * Since domain assignments can in fact be arbitrary, the
462 * 'domain == 0' check below is required to insure that ARMv6
463 * supersections are only allocated for domain 0 regardless
464 * of the actual domain assignments in use.
465 */
466 if (type->domain) {
467 printk(KERN_ERR "MM: invalid domain in supersection "
468 "mapping for 0x%08llx at 0x%08lx\n",
469 __pfn_to_phys((u64)md->pfn), addr);
470 return;
471 }
472
473 if ((addr | length | __pfn_to_phys(md->pfn)) & ~SUPERSECTION_MASK) {
474 printk(KERN_ERR "MM: cannot create mapping for "
475 "0x%08llx at 0x%08lx invalid alignment\n",
476 __pfn_to_phys((u64)md->pfn), addr);
477 return;
478 }
479
480 /*
481 * Shift bits [35:32] of address into bits [23:20] of PMD
482 * (See ARMv6 spec).
483 */
484 phys |= (((md->pfn >> (32 - PAGE_SHIFT)) & 0xF) << 20);
485
486 pgd = pgd_offset_k(addr);
487 end = addr + length;
488 do {
489 pmd_t *pmd = pmd_offset(pgd, addr);
490 int i;
491
492 for (i = 0; i < 16; i++)
493 *pmd++ = __pmd(phys | type->prot_sect | PMD_SECT_SUPER);
494
495 addr += SUPERSECTION_SIZE;
496 phys += SUPERSECTION_SIZE;
497 pgd += SUPERSECTION_SIZE >> PGDIR_SHIFT;
498 } while (addr != end);
499}
500
Russell Kingae8f1542006-09-27 15:38:34 +0100501/*
502 * Create the page directory entries and any necessary
503 * page tables for the mapping specified by `md'. We
504 * are able to cope here with varying sizes and address
505 * offsets, and we take full advantage of sections and
506 * supersections.
507 */
508void __init create_mapping(struct map_desc *md)
509{
Russell King24e6c692007-04-21 10:21:28 +0100510 unsigned long phys, addr, length, end;
Russell Kingd5c98172007-04-21 10:05:32 +0100511 const struct mem_type *type;
Russell King24e6c692007-04-21 10:21:28 +0100512 pgd_t *pgd;
Russell Kingae8f1542006-09-27 15:38:34 +0100513
514 if (md->virtual != vectors_base() && md->virtual < TASK_SIZE) {
515 printk(KERN_WARNING "BUG: not creating mapping for "
516 "0x%08llx at 0x%08lx in user region\n",
517 __pfn_to_phys((u64)md->pfn), md->virtual);
518 return;
519 }
520
521 if ((md->type == MT_DEVICE || md->type == MT_ROM) &&
522 md->virtual >= PAGE_OFFSET && md->virtual < VMALLOC_END) {
523 printk(KERN_WARNING "BUG: mapping for 0x%08llx at 0x%08lx "
524 "overlaps vmalloc space\n",
525 __pfn_to_phys((u64)md->pfn), md->virtual);
526 }
527
Russell Kingd5c98172007-04-21 10:05:32 +0100528 type = &mem_types[md->type];
Russell Kingae8f1542006-09-27 15:38:34 +0100529
530 /*
531 * Catch 36-bit addresses
532 */
Russell King4a56c1e2007-04-21 10:16:48 +0100533 if (md->pfn >= 0x100000) {
534 create_36bit_mapping(md, type);
535 return;
Russell Kingae8f1542006-09-27 15:38:34 +0100536 }
537
Russell King7b9c7b42007-07-04 21:16:33 +0100538 addr = md->virtual & PAGE_MASK;
Russell King24e6c692007-04-21 10:21:28 +0100539 phys = (unsigned long)__pfn_to_phys(md->pfn);
Russell King7b9c7b42007-07-04 21:16:33 +0100540 length = PAGE_ALIGN(md->length + (md->virtual & ~PAGE_MASK));
Russell Kingae8f1542006-09-27 15:38:34 +0100541
Russell King24e6c692007-04-21 10:21:28 +0100542 if (type->prot_l1 == 0 && ((addr | phys | length) & ~SECTION_MASK)) {
Russell Kingae8f1542006-09-27 15:38:34 +0100543 printk(KERN_WARNING "BUG: map for 0x%08lx at 0x%08lx can not "
544 "be mapped using pages, ignoring.\n",
Russell King24e6c692007-04-21 10:21:28 +0100545 __pfn_to_phys(md->pfn), addr);
Russell Kingae8f1542006-09-27 15:38:34 +0100546 return;
547 }
548
Russell King24e6c692007-04-21 10:21:28 +0100549 pgd = pgd_offset_k(addr);
550 end = addr + length;
551 do {
552 unsigned long next = pgd_addr_end(addr, end);
Russell Kingae8f1542006-09-27 15:38:34 +0100553
Russell King24e6c692007-04-21 10:21:28 +0100554 alloc_init_section(pgd, addr, next, phys, type);
Russell Kingae8f1542006-09-27 15:38:34 +0100555
Russell King24e6c692007-04-21 10:21:28 +0100556 phys += next - addr;
557 addr = next;
558 } while (pgd++, addr != end);
Russell Kingae8f1542006-09-27 15:38:34 +0100559}
560
561/*
562 * Create the architecture specific mappings
563 */
564void __init iotable_init(struct map_desc *io_desc, int nr)
565{
566 int i;
567
568 for (i = 0; i < nr; i++)
569 create_mapping(io_desc + i);
570}
571
Lennert Buytenhek60296c72008-08-05 01:56:13 +0200572static int __init check_membank_valid(struct membank *mb)
573{
574 /*
575 * Check whether this memory region has non-zero size.
576 */
577 if (mb->size == 0)
578 return 0;
579
580 /*
581 * Check whether this memory region would entirely overlap
582 * the vmalloc area.
583 */
584 if (phys_to_virt(mb->start) >= VMALLOC_MIN) {
585 printk(KERN_NOTICE "Ignoring RAM at %.8lx-%.8lx "
586 "(vmalloc region overlap).\n",
587 mb->start, mb->start + mb->size - 1);
588 return 0;
589 }
590
591 /*
592 * Check whether this memory region would partially overlap
593 * the vmalloc area.
594 */
595 if (phys_to_virt(mb->start + mb->size) < phys_to_virt(mb->start) ||
596 phys_to_virt(mb->start + mb->size) > VMALLOC_MIN) {
597 unsigned long newsize = VMALLOC_MIN - phys_to_virt(mb->start);
598
599 printk(KERN_NOTICE "Truncating RAM at %.8lx-%.8lx "
600 "to -%.8lx (vmalloc region overlap).\n",
601 mb->start, mb->start + mb->size - 1,
602 mb->start + newsize - 1);
603 mb->size = newsize;
604 }
605
606 return 1;
607}
608
609static void __init sanity_check_meminfo(struct meminfo *mi)
610{
611 int i;
612 int j;
613
614 for (i = 0, j = 0; i < mi->nr_banks; i++) {
615 if (check_membank_valid(&mi->bank[i]))
616 mi->bank[j++] = mi->bank[i];
617 }
618 mi->nr_banks = j;
619}
620
Russell Kingd111e8f2006-09-27 15:27:33 +0100621static inline void prepare_page_table(struct meminfo *mi)
622{
623 unsigned long addr;
624
625 /*
626 * Clear out all the mappings below the kernel image.
627 */
628 for (addr = 0; addr < MODULE_START; addr += PGDIR_SIZE)
629 pmd_clear(pmd_off_k(addr));
630
631#ifdef CONFIG_XIP_KERNEL
632 /* The XIP kernel is mapped in the module area -- skip over it */
633 addr = ((unsigned long)&_etext + PGDIR_SIZE - 1) & PGDIR_MASK;
634#endif
635 for ( ; addr < PAGE_OFFSET; addr += PGDIR_SIZE)
636 pmd_clear(pmd_off_k(addr));
637
638 /*
639 * Clear out all the kernel space mappings, except for the first
640 * memory bank, up to the end of the vmalloc region.
641 */
642 for (addr = __phys_to_virt(mi->bank[0].start + mi->bank[0].size);
643 addr < VMALLOC_END; addr += PGDIR_SIZE)
644 pmd_clear(pmd_off_k(addr));
645}
646
647/*
648 * Reserve the various regions of node 0
649 */
650void __init reserve_node_zero(pg_data_t *pgdat)
651{
652 unsigned long res_size = 0;
653
654 /*
655 * Register the kernel text and data with bootmem.
656 * Note that this can only be in node 0.
657 */
658#ifdef CONFIG_XIP_KERNEL
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800659 reserve_bootmem_node(pgdat, __pa(&__data_start), &_end - &__data_start,
660 BOOTMEM_DEFAULT);
Russell Kingd111e8f2006-09-27 15:27:33 +0100661#else
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800662 reserve_bootmem_node(pgdat, __pa(&_stext), &_end - &_stext,
663 BOOTMEM_DEFAULT);
Russell Kingd111e8f2006-09-27 15:27:33 +0100664#endif
665
666 /*
667 * Reserve the page tables. These are already in use,
668 * and can only be in node 0.
669 */
670 reserve_bootmem_node(pgdat, __pa(swapper_pg_dir),
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800671 PTRS_PER_PGD * sizeof(pgd_t), BOOTMEM_DEFAULT);
Russell Kingd111e8f2006-09-27 15:27:33 +0100672
673 /*
674 * Hmm... This should go elsewhere, but we really really need to
675 * stop things allocating the low memory; ideally we need a better
676 * implementation of GFP_DMA which does not assume that DMA-able
677 * memory starts at zero.
678 */
679 if (machine_is_integrator() || machine_is_cintegrator())
680 res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
681
682 /*
683 * These should likewise go elsewhere. They pre-reserve the
684 * screen memory region at the start of main system memory.
685 */
686 if (machine_is_edb7211())
687 res_size = 0x00020000;
688 if (machine_is_p720t())
689 res_size = 0x00014000;
690
Ben Dooksbbf6f282006-12-07 20:47:58 +0100691 /* H1940 and RX3715 need to reserve this for suspend */
692
693 if (machine_is_h1940() || machine_is_rx3715()) {
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800694 reserve_bootmem_node(pgdat, 0x30003000, 0x1000,
695 BOOTMEM_DEFAULT);
696 reserve_bootmem_node(pgdat, 0x30081000, 0x1000,
697 BOOTMEM_DEFAULT);
Ben Dooks90733412006-12-06 01:50:24 +0100698 }
699
Russell Kingd111e8f2006-09-27 15:27:33 +0100700#ifdef CONFIG_SA1111
701 /*
702 * Because of the SA1111 DMA bug, we want to preserve our
703 * precious DMA-able memory...
704 */
705 res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
706#endif
707 if (res_size)
Bernhard Walle72a7fe32008-02-07 00:15:17 -0800708 reserve_bootmem_node(pgdat, PHYS_OFFSET, res_size,
709 BOOTMEM_DEFAULT);
Russell Kingd111e8f2006-09-27 15:27:33 +0100710}
711
712/*
713 * Set up device the mappings. Since we clear out the page tables for all
714 * mappings above VMALLOC_END, we will remove any debug device mappings.
715 * This means you have to be careful how you debug this function, or any
716 * called function. This means you can't use any function or debugging
717 * method which may touch any device, otherwise the kernel _will_ crash.
718 */
719static void __init devicemaps_init(struct machine_desc *mdesc)
720{
721 struct map_desc map;
722 unsigned long addr;
723 void *vectors;
724
725 /*
726 * Allocate the vector page early.
727 */
728 vectors = alloc_bootmem_low_pages(PAGE_SIZE);
729 BUG_ON(!vectors);
730
731 for (addr = VMALLOC_END; addr; addr += PGDIR_SIZE)
732 pmd_clear(pmd_off_k(addr));
733
734 /*
735 * Map the kernel if it is XIP.
736 * It is always first in the modulearea.
737 */
738#ifdef CONFIG_XIP_KERNEL
739 map.pfn = __phys_to_pfn(CONFIG_XIP_PHYS_ADDR & SECTION_MASK);
740 map.virtual = MODULE_START;
741 map.length = ((unsigned long)&_etext - map.virtual + ~SECTION_MASK) & SECTION_MASK;
742 map.type = MT_ROM;
743 create_mapping(&map);
744#endif
745
746 /*
747 * Map the cache flushing regions.
748 */
749#ifdef FLUSH_BASE
750 map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS);
751 map.virtual = FLUSH_BASE;
752 map.length = SZ_1M;
753 map.type = MT_CACHECLEAN;
754 create_mapping(&map);
755#endif
756#ifdef FLUSH_BASE_MINICACHE
757 map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS + SZ_1M);
758 map.virtual = FLUSH_BASE_MINICACHE;
759 map.length = SZ_1M;
760 map.type = MT_MINICLEAN;
761 create_mapping(&map);
762#endif
763
764 /*
765 * Create a mapping for the machine vectors at the high-vectors
766 * location (0xffff0000). If we aren't using high-vectors, also
767 * create a mapping at the low-vectors virtual address.
768 */
769 map.pfn = __phys_to_pfn(virt_to_phys(vectors));
770 map.virtual = 0xffff0000;
771 map.length = PAGE_SIZE;
772 map.type = MT_HIGH_VECTORS;
773 create_mapping(&map);
774
775 if (!vectors_high()) {
776 map.virtual = 0;
777 map.type = MT_LOW_VECTORS;
778 create_mapping(&map);
779 }
780
781 /*
782 * Ask the machine support to map in the statically mapped devices.
783 */
784 if (mdesc->map_io)
785 mdesc->map_io();
786
787 /*
788 * Finally flush the caches and tlb to ensure that we're in a
789 * consistent state wrt the writebuffer. This also ensures that
790 * any write-allocated cache lines in the vector page are written
791 * back. After this point, we can start to touch devices again.
792 */
793 local_flush_tlb_all();
794 flush_cache_all();
795}
796
797/*
798 * paging_init() sets up the page tables, initialises the zone memory
799 * maps, and sets up the zero page, bad page and bad page tables.
800 */
801void __init paging_init(struct meminfo *mi, struct machine_desc *mdesc)
802{
803 void *zero_page;
804
805 build_mem_type_table();
Lennert Buytenhek60296c72008-08-05 01:56:13 +0200806 sanity_check_meminfo(mi);
Russell Kingd111e8f2006-09-27 15:27:33 +0100807 prepare_page_table(mi);
808 bootmem_init(mi);
809 devicemaps_init(mdesc);
810
811 top_pmd = pmd_off_k(0xffff0000);
812
813 /*
814 * allocate the zero page. Note that we count on this going ok.
815 */
816 zero_page = alloc_bootmem_low_pages(PAGE_SIZE);
817 memzero(zero_page, PAGE_SIZE);
818 empty_zero_page = virt_to_page(zero_page);
819 flush_dcache_page(empty_zero_page);
820}
Russell Kingae8f1542006-09-27 15:38:34 +0100821
822/*
823 * In order to soft-boot, we need to insert a 1:1 mapping in place of
824 * the user-mode pages. This will then ensure that we have predictable
825 * results when turning the mmu off
826 */
827void setup_mm_for_reboot(char mode)
828{
829 unsigned long base_pmdval;
830 pgd_t *pgd;
831 int i;
832
833 if (current->mm && current->mm->pgd)
834 pgd = current->mm->pgd;
835 else
836 pgd = init_mm.pgd;
837
838 base_pmdval = PMD_SECT_AP_WRITE | PMD_SECT_AP_READ | PMD_TYPE_SECT;
839 if (cpu_architecture() <= CPU_ARCH_ARMv5TEJ && !cpu_is_xscale())
840 base_pmdval |= PMD_BIT4;
841
842 for (i = 0; i < FIRST_USER_PGD_NR + USER_PTRS_PER_PGD; i++, pgd++) {
843 unsigned long pmdval = (i << PGDIR_SHIFT) | base_pmdval;
844 pmd_t *pmd;
845
846 pmd = pmd_off(pgd, i << PGDIR_SHIFT);
847 pmd[0] = __pmd(pmdval);
848 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
849 flush_pmd_entry(pmd);
850 }
851}