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