1
2
3
4
5
6
7
8
9
10#include <linux/init.h>
11#include <linux/sched.h>
12#include <linux/mm.h>
13#include <linux/smp.h>
14#include <linux/percpu.h>
15
16#include <asm/mmu_context.h>
17#include <asm/tlbflush.h>
18
19static DEFINE_SPINLOCK(cpu_asid_lock);
20unsigned int cpu_last_asid = ASID_FIRST_VERSION;
21#ifdef CONFIG_SMP
22DEFINE_PER_CPU(struct mm_struct *, current_mm);
23#endif
24
25
26
27
28
29
30
31void __init_new_context(struct task_struct *tsk, struct mm_struct *mm)
32{
33 mm->context.id = 0;
34 spin_lock_init(&mm->context.id_lock);
35}
36
37static void flush_context(void)
38{
39
40 asm("mcr p15, 0, %0, c13, c0, 1\n" : : "r" (0));
41 isb();
42 local_flush_tlb_all();
43 if (icache_is_vivt_asid_tagged()) {
44 __flush_icache_all();
45 dsb();
46 }
47}
48
49#ifdef CONFIG_SMP
50
51static void set_mm_context(struct mm_struct *mm, unsigned int asid)
52{
53 unsigned long flags;
54
55
56
57
58
59
60
61 spin_lock_irqsave(&mm->context.id_lock, flags);
62 if (likely((mm->context.id ^ cpu_last_asid) >> ASID_BITS)) {
63
64
65
66
67 mm->context.id = asid;
68 cpumask_clear(mm_cpumask(mm));
69 }
70 spin_unlock_irqrestore(&mm->context.id_lock, flags);
71
72
73
74
75 cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
76}
77
78
79
80
81
82static void reset_context(void *info)
83{
84 unsigned int asid;
85 unsigned int cpu = smp_processor_id();
86 struct mm_struct *mm = per_cpu(current_mm, cpu);
87
88
89
90
91
92 if (!mm)
93 return;
94
95 smp_rmb();
96 asid = cpu_last_asid + cpu + 1;
97
98 flush_context();
99 set_mm_context(mm, asid);
100
101
102 asm("mcr p15, 0, %0, c13, c0, 1\n" : : "r" (mm->context.id));
103 isb();
104}
105
106#else
107
108static inline void set_mm_context(struct mm_struct *mm, unsigned int asid)
109{
110 mm->context.id = asid;
111 cpumask_copy(mm_cpumask(mm), cpumask_of(smp_processor_id()));
112}
113
114#endif
115
116void __new_context(struct mm_struct *mm)
117{
118 unsigned int asid;
119
120 spin_lock(&cpu_asid_lock);
121#ifdef CONFIG_SMP
122
123
124
125
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);
129 return;
130 }
131#endif
132
133
134
135
136
137 asid = ++cpu_last_asid;
138 if (asid == 0)
139 asid = cpu_last_asid = ASID_FIRST_VERSION;
140
141
142
143
144
145 if (unlikely((asid & ~ASID_MASK) == 0)) {
146 asid = cpu_last_asid + smp_processor_id() + 1;
147 flush_context();
148#ifdef CONFIG_SMP
149 smp_wmb();
150 smp_call_function(reset_context, NULL, 1);
151#endif
152 cpu_last_asid += NR_CPUS;
153 }
154
155 set_mm_context(mm, asid);
156 spin_unlock(&cpu_asid_lock);
157}
158