1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <asm/processor.h>
3 #include <asm/ppc_asm.h>
5 #include <asm/asm-offsets.h>
6 #include <asm/cputable.h>
7 #include <asm/thread_info.h>
9 #include <asm/ptrace.h>
10 #include <asm/export.h>
13 * Load state from memory into VMX registers including VSCR.
14 * Assumes the caller has enabled VMX in the MSR.
16 _GLOBAL(load_vr_state)
22 EXPORT_SYMBOL(load_vr_state)
25 * Store VMX state into memory, including VSCR.
26 * Assumes the caller has enabled VMX in the MSR.
28 _GLOBAL(store_vr_state)
34 EXPORT_SYMBOL(store_vr_state)
37 * Disable VMX for the task which had it previously,
38 * and save its vector registers in its thread_struct.
39 * Enables the VMX for use in the kernel on return.
40 * On SMP we know the VMX is free, since we give it up every
41 * switch (ie, no lazy save of the vector registers).
43 * Note that on 32-bit this can only use registers that will be
44 * restored by fast_exception_return, i.e. r3 - r6, r10 and r11.
46 _GLOBAL(load_up_altivec)
47 mfmsr r5 /* grab the current MSR */
49 MTMSRD(r5) /* enable use of AltiVec now */
53 * While userspace in general ignores VRSAVE, glibc uses it as a boolean
54 * to optimise userspace context save/restore. Whenever we take an
55 * altivec unavailable exception we must set VRSAVE to something non
56 * zero. Set it to all 1s. See also the programming note in the ISA.
64 /* enable use of VMX after return */
66 mfspr r5,SPRN_SPRG_THREAD /* current task's THREAD (phys) */
69 ld r4,PACACURRENT(r13)
70 addi r5,r4,THREAD /* Get THREAD */
71 oris r12,r12,MSR_VEC@h
74 /* Don't care if r4 overflows, this is desired behaviour */
75 lbz r4,THREAD_LOAD_VEC(r5)
77 stb r4,THREAD_LOAD_VEC(r5)
78 addi r6,r5,THREAD_VRSTATE
81 stw r4,THREAD_USED_VR(r5)
85 /* restore registers and return */
90 * Save the vector registers to its thread_struct
93 addi r3,r3,THREAD /* want THREAD of task */
94 PPC_LL r7,THREAD_VRSAVEAREA(r3)
98 addi r7,r3,THREAD_VRSTATE
99 2: SAVE_32VRS(0,r4,r7)
108 #error This asm code isn't ready for 32-bit kernels
112 * load_up_vsx(unused, unused, tsk)
113 * Disable VSX for the task which had it previously,
114 * and save its vector registers in its thread_struct.
115 * Reuse the fp and vsx saves, but first check to see if they have
116 * been saved already.
119 /* Load FP and VSX registers if they haven't been done yet */
121 beql+ load_up_fpu /* skip if already loaded */
122 andis. r5,r12,MSR_VEC@h
123 beql+ load_up_altivec /* skip if already loaded */
125 ld r4,PACACURRENT(r13)
126 addi r4,r4,THREAD /* Get THREAD */
128 stw r6,THREAD_USED_VSR(r4) /* ... also set thread used vsr */
129 /* enable use of VSX after return */
130 oris r12,r12,MSR_VSX@h
132 b fast_exception_return
134 #endif /* CONFIG_VSX */
138 * The routines below are in assembler so we can closely control the
139 * usage of floating-point registers. These routines must be called
140 * with preempt disabled.
147 .long 0x3f800000 /* 1.0 in single-precision FP */
149 .long 0x3f000000 /* 0.5 in single-precision FP */
151 #define LDCONST(fr, name) \
160 .tc FD_3ff00000_0[TC],0x3ff0000000000000 /* 1.0 */
162 .tc FD_3fe00000_0[TC],0x3fe0000000000000 /* 0.5 */
164 #define LDCONST(fr, name) \
170 * Internal routine to enable floating point and set FPSCR to 0.
171 * Don't call it from C; it doesn't use the normal calling convention.
203 * Vector add, floating point.
220 * Vector subtract, floating point.
237 * Vector multiply and add, floating point.
249 fmadds fr0,fr0,fr2,fr1
257 * Vector negative multiply and subtract, floating point.
269 fnmsubs fr0,fr0,fr2,fr1
277 * Vector reciprocal estimate. We just compute 1.0/x.
278 * r3 -> destination, r4 -> source.
295 * Vector reciprocal square-root estimate, floating point.
296 * We use the frsqrte instruction for the initial estimate followed
297 * by 2 iterations of Newton-Raphson to get sufficient accuracy.
298 * r3 -> destination, r4 -> source.
313 frsqrte fr1,fr0 /* r = frsqrte(s) */
314 fmuls fr3,fr1,fr0 /* r * s */
315 fmuls fr2,fr1,fr5 /* r * 0.5 */
316 fnmsubs fr3,fr1,fr3,fr4 /* 1 - s * r * r */
317 fmadds fr1,fr2,fr3,fr1 /* r = r + 0.5 * r * (1 - s * r * r) */
318 fmuls fr3,fr1,fr0 /* r * s */
319 fmuls fr2,fr1,fr5 /* r * 0.5 */
320 fnmsubs fr3,fr1,fr3,fr4 /* 1 - s * r * r */
321 fmadds fr1,fr2,fr3,fr1 /* r = r + 0.5 * r * (1 - s * r * r) */