blob: 2ac37372ef52f4ba4db642d39bef349798f1785a [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.
Will Deaconb5466f82012-06-15 14:47:31 +01005 * Copyright (C) 2012 ARM Limited
6 *
7 * Author: Will Deacon <will.deacon@arm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13#include <linux/init.h>
14#include <linux/sched.h>
15#include <linux/mm.h>
Catalin Marinas11805bc2010-01-26 19:09:42 +010016#include <linux/smp.h>
17#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <asm/mmu_context.h>
Will Deaconb5466f82012-06-15 14:47:31 +010020#include <asm/smp_plat.h>
Will Deacon575320d2012-07-06 15:43:03 +010021#include <asm/thread_notify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/tlbflush.h>
23
Will Deaconb5466f82012-06-15 14:47:31 +010024/*
25 * On ARMv6, we have the following structure in the Context ID:
26 *
27 * 31 7 0
28 * +-------------------------+-----------+
29 * | process ID | ASID |
30 * +-------------------------+-----------+
31 * | context ID |
32 * +-------------------------------------+
33 *
34 * The ASID is used to tag entries in the CPU caches and TLBs.
35 * The context ID is used by debuggers and trace logic, and
36 * should be unique within all running processes.
Ben Dooks9520a5b2013-02-11 12:25:06 +010037 *
38 * In big endian operation, the two 32 bit words are swapped if accesed by
39 * non 64-bit operations.
Will Deaconb5466f82012-06-15 14:47:31 +010040 */
41#define ASID_FIRST_VERSION (1ULL << ASID_BITS)
Will Deaconbf51bb82012-08-01 14:57:49 +010042#define NUM_USER_ASIDS (ASID_FIRST_VERSION - 1)
43
44#define ASID_TO_IDX(asid) ((asid & ~ASID_MASK) - 1)
45#define IDX_TO_ASID(idx) ((idx + 1) & ~ASID_MASK)
Will Deaconb5466f82012-06-15 14:47:31 +010046
Thomas Gleixnerbd31b852009-07-03 08:44:46 -050047static DEFINE_RAW_SPINLOCK(cpu_asid_lock);
Will Deaconbf51bb82012-08-01 14:57:49 +010048static atomic64_t asid_generation = ATOMIC64_INIT(ASID_FIRST_VERSION);
49static DECLARE_BITMAP(asid_map, NUM_USER_ASIDS);
Will Deaconb5466f82012-06-15 14:47:31 +010050
Catalin Marinas93dc6882013-03-26 23:35:04 +010051DEFINE_PER_CPU(atomic64_t, active_asids);
Will Deaconb5466f82012-06-15 14:47:31 +010052static DEFINE_PER_CPU(u64, reserved_asids);
53static cpumask_t tlb_flush_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Catalin Marinas14d8c952011-11-22 17:30:31 +000055#ifdef CONFIG_ARM_LPAE
Will Deaconb5466f82012-06-15 14:47:31 +010056static void cpu_set_reserved_ttbr0(void)
Will Deacon3c5f7e72011-05-31 15:38:43 +010057{
58 unsigned long ttbl = __pa(swapper_pg_dir);
59 unsigned long ttbh = 0;
60
61 /*
62 * Set TTBR0 to swapper_pg_dir which contains only global entries. The
63 * ASID is set to 0.
64 */
65 asm volatile(
66 " mcrr p15, 0, %0, %1, c2 @ set TTBR0\n"
67 :
68 : "r" (ttbl), "r" (ttbh));
69 isb();
Catalin Marinas14d8c952011-11-22 17:30:31 +000070}
71#else
Will Deaconb5466f82012-06-15 14:47:31 +010072static void cpu_set_reserved_ttbr0(void)
Will Deacon3c5f7e72011-05-31 15:38:43 +010073{
74 u32 ttb;
75 /* Copy TTBR1 into TTBR0 */
76 asm volatile(
77 " mrc p15, 0, %0, c2, c0, 1 @ read TTBR1\n"
78 " mcr p15, 0, %0, c2, c0, 0 @ set TTBR0\n"
79 : "=r" (ttb));
80 isb();
81}
Catalin Marinas14d8c952011-11-22 17:30:31 +000082#endif
83
Will Deacon575320d2012-07-06 15:43:03 +010084#ifdef CONFIG_PID_IN_CONTEXTIDR
85static int contextidr_notifier(struct notifier_block *unused, unsigned long cmd,
86 void *t)
87{
88 u32 contextidr;
89 pid_t pid;
90 struct thread_info *thread = t;
91
92 if (cmd != THREAD_NOTIFY_SWITCH)
93 return NOTIFY_DONE;
94
95 pid = task_pid_nr(thread->task) << ASID_BITS;
96 asm volatile(
97 " mrc p15, 0, %0, c13, c0, 1\n"
Will Deaconae3790b2012-08-24 15:21:52 +010098 " and %0, %0, %2\n"
99 " orr %0, %0, %1\n"
100 " mcr p15, 0, %0, c13, c0, 1\n"
Will Deacon575320d2012-07-06 15:43:03 +0100101 : "=r" (contextidr), "+r" (pid)
Will Deaconae3790b2012-08-24 15:21:52 +0100102 : "I" (~ASID_MASK));
Will Deacon575320d2012-07-06 15:43:03 +0100103 isb();
104
105 return NOTIFY_OK;
106}
107
108static struct notifier_block contextidr_notifier_block = {
109 .notifier_call = contextidr_notifier,
110};
111
112static int __init contextidr_notifier_init(void)
113{
114 return thread_register_notifier(&contextidr_notifier_block);
115}
116arch_initcall(contextidr_notifier_init);
117#endif
118
Will Deaconb5466f82012-06-15 14:47:31 +0100119static void flush_context(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Will Deaconb5466f82012-06-15 14:47:31 +0100121 int i;
Will Deaconbf51bb82012-08-01 14:57:49 +0100122 u64 asid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Will Deaconbf51bb82012-08-01 14:57:49 +0100124 /* Update the list of reserved ASIDs and the ASID bitmap. */
125 bitmap_clear(asid_map, 0, NUM_USER_ASIDS);
126 for_each_possible_cpu(i) {
127 if (i == cpu) {
128 asid = 0;
129 } else {
130 asid = atomic64_xchg(&per_cpu(active_asids, i), 0);
131 __set_bit(ASID_TO_IDX(asid), asid_map);
132 }
133 per_cpu(reserved_asids, i) = asid;
134 }
Will Deaconb5466f82012-06-15 14:47:31 +0100135
136 /* Queue a TLB invalidate and flush the I-cache if necessary. */
137 if (!tlb_ops_need_broadcast())
138 cpumask_set_cpu(cpu, &tlb_flush_pending);
139 else
140 cpumask_setall(&tlb_flush_pending);
141
142 if (icache_is_vivt_asid_tagged())
Catalin Marinas11805bc2010-01-26 19:09:42 +0100143 __flush_icache_all();
Catalin Marinas11805bc2010-01-26 19:09:42 +0100144}
145
Will Deaconbf51bb82012-08-01 14:57:49 +0100146static int is_reserved_asid(u64 asid)
Catalin Marinas11805bc2010-01-26 19:09:42 +0100147{
Will Deaconb5466f82012-06-15 14:47:31 +0100148 int cpu;
149 for_each_possible_cpu(cpu)
Will Deaconbf51bb82012-08-01 14:57:49 +0100150 if (per_cpu(reserved_asids, cpu) == asid)
Will Deaconb5466f82012-06-15 14:47:31 +0100151 return 1;
152 return 0;
153}
Catalin Marinas11805bc2010-01-26 19:09:42 +0100154
Will Deacon8a4e3a92013-02-28 17:47:36 +0100155static u64 new_context(struct mm_struct *mm, unsigned int cpu)
Will Deaconb5466f82012-06-15 14:47:31 +0100156{
Will Deacon8a4e3a92013-02-28 17:47:36 +0100157 u64 asid = atomic64_read(&mm->context.id);
Will Deaconbf51bb82012-08-01 14:57:49 +0100158 u64 generation = atomic64_read(&asid_generation);
Will Deaconb5466f82012-06-15 14:47:31 +0100159
Will Deaconbf51bb82012-08-01 14:57:49 +0100160 if (asid != 0 && is_reserved_asid(asid)) {
Catalin Marinas11805bc2010-01-26 19:09:42 +0100161 /*
Will Deaconb5466f82012-06-15 14:47:31 +0100162 * Our current ASID was active during a rollover, we can
163 * continue to use it and this was just a false alarm.
Catalin Marinas11805bc2010-01-26 19:09:42 +0100164 */
Will Deaconbf51bb82012-08-01 14:57:49 +0100165 asid = generation | (asid & ~ASID_MASK);
Will Deaconb5466f82012-06-15 14:47:31 +0100166 } else {
167 /*
168 * Allocate a free ASID. If we can't find one, take a
169 * note of the currently active ASIDs and mark the TLBs
170 * as requiring flushes.
171 */
Will Deaconbf51bb82012-08-01 14:57:49 +0100172 asid = find_first_zero_bit(asid_map, NUM_USER_ASIDS);
173 if (asid == NUM_USER_ASIDS) {
174 generation = atomic64_add_return(ASID_FIRST_VERSION,
175 &asid_generation);
176 flush_context(cpu);
177 asid = find_first_zero_bit(asid_map, NUM_USER_ASIDS);
178 }
179 __set_bit(asid, asid_map);
180 asid = generation | IDX_TO_ASID(asid);
Catalin Marinas11805bc2010-01-26 19:09:42 +0100181 cpumask_clear(mm_cpumask(mm));
182 }
Catalin Marinas11805bc2010-01-26 19:09:42 +0100183
Will Deacon8a4e3a92013-02-28 17:47:36 +0100184 return asid;
Catalin Marinas11805bc2010-01-26 19:09:42 +0100185}
186
Will Deaconb5466f82012-06-15 14:47:31 +0100187void check_and_switch_context(struct mm_struct *mm, struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Will Deaconb5466f82012-06-15 14:47:31 +0100189 unsigned long flags;
190 unsigned int cpu = smp_processor_id();
Will Deacon8a4e3a92013-02-28 17:47:36 +0100191 u64 asid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Nicolas Pitre3e996752012-11-25 03:24:32 +0100193 if (unlikely(mm->context.vmalloc_seq != init_mm.context.vmalloc_seq))
194 __check_vmalloc_seq(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 /*
Will Deaconb5466f82012-06-15 14:47:31 +0100197 * Required during context switch to avoid speculative page table
198 * walking with the wrong TTBR.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
Will Deaconb5466f82012-06-15 14:47:31 +0100200 cpu_set_reserved_ttbr0();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Will Deacon8a4e3a92013-02-28 17:47:36 +0100202 asid = atomic64_read(&mm->context.id);
203 if (!((asid ^ atomic64_read(&asid_generation)) >> ASID_BITS)
204 && atomic64_xchg(&per_cpu(active_asids, cpu), asid))
Will Deacon4b883162012-07-27 12:31:35 +0100205 goto switch_mm_fastpath;
206
Will Deaconb5466f82012-06-15 14:47:31 +0100207 raw_spin_lock_irqsave(&cpu_asid_lock, flags);
208 /* Check that our ASID belongs to the current generation. */
Will Deacon8a4e3a92013-02-28 17:47:36 +0100209 asid = atomic64_read(&mm->context.id);
210 if ((asid ^ atomic64_read(&asid_generation)) >> ASID_BITS) {
211 asid = new_context(mm, cpu);
212 atomic64_set(&mm->context.id, asid);
213 }
Will Deaconb5466f82012-06-15 14:47:31 +0100214
Will Deacon89c7e4b2013-02-28 17:48:40 +0100215 if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending)) {
216 local_flush_bp_all();
Will Deaconb5466f82012-06-15 14:47:31 +0100217 local_flush_tlb_all();
Catalin Marinas93dc6882013-03-26 23:35:04 +0100218 dummy_flush_tlb_a15_erratum();
Will Deacon89c7e4b2013-02-28 17:48:40 +0100219 }
Will Deacon37f47e32013-02-28 17:47:20 +0100220
Will Deacon8a4e3a92013-02-28 17:47:36 +0100221 atomic64_set(&per_cpu(active_asids, cpu), asid);
Will Deacon37f47e32013-02-28 17:47:20 +0100222 cpumask_set_cpu(cpu, mm_cpumask(mm));
Will Deaconb5466f82012-06-15 14:47:31 +0100223 raw_spin_unlock_irqrestore(&cpu_asid_lock, flags);
224
Will Deacon4b883162012-07-27 12:31:35 +0100225switch_mm_fastpath:
Will Deaconb5466f82012-06-15 14:47:31 +0100226 cpu_switch_mm(mm->pgd, mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}