altivec.c revision 1.11.20.1 1 /* $NetBSD: altivec.c,v 1.11.20.1 2007/01/30 13:49:37 ad Exp $ */
2
3 /*
4 * Copyright (C) 1996 Wolfgang Solfrank.
5 * Copyright (C) 1996 TooLs GmbH.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: altivec.c,v 1.11.20.1 2007/01/30 13:49:37 ad Exp $");
36
37 #include "opt_multiprocessor.h"
38
39 #include <sys/param.h>
40 #include <sys/proc.h>
41 #include <sys/systm.h>
42 #include <sys/user.h>
43 #include <sys/malloc.h>
44 #include <sys/pool.h>
45
46 #include <uvm/uvm_extern.h>
47
48 #include <powerpc/altivec.h>
49 #include <powerpc/spr.h>
50 #include <powerpc/psl.h>
51
52 void
53 enable_vec(void)
54 {
55 struct cpu_info *ci = curcpu();
56 struct lwp *l = curlwp;
57 struct pcb *pcb = &l->l_addr->u_pcb;
58 struct trapframe *tf = trapframe(l);
59 struct vreg *vr = &pcb->pcb_vr;
60 register_t msr;
61
62 KASSERT(pcb->pcb_veccpu == NULL);
63
64 pcb->pcb_flags |= PCB_ALTIVEC;
65
66 /*
67 * Enable AltiVec temporarily (and disable interrupts).
68 */
69 msr = mfmsr();
70 mtmsr((msr & ~PSL_EE) | PSL_VEC);
71 __asm volatile ("isync");
72 if (ci->ci_veclwp) {
73 save_vec_cpu();
74 }
75 KASSERT(curcpu()->ci_veclwp == NULL);
76
77 /*
78 * Restore VSCR by first loading it into a vector and then into VSCR.
79 * (this needs to done before loading the user's vector registers
80 * since we need to use a scratch vector register)
81 */
82 __asm volatile("vxor %2,%2,%2; lvewx %2,%0,%1; mtvscr %2" \
83 :: "b"(vr), "r"(offsetof(struct vreg, vscr)), "n"(0));
84
85 /*
86 * VRSAVE will be restored when trap frame returns
87 */
88 tf->tf_xtra[TF_VRSAVE] = vr->vrsave;
89
90 #define LVX(n,vr) __asm /*volatile*/("lvx %2,%0,%1" \
91 :: "b"(vr), "r"(offsetof(struct vreg, vreg[n])), "n"(n));
92
93 /*
94 * Load all 32 vector registers
95 */
96 LVX( 0,vr); LVX( 1,vr); LVX( 2,vr); LVX( 3,vr);
97 LVX( 4,vr); LVX( 5,vr); LVX( 6,vr); LVX( 7,vr);
98 LVX( 8,vr); LVX( 9,vr); LVX(10,vr); LVX(11,vr);
99 LVX(12,vr); LVX(13,vr); LVX(14,vr); LVX(15,vr);
100
101 LVX(16,vr); LVX(17,vr); LVX(18,vr); LVX(19,vr);
102 LVX(20,vr); LVX(21,vr); LVX(22,vr); LVX(23,vr);
103 LVX(24,vr); LVX(25,vr); LVX(26,vr); LVX(27,vr);
104 LVX(28,vr); LVX(29,vr); LVX(30,vr); LVX(31,vr);
105 __asm volatile ("isync");
106
107 /*
108 * Enable AltiVec when we return to user-mode.
109 * Record the new ownership of the AltiVec unit.
110 */
111 curcpu()->ci_veclwp = l;
112 pcb->pcb_veccpu = curcpu();
113 pcb->pcb_flags |= PCB_OWNALTIVEC;
114 __asm volatile ("sync");
115
116 /*
117 * Restore MSR (turn off AltiVec)
118 */
119 mtmsr(msr);
120 }
121
122 void
123 save_vec_cpu(void)
124 {
125 struct cpu_info *ci = curcpu();
126 struct lwp *l;
127 struct pcb *pcb;
128 struct vreg *vr;
129 struct trapframe *tf;
130 register_t msr;
131
132 /*
133 * Turn on AltiVEC, turn off interrupts.
134 */
135 msr = mfmsr();
136 mtmsr((msr & ~PSL_EE) | PSL_VEC);
137 __asm volatile ("isync");
138 l = ci->ci_veclwp;
139 if (l == NULL)
140 goto out;
141 pcb = &l->l_addr->u_pcb;
142 vr = &pcb->pcb_vr;
143 tf = trapframe(l);
144
145 #define STVX(n,vr) __asm /*volatile*/("stvx %2,%0,%1" \
146 :: "b"(vr), "r"(offsetof(struct vreg, vreg[n])), "n"(n));
147
148 /*
149 * Save the vector registers.
150 */
151 STVX( 0,vr); STVX( 1,vr); STVX( 2,vr); STVX( 3,vr);
152 STVX( 4,vr); STVX( 5,vr); STVX( 6,vr); STVX( 7,vr);
153 STVX( 8,vr); STVX( 9,vr); STVX(10,vr); STVX(11,vr);
154 STVX(12,vr); STVX(13,vr); STVX(14,vr); STVX(15,vr);
155
156 STVX(16,vr); STVX(17,vr); STVX(18,vr); STVX(19,vr);
157 STVX(20,vr); STVX(21,vr); STVX(22,vr); STVX(23,vr);
158 STVX(24,vr); STVX(25,vr); STVX(26,vr); STVX(27,vr);
159 STVX(28,vr); STVX(29,vr); STVX(30,vr); STVX(31,vr);
160
161 /*
162 * Save VSCR (this needs to be done after save the vector registers
163 * since we need to use one as scratch).
164 */
165 __asm volatile("mfvscr %2; stvewx %2,%0,%1" \
166 :: "b"(vr), "r"(offsetof(struct vreg, vscr)), "n"(0));
167
168 /*
169 * Save VRSAVE
170 */
171 vr->vrsave = tf->tf_xtra[TF_VRSAVE];
172
173 /*
174 * Note that we aren't using any CPU resources and stop any
175 * data streams.
176 */
177 pcb->pcb_veccpu = NULL;
178 ci->ci_veclwp = NULL;
179 __asm volatile ("dssall; sync");
180
181 out:
182
183 /*
184 * Restore MSR (turn off AltiVec)
185 */
186 mtmsr(msr);
187 }
188
189 /*
190 * Save a process's AltiVEC state to its PCB. The state may be in any CPU.
191 * The process must either be curproc or traced by curproc (and stopped).
192 * (The point being that the process must not run on another CPU during
193 * this function).
194 */
195 void
196 save_vec_lwp(struct lwp *l, int discard)
197 {
198 struct pcb * const pcb = &l->l_addr->u_pcb;
199 struct cpu_info * const ci = curcpu();
200
201 /*
202 * If it's already in the PCB, there's nothing to do.
203 */
204 if (pcb->pcb_veccpu == NULL)
205 return;
206
207 /*
208 * If we simply need to discard the information, then don't
209 * to save anything.
210 */
211 if (discard) {
212 #ifndef MULTIPROCESSOR
213 KASSERT(ci == pcb->pcb_veccpu);
214 #endif
215 KASSERT(l == pcb->pcb_veccpu->ci_veclwp);
216 pcb->pcb_veccpu->ci_veclwp = NULL;
217 pcb->pcb_veccpu = NULL;
218 pcb->pcb_flags &= ~PCB_OWNALTIVEC;
219 return;
220 }
221
222 /*
223 * If the state is in the current CPU, just flush the current CPU's
224 * state.
225 */
226 if (l == ci->ci_veclwp) {
227 save_vec_cpu();
228 return;
229 }
230
231
232 #ifdef MULTIPROCESSOR
233 /*
234 * It must be on another CPU, flush it from there.
235 */
236
237 mp_save_vec_lwp(l);
238 #endif
239 }
240
241 #define ZERO_VEC 19
242
243 void
244 vzeropage(paddr_t pa)
245 {
246 const paddr_t ea = pa + PAGE_SIZE;
247 uint32_t vec[7], *vp = (void *) roundup((uintptr_t) vec, 16);
248 register_t omsr, msr;
249
250 __asm volatile("mfmsr %0" : "=r"(omsr) :);
251
252 /*
253 * Turn on AltiVec, turn off interrupts.
254 */
255 msr = (omsr & ~PSL_EE) | PSL_VEC;
256 __asm volatile("sync; mtmsr %0; isync" :: "r"(msr));
257
258 /*
259 * Save the VEC register we are going to use before we disable
260 * relocation.
261 */
262 __asm("stvx %1,0,%0" :: "r"(vp), "n"(ZERO_VEC));
263 __asm("vxor %0,%0,%0" :: "n"(ZERO_VEC));
264
265 /*
266 * Zero the page using a single cache line.
267 */
268 __asm volatile(
269 " sync ;"
270 " mfmsr %[msr];"
271 " rlwinm %[msr],%[msr],0,28,26;" /* Clear PSL_DR */
272 " mtmsr %[msr];" /* Turn off DMMU */
273 " isync;"
274 "1: stvx %[zv], %[pa], %[off0];"
275 " stvxl %[zv], %[pa], %[off16];"
276 " stvx %[zv], %[pa], %[off32];"
277 " stvxl %[zv], %[pa], %[off48];"
278 " addi %[pa], %[pa], 64;"
279 " cmplw %[pa], %[ea];"
280 " blt+ 1b;"
281 " ori %[msr], %[msr], 0x10;" /* Set PSL_DR */
282 " sync;"
283 " mtmsr %[msr];" /* Turn on DMMU */
284 " isync;"
285 :: [msr] "r"(msr), [pa] "b"(pa), [ea] "b"(ea),
286 [off0] "r"(0), [off16] "r"(16), [off32] "r"(32), [off48] "r"(48),
287 [zv] "n"(ZERO_VEC));
288
289 /*
290 * Restore VEC register (now that we can access the stack again).
291 */
292 __asm("lvx %1,0,%0" :: "r"(vp), "n"(ZERO_VEC));
293
294 /*
295 * Restore old MSR (AltiVec OFF).
296 */
297 __asm volatile("sync; mtmsr %0; isync" :: "r"(omsr));
298 }
299
300 #define LO_VEC 16
301 #define HI_VEC 17
302
303 void
304 vcopypage(paddr_t dst, paddr_t src)
305 {
306 const paddr_t edst = dst + PAGE_SIZE;
307 uint32_t vec[11], *vp = (void *) roundup((uintptr_t) vec, 16);
308 register_t omsr, msr;
309
310 __asm volatile("mfmsr %0" : "=r"(omsr) :);
311
312 /*
313 * Turn on AltiVec, turn off interrupts.
314 */
315 msr = (omsr & ~PSL_EE) | PSL_VEC;
316 __asm volatile("sync; mtmsr %0; isync" :: "r"(msr));
317
318 /*
319 * Save the VEC registers we will be using before we disable
320 * relocation.
321 */
322 __asm("stvx %2,%1,%0" :: "b"(vp), "r"( 0), "n"(LO_VEC));
323 __asm("stvx %2,%1,%0" :: "b"(vp), "r"(16), "n"(HI_VEC));
324
325 /*
326 * Copy the page using a single cache line, with DMMU
327 * disabled. On most PPCs, two vector registers occupy one
328 * cache line.
329 */
330 __asm volatile(
331 " sync ;"
332 " mfmsr %[msr];"
333 " rlwinm %[msr],%[msr],0,28,26;" /* Clear PSL_DR */
334 " mtmsr %[msr];" /* Turn off DMMU */
335 " isync;"
336 "1: lvx %[lv], %[src], %[off0];"
337 " stvx %[lv], %[dst], %[off0];"
338 " lvxl %[hv], %[src], %[off16];"
339 " stvxl %[hv], %[dst], %[off16];"
340 " addi %[src], %[src], 32;"
341 " addi %[dst], %[dst], 32;"
342 " cmplw %[dst], %[edst];"
343 " blt+ 1b;"
344 " ori %[msr], %[msr], 0x10;" /* Set PSL_DR */
345 " sync;"
346 " mtmsr %[msr];" /* Turn on DMMU */
347 " isync;"
348 :: [msr] "r"(msr), [src] "b"(src), [dst] "b"(dst),
349 [edst] "b"(edst), [off0] "r"(0), [off16] "r"(16),
350 [lv] "n"(LO_VEC), [hv] "n"(HI_VEC));
351
352 /*
353 * Restore VEC registers (now that we can access the stack again).
354 */
355 __asm("lvx %2,%1,%0" :: "b"(vp), "r"( 0), "n"(LO_VEC));
356 __asm("lvx %2,%1,%0" :: "b"(vp), "r"(16), "n"(HI_VEC));
357
358 /*
359 * Restore old MSR (AltiVec OFF).
360 */
361 __asm volatile("sync; mtmsr %0; isync" :: "r"(omsr));
362 }
363