context.c 5.6 KB

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