Home | History | Annotate | Line # | Download | only in oea
altivec.c revision 1.26
      1  1.26     matt /*	$NetBSD: altivec.c,v 1.26 2012/12/26 19:05:03 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.26     matt __KERNEL_RCSID(0, "$NetBSD: altivec.c,v 1.26 2012/12/26 19:05:03 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.26     matt static void vec_state_load(lwp_t *, u_int);
     53  1.26     matt static void vec_state_save(lwp_t *, u_int);
     54  1.26     matt static void vec_state_release(lwp_t *, u_int);
     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.26     matt 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     matt 	if (__predict_false(!vec_used_p(l))) {
     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.1     matt 	/*
     86   1.1     matt 	 * Enable AltiVec temporarily (and disable interrupts).
     87   1.1     matt 	 */
     88  1.22     matt 	const register_t msr = mfmsr();
     89   1.1     matt 	mtmsr((msr & ~PSL_EE) | PSL_VEC);
     90  1.11    perry 	__asm volatile ("isync");
     91  1.18     matt 
     92   1.1     matt 	/*
     93  1.22     matt 	 * Load the vector unit from vreg which is best done in
     94  1.22     matt 	 * assembly.
     95   1.1     matt 	 */
     96  1.22     matt 	vec_load_from_vreg(&pcb->pcb_vr);
     97   1.1     matt 
     98   1.1     matt 	/*
     99  1.22     matt 	 * VRSAVE will be restored when trap frame returns
    100   1.1     matt 	 */
    101  1.22     matt 	l->l_md.md_utf->tf_vrsave = pcb->pcb_vr.vrsave;
    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.22     matt 	__asm volatile ("isync");
    108   1.1     matt 
    109  1.13  garbled 	/*
    110  1.22     matt 	 * Mark vector registers as modified.
    111  1.22     matt 	 */
    112  1.24     matt 	l->l_md.md_flags |= MDLWP_USEDVEC|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.26     matt vec_state_save(lwp_t *l, u_int flags)
    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.26     matt vec_state_release(lwp_t *l, u_int flags)
    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.22     matt 	pcu_discard(&vec_ops);
    168  1.18     matt 	memcpy(pcb->pcb_vr.vreg, &mcp->__vrf.__vrs, sizeof (pcb->pcb_vr.vreg));
    169  1.18     matt 	pcb->pcb_vr.vscr = mcp->__vrf.__vscr;
    170  1.18     matt 	pcb->pcb_vr.vrsave = mcp->__vrf.__vrsave;
    171  1.18     matt 	l->l_md.md_utf->tf_vrsave = pcb->pcb_vr.vrsave;
    172  1.18     matt }
    173  1.18     matt 
    174  1.18     matt bool
    175  1.18     matt vec_save_to_mcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flagp)
    176  1.18     matt {
    177  1.22     matt 	struct pcb * const pcb = lwp_getpcb(l);
    178  1.22     matt 
    179  1.22     matt 	KASSERT(l == curlwp);
    180  1.22     matt 
    181  1.18     matt 	/* Save AltiVec context, if any. */
    182  1.22     matt 	if (!vec_used_p(l))
    183  1.18     matt 		return false;
    184  1.18     matt 
    185  1.18     matt 	/*
    186  1.18     matt 	 * If we're the AltiVec owner, dump its context to the PCB first.
    187  1.18     matt 	 */
    188  1.22     matt 	pcu_save(&vec_ops);
    189   1.1     matt 
    190  1.18     matt 	mcp->__gregs[_REG_MSR] |= PSL_VEC;
    191  1.18     matt 	mcp->__vrf.__vscr = pcb->pcb_vr.vscr;
    192  1.18     matt 	mcp->__vrf.__vrsave = l->l_md.md_utf->tf_vrsave;
    193  1.18     matt 	memcpy(mcp->__vrf.__vrs, pcb->pcb_vr.vreg, sizeof (mcp->__vrf.__vrs));
    194  1.18     matt 	*flagp |= _UC_POWERPC_VEC;
    195  1.18     matt 	return true;
    196   1.1     matt }
    197   1.1     matt 
    198   1.1     matt #define ZERO_VEC	19
    199   1.1     matt 
    200   1.1     matt void
    201   1.1     matt vzeropage(paddr_t pa)
    202   1.1     matt {
    203   1.3  thorpej 	const paddr_t ea = pa + PAGE_SIZE;
    204   1.1     matt 	uint32_t vec[7], *vp = (void *) roundup((uintptr_t) vec, 16);
    205   1.2     matt 	register_t omsr, msr;
    206   1.1     matt 
    207  1.11    perry 	__asm volatile("mfmsr %0" : "=r"(omsr) :);
    208   1.1     matt 
    209   1.1     matt 	/*
    210   1.1     matt 	 * Turn on AltiVec, turn off interrupts.
    211   1.1     matt 	 */
    212   1.1     matt 	msr = (omsr & ~PSL_EE) | PSL_VEC;
    213  1.11    perry 	__asm volatile("sync; mtmsr %0; isync" :: "r"(msr));
    214   1.1     matt 
    215   1.1     matt 	/*
    216   1.1     matt 	 * Save the VEC register we are going to use before we disable
    217   1.1     matt 	 * relocation.
    218   1.1     matt 	 */
    219   1.1     matt 	__asm("stvx %1,0,%0" :: "r"(vp), "n"(ZERO_VEC));
    220   1.1     matt 	__asm("vxor %0,%0,%0" :: "n"(ZERO_VEC));
    221   1.1     matt 
    222   1.1     matt 	/*
    223   1.1     matt 	 * Zero the page using a single cache line.
    224   1.1     matt 	 */
    225  1.11    perry 	__asm volatile(
    226   1.9  nathanw 	    "   sync ;"
    227   1.9  nathanw 	    "   mfmsr  %[msr];"
    228   1.9  nathanw 	    "   rlwinm %[msr],%[msr],0,28,26;"	/* Clear PSL_DR */
    229   1.9  nathanw 	    "   mtmsr  %[msr];"			/* Turn off DMMU */
    230   1.9  nathanw 	    "   isync;"
    231   1.9  nathanw 	    "1: stvx   %[zv], %[pa], %[off0];"
    232   1.9  nathanw 	    "   stvxl  %[zv], %[pa], %[off16];"
    233   1.9  nathanw 	    "   stvx   %[zv], %[pa], %[off32];"
    234   1.9  nathanw 	    "   stvxl  %[zv], %[pa], %[off48];"
    235   1.9  nathanw 	    "   addi   %[pa], %[pa], 64;"
    236   1.9  nathanw 	    "   cmplw  %[pa], %[ea];"
    237   1.9  nathanw 	    "	blt+   1b;"
    238   1.9  nathanw 	    "   ori    %[msr], %[msr], 0x10;"	/* Set PSL_DR */
    239   1.9  nathanw 	    "   sync;"
    240   1.9  nathanw 	    "	mtmsr  %[msr];"			/* Turn on DMMU */
    241   1.9  nathanw 	    "   isync;"
    242   1.9  nathanw 	    :: [msr] "r"(msr), [pa] "b"(pa), [ea] "b"(ea),
    243   1.9  nathanw 	    [off0] "r"(0), [off16] "r"(16), [off32] "r"(32), [off48] "r"(48),
    244   1.9  nathanw 	    [zv] "n"(ZERO_VEC));
    245   1.1     matt 
    246   1.1     matt 	/*
    247   1.1     matt 	 * Restore VEC register (now that we can access the stack again).
    248   1.1     matt 	 */
    249   1.1     matt 	__asm("lvx %1,0,%0" :: "r"(vp), "n"(ZERO_VEC));
    250   1.1     matt 
    251   1.1     matt 	/*
    252   1.1     matt 	 * Restore old MSR (AltiVec OFF).
    253   1.1     matt 	 */
    254  1.11    perry 	__asm volatile("sync; mtmsr %0; isync" :: "r"(omsr));
    255   1.1     matt }
    256   1.1     matt 
    257   1.1     matt #define LO_VEC	16
    258   1.1     matt #define HI_VEC	17
    259   1.1     matt 
    260   1.1     matt void
    261   1.1     matt vcopypage(paddr_t dst, paddr_t src)
    262   1.1     matt {
    263   1.3  thorpej 	const paddr_t edst = dst + PAGE_SIZE;
    264   1.1     matt 	uint32_t vec[11], *vp = (void *) roundup((uintptr_t) vec, 16);
    265   1.2     matt 	register_t omsr, msr;
    266   1.1     matt 
    267  1.11    perry 	__asm volatile("mfmsr %0" : "=r"(omsr) :);
    268   1.1     matt 
    269   1.1     matt 	/*
    270   1.1     matt 	 * Turn on AltiVec, turn off interrupts.
    271   1.1     matt 	 */
    272   1.1     matt 	msr = (omsr & ~PSL_EE) | PSL_VEC;
    273  1.11    perry 	__asm volatile("sync; mtmsr %0; isync" :: "r"(msr));
    274   1.1     matt 
    275   1.1     matt 	/*
    276   1.1     matt 	 * Save the VEC registers we will be using before we disable
    277   1.1     matt 	 * relocation.
    278   1.1     matt 	 */
    279   1.2     matt 	__asm("stvx %2,%1,%0" :: "b"(vp), "r"( 0), "n"(LO_VEC));
    280   1.2     matt 	__asm("stvx %2,%1,%0" :: "b"(vp), "r"(16), "n"(HI_VEC));
    281   1.1     matt 
    282   1.1     matt 	/*
    283   1.9  nathanw 	 * Copy the page using a single cache line, with DMMU
    284   1.9  nathanw 	 * disabled.  On most PPCs, two vector registers occupy one
    285   1.9  nathanw 	 * cache line.
    286   1.9  nathanw 	 */
    287  1.11    perry 	__asm volatile(
    288   1.9  nathanw 	    "   sync ;"
    289   1.9  nathanw 	    "   mfmsr  %[msr];"
    290   1.9  nathanw 	    "   rlwinm %[msr],%[msr],0,28,26;"	/* Clear PSL_DR */
    291   1.9  nathanw 	    "   mtmsr  %[msr];"			/* Turn off DMMU */
    292   1.9  nathanw 	    "   isync;"
    293   1.9  nathanw 	    "1: lvx    %[lv], %[src], %[off0];"
    294   1.9  nathanw 	    "   stvx   %[lv], %[dst], %[off0];"
    295   1.9  nathanw 	    "   lvxl   %[hv], %[src], %[off16];"
    296   1.9  nathanw 	    "   stvxl  %[hv], %[dst], %[off16];"
    297   1.9  nathanw 	    "   addi   %[src], %[src], 32;"
    298   1.9  nathanw 	    "   addi   %[dst], %[dst], 32;"
    299   1.9  nathanw 	    "   cmplw  %[dst], %[edst];"
    300   1.9  nathanw 	    "	blt+   1b;"
    301   1.9  nathanw 	    "   ori    %[msr], %[msr], 0x10;"	/* Set PSL_DR */
    302   1.9  nathanw 	    "   sync;"
    303   1.9  nathanw 	    "	mtmsr  %[msr];"			/* Turn on DMMU */
    304   1.9  nathanw 	    "   isync;"
    305   1.9  nathanw 	    :: [msr] "r"(msr), [src] "b"(src), [dst] "b"(dst),
    306   1.9  nathanw 	    [edst] "b"(edst), [off0] "r"(0), [off16] "r"(16),
    307   1.9  nathanw 	    [lv] "n"(LO_VEC), [hv] "n"(HI_VEC));
    308   1.1     matt 
    309   1.1     matt 	/*
    310   1.1     matt 	 * Restore VEC registers (now that we can access the stack again).
    311   1.1     matt 	 */
    312   1.2     matt 	__asm("lvx %2,%1,%0" :: "b"(vp), "r"( 0), "n"(LO_VEC));
    313   1.2     matt 	__asm("lvx %2,%1,%0" :: "b"(vp), "r"(16), "n"(HI_VEC));
    314   1.1     matt 
    315   1.1     matt 	/*
    316   1.1     matt 	 * Restore old MSR (AltiVec OFF).
    317   1.1     matt 	 */
    318  1.11    perry 	__asm volatile("sync; mtmsr %0; isync" :: "r"(omsr));
    319   1.1     matt }
    320