blob: 4a9291d9fef8a2b9511d581b2bd17deb3ab8e3c1 [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
15#include <linux/config.h>
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>
David Gibson533f0812005-07-27 11:44:19 -070021#include <asm/lmb.h>
22#include <asm/abs_addr.h>
Paul Mackerras799d6042005-11-10 13:37:51 +110023#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
David Gibson1f8d4192005-05-05 16:15:13 -070025struct stab_entry {
26 unsigned long esid_data;
27 unsigned long vsid_data;
28};
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define NR_STAB_CACHE_ENTRIES 8
31DEFINE_PER_CPU(long, stab_cache_ptr);
32DEFINE_PER_CPU(long, stab_cache[NR_STAB_CACHE_ENTRIES]);
33
34/*
35 * Create a segment table entry for the given esid/vsid pair.
36 */
37static int make_ste(unsigned long stab, unsigned long esid, unsigned long vsid)
38{
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;
Michael Ellermanb5666f72005-12-05 10:24:33 -060043 unsigned long kernel_segment = (esid << SID_SHIFT) >= PAGE_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 vsid_data = vsid << STE_VSID_SHIFT;
46 esid_data = esid << SID_SHIFT | STE_ESID_KP | STE_ESID_V;
47 if (! kernel_segment)
48 esid_data |= STE_ESID_KS;
49
50 /* Search the primary group first. */
51 global_entry = (esid & 0x1f) << 3;
52 ste = (struct stab_entry *)(stab | ((esid & 0x1f) << 7));
53
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);
62 }
63 }
64 /* Now search the secondary group. */
65 global_entry = ((~esid) & 0x1f) << 3;
66 ste = (struct stab_entry *)(stab | (((~esid) & 0x1f) << 7));
67 }
68
69 /*
70 * Could not find empty entry, pick one with a round robin selection.
71 * Search all entries in the two groups.
72 */
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;
79 } else {
80 global_entry = ((~esid) & 0x1f) << 3;
81 ste = (struct stab_entry *)(stab | (((~esid) & 0x1f) << 7));
82 castout_ste = ste + (castout_entry - 8);
83 }
84
85 /* Dont cast out the first kernel segment */
Michael Ellermanb5666f72005-12-05 10:24:33 -060086 if ((castout_ste->esid_data & ESID_MASK) != PAGE_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 break;
88
89 castout_entry = (castout_entry + 1) & 0xf;
90 }
91
92 get_paca()->stab_rr = (castout_entry + 1) & 0xf;
93
94 /* Modify the old entry to the new value. */
95
96 /* Force previous translations to complete. DRENG */
97 asm volatile("isync" : : : "memory");
98
99 old_esid = castout_ste->esid_data >> SID_SHIFT;
100 castout_ste->esid_data = 0; /* Invalidate old entry */
101
102 asm volatile("sync" : : : "memory"); /* Order update */
103
104 castout_ste->vsid_data = vsid_data;
105 asm volatile("eieio" : : : "memory"); /* Order update */
106 castout_ste->esid_data = esid_data;
107
108 asm volatile("slbie %0" : : "r" (old_esid << SID_SHIFT));
109 /* Ensure completion of slbie */
110 asm volatile("sync" : : : "memory");
111
112 return (global_entry | (castout_entry & 0x7));
113}
114
115/*
116 * Allocate a segment table entry for the given ea and mm
117 */
118static int __ste_allocate(unsigned long ea, struct mm_struct *mm)
119{
120 unsigned long vsid;
121 unsigned char stab_entry;
122 unsigned long offset;
123
124 /* Kernel or user address? */
Michael Ellerman51fae6de2005-12-04 18:39:15 +1100125 if (is_kernel_addr(ea)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 vsid = get_kernel_vsid(ea);
127 } else {
128 if ((ea >= TASK_SIZE_USER64) || (! mm))
129 return 1;
130
131 vsid = get_vsid(mm->context.id, ea);
132 }
133
134 stab_entry = make_ste(get_paca()->stab_addr, GET_ESID(ea), vsid);
135
Michael Ellerman51fae6de2005-12-04 18:39:15 +1100136 if (!is_kernel_addr(ea)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 offset = __get_cpu_var(stab_cache_ptr);
138 if (offset < NR_STAB_CACHE_ENTRIES)
139 __get_cpu_var(stab_cache[offset++]) = stab_entry;
140 else
141 offset = NR_STAB_CACHE_ENTRIES+1;
142 __get_cpu_var(stab_cache_ptr) = offset;
143
144 /* Order update */
145 asm volatile("sync":::"memory");
146 }
147
148 return 0;
149}
150
151int ste_allocate(unsigned long ea)
152{
153 return __ste_allocate(ea, current->mm);
154}
155
156/*
157 * Do the segment table work for a context switch: flush all user
158 * entries from the table, then preload some probably useful entries
159 * for the new task
160 */
161void switch_stab(struct task_struct *tsk, struct mm_struct *mm)
162{
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;
169
170 /* Force previous translations to complete. DRENG */
171 asm volatile("isync" : : : "memory");
172
173 if (offset <= NR_STAB_CACHE_ENTRIES) {
174 int i;
175
176 for (i = 0; i < offset; i++) {
177 ste = stab + __get_cpu_var(stab_cache[i]);
178 ste->esid_data = 0; /* invalidate entry */
179 }
180 } else {
181 unsigned long entry;
182
183 /* Invalidate all entries. */
184 ste = stab;
185
186 /* Never flush the first entry. */
187 ste += 1;
188 for (entry = 1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100189 entry < (HW_PAGE_SIZE / sizeof(struct stab_entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 entry++, ste++) {
191 unsigned long ea;
192 ea = ste->esid_data & ESID_MASK;
Michael Ellerman51fae6de2005-12-04 18:39:15 +1100193 if (!is_kernel_addr(ea)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 ste->esid_data = 0;
195 }
196 }
197 }
198
199 asm volatile("sync; slbia; sync":::"memory");
200
201 __get_cpu_var(stab_cache_ptr) = 0;
202
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100203#ifdef CONFIG_PPC_64K_PAGES
204 get_paca()->pgdir = mm->pgd;
205#endif /* CONFIG_PPC_64K_PAGES */
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* Now preload some entries for the new task */
208 if (test_tsk_thread_flag(tsk, TIF_32BIT))
209 unmapped_base = TASK_UNMAPPED_BASE_USER32;
210 else
211 unmapped_base = TASK_UNMAPPED_BASE_USER64;
212
213 __ste_allocate(pc, mm);
214
215 if (GET_ESID(pc) == GET_ESID(stack))
216 return;
217
218 __ste_allocate(stack, mm);
219
220 if ((GET_ESID(pc) == GET_ESID(unmapped_base))
221 || (GET_ESID(stack) == GET_ESID(unmapped_base)))
222 return;
223
224 __ste_allocate(unmapped_base, mm);
225
226 /* Order update */
227 asm volatile("sync" : : : "memory");
228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/*
David Gibson533f0812005-07-27 11:44:19 -0700231 * Allocate segment tables for secondary CPUs. These must all go in
232 * the first (bolted) segment, so that do_stab_bolted won't get a
233 * recursive segment miss on the segment table itself.
234 */
235void stabs_alloc(void)
236{
237 int cpu;
238
239 if (cpu_has_feature(CPU_FTR_SLB))
240 return;
241
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -0800242 for_each_possible_cpu(cpu) {
David Gibson533f0812005-07-27 11:44:19 -0700243 unsigned long newstab;
244
245 if (cpu == 0)
246 continue; /* stab for CPU 0 is statically allocated */
247
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100248 newstab = lmb_alloc_base(HW_PAGE_SIZE, HW_PAGE_SIZE,
249 1<<SID_SHIFT);
Michael Ellermanb5666f72005-12-05 10:24:33 -0600250 newstab = (unsigned long)__va(newstab);
David Gibson533f0812005-07-27 11:44:19 -0700251
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100252 memset((void *)newstab, 0, HW_PAGE_SIZE);
David Gibson533f0812005-07-27 11:44:19 -0700253
254 paca[cpu].stab_addr = newstab;
255 paca[cpu].stab_real = virt_to_abs(newstab);
Paul Mackerras799d6042005-11-10 13:37:51 +1100256 printk(KERN_INFO "Segment table for CPU %d at 0x%lx "
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100257 "virtual, 0x%lx absolute\n",
258 cpu, paca[cpu].stab_addr, paca[cpu].stab_real);
David Gibson533f0812005-07-27 11:44:19 -0700259 }
260}
261
262/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 * Build an entry for the base kernel segment and put it into
264 * the segment table or SLB. All other segment table or SLB
265 * entries are faulted in.
266 */
267void stab_initialize(unsigned long stab)
268{
Michael Ellermanb5666f72005-12-05 10:24:33 -0600269 unsigned long vsid = get_kernel_vsid(PAGE_OFFSET);
Paul Mackerras799d6042005-11-10 13:37:51 +1100270 unsigned long stabreal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100272 asm volatile("isync; slbia; isync":::"memory");
Michael Ellermanb5666f72005-12-05 10:24:33 -0600273 make_ste(stab, GET_ESID(PAGE_OFFSET), vsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100275 /* Order update */
276 asm volatile("sync":::"memory");
Paul Mackerras799d6042005-11-10 13:37:51 +1100277
278 /* Set ASR */
279 stabreal = get_paca()->stab_real | 0x1ul;
280
281#ifdef CONFIG_PPC_ISERIES
282 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
283 HvCall1(HvCallBaseSetASR, stabreal);
284 return;
285 }
286#endif /* CONFIG_PPC_ISERIES */
Olof Johanssonca507eaf2005-11-29 14:04:17 -0600287
Paul Mackerras799d6042005-11-10 13:37:51 +1100288 mtspr(SPRN_ASR, stabreal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}