context.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. static void set_mm_context(struct mm_struct *mm, unsigned int asid)
  49. {
  50. unsigned long flags;
  51. /*
  52. * Locking needed for multi-threaded applications where the same
  53. * mm->context.id could be set from different CPUs during the
  54. * broadcast. This function is also called via IPI so the
  55. * mm->context.id_lock has to be IRQ-safe.
  56. */
  57. raw_spin_lock_irqsave(&mm->context.id_lock, flags);
  58. if (likely((mm->context.id ^ cpu_last_asid) >> MAX_ASID_BITS)) {
  59. /*
  60. * Old version of ASID found. Set the new one and reset
  61. * mm_cpumask(mm).
  62. */
  63. mm->context.id = asid;
  64. cpumask_clear(mm_cpumask(mm));
  65. }
  66. raw_spin_unlock_irqrestore(&mm->context.id_lock, flags);
  67. /*
  68. * Set the mm_cpumask(mm) bit for the current CPU.
  69. */
  70. cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
  71. }
  72. /*
  73. * Reset the ASID on the current CPU. This function call is broadcast from the
  74. * CPU handling the ASID rollover and holding cpu_asid_lock.
  75. */
  76. static void reset_context(void *info)
  77. {
  78. unsigned int asid;
  79. unsigned int cpu = smp_processor_id();
  80. struct mm_struct *mm = current->active_mm;
  81. /*
  82. * current->active_mm could be init_mm for the idle thread immediately
  83. * after secondary CPU boot or hotplug. TTBR0_EL1 is already set to
  84. * the reserved value, so no need to reset any context.
  85. */
  86. if (mm == &init_mm)
  87. return;
  88. smp_rmb();
  89. asid = cpu_last_asid + cpu;
  90. flush_context();
  91. set_mm_context(mm, asid);
  92. /* set the new ASID */
  93. cpu_switch_mm(mm->pgd, mm);
  94. }
  95. void __new_context(struct mm_struct *mm)
  96. {
  97. unsigned int asid;
  98. unsigned int bits = asid_bits();
  99. raw_spin_lock(&cpu_asid_lock);
  100. /*
  101. * Check the ASID again, in case the change was broadcast from another
  102. * CPU before we acquired the lock.
  103. */
  104. if (!unlikely((mm->context.id ^ cpu_last_asid) >> MAX_ASID_BITS)) {
  105. cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
  106. raw_spin_unlock(&cpu_asid_lock);
  107. return;
  108. }
  109. /*
  110. * At this point, it is guaranteed that the current mm (with an old
  111. * ASID) isn't active on any other CPU since the ASIDs are changed
  112. * simultaneously via IPI.
  113. */
  114. asid = ++cpu_last_asid;
  115. /*
  116. * If we've used up all our ASIDs, we need to start a new version and
  117. * flush the TLB.
  118. */
  119. if (unlikely((asid & ((1 << bits) - 1)) == 0)) {
  120. /* increment the ASID version */
  121. cpu_last_asid += (1 << MAX_ASID_BITS) - (1 << bits);
  122. if (cpu_last_asid == 0)
  123. cpu_last_asid = ASID_FIRST_VERSION;
  124. asid = cpu_last_asid + smp_processor_id();
  125. flush_context();
  126. smp_wmb();
  127. smp_call_function(reset_context, NULL, 1);
  128. cpu_last_asid += NR_CPUS - 1;
  129. }
  130. set_mm_context(mm, asid);
  131. raw_spin_unlock(&cpu_asid_lock);
  132. }