162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Common implementation of switch_mm_irqs_off 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright IBM Corp. 2017 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci#include <linux/mm.h> 962306a36Sopenharmony_ci#include <linux/cpu.h> 1062306a36Sopenharmony_ci#include <linux/sched/mm.h> 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ci#include <asm/mmu_context.h> 1362306a36Sopenharmony_ci#include <asm/pgalloc.h> 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#if defined(CONFIG_PPC32) 1662306a36Sopenharmony_cistatic inline void switch_mm_pgdir(struct task_struct *tsk, 1762306a36Sopenharmony_ci struct mm_struct *mm) 1862306a36Sopenharmony_ci{ 1962306a36Sopenharmony_ci /* 32-bit keeps track of the current PGDIR in the thread struct */ 2062306a36Sopenharmony_ci tsk->thread.pgdir = mm->pgd; 2162306a36Sopenharmony_ci#ifdef CONFIG_PPC_BOOK3S_32 2262306a36Sopenharmony_ci tsk->thread.sr0 = mm->context.sr0; 2362306a36Sopenharmony_ci#endif 2462306a36Sopenharmony_ci#if defined(CONFIG_BOOKE_OR_40x) && defined(CONFIG_PPC_KUAP) 2562306a36Sopenharmony_ci tsk->thread.pid = mm->context.id; 2662306a36Sopenharmony_ci#endif 2762306a36Sopenharmony_ci} 2862306a36Sopenharmony_ci#elif defined(CONFIG_PPC_BOOK3E_64) 2962306a36Sopenharmony_cistatic inline void switch_mm_pgdir(struct task_struct *tsk, 3062306a36Sopenharmony_ci struct mm_struct *mm) 3162306a36Sopenharmony_ci{ 3262306a36Sopenharmony_ci /* 64-bit Book3E keeps track of current PGD in the PACA */ 3362306a36Sopenharmony_ci get_paca()->pgd = mm->pgd; 3462306a36Sopenharmony_ci#ifdef CONFIG_PPC_KUAP 3562306a36Sopenharmony_ci tsk->thread.pid = mm->context.id; 3662306a36Sopenharmony_ci#endif 3762306a36Sopenharmony_ci} 3862306a36Sopenharmony_ci#else 3962306a36Sopenharmony_cistatic inline void switch_mm_pgdir(struct task_struct *tsk, 4062306a36Sopenharmony_ci struct mm_struct *mm) { } 4162306a36Sopenharmony_ci#endif 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_civoid switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next, 4462306a36Sopenharmony_ci struct task_struct *tsk) 4562306a36Sopenharmony_ci{ 4662306a36Sopenharmony_ci int cpu = smp_processor_id(); 4762306a36Sopenharmony_ci bool new_on_cpu = false; 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci /* Mark this context has been used on the new CPU */ 5062306a36Sopenharmony_ci if (!cpumask_test_cpu(cpu, mm_cpumask(next))) { 5162306a36Sopenharmony_ci VM_WARN_ON_ONCE(next == &init_mm); 5262306a36Sopenharmony_ci cpumask_set_cpu(cpu, mm_cpumask(next)); 5362306a36Sopenharmony_ci inc_mm_active_cpus(next); 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci /* 5662306a36Sopenharmony_ci * This full barrier orders the store to the cpumask above vs 5762306a36Sopenharmony_ci * a subsequent load which allows this CPU/MMU to begin loading 5862306a36Sopenharmony_ci * translations for 'next' from page table PTEs into the TLB. 5962306a36Sopenharmony_ci * 6062306a36Sopenharmony_ci * When using the radix MMU, that operation is the load of the 6162306a36Sopenharmony_ci * MMU context id, which is then moved to SPRN_PID. 6262306a36Sopenharmony_ci * 6362306a36Sopenharmony_ci * For the hash MMU it is either the first load from slb_cache 6462306a36Sopenharmony_ci * in switch_slb() to preload the SLBs, or the load of 6562306a36Sopenharmony_ci * get_user_context which loads the context for the VSID hash 6662306a36Sopenharmony_ci * to insert a new SLB, in the SLB fault handler. 6762306a36Sopenharmony_ci * 6862306a36Sopenharmony_ci * On the other side, the barrier is in mm/tlb-radix.c for 6962306a36Sopenharmony_ci * radix which orders earlier stores to clear the PTEs before 7062306a36Sopenharmony_ci * the load of mm_cpumask to check which CPU TLBs should be 7162306a36Sopenharmony_ci * flushed. For hash, pte_xchg to clear the PTE includes the 7262306a36Sopenharmony_ci * barrier. 7362306a36Sopenharmony_ci * 7462306a36Sopenharmony_ci * This full barrier is also needed by membarrier when 7562306a36Sopenharmony_ci * switching between processes after store to rq->curr, before 7662306a36Sopenharmony_ci * user-space memory accesses. 7762306a36Sopenharmony_ci */ 7862306a36Sopenharmony_ci smp_mb(); 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci new_on_cpu = true; 8162306a36Sopenharmony_ci } 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci /* Some subarchs need to track the PGD elsewhere */ 8462306a36Sopenharmony_ci switch_mm_pgdir(tsk, next); 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci /* Nothing else to do if we aren't actually switching */ 8762306a36Sopenharmony_ci if (prev == next) 8862306a36Sopenharmony_ci return; 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci /* 9162306a36Sopenharmony_ci * We must stop all altivec streams before changing the HW 9262306a36Sopenharmony_ci * context 9362306a36Sopenharmony_ci */ 9462306a36Sopenharmony_ci if (cpu_has_feature(CPU_FTR_ALTIVEC)) 9562306a36Sopenharmony_ci asm volatile (PPC_DSSALL); 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ci if (!new_on_cpu) 9862306a36Sopenharmony_ci membarrier_arch_switch_mm(prev, next, tsk); 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci /* 10162306a36Sopenharmony_ci * The actual HW switching method differs between the various 10262306a36Sopenharmony_ci * sub architectures. Out of line for now 10362306a36Sopenharmony_ci */ 10462306a36Sopenharmony_ci switch_mmu_context(prev, next, tsk); 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci VM_WARN_ON_ONCE(!cpumask_test_cpu(cpu, mm_cpumask(prev))); 10762306a36Sopenharmony_ci} 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci#ifndef CONFIG_PPC_BOOK3S_64 11062306a36Sopenharmony_civoid arch_exit_mmap(struct mm_struct *mm) 11162306a36Sopenharmony_ci{ 11262306a36Sopenharmony_ci void *frag = pte_frag_get(&mm->context); 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci if (frag) 11562306a36Sopenharmony_ci pte_frag_destroy(frag); 11662306a36Sopenharmony_ci} 11762306a36Sopenharmony_ci#endif 118