xref: /linux/drivers/net/ethernet/netronome/nfp/nfp_asm.h (revision b7d3826c2ed6c3e626e7ae796c5df2c0d2551c6a)
1 /*
2  * Copyright (C) 2016-2017 Netronome Systems, Inc.
3  *
4  * This software is dual licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree or the BSD 2-Clause License provided below.  You have the
7  * option to license this software under the complete terms of either license.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
19  *      2. Redistributions in binary form must reproduce the above
20  *         copyright notice, this list of conditions and the following
21  *         disclaimer in the documentation and/or other materials
22  *         provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #ifndef __NFP_ASM_H__
35 #define __NFP_ASM_H__ 1
36 
37 #include <linux/bitfield.h>
38 #include <linux/bug.h>
39 #include <linux/types.h>
40 
41 #define REG_NONE	0
42 #define REG_WIDTH	4
43 
44 #define RE_REG_NO_DST	0x020
45 #define RE_REG_IMM	0x020
46 #define RE_REG_IMM_encode(x)					\
47 	(RE_REG_IMM | ((x) & 0x1f) | (((x) & 0x60) << 1))
48 #define RE_REG_IMM_MAX	 0x07fULL
49 #define RE_REG_LM	0x050
50 #define RE_REG_LM_IDX	0x008
51 #define RE_REG_LM_IDX_MAX	0x7
52 #define RE_REG_XFR	0x080
53 
54 #define UR_REG_XFR	0x180
55 #define UR_REG_LM	0x200
56 #define UR_REG_LM_IDX	0x020
57 #define UR_REG_LM_POST_MOD	0x010
58 #define UR_REG_LM_POST_MOD_DEC	0x001
59 #define UR_REG_LM_IDX_MAX	0xf
60 #define UR_REG_NN	0x280
61 #define UR_REG_NO_DST	0x300
62 #define UR_REG_IMM	UR_REG_NO_DST
63 #define UR_REG_IMM_encode(x) (UR_REG_IMM | (x))
64 #define UR_REG_IMM_MAX	 0x0ffULL
65 
66 #define OP_BR_BASE		0x0d800000020ULL
67 #define OP_BR_BASE_MASK		0x0f8000c3ce0ULL
68 #define OP_BR_MASK		0x0000000001fULL
69 #define OP_BR_EV_PIP		0x00000000300ULL
70 #define OP_BR_CSS		0x0000003c000ULL
71 #define OP_BR_DEFBR		0x00000300000ULL
72 #define OP_BR_ADDR_LO		0x007ffc00000ULL
73 #define OP_BR_ADDR_HI		0x10000000000ULL
74 
75 #define OP_BR_BIT_BASE		0x0d000000000ULL
76 #define OP_BR_BIT_BASE_MASK	0x0f800080300ULL
77 #define OP_BR_BIT_A_SRC		0x000000000ffULL
78 #define OP_BR_BIT_B_SRC		0x0000003fc00ULL
79 #define OP_BR_BIT_BV		0x00000040000ULL
80 #define OP_BR_BIT_SRC_LMEXTN	0x40000000000ULL
81 #define OP_BR_BIT_DEFBR		OP_BR_DEFBR
82 #define OP_BR_BIT_ADDR_LO	OP_BR_ADDR_LO
83 #define OP_BR_BIT_ADDR_HI	OP_BR_ADDR_HI
84 
85 #define OP_BR_ALU_BASE		0x0e800000000ULL
86 #define OP_BR_ALU_BASE_MASK	0x0ff80000000ULL
87 #define OP_BR_ALU_A_SRC		0x000000003ffULL
88 #define OP_BR_ALU_B_SRC		0x000000ffc00ULL
89 #define OP_BR_ALU_DEFBR		0x00000300000ULL
90 #define OP_BR_ALU_IMM_HI	0x0007fc00000ULL
91 #define OP_BR_ALU_SRC_LMEXTN	0x40000000000ULL
92 #define OP_BR_ALU_DST_LMEXTN	0x80000000000ULL
93 
94 static inline bool nfp_is_br(u64 insn)
95 {
96 	return (insn & OP_BR_BASE_MASK) == OP_BR_BASE ||
97 	       (insn & OP_BR_BIT_BASE_MASK) == OP_BR_BIT_BASE;
98 }
99 
100 enum br_mask {
101 	BR_BEQ = 0x00,
102 	BR_BNE = 0x01,
103 	BR_BMI = 0x02,
104 	BR_BHS = 0x04,
105 	BR_BCC = 0x05,
106 	BR_BLO = 0x05,
107 	BR_BGE = 0x08,
108 	BR_BLT = 0x09,
109 	BR_UNC = 0x18,
110 };
111 
112 enum br_ev_pip {
113 	BR_EV_PIP_UNCOND = 0,
114 	BR_EV_PIP_COND = 1,
115 };
116 
117 enum br_ctx_signal_state {
118 	BR_CSS_NONE = 2,
119 };
120 
121 u16 br_get_offset(u64 instr);
122 void br_set_offset(u64 *instr, u16 offset);
123 void br_add_offset(u64 *instr, u16 offset);
124 
125 #define OP_BBYTE_BASE		0x0c800000000ULL
126 #define OP_BB_A_SRC		0x000000000ffULL
127 #define OP_BB_BYTE		0x00000000300ULL
128 #define OP_BB_B_SRC		0x0000003fc00ULL
129 #define OP_BB_I8		0x00000040000ULL
130 #define OP_BB_EQ		0x00000080000ULL
131 #define OP_BB_DEFBR		0x00000300000ULL
132 #define OP_BB_ADDR_LO		0x007ffc00000ULL
133 #define OP_BB_ADDR_HI		0x10000000000ULL
134 #define OP_BB_SRC_LMEXTN	0x40000000000ULL
135 
136 #define OP_BALU_BASE		0x0e800000000ULL
137 #define OP_BA_A_SRC		0x000000003ffULL
138 #define OP_BA_B_SRC		0x000000ffc00ULL
139 #define OP_BA_DEFBR		0x00000300000ULL
140 #define OP_BA_ADDR_HI		0x0007fc00000ULL
141 
142 #define OP_IMMED_A_SRC		0x000000003ffULL
143 #define OP_IMMED_B_SRC		0x000000ffc00ULL
144 #define OP_IMMED_IMM		0x0000ff00000ULL
145 #define OP_IMMED_WIDTH		0x00060000000ULL
146 #define OP_IMMED_INV		0x00080000000ULL
147 #define OP_IMMED_SHIFT		0x00600000000ULL
148 #define OP_IMMED_BASE		0x0f000000000ULL
149 #define OP_IMMED_WR_AB		0x20000000000ULL
150 #define OP_IMMED_SRC_LMEXTN	0x40000000000ULL
151 #define OP_IMMED_DST_LMEXTN	0x80000000000ULL
152 
153 enum immed_width {
154 	IMMED_WIDTH_ALL = 0,
155 	IMMED_WIDTH_BYTE = 1,
156 	IMMED_WIDTH_WORD = 2,
157 };
158 
159 enum immed_shift {
160 	IMMED_SHIFT_0B = 0,
161 	IMMED_SHIFT_1B = 1,
162 	IMMED_SHIFT_2B = 2,
163 };
164 
165 u16 immed_get_value(u64 instr);
166 void immed_set_value(u64 *instr, u16 immed);
167 void immed_add_value(u64 *instr, u16 offset);
168 
169 #define OP_SHF_BASE		0x08000000000ULL
170 #define OP_SHF_A_SRC		0x000000000ffULL
171 #define OP_SHF_SC		0x00000000300ULL
172 #define OP_SHF_B_SRC		0x0000003fc00ULL
173 #define OP_SHF_I8		0x00000040000ULL
174 #define OP_SHF_SW		0x00000080000ULL
175 #define OP_SHF_DST		0x0000ff00000ULL
176 #define OP_SHF_SHIFT		0x001f0000000ULL
177 #define OP_SHF_OP		0x00e00000000ULL
178 #define OP_SHF_DST_AB		0x01000000000ULL
179 #define OP_SHF_WR_AB		0x20000000000ULL
180 #define OP_SHF_SRC_LMEXTN	0x40000000000ULL
181 #define OP_SHF_DST_LMEXTN	0x80000000000ULL
182 
183 enum shf_op {
184 	SHF_OP_NONE = 0,
185 	SHF_OP_AND = 2,
186 	SHF_OP_OR = 5,
187 	SHF_OP_ASHR = 6,
188 };
189 
190 enum shf_sc {
191 	SHF_SC_R_ROT = 0,
192 	SHF_SC_NONE = SHF_SC_R_ROT,
193 	SHF_SC_R_SHF = 1,
194 	SHF_SC_L_SHF = 2,
195 	SHF_SC_R_DSHF = 3,
196 };
197 
198 #define OP_ALU_A_SRC		0x000000003ffULL
199 #define OP_ALU_B_SRC		0x000000ffc00ULL
200 #define OP_ALU_DST		0x0003ff00000ULL
201 #define OP_ALU_SW		0x00040000000ULL
202 #define OP_ALU_OP		0x00f80000000ULL
203 #define OP_ALU_DST_AB		0x01000000000ULL
204 #define OP_ALU_BASE		0x0a000000000ULL
205 #define OP_ALU_WR_AB		0x20000000000ULL
206 #define OP_ALU_SRC_LMEXTN	0x40000000000ULL
207 #define OP_ALU_DST_LMEXTN	0x80000000000ULL
208 
209 enum alu_op {
210 	ALU_OP_NONE		= 0x00,
211 	ALU_OP_ADD		= 0x01,
212 	ALU_OP_NOT		= 0x04,
213 	ALU_OP_ADD_2B		= 0x05,
214 	ALU_OP_AND		= 0x08,
215 	ALU_OP_AND_NOT_A	= 0x0c,
216 	ALU_OP_SUB_C		= 0x0d,
217 	ALU_OP_AND_NOT_B	= 0x10,
218 	ALU_OP_ADD_C		= 0x11,
219 	ALU_OP_OR		= 0x14,
220 	ALU_OP_SUB		= 0x15,
221 	ALU_OP_XOR		= 0x18,
222 };
223 
224 enum alu_dst_ab {
225 	ALU_DST_A = 0,
226 	ALU_DST_B = 1,
227 };
228 
229 #define OP_LDF_BASE		0x0c000000000ULL
230 #define OP_LDF_A_SRC		0x000000000ffULL
231 #define OP_LDF_SC		0x00000000300ULL
232 #define OP_LDF_B_SRC		0x0000003fc00ULL
233 #define OP_LDF_I8		0x00000040000ULL
234 #define OP_LDF_SW		0x00000080000ULL
235 #define OP_LDF_ZF		0x00000100000ULL
236 #define OP_LDF_BMASK		0x0000f000000ULL
237 #define OP_LDF_SHF		0x001f0000000ULL
238 #define OP_LDF_WR_AB		0x20000000000ULL
239 #define OP_LDF_SRC_LMEXTN	0x40000000000ULL
240 #define OP_LDF_DST_LMEXTN	0x80000000000ULL
241 
242 #define OP_CMD_A_SRC		0x000000000ffULL
243 #define OP_CMD_CTX		0x00000000300ULL
244 #define OP_CMD_B_SRC		0x0000003fc00ULL
245 #define OP_CMD_TOKEN		0x000000c0000ULL
246 #define OP_CMD_XFER		0x00001f00000ULL
247 #define OP_CMD_CNT		0x0000e000000ULL
248 #define OP_CMD_SIG		0x000f0000000ULL
249 #define OP_CMD_TGT_CMD		0x07f00000000ULL
250 #define OP_CMD_INDIR		0x20000000000ULL
251 #define OP_CMD_MODE	       0x1c0000000000ULL
252 
253 struct cmd_tgt_act {
254 	u8 token;
255 	u8 tgt_cmd;
256 };
257 
258 enum cmd_tgt_map {
259 	CMD_TGT_READ8,
260 	CMD_TGT_WRITE8_SWAP,
261 	CMD_TGT_WRITE32_SWAP,
262 	CMD_TGT_READ32,
263 	CMD_TGT_READ32_LE,
264 	CMD_TGT_READ32_SWAP,
265 	CMD_TGT_READ_LE,
266 	CMD_TGT_READ_SWAP_LE,
267 	CMD_TGT_ADD,
268 	CMD_TGT_ADD_IMM,
269 	__CMD_TGT_MAP_SIZE,
270 };
271 
272 extern const struct cmd_tgt_act cmd_tgt_act[__CMD_TGT_MAP_SIZE];
273 
274 enum cmd_mode {
275 	CMD_MODE_40b_AB	= 0,
276 	CMD_MODE_40b_BA	= 1,
277 	CMD_MODE_32b	= 4,
278 };
279 
280 enum cmd_ctx_swap {
281 	CMD_CTX_SWAP = 0,
282 	CMD_CTX_SWAP_DEFER1 = 1,
283 	CMD_CTX_SWAP_DEFER2 = 2,
284 	CMD_CTX_NO_SWAP = 3,
285 };
286 
287 #define CMD_OVE_DATA	GENMASK(5, 3)
288 #define CMD_OVE_LEN	BIT(7)
289 #define CMD_OV_LEN	GENMASK(12, 8)
290 
291 #define OP_LCSR_BASE		0x0fc00000000ULL
292 #define OP_LCSR_A_SRC		0x000000003ffULL
293 #define OP_LCSR_B_SRC		0x000000ffc00ULL
294 #define OP_LCSR_WRITE		0x00000200000ULL
295 #define OP_LCSR_ADDR		0x001ffc00000ULL
296 #define OP_LCSR_SRC_LMEXTN	0x40000000000ULL
297 #define OP_LCSR_DST_LMEXTN	0x80000000000ULL
298 
299 enum lcsr_wr_src {
300 	LCSR_WR_AREG,
301 	LCSR_WR_BREG,
302 	LCSR_WR_IMM,
303 };
304 
305 #define OP_CARB_BASE		0x0e000000000ULL
306 #define OP_CARB_OR		0x00000010000ULL
307 
308 #define NFP_CSR_CTX_PTR		0x20
309 #define NFP_CSR_ACT_LM_ADDR0	0x64
310 #define NFP_CSR_ACT_LM_ADDR1	0x6c
311 #define NFP_CSR_ACT_LM_ADDR2	0x94
312 #define NFP_CSR_ACT_LM_ADDR3	0x9c
313 #define NFP_CSR_PSEUDO_RND_NUM	0x148
314 
315 /* Software register representation, independent of operand type */
316 #define NN_REG_TYPE	GENMASK(31, 24)
317 #define NN_REG_LM_IDX	GENMASK(23, 22)
318 #define NN_REG_LM_IDX_HI	BIT(23)
319 #define NN_REG_LM_IDX_LO	BIT(22)
320 #define NN_REG_LM_MOD	GENMASK(21, 20)
321 #define NN_REG_VAL	GENMASK(7, 0)
322 
323 enum nfp_bpf_reg_type {
324 	NN_REG_GPR_A =	BIT(0),
325 	NN_REG_GPR_B =	BIT(1),
326 	NN_REG_GPR_BOTH = NN_REG_GPR_A | NN_REG_GPR_B,
327 	NN_REG_NNR =	BIT(2),
328 	NN_REG_XFER =	BIT(3),
329 	NN_REG_IMM =	BIT(4),
330 	NN_REG_NONE =	BIT(5),
331 	NN_REG_LMEM =	BIT(6),
332 };
333 
334 enum nfp_bpf_lm_mode {
335 	NN_LM_MOD_NONE = 0,
336 	NN_LM_MOD_INC,
337 	NN_LM_MOD_DEC,
338 };
339 
340 #define reg_both(x)	__enc_swreg((x), NN_REG_GPR_BOTH)
341 #define reg_a(x)	__enc_swreg((x), NN_REG_GPR_A)
342 #define reg_b(x)	__enc_swreg((x), NN_REG_GPR_B)
343 #define reg_nnr(x)	__enc_swreg((x), NN_REG_NNR)
344 #define reg_xfer(x)	__enc_swreg((x), NN_REG_XFER)
345 #define reg_imm(x)	__enc_swreg((x), NN_REG_IMM)
346 #define reg_none()	__enc_swreg(0, NN_REG_NONE)
347 #define reg_lm(x, off)	__enc_swreg_lm((x), NN_LM_MOD_NONE, (off))
348 #define reg_lm_inc(x)	__enc_swreg_lm((x), NN_LM_MOD_INC, 0)
349 #define reg_lm_dec(x)	__enc_swreg_lm((x), NN_LM_MOD_DEC, 0)
350 #define __reg_lm(x, mod, off)	__enc_swreg_lm((x), (mod), (off))
351 
352 typedef __u32 __bitwise swreg;
353 
354 static inline swreg __enc_swreg(u16 id, u8 type)
355 {
356 	return (__force swreg)(id | FIELD_PREP(NN_REG_TYPE, type));
357 }
358 
359 static inline swreg __enc_swreg_lm(u8 id, enum nfp_bpf_lm_mode mode, u8 off)
360 {
361 	WARN_ON(id > 3 || (off && mode != NN_LM_MOD_NONE));
362 
363 	return (__force swreg)(FIELD_PREP(NN_REG_TYPE, NN_REG_LMEM) |
364 			       FIELD_PREP(NN_REG_LM_IDX, id) |
365 			       FIELD_PREP(NN_REG_LM_MOD, mode) |
366 			       off);
367 }
368 
369 static inline u32 swreg_raw(swreg reg)
370 {
371 	return (__force u32)reg;
372 }
373 
374 static inline enum nfp_bpf_reg_type swreg_type(swreg reg)
375 {
376 	return FIELD_GET(NN_REG_TYPE, swreg_raw(reg));
377 }
378 
379 static inline u16 swreg_value(swreg reg)
380 {
381 	return FIELD_GET(NN_REG_VAL, swreg_raw(reg));
382 }
383 
384 static inline bool swreg_lm_idx(swreg reg)
385 {
386 	return FIELD_GET(NN_REG_LM_IDX_LO, swreg_raw(reg));
387 }
388 
389 static inline bool swreg_lmextn(swreg reg)
390 {
391 	return FIELD_GET(NN_REG_LM_IDX_HI, swreg_raw(reg));
392 }
393 
394 static inline enum nfp_bpf_lm_mode swreg_lm_mode(swreg reg)
395 {
396 	return FIELD_GET(NN_REG_LM_MOD, swreg_raw(reg));
397 }
398 
399 struct nfp_insn_ur_regs {
400 	enum alu_dst_ab dst_ab;
401 	u16 dst;
402 	u16 areg, breg;
403 	bool swap;
404 	bool wr_both;
405 	bool dst_lmextn;
406 	bool src_lmextn;
407 };
408 
409 struct nfp_insn_re_regs {
410 	enum alu_dst_ab dst_ab;
411 	u8 dst;
412 	u8 areg, breg;
413 	bool swap;
414 	bool wr_both;
415 	bool i8;
416 	bool dst_lmextn;
417 	bool src_lmextn;
418 };
419 
420 int swreg_to_unrestricted(swreg dst, swreg lreg, swreg rreg,
421 			  struct nfp_insn_ur_regs *reg);
422 int swreg_to_restricted(swreg dst, swreg lreg, swreg rreg,
423 			struct nfp_insn_re_regs *reg, bool has_imm8);
424 
425 #define NFP_USTORE_PREFETCH_WINDOW	8
426 
427 int nfp_ustore_check_valid_no_ecc(u64 insn);
428 u64 nfp_ustore_calc_ecc_insn(u64 insn);
429 
430 #define NFP_IND_ME_REFL_WR_SIG_INIT	3
431 #define NFP_IND_ME_CTX_PTR_BASE_MASK	GENMASK(9, 0)
432 #define NFP_IND_NUM_CONTEXTS		8
433 
434 static inline u32 nfp_get_ind_csr_ctx_ptr_offs(u32 read_offset)
435 {
436 	return (read_offset & ~NFP_IND_ME_CTX_PTR_BASE_MASK) | NFP_CSR_CTX_PTR;
437 }
438 
439 enum mul_type {
440 	MUL_TYPE_START		= 0x00,
441 	MUL_TYPE_STEP_24x8	= 0x01,
442 	MUL_TYPE_STEP_16x16	= 0x02,
443 	MUL_TYPE_STEP_32x32	= 0x03,
444 };
445 
446 enum mul_step {
447 	MUL_STEP_1		= 0x00,
448 	MUL_STEP_NONE		= MUL_STEP_1,
449 	MUL_STEP_2		= 0x01,
450 	MUL_STEP_3		= 0x02,
451 	MUL_STEP_4		= 0x03,
452 	MUL_LAST		= 0x04,
453 	MUL_LAST_2		= 0x05,
454 };
455 
456 #define OP_MUL_BASE		0x0f800000000ULL
457 #define OP_MUL_A_SRC		0x000000003ffULL
458 #define OP_MUL_B_SRC		0x000000ffc00ULL
459 #define OP_MUL_STEP		0x00000700000ULL
460 #define OP_MUL_DST_AB		0x00000800000ULL
461 #define OP_MUL_SW		0x00040000000ULL
462 #define OP_MUL_TYPE		0x00180000000ULL
463 #define OP_MUL_WR_AB		0x20000000000ULL
464 #define OP_MUL_SRC_LMEXTN	0x40000000000ULL
465 #define OP_MUL_DST_LMEXTN	0x80000000000ULL
466 
467 #endif
468