nfp_asm.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. #ifndef __NFP_ASM_H__
  34. #define __NFP_ASM_H__ 1
  35. #include <linux/bitfield.h>
  36. #include <linux/bug.h>
  37. #include <linux/types.h>
  38. #define REG_NONE 0
  39. #define REG_WIDTH 4
  40. #define RE_REG_NO_DST 0x020
  41. #define RE_REG_IMM 0x020
  42. #define RE_REG_IMM_encode(x) \
  43. (RE_REG_IMM | ((x) & 0x1f) | (((x) & 0x60) << 1))
  44. #define RE_REG_IMM_MAX 0x07fULL
  45. #define RE_REG_LM 0x050
  46. #define RE_REG_LM_IDX 0x008
  47. #define RE_REG_LM_IDX_MAX 0x7
  48. #define RE_REG_XFR 0x080
  49. #define UR_REG_XFR 0x180
  50. #define UR_REG_LM 0x200
  51. #define UR_REG_LM_IDX 0x020
  52. #define UR_REG_LM_POST_MOD 0x010
  53. #define UR_REG_LM_POST_MOD_DEC 0x001
  54. #define UR_REG_LM_IDX_MAX 0xf
  55. #define UR_REG_NN 0x280
  56. #define UR_REG_NO_DST 0x300
  57. #define UR_REG_IMM UR_REG_NO_DST
  58. #define UR_REG_IMM_encode(x) (UR_REG_IMM | (x))
  59. #define UR_REG_IMM_MAX 0x0ffULL
  60. #define OP_BR_BASE 0x0d800000020ULL
  61. #define OP_BR_BASE_MASK 0x0f8000c3ce0ULL
  62. #define OP_BR_MASK 0x0000000001fULL
  63. #define OP_BR_EV_PIP 0x00000000300ULL
  64. #define OP_BR_CSS 0x0000003c000ULL
  65. #define OP_BR_DEFBR 0x00000300000ULL
  66. #define OP_BR_ADDR_LO 0x007ffc00000ULL
  67. #define OP_BR_ADDR_HI 0x10000000000ULL
  68. #define nfp_is_br(_insn) \
  69. (((_insn) & OP_BR_BASE_MASK) == OP_BR_BASE)
  70. enum br_mask {
  71. BR_BEQ = 0x00,
  72. BR_BNE = 0x01,
  73. BR_BMI = 0x02,
  74. BR_BHS = 0x04,
  75. BR_BLO = 0x05,
  76. BR_BGE = 0x08,
  77. BR_BLT = 0x09,
  78. BR_UNC = 0x18,
  79. };
  80. enum br_ev_pip {
  81. BR_EV_PIP_UNCOND = 0,
  82. BR_EV_PIP_COND = 1,
  83. };
  84. enum br_ctx_signal_state {
  85. BR_CSS_NONE = 2,
  86. };
  87. u16 br_get_offset(u64 instr);
  88. void br_set_offset(u64 *instr, u16 offset);
  89. void br_add_offset(u64 *instr, u16 offset);
  90. #define OP_BBYTE_BASE 0x0c800000000ULL
  91. #define OP_BB_A_SRC 0x000000000ffULL
  92. #define OP_BB_BYTE 0x00000000300ULL
  93. #define OP_BB_B_SRC 0x0000003fc00ULL
  94. #define OP_BB_I8 0x00000040000ULL
  95. #define OP_BB_EQ 0x00000080000ULL
  96. #define OP_BB_DEFBR 0x00000300000ULL
  97. #define OP_BB_ADDR_LO 0x007ffc00000ULL
  98. #define OP_BB_ADDR_HI 0x10000000000ULL
  99. #define OP_BB_SRC_LMEXTN 0x40000000000ULL
  100. #define OP_BALU_BASE 0x0e800000000ULL
  101. #define OP_BA_A_SRC 0x000000003ffULL
  102. #define OP_BA_B_SRC 0x000000ffc00ULL
  103. #define OP_BA_DEFBR 0x00000300000ULL
  104. #define OP_BA_ADDR_HI 0x0007fc00000ULL
  105. #define OP_IMMED_A_SRC 0x000000003ffULL
  106. #define OP_IMMED_B_SRC 0x000000ffc00ULL
  107. #define OP_IMMED_IMM 0x0000ff00000ULL
  108. #define OP_IMMED_WIDTH 0x00060000000ULL
  109. #define OP_IMMED_INV 0x00080000000ULL
  110. #define OP_IMMED_SHIFT 0x00600000000ULL
  111. #define OP_IMMED_BASE 0x0f000000000ULL
  112. #define OP_IMMED_WR_AB 0x20000000000ULL
  113. #define OP_IMMED_SRC_LMEXTN 0x40000000000ULL
  114. #define OP_IMMED_DST_LMEXTN 0x80000000000ULL
  115. enum immed_width {
  116. IMMED_WIDTH_ALL = 0,
  117. IMMED_WIDTH_BYTE = 1,
  118. IMMED_WIDTH_WORD = 2,
  119. };
  120. enum immed_shift {
  121. IMMED_SHIFT_0B = 0,
  122. IMMED_SHIFT_1B = 1,
  123. IMMED_SHIFT_2B = 2,
  124. };
  125. u16 immed_get_value(u64 instr);
  126. void immed_set_value(u64 *instr, u16 immed);
  127. void immed_add_value(u64 *instr, u16 offset);
  128. #define OP_SHF_BASE 0x08000000000ULL
  129. #define OP_SHF_A_SRC 0x000000000ffULL
  130. #define OP_SHF_SC 0x00000000300ULL
  131. #define OP_SHF_B_SRC 0x0000003fc00ULL
  132. #define OP_SHF_I8 0x00000040000ULL
  133. #define OP_SHF_SW 0x00000080000ULL
  134. #define OP_SHF_DST 0x0000ff00000ULL
  135. #define OP_SHF_SHIFT 0x001f0000000ULL
  136. #define OP_SHF_OP 0x00e00000000ULL
  137. #define OP_SHF_DST_AB 0x01000000000ULL
  138. #define OP_SHF_WR_AB 0x20000000000ULL
  139. #define OP_SHF_SRC_LMEXTN 0x40000000000ULL
  140. #define OP_SHF_DST_LMEXTN 0x80000000000ULL
  141. enum shf_op {
  142. SHF_OP_NONE = 0,
  143. SHF_OP_AND = 2,
  144. SHF_OP_OR = 5,
  145. };
  146. enum shf_sc {
  147. SHF_SC_R_ROT = 0,
  148. SHF_SC_NONE = SHF_SC_R_ROT,
  149. SHF_SC_R_SHF = 1,
  150. SHF_SC_L_SHF = 2,
  151. SHF_SC_R_DSHF = 3,
  152. };
  153. #define OP_ALU_A_SRC 0x000000003ffULL
  154. #define OP_ALU_B_SRC 0x000000ffc00ULL
  155. #define OP_ALU_DST 0x0003ff00000ULL
  156. #define OP_ALU_SW 0x00040000000ULL
  157. #define OP_ALU_OP 0x00f80000000ULL
  158. #define OP_ALU_DST_AB 0x01000000000ULL
  159. #define OP_ALU_BASE 0x0a000000000ULL
  160. #define OP_ALU_WR_AB 0x20000000000ULL
  161. #define OP_ALU_SRC_LMEXTN 0x40000000000ULL
  162. #define OP_ALU_DST_LMEXTN 0x80000000000ULL
  163. enum alu_op {
  164. ALU_OP_NONE = 0x00,
  165. ALU_OP_ADD = 0x01,
  166. ALU_OP_NOT = 0x04,
  167. ALU_OP_ADD_2B = 0x05,
  168. ALU_OP_AND = 0x08,
  169. ALU_OP_SUB_C = 0x0d,
  170. ALU_OP_ADD_C = 0x11,
  171. ALU_OP_OR = 0x14,
  172. ALU_OP_SUB = 0x15,
  173. ALU_OP_XOR = 0x18,
  174. };
  175. enum alu_dst_ab {
  176. ALU_DST_A = 0,
  177. ALU_DST_B = 1,
  178. };
  179. #define OP_LDF_BASE 0x0c000000000ULL
  180. #define OP_LDF_A_SRC 0x000000000ffULL
  181. #define OP_LDF_SC 0x00000000300ULL
  182. #define OP_LDF_B_SRC 0x0000003fc00ULL
  183. #define OP_LDF_I8 0x00000040000ULL
  184. #define OP_LDF_SW 0x00000080000ULL
  185. #define OP_LDF_ZF 0x00000100000ULL
  186. #define OP_LDF_BMASK 0x0000f000000ULL
  187. #define OP_LDF_SHF 0x001f0000000ULL
  188. #define OP_LDF_WR_AB 0x20000000000ULL
  189. #define OP_LDF_SRC_LMEXTN 0x40000000000ULL
  190. #define OP_LDF_DST_LMEXTN 0x80000000000ULL
  191. #define OP_CMD_A_SRC 0x000000000ffULL
  192. #define OP_CMD_CTX 0x00000000300ULL
  193. #define OP_CMD_B_SRC 0x0000003fc00ULL
  194. #define OP_CMD_TOKEN 0x000000c0000ULL
  195. #define OP_CMD_XFER 0x00001f00000ULL
  196. #define OP_CMD_CNT 0x0000e000000ULL
  197. #define OP_CMD_SIG 0x000f0000000ULL
  198. #define OP_CMD_TGT_CMD 0x07f00000000ULL
  199. #define OP_CMD_INDIR 0x20000000000ULL
  200. #define OP_CMD_MODE 0x1c0000000000ULL
  201. struct cmd_tgt_act {
  202. u8 token;
  203. u8 tgt_cmd;
  204. };
  205. enum cmd_tgt_map {
  206. CMD_TGT_READ8,
  207. CMD_TGT_WRITE8_SWAP,
  208. CMD_TGT_WRITE32_SWAP,
  209. CMD_TGT_READ32,
  210. CMD_TGT_READ32_LE,
  211. CMD_TGT_READ32_SWAP,
  212. CMD_TGT_READ_LE,
  213. CMD_TGT_READ_SWAP_LE,
  214. CMD_TGT_ADD,
  215. CMD_TGT_ADD_IMM,
  216. __CMD_TGT_MAP_SIZE,
  217. };
  218. extern const struct cmd_tgt_act cmd_tgt_act[__CMD_TGT_MAP_SIZE];
  219. enum cmd_mode {
  220. CMD_MODE_40b_AB = 0,
  221. CMD_MODE_40b_BA = 1,
  222. CMD_MODE_32b = 4,
  223. };
  224. enum cmd_ctx_swap {
  225. CMD_CTX_SWAP = 0,
  226. CMD_CTX_SWAP_DEFER1 = 1,
  227. CMD_CTX_SWAP_DEFER2 = 2,
  228. CMD_CTX_NO_SWAP = 3,
  229. };
  230. #define CMD_OVE_DATA GENMASK(5, 3)
  231. #define CMD_OVE_LEN BIT(7)
  232. #define CMD_OV_LEN GENMASK(12, 8)
  233. #define OP_LCSR_BASE 0x0fc00000000ULL
  234. #define OP_LCSR_A_SRC 0x000000003ffULL
  235. #define OP_LCSR_B_SRC 0x000000ffc00ULL
  236. #define OP_LCSR_WRITE 0x00000200000ULL
  237. #define OP_LCSR_ADDR 0x001ffc00000ULL
  238. #define OP_LCSR_SRC_LMEXTN 0x40000000000ULL
  239. #define OP_LCSR_DST_LMEXTN 0x80000000000ULL
  240. enum lcsr_wr_src {
  241. LCSR_WR_AREG,
  242. LCSR_WR_BREG,
  243. LCSR_WR_IMM,
  244. };
  245. #define OP_CARB_BASE 0x0e000000000ULL
  246. #define OP_CARB_OR 0x00000010000ULL
  247. #define NFP_CSR_CTX_PTR 0x20
  248. #define NFP_CSR_ACT_LM_ADDR0 0x64
  249. #define NFP_CSR_ACT_LM_ADDR1 0x6c
  250. #define NFP_CSR_ACT_LM_ADDR2 0x94
  251. #define NFP_CSR_ACT_LM_ADDR3 0x9c
  252. #define NFP_CSR_PSEUDO_RND_NUM 0x148
  253. /* Software register representation, independent of operand type */
  254. #define NN_REG_TYPE GENMASK(31, 24)
  255. #define NN_REG_LM_IDX GENMASK(23, 22)
  256. #define NN_REG_LM_IDX_HI BIT(23)
  257. #define NN_REG_LM_IDX_LO BIT(22)
  258. #define NN_REG_LM_MOD GENMASK(21, 20)
  259. #define NN_REG_VAL GENMASK(7, 0)
  260. enum nfp_bpf_reg_type {
  261. NN_REG_GPR_A = BIT(0),
  262. NN_REG_GPR_B = BIT(1),
  263. NN_REG_GPR_BOTH = NN_REG_GPR_A | NN_REG_GPR_B,
  264. NN_REG_NNR = BIT(2),
  265. NN_REG_XFER = BIT(3),
  266. NN_REG_IMM = BIT(4),
  267. NN_REG_NONE = BIT(5),
  268. NN_REG_LMEM = BIT(6),
  269. };
  270. enum nfp_bpf_lm_mode {
  271. NN_LM_MOD_NONE = 0,
  272. NN_LM_MOD_INC,
  273. NN_LM_MOD_DEC,
  274. };
  275. #define reg_both(x) __enc_swreg((x), NN_REG_GPR_BOTH)
  276. #define reg_a(x) __enc_swreg((x), NN_REG_GPR_A)
  277. #define reg_b(x) __enc_swreg((x), NN_REG_GPR_B)
  278. #define reg_nnr(x) __enc_swreg((x), NN_REG_NNR)
  279. #define reg_xfer(x) __enc_swreg((x), NN_REG_XFER)
  280. #define reg_imm(x) __enc_swreg((x), NN_REG_IMM)
  281. #define reg_none() __enc_swreg(0, NN_REG_NONE)
  282. #define reg_lm(x, off) __enc_swreg_lm((x), NN_LM_MOD_NONE, (off))
  283. #define reg_lm_inc(x) __enc_swreg_lm((x), NN_LM_MOD_INC, 0)
  284. #define reg_lm_dec(x) __enc_swreg_lm((x), NN_LM_MOD_DEC, 0)
  285. #define __reg_lm(x, mod, off) __enc_swreg_lm((x), (mod), (off))
  286. typedef __u32 __bitwise swreg;
  287. static inline swreg __enc_swreg(u16 id, u8 type)
  288. {
  289. return (__force swreg)(id | FIELD_PREP(NN_REG_TYPE, type));
  290. }
  291. static inline swreg __enc_swreg_lm(u8 id, enum nfp_bpf_lm_mode mode, u8 off)
  292. {
  293. WARN_ON(id > 3 || (off && mode != NN_LM_MOD_NONE));
  294. return (__force swreg)(FIELD_PREP(NN_REG_TYPE, NN_REG_LMEM) |
  295. FIELD_PREP(NN_REG_LM_IDX, id) |
  296. FIELD_PREP(NN_REG_LM_MOD, mode) |
  297. off);
  298. }
  299. static inline u32 swreg_raw(swreg reg)
  300. {
  301. return (__force u32)reg;
  302. }
  303. static inline enum nfp_bpf_reg_type swreg_type(swreg reg)
  304. {
  305. return FIELD_GET(NN_REG_TYPE, swreg_raw(reg));
  306. }
  307. static inline u16 swreg_value(swreg reg)
  308. {
  309. return FIELD_GET(NN_REG_VAL, swreg_raw(reg));
  310. }
  311. static inline bool swreg_lm_idx(swreg reg)
  312. {
  313. return FIELD_GET(NN_REG_LM_IDX_LO, swreg_raw(reg));
  314. }
  315. static inline bool swreg_lmextn(swreg reg)
  316. {
  317. return FIELD_GET(NN_REG_LM_IDX_HI, swreg_raw(reg));
  318. }
  319. static inline enum nfp_bpf_lm_mode swreg_lm_mode(swreg reg)
  320. {
  321. return FIELD_GET(NN_REG_LM_MOD, swreg_raw(reg));
  322. }
  323. struct nfp_insn_ur_regs {
  324. enum alu_dst_ab dst_ab;
  325. u16 dst;
  326. u16 areg, breg;
  327. bool swap;
  328. bool wr_both;
  329. bool dst_lmextn;
  330. bool src_lmextn;
  331. };
  332. struct nfp_insn_re_regs {
  333. enum alu_dst_ab dst_ab;
  334. u8 dst;
  335. u8 areg, breg;
  336. bool swap;
  337. bool wr_both;
  338. bool i8;
  339. bool dst_lmextn;
  340. bool src_lmextn;
  341. };
  342. int swreg_to_unrestricted(swreg dst, swreg lreg, swreg rreg,
  343. struct nfp_insn_ur_regs *reg);
  344. int swreg_to_restricted(swreg dst, swreg lreg, swreg rreg,
  345. struct nfp_insn_re_regs *reg, bool has_imm8);
  346. #define NFP_USTORE_PREFETCH_WINDOW 8
  347. int nfp_ustore_check_valid_no_ecc(u64 insn);
  348. u64 nfp_ustore_calc_ecc_insn(u64 insn);
  349. #define NFP_IND_ME_REFL_WR_SIG_INIT 3
  350. #define NFP_IND_ME_CTX_PTR_BASE_MASK GENMASK(9, 0)
  351. #define NFP_IND_NUM_CONTEXTS 8
  352. static inline u32 nfp_get_ind_csr_ctx_ptr_offs(u32 read_offset)
  353. {
  354. return (read_offset & ~NFP_IND_ME_CTX_PTR_BASE_MASK) | NFP_CSR_CTX_PTR;
  355. }
  356. #endif