Merge branches 'fixes', 'misc', 'pm' and 'sa1100' into for-next
[firefly-linux-kernel-4.4.55.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/mman.h>
15 #include <linux/nodemask.h>
16 #include <linux/memblock.h>
17 #include <linux/fs.h>
18 #include <linux/vmalloc.h>
19 #include <linux/sizes.h>
20
21 #include <asm/cp15.h>
22 #include <asm/cputype.h>
23 #include <asm/sections.h>
24 #include <asm/cachetype.h>
25 #include <asm/sections.h>
26 #include <asm/setup.h>
27 #include <asm/smp_plat.h>
28 #include <asm/tlb.h>
29 #include <asm/highmem.h>
30 #include <asm/system_info.h>
31 #include <asm/traps.h>
32 #include <asm/procinfo.h>
33 #include <asm/memory.h>
34
35 #include <asm/mach/arch.h>
36 #include <asm/mach/map.h>
37 #include <asm/mach/pci.h>
38 #include <asm/fixmap.h>
39
40 #include "mm.h"
41 #include "tcm.h"
42
43 /*
44  * empty_zero_page is a special page that is used for
45  * zero-initialized data and COW.
46  */
47 struct page *empty_zero_page;
48 EXPORT_SYMBOL(empty_zero_page);
49
50 /*
51  * The pmd table for the upper-most set of pages.
52  */
53 pmd_t *top_pmd;
54
55 pmdval_t user_pmd_table = _PAGE_USER_TABLE;
56
57 #define CPOLICY_UNCACHED        0
58 #define CPOLICY_BUFFERED        1
59 #define CPOLICY_WRITETHROUGH    2
60 #define CPOLICY_WRITEBACK       3
61 #define CPOLICY_WRITEALLOC      4
62
63 static unsigned int cachepolicy __initdata = CPOLICY_WRITEBACK;
64 static unsigned int ecc_mask __initdata = 0;
65 pgprot_t pgprot_user;
66 pgprot_t pgprot_kernel;
67 pgprot_t pgprot_hyp_device;
68 pgprot_t pgprot_s2;
69 pgprot_t pgprot_s2_device;
70
71 EXPORT_SYMBOL(pgprot_user);
72 EXPORT_SYMBOL(pgprot_kernel);
73
74 struct cachepolicy {
75         const char      policy[16];
76         unsigned int    cr_mask;
77         pmdval_t        pmd;
78         pteval_t        pte;
79         pteval_t        pte_s2;
80 };
81
82 #ifdef CONFIG_ARM_LPAE
83 #define s2_policy(policy)       policy
84 #else
85 #define s2_policy(policy)       0
86 #endif
87
88 static struct cachepolicy cache_policies[] __initdata = {
89         {
90                 .policy         = "uncached",
91                 .cr_mask        = CR_W|CR_C,
92                 .pmd            = PMD_SECT_UNCACHED,
93                 .pte            = L_PTE_MT_UNCACHED,
94                 .pte_s2         = s2_policy(L_PTE_S2_MT_UNCACHED),
95         }, {
96                 .policy         = "buffered",
97                 .cr_mask        = CR_C,
98                 .pmd            = PMD_SECT_BUFFERED,
99                 .pte            = L_PTE_MT_BUFFERABLE,
100                 .pte_s2         = s2_policy(L_PTE_S2_MT_UNCACHED),
101         }, {
102                 .policy         = "writethrough",
103                 .cr_mask        = 0,
104                 .pmd            = PMD_SECT_WT,
105                 .pte            = L_PTE_MT_WRITETHROUGH,
106                 .pte_s2         = s2_policy(L_PTE_S2_MT_WRITETHROUGH),
107         }, {
108                 .policy         = "writeback",
109                 .cr_mask        = 0,
110                 .pmd            = PMD_SECT_WB,
111                 .pte            = L_PTE_MT_WRITEBACK,
112                 .pte_s2         = s2_policy(L_PTE_S2_MT_WRITEBACK),
113         }, {
114                 .policy         = "writealloc",
115                 .cr_mask        = 0,
116                 .pmd            = PMD_SECT_WBWA,
117                 .pte            = L_PTE_MT_WRITEALLOC,
118                 .pte_s2         = s2_policy(L_PTE_S2_MT_WRITEBACK),
119         }
120 };
121
122 #ifdef CONFIG_CPU_CP15
123 static unsigned long initial_pmd_value __initdata = 0;
124
125 /*
126  * Initialise the cache_policy variable with the initial state specified
127  * via the "pmd" value.  This is used to ensure that on ARMv6 and later,
128  * the C code sets the page tables up with the same policy as the head
129  * assembly code, which avoids an illegal state where the TLBs can get
130  * confused.  See comments in early_cachepolicy() for more information.
131  */
132 void __init init_default_cache_policy(unsigned long pmd)
133 {
134         int i;
135
136         initial_pmd_value = pmd;
137
138         pmd &= PMD_SECT_TEX(1) | PMD_SECT_BUFFERABLE | PMD_SECT_CACHEABLE;
139
140         for (i = 0; i < ARRAY_SIZE(cache_policies); i++)
141                 if (cache_policies[i].pmd == pmd) {
142                         cachepolicy = i;
143                         break;
144                 }
145
146         if (i == ARRAY_SIZE(cache_policies))
147                 pr_err("ERROR: could not find cache policy\n");
148 }
149
150 /*
151  * These are useful for identifying cache coherency problems by allowing
152  * the cache or the cache and writebuffer to be turned off.  (Note: the
153  * write buffer should not be on and the cache off).
154  */
155 static int __init early_cachepolicy(char *p)
156 {
157         int i, selected = -1;
158
159         for (i = 0; i < ARRAY_SIZE(cache_policies); i++) {
160                 int len = strlen(cache_policies[i].policy);
161
162                 if (memcmp(p, cache_policies[i].policy, len) == 0) {
163                         selected = i;
164                         break;
165                 }
166         }
167
168         if (selected == -1)
169                 pr_err("ERROR: unknown or unsupported cache policy\n");
170
171         /*
172          * This restriction is partly to do with the way we boot; it is
173          * unpredictable to have memory mapped using two different sets of
174          * memory attributes (shared, type, and cache attribs).  We can not
175          * change these attributes once the initial assembly has setup the
176          * page tables.
177          */
178         if (cpu_architecture() >= CPU_ARCH_ARMv6 && selected != cachepolicy) {
179                 pr_warn("Only cachepolicy=%s supported on ARMv6 and later\n",
180                         cache_policies[cachepolicy].policy);
181                 return 0;
182         }
183
184         if (selected != cachepolicy) {
185                 unsigned long cr = __clear_cr(cache_policies[selected].cr_mask);
186                 cachepolicy = selected;
187                 flush_cache_all();
188                 set_cr(cr);
189         }
190         return 0;
191 }
192 early_param("cachepolicy", early_cachepolicy);
193
194 static int __init early_nocache(char *__unused)
195 {
196         char *p = "buffered";
197         pr_warn("nocache is deprecated; use cachepolicy=%s\n", p);
198         early_cachepolicy(p);
199         return 0;
200 }
201 early_param("nocache", early_nocache);
202
203 static int __init early_nowrite(char *__unused)
204 {
205         char *p = "uncached";
206         pr_warn("nowb is deprecated; use cachepolicy=%s\n", p);
207         early_cachepolicy(p);
208         return 0;
209 }
210 early_param("nowb", early_nowrite);
211
212 #ifndef CONFIG_ARM_LPAE
213 static int __init early_ecc(char *p)
214 {
215         if (memcmp(p, "on", 2) == 0)
216                 ecc_mask = PMD_PROTECTION;
217         else if (memcmp(p, "off", 3) == 0)
218                 ecc_mask = 0;
219         return 0;
220 }
221 early_param("ecc", early_ecc);
222 #endif
223
224 #else /* ifdef CONFIG_CPU_CP15 */
225
226 static int __init early_cachepolicy(char *p)
227 {
228         pr_warn("cachepolicy kernel parameter not supported without cp15\n");
229 }
230 early_param("cachepolicy", early_cachepolicy);
231
232 static int __init noalign_setup(char *__unused)
233 {
234         pr_warn("noalign kernel parameter not supported without cp15\n");
235 }
236 __setup("noalign", noalign_setup);
237
238 #endif /* ifdef CONFIG_CPU_CP15 / else */
239
240 #define PROT_PTE_DEVICE         L_PTE_PRESENT|L_PTE_YOUNG|L_PTE_DIRTY|L_PTE_XN
241 #define PROT_PTE_S2_DEVICE      PROT_PTE_DEVICE
242 #define PROT_SECT_DEVICE        PMD_TYPE_SECT|PMD_SECT_AP_WRITE
243
244 static struct mem_type mem_types[] = {
245         [MT_DEVICE] = {           /* Strongly ordered / ARMv6 shared device */
246                 .prot_pte       = PROT_PTE_DEVICE | L_PTE_MT_DEV_SHARED |
247                                   L_PTE_SHARED,
248                 .prot_pte_s2    = s2_policy(PROT_PTE_S2_DEVICE) |
249                                   s2_policy(L_PTE_S2_MT_DEV_SHARED) |
250                                   L_PTE_SHARED,
251                 .prot_l1        = PMD_TYPE_TABLE,
252                 .prot_sect      = PROT_SECT_DEVICE | PMD_SECT_S,
253                 .domain         = DOMAIN_IO,
254         },
255         [MT_DEVICE_NONSHARED] = { /* ARMv6 non-shared device */
256                 .prot_pte       = PROT_PTE_DEVICE | L_PTE_MT_DEV_NONSHARED,
257                 .prot_l1        = PMD_TYPE_TABLE,
258                 .prot_sect      = PROT_SECT_DEVICE,
259                 .domain         = DOMAIN_IO,
260         },
261         [MT_DEVICE_CACHED] = {    /* ioremap_cached */
262                 .prot_pte       = PROT_PTE_DEVICE | L_PTE_MT_DEV_CACHED,
263                 .prot_l1        = PMD_TYPE_TABLE,
264                 .prot_sect      = PROT_SECT_DEVICE | PMD_SECT_WB,
265                 .domain         = DOMAIN_IO,
266         },
267         [MT_DEVICE_WC] = {      /* ioremap_wc */
268                 .prot_pte       = PROT_PTE_DEVICE | L_PTE_MT_DEV_WC,
269                 .prot_l1        = PMD_TYPE_TABLE,
270                 .prot_sect      = PROT_SECT_DEVICE,
271                 .domain         = DOMAIN_IO,
272         },
273         [MT_UNCACHED] = {
274                 .prot_pte       = PROT_PTE_DEVICE,
275                 .prot_l1        = PMD_TYPE_TABLE,
276                 .prot_sect      = PMD_TYPE_SECT | PMD_SECT_XN,
277                 .domain         = DOMAIN_IO,
278         },
279         [MT_CACHECLEAN] = {
280                 .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN,
281                 .domain    = DOMAIN_KERNEL,
282         },
283 #ifndef CONFIG_ARM_LPAE
284         [MT_MINICLEAN] = {
285                 .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN | PMD_SECT_MINICACHE,
286                 .domain    = DOMAIN_KERNEL,
287         },
288 #endif
289         [MT_LOW_VECTORS] = {
290                 .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
291                                 L_PTE_RDONLY,
292                 .prot_l1   = PMD_TYPE_TABLE,
293                 .domain    = DOMAIN_USER,
294         },
295         [MT_HIGH_VECTORS] = {
296                 .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
297                                 L_PTE_USER | L_PTE_RDONLY,
298                 .prot_l1   = PMD_TYPE_TABLE,
299                 .domain    = DOMAIN_USER,
300         },
301         [MT_MEMORY_RWX] = {
302                 .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY,
303                 .prot_l1   = PMD_TYPE_TABLE,
304                 .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
305                 .domain    = DOMAIN_KERNEL,
306         },
307         [MT_MEMORY_RW] = {
308                 .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
309                              L_PTE_XN,
310                 .prot_l1   = PMD_TYPE_TABLE,
311                 .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
312                 .domain    = DOMAIN_KERNEL,
313         },
314         [MT_ROM] = {
315                 .prot_sect = PMD_TYPE_SECT,
316                 .domain    = DOMAIN_KERNEL,
317         },
318         [MT_MEMORY_RWX_NONCACHED] = {
319                 .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
320                                 L_PTE_MT_BUFFERABLE,
321                 .prot_l1   = PMD_TYPE_TABLE,
322                 .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE,
323                 .domain    = DOMAIN_KERNEL,
324         },
325         [MT_MEMORY_RW_DTCM] = {
326                 .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
327                                 L_PTE_XN,
328                 .prot_l1   = PMD_TYPE_TABLE,
329                 .prot_sect = PMD_TYPE_SECT | PMD_SECT_XN,
330                 .domain    = DOMAIN_KERNEL,
331         },
332         [MT_MEMORY_RWX_ITCM] = {
333                 .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY,
334                 .prot_l1   = PMD_TYPE_TABLE,
335                 .domain    = DOMAIN_KERNEL,
336         },
337         [MT_MEMORY_RW_SO] = {
338                 .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
339                                 L_PTE_MT_UNCACHED | L_PTE_XN,
340                 .prot_l1   = PMD_TYPE_TABLE,
341                 .prot_sect = PMD_TYPE_SECT | PMD_SECT_AP_WRITE | PMD_SECT_S |
342                                 PMD_SECT_UNCACHED | PMD_SECT_XN,
343                 .domain    = DOMAIN_KERNEL,
344         },
345         [MT_MEMORY_DMA_READY] = {
346                 .prot_pte  = L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY |
347                                 L_PTE_XN,
348                 .prot_l1   = PMD_TYPE_TABLE,
349                 .domain    = DOMAIN_KERNEL,
350         },
351 };
352
353 const struct mem_type *get_mem_type(unsigned int type)
354 {
355         return type < ARRAY_SIZE(mem_types) ? &mem_types[type] : NULL;
356 }
357 EXPORT_SYMBOL(get_mem_type);
358
359 /*
360  * Adjust the PMD section entries according to the CPU in use.
361  */
362 static void __init build_mem_type_table(void)
363 {
364         struct cachepolicy *cp;
365         unsigned int cr = get_cr();
366         pteval_t user_pgprot, kern_pgprot, vecs_pgprot;
367         pteval_t hyp_device_pgprot, s2_pgprot, s2_device_pgprot;
368         int cpu_arch = cpu_architecture();
369         int i;
370
371         if (cpu_arch < CPU_ARCH_ARMv6) {
372 #if defined(CONFIG_CPU_DCACHE_DISABLE)
373                 if (cachepolicy > CPOLICY_BUFFERED)
374                         cachepolicy = CPOLICY_BUFFERED;
375 #elif defined(CONFIG_CPU_DCACHE_WRITETHROUGH)
376                 if (cachepolicy > CPOLICY_WRITETHROUGH)
377                         cachepolicy = CPOLICY_WRITETHROUGH;
378 #endif
379         }
380         if (cpu_arch < CPU_ARCH_ARMv5) {
381                 if (cachepolicy >= CPOLICY_WRITEALLOC)
382                         cachepolicy = CPOLICY_WRITEBACK;
383                 ecc_mask = 0;
384         }
385
386         if (is_smp()) {
387                 if (cachepolicy != CPOLICY_WRITEALLOC) {
388                         pr_warn("Forcing write-allocate cache policy for SMP\n");
389                         cachepolicy = CPOLICY_WRITEALLOC;
390                 }
391                 if (!(initial_pmd_value & PMD_SECT_S)) {
392                         pr_warn("Forcing shared mappings for SMP\n");
393                         initial_pmd_value |= PMD_SECT_S;
394                 }
395         }
396
397         /*
398          * Strip out features not present on earlier architectures.
399          * Pre-ARMv5 CPUs don't have TEX bits.  Pre-ARMv6 CPUs or those
400          * without extended page tables don't have the 'Shared' bit.
401          */
402         if (cpu_arch < CPU_ARCH_ARMv5)
403                 for (i = 0; i < ARRAY_SIZE(mem_types); i++)
404                         mem_types[i].prot_sect &= ~PMD_SECT_TEX(7);
405         if ((cpu_arch < CPU_ARCH_ARMv6 || !(cr & CR_XP)) && !cpu_is_xsc3())
406                 for (i = 0; i < ARRAY_SIZE(mem_types); i++)
407                         mem_types[i].prot_sect &= ~PMD_SECT_S;
408
409         /*
410          * ARMv5 and lower, bit 4 must be set for page tables (was: cache
411          * "update-able on write" bit on ARM610).  However, Xscale and
412          * Xscale3 require this bit to be cleared.
413          */
414         if (cpu_is_xscale() || cpu_is_xsc3()) {
415                 for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
416                         mem_types[i].prot_sect &= ~PMD_BIT4;
417                         mem_types[i].prot_l1 &= ~PMD_BIT4;
418                 }
419         } else if (cpu_arch < CPU_ARCH_ARMv6) {
420                 for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
421                         if (mem_types[i].prot_l1)
422                                 mem_types[i].prot_l1 |= PMD_BIT4;
423                         if (mem_types[i].prot_sect)
424                                 mem_types[i].prot_sect |= PMD_BIT4;
425                 }
426         }
427
428         /*
429          * Mark the device areas according to the CPU/architecture.
430          */
431         if (cpu_is_xsc3() || (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP))) {
432                 if (!cpu_is_xsc3()) {
433                         /*
434                          * Mark device regions on ARMv6+ as execute-never
435                          * to prevent speculative instruction fetches.
436                          */
437                         mem_types[MT_DEVICE].prot_sect |= PMD_SECT_XN;
438                         mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_XN;
439                         mem_types[MT_DEVICE_CACHED].prot_sect |= PMD_SECT_XN;
440                         mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_XN;
441
442                         /* Also setup NX memory mapping */
443                         mem_types[MT_MEMORY_RW].prot_sect |= PMD_SECT_XN;
444                 }
445                 if (cpu_arch >= CPU_ARCH_ARMv7 && (cr & CR_TRE)) {
446                         /*
447                          * For ARMv7 with TEX remapping,
448                          * - shared device is SXCB=1100
449                          * - nonshared device is SXCB=0100
450                          * - write combine device mem is SXCB=0001
451                          * (Uncached Normal memory)
452                          */
453                         mem_types[MT_DEVICE].prot_sect |= PMD_SECT_TEX(1);
454                         mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(1);
455                         mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_BUFFERABLE;
456                 } else if (cpu_is_xsc3()) {
457                         /*
458                          * For Xscale3,
459                          * - shared device is TEXCB=00101
460                          * - nonshared device is TEXCB=01000
461                          * - write combine device mem is TEXCB=00100
462                          * (Inner/Outer Uncacheable in xsc3 parlance)
463                          */
464                         mem_types[MT_DEVICE].prot_sect |= PMD_SECT_TEX(1) | PMD_SECT_BUFFERED;
465                         mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(2);
466                         mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_TEX(1);
467                 } else {
468                         /*
469                          * For ARMv6 and ARMv7 without TEX remapping,
470                          * - shared device is TEXCB=00001
471                          * - nonshared device is TEXCB=01000
472                          * - write combine device mem is TEXCB=00100
473                          * (Uncached Normal in ARMv6 parlance).
474                          */
475                         mem_types[MT_DEVICE].prot_sect |= PMD_SECT_BUFFERED;
476                         mem_types[MT_DEVICE_NONSHARED].prot_sect |= PMD_SECT_TEX(2);
477                         mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_TEX(1);
478                 }
479         } else {
480                 /*
481                  * On others, write combining is "Uncached/Buffered"
482                  */
483                 mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_BUFFERABLE;
484         }
485
486         /*
487          * Now deal with the memory-type mappings
488          */
489         cp = &cache_policies[cachepolicy];
490         vecs_pgprot = kern_pgprot = user_pgprot = cp->pte;
491         s2_pgprot = cp->pte_s2;
492         hyp_device_pgprot = mem_types[MT_DEVICE].prot_pte;
493         s2_device_pgprot = mem_types[MT_DEVICE].prot_pte_s2;
494
495 #ifndef CONFIG_ARM_LPAE
496         /*
497          * We don't use domains on ARMv6 (since this causes problems with
498          * v6/v7 kernels), so we must use a separate memory type for user
499          * r/o, kernel r/w to map the vectors page.
500          */
501         if (cpu_arch == CPU_ARCH_ARMv6)
502                 vecs_pgprot |= L_PTE_MT_VECTORS;
503
504         /*
505          * Check is it with support for the PXN bit
506          * in the Short-descriptor translation table format descriptors.
507          */
508         if (cpu_arch == CPU_ARCH_ARMv7 &&
509                 (read_cpuid_ext(CPUID_EXT_MMFR0) & 0xF) == 4) {
510                 user_pmd_table |= PMD_PXNTABLE;
511         }
512 #endif
513
514         /*
515          * ARMv6 and above have extended page tables.
516          */
517         if (cpu_arch >= CPU_ARCH_ARMv6 && (cr & CR_XP)) {
518 #ifndef CONFIG_ARM_LPAE
519                 /*
520                  * Mark cache clean areas and XIP ROM read only
521                  * from SVC mode and no access from userspace.
522                  */
523                 mem_types[MT_ROM].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
524                 mem_types[MT_MINICLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
525                 mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_APX|PMD_SECT_AP_WRITE;
526 #endif
527
528                 /*
529                  * If the initial page tables were created with the S bit
530                  * set, then we need to do the same here for the same
531                  * reasons given in early_cachepolicy().
532                  */
533                 if (initial_pmd_value & PMD_SECT_S) {
534                         user_pgprot |= L_PTE_SHARED;
535                         kern_pgprot |= L_PTE_SHARED;
536                         vecs_pgprot |= L_PTE_SHARED;
537                         s2_pgprot |= L_PTE_SHARED;
538                         mem_types[MT_DEVICE_WC].prot_sect |= PMD_SECT_S;
539                         mem_types[MT_DEVICE_WC].prot_pte |= L_PTE_SHARED;
540                         mem_types[MT_DEVICE_CACHED].prot_sect |= PMD_SECT_S;
541                         mem_types[MT_DEVICE_CACHED].prot_pte |= L_PTE_SHARED;
542                         mem_types[MT_MEMORY_RWX].prot_sect |= PMD_SECT_S;
543                         mem_types[MT_MEMORY_RWX].prot_pte |= L_PTE_SHARED;
544                         mem_types[MT_MEMORY_RW].prot_sect |= PMD_SECT_S;
545                         mem_types[MT_MEMORY_RW].prot_pte |= L_PTE_SHARED;
546                         mem_types[MT_MEMORY_DMA_READY].prot_pte |= L_PTE_SHARED;
547                         mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |= PMD_SECT_S;
548                         mem_types[MT_MEMORY_RWX_NONCACHED].prot_pte |= L_PTE_SHARED;
549                 }
550         }
551
552         /*
553          * Non-cacheable Normal - intended for memory areas that must
554          * not cause dirty cache line writebacks when used
555          */
556         if (cpu_arch >= CPU_ARCH_ARMv6) {
557                 if (cpu_arch >= CPU_ARCH_ARMv7 && (cr & CR_TRE)) {
558                         /* Non-cacheable Normal is XCB = 001 */
559                         mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |=
560                                 PMD_SECT_BUFFERED;
561                 } else {
562                         /* For both ARMv6 and non-TEX-remapping ARMv7 */
563                         mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |=
564                                 PMD_SECT_TEX(1);
565                 }
566         } else {
567                 mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |= PMD_SECT_BUFFERABLE;
568         }
569
570 #ifdef CONFIG_ARM_LPAE
571         /*
572          * Do not generate access flag faults for the kernel mappings.
573          */
574         for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
575                 mem_types[i].prot_pte |= PTE_EXT_AF;
576                 if (mem_types[i].prot_sect)
577                         mem_types[i].prot_sect |= PMD_SECT_AF;
578         }
579         kern_pgprot |= PTE_EXT_AF;
580         vecs_pgprot |= PTE_EXT_AF;
581
582         /*
583          * Set PXN for user mappings
584          */
585         user_pgprot |= PTE_EXT_PXN;
586 #endif
587
588         for (i = 0; i < 16; i++) {
589                 pteval_t v = pgprot_val(protection_map[i]);
590                 protection_map[i] = __pgprot(v | user_pgprot);
591         }
592
593         mem_types[MT_LOW_VECTORS].prot_pte |= vecs_pgprot;
594         mem_types[MT_HIGH_VECTORS].prot_pte |= vecs_pgprot;
595
596         pgprot_user   = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | user_pgprot);
597         pgprot_kernel = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG |
598                                  L_PTE_DIRTY | kern_pgprot);
599         pgprot_s2  = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | s2_pgprot);
600         pgprot_s2_device  = __pgprot(s2_device_pgprot);
601         pgprot_hyp_device  = __pgprot(hyp_device_pgprot);
602
603         mem_types[MT_LOW_VECTORS].prot_l1 |= ecc_mask;
604         mem_types[MT_HIGH_VECTORS].prot_l1 |= ecc_mask;
605         mem_types[MT_MEMORY_RWX].prot_sect |= ecc_mask | cp->pmd;
606         mem_types[MT_MEMORY_RWX].prot_pte |= kern_pgprot;
607         mem_types[MT_MEMORY_RW].prot_sect |= ecc_mask | cp->pmd;
608         mem_types[MT_MEMORY_RW].prot_pte |= kern_pgprot;
609         mem_types[MT_MEMORY_DMA_READY].prot_pte |= kern_pgprot;
610         mem_types[MT_MEMORY_RWX_NONCACHED].prot_sect |= ecc_mask;
611         mem_types[MT_ROM].prot_sect |= cp->pmd;
612
613         switch (cp->pmd) {
614         case PMD_SECT_WT:
615                 mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WT;
616                 break;
617         case PMD_SECT_WB:
618         case PMD_SECT_WBWA:
619                 mem_types[MT_CACHECLEAN].prot_sect |= PMD_SECT_WB;
620                 break;
621         }
622         pr_info("Memory policy: %sData cache %s\n",
623                 ecc_mask ? "ECC enabled, " : "", cp->policy);
624
625         for (i = 0; i < ARRAY_SIZE(mem_types); i++) {
626                 struct mem_type *t = &mem_types[i];
627                 if (t->prot_l1)
628                         t->prot_l1 |= PMD_DOMAIN(t->domain);
629                 if (t->prot_sect)
630                         t->prot_sect |= PMD_DOMAIN(t->domain);
631         }
632 }
633
634 #ifdef CONFIG_ARM_DMA_MEM_BUFFERABLE
635 pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
636                               unsigned long size, pgprot_t vma_prot)
637 {
638         if (!pfn_valid(pfn))
639                 return pgprot_noncached(vma_prot);
640         else if (file->f_flags & O_SYNC)
641                 return pgprot_writecombine(vma_prot);
642         return vma_prot;
643 }
644 EXPORT_SYMBOL(phys_mem_access_prot);
645 #endif
646
647 #define vectors_base()  (vectors_high() ? 0xffff0000 : 0)
648
649 static void __init *early_alloc_aligned(unsigned long sz, unsigned long align)
650 {
651         void *ptr = __va(memblock_alloc(sz, align));
652         memset(ptr, 0, sz);
653         return ptr;
654 }
655
656 static void __init *early_alloc(unsigned long sz)
657 {
658         return early_alloc_aligned(sz, sz);
659 }
660
661 static pte_t * __init early_pte_alloc(pmd_t *pmd, unsigned long addr, unsigned long prot)
662 {
663         if (pmd_none(*pmd)) {
664                 pte_t *pte = early_alloc(PTE_HWTABLE_OFF + PTE_HWTABLE_SIZE);
665                 __pmd_populate(pmd, __pa(pte), prot);
666         }
667         BUG_ON(pmd_bad(*pmd));
668         return pte_offset_kernel(pmd, addr);
669 }
670
671 static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
672                                   unsigned long end, unsigned long pfn,
673                                   const struct mem_type *type)
674 {
675         pte_t *pte = early_pte_alloc(pmd, addr, type->prot_l1);
676         do {
677                 set_pte_ext(pte, pfn_pte(pfn, __pgprot(type->prot_pte)), 0);
678                 pfn++;
679         } while (pte++, addr += PAGE_SIZE, addr != end);
680 }
681
682 static void __init __map_init_section(pmd_t *pmd, unsigned long addr,
683                         unsigned long end, phys_addr_t phys,
684                         const struct mem_type *type)
685 {
686         pmd_t *p = pmd;
687
688 #ifndef CONFIG_ARM_LPAE
689         /*
690          * In classic MMU format, puds and pmds are folded in to
691          * the pgds. pmd_offset gives the PGD entry. PGDs refer to a
692          * group of L1 entries making up one logical pointer to
693          * an L2 table (2MB), where as PMDs refer to the individual
694          * L1 entries (1MB). Hence increment to get the correct
695          * offset for odd 1MB sections.
696          * (See arch/arm/include/asm/pgtable-2level.h)
697          */
698         if (addr & SECTION_SIZE)
699                 pmd++;
700 #endif
701         do {
702                 *pmd = __pmd(phys | type->prot_sect);
703                 phys += SECTION_SIZE;
704         } while (pmd++, addr += SECTION_SIZE, addr != end);
705
706         flush_pmd_entry(p);
707 }
708
709 static void __init alloc_init_pmd(pud_t *pud, unsigned long addr,
710                                       unsigned long end, phys_addr_t phys,
711                                       const struct mem_type *type)
712 {
713         pmd_t *pmd = pmd_offset(pud, addr);
714         unsigned long next;
715
716         do {
717                 /*
718                  * With LPAE, we must loop over to map
719                  * all the pmds for the given range.
720                  */
721                 next = pmd_addr_end(addr, end);
722
723                 /*
724                  * Try a section mapping - addr, next and phys must all be
725                  * aligned to a section boundary.
726                  */
727                 if (type->prot_sect &&
728                                 ((addr | next | phys) & ~SECTION_MASK) == 0) {
729                         __map_init_section(pmd, addr, next, phys, type);
730                 } else {
731                         alloc_init_pte(pmd, addr, next,
732                                                 __phys_to_pfn(phys), type);
733                 }
734
735                 phys += next - addr;
736
737         } while (pmd++, addr = next, addr != end);
738 }
739
740 static void __init alloc_init_pud(pgd_t *pgd, unsigned long addr,
741                                   unsigned long end, phys_addr_t phys,
742                                   const struct mem_type *type)
743 {
744         pud_t *pud = pud_offset(pgd, addr);
745         unsigned long next;
746
747         do {
748                 next = pud_addr_end(addr, end);
749                 alloc_init_pmd(pud, addr, next, phys, type);
750                 phys += next - addr;
751         } while (pud++, addr = next, addr != end);
752 }
753
754 #ifndef CONFIG_ARM_LPAE
755 static void __init create_36bit_mapping(struct map_desc *md,
756                                         const struct mem_type *type)
757 {
758         unsigned long addr, length, end;
759         phys_addr_t phys;
760         pgd_t *pgd;
761
762         addr = md->virtual;
763         phys = __pfn_to_phys(md->pfn);
764         length = PAGE_ALIGN(md->length);
765
766         if (!(cpu_architecture() >= CPU_ARCH_ARMv6 || cpu_is_xsc3())) {
767                 pr_err("MM: CPU does not support supersection mapping for 0x%08llx at 0x%08lx\n",
768                        (long long)__pfn_to_phys((u64)md->pfn), addr);
769                 return;
770         }
771
772         /* N.B. ARMv6 supersections are only defined to work with domain 0.
773          *      Since domain assignments can in fact be arbitrary, the
774          *      'domain == 0' check below is required to insure that ARMv6
775          *      supersections are only allocated for domain 0 regardless
776          *      of the actual domain assignments in use.
777          */
778         if (type->domain) {
779                 pr_err("MM: invalid domain in supersection mapping for 0x%08llx at 0x%08lx\n",
780                        (long long)__pfn_to_phys((u64)md->pfn), addr);
781                 return;
782         }
783
784         if ((addr | length | __pfn_to_phys(md->pfn)) & ~SUPERSECTION_MASK) {
785                 pr_err("MM: cannot create mapping for 0x%08llx at 0x%08lx invalid alignment\n",
786                        (long long)__pfn_to_phys((u64)md->pfn), addr);
787                 return;
788         }
789
790         /*
791          * Shift bits [35:32] of address into bits [23:20] of PMD
792          * (See ARMv6 spec).
793          */
794         phys |= (((md->pfn >> (32 - PAGE_SHIFT)) & 0xF) << 20);
795
796         pgd = pgd_offset_k(addr);
797         end = addr + length;
798         do {
799                 pud_t *pud = pud_offset(pgd, addr);
800                 pmd_t *pmd = pmd_offset(pud, addr);
801                 int i;
802
803                 for (i = 0; i < 16; i++)
804                         *pmd++ = __pmd(phys | type->prot_sect | PMD_SECT_SUPER);
805
806                 addr += SUPERSECTION_SIZE;
807                 phys += SUPERSECTION_SIZE;
808                 pgd += SUPERSECTION_SIZE >> PGDIR_SHIFT;
809         } while (addr != end);
810 }
811 #endif  /* !CONFIG_ARM_LPAE */
812
813 /*
814  * Create the page directory entries and any necessary
815  * page tables for the mapping specified by `md'.  We
816  * are able to cope here with varying sizes and address
817  * offsets, and we take full advantage of sections and
818  * supersections.
819  */
820 static void __init create_mapping(struct map_desc *md)
821 {
822         unsigned long addr, length, end;
823         phys_addr_t phys;
824         const struct mem_type *type;
825         pgd_t *pgd;
826
827         if (md->virtual != vectors_base() && md->virtual < TASK_SIZE) {
828                 pr_warn("BUG: not creating mapping for 0x%08llx at 0x%08lx in user region\n",
829                         (long long)__pfn_to_phys((u64)md->pfn), md->virtual);
830                 return;
831         }
832
833         if ((md->type == MT_DEVICE || md->type == MT_ROM) &&
834             md->virtual >= PAGE_OFFSET &&
835             (md->virtual < VMALLOC_START || md->virtual >= VMALLOC_END)) {
836                 pr_warn("BUG: mapping for 0x%08llx at 0x%08lx out of vmalloc space\n",
837                         (long long)__pfn_to_phys((u64)md->pfn), md->virtual);
838         }
839
840         type = &mem_types[md->type];
841
842 #ifndef CONFIG_ARM_LPAE
843         /*
844          * Catch 36-bit addresses
845          */
846         if (md->pfn >= 0x100000) {
847                 create_36bit_mapping(md, type);
848                 return;
849         }
850 #endif
851
852         addr = md->virtual & PAGE_MASK;
853         phys = __pfn_to_phys(md->pfn);
854         length = PAGE_ALIGN(md->length + (md->virtual & ~PAGE_MASK));
855
856         if (type->prot_l1 == 0 && ((addr | phys | length) & ~SECTION_MASK)) {
857                 pr_warn("BUG: map for 0x%08llx at 0x%08lx can not be mapped using pages, ignoring.\n",
858                         (long long)__pfn_to_phys(md->pfn), addr);
859                 return;
860         }
861
862         pgd = pgd_offset_k(addr);
863         end = addr + length;
864         do {
865                 unsigned long next = pgd_addr_end(addr, end);
866
867                 alloc_init_pud(pgd, addr, next, phys, type);
868
869                 phys += next - addr;
870                 addr = next;
871         } while (pgd++, addr != end);
872 }
873
874 /*
875  * Create the architecture specific mappings
876  */
877 void __init iotable_init(struct map_desc *io_desc, int nr)
878 {
879         struct map_desc *md;
880         struct vm_struct *vm;
881         struct static_vm *svm;
882
883         if (!nr)
884                 return;
885
886         svm = early_alloc_aligned(sizeof(*svm) * nr, __alignof__(*svm));
887
888         for (md = io_desc; nr; md++, nr--) {
889                 create_mapping(md);
890
891                 vm = &svm->vm;
892                 vm->addr = (void *)(md->virtual & PAGE_MASK);
893                 vm->size = PAGE_ALIGN(md->length + (md->virtual & ~PAGE_MASK));
894                 vm->phys_addr = __pfn_to_phys(md->pfn);
895                 vm->flags = VM_IOREMAP | VM_ARM_STATIC_MAPPING;
896                 vm->flags |= VM_ARM_MTYPE(md->type);
897                 vm->caller = iotable_init;
898                 add_static_vm_early(svm++);
899         }
900 }
901
902 void __init vm_reserve_area_early(unsigned long addr, unsigned long size,
903                                   void *caller)
904 {
905         struct vm_struct *vm;
906         struct static_vm *svm;
907
908         svm = early_alloc_aligned(sizeof(*svm), __alignof__(*svm));
909
910         vm = &svm->vm;
911         vm->addr = (void *)addr;
912         vm->size = size;
913         vm->flags = VM_IOREMAP | VM_ARM_EMPTY_MAPPING;
914         vm->caller = caller;
915         add_static_vm_early(svm);
916 }
917
918 #ifndef CONFIG_ARM_LPAE
919
920 /*
921  * The Linux PMD is made of two consecutive section entries covering 2MB
922  * (see definition in include/asm/pgtable-2level.h).  However a call to
923  * create_mapping() may optimize static mappings by using individual
924  * 1MB section mappings.  This leaves the actual PMD potentially half
925  * initialized if the top or bottom section entry isn't used, leaving it
926  * open to problems if a subsequent ioremap() or vmalloc() tries to use
927  * the virtual space left free by that unused section entry.
928  *
929  * Let's avoid the issue by inserting dummy vm entries covering the unused
930  * PMD halves once the static mappings are in place.
931  */
932
933 static void __init pmd_empty_section_gap(unsigned long addr)
934 {
935         vm_reserve_area_early(addr, SECTION_SIZE, pmd_empty_section_gap);
936 }
937
938 static void __init fill_pmd_gaps(void)
939 {
940         struct static_vm *svm;
941         struct vm_struct *vm;
942         unsigned long addr, next = 0;
943         pmd_t *pmd;
944
945         list_for_each_entry(svm, &static_vmlist, list) {
946                 vm = &svm->vm;
947                 addr = (unsigned long)vm->addr;
948                 if (addr < next)
949                         continue;
950
951                 /*
952                  * Check if this vm starts on an odd section boundary.
953                  * If so and the first section entry for this PMD is free
954                  * then we block the corresponding virtual address.
955                  */
956                 if ((addr & ~PMD_MASK) == SECTION_SIZE) {
957                         pmd = pmd_off_k(addr);
958                         if (pmd_none(*pmd))
959                                 pmd_empty_section_gap(addr & PMD_MASK);
960                 }
961
962                 /*
963                  * Then check if this vm ends on an odd section boundary.
964                  * If so and the second section entry for this PMD is empty
965                  * then we block the corresponding virtual address.
966                  */
967                 addr += vm->size;
968                 if ((addr & ~PMD_MASK) == SECTION_SIZE) {
969                         pmd = pmd_off_k(addr) + 1;
970                         if (pmd_none(*pmd))
971                                 pmd_empty_section_gap(addr);
972                 }
973
974                 /* no need to look at any vm entry until we hit the next PMD */
975                 next = (addr + PMD_SIZE - 1) & PMD_MASK;
976         }
977 }
978
979 #else
980 #define fill_pmd_gaps() do { } while (0)
981 #endif
982
983 #if defined(CONFIG_PCI) && !defined(CONFIG_NEED_MACH_IO_H)
984 static void __init pci_reserve_io(void)
985 {
986         struct static_vm *svm;
987
988         svm = find_static_vm_vaddr((void *)PCI_IO_VIRT_BASE);
989         if (svm)
990                 return;
991
992         vm_reserve_area_early(PCI_IO_VIRT_BASE, SZ_2M, pci_reserve_io);
993 }
994 #else
995 #define pci_reserve_io() do { } while (0)
996 #endif
997
998 #ifdef CONFIG_DEBUG_LL
999 void __init debug_ll_io_init(void)
1000 {
1001         struct map_desc map;
1002
1003         debug_ll_addr(&map.pfn, &map.virtual);
1004         if (!map.pfn || !map.virtual)
1005                 return;
1006         map.pfn = __phys_to_pfn(map.pfn);
1007         map.virtual &= PAGE_MASK;
1008         map.length = PAGE_SIZE;
1009         map.type = MT_DEVICE;
1010         iotable_init(&map, 1);
1011 }
1012 #endif
1013
1014 static void * __initdata vmalloc_min =
1015         (void *)(VMALLOC_END - (240 << 20) - VMALLOC_OFFSET);
1016
1017 /*
1018  * vmalloc=size forces the vmalloc area to be exactly 'size'
1019  * bytes. This can be used to increase (or decrease) the vmalloc
1020  * area - the default is 240m.
1021  */
1022 static int __init early_vmalloc(char *arg)
1023 {
1024         unsigned long vmalloc_reserve = memparse(arg, NULL);
1025
1026         if (vmalloc_reserve < SZ_16M) {
1027                 vmalloc_reserve = SZ_16M;
1028                 pr_warn("vmalloc area too small, limiting to %luMB\n",
1029                         vmalloc_reserve >> 20);
1030         }
1031
1032         if (vmalloc_reserve > VMALLOC_END - (PAGE_OFFSET + SZ_32M)) {
1033                 vmalloc_reserve = VMALLOC_END - (PAGE_OFFSET + SZ_32M);
1034                 pr_warn("vmalloc area is too big, limiting to %luMB\n",
1035                         vmalloc_reserve >> 20);
1036         }
1037
1038         vmalloc_min = (void *)(VMALLOC_END - vmalloc_reserve);
1039         return 0;
1040 }
1041 early_param("vmalloc", early_vmalloc);
1042
1043 phys_addr_t arm_lowmem_limit __initdata = 0;
1044
1045 void __init sanity_check_meminfo(void)
1046 {
1047         phys_addr_t memblock_limit = 0;
1048         int highmem = 0;
1049         phys_addr_t vmalloc_limit = __pa(vmalloc_min - 1) + 1;
1050         struct memblock_region *reg;
1051
1052         for_each_memblock(memory, reg) {
1053                 phys_addr_t block_start = reg->base;
1054                 phys_addr_t block_end = reg->base + reg->size;
1055                 phys_addr_t size_limit = reg->size;
1056
1057                 if (reg->base >= vmalloc_limit)
1058                         highmem = 1;
1059                 else
1060                         size_limit = vmalloc_limit - reg->base;
1061
1062
1063                 if (!IS_ENABLED(CONFIG_HIGHMEM) || cache_is_vipt_aliasing()) {
1064
1065                         if (highmem) {
1066                                 pr_notice("Ignoring RAM at %pa-%pa (!CONFIG_HIGHMEM)\n",
1067                                           &block_start, &block_end);
1068                                 memblock_remove(reg->base, reg->size);
1069                                 continue;
1070                         }
1071
1072                         if (reg->size > size_limit) {
1073                                 phys_addr_t overlap_size = reg->size - size_limit;
1074
1075                                 pr_notice("Truncating RAM at %pa-%pa to -%pa",
1076                                           &block_start, &block_end, &vmalloc_limit);
1077                                 memblock_remove(vmalloc_limit, overlap_size);
1078                                 block_end = vmalloc_limit;
1079                         }
1080                 }
1081
1082                 if (!highmem) {
1083                         if (block_end > arm_lowmem_limit) {
1084                                 if (reg->size > size_limit)
1085                                         arm_lowmem_limit = vmalloc_limit;
1086                                 else
1087                                         arm_lowmem_limit = block_end;
1088                         }
1089
1090                         /*
1091                          * Find the first non-section-aligned page, and point
1092                          * memblock_limit at it. This relies on rounding the
1093                          * limit down to be section-aligned, which happens at
1094                          * the end of this function.
1095                          *
1096                          * With this algorithm, the start or end of almost any
1097                          * bank can be non-section-aligned. The only exception
1098                          * is that the start of the bank 0 must be section-
1099                          * aligned, since otherwise memory would need to be
1100                          * allocated when mapping the start of bank 0, which
1101                          * occurs before any free memory is mapped.
1102                          */
1103                         if (!memblock_limit) {
1104                                 if (!IS_ALIGNED(block_start, SECTION_SIZE))
1105                                         memblock_limit = block_start;
1106                                 else if (!IS_ALIGNED(block_end, SECTION_SIZE))
1107                                         memblock_limit = arm_lowmem_limit;
1108                         }
1109
1110                 }
1111         }
1112
1113         high_memory = __va(arm_lowmem_limit - 1) + 1;
1114
1115         /*
1116          * Round the memblock limit down to a section size.  This
1117          * helps to ensure that we will allocate memory from the
1118          * last full section, which should be mapped.
1119          */
1120         if (memblock_limit)
1121                 memblock_limit = round_down(memblock_limit, SECTION_SIZE);
1122         if (!memblock_limit)
1123                 memblock_limit = arm_lowmem_limit;
1124
1125         memblock_set_current_limit(memblock_limit);
1126 }
1127
1128 static inline void prepare_page_table(void)
1129 {
1130         unsigned long addr;
1131         phys_addr_t end;
1132
1133         /*
1134          * Clear out all the mappings below the kernel image.
1135          */
1136         for (addr = 0; addr < MODULES_VADDR; addr += PMD_SIZE)
1137                 pmd_clear(pmd_off_k(addr));
1138
1139 #ifdef CONFIG_XIP_KERNEL
1140         /* The XIP kernel is mapped in the module area -- skip over it */
1141         addr = ((unsigned long)_etext + PMD_SIZE - 1) & PMD_MASK;
1142 #endif
1143         for ( ; addr < PAGE_OFFSET; addr += PMD_SIZE)
1144                 pmd_clear(pmd_off_k(addr));
1145
1146         /*
1147          * Find the end of the first block of lowmem.
1148          */
1149         end = memblock.memory.regions[0].base + memblock.memory.regions[0].size;
1150         if (end >= arm_lowmem_limit)
1151                 end = arm_lowmem_limit;
1152
1153         /*
1154          * Clear out all the kernel space mappings, except for the first
1155          * memory bank, up to the vmalloc region.
1156          */
1157         for (addr = __phys_to_virt(end);
1158              addr < VMALLOC_START; addr += PMD_SIZE)
1159                 pmd_clear(pmd_off_k(addr));
1160 }
1161
1162 #ifdef CONFIG_ARM_LPAE
1163 /* the first page is reserved for pgd */
1164 #define SWAPPER_PG_DIR_SIZE     (PAGE_SIZE + \
1165                                  PTRS_PER_PGD * PTRS_PER_PMD * sizeof(pmd_t))
1166 #else
1167 #define SWAPPER_PG_DIR_SIZE     (PTRS_PER_PGD * sizeof(pgd_t))
1168 #endif
1169
1170 /*
1171  * Reserve the special regions of memory
1172  */
1173 void __init arm_mm_memblock_reserve(void)
1174 {
1175         /*
1176          * Reserve the page tables.  These are already in use,
1177          * and can only be in node 0.
1178          */
1179         memblock_reserve(__pa(swapper_pg_dir), SWAPPER_PG_DIR_SIZE);
1180
1181 #ifdef CONFIG_SA1111
1182         /*
1183          * Because of the SA1111 DMA bug, we want to preserve our
1184          * precious DMA-able memory...
1185          */
1186         memblock_reserve(PHYS_OFFSET, __pa(swapper_pg_dir) - PHYS_OFFSET);
1187 #endif
1188 }
1189
1190 /*
1191  * Set up the device mappings.  Since we clear out the page tables for all
1192  * mappings above VMALLOC_START, we will remove any debug device mappings.
1193  * This means you have to be careful how you debug this function, or any
1194  * called function.  This means you can't use any function or debugging
1195  * method which may touch any device, otherwise the kernel _will_ crash.
1196  */
1197 static void __init devicemaps_init(const struct machine_desc *mdesc)
1198 {
1199         struct map_desc map;
1200         unsigned long addr;
1201         void *vectors;
1202
1203         /*
1204          * Allocate the vector page early.
1205          */
1206         vectors = early_alloc(PAGE_SIZE * 2);
1207
1208         early_trap_init(vectors);
1209
1210         for (addr = VMALLOC_START; addr; addr += PMD_SIZE)
1211                 pmd_clear(pmd_off_k(addr));
1212
1213         /*
1214          * Map the kernel if it is XIP.
1215          * It is always first in the modulearea.
1216          */
1217 #ifdef CONFIG_XIP_KERNEL
1218         map.pfn = __phys_to_pfn(CONFIG_XIP_PHYS_ADDR & SECTION_MASK);
1219         map.virtual = MODULES_VADDR;
1220         map.length = ((unsigned long)_etext - map.virtual + ~SECTION_MASK) & SECTION_MASK;
1221         map.type = MT_ROM;
1222         create_mapping(&map);
1223 #endif
1224
1225         /*
1226          * Map the cache flushing regions.
1227          */
1228 #ifdef FLUSH_BASE
1229         map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS);
1230         map.virtual = FLUSH_BASE;
1231         map.length = SZ_1M;
1232         map.type = MT_CACHECLEAN;
1233         create_mapping(&map);
1234 #endif
1235 #ifdef FLUSH_BASE_MINICACHE
1236         map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS + SZ_1M);
1237         map.virtual = FLUSH_BASE_MINICACHE;
1238         map.length = SZ_1M;
1239         map.type = MT_MINICLEAN;
1240         create_mapping(&map);
1241 #endif
1242
1243         /*
1244          * Create a mapping for the machine vectors at the high-vectors
1245          * location (0xffff0000).  If we aren't using high-vectors, also
1246          * create a mapping at the low-vectors virtual address.
1247          */
1248         map.pfn = __phys_to_pfn(virt_to_phys(vectors));
1249         map.virtual = 0xffff0000;
1250         map.length = PAGE_SIZE;
1251 #ifdef CONFIG_KUSER_HELPERS
1252         map.type = MT_HIGH_VECTORS;
1253 #else
1254         map.type = MT_LOW_VECTORS;
1255 #endif
1256         create_mapping(&map);
1257
1258         if (!vectors_high()) {
1259                 map.virtual = 0;
1260                 map.length = PAGE_SIZE * 2;
1261                 map.type = MT_LOW_VECTORS;
1262                 create_mapping(&map);
1263         }
1264
1265         /* Now create a kernel read-only mapping */
1266         map.pfn += 1;
1267         map.virtual = 0xffff0000 + PAGE_SIZE;
1268         map.length = PAGE_SIZE;
1269         map.type = MT_LOW_VECTORS;
1270         create_mapping(&map);
1271
1272         /*
1273          * Ask the machine support to map in the statically mapped devices.
1274          */
1275         if (mdesc->map_io)
1276                 mdesc->map_io();
1277         else
1278                 debug_ll_io_init();
1279         fill_pmd_gaps();
1280
1281         /* Reserve fixed i/o space in VMALLOC region */
1282         pci_reserve_io();
1283
1284         /*
1285          * Finally flush the caches and tlb to ensure that we're in a
1286          * consistent state wrt the writebuffer.  This also ensures that
1287          * any write-allocated cache lines in the vector page are written
1288          * back.  After this point, we can start to touch devices again.
1289          */
1290         local_flush_tlb_all();
1291         flush_cache_all();
1292 }
1293
1294 static void __init kmap_init(void)
1295 {
1296 #ifdef CONFIG_HIGHMEM
1297         pkmap_page_table = early_pte_alloc(pmd_off_k(PKMAP_BASE),
1298                 PKMAP_BASE, _PAGE_KERNEL_TABLE);
1299
1300         fixmap_page_table = early_pte_alloc(pmd_off_k(FIXADDR_START),
1301                 FIXADDR_START, _PAGE_KERNEL_TABLE);
1302 #endif
1303 }
1304
1305 static void __init map_lowmem(void)
1306 {
1307         struct memblock_region *reg;
1308         unsigned long kernel_x_start = round_down(__pa(_stext), SECTION_SIZE);
1309         unsigned long kernel_x_end = round_up(__pa(__init_end), SECTION_SIZE);
1310
1311         /* Map all the lowmem memory banks. */
1312         for_each_memblock(memory, reg) {
1313                 phys_addr_t start = reg->base;
1314                 phys_addr_t end = start + reg->size;
1315                 struct map_desc map;
1316
1317                 if (end > arm_lowmem_limit)
1318                         end = arm_lowmem_limit;
1319                 if (start >= end)
1320                         break;
1321
1322                 if (end < kernel_x_start || start >= kernel_x_end) {
1323                         map.pfn = __phys_to_pfn(start);
1324                         map.virtual = __phys_to_virt(start);
1325                         map.length = end - start;
1326                         map.type = MT_MEMORY_RWX;
1327
1328                         create_mapping(&map);
1329                 } else {
1330                         /* This better cover the entire kernel */
1331                         if (start < kernel_x_start) {
1332                                 map.pfn = __phys_to_pfn(start);
1333                                 map.virtual = __phys_to_virt(start);
1334                                 map.length = kernel_x_start - start;
1335                                 map.type = MT_MEMORY_RW;
1336
1337                                 create_mapping(&map);
1338                         }
1339
1340                         map.pfn = __phys_to_pfn(kernel_x_start);
1341                         map.virtual = __phys_to_virt(kernel_x_start);
1342                         map.length = kernel_x_end - kernel_x_start;
1343                         map.type = MT_MEMORY_RWX;
1344
1345                         create_mapping(&map);
1346
1347                         if (kernel_x_end < end) {
1348                                 map.pfn = __phys_to_pfn(kernel_x_end);
1349                                 map.virtual = __phys_to_virt(kernel_x_end);
1350                                 map.length = end - kernel_x_end;
1351                                 map.type = MT_MEMORY_RW;
1352
1353                                 create_mapping(&map);
1354                         }
1355                 }
1356         }
1357 }
1358
1359 #ifdef CONFIG_ARM_LPAE
1360 /*
1361  * early_paging_init() recreates boot time page table setup, allowing machines
1362  * to switch over to a high (>4G) address space on LPAE systems
1363  */
1364 void __init early_paging_init(const struct machine_desc *mdesc,
1365                               struct proc_info_list *procinfo)
1366 {
1367         pmdval_t pmdprot = procinfo->__cpu_mm_mmu_flags;
1368         unsigned long map_start, map_end;
1369         pgd_t *pgd0, *pgdk;
1370         pud_t *pud0, *pudk, *pud_start;
1371         pmd_t *pmd0, *pmdk;
1372         phys_addr_t phys;
1373         int i;
1374
1375         if (!(mdesc->init_meminfo))
1376                 return;
1377
1378         /* remap kernel code and data */
1379         map_start = init_mm.start_code & PMD_MASK;
1380         map_end   = ALIGN(init_mm.brk, PMD_SIZE);
1381
1382         /* get a handle on things... */
1383         pgd0 = pgd_offset_k(0);
1384         pud_start = pud0 = pud_offset(pgd0, 0);
1385         pmd0 = pmd_offset(pud0, 0);
1386
1387         pgdk = pgd_offset_k(map_start);
1388         pudk = pud_offset(pgdk, map_start);
1389         pmdk = pmd_offset(pudk, map_start);
1390
1391         mdesc->init_meminfo();
1392
1393         /* Run the patch stub to update the constants */
1394         fixup_pv_table(&__pv_table_begin,
1395                 (&__pv_table_end - &__pv_table_begin) << 2);
1396
1397         /*
1398          * Cache cleaning operations for self-modifying code
1399          * We should clean the entries by MVA but running a
1400          * for loop over every pv_table entry pointer would
1401          * just complicate the code.
1402          */
1403         flush_cache_louis();
1404         dsb(ishst);
1405         isb();
1406
1407         /*
1408          * FIXME: This code is not architecturally compliant: we modify
1409          * the mappings in-place, indeed while they are in use by this
1410          * very same code.  This may lead to unpredictable behaviour of
1411          * the CPU.
1412          *
1413          * Even modifying the mappings in a separate page table does
1414          * not resolve this.
1415          *
1416          * The architecture strongly recommends that when a mapping is
1417          * changed, that it is changed by first going via an invalid
1418          * mapping and back to the new mapping.  This is to ensure that
1419          * no TLB conflicts (caused by the TLB having more than one TLB
1420          * entry match a translation) can occur.  However, doing that
1421          * here will result in unmapping the code we are running.
1422          */
1423         pr_warn("WARNING: unsafe modification of in-place page tables - tainting kernel\n");
1424         add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
1425
1426         /*
1427          * Remap level 1 table.  This changes the physical addresses
1428          * used to refer to the level 2 page tables to the high
1429          * physical address alias, leaving everything else the same.
1430          */
1431         for (i = 0; i < PTRS_PER_PGD; pud0++, i++) {
1432                 set_pud(pud0,
1433                         __pud(__pa(pmd0) | PMD_TYPE_TABLE | L_PGD_SWAPPER));
1434                 pmd0 += PTRS_PER_PMD;
1435         }
1436
1437         /*
1438          * Remap the level 2 table, pointing the mappings at the high
1439          * physical address alias of these pages.
1440          */
1441         phys = __pa(map_start);
1442         do {
1443                 *pmdk++ = __pmd(phys | pmdprot);
1444                 phys += PMD_SIZE;
1445         } while (phys < map_end);
1446
1447         /*
1448          * Ensure that the above updates are flushed out of the cache.
1449          * This is not strictly correct; on a system where the caches
1450          * are coherent with each other, but the MMU page table walks
1451          * may not be coherent, flush_cache_all() may be a no-op, and
1452          * this will fail.
1453          */
1454         flush_cache_all();
1455
1456         /*
1457          * Re-write the TTBR values to point them at the high physical
1458          * alias of the page tables.  We expect __va() will work on
1459          * cpu_get_pgd(), which returns the value of TTBR0.
1460          */
1461         cpu_switch_mm(pgd0, &init_mm);
1462         cpu_set_ttbr(1, __pa(pgd0) + TTBR1_OFFSET);
1463
1464         /* Finally flush any stale TLB values. */
1465         local_flush_bp_all();
1466         local_flush_tlb_all();
1467 }
1468
1469 #else
1470
1471 void __init early_paging_init(const struct machine_desc *mdesc,
1472                               struct proc_info_list *procinfo)
1473 {
1474         if (mdesc->init_meminfo)
1475                 mdesc->init_meminfo();
1476 }
1477
1478 #endif
1479
1480 /*
1481  * paging_init() sets up the page tables, initialises the zone memory
1482  * maps, and sets up the zero page, bad page and bad page tables.
1483  */
1484 void __init paging_init(const struct machine_desc *mdesc)
1485 {
1486         void *zero_page;
1487
1488         build_mem_type_table();
1489         prepare_page_table();
1490         map_lowmem();
1491         dma_contiguous_remap();
1492         devicemaps_init(mdesc);
1493         kmap_init();
1494         tcm_init();
1495
1496         top_pmd = pmd_off_k(0xffff0000);
1497
1498         /* allocate the zero page. */
1499         zero_page = early_alloc(PAGE_SIZE);
1500
1501         bootmem_init();
1502
1503         empty_zero_page = virt_to_page(zero_page);
1504         __flush_dcache_page(NULL, empty_zero_page);
1505 }