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