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