slb.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * PowerPC64 SLB support.
  3. *
  4. * Copyright (C) 2004 David Gibson <dwg@au.ibm.com>, IBM
  5. * Based on earlier code writteh by:
  6. * Dave Engebretsen and Mike Corrigan {engebret|mikejc}@us.ibm.com
  7. * Copyright (c) 2001 Dave Engebretsen
  8. * Copyright (C) 2002 Anton Blanchard <anton@au.ibm.com>, IBM
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #include <linux/config.h>
  17. #include <asm/pgtable.h>
  18. #include <asm/mmu.h>
  19. #include <asm/mmu_context.h>
  20. #include <asm/paca.h>
  21. #include <asm/cputable.h>
  22. extern void slb_allocate(unsigned long ea);
  23. static inline unsigned long mk_esid_data(unsigned long ea, unsigned long slot)
  24. {
  25. return (ea & ESID_MASK) | SLB_ESID_V | slot;
  26. }
  27. static inline unsigned long mk_vsid_data(unsigned long ea, unsigned long flags)
  28. {
  29. return (get_kernel_vsid(ea) << SLB_VSID_SHIFT) | flags;
  30. }
  31. static inline void create_slbe(unsigned long ea, unsigned long flags,
  32. unsigned long entry)
  33. {
  34. asm volatile("slbmte %0,%1" :
  35. : "r" (mk_vsid_data(ea, flags)),
  36. "r" (mk_esid_data(ea, entry))
  37. : "memory" );
  38. }
  39. static void slb_flush_and_rebolt(void)
  40. {
  41. /* If you change this make sure you change SLB_NUM_BOLTED
  42. * appropriately too. */
  43. unsigned long ksp_flags = SLB_VSID_KERNEL;
  44. unsigned long ksp_esid_data;
  45. WARN_ON(!irqs_disabled());
  46. if (cpu_has_feature(CPU_FTR_16M_PAGE))
  47. ksp_flags |= SLB_VSID_L;
  48. ksp_esid_data = mk_esid_data(get_paca()->kstack, 2);
  49. if ((ksp_esid_data & ESID_MASK) == KERNELBASE)
  50. ksp_esid_data &= ~SLB_ESID_V;
  51. /* We need to do this all in asm, so we're sure we don't touch
  52. * the stack between the slbia and rebolting it. */
  53. asm volatile("isync\n"
  54. "slbia\n"
  55. /* Slot 1 - first VMALLOC segment */
  56. "slbmte %0,%1\n"
  57. /* Slot 2 - kernel stack */
  58. "slbmte %2,%3\n"
  59. "isync"
  60. :: "r"(mk_vsid_data(VMALLOCBASE, SLB_VSID_KERNEL)),
  61. "r"(mk_esid_data(VMALLOCBASE, 1)),
  62. "r"(mk_vsid_data(ksp_esid_data, ksp_flags)),
  63. "r"(ksp_esid_data)
  64. : "memory");
  65. }
  66. /* Flush all user entries from the segment table of the current processor. */
  67. void switch_slb(struct task_struct *tsk, struct mm_struct *mm)
  68. {
  69. unsigned long offset = get_paca()->slb_cache_ptr;
  70. unsigned long esid_data = 0;
  71. unsigned long pc = KSTK_EIP(tsk);
  72. unsigned long stack = KSTK_ESP(tsk);
  73. unsigned long unmapped_base;
  74. if (offset <= SLB_CACHE_ENTRIES) {
  75. int i;
  76. asm volatile("isync" : : : "memory");
  77. for (i = 0; i < offset; i++) {
  78. esid_data = ((unsigned long)get_paca()->slb_cache[i]
  79. << SID_SHIFT) | SLBIE_C;
  80. asm volatile("slbie %0" : : "r" (esid_data));
  81. }
  82. asm volatile("isync" : : : "memory");
  83. } else {
  84. slb_flush_and_rebolt();
  85. }
  86. /* Workaround POWER5 < DD2.1 issue */
  87. if (offset == 1 || offset > SLB_CACHE_ENTRIES)
  88. asm volatile("slbie %0" : : "r" (esid_data));
  89. get_paca()->slb_cache_ptr = 0;
  90. get_paca()->context = mm->context;
  91. /*
  92. * preload some userspace segments into the SLB.
  93. */
  94. if (test_tsk_thread_flag(tsk, TIF_32BIT))
  95. unmapped_base = TASK_UNMAPPED_BASE_USER32;
  96. else
  97. unmapped_base = TASK_UNMAPPED_BASE_USER64;
  98. if (pc >= KERNELBASE)
  99. return;
  100. slb_allocate(pc);
  101. if (GET_ESID(pc) == GET_ESID(stack))
  102. return;
  103. if (stack >= KERNELBASE)
  104. return;
  105. slb_allocate(stack);
  106. if ((GET_ESID(pc) == GET_ESID(unmapped_base))
  107. || (GET_ESID(stack) == GET_ESID(unmapped_base)))
  108. return;
  109. if (unmapped_base >= KERNELBASE)
  110. return;
  111. slb_allocate(unmapped_base);
  112. }
  113. void slb_initialize(void)
  114. {
  115. /* On iSeries the bolted entries have already been set up by
  116. * the hypervisor from the lparMap data in head.S */
  117. #ifndef CONFIG_PPC_ISERIES
  118. unsigned long flags = SLB_VSID_KERNEL;
  119. /* Invalidate the entire SLB (even slot 0) & all the ERATS */
  120. if (cpu_has_feature(CPU_FTR_16M_PAGE))
  121. flags |= SLB_VSID_L;
  122. asm volatile("isync":::"memory");
  123. asm volatile("slbmte %0,%0"::"r" (0) : "memory");
  124. asm volatile("isync; slbia; isync":::"memory");
  125. create_slbe(KERNELBASE, flags, 0);
  126. create_slbe(VMALLOCBASE, SLB_VSID_KERNEL, 1);
  127. /* We don't bolt the stack for the time being - we're in boot,
  128. * so the stack is in the bolted segment. By the time it goes
  129. * elsewhere, we'll call _switch() which will bolt in the new
  130. * one. */
  131. asm volatile("isync":::"memory");
  132. #endif
  133. get_paca()->stab_rr = SLB_NUM_BOLTED;
  134. }