1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Switch a MMU context.
5 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
7 #ifndef _ASM_MMU_CONTEXT_H
8 #define _ASM_MMU_CONTEXT_H
10 #include <linux/errno.h>
11 #include <linux/sched.h>
12 #include <linux/mm_types.h>
13 #include <linux/smp.h>
14 #include <linux/slab.h>
16 #include <asm/cacheflush.h>
17 #include <asm/tlbflush.h>
18 #include <asm-generic/mm_hooks.h>
21 * All unused by hardware upper bits will be considered
22 * as a software asid extension.
24 static inline u64 asid_version_mask(unsigned int cpu)
26 return ~(u64)(cpu_asid_mask(&cpu_data[cpu]));
29 static inline u64 asid_first_version(unsigned int cpu)
31 return cpu_asid_mask(&cpu_data[cpu]) + 1;
34 #define cpu_context(cpu, mm) ((mm)->context.asid[cpu])
35 #define asid_cache(cpu) (cpu_data[cpu].asid_cache)
36 #define cpu_asid(cpu, mm) (cpu_context((cpu), (mm)) & cpu_asid_mask(&cpu_data[cpu]))
38 static inline int asid_valid(struct mm_struct *mm, unsigned int cpu)
40 if ((cpu_context(cpu, mm) ^ asid_cache(cpu)) & asid_version_mask(cpu))
46 static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
50 /* Normal, classic get_new_mmu_context */
52 get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
54 u64 asid = asid_cache(cpu);
56 if (!((++asid) & cpu_asid_mask(&cpu_data[cpu])))
57 local_flush_tlb_user(); /* start new asid cycle */
59 cpu_context(cpu, mm) = asid_cache(cpu) = asid;
63 * Initialize the context related info for a new mm_struct
67 init_new_context(struct task_struct *tsk, struct mm_struct *mm)
71 for_each_possible_cpu(i)
72 cpu_context(i, mm) = 0;
77 static inline void switch_mm_irqs_off(struct mm_struct *prev, struct mm_struct *next,
78 struct task_struct *tsk)
80 unsigned int cpu = smp_processor_id();
82 /* Check if our ASID is of an older version and thus invalid */
83 if (!asid_valid(next, cpu))
84 get_new_mmu_context(next, cpu);
86 write_csr_asid(cpu_asid(cpu, next));
89 csr_write64((unsigned long)next->pgd, LOONGARCH_CSR_PGDL);
91 csr_write64((unsigned long)invalid_pg_dir, LOONGARCH_CSR_PGDL);
94 * Mark current->active_mm as not "active" anymore.
95 * We don't want to mislead possible IPI tlb flush routines.
97 cpumask_set_cpu(cpu, mm_cpumask(next));
100 #define switch_mm_irqs_off switch_mm_irqs_off
102 static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
103 struct task_struct *tsk)
107 local_irq_save(flags);
108 switch_mm_irqs_off(prev, next, tsk);
109 local_irq_restore(flags);
113 * Destroy context related info for an mm_struct that is about
116 static inline void destroy_context(struct mm_struct *mm)
120 #define activate_mm(prev, next) switch_mm(prev, next, current)
121 #define deactivate_mm(task, mm) do { } while (0)
124 * If mm is currently active, we can't really drop it.
125 * Instead, we will get a new one for it.
128 drop_mmu_context(struct mm_struct *mm, unsigned int cpu)
133 local_irq_save(flags);
135 asid = read_csr_asid() & cpu_asid_mask(¤t_cpu_data);
137 if (asid == cpu_asid(cpu, mm)) {
138 if (!current->mm || (current->mm == mm)) {
139 get_new_mmu_context(mm, cpu);
140 write_csr_asid(cpu_asid(cpu, mm));
145 /* Will get a new context next time */
146 cpu_context(cpu, mm) = 0;
147 cpumask_clear_cpu(cpu, mm_cpumask(mm));
149 local_irq_restore(flags);
152 #endif /* _ASM_MMU_CONTEXT_H */