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