blob: 98cd1dc2ae752f46d37b1dc28badac59794204c7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
David S. Millerd9b2b2a2008-02-13 16:56:49 -080015#include <linux/lmb.h>
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080022#include <asm/prom.h>
David Gibson533f0812005-07-27 11:44:19 -070023#include <asm/abs_addr.h>
Paul Mackerras799d6042005-11-10 13:37:51 +110024#include <asm/firmware.h>
Kamalesh Babulalf9b6c1d2007-11-19 17:44:05 +110025#include <asm/iseries/hv_call.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
David Gibson1f8d4192005-05-05 16:15:13 -070027struct stab_entry {
28 unsigned long esid_data;
29 unsigned long vsid_data;
30};
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define NR_STAB_CACHE_ENTRIES 8
Michael Ellerman09de9ff2008-05-08 14:27:07 +100033static DEFINE_PER_CPU(long, stab_cache_ptr);
34static DEFINE_PER_CPU(long, stab_cache[NR_STAB_CACHE_ENTRIES]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/*
37 * Create a segment table entry for the given esid/vsid pair.
38 */
39static int make_ste(unsigned long stab, unsigned long esid, unsigned long vsid)
40{
41 unsigned long esid_data, vsid_data;
42 unsigned long entry, group, old_esid, castout_entry, i;
43 unsigned int global_entry;
44 struct stab_entry *ste, *castout_ste;
Michael Ellermanb5666f72005-12-05 10:24:33 -060045 unsigned long kernel_segment = (esid << SID_SHIFT) >= PAGE_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 vsid_data = vsid << STE_VSID_SHIFT;
48 esid_data = esid << SID_SHIFT | STE_ESID_KP | STE_ESID_V;
49 if (! kernel_segment)
50 esid_data |= STE_ESID_KS;
51
52 /* Search the primary group first. */
53 global_entry = (esid & 0x1f) << 3;
54 ste = (struct stab_entry *)(stab | ((esid & 0x1f) << 7));
55
56 /* Find an empty entry, if one exists. */
57 for (group = 0; group < 2; group++) {
58 for (entry = 0; entry < 8; entry++, ste++) {
59 if (!(ste->esid_data & STE_ESID_V)) {
60 ste->vsid_data = vsid_data;
Kumar Gala74a0ba62007-07-09 23:49:09 -050061 eieio();
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 ste->esid_data = esid_data;
63 return (global_entry | entry);
64 }
65 }
66 /* Now search the secondary group. */
67 global_entry = ((~esid) & 0x1f) << 3;
68 ste = (struct stab_entry *)(stab | (((~esid) & 0x1f) << 7));
69 }
70
71 /*
72 * Could not find empty entry, pick one with a round robin selection.
73 * Search all entries in the two groups.
74 */
75 castout_entry = get_paca()->stab_rr;
76 for (i = 0; i < 16; i++) {
77 if (castout_entry < 8) {
78 global_entry = (esid & 0x1f) << 3;
79 ste = (struct stab_entry *)(stab | ((esid & 0x1f) << 7));
80 castout_ste = ste + castout_entry;
81 } else {
82 global_entry = ((~esid) & 0x1f) << 3;
83 ste = (struct stab_entry *)(stab | (((~esid) & 0x1f) << 7));
84 castout_ste = ste + (castout_entry - 8);
85 }
86
87 /* Dont cast out the first kernel segment */
Michael Ellermanb5666f72005-12-05 10:24:33 -060088 if ((castout_ste->esid_data & ESID_MASK) != PAGE_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 break;
90
91 castout_entry = (castout_entry + 1) & 0xf;
92 }
93
94 get_paca()->stab_rr = (castout_entry + 1) & 0xf;
95
96 /* Modify the old entry to the new value. */
97
98 /* Force previous translations to complete. DRENG */
99 asm volatile("isync" : : : "memory");
100
101 old_esid = castout_ste->esid_data >> SID_SHIFT;
102 castout_ste->esid_data = 0; /* Invalidate old entry */
103
104 asm volatile("sync" : : : "memory"); /* Order update */
105
106 castout_ste->vsid_data = vsid_data;
Kumar Gala74a0ba62007-07-09 23:49:09 -0500107 eieio(); /* Order update */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 castout_ste->esid_data = esid_data;
109
110 asm volatile("slbie %0" : : "r" (old_esid << SID_SHIFT));
111 /* Ensure completion of slbie */
112 asm volatile("sync" : : : "memory");
113
114 return (global_entry | (castout_entry & 0x7));
115}
116
117/*
118 * Allocate a segment table entry for the given ea and mm
119 */
120static int __ste_allocate(unsigned long ea, struct mm_struct *mm)
121{
122 unsigned long vsid;
123 unsigned char stab_entry;
124 unsigned long offset;
125
126 /* Kernel or user address? */
Michael Ellerman51fae6de2005-12-04 18:39:15 +1100127 if (is_kernel_addr(ea)) {
Paul Mackerras1189be62007-10-11 20:37:10 +1000128 vsid = get_kernel_vsid(ea, MMU_SEGSIZE_256M);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 } else {
130 if ((ea >= TASK_SIZE_USER64) || (! mm))
131 return 1;
132
Paul Mackerras1189be62007-10-11 20:37:10 +1000133 vsid = get_vsid(mm->context.id, ea, MMU_SEGSIZE_256M);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135
136 stab_entry = make_ste(get_paca()->stab_addr, GET_ESID(ea), vsid);
137
Michael Ellerman51fae6de2005-12-04 18:39:15 +1100138 if (!is_kernel_addr(ea)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 offset = __get_cpu_var(stab_cache_ptr);
140 if (offset < NR_STAB_CACHE_ENTRIES)
141 __get_cpu_var(stab_cache[offset++]) = stab_entry;
142 else
143 offset = NR_STAB_CACHE_ENTRIES+1;
144 __get_cpu_var(stab_cache_ptr) = offset;
145
146 /* Order update */
147 asm volatile("sync":::"memory");
148 }
149
150 return 0;
151}
152
153int ste_allocate(unsigned long ea)
154{
155 return __ste_allocate(ea, current->mm);
156}
157
158/*
159 * Do the segment table work for a context switch: flush all user
160 * entries from the table, then preload some probably useful entries
161 * for the new task
162 */
163void switch_stab(struct task_struct *tsk, struct mm_struct *mm)
164{
165 struct stab_entry *stab = (struct stab_entry *) get_paca()->stab_addr;
166 struct stab_entry *ste;
167 unsigned long offset = __get_cpu_var(stab_cache_ptr);
168 unsigned long pc = KSTK_EIP(tsk);
169 unsigned long stack = KSTK_ESP(tsk);
170 unsigned long unmapped_base;
171
172 /* Force previous translations to complete. DRENG */
173 asm volatile("isync" : : : "memory");
174
175 if (offset <= NR_STAB_CACHE_ENTRIES) {
176 int i;
177
178 for (i = 0; i < offset; i++) {
179 ste = stab + __get_cpu_var(stab_cache[i]);
180 ste->esid_data = 0; /* invalidate entry */
181 }
182 } else {
183 unsigned long entry;
184
185 /* Invalidate all entries. */
186 ste = stab;
187
188 /* Never flush the first entry. */
189 ste += 1;
190 for (entry = 1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100191 entry < (HW_PAGE_SIZE / sizeof(struct stab_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 entry++, ste++) {
193 unsigned long ea;
194 ea = ste->esid_data & ESID_MASK;
Michael Ellerman51fae6de2005-12-04 18:39:15 +1100195 if (!is_kernel_addr(ea)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 ste->esid_data = 0;
197 }
198 }
199 }
200
201 asm volatile("sync; slbia; sync":::"memory");
202
203 __get_cpu_var(stab_cache_ptr) = 0;
204
205 /* Now preload some entries for the new task */
206 if (test_tsk_thread_flag(tsk, TIF_32BIT))
207 unmapped_base = TASK_UNMAPPED_BASE_USER32;
208 else
209 unmapped_base = TASK_UNMAPPED_BASE_USER64;
210
211 __ste_allocate(pc, mm);
212
213 if (GET_ESID(pc) == GET_ESID(stack))
214 return;
215
216 __ste_allocate(stack, mm);
217
218 if ((GET_ESID(pc) == GET_ESID(unmapped_base))
219 || (GET_ESID(stack) == GET_ESID(unmapped_base)))
220 return;
221
222 __ste_allocate(unmapped_base, mm);
223
224 /* Order update */
225 asm volatile("sync" : : : "memory");
226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/*
David Gibson533f0812005-07-27 11:44:19 -0700229 * 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.
232 */
Michael Ellerman0108d3f2007-05-07 15:58:28 +1000233void __init stabs_alloc(void)
David Gibson533f0812005-07-27 11:44:19 -0700234{
235 int cpu;
236
237 if (cpu_has_feature(CPU_FTR_SLB))
238 return;
239
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800240 for_each_possible_cpu(cpu) {
David Gibson533f0812005-07-27 11:44:19 -0700241 unsigned long newstab;
242
243 if (cpu == 0)
244 continue; /* stab for CPU 0 is statically allocated */
245
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100246 newstab = lmb_alloc_base(HW_PAGE_SIZE, HW_PAGE_SIZE,
247 1<<SID_SHIFT);
Michael Ellermanb5666f72005-12-05 10:24:33 -0600248 newstab = (unsigned long)__va(newstab);
David Gibson533f0812005-07-27 11:44:19 -0700249
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100250 memset((void *)newstab, 0, HW_PAGE_SIZE);
David Gibson533f0812005-07-27 11:44:19 -0700251
252 paca[cpu].stab_addr = newstab;
253 paca[cpu].stab_real = virt_to_abs(newstab);
Ingo Molnarfe333322009-01-06 14:26:03 +0000254 printk(KERN_INFO "Segment table for CPU %d at 0x%llx "
255 "virtual, 0x%llx absolute\n",
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100256 cpu, paca[cpu].stab_addr, paca[cpu].stab_real);
David Gibson533f0812005-07-27 11:44:19 -0700257 }
258}
259
260/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * Build an entry for the base kernel segment and put it into
262 * the segment table or SLB. All other segment table or SLB
263 * entries are faulted in.
264 */
265void stab_initialize(unsigned long stab)
266{
Paul Mackerras1189be62007-10-11 20:37:10 +1000267 unsigned long vsid = get_kernel_vsid(PAGE_OFFSET, MMU_SEGSIZE_256M);
Paul Mackerras799d6042005-11-10 13:37:51 +1100268 unsigned long stabreal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100270 asm volatile("isync; slbia; isync":::"memory");
Michael Ellermanb5666f72005-12-05 10:24:33 -0600271 make_ste(stab, GET_ESID(PAGE_OFFSET), vsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100273 /* Order update */
274 asm volatile("sync":::"memory");
Paul Mackerras799d6042005-11-10 13:37:51 +1100275
276 /* Set ASR */
277 stabreal = get_paca()->stab_real | 0x1ul;
278
279#ifdef CONFIG_PPC_ISERIES
280 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
281 HvCall1(HvCallBaseSetASR, stabreal);
282 return;
283 }
284#endif /* CONFIG_PPC_ISERIES */
Olof Johanssonca507eaf2005-11-29 14:04:17 -0600285
Paul Mackerras799d6042005-11-10 13:37:51 +1100286 mtspr(SPRN_ASR, stabreal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}