Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 1 | /* |
| 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 Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 20 | #include <linux/bitops.h> |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 21 | #include <linux/sched.h> |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 22 | #include <linux/slab.h> |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 23 | #include <linux/mm.h> |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 24 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 25 | #include <asm/cpufeature.h> |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 26 | #include <asm/mmu_context.h> |
Suzuki K Poulose | 13f417f | 2016-02-23 10:31:45 +0000 | [diff] [blame] | 27 | #include <asm/smp.h> |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 28 | #include <asm/tlbflush.h> |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 29 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 30 | static u32 asid_bits; |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 31 | static DEFINE_RAW_SPINLOCK(cpu_asid_lock); |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 32 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 33 | static atomic64_t asid_generation; |
| 34 | static unsigned long *asid_map; |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 35 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 36 | static DEFINE_PER_CPU(atomic64_t, active_asids); |
| 37 | static DEFINE_PER_CPU(u64, reserved_asids); |
| 38 | static cpumask_t tlb_flush_pending; |
| 39 | |
| 40 | #define ASID_MASK (~GENMASK(asid_bits - 1, 0)) |
| 41 | #define ASID_FIRST_VERSION (1UL << asid_bits) |
| 42 | #define NUM_USER_ASIDS ASID_FIRST_VERSION |
| 43 | |
Suzuki K Poulose | 038dc9c | 2016-02-23 10:31:44 +0000 | [diff] [blame] | 44 | /* Get the ASIDBits supported by the current CPU */ |
| 45 | static u32 get_cpu_asid_bits(void) |
| 46 | { |
| 47 | u32 asid; |
Mark Rutland | 1cc6ed9 | 2016-03-04 12:54:05 +0000 | [diff] [blame] | 48 | int fld = cpuid_feature_extract_unsigned_field(read_cpuid(ID_AA64MMFR0_EL1), |
Suzuki K Poulose | 038dc9c | 2016-02-23 10:31:44 +0000 | [diff] [blame] | 49 | ID_AA64MMFR0_ASID_SHIFT); |
| 50 | |
| 51 | switch (fld) { |
| 52 | default: |
| 53 | pr_warn("CPU%d: Unknown ASID size (%d); assuming 8-bit\n", |
| 54 | smp_processor_id(), fld); |
| 55 | /* Fallthrough */ |
| 56 | case 0: |
| 57 | asid = 8; |
| 58 | break; |
| 59 | case 2: |
| 60 | asid = 16; |
| 61 | } |
| 62 | |
| 63 | return asid; |
| 64 | } |
| 65 | |
Suzuki K Poulose | 13f417f | 2016-02-23 10:31:45 +0000 | [diff] [blame] | 66 | /* Check if the current cpu's ASIDBits is compatible with asid_bits */ |
| 67 | void verify_cpu_asid_bits(void) |
| 68 | { |
| 69 | u32 asid = get_cpu_asid_bits(); |
| 70 | |
| 71 | if (asid < asid_bits) { |
| 72 | /* |
| 73 | * We cannot decrease the ASID size at runtime, so panic if we support |
| 74 | * fewer ASID bits than the boot CPU. |
| 75 | */ |
| 76 | pr_crit("CPU%d: smaller ASID size(%u) than boot CPU (%u)\n", |
| 77 | smp_processor_id(), asid, asid_bits); |
Suzuki K Poulose | 17eebd1 | 2016-04-12 15:46:00 +0100 | [diff] [blame] | 78 | cpu_panic_kernel(); |
Suzuki K Poulose | 13f417f | 2016-02-23 10:31:45 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Christopher Covington | 38fd94b | 2017-02-08 15:08:37 -0500 | [diff] [blame] | 82 | static void set_reserved_asid_bits(void) |
| 83 | { |
| 84 | if (IS_ENABLED(CONFIG_QCOM_FALKOR_ERRATUM_1003) && |
| 85 | cpus_have_const_cap(ARM64_WORKAROUND_QCOM_FALKOR_E1003)) |
| 86 | __set_bit(FALKOR_RESERVED_ASID, asid_map); |
| 87 | } |
| 88 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 89 | static void flush_context(unsigned int cpu) |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 90 | { |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 91 | int i; |
| 92 | u64 asid; |
| 93 | |
| 94 | /* Update the list of reserved ASIDs and the ASID bitmap. */ |
| 95 | bitmap_clear(asid_map, 0, NUM_USER_ASIDS); |
| 96 | |
Christopher Covington | 38fd94b | 2017-02-08 15:08:37 -0500 | [diff] [blame] | 97 | set_reserved_asid_bits(); |
| 98 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 99 | for_each_possible_cpu(i) { |
| 100 | asid = atomic64_xchg_relaxed(&per_cpu(active_asids, i), 0); |
| 101 | /* |
| 102 | * If this CPU has already been through a |
| 103 | * rollover, but hasn't run another task in |
| 104 | * the meantime, we must preserve its reserved |
| 105 | * ASID, as this is the only trace we have of |
| 106 | * the process it is still running. |
| 107 | */ |
| 108 | if (asid == 0) |
| 109 | asid = per_cpu(reserved_asids, i); |
| 110 | __set_bit(asid & ~ASID_MASK, asid_map); |
| 111 | per_cpu(reserved_asids, i) = asid; |
| 112 | } |
| 113 | |
Mark Rutland | f81a348 | 2017-11-21 11:59:13 +0000 | [diff] [blame] | 114 | /* |
| 115 | * Queue a TLB invalidation for each CPU to perform on next |
| 116 | * context-switch |
| 117 | */ |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 118 | cpumask_setall(&tlb_flush_pending); |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Will Deacon | 0ebea80 | 2015-11-26 13:49:39 +0000 | [diff] [blame] | 121 | static bool check_update_reserved_asid(u64 asid, u64 newasid) |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 122 | { |
| 123 | int cpu; |
Will Deacon | 0ebea80 | 2015-11-26 13:49:39 +0000 | [diff] [blame] | 124 | bool hit = false; |
| 125 | |
| 126 | /* |
| 127 | * Iterate over the set of reserved ASIDs looking for a match. |
| 128 | * If we find one, then we can update our mm to use newasid |
| 129 | * (i.e. the same ASID in the current generation) but we can't |
| 130 | * exit the loop early, since we need to ensure that all copies |
| 131 | * of the old ASID are updated to reflect the mm. Failure to do |
| 132 | * so could result in us missing the reserved ASID in a future |
| 133 | * generation. |
| 134 | */ |
| 135 | for_each_possible_cpu(cpu) { |
| 136 | if (per_cpu(reserved_asids, cpu) == asid) { |
| 137 | hit = true; |
| 138 | per_cpu(reserved_asids, cpu) = newasid; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return hit; |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static u64 new_context(struct mm_struct *mm, unsigned int cpu) |
| 146 | { |
| 147 | static u32 cur_idx = 1; |
| 148 | u64 asid = atomic64_read(&mm->context.id); |
| 149 | u64 generation = atomic64_read(&asid_generation); |
| 150 | |
| 151 | if (asid != 0) { |
Will Deacon | 0ebea80 | 2015-11-26 13:49:39 +0000 | [diff] [blame] | 152 | u64 newasid = generation | (asid & ~ASID_MASK); |
| 153 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 154 | /* |
| 155 | * If our current ASID was active during a rollover, we |
| 156 | * can continue to use it and this was just a false alarm. |
| 157 | */ |
Will Deacon | 0ebea80 | 2015-11-26 13:49:39 +0000 | [diff] [blame] | 158 | if (check_update_reserved_asid(asid, newasid)) |
| 159 | return newasid; |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | * We had a valid ASID in a previous life, so try to re-use |
| 163 | * it if possible. |
| 164 | */ |
| 165 | asid &= ~ASID_MASK; |
| 166 | if (!__test_and_set_bit(asid, asid_map)) |
Will Deacon | 0ebea80 | 2015-11-26 13:49:39 +0000 | [diff] [blame] | 167 | return newasid; |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Allocate a free ASID. If we can't find one, take a note of the |
| 172 | * currently active ASIDs and mark the TLBs as requiring flushes. |
| 173 | * We always count from ASID #1, as we use ASID #0 when setting a |
| 174 | * reserved TTBR0 for the init_mm. |
| 175 | */ |
| 176 | asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, cur_idx); |
| 177 | if (asid != NUM_USER_ASIDS) |
| 178 | goto set_asid; |
| 179 | |
| 180 | /* We're out of ASIDs, so increment the global generation count */ |
| 181 | generation = atomic64_add_return_relaxed(ASID_FIRST_VERSION, |
| 182 | &asid_generation); |
| 183 | flush_context(cpu); |
| 184 | |
Jean-Philippe Brucker | f7e0efc | 2016-06-17 18:33:00 +0100 | [diff] [blame] | 185 | /* We have more ASIDs than CPUs, so this will always succeed */ |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 186 | asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, 1); |
| 187 | |
| 188 | set_asid: |
| 189 | __set_bit(asid, asid_map); |
| 190 | cur_idx = asid; |
Will Deacon | 0ebea80 | 2015-11-26 13:49:39 +0000 | [diff] [blame] | 191 | return asid | generation; |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void check_and_switch_context(struct mm_struct *mm, unsigned int cpu) |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 195 | { |
| 196 | unsigned long flags; |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 197 | u64 asid; |
| 198 | |
| 199 | asid = atomic64_read(&mm->context.id); |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 200 | |
| 201 | /* |
Will Deacon | 3a33c76 | 2017-11-30 18:25:17 +0000 | [diff] [blame^] | 202 | * The memory ordering here is subtle. |
| 203 | * If our ASID matches the current generation, then we update |
| 204 | * our active_asids entry with a relaxed xchg. Racing with a |
| 205 | * concurrent rollover means that either: |
| 206 | * |
| 207 | * - We get a zero back from the xchg and end up waiting on the |
| 208 | * lock. Taking the lock synchronises with the rollover and so |
| 209 | * we are forced to see the updated generation. |
| 210 | * |
| 211 | * - We get a valid ASID back from the xchg, which means the |
| 212 | * relaxed xchg in flush_context will treat us as reserved |
| 213 | * because atomic RmWs are totally ordered for a given location. |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 214 | */ |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 215 | if (!((asid ^ atomic64_read(&asid_generation)) >> asid_bits) |
| 216 | && atomic64_xchg_relaxed(&per_cpu(active_asids, cpu), asid)) |
| 217 | goto switch_mm_fastpath; |
| 218 | |
| 219 | raw_spin_lock_irqsave(&cpu_asid_lock, flags); |
| 220 | /* Check that our ASID belongs to the current generation. */ |
| 221 | asid = atomic64_read(&mm->context.id); |
| 222 | if ((asid ^ atomic64_read(&asid_generation)) >> asid_bits) { |
| 223 | asid = new_context(mm, cpu); |
| 224 | atomic64_set(&mm->context.id, asid); |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 225 | } |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 226 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 227 | if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending)) |
| 228 | local_flush_tlb_all(); |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 229 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 230 | atomic64_set(&per_cpu(active_asids, cpu), asid); |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 231 | raw_spin_unlock_irqrestore(&cpu_asid_lock, flags); |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 232 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 233 | switch_mm_fastpath: |
Catalin Marinas | 39bc88e | 2016-09-02 14:54:03 +0100 | [diff] [blame] | 234 | /* |
| 235 | * Defer TTBR0_EL1 setting for user threads to uaccess_enable() when |
| 236 | * emulating PAN. |
| 237 | */ |
| 238 | if (!system_uses_ttbr0_pan()) |
| 239 | cpu_switch_mm(mm->pgd, mm); |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 242 | static int asids_init(void) |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 243 | { |
Suzuki K Poulose | 038dc9c | 2016-02-23 10:31:44 +0000 | [diff] [blame] | 244 | asid_bits = get_cpu_asid_bits(); |
Jean-Philippe Brucker | f7e0efc | 2016-06-17 18:33:00 +0100 | [diff] [blame] | 245 | /* |
| 246 | * Expect allocation after rollover to fail if we don't have at least |
| 247 | * one more ASID than CPUs. ASID #0 is reserved for init_mm. |
| 248 | */ |
| 249 | WARN_ON(NUM_USER_ASIDS - 1 <= num_possible_cpus()); |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 250 | atomic64_set(&asid_generation, ASID_FIRST_VERSION); |
| 251 | asid_map = kzalloc(BITS_TO_LONGS(NUM_USER_ASIDS) * sizeof(*asid_map), |
| 252 | GFP_KERNEL); |
| 253 | if (!asid_map) |
| 254 | panic("Failed to allocate bitmap for %lu ASIDs\n", |
| 255 | NUM_USER_ASIDS); |
| 256 | |
Christopher Covington | 38fd94b | 2017-02-08 15:08:37 -0500 | [diff] [blame] | 257 | set_reserved_asid_bits(); |
| 258 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 259 | pr_info("ASID allocator initialised with %lu entries\n", NUM_USER_ASIDS); |
| 260 | return 0; |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 261 | } |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame] | 262 | early_initcall(asids_init); |