context.c 3.7 KB

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