^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Copyright (C) 2005-2018 Andes Technology Corporation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #ifndef __ASM_NDS32_FPU_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #define __ASM_NDS32_FPU_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #if IS_ENABLED(CONFIG_FPU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #ifndef __ASSEMBLY__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/sched/task_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/preempt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <asm/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) extern bool has_fpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) extern void save_fpu(struct task_struct *__tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) extern void load_fpu(const struct fpu_struct *fpregs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) extern bool do_fpu_exception(unsigned int subtype, struct pt_regs *regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) extern int do_fpuemu(struct pt_regs *regs, struct fpu_struct *fpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define test_tsk_fpu(regs) (regs->fucop_ctl & FUCOP_CTL_mskCP0EN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Initially load the FPU with signalling NANS. This bit pattern
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * has the property that no matter whether considered as single or as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * double precision, it still represents a signalling NAN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define sNAN64 0xFFFFFFFFFFFFFFFFULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define sNAN32 0xFFFFFFFFUL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #if IS_ENABLED(CONFIG_SUPPORT_DENORMAL_ARITHMETIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Denormalized number is unsupported by nds32 FPU. Hence the operation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * is treated as underflow cases when the final result is a denormalized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * number. To enhance precision, underflow exception trap should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * enabled by default and kerenl will re-execute it by fpu emulator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * when getting underflow exception.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define FPCSR_INIT (FPCSR_mskUDFE | FPCSR_mskIEXE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define FPCSR_INIT 0x0UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) extern const struct fpu_struct init_fpuregs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static inline void disable_ptreg_fpu(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) regs->fucop_ctl &= ~FUCOP_CTL_mskCP0EN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static inline void enable_ptreg_fpu(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) regs->fucop_ctl |= FUCOP_CTL_mskCP0EN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static inline void enable_fpu(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) unsigned long fucop_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) fucop_ctl = __nds32__mfsr(NDS32_SR_FUCOP_CTL) | FUCOP_CTL_mskCP0EN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) __nds32__mtsr(fucop_ctl, NDS32_SR_FUCOP_CTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) __nds32__isb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static inline void disable_fpu(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) unsigned long fucop_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) fucop_ctl = __nds32__mfsr(NDS32_SR_FUCOP_CTL) & ~FUCOP_CTL_mskCP0EN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) __nds32__mtsr(fucop_ctl, NDS32_SR_FUCOP_CTL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) __nds32__isb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static inline void lose_fpu(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #if IS_ENABLED(CONFIG_LAZY_FPU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (last_task_used_math == current) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) last_task_used_math = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (test_tsk_fpu(task_pt_regs(current))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) save_fpu(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) disable_ptreg_fpu(task_pt_regs(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static inline void own_fpu(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #if IS_ENABLED(CONFIG_LAZY_FPU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (last_task_used_math != current) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (last_task_used_math != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) save_fpu(last_task_used_math);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) load_fpu(¤t->thread.fpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) last_task_used_math = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (!test_tsk_fpu(task_pt_regs(current))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) load_fpu(¤t->thread.fpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) enable_ptreg_fpu(task_pt_regs(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #if !IS_ENABLED(CONFIG_LAZY_FPU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static inline void unlazy_fpu(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (test_tsk_fpu(task_pt_regs(tsk)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) save_fpu(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) #endif /* !CONFIG_LAZY_FPU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static inline void clear_fpu(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (test_tsk_fpu(regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) disable_ptreg_fpu(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) #endif /* CONFIG_FPU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #endif /* __ASSEMBLY__ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #endif /* __ASM_NDS32_FPU_H */