context.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Based on arch/arm/mm/context.c
  3. *
  4. * Copyright (C) 2002-2003 Deep Blue Solutions Ltd, all rights reserved.
  5. * Copyright (C) 2012 ARM Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/init.h>
  20. #include <linux/sched.h>
  21. #include <linux/mm.h>
  22. #include <linux/smp.h>
  23. #include <linux/percpu.h>
  24. #include <asm/mmu_context.h>
  25. #include <asm/tlbflush.h>
  26. #include <asm/cachetype.h>
  27. #define asid_bits(reg) \
  28. (((read_cpuid(ID_AA64MMFR0_EL1) & 0xf0) >> 2) + 8)
  29. #define ASID_FIRST_VERSION (1 << MAX_ASID_BITS)
  30. static DEFINE_RAW_SPINLOCK(cpu_asid_lock);
  31. unsigned int cpu_last_asid = ASID_FIRST_VERSION;
  32. /*
  33. * We fork()ed a process, and we need a new context for the child to run in.
  34. */
  35. void __init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  36. {
  37. mm->context.id = 0;
  38. raw_spin_lock_init(&mm->context.id_lock);
  39. }
  40. static void flush_context(void)
  41. {
  42. /* set the reserved TTBR0 before flushing the TLB */
  43. cpu_set_reserved_ttbr0();
  44. flush_tlb_all();
  45. if (icache_is_aivivt())
  46. __flush_icache_all();
  47. }
  48. #ifdef CONFIG_SMP
  49. static void set_mm_context(struct mm_struct *mm, unsigned int asid)
  50. {
  51. unsigned long flags;
  52. /*
  53. * Locking needed for multi-threaded applications where the same
  54. * mm->context.id could be set from different CPUs during the
  55. * broadcast. This function is also called via IPI so the
  56. * mm->context.id_lock has to be IRQ-safe.
  57. */
  58. raw_spin_lock_irqsave(&mm->context.id_lock, flags);
  59. if (likely((mm->context.id ^ cpu_last_asid) >> MAX_ASID_BITS)) {
  60. /*
  61. * Old version of ASID found. Set the new one and reset
  62. * mm_cpumask(mm).
  63. */
  64. mm->context.id = asid;
  65. cpumask_clear(mm_cpumask(mm));
  66. }
  67. raw_spin_unlock_irqrestore(&mm->context.id_lock, flags);
  68. /*
  69. * Set the mm_cpumask(mm) bit for the current CPU.
  70. */
  71. cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
  72. }
  73. /*
  74. * Reset the ASID on the current CPU. This function call is broadcast from the
  75. * CPU handling the ASID rollover and holding cpu_asid_lock.
  76. */
  77. static void reset_context(void *info)
  78. {
  79. unsigned int asid;
  80. unsigned int cpu = smp_processor_id();
  81. struct mm_struct *mm = current->active_mm;
  82. /*
  83. * current->active_mm could be init_mm for the idle thread immediately
  84. * after secondary CPU boot or hotplug. TTBR0_EL1 is already set to
  85. * the reserved value, so no need to reset any context.
  86. */
  87. if (mm == &init_mm)
  88. return;
  89. smp_rmb();
  90. asid = cpu_last_asid + cpu;
  91. flush_context();
  92. set_mm_context(mm, asid);
  93. /* set the new ASID */
  94. cpu_switch_mm(mm->pgd, mm);
  95. }
  96. #else
  97. static inline void set_mm_context(struct mm_struct *mm, unsigned int asid)
  98. {
  99. mm->context.id = asid;
  100. cpumask_copy(mm_cpumask(mm), cpumask_of(smp_processor_id()));
  101. }
  102. #endif
  103. void __new_context(struct mm_struct *mm)
  104. {
  105. unsigned int asid;
  106. unsigned int bits = asid_bits();
  107. raw_spin_lock(&cpu_asid_lock);
  108. #ifdef CONFIG_SMP
  109. /*
  110. * Check the ASID again, in case the change was broadcast from another
  111. * CPU before we acquired the lock.
  112. */
  113. if (!unlikely((mm->context.id ^ cpu_last_asid) >> MAX_ASID_BITS)) {
  114. cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
  115. raw_spin_unlock(&cpu_asid_lock);
  116. return;
  117. }
  118. #endif
  119. /*
  120. * At this point, it is guaranteed that the current mm (with an old
  121. * ASID) isn't active on any other CPU since the ASIDs are changed
  122. * simultaneously via IPI.
  123. */
  124. asid = ++cpu_last_asid;
  125. /*
  126. * If we've used up all our ASIDs, we need to start a new version and
  127. * flush the TLB.
  128. */
  129. if (unlikely((asid & ((1 << bits) - 1)) == 0)) {
  130. /* increment the ASID version */
  131. cpu_last_asid += (1 << MAX_ASID_BITS) - (1 << bits);
  132. if (cpu_last_asid == 0)
  133. cpu_last_asid = ASID_FIRST_VERSION;
  134. asid = cpu_last_asid + smp_processor_id();
  135. flush_context();
  136. #ifdef CONFIG_SMP
  137. smp_wmb();
  138. smp_call_function(reset_context, NULL, 1);
  139. #endif
  140. cpu_last_asid += NR_CPUS - 1;
  141. }
  142. set_mm_context(mm, asid);
  143. raw_spin_unlock(&cpu_asid_lock);
  144. }