Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Russell King | d84b471 | 2006-08-21 19:23:38 +0100 | [diff] [blame] | 2 | * linux/arch/arm/mm/context.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 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 Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 13 | #include <linux/smp.h> |
| 14 | #include <linux/percpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | #include <asm/mmu_context.h> |
Will Deacon | a7a6f92 | 2012-01-23 13:48:48 -0800 | [diff] [blame] | 17 | #include <asm/thread_notify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/tlbflush.h> |
| 19 | |
Laura Abbott | 445eb9a | 2012-01-25 10:42:13 -0800 | [diff] [blame] | 20 | #include <mach/msm_rtb.h> |
| 21 | |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 22 | static DEFINE_RAW_SPINLOCK(cpu_asid_lock); |
Russell King | 8678c1f | 2007-05-08 20:03:09 +0100 | [diff] [blame] | 23 | unsigned int cpu_last_asid = ASID_FIRST_VERSION; |
Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 24 | #ifdef CONFIG_SMP |
| 25 | DEFINE_PER_CPU(struct mm_struct *, current_mm); |
| 26 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Catalin Marinas | 14d8c95 | 2011-11-22 17:30:31 +0000 | [diff] [blame] | 28 | #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 Deacon | a7a6f92 | 2012-01-23 13:48:48 -0800 | [diff] [blame] | 43 | static void write_contextidr(u32 contextidr) |
| 44 | { |
Laura Abbott | 445eb9a | 2012-01-25 10:42:13 -0800 | [diff] [blame] | 45 | uncached_logk(LOGK_CTXID, (void *)contextidr); |
Will Deacon | a7a6f92 | 2012-01-23 13:48:48 -0800 | [diff] [blame] | 46 | asm("mcr p15, 0, %0, c13, c0, 1" : : "r" (contextidr)); |
| 47 | isb(); |
| 48 | } |
| 49 | |
| 50 | #ifdef CONFIG_PID_IN_CONTEXTIDR |
| 51 | static u32 read_contextidr(void) |
| 52 | { |
| 53 | u32 contextidr; |
| 54 | asm("mrc p15, 0, %0, c13, c0, 1" : "=r" (contextidr)); |
| 55 | return contextidr; |
| 56 | } |
| 57 | |
| 58 | static 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 | |
| 80 | static struct notifier_block contextidr_notifier_block = { |
| 81 | .notifier_call = contextidr_notifier, |
| 82 | }; |
| 83 | |
| 84 | static int __init contextidr_notifier_init(void) |
| 85 | { |
| 86 | return thread_register_notifier(&contextidr_notifier_block); |
| 87 | } |
| 88 | arch_initcall(contextidr_notifier_init); |
| 89 | |
| 90 | static 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 |
| 98 | static void set_asid(unsigned int asid) |
| 99 | { |
| 100 | write_contextidr(asid); |
| 101 | } |
| 102 | #endif |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | /* |
| 105 | * We fork()ed a process, and we need a new context for the child |
Russell King | 07989b7 | 2011-06-09 10:10:27 +0100 | [diff] [blame] | 106 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | */ |
| 110 | void __init_new_context(struct task_struct *tsk, struct mm_struct *mm) |
| 111 | { |
| 112 | mm->context.id = 0; |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 113 | raw_spin_lock_init(&mm->context.id_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 116 | static void flush_context(void) |
| 117 | { |
Russell King | 07989b7 | 2011-06-09 10:10:27 +0100 | [diff] [blame] | 118 | /* set the reserved ASID before flushing the TLB */ |
Will Deacon | a7a6f92 | 2012-01-23 13:48:48 -0800 | [diff] [blame] | 119 | set_asid(0); |
Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 120 | 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 | |
| 129 | static 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 Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 139 | raw_spin_lock_irqsave(&mm->context.id_lock, flags); |
Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 140 | 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 Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 148 | raw_spin_unlock_irqrestore(&mm->context.id_lock, flags); |
Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 149 | |
| 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 | */ |
| 160 | static 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 King | a0a54d3 | 2011-06-09 10:12:41 +0100 | [diff] [blame] | 174 | asid = cpu_last_asid + cpu + 1; |
Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 175 | |
| 176 | flush_context(); |
| 177 | set_mm_context(mm, asid); |
| 178 | |
| 179 | /* set the new ASID */ |
Will Deacon | a7a6f92 | 2012-01-23 13:48:48 -0800 | [diff] [blame] | 180 | set_asid(mm->context.id); |
Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | #else |
| 184 | |
| 185 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | void __new_context(struct mm_struct *mm) |
| 194 | { |
| 195 | unsigned int asid; |
| 196 | |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 197 | raw_spin_lock(&cpu_asid_lock); |
Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 198 | #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 Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 205 | raw_spin_unlock(&cpu_asid_lock); |
Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 206 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | asid = ++cpu_last_asid; |
| 215 | if (asid == 0) |
Russell King | 8678c1f | 2007-05-08 20:03:09 +0100 | [diff] [blame] | 216 | asid = cpu_last_asid = ASID_FIRST_VERSION; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
| 218 | /* |
| 219 | * If we've used up all our ASIDs, we need |
| 220 | * to start a new version and flush the TLB. |
| 221 | */ |
Russell King | 8678c1f | 2007-05-08 20:03:09 +0100 | [diff] [blame] | 222 | if (unlikely((asid & ~ASID_MASK) == 0)) { |
Russell King | a0a54d3 | 2011-06-09 10:12:41 +0100 | [diff] [blame] | 223 | asid = cpu_last_asid + smp_processor_id() + 1; |
Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 224 | flush_context(); |
| 225 | #ifdef CONFIG_SMP |
| 226 | smp_wmb(); |
| 227 | smp_call_function(reset_context, NULL, 1); |
| 228 | #endif |
Russell King | a0a54d3 | 2011-06-09 10:12:41 +0100 | [diff] [blame] | 229 | cpu_last_asid += NR_CPUS; |
Catalin Marinas | 9d99df4 | 2007-02-05 14:47:40 +0100 | [diff] [blame] | 230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 232 | set_mm_context(mm, asid); |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 233 | raw_spin_unlock(&cpu_asid_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |