New upstream version 17.08
[deb_dpdk.git] / drivers / crypto / dpaa2_sec / hw / rta / jump_cmd.h
1 /*-
2  * This file is provided under a dual BSD/GPLv2 license. When using or
3  * redistributing this file, you may do so under either license.
4  *
5  *   BSD LICENSE
6  *
7  * Copyright 2008-2016 Freescale Semiconductor Inc.
8  * Copyright 2016 NXP.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * * Neither the name of the above-listed copyright holders nor the
18  * names of any contributors may be used to endorse or promote products
19  * derived from this software without specific prior written permission.
20  *
21  *   GPL LICENSE SUMMARY
22  *
23  * ALTERNATIVELY, this software may be distributed under the terms of the
24  * GNU General Public License ("GPL") as published by the Free Software
25  * Foundation, either version 2 of that License or (at your option) any
26  * later version.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
32  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40
41 #ifndef __RTA_JUMP_CMD_H__
42 #define __RTA_JUMP_CMD_H__
43
44 extern enum rta_sec_era rta_sec_era;
45
46 static const uint32_t jump_test_cond[][2] = {
47         { NIFP,     JUMP_COND_NIFP },
48         { NIP,      JUMP_COND_NIP },
49         { NOP,      JUMP_COND_NOP },
50         { NCP,      JUMP_COND_NCP },
51         { CALM,     JUMP_COND_CALM },
52         { SELF,     JUMP_COND_SELF },
53         { SHRD,     JUMP_COND_SHRD },
54         { JQP,      JUMP_COND_JQP },
55         { MATH_Z,   JUMP_COND_MATH_Z },
56         { MATH_N,   JUMP_COND_MATH_N },
57         { MATH_NV,  JUMP_COND_MATH_NV },
58         { MATH_C,   JUMP_COND_MATH_C },
59         { PK_0,     JUMP_COND_PK_0 },
60         { PK_GCD_1, JUMP_COND_PK_GCD_1 },
61         { PK_PRIME, JUMP_COND_PK_PRIME },
62         { CLASS1,   JUMP_CLASS_CLASS1 },
63         { CLASS2,   JUMP_CLASS_CLASS2 },
64         { BOTH,     JUMP_CLASS_BOTH }
65 };
66
67 static const uint32_t jump_test_math_cond[][2] = {
68         { MATH_Z,   JUMP_COND_MATH_Z },
69         { MATH_N,   JUMP_COND_MATH_N },
70         { MATH_NV,  JUMP_COND_MATH_NV },
71         { MATH_C,   JUMP_COND_MATH_C }
72 };
73
74 static const uint32_t jump_src_dst[][2] = {
75         { MATH0,     JUMP_SRC_DST_MATH0 },
76         { MATH1,     JUMP_SRC_DST_MATH1 },
77         { MATH2,     JUMP_SRC_DST_MATH2 },
78         { MATH3,     JUMP_SRC_DST_MATH3 },
79         { DPOVRD,    JUMP_SRC_DST_DPOVRD },
80         { SEQINSZ,   JUMP_SRC_DST_SEQINLEN },
81         { SEQOUTSZ,  JUMP_SRC_DST_SEQOUTLEN },
82         { VSEQINSZ,  JUMP_SRC_DST_VARSEQINLEN },
83         { VSEQOUTSZ, JUMP_SRC_DST_VARSEQOUTLEN }
84 };
85
86 static inline int
87 rta_jump(struct program *program, uint64_t address,
88          enum rta_jump_type jump_type,
89          enum rta_jump_cond test_type,
90          uint32_t test_condition, uint32_t src_dst)
91 {
92         uint32_t opcode = CMD_JUMP;
93         unsigned int start_pc = program->current_pc;
94         int ret = -EINVAL;
95
96         if (((jump_type == GOSUB) || (jump_type == RETURN)) &&
97             (rta_sec_era < RTA_SEC_ERA_4)) {
98                 pr_err("JUMP: Jump type not supported by SEC Era %d\n",
99                        USER_SEC_ERA(rta_sec_era));
100                 goto err;
101         }
102
103         if (((jump_type == LOCAL_JUMP_INC) || (jump_type == LOCAL_JUMP_DEC)) &&
104             (rta_sec_era <= RTA_SEC_ERA_5)) {
105                 pr_err("JUMP_INCDEC: Jump type not supported by SEC Era %d\n",
106                        USER_SEC_ERA(rta_sec_era));
107                 goto err;
108         }
109
110         switch (jump_type) {
111         case (LOCAL_JUMP):
112                 /*
113                  * opcode |= JUMP_TYPE_LOCAL;
114                  * JUMP_TYPE_LOCAL is 0
115                  */
116                 break;
117         case (HALT):
118                 opcode |= JUMP_TYPE_HALT;
119                 break;
120         case (HALT_STATUS):
121                 opcode |= JUMP_TYPE_HALT_USER;
122                 break;
123         case (FAR_JUMP):
124                 opcode |= JUMP_TYPE_NONLOCAL;
125                 break;
126         case (GOSUB):
127                 opcode |= JUMP_TYPE_GOSUB;
128                 break;
129         case (RETURN):
130                 opcode |= JUMP_TYPE_RETURN;
131                 break;
132         case (LOCAL_JUMP_INC):
133                 opcode |= JUMP_TYPE_LOCAL_INC;
134                 break;
135         case (LOCAL_JUMP_DEC):
136                 opcode |= JUMP_TYPE_LOCAL_DEC;
137                 break;
138         default:
139                 pr_err("JUMP: Invalid jump type. SEC Program Line: %d\n",
140                        program->current_pc);
141                 goto err;
142         }
143
144         switch (test_type) {
145         case (ALL_TRUE):
146                 /*
147                  * opcode |= JUMP_TEST_ALL;
148                  * JUMP_TEST_ALL is 0
149                  */
150                 break;
151         case (ALL_FALSE):
152                 opcode |= JUMP_TEST_INVALL;
153                 break;
154         case (ANY_TRUE):
155                 opcode |= JUMP_TEST_ANY;
156                 break;
157         case (ANY_FALSE):
158                 opcode |= JUMP_TEST_INVANY;
159                 break;
160         default:
161                 pr_err("JUMP: test type not supported. SEC Program Line: %d\n",
162                        program->current_pc);
163                 goto err;
164         }
165
166         /* write test condition field */
167         if ((jump_type != LOCAL_JUMP_INC) && (jump_type != LOCAL_JUMP_DEC)) {
168                 __rta_map_flags(test_condition, jump_test_cond,
169                                 ARRAY_SIZE(jump_test_cond), &opcode);
170         } else {
171                 uint32_t val = 0;
172
173                 ret = __rta_map_opcode(src_dst, jump_src_dst,
174                                        ARRAY_SIZE(jump_src_dst), &val);
175                 if (ret < 0) {
176                         pr_err("JUMP_INCDEC: SRC_DST not supported. SEC PC: %d; Instr: %d\n",
177                                program->current_pc,
178                                program->current_instruction);
179                         goto err;
180                 }
181                 opcode |= val;
182
183                 __rta_map_flags(test_condition, jump_test_math_cond,
184                                 ARRAY_SIZE(jump_test_math_cond), &opcode);
185         }
186
187         /* write local offset field for local jumps and user-defined halt */
188         if ((jump_type == LOCAL_JUMP) || (jump_type == LOCAL_JUMP_INC) ||
189             (jump_type == LOCAL_JUMP_DEC) || (jump_type == GOSUB) ||
190             (jump_type == HALT_STATUS))
191                 opcode |= (uint32_t)(address & JUMP_OFFSET_MASK);
192
193         __rta_out32(program, opcode);
194         program->current_instruction++;
195
196         if (jump_type == FAR_JUMP)
197                 __rta_out64(program, program->ps, address);
198
199         return (int)start_pc;
200
201  err:
202         program->first_error_pc = start_pc;
203         program->current_instruction++;
204         return ret;
205 }
206
207 #endif /* __RTA_JUMP_CMD_H__ */