| 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 | 450ea48 | 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 |  | 
| Will Deacon | a7a6f92 | 2012-01-23 13:48:48 -0800 | [diff] [blame] | 28 | static void write_contextidr(u32 contextidr) | 
|  | 29 | { | 
| Laura Abbott | 445eb9a | 2012-01-25 10:42:13 -0800 | [diff] [blame] | 30 | uncached_logk(LOGK_CTXID, (void *)contextidr); | 
| Will Deacon | a7a6f92 | 2012-01-23 13:48:48 -0800 | [diff] [blame] | 31 | asm("mcr	p15, 0, %0, c13, c0, 1" : : "r" (contextidr)); | 
|  | 32 | isb(); | 
|  | 33 | } | 
|  | 34 |  | 
|  | 35 | #ifdef CONFIG_PID_IN_CONTEXTIDR | 
|  | 36 | static u32 read_contextidr(void) | 
|  | 37 | { | 
|  | 38 | u32 contextidr; | 
|  | 39 | asm("mrc	p15, 0, %0, c13, c0, 1" : "=r" (contextidr)); | 
|  | 40 | return contextidr; | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | static 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 |  | 
|  | 65 | static struct notifier_block contextidr_notifier_block = { | 
|  | 66 | .notifier_call = contextidr_notifier, | 
|  | 67 | }; | 
|  | 68 |  | 
|  | 69 | static int __init contextidr_notifier_init(void) | 
|  | 70 | { | 
|  | 71 | return thread_register_notifier(&contextidr_notifier_block); | 
|  | 72 | } | 
|  | 73 | arch_initcall(contextidr_notifier_init); | 
|  | 74 |  | 
|  | 75 | static 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 | 
|  | 83 | static void set_asid(unsigned int asid) | 
|  | 84 | { | 
|  | 85 | write_contextidr(asid); | 
|  | 86 | } | 
|  | 87 | #endif | 
|  | 88 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | /* | 
|  | 90 | * 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] | 91 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | */ | 
|  | 95 | void __init_new_context(struct task_struct *tsk, struct mm_struct *mm) | 
|  | 96 | { | 
|  | 97 | mm->context.id = 0; | 
| Thomas Gleixner | 450ea48 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 98 | raw_spin_lock_init(&mm->context.id_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } | 
|  | 100 |  | 
| Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 101 | static void flush_context(void) | 
|  | 102 | { | 
| Russell King | 07989b7 | 2011-06-09 10:10:27 +0100 | [diff] [blame] | 103 | /* set the reserved ASID before flushing the TLB */ | 
| Will Deacon | a7a6f92 | 2012-01-23 13:48:48 -0800 | [diff] [blame] | 104 | set_asid(0); | 
| Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 105 | 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 |  | 
|  | 114 | static 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 Gleixner | 450ea48 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 124 | raw_spin_lock_irqsave(&mm->context.id_lock, flags); | 
| Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 125 | 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 Gleixner | 450ea48 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 133 | raw_spin_unlock_irqrestore(&mm->context.id_lock, flags); | 
| Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 134 |  | 
|  | 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 | */ | 
|  | 145 | static 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 King | a0a54d3 | 2011-06-09 10:12:41 +0100 | [diff] [blame] | 159 | asid = cpu_last_asid + cpu + 1; | 
| Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 160 |  | 
|  | 161 | flush_context(); | 
|  | 162 | set_mm_context(mm, asid); | 
|  | 163 |  | 
|  | 164 | /* set the new ASID */ | 
| Will Deacon | a7a6f92 | 2012-01-23 13:48:48 -0800 | [diff] [blame] | 165 | set_asid(mm->context.id); | 
| Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 166 | } | 
|  | 167 |  | 
|  | 168 | #else | 
|  | 169 |  | 
|  | 170 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | void __new_context(struct mm_struct *mm) | 
|  | 179 | { | 
|  | 180 | unsigned int asid; | 
|  | 181 |  | 
| Thomas Gleixner | 450ea48 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 182 | raw_spin_lock(&cpu_asid_lock); | 
| Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 183 | #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 Gleixner | 450ea48 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 190 | raw_spin_unlock(&cpu_asid_lock); | 
| Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 191 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | asid = ++cpu_last_asid; | 
|  | 200 | if (asid == 0) | 
| Russell King | 8678c1f | 2007-05-08 20:03:09 +0100 | [diff] [blame] | 201 | asid = cpu_last_asid = ASID_FIRST_VERSION; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 |  | 
|  | 203 | /* | 
|  | 204 | * If we've used up all our ASIDs, we need | 
|  | 205 | * to start a new version and flush the TLB. | 
|  | 206 | */ | 
| Russell King | 8678c1f | 2007-05-08 20:03:09 +0100 | [diff] [blame] | 207 | if (unlikely((asid & ~ASID_MASK) == 0)) { | 
| Russell King | a0a54d3 | 2011-06-09 10:12:41 +0100 | [diff] [blame] | 208 | asid = cpu_last_asid + smp_processor_id() + 1; | 
| Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 209 | flush_context(); | 
|  | 210 | #ifdef CONFIG_SMP | 
|  | 211 | smp_wmb(); | 
|  | 212 | smp_call_function(reset_context, NULL, 1); | 
|  | 213 | #endif | 
| Russell King | a0a54d3 | 2011-06-09 10:12:41 +0100 | [diff] [blame] | 214 | cpu_last_asid += NR_CPUS; | 
| Catalin Marinas | 9d99df4 | 2007-02-05 14:47:40 +0100 | [diff] [blame] | 215 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 |  | 
| Catalin Marinas | 11805bc | 2010-01-26 19:09:42 +0100 | [diff] [blame] | 217 | set_mm_context(mm, asid); | 
| Thomas Gleixner | 450ea48 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 218 | raw_spin_unlock(&cpu_asid_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |