slb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * PowerPC64 SLB support.
  3. *
  4. * Copyright (C) 2004 David Gibson <dwg@au.ibm.com>, IBM
  5. * Based on earlier code written 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 <asm/pgtable.h>
  17. #include <asm/mmu.h>
  18. #include <asm/mmu_context.h>
  19. #include <asm/paca.h>
  20. #include <asm/cputable.h>
  21. #include <asm/cacheflush.h>
  22. #include <asm/smp.h>
  23. #include <linux/compiler.h>
  24. #include <asm/udbg.h>
  25. #include <asm/code-patching.h>
  26. enum slb_index {
  27. LINEAR_INDEX = 0, /* Kernel linear map (0xc000000000000000) */
  28. VMALLOC_INDEX = 1, /* Kernel virtual map (0xd000000000000000) */
  29. KSTACK_INDEX = 2, /* Kernel stack map */
  30. };
  31. extern void slb_allocate_realmode(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. #define slb_esid_mask(ssize) \
  40. (((ssize) == MMU_SEGSIZE_256M)? ESID_MASK: ESID_MASK_1T)
  41. static inline unsigned long mk_esid_data(unsigned long ea, int ssize,
  42. enum slb_index index)
  43. {
  44. return (ea & slb_esid_mask(ssize)) | SLB_ESID_V | index;
  45. }
  46. static inline unsigned long mk_vsid_data(unsigned long ea, int ssize,
  47. unsigned long flags)
  48. {
  49. return (get_kernel_vsid(ea, ssize) << slb_vsid_shift(ssize)) | flags |
  50. ((unsigned long) ssize << SLB_VSID_SSIZE_SHIFT);
  51. }
  52. static inline void slb_shadow_update(unsigned long ea, int ssize,
  53. unsigned long flags,
  54. enum slb_index index)
  55. {
  56. struct slb_shadow *p = get_slb_shadow();
  57. /*
  58. * Clear the ESID first so the entry is not valid while we are
  59. * updating it. No write barriers are needed here, provided
  60. * we only update the current CPU's SLB shadow buffer.
  61. */
  62. p->save_area[index].esid = 0;
  63. p->save_area[index].vsid = cpu_to_be64(mk_vsid_data(ea, ssize, flags));
  64. p->save_area[index].esid = cpu_to_be64(mk_esid_data(ea, ssize, index));
  65. }
  66. static inline void slb_shadow_clear(enum slb_index index)
  67. {
  68. get_slb_shadow()->save_area[index].esid = 0;
  69. }
  70. static inline void create_shadowed_slbe(unsigned long ea, int ssize,
  71. unsigned long flags,
  72. enum slb_index index)
  73. {
  74. /*
  75. * Updating the shadow buffer before writing the SLB ensures
  76. * we don't get a stale entry here if we get preempted by PHYP
  77. * between these two statements.
  78. */
  79. slb_shadow_update(ea, ssize, flags, index);
  80. asm volatile("slbmte %0,%1" :
  81. : "r" (mk_vsid_data(ea, ssize, flags)),
  82. "r" (mk_esid_data(ea, ssize, index))
  83. : "memory" );
  84. }
  85. static void __slb_flush_and_rebolt(void)
  86. {
  87. /* If you change this make sure you change SLB_NUM_BOLTED
  88. * and PR KVM appropriately too. */
  89. unsigned long linear_llp, vmalloc_llp, lflags, vflags;
  90. unsigned long ksp_esid_data, ksp_vsid_data;
  91. linear_llp = mmu_psize_defs[mmu_linear_psize].sllp;
  92. vmalloc_llp = mmu_psize_defs[mmu_vmalloc_psize].sllp;
  93. lflags = SLB_VSID_KERNEL | linear_llp;
  94. vflags = SLB_VSID_KERNEL | vmalloc_llp;
  95. ksp_esid_data = mk_esid_data(get_paca()->kstack, mmu_kernel_ssize, KSTACK_INDEX);
  96. if ((ksp_esid_data & ~0xfffffffUL) <= PAGE_OFFSET) {
  97. ksp_esid_data &= ~SLB_ESID_V;
  98. ksp_vsid_data = 0;
  99. slb_shadow_clear(KSTACK_INDEX);
  100. } else {
  101. /* Update stack entry; others don't change */
  102. slb_shadow_update(get_paca()->kstack, mmu_kernel_ssize, lflags, KSTACK_INDEX);
  103. ksp_vsid_data =
  104. be64_to_cpu(get_slb_shadow()->save_area[KSTACK_INDEX].vsid);
  105. }
  106. /* We need to do this all in asm, so we're sure we don't touch
  107. * the stack between the slbia and rebolting it. */
  108. asm volatile("isync\n"
  109. "slbia\n"
  110. /* Slot 1 - first VMALLOC segment */
  111. "slbmte %0,%1\n"
  112. /* Slot 2 - kernel stack */
  113. "slbmte %2,%3\n"
  114. "isync"
  115. :: "r"(mk_vsid_data(VMALLOC_START, mmu_kernel_ssize, vflags)),
  116. "r"(mk_esid_data(VMALLOC_START, mmu_kernel_ssize, 1)),
  117. "r"(ksp_vsid_data),
  118. "r"(ksp_esid_data)
  119. : "memory");
  120. }
  121. void slb_flush_and_rebolt(void)
  122. {
  123. WARN_ON(!irqs_disabled());
  124. /*
  125. * We can't take a PMU exception in the following code, so hard
  126. * disable interrupts.
  127. */
  128. hard_irq_disable();
  129. __slb_flush_and_rebolt();
  130. get_paca()->slb_cache_ptr = 0;
  131. }
  132. void slb_vmalloc_update(void)
  133. {
  134. unsigned long vflags;
  135. vflags = SLB_VSID_KERNEL | mmu_psize_defs[mmu_vmalloc_psize].sllp;
  136. slb_shadow_update(VMALLOC_START, mmu_kernel_ssize, vflags, VMALLOC_INDEX);
  137. slb_flush_and_rebolt();
  138. }
  139. /* Helper function to compare esids. There are four cases to handle.
  140. * 1. The system is not 1T segment size capable. Use the GET_ESID compare.
  141. * 2. The system is 1T capable, both addresses are < 1T, use the GET_ESID compare.
  142. * 3. The system is 1T capable, only one of the two addresses is > 1T. This is not a match.
  143. * 4. The system is 1T capable, both addresses are > 1T, use the GET_ESID_1T macro to compare.
  144. */
  145. static inline int esids_match(unsigned long addr1, unsigned long addr2)
  146. {
  147. int esid_1t_count;
  148. /* System is not 1T segment size capable. */
  149. if (!mmu_has_feature(MMU_FTR_1T_SEGMENT))
  150. return (GET_ESID(addr1) == GET_ESID(addr2));
  151. esid_1t_count = (((addr1 >> SID_SHIFT_1T) != 0) +
  152. ((addr2 >> SID_SHIFT_1T) != 0));
  153. /* both addresses are < 1T */
  154. if (esid_1t_count == 0)
  155. return (GET_ESID(addr1) == GET_ESID(addr2));
  156. /* One address < 1T, the other > 1T. Not a match */
  157. if (esid_1t_count == 1)
  158. return 0;
  159. /* Both addresses are > 1T. */
  160. return (GET_ESID_1T(addr1) == GET_ESID_1T(addr2));
  161. }
  162. /* Flush all user entries from the segment table of the current processor. */
  163. void switch_slb(struct task_struct *tsk, struct mm_struct *mm)
  164. {
  165. unsigned long offset;
  166. unsigned long slbie_data = 0;
  167. unsigned long pc = KSTK_EIP(tsk);
  168. unsigned long stack = KSTK_ESP(tsk);
  169. unsigned long exec_base;
  170. /*
  171. * We need interrupts hard-disabled here, not just soft-disabled,
  172. * so that a PMU interrupt can't occur, which might try to access
  173. * user memory (to get a stack trace) and possible cause an SLB miss
  174. * which would update the slb_cache/slb_cache_ptr fields in the PACA.
  175. */
  176. hard_irq_disable();
  177. offset = get_paca()->slb_cache_ptr;
  178. if (!mmu_has_feature(MMU_FTR_NO_SLBIE_B) &&
  179. offset <= SLB_CACHE_ENTRIES) {
  180. int i;
  181. asm volatile("isync" : : : "memory");
  182. for (i = 0; i < offset; i++) {
  183. slbie_data = (unsigned long)get_paca()->slb_cache[i]
  184. << SID_SHIFT; /* EA */
  185. slbie_data |= user_segment_size(slbie_data)
  186. << SLBIE_SSIZE_SHIFT;
  187. slbie_data |= SLBIE_C; /* C set for user addresses */
  188. asm volatile("slbie %0" : : "r" (slbie_data));
  189. }
  190. asm volatile("isync" : : : "memory");
  191. } else {
  192. __slb_flush_and_rebolt();
  193. }
  194. /* Workaround POWER5 < DD2.1 issue */
  195. if (offset == 1 || offset > SLB_CACHE_ENTRIES)
  196. asm volatile("slbie %0" : : "r" (slbie_data));
  197. get_paca()->slb_cache_ptr = 0;
  198. copy_mm_to_paca(&mm->context);
  199. /*
  200. * preload some userspace segments into the SLB.
  201. * Almost all 32 and 64bit PowerPC executables are linked at
  202. * 0x10000000 so it makes sense to preload this segment.
  203. */
  204. exec_base = 0x10000000;
  205. if (is_kernel_addr(pc) || is_kernel_addr(stack) ||
  206. is_kernel_addr(exec_base))
  207. return;
  208. slb_allocate(pc);
  209. if (!esids_match(pc, stack))
  210. slb_allocate(stack);
  211. if (!esids_match(pc, exec_base) &&
  212. !esids_match(stack, exec_base))
  213. slb_allocate(exec_base);
  214. }
  215. static inline void patch_slb_encoding(unsigned int *insn_addr,
  216. unsigned int immed)
  217. {
  218. /*
  219. * This function patches either an li or a cmpldi instruction with
  220. * a new immediate value. This relies on the fact that both li
  221. * (which is actually addi) and cmpldi both take a 16-bit immediate
  222. * value, and it is situated in the same location in the instruction,
  223. * ie. bits 16-31 (Big endian bit order) or the lower 16 bits.
  224. * The signedness of the immediate operand differs between the two
  225. * instructions however this code is only ever patching a small value,
  226. * much less than 1 << 15, so we can get away with it.
  227. * To patch the value we read the existing instruction, clear the
  228. * immediate value, and or in our new value, then write the instruction
  229. * back.
  230. */
  231. unsigned int insn = (*insn_addr & 0xffff0000) | immed;
  232. patch_instruction(insn_addr, insn);
  233. }
  234. extern u32 slb_miss_kernel_load_linear[];
  235. extern u32 slb_miss_kernel_load_io[];
  236. extern u32 slb_compare_rr_to_size[];
  237. extern u32 slb_miss_kernel_load_vmemmap[];
  238. void slb_set_size(u16 size)
  239. {
  240. if (mmu_slb_size == size)
  241. return;
  242. mmu_slb_size = size;
  243. patch_slb_encoding(slb_compare_rr_to_size, mmu_slb_size);
  244. }
  245. void slb_initialize(void)
  246. {
  247. unsigned long linear_llp, vmalloc_llp, io_llp;
  248. unsigned long lflags, vflags;
  249. static int slb_encoding_inited;
  250. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  251. unsigned long vmemmap_llp;
  252. #endif
  253. /* Prepare our SLB miss handler based on our page size */
  254. linear_llp = mmu_psize_defs[mmu_linear_psize].sllp;
  255. io_llp = mmu_psize_defs[mmu_io_psize].sllp;
  256. vmalloc_llp = mmu_psize_defs[mmu_vmalloc_psize].sllp;
  257. get_paca()->vmalloc_sllp = SLB_VSID_KERNEL | vmalloc_llp;
  258. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  259. vmemmap_llp = mmu_psize_defs[mmu_vmemmap_psize].sllp;
  260. #endif
  261. if (!slb_encoding_inited) {
  262. slb_encoding_inited = 1;
  263. patch_slb_encoding(slb_miss_kernel_load_linear,
  264. SLB_VSID_KERNEL | linear_llp);
  265. patch_slb_encoding(slb_miss_kernel_load_io,
  266. SLB_VSID_KERNEL | io_llp);
  267. patch_slb_encoding(slb_compare_rr_to_size,
  268. mmu_slb_size);
  269. pr_devel("SLB: linear LLP = %04lx\n", linear_llp);
  270. pr_devel("SLB: io LLP = %04lx\n", io_llp);
  271. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  272. patch_slb_encoding(slb_miss_kernel_load_vmemmap,
  273. SLB_VSID_KERNEL | vmemmap_llp);
  274. pr_devel("SLB: vmemmap LLP = %04lx\n", vmemmap_llp);
  275. #endif
  276. }
  277. get_paca()->stab_rr = SLB_NUM_BOLTED;
  278. lflags = SLB_VSID_KERNEL | linear_llp;
  279. vflags = SLB_VSID_KERNEL | vmalloc_llp;
  280. /* Invalidate the entire SLB (even entry 0) & all the ERATS */
  281. asm volatile("isync":::"memory");
  282. asm volatile("slbmte %0,%0"::"r" (0) : "memory");
  283. asm volatile("isync; slbia; isync":::"memory");
  284. create_shadowed_slbe(PAGE_OFFSET, mmu_kernel_ssize, lflags, LINEAR_INDEX);
  285. create_shadowed_slbe(VMALLOC_START, mmu_kernel_ssize, vflags, VMALLOC_INDEX);
  286. /* For the boot cpu, we're running on the stack in init_thread_union,
  287. * which is in the first segment of the linear mapping, and also
  288. * get_paca()->kstack hasn't been initialized yet.
  289. * For secondary cpus, we need to bolt the kernel stack entry now.
  290. */
  291. slb_shadow_clear(KSTACK_INDEX);
  292. if (raw_smp_processor_id() != boot_cpuid &&
  293. (get_paca()->kstack & slb_esid_mask(mmu_kernel_ssize)) > PAGE_OFFSET)
  294. create_shadowed_slbe(get_paca()->kstack,
  295. mmu_kernel_ssize, lflags, KSTACK_INDEX);
  296. asm volatile("isync":::"memory");
  297. }