18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Format of an instruction in memory.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
68c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
78c2ecf20Sopenharmony_ci * for more details.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyright (C) 2020 Loongson Technology Co., Ltd.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci#ifndef _ASM_INST_H
128c2ecf20Sopenharmony_ci#define _ASM_INST_H
138c2ecf20Sopenharmony_ci#include <linux/types.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <asm/asm.h>
168c2ecf20Sopenharmony_ci#include <asm/errno.h>
178c2ecf20Sopenharmony_ci#include <asm/ptrace.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <uapi/asm/inst.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/* HACHACHAHCAHC ...  */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* In case some other massaging is needed, keep LOONGARCHInst as wrapper */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define LOONGARCHInst(x) x
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define I_OPCODE_SFT	26
288c2ecf20Sopenharmony_ci#define LOONGARCHInst_OPCODE(x) (LOONGARCHInst(x) >> I_OPCODE_SFT)
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define I_JTARGET_SFT	0
318c2ecf20Sopenharmony_ci#define LOONGARCHInst_JTARGET(x) (LOONGARCHInst(x) & 0x03ffffff)
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define I_RS_SFT	21
348c2ecf20Sopenharmony_ci#define LOONGARCHInst_RS(x) ((LOONGARCHInst(x) & 0x03e00000) >> I_RS_SFT)
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define I_RT_SFT	16
378c2ecf20Sopenharmony_ci#define LOONGARCHInst_RT(x) ((LOONGARCHInst(x) & 0x001f0000) >> I_RT_SFT)
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define I_IMM_SFT	0
408c2ecf20Sopenharmony_ci#define LOONGARCHInst_SIMM(x) ((int)((short)(LOONGARCHInst(x) & 0xffff)))
418c2ecf20Sopenharmony_ci#define LOONGARCHInst_UIMM(x) (LOONGARCHInst(x) & 0xffff)
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define I_CACHEOP_SFT	18
448c2ecf20Sopenharmony_ci#define LOONGARCHInst_CACHEOP(x) ((LOONGARCHInst(x) & 0x001c0000) >> I_CACHEOP_SFT)
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define I_CACHESEL_SFT	16
478c2ecf20Sopenharmony_ci#define LOONGARCHInst_CACHESEL(x) ((LOONGARCHInst(x) & 0x00030000) >> I_CACHESEL_SFT)
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define I_RD_SFT	11
508c2ecf20Sopenharmony_ci#define LOONGARCHInst_RD(x) ((LOONGARCHInst(x) & 0x0000f800) >> I_RD_SFT)
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define I_RE_SFT	6
538c2ecf20Sopenharmony_ci#define LOONGARCHInst_RE(x) ((LOONGARCHInst(x) & 0x000007c0) >> I_RE_SFT)
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#define I_FUNC_SFT	0
568c2ecf20Sopenharmony_ci#define LOONGARCHInst_FUNC(x) (LOONGARCHInst(x) & 0x0000003f)
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define I_FFMT_SFT	21
598c2ecf20Sopenharmony_ci#define LOONGARCHInst_FFMT(x) ((LOONGARCHInst(x) & 0x01e00000) >> I_FFMT_SFT)
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define I_FT_SFT	16
628c2ecf20Sopenharmony_ci#define LOONGARCHInst_FT(x) ((LOONGARCHInst(x) & 0x001f0000) >> I_FT_SFT)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define I_FS_SFT	11
658c2ecf20Sopenharmony_ci#define LOONGARCHInst_FS(x) ((LOONGARCHInst(x) & 0x0000f800) >> I_FS_SFT)
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#define I_FD_SFT	6
688c2ecf20Sopenharmony_ci#define LOONGARCHInst_FD(x) ((LOONGARCHInst(x) & 0x000007c0) >> I_FD_SFT)
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#define I_FR_SFT	21
718c2ecf20Sopenharmony_ci#define LOONGARCHInst_FR(x) ((LOONGARCHInst(x) & 0x03e00000) >> I_FR_SFT)
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define I_FMA_FUNC_SFT	2
748c2ecf20Sopenharmony_ci#define LOONGARCHInst_FMA_FUNC(x) ((LOONGARCHInst(x) & 0x0000003c) >> I_FMA_FUNC_SFT)
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci#define I_FMA_FFMT_SFT	0
778c2ecf20Sopenharmony_ci#define LOONGARCHInst_FMA_FFMT(x) (LOONGARCHInst(x) & 0x00000003)
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistruct pt_regs;
808c2ecf20Sopenharmony_citypedef unsigned int loongarch_instruction;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/* Recode table from 16-bit register notation to 32-bit GPR. Do NOT export!!! */
838c2ecf20Sopenharmony_ciextern const int reg16to32[];
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#define LOONGARCH_INSN_SIZE	sizeof(union loongarch_instruction)
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cienum loongarch_gpr {
888c2ecf20Sopenharmony_ci	LOONGARCH_GPR_ZERO = 0,
898c2ecf20Sopenharmony_ci	LOONGARCH_GPR_RA = 1,
908c2ecf20Sopenharmony_ci	LOONGARCH_GPR_TP = 2,
918c2ecf20Sopenharmony_ci	LOONGARCH_GPR_SP = 3,
928c2ecf20Sopenharmony_ci	LOONGARCH_GPR_A0 = 4,	/* Reused as V0 for return value */
938c2ecf20Sopenharmony_ci	LOONGARCH_GPR_A1,	/* Reused as V1 for return value */
948c2ecf20Sopenharmony_ci	LOONGARCH_GPR_A2,
958c2ecf20Sopenharmony_ci	LOONGARCH_GPR_A3,
968c2ecf20Sopenharmony_ci	LOONGARCH_GPR_A4,
978c2ecf20Sopenharmony_ci	LOONGARCH_GPR_A5,
988c2ecf20Sopenharmony_ci	LOONGARCH_GPR_A6,
998c2ecf20Sopenharmony_ci	LOONGARCH_GPR_A7,
1008c2ecf20Sopenharmony_ci	LOONGARCH_GPR_T0 = 12,
1018c2ecf20Sopenharmony_ci	LOONGARCH_GPR_T1,
1028c2ecf20Sopenharmony_ci	LOONGARCH_GPR_T2,
1038c2ecf20Sopenharmony_ci	LOONGARCH_GPR_T3,
1048c2ecf20Sopenharmony_ci	LOONGARCH_GPR_T4,
1058c2ecf20Sopenharmony_ci	LOONGARCH_GPR_T5,
1068c2ecf20Sopenharmony_ci	LOONGARCH_GPR_T6,
1078c2ecf20Sopenharmony_ci	LOONGARCH_GPR_T7,
1088c2ecf20Sopenharmony_ci	LOONGARCH_GPR_T8,
1098c2ecf20Sopenharmony_ci	LOONGARCH_GPR_FP = 22,
1108c2ecf20Sopenharmony_ci	LOONGARCH_GPR_S0 = 23,
1118c2ecf20Sopenharmony_ci	LOONGARCH_GPR_S1,
1128c2ecf20Sopenharmony_ci	LOONGARCH_GPR_S2,
1138c2ecf20Sopenharmony_ci	LOONGARCH_GPR_S3,
1148c2ecf20Sopenharmony_ci	LOONGARCH_GPR_S4,
1158c2ecf20Sopenharmony_ci	LOONGARCH_GPR_S5,
1168c2ecf20Sopenharmony_ci	LOONGARCH_GPR_S6,
1178c2ecf20Sopenharmony_ci	LOONGARCH_GPR_S7,
1188c2ecf20Sopenharmony_ci	LOONGARCH_GPR_S8,
1198c2ecf20Sopenharmony_ci	LOONGARCH_GPR_MAX
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci#define INSN_NOP 0x03400000
1238c2ecf20Sopenharmony_ci#define INSN_BREAK 0x002a0000
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci#define ADDR_IMMMASK_ADDU16ID	0x00000000FFFF0000
1268c2ecf20Sopenharmony_ci#define ADDR_IMMMASK_LU32ID	0x000FFFFF00000000
1278c2ecf20Sopenharmony_ci#define ADDR_IMMMASK_LU52ID	0xFFF0000000000000
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci#define ADDR_IMMSHIFT_ADDU16ID	16
1308c2ecf20Sopenharmony_ci#define ADDR_IMMSHIFT_LU32ID	32
1318c2ecf20Sopenharmony_ci#define ADDR_IMMSHIFT_LU52ID	52
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci#define ADDR_IMM(addr, INSN)	((addr & ADDR_IMMMASK_##INSN) >> ADDR_IMMSHIFT_##INSN)
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_civoid emulate_load_store_insn(struct pt_regs *regs, void __user *addr, unsigned int *pc);
1368c2ecf20Sopenharmony_ciunsigned long unaligned_read(void __user *addr, void *value, unsigned long n, bool sign);
1378c2ecf20Sopenharmony_ciunsigned long unaligned_write(void __user *addr, unsigned long value, unsigned long n);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic inline bool cond_beqz(struct pt_regs *regs, int rj)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	return regs->regs[rj] == 0;
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic inline bool cond_bnez(struct pt_regs *regs, int rj)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	return regs->regs[rj] != 0;
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic inline bool cond_beq(struct pt_regs *regs, int rj, int rd)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	return regs->regs[rj] == regs->regs[rd];
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic inline bool cond_bne(struct pt_regs *regs, int rj, int rd)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	return regs->regs[rj] != regs->regs[rd];
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic inline bool cond_blt(struct pt_regs *regs, int rj, int rd)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	return (long)regs->regs[rj] < (long)regs->regs[rd];
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic inline bool cond_bge(struct pt_regs *regs, int rj, int rd)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	return (long)regs->regs[rj] >= (long)regs->regs[rd];
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic inline bool cond_bltu(struct pt_regs *regs, int rj, int rd)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	return regs->regs[rj] < regs->regs[rd];
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic inline bool cond_bgeu(struct pt_regs *regs, int rj, int rd)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	return regs->regs[rj] >= regs->regs[rd];
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic inline bool is_branch_insn(union loongarch_instruction insn)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	return insn.reg1i21_format.opcode >= beqz_op &&
1828c2ecf20Sopenharmony_ci			insn.reg1i21_format.opcode <= bgeu_op;
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic inline bool is_pc_insn(union loongarch_instruction insn)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	return insn.reg1i20_format.opcode >= pcaddi_op &&
1888c2ecf20Sopenharmony_ci			insn.reg1i20_format.opcode <= pcaddu18i_op;
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ciunsigned long bs_dest_16(unsigned long now, unsigned int si);
1928c2ecf20Sopenharmony_ciunsigned long bs_dest_21(unsigned long now, unsigned int h, unsigned int l);
1938c2ecf20Sopenharmony_ciunsigned long bs_dest_26(unsigned long now, unsigned int h, unsigned int l);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ciint simu_branch(struct pt_regs *regs, union loongarch_instruction insn);
1968c2ecf20Sopenharmony_ciint simu_pc(struct pt_regs *regs, union loongarch_instruction insn);
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ciint larch_insn_read(void *addr, u32 *insnp);
1998c2ecf20Sopenharmony_ciint larch_insn_write(void *addr, u32 insn);
2008c2ecf20Sopenharmony_ciint larch_insn_patch_text(void *addr, u32 insn);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ciu32 larch_insn_gen_nop(void);
2038c2ecf20Sopenharmony_ciu32 larch_insn_gen_b(unsigned long pc, unsigned long dest);
2048c2ecf20Sopenharmony_ciu32 larch_insn_gen_bl(unsigned long pc, unsigned long dest);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ciu32 larch_insn_gen_addu16id(enum loongarch_gpr rd, enum loongarch_gpr rj, int imm);
2078c2ecf20Sopenharmony_ciu32 larch_insn_gen_lu32id(enum loongarch_gpr rd, int imm);
2088c2ecf20Sopenharmony_ciu32 larch_insn_gen_lu52id(enum loongarch_gpr rd, enum loongarch_gpr rj, int imm);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ciu32 larch_insn_gen_jirl(enum loongarch_gpr rd, enum loongarch_gpr rj,
2118c2ecf20Sopenharmony_ci			unsigned long pc, unsigned long dest);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ciu32 larch_insn_gen_or(enum loongarch_gpr rd, enum loongarch_gpr rj,
2148c2ecf20Sopenharmony_ci			enum loongarch_gpr rk);
2158c2ecf20Sopenharmony_ciu32 larch_insn_gen_move(enum loongarch_gpr rd, enum loongarch_gpr rj);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci#endif /* _ASM_INST_H */
218