context.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * linux/arch/arm/mm/context.c
  3. *
  4. * Copyright (C) 2002-2003 Deep Blue Solutions Ltd, all rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/sched.h>
  12. #include <linux/mm.h>
  13. #include <linux/smp.h>
  14. #include <linux/percpu.h>
  15. #include <asm/mmu_context.h>
  16. #include <asm/tlbflush.h>
  17. static DEFINE_RAW_SPINLOCK(cpu_asid_lock);
  18. unsigned int cpu_last_asid = ASID_FIRST_VERSION;
  19. #ifdef CONFIG_SMP
  20. DEFINE_PER_CPU(struct mm_struct *, current_mm);
  21. #endif
  22. #ifdef CONFIG_ARM_LPAE
  23. #define cpu_set_asid(asid) { \
  24. unsigned long ttbl, ttbh; \
  25. asm volatile( \
  26. " mrrc p15, 0, %0, %1, c2 @ read TTBR0\n" \
  27. " mov %1, %2, lsl #(48 - 32) @ set ASID\n" \
  28. " mcrr p15, 0, %0, %1, c2 @ set TTBR0\n" \
  29. : "=&r" (ttbl), "=&r" (ttbh) \
  30. : "r" (asid & ~ASID_MASK)); \
  31. }
  32. #else
  33. #define cpu_set_asid(asid) \
  34. asm(" mcr p15, 0, %0, c13, c0, 1\n" : : "r" (asid))
  35. #endif
  36. /*
  37. * We fork()ed a process, and we need a new context for the child
  38. * to run in. We reserve version 0 for initial tasks so we will
  39. * always allocate an ASID. The ASID 0 is reserved for the TTBR
  40. * register changing sequence.
  41. */
  42. void __init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  43. {
  44. mm->context.id = 0;
  45. raw_spin_lock_init(&mm->context.id_lock);
  46. }
  47. static void flush_context(void)
  48. {
  49. /* set the reserved ASID before flushing the TLB */
  50. cpu_set_asid(0);
  51. isb();
  52. local_flush_tlb_all();
  53. if (icache_is_vivt_asid_tagged()) {
  54. __flush_icache_all();
  55. dsb();
  56. }
  57. }
  58. #ifdef CONFIG_SMP
  59. static void set_mm_context(struct mm_struct *mm, unsigned int asid)
  60. {
  61. unsigned long flags;
  62. /*
  63. * Locking needed for multi-threaded applications where the
  64. * same mm->context.id could be set from different CPUs during
  65. * the broadcast. This function is also called via IPI so the
  66. * mm->context.id_lock has to be IRQ-safe.
  67. */
  68. raw_spin_lock_irqsave(&mm->context.id_lock, flags);
  69. if (likely((mm->context.id ^ cpu_last_asid) >> ASID_BITS)) {
  70. /*
  71. * Old version of ASID found. Set the new one and
  72. * reset mm_cpumask(mm).
  73. */
  74. mm->context.id = asid;
  75. cpumask_clear(mm_cpumask(mm));
  76. }
  77. raw_spin_unlock_irqrestore(&mm->context.id_lock, flags);
  78. /*
  79. * Set the mm_cpumask(mm) bit for the current CPU.
  80. */
  81. cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
  82. }
  83. /*
  84. * Reset the ASID on the current CPU. This function call is broadcast
  85. * from the CPU handling the ASID rollover and holding cpu_asid_lock.
  86. */
  87. static void reset_context(void *info)
  88. {
  89. unsigned int asid;
  90. unsigned int cpu = smp_processor_id();
  91. struct mm_struct *mm = per_cpu(current_mm, cpu);
  92. /*
  93. * Check if a current_mm was set on this CPU as it might still
  94. * be in the early booting stages and using the reserved ASID.
  95. */
  96. if (!mm)
  97. return;
  98. smp_rmb();
  99. asid = cpu_last_asid + cpu + 1;
  100. flush_context();
  101. set_mm_context(mm, asid);
  102. /* set the new ASID */
  103. cpu_set_asid(mm->context.id);
  104. isb();
  105. }
  106. #else
  107. static inline void set_mm_context(struct mm_struct *mm, unsigned int asid)
  108. {
  109. mm->context.id = asid;
  110. cpumask_copy(mm_cpumask(mm), cpumask_of(smp_processor_id()));
  111. }
  112. #endif
  113. void __new_context(struct mm_struct *mm)
  114. {
  115. unsigned int asid;
  116. raw_spin_lock(&cpu_asid_lock);
  117. #ifdef CONFIG_SMP
  118. /*
  119. * Check the ASID again, in case the change was broadcast from
  120. * another CPU before we acquired the lock.
  121. */
  122. if (unlikely(((mm->context.id ^ cpu_last_asid) >> ASID_BITS) == 0)) {
  123. cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
  124. raw_spin_unlock(&cpu_asid_lock);
  125. return;
  126. }
  127. #endif
  128. /*
  129. * At this point, it is guaranteed that the current mm (with
  130. * an old ASID) isn't active on any other CPU since the ASIDs
  131. * are changed simultaneously via IPI.
  132. */
  133. asid = ++cpu_last_asid;
  134. if (asid == 0)
  135. asid = cpu_last_asid = ASID_FIRST_VERSION;
  136. /*
  137. * If we've used up all our ASIDs, we need
  138. * to start a new version and flush the TLB.
  139. */
  140. if (unlikely((asid & ~ASID_MASK) == 0)) {
  141. asid = cpu_last_asid + smp_processor_id() + 1;
  142. flush_context();
  143. #ifdef CONFIG_SMP
  144. smp_wmb();
  145. smp_call_function(reset_context, NULL, 1);
  146. #endif
  147. cpu_last_asid += NR_CPUS;
  148. }
  149. set_mm_context(mm, asid);
  150. raw_spin_unlock(&cpu_asid_lock);
  151. }