1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <linux/linkage.h>
3 #include <asm/processor.h>
4 #include <asm/ppc_asm.h>
6 #include <asm/asm-offsets.h>
7 #include <asm/cputable.h>
8 #include <asm/thread_info.h>
10 #include <asm/ptrace.h>
11 #include <asm/export.h>
12 #include <asm/asm-compat.h>
15 * Load state from memory into VMX registers including VSCR.
16 * Assumes the caller has enabled VMX in the MSR.
18 _GLOBAL(load_vr_state)
24 EXPORT_SYMBOL(load_vr_state)
25 _ASM_NOKPROBE_SYMBOL(load_vr_state); /* used by restore_math */
28 * Store VMX state into memory, including VSCR.
29 * Assumes the caller has enabled VMX in the MSR.
31 _GLOBAL(store_vr_state)
37 EXPORT_SYMBOL(store_vr_state)
40 * Disable VMX for the task which had it previously,
41 * and save its vector registers in its thread_struct.
42 * Enables the VMX for use in the kernel on return.
43 * On SMP we know the VMX is free, since we give it up every
44 * switch (ie, no lazy save of the vector registers).
46 * Note that on 32-bit this can only use registers that will be
47 * restored by fast_exception_return, i.e. r3 - r6, r10 and r11.
49 _GLOBAL(load_up_altivec)
50 mfmsr r5 /* grab the current MSR */
51 #ifdef CONFIG_PPC_BOOK3S_64
52 /* interrupt doesn't set MSR[RI] and HPT can fault on current access */
56 MTMSRD(r5) /* enable use of AltiVec now */
60 * While userspace in general ignores VRSAVE, glibc uses it as a boolean
61 * to optimise userspace context save/restore. Whenever we take an
62 * altivec unavailable exception we must set VRSAVE to something non
63 * zero. Set it to all 1s. See also the programming note in the ISA.
71 /* enable use of VMX after return */
76 ld r4,PACACURRENT(r13)
77 addi r5,r4,THREAD /* Get THREAD */
78 oris r12,r12,MSR_VEC@h
80 #ifdef CONFIG_PPC_BOOK3S_64
82 stb r4,PACASRR_VALID(r13)
86 stb r4,THREAD_LOAD_VEC(r5)
87 addi r6,r5,THREAD_VRSTATE
89 stw r4,THREAD_USED_VR(r5)
93 /* restore registers and return */
95 _ASM_NOKPROBE_SYMBOL(load_up_altivec)
99 * Save the vector registers to its thread_struct
101 _GLOBAL(save_altivec)
102 addi r3,r3,THREAD /* want THREAD of task */
103 PPC_LL r7,THREAD_VRSAVEAREA(r3)
104 PPC_LL r5,PT_REGS(r3)
107 addi r7,r3,THREAD_VRSTATE
108 2: SAVE_32VRS(0,r4,r7)
117 #error This asm code isn't ready for 32-bit kernels
121 * load_up_vsx(unused, unused, tsk)
122 * Disable VSX for the task which had it previously,
123 * and save its vector registers in its thread_struct.
124 * Reuse the fp and vsx saves, but first check to see if they have
125 * been saved already.
128 /* Load FP and VSX registers if they haven't been done yet */
130 beql+ load_up_fpu /* skip if already loaded */
131 andis. r5,r12,MSR_VEC@h
132 beql+ load_up_altivec /* skip if already loaded */
134 #ifdef CONFIG_PPC_BOOK3S_64
135 /* interrupt doesn't set MSR[RI] and HPT can fault on current access */
140 ld r4,PACACURRENT(r13)
141 addi r4,r4,THREAD /* Get THREAD */
143 stw r6,THREAD_USED_VSR(r4) /* ... also set thread used vsr */
144 /* enable use of VSX after return */
145 oris r12,r12,MSR_VSX@h
148 stb r4,PACASRR_VALID(r13)
149 b fast_interrupt_return_srr
151 #endif /* CONFIG_VSX */
155 * The routines below are in assembler so we can closely control the
156 * usage of floating-point registers. These routines must be called
157 * with preempt disabled.
164 .long 0x3f800000 /* 1.0 in single-precision FP */
166 .long 0x3f000000 /* 0.5 in single-precision FP */
168 #define LDCONST(fr, name) \
176 .quad 0x3ff0000000000000 /* 1.0 */
178 .quad 0x3fe0000000000000 /* 0.5 */
180 #define LDCONST(fr, name) \
181 addis r11,r2,name@toc@ha; \
182 lfd fr,name@toc@l(r11)
186 * Internal routine to enable floating point and set FPSCR to 0.
187 * Don't call it from C; it doesn't use the normal calling convention.
189 SYM_FUNC_START_LOCAL(fpenable)
206 SYM_FUNC_END(fpenable)
220 * Vector add, floating point.
237 * Vector subtract, floating point.
254 * Vector multiply and add, floating point.
266 fmadds fr0,fr0,fr2,fr1
274 * Vector negative multiply and subtract, floating point.
286 fnmsubs fr0,fr0,fr2,fr1
294 * Vector reciprocal estimate. We just compute 1.0/x.
295 * r3 -> destination, r4 -> source.
312 * Vector reciprocal square-root estimate, floating point.
313 * We use the frsqrte instruction for the initial estimate followed
314 * by 2 iterations of Newton-Raphson to get sufficient accuracy.
315 * r3 -> destination, r4 -> source.
330 frsqrte fr1,fr0 /* r = frsqrte(s) */
331 fmuls fr3,fr1,fr0 /* r * s */
332 fmuls fr2,fr1,fr5 /* r * 0.5 */
333 fnmsubs fr3,fr1,fr3,fr4 /* 1 - s * r * r */
334 fmadds fr1,fr2,fr3,fr1 /* r = r + 0.5 * r * (1 - s * r * r) */
335 fmuls fr3,fr1,fr0 /* r * s */
336 fmuls fr2,fr1,fr5 /* r * 0.5 */
337 fnmsubs fr3,fr1,fr3,fr4 /* 1 - s * r * r */
338 fmadds fr1,fr2,fr3,fr1 /* r = r + 0.5 * r * (1 - s * r * r) */