stab.c 7.5 KB

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