context.c 3.8 KB

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