altivec.c revision 1.23 1 1.23 matt /* $NetBSD: altivec.c,v 1.23 2011/05/02 06:43:16 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.23 matt __KERNEL_RCSID(0, "$NetBSD: altivec.c,v 1.23 2011/05/02 06:43:16 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.22 matt static void vec_state_load(lwp_t *, bool);
53 1.22 matt static void vec_state_save(lwp_t *);
54 1.22 matt static void vec_state_release(lwp_t *);
55 1.22 matt
56 1.22 matt const pcu_ops_t vec_ops = {
57 1.22 matt .pcu_id = PCU_VEC,
58 1.22 matt .pcu_state_load = vec_state_load,
59 1.22 matt .pcu_state_save = vec_state_save,
60 1.22 matt .pcu_state_release = vec_state_release,
61 1.22 matt };
62 1.22 matt
63 1.22 matt bool
64 1.22 matt vec_used_p(lwp_t *l)
65 1.22 matt {
66 1.22 matt return (l->l_md.md_flags & MDLWP_USEDVEC) != 0;
67 1.22 matt }
68 1.13 garbled
69 1.1 matt void
70 1.22 matt vec_mark_used(lwp_t *l)
71 1.1 matt {
72 1.22 matt l->l_md.md_flags |= MDLWP_USEDVEC;
73 1.22 matt }
74 1.1 matt
75 1.22 matt void
76 1.22 matt vec_state_load(lwp_t *l, bool used)
77 1.22 matt {
78 1.22 matt struct pcb * const pcb = lwp_getpcb(l);
79 1.1 matt
80 1.1 matt /*
81 1.1 matt * Enable AltiVec temporarily (and disable interrupts).
82 1.1 matt */
83 1.22 matt const register_t msr = mfmsr();
84 1.1 matt mtmsr((msr & ~PSL_EE) | PSL_VEC);
85 1.11 perry __asm volatile ("isync");
86 1.18 matt
87 1.1 matt /*
88 1.22 matt * Load the vector unit from vreg which is best done in
89 1.22 matt * assembly.
90 1.1 matt */
91 1.22 matt vec_load_from_vreg(&pcb->pcb_vr);
92 1.1 matt
93 1.1 matt /*
94 1.22 matt * VRSAVE will be restored when trap frame returns
95 1.1 matt */
96 1.22 matt l->l_md.md_utf->tf_vrsave = pcb->pcb_vr.vrsave;
97 1.1 matt
98 1.1 matt /*
99 1.1 matt * Restore MSR (turn off AltiVec)
100 1.1 matt */
101 1.1 matt mtmsr(msr);
102 1.22 matt __asm volatile ("isync");
103 1.1 matt
104 1.13 garbled /*
105 1.22 matt * Mark vector registers as modified.
106 1.22 matt */
107 1.22 matt l->l_md.md_flags |= MDLWP_USEDVEC;
108 1.23 matt l->l_md.md_utf->tf_srr1 |= PSL_VEC;
109 1.13 garbled }
110 1.13 garbled
111 1.1 matt void
112 1.22 matt vec_state_save(lwp_t *l)
113 1.1 matt {
114 1.22 matt struct pcb * const pcb = lwp_getpcb(l);
115 1.1 matt
116 1.22 matt /*
117 1.22 matt * Turn on AltiVEC, turn off interrupts.
118 1.22 matt */
119 1.22 matt const register_t msr = mfmsr();
120 1.22 matt mtmsr((msr & ~PSL_EE) | PSL_VEC);
121 1.22 matt __asm volatile ("isync");
122 1.18 matt
123 1.1 matt /*
124 1.22 matt * Grab contents of vector unit.
125 1.1 matt */
126 1.22 matt vec_unload_to_vreg(&pcb->pcb_vr);
127 1.1 matt
128 1.7 matt /*
129 1.22 matt * Save VRSAVE
130 1.7 matt */
131 1.22 matt pcb->pcb_vr.vrsave = l->l_md.md_utf->tf_vrsave;
132 1.1 matt
133 1.1 matt /*
134 1.22 matt * Note that we aren't using any CPU resources and stop any
135 1.22 matt * data streams.
136 1.1 matt */
137 1.22 matt __asm volatile ("dssall; sync");
138 1.7 matt
139 1.1 matt /*
140 1.22 matt * Restore MSR (turn off AltiVec)
141 1.1 matt */
142 1.22 matt mtmsr(msr);
143 1.22 matt __asm volatile ("isync");
144 1.22 matt }
145 1.22 matt
146 1.22 matt void
147 1.22 matt vec_state_release(lwp_t *l)
148 1.22 matt {
149 1.22 matt __asm volatile("dssall;sync");
150 1.22 matt l->l_md.md_utf->tf_srr1 &= ~PSL_VEC;
151 1.22 matt l->l_md.md_flags &= ~PSL_VEC;
152 1.18 matt }
153 1.18 matt
154 1.18 matt void
155 1.18 matt vec_restore_from_mcontext(struct lwp *l, const mcontext_t *mcp)
156 1.18 matt {
157 1.18 matt struct pcb * const pcb = lwp_getpcb(l);
158 1.18 matt
159 1.22 matt KASSERT(l == curlwp);
160 1.22 matt
161 1.18 matt /* we don't need to save the state, just drop it */
162 1.22 matt pcu_discard(&vec_ops);
163 1.18 matt memcpy(pcb->pcb_vr.vreg, &mcp->__vrf.__vrs, sizeof (pcb->pcb_vr.vreg));
164 1.18 matt pcb->pcb_vr.vscr = mcp->__vrf.__vscr;
165 1.18 matt pcb->pcb_vr.vrsave = mcp->__vrf.__vrsave;
166 1.18 matt l->l_md.md_utf->tf_vrsave = pcb->pcb_vr.vrsave;
167 1.18 matt }
168 1.18 matt
169 1.18 matt bool
170 1.18 matt vec_save_to_mcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flagp)
171 1.18 matt {
172 1.22 matt struct pcb * const pcb = lwp_getpcb(l);
173 1.22 matt
174 1.22 matt KASSERT(l == curlwp);
175 1.22 matt
176 1.18 matt /* Save AltiVec context, if any. */
177 1.22 matt if (!vec_used_p(l))
178 1.18 matt return false;
179 1.18 matt
180 1.18 matt /*
181 1.18 matt * If we're the AltiVec owner, dump its context to the PCB first.
182 1.18 matt */
183 1.22 matt pcu_save(&vec_ops);
184 1.1 matt
185 1.18 matt mcp->__gregs[_REG_MSR] |= PSL_VEC;
186 1.18 matt mcp->__vrf.__vscr = pcb->pcb_vr.vscr;
187 1.18 matt mcp->__vrf.__vrsave = l->l_md.md_utf->tf_vrsave;
188 1.18 matt memcpy(mcp->__vrf.__vrs, pcb->pcb_vr.vreg, sizeof (mcp->__vrf.__vrs));
189 1.18 matt *flagp |= _UC_POWERPC_VEC;
190 1.18 matt return true;
191 1.1 matt }
192 1.1 matt
193 1.1 matt #define ZERO_VEC 19
194 1.1 matt
195 1.1 matt void
196 1.1 matt vzeropage(paddr_t pa)
197 1.1 matt {
198 1.3 thorpej const paddr_t ea = pa + PAGE_SIZE;
199 1.1 matt uint32_t vec[7], *vp = (void *) roundup((uintptr_t) vec, 16);
200 1.2 matt register_t omsr, msr;
201 1.1 matt
202 1.11 perry __asm volatile("mfmsr %0" : "=r"(omsr) :);
203 1.1 matt
204 1.1 matt /*
205 1.1 matt * Turn on AltiVec, turn off interrupts.
206 1.1 matt */
207 1.1 matt msr = (omsr & ~PSL_EE) | PSL_VEC;
208 1.11 perry __asm volatile("sync; mtmsr %0; isync" :: "r"(msr));
209 1.1 matt
210 1.1 matt /*
211 1.1 matt * Save the VEC register we are going to use before we disable
212 1.1 matt * relocation.
213 1.1 matt */
214 1.1 matt __asm("stvx %1,0,%0" :: "r"(vp), "n"(ZERO_VEC));
215 1.1 matt __asm("vxor %0,%0,%0" :: "n"(ZERO_VEC));
216 1.1 matt
217 1.1 matt /*
218 1.1 matt * Zero the page using a single cache line.
219 1.1 matt */
220 1.11 perry __asm volatile(
221 1.9 nathanw " sync ;"
222 1.9 nathanw " mfmsr %[msr];"
223 1.9 nathanw " rlwinm %[msr],%[msr],0,28,26;" /* Clear PSL_DR */
224 1.9 nathanw " mtmsr %[msr];" /* Turn off DMMU */
225 1.9 nathanw " isync;"
226 1.9 nathanw "1: stvx %[zv], %[pa], %[off0];"
227 1.9 nathanw " stvxl %[zv], %[pa], %[off16];"
228 1.9 nathanw " stvx %[zv], %[pa], %[off32];"
229 1.9 nathanw " stvxl %[zv], %[pa], %[off48];"
230 1.9 nathanw " addi %[pa], %[pa], 64;"
231 1.9 nathanw " cmplw %[pa], %[ea];"
232 1.9 nathanw " blt+ 1b;"
233 1.9 nathanw " ori %[msr], %[msr], 0x10;" /* Set PSL_DR */
234 1.9 nathanw " sync;"
235 1.9 nathanw " mtmsr %[msr];" /* Turn on DMMU */
236 1.9 nathanw " isync;"
237 1.9 nathanw :: [msr] "r"(msr), [pa] "b"(pa), [ea] "b"(ea),
238 1.9 nathanw [off0] "r"(0), [off16] "r"(16), [off32] "r"(32), [off48] "r"(48),
239 1.9 nathanw [zv] "n"(ZERO_VEC));
240 1.1 matt
241 1.1 matt /*
242 1.1 matt * Restore VEC register (now that we can access the stack again).
243 1.1 matt */
244 1.1 matt __asm("lvx %1,0,%0" :: "r"(vp), "n"(ZERO_VEC));
245 1.1 matt
246 1.1 matt /*
247 1.1 matt * Restore old MSR (AltiVec OFF).
248 1.1 matt */
249 1.11 perry __asm volatile("sync; mtmsr %0; isync" :: "r"(omsr));
250 1.1 matt }
251 1.1 matt
252 1.1 matt #define LO_VEC 16
253 1.1 matt #define HI_VEC 17
254 1.1 matt
255 1.1 matt void
256 1.1 matt vcopypage(paddr_t dst, paddr_t src)
257 1.1 matt {
258 1.3 thorpej const paddr_t edst = dst + PAGE_SIZE;
259 1.1 matt uint32_t vec[11], *vp = (void *) roundup((uintptr_t) vec, 16);
260 1.2 matt register_t omsr, msr;
261 1.1 matt
262 1.11 perry __asm volatile("mfmsr %0" : "=r"(omsr) :);
263 1.1 matt
264 1.1 matt /*
265 1.1 matt * Turn on AltiVec, turn off interrupts.
266 1.1 matt */
267 1.1 matt msr = (omsr & ~PSL_EE) | PSL_VEC;
268 1.11 perry __asm volatile("sync; mtmsr %0; isync" :: "r"(msr));
269 1.1 matt
270 1.1 matt /*
271 1.1 matt * Save the VEC registers we will be using before we disable
272 1.1 matt * relocation.
273 1.1 matt */
274 1.2 matt __asm("stvx %2,%1,%0" :: "b"(vp), "r"( 0), "n"(LO_VEC));
275 1.2 matt __asm("stvx %2,%1,%0" :: "b"(vp), "r"(16), "n"(HI_VEC));
276 1.1 matt
277 1.1 matt /*
278 1.9 nathanw * Copy the page using a single cache line, with DMMU
279 1.9 nathanw * disabled. On most PPCs, two vector registers occupy one
280 1.9 nathanw * cache line.
281 1.9 nathanw */
282 1.11 perry __asm volatile(
283 1.9 nathanw " sync ;"
284 1.9 nathanw " mfmsr %[msr];"
285 1.9 nathanw " rlwinm %[msr],%[msr],0,28,26;" /* Clear PSL_DR */
286 1.9 nathanw " mtmsr %[msr];" /* Turn off DMMU */
287 1.9 nathanw " isync;"
288 1.9 nathanw "1: lvx %[lv], %[src], %[off0];"
289 1.9 nathanw " stvx %[lv], %[dst], %[off0];"
290 1.9 nathanw " lvxl %[hv], %[src], %[off16];"
291 1.9 nathanw " stvxl %[hv], %[dst], %[off16];"
292 1.9 nathanw " addi %[src], %[src], 32;"
293 1.9 nathanw " addi %[dst], %[dst], 32;"
294 1.9 nathanw " cmplw %[dst], %[edst];"
295 1.9 nathanw " blt+ 1b;"
296 1.9 nathanw " ori %[msr], %[msr], 0x10;" /* Set PSL_DR */
297 1.9 nathanw " sync;"
298 1.9 nathanw " mtmsr %[msr];" /* Turn on DMMU */
299 1.9 nathanw " isync;"
300 1.9 nathanw :: [msr] "r"(msr), [src] "b"(src), [dst] "b"(dst),
301 1.9 nathanw [edst] "b"(edst), [off0] "r"(0), [off16] "r"(16),
302 1.9 nathanw [lv] "n"(LO_VEC), [hv] "n"(HI_VEC));
303 1.1 matt
304 1.1 matt /*
305 1.1 matt * Restore VEC registers (now that we can access the stack again).
306 1.1 matt */
307 1.2 matt __asm("lvx %2,%1,%0" :: "b"(vp), "r"( 0), "n"(LO_VEC));
308 1.2 matt __asm("lvx %2,%1,%0" :: "b"(vp), "r"(16), "n"(HI_VEC));
309 1.1 matt
310 1.1 matt /*
311 1.1 matt * Restore old MSR (AltiVec OFF).
312 1.1 matt */
313 1.11 perry __asm volatile("sync; mtmsr %0; isync" :: "r"(omsr));
314 1.1 matt }
315