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> |
| 27 | #include <asm/tlbflush.h> |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 28 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame^] | 29 | static u32 asid_bits; |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 30 | static DEFINE_RAW_SPINLOCK(cpu_asid_lock); |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 31 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame^] | 32 | static atomic64_t asid_generation; |
| 33 | static unsigned long *asid_map; |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 34 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame^] | 35 | static DEFINE_PER_CPU(atomic64_t, active_asids); |
| 36 | static DEFINE_PER_CPU(u64, reserved_asids); |
| 37 | static cpumask_t tlb_flush_pending; |
| 38 | |
| 39 | #define ASID_MASK (~GENMASK(asid_bits - 1, 0)) |
| 40 | #define ASID_FIRST_VERSION (1UL << asid_bits) |
| 41 | #define NUM_USER_ASIDS ASID_FIRST_VERSION |
| 42 | |
| 43 | static void flush_context(unsigned int cpu) |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 44 | { |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame^] | 45 | int i; |
| 46 | u64 asid; |
| 47 | |
| 48 | /* Update the list of reserved ASIDs and the ASID bitmap. */ |
| 49 | bitmap_clear(asid_map, 0, NUM_USER_ASIDS); |
| 50 | |
| 51 | /* |
| 52 | * Ensure the generation bump is observed before we xchg the |
| 53 | * active_asids. |
| 54 | */ |
| 55 | smp_wmb(); |
| 56 | |
| 57 | for_each_possible_cpu(i) { |
| 58 | asid = atomic64_xchg_relaxed(&per_cpu(active_asids, i), 0); |
| 59 | /* |
| 60 | * If this CPU has already been through a |
| 61 | * rollover, but hasn't run another task in |
| 62 | * the meantime, we must preserve its reserved |
| 63 | * ASID, as this is the only trace we have of |
| 64 | * the process it is still running. |
| 65 | */ |
| 66 | if (asid == 0) |
| 67 | asid = per_cpu(reserved_asids, i); |
| 68 | __set_bit(asid & ~ASID_MASK, asid_map); |
| 69 | per_cpu(reserved_asids, i) = asid; |
| 70 | } |
| 71 | |
| 72 | /* Queue a TLB invalidate and flush the I-cache if necessary. */ |
| 73 | cpumask_setall(&tlb_flush_pending); |
| 74 | |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 75 | if (icache_is_aivivt()) |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame^] | 76 | __flush_icache_all(); |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame^] | 79 | static int is_reserved_asid(u64 asid) |
| 80 | { |
| 81 | int cpu; |
| 82 | for_each_possible_cpu(cpu) |
| 83 | if (per_cpu(reserved_asids, cpu) == asid) |
| 84 | return 1; |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static u64 new_context(struct mm_struct *mm, unsigned int cpu) |
| 89 | { |
| 90 | static u32 cur_idx = 1; |
| 91 | u64 asid = atomic64_read(&mm->context.id); |
| 92 | u64 generation = atomic64_read(&asid_generation); |
| 93 | |
| 94 | if (asid != 0) { |
| 95 | /* |
| 96 | * If our current ASID was active during a rollover, we |
| 97 | * can continue to use it and this was just a false alarm. |
| 98 | */ |
| 99 | if (is_reserved_asid(asid)) |
| 100 | return generation | (asid & ~ASID_MASK); |
| 101 | |
| 102 | /* |
| 103 | * We had a valid ASID in a previous life, so try to re-use |
| 104 | * it if possible. |
| 105 | */ |
| 106 | asid &= ~ASID_MASK; |
| 107 | if (!__test_and_set_bit(asid, asid_map)) |
| 108 | goto bump_gen; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Allocate a free ASID. If we can't find one, take a note of the |
| 113 | * currently active ASIDs and mark the TLBs as requiring flushes. |
| 114 | * We always count from ASID #1, as we use ASID #0 when setting a |
| 115 | * reserved TTBR0 for the init_mm. |
| 116 | */ |
| 117 | asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, cur_idx); |
| 118 | if (asid != NUM_USER_ASIDS) |
| 119 | goto set_asid; |
| 120 | |
| 121 | /* We're out of ASIDs, so increment the global generation count */ |
| 122 | generation = atomic64_add_return_relaxed(ASID_FIRST_VERSION, |
| 123 | &asid_generation); |
| 124 | flush_context(cpu); |
| 125 | |
| 126 | /* We have at least 1 ASID per CPU, so this will always succeed */ |
| 127 | asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, 1); |
| 128 | |
| 129 | set_asid: |
| 130 | __set_bit(asid, asid_map); |
| 131 | cur_idx = asid; |
| 132 | |
| 133 | bump_gen: |
| 134 | asid |= generation; |
| 135 | cpumask_clear(mm_cpumask(mm)); |
| 136 | return asid; |
| 137 | } |
| 138 | |
| 139 | void check_and_switch_context(struct mm_struct *mm, unsigned int cpu) |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 140 | { |
| 141 | unsigned long flags; |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame^] | 142 | u64 asid; |
| 143 | |
| 144 | asid = atomic64_read(&mm->context.id); |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 145 | |
| 146 | /* |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame^] | 147 | * The memory ordering here is subtle. We rely on the control |
| 148 | * dependency between the generation read and the update of |
| 149 | * active_asids to ensure that we are synchronised with a |
| 150 | * parallel rollover (i.e. this pairs with the smp_wmb() in |
| 151 | * flush_context). |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 152 | */ |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame^] | 153 | if (!((asid ^ atomic64_read(&asid_generation)) >> asid_bits) |
| 154 | && atomic64_xchg_relaxed(&per_cpu(active_asids, cpu), asid)) |
| 155 | goto switch_mm_fastpath; |
| 156 | |
| 157 | raw_spin_lock_irqsave(&cpu_asid_lock, flags); |
| 158 | /* Check that our ASID belongs to the current generation. */ |
| 159 | asid = atomic64_read(&mm->context.id); |
| 160 | if ((asid ^ atomic64_read(&asid_generation)) >> asid_bits) { |
| 161 | asid = new_context(mm, cpu); |
| 162 | atomic64_set(&mm->context.id, asid); |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 163 | } |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 164 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame^] | 165 | if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending)) |
| 166 | local_flush_tlb_all(); |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 167 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame^] | 168 | atomic64_set(&per_cpu(active_asids, cpu), asid); |
| 169 | cpumask_set_cpu(cpu, mm_cpumask(mm)); |
| 170 | raw_spin_unlock_irqrestore(&cpu_asid_lock, flags); |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 171 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame^] | 172 | switch_mm_fastpath: |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 173 | cpu_switch_mm(mm->pgd, mm); |
| 174 | } |
| 175 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame^] | 176 | static int asids_init(void) |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 177 | { |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame^] | 178 | int fld = cpuid_feature_extract_field(read_cpuid(ID_AA64MMFR0_EL1), 4); |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 179 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame^] | 180 | switch (fld) { |
| 181 | default: |
| 182 | pr_warn("Unknown ASID size (%d); assuming 8-bit\n", fld); |
| 183 | /* Fallthrough */ |
| 184 | case 0: |
| 185 | asid_bits = 8; |
| 186 | break; |
| 187 | case 2: |
| 188 | asid_bits = 16; |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame^] | 191 | /* If we end up with more CPUs than ASIDs, expect things to crash */ |
| 192 | WARN_ON(NUM_USER_ASIDS < num_possible_cpus()); |
| 193 | atomic64_set(&asid_generation, ASID_FIRST_VERSION); |
| 194 | asid_map = kzalloc(BITS_TO_LONGS(NUM_USER_ASIDS) * sizeof(*asid_map), |
| 195 | GFP_KERNEL); |
| 196 | if (!asid_map) |
| 197 | panic("Failed to allocate bitmap for %lu ASIDs\n", |
| 198 | NUM_USER_ASIDS); |
| 199 | |
| 200 | pr_info("ASID allocator initialised with %lu entries\n", NUM_USER_ASIDS); |
| 201 | return 0; |
Catalin Marinas | b3901d5 | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 202 | } |
Will Deacon | 5aec715 | 2015-10-06 18:46:24 +0100 | [diff] [blame^] | 203 | early_initcall(asids_init); |