blob: 806cc4f63516fd4bfddecad4ec93be51a830f49a [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>
17#include <asm/tlbflush.h>
18
Thomas Gleixnerbd31b852009-07-03 08:44:46 -050019static DEFINE_RAW_SPINLOCK(cpu_asid_lock);
Russell King8678c1f2007-05-08 20:03:09 +010020unsigned int cpu_last_asid = ASID_FIRST_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Catalin Marinas14d8c952011-11-22 17:30:31 +000022#ifdef CONFIG_ARM_LPAE
Catalin Marinas7fec1b52011-11-28 13:53:28 +000023void cpu_set_reserved_ttbr0(void)
Will Deacon3c5f7e72011-05-31 15:38:43 +010024{
25 unsigned long ttbl = __pa(swapper_pg_dir);
26 unsigned long ttbh = 0;
27
28 /*
29 * Set TTBR0 to swapper_pg_dir which contains only global entries. The
30 * ASID is set to 0.
31 */
32 asm volatile(
33 " mcrr p15, 0, %0, %1, c2 @ set TTBR0\n"
34 :
35 : "r" (ttbl), "r" (ttbh));
36 isb();
Catalin Marinas14d8c952011-11-22 17:30:31 +000037}
38#else
Catalin Marinas7fec1b52011-11-28 13:53:28 +000039void cpu_set_reserved_ttbr0(void)
Will Deacon3c5f7e72011-05-31 15:38:43 +010040{
41 u32 ttb;
42 /* Copy TTBR1 into TTBR0 */
43 asm volatile(
44 " mrc p15, 0, %0, c2, c0, 1 @ read TTBR1\n"
45 " mcr p15, 0, %0, c2, c0, 0 @ set TTBR0\n"
46 : "=r" (ttb));
47 isb();
48}
Catalin Marinas14d8c952011-11-22 17:30:31 +000049#endif
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * We fork()ed a process, and we need a new context for the child
Will Deacon3c5f7e72011-05-31 15:38:43 +010053 * to run in.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 */
55void __init_new_context(struct task_struct *tsk, struct mm_struct *mm)
56{
57 mm->context.id = 0;
Thomas Gleixnerbd31b852009-07-03 08:44:46 -050058 raw_spin_lock_init(&mm->context.id_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Catalin Marinas11805bc2010-01-26 19:09:42 +010061static void flush_context(void)
62{
Will Deacon3c5f7e72011-05-31 15:38:43 +010063 cpu_set_reserved_ttbr0();
Catalin Marinas11805bc2010-01-26 19:09:42 +010064 local_flush_tlb_all();
65 if (icache_is_vivt_asid_tagged()) {
66 __flush_icache_all();
67 dsb();
68 }
69}
70
71#ifdef CONFIG_SMP
72
73static void set_mm_context(struct mm_struct *mm, unsigned int asid)
74{
75 unsigned long flags;
76
77 /*
78 * Locking needed for multi-threaded applications where the
79 * same mm->context.id could be set from different CPUs during
80 * the broadcast. This function is also called via IPI so the
81 * mm->context.id_lock has to be IRQ-safe.
82 */
Thomas Gleixnerbd31b852009-07-03 08:44:46 -050083 raw_spin_lock_irqsave(&mm->context.id_lock, flags);
Catalin Marinas11805bc2010-01-26 19:09:42 +010084 if (likely((mm->context.id ^ cpu_last_asid) >> ASID_BITS)) {
85 /*
86 * Old version of ASID found. Set the new one and
87 * reset mm_cpumask(mm).
88 */
89 mm->context.id = asid;
90 cpumask_clear(mm_cpumask(mm));
91 }
Thomas Gleixnerbd31b852009-07-03 08:44:46 -050092 raw_spin_unlock_irqrestore(&mm->context.id_lock, flags);
Catalin Marinas11805bc2010-01-26 19:09:42 +010093
94 /*
95 * Set the mm_cpumask(mm) bit for the current CPU.
96 */
97 cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
98}
99
100/*
101 * Reset the ASID on the current CPU. This function call is broadcast
102 * from the CPU handling the ASID rollover and holding cpu_asid_lock.
103 */
104static void reset_context(void *info)
105{
106 unsigned int asid;
107 unsigned int cpu = smp_processor_id();
Catalin Marinase3239692011-11-28 15:59:10 +0000108 struct mm_struct *mm = current->active_mm;
Catalin Marinas11805bc2010-01-26 19:09:42 +0100109
110 smp_rmb();
Russell Kinga0a54d32011-06-09 10:12:41 +0100111 asid = cpu_last_asid + cpu + 1;
Catalin Marinas11805bc2010-01-26 19:09:42 +0100112
113 flush_context();
114 set_mm_context(mm, asid);
115
116 /* set the new ASID */
Will Deacon3c5f7e72011-05-31 15:38:43 +0100117 cpu_switch_mm(mm->pgd, mm);
Catalin Marinas11805bc2010-01-26 19:09:42 +0100118}
119
120#else
121
122static inline void set_mm_context(struct mm_struct *mm, unsigned int asid)
123{
124 mm->context.id = asid;
125 cpumask_copy(mm_cpumask(mm), cpumask_of(smp_processor_id()));
126}
127
128#endif
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130void __new_context(struct mm_struct *mm)
131{
132 unsigned int asid;
133
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500134 raw_spin_lock(&cpu_asid_lock);
Catalin Marinas11805bc2010-01-26 19:09:42 +0100135#ifdef CONFIG_SMP
136 /*
137 * Check the ASID again, in case the change was broadcast from
138 * another CPU before we acquired the lock.
139 */
140 if (unlikely(((mm->context.id ^ cpu_last_asid) >> ASID_BITS) == 0)) {
141 cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500142 raw_spin_unlock(&cpu_asid_lock);
Catalin Marinas11805bc2010-01-26 19:09:42 +0100143 return;
144 }
145#endif
146 /*
147 * At this point, it is guaranteed that the current mm (with
148 * an old ASID) isn't active on any other CPU since the ASIDs
149 * are changed simultaneously via IPI.
150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 asid = ++cpu_last_asid;
152 if (asid == 0)
Russell King8678c1f2007-05-08 20:03:09 +0100153 asid = cpu_last_asid = ASID_FIRST_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /*
156 * If we've used up all our ASIDs, we need
157 * to start a new version and flush the TLB.
158 */
Russell King8678c1f2007-05-08 20:03:09 +0100159 if (unlikely((asid & ~ASID_MASK) == 0)) {
Russell Kinga0a54d32011-06-09 10:12:41 +0100160 asid = cpu_last_asid + smp_processor_id() + 1;
Catalin Marinas11805bc2010-01-26 19:09:42 +0100161 flush_context();
162#ifdef CONFIG_SMP
163 smp_wmb();
164 smp_call_function(reset_context, NULL, 1);
165#endif
Russell Kinga0a54d32011-06-09 10:12:41 +0100166 cpu_last_asid += NR_CPUS;
Catalin Marinas9d99df42007-02-05 14:47:40 +0100167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Catalin Marinas11805bc2010-01-26 19:09:42 +0100169 set_mm_context(mm, asid);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500170 raw_spin_unlock(&cpu_asid_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}