xref: /openbmc/linux/arch/arm/mm/idmap.c (revision e5242c5f)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/module.h>
3 #include <linux/kernel.h>
4 #include <linux/slab.h>
5 #include <linux/mm_types.h>
6 #include <linux/pgtable.h>
7 
8 #include <asm/cputype.h>
9 #include <asm/idmap.h>
10 #include <asm/hwcap.h>
11 #include <asm/pgalloc.h>
12 #include <asm/sections.h>
13 #include <asm/system_info.h>
14 
15 /*
16  * Note: accesses outside of the kernel image and the identity map area
17  * are not supported on any CPU using the idmap tables as its current
18  * page tables.
19  */
20 pgd_t *idmap_pgd __ro_after_init;
21 long long arch_phys_to_idmap_offset __ro_after_init;
22 
23 #ifdef CONFIG_ARM_LPAE
24 static void idmap_add_pmd(pud_t *pud, unsigned long addr, unsigned long end,
25 	unsigned long prot)
26 {
27 	pmd_t *pmd;
28 	unsigned long next;
29 
30 	if (pud_none_or_clear_bad(pud) || (pud_val(*pud) & L_PGD_SWAPPER)) {
31 		pmd = pmd_alloc_one(&init_mm, addr);
32 		if (!pmd) {
33 			pr_warn("Failed to allocate identity pmd.\n");
34 			return;
35 		}
36 		/*
37 		 * Copy the original PMD to ensure that the PMD entries for
38 		 * the kernel image are preserved.
39 		 */
40 		if (!pud_none(*pud))
41 			memcpy(pmd, pmd_offset(pud, 0),
42 			       PTRS_PER_PMD * sizeof(pmd_t));
43 		pud_populate(&init_mm, pud, pmd);
44 		pmd += pmd_index(addr);
45 	} else
46 		pmd = pmd_offset(pud, addr);
47 
48 	do {
49 		next = pmd_addr_end(addr, end);
50 		*pmd = __pmd((addr & PMD_MASK) | prot);
51 		flush_pmd_entry(pmd);
52 	} while (pmd++, addr = next, addr != end);
53 }
54 #else	/* !CONFIG_ARM_LPAE */
55 static void idmap_add_pmd(pud_t *pud, unsigned long addr, unsigned long end,
56 	unsigned long prot)
57 {
58 	pmd_t *pmd = pmd_offset(pud, addr);
59 
60 	addr = (addr & PMD_MASK) | prot;
61 	pmd[0] = __pmd(addr);
62 	addr += SECTION_SIZE;
63 	pmd[1] = __pmd(addr);
64 	flush_pmd_entry(pmd);
65 }
66 #endif	/* CONFIG_ARM_LPAE */
67 
68 static void idmap_add_pud(pgd_t *pgd, unsigned long addr, unsigned long end,
69 	unsigned long prot)
70 {
71 	p4d_t *p4d = p4d_offset(pgd, addr);
72 	pud_t *pud = pud_offset(p4d, addr);
73 	unsigned long next;
74 
75 	do {
76 		next = pud_addr_end(addr, end);
77 		idmap_add_pmd(pud, addr, next, prot);
78 	} while (pud++, addr = next, addr != end);
79 }
80 
81 static void identity_mapping_add(pgd_t *pgd, const char *text_start,
82 				 const char *text_end, unsigned long prot)
83 {
84 	unsigned long addr, end;
85 	unsigned long next;
86 
87 #ifdef CONFIG_XIP_KERNEL
88 	addr = (phys_addr_t)(text_start) - XIP_VIRT_ADDR(CONFIG_XIP_PHYS_ADDR)
89 		+ CONFIG_XIP_PHYS_ADDR;
90 	end = (phys_addr_t)(text_end) - XIP_VIRT_ADDR(CONFIG_XIP_PHYS_ADDR)
91 		+ CONFIG_XIP_PHYS_ADDR;
92 #else
93 	addr = virt_to_idmap(text_start);
94 	end = virt_to_idmap(text_end);
95 #endif
96 	pr_info("Setting up static identity map for 0x%lx - 0x%lx\n", addr, end);
97 
98 	prot |= PMD_TYPE_SECT | PMD_SECT_AP_WRITE | PMD_SECT_AF;
99 
100 	if (cpu_architecture() <= CPU_ARCH_ARMv5TEJ && !cpu_is_xscale_family())
101 		prot |= PMD_BIT4;
102 
103 	pgd += pgd_index(addr);
104 	do {
105 		next = pgd_addr_end(addr, end);
106 		idmap_add_pud(pgd, addr, next, prot);
107 	} while (pgd++, addr = next, addr != end);
108 }
109 
110 extern char  __idmap_text_start[], __idmap_text_end[];
111 
112 static int __init init_static_idmap(void)
113 {
114 	idmap_pgd = pgd_alloc(&init_mm);
115 	if (!idmap_pgd)
116 		return -ENOMEM;
117 
118 	identity_mapping_add(idmap_pgd, __idmap_text_start,
119 			     __idmap_text_end, 0);
120 
121 	/* Flush L1 for the hardware to see this page table content */
122 	if (!(elf_hwcap & HWCAP_LPAE))
123 		flush_cache_louis();
124 
125 	return 0;
126 }
127 early_initcall(init_static_idmap);
128 
129 /*
130  * In order to soft-boot, we need to switch to a 1:1 mapping for the
131  * cpu_reset functions. This will then ensure that we have predictable
132  * results when turning off the mmu.
133  */
134 void setup_mm_for_reboot(void)
135 {
136 	/* Switch to the identity mapping. */
137 	cpu_switch_mm(idmap_pgd, &init_mm);
138 	local_flush_bp_all();
139 
140 #ifdef CONFIG_CPU_HAS_ASID
141 	/*
142 	 * We don't have a clean ASID for the identity mapping, which
143 	 * may clash with virtual addresses of the previous page tables
144 	 * and therefore potentially in the TLB.
145 	 */
146 	local_flush_tlb_all();
147 #endif
148 }
149