2254a38dfe862bab0afc405caf8503baf2f51d0a
[dpdk.git] / drivers / crypto / dpaa2_sec / hw / rta / math_cmd.h
1 /*
2  * Copyright 2008-2016 Freescale Semiconductor, Inc.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause or GPL-2.0+
5  */
6
7 #ifndef __RTA_MATH_CMD_H__
8 #define __RTA_MATH_CMD_H__
9
10 extern enum rta_sec_era rta_sec_era;
11
12 static const uint32_t math_op1[][2] = {
13 /*1*/   { MATH0,     MATH_SRC0_REG0 },
14         { MATH1,     MATH_SRC0_REG1 },
15         { MATH2,     MATH_SRC0_REG2 },
16         { MATH3,     MATH_SRC0_REG3 },
17         { SEQINSZ,   MATH_SRC0_SEQINLEN },
18         { SEQOUTSZ,  MATH_SRC0_SEQOUTLEN },
19         { VSEQINSZ,  MATH_SRC0_VARSEQINLEN },
20         { VSEQOUTSZ, MATH_SRC0_VARSEQOUTLEN },
21         { ZERO,      MATH_SRC0_ZERO },
22 /*10*/  { NONE,      0 }, /* dummy value */
23         { DPOVRD,    MATH_SRC0_DPOVRD },
24         { ONE,       MATH_SRC0_ONE }
25 };
26
27 /*
28  * Allowed MATH op1 sources for each SEC Era.
29  * Values represent the number of entries from math_op1[] that are supported.
30  */
31 static const unsigned int math_op1_sz[] = {10, 10, 12, 12, 12, 12, 12, 12};
32
33 static const uint32_t math_op2[][2] = {
34 /*1*/   { MATH0,     MATH_SRC1_REG0 },
35         { MATH1,     MATH_SRC1_REG1 },
36         { MATH2,     MATH_SRC1_REG2 },
37         { MATH3,     MATH_SRC1_REG3 },
38         { ABD,       MATH_SRC1_INFIFO },
39         { OFIFO,     MATH_SRC1_OUTFIFO },
40         { ONE,       MATH_SRC1_ONE },
41 /*8*/   { NONE,      0 }, /* dummy value */
42         { JOBSRC,    MATH_SRC1_JOBSOURCE },
43         { DPOVRD,    MATH_SRC1_DPOVRD },
44         { VSEQINSZ,  MATH_SRC1_VARSEQINLEN },
45         { VSEQOUTSZ, MATH_SRC1_VARSEQOUTLEN },
46 /*13*/  { ZERO,      MATH_SRC1_ZERO }
47 };
48
49 /*
50  * Allowed MATH op2 sources for each SEC Era.
51  * Values represent the number of entries from math_op2[] that are supported.
52  */
53 static const unsigned int math_op2_sz[] = {8, 9, 13, 13, 13, 13, 13, 13};
54
55 static const uint32_t math_result[][2] = {
56 /*1*/   { MATH0,     MATH_DEST_REG0 },
57         { MATH1,     MATH_DEST_REG1 },
58         { MATH2,     MATH_DEST_REG2 },
59         { MATH3,     MATH_DEST_REG3 },
60         { SEQINSZ,   MATH_DEST_SEQINLEN },
61         { SEQOUTSZ,  MATH_DEST_SEQOUTLEN },
62         { VSEQINSZ,  MATH_DEST_VARSEQINLEN },
63         { VSEQOUTSZ, MATH_DEST_VARSEQOUTLEN },
64 /*9*/   { NONE,      MATH_DEST_NONE },
65         { DPOVRD,    MATH_DEST_DPOVRD }
66 };
67
68 /*
69  * Allowed MATH result destinations for each SEC Era.
70  * Values represent the number of entries from math_result[] that are
71  * supported.
72  */
73 static const unsigned int math_result_sz[] = {9, 9, 10, 10, 10, 10, 10, 10};
74
75 static inline int
76 rta_math(struct program *program, uint64_t operand1,
77          uint32_t op, uint64_t operand2, uint32_t result,
78          int length, uint32_t options)
79 {
80         uint32_t opcode = CMD_MATH;
81         uint32_t val = 0;
82         int ret = -EINVAL;
83         unsigned int start_pc = program->current_pc;
84
85         if (((op == MATH_FUN_BSWAP) && (rta_sec_era < RTA_SEC_ERA_4)) ||
86             ((op == MATH_FUN_ZBYT) && (rta_sec_era < RTA_SEC_ERA_2))) {
87                 pr_err("MATH: operation not supported by SEC Era %d. SEC PC: %d; Instr: %d\n",
88                        USER_SEC_ERA(rta_sec_era), program->current_pc,
89                        program->current_instruction);
90                 goto err;
91         }
92
93         if (options & SWP) {
94                 if (rta_sec_era < RTA_SEC_ERA_7) {
95                         pr_err("MATH: operation not supported by SEC Era %d. SEC PC: %d; Instr: %d\n",
96                                USER_SEC_ERA(rta_sec_era), program->current_pc,
97                                program->current_instruction);
98                         goto err;
99                 }
100
101                 if ((options & IFB) ||
102                     (!(options & IMMED) && !(options & IMMED2)) ||
103                     ((options & IMMED) && (options & IMMED2))) {
104                         pr_err("MATH: SWP - invalid configuration. SEC PC: %d; Instr: %d\n",
105                                program->current_pc,
106                                program->current_instruction);
107                         goto err;
108                 }
109         }
110
111         /*
112          * SHLD operation is different from others and we
113          * assume that we can have _NONE as first operand
114          * or _SEQINSZ as second operand
115          */
116         if ((op != MATH_FUN_SHLD) && ((operand1 == NONE) ||
117                                       (operand2 == SEQINSZ))) {
118                 pr_err("MATH: Invalid operand. SEC PC: %d; Instr: %d\n",
119                        program->current_pc, program->current_instruction);
120                 goto err;
121         }
122
123         /*
124          * We first check if it is unary operation. In that
125          * case second operand must be _NONE
126          */
127         if (((op == MATH_FUN_ZBYT) || (op == MATH_FUN_BSWAP)) &&
128             (operand2 != NONE)) {
129                 pr_err("MATH: Invalid operand2. SEC PC: %d; Instr: %d\n",
130                        program->current_pc, program->current_instruction);
131                 goto err;
132         }
133
134         /* Write first operand field */
135         if (options & IMMED) {
136                 opcode |= MATH_SRC0_IMM;
137         } else {
138                 ret = __rta_map_opcode((uint32_t)operand1, math_op1,
139                                        math_op1_sz[rta_sec_era], &val);
140                 if (ret < 0) {
141                         pr_err("MATH: operand1 not supported. SEC PC: %d; Instr: %d\n",
142                                program->current_pc,
143                                program->current_instruction);
144                         goto err;
145                 }
146                 opcode |= val;
147         }
148
149         /* Write second operand field */
150         if (options & IMMED2) {
151                 opcode |= MATH_SRC1_IMM;
152         } else {
153                 ret = __rta_map_opcode((uint32_t)operand2, math_op2,
154                                        math_op2_sz[rta_sec_era], &val);
155                 if (ret < 0) {
156                         pr_err("MATH: operand2 not supported. SEC PC: %d; Instr: %d\n",
157                                program->current_pc,
158                                program->current_instruction);
159                         goto err;
160                 }
161                 opcode |= val;
162         }
163
164         /* Write result field */
165         ret = __rta_map_opcode(result, math_result, math_result_sz[rta_sec_era],
166                                &val);
167         if (ret < 0) {
168                 pr_err("MATH: result not supported. SEC PC: %d; Instr: %d\n",
169                        program->current_pc, program->current_instruction);
170                 goto err;
171         }
172         opcode |= val;
173
174         /*
175          * as we encode operations with their "real" values, we do not
176          * to translate but we do need to validate the value
177          */
178         switch (op) {
179         /*Binary operators */
180         case (MATH_FUN_ADD):
181         case (MATH_FUN_ADDC):
182         case (MATH_FUN_SUB):
183         case (MATH_FUN_SUBB):
184         case (MATH_FUN_OR):
185         case (MATH_FUN_AND):
186         case (MATH_FUN_XOR):
187         case (MATH_FUN_LSHIFT):
188         case (MATH_FUN_RSHIFT):
189         case (MATH_FUN_SHLD):
190         /* Unary operators */
191         case (MATH_FUN_ZBYT):
192         case (MATH_FUN_BSWAP):
193                 opcode |= op;
194                 break;
195         default:
196                 pr_err("MATH: operator is not supported. SEC PC: %d; Instr: %d\n",
197                        program->current_pc, program->current_instruction);
198                 ret = -EINVAL;
199                 goto err;
200         }
201
202         opcode |= (options & ~(IMMED | IMMED2));
203
204         /* Verify length */
205         switch (length) {
206         case (1):
207                 opcode |= MATH_LEN_1BYTE;
208                 break;
209         case (2):
210                 opcode |= MATH_LEN_2BYTE;
211                 break;
212         case (4):
213                 opcode |= MATH_LEN_4BYTE;
214                 break;
215         case (8):
216                 opcode |= MATH_LEN_8BYTE;
217                 break;
218         default:
219                 pr_err("MATH: length is not supported. SEC PC: %d; Instr: %d\n",
220                        program->current_pc, program->current_instruction);
221                 ret = -EINVAL;
222                 goto err;
223         }
224
225         __rta_out32(program, opcode);
226         program->current_instruction++;
227
228         /* Write immediate value */
229         if ((options & IMMED) && !(options & IMMED2)) {
230                 __rta_out64(program, (length > 4) && !(options & IFB),
231                             operand1);
232         } else if ((options & IMMED2) && !(options & IMMED)) {
233                 __rta_out64(program, (length > 4) && !(options & IFB),
234                             operand2);
235         } else if ((options & IMMED) && (options & IMMED2)) {
236                 __rta_out32(program, lower_32_bits(operand1));
237                 __rta_out32(program, lower_32_bits(operand2));
238         }
239
240         return (int)start_pc;
241
242  err:
243         program->first_error_pc = start_pc;
244         program->current_instruction++;
245         return ret;
246 }
247
248 static inline int
249 rta_mathi(struct program *program, uint64_t operand,
250           uint32_t op, uint8_t imm, uint32_t result,
251           int length, uint32_t options)
252 {
253         uint32_t opcode = CMD_MATHI;
254         uint32_t val = 0;
255         int ret = -EINVAL;
256         unsigned int start_pc = program->current_pc;
257
258         if (rta_sec_era < RTA_SEC_ERA_6) {
259                 pr_err("MATHI: Command not supported by SEC Era %d. SEC PC: %d; Instr: %d\n",
260                        USER_SEC_ERA(rta_sec_era), program->current_pc,
261                        program->current_instruction);
262                 goto err;
263         }
264
265         if (((op == MATH_FUN_FBYT) && (options & SSEL))) {
266                 pr_err("MATHI: Illegal combination - FBYT and SSEL. SEC PC: %d; Instr: %d\n",
267                        program->current_pc, program->current_instruction);
268                 goto err;
269         }
270
271         if ((options & SWP) && (rta_sec_era < RTA_SEC_ERA_7)) {
272                 pr_err("MATHI: SWP not supported by SEC Era %d. SEC PC: %d; Instr: %d\n",
273                        USER_SEC_ERA(rta_sec_era), program->current_pc,
274                        program->current_instruction);
275                 goto err;
276         }
277
278         /* Write first operand field */
279         if (!(options & SSEL))
280                 ret = __rta_map_opcode((uint32_t)operand, math_op1,
281                                        math_op1_sz[rta_sec_era], &val);
282         else
283                 ret = __rta_map_opcode((uint32_t)operand, math_op2,
284                                        math_op2_sz[rta_sec_era], &val);
285         if (ret < 0) {
286                 pr_err("MATHI: operand not supported. SEC PC: %d; Instr: %d\n",
287                        program->current_pc, program->current_instruction);
288                 goto err;
289         }
290
291         if (!(options & SSEL))
292                 opcode |= val;
293         else
294                 opcode |= (val << (MATHI_SRC1_SHIFT - MATH_SRC1_SHIFT));
295
296         /* Write second operand field */
297         opcode |= (imm << MATHI_IMM_SHIFT);
298
299         /* Write result field */
300         ret = __rta_map_opcode(result, math_result, math_result_sz[rta_sec_era],
301                                &val);
302         if (ret < 0) {
303                 pr_err("MATHI: result not supported. SEC PC: %d; Instr: %d\n",
304                        program->current_pc, program->current_instruction);
305                 goto err;
306         }
307         opcode |= (val << (MATHI_DEST_SHIFT - MATH_DEST_SHIFT));
308
309         /*
310          * as we encode operations with their "real" values, we do not have to
311          * translate but we do need to validate the value
312          */
313         switch (op) {
314         case (MATH_FUN_ADD):
315         case (MATH_FUN_ADDC):
316         case (MATH_FUN_SUB):
317         case (MATH_FUN_SUBB):
318         case (MATH_FUN_OR):
319         case (MATH_FUN_AND):
320         case (MATH_FUN_XOR):
321         case (MATH_FUN_LSHIFT):
322         case (MATH_FUN_RSHIFT):
323         case (MATH_FUN_FBYT):
324                 opcode |= op;
325                 break;
326         default:
327                 pr_err("MATHI: operator not supported. SEC PC: %d; Instr: %d\n",
328                        program->current_pc, program->current_instruction);
329                 ret = -EINVAL;
330                 goto err;
331         }
332
333         opcode |= options;
334
335         /* Verify length */
336         switch (length) {
337         case (1):
338                 opcode |= MATH_LEN_1BYTE;
339                 break;
340         case (2):
341                 opcode |= MATH_LEN_2BYTE;
342                 break;
343         case (4):
344                 opcode |= MATH_LEN_4BYTE;
345                 break;
346         case (8):
347                 opcode |= MATH_LEN_8BYTE;
348                 break;
349         default:
350                 pr_err("MATHI: length %d not supported. SEC PC: %d; Instr: %d\n",
351                        length, program->current_pc,
352                        program->current_instruction);
353                 ret = -EINVAL;
354                 goto err;
355         }
356
357         __rta_out32(program, opcode);
358         program->current_instruction++;
359
360         return (int)start_pc;
361
362  err:
363         program->first_error_pc = start_pc;
364         program->current_instruction++;
365         return ret;
366 }
367
368 #endif /* __RTA_MATH_CMD_H__ */