slb.c 7.1 KB

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