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 #define DESC_JOB_IO_LEN (CAAM_CMD_SZ * 5 + CAAM_PTR_SZ * 3)
14 
15 #ifdef DEBUG
16 #define PRINT_POS do { printk(KERN_DEBUG "%02d: %s\n", desc_len(desc),\
17 			      &__func__[sizeof("append")]); } while (0)
18 #else
19 #define PRINT_POS
20 #endif
21 
22 #define SET_OK_NO_PROP_ERRORS (IMMEDIATE | LDST_CLASS_DECO | \
23 			       LDST_SRCDST_WORD_DECOCTRL | \
24 			       (LDOFF_CHG_SHARE_OK_NO_PROP << \
25 				LDST_OFFSET_SHIFT))
26 #define DISABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
27 				LDST_SRCDST_WORD_DECOCTRL | \
28 				(LDOFF_DISABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
29 #define ENABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
30 			       LDST_SRCDST_WORD_DECOCTRL | \
31 			       (LDOFF_ENABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
32 
33 static inline int desc_len(u32 *desc)
34 {
35 	return *desc & HDR_DESCLEN_MASK;
36 }
37 
38 static inline int desc_bytes(void *desc)
39 {
40 	return desc_len(desc) * CAAM_CMD_SZ;
41 }
42 
43 static inline u32 *desc_end(u32 *desc)
44 {
45 	return desc + desc_len(desc);
46 }
47 
48 static inline void *sh_desc_pdb(u32 *desc)
49 {
50 	return desc + 1;
51 }
52 
53 static inline void init_desc(u32 *desc, u32 options)
54 {
55 	*desc = (options | HDR_ONE) + 1;
56 }
57 
58 static inline void init_sh_desc(u32 *desc, u32 options)
59 {
60 	PRINT_POS;
61 	init_desc(desc, CMD_SHARED_DESC_HDR | options);
62 }
63 
64 static inline void init_sh_desc_pdb(u32 *desc, u32 options, size_t pdb_bytes)
65 {
66 	u32 pdb_len = (pdb_bytes + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
67 
68 	init_sh_desc(desc, (((pdb_len + 1) << HDR_START_IDX_SHIFT) + pdb_len) |
69 		     options);
70 }
71 
72 static inline void init_job_desc(u32 *desc, u32 options)
73 {
74 	init_desc(desc, CMD_DESC_HDR | options);
75 }
76 
77 static inline void append_ptr(u32 *desc, dma_addr_t ptr)
78 {
79 	dma_addr_t *offset = (dma_addr_t *)desc_end(desc);
80 
81 	*offset = ptr;
82 
83 	(*desc) += CAAM_PTR_SZ / CAAM_CMD_SZ;
84 }
85 
86 static inline void init_job_desc_shared(u32 *desc, dma_addr_t ptr, int len,
87 					u32 options)
88 {
89 	PRINT_POS;
90 	init_job_desc(desc, HDR_SHARED | options |
91 		      (len << HDR_START_IDX_SHIFT));
92 	append_ptr(desc, ptr);
93 }
94 
95 static inline void append_data(u32 *desc, void *data, int len)
96 {
97 	u32 *offset = desc_end(desc);
98 
99 	if (len) /* avoid sparse warning: memcpy with byte count of 0 */
100 		memcpy(offset, data, len);
101 
102 	(*desc) += (len + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
103 }
104 
105 static inline void append_cmd(u32 *desc, u32 command)
106 {
107 	u32 *cmd = desc_end(desc);
108 
109 	*cmd = command;
110 
111 	(*desc)++;
112 }
113 
114 #define append_u32 append_cmd
115 
116 static inline void append_u64(u32 *desc, u64 data)
117 {
118 	u32 *offset = desc_end(desc);
119 
120 	*offset = upper_32_bits(data);
121 	*(++offset) = lower_32_bits(data);
122 
123 	(*desc) += 2;
124 }
125 
126 /* Write command without affecting header, and return pointer to next word */
127 static inline u32 *write_cmd(u32 *desc, u32 command)
128 {
129 	*desc = command;
130 
131 	return desc + 1;
132 }
133 
134 static inline void append_cmd_ptr(u32 *desc, dma_addr_t ptr, int len,
135 				  u32 command)
136 {
137 	append_cmd(desc, command | len);
138 	append_ptr(desc, ptr);
139 }
140 
141 /* Write length after pointer, rather than inside command */
142 static inline void append_cmd_ptr_extlen(u32 *desc, dma_addr_t ptr,
143 					 unsigned int len, u32 command)
144 {
145 	append_cmd(desc, command);
146 	if (!(command & (SQIN_RTO | SQIN_PRE)))
147 		append_ptr(desc, ptr);
148 	append_cmd(desc, len);
149 }
150 
151 static inline void append_cmd_data(u32 *desc, void *data, int len,
152 				   u32 command)
153 {
154 	append_cmd(desc, command | IMMEDIATE | len);
155 	append_data(desc, data, len);
156 }
157 
158 static inline u32 *append_jump(u32 *desc, u32 options)
159 {
160 	u32 *cmd = desc_end(desc);
161 
162 	PRINT_POS;
163 	append_cmd(desc, CMD_JUMP | options);
164 
165 	return cmd;
166 }
167 
168 static inline void set_jump_tgt_here(u32 *desc, u32 *jump_cmd)
169 {
170 	*jump_cmd = *jump_cmd | (desc_len(desc) - (jump_cmd - desc));
171 }
172 
173 #define APPEND_CMD(cmd, op) \
174 static inline void append_##cmd(u32 *desc, u32 options) \
175 { \
176 	PRINT_POS; \
177 	append_cmd(desc, CMD_##op | options); \
178 }
179 APPEND_CMD(operation, OPERATION)
180 APPEND_CMD(move, MOVE)
181 
182 #define APPEND_CMD_LEN(cmd, op) \
183 static inline void append_##cmd(u32 *desc, unsigned int len, u32 options) \
184 { \
185 	PRINT_POS; \
186 	append_cmd(desc, CMD_##op | len | options); \
187 }
188 APPEND_CMD_LEN(seq_store, SEQ_STORE)
189 APPEND_CMD_LEN(seq_fifo_load, SEQ_FIFO_LOAD)
190 APPEND_CMD_LEN(seq_fifo_store, SEQ_FIFO_STORE)
191 
192 #define APPEND_CMD_PTR(cmd, op) \
193 static inline void append_##cmd(u32 *desc, dma_addr_t ptr, unsigned int len, \
194 				u32 options) \
195 { \
196 	PRINT_POS; \
197 	append_cmd_ptr(desc, ptr, len, CMD_##op | options); \
198 }
199 APPEND_CMD_PTR(key, KEY)
200 APPEND_CMD_PTR(load, LOAD)
201 APPEND_CMD_PTR(fifo_load, FIFO_LOAD)
202 APPEND_CMD_PTR(fifo_store, FIFO_STORE)
203 
204 static inline void append_store(u32 *desc, dma_addr_t ptr, unsigned int len,
205 				u32 options)
206 {
207 	u32 cmd_src;
208 
209 	cmd_src = options & LDST_SRCDST_MASK;
210 
211 	append_cmd(desc, CMD_STORE | options | len);
212 
213 	/* The following options do not require pointer */
214 	if (!(cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED ||
215 	      cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB    ||
216 	      cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB_WE ||
217 	      cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED_WE))
218 		append_ptr(desc, ptr);
219 }
220 
221 #define APPEND_SEQ_PTR_INTLEN(cmd, op) \
222 static inline void append_seq_##cmd##_ptr_intlen(u32 *desc, dma_addr_t ptr, \
223 						 unsigned int len, \
224 						 u32 options) \
225 { \
226 	PRINT_POS; \
227 	if (options & (SQIN_RTO | SQIN_PRE)) \
228 		append_cmd(desc, CMD_SEQ_##op##_PTR | len | options); \
229 	else \
230 		append_cmd_ptr(desc, ptr, len, CMD_SEQ_##op##_PTR | options); \
231 }
232 APPEND_SEQ_PTR_INTLEN(in, IN)
233 APPEND_SEQ_PTR_INTLEN(out, OUT)
234 
235 #define APPEND_CMD_PTR_TO_IMM(cmd, op) \
236 static inline void append_##cmd##_as_imm(u32 *desc, void *data, \
237 					 unsigned int len, u32 options) \
238 { \
239 	PRINT_POS; \
240 	append_cmd_data(desc, data, len, CMD_##op | options); \
241 }
242 APPEND_CMD_PTR_TO_IMM(load, LOAD);
243 APPEND_CMD_PTR_TO_IMM(fifo_load, FIFO_LOAD);
244 
245 #define APPEND_CMD_PTR_EXTLEN(cmd, op) \
246 static inline void append_##cmd##_extlen(u32 *desc, dma_addr_t ptr, \
247 					 unsigned int len, u32 options) \
248 { \
249 	PRINT_POS; \
250 	append_cmd_ptr_extlen(desc, ptr, len, CMD_##op | SQIN_EXT | options); \
251 }
252 APPEND_CMD_PTR_EXTLEN(seq_in_ptr, SEQ_IN_PTR)
253 APPEND_CMD_PTR_EXTLEN(seq_out_ptr, SEQ_OUT_PTR)
254 
255 /*
256  * Determine whether to store length internally or externally depending on
257  * the size of its type
258  */
259 #define APPEND_CMD_PTR_LEN(cmd, op, type) \
260 static inline void append_##cmd(u32 *desc, dma_addr_t ptr, \
261 				type len, u32 options) \
262 { \
263 	PRINT_POS; \
264 	if (sizeof(type) > sizeof(u16)) \
265 		append_##cmd##_extlen(desc, ptr, len, options); \
266 	else \
267 		append_##cmd##_intlen(desc, ptr, len, options); \
268 }
269 APPEND_CMD_PTR_LEN(seq_in_ptr, SEQ_IN_PTR, u32)
270 APPEND_CMD_PTR_LEN(seq_out_ptr, SEQ_OUT_PTR, u32)
271 
272 /*
273  * 2nd variant for commands whose specified immediate length differs
274  * from length of immediate data provided, e.g., split keys
275  */
276 #define APPEND_CMD_PTR_TO_IMM2(cmd, op) \
277 static inline void append_##cmd##_as_imm(u32 *desc, void *data, \
278 					 unsigned int data_len, \
279 					 unsigned int len, u32 options) \
280 { \
281 	PRINT_POS; \
282 	append_cmd(desc, CMD_##op | IMMEDIATE | len | options); \
283 	append_data(desc, data, data_len); \
284 }
285 APPEND_CMD_PTR_TO_IMM2(key, KEY);
286 
287 #define APPEND_CMD_RAW_IMM(cmd, op, type) \
288 static inline void append_##cmd##_imm_##type(u32 *desc, type immediate, \
289 					     u32 options) \
290 { \
291 	PRINT_POS; \
292 	append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(type)); \
293 	append_cmd(desc, immediate); \
294 }
295 APPEND_CMD_RAW_IMM(load, LOAD, u32);
296 
297 /*
298  * Append math command. Only the last part of destination and source need to
299  * be specified
300  */
301 #define APPEND_MATH(op, desc, dest, src_0, src_1, len) \
302 append_cmd(desc, CMD_MATH | MATH_FUN_##op | MATH_DEST_##dest | \
303 	MATH_SRC0_##src_0 | MATH_SRC1_##src_1 | (u32)len);
304 
305 #define append_math_add(desc, dest, src0, src1, len) \
306 	APPEND_MATH(ADD, desc, dest, src0, src1, len)
307 #define append_math_sub(desc, dest, src0, src1, len) \
308 	APPEND_MATH(SUB, desc, dest, src0, src1, len)
309 #define append_math_add_c(desc, dest, src0, src1, len) \
310 	APPEND_MATH(ADDC, desc, dest, src0, src1, len)
311 #define append_math_sub_b(desc, dest, src0, src1, len) \
312 	APPEND_MATH(SUBB, desc, dest, src0, src1, len)
313 #define append_math_and(desc, dest, src0, src1, len) \
314 	APPEND_MATH(AND, desc, dest, src0, src1, len)
315 #define append_math_or(desc, dest, src0, src1, len) \
316 	APPEND_MATH(OR, desc, dest, src0, src1, len)
317 #define append_math_xor(desc, dest, src0, src1, len) \
318 	APPEND_MATH(XOR, desc, dest, src0, src1, len)
319 #define append_math_lshift(desc, dest, src0, src1, len) \
320 	APPEND_MATH(LSHIFT, desc, dest, src0, src1, len)
321 #define append_math_rshift(desc, dest, src0, src1, len) \
322 	APPEND_MATH(RSHIFT, desc, dest, src0, src1, len)
323 #define append_math_ldshift(desc, dest, src0, src1, len) \
324 	APPEND_MATH(SHLD, desc, dest, src0, src1, len)
325 
326 /* Exactly one source is IMM. Data is passed in as u32 value */
327 #define APPEND_MATH_IMM_u32(op, desc, dest, src_0, src_1, data) \
328 do { \
329 	APPEND_MATH(op, desc, dest, src_0, src_1, CAAM_CMD_SZ); \
330 	append_cmd(desc, data); \
331 } while (0);
332 
333 #define append_math_add_imm_u32(desc, dest, src0, src1, data) \
334 	APPEND_MATH_IMM_u32(ADD, desc, dest, src0, src1, data)
335 #define append_math_sub_imm_u32(desc, dest, src0, src1, data) \
336 	APPEND_MATH_IMM_u32(SUB, desc, dest, src0, src1, data)
337 #define append_math_add_c_imm_u32(desc, dest, src0, src1, data) \
338 	APPEND_MATH_IMM_u32(ADDC, desc, dest, src0, src1, data)
339 #define append_math_sub_b_imm_u32(desc, dest, src0, src1, data) \
340 	APPEND_MATH_IMM_u32(SUBB, desc, dest, src0, src1, data)
341 #define append_math_and_imm_u32(desc, dest, src0, src1, data) \
342 	APPEND_MATH_IMM_u32(AND, desc, dest, src0, src1, data)
343 #define append_math_or_imm_u32(desc, dest, src0, src1, data) \
344 	APPEND_MATH_IMM_u32(OR, desc, dest, src0, src1, data)
345 #define append_math_xor_imm_u32(desc, dest, src0, src1, data) \
346 	APPEND_MATH_IMM_u32(XOR, desc, dest, src0, src1, data)
347 #define append_math_lshift_imm_u32(desc, dest, src0, src1, data) \
348 	APPEND_MATH_IMM_u32(LSHIFT, desc, dest, src0, src1, data)
349 #define append_math_rshift_imm_u32(desc, dest, src0, src1, data) \
350 	APPEND_MATH_IMM_u32(RSHIFT, desc, dest, src0, src1, data)
351 
352 /* Exactly one source is IMM. Data is passed in as u64 value */
353 #define APPEND_MATH_IMM_u64(op, desc, dest, src_0, src_1, data) \
354 do { \
355 	u32 upper = (data >> 16) >> 16; \
356 	APPEND_MATH(op, desc, dest, src_0, src_1, CAAM_CMD_SZ * 2 | \
357 		    (upper ? 0 : MATH_IFB)); \
358 	if (upper) \
359 		append_u64(desc, data); \
360 	else \
361 		append_u32(desc, data); \
362 } while (0)
363 
364 #define append_math_add_imm_u64(desc, dest, src0, src1, data) \
365 	APPEND_MATH_IMM_u64(ADD, desc, dest, src0, src1, data)
366 #define append_math_sub_imm_u64(desc, dest, src0, src1, data) \
367 	APPEND_MATH_IMM_u64(SUB, desc, dest, src0, src1, data)
368 #define append_math_add_c_imm_u64(desc, dest, src0, src1, data) \
369 	APPEND_MATH_IMM_u64(ADDC, desc, dest, src0, src1, data)
370 #define append_math_sub_b_imm_u64(desc, dest, src0, src1, data) \
371 	APPEND_MATH_IMM_u64(SUBB, desc, dest, src0, src1, data)
372 #define append_math_and_imm_u64(desc, dest, src0, src1, data) \
373 	APPEND_MATH_IMM_u64(AND, desc, dest, src0, src1, data)
374 #define append_math_or_imm_u64(desc, dest, src0, src1, data) \
375 	APPEND_MATH_IMM_u64(OR, desc, dest, src0, src1, data)
376 #define append_math_xor_imm_u64(desc, dest, src0, src1, data) \
377 	APPEND_MATH_IMM_u64(XOR, desc, dest, src0, src1, data)
378 #define append_math_lshift_imm_u64(desc, dest, src0, src1, data) \
379 	APPEND_MATH_IMM_u64(LSHIFT, desc, dest, src0, src1, data)
380 #define append_math_rshift_imm_u64(desc, dest, src0, src1, data) \
381 	APPEND_MATH_IMM_u64(RSHIFT, desc, dest, src0, src1, data)
382