slb.c 7.1 KB

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