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