mmu_context.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * MMU context handling.
  3. *
  4. * Copyright (C) 2011 Tobias Klauser <tklauser@distanz.ch>
  5. * Copyright (C) 2009 Wind River Systems Inc
  6. * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/mm.h>
  13. #include <asm/cpuinfo.h>
  14. #include <asm/mmu_context.h>
  15. #include <asm/tlb.h>
  16. /* The pids position and mask in context */
  17. #define PID_SHIFT 0
  18. #define PID_BITS (cpuinfo.tlb_pid_num_bits)
  19. #define PID_MASK ((1UL << PID_BITS) - 1)
  20. /* The versions position and mask in context */
  21. #define VERSION_BITS (32 - PID_BITS)
  22. #define VERSION_SHIFT (PID_SHIFT + PID_BITS)
  23. #define VERSION_MASK ((1UL << VERSION_BITS) - 1)
  24. /* Return the version part of a context */
  25. #define CTX_VERSION(c) (((c) >> VERSION_SHIFT) & VERSION_MASK)
  26. /* Return the pid part of a context */
  27. #define CTX_PID(c) (((c) >> PID_SHIFT) & PID_MASK)
  28. /* Value of the first context (version 1, pid 0) */
  29. #define FIRST_CTX ((1UL << VERSION_SHIFT) | (0 << PID_SHIFT))
  30. static mm_context_t next_mmu_context;
  31. /*
  32. * Initialize MMU context management stuff.
  33. */
  34. void __init mmu_context_init(void)
  35. {
  36. /* We need to set this here because the value depends on runtime data
  37. * from cpuinfo */
  38. next_mmu_context = FIRST_CTX;
  39. }
  40. /*
  41. * Set new context (pid), keep way
  42. */
  43. static void set_context(mm_context_t context)
  44. {
  45. set_mmu_pid(CTX_PID(context));
  46. }
  47. static mm_context_t get_new_context(void)
  48. {
  49. /* Return the next pid */
  50. next_mmu_context += (1UL << PID_SHIFT);
  51. /* If the pid field wraps around we increase the version and
  52. * flush the tlb */
  53. if (unlikely(CTX_PID(next_mmu_context) == 0)) {
  54. /* Version is incremented since the pid increment above
  55. * overflows info version */
  56. flush_cache_all();
  57. flush_tlb_all();
  58. }
  59. /* If the version wraps we start over with the first generation, we do
  60. * not need to flush the tlb here since it's always done above */
  61. if (unlikely(CTX_VERSION(next_mmu_context) == 0))
  62. next_mmu_context = FIRST_CTX;
  63. return next_mmu_context;
  64. }
  65. void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  66. struct task_struct *tsk)
  67. {
  68. unsigned long flags;
  69. local_irq_save(flags);
  70. /* If the process context we are swapping in has a different context
  71. * generation then we have it should get a new generation/pid */
  72. if (unlikely(CTX_VERSION(next->context) !=
  73. CTX_VERSION(next_mmu_context)))
  74. next->context = get_new_context();
  75. /* Save the current pgd so the fast tlb handler can find it */
  76. pgd_current = next->pgd;
  77. /* Set the current context */
  78. set_context(next->context);
  79. local_irq_restore(flags);
  80. }
  81. /*
  82. * After we have set current->mm to a new value, this activates
  83. * the context for the new mm so we see the new mappings.
  84. */
  85. void activate_mm(struct mm_struct *prev, struct mm_struct *next)
  86. {
  87. next->context = get_new_context();
  88. set_context(next->context);
  89. pgd_current = next->pgd;
  90. }
  91. unsigned long get_pid_from_context(mm_context_t *context)
  92. {
  93. return CTX_PID((*context));
  94. }