altivec.c revision 1.18 1 /* $NetBSD: altivec.c,v 1.18 2011/01/18 01:02:55 matt 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.18 2011/01/18 01:02:55 matt 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/atomic.h>
43
44 #include <uvm/uvm_extern.h> /* for vcopypage/vzeropage */
45
46 #include <powerpc/pcb.h>
47 #include <powerpc/altivec.h>
48 #include <powerpc/spr.h>
49 #include <powerpc/oea/spr.h>
50 #include <powerpc/psl.h>
51
52 #ifdef MULTIPROCESSOR
53 #include <arch/powerpc/pic/picvar.h>
54 #include <arch/powerpc/pic/ipivar.h>
55 static void vec_mp_save_lwp(struct lwp *);
56 #endif
57
58 void
59 vec_enable(void)
60 {
61 struct cpu_info *ci = curcpu();
62 struct lwp *l = curlwp;
63 register_t msr;
64
65 KASSERT(l->l_md.md_veccpu != NULL);
66
67 l->l_md.md_flags |= MDLWP_USEDVEC;
68
69 /*
70 * Enable AltiVec temporarily (and disable interrupts).
71 */
72 msr = mfmsr();
73 mtmsr((msr & ~PSL_EE) | PSL_VEC);
74 __asm volatile ("isync");
75
76 if (ci->ci_veclwp != l) {
77 struct pcb * const pcb = lwp_getpcb(l);
78 struct trapframe * const tf = l->l_md.md_utf;
79
80 vec_save_cpu(VEC_SAVE_AND_RELEASE);
81
82 /*
83 * Load the vector unit from vreg which is best done in
84 * assembly.
85 */
86 vec_load_from_vreg(&pcb->pcb_vr);
87
88 /*
89 * VRSAVE will be restored when trap frame returns
90 */
91 tf->tf_vrsave = pcb->pcb_vr.vrsave;
92
93 /*
94 * Enable AltiVec when we return to user-mode.
95 * Record the new ownership of the AltiVec unit.
96 */
97 ci->ci_veclwp = l;
98 l->l_md.md_veccpu = ci;
99 __asm volatile ("sync");
100 }
101 l->l_md.md_flags |= MDLWP_OWNVEC;
102
103 /*
104 * Restore MSR (turn off AltiVec)
105 */
106 mtmsr(msr);
107 }
108
109 void
110 vec_save_cpu(enum vec_op op)
111 {
112 /*
113 * Turn on AltiVEC, turn off interrupts.
114 */
115 const register_t msr = mfmsr();
116 mtmsr((msr & ~PSL_EE) | PSL_VEC);
117 __asm volatile ("isync");
118
119 struct cpu_info * const ci = curcpu();
120 lwp_t * const l = ci->ci_veclwp;
121
122 if (l->l_md.md_flags & MDLWP_OWNVEC) {
123 struct pcb * const pcb = lwp_getpcb(l);
124 struct trapframe * const tf = l->l_md.md_utf;
125
126 /*
127 * Grab contents of vector unit.
128 */
129 vec_unload_to_vreg(&pcb->pcb_vr);
130
131 /*
132 * Save VRSAVE
133 */
134 pcb->pcb_vr.vrsave = tf->tf_vrsave;
135
136 /*
137 * Note that we aren't using any CPU resources and stop any
138 * data streams.
139 */
140 __asm volatile ("dssall; sync");
141
142 /*
143 * Give up the VEC unit if are releasing it too.
144 */
145 if (op == VEC_SAVE_AND_RELEASE)
146 ci->ci_veclwp = ci->ci_data.cpu_idlelwp;
147 }
148 l->l_md.md_flags &= ~MDLWP_OWNVEC;
149
150 /*
151 * Restore MSR (turn off AltiVec)
152 */
153 mtmsr(msr);
154 }
155
156 #ifdef MULTIPROCESSOR
157 /*
158 * Save a process's AltiVEC state to its PCB. The state may be in any CPU.
159 * The process must either be curproc or traced by curproc (and stopped).
160 * (The point being that the process must not run on another CPU during
161 * this function).
162 */
163 static void
164 vec_mp_save_lwp(struct lwp *l)
165 {
166 /*
167 * Send an IPI to the other CPU with the data and wait for that CPU
168 * to flush the data. Note that the other CPU might have switched
169 * to a different proc's AltiVEC state by the time it receives the IPI,
170 * but that will only result in an unnecessary reload.
171 */
172
173 if ((l->l_md.md_flags & MDLWP_OWNVEC) == 0)
174 return;
175
176 ppc_send_ipi(l->l_md.md_veccpu->ci_index, PPC_IPI_FLUSH_VEC);
177
178 /* Wait for flush. */
179 for (u_int i = 0; i < 0x3fffffff; i++) {
180 if ((l->l_md.md_flags & MDLWP_OWNVEC) == 0)
181 return;
182 }
183
184 panic("%s/%d timed out: pid = %d.%d, veccpu->ci_cpuid = %d\n",
185 __func__, cpu_number(), l->l_proc->p_pid, l->l_lid,
186 veccpu->ci_cpuid);
187 }
188 #endif /*MULTIPROCESSOR*/
189
190 /*
191 * Save a process's AltiVEC state to its PCB. The state may be in any CPU.
192 * The process must either be curproc or traced by curproc (and stopped).
193 * (The point being that the process must not run on another CPU during
194 * this function).
195 */
196 void
197 vec_save_lwp(struct lwp *l, enum vec_op op)
198 {
199 struct cpu_info * const ci = curcpu();
200
201 KASSERT(l->l_md.md_veccpu != NULL);
202
203 /*
204 * If it's already in the PCB, there's nothing to do.
205 */
206 if ((l->l_md.md_flags & MDLWP_OWNVEC) == 0)
207 return;
208
209 /*
210 * If we simply need to discard the information, then don't
211 * to save anything.
212 */
213 if (op == VEC_DISCARD) {
214 #ifndef MULTIPROCESSOR
215 KASSERT(ci == l->l_md.md_veccpu);
216 #endif
217 KASSERT(l == l->l_md.md_veccpu->ci_veclwp);
218 KASSERT(l == curlwp || ci == l->l_md.md_veccpu);
219 ci->ci_veclwp = ci->ci_data.cpu_idlelwp;
220 atomic_and_uint(&l->l_md.md_flags, ~MDLWP_OWNVEC);
221 return;
222 }
223
224 /*
225 * If the state is in the current CPU, just flush the current CPU's
226 * state.
227 */
228 if (l == ci->ci_veclwp) {
229 vec_save_cpu(op);
230 return;
231 }
232
233
234 #ifdef MULTIPROCESSOR
235 /*
236 * It must be on another CPU, flush it from there.
237 */
238 vec_mp_save_lwp(l);
239 #endif
240 }
241
242 void
243 vec_restore_from_mcontext(struct lwp *l, const mcontext_t *mcp)
244 {
245 struct pcb * const pcb = lwp_getpcb(l);
246
247 /* we don't need to save the state, just drop it */
248 vec_save_lwp(l, VEC_DISCARD);
249 memcpy(pcb->pcb_vr.vreg, &mcp->__vrf.__vrs, sizeof (pcb->pcb_vr.vreg));
250 pcb->pcb_vr.vscr = mcp->__vrf.__vscr;
251 pcb->pcb_vr.vrsave = mcp->__vrf.__vrsave;
252 l->l_md.md_utf->tf_vrsave = pcb->pcb_vr.vrsave;
253 }
254
255 bool
256 vec_save_to_mcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flagp)
257 {
258 /* Save AltiVec context, if any. */
259 if ((l->l_md.md_flags & MDLWP_USEDVEC) == 0)
260 return false;
261
262 struct pcb * const pcb = lwp_getpcb(l);
263
264 /*
265 * If we're the AltiVec owner, dump its context to the PCB first.
266 */
267 vec_save_lwp(l, VEC_SAVE);
268
269 mcp->__gregs[_REG_MSR] |= PSL_VEC;
270 mcp->__vrf.__vscr = pcb->pcb_vr.vscr;
271 mcp->__vrf.__vrsave = l->l_md.md_utf->tf_vrsave;
272 memcpy(mcp->__vrf.__vrs, pcb->pcb_vr.vreg, sizeof (mcp->__vrf.__vrs));
273 *flagp |= _UC_POWERPC_VEC;
274 return true;
275 }
276
277 #define ZERO_VEC 19
278
279 void
280 vzeropage(paddr_t pa)
281 {
282 const paddr_t ea = pa + PAGE_SIZE;
283 uint32_t vec[7], *vp = (void *) roundup((uintptr_t) vec, 16);
284 register_t omsr, msr;
285
286 __asm volatile("mfmsr %0" : "=r"(omsr) :);
287
288 /*
289 * Turn on AltiVec, turn off interrupts.
290 */
291 msr = (omsr & ~PSL_EE) | PSL_VEC;
292 __asm volatile("sync; mtmsr %0; isync" :: "r"(msr));
293
294 /*
295 * Save the VEC register we are going to use before we disable
296 * relocation.
297 */
298 __asm("stvx %1,0,%0" :: "r"(vp), "n"(ZERO_VEC));
299 __asm("vxor %0,%0,%0" :: "n"(ZERO_VEC));
300
301 /*
302 * Zero the page using a single cache line.
303 */
304 __asm volatile(
305 " sync ;"
306 " mfmsr %[msr];"
307 " rlwinm %[msr],%[msr],0,28,26;" /* Clear PSL_DR */
308 " mtmsr %[msr];" /* Turn off DMMU */
309 " isync;"
310 "1: stvx %[zv], %[pa], %[off0];"
311 " stvxl %[zv], %[pa], %[off16];"
312 " stvx %[zv], %[pa], %[off32];"
313 " stvxl %[zv], %[pa], %[off48];"
314 " addi %[pa], %[pa], 64;"
315 " cmplw %[pa], %[ea];"
316 " blt+ 1b;"
317 " ori %[msr], %[msr], 0x10;" /* Set PSL_DR */
318 " sync;"
319 " mtmsr %[msr];" /* Turn on DMMU */
320 " isync;"
321 :: [msr] "r"(msr), [pa] "b"(pa), [ea] "b"(ea),
322 [off0] "r"(0), [off16] "r"(16), [off32] "r"(32), [off48] "r"(48),
323 [zv] "n"(ZERO_VEC));
324
325 /*
326 * Restore VEC register (now that we can access the stack again).
327 */
328 __asm("lvx %1,0,%0" :: "r"(vp), "n"(ZERO_VEC));
329
330 /*
331 * Restore old MSR (AltiVec OFF).
332 */
333 __asm volatile("sync; mtmsr %0; isync" :: "r"(omsr));
334 }
335
336 #define LO_VEC 16
337 #define HI_VEC 17
338
339 void
340 vcopypage(paddr_t dst, paddr_t src)
341 {
342 const paddr_t edst = dst + PAGE_SIZE;
343 uint32_t vec[11], *vp = (void *) roundup((uintptr_t) vec, 16);
344 register_t omsr, msr;
345
346 __asm volatile("mfmsr %0" : "=r"(omsr) :);
347
348 /*
349 * Turn on AltiVec, turn off interrupts.
350 */
351 msr = (omsr & ~PSL_EE) | PSL_VEC;
352 __asm volatile("sync; mtmsr %0; isync" :: "r"(msr));
353
354 /*
355 * Save the VEC registers we will be using before we disable
356 * relocation.
357 */
358 __asm("stvx %2,%1,%0" :: "b"(vp), "r"( 0), "n"(LO_VEC));
359 __asm("stvx %2,%1,%0" :: "b"(vp), "r"(16), "n"(HI_VEC));
360
361 /*
362 * Copy the page using a single cache line, with DMMU
363 * disabled. On most PPCs, two vector registers occupy one
364 * cache line.
365 */
366 __asm volatile(
367 " sync ;"
368 " mfmsr %[msr];"
369 " rlwinm %[msr],%[msr],0,28,26;" /* Clear PSL_DR */
370 " mtmsr %[msr];" /* Turn off DMMU */
371 " isync;"
372 "1: lvx %[lv], %[src], %[off0];"
373 " stvx %[lv], %[dst], %[off0];"
374 " lvxl %[hv], %[src], %[off16];"
375 " stvxl %[hv], %[dst], %[off16];"
376 " addi %[src], %[src], 32;"
377 " addi %[dst], %[dst], 32;"
378 " cmplw %[dst], %[edst];"
379 " blt+ 1b;"
380 " ori %[msr], %[msr], 0x10;" /* Set PSL_DR */
381 " sync;"
382 " mtmsr %[msr];" /* Turn on DMMU */
383 " isync;"
384 :: [msr] "r"(msr), [src] "b"(src), [dst] "b"(dst),
385 [edst] "b"(edst), [off0] "r"(0), [off16] "r"(16),
386 [lv] "n"(LO_VEC), [hv] "n"(HI_VEC));
387
388 /*
389 * Restore VEC registers (now that we can access the stack again).
390 */
391 __asm("lvx %2,%1,%0" :: "b"(vp), "r"( 0), "n"(LO_VEC));
392 __asm("lvx %2,%1,%0" :: "b"(vp), "r"(16), "n"(HI_VEC));
393
394 /*
395 * Restore old MSR (AltiVec OFF).
396 */
397 __asm volatile("sync; mtmsr %0; isync" :: "r"(omsr));
398 }
399