context.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * linux/arch/arm/mm/context.c
  3. *
  4. * Copyright (C) 2002-2003 Deep Blue Solutions Ltd, all rights reserved.
  5. * Copyright (C) 2012 ARM Limited
  6. *
  7. * Author: Will Deacon <will.deacon@arm.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/sched.h>
  15. #include <linux/mm.h>
  16. #include <linux/smp.h>
  17. #include <linux/percpu.h>
  18. #include <asm/mmu_context.h>
  19. #include <asm/smp_plat.h>
  20. #include <asm/thread_notify.h>
  21. #include <asm/tlbflush.h>
  22. /*
  23. * On ARMv6, we have the following structure in the Context ID:
  24. *
  25. * 31 7 0
  26. * +-------------------------+-----------+
  27. * | process ID | ASID |
  28. * +-------------------------+-----------+
  29. * | context ID |
  30. * +-------------------------------------+
  31. *
  32. * The ASID is used to tag entries in the CPU caches and TLBs.
  33. * The context ID is used by debuggers and trace logic, and
  34. * should be unique within all running processes.
  35. *
  36. * In big endian operation, the two 32 bit words are swapped if accesed by
  37. * non 64-bit operations.
  38. */
  39. #define ASID_FIRST_VERSION (1ULL << ASID_BITS)
  40. #define NUM_USER_ASIDS ASID_FIRST_VERSION
  41. static DEFINE_RAW_SPINLOCK(cpu_asid_lock);
  42. static atomic64_t asid_generation = ATOMIC64_INIT(ASID_FIRST_VERSION);
  43. static DECLARE_BITMAP(asid_map, NUM_USER_ASIDS);
  44. static DEFINE_PER_CPU(atomic64_t, active_asids);
  45. static DEFINE_PER_CPU(u64, reserved_asids);
  46. static cpumask_t tlb_flush_pending;
  47. #ifdef CONFIG_ARM_ERRATA_798181
  48. void a15_erratum_get_cpumask(int this_cpu, struct mm_struct *mm,
  49. cpumask_t *mask)
  50. {
  51. int cpu;
  52. unsigned long flags;
  53. u64 context_id, asid;
  54. raw_spin_lock_irqsave(&cpu_asid_lock, flags);
  55. context_id = mm->context.id.counter;
  56. for_each_online_cpu(cpu) {
  57. if (cpu == this_cpu)
  58. continue;
  59. /*
  60. * We only need to send an IPI if the other CPUs are
  61. * running the same ASID as the one being invalidated.
  62. */
  63. asid = per_cpu(active_asids, cpu).counter;
  64. if (asid == 0)
  65. asid = per_cpu(reserved_asids, cpu);
  66. if (context_id == asid)
  67. cpumask_set_cpu(cpu, mask);
  68. }
  69. raw_spin_unlock_irqrestore(&cpu_asid_lock, flags);
  70. }
  71. #endif
  72. #ifdef CONFIG_ARM_LPAE
  73. static void cpu_set_reserved_ttbr0(void)
  74. {
  75. unsigned long ttbl = __pa(swapper_pg_dir);
  76. unsigned long ttbh = 0;
  77. /*
  78. * Set TTBR0 to swapper_pg_dir which contains only global entries. The
  79. * ASID is set to 0.
  80. */
  81. asm volatile(
  82. " mcrr p15, 0, %0, %1, c2 @ set TTBR0\n"
  83. :
  84. : "r" (ttbl), "r" (ttbh));
  85. isb();
  86. }
  87. #else
  88. static void cpu_set_reserved_ttbr0(void)
  89. {
  90. u32 ttb;
  91. /* Copy TTBR1 into TTBR0 */
  92. asm volatile(
  93. " mrc p15, 0, %0, c2, c0, 1 @ read TTBR1\n"
  94. " mcr p15, 0, %0, c2, c0, 0 @ set TTBR0\n"
  95. : "=r" (ttb));
  96. isb();
  97. }
  98. #endif
  99. #ifdef CONFIG_PID_IN_CONTEXTIDR
  100. static int contextidr_notifier(struct notifier_block *unused, unsigned long cmd,
  101. void *t)
  102. {
  103. u32 contextidr;
  104. pid_t pid;
  105. struct thread_info *thread = t;
  106. if (cmd != THREAD_NOTIFY_SWITCH)
  107. return NOTIFY_DONE;
  108. pid = task_pid_nr(thread->task) << ASID_BITS;
  109. asm volatile(
  110. " mrc p15, 0, %0, c13, c0, 1\n"
  111. " and %0, %0, %2\n"
  112. " orr %0, %0, %1\n"
  113. " mcr p15, 0, %0, c13, c0, 1\n"
  114. : "=r" (contextidr), "+r" (pid)
  115. : "I" (~ASID_MASK));
  116. isb();
  117. return NOTIFY_OK;
  118. }
  119. static struct notifier_block contextidr_notifier_block = {
  120. .notifier_call = contextidr_notifier,
  121. };
  122. static int __init contextidr_notifier_init(void)
  123. {
  124. return thread_register_notifier(&contextidr_notifier_block);
  125. }
  126. arch_initcall(contextidr_notifier_init);
  127. #endif
  128. static void flush_context(unsigned int cpu)
  129. {
  130. int i;
  131. u64 asid;
  132. /* Update the list of reserved ASIDs and the ASID bitmap. */
  133. bitmap_clear(asid_map, 0, NUM_USER_ASIDS);
  134. for_each_possible_cpu(i) {
  135. if (i == cpu) {
  136. asid = 0;
  137. } else {
  138. asid = atomic64_xchg(&per_cpu(active_asids, i), 0);
  139. /*
  140. * If this CPU has already been through a
  141. * rollover, but hasn't run another task in
  142. * the meantime, we must preserve its reserved
  143. * ASID, as this is the only trace we have of
  144. * the process it is still running.
  145. */
  146. if (asid == 0)
  147. asid = per_cpu(reserved_asids, i);
  148. __set_bit(asid & ~ASID_MASK, asid_map);
  149. }
  150. per_cpu(reserved_asids, i) = asid;
  151. }
  152. /* Queue a TLB invalidate and flush the I-cache if necessary. */
  153. if (!tlb_ops_need_broadcast())
  154. cpumask_set_cpu(cpu, &tlb_flush_pending);
  155. else
  156. cpumask_setall(&tlb_flush_pending);
  157. if (icache_is_vivt_asid_tagged())
  158. __flush_icache_all();
  159. }
  160. static int is_reserved_asid(u64 asid)
  161. {
  162. int cpu;
  163. for_each_possible_cpu(cpu)
  164. if (per_cpu(reserved_asids, cpu) == asid)
  165. return 1;
  166. return 0;
  167. }
  168. static u64 new_context(struct mm_struct *mm, unsigned int cpu)
  169. {
  170. u64 asid = atomic64_read(&mm->context.id);
  171. u64 generation = atomic64_read(&asid_generation);
  172. if (asid != 0 && is_reserved_asid(asid)) {
  173. /*
  174. * Our current ASID was active during a rollover, we can
  175. * continue to use it and this was just a false alarm.
  176. */
  177. asid = generation | (asid & ~ASID_MASK);
  178. } else {
  179. /*
  180. * Allocate a free ASID. If we can't find one, take a
  181. * note of the currently active ASIDs and mark the TLBs
  182. * as requiring flushes. We always count from ASID #1,
  183. * as we reserve ASID #0 to switch via TTBR0 and indicate
  184. * rollover events.
  185. */
  186. asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, 1);
  187. if (asid == NUM_USER_ASIDS) {
  188. generation = atomic64_add_return(ASID_FIRST_VERSION,
  189. &asid_generation);
  190. flush_context(cpu);
  191. asid = find_next_zero_bit(asid_map, NUM_USER_ASIDS, 1);
  192. }
  193. __set_bit(asid, asid_map);
  194. asid |= generation;
  195. cpumask_clear(mm_cpumask(mm));
  196. }
  197. return asid;
  198. }
  199. void check_and_switch_context(struct mm_struct *mm, struct task_struct *tsk)
  200. {
  201. unsigned long flags;
  202. unsigned int cpu = smp_processor_id();
  203. u64 asid;
  204. if (unlikely(mm->context.vmalloc_seq != init_mm.context.vmalloc_seq))
  205. __check_vmalloc_seq(mm);
  206. /*
  207. * Required during context switch to avoid speculative page table
  208. * walking with the wrong TTBR.
  209. */
  210. cpu_set_reserved_ttbr0();
  211. asid = atomic64_read(&mm->context.id);
  212. if (!((asid ^ atomic64_read(&asid_generation)) >> ASID_BITS)
  213. && atomic64_xchg(&per_cpu(active_asids, cpu), asid))
  214. goto switch_mm_fastpath;
  215. raw_spin_lock_irqsave(&cpu_asid_lock, flags);
  216. /* Check that our ASID belongs to the current generation. */
  217. asid = atomic64_read(&mm->context.id);
  218. if ((asid ^ atomic64_read(&asid_generation)) >> ASID_BITS) {
  219. asid = new_context(mm, cpu);
  220. atomic64_set(&mm->context.id, asid);
  221. }
  222. if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending)) {
  223. local_flush_bp_all();
  224. local_flush_tlb_all();
  225. dummy_flush_tlb_a15_erratum();
  226. }
  227. atomic64_set(&per_cpu(active_asids, cpu), asid);
  228. cpumask_set_cpu(cpu, mm_cpumask(mm));
  229. raw_spin_unlock_irqrestore(&cpu_asid_lock, flags);
  230. switch_mm_fastpath:
  231. cpu_switch_mm(mm->pgd, mm);
  232. }