1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * caam descriptor construction helper functions
5 * Copyright 2008-2014 Freescale Semiconductor, Inc.
7 * Based on desc_constr.h file in linux drivers/crypto/caam
10 #include <linux/compat.h>
13 #define IMMEDIATE (1 << 23)
14 #define CAAM_CMD_SZ sizeof(u32)
15 #define CAAM_PTR_SZ sizeof(dma_addr_t)
16 #define CAAM_DESC_BYTES_MAX (CAAM_CMD_SZ * MAX_CAAM_DESCSIZE)
17 #define DESC_JOB_IO_LEN (CAAM_CMD_SZ * 5 + CAAM_PTR_SZ * 3)
20 #define PRINT_POS do { printf("%02d: %s\n", desc_len(desc),\
21 &__func__[sizeof("append")]); \
27 #define SET_OK_NO_PROP_ERRORS (IMMEDIATE | LDST_CLASS_DECO | \
28 LDST_SRCDST_WORD_DECOCTRL | \
29 (LDOFF_CHG_SHARE_OK_NO_PROP << \
31 #define DISABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
32 LDST_SRCDST_WORD_DECOCTRL | \
33 (LDOFF_DISABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
34 #define ENABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
35 LDST_SRCDST_WORD_DECOCTRL | \
36 (LDOFF_ENABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
38 #ifdef CONFIG_PHYS_64BIT
42 #ifdef CONFIG_SYS_FSL_SEC_LE
45 #elif defined(CONFIG_SYS_FSL_SEC_BE)
49 #error Neither CONFIG_SYS_FSL_SEC_LE nor CONFIG_SYS_FSL_SEC_BE is defined
55 static inline void pdb_add_ptr(dma_addr_t *offset, dma_addr_t ptr)
57 #ifdef CONFIG_PHYS_64BIT
58 /* The Position of low and high part of 64 bit address
59 * will depend on the endianness of CAAM Block */
60 union ptr_addr_t *ptr_addr = (union ptr_addr_t *)offset;
61 ptr_addr->m_halfs.high = (u32)(ptr >> 32);
62 ptr_addr->m_halfs.low = (u32)ptr;
68 static inline int desc_len(u32 *desc)
70 return *desc & HDR_DESCLEN_MASK;
73 static inline int desc_bytes(void *desc)
75 return desc_len(desc) * CAAM_CMD_SZ;
78 static inline u32 *desc_end(u32 *desc)
80 return desc + desc_len(desc);
83 static inline void *desc_pdb(u32 *desc)
88 static inline void init_desc(u32 *desc, u32 options)
90 *desc = (options | HDR_ONE) + 1;
93 static inline void init_job_desc(u32 *desc, u32 options)
95 init_desc(desc, CMD_DESC_HDR | options);
98 static inline void init_job_desc_pdb(u32 *desc, u32 options, size_t pdb_bytes)
100 u32 pdb_len = (pdb_bytes + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
103 (((pdb_len + 1) << HDR_START_IDX_SHIFT) + pdb_len) |
107 static inline void append_ptr(u32 *desc, dma_addr_t ptr)
109 dma_addr_t *offset = (dma_addr_t *)desc_end(desc);
111 #ifdef CONFIG_PHYS_64BIT
112 /* The Position of low and high part of 64 bit address
113 * will depend on the endianness of CAAM Block */
114 union ptr_addr_t *ptr_addr = (union ptr_addr_t *)offset;
115 ptr_addr->m_halfs.high = (u32)(ptr >> 32);
116 ptr_addr->m_halfs.low = (u32)ptr;
121 (*desc) += CAAM_PTR_SZ / CAAM_CMD_SZ;
124 static inline void append_data(u32 *desc, void *data, int len)
126 u32 *offset = desc_end(desc);
128 if (len) /* avoid sparse warning: memcpy with byte count of 0 */
129 memcpy(offset, data, len);
131 (*desc) += (len + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
134 static inline void append_cmd(u32 *desc, u32 command)
136 u32 *cmd = desc_end(desc);
143 #define append_u32 append_cmd
145 static inline void append_u64(u32 *desc, u64 data)
147 u32 *offset = desc_end(desc);
149 *offset = upper_32_bits(data);
150 *(++offset) = lower_32_bits(data);
155 /* Write command without affecting header, and return pointer to next word */
156 static inline u32 *write_cmd(u32 *desc, u32 command)
163 static inline void append_cmd_ptr(u32 *desc, dma_addr_t ptr, int len,
166 append_cmd(desc, command | len);
167 append_ptr(desc, ptr);
170 /* Write length after pointer, rather than inside command */
171 static inline void append_cmd_ptr_extlen(u32 *desc, dma_addr_t ptr,
172 unsigned int len, u32 command)
174 append_cmd(desc, command);
175 if (!(command & (SQIN_RTO | SQIN_PRE)))
176 append_ptr(desc, ptr);
177 append_cmd(desc, len);
180 static inline void append_cmd_data(u32 *desc, void *data, int len,
183 append_cmd(desc, command | IMMEDIATE | len);
184 append_data(desc, data, len);
187 #define APPEND_CMD_RET(cmd, op) \
188 static inline u32 *append_##cmd(u32 *desc, u32 options) \
190 u32 *cmd = desc_end(desc); \
192 append_cmd(desc, CMD_##op | options); \
195 APPEND_CMD_RET(jump, JUMP)
196 APPEND_CMD_RET(move, MOVE)
198 static inline void set_jump_tgt_here(u32 *desc, u32 *jump_cmd)
200 *jump_cmd = *jump_cmd | (desc_len(desc) - (jump_cmd - desc));
203 static inline void set_move_tgt_here(u32 *desc, u32 *move_cmd)
205 *move_cmd &= ~MOVE_OFFSET_MASK;
206 *move_cmd = *move_cmd | ((desc_len(desc) << (MOVE_OFFSET_SHIFT + 2)) &
210 #define APPEND_CMD(cmd, op) \
211 static inline void append_##cmd(u32 *desc, u32 options) \
214 append_cmd(desc, CMD_##op | options); \
216 APPEND_CMD(operation, OPERATION)
218 #define APPEND_CMD_LEN(cmd, op) \
219 static inline void append_##cmd(u32 *desc, unsigned int len, u32 options) \
222 append_cmd(desc, CMD_##op | len | options); \
224 APPEND_CMD_LEN(seq_store, SEQ_STORE)
225 APPEND_CMD_LEN(seq_fifo_load, SEQ_FIFO_LOAD)
226 APPEND_CMD_LEN(seq_fifo_store, SEQ_FIFO_STORE)
228 #define APPEND_CMD_PTR(cmd, op) \
229 static inline void append_##cmd(u32 *desc, dma_addr_t ptr, unsigned int len, \
233 append_cmd_ptr(desc, ptr, len, CMD_##op | options); \
235 APPEND_CMD_PTR(key, KEY)
236 APPEND_CMD_PTR(load, LOAD)
237 APPEND_CMD_PTR(fifo_load, FIFO_LOAD)
238 APPEND_CMD_PTR(fifo_store, FIFO_STORE)
240 static inline void append_store(u32 *desc, dma_addr_t ptr, unsigned int len,
245 cmd_src = options & LDST_SRCDST_MASK;
247 append_cmd(desc, CMD_STORE | options | len);
249 /* The following options do not require pointer */
250 if (!(cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED ||
251 cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB ||
252 cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB_WE ||
253 cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED_WE))
254 append_ptr(desc, ptr);
257 #define APPEND_SEQ_PTR_INTLEN(cmd, op) \
258 static inline void append_seq_##cmd##_ptr_intlen(u32 *desc, dma_addr_t ptr, \
263 if (options & (SQIN_RTO | SQIN_PRE)) \
264 append_cmd(desc, CMD_SEQ_##op##_PTR | len | options); \
266 append_cmd_ptr(desc, ptr, len, CMD_SEQ_##op##_PTR | options); \
268 APPEND_SEQ_PTR_INTLEN(in, IN)
269 APPEND_SEQ_PTR_INTLEN(out, OUT)
271 #define APPEND_CMD_PTR_TO_IMM(cmd, op) \
272 static inline void append_##cmd##_as_imm(u32 *desc, void *data, \
273 unsigned int len, u32 options) \
276 append_cmd_data(desc, data, len, CMD_##op | options); \
278 APPEND_CMD_PTR_TO_IMM(load, LOAD);
279 APPEND_CMD_PTR_TO_IMM(fifo_load, FIFO_LOAD);
281 #define APPEND_CMD_PTR_EXTLEN(cmd, op) \
282 static inline void append_##cmd##_extlen(u32 *desc, dma_addr_t ptr, \
283 unsigned int len, u32 options) \
286 append_cmd_ptr_extlen(desc, ptr, len, CMD_##op | SQIN_EXT | options); \
288 APPEND_CMD_PTR_EXTLEN(seq_in_ptr, SEQ_IN_PTR)
289 APPEND_CMD_PTR_EXTLEN(seq_out_ptr, SEQ_OUT_PTR)
292 * Determine whether to store length internally or externally depending on
293 * the size of its type
295 #define APPEND_CMD_PTR_LEN(cmd, op, type) \
296 static inline void append_##cmd(u32 *desc, dma_addr_t ptr, \
297 type len, u32 options) \
300 if (sizeof(type) > sizeof(u16)) \
301 append_##cmd##_extlen(desc, ptr, len, options); \
303 append_##cmd##_intlen(desc, ptr, len, options); \
305 APPEND_CMD_PTR_LEN(seq_in_ptr, SEQ_IN_PTR, u32)
306 APPEND_CMD_PTR_LEN(seq_out_ptr, SEQ_OUT_PTR, u32)
309 * 2nd variant for commands whose specified immediate length differs
310 * from length of immediate data provided, e.g., split keys
312 #define APPEND_CMD_PTR_TO_IMM2(cmd, op) \
313 static inline void append_##cmd##_as_imm(u32 *desc, void *data, \
314 unsigned int data_len, \
315 unsigned int len, u32 options) \
318 append_cmd(desc, CMD_##op | IMMEDIATE | len | options); \
319 append_data(desc, data, data_len); \
321 APPEND_CMD_PTR_TO_IMM2(key, KEY);
323 #define APPEND_CMD_RAW_IMM(cmd, op, type) \
324 static inline void append_##cmd##_imm_##type(u32 *desc, type immediate, \
328 append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(type)); \
329 append_cmd(desc, immediate); \
331 APPEND_CMD_RAW_IMM(load, LOAD, u32);