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 #include <linux/bitops.h> 35 #include <linux/errno.h> 36 #include <linux/kernel.h> 37 #include <linux/string.h> 38 #include <linux/types.h> 39 40 #include "nfp_asm.h" 41 42 const struct cmd_tgt_act cmd_tgt_act[__CMD_TGT_MAP_SIZE] = { 43 [CMD_TGT_WRITE8_SWAP] = { 0x02, 0x42 }, 44 [CMD_TGT_READ8] = { 0x01, 0x43 }, 45 [CMD_TGT_READ32] = { 0x00, 0x5c }, 46 [CMD_TGT_READ32_LE] = { 0x01, 0x5c }, 47 [CMD_TGT_READ32_SWAP] = { 0x02, 0x5c }, 48 [CMD_TGT_READ_LE] = { 0x01, 0x40 }, 49 [CMD_TGT_READ_SWAP_LE] = { 0x03, 0x40 }, 50 }; 51 52 static u16 nfp_swreg_to_unreg(swreg reg, bool is_dst) 53 { 54 bool lm_id, lm_dec = false; 55 u16 val = swreg_value(reg); 56 57 switch (swreg_type(reg)) { 58 case NN_REG_GPR_A: 59 case NN_REG_GPR_B: 60 case NN_REG_GPR_BOTH: 61 return val; 62 case NN_REG_NNR: 63 return UR_REG_NN | val; 64 case NN_REG_XFER: 65 return UR_REG_XFR | val; 66 case NN_REG_LMEM: 67 lm_id = swreg_lm_idx(reg); 68 69 switch (swreg_lm_mode(reg)) { 70 case NN_LM_MOD_NONE: 71 if (val & ~UR_REG_LM_IDX_MAX) { 72 pr_err("LM offset too large\n"); 73 return 0; 74 } 75 return UR_REG_LM | FIELD_PREP(UR_REG_LM_IDX, lm_id) | 76 val; 77 case NN_LM_MOD_DEC: 78 lm_dec = true; 79 /* fall through */ 80 case NN_LM_MOD_INC: 81 if (val) { 82 pr_err("LM offset in inc/dev mode\n"); 83 return 0; 84 } 85 return UR_REG_LM | UR_REG_LM_POST_MOD | 86 FIELD_PREP(UR_REG_LM_IDX, lm_id) | 87 FIELD_PREP(UR_REG_LM_POST_MOD_DEC, lm_dec); 88 default: 89 pr_err("bad LM mode for unrestricted operands %d\n", 90 swreg_lm_mode(reg)); 91 return 0; 92 } 93 case NN_REG_IMM: 94 if (val & ~0xff) { 95 pr_err("immediate too large\n"); 96 return 0; 97 } 98 return UR_REG_IMM_encode(val); 99 case NN_REG_NONE: 100 return is_dst ? UR_REG_NO_DST : REG_NONE; 101 } 102 103 pr_err("unrecognized reg encoding %08x\n", reg); 104 return 0; 105 } 106 107 int swreg_to_unrestricted(swreg dst, swreg lreg, swreg rreg, 108 struct nfp_insn_ur_regs *reg) 109 { 110 memset(reg, 0, sizeof(*reg)); 111 112 /* Decode destination */ 113 if (swreg_type(dst) == NN_REG_IMM) 114 return -EFAULT; 115 116 if (swreg_type(dst) == NN_REG_GPR_B) 117 reg->dst_ab = ALU_DST_B; 118 if (swreg_type(dst) == NN_REG_GPR_BOTH) 119 reg->wr_both = true; 120 reg->dst = nfp_swreg_to_unreg(dst, true); 121 122 /* Decode source operands */ 123 if (swreg_type(lreg) == swreg_type(rreg) && 124 swreg_type(lreg) != NN_REG_NONE) 125 return -EFAULT; 126 127 if (swreg_type(lreg) == NN_REG_GPR_B || 128 swreg_type(rreg) == NN_REG_GPR_A) { 129 reg->areg = nfp_swreg_to_unreg(rreg, false); 130 reg->breg = nfp_swreg_to_unreg(lreg, false); 131 reg->swap = true; 132 } else { 133 reg->areg = nfp_swreg_to_unreg(lreg, false); 134 reg->breg = nfp_swreg_to_unreg(rreg, false); 135 } 136 137 reg->dst_lmextn = swreg_lmextn(dst); 138 reg->src_lmextn = swreg_lmextn(lreg) | swreg_lmextn(rreg); 139 140 return 0; 141 } 142 143 static u16 nfp_swreg_to_rereg(swreg reg, bool is_dst, bool has_imm8, bool *i8) 144 { 145 u16 val = swreg_value(reg); 146 bool lm_id; 147 148 switch (swreg_type(reg)) { 149 case NN_REG_GPR_A: 150 case NN_REG_GPR_B: 151 case NN_REG_GPR_BOTH: 152 return val; 153 case NN_REG_XFER: 154 return RE_REG_XFR | val; 155 case NN_REG_LMEM: 156 lm_id = swreg_lm_idx(reg); 157 158 if (swreg_lm_mode(reg) != NN_LM_MOD_NONE) { 159 pr_err("bad LM mode for restricted operands %d\n", 160 swreg_lm_mode(reg)); 161 return 0; 162 } 163 164 if (val & ~RE_REG_LM_IDX_MAX) { 165 pr_err("LM offset too large\n"); 166 return 0; 167 } 168 169 return RE_REG_LM | FIELD_PREP(RE_REG_LM_IDX, lm_id) | val; 170 case NN_REG_IMM: 171 if (val & ~(0x7f | has_imm8 << 7)) { 172 pr_err("immediate too large\n"); 173 return 0; 174 } 175 *i8 = val & 0x80; 176 return RE_REG_IMM_encode(val & 0x7f); 177 case NN_REG_NONE: 178 return is_dst ? RE_REG_NO_DST : REG_NONE; 179 case NN_REG_NNR: 180 pr_err("NNRs used with restricted encoding\n"); 181 return 0; 182 } 183 184 pr_err("unrecognized reg encoding\n"); 185 return 0; 186 } 187 188 int swreg_to_restricted(swreg dst, swreg lreg, swreg rreg, 189 struct nfp_insn_re_regs *reg, bool has_imm8) 190 { 191 memset(reg, 0, sizeof(*reg)); 192 193 /* Decode destination */ 194 if (swreg_type(dst) == NN_REG_IMM) 195 return -EFAULT; 196 197 if (swreg_type(dst) == NN_REG_GPR_B) 198 reg->dst_ab = ALU_DST_B; 199 if (swreg_type(dst) == NN_REG_GPR_BOTH) 200 reg->wr_both = true; 201 reg->dst = nfp_swreg_to_rereg(dst, true, false, NULL); 202 203 /* Decode source operands */ 204 if (swreg_type(lreg) == swreg_type(rreg) && 205 swreg_type(lreg) != NN_REG_NONE) 206 return -EFAULT; 207 208 if (swreg_type(lreg) == NN_REG_GPR_B || 209 swreg_type(rreg) == NN_REG_GPR_A) { 210 reg->areg = nfp_swreg_to_rereg(rreg, false, has_imm8, ®->i8); 211 reg->breg = nfp_swreg_to_rereg(lreg, false, has_imm8, ®->i8); 212 reg->swap = true; 213 } else { 214 reg->areg = nfp_swreg_to_rereg(lreg, false, has_imm8, ®->i8); 215 reg->breg = nfp_swreg_to_rereg(rreg, false, has_imm8, ®->i8); 216 } 217 218 reg->dst_lmextn = swreg_lmextn(dst); 219 reg->src_lmextn = swreg_lmextn(lreg) | swreg_lmextn(rreg); 220 221 return 0; 222 } 223 224 #define NFP_USTORE_ECC_POLY_WORDS 7 225 #define NFP_USTORE_OP_BITS 45 226 227 static const u64 nfp_ustore_ecc_polynomials[NFP_USTORE_ECC_POLY_WORDS] = { 228 0x0ff800007fffULL, 229 0x11f801ff801fULL, 230 0x1e387e0781e1ULL, 231 0x17cb8e388e22ULL, 232 0x1af5b2c93244ULL, 233 0x1f56d5525488ULL, 234 0x0daf69a46910ULL, 235 }; 236 237 static bool parity(u64 value) 238 { 239 return hweight64(value) & 1; 240 } 241 242 int nfp_ustore_check_valid_no_ecc(u64 insn) 243 { 244 if (insn & ~GENMASK_ULL(NFP_USTORE_OP_BITS, 0)) 245 return -EINVAL; 246 247 return 0; 248 } 249 250 u64 nfp_ustore_calc_ecc_insn(u64 insn) 251 { 252 u8 ecc = 0; 253 int i; 254 255 for (i = 0; i < NFP_USTORE_ECC_POLY_WORDS; i++) 256 ecc |= parity(nfp_ustore_ecc_polynomials[i] & insn) << i; 257 258 return insn | (u64)ecc << NFP_USTORE_OP_BITS; 259 } 260