xref: /linux/arch/powerpc/mm/mmu_context.c (revision 177255af)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
23a2df379SBenjamin Herrenschmidt /*
33a2df379SBenjamin Herrenschmidt  *  Common implementation of switch_mm_irqs_off
43a2df379SBenjamin Herrenschmidt  *
53a2df379SBenjamin Herrenschmidt  *  Copyright IBM Corp. 2017
63a2df379SBenjamin Herrenschmidt  */
73a2df379SBenjamin Herrenschmidt 
83a2df379SBenjamin Herrenschmidt #include <linux/mm.h>
93a2df379SBenjamin Herrenschmidt #include <linux/cpu.h>
103ccfebedSMathieu Desnoyers #include <linux/sched/mm.h>
113a2df379SBenjamin Herrenschmidt 
123a2df379SBenjamin Herrenschmidt #include <asm/mmu_context.h>
1332ea4c14SChristophe Leroy #include <asm/pgalloc.h>
143a2df379SBenjamin Herrenschmidt 
153a2df379SBenjamin Herrenschmidt #if defined(CONFIG_PPC32)
switch_mm_pgdir(struct task_struct * tsk,struct mm_struct * mm)163a2df379SBenjamin Herrenschmidt static inline void switch_mm_pgdir(struct task_struct *tsk,
173a2df379SBenjamin Herrenschmidt 				   struct mm_struct *mm)
183a2df379SBenjamin Herrenschmidt {
193a2df379SBenjamin Herrenschmidt 	/* 32-bit keeps track of the current PGDIR in the thread struct */
203a2df379SBenjamin Herrenschmidt 	tsk->thread.pgdir = mm->pgd;
2170428da9SChristophe Leroy #ifdef CONFIG_PPC_BOOK3S_32
2270428da9SChristophe Leroy 	tsk->thread.sr0 = mm->context.sr0;
2370428da9SChristophe Leroy #endif
2443afcf8fSChristophe Leroy #if defined(CONFIG_BOOKE_OR_40x) && defined(CONFIG_PPC_KUAP)
2543afcf8fSChristophe Leroy 	tsk->thread.pid = mm->context.id;
2643afcf8fSChristophe Leroy #endif
273a2df379SBenjamin Herrenschmidt }
283a2df379SBenjamin Herrenschmidt #elif defined(CONFIG_PPC_BOOK3E_64)
switch_mm_pgdir(struct task_struct * tsk,struct mm_struct * mm)293a2df379SBenjamin Herrenschmidt static inline void switch_mm_pgdir(struct task_struct *tsk,
303a2df379SBenjamin Herrenschmidt 				   struct mm_struct *mm)
313a2df379SBenjamin Herrenschmidt {
323a2df379SBenjamin Herrenschmidt 	/* 64-bit Book3E keeps track of current PGD in the PACA */
333a2df379SBenjamin Herrenschmidt 	get_paca()->pgd = mm->pgd;
3443afcf8fSChristophe Leroy #ifdef CONFIG_PPC_KUAP
3543afcf8fSChristophe Leroy 	tsk->thread.pid = mm->context.id;
3643afcf8fSChristophe Leroy #endif
373a2df379SBenjamin Herrenschmidt }
383a2df379SBenjamin Herrenschmidt #else
switch_mm_pgdir(struct task_struct * tsk,struct mm_struct * mm)393a2df379SBenjamin Herrenschmidt static inline void switch_mm_pgdir(struct task_struct *tsk,
403a2df379SBenjamin Herrenschmidt 				   struct mm_struct *mm) { }
413a2df379SBenjamin Herrenschmidt #endif
423a2df379SBenjamin Herrenschmidt 
switch_mm_irqs_off(struct mm_struct * prev,struct mm_struct * next,struct task_struct * tsk)433a2df379SBenjamin Herrenschmidt void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
443a2df379SBenjamin Herrenschmidt 			struct task_struct *tsk)
453a2df379SBenjamin Herrenschmidt {
46*177255afSNicholas Piggin 	int cpu = smp_processor_id();
473a2df379SBenjamin Herrenschmidt 	bool new_on_cpu = false;
483a2df379SBenjamin Herrenschmidt 
493a2df379SBenjamin Herrenschmidt 	/* Mark this context has been used on the new CPU */
50*177255afSNicholas Piggin 	if (!cpumask_test_cpu(cpu, mm_cpumask(next))) {
51c3c2e937SNicholas Piggin 		VM_WARN_ON_ONCE(next == &init_mm);
52*177255afSNicholas Piggin 		cpumask_set_cpu(cpu, mm_cpumask(next));
533a2df379SBenjamin Herrenschmidt 		inc_mm_active_cpus(next);
543a2df379SBenjamin Herrenschmidt 
553a2df379SBenjamin Herrenschmidt 		/*
563a2df379SBenjamin Herrenschmidt 		 * This full barrier orders the store to the cpumask above vs
570f197ddcSNicholas Piggin 		 * a subsequent load which allows this CPU/MMU to begin loading
580f197ddcSNicholas Piggin 		 * translations for 'next' from page table PTEs into the TLB.
593a2df379SBenjamin Herrenschmidt 		 *
600f197ddcSNicholas Piggin 		 * When using the radix MMU, that operation is the load of the
613a2df379SBenjamin Herrenschmidt 		 * MMU context id, which is then moved to SPRN_PID.
623a2df379SBenjamin Herrenschmidt 		 *
633a2df379SBenjamin Herrenschmidt 		 * For the hash MMU it is either the first load from slb_cache
640f197ddcSNicholas Piggin 		 * in switch_slb() to preload the SLBs, or the load of
650f197ddcSNicholas Piggin 		 * get_user_context which loads the context for the VSID hash
660f197ddcSNicholas Piggin 		 * to insert a new SLB, in the SLB fault handler.
673a2df379SBenjamin Herrenschmidt 		 *
6885bcfaf6SNicholas Piggin 		 * On the other side, the barrier is in mm/tlb-radix.c for
690f197ddcSNicholas Piggin 		 * radix which orders earlier stores to clear the PTEs before
700f197ddcSNicholas Piggin 		 * the load of mm_cpumask to check which CPU TLBs should be
710f197ddcSNicholas Piggin 		 * flushed. For hash, pte_xchg to clear the PTE includes the
720f197ddcSNicholas Piggin 		 * barrier.
733ccfebedSMathieu Desnoyers 		 *
740f197ddcSNicholas Piggin 		 * This full barrier is also needed by membarrier when
750f197ddcSNicholas Piggin 		 * switching between processes after store to rq->curr, before
760f197ddcSNicholas Piggin 		 * user-space memory accesses.
773a2df379SBenjamin Herrenschmidt 		 */
783a2df379SBenjamin Herrenschmidt 		smp_mb();
793a2df379SBenjamin Herrenschmidt 
803a2df379SBenjamin Herrenschmidt 		new_on_cpu = true;
813a2df379SBenjamin Herrenschmidt 	}
823a2df379SBenjamin Herrenschmidt 
833a2df379SBenjamin Herrenschmidt 	/* Some subarchs need to track the PGD elsewhere */
843a2df379SBenjamin Herrenschmidt 	switch_mm_pgdir(tsk, next);
853a2df379SBenjamin Herrenschmidt 
863a2df379SBenjamin Herrenschmidt 	/* Nothing else to do if we aren't actually switching */
873a2df379SBenjamin Herrenschmidt 	if (prev == next)
883a2df379SBenjamin Herrenschmidt 		return;
893a2df379SBenjamin Herrenschmidt 
903a2df379SBenjamin Herrenschmidt 	/*
913a2df379SBenjamin Herrenschmidt 	 * We must stop all altivec streams before changing the HW
923a2df379SBenjamin Herrenschmidt 	 * context
933a2df379SBenjamin Herrenschmidt 	 */
943a2df379SBenjamin Herrenschmidt 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
95d51f86cfSAlexey Kardashevskiy 		asm volatile (PPC_DSSALL);
963a2df379SBenjamin Herrenschmidt 
972e1ae9cdSNicholas Piggin 	if (!new_on_cpu)
983ccfebedSMathieu Desnoyers 		membarrier_arch_switch_mm(prev, next, tsk);
993a2df379SBenjamin Herrenschmidt 
1003a2df379SBenjamin Herrenschmidt 	/*
1013a2df379SBenjamin Herrenschmidt 	 * The actual HW switching method differs between the various
1023a2df379SBenjamin Herrenschmidt 	 * sub architectures. Out of line for now
1033a2df379SBenjamin Herrenschmidt 	 */
1043a2df379SBenjamin Herrenschmidt 	switch_mmu_context(prev, next, tsk);
105*177255afSNicholas Piggin 
106*177255afSNicholas Piggin 	VM_WARN_ON_ONCE(!cpumask_test_cpu(cpu, mm_cpumask(prev)));
1073a2df379SBenjamin Herrenschmidt }
1083a2df379SBenjamin Herrenschmidt 
109737b434dSChristophe Leroy #ifndef CONFIG_PPC_BOOK3S_64
arch_exit_mmap(struct mm_struct * mm)11032ea4c14SChristophe Leroy void arch_exit_mmap(struct mm_struct *mm)
11132ea4c14SChristophe Leroy {
11232ea4c14SChristophe Leroy 	void *frag = pte_frag_get(&mm->context);
11332ea4c14SChristophe Leroy 
11432ea4c14SChristophe Leroy 	if (frag)
11532ea4c14SChristophe Leroy 		pte_frag_destroy(frag);
11632ea4c14SChristophe Leroy }
11732ea4c14SChristophe Leroy #endif
118