blob: c841ccecbc4d21d1fa5e20101b9e610d884dc2f1 [file] [log] [blame]
Catalin Marinasb3901d52012-03-05 11:49:28 +00001/*
2 * Based on arch/arm/mm/context.c
3 *
4 * Copyright (C) 2002-2003 Deep Blue Solutions Ltd, all rights reserved.
5 * Copyright (C) 2012 ARM Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
Will Deacon5aec7152015-10-06 18:46:24 +010020#include <linux/bitops.h>
Catalin Marinasb3901d52012-03-05 11:49:28 +000021#include <linux/sched.h>
Will Deacon5aec7152015-10-06 18:46:24 +010022#include <linux/slab.h>
Catalin Marinasb3901d52012-03-05 11:49:28 +000023#include <linux/mm.h>
Catalin Marinasb3901d52012-03-05 11:49:28 +000024
Will Deacon5aec7152015-10-06 18:46:24 +010025#include <asm/cpufeature.h>
Catalin Marinasb3901d52012-03-05 11:49:28 +000026#include <asm/mmu_context.h>
Suzuki K Poulose13f417f2016-02-23 10:31:45 +000027#include <asm/smp.h>
Catalin Marinasb3901d52012-03-05 11:49:28 +000028#include <asm/tlbflush.h>
Catalin Marinasb3901d52012-03-05 11:49:28 +000029
Will Deacon5aec7152015-10-06 18:46:24 +010030static u32 asid_bits;
Catalin Marinasb3901d52012-03-05 11:49:28 +000031static DEFINE_RAW_SPINLOCK(cpu_asid_lock);
Catalin Marinasb3901d52012-03-05 11:49:28 +000032
Will Deacon5aec7152015-10-06 18:46:24 +010033static atomic64_t asid_generation;
34static unsigned long *asid_map;
Catalin Marinasb3901d52012-03-05 11:49:28 +000035
Will Deacon5aec7152015-10-06 18:46:24 +010036static DEFINE_PER_CPU(atomic64_t, active_asids);
37static DEFINE_PER_CPU(u64, reserved_asids);
38static cpumask_t tlb_flush_pending;
39
40#define ASID_MASK (~GENMASK(asid_bits - 1, 0))
41#define ASID_FIRST_VERSION (1UL << asid_bits)
Will Deacon5914b112017-08-10 14:10:28 +010042
43#ifdef CONFIG_UNMAP_KERNEL_AT_EL0
44#define NUM_USER_ASIDS (ASID_FIRST_VERSION >> 1)
45#define asid2idx(asid) (((asid) & ~ASID_MASK) >> 1)
46#define idx2asid(idx) (((idx) << 1) & ~ASID_MASK)
47#else
48#define NUM_USER_ASIDS (ASID_FIRST_VERSION)
49#define asid2idx(asid) ((asid) & ~ASID_MASK)
50#define idx2asid(idx) asid2idx(idx)
51#endif
Will Deacon5aec7152015-10-06 18:46:24 +010052
Suzuki K Poulose038dc9c2016-02-23 10:31:44 +000053/* Get the ASIDBits supported by the current CPU */
54static u32 get_cpu_asid_bits(void)
55{
56 u32 asid;
Mark Rutland1cc6ed92016-03-04 12:54:05 +000057 int fld = cpuid_feature_extract_unsigned_field(read_cpuid(ID_AA64MMFR0_EL1),
Suzuki K Poulose038dc9c2016-02-23 10:31:44 +000058 ID_AA64MMFR0_ASID_SHIFT);
59
60 switch (fld) {
61 default:
62 pr_warn("CPU%d: Unknown ASID size (%d); assuming 8-bit\n",
63 smp_processor_id(), fld);
64 /* Fallthrough */
65 case 0:
66 asid = 8;
67 break;
68 case 2:
69 asid = 16;
70 }
71
72 return asid;
73}
74
Suzuki K Poulose13f417f2016-02-23 10:31:45 +000075/* Check if the current cpu's ASIDBits is compatible with asid_bits */
76void verify_cpu_asid_bits(void)
77{
78 u32 asid = get_cpu_asid_bits();
79
80 if (asid < asid_bits) {
81 /*
82 * We cannot decrease the ASID size at runtime, so panic if we support
83 * fewer ASID bits than the boot CPU.
84 */
85 pr_crit("CPU%d: smaller ASID size(%u) than boot CPU (%u)\n",
86 smp_processor_id(), asid, asid_bits);
Suzuki K Poulose17eebd12016-04-12 15:46:00 +010087 cpu_panic_kernel();
Suzuki K Poulose13f417f2016-02-23 10:31:45 +000088 }
89}
90
Will Deacon5aec7152015-10-06 18:46:24 +010091static void flush_context(unsigned int cpu)
Catalin Marinasb3901d52012-03-05 11:49:28 +000092{
Will Deacon5aec7152015-10-06 18:46:24 +010093 int i;
94 u64 asid;
95
96 /* Update the list of reserved ASIDs and the ASID bitmap. */
97 bitmap_clear(asid_map, 0, NUM_USER_ASIDS);
98
99 /*
100 * Ensure the generation bump is observed before we xchg the
101 * active_asids.
102 */
103 smp_wmb();
104
105 for_each_possible_cpu(i) {
106 asid = atomic64_xchg_relaxed(&per_cpu(active_asids, i), 0);
107 /*
108 * If this CPU has already been through a
109 * rollover, but hasn't run another task in
110 * the meantime, we must preserve its reserved
111 * ASID, as this is the only trace we have of
112 * the process it is still running.
113 */
114 if (asid == 0)
115 asid = per_cpu(reserved_asids, i);
Will Deacon5914b112017-08-10 14:10:28 +0100116 __set_bit(asid2idx(asid), asid_map);
Will Deacon5aec7152015-10-06 18:46:24 +0100117 per_cpu(reserved_asids, i) = asid;
118 }
119
120 /* Queue a TLB invalidate and flush the I-cache if necessary. */
121 cpumask_setall(&tlb_flush_pending);
122
Catalin Marinasb3901d52012-03-05 11:49:28 +0000123 if (icache_is_aivivt())
Will Deacon5aec7152015-10-06 18:46:24 +0100124 __flush_icache_all();
Catalin Marinasb3901d52012-03-05 11:49:28 +0000125}
126
Will Deacon0ebea8082015-11-26 13:49:39 +0000127static bool check_update_reserved_asid(u64 asid, u64 newasid)
Will Deacon5aec7152015-10-06 18:46:24 +0100128{
129 int cpu;
Will Deacon0ebea8082015-11-26 13:49:39 +0000130 bool hit = false;
131
132 /*
133 * Iterate over the set of reserved ASIDs looking for a match.
134 * If we find one, then we can update our mm to use newasid
135 * (i.e. the same ASID in the current generation) but we can't
136 * exit the loop early, since we need to ensure that all copies
137 * of the old ASID are updated to reflect the mm. Failure to do
138 * so could result in us missing the reserved ASID in a future
139 * generation.
140 */
141 for_each_possible_cpu(cpu) {
142 if (per_cpu(reserved_asids, cpu) == asid) {
143 hit = true;
144 per_cpu(reserved_asids, cpu) = newasid;
145 }
146 }
147
148 return hit;
Will Deacon5aec7152015-10-06 18:46:24 +0100149}
150
151static u64 new_context(struct mm_struct *mm, unsigned int cpu)
152{
153 static u32 cur_idx = 1;
154 u64 asid = atomic64_read(&mm->context.id);
155 u64 generation = atomic64_read(&asid_generation);
156
157 if (asid != 0) {
Will Deacon0ebea8082015-11-26 13:49:39 +0000158 u64 newasid = generation | (asid & ~ASID_MASK);
159
Will Deacon5aec7152015-10-06 18:46:24 +0100160 /*
161 * If our current ASID was active during a rollover, we
162 * can continue to use it and this was just a false alarm.
163 */
Will Deacon0ebea8082015-11-26 13:49:39 +0000164 if (check_update_reserved_asid(asid, newasid))
165 return newasid;
Will Deacon5aec7152015-10-06 18:46:24 +0100166
167 /*
168 * We had a valid ASID in a previous life, so try to re-use
169 * it if possible.
170 */
Will Deacon5914b112017-08-10 14:10:28 +0100171 if (!__test_and_set_bit(asid2idx(asid), asid_map))
Will Deacon0ebea8082015-11-26 13:49:39 +0000172 return newasid;
Will Deacon5aec7152015-10-06 18:46:24 +0100173 }
174
175 /*
176 * Allocate a free ASID. If we can't find one, take a note of the
Will Deacon5914b112017-08-10 14:10:28 +0100177 * currently active ASIDs and mark the TLBs as requiring flushes. We
178 * always count from ASID #2 (index 1), as we use ASID #0 when setting
179 * a reserved TTBR0 for the init_mm and we allocate ASIDs in even/odd
180 * pairs.
Will Deacon5aec7152015-10-06 18:46:24 +0100181 */
182 asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, cur_idx);
183 if (asid != NUM_USER_ASIDS)
184 goto set_asid;
185
186 /* We're out of ASIDs, so increment the global generation count */
187 generation = atomic64_add_return_relaxed(ASID_FIRST_VERSION,
188 &asid_generation);
189 flush_context(cpu);
190
Jean-Philippe Bruckerf7e0efc2016-06-17 18:33:00 +0100191 /* We have more ASIDs than CPUs, so this will always succeed */
Will Deacon5aec7152015-10-06 18:46:24 +0100192 asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, 1);
193
194set_asid:
195 __set_bit(asid, asid_map);
196 cur_idx = asid;
Will Deacon5914b112017-08-10 14:10:28 +0100197 return idx2asid(asid) | generation;
Will Deacon5aec7152015-10-06 18:46:24 +0100198}
199
200void check_and_switch_context(struct mm_struct *mm, unsigned int cpu)
Catalin Marinasb3901d52012-03-05 11:49:28 +0000201{
202 unsigned long flags;
Will Deacon5aec7152015-10-06 18:46:24 +0100203 u64 asid;
204
205 asid = atomic64_read(&mm->context.id);
Catalin Marinasb3901d52012-03-05 11:49:28 +0000206
207 /*
Will Deacon5aec7152015-10-06 18:46:24 +0100208 * The memory ordering here is subtle. We rely on the control
209 * dependency between the generation read and the update of
210 * active_asids to ensure that we are synchronised with a
211 * parallel rollover (i.e. this pairs with the smp_wmb() in
212 * flush_context).
Catalin Marinasb3901d52012-03-05 11:49:28 +0000213 */
Will Deacon5aec7152015-10-06 18:46:24 +0100214 if (!((asid ^ atomic64_read(&asid_generation)) >> asid_bits)
215 && atomic64_xchg_relaxed(&per_cpu(active_asids, cpu), asid))
216 goto switch_mm_fastpath;
217
218 raw_spin_lock_irqsave(&cpu_asid_lock, flags);
219 /* Check that our ASID belongs to the current generation. */
220 asid = atomic64_read(&mm->context.id);
221 if ((asid ^ atomic64_read(&asid_generation)) >> asid_bits) {
222 asid = new_context(mm, cpu);
223 atomic64_set(&mm->context.id, asid);
Catalin Marinasb3901d52012-03-05 11:49:28 +0000224 }
Catalin Marinasb3901d52012-03-05 11:49:28 +0000225
Will Deacon5aec7152015-10-06 18:46:24 +0100226 if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending))
227 local_flush_tlb_all();
Catalin Marinasb3901d52012-03-05 11:49:28 +0000228
Will Deacon5aec7152015-10-06 18:46:24 +0100229 atomic64_set(&per_cpu(active_asids, cpu), asid);
Will Deacon5aec7152015-10-06 18:46:24 +0100230 raw_spin_unlock_irqrestore(&cpu_asid_lock, flags);
Catalin Marinasb3901d52012-03-05 11:49:28 +0000231
Will Deacon5aec7152015-10-06 18:46:24 +0100232switch_mm_fastpath:
Mark Rutlandbfaccecc2018-04-12 12:11:14 +0100233
234 arm64_apply_bp_hardening();
235
Catalin Marinascfa93772016-09-02 14:54:03 +0100236 /*
237 * Defer TTBR0_EL1 setting for user threads to uaccess_enable() when
238 * emulating PAN.
239 */
240 if (!system_uses_ttbr0_pan())
241 cpu_switch_mm(mm->pgd, mm);
Catalin Marinasb3901d52012-03-05 11:49:28 +0000242}
243
Marc Zyngier95bfec62018-01-02 18:19:39 +0000244/* Errata workaround post TTBRx_EL1 update. */
245asmlinkage void post_ttbr_update_workaround(void)
246{
247 asm(ALTERNATIVE("nop; nop; nop",
248 "ic iallu; dsb nsh; isb",
249 ARM64_WORKAROUND_CAVIUM_27456,
250 CONFIG_CAVIUM_ERRATUM_27456));
251}
252
Will Deacon5aec7152015-10-06 18:46:24 +0100253static int asids_init(void)
Catalin Marinasb3901d52012-03-05 11:49:28 +0000254{
Suzuki K Poulose038dc9c2016-02-23 10:31:44 +0000255 asid_bits = get_cpu_asid_bits();
Jean-Philippe Bruckerf7e0efc2016-06-17 18:33:00 +0100256 /*
257 * Expect allocation after rollover to fail if we don't have at least
258 * one more ASID than CPUs. ASID #0 is reserved for init_mm.
259 */
260 WARN_ON(NUM_USER_ASIDS - 1 <= num_possible_cpus());
Will Deacon5aec7152015-10-06 18:46:24 +0100261 atomic64_set(&asid_generation, ASID_FIRST_VERSION);
262 asid_map = kzalloc(BITS_TO_LONGS(NUM_USER_ASIDS) * sizeof(*asid_map),
263 GFP_KERNEL);
264 if (!asid_map)
265 panic("Failed to allocate bitmap for %lu ASIDs\n",
266 NUM_USER_ASIDS);
267
268 pr_info("ASID allocator initialised with %lu entries\n", NUM_USER_ASIDS);
269 return 0;
Catalin Marinasb3901d52012-03-05 11:49:28 +0000270}
Will Deacon5aec7152015-10-06 18:46:24 +0100271early_initcall(asids_init);