1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2016-2018 Netronome Systems, Inc. */
4 #include <linux/bitops.h>
5 #include <linux/errno.h>
6 #include <linux/kernel.h>
7 #include <linux/string.h>
8 #include <linux/types.h>
12 const struct cmd_tgt_act cmd_tgt_act
[__CMD_TGT_MAP_SIZE
] = {
13 [CMD_TGT_WRITE8_SWAP
] = { 0x02, 0x42 },
14 [CMD_TGT_WRITE32_SWAP
] = { 0x02, 0x5f },
15 [CMD_TGT_READ8
] = { 0x01, 0x43 },
16 [CMD_TGT_READ32
] = { 0x00, 0x5c },
17 [CMD_TGT_READ32_LE
] = { 0x01, 0x5c },
18 [CMD_TGT_READ32_SWAP
] = { 0x02, 0x5c },
19 [CMD_TGT_READ_LE
] = { 0x01, 0x40 },
20 [CMD_TGT_READ_SWAP_LE
] = { 0x03, 0x40 },
21 [CMD_TGT_ADD
] = { 0x00, 0x47 },
22 [CMD_TGT_ADD_IMM
] = { 0x02, 0x47 },
25 static bool unreg_is_imm(u16 reg
)
27 return (reg
& UR_REG_IMM
) == UR_REG_IMM
;
30 u16
br_get_offset(u64 instr
)
34 addr_lo
= FIELD_GET(OP_BR_ADDR_LO
, instr
);
35 addr_hi
= FIELD_GET(OP_BR_ADDR_HI
, instr
);
37 return (addr_hi
* ((OP_BR_ADDR_LO
>> __bf_shf(OP_BR_ADDR_LO
)) + 1)) |
41 void br_set_offset(u64
*instr
, u16 offset
)
45 addr_lo
= offset
& (OP_BR_ADDR_LO
>> __bf_shf(OP_BR_ADDR_LO
));
46 addr_hi
= offset
!= addr_lo
;
47 *instr
&= ~(OP_BR_ADDR_HI
| OP_BR_ADDR_LO
);
48 *instr
|= FIELD_PREP(OP_BR_ADDR_HI
, addr_hi
);
49 *instr
|= FIELD_PREP(OP_BR_ADDR_LO
, addr_lo
);
52 void br_add_offset(u64
*instr
, u16 offset
)
56 addr
= br_get_offset(*instr
);
57 br_set_offset(instr
, addr
+ offset
);
60 static bool immed_can_modify(u64 instr
)
62 if (FIELD_GET(OP_IMMED_INV
, instr
) ||
63 FIELD_GET(OP_IMMED_SHIFT
, instr
) ||
64 FIELD_GET(OP_IMMED_WIDTH
, instr
) != IMMED_WIDTH_ALL
) {
65 pr_err("Can't decode/encode immed!\n");
71 u16
immed_get_value(u64 instr
)
75 if (!immed_can_modify(instr
))
78 reg
= FIELD_GET(OP_IMMED_A_SRC
, instr
);
79 if (!unreg_is_imm(reg
))
80 reg
= FIELD_GET(OP_IMMED_B_SRC
, instr
);
82 return (reg
& 0xff) | FIELD_GET(OP_IMMED_IMM
, instr
) << 8;
85 void immed_set_value(u64
*instr
, u16 immed
)
87 if (!immed_can_modify(*instr
))
90 if (unreg_is_imm(FIELD_GET(OP_IMMED_A_SRC
, *instr
))) {
91 *instr
&= ~FIELD_PREP(OP_IMMED_A_SRC
, 0xff);
92 *instr
|= FIELD_PREP(OP_IMMED_A_SRC
, immed
& 0xff);
94 *instr
&= ~FIELD_PREP(OP_IMMED_B_SRC
, 0xff);
95 *instr
|= FIELD_PREP(OP_IMMED_B_SRC
, immed
& 0xff);
98 *instr
&= ~OP_IMMED_IMM
;
99 *instr
|= FIELD_PREP(OP_IMMED_IMM
, immed
>> 8);
102 void immed_add_value(u64
*instr
, u16 offset
)
106 if (!immed_can_modify(*instr
))
109 val
= immed_get_value(*instr
);
110 immed_set_value(instr
, val
+ offset
);
113 static u16
nfp_swreg_to_unreg(swreg reg
, bool is_dst
)
115 bool lm_id
, lm_dec
= false;
116 u16 val
= swreg_value(reg
);
118 switch (swreg_type(reg
)) {
121 case NN_REG_GPR_BOTH
:
124 return UR_REG_NN
| val
;
126 return UR_REG_XFR
| val
;
128 lm_id
= swreg_lm_idx(reg
);
130 switch (swreg_lm_mode(reg
)) {
132 if (val
& ~UR_REG_LM_IDX_MAX
) {
133 pr_err("LM offset too large\n");
136 return UR_REG_LM
| FIELD_PREP(UR_REG_LM_IDX
, lm_id
) |
143 pr_err("LM offset in inc/dev mode\n");
146 return UR_REG_LM
| UR_REG_LM_POST_MOD
|
147 FIELD_PREP(UR_REG_LM_IDX
, lm_id
) |
148 FIELD_PREP(UR_REG_LM_POST_MOD_DEC
, lm_dec
);
150 pr_err("bad LM mode for unrestricted operands %d\n",
156 pr_err("immediate too large\n");
159 return UR_REG_IMM_encode(val
);
161 return is_dst
? UR_REG_NO_DST
: REG_NONE
;
164 pr_err("unrecognized reg encoding %08x\n", reg
);
168 int swreg_to_unrestricted(swreg dst
, swreg lreg
, swreg rreg
,
169 struct nfp_insn_ur_regs
*reg
)
171 memset(reg
, 0, sizeof(*reg
));
173 /* Decode destination */
174 if (swreg_type(dst
) == NN_REG_IMM
)
177 if (swreg_type(dst
) == NN_REG_GPR_B
)
178 reg
->dst_ab
= ALU_DST_B
;
179 if (swreg_type(dst
) == NN_REG_GPR_BOTH
)
181 reg
->dst
= nfp_swreg_to_unreg(dst
, true);
183 /* Decode source operands */
184 if (swreg_type(lreg
) == swreg_type(rreg
) &&
185 swreg_type(lreg
) != NN_REG_NONE
)
188 if (swreg_type(lreg
) == NN_REG_GPR_B
||
189 swreg_type(rreg
) == NN_REG_GPR_A
) {
190 reg
->areg
= nfp_swreg_to_unreg(rreg
, false);
191 reg
->breg
= nfp_swreg_to_unreg(lreg
, false);
194 reg
->areg
= nfp_swreg_to_unreg(lreg
, false);
195 reg
->breg
= nfp_swreg_to_unreg(rreg
, false);
198 reg
->dst_lmextn
= swreg_lmextn(dst
);
199 reg
->src_lmextn
= swreg_lmextn(lreg
) | swreg_lmextn(rreg
);
204 static u16
nfp_swreg_to_rereg(swreg reg
, bool is_dst
, bool has_imm8
, bool *i8
)
206 u16 val
= swreg_value(reg
);
209 switch (swreg_type(reg
)) {
212 case NN_REG_GPR_BOTH
:
215 return RE_REG_XFR
| val
;
217 lm_id
= swreg_lm_idx(reg
);
219 if (swreg_lm_mode(reg
) != NN_LM_MOD_NONE
) {
220 pr_err("bad LM mode for restricted operands %d\n",
225 if (val
& ~RE_REG_LM_IDX_MAX
) {
226 pr_err("LM offset too large\n");
230 return RE_REG_LM
| FIELD_PREP(RE_REG_LM_IDX
, lm_id
) | val
;
232 if (val
& ~(0x7f | has_imm8
<< 7)) {
233 pr_err("immediate too large\n");
237 return RE_REG_IMM_encode(val
& 0x7f);
239 return is_dst
? RE_REG_NO_DST
: REG_NONE
;
241 pr_err("NNRs used with restricted encoding\n");
245 pr_err("unrecognized reg encoding\n");
249 int swreg_to_restricted(swreg dst
, swreg lreg
, swreg rreg
,
250 struct nfp_insn_re_regs
*reg
, bool has_imm8
)
252 memset(reg
, 0, sizeof(*reg
));
254 /* Decode destination */
255 if (swreg_type(dst
) == NN_REG_IMM
)
258 if (swreg_type(dst
) == NN_REG_GPR_B
)
259 reg
->dst_ab
= ALU_DST_B
;
260 if (swreg_type(dst
) == NN_REG_GPR_BOTH
)
262 reg
->dst
= nfp_swreg_to_rereg(dst
, true, false, NULL
);
264 /* Decode source operands */
265 if (swreg_type(lreg
) == swreg_type(rreg
) &&
266 swreg_type(lreg
) != NN_REG_NONE
)
269 if (swreg_type(lreg
) == NN_REG_GPR_B
||
270 swreg_type(rreg
) == NN_REG_GPR_A
) {
271 reg
->areg
= nfp_swreg_to_rereg(rreg
, false, has_imm8
, ®
->i8
);
272 reg
->breg
= nfp_swreg_to_rereg(lreg
, false, has_imm8
, ®
->i8
);
275 reg
->areg
= nfp_swreg_to_rereg(lreg
, false, has_imm8
, ®
->i8
);
276 reg
->breg
= nfp_swreg_to_rereg(rreg
, false, has_imm8
, ®
->i8
);
279 reg
->dst_lmextn
= swreg_lmextn(dst
);
280 reg
->src_lmextn
= swreg_lmextn(lreg
) | swreg_lmextn(rreg
);
285 #define NFP_USTORE_ECC_POLY_WORDS 7
286 #define NFP_USTORE_OP_BITS 45
288 static const u64 nfp_ustore_ecc_polynomials
[NFP_USTORE_ECC_POLY_WORDS
] = {
298 static bool parity(u64 value
)
300 return hweight64(value
) & 1;
303 int nfp_ustore_check_valid_no_ecc(u64 insn
)
305 if (insn
& ~GENMASK_ULL(NFP_USTORE_OP_BITS
, 0))
311 u64
nfp_ustore_calc_ecc_insn(u64 insn
)
316 for (i
= 0; i
< NFP_USTORE_ECC_POLY_WORDS
; i
++)
317 ecc
|= parity(nfp_ustore_ecc_polynomials
[i
] & insn
) << i
;
319 return insn
| (u64
)ecc
<< NFP_USTORE_OP_BITS
;