context.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. smp_rmb();
  83. asid = cpu_last_asid + cpu;
  84. flush_context();
  85. set_mm_context(mm, asid);
  86. /* set the new ASID */
  87. cpu_switch_mm(mm->pgd, mm);
  88. }
  89. #else
  90. static inline void set_mm_context(struct mm_struct *mm, unsigned int asid)
  91. {
  92. mm->context.id = asid;
  93. cpumask_copy(mm_cpumask(mm), cpumask_of(smp_processor_id()));
  94. }
  95. #endif
  96. void __new_context(struct mm_struct *mm)
  97. {
  98. unsigned int asid;
  99. unsigned int bits = asid_bits();
  100. raw_spin_lock(&cpu_asid_lock);
  101. #ifdef CONFIG_SMP
  102. /*
  103. * Check the ASID again, in case the change was broadcast from another
  104. * CPU before we acquired the lock.
  105. */
  106. if (!unlikely((mm->context.id ^ cpu_last_asid) >> MAX_ASID_BITS)) {
  107. cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
  108. raw_spin_unlock(&cpu_asid_lock);
  109. return;
  110. }
  111. #endif
  112. /*
  113. * At this point, it is guaranteed that the current mm (with an old
  114. * ASID) isn't active on any other CPU since the ASIDs are changed
  115. * simultaneously via IPI.
  116. */
  117. asid = ++cpu_last_asid;
  118. /*
  119. * If we've used up all our ASIDs, we need to start a new version and
  120. * flush the TLB.
  121. */
  122. if (unlikely((asid & ((1 << bits) - 1)) == 0)) {
  123. /* increment the ASID version */
  124. cpu_last_asid += (1 << MAX_ASID_BITS) - (1 << bits);
  125. if (cpu_last_asid == 0)
  126. cpu_last_asid = ASID_FIRST_VERSION;
  127. asid = cpu_last_asid + smp_processor_id();
  128. flush_context();
  129. #ifdef CONFIG_SMP
  130. smp_wmb();
  131. smp_call_function(reset_context, NULL, 1);
  132. #endif
  133. cpu_last_asid += NR_CPUS - 1;
  134. }
  135. set_mm_context(mm, asid);
  136. raw_spin_unlock(&cpu_asid_lock);
  137. }