context.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #include <asm/proc-fns.h>
  23. /*
  24. * On ARMv6, we have the following structure in the Context ID:
  25. *
  26. * 31 7 0
  27. * +-------------------------+-----------+
  28. * | process ID | ASID |
  29. * +-------------------------+-----------+
  30. * | context ID |
  31. * +-------------------------------------+
  32. *
  33. * The ASID is used to tag entries in the CPU caches and TLBs.
  34. * The context ID is used by debuggers and trace logic, and
  35. * should be unique within all running processes.
  36. *
  37. * In big endian operation, the two 32 bit words are swapped if accesed by
  38. * non 64-bit operations.
  39. */
  40. #define ASID_FIRST_VERSION (1ULL << ASID_BITS)
  41. #define NUM_USER_ASIDS (ASID_FIRST_VERSION - 1)
  42. #define ASID_TO_IDX(asid) ((asid & ~ASID_MASK) - 1)
  43. #define IDX_TO_ASID(idx) ((idx + 1) & ~ASID_MASK)
  44. static DEFINE_RAW_SPINLOCK(cpu_asid_lock);
  45. static atomic64_t asid_generation = ATOMIC64_INIT(ASID_FIRST_VERSION);
  46. static DECLARE_BITMAP(asid_map, NUM_USER_ASIDS);
  47. DEFINE_PER_CPU(atomic64_t, active_asids);
  48. static DEFINE_PER_CPU(u64, reserved_asids);
  49. static cpumask_t tlb_flush_pending;
  50. #ifdef CONFIG_ARM_LPAE
  51. static void cpu_set_reserved_ttbr0(void)
  52. {
  53. /*
  54. * Set TTBR0 to swapper_pg_dir which contains only global entries. The
  55. * ASID is set to 0.
  56. */
  57. cpu_set_ttbr(0, __pa(swapper_pg_dir));
  58. isb();
  59. }
  60. #else
  61. static void cpu_set_reserved_ttbr0(void)
  62. {
  63. u32 ttb;
  64. /* Copy TTBR1 into TTBR0 */
  65. asm volatile(
  66. " mrc p15, 0, %0, c2, c0, 1 @ read TTBR1\n"
  67. " mcr p15, 0, %0, c2, c0, 0 @ set TTBR0\n"
  68. : "=r" (ttb));
  69. isb();
  70. }
  71. #endif
  72. #ifdef CONFIG_PID_IN_CONTEXTIDR
  73. static int contextidr_notifier(struct notifier_block *unused, unsigned long cmd,
  74. void *t)
  75. {
  76. u32 contextidr;
  77. pid_t pid;
  78. struct thread_info *thread = t;
  79. if (cmd != THREAD_NOTIFY_SWITCH)
  80. return NOTIFY_DONE;
  81. pid = task_pid_nr(thread->task) << ASID_BITS;
  82. asm volatile(
  83. " mrc p15, 0, %0, c13, c0, 1\n"
  84. " and %0, %0, %2\n"
  85. " orr %0, %0, %1\n"
  86. " mcr p15, 0, %0, c13, c0, 1\n"
  87. : "=r" (contextidr), "+r" (pid)
  88. : "I" (~ASID_MASK));
  89. isb();
  90. return NOTIFY_OK;
  91. }
  92. static struct notifier_block contextidr_notifier_block = {
  93. .notifier_call = contextidr_notifier,
  94. };
  95. static int __init contextidr_notifier_init(void)
  96. {
  97. return thread_register_notifier(&contextidr_notifier_block);
  98. }
  99. arch_initcall(contextidr_notifier_init);
  100. #endif
  101. static void flush_context(unsigned int cpu)
  102. {
  103. int i;
  104. u64 asid;
  105. /* Update the list of reserved ASIDs and the ASID bitmap. */
  106. bitmap_clear(asid_map, 0, NUM_USER_ASIDS);
  107. for_each_possible_cpu(i) {
  108. if (i == cpu) {
  109. asid = 0;
  110. } else {
  111. asid = atomic64_xchg(&per_cpu(active_asids, i), 0);
  112. __set_bit(ASID_TO_IDX(asid), asid_map);
  113. }
  114. per_cpu(reserved_asids, i) = asid;
  115. }
  116. /* Queue a TLB invalidate and flush the I-cache if necessary. */
  117. if (!tlb_ops_need_broadcast())
  118. cpumask_set_cpu(cpu, &tlb_flush_pending);
  119. else
  120. cpumask_setall(&tlb_flush_pending);
  121. if (icache_is_vivt_asid_tagged())
  122. __flush_icache_all();
  123. }
  124. static int is_reserved_asid(u64 asid)
  125. {
  126. int cpu;
  127. for_each_possible_cpu(cpu)
  128. if (per_cpu(reserved_asids, cpu) == asid)
  129. return 1;
  130. return 0;
  131. }
  132. static u64 new_context(struct mm_struct *mm, unsigned int cpu)
  133. {
  134. u64 asid = atomic64_read(&mm->context.id);
  135. u64 generation = atomic64_read(&asid_generation);
  136. if (asid != 0 && is_reserved_asid(asid)) {
  137. /*
  138. * Our current ASID was active during a rollover, we can
  139. * continue to use it and this was just a false alarm.
  140. */
  141. asid = generation | (asid & ~ASID_MASK);
  142. } else {
  143. /*
  144. * Allocate a free ASID. If we can't find one, take a
  145. * note of the currently active ASIDs and mark the TLBs
  146. * as requiring flushes.
  147. */
  148. asid = find_first_zero_bit(asid_map, NUM_USER_ASIDS);
  149. if (asid == NUM_USER_ASIDS) {
  150. generation = atomic64_add_return(ASID_FIRST_VERSION,
  151. &asid_generation);
  152. flush_context(cpu);
  153. asid = find_first_zero_bit(asid_map, NUM_USER_ASIDS);
  154. }
  155. __set_bit(asid, asid_map);
  156. asid = generation | IDX_TO_ASID(asid);
  157. cpumask_clear(mm_cpumask(mm));
  158. }
  159. return asid;
  160. }
  161. void check_and_switch_context(struct mm_struct *mm, struct task_struct *tsk)
  162. {
  163. unsigned long flags;
  164. unsigned int cpu = smp_processor_id();
  165. u64 asid;
  166. if (unlikely(mm->context.vmalloc_seq != init_mm.context.vmalloc_seq))
  167. __check_vmalloc_seq(mm);
  168. /*
  169. * Required during context switch to avoid speculative page table
  170. * walking with the wrong TTBR.
  171. */
  172. cpu_set_reserved_ttbr0();
  173. asid = atomic64_read(&mm->context.id);
  174. if (!((asid ^ atomic64_read(&asid_generation)) >> ASID_BITS)
  175. && atomic64_xchg(&per_cpu(active_asids, cpu), asid))
  176. goto switch_mm_fastpath;
  177. raw_spin_lock_irqsave(&cpu_asid_lock, flags);
  178. /* Check that our ASID belongs to the current generation. */
  179. asid = atomic64_read(&mm->context.id);
  180. if ((asid ^ atomic64_read(&asid_generation)) >> ASID_BITS) {
  181. asid = new_context(mm, cpu);
  182. atomic64_set(&mm->context.id, asid);
  183. }
  184. if (cpumask_test_and_clear_cpu(cpu, &tlb_flush_pending)) {
  185. local_flush_bp_all();
  186. local_flush_tlb_all();
  187. dummy_flush_tlb_a15_erratum();
  188. }
  189. atomic64_set(&per_cpu(active_asids, cpu), asid);
  190. cpumask_set_cpu(cpu, mm_cpumask(mm));
  191. raw_spin_unlock_irqrestore(&cpu_asid_lock, flags);
  192. switch_mm_fastpath:
  193. cpu_switch_mm(mm->pgd, mm);
  194. }