context.c 6.0 KB

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