xref: /openbmc/linux/arch/arc/include/asm/mmu_context.h (revision 2547476a5e4061f6addb88d5fc837d3a950f54c4)
1f1f3347dSVineet Gupta /*
2f1f3347dSVineet Gupta  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
3f1f3347dSVineet Gupta  *
4f1f3347dSVineet Gupta  * This program is free software; you can redistribute it and/or modify
5f1f3347dSVineet Gupta  * it under the terms of the GNU General Public License version 2 as
6f1f3347dSVineet Gupta  * published by the Free Software Foundation.
7f1f3347dSVineet Gupta  *
8f1f3347dSVineet Gupta  * vineetg: May 2011
9f1f3347dSVineet Gupta  *  -Refactored get_new_mmu_context( ) to only handle live-mm.
10f1f3347dSVineet Gupta  *   retiring-mm handled in other hooks
11f1f3347dSVineet Gupta  *
12f1f3347dSVineet Gupta  * Vineetg: March 25th, 2008: Bug #92690
13f1f3347dSVineet Gupta  *  -Major rewrite of Core ASID allocation routine get_new_mmu_context
14f1f3347dSVineet Gupta  *
15f1f3347dSVineet Gupta  * Amit Bhor, Sameer Dhavale: Codito Technologies 2004
16f1f3347dSVineet Gupta  */
17f1f3347dSVineet Gupta 
18f1f3347dSVineet Gupta #ifndef _ASM_ARC_MMU_CONTEXT_H
19f1f3347dSVineet Gupta #define _ASM_ARC_MMU_CONTEXT_H
20f1f3347dSVineet Gupta 
21f1f3347dSVineet Gupta #include <asm/arcregs.h>
22f1f3347dSVineet Gupta #include <asm/tlb.h>
23f1f3347dSVineet Gupta 
24f1f3347dSVineet Gupta #include <asm-generic/mm_hooks.h>
25f1f3347dSVineet Gupta 
26f1f3347dSVineet Gupta /*		ARC700 ASID Management
27f1f3347dSVineet Gupta  *
28f1f3347dSVineet Gupta  * ARC MMU provides 8-bit ASID (0..255) to TAG TLB entries, allowing entries
29f1f3347dSVineet Gupta  * with same vaddr (different tasks) to co-exit. This provides for
30f1f3347dSVineet Gupta  * "Fast Context Switch" i.e. no TLB flush on ctxt-switch
31f1f3347dSVineet Gupta  *
32f1f3347dSVineet Gupta  * Linux assigns each task a unique ASID. A simple round-robin allocation
3363eca94cSVineet Gupta  * of H/w ASID is done using software tracker @asid_cpu.
34f1f3347dSVineet Gupta  * When it reaches max 255, the allocation cycle starts afresh by flushing
35f1f3347dSVineet Gupta  * the entire TLB and wrapping ASID back to zero.
36f1f3347dSVineet Gupta  *
37947bf103SVineet Gupta  * A new allocation cycle, post rollover, could potentially reassign an ASID
38947bf103SVineet Gupta  * to a different task. Thus the rule is to refresh the ASID in a new cycle.
3963eca94cSVineet Gupta  * The 32 bit @asid_cpu (and mm->asid) have 8 bits MMU PID and rest 24 bits
40947bf103SVineet Gupta  * serve as cycle/generation indicator and natural 32 bit unsigned math
41947bf103SVineet Gupta  * automagically increments the generation when lower 8 bits rollover.
42f1f3347dSVineet Gupta  */
43f1f3347dSVineet Gupta 
44947bf103SVineet Gupta #define MM_CTXT_ASID_MASK	0x000000ff /* MMU PID reg :8 bit PID */
45947bf103SVineet Gupta #define MM_CTXT_CYCLE_MASK	(~MM_CTXT_ASID_MASK)
46f1f3347dSVineet Gupta 
47947bf103SVineet Gupta #define MM_CTXT_FIRST_CYCLE	(MM_CTXT_ASID_MASK + 1)
48947bf103SVineet Gupta #define MM_CTXT_NO_ASID		0UL
49f1f3347dSVineet Gupta 
5063eca94cSVineet Gupta #define asid_mm(mm, cpu)	mm->context.asid[cpu]
5163eca94cSVineet Gupta #define hw_pid(mm, cpu)		(asid_mm(mm, cpu) & MM_CTXT_ASID_MASK)
52947bf103SVineet Gupta 
5363eca94cSVineet Gupta DECLARE_PER_CPU(unsigned int, asid_cache);
5463eca94cSVineet Gupta #define asid_cpu(cpu)		per_cpu(asid_cache, cpu)
55f1f3347dSVineet Gupta 
56f1f3347dSVineet Gupta /*
573daa48d1SVineet Gupta  * Get a new ASID if task doesn't have a valid one (unalloc or from prev cycle)
583daa48d1SVineet Gupta  * Also set the MMU PID register to existing/updated ASID
59f1f3347dSVineet Gupta  */
60f1f3347dSVineet Gupta static inline void get_new_mmu_context(struct mm_struct *mm)
61f1f3347dSVineet Gupta {
6263eca94cSVineet Gupta 	const unsigned int cpu = smp_processor_id();
63f1f3347dSVineet Gupta 	unsigned long flags;
64f1f3347dSVineet Gupta 
65f1f3347dSVineet Gupta 	local_irq_save(flags);
66f1f3347dSVineet Gupta 
67f1f3347dSVineet Gupta 	/*
683daa48d1SVineet Gupta 	 * Move to new ASID if it was not from current alloc-cycle/generation.
69947bf103SVineet Gupta 	 * This is done by ensuring that the generation bits in both mm->ASID
70947bf103SVineet Gupta 	 * and cpu's ASID counter are exactly same.
713daa48d1SVineet Gupta 	 *
723daa48d1SVineet Gupta 	 * Note: Callers needing new ASID unconditionally, independent of
733daa48d1SVineet Gupta 	 * 	 generation, e.g. local_flush_tlb_mm() for forking  parent,
743daa48d1SVineet Gupta 	 * 	 first need to destroy the context, setting it to invalid
753daa48d1SVineet Gupta 	 * 	 value.
763daa48d1SVineet Gupta 	 */
7763eca94cSVineet Gupta 	if (!((asid_mm(mm, cpu) ^ asid_cpu(cpu)) & MM_CTXT_CYCLE_MASK))
783daa48d1SVineet Gupta 		goto set_hw;
793daa48d1SVineet Gupta 
80947bf103SVineet Gupta 	/* move to new ASID and handle rollover */
8163eca94cSVineet Gupta 	if (unlikely(!(++asid_cpu(cpu) & MM_CTXT_ASID_MASK))) {
82f1f3347dSVineet Gupta 
835ea72a90SVineet Gupta 		local_flush_tlb_all();
84947bf103SVineet Gupta 
85947bf103SVineet Gupta 		/*
86*2547476aSAndrea Gelmini 		 * Above check for rollover of 8 bit ASID in 32 bit container.
87947bf103SVineet Gupta 		 * If the container itself wrapped around, set it to a non zero
88947bf103SVineet Gupta 		 * "generation" to distinguish from no context
89947bf103SVineet Gupta 		 */
9063eca94cSVineet Gupta 		if (!asid_cpu(cpu))
9163eca94cSVineet Gupta 			asid_cpu(cpu) = MM_CTXT_FIRST_CYCLE;
92f1f3347dSVineet Gupta 	}
93f1f3347dSVineet Gupta 
94f1f3347dSVineet Gupta 	/* Assign new ASID to tsk */
9563eca94cSVineet Gupta 	asid_mm(mm, cpu) = asid_cpu(cpu);
96f1f3347dSVineet Gupta 
973daa48d1SVineet Gupta set_hw:
9863eca94cSVineet Gupta 	write_aux_reg(ARC_REG_PID, hw_pid(mm, cpu) | MMU_ENABLE);
99f1f3347dSVineet Gupta 
100f1f3347dSVineet Gupta 	local_irq_restore(flags);
101f1f3347dSVineet Gupta }
102f1f3347dSVineet Gupta 
103f1f3347dSVineet Gupta /*
104f1f3347dSVineet Gupta  * Initialize the context related info for a new mm_struct
105f1f3347dSVineet Gupta  * instance.
106f1f3347dSVineet Gupta  */
107f1f3347dSVineet Gupta static inline int
108f1f3347dSVineet Gupta init_new_context(struct task_struct *tsk, struct mm_struct *mm)
109f1f3347dSVineet Gupta {
11063eca94cSVineet Gupta 	int i;
11163eca94cSVineet Gupta 
11263eca94cSVineet Gupta 	for_each_possible_cpu(i)
11363eca94cSVineet Gupta 		asid_mm(mm, i) = MM_CTXT_NO_ASID;
11463eca94cSVineet Gupta 
115f1f3347dSVineet Gupta 	return 0;
116f1f3347dSVineet Gupta }
117f1f3347dSVineet Gupta 
11863eca94cSVineet Gupta static inline void destroy_context(struct mm_struct *mm)
11963eca94cSVineet Gupta {
12063eca94cSVineet Gupta 	unsigned long flags;
12163eca94cSVineet Gupta 
12263eca94cSVineet Gupta 	/* Needed to elide CONFIG_DEBUG_PREEMPT warning */
12363eca94cSVineet Gupta 	local_irq_save(flags);
12463eca94cSVineet Gupta 	asid_mm(mm, smp_processor_id()) = MM_CTXT_NO_ASID;
12563eca94cSVineet Gupta 	local_irq_restore(flags);
12663eca94cSVineet Gupta }
12763eca94cSVineet Gupta 
128f1f3347dSVineet Gupta /* Prepare the MMU for task: setup PID reg with allocated ASID
129f1f3347dSVineet Gupta     If task doesn't have an ASID (never alloc or stolen, get a new ASID)
130f1f3347dSVineet Gupta */
131f1f3347dSVineet Gupta static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
132f1f3347dSVineet Gupta 			     struct task_struct *tsk)
133f1f3347dSVineet Gupta {
1345ea72a90SVineet Gupta 	const int cpu = smp_processor_id();
1355ea72a90SVineet Gupta 
1365ea72a90SVineet Gupta 	/*
1375ea72a90SVineet Gupta 	 * Note that the mm_cpumask is "aggregating" only, we don't clear it
1385ea72a90SVineet Gupta 	 * for the switched-out task, unlike some other arches.
1395ea72a90SVineet Gupta 	 * It is used to enlist cpus for sending TLB flush IPIs and not sending
1405ea72a90SVineet Gupta 	 * it to CPUs where a task once ran-on, could cause stale TLB entry
1415ea72a90SVineet Gupta 	 * re-use, specially for a multi-threaded task.
1425ea72a90SVineet Gupta 	 * e.g. T1 runs on C1, migrates to C3. T2 running on C2 munmaps.
1435ea72a90SVineet Gupta 	 *      For a non-aggregating mm_cpumask, IPI not sent C1, and if T1
1445ea72a90SVineet Gupta 	 *      were to re-migrate to C1, it could access the unmapped region
1455ea72a90SVineet Gupta 	 *      via any existing stale TLB entries.
1465ea72a90SVineet Gupta 	 */
1475ea72a90SVineet Gupta 	cpumask_set_cpu(cpu, mm_cpumask(next));
1485ea72a90SVineet Gupta 
14941195d23SVineet Gupta #ifndef CONFIG_SMP
150f1f3347dSVineet Gupta 	/* PGD cached in MMU reg to avoid 3 mem lookups: task->mm->pgd */
151f1f3347dSVineet Gupta 	write_aux_reg(ARC_REG_SCRATCH_DATA0, next->pgd);
15241195d23SVineet Gupta #endif
153f1f3347dSVineet Gupta 
154f1f3347dSVineet Gupta 	get_new_mmu_context(next);
155f1f3347dSVineet Gupta }
156f1f3347dSVineet Gupta 
157c6011553SVineet Gupta /*
158c6011553SVineet Gupta  * Called at the time of execve() to get a new ASID
159c6011553SVineet Gupta  * Note the subtlety here: get_new_mmu_context() behaves differently here
160c6011553SVineet Gupta  * vs. in switch_mm(). Here it always returns a new ASID, because mm has
161c6011553SVineet Gupta  * an unallocated "initial" value, while in latter, it moves to a new ASID,
162c6011553SVineet Gupta  * only if it was unallocated
163c6011553SVineet Gupta  */
164c6011553SVineet Gupta #define activate_mm(prev, next)		switch_mm(prev, next, NULL)
165c6011553SVineet Gupta 
166f1f3347dSVineet Gupta /* it seemed that deactivate_mm( ) is a reasonable place to do book-keeping
167f1f3347dSVineet Gupta  * for retiring-mm. However destroy_context( ) still needs to do that because
168f1f3347dSVineet Gupta  * between mm_release( ) = >deactive_mm( ) and
169f1f3347dSVineet Gupta  * mmput => .. => __mmdrop( ) => destroy_context( )
170f1f3347dSVineet Gupta  * there is a good chance that task gets sched-out/in, making it's ASID valid
171f1f3347dSVineet Gupta  * again (this teased me for a whole day).
172f1f3347dSVineet Gupta  */
173f1f3347dSVineet Gupta #define deactivate_mm(tsk, mm)   do { } while (0)
174f1f3347dSVineet Gupta 
175f1f3347dSVineet Gupta #define enter_lazy_tlb(mm, tsk)
176f1f3347dSVineet Gupta 
177f1f3347dSVineet Gupta #endif /* __ASM_ARC_MMU_CONTEXT_H */
178