context.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_ARM_LPAE
  20. void cpu_set_reserved_ttbr0(void)
  21. {
  22. unsigned long ttbl = __pa(swapper_pg_dir);
  23. unsigned long ttbh = 0;
  24. /*
  25. * Set TTBR0 to swapper_pg_dir which contains only global entries. The
  26. * ASID is set to 0.
  27. */
  28. asm volatile(
  29. " mcrr p15, 0, %0, %1, c2 @ set TTBR0\n"
  30. :
  31. : "r" (ttbl), "r" (ttbh));
  32. isb();
  33. }
  34. #else
  35. void cpu_set_reserved_ttbr0(void)
  36. {
  37. u32 ttb;
  38. /* Copy TTBR1 into TTBR0 */
  39. asm volatile(
  40. " mrc p15, 0, %0, c2, c0, 1 @ read TTBR1\n"
  41. " mcr p15, 0, %0, c2, c0, 0 @ set TTBR0\n"
  42. : "=r" (ttb));
  43. isb();
  44. }
  45. #endif
  46. /*
  47. * We fork()ed a process, and we need a new context for the child
  48. * to run in.
  49. */
  50. void __init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  51. {
  52. mm->context.id = 0;
  53. raw_spin_lock_init(&mm->context.id_lock);
  54. }
  55. static void flush_context(void)
  56. {
  57. cpu_set_reserved_ttbr0();
  58. local_flush_tlb_all();
  59. if (icache_is_vivt_asid_tagged()) {
  60. __flush_icache_all();
  61. dsb();
  62. }
  63. }
  64. #ifdef CONFIG_SMP
  65. static void set_mm_context(struct mm_struct *mm, unsigned int asid)
  66. {
  67. unsigned long flags;
  68. /*
  69. * Locking needed for multi-threaded applications where the
  70. * same mm->context.id could be set from different CPUs during
  71. * the broadcast. This function is also called via IPI so the
  72. * mm->context.id_lock has to be IRQ-safe.
  73. */
  74. raw_spin_lock_irqsave(&mm->context.id_lock, flags);
  75. if (likely((mm->context.id ^ cpu_last_asid) >> ASID_BITS)) {
  76. /*
  77. * Old version of ASID found. Set the new one and
  78. * reset mm_cpumask(mm).
  79. */
  80. mm->context.id = asid;
  81. cpumask_clear(mm_cpumask(mm));
  82. }
  83. raw_spin_unlock_irqrestore(&mm->context.id_lock, flags);
  84. /*
  85. * Set the mm_cpumask(mm) bit for the current CPU.
  86. */
  87. cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
  88. }
  89. /*
  90. * Reset the ASID on the current CPU. This function call is broadcast
  91. * from the CPU handling the ASID rollover and holding cpu_asid_lock.
  92. */
  93. static void reset_context(void *info)
  94. {
  95. unsigned int asid;
  96. unsigned int cpu = smp_processor_id();
  97. struct mm_struct *mm = current->active_mm;
  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_switch_mm(mm->pgd, mm);
  104. }
  105. #else
  106. static inline void set_mm_context(struct mm_struct *mm, unsigned int asid)
  107. {
  108. mm->context.id = asid;
  109. cpumask_copy(mm_cpumask(mm), cpumask_of(smp_processor_id()));
  110. }
  111. #endif
  112. void __new_context(struct mm_struct *mm)
  113. {
  114. unsigned int asid;
  115. raw_spin_lock(&cpu_asid_lock);
  116. #ifdef CONFIG_SMP
  117. /*
  118. * Check the ASID again, in case the change was broadcast from
  119. * another CPU before we acquired the lock.
  120. */
  121. if (unlikely(((mm->context.id ^ cpu_last_asid) >> ASID_BITS) == 0)) {
  122. cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
  123. raw_spin_unlock(&cpu_asid_lock);
  124. return;
  125. }
  126. #endif
  127. /*
  128. * At this point, it is guaranteed that the current mm (with
  129. * an old ASID) isn't active on any other CPU since the ASIDs
  130. * are changed simultaneously via IPI.
  131. */
  132. asid = ++cpu_last_asid;
  133. if (asid == 0)
  134. asid = cpu_last_asid = ASID_FIRST_VERSION;
  135. /*
  136. * If we've used up all our ASIDs, we need
  137. * to start a new version and flush the TLB.
  138. */
  139. if (unlikely((asid & ~ASID_MASK) == 0)) {
  140. asid = cpu_last_asid + smp_processor_id() + 1;
  141. flush_context();
  142. #ifdef CONFIG_SMP
  143. smp_wmb();
  144. smp_call_function(reset_context, NULL, 1);
  145. #endif
  146. cpu_last_asid += NR_CPUS;
  147. }
  148. set_mm_context(mm, asid);
  149. raw_spin_unlock(&cpu_asid_lock);
  150. }