mmu_context_nohash.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * This file contains the routines for handling the MMU on those
  3. * PowerPC implementations where the MMU is not using the hash
  4. * table, such as 8xx, 4xx, BookE's etc...
  5. *
  6. * Copyright 2008 Ben Herrenschmidt <benh@kernel.crashing.org>
  7. * IBM Corp.
  8. *
  9. * Derived from previous arch/powerpc/mm/mmu_context.c
  10. * and arch/powerpc/include/asm/mmu_context.h
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. *
  17. */
  18. #include <linux/mm.h>
  19. #include <linux/init.h>
  20. #include <asm/mmu_context.h>
  21. #include <asm/tlbflush.h>
  22. /*
  23. * The MPC8xx has only 16 contexts. We rotate through them on each
  24. * task switch. A better way would be to keep track of tasks that
  25. * own contexts, and implement an LRU usage. That way very active
  26. * tasks don't always have to pay the TLB reload overhead. The
  27. * kernel pages are mapped shared, so the kernel can run on behalf
  28. * of any task that makes a kernel entry. Shared does not mean they
  29. * are not protected, just that the ASID comparison is not performed.
  30. * -- Dan
  31. *
  32. * The IBM4xx has 256 contexts, so we can just rotate through these
  33. * as a way of "switching" contexts. If the TID of the TLB is zero,
  34. * the PID/TID comparison is disabled, so we can use a TID of zero
  35. * to represent all kernel pages as shared among all contexts.
  36. * -- Dan
  37. */
  38. #ifdef CONFIG_8xx
  39. #define NO_CONTEXT 16
  40. #define LAST_CONTEXT 15
  41. #define FIRST_CONTEXT 0
  42. #elif defined(CONFIG_4xx)
  43. #define NO_CONTEXT 256
  44. #define LAST_CONTEXT 255
  45. #define FIRST_CONTEXT 1
  46. #elif defined(CONFIG_E200) || defined(CONFIG_E500)
  47. #define NO_CONTEXT 256
  48. #define LAST_CONTEXT 255
  49. #define FIRST_CONTEXT 1
  50. #else
  51. #error Unsupported processor type
  52. #endif
  53. static unsigned long next_mmu_context;
  54. static unsigned long context_map[LAST_CONTEXT / BITS_PER_LONG + 1];
  55. static atomic_t nr_free_contexts;
  56. static struct mm_struct *context_mm[LAST_CONTEXT+1];
  57. static void steal_context(void);
  58. /* Steal a context from a task that has one at the moment.
  59. * This is only used on 8xx and 4xx and we presently assume that
  60. * they don't do SMP. If they do then this will have to check
  61. * whether the MM we steal is in use.
  62. * We also assume that this is only used on systems that don't
  63. * use an MMU hash table - this is true for 8xx and 4xx.
  64. * This isn't an LRU system, it just frees up each context in
  65. * turn (sort-of pseudo-random replacement :). This would be the
  66. * place to implement an LRU scheme if anyone was motivated to do it.
  67. * -- paulus
  68. */
  69. static void steal_context(void)
  70. {
  71. struct mm_struct *mm;
  72. /* free up context `next_mmu_context' */
  73. /* if we shouldn't free context 0, don't... */
  74. if (next_mmu_context < FIRST_CONTEXT)
  75. next_mmu_context = FIRST_CONTEXT;
  76. mm = context_mm[next_mmu_context];
  77. flush_tlb_mm(mm);
  78. destroy_context(mm);
  79. }
  80. /*
  81. * Get a new mmu context for the address space described by `mm'.
  82. */
  83. static inline void get_mmu_context(struct mm_struct *mm)
  84. {
  85. unsigned long ctx;
  86. if (mm->context.id != NO_CONTEXT)
  87. return;
  88. while (atomic_dec_if_positive(&nr_free_contexts) < 0)
  89. steal_context();
  90. ctx = next_mmu_context;
  91. while (test_and_set_bit(ctx, context_map)) {
  92. ctx = find_next_zero_bit(context_map, LAST_CONTEXT+1, ctx);
  93. if (ctx > LAST_CONTEXT)
  94. ctx = 0;
  95. }
  96. next_mmu_context = (ctx + 1) & LAST_CONTEXT;
  97. mm->context.id = ctx;
  98. context_mm[ctx] = mm;
  99. }
  100. void switch_mmu_context(struct mm_struct *prev, struct mm_struct *next)
  101. {
  102. get_mmu_context(next);
  103. set_context(next->context.id, next->pgd);
  104. }
  105. /*
  106. * Set up the context for a new address space.
  107. */
  108. int init_new_context(struct task_struct *t, struct mm_struct *mm)
  109. {
  110. mm->context.id = NO_CONTEXT;
  111. return 0;
  112. }
  113. /*
  114. * We're finished using the context for an address space.
  115. */
  116. void destroy_context(struct mm_struct *mm)
  117. {
  118. preempt_disable();
  119. if (mm->context.id != NO_CONTEXT) {
  120. clear_bit(mm->context.id, context_map);
  121. mm->context.id = NO_CONTEXT;
  122. atomic_inc(&nr_free_contexts);
  123. }
  124. preempt_enable();
  125. }
  126. /*
  127. * Initialize the context management stuff.
  128. */
  129. void __init mmu_context_init(void)
  130. {
  131. /*
  132. * Some processors have too few contexts to reserve one for
  133. * init_mm, and require using context 0 for a normal task.
  134. * Other processors reserve the use of context zero for the kernel.
  135. * This code assumes FIRST_CONTEXT < 32.
  136. */
  137. context_map[0] = (1 << FIRST_CONTEXT) - 1;
  138. next_mmu_context = FIRST_CONTEXT;
  139. atomic_set(&nr_free_contexts, LAST_CONTEXT - FIRST_CONTEXT + 1);
  140. }