xref: /linux/drivers/crypto/caam/desc_constr.h (revision 4413e16d9d21673bb5048a2e542f1aaa00015c2e)
1 /*
2  * caam descriptor construction helper functions
3  *
4  * Copyright 2008-2012 Freescale Semiconductor, Inc.
5  */
6 
7 #include "desc.h"
8 
9 #define IMMEDIATE (1 << 23)
10 #define CAAM_CMD_SZ sizeof(u32)
11 #define CAAM_PTR_SZ sizeof(dma_addr_t)
12 #define CAAM_DESC_BYTES_MAX (CAAM_CMD_SZ * MAX_CAAM_DESCSIZE)
13 
14 #ifdef DEBUG
15 #define PRINT_POS do { printk(KERN_DEBUG "%02d: %s\n", desc_len(desc),\
16 			      &__func__[sizeof("append")]); } while (0)
17 #else
18 #define PRINT_POS
19 #endif
20 
21 #define SET_OK_NO_PROP_ERRORS (IMMEDIATE | LDST_CLASS_DECO | \
22 			       LDST_SRCDST_WORD_DECOCTRL | \
23 			       (LDOFF_CHG_SHARE_OK_NO_PROP << \
24 				LDST_OFFSET_SHIFT))
25 #define DISABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
26 				LDST_SRCDST_WORD_DECOCTRL | \
27 				(LDOFF_DISABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
28 #define ENABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
29 			       LDST_SRCDST_WORD_DECOCTRL | \
30 			       (LDOFF_ENABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
31 
32 static inline int desc_len(u32 *desc)
33 {
34 	return *desc & HDR_DESCLEN_MASK;
35 }
36 
37 static inline int desc_bytes(void *desc)
38 {
39 	return desc_len(desc) * CAAM_CMD_SZ;
40 }
41 
42 static inline u32 *desc_end(u32 *desc)
43 {
44 	return desc + desc_len(desc);
45 }
46 
47 static inline void *sh_desc_pdb(u32 *desc)
48 {
49 	return desc + 1;
50 }
51 
52 static inline void init_desc(u32 *desc, u32 options)
53 {
54 	*desc = (options | HDR_ONE) + 1;
55 }
56 
57 static inline void init_sh_desc(u32 *desc, u32 options)
58 {
59 	PRINT_POS;
60 	init_desc(desc, CMD_SHARED_DESC_HDR | options);
61 }
62 
63 static inline void init_sh_desc_pdb(u32 *desc, u32 options, size_t pdb_bytes)
64 {
65 	u32 pdb_len = (pdb_bytes + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
66 
67 	init_sh_desc(desc, (((pdb_len + 1) << HDR_START_IDX_SHIFT) + pdb_len) |
68 		     options);
69 }
70 
71 static inline void init_job_desc(u32 *desc, u32 options)
72 {
73 	init_desc(desc, CMD_DESC_HDR | options);
74 }
75 
76 static inline void append_ptr(u32 *desc, dma_addr_t ptr)
77 {
78 	dma_addr_t *offset = (dma_addr_t *)desc_end(desc);
79 
80 	*offset = ptr;
81 
82 	(*desc) += CAAM_PTR_SZ / CAAM_CMD_SZ;
83 }
84 
85 static inline void init_job_desc_shared(u32 *desc, dma_addr_t ptr, int len,
86 					u32 options)
87 {
88 	PRINT_POS;
89 	init_job_desc(desc, HDR_SHARED | options |
90 		      (len << HDR_START_IDX_SHIFT));
91 	append_ptr(desc, ptr);
92 }
93 
94 static inline void append_data(u32 *desc, void *data, int len)
95 {
96 	u32 *offset = desc_end(desc);
97 
98 	if (len) /* avoid sparse warning: memcpy with byte count of 0 */
99 		memcpy(offset, data, len);
100 
101 	(*desc) += (len + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
102 }
103 
104 static inline void append_cmd(u32 *desc, u32 command)
105 {
106 	u32 *cmd = desc_end(desc);
107 
108 	*cmd = command;
109 
110 	(*desc)++;
111 }
112 
113 static inline void append_cmd_ptr(u32 *desc, dma_addr_t ptr, int len,
114 				  u32 command)
115 {
116 	append_cmd(desc, command | len);
117 	append_ptr(desc, ptr);
118 }
119 
120 /* Write length after pointer, rather than inside command */
121 static inline void append_cmd_ptr_extlen(u32 *desc, dma_addr_t ptr,
122 					 unsigned int len, u32 command)
123 {
124 	append_cmd(desc, command);
125 	append_ptr(desc, ptr);
126 	append_cmd(desc, len);
127 }
128 
129 static inline void append_cmd_data(u32 *desc, void *data, int len,
130 				   u32 command)
131 {
132 	append_cmd(desc, command | IMMEDIATE | len);
133 	append_data(desc, data, len);
134 }
135 
136 static inline u32 *append_jump(u32 *desc, u32 options)
137 {
138 	u32 *cmd = desc_end(desc);
139 
140 	PRINT_POS;
141 	append_cmd(desc, CMD_JUMP | options);
142 
143 	return cmd;
144 }
145 
146 static inline void set_jump_tgt_here(u32 *desc, u32 *jump_cmd)
147 {
148 	*jump_cmd = *jump_cmd | (desc_len(desc) - (jump_cmd - desc));
149 }
150 
151 #define APPEND_CMD(cmd, op) \
152 static inline void append_##cmd(u32 *desc, u32 options) \
153 { \
154 	PRINT_POS; \
155 	append_cmd(desc, CMD_##op | options); \
156 }
157 APPEND_CMD(operation, OPERATION)
158 APPEND_CMD(move, MOVE)
159 
160 #define APPEND_CMD_LEN(cmd, op) \
161 static inline void append_##cmd(u32 *desc, unsigned int len, u32 options) \
162 { \
163 	PRINT_POS; \
164 	append_cmd(desc, CMD_##op | len | options); \
165 }
166 APPEND_CMD_LEN(seq_store, SEQ_STORE)
167 APPEND_CMD_LEN(seq_fifo_load, SEQ_FIFO_LOAD)
168 APPEND_CMD_LEN(seq_fifo_store, SEQ_FIFO_STORE)
169 
170 #define APPEND_CMD_PTR(cmd, op) \
171 static inline void append_##cmd(u32 *desc, dma_addr_t ptr, unsigned int len, \
172 				u32 options) \
173 { \
174 	PRINT_POS; \
175 	append_cmd_ptr(desc, ptr, len, CMD_##op | options); \
176 }
177 APPEND_CMD_PTR(key, KEY)
178 APPEND_CMD_PTR(load, LOAD)
179 APPEND_CMD_PTR(store, STORE)
180 APPEND_CMD_PTR(fifo_load, FIFO_LOAD)
181 APPEND_CMD_PTR(fifo_store, FIFO_STORE)
182 
183 #define APPEND_SEQ_PTR_INTLEN(cmd, op) \
184 static inline void append_seq_##cmd##_ptr_intlen(u32 *desc, dma_addr_t ptr, \
185 						 unsigned int len, \
186 						 u32 options) \
187 { \
188 	PRINT_POS; \
189 	append_cmd_ptr(desc, ptr, len, CMD_SEQ_##op##_PTR | options); \
190 }
191 APPEND_SEQ_PTR_INTLEN(in, IN)
192 APPEND_SEQ_PTR_INTLEN(out, OUT)
193 
194 #define APPEND_CMD_PTR_TO_IMM(cmd, op) \
195 static inline void append_##cmd##_as_imm(u32 *desc, void *data, \
196 					 unsigned int len, u32 options) \
197 { \
198 	PRINT_POS; \
199 	append_cmd_data(desc, data, len, CMD_##op | options); \
200 }
201 APPEND_CMD_PTR_TO_IMM(load, LOAD);
202 APPEND_CMD_PTR_TO_IMM(fifo_load, FIFO_LOAD);
203 
204 #define APPEND_CMD_PTR_EXTLEN(cmd, op) \
205 static inline void append_##cmd##_extlen(u32 *desc, dma_addr_t ptr, \
206 					 unsigned int len, u32 options) \
207 { \
208 	PRINT_POS; \
209 	append_cmd_ptr_extlen(desc, ptr, len, CMD_##op | SQIN_EXT | options); \
210 }
211 APPEND_CMD_PTR_EXTLEN(seq_in_ptr, SEQ_IN_PTR)
212 APPEND_CMD_PTR_EXTLEN(seq_out_ptr, SEQ_OUT_PTR)
213 
214 /*
215  * Determine whether to store length internally or externally depending on
216  * the size of its type
217  */
218 #define APPEND_CMD_PTR_LEN(cmd, op, type) \
219 static inline void append_##cmd(u32 *desc, dma_addr_t ptr, \
220 				type len, u32 options) \
221 { \
222 	PRINT_POS; \
223 	if (sizeof(type) > sizeof(u16)) \
224 		append_##cmd##_extlen(desc, ptr, len, options); \
225 	else \
226 		append_##cmd##_intlen(desc, ptr, len, options); \
227 }
228 APPEND_CMD_PTR_LEN(seq_in_ptr, SEQ_IN_PTR, u32)
229 APPEND_CMD_PTR_LEN(seq_out_ptr, SEQ_OUT_PTR, u32)
230 
231 /*
232  * 2nd variant for commands whose specified immediate length differs
233  * from length of immediate data provided, e.g., split keys
234  */
235 #define APPEND_CMD_PTR_TO_IMM2(cmd, op) \
236 static inline void append_##cmd##_as_imm(u32 *desc, void *data, \
237 					 unsigned int data_len, \
238 					 unsigned int len, u32 options) \
239 { \
240 	PRINT_POS; \
241 	append_cmd(desc, CMD_##op | IMMEDIATE | len | options); \
242 	append_data(desc, data, data_len); \
243 }
244 APPEND_CMD_PTR_TO_IMM2(key, KEY);
245 
246 #define APPEND_CMD_RAW_IMM(cmd, op, type) \
247 static inline void append_##cmd##_imm_##type(u32 *desc, type immediate, \
248 					     u32 options) \
249 { \
250 	PRINT_POS; \
251 	append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(type)); \
252 	append_cmd(desc, immediate); \
253 }
254 APPEND_CMD_RAW_IMM(load, LOAD, u32);
255 
256 /*
257  * Append math command. Only the last part of destination and source need to
258  * be specified
259  */
260 #define APPEND_MATH(op, desc, dest, src_0, src_1, len) \
261 append_cmd(desc, CMD_MATH | MATH_FUN_##op | MATH_DEST_##dest | \
262 	   MATH_SRC0_##src_0 | MATH_SRC1_##src_1 | (u32) (len & MATH_LEN_MASK));
263 
264 #define append_math_add(desc, dest, src0, src1, len) \
265 	APPEND_MATH(ADD, desc, dest, src0, src1, len)
266 #define append_math_sub(desc, dest, src0, src1, len) \
267 	APPEND_MATH(SUB, desc, dest, src0, src1, len)
268 #define append_math_add_c(desc, dest, src0, src1, len) \
269 	APPEND_MATH(ADDC, desc, dest, src0, src1, len)
270 #define append_math_sub_b(desc, dest, src0, src1, len) \
271 	APPEND_MATH(SUBB, desc, dest, src0, src1, len)
272 #define append_math_and(desc, dest, src0, src1, len) \
273 	APPEND_MATH(AND, desc, dest, src0, src1, len)
274 #define append_math_or(desc, dest, src0, src1, len) \
275 	APPEND_MATH(OR, desc, dest, src0, src1, len)
276 #define append_math_xor(desc, dest, src0, src1, len) \
277 	APPEND_MATH(XOR, desc, dest, src0, src1, len)
278 #define append_math_lshift(desc, dest, src0, src1, len) \
279 	APPEND_MATH(LSHIFT, desc, dest, src0, src1, len)
280 #define append_math_rshift(desc, dest, src0, src1, len) \
281 	APPEND_MATH(RSHIFT, desc, dest, src0, src1, len)
282 
283 /* Exactly one source is IMM. Data is passed in as u32 value */
284 #define APPEND_MATH_IMM_u32(op, desc, dest, src_0, src_1, data) \
285 do { \
286 	APPEND_MATH(op, desc, dest, src_0, src_1, CAAM_CMD_SZ); \
287 	append_cmd(desc, data); \
288 } while (0);
289 
290 #define append_math_add_imm_u32(desc, dest, src0, src1, data) \
291 	APPEND_MATH_IMM_u32(ADD, desc, dest, src0, src1, data)
292 #define append_math_sub_imm_u32(desc, dest, src0, src1, data) \
293 	APPEND_MATH_IMM_u32(SUB, desc, dest, src0, src1, data)
294 #define append_math_add_c_imm_u32(desc, dest, src0, src1, data) \
295 	APPEND_MATH_IMM_u32(ADDC, desc, dest, src0, src1, data)
296 #define append_math_sub_b_imm_u32(desc, dest, src0, src1, data) \
297 	APPEND_MATH_IMM_u32(SUBB, desc, dest, src0, src1, data)
298 #define append_math_and_imm_u32(desc, dest, src0, src1, data) \
299 	APPEND_MATH_IMM_u32(AND, desc, dest, src0, src1, data)
300 #define append_math_or_imm_u32(desc, dest, src0, src1, data) \
301 	APPEND_MATH_IMM_u32(OR, desc, dest, src0, src1, data)
302 #define append_math_xor_imm_u32(desc, dest, src0, src1, data) \
303 	APPEND_MATH_IMM_u32(XOR, desc, dest, src0, src1, data)
304 #define append_math_lshift_imm_u32(desc, dest, src0, src1, data) \
305 	APPEND_MATH_IMM_u32(LSHIFT, desc, dest, src0, src1, data)
306 #define append_math_rshift_imm_u32(desc, dest, src0, src1, data) \
307 	APPEND_MATH_IMM_u32(RSHIFT, desc, dest, src0, src1, data)
308