mmu_context.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* MN10300 MMU context management
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Modified by David Howells (dhowells@redhat.com)
  5. * - Derived from include/asm-m32r/mmu_context.h
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public Licence
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the Licence, or (at your option) any later version.
  11. *
  12. *
  13. * This implements an algorithm to provide TLB PID mappings to provide
  14. * selective access to the TLB for processes, thus reducing the number of TLB
  15. * flushes required.
  16. *
  17. * Note, however, that the M32R algorithm is technically broken as it does not
  18. * handle version wrap-around, and could, theoretically, have a problem with a
  19. * very long lived program that sleeps long enough for the version number to
  20. * wrap all the way around so that its TLB mappings appear valid once again.
  21. */
  22. #ifndef _ASM_MMU_CONTEXT_H
  23. #define _ASM_MMU_CONTEXT_H
  24. #include <linux/atomic.h>
  25. #include <asm/pgalloc.h>
  26. #include <asm/tlbflush.h>
  27. #include <asm-generic/mm_hooks.h>
  28. #define MMU_CONTEXT_TLBPID_NR 256
  29. #define MMU_CONTEXT_TLBPID_MASK 0x000000ffUL
  30. #define MMU_CONTEXT_VERSION_MASK 0xffffff00UL
  31. #define MMU_CONTEXT_FIRST_VERSION 0x00000100UL
  32. #define MMU_NO_CONTEXT 0x00000000UL
  33. #define MMU_CONTEXT_TLBPID_LOCK_NR 0
  34. #define enter_lazy_tlb(mm, tsk) do {} while (0)
  35. static inline void cpu_ran_vm(int cpu, struct mm_struct *mm)
  36. {
  37. #ifdef CONFIG_SMP
  38. cpumask_set_cpu(cpu, mm_cpumask(mm));
  39. #endif
  40. }
  41. static inline bool cpu_maybe_ran_vm(int cpu, struct mm_struct *mm)
  42. {
  43. #ifdef CONFIG_SMP
  44. return cpumask_test_and_set_cpu(cpu, mm_cpumask(mm));
  45. #else
  46. return true;
  47. #endif
  48. }
  49. #ifdef CONFIG_MN10300_TLB_USE_PIDR
  50. extern unsigned long mmu_context_cache[NR_CPUS];
  51. #define mm_context(mm) (mm->context.tlbpid[smp_processor_id()])
  52. /**
  53. * allocate_mmu_context - Allocate storage for the arch-specific MMU data
  54. * @mm: The userspace VM context being set up
  55. */
  56. static inline unsigned long allocate_mmu_context(struct mm_struct *mm)
  57. {
  58. unsigned long *pmc = &mmu_context_cache[smp_processor_id()];
  59. unsigned long mc = ++(*pmc);
  60. if (!(mc & MMU_CONTEXT_TLBPID_MASK)) {
  61. /* we exhausted the TLB PIDs of this version on this CPU, so we
  62. * flush this CPU's TLB in its entirety and start new cycle */
  63. local_flush_tlb_all();
  64. /* fix the TLB version if needed (we avoid version #0 so as to
  65. * distinguish MMU_NO_CONTEXT) */
  66. if (!mc)
  67. *pmc = mc = MMU_CONTEXT_FIRST_VERSION;
  68. }
  69. mm_context(mm) = mc;
  70. return mc;
  71. }
  72. /*
  73. * get an MMU context if one is needed
  74. */
  75. static inline unsigned long get_mmu_context(struct mm_struct *mm)
  76. {
  77. unsigned long mc = MMU_NO_CONTEXT, cache;
  78. if (mm) {
  79. cache = mmu_context_cache[smp_processor_id()];
  80. mc = mm_context(mm);
  81. /* if we have an old version of the context, replace it */
  82. if ((mc ^ cache) & MMU_CONTEXT_VERSION_MASK)
  83. mc = allocate_mmu_context(mm);
  84. }
  85. return mc;
  86. }
  87. /*
  88. * initialise the context related info for a new mm_struct instance
  89. */
  90. static inline int init_new_context(struct task_struct *tsk,
  91. struct mm_struct *mm)
  92. {
  93. int num_cpus = NR_CPUS, i;
  94. for (i = 0; i < num_cpus; i++)
  95. mm->context.tlbpid[i] = MMU_NO_CONTEXT;
  96. return 0;
  97. }
  98. /*
  99. * after we have set current->mm to a new value, this activates the context for
  100. * the new mm so we see the new mappings.
  101. */
  102. static inline void activate_context(struct mm_struct *mm)
  103. {
  104. PIDR = get_mmu_context(mm) & MMU_CONTEXT_TLBPID_MASK;
  105. }
  106. #else /* CONFIG_MN10300_TLB_USE_PIDR */
  107. #define init_new_context(tsk, mm) (0)
  108. #define activate_context(mm) local_flush_tlb()
  109. #endif /* CONFIG_MN10300_TLB_USE_PIDR */
  110. /**
  111. * destroy_context - Destroy mm context information
  112. * @mm: The MM being destroyed.
  113. *
  114. * Destroy context related info for an mm_struct that is about to be put to
  115. * rest
  116. */
  117. #define destroy_context(mm) do {} while (0)
  118. /**
  119. * switch_mm - Change between userspace virtual memory contexts
  120. * @prev: The outgoing MM context.
  121. * @next: The incoming MM context.
  122. * @tsk: The incoming task.
  123. */
  124. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  125. struct task_struct *tsk)
  126. {
  127. int cpu = smp_processor_id();
  128. if (prev != next) {
  129. #ifdef CONFIG_SMP
  130. per_cpu(cpu_tlbstate, cpu).active_mm = next;
  131. #endif
  132. cpu_ran_vm(cpu, next);
  133. PTBR = (unsigned long) next->pgd;
  134. activate_context(next);
  135. }
  136. }
  137. #define deactivate_mm(tsk, mm) do {} while (0)
  138. #define activate_mm(prev, next) switch_mm((prev), (next), NULL)
  139. #endif /* _ASM_MMU_CONTEXT_H */