[ARM] ignore high memory with VIPT aliasing caches
[linux-flexiantxendom0-3.2.10.git] / arch / arm / mm / mmu.c
1 /*
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  */
10 #include <linux/module.h>
11 #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/cputype.h>
19 #include <asm/mach-types.h>
20 #include <asm/sections.h>
21 #include <asm/cachetype.h>
22 #include <asm/setup.h>
23 #include <asm/sizes.h>
24 #include <asm/tlb.h>
25 #include <asm/highmem.h>
26
27 #include <asm/mach/arch.h>
28 #include <asm/mach/map.h>
29
30 #include "mm.h"
31
32 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
33
34 /*
35  * empty_zero_page is a special page that is used for
36  * zero-initialized data and COW.
37  */
38 struct page *empty_zero_page;
39 EXPORT_SYMBOL(empty_zero_page);
40
41 /*
42  * The pmd table for the upper-most set of pages.
43  */
44 pmd_t *top_pmd;
45
46 #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
52 static unsigned int cachepolicy __initdata = CPOLICY_WRITEBACK;
53 static unsigned int ecc_mask __initdata = 0;
54 pgprot_t pgprot_user;
55 pgprot_t pgprot_kernel;
56
57 EXPORT_SYMBOL(pgprot_user);
58 EXPORT_SYMBOL(pgprot_kernel);
59
60 struct cachepolicy {
61         const char      policy[16];
62         unsigned int    cr_mask;
63         unsigned int    pmd;
64         unsigned int    pte;
65 };
66
67 static struct cachepolicy cache_policies[] __initdata = {
68         {
69                 .policy         = "uncached",
70                 .cr_mask        = CR_W|CR_C,
71                 .pmd            = PMD_SECT_UNCACHED,
72                 .pte            = L_PTE_MT_UNCACHED,
73         }, {
74                 .policy         = "buffered",
75                 .cr_mask        = CR_C,
76                 .pmd            = PMD_SECT_BUFFERED,
77                 .pte            = L_PTE_MT_BUFFERABLE,
78         }, {
79                 .policy         = "writethrough",
80                 .cr_mask        = 0,
81                 .pmd            = PMD_SECT_WT,
82                 .pte            = L_PTE_MT_WRITETHROUGH,
83         }, {
84                 .policy         = "writeback",
85                 .cr_mask        = 0,
86                 .pmd            = PMD_SECT_WB,
87                 .pte            = L_PTE_MT_WRITEBACK,
88         }, {
89                 .policy         = "writealloc",
90                 .cr_mask        = 0,
91                 .pmd            = PMD_SECT_WBWA,
92                 .pte            = L_PTE_MT_WRITEALLOC,
93         }
94 };
95
96 /*
97  * These are useful for identifying cache coherency
98  * 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  */
102 static 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");
119         if (cpu_architecture() >= CPU_ARCH_ARMv6) {
120                 printk(KERN_WARNING "Only cachepolicy=writeback supported on ARMv6 and later\n");
121                 cachepolicy = CPOLICY_WRITEBACK;
122         }
123         flush_cache_all();
124         set_cr(cr_alignment);
125 }
126 __early_param("cachepolicy=", early_cachepolicy);
127
128 static 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
136 static 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
144 static 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
156 static 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
165 #ifndef CONFIG_SMP
166 void 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
185 #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_AP_WRITE
187
188 static struct mem_type mem_types[] = {
189         [MT_DEVICE] = {           /* Strongly ordered / ARMv6 shared device */
190                 .prot_pte       = PROT_PTE_DEVICE | L_PTE_MT_DEV_SHARED |
191                                   L_PTE_SHARED,
192                 .prot_l1        = PMD_TYPE_TABLE,
193                 .prot_sect      = PROT_SECT_DEVICE | PMD_SECT_S,
194                 .domain         = DOMAIN_IO,
195         },
196         [MT_DEVICE_NONSHARED] = { /* ARMv6 non-shared device */
197                 .prot_pte       = PROT_PTE_DEVICE | L_PTE_MT_DEV_NONSHARED,
198                 .prot_l1        = PMD_TYPE_TABLE,
199                 .prot_sect      = PROT_SECT_DEVICE,
200                 .domain         = DOMAIN_IO,
201         },
202         [MT_DEVICE_CACHED] = {    /* ioremap_cached */
203                 .prot_pte       = PROT_PTE_DEVICE | L_PTE_MT_DEV_CACHED,
204                 .prot_l1        = PMD_TYPE_TABLE,
205                 .prot_sect      = PROT_SECT_DEVICE | PMD_SECT_WB,
206                 .domain         = DOMAIN_IO,
207         },      
208         [MT_DEVICE_WC] = {      /* ioremap_wc */
209                 .prot_pte       = PROT_PTE_DEVICE | L_PTE_MT_DEV_WC,
210                 .prot_l1        = PMD_TYPE_TABLE,
211                 .prot_sect      = PROT_SECT_DEVICE,
212                 .domain         = DOMAIN_IO,
213         },
214         [MT_UNCACHED] = {
215                 .prot_pte       = PROT_PTE_DEVICE,
216                 .prot_l1        = PMD_TYPE_TABLE,
217                 .prot_sect      = PMD_TYPE_SECT | PMD_SECT_XN,
218                 .domain         = DOMAIN_IO,
219         },
220         [MT_CACHECLEAN] = {
221                 .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN,
222                 .domain    = DOMAIN_KERNEL,
223         },
224         [MT_MINICLEAN] = {
225                 .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN | PMD_SECT_MINICACHE,
226                 .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] = {
241                 .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
242                 .domain    = DOMAIN_KERNEL,
243         },
244         [MT_ROM] = {
245                 .prot_sect = PMD_TYPE_SECT,
246                 .domain    = DOMAIN_KERNEL,
247         },
248 };
249
250 const struct mem_type *get_mem_type(unsigned int type)
251 {
252         return type < ARRAY_SIZE(mem_types) ? &mem_types[type] : NULL;
253 }
254
255 /*
256  * Adjust the PMD section entries according to the CPU in use.
257  */
258 static void __init build_mem_type_table(void)
259 {
260         struct cachepolicy *cp;
261         unsigned int cr = get_cr();
262         unsigned int user_pgprot, kern_pgprot, vecs_pgprot;
263         int cpu_arch = cpu_architecture();
264         int i;
265
266         if (cpu_arch < CPU_ARCH_ARMv6) {
267 #if defined(CONFIG_CPU_DCACHE_DISABLE)
268                 if (cachepolicy > CPOLICY_BUFFERED)
269                         cachepolicy = CPOLICY_BUFFERED;
270 #elif defined(CONFIG_CPU_DCACHE_WRITETHROUGH)
271                 if (cachepolicy > CPOLICY_WRITETHROUGH)
272                         cachepolicy = CPOLICY_WRITETHROUGH;
273 #endif
274         }
275         if (cpu_arch < CPU_ARCH_ARMv5) {
276                 if (cachepolicy >= CPOLICY_WRITEALLOC)
277                         cachepolicy = CPOLICY_WRITEBACK;
278                 ecc_mask = 0;
279         }
280 #ifdef CONFIG_SMP
281         cachepolicy = CPOLICY_WRITEALLOC;
282 #endif
283
284         /*
285          * Strip out features not present on earlier architectures.
286          * Pre-ARMv5 CPUs don't have TEX bits.  Pre-ARMv6 CPUs or those
287          * without extended page tables don't have the 'Shared' bit.
288          */
289         if (cpu_arch < CPU_ARCH_ARMv5)
290                 for (i = 0; i < ARRAY_SIZE(mem_types); i++)
291                         mem_types[i].prot_sect &= ~PMD_SECT_TEX(7);
292         if ((cpu_arch < CPU_ARCH_ARMv6 || !(cr & CR_XP)) && !cpu_is_xsc3())
293                 for (i = 0; i < ARRAY_SIZE(mem_types); i++)
294                         mem_types[i].prot_sect &= ~PMD_SECT_S;
295
296         /*
297          * ARMv5 and lower, bit 4 must be set for page tables (was: cache
298          * "update-able on write" bit on ARM610).  However, Xscale and
299          * Xscale3 require this bit to be cleared.
300          */
301         if (cpu_is_xscale() || cpu_is_xsc3()) {
302                 for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
303                         mem_types[i].prot_sect &= ~PMD_BIT4;
304                         mem_types[i].prot_l1 &= ~PMD_BIT4;
305                 }
306         } else if (cpu_arch < CPU_ARCH_ARMv6) {
307                 for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
308                         if (mem_types[i].prot_l1)
309                                 mem_types[i].prot_l1 |= PMD_BIT4;
310                         if (mem_types[i].prot_sect)
311                                 mem_types[i].prot_sect |= PMD_BIT4;
312                 }
313         }
314
315         /*
316          * Mark the device areas according to the CPU/architecture.
317          */
318         if (cpu_is_xsc3() || (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP))) {
319                 if (!cpu_is_xsc3()) {
320                         /*
321                          * Mark device regions on ARMv6+ as execute-never
322                          * to prevent speculative instruction fetches.
323                          */
324                         mem_types[MT_DEVICE].prot_sect |= PMD_SECT_XN;
325                         mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_XN;
326                         mem_types[MT_DEVICE_CACHED].prot_sect |= PMD_SECT_XN;
327                         mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_XN;
328                 }
329                 if (cpu_arch >= CPU_ARCH_ARMv7 && (cr & CR_TRE)) {
330                         /*
331                          * For ARMv7 with TEX remapping,
332                          * - shared device is SXCB=1100
333                          * - nonshared device is SXCB=0100
334                          * - write combine device mem is SXCB=0001
335                          * (Uncached Normal memory)
336                          */
337                         mem_types[MT_DEVICE].prot_sect |= PMD_SECT_TEX(1);
338                         mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(1);
339                         mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_BUFFERABLE;
340                 } else if (cpu_is_xsc3()) {
341                         /*
342                          * For Xscale3,
343                          * - shared device is TEXCB=00101
344                          * - nonshared device is TEXCB=01000
345                          * - write combine device mem is TEXCB=00100
346                          * (Inner/Outer Uncacheable in xsc3 parlance)
347                          */
348                         mem_types[MT_DEVICE].prot_sect |= PMD_SECT_TEX(1) | PMD_SECT_BUFFERED;
349                         mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(2);
350                         mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_TEX(1);
351                 } else {
352                         /*
353                          * For ARMv6 and ARMv7 without TEX remapping,
354                          * - shared device is TEXCB=00001
355                          * - nonshared device is TEXCB=01000
356                          * - write combine device mem is TEXCB=00100
357                          * (Uncached Normal in ARMv6 parlance).
358                          */
359                         mem_types[MT_DEVICE].prot_sect |= PMD_SECT_BUFFERED;
360                         mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(2);
361                         mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_TEX(1);
362                 }
363         } else {
364                 /*
365                  * On others, write combining is "Uncached/Buffered"
366                  */
367                 mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_BUFFERABLE;
368         }
369
370         /*
371          * Now deal with the memory-type mappings
372          */
373         cp = &cache_policies[cachepolicy];
374         vecs_pgprot = kern_pgprot = user_pgprot = cp->pte;
375
376 #ifndef CONFIG_SMP
377         /*
378          * Only use write-through for non-SMP systems
379          */
380         if (cpu_arch >= CPU_ARCH_ARMv5 && cachepolicy > CPOLICY_WRITETHROUGH)
381                 vecs_pgprot = cache_policies[CPOLICY_WRITETHROUGH].pte;
382 #endif
383
384         /*
385          * Enable CPU-specific coherency if supported.
386          * (Only available on XSC3 at the moment.)
387          */
388         if (arch_is_coherent() && cpu_is_xsc3())
389                 mem_types[MT_MEMORY].prot_sect |= PMD_SECT_S;
390
391         /*
392          * ARMv6 and above have extended page tables.
393          */
394         if (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP)) {
395                 /*
396                  * Mark cache clean areas and XIP ROM read only
397                  * from SVC mode and no access from userspace.
398                  */
399                 mem_types[MT_ROM].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
400                 mem_types[MT_MINICLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
401                 mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
402
403 #ifdef CONFIG_SMP
404                 /*
405                  * Mark memory with the "shared" attribute for SMP systems
406                  */
407                 user_pgprot |= L_PTE_SHARED;
408                 kern_pgprot |= L_PTE_SHARED;
409                 vecs_pgprot |= L_PTE_SHARED;
410                 mem_types[MT_MEMORY].prot_sect |= PMD_SECT_S;
411 #endif
412         }
413
414         for (i = 0; i < 16; i++) {
415                 unsigned long v = pgprot_val(protection_map[i]);
416                 protection_map[i] = __pgprot(v | user_pgprot);
417         }
418
419         mem_types[MT_LOW_VECTORS].prot_pte |= vecs_pgprot;
420         mem_types[MT_HIGH_VECTORS].prot_pte |= vecs_pgprot;
421
422         pgprot_user   = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | user_pgprot);
423         pgprot_kernel = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG |
424                                  L_PTE_DIRTY | L_PTE_WRITE |
425                                  L_PTE_EXEC | kern_pgprot);
426
427         mem_types[MT_LOW_VECTORS].prot_l1 |= ecc_mask;
428         mem_types[MT_HIGH_VECTORS].prot_l1 |= ecc_mask;
429         mem_types[MT_MEMORY].prot_sect |= ecc_mask | cp->pmd;
430         mem_types[MT_ROM].prot_sect |= cp->pmd;
431
432         switch (cp->pmd) {
433         case PMD_SECT_WT:
434                 mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WT;
435                 break;
436         case PMD_SECT_WB:
437         case PMD_SECT_WBWA:
438                 mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WB;
439                 break;
440         }
441         printk("Memory policy: ECC %sabled, Data cache %s\n",
442                 ecc_mask ? "en" : "dis", cp->policy);
443
444         for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
445                 struct mem_type *t = &mem_types[i];
446                 if (t->prot_l1)
447                         t->prot_l1 |= PMD_DOMAIN(t->domain);
448                 if (t->prot_sect)
449                         t->prot_sect |= PMD_DOMAIN(t->domain);
450         }
451 }
452
453 #define vectors_base()  (vectors_high() ? 0xffff0000 : 0)
454
455 static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
456                                   unsigned long end, unsigned long pfn,
457                                   const struct mem_type *type)
458 {
459         pte_t *pte;
460
461         if (pmd_none(*pmd)) {
462                 pte = alloc_bootmem_low_pages(2 * PTRS_PER_PTE * sizeof(pte_t));
463                 __pmd_populate(pmd, __pa(pte) | type->prot_l1);
464         }
465
466         pte = pte_offset_kernel(pmd, addr);
467         do {
468                 set_pte_ext(pte, pfn_pte(pfn, __pgprot(type->prot_pte)), 0);
469                 pfn++;
470         } while (pte++, addr += PAGE_SIZE, addr != end);
471 }
472
473 static void __init alloc_init_section(pgd_t *pgd, unsigned long addr,
474                                       unsigned long end, unsigned long phys,
475                                       const struct mem_type *type)
476 {
477         pmd_t *pmd = pmd_offset(pgd, addr);
478
479         /*
480          * Try a section mapping - end, addr and phys must all be aligned
481          * to a section boundary.  Note that PMDs refer to the individual
482          * L1 entries, whereas PGDs refer to a group of L1 entries making
483          * up one logical pointer to an L2 table.
484          */
485         if (((addr | end | phys) & ~SECTION_MASK) == 0) {
486                 pmd_t *p = pmd;
487
488                 if (addr & SECTION_SIZE)
489                         pmd++;
490
491                 do {
492                         *pmd = __pmd(phys | type->prot_sect);
493                         phys += SECTION_SIZE;
494                 } while (pmd++, addr += SECTION_SIZE, addr != end);
495
496                 flush_pmd_entry(p);
497         } else {
498                 /*
499                  * No need to loop; pte's aren't interested in the
500                  * individual L1 entries.
501                  */
502                 alloc_init_pte(pmd, addr, end, __phys_to_pfn(phys), type);
503         }
504 }
505
506 static void __init create_36bit_mapping(struct map_desc *md,
507                                         const struct mem_type *type)
508 {
509         unsigned long phys, addr, length, end;
510         pgd_t *pgd;
511
512         addr = md->virtual;
513         phys = (unsigned long)__pfn_to_phys(md->pfn);
514         length = PAGE_ALIGN(md->length);
515
516         if (!(cpu_architecture() >= CPU_ARCH_ARMv6 || cpu_is_xsc3())) {
517                 printk(KERN_ERR "MM: CPU does not support supersection "
518                        "mapping for 0x%08llx at 0x%08lx\n",
519                        __pfn_to_phys((u64)md->pfn), addr);
520                 return;
521         }
522
523         /* N.B. ARMv6 supersections are only defined to work with domain 0.
524          *      Since domain assignments can in fact be arbitrary, the
525          *      'domain == 0' check below is required to insure that ARMv6
526          *      supersections are only allocated for domain 0 regardless
527          *      of the actual domain assignments in use.
528          */
529         if (type->domain) {
530                 printk(KERN_ERR "MM: invalid domain in supersection "
531                        "mapping for 0x%08llx at 0x%08lx\n",
532                        __pfn_to_phys((u64)md->pfn), addr);
533                 return;
534         }
535
536         if ((addr | length | __pfn_to_phys(md->pfn)) & ~SUPERSECTION_MASK) {
537                 printk(KERN_ERR "MM: cannot create mapping for "
538                        "0x%08llx at 0x%08lx invalid alignment\n",
539                        __pfn_to_phys((u64)md->pfn), addr);
540                 return;
541         }
542
543         /*
544          * Shift bits [35:32] of address into bits [23:20] of PMD
545          * (See ARMv6 spec).
546          */
547         phys |= (((md->pfn >> (32 - PAGE_SHIFT)) & 0xF) << 20);
548
549         pgd = pgd_offset_k(addr);
550         end = addr + length;
551         do {
552                 pmd_t *pmd = pmd_offset(pgd, addr);
553                 int i;
554
555                 for (i = 0; i < 16; i++)
556                         *pmd++ = __pmd(phys | type->prot_sect | PMD_SECT_SUPER);
557
558                 addr += SUPERSECTION_SIZE;
559                 phys += SUPERSECTION_SIZE;
560                 pgd += SUPERSECTION_SIZE >> PGDIR_SHIFT;
561         } while (addr != end);
562 }
563
564 /*
565  * Create the page directory entries and any necessary
566  * page tables for the mapping specified by `md'.  We
567  * are able to cope here with varying sizes and address
568  * offsets, and we take full advantage of sections and
569  * supersections.
570  */
571 void __init create_mapping(struct map_desc *md)
572 {
573         unsigned long phys, addr, length, end;
574         const struct mem_type *type;
575         pgd_t *pgd;
576
577         if (md->virtual != vectors_base() && md->virtual < TASK_SIZE) {
578                 printk(KERN_WARNING "BUG: not creating mapping for "
579                        "0x%08llx at 0x%08lx in user region\n",
580                        __pfn_to_phys((u64)md->pfn), md->virtual);
581                 return;
582         }
583
584         if ((md->type == MT_DEVICE || md->type == MT_ROM) &&
585             md->virtual >= PAGE_OFFSET && md->virtual < VMALLOC_END) {
586                 printk(KERN_WARNING "BUG: mapping for 0x%08llx at 0x%08lx "
587                        "overlaps vmalloc space\n",
588                        __pfn_to_phys((u64)md->pfn), md->virtual);
589         }
590
591         type = &mem_types[md->type];
592
593         /*
594          * Catch 36-bit addresses
595          */
596         if (md->pfn >= 0x100000) {
597                 create_36bit_mapping(md, type);
598                 return;
599         }
600
601         addr = md->virtual & PAGE_MASK;
602         phys = (unsigned long)__pfn_to_phys(md->pfn);
603         length = PAGE_ALIGN(md->length + (md->virtual & ~PAGE_MASK));
604
605         if (type->prot_l1 == 0 && ((addr | phys | length) & ~SECTION_MASK)) {
606                 printk(KERN_WARNING "BUG: map for 0x%08lx at 0x%08lx can not "
607                        "be mapped using pages, ignoring.\n",
608                        __pfn_to_phys(md->pfn), addr);
609                 return;
610         }
611
612         pgd = pgd_offset_k(addr);
613         end = addr + length;
614         do {
615                 unsigned long next = pgd_addr_end(addr, end);
616
617                 alloc_init_section(pgd, addr, next, phys, type);
618
619                 phys += next - addr;
620                 addr = next;
621         } while (pgd++, addr != end);
622 }
623
624 /*
625  * Create the architecture specific mappings
626  */
627 void __init iotable_init(struct map_desc *io_desc, int nr)
628 {
629         int i;
630
631         for (i = 0; i < nr; i++)
632                 create_mapping(io_desc + i);
633 }
634
635 static unsigned long __initdata vmalloc_reserve = SZ_128M;
636
637 /*
638  * vmalloc=size forces the vmalloc area to be exactly 'size'
639  * bytes. This can be used to increase (or decrease) the vmalloc
640  * area - the default is 128m.
641  */
642 static void __init early_vmalloc(char **arg)
643 {
644         vmalloc_reserve = memparse(*arg, arg);
645
646         if (vmalloc_reserve < SZ_16M) {
647                 vmalloc_reserve = SZ_16M;
648                 printk(KERN_WARNING
649                         "vmalloc area too small, limiting to %luMB\n",
650                         vmalloc_reserve >> 20);
651         }
652
653         if (vmalloc_reserve > VMALLOC_END - (PAGE_OFFSET + SZ_32M)) {
654                 vmalloc_reserve = VMALLOC_END - (PAGE_OFFSET + SZ_32M);
655                 printk(KERN_WARNING
656                         "vmalloc area is too big, limiting to %luMB\n",
657                         vmalloc_reserve >> 20);
658         }
659 }
660 __early_param("vmalloc=", early_vmalloc);
661
662 #define VMALLOC_MIN     (void *)(VMALLOC_END - vmalloc_reserve)
663
664 static void __init sanity_check_meminfo(void)
665 {
666         int i, j;
667
668         for (i = 0, j = 0; i < meminfo.nr_banks; i++) {
669                 struct membank *bank = &meminfo.bank[j];
670                 *bank = meminfo.bank[i];
671
672 #ifdef CONFIG_HIGHMEM
673                 /*
674                  * Split those memory banks which are partially overlapping
675                  * the vmalloc area greatly simplifying things later.
676                  */
677                 if (__va(bank->start) < VMALLOC_MIN &&
678                     bank->size > VMALLOC_MIN - __va(bank->start)) {
679                         if (meminfo.nr_banks >= NR_BANKS) {
680                                 printk(KERN_CRIT "NR_BANKS too low, "
681                                                  "ignoring high memory\n");
682                         } else if (cache_is_vipt_aliasing()) {
683                                 printk(KERN_CRIT "HIGHMEM is not yet supported "
684                                                  "with VIPT aliasing cache, "
685                                                  "ignoring high memory\n");
686                         } else {
687                                 memmove(bank + 1, bank,
688                                         (meminfo.nr_banks - i) * sizeof(*bank));
689                                 meminfo.nr_banks++;
690                                 i++;
691                                 bank[1].size -= VMALLOC_MIN - __va(bank->start);
692                                 bank[1].start = __pa(VMALLOC_MIN - 1) + 1;
693                                 j++;
694                         }
695                         bank->size = VMALLOC_MIN - __va(bank->start);
696                 }
697 #else
698                 /*
699                  * Check whether this memory bank would entirely overlap
700                  * the vmalloc area.
701                  */
702                 if (__va(bank->start) >= VMALLOC_MIN ||
703                     __va(bank->start) < PAGE_OFFSET) {
704                         printk(KERN_NOTICE "Ignoring RAM at %.8lx-%.8lx "
705                                "(vmalloc region overlap).\n",
706                                bank->start, bank->start + bank->size - 1);
707                         continue;
708                 }
709
710                 /*
711                  * Check whether this memory bank would partially overlap
712                  * the vmalloc area.
713                  */
714                 if (__va(bank->start + bank->size) > VMALLOC_MIN ||
715                     __va(bank->start + bank->size) < __va(bank->start)) {
716                         unsigned long newsize = VMALLOC_MIN - __va(bank->start);
717                         printk(KERN_NOTICE "Truncating RAM at %.8lx-%.8lx "
718                                "to -%.8lx (vmalloc region overlap).\n",
719                                bank->start, bank->start + bank->size - 1,
720                                bank->start + newsize - 1);
721                         bank->size = newsize;
722                 }
723 #endif
724                 j++;
725         }
726         meminfo.nr_banks = j;
727 }
728
729 static inline void prepare_page_table(void)
730 {
731         unsigned long addr;
732
733         /*
734          * Clear out all the mappings below the kernel image.
735          */
736         for (addr = 0; addr < MODULES_VADDR; addr += PGDIR_SIZE)
737                 pmd_clear(pmd_off_k(addr));
738
739 #ifdef CONFIG_XIP_KERNEL
740         /* The XIP kernel is mapped in the module area -- skip over it */
741         addr = ((unsigned long)_etext + PGDIR_SIZE - 1) & PGDIR_MASK;
742 #endif
743         for ( ; addr < PAGE_OFFSET; addr += PGDIR_SIZE)
744                 pmd_clear(pmd_off_k(addr));
745
746         /*
747          * Clear out all the kernel space mappings, except for the first
748          * memory bank, up to the end of the vmalloc region.
749          */
750         for (addr = __phys_to_virt(bank_phys_end(&meminfo.bank[0]));
751              addr < VMALLOC_END; addr += PGDIR_SIZE)
752                 pmd_clear(pmd_off_k(addr));
753 }
754
755 /*
756  * Reserve the various regions of node 0
757  */
758 void __init reserve_node_zero(pg_data_t *pgdat)
759 {
760         unsigned long res_size = 0;
761
762         /*
763          * Register the kernel text and data with bootmem.
764          * Note that this can only be in node 0.
765          */
766 #ifdef CONFIG_XIP_KERNEL
767         reserve_bootmem_node(pgdat, __pa(_data), _end - _data,
768                         BOOTMEM_DEFAULT);
769 #else
770         reserve_bootmem_node(pgdat, __pa(_stext), _end - _stext,
771                         BOOTMEM_DEFAULT);
772 #endif
773
774         /*
775          * Reserve the page tables.  These are already in use,
776          * and can only be in node 0.
777          */
778         reserve_bootmem_node(pgdat, __pa(swapper_pg_dir),
779                              PTRS_PER_PGD * sizeof(pgd_t), BOOTMEM_DEFAULT);
780
781         /*
782          * Hmm... This should go elsewhere, but we really really need to
783          * stop things allocating the low memory; ideally we need a better
784          * implementation of GFP_DMA which does not assume that DMA-able
785          * memory starts at zero.
786          */
787         if (machine_is_integrator() || machine_is_cintegrator())
788                 res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
789
790         /*
791          * These should likewise go elsewhere.  They pre-reserve the
792          * screen memory region at the start of main system memory.
793          */
794         if (machine_is_edb7211())
795                 res_size = 0x00020000;
796         if (machine_is_p720t())
797                 res_size = 0x00014000;
798
799         /* H1940 and RX3715 need to reserve this for suspend */
800
801         if (machine_is_h1940() || machine_is_rx3715()) {
802                 reserve_bootmem_node(pgdat, 0x30003000, 0x1000,
803                                 BOOTMEM_DEFAULT);
804                 reserve_bootmem_node(pgdat, 0x30081000, 0x1000,
805                                 BOOTMEM_DEFAULT);
806         }
807
808 #ifdef CONFIG_SA1111
809         /*
810          * Because of the SA1111 DMA bug, we want to preserve our
811          * precious DMA-able memory...
812          */
813         res_size = __pa(swapper_pg_dir) - PHYS_OFFSET;
814 #endif
815         if (res_size)
816                 reserve_bootmem_node(pgdat, PHYS_OFFSET, res_size,
817                                 BOOTMEM_DEFAULT);
818 }
819
820 /*
821  * Set up device the mappings.  Since we clear out the page tables for all
822  * mappings above VMALLOC_END, we will remove any debug device mappings.
823  * This means you have to be careful how you debug this function, or any
824  * called function.  This means you can't use any function or debugging
825  * method which may touch any device, otherwise the kernel _will_ crash.
826  */
827 static void __init devicemaps_init(struct machine_desc *mdesc)
828 {
829         struct map_desc map;
830         unsigned long addr;
831         void *vectors;
832
833         /*
834          * Allocate the vector page early.
835          */
836         vectors = alloc_bootmem_low_pages(PAGE_SIZE);
837
838         for (addr = VMALLOC_END; addr; addr += PGDIR_SIZE)
839                 pmd_clear(pmd_off_k(addr));
840
841         /*
842          * Map the kernel if it is XIP.
843          * It is always first in the modulearea.
844          */
845 #ifdef CONFIG_XIP_KERNEL
846         map.pfn = __phys_to_pfn(CONFIG_XIP_PHYS_ADDR & SECTION_MASK);
847         map.virtual = MODULES_VADDR;
848         map.length = ((unsigned long)_etext - map.virtual + ~SECTION_MASK) & SECTION_MASK;
849         map.type = MT_ROM;
850         create_mapping(&map);
851 #endif
852
853         /*
854          * Map the cache flushing regions.
855          */
856 #ifdef FLUSH_BASE
857         map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS);
858         map.virtual = FLUSH_BASE;
859         map.length = SZ_1M;
860         map.type = MT_CACHECLEAN;
861         create_mapping(&map);
862 #endif
863 #ifdef FLUSH_BASE_MINICACHE
864         map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS + SZ_1M);
865         map.virtual = FLUSH_BASE_MINICACHE;
866         map.length = SZ_1M;
867         map.type = MT_MINICLEAN;
868         create_mapping(&map);
869 #endif
870
871         /*
872          * Create a mapping for the machine vectors at the high-vectors
873          * location (0xffff0000).  If we aren't using high-vectors, also
874          * create a mapping at the low-vectors virtual address.
875          */
876         map.pfn = __phys_to_pfn(virt_to_phys(vectors));
877         map.virtual = 0xffff0000;
878         map.length = PAGE_SIZE;
879         map.type = MT_HIGH_VECTORS;
880         create_mapping(&map);
881
882         if (!vectors_high()) {
883                 map.virtual = 0;
884                 map.type = MT_LOW_VECTORS;
885                 create_mapping(&map);
886         }
887
888         /*
889          * Ask the machine support to map in the statically mapped devices.
890          */
891         if (mdesc->map_io)
892                 mdesc->map_io();
893
894         /*
895          * Finally flush the caches and tlb to ensure that we're in a
896          * consistent state wrt the writebuffer.  This also ensures that
897          * any write-allocated cache lines in the vector page are written
898          * back.  After this point, we can start to touch devices again.
899          */
900         local_flush_tlb_all();
901         flush_cache_all();
902 }
903
904 static void __init kmap_init(void)
905 {
906 #ifdef CONFIG_HIGHMEM
907         pmd_t *pmd = pmd_off_k(PKMAP_BASE);
908         pte_t *pte = alloc_bootmem_low_pages(2 * PTRS_PER_PTE * sizeof(pte_t));
909         BUG_ON(!pmd_none(*pmd) || !pte);
910         __pmd_populate(pmd, __pa(pte) | _PAGE_KERNEL_TABLE);
911         pkmap_page_table = pte + PTRS_PER_PTE;
912 #endif
913 }
914
915 /*
916  * paging_init() sets up the page tables, initialises the zone memory
917  * maps, and sets up the zero page, bad page and bad page tables.
918  */
919 void __init paging_init(struct machine_desc *mdesc)
920 {
921         void *zero_page;
922
923         build_mem_type_table();
924         sanity_check_meminfo();
925         prepare_page_table();
926         bootmem_init();
927         devicemaps_init(mdesc);
928         kmap_init();
929
930         top_pmd = pmd_off_k(0xffff0000);
931
932         /*
933          * allocate the zero page.  Note that this always succeeds and
934          * returns a zeroed result.
935          */
936         zero_page = alloc_bootmem_low_pages(PAGE_SIZE);
937         empty_zero_page = virt_to_page(zero_page);
938         flush_dcache_page(empty_zero_page);
939 }
940
941 /*
942  * In order to soft-boot, we need to insert a 1:1 mapping in place of
943  * the user-mode pages.  This will then ensure that we have predictable
944  * results when turning the mmu off
945  */
946 void setup_mm_for_reboot(char mode)
947 {
948         unsigned long base_pmdval;
949         pgd_t *pgd;
950         int i;
951
952         if (current->mm && current->mm->pgd)
953                 pgd = current->mm->pgd;
954         else
955                 pgd = init_mm.pgd;
956
957         base_pmdval = PMD_SECT_AP_WRITE | PMD_SECT_AP_READ | PMD_TYPE_SECT;
958         if (cpu_architecture() <= CPU_ARCH_ARMv5TEJ && !cpu_is_xscale())
959                 base_pmdval |= PMD_BIT4;
960
961         for (i = 0; i < FIRST_USER_PGD_NR + USER_PTRS_PER_PGD; i++, pgd++) {
962                 unsigned long pmdval = (i << PGDIR_SHIFT) | base_pmdval;
963                 pmd_t *pmd;
964
965                 pmd = pmd_off(pgd, i << PGDIR_SHIFT);
966                 pmd[0] = __pmd(pmdval);
967                 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
968                 flush_pmd_entry(pmd);
969         }
970 }