2 * linux/arch/arm/mm/context.c
4 * Copyright (C) 2002-2003 Deep Blue Solutions Ltd, all rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 #include <linux/init.h>
11 #include <linux/sched.h>
13 #include <linux/smp.h>
14 #include <linux/percpu.h>
16 #include <asm/mmu_context.h>
17 #include <asm/tlbflush.h>
19 static DEFINE_SPINLOCK(cpu_asid_lock);
20 unsigned int cpu_last_asid = ASID_FIRST_VERSION;
22 DEFINE_PER_CPU(struct mm_struct *, current_mm);
26 * We fork()ed a process, and we need a new context for the child
27 * to run in. We reserve version 0 for initial tasks so we will
28 * always allocate an ASID. The ASID 0 is reserved for the TTBR
29 * register changing sequence.
31 void __init_new_context(struct task_struct *tsk, struct mm_struct *mm)
34 spin_lock_init(&mm->context.id_lock);
37 static void flush_context(void)
39 /* set the reserved ASID before flushing the TLB */
40 asm("mcr p15, 0, %0, c13, c0, 1\n" : : "r" (0));
42 local_flush_tlb_all();
43 if (icache_is_vivt_asid_tagged()) {
51 static void set_mm_context(struct mm_struct *mm, unsigned int asid)
56 * Locking needed for multi-threaded applications where the
57 * same mm->context.id could be set from different CPUs during
58 * the broadcast. This function is also called via IPI so the
59 * mm->context.id_lock has to be IRQ-safe.
61 spin_lock_irqsave(&mm->context.id_lock, flags);
62 if (likely((mm->context.id ^ cpu_last_asid) >> ASID_BITS)) {
64 * Old version of ASID found. Set the new one and
65 * reset mm_cpumask(mm).
67 mm->context.id = asid;
68 cpumask_clear(mm_cpumask(mm));
70 spin_unlock_irqrestore(&mm->context.id_lock, flags);
73 * Set the mm_cpumask(mm) bit for the current CPU.
75 cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
79 * Reset the ASID on the current CPU. This function call is broadcast
80 * from the CPU handling the ASID rollover and holding cpu_asid_lock.
82 static void reset_context(void *info)
85 unsigned int cpu = smp_processor_id();
86 struct mm_struct *mm = per_cpu(current_mm, cpu);
89 * Check if a current_mm was set on this CPU as it might still
90 * be in the early booting stages and using the reserved ASID.
96 asid = cpu_last_asid + cpu + 1;
99 set_mm_context(mm, asid);
101 /* set the new ASID */
102 asm("mcr p15, 0, %0, c13, c0, 1\n" : : "r" (mm->context.id));
108 static inline void set_mm_context(struct mm_struct *mm, unsigned int asid)
110 mm->context.id = asid;
111 cpumask_copy(mm_cpumask(mm), cpumask_of(smp_processor_id()));
116 void __new_context(struct mm_struct *mm)
120 spin_lock(&cpu_asid_lock);
123 * Check the ASID again, in case the change was broadcast from
124 * another CPU before we acquired the lock.
126 if (unlikely(((mm->context.id ^ cpu_last_asid) >> ASID_BITS) == 0)) {
127 cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
128 spin_unlock(&cpu_asid_lock);
133 * At this point, it is guaranteed that the current mm (with
134 * an old ASID) isn't active on any other CPU since the ASIDs
135 * are changed simultaneously via IPI.
137 asid = ++cpu_last_asid;
139 asid = cpu_last_asid = ASID_FIRST_VERSION;
142 * If we've used up all our ASIDs, we need
143 * to start a new version and flush the TLB.
145 if (unlikely((asid & ~ASID_MASK) == 0)) {
146 asid = cpu_last_asid + smp_processor_id() + 1;
150 smp_call_function(reset_context, NULL, 1);
152 cpu_last_asid += NR_CPUS;
155 set_mm_context(mm, asid);
156 spin_unlock(&cpu_asid_lock);