mmu_context.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  4. *
  5. * vineetg: May 2011
  6. * -Refactored get_new_mmu_context( ) to only handle live-mm.
  7. * retiring-mm handled in other hooks
  8. *
  9. * Vineetg: March 25th, 2008: Bug #92690
  10. * -Major rewrite of Core ASID allocation routine get_new_mmu_context
  11. *
  12. * Amit Bhor, Sameer Dhavale: Codito Technologies 2004
  13. */
  14. #ifndef _ASM_ARC_MMU_CONTEXT_H
  15. #define _ASM_ARC_MMU_CONTEXT_H
  16. #include <linux/sched/mm.h>
  17. #include <asm/tlb.h>
  18. #include <asm-generic/mm_hooks.h>
  19. /* ARC ASID Management
  20. *
  21. * MMU tags TLBs with an 8-bit ASID, avoiding need to flush the TLB on
  22. * context-switch.
  23. *
  24. * ASID is managed per cpu, so task threads across CPUs can have different
  25. * ASID. Global ASID management is needed if hardware supports TLB shootdown
  26. * and/or shared TLB across cores, which ARC doesn't.
  27. *
  28. * Each task is assigned unique ASID, with a simple round-robin allocator
  29. * tracked in @asid_cpu. When 8-bit value rolls over,a new cycle is started
  30. * over from 0, and TLB is flushed
  31. *
  32. * A new allocation cycle, post rollover, could potentially reassign an ASID
  33. * to a different task. Thus the rule is to refresh the ASID in a new cycle.
  34. * The 32 bit @asid_cpu (and mm->asid) have 8 bits MMU PID and rest 24 bits
  35. * serve as cycle/generation indicator and natural 32 bit unsigned math
  36. * automagically increments the generation when lower 8 bits rollover.
  37. */
  38. #define MM_CTXT_ASID_MASK 0x000000ff /* MMU PID reg :8 bit PID */
  39. #define MM_CTXT_CYCLE_MASK (~MM_CTXT_ASID_MASK)
  40. #define MM_CTXT_FIRST_CYCLE (MM_CTXT_ASID_MASK + 1)
  41. #define MM_CTXT_NO_ASID 0UL
  42. #define asid_mm(mm, cpu) mm->context.asid[cpu]
  43. #define hw_pid(mm, cpu) (asid_mm(mm, cpu) & MM_CTXT_ASID_MASK)
  44. DECLARE_PER_CPU(unsigned int, asid_cache);
  45. #define asid_cpu(cpu) per_cpu(asid_cache, cpu)
  46. /*
  47. * Get a new ASID if task doesn't have a valid one (unalloc or from prev cycle)
  48. * Also set the MMU PID register to existing/updated ASID
  49. */
  50. static inline void get_new_mmu_context(struct mm_struct *mm)
  51. {
  52. const unsigned int cpu = smp_processor_id();
  53. unsigned long flags;
  54. local_irq_save(flags);
  55. /*
  56. * Move to new ASID if it was not from current alloc-cycle/generation.
  57. * This is done by ensuring that the generation bits in both mm->ASID
  58. * and cpu's ASID counter are exactly same.
  59. *
  60. * Note: Callers needing new ASID unconditionally, independent of
  61. * generation, e.g. local_flush_tlb_mm() for forking parent,
  62. * first need to destroy the context, setting it to invalid
  63. * value.
  64. */
  65. if (!((asid_mm(mm, cpu) ^ asid_cpu(cpu)) & MM_CTXT_CYCLE_MASK))
  66. goto set_hw;
  67. /* move to new ASID and handle rollover */
  68. if (unlikely(!(++asid_cpu(cpu) & MM_CTXT_ASID_MASK))) {
  69. local_flush_tlb_all();
  70. /*
  71. * Above check for rollover of 8 bit ASID in 32 bit container.
  72. * If the container itself wrapped around, set it to a non zero
  73. * "generation" to distinguish from no context
  74. */
  75. if (!asid_cpu(cpu))
  76. asid_cpu(cpu) = MM_CTXT_FIRST_CYCLE;
  77. }
  78. /* Assign new ASID to tsk */
  79. asid_mm(mm, cpu) = asid_cpu(cpu);
  80. set_hw:
  81. mmu_setup_asid(mm, hw_pid(mm, cpu));
  82. local_irq_restore(flags);
  83. }
  84. /*
  85. * Initialize the context related info for a new mm_struct
  86. * instance.
  87. */
  88. #define init_new_context init_new_context
  89. static inline int
  90. init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  91. {
  92. int i;
  93. for_each_possible_cpu(i)
  94. asid_mm(mm, i) = MM_CTXT_NO_ASID;
  95. return 0;
  96. }
  97. #define destroy_context destroy_context
  98. static inline void destroy_context(struct mm_struct *mm)
  99. {
  100. unsigned long flags;
  101. /* Needed to elide CONFIG_DEBUG_PREEMPT warning */
  102. local_irq_save(flags);
  103. asid_mm(mm, smp_processor_id()) = MM_CTXT_NO_ASID;
  104. local_irq_restore(flags);
  105. }
  106. /* Prepare the MMU for task: setup PID reg with allocated ASID
  107. If task doesn't have an ASID (never alloc or stolen, get a new ASID)
  108. */
  109. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  110. struct task_struct *tsk)
  111. {
  112. const int cpu = smp_processor_id();
  113. /*
  114. * Note that the mm_cpumask is "aggregating" only, we don't clear it
  115. * for the switched-out task, unlike some other arches.
  116. * It is used to enlist cpus for sending TLB flush IPIs and not sending
  117. * it to CPUs where a task once ran-on, could cause stale TLB entry
  118. * re-use, specially for a multi-threaded task.
  119. * e.g. T1 runs on C1, migrates to C3. T2 running on C2 munmaps.
  120. * For a non-aggregating mm_cpumask, IPI not sent C1, and if T1
  121. * were to re-migrate to C1, it could access the unmapped region
  122. * via any existing stale TLB entries.
  123. */
  124. cpumask_set_cpu(cpu, mm_cpumask(next));
  125. mmu_setup_pgd(next, next->pgd);
  126. get_new_mmu_context(next);
  127. }
  128. /*
  129. * activate_mm defaults (in asm-generic) to switch_mm and is called at the
  130. * time of execve() to get a new ASID Note the subtlety here:
  131. * get_new_mmu_context() behaves differently here vs. in switch_mm(). Here
  132. * it always returns a new ASID, because mm has an unallocated "initial"
  133. * value, while in latter, it moves to a new ASID, only if it was
  134. * unallocated
  135. */
  136. /* it seemed that deactivate_mm( ) is a reasonable place to do book-keeping
  137. * for retiring-mm. However destroy_context( ) still needs to do that because
  138. * between mm_release( ) = >deactive_mm( ) and
  139. * mmput => .. => __mmdrop( ) => destroy_context( )
  140. * there is a good chance that task gets sched-out/in, making it's ASID valid
  141. * again (this teased me for a whole day).
  142. */
  143. #include <asm-generic/mmu_context.h>
  144. #endif /* __ASM_ARC_MMU_CONTEXT_H */