stab.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * PowerPC64 Segment Translation Support.
  3. *
  4. * Dave Engebretsen and Mike Corrigan {engebret|mikejc}@us.ibm.com
  5. * Copyright (c) 2001 Dave Engebretsen
  6. *
  7. * Copyright (C) 2002 Anton Blanchard <anton@au.ibm.com>, IBM
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #include <linux/lmb.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/mmu.h>
  17. #include <asm/mmu_context.h>
  18. #include <asm/paca.h>
  19. #include <asm/cputable.h>
  20. #include <asm/prom.h>
  21. #include <asm/abs_addr.h>
  22. #include <asm/firmware.h>
  23. #include <asm/iseries/hv_call.h>
  24. struct stab_entry {
  25. unsigned long esid_data;
  26. unsigned long vsid_data;
  27. };
  28. #define NR_STAB_CACHE_ENTRIES 8
  29. static DEFINE_PER_CPU(long, stab_cache_ptr);
  30. static DEFINE_PER_CPU(long, stab_cache[NR_STAB_CACHE_ENTRIES]);
  31. /*
  32. * Create a segment table entry for the given esid/vsid pair.
  33. */
  34. static int make_ste(unsigned long stab, unsigned long esid, unsigned long vsid)
  35. {
  36. unsigned long esid_data, vsid_data;
  37. unsigned long entry, group, old_esid, castout_entry, i;
  38. unsigned int global_entry;
  39. struct stab_entry *ste, *castout_ste;
  40. unsigned long kernel_segment = (esid << SID_SHIFT) >= PAGE_OFFSET;
  41. vsid_data = vsid << STE_VSID_SHIFT;
  42. esid_data = esid << SID_SHIFT | STE_ESID_KP | STE_ESID_V;
  43. if (! kernel_segment)
  44. esid_data |= STE_ESID_KS;
  45. /* Search the primary group first. */
  46. global_entry = (esid & 0x1f) << 3;
  47. ste = (struct stab_entry *)(stab | ((esid & 0x1f) << 7));
  48. /* Find an empty entry, if one exists. */
  49. for (group = 0; group < 2; group++) {
  50. for (entry = 0; entry < 8; entry++, ste++) {
  51. if (!(ste->esid_data & STE_ESID_V)) {
  52. ste->vsid_data = vsid_data;
  53. eieio();
  54. ste->esid_data = esid_data;
  55. return (global_entry | entry);
  56. }
  57. }
  58. /* Now search the secondary group. */
  59. global_entry = ((~esid) & 0x1f) << 3;
  60. ste = (struct stab_entry *)(stab | (((~esid) & 0x1f) << 7));
  61. }
  62. /*
  63. * Could not find empty entry, pick one with a round robin selection.
  64. * Search all entries in the two groups.
  65. */
  66. castout_entry = get_paca()->stab_rr;
  67. for (i = 0; i < 16; i++) {
  68. if (castout_entry < 8) {
  69. global_entry = (esid & 0x1f) << 3;
  70. ste = (struct stab_entry *)(stab | ((esid & 0x1f) << 7));
  71. castout_ste = ste + castout_entry;
  72. } else {
  73. global_entry = ((~esid) & 0x1f) << 3;
  74. ste = (struct stab_entry *)(stab | (((~esid) & 0x1f) << 7));
  75. castout_ste = ste + (castout_entry - 8);
  76. }
  77. /* Dont cast out the first kernel segment */
  78. if ((castout_ste->esid_data & ESID_MASK) != PAGE_OFFSET)
  79. break;
  80. castout_entry = (castout_entry + 1) & 0xf;
  81. }
  82. get_paca()->stab_rr = (castout_entry + 1) & 0xf;
  83. /* Modify the old entry to the new value. */
  84. /* Force previous translations to complete. DRENG */
  85. asm volatile("isync" : : : "memory");
  86. old_esid = castout_ste->esid_data >> SID_SHIFT;
  87. castout_ste->esid_data = 0; /* Invalidate old entry */
  88. asm volatile("sync" : : : "memory"); /* Order update */
  89. castout_ste->vsid_data = vsid_data;
  90. eieio(); /* Order update */
  91. castout_ste->esid_data = esid_data;
  92. asm volatile("slbie %0" : : "r" (old_esid << SID_SHIFT));
  93. /* Ensure completion of slbie */
  94. asm volatile("sync" : : : "memory");
  95. return (global_entry | (castout_entry & 0x7));
  96. }
  97. /*
  98. * Allocate a segment table entry for the given ea and mm
  99. */
  100. static int __ste_allocate(unsigned long ea, struct mm_struct *mm)
  101. {
  102. unsigned long vsid;
  103. unsigned char stab_entry;
  104. unsigned long offset;
  105. /* Kernel or user address? */
  106. if (is_kernel_addr(ea)) {
  107. vsid = get_kernel_vsid(ea, MMU_SEGSIZE_256M);
  108. } else {
  109. if ((ea >= TASK_SIZE_USER64) || (! mm))
  110. return 1;
  111. vsid = get_vsid(mm->context.id, ea, MMU_SEGSIZE_256M);
  112. }
  113. stab_entry = make_ste(get_paca()->stab_addr, GET_ESID(ea), vsid);
  114. if (!is_kernel_addr(ea)) {
  115. offset = __get_cpu_var(stab_cache_ptr);
  116. if (offset < NR_STAB_CACHE_ENTRIES)
  117. __get_cpu_var(stab_cache[offset++]) = stab_entry;
  118. else
  119. offset = NR_STAB_CACHE_ENTRIES+1;
  120. __get_cpu_var(stab_cache_ptr) = offset;
  121. /* Order update */
  122. asm volatile("sync":::"memory");
  123. }
  124. return 0;
  125. }
  126. int ste_allocate(unsigned long ea)
  127. {
  128. return __ste_allocate(ea, current->mm);
  129. }
  130. /*
  131. * Do the segment table work for a context switch: flush all user
  132. * entries from the table, then preload some probably useful entries
  133. * for the new task
  134. */
  135. void switch_stab(struct task_struct *tsk, struct mm_struct *mm)
  136. {
  137. struct stab_entry *stab = (struct stab_entry *) get_paca()->stab_addr;
  138. struct stab_entry *ste;
  139. unsigned long offset = __get_cpu_var(stab_cache_ptr);
  140. unsigned long pc = KSTK_EIP(tsk);
  141. unsigned long stack = KSTK_ESP(tsk);
  142. unsigned long unmapped_base;
  143. /* Force previous translations to complete. DRENG */
  144. asm volatile("isync" : : : "memory");
  145. if (offset <= NR_STAB_CACHE_ENTRIES) {
  146. int i;
  147. for (i = 0; i < offset; i++) {
  148. ste = stab + __get_cpu_var(stab_cache[i]);
  149. ste->esid_data = 0; /* invalidate entry */
  150. }
  151. } else {
  152. unsigned long entry;
  153. /* Invalidate all entries. */
  154. ste = stab;
  155. /* Never flush the first entry. */
  156. ste += 1;
  157. for (entry = 1;
  158. entry < (HW_PAGE_SIZE / sizeof(struct stab_entry));
  159. entry++, ste++) {
  160. unsigned long ea;
  161. ea = ste->esid_data & ESID_MASK;
  162. if (!is_kernel_addr(ea)) {
  163. ste->esid_data = 0;
  164. }
  165. }
  166. }
  167. asm volatile("sync; slbia; sync":::"memory");
  168. __get_cpu_var(stab_cache_ptr) = 0;
  169. /* Now preload some entries for the new task */
  170. if (test_tsk_thread_flag(tsk, TIF_32BIT))
  171. unmapped_base = TASK_UNMAPPED_BASE_USER32;
  172. else
  173. unmapped_base = TASK_UNMAPPED_BASE_USER64;
  174. __ste_allocate(pc, mm);
  175. if (GET_ESID(pc) == GET_ESID(stack))
  176. return;
  177. __ste_allocate(stack, mm);
  178. if ((GET_ESID(pc) == GET_ESID(unmapped_base))
  179. || (GET_ESID(stack) == GET_ESID(unmapped_base)))
  180. return;
  181. __ste_allocate(unmapped_base, mm);
  182. /* Order update */
  183. asm volatile("sync" : : : "memory");
  184. }
  185. /*
  186. * Allocate segment tables for secondary CPUs. These must all go in
  187. * the first (bolted) segment, so that do_stab_bolted won't get a
  188. * recursive segment miss on the segment table itself.
  189. */
  190. void __init stabs_alloc(void)
  191. {
  192. int cpu;
  193. if (cpu_has_feature(CPU_FTR_SLB))
  194. return;
  195. for_each_possible_cpu(cpu) {
  196. unsigned long newstab;
  197. if (cpu == 0)
  198. continue; /* stab for CPU 0 is statically allocated */
  199. newstab = lmb_alloc_base(HW_PAGE_SIZE, HW_PAGE_SIZE,
  200. 1<<SID_SHIFT);
  201. newstab = (unsigned long)__va(newstab);
  202. memset((void *)newstab, 0, HW_PAGE_SIZE);
  203. paca[cpu].stab_addr = newstab;
  204. paca[cpu].stab_real = virt_to_abs(newstab);
  205. printk(KERN_INFO "Segment table for CPU %d at 0x%llx "
  206. "virtual, 0x%llx absolute\n",
  207. cpu, paca[cpu].stab_addr, paca[cpu].stab_real);
  208. }
  209. }
  210. /*
  211. * Build an entry for the base kernel segment and put it into
  212. * the segment table or SLB. All other segment table or SLB
  213. * entries are faulted in.
  214. */
  215. void stab_initialize(unsigned long stab)
  216. {
  217. unsigned long vsid = get_kernel_vsid(PAGE_OFFSET, MMU_SEGSIZE_256M);
  218. unsigned long stabreal;
  219. asm volatile("isync; slbia; isync":::"memory");
  220. make_ste(stab, GET_ESID(PAGE_OFFSET), vsid);
  221. /* Order update */
  222. asm volatile("sync":::"memory");
  223. /* Set ASR */
  224. stabreal = get_paca()->stab_real | 0x1ul;
  225. #ifdef CONFIG_PPC_ISERIES
  226. if (firmware_has_feature(FW_FEATURE_ISERIES)) {
  227. HvCall1(HvCallBaseSetASR, stabreal);
  228. return;
  229. }
  230. #endif /* CONFIG_PPC_ISERIES */
  231. mtspr(SPRN_ASR, stabreal);
  232. }