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