blob: 946899d3232ba7ffe2baee249013fe81b652acfc [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 Gleixner450ea482009-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
Will Deacona7a6f922012-01-23 13:48:48 -080028static void write_contextidr(u32 contextidr)
29{
Laura Abbott445eb9a2012-01-25 10:42:13 -080030 uncached_logk(LOGK_CTXID, (void *)contextidr);
Will Deacona7a6f922012-01-23 13:48:48 -080031 asm("mcr p15, 0, %0, c13, c0, 1" : : "r" (contextidr));
32 isb();
33}
34
35#ifdef CONFIG_PID_IN_CONTEXTIDR
36static u32 read_contextidr(void)
37{
38 u32 contextidr;
39 asm("mrc p15, 0, %0, c13, c0, 1" : "=r" (contextidr));
40 return contextidr;
41}
42
43static int contextidr_notifier(struct notifier_block *unused, unsigned long cmd,
44 void *t)
45{
46 unsigned long flags;
47 u32 contextidr;
48 pid_t pid;
49 struct thread_info *thread = t;
50
51 if (cmd != THREAD_NOTIFY_SWITCH)
52 return NOTIFY_DONE;
53
54 pid = task_pid_nr(thread->task);
55 local_irq_save(flags);
56 contextidr = read_contextidr();
57 contextidr &= ~ASID_MASK;
58 contextidr |= pid << ASID_BITS;
59 write_contextidr(contextidr);
60 local_irq_restore(flags);
61
62 return NOTIFY_OK;
63}
64
65static struct notifier_block contextidr_notifier_block = {
66 .notifier_call = contextidr_notifier,
67};
68
69static int __init contextidr_notifier_init(void)
70{
71 return thread_register_notifier(&contextidr_notifier_block);
72}
73arch_initcall(contextidr_notifier_init);
74
75static void set_asid(unsigned int asid)
76{
77 u32 contextidr = read_contextidr();
78 contextidr &= ASID_MASK;
79 contextidr |= asid & ~ASID_MASK;
80 write_contextidr(contextidr);
81}
82#else
83static void set_asid(unsigned int asid)
84{
85 write_contextidr(asid);
86}
87#endif
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * We fork()ed a process, and we need a new context for the child
Russell King07989b72011-06-09 10:10:27 +010091 * to run in. We reserve version 0 for initial tasks so we will
92 * always allocate an ASID. The ASID 0 is reserved for the TTBR
93 * register changing sequence.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
95void __init_new_context(struct task_struct *tsk, struct mm_struct *mm)
96{
97 mm->context.id = 0;
Thomas Gleixner450ea482009-07-03 08:44:46 -050098 raw_spin_lock_init(&mm->context.id_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Catalin Marinas11805bc2010-01-26 19:09:42 +0100101static void flush_context(void)
102{
Russell King07989b72011-06-09 10:10:27 +0100103 /* set the reserved ASID before flushing the TLB */
Will Deacona7a6f922012-01-23 13:48:48 -0800104 set_asid(0);
Catalin Marinas11805bc2010-01-26 19:09:42 +0100105 local_flush_tlb_all();
106 if (icache_is_vivt_asid_tagged()) {
107 __flush_icache_all();
108 dsb();
109 }
110}
111
112#ifdef CONFIG_SMP
113
114static void set_mm_context(struct mm_struct *mm, unsigned int asid)
115{
116 unsigned long flags;
117
118 /*
119 * Locking needed for multi-threaded applications where the
120 * same mm->context.id could be set from different CPUs during
121 * the broadcast. This function is also called via IPI so the
122 * mm->context.id_lock has to be IRQ-safe.
123 */
Thomas Gleixner450ea482009-07-03 08:44:46 -0500124 raw_spin_lock_irqsave(&mm->context.id_lock, flags);
Catalin Marinas11805bc2010-01-26 19:09:42 +0100125 if (likely((mm->context.id ^ cpu_last_asid) >> ASID_BITS)) {
126 /*
127 * Old version of ASID found. Set the new one and
128 * reset mm_cpumask(mm).
129 */
130 mm->context.id = asid;
131 cpumask_clear(mm_cpumask(mm));
132 }
Thomas Gleixner450ea482009-07-03 08:44:46 -0500133 raw_spin_unlock_irqrestore(&mm->context.id_lock, flags);
Catalin Marinas11805bc2010-01-26 19:09:42 +0100134
135 /*
136 * Set the mm_cpumask(mm) bit for the current CPU.
137 */
138 cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
139}
140
141/*
142 * Reset the ASID on the current CPU. This function call is broadcast
143 * from the CPU handling the ASID rollover and holding cpu_asid_lock.
144 */
145static void reset_context(void *info)
146{
147 unsigned int asid;
148 unsigned int cpu = smp_processor_id();
149 struct mm_struct *mm = per_cpu(current_mm, cpu);
150
151 /*
152 * Check if a current_mm was set on this CPU as it might still
153 * be in the early booting stages and using the reserved ASID.
154 */
155 if (!mm)
156 return;
157
158 smp_rmb();
Russell Kinga0a54d32011-06-09 10:12:41 +0100159 asid = cpu_last_asid + cpu + 1;
Catalin Marinas11805bc2010-01-26 19:09:42 +0100160
161 flush_context();
162 set_mm_context(mm, asid);
163
164 /* set the new ASID */
Will Deacona7a6f922012-01-23 13:48:48 -0800165 set_asid(mm->context.id);
Catalin Marinas11805bc2010-01-26 19:09:42 +0100166}
167
168#else
169
170static inline void set_mm_context(struct mm_struct *mm, unsigned int asid)
171{
172 mm->context.id = asid;
173 cpumask_copy(mm_cpumask(mm), cpumask_of(smp_processor_id()));
174}
175
176#endif
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178void __new_context(struct mm_struct *mm)
179{
180 unsigned int asid;
181
Thomas Gleixner450ea482009-07-03 08:44:46 -0500182 raw_spin_lock(&cpu_asid_lock);
Catalin Marinas11805bc2010-01-26 19:09:42 +0100183#ifdef CONFIG_SMP
184 /*
185 * Check the ASID again, in case the change was broadcast from
186 * another CPU before we acquired the lock.
187 */
188 if (unlikely(((mm->context.id ^ cpu_last_asid) >> ASID_BITS) == 0)) {
189 cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
Thomas Gleixner450ea482009-07-03 08:44:46 -0500190 raw_spin_unlock(&cpu_asid_lock);
Catalin Marinas11805bc2010-01-26 19:09:42 +0100191 return;
192 }
193#endif
194 /*
195 * At this point, it is guaranteed that the current mm (with
196 * an old ASID) isn't active on any other CPU since the ASIDs
197 * are changed simultaneously via IPI.
198 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 asid = ++cpu_last_asid;
200 if (asid == 0)
Russell King8678c1f2007-05-08 20:03:09 +0100201 asid = cpu_last_asid = ASID_FIRST_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 /*
204 * If we've used up all our ASIDs, we need
205 * to start a new version and flush the TLB.
206 */
Russell King8678c1f2007-05-08 20:03:09 +0100207 if (unlikely((asid & ~ASID_MASK) == 0)) {
Russell Kinga0a54d32011-06-09 10:12:41 +0100208 asid = cpu_last_asid + smp_processor_id() + 1;
Catalin Marinas11805bc2010-01-26 19:09:42 +0100209 flush_context();
210#ifdef CONFIG_SMP
211 smp_wmb();
212 smp_call_function(reset_context, NULL, 1);
213#endif
Russell Kinga0a54d32011-06-09 10:12:41 +0100214 cpu_last_asid += NR_CPUS;
Catalin Marinas9d99df42007-02-05 14:47:40 +0100215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Catalin Marinas11805bc2010-01-26 19:09:42 +0100217 set_mm_context(mm, asid);
Thomas Gleixner450ea482009-07-03 08:44:46 -0500218 raw_spin_unlock(&cpu_asid_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}