mmu_context_mm.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
  4. * Copyright (C) 2008-2009 PetaLogix
  5. * Copyright (C) 2006 Atmark Techno, Inc.
  6. */
  7. #ifndef _ASM_MICROBLAZE_MMU_CONTEXT_H
  8. #define _ASM_MICROBLAZE_MMU_CONTEXT_H
  9. #include <linux/atomic.h>
  10. #include <linux/mm_types.h>
  11. #include <linux/sched.h>
  12. #include <asm/bitops.h>
  13. #include <asm/mmu.h>
  14. #include <asm-generic/mm_hooks.h>
  15. # ifdef __KERNEL__
  16. /*
  17. * This function defines the mapping from contexts to VSIDs (virtual
  18. * segment IDs). We use a skew on both the context and the high 4 bits
  19. * of the 32-bit virtual address (the "effective segment ID") in order
  20. * to spread out the entries in the MMU hash table.
  21. */
  22. # define CTX_TO_VSID(ctx, va) (((ctx) * (897 * 16) + ((va) >> 28) * 0x111) \
  23. & 0xffffff)
  24. /*
  25. MicroBlaze has 256 contexts, so we can just rotate through these
  26. as a way of "switching" contexts. If the TID of the TLB is zero,
  27. the PID/TID comparison is disabled, so we can use a TID of zero
  28. to represent all kernel pages as shared among all contexts.
  29. */
  30. # define NO_CONTEXT 256
  31. # define LAST_CONTEXT 255
  32. # define FIRST_CONTEXT 1
  33. /*
  34. * Set the current MMU context.
  35. * This is done byloading up the segment registers for the user part of the
  36. * address space.
  37. *
  38. * Since the PGD is immediately available, it is much faster to simply
  39. * pass this along as a second parameter, which is required for 8xx and
  40. * can be used for debugging on all processors (if you happen to have
  41. * an Abatron).
  42. */
  43. extern void set_context(mm_context_t context, pgd_t *pgd);
  44. /*
  45. * Bitmap of contexts in use.
  46. * The size of this bitmap is LAST_CONTEXT + 1 bits.
  47. */
  48. extern unsigned long context_map[];
  49. /*
  50. * This caches the next context number that we expect to be free.
  51. * Its use is an optimization only, we can't rely on this context
  52. * number to be free, but it usually will be.
  53. */
  54. extern mm_context_t next_mmu_context;
  55. /*
  56. * Since we don't have sufficient contexts to give one to every task
  57. * that could be in the system, we need to be able to steal contexts.
  58. * These variables support that.
  59. */
  60. extern atomic_t nr_free_contexts;
  61. extern struct mm_struct *context_mm[LAST_CONTEXT+1];
  62. extern void steal_context(void);
  63. /*
  64. * Get a new mmu context for the address space described by `mm'.
  65. */
  66. static inline void get_mmu_context(struct mm_struct *mm)
  67. {
  68. mm_context_t ctx;
  69. if (mm->context != NO_CONTEXT)
  70. return;
  71. while (atomic_dec_if_positive(&nr_free_contexts) < 0)
  72. steal_context();
  73. ctx = next_mmu_context;
  74. while (test_and_set_bit(ctx, context_map)) {
  75. ctx = find_next_zero_bit(context_map, LAST_CONTEXT+1, ctx);
  76. if (ctx > LAST_CONTEXT)
  77. ctx = 0;
  78. }
  79. next_mmu_context = (ctx + 1) & LAST_CONTEXT;
  80. mm->context = ctx;
  81. context_mm[ctx] = mm;
  82. }
  83. /*
  84. * Set up the context for a new address space.
  85. */
  86. # define init_new_context(tsk, mm) (((mm)->context = NO_CONTEXT), 0)
  87. /*
  88. * We're finished using the context for an address space.
  89. */
  90. #define destroy_context destroy_context
  91. static inline void destroy_context(struct mm_struct *mm)
  92. {
  93. if (mm->context != NO_CONTEXT) {
  94. clear_bit(mm->context, context_map);
  95. mm->context = NO_CONTEXT;
  96. atomic_inc(&nr_free_contexts);
  97. }
  98. }
  99. static inline void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  100. struct task_struct *tsk)
  101. {
  102. tsk->thread.pgdir = next->pgd;
  103. get_mmu_context(next);
  104. set_context(next->context, next->pgd);
  105. }
  106. /*
  107. * After we have set current->mm to a new value, this activates
  108. * the context for the new mm so we see the new mappings.
  109. */
  110. #define activate_mm activate_mm
  111. static inline void activate_mm(struct mm_struct *active_mm,
  112. struct mm_struct *mm)
  113. {
  114. current->thread.pgdir = mm->pgd;
  115. get_mmu_context(mm);
  116. set_context(mm->context, mm->pgd);
  117. }
  118. extern void mmu_context_init(void);
  119. #include <asm-generic/mmu_context.h>
  120. # endif /* __KERNEL__ */
  121. #endif /* _ASM_MICROBLAZE_MMU_CONTEXT_H */