1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
6 * -Refactored get_new_mmu_context( ) to only handle live-mm.
7 * retiring-mm handled in other hooks
9 * Vineetg: March 25th, 2008: Bug #92690
10 * -Major rewrite of Core ASID allocation routine get_new_mmu_context
12 * Amit Bhor, Sameer Dhavale: Codito Technologies 2004
15 #ifndef _ASM_ARC_MMU_CONTEXT_H
16 #define _ASM_ARC_MMU_CONTEXT_H
18 #include <asm/arcregs.h>
20 #include <linux/sched/mm.h>
22 #include <asm-generic/mm_hooks.h>
24 /* ARC700 ASID Management
26 * ARC MMU provides 8-bit ASID (0..255) to TAG TLB entries, allowing entries
27 * with same vaddr (different tasks) to co-exit. This provides for
28 * "Fast Context Switch" i.e. no TLB flush on ctxt-switch
30 * Linux assigns each task a unique ASID. A simple round-robin allocation
31 * of H/w ASID is done using software tracker @asid_cpu.
32 * When it reaches max 255, the allocation cycle starts afresh by flushing
33 * the entire TLB and wrapping ASID back to zero.
35 * A new allocation cycle, post rollover, could potentially reassign an ASID
36 * to a different task. Thus the rule is to refresh the ASID in a new cycle.
37 * The 32 bit @asid_cpu (and mm->asid) have 8 bits MMU PID and rest 24 bits
38 * serve as cycle/generation indicator and natural 32 bit unsigned math
39 * automagically increments the generation when lower 8 bits rollover.
42 #define MM_CTXT_ASID_MASK 0x000000ff /* MMU PID reg :8 bit PID */
43 #define MM_CTXT_CYCLE_MASK (~MM_CTXT_ASID_MASK)
45 #define MM_CTXT_FIRST_CYCLE (MM_CTXT_ASID_MASK + 1)
46 #define MM_CTXT_NO_ASID 0UL
48 #define asid_mm(mm, cpu) mm->context.asid[cpu]
49 #define hw_pid(mm, cpu) (asid_mm(mm, cpu) & MM_CTXT_ASID_MASK)
51 DECLARE_PER_CPU(unsigned int, asid_cache
);
52 #define asid_cpu(cpu) per_cpu(asid_cache, cpu)
55 * Get a new ASID if task doesn't have a valid one (unalloc or from prev cycle)
56 * Also set the MMU PID register to existing/updated ASID
58 static inline void get_new_mmu_context(struct mm_struct
*mm
)
60 const unsigned int cpu
= smp_processor_id();
63 local_irq_save(flags
);
66 * Move to new ASID if it was not from current alloc-cycle/generation.
67 * This is done by ensuring that the generation bits in both mm->ASID
68 * and cpu's ASID counter are exactly same.
70 * Note: Callers needing new ASID unconditionally, independent of
71 * generation, e.g. local_flush_tlb_mm() for forking parent,
72 * first need to destroy the context, setting it to invalid
75 if (!((asid_mm(mm
, cpu
) ^ asid_cpu(cpu
)) & MM_CTXT_CYCLE_MASK
))
78 /* move to new ASID and handle rollover */
79 if (unlikely(!(++asid_cpu(cpu
) & MM_CTXT_ASID_MASK
))) {
81 local_flush_tlb_all();
84 * Above check for rollover of 8 bit ASID in 32 bit container.
85 * If the container itself wrapped around, set it to a non zero
86 * "generation" to distinguish from no context
89 asid_cpu(cpu
) = MM_CTXT_FIRST_CYCLE
;
92 /* Assign new ASID to tsk */
93 asid_mm(mm
, cpu
) = asid_cpu(cpu
);
96 write_aux_reg(ARC_REG_PID
, hw_pid(mm
, cpu
) | MMU_ENABLE
);
98 local_irq_restore(flags
);
102 * Initialize the context related info for a new mm_struct
105 #define init_new_context init_new_context
107 init_new_context(struct task_struct
*tsk
, struct mm_struct
*mm
)
111 for_each_possible_cpu(i
)
112 asid_mm(mm
, i
) = MM_CTXT_NO_ASID
;
117 #define destroy_context destroy_context
118 static inline void destroy_context(struct mm_struct
*mm
)
122 /* Needed to elide CONFIG_DEBUG_PREEMPT warning */
123 local_irq_save(flags
);
124 asid_mm(mm
, smp_processor_id()) = MM_CTXT_NO_ASID
;
125 local_irq_restore(flags
);
128 /* Prepare the MMU for task: setup PID reg with allocated ASID
129 If task doesn't have an ASID (never alloc or stolen, get a new ASID)
131 static inline void switch_mm(struct mm_struct
*prev
, struct mm_struct
*next
,
132 struct task_struct
*tsk
)
134 const int cpu
= smp_processor_id();
137 * Note that the mm_cpumask is "aggregating" only, we don't clear it
138 * for the switched-out task, unlike some other arches.
139 * It is used to enlist cpus for sending TLB flush IPIs and not sending
140 * it to CPUs where a task once ran-on, could cause stale TLB entry
141 * re-use, specially for a multi-threaded task.
142 * e.g. T1 runs on C1, migrates to C3. T2 running on C2 munmaps.
143 * For a non-aggregating mm_cpumask, IPI not sent C1, and if T1
144 * were to re-migrate to C1, it could access the unmapped region
145 * via any existing stale TLB entries.
147 cpumask_set_cpu(cpu
, mm_cpumask(next
));
149 #ifdef ARC_USE_SCRATCH_REG
150 /* PGD cached in MMU reg to avoid 3 mem lookups: task->mm->pgd */
151 write_aux_reg(ARC_REG_SCRATCH_DATA0
, next
->pgd
);
154 get_new_mmu_context(next
);
158 * activate_mm defaults (in asm-generic) to switch_mm and is called at the
159 * time of execve() to get a new ASID Note the subtlety here:
160 * get_new_mmu_context() behaves differently here vs. in switch_mm(). Here
161 * it always returns a new ASID, because mm has an unallocated "initial"
162 * value, while in latter, it moves to a new ASID, only if it was
166 /* it seemed that deactivate_mm( ) is a reasonable place to do book-keeping
167 * for retiring-mm. However destroy_context( ) still needs to do that because
168 * between mm_release( ) = >deactive_mm( ) and
169 * mmput => .. => __mmdrop( ) => destroy_context( )
170 * there is a good chance that task gets sched-out/in, making it's ASID valid
171 * again (this teased me for a whole day).
174 #include <asm-generic/mmu_context.h>
176 #endif /* __ASM_ARC_MMU_CONTEXT_H */