// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Common implementation of switch_mm_irqs_off3*4* Copyright IBM Corp. 20175*/67#include <linux/mm.h>8#include <linux/cpu.h>9#include <linux/sched/mm.h>1011#include <asm/mmu_context.h>12#include <asm/pgalloc.h>1314#if defined(CONFIG_PPC32)15static inline void switch_mm_pgdir(struct task_struct *tsk,16struct mm_struct *mm)17{18/* 32-bit keeps track of the current PGDIR in the thread struct */19tsk->thread.pgdir = mm->pgd;20#ifdef CONFIG_PPC_BOOK3S_3221tsk->thread.sr0 = mm->context.sr0;22#endif23#if defined(CONFIG_BOOKE) && defined(CONFIG_PPC_KUAP)24tsk->thread.pid = mm->context.id;25#endif26}27#elif defined(CONFIG_PPC_BOOK3E_64)28static inline void switch_mm_pgdir(struct task_struct *tsk,29struct mm_struct *mm)30{31/* 64-bit Book3E keeps track of current PGD in the PACA */32get_paca()->pgd = mm->pgd;33#ifdef CONFIG_PPC_KUAP34tsk->thread.pid = mm->context.id;35#endif36}37#else38static inline void switch_mm_pgdir(struct task_struct *tsk,39struct mm_struct *mm) { }40#endif4142void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,43struct task_struct *tsk)44{45int cpu = smp_processor_id();46bool new_on_cpu = false;4748/* Mark this context has been used on the new CPU */49if (!cpumask_test_cpu(cpu, mm_cpumask(next))) {50VM_WARN_ON_ONCE(next == &init_mm);51cpumask_set_cpu(cpu, mm_cpumask(next));52inc_mm_active_cpus(next);5354/*55* This full barrier orders the store to the cpumask above vs56* a subsequent load which allows this CPU/MMU to begin loading57* translations for 'next' from page table PTEs into the TLB.58*59* When using the radix MMU, that operation is the load of the60* MMU context id, which is then moved to SPRN_PID.61*62* For the hash MMU it is either the first load from slb_cache63* in switch_slb() to preload the SLBs, or the load of64* get_user_context which loads the context for the VSID hash65* to insert a new SLB, in the SLB fault handler.66*67* On the other side, the barrier is in mm/tlb-radix.c for68* radix which orders earlier stores to clear the PTEs before69* the load of mm_cpumask to check which CPU TLBs should be70* flushed. For hash, pte_xchg to clear the PTE includes the71* barrier.72*73* This full barrier is also needed by membarrier when74* switching between processes after store to rq->curr, before75* user-space memory accesses.76*/77smp_mb();7879new_on_cpu = true;80}8182/* Some subarchs need to track the PGD elsewhere */83switch_mm_pgdir(tsk, next);8485/* Nothing else to do if we aren't actually switching */86if (prev == next)87return;8889/*90* We must stop all altivec streams before changing the HW91* context92*/93if (cpu_has_feature(CPU_FTR_ALTIVEC))94asm volatile (PPC_DSSALL);9596if (!new_on_cpu)97membarrier_arch_switch_mm(prev, next, tsk);9899/*100* The actual HW switching method differs between the various101* sub architectures. Out of line for now102*/103switch_mmu_context(prev, next, tsk);104105VM_WARN_ON_ONCE(!cpumask_test_cpu(cpu, mm_cpumask(prev)));106}107108#ifndef CONFIG_PPC_BOOK3S_64109void arch_exit_mmap(struct mm_struct *mm)110{111void *frag = pte_frag_get(&mm->context);112113if (frag)114pte_frag_destroy(frag);115}116#endif117118119