blob: 847ea19e8d63a573543d51321bd32ab43a29e7c2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Russell Kingd84b4712006-08-21 19:23:38 +01002 * linux/arch/arm/mm/context.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2002-2003 Deep Blue Solutions Ltd, all rights reserved.
5 *
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.
9 */
10#include <linux/init.h>
11#include <linux/sched.h>
12#include <linux/mm.h>
Catalin Marinas11805bc2010-01-26 19:09:42 +010013#include <linux/smp.h>
14#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <asm/mmu_context.h>
Will Deacona7a6f922012-01-23 13:48:48 -080017#include <asm/thread_notify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/tlbflush.h>
19
Laura Abbott445eb9a2012-01-25 10:42:13 -080020#include <mach/msm_rtb.h>
21
Thomas Gleixnerbd31b852009-07-03 08:44:46 -050022static DEFINE_RAW_SPINLOCK(cpu_asid_lock);
Russell King8678c1f2007-05-08 20:03:09 +010023unsigned int cpu_last_asid = ASID_FIRST_VERSION;
Catalin Marinas11805bc2010-01-26 19:09:42 +010024#ifdef CONFIG_SMP
25DEFINE_PER_CPU(struct mm_struct *, current_mm);
26#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Catalin Marinas14d8c952011-11-22 17:30:31 +000028#ifdef CONFIG_ARM_LPAE
29#define cpu_set_asid(asid) { \
30 unsigned long ttbl, ttbh; \
31 asm volatile( \
32 " mrrc p15, 0, %0, %1, c2 @ read TTBR0\n" \
33 " mov %1, %2, lsl #(48 - 32) @ set ASID\n" \
34 " mcrr p15, 0, %0, %1, c2 @ set TTBR0\n" \
35 : "=&r" (ttbl), "=&r" (ttbh) \
36 : "r" (asid & ~ASID_MASK)); \
37}
38#else
39#define cpu_set_asid(asid) \
40 asm(" mcr p15, 0, %0, c13, c0, 1\n" : : "r" (asid))
41#endif
42
Will Deacona7a6f922012-01-23 13:48:48 -080043static void write_contextidr(u32 contextidr)
44{
Laura Abbott445eb9a2012-01-25 10:42:13 -080045 uncached_logk(LOGK_CTXID, (void *)contextidr);
Will Deacona7a6f922012-01-23 13:48:48 -080046 asm("mcr p15, 0, %0, c13, c0, 1" : : "r" (contextidr));
47 isb();
48}
49
50#ifdef CONFIG_PID_IN_CONTEXTIDR
51static u32 read_contextidr(void)
52{
53 u32 contextidr;
54 asm("mrc p15, 0, %0, c13, c0, 1" : "=r" (contextidr));
55 return contextidr;
56}
57
58static int contextidr_notifier(struct notifier_block *unused, unsigned long cmd,
59 void *t)
60{
61 unsigned long flags;
62 u32 contextidr;
63 pid_t pid;
64 struct thread_info *thread = t;
65
66 if (cmd != THREAD_NOTIFY_SWITCH)
67 return NOTIFY_DONE;
68
69 pid = task_pid_nr(thread->task);
70 local_irq_save(flags);
71 contextidr = read_contextidr();
72 contextidr &= ~ASID_MASK;
73 contextidr |= pid << ASID_BITS;
74 write_contextidr(contextidr);
75 local_irq_restore(flags);
76
77 return NOTIFY_OK;
78}
79
80static struct notifier_block contextidr_notifier_block = {
81 .notifier_call = contextidr_notifier,
82};
83
84static int __init contextidr_notifier_init(void)
85{
86 return thread_register_notifier(&contextidr_notifier_block);
87}
88arch_initcall(contextidr_notifier_init);
89
90static void set_asid(unsigned int asid)
91{
92 u32 contextidr = read_contextidr();
93 contextidr &= ASID_MASK;
94 contextidr |= asid & ~ASID_MASK;
95 write_contextidr(contextidr);
96}
97#else
98static void set_asid(unsigned int asid)
99{
100 write_contextidr(asid);
101}
102#endif
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
105 * We fork()ed a process, and we need a new context for the child
Russell King07989b72011-06-09 10:10:27 +0100106 * to run in. We reserve version 0 for initial tasks so we will
107 * always allocate an ASID. The ASID 0 is reserved for the TTBR
108 * register changing sequence.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 */
110void __init_new_context(struct task_struct *tsk, struct mm_struct *mm)
111{
112 mm->context.id = 0;
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500113 raw_spin_lock_init(&mm->context.id_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Catalin Marinas11805bc2010-01-26 19:09:42 +0100116static void flush_context(void)
117{
Russell King07989b72011-06-09 10:10:27 +0100118 /* set the reserved ASID before flushing the TLB */
Will Deacona7a6f922012-01-23 13:48:48 -0800119 set_asid(0);
Catalin Marinas11805bc2010-01-26 19:09:42 +0100120 local_flush_tlb_all();
121 if (icache_is_vivt_asid_tagged()) {
122 __flush_icache_all();
123 dsb();
124 }
125}
126
127#ifdef CONFIG_SMP
128
129static void set_mm_context(struct mm_struct *mm, unsigned int asid)
130{
131 unsigned long flags;
132
133 /*
134 * Locking needed for multi-threaded applications where the
135 * same mm->context.id could be set from different CPUs during
136 * the broadcast. This function is also called via IPI so the
137 * mm->context.id_lock has to be IRQ-safe.
138 */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500139 raw_spin_lock_irqsave(&mm->context.id_lock, flags);
Catalin Marinas11805bc2010-01-26 19:09:42 +0100140 if (likely((mm->context.id ^ cpu_last_asid) >> ASID_BITS)) {
141 /*
142 * Old version of ASID found. Set the new one and
143 * reset mm_cpumask(mm).
144 */
145 mm->context.id = asid;
146 cpumask_clear(mm_cpumask(mm));
147 }
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500148 raw_spin_unlock_irqrestore(&mm->context.id_lock, flags);
Catalin Marinas11805bc2010-01-26 19:09:42 +0100149
150 /*
151 * Set the mm_cpumask(mm) bit for the current CPU.
152 */
153 cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
154}
155
156/*
157 * Reset the ASID on the current CPU. This function call is broadcast
158 * from the CPU handling the ASID rollover and holding cpu_asid_lock.
159 */
160static void reset_context(void *info)
161{
162 unsigned int asid;
163 unsigned int cpu = smp_processor_id();
164 struct mm_struct *mm = per_cpu(current_mm, cpu);
165
166 /*
167 * Check if a current_mm was set on this CPU as it might still
168 * be in the early booting stages and using the reserved ASID.
169 */
170 if (!mm)
171 return;
172
173 smp_rmb();
Russell Kinga0a54d32011-06-09 10:12:41 +0100174 asid = cpu_last_asid + cpu + 1;
Catalin Marinas11805bc2010-01-26 19:09:42 +0100175
176 flush_context();
177 set_mm_context(mm, asid);
178
179 /* set the new ASID */
Will Deacona7a6f922012-01-23 13:48:48 -0800180 set_asid(mm->context.id);
Catalin Marinas11805bc2010-01-26 19:09:42 +0100181}
182
183#else
184
185static inline void set_mm_context(struct mm_struct *mm, unsigned int asid)
186{
187 mm->context.id = asid;
188 cpumask_copy(mm_cpumask(mm), cpumask_of(smp_processor_id()));
189}
190
191#endif
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193void __new_context(struct mm_struct *mm)
194{
195 unsigned int asid;
196
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500197 raw_spin_lock(&cpu_asid_lock);
Catalin Marinas11805bc2010-01-26 19:09:42 +0100198#ifdef CONFIG_SMP
199 /*
200 * Check the ASID again, in case the change was broadcast from
201 * another CPU before we acquired the lock.
202 */
203 if (unlikely(((mm->context.id ^ cpu_last_asid) >> ASID_BITS) == 0)) {
204 cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500205 raw_spin_unlock(&cpu_asid_lock);
Catalin Marinas11805bc2010-01-26 19:09:42 +0100206 return;
207 }
208#endif
209 /*
210 * At this point, it is guaranteed that the current mm (with
211 * an old ASID) isn't active on any other CPU since the ASIDs
212 * are changed simultaneously via IPI.
213 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 asid = ++cpu_last_asid;
215 if (asid == 0)
Russell King8678c1f2007-05-08 20:03:09 +0100216 asid = cpu_last_asid = ASID_FIRST_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 /*
219 * If we've used up all our ASIDs, we need
220 * to start a new version and flush the TLB.
221 */
Russell King8678c1f2007-05-08 20:03:09 +0100222 if (unlikely((asid & ~ASID_MASK) == 0)) {
Russell Kinga0a54d32011-06-09 10:12:41 +0100223 asid = cpu_last_asid + smp_processor_id() + 1;
Catalin Marinas11805bc2010-01-26 19:09:42 +0100224 flush_context();
225#ifdef CONFIG_SMP
226 smp_wmb();
227 smp_call_function(reset_context, NULL, 1);
228#endif
Russell Kinga0a54d32011-06-09 10:12:41 +0100229 cpu_last_asid += NR_CPUS;
Catalin Marinas9d99df42007-02-05 14:47:40 +0100230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Catalin Marinas11805bc2010-01-26 19:09:42 +0100232 set_mm_context(mm, asid);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500233 raw_spin_unlock(&cpu_asid_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}