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