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