2 * PowerPC64 Segment Translation Support.
4 * Dave Engebretsen and Mike Corrigan {engebret|mikejc}@us.ibm.com
5 * Copyright (c) 2001 Dave Engebretsen
7 * Copyright (C) 2002 Anton Blanchard <anton@au.ibm.com>, IBM
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.
15 #include <linux/config.h>
16 #include <asm/pgtable.h>
18 #include <asm/mmu_context.h>
20 #include <asm/cputable.h>
22 #include <asm/abs_addr.h>
25 unsigned long esid_data
;
26 unsigned long vsid_data
;
29 /* Both the segment table and SLB code uses the following cache */
30 #define NR_STAB_CACHE_ENTRIES 8
31 DEFINE_PER_CPU(long, stab_cache_ptr
);
32 DEFINE_PER_CPU(long, stab_cache
[NR_STAB_CACHE_ENTRIES
]);
35 * Create a segment table entry for the given esid/vsid pair.
37 static int make_ste(unsigned long stab
, unsigned long esid
, unsigned long vsid
)
39 unsigned long esid_data
, vsid_data
;
40 unsigned long entry
, group
, old_esid
, castout_entry
, i
;
41 unsigned int global_entry
;
42 struct stab_entry
*ste
, *castout_ste
;
43 unsigned long kernel_segment
= (esid
<< SID_SHIFT
) >= KERNELBASE
;
45 vsid_data
= vsid
<< STE_VSID_SHIFT
;
46 esid_data
= esid
<< SID_SHIFT
| STE_ESID_KP
| STE_ESID_V
;
48 esid_data
|= STE_ESID_KS
;
50 /* Search the primary group first. */
51 global_entry
= (esid
& 0x1f) << 3;
52 ste
= (struct stab_entry
*)(stab
| ((esid
& 0x1f) << 7));
54 /* Find an empty entry, if one exists. */
55 for (group
= 0; group
< 2; group
++) {
56 for (entry
= 0; entry
< 8; entry
++, ste
++) {
57 if (!(ste
->esid_data
& STE_ESID_V
)) {
58 ste
->vsid_data
= vsid_data
;
59 asm volatile("eieio":::"memory");
60 ste
->esid_data
= esid_data
;
61 return (global_entry
| entry
);
64 /* Now search the secondary group. */
65 global_entry
= ((~esid
) & 0x1f) << 3;
66 ste
= (struct stab_entry
*)(stab
| (((~esid
) & 0x1f) << 7));
70 * Could not find empty entry, pick one with a round robin selection.
71 * Search all entries in the two groups.
73 castout_entry
= get_paca()->stab_rr
;
74 for (i
= 0; i
< 16; i
++) {
75 if (castout_entry
< 8) {
76 global_entry
= (esid
& 0x1f) << 3;
77 ste
= (struct stab_entry
*)(stab
| ((esid
& 0x1f) << 7));
78 castout_ste
= ste
+ castout_entry
;
80 global_entry
= ((~esid
) & 0x1f) << 3;
81 ste
= (struct stab_entry
*)(stab
| (((~esid
) & 0x1f) << 7));
82 castout_ste
= ste
+ (castout_entry
- 8);
85 /* Dont cast out the first kernel segment */
86 if ((castout_ste
->esid_data
& ESID_MASK
) != KERNELBASE
)
89 castout_entry
= (castout_entry
+ 1) & 0xf;
92 get_paca()->stab_rr
= (castout_entry
+ 1) & 0xf;
94 /* Modify the old entry to the new value. */
96 /* Force previous translations to complete. DRENG */
97 asm volatile("isync" : : : "memory");
99 old_esid
= castout_ste
->esid_data
>> SID_SHIFT
;
100 castout_ste
->esid_data
= 0; /* Invalidate old entry */
102 asm volatile("sync" : : : "memory"); /* Order update */
104 castout_ste
->vsid_data
= vsid_data
;
105 asm volatile("eieio" : : : "memory"); /* Order update */
106 castout_ste
->esid_data
= esid_data
;
108 asm volatile("slbie %0" : : "r" (old_esid
<< SID_SHIFT
));
109 /* Ensure completion of slbie */
110 asm volatile("sync" : : : "memory");
112 return (global_entry
| (castout_entry
& 0x7));
116 * Allocate a segment table entry for the given ea and mm
118 static int __ste_allocate(unsigned long ea
, struct mm_struct
*mm
)
121 unsigned char stab_entry
;
122 unsigned long offset
;
124 /* Kernel or user address? */
125 if (ea
>= KERNELBASE
) {
126 vsid
= get_kernel_vsid(ea
);
128 if ((ea
>= TASK_SIZE_USER64
) || (! mm
))
131 vsid
= get_vsid(mm
->context
.id
, ea
);
134 stab_entry
= make_ste(get_paca()->stab_addr
, GET_ESID(ea
), vsid
);
136 if (ea
< KERNELBASE
) {
137 offset
= __get_cpu_var(stab_cache_ptr
);
138 if (offset
< NR_STAB_CACHE_ENTRIES
)
139 __get_cpu_var(stab_cache
[offset
++]) = stab_entry
;
141 offset
= NR_STAB_CACHE_ENTRIES
+1;
142 __get_cpu_var(stab_cache_ptr
) = offset
;
145 asm volatile("sync":::"memory");
151 int ste_allocate(unsigned long ea
)
153 return __ste_allocate(ea
, current
->mm
);
157 * Do the segment table work for a context switch: flush all user
158 * entries from the table, then preload some probably useful entries
161 void switch_stab(struct task_struct
*tsk
, struct mm_struct
*mm
)
163 struct stab_entry
*stab
= (struct stab_entry
*) get_paca()->stab_addr
;
164 struct stab_entry
*ste
;
165 unsigned long offset
= __get_cpu_var(stab_cache_ptr
);
166 unsigned long pc
= KSTK_EIP(tsk
);
167 unsigned long stack
= KSTK_ESP(tsk
);
168 unsigned long unmapped_base
;
170 /* Force previous translations to complete. DRENG */
171 asm volatile("isync" : : : "memory");
173 if (offset
<= NR_STAB_CACHE_ENTRIES
) {
176 for (i
= 0; i
< offset
; i
++) {
177 ste
= stab
+ __get_cpu_var(stab_cache
[i
]);
178 ste
->esid_data
= 0; /* invalidate entry */
183 /* Invalidate all entries. */
186 /* Never flush the first entry. */
189 entry
< (PAGE_SIZE
/ sizeof(struct stab_entry
));
192 ea
= ste
->esid_data
& ESID_MASK
;
193 if (ea
< KERNELBASE
) {
199 asm volatile("sync; slbia; sync":::"memory");
201 __get_cpu_var(stab_cache_ptr
) = 0;
203 /* Now preload some entries for the new task */
204 if (test_tsk_thread_flag(tsk
, TIF_32BIT
))
205 unmapped_base
= TASK_UNMAPPED_BASE_USER32
;
207 unmapped_base
= TASK_UNMAPPED_BASE_USER64
;
209 __ste_allocate(pc
, mm
);
211 if (GET_ESID(pc
) == GET_ESID(stack
))
214 __ste_allocate(stack
, mm
);
216 if ((GET_ESID(pc
) == GET_ESID(unmapped_base
))
217 || (GET_ESID(stack
) == GET_ESID(unmapped_base
)))
220 __ste_allocate(unmapped_base
, mm
);
223 asm volatile("sync" : : : "memory");
226 extern void slb_initialize(void);
229 * Allocate segment tables for secondary CPUs. These must all go in
230 * the first (bolted) segment, so that do_stab_bolted won't get a
231 * recursive segment miss on the segment table itself.
233 void stabs_alloc(void)
237 if (cpu_has_feature(CPU_FTR_SLB
))
241 unsigned long newstab
;
244 continue; /* stab for CPU 0 is statically allocated */
246 newstab
= lmb_alloc_base(PAGE_SIZE
, PAGE_SIZE
, 1<<SID_SHIFT
);
248 panic("Unable to allocate segment table for CPU %d.\n",
251 newstab
+= KERNELBASE
;
253 memset((void *)newstab
, 0, PAGE_SIZE
);
255 paca
[cpu
].stab_addr
= newstab
;
256 paca
[cpu
].stab_real
= virt_to_abs(newstab
);
257 printk(KERN_DEBUG
"Segment table for CPU %d at 0x%lx virtual, 0x%lx absolute\n", cpu
, paca
[cpu
].stab_addr
, paca
[cpu
].stab_real
);
262 * Build an entry for the base kernel segment and put it into
263 * the segment table or SLB. All other segment table or SLB
264 * entries are faulted in.
266 void stab_initialize(unsigned long stab
)
268 unsigned long vsid
= get_kernel_vsid(KERNELBASE
);
270 if (cpu_has_feature(CPU_FTR_SLB
)) {
273 asm volatile("isync; slbia; isync":::"memory");
274 make_ste(stab
, GET_ESID(KERNELBASE
), vsid
);
277 asm volatile("sync":::"memory");