196de2506SJakub Kicinski // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
296de2506SJakub Kicinski /* Copyright (C) 2016-2018 Netronome Systems, Inc. */
32a15bb1aSJakub Kicinski
42a15bb1aSJakub Kicinski #include <linux/bitops.h>
52a15bb1aSJakub Kicinski #include <linux/errno.h>
62a15bb1aSJakub Kicinski #include <linux/kernel.h>
72a15bb1aSJakub Kicinski #include <linux/string.h>
82a15bb1aSJakub Kicinski #include <linux/types.h>
92a15bb1aSJakub Kicinski
102a15bb1aSJakub Kicinski #include "nfp_asm.h"
112a15bb1aSJakub Kicinski
122a15bb1aSJakub Kicinski const struct cmd_tgt_act cmd_tgt_act[__CMD_TGT_MAP_SIZE] = {
13e663fe38SJakub Kicinski [CMD_TGT_WRITE8_SWAP] = { 0x02, 0x42 },
149879a381SJiong Wang [CMD_TGT_WRITE32_SWAP] = { 0x02, 0x5f },
152a15bb1aSJakub Kicinski [CMD_TGT_READ8] = { 0x01, 0x43 },
162ca71441SJakub Kicinski [CMD_TGT_READ32] = { 0x00, 0x5c },
172ca71441SJakub Kicinski [CMD_TGT_READ32_LE] = { 0x01, 0x5c },
182ca71441SJakub Kicinski [CMD_TGT_READ32_SWAP] = { 0x02, 0x5c },
192a15bb1aSJakub Kicinski [CMD_TGT_READ_LE] = { 0x01, 0x40 },
202a15bb1aSJakub Kicinski [CMD_TGT_READ_SWAP_LE] = { 0x03, 0x40 },
2141aed09cSJakub Kicinski [CMD_TGT_ADD] = { 0x00, 0x47 },
22dcb0c27fSJakub Kicinski [CMD_TGT_ADD_IMM] = { 0x02, 0x47 },
232a15bb1aSJakub Kicinski };
242a15bb1aSJakub Kicinski
unreg_is_imm(u16 reg)25ce4ebfd8SJakub Kicinski static bool unreg_is_imm(u16 reg)
26ce4ebfd8SJakub Kicinski {
27ce4ebfd8SJakub Kicinski return (reg & UR_REG_IMM) == UR_REG_IMM;
28ce4ebfd8SJakub Kicinski }
29ce4ebfd8SJakub Kicinski
br_get_offset(u64 instr)30488feeafSJakub Kicinski u16 br_get_offset(u64 instr)
31488feeafSJakub Kicinski {
32488feeafSJakub Kicinski u16 addr_lo, addr_hi;
33488feeafSJakub Kicinski
34488feeafSJakub Kicinski addr_lo = FIELD_GET(OP_BR_ADDR_LO, instr);
35488feeafSJakub Kicinski addr_hi = FIELD_GET(OP_BR_ADDR_HI, instr);
36488feeafSJakub Kicinski
37488feeafSJakub Kicinski return (addr_hi * ((OP_BR_ADDR_LO >> __bf_shf(OP_BR_ADDR_LO)) + 1)) |
38488feeafSJakub Kicinski addr_lo;
39488feeafSJakub Kicinski }
40488feeafSJakub Kicinski
br_set_offset(u64 * instr,u16 offset)41488feeafSJakub Kicinski void br_set_offset(u64 *instr, u16 offset)
42488feeafSJakub Kicinski {
43488feeafSJakub Kicinski u16 addr_lo, addr_hi;
44488feeafSJakub Kicinski
45488feeafSJakub Kicinski addr_lo = offset & (OP_BR_ADDR_LO >> __bf_shf(OP_BR_ADDR_LO));
46488feeafSJakub Kicinski addr_hi = offset != addr_lo;
47488feeafSJakub Kicinski *instr &= ~(OP_BR_ADDR_HI | OP_BR_ADDR_LO);
48488feeafSJakub Kicinski *instr |= FIELD_PREP(OP_BR_ADDR_HI, addr_hi);
49488feeafSJakub Kicinski *instr |= FIELD_PREP(OP_BR_ADDR_LO, addr_lo);
50488feeafSJakub Kicinski }
51488feeafSJakub Kicinski
br_add_offset(u64 * instr,u16 offset)52488feeafSJakub Kicinski void br_add_offset(u64 *instr, u16 offset)
53488feeafSJakub Kicinski {
54488feeafSJakub Kicinski u16 addr;
55488feeafSJakub Kicinski
56488feeafSJakub Kicinski addr = br_get_offset(*instr);
57488feeafSJakub Kicinski br_set_offset(instr, addr + offset);
58488feeafSJakub Kicinski }
59488feeafSJakub Kicinski
immed_can_modify(u64 instr)60ce4ebfd8SJakub Kicinski static bool immed_can_modify(u64 instr)
61ce4ebfd8SJakub Kicinski {
62ce4ebfd8SJakub Kicinski if (FIELD_GET(OP_IMMED_INV, instr) ||
63ce4ebfd8SJakub Kicinski FIELD_GET(OP_IMMED_SHIFT, instr) ||
64ce4ebfd8SJakub Kicinski FIELD_GET(OP_IMMED_WIDTH, instr) != IMMED_WIDTH_ALL) {
65ce4ebfd8SJakub Kicinski pr_err("Can't decode/encode immed!\n");
66ce4ebfd8SJakub Kicinski return false;
67ce4ebfd8SJakub Kicinski }
68ce4ebfd8SJakub Kicinski return true;
69ce4ebfd8SJakub Kicinski }
70ce4ebfd8SJakub Kicinski
immed_get_value(u64 instr)71ce4ebfd8SJakub Kicinski u16 immed_get_value(u64 instr)
72ce4ebfd8SJakub Kicinski {
73ce4ebfd8SJakub Kicinski u16 reg;
74ce4ebfd8SJakub Kicinski
75ce4ebfd8SJakub Kicinski if (!immed_can_modify(instr))
76ce4ebfd8SJakub Kicinski return 0;
77ce4ebfd8SJakub Kicinski
78ce4ebfd8SJakub Kicinski reg = FIELD_GET(OP_IMMED_A_SRC, instr);
79ce4ebfd8SJakub Kicinski if (!unreg_is_imm(reg))
80ce4ebfd8SJakub Kicinski reg = FIELD_GET(OP_IMMED_B_SRC, instr);
81ce4ebfd8SJakub Kicinski
82b7d99235SJakub Kicinski return (reg & 0xff) | FIELD_GET(OP_IMMED_IMM, instr) << 8;
83ce4ebfd8SJakub Kicinski }
84ce4ebfd8SJakub Kicinski
immed_set_value(u64 * instr,u16 immed)85ce4ebfd8SJakub Kicinski void immed_set_value(u64 *instr, u16 immed)
86ce4ebfd8SJakub Kicinski {
87ce4ebfd8SJakub Kicinski if (!immed_can_modify(*instr))
88ce4ebfd8SJakub Kicinski return;
89ce4ebfd8SJakub Kicinski
90ce4ebfd8SJakub Kicinski if (unreg_is_imm(FIELD_GET(OP_IMMED_A_SRC, *instr))) {
91ce4ebfd8SJakub Kicinski *instr &= ~FIELD_PREP(OP_IMMED_A_SRC, 0xff);
92ce4ebfd8SJakub Kicinski *instr |= FIELD_PREP(OP_IMMED_A_SRC, immed & 0xff);
93ce4ebfd8SJakub Kicinski } else {
94ce4ebfd8SJakub Kicinski *instr &= ~FIELD_PREP(OP_IMMED_B_SRC, 0xff);
95ce4ebfd8SJakub Kicinski *instr |= FIELD_PREP(OP_IMMED_B_SRC, immed & 0xff);
96ce4ebfd8SJakub Kicinski }
97ce4ebfd8SJakub Kicinski
98ce4ebfd8SJakub Kicinski *instr &= ~OP_IMMED_IMM;
99ce4ebfd8SJakub Kicinski *instr |= FIELD_PREP(OP_IMMED_IMM, immed >> 8);
100ce4ebfd8SJakub Kicinski }
101ce4ebfd8SJakub Kicinski
immed_add_value(u64 * instr,u16 offset)102ce4ebfd8SJakub Kicinski void immed_add_value(u64 *instr, u16 offset)
103ce4ebfd8SJakub Kicinski {
104ce4ebfd8SJakub Kicinski u16 val;
105ce4ebfd8SJakub Kicinski
106ce4ebfd8SJakub Kicinski if (!immed_can_modify(*instr))
107ce4ebfd8SJakub Kicinski return;
108ce4ebfd8SJakub Kicinski
109ce4ebfd8SJakub Kicinski val = immed_get_value(*instr);
110ce4ebfd8SJakub Kicinski immed_set_value(instr, val + offset);
111ce4ebfd8SJakub Kicinski }
112ce4ebfd8SJakub Kicinski
nfp_swreg_to_unreg(swreg reg,bool is_dst)1132a15bb1aSJakub Kicinski static u16 nfp_swreg_to_unreg(swreg reg, bool is_dst)
1142a15bb1aSJakub Kicinski {
1159f15d0f4SJakub Kicinski bool lm_id, lm_dec = false;
1162a15bb1aSJakub Kicinski u16 val = swreg_value(reg);
1172a15bb1aSJakub Kicinski
1182a15bb1aSJakub Kicinski switch (swreg_type(reg)) {
1192a15bb1aSJakub Kicinski case NN_REG_GPR_A:
1202a15bb1aSJakub Kicinski case NN_REG_GPR_B:
1212a15bb1aSJakub Kicinski case NN_REG_GPR_BOTH:
1222a15bb1aSJakub Kicinski return val;
1232a15bb1aSJakub Kicinski case NN_REG_NNR:
1242a15bb1aSJakub Kicinski return UR_REG_NN | val;
1252a15bb1aSJakub Kicinski case NN_REG_XFER:
1262a15bb1aSJakub Kicinski return UR_REG_XFR | val;
1279f15d0f4SJakub Kicinski case NN_REG_LMEM:
1289f15d0f4SJakub Kicinski lm_id = swreg_lm_idx(reg);
1299f15d0f4SJakub Kicinski
1309f15d0f4SJakub Kicinski switch (swreg_lm_mode(reg)) {
1319f15d0f4SJakub Kicinski case NN_LM_MOD_NONE:
1329f15d0f4SJakub Kicinski if (val & ~UR_REG_LM_IDX_MAX) {
1339f15d0f4SJakub Kicinski pr_err("LM offset too large\n");
1349f15d0f4SJakub Kicinski return 0;
1359f15d0f4SJakub Kicinski }
1369f15d0f4SJakub Kicinski return UR_REG_LM | FIELD_PREP(UR_REG_LM_IDX, lm_id) |
1379f15d0f4SJakub Kicinski val;
1389f15d0f4SJakub Kicinski case NN_LM_MOD_DEC:
1399f15d0f4SJakub Kicinski lm_dec = true;
140df561f66SGustavo A. R. Silva fallthrough;
1419f15d0f4SJakub Kicinski case NN_LM_MOD_INC:
1429f15d0f4SJakub Kicinski if (val) {
1439f15d0f4SJakub Kicinski pr_err("LM offset in inc/dev mode\n");
1449f15d0f4SJakub Kicinski return 0;
1459f15d0f4SJakub Kicinski }
1469f15d0f4SJakub Kicinski return UR_REG_LM | UR_REG_LM_POST_MOD |
1479f15d0f4SJakub Kicinski FIELD_PREP(UR_REG_LM_IDX, lm_id) |
1489f15d0f4SJakub Kicinski FIELD_PREP(UR_REG_LM_POST_MOD_DEC, lm_dec);
1499f15d0f4SJakub Kicinski default:
1509f15d0f4SJakub Kicinski pr_err("bad LM mode for unrestricted operands %d\n",
1519f15d0f4SJakub Kicinski swreg_lm_mode(reg));
1529f15d0f4SJakub Kicinski return 0;
1539f15d0f4SJakub Kicinski }
1542a15bb1aSJakub Kicinski case NN_REG_IMM:
1552a15bb1aSJakub Kicinski if (val & ~0xff) {
1562a15bb1aSJakub Kicinski pr_err("immediate too large\n");
1572a15bb1aSJakub Kicinski return 0;
1582a15bb1aSJakub Kicinski }
1592a15bb1aSJakub Kicinski return UR_REG_IMM_encode(val);
1602a15bb1aSJakub Kicinski case NN_REG_NONE:
1612a15bb1aSJakub Kicinski return is_dst ? UR_REG_NO_DST : REG_NONE;
1622a15bb1aSJakub Kicinski }
1632a15bb1aSJakub Kicinski
1642a15bb1aSJakub Kicinski pr_err("unrecognized reg encoding %08x\n", reg);
1652a15bb1aSJakub Kicinski return 0;
1662a15bb1aSJakub Kicinski }
1672a15bb1aSJakub Kicinski
swreg_to_unrestricted(swreg dst,swreg lreg,swreg rreg,struct nfp_insn_ur_regs * reg)1682a15bb1aSJakub Kicinski int swreg_to_unrestricted(swreg dst, swreg lreg, swreg rreg,
1692a15bb1aSJakub Kicinski struct nfp_insn_ur_regs *reg)
1702a15bb1aSJakub Kicinski {
1712a15bb1aSJakub Kicinski memset(reg, 0, sizeof(*reg));
1722a15bb1aSJakub Kicinski
1732a15bb1aSJakub Kicinski /* Decode destination */
1742a15bb1aSJakub Kicinski if (swreg_type(dst) == NN_REG_IMM)
1752a15bb1aSJakub Kicinski return -EFAULT;
1762a15bb1aSJakub Kicinski
1772a15bb1aSJakub Kicinski if (swreg_type(dst) == NN_REG_GPR_B)
1782a15bb1aSJakub Kicinski reg->dst_ab = ALU_DST_B;
1792a15bb1aSJakub Kicinski if (swreg_type(dst) == NN_REG_GPR_BOTH)
1802a15bb1aSJakub Kicinski reg->wr_both = true;
1812a15bb1aSJakub Kicinski reg->dst = nfp_swreg_to_unreg(dst, true);
1822a15bb1aSJakub Kicinski
1832a15bb1aSJakub Kicinski /* Decode source operands */
18408859f15SJiong Wang if (swreg_type(lreg) == swreg_type(rreg) &&
18508859f15SJiong Wang swreg_type(lreg) != NN_REG_NONE)
1862a15bb1aSJakub Kicinski return -EFAULT;
1872a15bb1aSJakub Kicinski
1882a15bb1aSJakub Kicinski if (swreg_type(lreg) == NN_REG_GPR_B ||
1892a15bb1aSJakub Kicinski swreg_type(rreg) == NN_REG_GPR_A) {
1902a15bb1aSJakub Kicinski reg->areg = nfp_swreg_to_unreg(rreg, false);
1912a15bb1aSJakub Kicinski reg->breg = nfp_swreg_to_unreg(lreg, false);
1922a15bb1aSJakub Kicinski reg->swap = true;
1932a15bb1aSJakub Kicinski } else {
1942a15bb1aSJakub Kicinski reg->areg = nfp_swreg_to_unreg(lreg, false);
1952a15bb1aSJakub Kicinski reg->breg = nfp_swreg_to_unreg(rreg, false);
1962a15bb1aSJakub Kicinski }
1972a15bb1aSJakub Kicinski
198995e101fSJakub Kicinski reg->dst_lmextn = swreg_lmextn(dst);
199*8a64ef04SNathan Chancellor reg->src_lmextn = swreg_lmextn(lreg) || swreg_lmextn(rreg);
200995e101fSJakub Kicinski
2012a15bb1aSJakub Kicinski return 0;
2022a15bb1aSJakub Kicinski }
2032a15bb1aSJakub Kicinski
nfp_swreg_to_rereg(swreg reg,bool is_dst,bool has_imm8,bool * i8)2042a15bb1aSJakub Kicinski static u16 nfp_swreg_to_rereg(swreg reg, bool is_dst, bool has_imm8, bool *i8)
2052a15bb1aSJakub Kicinski {
2062a15bb1aSJakub Kicinski u16 val = swreg_value(reg);
2079f15d0f4SJakub Kicinski bool lm_id;
2082a15bb1aSJakub Kicinski
2092a15bb1aSJakub Kicinski switch (swreg_type(reg)) {
2102a15bb1aSJakub Kicinski case NN_REG_GPR_A:
2112a15bb1aSJakub Kicinski case NN_REG_GPR_B:
2122a15bb1aSJakub Kicinski case NN_REG_GPR_BOTH:
2132a15bb1aSJakub Kicinski return val;
2142a15bb1aSJakub Kicinski case NN_REG_XFER:
2152a15bb1aSJakub Kicinski return RE_REG_XFR | val;
2169f15d0f4SJakub Kicinski case NN_REG_LMEM:
2179f15d0f4SJakub Kicinski lm_id = swreg_lm_idx(reg);
2189f15d0f4SJakub Kicinski
2199f15d0f4SJakub Kicinski if (swreg_lm_mode(reg) != NN_LM_MOD_NONE) {
2209f15d0f4SJakub Kicinski pr_err("bad LM mode for restricted operands %d\n",
2219f15d0f4SJakub Kicinski swreg_lm_mode(reg));
2229f15d0f4SJakub Kicinski return 0;
2239f15d0f4SJakub Kicinski }
2249f15d0f4SJakub Kicinski
2259f15d0f4SJakub Kicinski if (val & ~RE_REG_LM_IDX_MAX) {
2269f15d0f4SJakub Kicinski pr_err("LM offset too large\n");
2279f15d0f4SJakub Kicinski return 0;
2289f15d0f4SJakub Kicinski }
2299f15d0f4SJakub Kicinski
2309f15d0f4SJakub Kicinski return RE_REG_LM | FIELD_PREP(RE_REG_LM_IDX, lm_id) | val;
2312a15bb1aSJakub Kicinski case NN_REG_IMM:
2322a15bb1aSJakub Kicinski if (val & ~(0x7f | has_imm8 << 7)) {
2332a15bb1aSJakub Kicinski pr_err("immediate too large\n");
2342a15bb1aSJakub Kicinski return 0;
2352a15bb1aSJakub Kicinski }
2362a15bb1aSJakub Kicinski *i8 = val & 0x80;
2372a15bb1aSJakub Kicinski return RE_REG_IMM_encode(val & 0x7f);
2382a15bb1aSJakub Kicinski case NN_REG_NONE:
2392a15bb1aSJakub Kicinski return is_dst ? RE_REG_NO_DST : REG_NONE;
2402a15bb1aSJakub Kicinski case NN_REG_NNR:
2412a15bb1aSJakub Kicinski pr_err("NNRs used with restricted encoding\n");
2422a15bb1aSJakub Kicinski return 0;
2432a15bb1aSJakub Kicinski }
2442a15bb1aSJakub Kicinski
2452a15bb1aSJakub Kicinski pr_err("unrecognized reg encoding\n");
2462a15bb1aSJakub Kicinski return 0;
2472a15bb1aSJakub Kicinski }
2482a15bb1aSJakub Kicinski
swreg_to_restricted(swreg dst,swreg lreg,swreg rreg,struct nfp_insn_re_regs * reg,bool has_imm8)2492a15bb1aSJakub Kicinski int swreg_to_restricted(swreg dst, swreg lreg, swreg rreg,
2502a15bb1aSJakub Kicinski struct nfp_insn_re_regs *reg, bool has_imm8)
2512a15bb1aSJakub Kicinski {
2522a15bb1aSJakub Kicinski memset(reg, 0, sizeof(*reg));
2532a15bb1aSJakub Kicinski
2542a15bb1aSJakub Kicinski /* Decode destination */
2552a15bb1aSJakub Kicinski if (swreg_type(dst) == NN_REG_IMM)
2562a15bb1aSJakub Kicinski return -EFAULT;
2572a15bb1aSJakub Kicinski
2582a15bb1aSJakub Kicinski if (swreg_type(dst) == NN_REG_GPR_B)
2592a15bb1aSJakub Kicinski reg->dst_ab = ALU_DST_B;
2602a15bb1aSJakub Kicinski if (swreg_type(dst) == NN_REG_GPR_BOTH)
2612a15bb1aSJakub Kicinski reg->wr_both = true;
2622a15bb1aSJakub Kicinski reg->dst = nfp_swreg_to_rereg(dst, true, false, NULL);
2632a15bb1aSJakub Kicinski
2642a15bb1aSJakub Kicinski /* Decode source operands */
26508859f15SJiong Wang if (swreg_type(lreg) == swreg_type(rreg) &&
26608859f15SJiong Wang swreg_type(lreg) != NN_REG_NONE)
2672a15bb1aSJakub Kicinski return -EFAULT;
2682a15bb1aSJakub Kicinski
2692a15bb1aSJakub Kicinski if (swreg_type(lreg) == NN_REG_GPR_B ||
2702a15bb1aSJakub Kicinski swreg_type(rreg) == NN_REG_GPR_A) {
2712a15bb1aSJakub Kicinski reg->areg = nfp_swreg_to_rereg(rreg, false, has_imm8, ®->i8);
2722a15bb1aSJakub Kicinski reg->breg = nfp_swreg_to_rereg(lreg, false, has_imm8, ®->i8);
2732a15bb1aSJakub Kicinski reg->swap = true;
2742a15bb1aSJakub Kicinski } else {
2752a15bb1aSJakub Kicinski reg->areg = nfp_swreg_to_rereg(lreg, false, has_imm8, ®->i8);
2762a15bb1aSJakub Kicinski reg->breg = nfp_swreg_to_rereg(rreg, false, has_imm8, ®->i8);
2772a15bb1aSJakub Kicinski }
2782a15bb1aSJakub Kicinski
279995e101fSJakub Kicinski reg->dst_lmextn = swreg_lmextn(dst);
280*8a64ef04SNathan Chancellor reg->src_lmextn = swreg_lmextn(lreg) || swreg_lmextn(rreg);
281995e101fSJakub Kicinski
2822a15bb1aSJakub Kicinski return 0;
2832a15bb1aSJakub Kicinski }
284fd068ddcSJakub Kicinski
285fd068ddcSJakub Kicinski #define NFP_USTORE_ECC_POLY_WORDS 7
286fd068ddcSJakub Kicinski #define NFP_USTORE_OP_BITS 45
287fd068ddcSJakub Kicinski
288fd068ddcSJakub Kicinski static const u64 nfp_ustore_ecc_polynomials[NFP_USTORE_ECC_POLY_WORDS] = {
289fd068ddcSJakub Kicinski 0x0ff800007fffULL,
290fd068ddcSJakub Kicinski 0x11f801ff801fULL,
291fd068ddcSJakub Kicinski 0x1e387e0781e1ULL,
292fd068ddcSJakub Kicinski 0x17cb8e388e22ULL,
293fd068ddcSJakub Kicinski 0x1af5b2c93244ULL,
294fd068ddcSJakub Kicinski 0x1f56d5525488ULL,
295fd068ddcSJakub Kicinski 0x0daf69a46910ULL,
296fd068ddcSJakub Kicinski };
297fd068ddcSJakub Kicinski
parity(u64 value)298fd068ddcSJakub Kicinski static bool parity(u64 value)
299fd068ddcSJakub Kicinski {
300fd068ddcSJakub Kicinski return hweight64(value) & 1;
301fd068ddcSJakub Kicinski }
302fd068ddcSJakub Kicinski
nfp_ustore_check_valid_no_ecc(u64 insn)303fd068ddcSJakub Kicinski int nfp_ustore_check_valid_no_ecc(u64 insn)
304fd068ddcSJakub Kicinski {
305fd068ddcSJakub Kicinski if (insn & ~GENMASK_ULL(NFP_USTORE_OP_BITS, 0))
306fd068ddcSJakub Kicinski return -EINVAL;
307fd068ddcSJakub Kicinski
308fd068ddcSJakub Kicinski return 0;
309fd068ddcSJakub Kicinski }
310fd068ddcSJakub Kicinski
nfp_ustore_calc_ecc_insn(u64 insn)311fd068ddcSJakub Kicinski u64 nfp_ustore_calc_ecc_insn(u64 insn)
312fd068ddcSJakub Kicinski {
313fd068ddcSJakub Kicinski u8 ecc = 0;
314fd068ddcSJakub Kicinski int i;
315fd068ddcSJakub Kicinski
316fd068ddcSJakub Kicinski for (i = 0; i < NFP_USTORE_ECC_POLY_WORDS; i++)
317fd068ddcSJakub Kicinski ecc |= parity(nfp_ustore_ecc_polynomials[i] & insn) << i;
318fd068ddcSJakub Kicinski
319fd068ddcSJakub Kicinski return insn | (u64)ecc << NFP_USTORE_OP_BITS;
320fd068ddcSJakub Kicinski }
321