mmu_context_hash32.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * This file contains the routines for handling the MMU on those
  3. * PowerPC implementations where the MMU substantially follows the
  4. * architecture specification. This includes the 6xx, 7xx, 7xxx,
  5. * 8260, and POWER3 implementations but excludes the 8xx and 4xx.
  6. * -- paulus
  7. *
  8. * Derived from arch/ppc/mm/init.c:
  9. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  10. *
  11. * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
  12. * and Cort Dougan (PReP) (cort@cs.nmt.edu)
  13. * Copyright (C) 1996 Paul Mackerras
  14. *
  15. * Derived from "arch/i386/mm/init.c"
  16. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License
  20. * as published by the Free Software Foundation; either version
  21. * 2 of the License, or (at your option) any later version.
  22. *
  23. */
  24. #include <linux/mm.h>
  25. #include <linux/init.h>
  26. #include <asm/mmu_context.h>
  27. #include <asm/tlbflush.h>
  28. /*
  29. * On 32-bit PowerPC 6xx/7xx/7xxx CPUs, we use a set of 16 VSIDs
  30. * (virtual segment identifiers) for each context. Although the
  31. * hardware supports 24-bit VSIDs, and thus >1 million contexts,
  32. * we only use 32,768 of them. That is ample, since there can be
  33. * at most around 30,000 tasks in the system anyway, and it means
  34. * that we can use a bitmap to indicate which contexts are in use.
  35. * Using a bitmap means that we entirely avoid all of the problems
  36. * that we used to have when the context number overflowed,
  37. * particularly on SMP systems.
  38. * -- paulus.
  39. */
  40. #define NO_CONTEXT ((unsigned long) -1)
  41. #define LAST_CONTEXT 32767
  42. #define FIRST_CONTEXT 1
  43. /*
  44. * This function defines the mapping from contexts to VSIDs (virtual
  45. * segment IDs). We use a skew on both the context and the high 4 bits
  46. * of the 32-bit virtual address (the "effective segment ID") in order
  47. * to spread out the entries in the MMU hash table. Note, if this
  48. * function is changed then arch/ppc/mm/hashtable.S will have to be
  49. * changed to correspond.
  50. *
  51. *
  52. * CTX_TO_VSID(ctx, va) (((ctx) * (897 * 16) + ((va) >> 28) * 0x111) \
  53. * & 0xffffff)
  54. */
  55. static unsigned long next_mmu_context;
  56. static unsigned long context_map[LAST_CONTEXT / BITS_PER_LONG + 1];
  57. unsigned long __init_new_context(void)
  58. {
  59. unsigned long ctx = next_mmu_context;
  60. while (test_and_set_bit(ctx, context_map)) {
  61. ctx = find_next_zero_bit(context_map, LAST_CONTEXT+1, ctx);
  62. if (ctx > LAST_CONTEXT)
  63. ctx = 0;
  64. }
  65. next_mmu_context = (ctx + 1) & LAST_CONTEXT;
  66. return ctx;
  67. }
  68. EXPORT_SYMBOL_GPL(__init_new_context);
  69. /*
  70. * Set up the context for a new address space.
  71. */
  72. int init_new_context(struct task_struct *t, struct mm_struct *mm)
  73. {
  74. mm->context.id = __init_new_context();
  75. return 0;
  76. }
  77. /*
  78. * Free a context ID. Make sure to call this with preempt disabled!
  79. */
  80. void __destroy_context(unsigned long ctx)
  81. {
  82. clear_bit(ctx, context_map);
  83. }
  84. EXPORT_SYMBOL_GPL(__destroy_context);
  85. /*
  86. * We're finished using the context for an address space.
  87. */
  88. void destroy_context(struct mm_struct *mm)
  89. {
  90. preempt_disable();
  91. if (mm->context.id != NO_CONTEXT) {
  92. __destroy_context(mm->context.id);
  93. mm->context.id = NO_CONTEXT;
  94. }
  95. preempt_enable();
  96. }
  97. /*
  98. * Initialize the context management stuff.
  99. */
  100. void __init mmu_context_init(void)
  101. {
  102. /* Reserve context 0 for kernel use */
  103. context_map[0] = (1 << FIRST_CONTEXT) - 1;
  104. next_mmu_context = FIRST_CONTEXT;
  105. }