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