xref: /openbmc/linux/arch/arm64/include/asm/mmu_context.h (revision 7655abb953860485940d4de74fb45a8192149bb6)
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 #define FALKOR_RESERVED_ASID	1
2338fd94b0SChristopher Covington 
2438fd94b0SChristopher Covington #ifndef __ASSEMBLY__
2538fd94b0SChristopher Covington 
26b3901d54SCatalin Marinas #include <linux/compiler.h>
27b3901d54SCatalin Marinas #include <linux/sched.h>
28ef8bd77fSIngo Molnar #include <linux/sched/hotplug.h>
29589ee628SIngo Molnar #include <linux/mm_types.h>
30b3901d54SCatalin Marinas 
31b3901d54SCatalin Marinas #include <asm/cacheflush.h>
3239bc88e5SCatalin Marinas #include <asm/cpufeature.h>
33b3901d54SCatalin Marinas #include <asm/proc-fns.h>
34b3901d54SCatalin Marinas #include <asm-generic/mm_hooks.h>
35b3901d54SCatalin Marinas #include <asm/cputype.h>
36b3901d54SCatalin Marinas #include <asm/pgtable.h>
37adf75899SMark Rutland #include <asm/sysreg.h>
389e8e865bSMark Rutland #include <asm/tlbflush.h>
39b3901d54SCatalin Marinas 
40ec45d1cfSWill Deacon static inline void contextidr_thread_switch(struct task_struct *next)
41ec45d1cfSWill Deacon {
42d3ea42aaSMark Rutland 	if (!IS_ENABLED(CONFIG_PID_IN_CONTEXTIDR))
43d3ea42aaSMark Rutland 		return;
44d3ea42aaSMark Rutland 
45adf75899SMark Rutland 	write_sysreg(task_pid_nr(next), contextidr_el1);
46adf75899SMark Rutland 	isb();
47ec45d1cfSWill Deacon }
48ec45d1cfSWill Deacon 
49b3901d54SCatalin Marinas /*
50b3901d54SCatalin Marinas  * Set TTBR0 to empty_zero_page. No translations will be possible via TTBR0.
51b3901d54SCatalin Marinas  */
52b3901d54SCatalin Marinas static inline void cpu_set_reserved_ttbr0(void)
53b3901d54SCatalin Marinas {
542077be67SLaura Abbott 	unsigned long ttbr = __pa_symbol(empty_zero_page);
55b3901d54SCatalin Marinas 
56adf75899SMark Rutland 	write_sysreg(ttbr, ttbr0_el1);
57adf75899SMark Rutland 	isb();
58b3901d54SCatalin Marinas }
59b3901d54SCatalin Marinas 
60*7655abb9SWill Deacon static inline void cpu_switch_mm(pgd_t *pgd, struct mm_struct *mm)
61*7655abb9SWill Deacon {
62*7655abb9SWill Deacon 	BUG_ON(pgd == swapper_pg_dir);
63*7655abb9SWill Deacon 	cpu_set_reserved_ttbr0();
64*7655abb9SWill Deacon 	cpu_do_switch_mm(virt_to_phys(pgd),mm);
65*7655abb9SWill Deacon }
66*7655abb9SWill Deacon 
67dd006da2SArd Biesheuvel /*
68dd006da2SArd Biesheuvel  * TCR.T0SZ value to use when the ID map is active. Usually equals
69dd006da2SArd Biesheuvel  * TCR_T0SZ(VA_BITS), unless system RAM is positioned very high in
70dd006da2SArd Biesheuvel  * physical memory, in which case it will be smaller.
71dd006da2SArd Biesheuvel  */
72dd006da2SArd Biesheuvel extern u64 idmap_t0sz;
73dd006da2SArd Biesheuvel 
74dd006da2SArd Biesheuvel static inline bool __cpu_uses_extended_idmap(void)
75dd006da2SArd Biesheuvel {
76dd006da2SArd Biesheuvel 	return (!IS_ENABLED(CONFIG_ARM64_VA_BITS_48) &&
77dd006da2SArd Biesheuvel 		unlikely(idmap_t0sz != TCR_T0SZ(VA_BITS)));
78dd006da2SArd Biesheuvel }
79dd006da2SArd Biesheuvel 
80c51e97d8SWill Deacon /*
81c51e97d8SWill Deacon  * Set TCR.T0SZ to its default value (based on VA_BITS)
82c51e97d8SWill Deacon  */
83609116d2SMark Rutland static inline void __cpu_set_tcr_t0sz(unsigned long t0sz)
84dd006da2SArd Biesheuvel {
85dd006da2SArd Biesheuvel 	unsigned long tcr;
86dd006da2SArd Biesheuvel 
87c51e97d8SWill Deacon 	if (!__cpu_uses_extended_idmap())
88c51e97d8SWill Deacon 		return;
89c51e97d8SWill Deacon 
90adf75899SMark Rutland 	tcr = read_sysreg(tcr_el1);
91adf75899SMark Rutland 	tcr &= ~TCR_T0SZ_MASK;
92adf75899SMark Rutland 	tcr |= t0sz << TCR_T0SZ_OFFSET;
93adf75899SMark Rutland 	write_sysreg(tcr, tcr_el1);
94adf75899SMark Rutland 	isb();
95dd006da2SArd Biesheuvel }
96dd006da2SArd Biesheuvel 
97609116d2SMark Rutland #define cpu_set_default_tcr_t0sz()	__cpu_set_tcr_t0sz(TCR_T0SZ(VA_BITS))
98609116d2SMark Rutland #define cpu_set_idmap_tcr_t0sz()	__cpu_set_tcr_t0sz(idmap_t0sz)
99609116d2SMark Rutland 
100b3901d54SCatalin Marinas /*
1019e8e865bSMark Rutland  * Remove the idmap from TTBR0_EL1 and install the pgd of the active mm.
1029e8e865bSMark Rutland  *
1039e8e865bSMark Rutland  * The idmap lives in the same VA range as userspace, but uses global entries
1049e8e865bSMark Rutland  * and may use a different TCR_EL1.T0SZ. To avoid issues resulting from
1059e8e865bSMark Rutland  * speculative TLB fetches, we must temporarily install the reserved page
1069e8e865bSMark Rutland  * tables while we invalidate the TLBs and set up the correct TCR_EL1.T0SZ.
1079e8e865bSMark Rutland  *
1089e8e865bSMark Rutland  * If current is a not a user task, the mm covers the TTBR1_EL1 page tables,
1099e8e865bSMark Rutland  * which should not be installed in TTBR0_EL1. In this case we can leave the
1109e8e865bSMark Rutland  * reserved page tables in place.
1119e8e865bSMark Rutland  */
1129e8e865bSMark Rutland static inline void cpu_uninstall_idmap(void)
1139e8e865bSMark Rutland {
1149e8e865bSMark Rutland 	struct mm_struct *mm = current->active_mm;
1159e8e865bSMark Rutland 
1169e8e865bSMark Rutland 	cpu_set_reserved_ttbr0();
1179e8e865bSMark Rutland 	local_flush_tlb_all();
1189e8e865bSMark Rutland 	cpu_set_default_tcr_t0sz();
1199e8e865bSMark Rutland 
12039bc88e5SCatalin Marinas 	if (mm != &init_mm && !system_uses_ttbr0_pan())
1219e8e865bSMark Rutland 		cpu_switch_mm(mm->pgd, mm);
1229e8e865bSMark Rutland }
1239e8e865bSMark Rutland 
124609116d2SMark Rutland static inline void cpu_install_idmap(void)
125609116d2SMark Rutland {
126609116d2SMark Rutland 	cpu_set_reserved_ttbr0();
127609116d2SMark Rutland 	local_flush_tlb_all();
128609116d2SMark Rutland 	cpu_set_idmap_tcr_t0sz();
129609116d2SMark Rutland 
1302077be67SLaura Abbott 	cpu_switch_mm(lm_alias(idmap_pg_dir), &init_mm);
131609116d2SMark Rutland }
132609116d2SMark Rutland 
1339e8e865bSMark Rutland /*
13450e1881dSMark Rutland  * Atomically replaces the active TTBR1_EL1 PGD with a new VA-compatible PGD,
13550e1881dSMark Rutland  * avoiding the possibility of conflicting TLB entries being allocated.
13650e1881dSMark Rutland  */
13750e1881dSMark Rutland static inline void cpu_replace_ttbr1(pgd_t *pgd)
13850e1881dSMark Rutland {
13950e1881dSMark Rutland 	typedef void (ttbr_replace_func)(phys_addr_t);
14050e1881dSMark Rutland 	extern ttbr_replace_func idmap_cpu_replace_ttbr1;
14150e1881dSMark Rutland 	ttbr_replace_func *replace_phys;
14250e1881dSMark Rutland 
14350e1881dSMark Rutland 	phys_addr_t pgd_phys = virt_to_phys(pgd);
14450e1881dSMark Rutland 
1452077be67SLaura Abbott 	replace_phys = (void *)__pa_symbol(idmap_cpu_replace_ttbr1);
14650e1881dSMark Rutland 
14750e1881dSMark Rutland 	cpu_install_idmap();
14850e1881dSMark Rutland 	replace_phys(pgd_phys);
14950e1881dSMark Rutland 	cpu_uninstall_idmap();
15050e1881dSMark Rutland }
15150e1881dSMark Rutland 
15250e1881dSMark Rutland /*
1535aec715dSWill Deacon  * It would be nice to return ASIDs back to the allocator, but unfortunately
1545aec715dSWill Deacon  * that introduces a race with a generation rollover where we could erroneously
1555aec715dSWill Deacon  * free an ASID allocated in a future generation. We could workaround this by
1565aec715dSWill Deacon  * freeing the ASID from the context of the dying mm (e.g. in arch_exit_mmap),
1575aec715dSWill Deacon  * but we'd then need to make sure that we didn't dirty any TLBs afterwards.
1585aec715dSWill Deacon  * Setting a reserved TTBR0 or EPD0 would work, but it all gets ugly when you
1595aec715dSWill Deacon  * take CPU migration into account.
160b3901d54SCatalin Marinas  */
161b3901d54SCatalin Marinas #define destroy_context(mm)		do { } while(0)
1625aec715dSWill Deacon void check_and_switch_context(struct mm_struct *mm, unsigned int cpu);
163b3901d54SCatalin Marinas 
16465da0a8eSArd Biesheuvel #define init_new_context(tsk,mm)	({ atomic64_set(&(mm)->context.id, 0); 0; })
165b3901d54SCatalin Marinas 
16639bc88e5SCatalin Marinas #ifdef CONFIG_ARM64_SW_TTBR0_PAN
16739bc88e5SCatalin Marinas static inline void update_saved_ttbr0(struct task_struct *tsk,
16839bc88e5SCatalin Marinas 				      struct mm_struct *mm)
16939bc88e5SCatalin Marinas {
1700adbdfdeSWill Deacon 	u64 ttbr;
1710adbdfdeSWill Deacon 
1720adbdfdeSWill Deacon 	if (!system_uses_ttbr0_pan())
1730adbdfdeSWill Deacon 		return;
1740adbdfdeSWill Deacon 
1750adbdfdeSWill Deacon 	if (mm == &init_mm)
1760adbdfdeSWill Deacon 		ttbr = __pa_symbol(empty_zero_page);
1770adbdfdeSWill Deacon 	else
1780adbdfdeSWill Deacon 		ttbr = virt_to_phys(mm->pgd) | ASID(mm) << 48;
1790adbdfdeSWill Deacon 
1800adbdfdeSWill Deacon 	task_thread_info(tsk)->ttbr0 = ttbr;
18139bc88e5SCatalin Marinas }
18239bc88e5SCatalin Marinas #else
18339bc88e5SCatalin Marinas static inline void update_saved_ttbr0(struct task_struct *tsk,
18439bc88e5SCatalin Marinas 				      struct mm_struct *mm)
18539bc88e5SCatalin Marinas {
18639bc88e5SCatalin Marinas }
18739bc88e5SCatalin Marinas #endif
18839bc88e5SCatalin Marinas 
189d96cc49bSWill Deacon static inline void
190d96cc49bSWill Deacon enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
191d96cc49bSWill Deacon {
192d96cc49bSWill Deacon 	/*
193d96cc49bSWill Deacon 	 * We don't actually care about the ttbr0 mapping, so point it at the
194d96cc49bSWill Deacon 	 * zero page.
195d96cc49bSWill Deacon 	 */
196d96cc49bSWill Deacon 	update_saved_ttbr0(tsk, &init_mm);
197d96cc49bSWill Deacon }
198d96cc49bSWill Deacon 
19939bc88e5SCatalin Marinas static inline void __switch_mm(struct mm_struct *next)
200b3901d54SCatalin Marinas {
201b3901d54SCatalin Marinas 	unsigned int cpu = smp_processor_id();
202b3901d54SCatalin Marinas 
203e53f21bcSCatalin Marinas 	/*
204e53f21bcSCatalin Marinas 	 * init_mm.pgd does not contain any user mappings and it is always
205e53f21bcSCatalin Marinas 	 * active for kernel addresses in TTBR1. Just set the reserved TTBR0.
206e53f21bcSCatalin Marinas 	 */
207e53f21bcSCatalin Marinas 	if (next == &init_mm) {
208e53f21bcSCatalin Marinas 		cpu_set_reserved_ttbr0();
209e53f21bcSCatalin Marinas 		return;
210e53f21bcSCatalin Marinas 	}
211e53f21bcSCatalin Marinas 
212c2775b2eSWill Deacon 	check_and_switch_context(next, cpu);
213b3901d54SCatalin Marinas }
214b3901d54SCatalin Marinas 
21539bc88e5SCatalin Marinas static inline void
21639bc88e5SCatalin Marinas switch_mm(struct mm_struct *prev, struct mm_struct *next,
21739bc88e5SCatalin Marinas 	  struct task_struct *tsk)
21839bc88e5SCatalin Marinas {
21939bc88e5SCatalin Marinas 	if (prev != next)
22039bc88e5SCatalin Marinas 		__switch_mm(next);
22139bc88e5SCatalin Marinas 
22239bc88e5SCatalin Marinas 	/*
22339bc88e5SCatalin Marinas 	 * Update the saved TTBR0_EL1 of the scheduled-in task as the previous
22439bc88e5SCatalin Marinas 	 * value may have not been initialised yet (activate_mm caller) or the
22539bc88e5SCatalin Marinas 	 * ASID has changed since the last run (following the context switch
2260adbdfdeSWill Deacon 	 * of another thread of the same process).
22739bc88e5SCatalin Marinas 	 */
22839bc88e5SCatalin Marinas 	update_saved_ttbr0(tsk, next);
22939bc88e5SCatalin Marinas }
23039bc88e5SCatalin Marinas 
231b3901d54SCatalin Marinas #define deactivate_mm(tsk,mm)	do { } while (0)
23239bc88e5SCatalin Marinas #define activate_mm(prev,next)	switch_mm(prev, next, current)
233b3901d54SCatalin Marinas 
23413f417f3SSuzuki K Poulose void verify_cpu_asid_bits(void);
23513f417f3SSuzuki K Poulose 
23638fd94b0SChristopher Covington #endif /* !__ASSEMBLY__ */
23738fd94b0SChristopher Covington 
23838fd94b0SChristopher Covington #endif /* !__ASM_MMU_CONTEXT_H */
239