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