context.c 5.8 KB

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