context.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. #include <linux/bitops.h>
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. #include <linux/mm.h>
  23. #include <asm/cpufeature.h>
  24. #include <asm/mmu_context.h>
  25. #include <asm/smp.h>
  26. #include <asm/tlbflush.h>
  27. static u32 asid_bits;
  28. static DEFINE_RAW_SPINLOCK(cpu_asid_lock);
  29. static atomic64_t asid_generation;
  30. static unsigned long *asid_map;
  31. static DEFINE_PER_CPU(atomic64_t, active_asids);
  32. static DEFINE_PER_CPU(u64, reserved_asids);
  33. static cpumask_t tlb_flush_pending;
  34. #define ASID_MASK (~GENMASK(asid_bits - 1, 0))
  35. #define ASID_FIRST_VERSION (1UL << asid_bits)
  36. #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
  37. #define NUM_USER_ASIDS (ASID_FIRST_VERSION >> 1)
  38. #define asid2idx(asid) (((asid) & ~ASID_MASK) >> 1)
  39. #define idx2asid(idx) (((idx) << 1) & ~ASID_MASK)
  40. #else
  41. #define NUM_USER_ASIDS (ASID_FIRST_VERSION)
  42. #define asid2idx(asid) ((asid) & ~ASID_MASK)
  43. #define idx2asid(idx) asid2idx(idx)
  44. #endif
  45. /* Get the ASIDBits supported by the current CPU */
  46. static u32 get_cpu_asid_bits(void)
  47. {
  48. u32 asid;
  49. int fld = cpuid_feature_extract_unsigned_field(read_cpuid(ID_AA64MMFR0_EL1),
  50. ID_AA64MMFR0_ASID_SHIFT);
  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. return asid;
  63. }
  64. /* Check if the current cpu's ASIDBits is compatible with asid_bits */
  65. void verify_cpu_asid_bits(void)
  66. {
  67. u32 asid = get_cpu_asid_bits();
  68. if (asid < asid_bits) {
  69. /*
  70. * We cannot decrease the ASID size at runtime, so panic if we support
  71. * fewer ASID bits than the boot CPU.
  72. */
  73. pr_crit("CPU%d: smaller ASID size(%u) than boot CPU (%u)\n",
  74. smp_processor_id(), asid, asid_bits);
  75. cpu_panic_kernel();
  76. }
  77. }
  78. static void flush_context(unsigned int cpu)
  79. {
  80. int i;
  81. u64 asid;
  82. /* Update the list of reserved ASIDs and the ASID bitmap. */
  83. bitmap_clear(asid_map, 0, NUM_USER_ASIDS);
  84. /*
  85. * Ensure the generation bump is observed before we xchg the
  86. * active_asids.
  87. */
  88. smp_wmb();
  89. for_each_possible_cpu(i) {
  90. asid = atomic64_xchg_relaxed(&per_cpu(active_asids, i), 0);
  91. /*
  92. * If this CPU has already been through a
  93. * rollover, but hasn't run another task in
  94. * the meantime, we must preserve its reserved
  95. * ASID, as this is the only trace we have of
  96. * the process it is still running.
  97. */
  98. if (asid == 0)
  99. asid = per_cpu(reserved_asids, i);
  100. __set_bit(asid2idx(asid), asid_map);
  101. per_cpu(reserved_asids, i) = asid;
  102. }
  103. /* Queue a TLB invalidate and flush the I-cache if necessary. */
  104. cpumask_setall(&tlb_flush_pending);
  105. if (icache_is_aivivt())
  106. __flush_icache_all();
  107. }
  108. static bool check_update_reserved_asid(u64 asid, u64 newasid)
  109. {
  110. int cpu;
  111. bool hit = false;
  112. /*
  113. * Iterate over the set of reserved ASIDs looking for a match.
  114. * If we find one, then we can update our mm to use newasid
  115. * (i.e. the same ASID in the current generation) but we can't
  116. * exit the loop early, since we need to ensure that all copies
  117. * of the old ASID are updated to reflect the mm. Failure to do
  118. * so could result in us missing the reserved ASID in a future
  119. * generation.
  120. */
  121. for_each_possible_cpu(cpu) {
  122. if (per_cpu(reserved_asids, cpu) == asid) {
  123. hit = true;
  124. per_cpu(reserved_asids, cpu) = newasid;
  125. }
  126. }
  127. return hit;
  128. }
  129. static u64 new_context(struct mm_struct *mm, unsigned int cpu)
  130. {
  131. static u32 cur_idx = 1;
  132. u64 asid = atomic64_read(&mm->context.id);
  133. u64 generation = atomic64_read(&asid_generation);
  134. if (asid != 0) {
  135. u64 newasid = generation | (asid & ~ASID_MASK);
  136. /*
  137. * If our current ASID was active during a rollover, we
  138. * can continue to use it and this was just a false alarm.
  139. */
  140. if (check_update_reserved_asid(asid, newasid))
  141. return newasid;
  142. /*
  143. * We had a valid ASID in a previous life, so try to re-use
  144. * it if possible.
  145. */
  146. if (!__test_and_set_bit(asid2idx(asid), asid_map))
  147. return newasid;
  148. }
  149. /*
  150. * Allocate a free ASID. If we can't find one, take a note of the
  151. * currently active ASIDs and mark the TLBs as requiring flushes. We
  152. * always count from ASID #2 (index 1), as we use ASID #0 when setting
  153. * a reserved TTBR0 for the init_mm and we allocate ASIDs in even/odd
  154. * pairs.
  155. */
  156. asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, cur_idx);
  157. if (asid != NUM_USER_ASIDS)
  158. goto set_asid;
  159. /* We're out of ASIDs, so increment the global generation count */
  160. generation = atomic64_add_return_relaxed(ASID_FIRST_VERSION,
  161. &asid_generation);
  162. flush_context(cpu);
  163. /* We have more ASIDs than CPUs, so this will always succeed */
  164. asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, 1);
  165. set_asid:
  166. __set_bit(asid, asid_map);
  167. cur_idx = asid;
  168. return idx2asid(asid) | generation;
  169. }
  170. void check_and_switch_context(struct mm_struct *mm, unsigned int cpu)
  171. {
  172. unsigned long flags;
  173. u64 asid;
  174. asid = atomic64_read(&mm->context.id);
  175. /*
  176. * The memory ordering here is subtle. We rely on the control
  177. * dependency between the generation read and the update of
  178. * active_asids to ensure that we are synchronised with a
  179. * parallel rollover (i.e. this pairs with the smp_wmb() in
  180. * flush_context).
  181. */
  182. if (!((asid ^ atomic64_read(&asid_generation)) >> asid_bits)
  183. && atomic64_xchg_relaxed(&per_cpu(active_asids, cpu), asid))
  184. goto switch_mm_fastpath;
  185. raw_spin_lock_irqsave(&cpu_asid_lock, flags);
  186. /* Check that our ASID belongs to the current generation. */
  187. asid = atomic64_read(&mm->context.id);
  188. if ((asid ^ atomic64_read(&asid_generation)) >> asid_bits) {
  189. asid = new_context(mm, cpu);
  190. atomic64_set(&mm->context.id, asid);
  191. }
  192. if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending))
  193. local_flush_tlb_all();
  194. atomic64_set(&per_cpu(active_asids, cpu), asid);
  195. raw_spin_unlock_irqrestore(&cpu_asid_lock, flags);
  196. switch_mm_fastpath:
  197. arm64_apply_bp_hardening();
  198. cpu_switch_mm(mm->pgd, mm);
  199. }
  200. /* Errata workaround post TTBRx_EL1 update. */
  201. asmlinkage void post_ttbr_update_workaround(void)
  202. {
  203. asm(ALTERNATIVE("nop; nop; nop",
  204. "ic iallu; dsb nsh; isb",
  205. ARM64_WORKAROUND_CAVIUM_27456,
  206. CONFIG_CAVIUM_ERRATUM_27456));
  207. }
  208. static int asids_init(void)
  209. {
  210. asid_bits = get_cpu_asid_bits();
  211. /*
  212. * Expect allocation after rollover to fail if we don't have at least
  213. * one more ASID than CPUs. ASID #0 is reserved for init_mm.
  214. */
  215. WARN_ON(NUM_USER_ASIDS - 1 <= num_possible_cpus());
  216. atomic64_set(&asid_generation, ASID_FIRST_VERSION);
  217. asid_map = kzalloc(BITS_TO_LONGS(NUM_USER_ASIDS) * sizeof(*asid_map),
  218. GFP_KERNEL);
  219. if (!asid_map)
  220. panic("Failed to allocate bitmap for %lu ASIDs\n",
  221. NUM_USER_ASIDS);
  222. pr_info("ASID allocator initialised with %lu entries\n", NUM_USER_ASIDS);
  223. return 0;
  224. }
  225. early_initcall(asids_init);