Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux/fpc-iii.git] / drivers / crypto / caam / desc_constr.h
blobcd5f678847ce3e7e93b1634a510bcd548abafe01
1 /*
2 * caam descriptor construction helper functions
4 * Copyright 2008-2012 Freescale Semiconductor, Inc.
5 */
7 #include "desc.h"
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)
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
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))
33 static inline int desc_len(u32 *desc)
35 return *desc & HDR_DESCLEN_MASK;
38 static inline int desc_bytes(void *desc)
40 return desc_len(desc) * CAAM_CMD_SZ;
43 static inline u32 *desc_end(u32 *desc)
45 return desc + desc_len(desc);
48 static inline void *sh_desc_pdb(u32 *desc)
50 return desc + 1;
53 static inline void init_desc(u32 *desc, u32 options)
55 *desc = (options | HDR_ONE) + 1;
58 static inline void init_sh_desc(u32 *desc, u32 options)
60 PRINT_POS;
61 init_desc(desc, CMD_SHARED_DESC_HDR | options);
64 static inline void init_sh_desc_pdb(u32 *desc, u32 options, size_t pdb_bytes)
66 u32 pdb_len = (pdb_bytes + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
68 init_sh_desc(desc, (((pdb_len + 1) << HDR_START_IDX_SHIFT) + pdb_len) |
69 options);
72 static inline void init_job_desc(u32 *desc, u32 options)
74 init_desc(desc, CMD_DESC_HDR | options);
77 static inline void append_ptr(u32 *desc, dma_addr_t ptr)
79 dma_addr_t *offset = (dma_addr_t *)desc_end(desc);
81 *offset = ptr;
83 (*desc) += CAAM_PTR_SZ / CAAM_CMD_SZ;
86 static inline void init_job_desc_shared(u32 *desc, dma_addr_t ptr, int len,
87 u32 options)
89 PRINT_POS;
90 init_job_desc(desc, HDR_SHARED | options |
91 (len << HDR_START_IDX_SHIFT));
92 append_ptr(desc, ptr);
95 static inline void append_data(u32 *desc, void *data, int len)
97 u32 *offset = desc_end(desc);
99 if (len) /* avoid sparse warning: memcpy with byte count of 0 */
100 memcpy(offset, data, len);
102 (*desc) += (len + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
105 static inline void append_cmd(u32 *desc, u32 command)
107 u32 *cmd = desc_end(desc);
109 *cmd = command;
111 (*desc)++;
114 #define append_u32 append_cmd
116 static inline void append_u64(u32 *desc, u64 data)
118 u32 *offset = desc_end(desc);
120 *offset = upper_32_bits(data);
121 *(++offset) = lower_32_bits(data);
123 (*desc) += 2;
126 /* Write command without affecting header, and return pointer to next word */
127 static inline u32 *write_cmd(u32 *desc, u32 command)
129 *desc = command;
131 return desc + 1;
134 static inline void append_cmd_ptr(u32 *desc, dma_addr_t ptr, int len,
135 u32 command)
137 append_cmd(desc, command | len);
138 append_ptr(desc, ptr);
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)
145 append_cmd(desc, command);
146 if (!(command & (SQIN_RTO | SQIN_PRE)))
147 append_ptr(desc, ptr);
148 append_cmd(desc, len);
151 static inline void append_cmd_data(u32 *desc, void *data, int len,
152 u32 command)
154 append_cmd(desc, command | IMMEDIATE | len);
155 append_data(desc, data, len);
158 static inline u32 *append_jump(u32 *desc, u32 options)
160 u32 *cmd = desc_end(desc);
162 PRINT_POS;
163 append_cmd(desc, CMD_JUMP | options);
165 return cmd;
168 static inline void set_jump_tgt_here(u32 *desc, u32 *jump_cmd)
170 *jump_cmd = *jump_cmd | (desc_len(desc) - (jump_cmd - desc));
173 #define APPEND_CMD(cmd, op) \
174 static inline void append_##cmd(u32 *desc, u32 options) \
176 PRINT_POS; \
177 append_cmd(desc, CMD_##op | options); \
179 APPEND_CMD(operation, OPERATION)
180 APPEND_CMD(move, MOVE)
182 #define APPEND_CMD_LEN(cmd, op) \
183 static inline void append_##cmd(u32 *desc, unsigned int len, u32 options) \
185 PRINT_POS; \
186 append_cmd(desc, CMD_##op | len | options); \
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)
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) \
196 PRINT_POS; \
197 append_cmd_ptr(desc, ptr, len, CMD_##op | options); \
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)
204 static inline void append_store(u32 *desc, dma_addr_t ptr, unsigned int len,
205 u32 options)
207 u32 cmd_src;
209 cmd_src = options & LDST_SRCDST_MASK;
211 append_cmd(desc, CMD_STORE | options | len);
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);
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) \
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); \
232 APPEND_SEQ_PTR_INTLEN(in, IN)
233 APPEND_SEQ_PTR_INTLEN(out, OUT)
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) \
239 PRINT_POS; \
240 append_cmd_data(desc, data, len, CMD_##op | options); \
242 APPEND_CMD_PTR_TO_IMM(load, LOAD);
243 APPEND_CMD_PTR_TO_IMM(fifo_load, FIFO_LOAD);
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) \
249 PRINT_POS; \
250 append_cmd_ptr_extlen(desc, ptr, len, CMD_##op | SQIN_EXT | options); \
252 APPEND_CMD_PTR_EXTLEN(seq_in_ptr, SEQ_IN_PTR)
253 APPEND_CMD_PTR_EXTLEN(seq_out_ptr, SEQ_OUT_PTR)
256 * Determine whether to store length internally or externally depending on
257 * the size of its type
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) \
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); \
269 APPEND_CMD_PTR_LEN(seq_in_ptr, SEQ_IN_PTR, u32)
270 APPEND_CMD_PTR_LEN(seq_out_ptr, SEQ_OUT_PTR, u32)
273 * 2nd variant for commands whose specified immediate length differs
274 * from length of immediate data provided, e.g., split keys
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) \
281 PRINT_POS; \
282 append_cmd(desc, CMD_##op | IMMEDIATE | len | options); \
283 append_data(desc, data, data_len); \
285 APPEND_CMD_PTR_TO_IMM2(key, KEY);
287 #define APPEND_CMD_RAW_IMM(cmd, op, type) \
288 static inline void append_##cmd##_imm_##type(u32 *desc, type immediate, \
289 u32 options) \
291 PRINT_POS; \
292 append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(type)); \
293 append_cmd(desc, immediate); \
295 APPEND_CMD_RAW_IMM(load, LOAD, u32);
298 * Append math command. Only the last part of destination and source need to
299 * be specified
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);
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)
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);
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)
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)
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)