xref: /openbmc/linux/arch/arm64/include/asm/mmu_context.h (revision 5ffdfaedfa0aba3f5db0fbb8ed4f3192be2b39b8)
1b3901d54SCatalin Marinas /*
2b3901d54SCatalin Marinas  * Based on arch/arm/include/asm/mmu_context.h
3b3901d54SCatalin Marinas  *
4b3901d54SCatalin Marinas  * Copyright (C) 1996 Russell King.
5b3901d54SCatalin Marinas  * Copyright (C) 2012 ARM Ltd.
6b3901d54SCatalin Marinas  *
7b3901d54SCatalin Marinas  * This program is free software; you can redistribute it and/or modify
8b3901d54SCatalin Marinas  * it under the terms of the GNU General Public License version 2 as
9b3901d54SCatalin Marinas  * published by the Free Software Foundation.
10b3901d54SCatalin Marinas  *
11b3901d54SCatalin Marinas  * This program is distributed in the hope that it will be useful,
12b3901d54SCatalin Marinas  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13b3901d54SCatalin Marinas  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14b3901d54SCatalin Marinas  * GNU General Public License for more details.
15b3901d54SCatalin Marinas  *
16b3901d54SCatalin Marinas  * You should have received a copy of the GNU General Public License
17b3901d54SCatalin Marinas  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18b3901d54SCatalin Marinas  */
19b3901d54SCatalin Marinas #ifndef __ASM_MMU_CONTEXT_H
20b3901d54SCatalin Marinas #define __ASM_MMU_CONTEXT_H
21b3901d54SCatalin Marinas 
2238fd94b0SChristopher Covington #ifndef __ASSEMBLY__
2338fd94b0SChristopher Covington 
24b3901d54SCatalin Marinas #include <linux/compiler.h>
25b3901d54SCatalin Marinas #include <linux/sched.h>
26ef8bd77fSIngo Molnar #include <linux/sched/hotplug.h>
27589ee628SIngo Molnar #include <linux/mm_types.h>
28b3901d54SCatalin Marinas 
29b3901d54SCatalin Marinas #include <asm/cacheflush.h>
3039bc88e5SCatalin Marinas #include <asm/cpufeature.h>
31b3901d54SCatalin Marinas #include <asm/proc-fns.h>
32b3901d54SCatalin Marinas #include <asm-generic/mm_hooks.h>
33b3901d54SCatalin Marinas #include <asm/cputype.h>
34b3901d54SCatalin Marinas #include <asm/pgtable.h>
35adf75899SMark Rutland #include <asm/sysreg.h>
369e8e865bSMark Rutland #include <asm/tlbflush.h>
37b3901d54SCatalin Marinas 
38ec45d1cfSWill Deacon static inline void contextidr_thread_switch(struct task_struct *next)
39ec45d1cfSWill Deacon {
40d3ea42aaSMark Rutland 	if (!IS_ENABLED(CONFIG_PID_IN_CONTEXTIDR))
41d3ea42aaSMark Rutland 		return;
42d3ea42aaSMark Rutland 
43adf75899SMark Rutland 	write_sysreg(task_pid_nr(next), contextidr_el1);
44adf75899SMark Rutland 	isb();
45ec45d1cfSWill Deacon }
46ec45d1cfSWill Deacon 
47b3901d54SCatalin Marinas /*
48b3901d54SCatalin Marinas  * Set TTBR0 to empty_zero_page. No translations will be possible via TTBR0.
49b3901d54SCatalin Marinas  */
50b3901d54SCatalin Marinas static inline void cpu_set_reserved_ttbr0(void)
51b3901d54SCatalin Marinas {
52529c4b05SKristina Martsenko 	unsigned long ttbr = phys_to_ttbr(__pa_symbol(empty_zero_page));
53b3901d54SCatalin Marinas 
54adf75899SMark Rutland 	write_sysreg(ttbr, ttbr0_el1);
55adf75899SMark Rutland 	isb();
56b3901d54SCatalin Marinas }
57b3901d54SCatalin Marinas 
587655abb9SWill Deacon static inline void cpu_switch_mm(pgd_t *pgd, struct mm_struct *mm)
597655abb9SWill Deacon {
607655abb9SWill Deacon 	BUG_ON(pgd == swapper_pg_dir);
617655abb9SWill Deacon 	cpu_set_reserved_ttbr0();
627655abb9SWill Deacon 	cpu_do_switch_mm(virt_to_phys(pgd),mm);
637655abb9SWill Deacon }
647655abb9SWill Deacon 
65dd006da2SArd Biesheuvel /*
66dd006da2SArd Biesheuvel  * TCR.T0SZ value to use when the ID map is active. Usually equals
67dd006da2SArd Biesheuvel  * TCR_T0SZ(VA_BITS), unless system RAM is positioned very high in
68dd006da2SArd Biesheuvel  * physical memory, in which case it will be smaller.
69dd006da2SArd Biesheuvel  */
70dd006da2SArd Biesheuvel extern u64 idmap_t0sz;
71fa2a8445SKristina Martsenko extern u64 idmap_ptrs_per_pgd;
72dd006da2SArd Biesheuvel 
73dd006da2SArd Biesheuvel static inline bool __cpu_uses_extended_idmap(void)
74dd006da2SArd Biesheuvel {
756a205420SKristina Martsenko 	return unlikely(idmap_t0sz != TCR_T0SZ(VA_BITS));
76dd006da2SArd Biesheuvel }
77dd006da2SArd Biesheuvel 
78c51e97d8SWill Deacon /*
79fa2a8445SKristina Martsenko  * True if the extended ID map requires an extra level of translation table
80fa2a8445SKristina Martsenko  * to be configured.
81fa2a8445SKristina Martsenko  */
82fa2a8445SKristina Martsenko static inline bool __cpu_uses_extended_idmap_level(void)
83fa2a8445SKristina Martsenko {
846a205420SKristina Martsenko 	return ARM64_HW_PGTABLE_LEVELS(64 - idmap_t0sz) > CONFIG_PGTABLE_LEVELS;
85fa2a8445SKristina Martsenko }
86fa2a8445SKristina Martsenko 
87fa2a8445SKristina Martsenko /*
88c51e97d8SWill Deacon  * Set TCR.T0SZ to its default value (based on VA_BITS)
89c51e97d8SWill Deacon  */
90609116d2SMark Rutland static inline void __cpu_set_tcr_t0sz(unsigned long t0sz)
91dd006da2SArd Biesheuvel {
92dd006da2SArd Biesheuvel 	unsigned long tcr;
93dd006da2SArd Biesheuvel 
94c51e97d8SWill Deacon 	if (!__cpu_uses_extended_idmap())
95c51e97d8SWill Deacon 		return;
96c51e97d8SWill Deacon 
97adf75899SMark Rutland 	tcr = read_sysreg(tcr_el1);
98adf75899SMark Rutland 	tcr &= ~TCR_T0SZ_MASK;
99adf75899SMark Rutland 	tcr |= t0sz << TCR_T0SZ_OFFSET;
100adf75899SMark Rutland 	write_sysreg(tcr, tcr_el1);
101adf75899SMark Rutland 	isb();
102dd006da2SArd Biesheuvel }
103dd006da2SArd Biesheuvel 
104609116d2SMark Rutland #define cpu_set_default_tcr_t0sz()	__cpu_set_tcr_t0sz(TCR_T0SZ(VA_BITS))
105609116d2SMark Rutland #define cpu_set_idmap_tcr_t0sz()	__cpu_set_tcr_t0sz(idmap_t0sz)
106609116d2SMark Rutland 
107b3901d54SCatalin Marinas /*
1089e8e865bSMark Rutland  * Remove the idmap from TTBR0_EL1 and install the pgd of the active mm.
1099e8e865bSMark Rutland  *
1109e8e865bSMark Rutland  * The idmap lives in the same VA range as userspace, but uses global entries
1119e8e865bSMark Rutland  * and may use a different TCR_EL1.T0SZ. To avoid issues resulting from
1129e8e865bSMark Rutland  * speculative TLB fetches, we must temporarily install the reserved page
1139e8e865bSMark Rutland  * tables while we invalidate the TLBs and set up the correct TCR_EL1.T0SZ.
1149e8e865bSMark Rutland  *
1159e8e865bSMark Rutland  * If current is a not a user task, the mm covers the TTBR1_EL1 page tables,
1169e8e865bSMark Rutland  * which should not be installed in TTBR0_EL1. In this case we can leave the
1179e8e865bSMark Rutland  * reserved page tables in place.
1189e8e865bSMark Rutland  */
1199e8e865bSMark Rutland static inline void cpu_uninstall_idmap(void)
1209e8e865bSMark Rutland {
1219e8e865bSMark Rutland 	struct mm_struct *mm = current->active_mm;
1229e8e865bSMark Rutland 
1239e8e865bSMark Rutland 	cpu_set_reserved_ttbr0();
1249e8e865bSMark Rutland 	local_flush_tlb_all();
1259e8e865bSMark Rutland 	cpu_set_default_tcr_t0sz();
1269e8e865bSMark Rutland 
12739bc88e5SCatalin Marinas 	if (mm != &init_mm && !system_uses_ttbr0_pan())
1289e8e865bSMark Rutland 		cpu_switch_mm(mm->pgd, mm);
1299e8e865bSMark Rutland }
1309e8e865bSMark Rutland 
131609116d2SMark Rutland static inline void cpu_install_idmap(void)
132609116d2SMark Rutland {
133609116d2SMark Rutland 	cpu_set_reserved_ttbr0();
134609116d2SMark Rutland 	local_flush_tlb_all();
135609116d2SMark Rutland 	cpu_set_idmap_tcr_t0sz();
136609116d2SMark Rutland 
1372077be67SLaura Abbott 	cpu_switch_mm(lm_alias(idmap_pg_dir), &init_mm);
138609116d2SMark Rutland }
139609116d2SMark Rutland 
1409e8e865bSMark Rutland /*
14150e1881dSMark Rutland  * Atomically replaces the active TTBR1_EL1 PGD with a new VA-compatible PGD,
14250e1881dSMark Rutland  * avoiding the possibility of conflicting TLB entries being allocated.
14350e1881dSMark Rutland  */
14420a004e7SWill Deacon static inline void cpu_replace_ttbr1(pgd_t *pgdp)
14550e1881dSMark Rutland {
14650e1881dSMark Rutland 	typedef void (ttbr_replace_func)(phys_addr_t);
14750e1881dSMark Rutland 	extern ttbr_replace_func idmap_cpu_replace_ttbr1;
14850e1881dSMark Rutland 	ttbr_replace_func *replace_phys;
14950e1881dSMark Rutland 
150*5ffdfaedSVladimir Murzin 	/* phys_to_ttbr() zeros lower 2 bits of ttbr with 52-bit PA */
151*5ffdfaedSVladimir Murzin 	phys_addr_t ttbr1 = phys_to_ttbr(virt_to_phys(pgdp));
152*5ffdfaedSVladimir Murzin 
153*5ffdfaedSVladimir Murzin 	if (system_supports_cnp() && !WARN_ON(pgdp != lm_alias(swapper_pg_dir))) {
154*5ffdfaedSVladimir Murzin 		/*
155*5ffdfaedSVladimir Murzin 		 * cpu_replace_ttbr1() is used when there's a boot CPU
156*5ffdfaedSVladimir Murzin 		 * up (i.e. cpufeature framework is not up yet) and
157*5ffdfaedSVladimir Murzin 		 * latter only when we enable CNP via cpufeature's
158*5ffdfaedSVladimir Murzin 		 * enable() callback.
159*5ffdfaedSVladimir Murzin 		 * Also we rely on the cpu_hwcap bit being set before
160*5ffdfaedSVladimir Murzin 		 * calling the enable() function.
161*5ffdfaedSVladimir Murzin 		 */
162*5ffdfaedSVladimir Murzin 		ttbr1 |= TTBR_CNP_BIT;
163*5ffdfaedSVladimir Murzin 	}
16450e1881dSMark Rutland 
1652077be67SLaura Abbott 	replace_phys = (void *)__pa_symbol(idmap_cpu_replace_ttbr1);
16650e1881dSMark Rutland 
16750e1881dSMark Rutland 	cpu_install_idmap();
168*5ffdfaedSVladimir Murzin 	replace_phys(ttbr1);
16950e1881dSMark Rutland 	cpu_uninstall_idmap();
17050e1881dSMark Rutland }
17150e1881dSMark Rutland 
17250e1881dSMark Rutland /*
1735aec715dSWill Deacon  * It would be nice to return ASIDs back to the allocator, but unfortunately
1745aec715dSWill Deacon  * that introduces a race with a generation rollover where we could erroneously
1755aec715dSWill Deacon  * free an ASID allocated in a future generation. We could workaround this by
1765aec715dSWill Deacon  * freeing the ASID from the context of the dying mm (e.g. in arch_exit_mmap),
1775aec715dSWill Deacon  * but we'd then need to make sure that we didn't dirty any TLBs afterwards.
1785aec715dSWill Deacon  * Setting a reserved TTBR0 or EPD0 would work, but it all gets ugly when you
1795aec715dSWill Deacon  * take CPU migration into account.
180b3901d54SCatalin Marinas  */
181b3901d54SCatalin Marinas #define destroy_context(mm)		do { } while(0)
1825aec715dSWill Deacon void check_and_switch_context(struct mm_struct *mm, unsigned int cpu);
183b3901d54SCatalin Marinas 
18465da0a8eSArd Biesheuvel #define init_new_context(tsk,mm)	({ atomic64_set(&(mm)->context.id, 0); 0; })
185b3901d54SCatalin Marinas 
18639bc88e5SCatalin Marinas #ifdef CONFIG_ARM64_SW_TTBR0_PAN
18739bc88e5SCatalin Marinas static inline void update_saved_ttbr0(struct task_struct *tsk,
18839bc88e5SCatalin Marinas 				      struct mm_struct *mm)
18939bc88e5SCatalin Marinas {
1900adbdfdeSWill Deacon 	u64 ttbr;
1910adbdfdeSWill Deacon 
1920adbdfdeSWill Deacon 	if (!system_uses_ttbr0_pan())
1930adbdfdeSWill Deacon 		return;
1940adbdfdeSWill Deacon 
1950adbdfdeSWill Deacon 	if (mm == &init_mm)
1960adbdfdeSWill Deacon 		ttbr = __pa_symbol(empty_zero_page);
1970adbdfdeSWill Deacon 	else
1980adbdfdeSWill Deacon 		ttbr = virt_to_phys(mm->pgd) | ASID(mm) << 48;
1990adbdfdeSWill Deacon 
2006b88a32cSCatalin Marinas 	WRITE_ONCE(task_thread_info(tsk)->ttbr0, ttbr);
20139bc88e5SCatalin Marinas }
20239bc88e5SCatalin Marinas #else
20339bc88e5SCatalin Marinas static inline void update_saved_ttbr0(struct task_struct *tsk,
20439bc88e5SCatalin Marinas 				      struct mm_struct *mm)
20539bc88e5SCatalin Marinas {
20639bc88e5SCatalin Marinas }
20739bc88e5SCatalin Marinas #endif
20839bc88e5SCatalin Marinas 
209d96cc49bSWill Deacon static inline void
210d96cc49bSWill Deacon enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
211d96cc49bSWill Deacon {
212d96cc49bSWill Deacon 	/*
213d96cc49bSWill Deacon 	 * We don't actually care about the ttbr0 mapping, so point it at the
214d96cc49bSWill Deacon 	 * zero page.
215d96cc49bSWill Deacon 	 */
216d96cc49bSWill Deacon 	update_saved_ttbr0(tsk, &init_mm);
217d96cc49bSWill Deacon }
218d96cc49bSWill Deacon 
21939bc88e5SCatalin Marinas static inline void __switch_mm(struct mm_struct *next)
220b3901d54SCatalin Marinas {
221b3901d54SCatalin Marinas 	unsigned int cpu = smp_processor_id();
222b3901d54SCatalin Marinas 
223e53f21bcSCatalin Marinas 	/*
224e53f21bcSCatalin Marinas 	 * init_mm.pgd does not contain any user mappings and it is always
225e53f21bcSCatalin Marinas 	 * active for kernel addresses in TTBR1. Just set the reserved TTBR0.
226e53f21bcSCatalin Marinas 	 */
227e53f21bcSCatalin Marinas 	if (next == &init_mm) {
228e53f21bcSCatalin Marinas 		cpu_set_reserved_ttbr0();
229e53f21bcSCatalin Marinas 		return;
230e53f21bcSCatalin Marinas 	}
231e53f21bcSCatalin Marinas 
232c2775b2eSWill Deacon 	check_and_switch_context(next, cpu);
233b3901d54SCatalin Marinas }
234b3901d54SCatalin Marinas 
23539bc88e5SCatalin Marinas static inline void
23639bc88e5SCatalin Marinas switch_mm(struct mm_struct *prev, struct mm_struct *next,
23739bc88e5SCatalin Marinas 	  struct task_struct *tsk)
23839bc88e5SCatalin Marinas {
23939bc88e5SCatalin Marinas 	if (prev != next)
24039bc88e5SCatalin Marinas 		__switch_mm(next);
24139bc88e5SCatalin Marinas 
24239bc88e5SCatalin Marinas 	/*
24339bc88e5SCatalin Marinas 	 * Update the saved TTBR0_EL1 of the scheduled-in task as the previous
24439bc88e5SCatalin Marinas 	 * value may have not been initialised yet (activate_mm caller) or the
24539bc88e5SCatalin Marinas 	 * ASID has changed since the last run (following the context switch
2460adbdfdeSWill Deacon 	 * of another thread of the same process).
24739bc88e5SCatalin Marinas 	 */
24839bc88e5SCatalin Marinas 	update_saved_ttbr0(tsk, next);
24939bc88e5SCatalin Marinas }
25039bc88e5SCatalin Marinas 
251b3901d54SCatalin Marinas #define deactivate_mm(tsk,mm)	do { } while (0)
25239bc88e5SCatalin Marinas #define activate_mm(prev,next)	switch_mm(prev, next, current)
253b3901d54SCatalin Marinas 
25413f417f3SSuzuki K Poulose void verify_cpu_asid_bits(void);
2556b88a32cSCatalin Marinas void post_ttbr_update_workaround(void);
25613f417f3SSuzuki K Poulose 
25738fd94b0SChristopher Covington #endif /* !__ASSEMBLY__ */
25838fd94b0SChristopher Covington 
25938fd94b0SChristopher Covington #endif /* !__ASM_MMU_CONTEXT_H */
260