altivec.c revision 1.18 1 1.18 matt /* $NetBSD: altivec.c,v 1.18 2011/01/18 01:02:55 matt Exp $ */
2 1.1 matt
3 1.1 matt /*
4 1.1 matt * Copyright (C) 1996 Wolfgang Solfrank.
5 1.1 matt * Copyright (C) 1996 TooLs GmbH.
6 1.1 matt * All rights reserved.
7 1.1 matt *
8 1.1 matt * Redistribution and use in source and binary forms, with or without
9 1.1 matt * modification, are permitted provided that the following conditions
10 1.1 matt * are met:
11 1.1 matt * 1. Redistributions of source code must retain the above copyright
12 1.1 matt * notice, this list of conditions and the following disclaimer.
13 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer in the
15 1.1 matt * documentation and/or other materials provided with the distribution.
16 1.1 matt * 3. All advertising materials mentioning features or use of this software
17 1.1 matt * must display the following acknowledgement:
18 1.1 matt * This product includes software developed by TooLs GmbH.
19 1.1 matt * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 1.1 matt * derived from this software without specific prior written permission.
21 1.1 matt *
22 1.1 matt * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 1.1 matt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 matt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 matt * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 1.1 matt * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 1.1 matt * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 1.1 matt * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.1 matt * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 1.1 matt * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 1.1 matt * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 matt */
33 1.5 lukem
34 1.5 lukem #include <sys/cdefs.h>
35 1.18 matt __KERNEL_RCSID(0, "$NetBSD: altivec.c,v 1.18 2011/01/18 01:02:55 matt Exp $");
36 1.4 martin
37 1.4 martin #include "opt_multiprocessor.h"
38 1.4 martin
39 1.1 matt #include <sys/param.h>
40 1.1 matt #include <sys/proc.h>
41 1.1 matt #include <sys/systm.h>
42 1.18 matt #include <sys/atomic.h>
43 1.1 matt
44 1.18 matt #include <uvm/uvm_extern.h> /* for vcopypage/vzeropage */
45 1.3 thorpej
46 1.17 rmind #include <powerpc/pcb.h>
47 1.1 matt #include <powerpc/altivec.h>
48 1.1 matt #include <powerpc/spr.h>
49 1.16 matt #include <powerpc/oea/spr.h>
50 1.18 matt #include <powerpc/psl.h>
51 1.1 matt
52 1.13 garbled #ifdef MULTIPROCESSOR
53 1.13 garbled #include <arch/powerpc/pic/picvar.h>
54 1.13 garbled #include <arch/powerpc/pic/ipivar.h>
55 1.18 matt static void vec_mp_save_lwp(struct lwp *);
56 1.13 garbled #endif
57 1.13 garbled
58 1.1 matt void
59 1.18 matt vec_enable(void)
60 1.1 matt {
61 1.1 matt struct cpu_info *ci = curcpu();
62 1.1 matt struct lwp *l = curlwp;
63 1.2 matt register_t msr;
64 1.1 matt
65 1.18 matt KASSERT(l->l_md.md_veccpu != NULL);
66 1.1 matt
67 1.18 matt l->l_md.md_flags |= MDLWP_USEDVEC;
68 1.1 matt
69 1.1 matt /*
70 1.1 matt * Enable AltiVec temporarily (and disable interrupts).
71 1.1 matt */
72 1.1 matt msr = mfmsr();
73 1.1 matt mtmsr((msr & ~PSL_EE) | PSL_VEC);
74 1.11 perry __asm volatile ("isync");
75 1.18 matt
76 1.18 matt if (ci->ci_veclwp != l) {
77 1.18 matt struct pcb * const pcb = lwp_getpcb(l);
78 1.18 matt struct trapframe * const tf = l->l_md.md_utf;
79 1.18 matt
80 1.18 matt vec_save_cpu(VEC_SAVE_AND_RELEASE);
81 1.18 matt
82 1.18 matt /*
83 1.18 matt * Load the vector unit from vreg which is best done in
84 1.18 matt * assembly.
85 1.18 matt */
86 1.18 matt vec_load_from_vreg(&pcb->pcb_vr);
87 1.18 matt
88 1.18 matt /*
89 1.18 matt * VRSAVE will be restored when trap frame returns
90 1.18 matt */
91 1.18 matt tf->tf_vrsave = pcb->pcb_vr.vrsave;
92 1.18 matt
93 1.18 matt /*
94 1.18 matt * Enable AltiVec when we return to user-mode.
95 1.18 matt * Record the new ownership of the AltiVec unit.
96 1.18 matt */
97 1.18 matt ci->ci_veclwp = l;
98 1.18 matt l->l_md.md_veccpu = ci;
99 1.18 matt __asm volatile ("sync");
100 1.1 matt }
101 1.18 matt l->l_md.md_flags |= MDLWP_OWNVEC;
102 1.1 matt
103 1.1 matt /*
104 1.1 matt * Restore MSR (turn off AltiVec)
105 1.1 matt */
106 1.1 matt mtmsr(msr);
107 1.1 matt }
108 1.1 matt
109 1.1 matt void
110 1.18 matt vec_save_cpu(enum vec_op op)
111 1.1 matt {
112 1.1 matt /*
113 1.1 matt * Turn on AltiVEC, turn off interrupts.
114 1.1 matt */
115 1.18 matt const register_t msr = mfmsr();
116 1.1 matt mtmsr((msr & ~PSL_EE) | PSL_VEC);
117 1.11 perry __asm volatile ("isync");
118 1.1 matt
119 1.18 matt struct cpu_info * const ci = curcpu();
120 1.18 matt lwp_t * const l = ci->ci_veclwp;
121 1.1 matt
122 1.18 matt if (l->l_md.md_flags & MDLWP_OWNVEC) {
123 1.18 matt struct pcb * const pcb = lwp_getpcb(l);
124 1.18 matt struct trapframe * const tf = l->l_md.md_utf;
125 1.18 matt
126 1.18 matt /*
127 1.18 matt * Grab contents of vector unit.
128 1.18 matt */
129 1.18 matt vec_unload_to_vreg(&pcb->pcb_vr);
130 1.18 matt
131 1.18 matt /*
132 1.18 matt * Save VRSAVE
133 1.18 matt */
134 1.18 matt pcb->pcb_vr.vrsave = tf->tf_vrsave;
135 1.18 matt
136 1.18 matt /*
137 1.18 matt * Note that we aren't using any CPU resources and stop any
138 1.18 matt * data streams.
139 1.18 matt */
140 1.18 matt __asm volatile ("dssall; sync");
141 1.18 matt
142 1.18 matt /*
143 1.18 matt * Give up the VEC unit if are releasing it too.
144 1.18 matt */
145 1.18 matt if (op == VEC_SAVE_AND_RELEASE)
146 1.18 matt ci->ci_veclwp = ci->ci_data.cpu_idlelwp;
147 1.18 matt }
148 1.18 matt l->l_md.md_flags &= ~MDLWP_OWNVEC;
149 1.1 matt
150 1.1 matt /*
151 1.1 matt * Restore MSR (turn off AltiVec)
152 1.1 matt */
153 1.1 matt mtmsr(msr);
154 1.1 matt }
155 1.1 matt
156 1.13 garbled #ifdef MULTIPROCESSOR
157 1.13 garbled /*
158 1.13 garbled * Save a process's AltiVEC state to its PCB. The state may be in any CPU.
159 1.13 garbled * The process must either be curproc or traced by curproc (and stopped).
160 1.13 garbled * (The point being that the process must not run on another CPU during
161 1.13 garbled * this function).
162 1.13 garbled */
163 1.13 garbled static void
164 1.18 matt vec_mp_save_lwp(struct lwp *l)
165 1.13 garbled {
166 1.13 garbled /*
167 1.13 garbled * Send an IPI to the other CPU with the data and wait for that CPU
168 1.13 garbled * to flush the data. Note that the other CPU might have switched
169 1.13 garbled * to a different proc's AltiVEC state by the time it receives the IPI,
170 1.13 garbled * but that will only result in an unnecessary reload.
171 1.13 garbled */
172 1.13 garbled
173 1.18 matt if ((l->l_md.md_flags & MDLWP_OWNVEC) == 0)
174 1.13 garbled return;
175 1.13 garbled
176 1.18 matt ppc_send_ipi(l->l_md.md_veccpu->ci_index, PPC_IPI_FLUSH_VEC);
177 1.13 garbled
178 1.13 garbled /* Wait for flush. */
179 1.18 matt for (u_int i = 0; i < 0x3fffffff; i++) {
180 1.18 matt if ((l->l_md.md_flags & MDLWP_OWNVEC) == 0)
181 1.13 garbled return;
182 1.18 matt }
183 1.13 garbled
184 1.18 matt panic("%s/%d timed out: pid = %d.%d, veccpu->ci_cpuid = %d\n",
185 1.18 matt __func__, cpu_number(), l->l_proc->p_pid, l->l_lid,
186 1.18 matt veccpu->ci_cpuid);
187 1.13 garbled }
188 1.13 garbled #endif /*MULTIPROCESSOR*/
189 1.13 garbled
190 1.1 matt /*
191 1.1 matt * Save a process's AltiVEC state to its PCB. The state may be in any CPU.
192 1.1 matt * The process must either be curproc or traced by curproc (and stopped).
193 1.1 matt * (The point being that the process must not run on another CPU during
194 1.1 matt * this function).
195 1.1 matt */
196 1.1 matt void
197 1.18 matt vec_save_lwp(struct lwp *l, enum vec_op op)
198 1.1 matt {
199 1.7 matt struct cpu_info * const ci = curcpu();
200 1.1 matt
201 1.18 matt KASSERT(l->l_md.md_veccpu != NULL);
202 1.18 matt
203 1.1 matt /*
204 1.1 matt * If it's already in the PCB, there's nothing to do.
205 1.1 matt */
206 1.18 matt if ((l->l_md.md_flags & MDLWP_OWNVEC) == 0)
207 1.7 matt return;
208 1.1 matt
209 1.7 matt /*
210 1.7 matt * If we simply need to discard the information, then don't
211 1.7 matt * to save anything.
212 1.7 matt */
213 1.18 matt if (op == VEC_DISCARD) {
214 1.7 matt #ifndef MULTIPROCESSOR
215 1.18 matt KASSERT(ci == l->l_md.md_veccpu);
216 1.7 matt #endif
217 1.18 matt KASSERT(l == l->l_md.md_veccpu->ci_veclwp);
218 1.18 matt KASSERT(l == curlwp || ci == l->l_md.md_veccpu);
219 1.18 matt ci->ci_veclwp = ci->ci_data.cpu_idlelwp;
220 1.18 matt atomic_and_uint(&l->l_md.md_flags, ~MDLWP_OWNVEC);
221 1.1 matt return;
222 1.1 matt }
223 1.1 matt
224 1.1 matt /*
225 1.1 matt * If the state is in the current CPU, just flush the current CPU's
226 1.1 matt * state.
227 1.1 matt */
228 1.1 matt if (l == ci->ci_veclwp) {
229 1.18 matt vec_save_cpu(op);
230 1.1 matt return;
231 1.1 matt }
232 1.1 matt
233 1.7 matt
234 1.1 matt #ifdef MULTIPROCESSOR
235 1.1 matt /*
236 1.1 matt * It must be on another CPU, flush it from there.
237 1.1 matt */
238 1.18 matt vec_mp_save_lwp(l);
239 1.18 matt #endif
240 1.18 matt }
241 1.18 matt
242 1.18 matt void
243 1.18 matt vec_restore_from_mcontext(struct lwp *l, const mcontext_t *mcp)
244 1.18 matt {
245 1.18 matt struct pcb * const pcb = lwp_getpcb(l);
246 1.18 matt
247 1.18 matt /* we don't need to save the state, just drop it */
248 1.18 matt vec_save_lwp(l, VEC_DISCARD);
249 1.18 matt memcpy(pcb->pcb_vr.vreg, &mcp->__vrf.__vrs, sizeof (pcb->pcb_vr.vreg));
250 1.18 matt pcb->pcb_vr.vscr = mcp->__vrf.__vscr;
251 1.18 matt pcb->pcb_vr.vrsave = mcp->__vrf.__vrsave;
252 1.18 matt l->l_md.md_utf->tf_vrsave = pcb->pcb_vr.vrsave;
253 1.18 matt }
254 1.18 matt
255 1.18 matt bool
256 1.18 matt vec_save_to_mcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flagp)
257 1.18 matt {
258 1.18 matt /* Save AltiVec context, if any. */
259 1.18 matt if ((l->l_md.md_flags & MDLWP_USEDVEC) == 0)
260 1.18 matt return false;
261 1.18 matt
262 1.18 matt struct pcb * const pcb = lwp_getpcb(l);
263 1.18 matt
264 1.18 matt /*
265 1.18 matt * If we're the AltiVec owner, dump its context to the PCB first.
266 1.18 matt */
267 1.18 matt vec_save_lwp(l, VEC_SAVE);
268 1.1 matt
269 1.18 matt mcp->__gregs[_REG_MSR] |= PSL_VEC;
270 1.18 matt mcp->__vrf.__vscr = pcb->pcb_vr.vscr;
271 1.18 matt mcp->__vrf.__vrsave = l->l_md.md_utf->tf_vrsave;
272 1.18 matt memcpy(mcp->__vrf.__vrs, pcb->pcb_vr.vreg, sizeof (mcp->__vrf.__vrs));
273 1.18 matt *flagp |= _UC_POWERPC_VEC;
274 1.18 matt return true;
275 1.1 matt }
276 1.1 matt
277 1.1 matt #define ZERO_VEC 19
278 1.1 matt
279 1.1 matt void
280 1.1 matt vzeropage(paddr_t pa)
281 1.1 matt {
282 1.3 thorpej const paddr_t ea = pa + PAGE_SIZE;
283 1.1 matt uint32_t vec[7], *vp = (void *) roundup((uintptr_t) vec, 16);
284 1.2 matt register_t omsr, msr;
285 1.1 matt
286 1.11 perry __asm volatile("mfmsr %0" : "=r"(omsr) :);
287 1.1 matt
288 1.1 matt /*
289 1.1 matt * Turn on AltiVec, turn off interrupts.
290 1.1 matt */
291 1.1 matt msr = (omsr & ~PSL_EE) | PSL_VEC;
292 1.11 perry __asm volatile("sync; mtmsr %0; isync" :: "r"(msr));
293 1.1 matt
294 1.1 matt /*
295 1.1 matt * Save the VEC register we are going to use before we disable
296 1.1 matt * relocation.
297 1.1 matt */
298 1.1 matt __asm("stvx %1,0,%0" :: "r"(vp), "n"(ZERO_VEC));
299 1.1 matt __asm("vxor %0,%0,%0" :: "n"(ZERO_VEC));
300 1.1 matt
301 1.1 matt /*
302 1.1 matt * Zero the page using a single cache line.
303 1.1 matt */
304 1.11 perry __asm volatile(
305 1.9 nathanw " sync ;"
306 1.9 nathanw " mfmsr %[msr];"
307 1.9 nathanw " rlwinm %[msr],%[msr],0,28,26;" /* Clear PSL_DR */
308 1.9 nathanw " mtmsr %[msr];" /* Turn off DMMU */
309 1.9 nathanw " isync;"
310 1.9 nathanw "1: stvx %[zv], %[pa], %[off0];"
311 1.9 nathanw " stvxl %[zv], %[pa], %[off16];"
312 1.9 nathanw " stvx %[zv], %[pa], %[off32];"
313 1.9 nathanw " stvxl %[zv], %[pa], %[off48];"
314 1.9 nathanw " addi %[pa], %[pa], 64;"
315 1.9 nathanw " cmplw %[pa], %[ea];"
316 1.9 nathanw " blt+ 1b;"
317 1.9 nathanw " ori %[msr], %[msr], 0x10;" /* Set PSL_DR */
318 1.9 nathanw " sync;"
319 1.9 nathanw " mtmsr %[msr];" /* Turn on DMMU */
320 1.9 nathanw " isync;"
321 1.9 nathanw :: [msr] "r"(msr), [pa] "b"(pa), [ea] "b"(ea),
322 1.9 nathanw [off0] "r"(0), [off16] "r"(16), [off32] "r"(32), [off48] "r"(48),
323 1.9 nathanw [zv] "n"(ZERO_VEC));
324 1.1 matt
325 1.1 matt /*
326 1.1 matt * Restore VEC register (now that we can access the stack again).
327 1.1 matt */
328 1.1 matt __asm("lvx %1,0,%0" :: "r"(vp), "n"(ZERO_VEC));
329 1.1 matt
330 1.1 matt /*
331 1.1 matt * Restore old MSR (AltiVec OFF).
332 1.1 matt */
333 1.11 perry __asm volatile("sync; mtmsr %0; isync" :: "r"(omsr));
334 1.1 matt }
335 1.1 matt
336 1.1 matt #define LO_VEC 16
337 1.1 matt #define HI_VEC 17
338 1.1 matt
339 1.1 matt void
340 1.1 matt vcopypage(paddr_t dst, paddr_t src)
341 1.1 matt {
342 1.3 thorpej const paddr_t edst = dst + PAGE_SIZE;
343 1.1 matt uint32_t vec[11], *vp = (void *) roundup((uintptr_t) vec, 16);
344 1.2 matt register_t omsr, msr;
345 1.1 matt
346 1.11 perry __asm volatile("mfmsr %0" : "=r"(omsr) :);
347 1.1 matt
348 1.1 matt /*
349 1.1 matt * Turn on AltiVec, turn off interrupts.
350 1.1 matt */
351 1.1 matt msr = (omsr & ~PSL_EE) | PSL_VEC;
352 1.11 perry __asm volatile("sync; mtmsr %0; isync" :: "r"(msr));
353 1.1 matt
354 1.1 matt /*
355 1.1 matt * Save the VEC registers we will be using before we disable
356 1.1 matt * relocation.
357 1.1 matt */
358 1.2 matt __asm("stvx %2,%1,%0" :: "b"(vp), "r"( 0), "n"(LO_VEC));
359 1.2 matt __asm("stvx %2,%1,%0" :: "b"(vp), "r"(16), "n"(HI_VEC));
360 1.1 matt
361 1.1 matt /*
362 1.9 nathanw * Copy the page using a single cache line, with DMMU
363 1.9 nathanw * disabled. On most PPCs, two vector registers occupy one
364 1.9 nathanw * cache line.
365 1.9 nathanw */
366 1.11 perry __asm volatile(
367 1.9 nathanw " sync ;"
368 1.9 nathanw " mfmsr %[msr];"
369 1.9 nathanw " rlwinm %[msr],%[msr],0,28,26;" /* Clear PSL_DR */
370 1.9 nathanw " mtmsr %[msr];" /* Turn off DMMU */
371 1.9 nathanw " isync;"
372 1.9 nathanw "1: lvx %[lv], %[src], %[off0];"
373 1.9 nathanw " stvx %[lv], %[dst], %[off0];"
374 1.9 nathanw " lvxl %[hv], %[src], %[off16];"
375 1.9 nathanw " stvxl %[hv], %[dst], %[off16];"
376 1.9 nathanw " addi %[src], %[src], 32;"
377 1.9 nathanw " addi %[dst], %[dst], 32;"
378 1.9 nathanw " cmplw %[dst], %[edst];"
379 1.9 nathanw " blt+ 1b;"
380 1.9 nathanw " ori %[msr], %[msr], 0x10;" /* Set PSL_DR */
381 1.9 nathanw " sync;"
382 1.9 nathanw " mtmsr %[msr];" /* Turn on DMMU */
383 1.9 nathanw " isync;"
384 1.9 nathanw :: [msr] "r"(msr), [src] "b"(src), [dst] "b"(dst),
385 1.9 nathanw [edst] "b"(edst), [off0] "r"(0), [off16] "r"(16),
386 1.9 nathanw [lv] "n"(LO_VEC), [hv] "n"(HI_VEC));
387 1.1 matt
388 1.1 matt /*
389 1.1 matt * Restore VEC registers (now that we can access the stack again).
390 1.1 matt */
391 1.2 matt __asm("lvx %2,%1,%0" :: "b"(vp), "r"( 0), "n"(LO_VEC));
392 1.2 matt __asm("lvx %2,%1,%0" :: "b"(vp), "r"(16), "n"(HI_VEC));
393 1.1 matt
394 1.1 matt /*
395 1.1 matt * Restore old MSR (AltiVec OFF).
396 1.1 matt */
397 1.11 perry __asm volatile("sync; mtmsr %0; isync" :: "r"(omsr));
398 1.1 matt }
399