1 1.35 mrg /* $NetBSD: altivec.c,v 1.35 2024/06/15 19:48:13 mrg 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.35 mrg __KERNEL_RCSID(0, "$NetBSD: altivec.c,v 1.35 2024/06/15 19:48:13 mrg 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.35 mrg /* Nothing to do here. */ 167 1.35 mrg if (!vec_used_p(l)) 168 1.35 mrg return; 169 1.35 mrg 170 1.18 matt /* we don't need to save the state, just drop it */ 171 1.31 chs pcu_discard(&vec_ops, l, true); 172 1.31 chs 173 1.34 thorpej if (mcp != NULL) { /* XXX see compat_16_sys___sigreturn14() */ 174 1.34 thorpej memcpy(pcb->pcb_vr.vreg, &mcp->__vrf.__vrs, 175 1.34 thorpej sizeof (pcb->pcb_vr.vreg)); 176 1.34 thorpej pcb->pcb_vr.vscr = mcp->__vrf.__vscr; 177 1.34 thorpej pcb->pcb_vr.vrsave = mcp->__vrf.__vrsave; 178 1.34 thorpej l->l_md.md_utf->tf_vrsave = pcb->pcb_vr.vrsave; 179 1.34 thorpej } 180 1.18 matt } 181 1.18 matt 182 1.18 matt bool 183 1.18 matt vec_save_to_mcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flagp) 184 1.18 matt { 185 1.22 matt struct pcb * const pcb = lwp_getpcb(l); 186 1.22 matt 187 1.22 matt KASSERT(l == curlwp); 188 1.22 matt 189 1.18 matt /* Save AltiVec context, if any. */ 190 1.22 matt if (!vec_used_p(l)) 191 1.18 matt return false; 192 1.18 matt 193 1.18 matt /* 194 1.18 matt * If we're the AltiVec owner, dump its context to the PCB first. 195 1.18 matt */ 196 1.31 chs pcu_save(&vec_ops, l); 197 1.1 matt 198 1.34 thorpej if (mcp != NULL) { /* XXX see sendsig_sigcontext() */ 199 1.34 thorpej mcp->__gregs[_REG_MSR] |= PSL_VEC; 200 1.34 thorpej mcp->__vrf.__vscr = pcb->pcb_vr.vscr; 201 1.34 thorpej mcp->__vrf.__vrsave = l->l_md.md_utf->tf_vrsave; 202 1.34 thorpej memcpy(mcp->__vrf.__vrs, pcb->pcb_vr.vreg, 203 1.34 thorpej sizeof (mcp->__vrf.__vrs)); 204 1.34 thorpej *flagp |= _UC_POWERPC_VEC; 205 1.34 thorpej } 206 1.18 matt return true; 207 1.1 matt } 208 1.1 matt 209 1.1 matt #define ZERO_VEC 19 210 1.1 matt 211 1.1 matt void 212 1.1 matt vzeropage(paddr_t pa) 213 1.1 matt { 214 1.3 thorpej const paddr_t ea = pa + PAGE_SIZE; 215 1.1 matt uint32_t vec[7], *vp = (void *) roundup((uintptr_t) vec, 16); 216 1.2 matt register_t omsr, msr; 217 1.1 matt 218 1.11 perry __asm volatile("mfmsr %0" : "=r"(omsr) :); 219 1.1 matt 220 1.1 matt /* 221 1.1 matt * Turn on AltiVec, turn off interrupts. 222 1.1 matt */ 223 1.1 matt msr = (omsr & ~PSL_EE) | PSL_VEC; 224 1.11 perry __asm volatile("sync; mtmsr %0; isync" :: "r"(msr)); 225 1.1 matt 226 1.1 matt /* 227 1.1 matt * Save the VEC register we are going to use before we disable 228 1.1 matt * relocation. 229 1.1 matt */ 230 1.1 matt __asm("stvx %1,0,%0" :: "r"(vp), "n"(ZERO_VEC)); 231 1.1 matt __asm("vxor %0,%0,%0" :: "n"(ZERO_VEC)); 232 1.1 matt 233 1.1 matt /* 234 1.1 matt * Zero the page using a single cache line. 235 1.1 matt */ 236 1.11 perry __asm volatile( 237 1.9 nathanw " sync ;" 238 1.9 nathanw " mfmsr %[msr];" 239 1.9 nathanw " rlwinm %[msr],%[msr],0,28,26;" /* Clear PSL_DR */ 240 1.9 nathanw " mtmsr %[msr];" /* Turn off DMMU */ 241 1.9 nathanw " isync;" 242 1.9 nathanw "1: stvx %[zv], %[pa], %[off0];" 243 1.9 nathanw " stvxl %[zv], %[pa], %[off16];" 244 1.9 nathanw " stvx %[zv], %[pa], %[off32];" 245 1.9 nathanw " stvxl %[zv], %[pa], %[off48];" 246 1.9 nathanw " addi %[pa], %[pa], 64;" 247 1.9 nathanw " cmplw %[pa], %[ea];" 248 1.9 nathanw " blt+ 1b;" 249 1.9 nathanw " ori %[msr], %[msr], 0x10;" /* Set PSL_DR */ 250 1.9 nathanw " sync;" 251 1.9 nathanw " mtmsr %[msr];" /* Turn on DMMU */ 252 1.9 nathanw " isync;" 253 1.9 nathanw :: [msr] "r"(msr), [pa] "b"(pa), [ea] "b"(ea), 254 1.9 nathanw [off0] "r"(0), [off16] "r"(16), [off32] "r"(32), [off48] "r"(48), 255 1.9 nathanw [zv] "n"(ZERO_VEC)); 256 1.1 matt 257 1.1 matt /* 258 1.1 matt * Restore VEC register (now that we can access the stack again). 259 1.1 matt */ 260 1.1 matt __asm("lvx %1,0,%0" :: "r"(vp), "n"(ZERO_VEC)); 261 1.1 matt 262 1.1 matt /* 263 1.1 matt * Restore old MSR (AltiVec OFF). 264 1.1 matt */ 265 1.11 perry __asm volatile("sync; mtmsr %0; isync" :: "r"(omsr)); 266 1.1 matt } 267 1.1 matt 268 1.1 matt #define LO_VEC 16 269 1.1 matt #define HI_VEC 17 270 1.1 matt 271 1.1 matt void 272 1.1 matt vcopypage(paddr_t dst, paddr_t src) 273 1.1 matt { 274 1.3 thorpej const paddr_t edst = dst + PAGE_SIZE; 275 1.1 matt uint32_t vec[11], *vp = (void *) roundup((uintptr_t) vec, 16); 276 1.2 matt register_t omsr, msr; 277 1.1 matt 278 1.11 perry __asm volatile("mfmsr %0" : "=r"(omsr) :); 279 1.1 matt 280 1.1 matt /* 281 1.1 matt * Turn on AltiVec, turn off interrupts. 282 1.1 matt */ 283 1.1 matt msr = (omsr & ~PSL_EE) | PSL_VEC; 284 1.11 perry __asm volatile("sync; mtmsr %0; isync" :: "r"(msr)); 285 1.1 matt 286 1.1 matt /* 287 1.1 matt * Save the VEC registers we will be using before we disable 288 1.1 matt * relocation. 289 1.1 matt */ 290 1.2 matt __asm("stvx %2,%1,%0" :: "b"(vp), "r"( 0), "n"(LO_VEC)); 291 1.2 matt __asm("stvx %2,%1,%0" :: "b"(vp), "r"(16), "n"(HI_VEC)); 292 1.1 matt 293 1.1 matt /* 294 1.9 nathanw * Copy the page using a single cache line, with DMMU 295 1.9 nathanw * disabled. On most PPCs, two vector registers occupy one 296 1.9 nathanw * cache line. 297 1.9 nathanw */ 298 1.11 perry __asm volatile( 299 1.9 nathanw " sync ;" 300 1.9 nathanw " mfmsr %[msr];" 301 1.9 nathanw " rlwinm %[msr],%[msr],0,28,26;" /* Clear PSL_DR */ 302 1.9 nathanw " mtmsr %[msr];" /* Turn off DMMU */ 303 1.9 nathanw " isync;" 304 1.9 nathanw "1: lvx %[lv], %[src], %[off0];" 305 1.9 nathanw " stvx %[lv], %[dst], %[off0];" 306 1.9 nathanw " lvxl %[hv], %[src], %[off16];" 307 1.9 nathanw " stvxl %[hv], %[dst], %[off16];" 308 1.9 nathanw " addi %[src], %[src], 32;" 309 1.9 nathanw " addi %[dst], %[dst], 32;" 310 1.9 nathanw " cmplw %[dst], %[edst];" 311 1.9 nathanw " blt+ 1b;" 312 1.9 nathanw " ori %[msr], %[msr], 0x10;" /* Set PSL_DR */ 313 1.9 nathanw " sync;" 314 1.9 nathanw " mtmsr %[msr];" /* Turn on DMMU */ 315 1.9 nathanw " isync;" 316 1.9 nathanw :: [msr] "r"(msr), [src] "b"(src), [dst] "b"(dst), 317 1.9 nathanw [edst] "b"(edst), [off0] "r"(0), [off16] "r"(16), 318 1.9 nathanw [lv] "n"(LO_VEC), [hv] "n"(HI_VEC)); 319 1.1 matt 320 1.1 matt /* 321 1.1 matt * Restore VEC registers (now that we can access the stack again). 322 1.1 matt */ 323 1.2 matt __asm("lvx %2,%1,%0" :: "b"(vp), "r"( 0), "n"(LO_VEC)); 324 1.2 matt __asm("lvx %2,%1,%0" :: "b"(vp), "r"(16), "n"(HI_VEC)); 325 1.1 matt 326 1.1 matt /* 327 1.1 matt * Restore old MSR (AltiVec OFF). 328 1.1 matt */ 329 1.11 perry __asm volatile("sync; mtmsr %0; isync" :: "r"(omsr)); 330 1.1 matt } 331