context.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/thread_notify.h>
  17. #include <asm/tlbflush.h>
  18. #include <mach/msm_rtb.h>
  19. static DEFINE_RAW_SPINLOCK(cpu_asid_lock);
  20. unsigned int cpu_last_asid = ASID_FIRST_VERSION;
  21. #ifdef CONFIG_SMP
  22. DEFINE_PER_CPU(struct mm_struct *, current_mm);
  23. #endif
  24. #ifdef CONFIG_ARM_LPAE
  25. void cpu_set_reserved_ttbr0(void)
  26. {
  27. unsigned long ttbl = __pa(swapper_pg_dir);
  28. unsigned long ttbh = 0;
  29. /*
  30. * Set TTBR0 to swapper_pg_dir which contains only global entries. The
  31. * ASID is set to 0.
  32. */
  33. asm volatile(
  34. " mcrr p15, 0, %0, %1, c2 @ set TTBR0\n"
  35. :
  36. : "r" (ttbl), "r" (ttbh));
  37. isb();
  38. }
  39. #else
  40. void cpu_set_reserved_ttbr0(void)
  41. {
  42. u32 ttb;
  43. /* Copy TTBR1 into TTBR0 */
  44. asm volatile(
  45. " mrc p15, 0, %0, c2, c0, 1 @ read TTBR1\n"
  46. " mcr p15, 0, %0, c2, c0, 0 @ set TTBR0\n"
  47. : "=r" (ttb));
  48. isb();
  49. }
  50. #endif
  51. static void write_contextidr(u32 contextidr)
  52. {
  53. uncached_logk(LOGK_CTXID, (void *)contextidr);
  54. asm("mcr p15, 0, %0, c13, c0, 1" : : "r" (contextidr));
  55. isb();
  56. }
  57. #ifdef CONFIG_PID_IN_CONTEXTIDR
  58. static u32 read_contextidr(void)
  59. {
  60. u32 contextidr;
  61. asm("mrc p15, 0, %0, c13, c0, 1" : "=r" (contextidr));
  62. return contextidr;
  63. }
  64. static int contextidr_notifier(struct notifier_block *unused, unsigned long cmd,
  65. void *t)
  66. {
  67. unsigned long flags;
  68. u32 contextidr;
  69. pid_t pid;
  70. struct thread_info *thread = t;
  71. if (cmd != THREAD_NOTIFY_SWITCH)
  72. return NOTIFY_DONE;
  73. pid = task_pid_nr(thread->task);
  74. local_irq_save(flags);
  75. contextidr = read_contextidr();
  76. contextidr &= ~ASID_MASK;
  77. contextidr |= pid << ASID_BITS;
  78. write_contextidr(contextidr);
  79. local_irq_restore(flags);
  80. return NOTIFY_OK;
  81. }
  82. static struct notifier_block contextidr_notifier_block = {
  83. .notifier_call = contextidr_notifier,
  84. };
  85. static int __init contextidr_notifier_init(void)
  86. {
  87. return thread_register_notifier(&contextidr_notifier_block);
  88. }
  89. arch_initcall(contextidr_notifier_init);
  90. #endif
  91. /*
  92. * We fork()ed a process, and we need a new context for the child
  93. * to run in.
  94. */
  95. void __init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  96. {
  97. mm->context.id = 0;
  98. raw_spin_lock_init(&mm->context.id_lock);
  99. }
  100. static void flush_context(void)
  101. {
  102. cpu_set_reserved_ttbr0();
  103. local_flush_tlb_all();
  104. if (icache_is_vivt_asid_tagged()) {
  105. __flush_icache_all();
  106. dsb();
  107. }
  108. }
  109. #ifdef CONFIG_SMP
  110. static void set_mm_context(struct mm_struct *mm, unsigned int asid)
  111. {
  112. unsigned long flags;
  113. /*
  114. * Locking needed for multi-threaded applications where the
  115. * same mm->context.id could be set from different CPUs during
  116. * the broadcast. This function is also called via IPI so the
  117. * mm->context.id_lock has to be IRQ-safe.
  118. */
  119. raw_spin_lock_irqsave(&mm->context.id_lock, flags);
  120. if (likely((mm->context.id ^ cpu_last_asid) >> ASID_BITS)) {
  121. /*
  122. * Old version of ASID found. Set the new one and
  123. * reset mm_cpumask(mm).
  124. */
  125. mm->context.id = asid;
  126. cpumask_clear(mm_cpumask(mm));
  127. }
  128. raw_spin_unlock_irqrestore(&mm->context.id_lock, flags);
  129. /*
  130. * Set the mm_cpumask(mm) bit for the current CPU.
  131. */
  132. cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
  133. }
  134. /*
  135. * Reset the ASID on the current CPU. This function call is broadcast
  136. * from the CPU handling the ASID rollover and holding cpu_asid_lock.
  137. */
  138. static void reset_context(void *info)
  139. {
  140. unsigned int asid;
  141. unsigned int cpu = smp_processor_id();
  142. struct mm_struct *mm = current->active_mm;
  143. smp_rmb();
  144. asid = cpu_last_asid + cpu + 1;
  145. flush_context();
  146. set_mm_context(mm, asid);
  147. /* set the new ASID */
  148. cpu_switch_mm(mm->pgd, mm);
  149. }
  150. #else
  151. static inline void set_mm_context(struct mm_struct *mm, unsigned int asid)
  152. {
  153. mm->context.id = asid;
  154. cpumask_copy(mm_cpumask(mm), cpumask_of(smp_processor_id()));
  155. }
  156. #endif
  157. void __new_context(struct mm_struct *mm)
  158. {
  159. unsigned int asid;
  160. raw_spin_lock(&cpu_asid_lock);
  161. #ifdef CONFIG_SMP
  162. /*
  163. * Check the ASID again, in case the change was broadcast from
  164. * another CPU before we acquired the lock.
  165. */
  166. if (unlikely(((mm->context.id ^ cpu_last_asid) >> ASID_BITS) == 0)) {
  167. cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
  168. raw_spin_unlock(&cpu_asid_lock);
  169. return;
  170. }
  171. #endif
  172. /*
  173. * At this point, it is guaranteed that the current mm (with
  174. * an old ASID) isn't active on any other CPU since the ASIDs
  175. * are changed simultaneously via IPI.
  176. */
  177. asid = ++cpu_last_asid;
  178. if (asid == 0)
  179. asid = cpu_last_asid = ASID_FIRST_VERSION;
  180. /*
  181. * If we've used up all our ASIDs, we need
  182. * to start a new version and flush the TLB.
  183. */
  184. if (unlikely((asid & ~ASID_MASK) == 0)) {
  185. asid = cpu_last_asid + smp_processor_id() + 1;
  186. flush_context();
  187. #ifdef CONFIG_SMP
  188. smp_wmb();
  189. smp_call_function(reset_context, NULL, 1);
  190. #endif
  191. cpu_last_asid += NR_CPUS;
  192. }
  193. set_mm_context(mm, asid);
  194. raw_spin_unlock(&cpu_asid_lock);
  195. }