18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * arch/sh/math-emu/math.c
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2006 Takashi YOSHII <takasi-y@ops.dti.ne.jp>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
78c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
88c2ecf20Sopenharmony_ci * for more details.
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/errno.h>
128c2ecf20Sopenharmony_ci#include <linux/types.h>
138c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
148c2ecf20Sopenharmony_ci#include <linux/signal.h>
158c2ecf20Sopenharmony_ci#include <linux/perf_event.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
188c2ecf20Sopenharmony_ci#include <asm/processor.h>
198c2ecf20Sopenharmony_ci#include <asm/io.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#include "sfp-util.h"
228c2ecf20Sopenharmony_ci#include <math-emu/soft-fp.h>
238c2ecf20Sopenharmony_ci#include <math-emu/single.h>
248c2ecf20Sopenharmony_ci#include <math-emu/double.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define	FPUL		(fregs->fpul)
278c2ecf20Sopenharmony_ci#define FPSCR		(fregs->fpscr)
288c2ecf20Sopenharmony_ci#define FPSCR_RM	(FPSCR&3)
298c2ecf20Sopenharmony_ci#define FPSCR_DN	((FPSCR>>18)&1)
308c2ecf20Sopenharmony_ci#define FPSCR_PR	((FPSCR>>19)&1)
318c2ecf20Sopenharmony_ci#define FPSCR_SZ	((FPSCR>>20)&1)
328c2ecf20Sopenharmony_ci#define FPSCR_FR	((FPSCR>>21)&1)
338c2ecf20Sopenharmony_ci#define FPSCR_MASK	0x003fffffUL
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define BANK(n)	(n^(FPSCR_FR?16:0))
368c2ecf20Sopenharmony_ci#define FR	((unsigned long*)(fregs->fp_regs))
378c2ecf20Sopenharmony_ci#define FR0	(FR[BANK(0)])
388c2ecf20Sopenharmony_ci#define FRn	(FR[BANK(n)])
398c2ecf20Sopenharmony_ci#define FRm	(FR[BANK(m)])
408c2ecf20Sopenharmony_ci#define DR	((unsigned long long*)(fregs->fp_regs))
418c2ecf20Sopenharmony_ci#define DRn	(DR[BANK(n)/2])
428c2ecf20Sopenharmony_ci#define DRm	(DR[BANK(m)/2])
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define XREG(n)	(n^16)
458c2ecf20Sopenharmony_ci#define XFn	(FR[BANK(XREG(n))])
468c2ecf20Sopenharmony_ci#define XFm	(FR[BANK(XREG(m))])
478c2ecf20Sopenharmony_ci#define XDn	(DR[BANK(XREG(n))/2])
488c2ecf20Sopenharmony_ci#define XDm	(DR[BANK(XREG(m))/2])
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define R0	(regs->regs[0])
518c2ecf20Sopenharmony_ci#define Rn	(regs->regs[n])
528c2ecf20Sopenharmony_ci#define Rm	(regs->regs[m])
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define WRITE(d,a)	({if(put_user(d, (typeof (d)*)a)) return -EFAULT;})
558c2ecf20Sopenharmony_ci#define READ(d,a)	({if(get_user(d, (typeof (d)*)a)) return -EFAULT;})
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#define PACK_S(r,f)	FP_PACK_SP(&r,f)
588c2ecf20Sopenharmony_ci#define UNPACK_S(f,r)	FP_UNPACK_SP(f,&r)
598c2ecf20Sopenharmony_ci#define PACK_D(r,f) \
608c2ecf20Sopenharmony_ci	{u32 t[2]; FP_PACK_DP(t,f); ((u32*)&r)[0]=t[1]; ((u32*)&r)[1]=t[0];}
618c2ecf20Sopenharmony_ci#define UNPACK_D(f,r) \
628c2ecf20Sopenharmony_ci	{u32 t[2]; t[0]=((u32*)&r)[1]; t[1]=((u32*)&r)[0]; FP_UNPACK_DP(f,t);}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci// 2 args instructions.
658c2ecf20Sopenharmony_ci#define BOTH_PRmn(op,x) \
668c2ecf20Sopenharmony_ci	FP_DECL_EX; if(FPSCR_PR) op(D,x,DRm,DRn); else op(S,x,FRm,FRn);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define CMP_X(SZ,R,M,N) do{ \
698c2ecf20Sopenharmony_ci	FP_DECL_##SZ(Fm); FP_DECL_##SZ(Fn); \
708c2ecf20Sopenharmony_ci	UNPACK_##SZ(Fm, M); UNPACK_##SZ(Fn, N); \
718c2ecf20Sopenharmony_ci	FP_CMP_##SZ(R, Fn, Fm, 2); }while(0)
728c2ecf20Sopenharmony_ci#define EQ_X(SZ,R,M,N) do{ \
738c2ecf20Sopenharmony_ci	FP_DECL_##SZ(Fm); FP_DECL_##SZ(Fn); \
748c2ecf20Sopenharmony_ci	UNPACK_##SZ(Fm, M); UNPACK_##SZ(Fn, N); \
758c2ecf20Sopenharmony_ci	FP_CMP_EQ_##SZ(R, Fn, Fm); }while(0)
768c2ecf20Sopenharmony_ci#define CMP(OP) ({ int r; BOTH_PRmn(OP##_X,r); r; })
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic int
798c2ecf20Sopenharmony_cifcmp_gt(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	if (CMP(CMP) > 0)
828c2ecf20Sopenharmony_ci		regs->sr |= 1;
838c2ecf20Sopenharmony_ci	else
848c2ecf20Sopenharmony_ci		regs->sr &= ~1;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	return 0;
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic int
908c2ecf20Sopenharmony_cifcmp_eq(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	if (CMP(CMP /*EQ*/) == 0)
938c2ecf20Sopenharmony_ci		regs->sr |= 1;
948c2ecf20Sopenharmony_ci	else
958c2ecf20Sopenharmony_ci		regs->sr &= ~1;
968c2ecf20Sopenharmony_ci	return 0;
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#define ARITH_X(SZ,OP,M,N) do{ \
1008c2ecf20Sopenharmony_ci	FP_DECL_##SZ(Fm); FP_DECL_##SZ(Fn); FP_DECL_##SZ(Fr); \
1018c2ecf20Sopenharmony_ci	UNPACK_##SZ(Fm, M); UNPACK_##SZ(Fn, N); \
1028c2ecf20Sopenharmony_ci	FP_##OP##_##SZ(Fr, Fn, Fm); \
1038c2ecf20Sopenharmony_ci	PACK_##SZ(N, Fr); }while(0)
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic int
1068c2ecf20Sopenharmony_cifadd(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	BOTH_PRmn(ARITH_X, ADD);
1098c2ecf20Sopenharmony_ci	return 0;
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic int
1138c2ecf20Sopenharmony_cifsub(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	BOTH_PRmn(ARITH_X, SUB);
1168c2ecf20Sopenharmony_ci	return 0;
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic int
1208c2ecf20Sopenharmony_cifmul(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	BOTH_PRmn(ARITH_X, MUL);
1238c2ecf20Sopenharmony_ci	return 0;
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic int
1278c2ecf20Sopenharmony_cifdiv(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	BOTH_PRmn(ARITH_X, DIV);
1308c2ecf20Sopenharmony_ci	return 0;
1318c2ecf20Sopenharmony_ci}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic int
1348c2ecf20Sopenharmony_cifmac(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	FP_DECL_EX;
1378c2ecf20Sopenharmony_ci	FP_DECL_S(Fr);
1388c2ecf20Sopenharmony_ci	FP_DECL_S(Ft);
1398c2ecf20Sopenharmony_ci	FP_DECL_S(F0);
1408c2ecf20Sopenharmony_ci	FP_DECL_S(Fm);
1418c2ecf20Sopenharmony_ci	FP_DECL_S(Fn);
1428c2ecf20Sopenharmony_ci	UNPACK_S(F0, FR0);
1438c2ecf20Sopenharmony_ci	UNPACK_S(Fm, FRm);
1448c2ecf20Sopenharmony_ci	UNPACK_S(Fn, FRn);
1458c2ecf20Sopenharmony_ci	FP_MUL_S(Ft, Fm, F0);
1468c2ecf20Sopenharmony_ci	FP_ADD_S(Fr, Fn, Ft);
1478c2ecf20Sopenharmony_ci	PACK_S(FRn, Fr);
1488c2ecf20Sopenharmony_ci	return 0;
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci// to process fmov's extension (odd n for DR access XD).
1528c2ecf20Sopenharmony_ci#define FMOV_EXT(x) if(x&1) x+=16-1
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic int
1558c2ecf20Sopenharmony_cifmov_idx_reg(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
1568c2ecf20Sopenharmony_ci	     int n)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	if (FPSCR_SZ) {
1598c2ecf20Sopenharmony_ci		FMOV_EXT(n);
1608c2ecf20Sopenharmony_ci		READ(FRn, Rm + R0 + 4);
1618c2ecf20Sopenharmony_ci		n++;
1628c2ecf20Sopenharmony_ci		READ(FRn, Rm + R0);
1638c2ecf20Sopenharmony_ci	} else {
1648c2ecf20Sopenharmony_ci		READ(FRn, Rm + R0);
1658c2ecf20Sopenharmony_ci	}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	return 0;
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic int
1718c2ecf20Sopenharmony_cifmov_mem_reg(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
1728c2ecf20Sopenharmony_ci	     int n)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	if (FPSCR_SZ) {
1758c2ecf20Sopenharmony_ci		FMOV_EXT(n);
1768c2ecf20Sopenharmony_ci		READ(FRn, Rm + 4);
1778c2ecf20Sopenharmony_ci		n++;
1788c2ecf20Sopenharmony_ci		READ(FRn, Rm);
1798c2ecf20Sopenharmony_ci	} else {
1808c2ecf20Sopenharmony_ci		READ(FRn, Rm);
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	return 0;
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic int
1878c2ecf20Sopenharmony_cifmov_inc_reg(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
1888c2ecf20Sopenharmony_ci	     int n)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	if (FPSCR_SZ) {
1918c2ecf20Sopenharmony_ci		FMOV_EXT(n);
1928c2ecf20Sopenharmony_ci		READ(FRn, Rm + 4);
1938c2ecf20Sopenharmony_ci		n++;
1948c2ecf20Sopenharmony_ci		READ(FRn, Rm);
1958c2ecf20Sopenharmony_ci		Rm += 8;
1968c2ecf20Sopenharmony_ci	} else {
1978c2ecf20Sopenharmony_ci		READ(FRn, Rm);
1988c2ecf20Sopenharmony_ci		Rm += 4;
1998c2ecf20Sopenharmony_ci	}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	return 0;
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistatic int
2058c2ecf20Sopenharmony_cifmov_reg_idx(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
2068c2ecf20Sopenharmony_ci	     int n)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	if (FPSCR_SZ) {
2098c2ecf20Sopenharmony_ci		FMOV_EXT(m);
2108c2ecf20Sopenharmony_ci		WRITE(FRm, Rn + R0 + 4);
2118c2ecf20Sopenharmony_ci		m++;
2128c2ecf20Sopenharmony_ci		WRITE(FRm, Rn + R0);
2138c2ecf20Sopenharmony_ci	} else {
2148c2ecf20Sopenharmony_ci		WRITE(FRm, Rn + R0);
2158c2ecf20Sopenharmony_ci	}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	return 0;
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistatic int
2218c2ecf20Sopenharmony_cifmov_reg_mem(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
2228c2ecf20Sopenharmony_ci	     int n)
2238c2ecf20Sopenharmony_ci{
2248c2ecf20Sopenharmony_ci	if (FPSCR_SZ) {
2258c2ecf20Sopenharmony_ci		FMOV_EXT(m);
2268c2ecf20Sopenharmony_ci		WRITE(FRm, Rn + 4);
2278c2ecf20Sopenharmony_ci		m++;
2288c2ecf20Sopenharmony_ci		WRITE(FRm, Rn);
2298c2ecf20Sopenharmony_ci	} else {
2308c2ecf20Sopenharmony_ci		WRITE(FRm, Rn);
2318c2ecf20Sopenharmony_ci	}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	return 0;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic int
2378c2ecf20Sopenharmony_cifmov_reg_dec(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
2388c2ecf20Sopenharmony_ci	     int n)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	if (FPSCR_SZ) {
2418c2ecf20Sopenharmony_ci		FMOV_EXT(m);
2428c2ecf20Sopenharmony_ci		Rn -= 8;
2438c2ecf20Sopenharmony_ci		WRITE(FRm, Rn + 4);
2448c2ecf20Sopenharmony_ci		m++;
2458c2ecf20Sopenharmony_ci		WRITE(FRm, Rn);
2468c2ecf20Sopenharmony_ci	} else {
2478c2ecf20Sopenharmony_ci		Rn -= 4;
2488c2ecf20Sopenharmony_ci		WRITE(FRm, Rn);
2498c2ecf20Sopenharmony_ci	}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	return 0;
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cistatic int
2558c2ecf20Sopenharmony_cifmov_reg_reg(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m,
2568c2ecf20Sopenharmony_ci	     int n)
2578c2ecf20Sopenharmony_ci{
2588c2ecf20Sopenharmony_ci	if (FPSCR_SZ) {
2598c2ecf20Sopenharmony_ci		FMOV_EXT(m);
2608c2ecf20Sopenharmony_ci		FMOV_EXT(n);
2618c2ecf20Sopenharmony_ci		DRn = DRm;
2628c2ecf20Sopenharmony_ci	} else {
2638c2ecf20Sopenharmony_ci		FRn = FRm;
2648c2ecf20Sopenharmony_ci	}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	return 0;
2678c2ecf20Sopenharmony_ci}
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cistatic int
2708c2ecf20Sopenharmony_cifnop_mn(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int m, int n)
2718c2ecf20Sopenharmony_ci{
2728c2ecf20Sopenharmony_ci	return -EINVAL;
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci// 1 arg instructions.
2768c2ecf20Sopenharmony_ci#define NOTYETn(i) static int i(struct sh_fpu_soft_struct *fregs, int n) \
2778c2ecf20Sopenharmony_ci	{ printk( #i " not yet done.\n"); return 0; }
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ciNOTYETn(ftrv)
2808c2ecf20Sopenharmony_ciNOTYETn(fsqrt)
2818c2ecf20Sopenharmony_ciNOTYETn(fipr)
2828c2ecf20Sopenharmony_ciNOTYETn(fsca)
2838c2ecf20Sopenharmony_ciNOTYETn(fsrra)
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci#define EMU_FLOAT_X(SZ,N) do { \
2868c2ecf20Sopenharmony_ci	FP_DECL_##SZ(Fn); \
2878c2ecf20Sopenharmony_ci	FP_FROM_INT_##SZ(Fn, FPUL, 32, int); \
2888c2ecf20Sopenharmony_ci	PACK_##SZ(N, Fn); }while(0)
2898c2ecf20Sopenharmony_cistatic int ffloat(struct sh_fpu_soft_struct *fregs, int n)
2908c2ecf20Sopenharmony_ci{
2918c2ecf20Sopenharmony_ci	FP_DECL_EX;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	if (FPSCR_PR)
2948c2ecf20Sopenharmony_ci		EMU_FLOAT_X(D, DRn);
2958c2ecf20Sopenharmony_ci	else
2968c2ecf20Sopenharmony_ci		EMU_FLOAT_X(S, FRn);
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	return 0;
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci#define EMU_FTRC_X(SZ,N) do { \
3028c2ecf20Sopenharmony_ci	FP_DECL_##SZ(Fn); \
3038c2ecf20Sopenharmony_ci	UNPACK_##SZ(Fn, N); \
3048c2ecf20Sopenharmony_ci	FP_TO_INT_##SZ(FPUL, Fn, 32, 1); }while(0)
3058c2ecf20Sopenharmony_cistatic int ftrc(struct sh_fpu_soft_struct *fregs, int n)
3068c2ecf20Sopenharmony_ci{
3078c2ecf20Sopenharmony_ci	FP_DECL_EX;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	if (FPSCR_PR)
3108c2ecf20Sopenharmony_ci		EMU_FTRC_X(D, DRn);
3118c2ecf20Sopenharmony_ci	else
3128c2ecf20Sopenharmony_ci		EMU_FTRC_X(S, FRn);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	return 0;
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic int fcnvsd(struct sh_fpu_soft_struct *fregs, int n)
3188c2ecf20Sopenharmony_ci{
3198c2ecf20Sopenharmony_ci	FP_DECL_EX;
3208c2ecf20Sopenharmony_ci	FP_DECL_S(Fn);
3218c2ecf20Sopenharmony_ci	FP_DECL_D(Fr);
3228c2ecf20Sopenharmony_ci	UNPACK_S(Fn, FPUL);
3238c2ecf20Sopenharmony_ci	FP_CONV(D, S, 2, 1, Fr, Fn);
3248c2ecf20Sopenharmony_ci	PACK_D(DRn, Fr);
3258c2ecf20Sopenharmony_ci	return 0;
3268c2ecf20Sopenharmony_ci}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_cistatic int fcnvds(struct sh_fpu_soft_struct *fregs, int n)
3298c2ecf20Sopenharmony_ci{
3308c2ecf20Sopenharmony_ci	FP_DECL_EX;
3318c2ecf20Sopenharmony_ci	FP_DECL_D(Fn);
3328c2ecf20Sopenharmony_ci	FP_DECL_S(Fr);
3338c2ecf20Sopenharmony_ci	UNPACK_D(Fn, DRn);
3348c2ecf20Sopenharmony_ci	FP_CONV(S, D, 1, 2, Fr, Fn);
3358c2ecf20Sopenharmony_ci	PACK_S(FPUL, Fr);
3368c2ecf20Sopenharmony_ci	return 0;
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistatic int fxchg(struct sh_fpu_soft_struct *fregs, int flag)
3408c2ecf20Sopenharmony_ci{
3418c2ecf20Sopenharmony_ci	FPSCR ^= flag;
3428c2ecf20Sopenharmony_ci	return 0;
3438c2ecf20Sopenharmony_ci}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic int fsts(struct sh_fpu_soft_struct *fregs, int n)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci	FRn = FPUL;
3488c2ecf20Sopenharmony_ci	return 0;
3498c2ecf20Sopenharmony_ci}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_cistatic int flds(struct sh_fpu_soft_struct *fregs, int n)
3528c2ecf20Sopenharmony_ci{
3538c2ecf20Sopenharmony_ci	FPUL = FRn;
3548c2ecf20Sopenharmony_ci	return 0;
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_cistatic int fneg(struct sh_fpu_soft_struct *fregs, int n)
3588c2ecf20Sopenharmony_ci{
3598c2ecf20Sopenharmony_ci	FRn ^= (1 << (_FP_W_TYPE_SIZE - 1));
3608c2ecf20Sopenharmony_ci	return 0;
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic int fabs(struct sh_fpu_soft_struct *fregs, int n)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	FRn &= ~(1 << (_FP_W_TYPE_SIZE - 1));
3668c2ecf20Sopenharmony_ci	return 0;
3678c2ecf20Sopenharmony_ci}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_cistatic int fld0(struct sh_fpu_soft_struct *fregs, int n)
3708c2ecf20Sopenharmony_ci{
3718c2ecf20Sopenharmony_ci	FRn = 0;
3728c2ecf20Sopenharmony_ci	return 0;
3738c2ecf20Sopenharmony_ci}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_cistatic int fld1(struct sh_fpu_soft_struct *fregs, int n)
3768c2ecf20Sopenharmony_ci{
3778c2ecf20Sopenharmony_ci	FRn = (_FP_EXPBIAS_S << (_FP_FRACBITS_S - 1));
3788c2ecf20Sopenharmony_ci	return 0;
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_cistatic int fnop_n(struct sh_fpu_soft_struct *fregs, int n)
3828c2ecf20Sopenharmony_ci{
3838c2ecf20Sopenharmony_ci	return -EINVAL;
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci/// Instruction decoders.
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_cistatic int id_fxfd(struct sh_fpu_soft_struct *, int);
3898c2ecf20Sopenharmony_cistatic int id_fnxd(struct sh_fpu_soft_struct *, struct pt_regs *, int, int);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic int (*fnxd[])(struct sh_fpu_soft_struct *, int) = {
3928c2ecf20Sopenharmony_ci	fsts, flds, ffloat, ftrc, fneg, fabs, fsqrt, fsrra,
3938c2ecf20Sopenharmony_ci	fld0, fld1, fcnvsd, fcnvds, fnop_n, fnop_n, fipr, id_fxfd
3948c2ecf20Sopenharmony_ci};
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_cistatic int (*fnmx[])(struct sh_fpu_soft_struct *, struct pt_regs *, int, int) = {
3978c2ecf20Sopenharmony_ci	fadd, fsub, fmul, fdiv, fcmp_eq, fcmp_gt, fmov_idx_reg, fmov_reg_idx,
3988c2ecf20Sopenharmony_ci	fmov_mem_reg, fmov_inc_reg, fmov_reg_mem, fmov_reg_dec,
3998c2ecf20Sopenharmony_ci	fmov_reg_reg, id_fnxd, fmac, fnop_mn};
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_cistatic int id_fxfd(struct sh_fpu_soft_struct *fregs, int x)
4028c2ecf20Sopenharmony_ci{
4038c2ecf20Sopenharmony_ci	const int flag[] = { FPSCR_SZ, FPSCR_PR, FPSCR_FR, 0 };
4048c2ecf20Sopenharmony_ci	switch (x & 3) {
4058c2ecf20Sopenharmony_ci	case 3:
4068c2ecf20Sopenharmony_ci		fxchg(fregs, flag[x >> 2]);
4078c2ecf20Sopenharmony_ci		break;
4088c2ecf20Sopenharmony_ci	case 1:
4098c2ecf20Sopenharmony_ci		ftrv(fregs, x - 1);
4108c2ecf20Sopenharmony_ci		break;
4118c2ecf20Sopenharmony_ci	default:
4128c2ecf20Sopenharmony_ci		fsca(fregs, x);
4138c2ecf20Sopenharmony_ci	}
4148c2ecf20Sopenharmony_ci	return 0;
4158c2ecf20Sopenharmony_ci}
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_cistatic int
4188c2ecf20Sopenharmony_ciid_fnxd(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, int x, int n)
4198c2ecf20Sopenharmony_ci{
4208c2ecf20Sopenharmony_ci	return (fnxd[x])(fregs, n);
4218c2ecf20Sopenharmony_ci}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_cistatic int
4248c2ecf20Sopenharmony_ciid_fnmx(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, u16 code)
4258c2ecf20Sopenharmony_ci{
4268c2ecf20Sopenharmony_ci	int n = (code >> 8) & 0xf, m = (code >> 4) & 0xf, x = code & 0xf;
4278c2ecf20Sopenharmony_ci	return (fnmx[x])(fregs, regs, m, n);
4288c2ecf20Sopenharmony_ci}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_cistatic int
4318c2ecf20Sopenharmony_ciid_sys(struct sh_fpu_soft_struct *fregs, struct pt_regs *regs, u16 code)
4328c2ecf20Sopenharmony_ci{
4338c2ecf20Sopenharmony_ci	int n = ((code >> 8) & 0xf);
4348c2ecf20Sopenharmony_ci	unsigned long *reg = (code & 0x0010) ? &FPUL : &FPSCR;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	switch (code & 0xf0ff) {
4378c2ecf20Sopenharmony_ci	case 0x005a:
4388c2ecf20Sopenharmony_ci	case 0x006a:
4398c2ecf20Sopenharmony_ci		Rn = *reg;
4408c2ecf20Sopenharmony_ci		break;
4418c2ecf20Sopenharmony_ci	case 0x405a:
4428c2ecf20Sopenharmony_ci	case 0x406a:
4438c2ecf20Sopenharmony_ci		*reg = Rn;
4448c2ecf20Sopenharmony_ci		break;
4458c2ecf20Sopenharmony_ci	case 0x4052:
4468c2ecf20Sopenharmony_ci	case 0x4062:
4478c2ecf20Sopenharmony_ci		Rn -= 4;
4488c2ecf20Sopenharmony_ci		WRITE(*reg, Rn);
4498c2ecf20Sopenharmony_ci		break;
4508c2ecf20Sopenharmony_ci	case 0x4056:
4518c2ecf20Sopenharmony_ci	case 0x4066:
4528c2ecf20Sopenharmony_ci		READ(*reg, Rn);
4538c2ecf20Sopenharmony_ci		Rn += 4;
4548c2ecf20Sopenharmony_ci		break;
4558c2ecf20Sopenharmony_ci	default:
4568c2ecf20Sopenharmony_ci		return -EINVAL;
4578c2ecf20Sopenharmony_ci	}
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	return 0;
4608c2ecf20Sopenharmony_ci}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_cistatic int fpu_emulate(u16 code, struct sh_fpu_soft_struct *fregs, struct pt_regs *regs)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	if ((code & 0xf000) == 0xf000)
4658c2ecf20Sopenharmony_ci		return id_fnmx(fregs, regs, code);
4668c2ecf20Sopenharmony_ci	else
4678c2ecf20Sopenharmony_ci		return id_sys(fregs, regs, code);
4688c2ecf20Sopenharmony_ci}
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci/**
4718c2ecf20Sopenharmony_ci * fpu_init - Initialize FPU registers
4728c2ecf20Sopenharmony_ci * @fpu: Pointer to software emulated FPU registers.
4738c2ecf20Sopenharmony_ci */
4748c2ecf20Sopenharmony_cistatic void fpu_init(struct sh_fpu_soft_struct *fpu)
4758c2ecf20Sopenharmony_ci{
4768c2ecf20Sopenharmony_ci	int i;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	fpu->fpscr = FPSCR_INIT;
4798c2ecf20Sopenharmony_ci	fpu->fpul = 0;
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	for (i = 0; i < 16; i++) {
4828c2ecf20Sopenharmony_ci		fpu->fp_regs[i] = 0;
4838c2ecf20Sopenharmony_ci		fpu->xfp_regs[i]= 0;
4848c2ecf20Sopenharmony_ci	}
4858c2ecf20Sopenharmony_ci}
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci/**
4888c2ecf20Sopenharmony_ci * do_fpu_inst - Handle reserved instructions for FPU emulation
4898c2ecf20Sopenharmony_ci * @inst: instruction code.
4908c2ecf20Sopenharmony_ci * @regs: registers on stack.
4918c2ecf20Sopenharmony_ci */
4928c2ecf20Sopenharmony_ciint do_fpu_inst(unsigned short inst, struct pt_regs *regs)
4938c2ecf20Sopenharmony_ci{
4948c2ecf20Sopenharmony_ci	struct task_struct *tsk = current;
4958c2ecf20Sopenharmony_ci	struct sh_fpu_soft_struct *fpu = &(tsk->thread.xstate->softfpu);
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, 0);
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	if (!(task_thread_info(tsk)->status & TS_USEDFPU)) {
5008c2ecf20Sopenharmony_ci		/* initialize once. */
5018c2ecf20Sopenharmony_ci		fpu_init(fpu);
5028c2ecf20Sopenharmony_ci		task_thread_info(tsk)->status |= TS_USEDFPU;
5038c2ecf20Sopenharmony_ci	}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	return fpu_emulate(inst, fpu, regs);
5068c2ecf20Sopenharmony_ci}
507