xref: /openbmc/linux/arch/arm64/include/asm/mmu_context.h (revision 609116d202a8c5fd3fe393eb85373cbee906df68)
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 
22b3901d54SCatalin Marinas #include <linux/compiler.h>
23b3901d54SCatalin Marinas #include <linux/sched.h>
24b3901d54SCatalin Marinas 
25b3901d54SCatalin Marinas #include <asm/cacheflush.h>
26b3901d54SCatalin Marinas #include <asm/proc-fns.h>
27b3901d54SCatalin Marinas #include <asm-generic/mm_hooks.h>
28b3901d54SCatalin Marinas #include <asm/cputype.h>
29b3901d54SCatalin Marinas #include <asm/pgtable.h>
309e8e865bSMark Rutland #include <asm/tlbflush.h>
31b3901d54SCatalin Marinas 
32ec45d1cfSWill Deacon #ifdef CONFIG_PID_IN_CONTEXTIDR
33ec45d1cfSWill Deacon static inline void contextidr_thread_switch(struct task_struct *next)
34ec45d1cfSWill Deacon {
35ec45d1cfSWill Deacon 	asm(
36ec45d1cfSWill Deacon 	"	msr	contextidr_el1, %0\n"
37ec45d1cfSWill Deacon 	"	isb"
38ec45d1cfSWill Deacon 	:
39ec45d1cfSWill Deacon 	: "r" (task_pid_nr(next)));
40ec45d1cfSWill Deacon }
41ec45d1cfSWill Deacon #else
42ec45d1cfSWill Deacon static inline void contextidr_thread_switch(struct task_struct *next)
43ec45d1cfSWill Deacon {
44ec45d1cfSWill Deacon }
45ec45d1cfSWill Deacon #endif
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 {
525227cfa7SMark Rutland 	unsigned long ttbr = virt_to_phys(empty_zero_page);
53b3901d54SCatalin Marinas 
54b3901d54SCatalin Marinas 	asm(
55b3901d54SCatalin Marinas 	"	msr	ttbr0_el1, %0			// set TTBR0\n"
56b3901d54SCatalin Marinas 	"	isb"
57b3901d54SCatalin Marinas 	:
58b3901d54SCatalin Marinas 	: "r" (ttbr));
59b3901d54SCatalin Marinas }
60b3901d54SCatalin Marinas 
61dd006da2SArd Biesheuvel /*
62dd006da2SArd Biesheuvel  * TCR.T0SZ value to use when the ID map is active. Usually equals
63dd006da2SArd Biesheuvel  * TCR_T0SZ(VA_BITS), unless system RAM is positioned very high in
64dd006da2SArd Biesheuvel  * physical memory, in which case it will be smaller.
65dd006da2SArd Biesheuvel  */
66dd006da2SArd Biesheuvel extern u64 idmap_t0sz;
67dd006da2SArd Biesheuvel 
68dd006da2SArd Biesheuvel static inline bool __cpu_uses_extended_idmap(void)
69dd006da2SArd Biesheuvel {
70dd006da2SArd Biesheuvel 	return (!IS_ENABLED(CONFIG_ARM64_VA_BITS_48) &&
71dd006da2SArd Biesheuvel 		unlikely(idmap_t0sz != TCR_T0SZ(VA_BITS)));
72dd006da2SArd Biesheuvel }
73dd006da2SArd Biesheuvel 
74c51e97d8SWill Deacon /*
75c51e97d8SWill Deacon  * Set TCR.T0SZ to its default value (based on VA_BITS)
76c51e97d8SWill Deacon  */
77*609116d2SMark Rutland static inline void __cpu_set_tcr_t0sz(unsigned long t0sz)
78dd006da2SArd Biesheuvel {
79dd006da2SArd Biesheuvel 	unsigned long tcr;
80dd006da2SArd Biesheuvel 
81c51e97d8SWill Deacon 	if (!__cpu_uses_extended_idmap())
82c51e97d8SWill Deacon 		return;
83c51e97d8SWill Deacon 
84dd006da2SArd Biesheuvel 	asm volatile (
85dd006da2SArd Biesheuvel 	"	mrs	%0, tcr_el1	;"
86dd006da2SArd Biesheuvel 	"	bfi	%0, %1, %2, %3	;"
87dd006da2SArd Biesheuvel 	"	msr	tcr_el1, %0	;"
88dd006da2SArd Biesheuvel 	"	isb"
89dd006da2SArd Biesheuvel 	: "=&r" (tcr)
90*609116d2SMark Rutland 	: "r"(t0sz), "I"(TCR_T0SZ_OFFSET), "I"(TCR_TxSZ_WIDTH));
91dd006da2SArd Biesheuvel }
92dd006da2SArd Biesheuvel 
93*609116d2SMark Rutland #define cpu_set_default_tcr_t0sz()	__cpu_set_tcr_t0sz(TCR_T0SZ(VA_BITS))
94*609116d2SMark Rutland #define cpu_set_idmap_tcr_t0sz()	__cpu_set_tcr_t0sz(idmap_t0sz)
95*609116d2SMark Rutland 
96b3901d54SCatalin Marinas /*
979e8e865bSMark Rutland  * Remove the idmap from TTBR0_EL1 and install the pgd of the active mm.
989e8e865bSMark Rutland  *
999e8e865bSMark Rutland  * The idmap lives in the same VA range as userspace, but uses global entries
1009e8e865bSMark Rutland  * and may use a different TCR_EL1.T0SZ. To avoid issues resulting from
1019e8e865bSMark Rutland  * speculative TLB fetches, we must temporarily install the reserved page
1029e8e865bSMark Rutland  * tables while we invalidate the TLBs and set up the correct TCR_EL1.T0SZ.
1039e8e865bSMark Rutland  *
1049e8e865bSMark Rutland  * If current is a not a user task, the mm covers the TTBR1_EL1 page tables,
1059e8e865bSMark Rutland  * which should not be installed in TTBR0_EL1. In this case we can leave the
1069e8e865bSMark Rutland  * reserved page tables in place.
1079e8e865bSMark Rutland  */
1089e8e865bSMark Rutland static inline void cpu_uninstall_idmap(void)
1099e8e865bSMark Rutland {
1109e8e865bSMark Rutland 	struct mm_struct *mm = current->active_mm;
1119e8e865bSMark Rutland 
1129e8e865bSMark Rutland 	cpu_set_reserved_ttbr0();
1139e8e865bSMark Rutland 	local_flush_tlb_all();
1149e8e865bSMark Rutland 	cpu_set_default_tcr_t0sz();
1159e8e865bSMark Rutland 
1169e8e865bSMark Rutland 	if (mm != &init_mm)
1179e8e865bSMark Rutland 		cpu_switch_mm(mm->pgd, mm);
1189e8e865bSMark Rutland }
1199e8e865bSMark Rutland 
120*609116d2SMark Rutland static inline void cpu_install_idmap(void)
121*609116d2SMark Rutland {
122*609116d2SMark Rutland 	cpu_set_reserved_ttbr0();
123*609116d2SMark Rutland 	local_flush_tlb_all();
124*609116d2SMark Rutland 	cpu_set_idmap_tcr_t0sz();
125*609116d2SMark Rutland 
126*609116d2SMark Rutland 	cpu_switch_mm(idmap_pg_dir, &init_mm);
127*609116d2SMark Rutland }
128*609116d2SMark Rutland 
1299e8e865bSMark Rutland /*
1305aec715dSWill Deacon  * It would be nice to return ASIDs back to the allocator, but unfortunately
1315aec715dSWill Deacon  * that introduces a race with a generation rollover where we could erroneously
1325aec715dSWill Deacon  * free an ASID allocated in a future generation. We could workaround this by
1335aec715dSWill Deacon  * freeing the ASID from the context of the dying mm (e.g. in arch_exit_mmap),
1345aec715dSWill Deacon  * but we'd then need to make sure that we didn't dirty any TLBs afterwards.
1355aec715dSWill Deacon  * Setting a reserved TTBR0 or EPD0 would work, but it all gets ugly when you
1365aec715dSWill Deacon  * take CPU migration into account.
137b3901d54SCatalin Marinas  */
138b3901d54SCatalin Marinas #define destroy_context(mm)		do { } while(0)
1395aec715dSWill Deacon void check_and_switch_context(struct mm_struct *mm, unsigned int cpu);
140b3901d54SCatalin Marinas 
14165da0a8eSArd Biesheuvel #define init_new_context(tsk,mm)	({ atomic64_set(&(mm)->context.id, 0); 0; })
142b3901d54SCatalin Marinas 
143b3901d54SCatalin Marinas /*
144b3901d54SCatalin Marinas  * This is called when "tsk" is about to enter lazy TLB mode.
145b3901d54SCatalin Marinas  *
146b3901d54SCatalin Marinas  * mm:  describes the currently active mm context
147b3901d54SCatalin Marinas  * tsk: task which is entering lazy tlb
148b3901d54SCatalin Marinas  * cpu: cpu number which is entering lazy tlb
149b3901d54SCatalin Marinas  *
150b3901d54SCatalin Marinas  * tsk->mm will be NULL
151b3901d54SCatalin Marinas  */
152b3901d54SCatalin Marinas static inline void
153b3901d54SCatalin Marinas enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
154b3901d54SCatalin Marinas {
155b3901d54SCatalin Marinas }
156b3901d54SCatalin Marinas 
157b3901d54SCatalin Marinas /*
158b3901d54SCatalin Marinas  * This is the actual mm switch as far as the scheduler
159b3901d54SCatalin Marinas  * is concerned.  No registers are touched.  We avoid
160b3901d54SCatalin Marinas  * calling the CPU specific function when the mm hasn't
161b3901d54SCatalin Marinas  * actually changed.
162b3901d54SCatalin Marinas  */
163b3901d54SCatalin Marinas static inline void
164b3901d54SCatalin Marinas switch_mm(struct mm_struct *prev, struct mm_struct *next,
165b3901d54SCatalin Marinas 	  struct task_struct *tsk)
166b3901d54SCatalin Marinas {
167b3901d54SCatalin Marinas 	unsigned int cpu = smp_processor_id();
168b3901d54SCatalin Marinas 
169c2775b2eSWill Deacon 	if (prev == next)
170c2775b2eSWill Deacon 		return;
171c2775b2eSWill Deacon 
172e53f21bcSCatalin Marinas 	/*
173e53f21bcSCatalin Marinas 	 * init_mm.pgd does not contain any user mappings and it is always
174e53f21bcSCatalin Marinas 	 * active for kernel addresses in TTBR1. Just set the reserved TTBR0.
175e53f21bcSCatalin Marinas 	 */
176e53f21bcSCatalin Marinas 	if (next == &init_mm) {
177e53f21bcSCatalin Marinas 		cpu_set_reserved_ttbr0();
178e53f21bcSCatalin Marinas 		return;
179e53f21bcSCatalin Marinas 	}
180e53f21bcSCatalin Marinas 
181c2775b2eSWill Deacon 	check_and_switch_context(next, cpu);
182b3901d54SCatalin Marinas }
183b3901d54SCatalin Marinas 
184b3901d54SCatalin Marinas #define deactivate_mm(tsk,mm)	do { } while (0)
185b3901d54SCatalin Marinas #define activate_mm(prev,next)	switch_mm(prev, next, NULL)
186b3901d54SCatalin Marinas 
187b3901d54SCatalin Marinas #endif
188