1 | /*
|
---|
2 | * Copyright (c) 2012 Jan Vesely
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - 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 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup arm32
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief arm32 FPU context
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <fpu_context.h>
|
---|
37 | #include <arch.h>
|
---|
38 | #include <cpu.h>
|
---|
39 |
|
---|
40 | #define FPSID_IMPLEMENTER(r) ((r) >> 24)
|
---|
41 | #define FPSID_SW_ONLY_FLAG (1 << 23)
|
---|
42 | #define FPSID_SUBACHITECTURE(r) (((r) >> 16) & 0x7f)
|
---|
43 | #define FPSID_PART_NUMBER(r) (((r) >> 8) & 0xff)
|
---|
44 | #define FPSID_VARIANT(r) (((r) >> 4) 0xf)
|
---|
45 | #define FPSID_REVISION(r) (((r) >> 0) 0xf)
|
---|
46 | enum {
|
---|
47 | FPU_VFPv1 = 0x00,
|
---|
48 | FPU_VFPv2_COMMONv1 = 0x01,
|
---|
49 | FPU_VFPv3_COMMONv2 = 0x02,
|
---|
50 | FPU_VFPv3_NO_COMMON = 0x3, /* Does not support trap */
|
---|
51 | FPU_VFPv3_COMMONv3 = 0x4,
|
---|
52 | };
|
---|
53 |
|
---|
54 | enum {
|
---|
55 | FPEXC_ENABLED_FLAG = 0x40000000,
|
---|
56 | FPEXC_EX_FLAG = 0x80000000,
|
---|
57 | };
|
---|
58 |
|
---|
59 | static inline uint32_t fpexc_read()
|
---|
60 | {
|
---|
61 | uint32_t reg;
|
---|
62 | asm volatile (
|
---|
63 | "vmrs %0, fpexc\n"
|
---|
64 | :"=r" (reg)::
|
---|
65 | );
|
---|
66 | return reg;
|
---|
67 | }
|
---|
68 |
|
---|
69 | static inline void fpexc_write(uint32_t val)
|
---|
70 | {
|
---|
71 | asm volatile (
|
---|
72 | "vmsr fpexc, %0\n"
|
---|
73 | ::"r" (val):
|
---|
74 | );
|
---|
75 | }
|
---|
76 |
|
---|
77 | static void (*save_context)(fpu_context_t *ctx);
|
---|
78 | static void (*restore_context)(fpu_context_t *ctx);
|
---|
79 |
|
---|
80 | /** Saves 32 single precision fpu registers.
|
---|
81 | * @param ctx FPU context area.
|
---|
82 | * Used by VFPv1
|
---|
83 | */
|
---|
84 | static void fpu_context_save_s32(fpu_context_t *ctx)
|
---|
85 | {
|
---|
86 | asm volatile (
|
---|
87 | "vmrs r1, fpexc\n"
|
---|
88 | "stmia %0!, {r1}\n"
|
---|
89 | "vmrs r1, fpscr\n"
|
---|
90 | "stmia %0!, {r1}\n"
|
---|
91 | "vstmia %0!, {s0-s31}\n"
|
---|
92 | ::"r" (ctx): "r1","memory"
|
---|
93 | );
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Restores 32 single precision fpu registers.
|
---|
97 | * @param ctx FPU context area.
|
---|
98 | * Used by VFPv1
|
---|
99 | */
|
---|
100 | static void fpu_context_restore_s32(fpu_context_t *ctx)
|
---|
101 | {
|
---|
102 | asm volatile (
|
---|
103 | "ldmia %0!, {r1}\n"
|
---|
104 | "vmsr fpexc, r1\n"
|
---|
105 | "ldmia %0!, {r1}\n"
|
---|
106 | "vmsr fpscr, r1\n"
|
---|
107 | "vldmia %0!, {s0-s31}\n"
|
---|
108 | ::"r" (ctx): "r1"
|
---|
109 | );
|
---|
110 | }
|
---|
111 |
|
---|
112 | /** Saves 16 double precision fpu registers.
|
---|
113 | * @param ctx FPU context area.
|
---|
114 | * Used by VFPv2, VFPv3-d16, and VFPv4-d16.
|
---|
115 | */
|
---|
116 | static void fpu_context_save_d16(fpu_context_t *ctx)
|
---|
117 | {
|
---|
118 | asm volatile (
|
---|
119 | "vmrs r1, fpexc\n"
|
---|
120 | "stmia %0!, {r1}\n"
|
---|
121 | "vmrs r1, fpscr\n"
|
---|
122 | "stmia %0!, {r1}\n"
|
---|
123 | "vstmia %0!, {d0-d15}\n"
|
---|
124 | ::"r" (ctx): "r1","memory"
|
---|
125 | );
|
---|
126 | }
|
---|
127 |
|
---|
128 | /** Restores 16 double precision fpu registers.
|
---|
129 | * @param ctx FPU context area.
|
---|
130 | * Used by VFPv2, VFPv3-d16, and VFPv4-d16.
|
---|
131 | */
|
---|
132 | static void fpu_context_restore_d16(fpu_context_t *ctx)
|
---|
133 | {
|
---|
134 | asm volatile (
|
---|
135 | "ldmia %0!, {r1}\n"
|
---|
136 | "vmsr fpexc, r1\n"
|
---|
137 | "ldmia %0!, {r1}\n"
|
---|
138 | "vmsr fpscr, r1\n"
|
---|
139 | "vldmia %0!, {d0-d15}\n"
|
---|
140 | ::"r" (ctx): "r1"
|
---|
141 | );
|
---|
142 | }
|
---|
143 |
|
---|
144 | /** Saves 32 double precision fpu registers.
|
---|
145 | * @param ctx FPU context area.
|
---|
146 | * Used by VFPv3-d32, VFPv4-d32, and advanced SIMD.
|
---|
147 | */
|
---|
148 | static void fpu_context_save_d32(fpu_context_t *ctx)
|
---|
149 | {
|
---|
150 | asm volatile (
|
---|
151 | "vmrs r1, fpexc\n"
|
---|
152 | "stmia %0!, {r1}\n"
|
---|
153 | "vmrs r1, fpscr\n"
|
---|
154 | "stmia %0!, {r1}\n"
|
---|
155 | "vstmia %0!, {d0-d15}\n"
|
---|
156 | "vstmia %0!, {d16-d31}\n"
|
---|
157 | ::"r" (ctx): "r1","memory"
|
---|
158 | );
|
---|
159 | }
|
---|
160 |
|
---|
161 | /** Restores 32 double precision fpu registers.
|
---|
162 | * @param ctx FPU context area.
|
---|
163 | * Used by VFPv3-d32, VFPv4-d32, and advanced SIMD.
|
---|
164 | */
|
---|
165 | static void fpu_context_restore_d32(fpu_context_t *ctx)
|
---|
166 | {
|
---|
167 | asm volatile (
|
---|
168 | "ldmia %0!, {r1}\n"
|
---|
169 | "vmsr fpexc, r1\n"
|
---|
170 | "ldmia %0!, {r1}\n"
|
---|
171 | "vmsr fpscr, r1\n"
|
---|
172 | "vldmia %0!, {d0-d15}\n"
|
---|
173 | "vldmia %0!, {d16-d31}\n"
|
---|
174 | ::"r" (ctx): "r1"
|
---|
175 | );
|
---|
176 | }
|
---|
177 |
|
---|
178 | void fpu_init(void)
|
---|
179 | {
|
---|
180 | /* Clear all fpu flags */
|
---|
181 | fpexc_write(0);
|
---|
182 | fpu_enable();
|
---|
183 | }
|
---|
184 |
|
---|
185 | void fpu_setup(void)
|
---|
186 | {
|
---|
187 | uint32_t fpsid = 0;
|
---|
188 | asm volatile (
|
---|
189 | "vmrs %0, fpsid\n"
|
---|
190 | :"=r"(fpsid)::
|
---|
191 | );
|
---|
192 | if (fpsid & FPSID_SW_ONLY_FLAG) {
|
---|
193 | printf("No FPU avaiable\n");
|
---|
194 | return;
|
---|
195 | }
|
---|
196 | switch (FPSID_SUBACHITECTURE(fpsid))
|
---|
197 | {
|
---|
198 | case FPU_VFPv1:
|
---|
199 | printf("Detected VFPv1\n");
|
---|
200 | save_context = fpu_context_save_s32;
|
---|
201 | restore_context = fpu_context_restore_s32;
|
---|
202 | break;
|
---|
203 | case FPU_VFPv2_COMMONv1:
|
---|
204 | printf("Detected VFPv2\n");
|
---|
205 | save_context = fpu_context_save_d16;
|
---|
206 | restore_context = fpu_context_restore_d16;
|
---|
207 | break;
|
---|
208 | case FPU_VFPv3_COMMONv2:
|
---|
209 | case FPU_VFPv3_NO_COMMON:
|
---|
210 | case FPU_VFPv3_COMMONv3: {
|
---|
211 | uint32_t mvfr0 = 0;
|
---|
212 | asm volatile (
|
---|
213 | "vmrs %0,mvfr0\n"
|
---|
214 | :"=r"(mvfr0)::
|
---|
215 | );
|
---|
216 | /* See page B4-1637 */
|
---|
217 | if ((mvfr0 & 0xf) == 0x1) {
|
---|
218 | printf("Detected VFPv3+ with 16 regs\n");
|
---|
219 | save_context = fpu_context_save_d16;
|
---|
220 | restore_context = fpu_context_restore_d16;
|
---|
221 | } else {
|
---|
222 | printf("Detected VFPv3+ with 32 regs\n");
|
---|
223 | save_context = fpu_context_save_d32;
|
---|
224 | restore_context = fpu_context_restore_d32;
|
---|
225 | }
|
---|
226 | break;
|
---|
227 | }
|
---|
228 |
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | bool handle_if_fpu_exception(void)
|
---|
233 | {
|
---|
234 | const uint32_t fpexc = fpexc_read();
|
---|
235 | if (fpexc & FPEXC_ENABLED_FLAG) {
|
---|
236 | printf("FPU exception with FPU on\n");
|
---|
237 | return false;
|
---|
238 | }
|
---|
239 | #ifdef CONFIG_FPU_LAZY
|
---|
240 | scheduler_fpu_lazy_request();
|
---|
241 | return true;
|
---|
242 | #else
|
---|
243 | return false;
|
---|
244 | #endif
|
---|
245 | }
|
---|
246 |
|
---|
247 | void fpu_enable(void)
|
---|
248 | {
|
---|
249 | /* Enable FPU instructions */
|
---|
250 | fpexc_write(fpexc_read() | FPEXC_ENABLED_FLAG);
|
---|
251 | }
|
---|
252 |
|
---|
253 | void fpu_disable(void)
|
---|
254 | {
|
---|
255 | /* Disable FPU instructions */
|
---|
256 | fpexc_write(fpexc_read() & ~FPEXC_ENABLED_FLAG);
|
---|
257 | }
|
---|
258 |
|
---|
259 | void fpu_context_save(fpu_context_t *ctx)
|
---|
260 | {
|
---|
261 | if (save_context)
|
---|
262 | save_context(ctx);
|
---|
263 | }
|
---|
264 |
|
---|
265 | void fpu_context_restore(fpu_context_t *ctx)
|
---|
266 | {
|
---|
267 | if (restore_context)
|
---|
268 | restore_context(ctx);
|
---|
269 | }
|
---|