slb.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #undef DEBUG
  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. #include <asm/cacheflush.h>
  23. #ifdef DEBUG
  24. #define DBG(fmt...) udbg_printf(fmt)
  25. #else
  26. #define DBG(fmt...)
  27. #endif
  28. extern void slb_allocate_realmode(unsigned long ea);
  29. extern void slb_allocate_user(unsigned long ea);
  30. static void slb_allocate(unsigned long ea)
  31. {
  32. /* Currently, we do real mode for all SLBs including user, but
  33. * that will change if we bring back dynamic VSIDs
  34. */
  35. slb_allocate_realmode(ea);
  36. }
  37. static inline unsigned long mk_esid_data(unsigned long ea, unsigned long slot)
  38. {
  39. return (ea & ESID_MASK) | SLB_ESID_V | slot;
  40. }
  41. static inline unsigned long mk_vsid_data(unsigned long ea, unsigned long flags)
  42. {
  43. return (get_kernel_vsid(ea) << SLB_VSID_SHIFT) | flags;
  44. }
  45. static inline void create_slbe(unsigned long ea, unsigned long flags,
  46. unsigned long entry)
  47. {
  48. asm volatile("slbmte %0,%1" :
  49. : "r" (mk_vsid_data(ea, flags)),
  50. "r" (mk_esid_data(ea, entry))
  51. : "memory" );
  52. }
  53. void slb_flush_and_rebolt(void)
  54. {
  55. /* If you change this make sure you change SLB_NUM_BOLTED
  56. * appropriately too. */
  57. unsigned long linear_llp, vmalloc_llp, lflags, vflags;
  58. unsigned long ksp_esid_data;
  59. WARN_ON(!irqs_disabled());
  60. linear_llp = mmu_psize_defs[mmu_linear_psize].sllp;
  61. vmalloc_llp = mmu_psize_defs[mmu_vmalloc_psize].sllp;
  62. lflags = SLB_VSID_KERNEL | linear_llp;
  63. vflags = SLB_VSID_KERNEL | vmalloc_llp;
  64. ksp_esid_data = mk_esid_data(get_paca()->kstack, 2);
  65. if ((ksp_esid_data & ESID_MASK) == PAGE_OFFSET)
  66. ksp_esid_data &= ~SLB_ESID_V;
  67. /* We need to do this all in asm, so we're sure we don't touch
  68. * the stack between the slbia and rebolting it. */
  69. asm volatile("isync\n"
  70. "slbia\n"
  71. /* Slot 1 - first VMALLOC segment */
  72. "slbmte %0,%1\n"
  73. /* Slot 2 - kernel stack */
  74. "slbmte %2,%3\n"
  75. "isync"
  76. :: "r"(mk_vsid_data(VMALLOC_START, vflags)),
  77. "r"(mk_esid_data(VMALLOC_START, 1)),
  78. "r"(mk_vsid_data(ksp_esid_data, lflags)),
  79. "r"(ksp_esid_data)
  80. : "memory");
  81. }
  82. /* Flush all user entries from the segment table of the current processor. */
  83. void switch_slb(struct task_struct *tsk, struct mm_struct *mm)
  84. {
  85. unsigned long offset = get_paca()->slb_cache_ptr;
  86. unsigned long esid_data = 0;
  87. unsigned long pc = KSTK_EIP(tsk);
  88. unsigned long stack = KSTK_ESP(tsk);
  89. unsigned long unmapped_base;
  90. if (offset <= SLB_CACHE_ENTRIES) {
  91. int i;
  92. asm volatile("isync" : : : "memory");
  93. for (i = 0; i < offset; i++) {
  94. esid_data = ((unsigned long)get_paca()->slb_cache[i]
  95. << SID_SHIFT) | SLBIE_C;
  96. asm volatile("slbie %0" : : "r" (esid_data));
  97. }
  98. asm volatile("isync" : : : "memory");
  99. } else {
  100. slb_flush_and_rebolt();
  101. }
  102. /* Workaround POWER5 < DD2.1 issue */
  103. if (offset == 1 || offset > SLB_CACHE_ENTRIES)
  104. asm volatile("slbie %0" : : "r" (esid_data));
  105. get_paca()->slb_cache_ptr = 0;
  106. get_paca()->context = mm->context;
  107. /*
  108. * preload some userspace segments into the SLB.
  109. */
  110. if (test_tsk_thread_flag(tsk, TIF_32BIT))
  111. unmapped_base = TASK_UNMAPPED_BASE_USER32;
  112. else
  113. unmapped_base = TASK_UNMAPPED_BASE_USER64;
  114. if (is_kernel_addr(pc))
  115. return;
  116. slb_allocate(pc);
  117. if (GET_ESID(pc) == GET_ESID(stack))
  118. return;
  119. if (is_kernel_addr(stack))
  120. return;
  121. slb_allocate(stack);
  122. if ((GET_ESID(pc) == GET_ESID(unmapped_base))
  123. || (GET_ESID(stack) == GET_ESID(unmapped_base)))
  124. return;
  125. if (is_kernel_addr(unmapped_base))
  126. return;
  127. slb_allocate(unmapped_base);
  128. }
  129. static inline void patch_slb_encoding(unsigned int *insn_addr,
  130. unsigned int immed)
  131. {
  132. /* Assume the instruction had a "0" immediate value, just
  133. * "or" in the new value
  134. */
  135. *insn_addr |= immed;
  136. flush_icache_range((unsigned long)insn_addr, 4+
  137. (unsigned long)insn_addr);
  138. }
  139. void slb_initialize(void)
  140. {
  141. unsigned long linear_llp, vmalloc_llp, io_llp;
  142. static int slb_encoding_inited;
  143. extern unsigned int *slb_miss_kernel_load_linear;
  144. extern unsigned int *slb_miss_kernel_load_io;
  145. #ifdef CONFIG_HUGETLB_PAGE
  146. extern unsigned int *slb_miss_user_load_huge;
  147. unsigned long huge_llp;
  148. huge_llp = mmu_psize_defs[mmu_huge_psize].sllp;
  149. #endif
  150. /* Prepare our SLB miss handler based on our page size */
  151. linear_llp = mmu_psize_defs[mmu_linear_psize].sllp;
  152. io_llp = mmu_psize_defs[mmu_io_psize].sllp;
  153. vmalloc_llp = mmu_psize_defs[mmu_vmalloc_psize].sllp;
  154. get_paca()->vmalloc_sllp = SLB_VSID_KERNEL | vmalloc_llp;
  155. if (!slb_encoding_inited) {
  156. slb_encoding_inited = 1;
  157. patch_slb_encoding(slb_miss_kernel_load_linear,
  158. SLB_VSID_KERNEL | linear_llp);
  159. patch_slb_encoding(slb_miss_kernel_load_io,
  160. SLB_VSID_KERNEL | io_llp);
  161. DBG("SLB: linear LLP = %04x\n", linear_llp);
  162. DBG("SLB: io LLP = %04x\n", io_llp);
  163. #ifdef CONFIG_HUGETLB_PAGE
  164. patch_slb_encoding(slb_miss_user_load_huge,
  165. SLB_VSID_USER | huge_llp);
  166. DBG("SLB: huge LLP = %04x\n", huge_llp);
  167. #endif
  168. }
  169. /* On iSeries the bolted entries have already been set up by
  170. * the hypervisor from the lparMap data in head.S */
  171. #ifndef CONFIG_PPC_ISERIES
  172. {
  173. unsigned long lflags, vflags;
  174. lflags = SLB_VSID_KERNEL | linear_llp;
  175. vflags = SLB_VSID_KERNEL | vmalloc_llp;
  176. /* Invalidate the entire SLB (even slot 0) & all the ERATS */
  177. asm volatile("isync":::"memory");
  178. asm volatile("slbmte %0,%0"::"r" (0) : "memory");
  179. asm volatile("isync; slbia; isync":::"memory");
  180. create_slbe(PAGE_OFFSET, lflags, 0);
  181. create_slbe(VMALLOC_START, vflags, 1);
  182. /* We don't bolt the stack for the time being - we're in boot,
  183. * so the stack is in the bolted segment. By the time it goes
  184. * elsewhere, we'll call _switch() which will bolt in the new
  185. * one. */
  186. asm volatile("isync":::"memory");
  187. }
  188. #endif /* CONFIG_PPC_ISERIES */
  189. get_paca()->stab_rr = SLB_NUM_BOLTED;
  190. }