mmu_context.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * vineetg: May 2011
  9. * -Refactored get_new_mmu_context( ) to only handle live-mm.
  10. * retiring-mm handled in other hooks
  11. *
  12. * Vineetg: March 25th, 2008: Bug #92690
  13. * -Major rewrite of Core ASID allocation routine get_new_mmu_context
  14. *
  15. * Amit Bhor, Sameer Dhavale: Codito Technologies 2004
  16. */
  17. #ifndef _ASM_ARC_MMU_CONTEXT_H
  18. #define _ASM_ARC_MMU_CONTEXT_H
  19. #include <asm/arcregs.h>
  20. #include <asm/tlb.h>
  21. #include <asm-generic/mm_hooks.h>
  22. /* ARC700 ASID Management
  23. *
  24. * ARC MMU provides 8-bit ASID (0..255) to TAG TLB entries, allowing entries
  25. * with same vaddr (different tasks) to co-exit. This provides for
  26. * "Fast Context Switch" i.e. no TLB flush on ctxt-switch
  27. *
  28. * Linux assigns each task a unique ASID. A simple round-robin allocation
  29. * of H/w ASID is done using software tracker @asid_cache.
  30. * When it reaches max 255, the allocation cycle starts afresh by flushing
  31. * the entire TLB and wrapping ASID back to zero.
  32. *
  33. * For book-keeping, Linux uses a couple of data-structures:
  34. * -mm_struct has an @asid field to keep a note of task's ASID (needed at the
  35. * time of say switch_mm( )
  36. * -An array of mm structs @asid_mm_map[] for asid->mm the reverse mapping,
  37. * given an ASID, finding the mm struct associated.
  38. *
  39. * The round-robin allocation algorithm allows for ASID stealing.
  40. * If asid tracker is at "x-1", a new req will allocate "x", even if "x" was
  41. * already assigned to another (switched-out) task. Obviously the prev owner
  42. * is marked with an invalid ASID to make it request for a new ASID when it
  43. * gets scheduled next time. However its TLB entries (with ASID "x") could
  44. * exist, which must be cleared before the same ASID is used by the new owner.
  45. * Flushing them would be plausible but costly solution. Instead we force a
  46. * allocation policy quirk, which ensures that a stolen ASID won't have any
  47. * TLB entries associates, alleviating the need to flush.
  48. * The quirk essentially is not allowing ASID allocated in prev cycle
  49. * to be used past a roll-over in the next cycle.
  50. * When this happens (i.e. task ASID > asid tracker), task needs to refresh
  51. * its ASID, aligning it to current value of tracker. If the task doesn't get
  52. * scheduled past a roll-over, hence its ASID is not yet realigned with
  53. * tracker, such ASID is anyways safely reusable because it is
  54. * gauranteed that TLB entries with that ASID wont exist.
  55. */
  56. #define FIRST_ASID 0
  57. #define MAX_ASID 255 /* 8 bit PID field in PID Aux reg */
  58. #define NO_ASID (MAX_ASID + 1) /* ASID Not alloc to mmu ctxt */
  59. #define NUM_ASID ((MAX_ASID - FIRST_ASID) + 1)
  60. /* ASID to mm struct mapping */
  61. extern struct mm_struct *asid_mm_map[NUM_ASID + 1];
  62. extern int asid_cache;
  63. /*
  64. * Assign a new ASID to task. If the task already has an ASID, it is
  65. * relinquished.
  66. */
  67. static inline void get_new_mmu_context(struct mm_struct *mm)
  68. {
  69. struct mm_struct *prev_owner;
  70. unsigned long flags;
  71. local_irq_save(flags);
  72. /*
  73. * Relinquish the currently owned ASID (if any).
  74. * Doing unconditionally saves a cmp-n-branch; for already unused
  75. * ASID slot, the value was/remains NULL
  76. */
  77. asid_mm_map[mm->context.asid] = (struct mm_struct *)NULL;
  78. /* move to new ASID */
  79. if (++asid_cache > MAX_ASID) { /* ASID roll-over */
  80. asid_cache = FIRST_ASID;
  81. flush_tlb_all();
  82. }
  83. /*
  84. * Is next ASID already owned by some-one else (we are stealing it).
  85. * If so, let the orig owner be aware of this, so when it runs, it
  86. * asks for a brand new ASID. This would only happen for a long-lived
  87. * task with ASID from prev allocation cycle (before ASID roll-over).
  88. *
  89. * This might look wrong - if we are re-using some other task's ASID,
  90. * won't we use it's stale TLB entries too. Actually switch_mm( ) takes
  91. * care of such a case: it ensures that task with ASID from prev alloc
  92. * cycle, when scheduled will refresh it's ASID: see switch_mm( ) below
  93. * The stealing scenario described here will only happen if that task
  94. * didn't get a chance to refresh it's ASID - implying stale entries
  95. * won't exist.
  96. */
  97. prev_owner = asid_mm_map[asid_cache];
  98. if (prev_owner)
  99. prev_owner->context.asid = NO_ASID;
  100. /* Assign new ASID to tsk */
  101. asid_mm_map[asid_cache] = mm;
  102. mm->context.asid = asid_cache;
  103. #ifdef CONFIG_ARC_TLB_DBG
  104. pr_info("ARC_TLB_DBG: NewMM=0x%x OldMM=0x%x task_struct=0x%x Task: %s,"
  105. " pid:%u, assigned asid:%lu\n",
  106. (unsigned int)mm, (unsigned int)prev_owner,
  107. (unsigned int)(mm->context.tsk), (mm->context.tsk)->comm,
  108. (mm->context.tsk)->pid, mm->context.asid);
  109. #endif
  110. write_aux_reg(ARC_REG_PID, asid_cache | MMU_ENABLE);
  111. local_irq_restore(flags);
  112. }
  113. /*
  114. * Initialize the context related info for a new mm_struct
  115. * instance.
  116. */
  117. static inline int
  118. init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  119. {
  120. mm->context.asid = NO_ASID;
  121. #ifdef CONFIG_ARC_TLB_DBG
  122. mm->context.tsk = tsk;
  123. #endif
  124. return 0;
  125. }
  126. /* Prepare the MMU for task: setup PID reg with allocated ASID
  127. If task doesn't have an ASID (never alloc or stolen, get a new ASID)
  128. */
  129. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  130. struct task_struct *tsk)
  131. {
  132. #ifndef CONFIG_SMP
  133. /* PGD cached in MMU reg to avoid 3 mem lookups: task->mm->pgd */
  134. write_aux_reg(ARC_REG_SCRATCH_DATA0, next->pgd);
  135. #endif
  136. /*
  137. * Get a new ASID if task doesn't have a valid one. Possible when
  138. * -task never had an ASID (fresh after fork)
  139. * -it's ASID was stolen - past an ASID roll-over.
  140. * -There's a third obscure scenario (if this task is running for the
  141. * first time afer an ASID rollover), where despite having a valid
  142. * ASID, we force a get for new ASID - see comments at top.
  143. *
  144. * Both the non-alloc scenario and first-use-after-rollover can be
  145. * detected using the single condition below: NO_ASID = 256
  146. * while asid_cache is always a valid ASID value (0-255).
  147. */
  148. if (next->context.asid > asid_cache) {
  149. get_new_mmu_context(next);
  150. } else {
  151. /*
  152. * XXX: This will never happen given the chks above
  153. * BUG_ON(next->context.asid > MAX_ASID);
  154. */
  155. write_aux_reg(ARC_REG_PID, next->context.asid | MMU_ENABLE);
  156. }
  157. }
  158. static inline void destroy_context(struct mm_struct *mm)
  159. {
  160. unsigned long flags;
  161. local_irq_save(flags);
  162. asid_mm_map[mm->context.asid] = NULL;
  163. mm->context.asid = NO_ASID;
  164. local_irq_restore(flags);
  165. }
  166. /* it seemed that deactivate_mm( ) is a reasonable place to do book-keeping
  167. * for retiring-mm. However destroy_context( ) still needs to do that because
  168. * between mm_release( ) = >deactive_mm( ) and
  169. * mmput => .. => __mmdrop( ) => destroy_context( )
  170. * there is a good chance that task gets sched-out/in, making it's ASID valid
  171. * again (this teased me for a whole day).
  172. */
  173. #define deactivate_mm(tsk, mm) do { } while (0)
  174. static inline void activate_mm(struct mm_struct *prev, struct mm_struct *next)
  175. {
  176. #ifndef CONFIG_SMP
  177. write_aux_reg(ARC_REG_SCRATCH_DATA0, next->pgd);
  178. #endif
  179. /* Unconditionally get a new ASID */
  180. get_new_mmu_context(next);
  181. }
  182. #define enter_lazy_tlb(mm, tsk)
  183. #endif /* __ASM_ARC_MMU_CONTEXT_H */