]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/powerpc/fpu.c
Merge lldb trunk r321017 to contrib/llvm/tools/lldb.
[FreeBSD/FreeBSD.git] / sys / powerpc / powerpc / fpu.c
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (C) 1996 Wolfgang Solfrank.
5  * Copyright (C) 1996 TooLs GmbH.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by TooLs GmbH.
19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *      $NetBSD: fpu.c,v 1.5 2001/07/22 11:29:46 wiz Exp $
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include <sys/param.h>
40 #include <sys/proc.h>
41 #include <sys/systm.h>
42 #include <sys/limits.h>
43
44 #include <machine/fpu.h>
45 #include <machine/pcb.h>
46 #include <machine/psl.h>
47
48 void
49 enable_fpu(struct thread *td)
50 {
51         int     msr;
52         struct  pcb *pcb;
53         struct  trapframe *tf;
54
55         pcb = td->td_pcb;
56         tf = trapframe(td);
57
58         /*
59          * Save the thread's FPU CPU number, and set the CPU's current
60          * FPU thread
61          */
62         td->td_pcb->pcb_fpcpu = PCPU_GET(cpuid);
63         PCPU_SET(fputhread, td);
64
65         /*
66          * Enable the FPU for when the thread returns from the exception.
67          * If this is the first time the FPU has been used by the thread,
68          * initialise the FPU registers and FPSCR to 0, and set the flag
69          * to indicate that the FPU is in use.
70          */
71         pcb->pcb_flags |= PCB_FPU;
72         if (pcb->pcb_flags & PCB_VSX)
73                 tf->srr1 |= PSL_FP | PSL_VSX;
74         else
75                 tf->srr1 |= PSL_FP;
76         if (!(pcb->pcb_flags & PCB_FPREGS)) {
77                 memset(&pcb->pcb_fpu, 0, sizeof pcb->pcb_fpu);
78                 pcb->pcb_flags |= PCB_FPREGS;
79         }
80
81         /*
82          * Temporarily enable floating-point so the registers
83          * can be restored.
84          */
85         msr = mfmsr();
86         if (pcb->pcb_flags & PCB_VSX)
87                 mtmsr(msr | PSL_FP | PSL_VSX);
88         else
89                 mtmsr(msr | PSL_FP);
90         isync();
91
92         /*
93          * Load the floating point registers and FPSCR from the PCB.
94          * (A value of 0xff for mtfsf specifies that all 8 4-bit fields
95          * of the saved FPSCR are to be loaded from the FPU reg).
96          */
97         __asm __volatile ("lfd 0,0(%0); mtfsf 0xff,0"
98                           :: "b"(&pcb->pcb_fpu.fpscr));
99
100         if (pcb->pcb_flags & PCB_VSX) {
101         #define LFP(n)   __asm ("lxvw4x " #n ", 0,%0" \
102                         :: "b"(&pcb->pcb_fpu.fpr[n]));
103                 LFP(0);         LFP(1);         LFP(2);         LFP(3);
104                 LFP(4);         LFP(5);         LFP(6);         LFP(7);
105                 LFP(8);         LFP(9);         LFP(10);        LFP(11);
106                 LFP(12);        LFP(13);        LFP(14);        LFP(15);
107                 LFP(16);        LFP(17);        LFP(18);        LFP(19);
108                 LFP(20);        LFP(21);        LFP(22);        LFP(23);
109                 LFP(24);        LFP(25);        LFP(26);        LFP(27);
110                 LFP(28);        LFP(29);        LFP(30);        LFP(31);
111         #undef LFP
112         } else {
113         #define LFP(n)   __asm ("lfd " #n ", 0(%0)" \
114                         :: "b"(&pcb->pcb_fpu.fpr[n]));
115                 LFP(0);         LFP(1);         LFP(2);         LFP(3);
116                 LFP(4);         LFP(5);         LFP(6);         LFP(7);
117                 LFP(8);         LFP(9);         LFP(10);        LFP(11);
118                 LFP(12);        LFP(13);        LFP(14);        LFP(15);
119                 LFP(16);        LFP(17);        LFP(18);        LFP(19);
120                 LFP(20);        LFP(21);        LFP(22);        LFP(23);
121                 LFP(24);        LFP(25);        LFP(26);        LFP(27);
122                 LFP(28);        LFP(29);        LFP(30);        LFP(31);
123         #undef LFP
124         }
125
126         isync();
127         mtmsr(msr);
128 }
129
130 void
131 save_fpu(struct thread *td)
132 {
133         int     msr;
134         struct  pcb *pcb;
135
136         pcb = td->td_pcb;
137
138         /*
139          * Temporarily re-enable floating-point during the save
140          */
141         msr = mfmsr();
142         if (pcb->pcb_flags & PCB_VSX)
143                 mtmsr(msr | PSL_FP | PSL_VSX);
144         else
145                 mtmsr(msr | PSL_FP);
146         isync();
147
148         /*
149          * Save the floating-point registers and FPSCR to the PCB
150          */
151         if (pcb->pcb_flags & PCB_VSX) {
152         #define SFP(n)   __asm ("stxvw4x " #n ", 0,%0" \
153                         :: "b"(&pcb->pcb_fpu.fpr[n]));
154                 SFP(0);         SFP(1);         SFP(2);         SFP(3);
155                 SFP(4);         SFP(5);         SFP(6);         SFP(7);
156                 SFP(8);         SFP(9);         SFP(10);        SFP(11);
157                 SFP(12);        SFP(13);        SFP(14);        SFP(15);
158                 SFP(16);        SFP(17);        SFP(18);        SFP(19);
159                 SFP(20);        SFP(21);        SFP(22);        SFP(23);
160                 SFP(24);        SFP(25);        SFP(26);        SFP(27);
161                 SFP(28);        SFP(29);        SFP(30);        SFP(31);
162         #undef SFP
163         } else {
164         #define SFP(n)   __asm ("stfd " #n ", 0(%0)" \
165                         :: "b"(&pcb->pcb_fpu.fpr[n]));
166                 SFP(0);         SFP(1);         SFP(2);         SFP(3);
167                 SFP(4);         SFP(5);         SFP(6);         SFP(7);
168                 SFP(8);         SFP(9);         SFP(10);        SFP(11);
169                 SFP(12);        SFP(13);        SFP(14);        SFP(15);
170                 SFP(16);        SFP(17);        SFP(18);        SFP(19);
171                 SFP(20);        SFP(21);        SFP(22);        SFP(23);
172                 SFP(24);        SFP(25);        SFP(26);        SFP(27);
173                 SFP(28);        SFP(29);        SFP(30);        SFP(31);
174         #undef SFP
175         }
176         __asm __volatile ("mffs 0; stfd 0,0(%0)" :: "b"(&pcb->pcb_fpu.fpscr));
177
178         /*
179          * Disable floating-point again
180          */
181         isync();
182         mtmsr(msr);
183
184         /*
185          * Clear the current fp thread and pcb's CPU id
186          * XXX should this be left clear to allow lazy save/restore ?
187          */
188         pcb->pcb_fpcpu = INT_MAX;
189         PCPU_SET(fputhread, NULL);
190 }
191