]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/powerpc/powerpc/altivec.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / powerpc / powerpc / altivec.c
1 /*-
2  * Copyright (C) 1996 Wolfgang Solfrank.
3  * Copyright (C) 1996 TooLs GmbH.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by TooLs GmbH.
17  * 4. The name of TooLs GmbH may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  *      $NetBSD: fpu.c,v 1.5 2001/07/22 11:29:46 wiz Exp $
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/proc.h>
39 #include <sys/systm.h>
40 #include <sys/limits.h>
41
42 #include <machine/altivec.h>
43 #include <machine/pcb.h>
44 #include <machine/psl.h>
45
46 void
47 enable_vec(struct thread *td)
48 {
49         int     msr;
50         struct  pcb *pcb;
51         struct  trapframe *tf;
52
53         pcb = td->td_pcb;
54         tf = trapframe(td);
55
56         /*
57          * Save the thread's Altivec CPU number, and set the CPU's current
58          * vector thread
59          */
60         td->td_pcb->pcb_veccpu = PCPU_GET(cpuid);
61         PCPU_SET(vecthread, td);
62
63         /*
64          * Enable the vector unit for when the thread returns from the
65          * exception. If this is the first time the unit has been used by
66          * the thread, initialise the vector registers and VSCR to 0, and
67          * set the flag to indicate that the vector unit is in use.
68          */
69         tf->srr1 |= PSL_VEC;
70         if (!(pcb->pcb_flags & PCB_VEC)) {
71                 memset(&pcb->pcb_vec, 0, sizeof pcb->pcb_vec);
72                 pcb->pcb_flags |= PCB_VEC;
73         }
74
75         /*
76          * Temporarily enable the vector unit so the registers
77          * can be restored.
78          */
79         msr = mfmsr();
80         mtmsr(msr | PSL_VEC);
81         isync();
82
83         /*
84          * Restore VSCR by first loading it into a vector and then into VSCR.
85          * (this needs to done before loading the user's vector registers
86          * since we need to use a scratch vector register)
87          */
88         __asm __volatile("vxor 0,0,0; lvewx 0,0,%0; mtvscr 0" \
89                           :: "b"(&pcb->pcb_vec.vscr));
90
91 #define LVX(n)   __asm ("lvx " #n ",0,%0" \
92                 :: "b"(&pcb->pcb_vec.vr[n]));
93         LVX(0);         LVX(1);         LVX(2);         LVX(3);
94         LVX(4);         LVX(5);         LVX(6);         LVX(7);
95         LVX(8);         LVX(9);         LVX(10);        LVX(11);
96         LVX(12);        LVX(13);        LVX(14);        LVX(15);
97         LVX(16);        LVX(17);        LVX(18);        LVX(19);
98         LVX(20);        LVX(21);        LVX(22);        LVX(23);
99         LVX(24);        LVX(25);        LVX(26);        LVX(27);
100         LVX(28);        LVX(29);        LVX(30);        LVX(31);
101 #undef LVX
102
103         isync();
104         mtmsr(msr);
105 }
106
107 void
108 save_vec(struct thread *td)
109 {
110         int     msr;
111         struct  pcb *pcb;
112
113         pcb = td->td_pcb;
114
115         /*
116          * Temporarily re-enable the vector unit during the save
117          */
118         msr = mfmsr();
119         mtmsr(msr | PSL_VEC);
120         isync();
121
122         /*
123          * Save the vector registers and VSCR to the PCB
124          */
125 #define STVX(n)   __asm ("stvx %1,0,%0" \
126                 :: "b"(pcb->pcb_vec.vr[n]), "n"(n));
127         STVX(0);        STVX(1);        STVX(2);        STVX(3);
128         STVX(4);        STVX(5);        STVX(6);        STVX(7);
129         STVX(8);        STVX(9);        STVX(10);       STVX(11);
130         STVX(12);       STVX(13);       STVX(14);       STVX(15);
131         STVX(16);       STVX(17);       STVX(18);       STVX(19);
132         STVX(20);       STVX(21);       STVX(22);       STVX(23);
133         STVX(24);       STVX(25);       STVX(26);       STVX(27);
134         STVX(28);       STVX(29);       STVX(30);       STVX(31);
135 #undef STVX
136
137         __asm __volatile("mfvscr 0; stvewx 0,0,%0" :: "b"(&pcb->pcb_vec.vscr));
138
139         /*
140          * Disable vector unit again
141          */
142         isync();
143         mtmsr(msr);
144
145         /*
146          * Clear the current vec thread and pcb's CPU id
147          * XXX should this be left clear to allow lazy save/restore ?
148          */
149         pcb->pcb_veccpu = INT_MAX;
150         PCPU_SET(vecthread, NULL);
151 }
152