blob: 209557c4ffa97de314917e625dfbf94af3a49741 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Ruchika Guptaac1b2692014-10-15 11:35:30 +05302/*
3 * caam descriptor construction helper functions
4 *
5 * Copyright 2008-2014 Freescale Semiconductor, Inc.
6 *
Ruchika Guptaac1b2692014-10-15 11:35:30 +05307 * Based on desc_constr.h file in linux drivers/crypto/caam
8 */
9
10#include <linux/compat.h>
11#include "desc.h"
12
13#define IMMEDIATE (1 << 23)
14#define CAAM_CMD_SZ sizeof(u32)
Ye Li3c3e9a12021-03-25 17:30:36 +080015#define CAAM_PTR_SZ sizeof(caam_dma_addr_t)
Ruchika Guptaac1b2692014-10-15 11:35:30 +053016#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)
18
19#ifdef DEBUG
20#define PRINT_POS do { printf("%02d: %s\n", desc_len(desc),\
21 &__func__[sizeof("append")]); \
22 } while (0)
23#else
24#define PRINT_POS
25#endif
26
27#define SET_OK_NO_PROP_ERRORS (IMMEDIATE | LDST_CLASS_DECO | \
28 LDST_SRCDST_WORD_DECOCTRL | \
29 (LDOFF_CHG_SHARE_OK_NO_PROP << \
30 LDST_OFFSET_SHIFT))
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))
37
Ye Li3c3e9a12021-03-25 17:30:36 +080038#ifdef CONFIG_CAAM_64BIT
Michael Wallef12c4462020-06-04 21:05:33 +020039struct ptr_addr_t {
Aneesh Bansal43421822015-10-29 22:58:03 +053040#ifdef CONFIG_SYS_FSL_SEC_LE
Michael Wallef12c4462020-06-04 21:05:33 +020041 u32 low;
42 u32 high;
Aneesh Bansal43421822015-10-29 22:58:03 +053043#elif defined(CONFIG_SYS_FSL_SEC_BE)
Michael Wallef12c4462020-06-04 21:05:33 +020044 u32 high;
45 u32 low;
Aneesh Bansal43421822015-10-29 22:58:03 +053046#else
47#error Neither CONFIG_SYS_FSL_SEC_LE nor CONFIG_SYS_FSL_SEC_BE is defined
48#endif
Aneesh Bansal43421822015-10-29 22:58:03 +053049};
50#endif
51
Ye Li3c3e9a12021-03-25 17:30:36 +080052static inline void pdb_add_ptr(caam_dma_addr_t *offset, caam_dma_addr_t ptr)
Aneesh Bansala2ecfff2016-02-15 15:12:56 +053053{
Ye Li3c3e9a12021-03-25 17:30:36 +080054#ifdef CONFIG_CAAM_64BIT
Aneesh Bansala2ecfff2016-02-15 15:12:56 +053055 /* The Position of low and high part of 64 bit address
56 * will depend on the endianness of CAAM Block */
Michael Wallef12c4462020-06-04 21:05:33 +020057 struct ptr_addr_t *ptr_addr = (struct ptr_addr_t *)offset;
58
59 ptr_addr->high = (u32)(ptr >> 32);
60 ptr_addr->low = (u32)ptr;
Aneesh Bansala2ecfff2016-02-15 15:12:56 +053061#else
62 *offset = ptr;
63#endif
64}
65
Ruchika Guptaac1b2692014-10-15 11:35:30 +053066static inline int desc_len(u32 *desc)
67{
68 return *desc & HDR_DESCLEN_MASK;
69}
70
71static inline int desc_bytes(void *desc)
72{
73 return desc_len(desc) * CAAM_CMD_SZ;
74}
75
76static inline u32 *desc_end(u32 *desc)
77{
78 return desc + desc_len(desc);
79}
80
Aneesh Bansala2ecfff2016-02-15 15:12:56 +053081static inline void *desc_pdb(u32 *desc)
82{
83 return desc + 1;
84}
85
Ruchika Guptaac1b2692014-10-15 11:35:30 +053086static inline void init_desc(u32 *desc, u32 options)
87{
88 *desc = (options | HDR_ONE) + 1;
89}
90
91static inline void init_job_desc(u32 *desc, u32 options)
92{
93 init_desc(desc, CMD_DESC_HDR | options);
94}
95
Aneesh Bansala2ecfff2016-02-15 15:12:56 +053096static inline void init_job_desc_pdb(u32 *desc, u32 options, size_t pdb_bytes)
97{
98 u32 pdb_len = (pdb_bytes + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
99
100 init_job_desc(desc,
101 (((pdb_len + 1) << HDR_START_IDX_SHIFT) + pdb_len) |
102 options);
103}
104
Ye Li3c3e9a12021-03-25 17:30:36 +0800105static inline void append_ptr(u32 *desc, caam_dma_addr_t ptr)
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530106{
Ye Li3c3e9a12021-03-25 17:30:36 +0800107 caam_dma_addr_t *offset = (caam_dma_addr_t *)desc_end(desc);
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530108
Ye Li3c3e9a12021-03-25 17:30:36 +0800109#ifdef CONFIG_CAAM_64BIT
Aneesh Bansal43421822015-10-29 22:58:03 +0530110 /* The Position of low and high part of 64 bit address
111 * will depend on the endianness of CAAM Block */
Michael Wallef12c4462020-06-04 21:05:33 +0200112 struct ptr_addr_t *ptr_addr = (struct ptr_addr_t *)offset;
113
114 ptr_addr->high = (u32)(ptr >> 32);
115 ptr_addr->low = (u32)ptr;
Aneesh Bansal43421822015-10-29 22:58:03 +0530116#else
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530117 *offset = ptr;
Aneesh Bansal43421822015-10-29 22:58:03 +0530118#endif
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530119
120 (*desc) += CAAM_PTR_SZ / CAAM_CMD_SZ;
121}
122
123static inline void append_data(u32 *desc, void *data, int len)
124{
125 u32 *offset = desc_end(desc);
126
127 if (len) /* avoid sparse warning: memcpy with byte count of 0 */
128 memcpy(offset, data, len);
129
130 (*desc) += (len + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
131}
132
133static inline void append_cmd(u32 *desc, u32 command)
134{
135 u32 *cmd = desc_end(desc);
136
137 *cmd = command;
138
139 (*desc)++;
140}
141
142#define append_u32 append_cmd
143
144static inline void append_u64(u32 *desc, u64 data)
145{
146 u32 *offset = desc_end(desc);
147
148 *offset = upper_32_bits(data);
149 *(++offset) = lower_32_bits(data);
150
151 (*desc) += 2;
152}
153
154/* Write command without affecting header, and return pointer to next word */
155static inline u32 *write_cmd(u32 *desc, u32 command)
156{
157 *desc = command;
158
159 return desc + 1;
160}
161
Ye Li3c3e9a12021-03-25 17:30:36 +0800162static inline void append_cmd_ptr(u32 *desc, caam_dma_addr_t ptr, int len,
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530163 u32 command)
164{
165 append_cmd(desc, command | len);
166 append_ptr(desc, ptr);
167}
168
169/* Write length after pointer, rather than inside command */
Ye Li3c3e9a12021-03-25 17:30:36 +0800170static inline void append_cmd_ptr_extlen(u32 *desc, caam_dma_addr_t ptr,
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530171 unsigned int len, u32 command)
172{
173 append_cmd(desc, command);
174 if (!(command & (SQIN_RTO | SQIN_PRE)))
175 append_ptr(desc, ptr);
176 append_cmd(desc, len);
177}
178
179static inline void append_cmd_data(u32 *desc, void *data, int len,
180 u32 command)
181{
182 append_cmd(desc, command | IMMEDIATE | len);
183 append_data(desc, data, len);
184}
185
186#define APPEND_CMD_RET(cmd, op) \
187static inline u32 *append_##cmd(u32 *desc, u32 options) \
188{ \
189 u32 *cmd = desc_end(desc); \
190 PRINT_POS; \
191 append_cmd(desc, CMD_##op | options); \
192 return cmd; \
193}
194APPEND_CMD_RET(jump, JUMP)
195APPEND_CMD_RET(move, MOVE)
196
197static inline void set_jump_tgt_here(u32 *desc, u32 *jump_cmd)
198{
199 *jump_cmd = *jump_cmd | (desc_len(desc) - (jump_cmd - desc));
200}
201
202static inline void set_move_tgt_here(u32 *desc, u32 *move_cmd)
203{
204 *move_cmd &= ~MOVE_OFFSET_MASK;
205 *move_cmd = *move_cmd | ((desc_len(desc) << (MOVE_OFFSET_SHIFT + 2)) &
206 MOVE_OFFSET_MASK);
207}
208
209#define APPEND_CMD(cmd, op) \
210static inline void append_##cmd(u32 *desc, u32 options) \
211{ \
212 PRINT_POS; \
213 append_cmd(desc, CMD_##op | options); \
214}
215APPEND_CMD(operation, OPERATION)
216
217#define APPEND_CMD_LEN(cmd, op) \
218static inline void append_##cmd(u32 *desc, unsigned int len, u32 options) \
219{ \
220 PRINT_POS; \
221 append_cmd(desc, CMD_##op | len | options); \
222}
223APPEND_CMD_LEN(seq_store, SEQ_STORE)
224APPEND_CMD_LEN(seq_fifo_load, SEQ_FIFO_LOAD)
225APPEND_CMD_LEN(seq_fifo_store, SEQ_FIFO_STORE)
226
227#define APPEND_CMD_PTR(cmd, op) \
Ye Li3c3e9a12021-03-25 17:30:36 +0800228static inline void append_##cmd(u32 *desc, caam_dma_addr_t ptr, unsigned int len, \
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530229 u32 options) \
230{ \
231 PRINT_POS; \
232 append_cmd_ptr(desc, ptr, len, CMD_##op | options); \
233}
234APPEND_CMD_PTR(key, KEY)
235APPEND_CMD_PTR(load, LOAD)
236APPEND_CMD_PTR(fifo_load, FIFO_LOAD)
237APPEND_CMD_PTR(fifo_store, FIFO_STORE)
238
Ye Li3c3e9a12021-03-25 17:30:36 +0800239static inline void append_store(u32 *desc, caam_dma_addr_t ptr, unsigned int len,
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530240 u32 options)
241{
242 u32 cmd_src;
243
244 cmd_src = options & LDST_SRCDST_MASK;
245
246 append_cmd(desc, CMD_STORE | options | len);
247
248 /* The following options do not require pointer */
249 if (!(cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED ||
250 cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB ||
251 cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB_WE ||
252 cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED_WE))
253 append_ptr(desc, ptr);
254}
255
256#define APPEND_SEQ_PTR_INTLEN(cmd, op) \
Ye Li3c3e9a12021-03-25 17:30:36 +0800257static inline void append_seq_##cmd##_ptr_intlen(u32 *desc, caam_dma_addr_t ptr, \
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530258 unsigned int len, \
259 u32 options) \
260{ \
261 PRINT_POS; \
262 if (options & (SQIN_RTO | SQIN_PRE)) \
263 append_cmd(desc, CMD_SEQ_##op##_PTR | len | options); \
264 else \
265 append_cmd_ptr(desc, ptr, len, CMD_SEQ_##op##_PTR | options); \
266}
267APPEND_SEQ_PTR_INTLEN(in, IN)
268APPEND_SEQ_PTR_INTLEN(out, OUT)
269
270#define APPEND_CMD_PTR_TO_IMM(cmd, op) \
271static inline void append_##cmd##_as_imm(u32 *desc, void *data, \
272 unsigned int len, u32 options) \
273{ \
274 PRINT_POS; \
275 append_cmd_data(desc, data, len, CMD_##op | options); \
276}
277APPEND_CMD_PTR_TO_IMM(load, LOAD);
278APPEND_CMD_PTR_TO_IMM(fifo_load, FIFO_LOAD);
279
280#define APPEND_CMD_PTR_EXTLEN(cmd, op) \
Ye Li3c3e9a12021-03-25 17:30:36 +0800281static inline void append_##cmd##_extlen(u32 *desc, caam_dma_addr_t ptr, \
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530282 unsigned int len, u32 options) \
283{ \
284 PRINT_POS; \
285 append_cmd_ptr_extlen(desc, ptr, len, CMD_##op | SQIN_EXT | options); \
286}
287APPEND_CMD_PTR_EXTLEN(seq_in_ptr, SEQ_IN_PTR)
288APPEND_CMD_PTR_EXTLEN(seq_out_ptr, SEQ_OUT_PTR)
289
290/*
291 * Determine whether to store length internally or externally depending on
292 * the size of its type
293 */
294#define APPEND_CMD_PTR_LEN(cmd, op, type) \
Ye Li3c3e9a12021-03-25 17:30:36 +0800295static inline void append_##cmd(u32 *desc, caam_dma_addr_t ptr, \
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530296 type len, u32 options) \
297{ \
298 PRINT_POS; \
299 if (sizeof(type) > sizeof(u16)) \
300 append_##cmd##_extlen(desc, ptr, len, options); \
301 else \
302 append_##cmd##_intlen(desc, ptr, len, options); \
303}
304APPEND_CMD_PTR_LEN(seq_in_ptr, SEQ_IN_PTR, u32)
305APPEND_CMD_PTR_LEN(seq_out_ptr, SEQ_OUT_PTR, u32)
306
307/*
308 * 2nd variant for commands whose specified immediate length differs
309 * from length of immediate data provided, e.g., split keys
310 */
311#define APPEND_CMD_PTR_TO_IMM2(cmd, op) \
312static inline void append_##cmd##_as_imm(u32 *desc, void *data, \
313 unsigned int data_len, \
314 unsigned int len, u32 options) \
315{ \
316 PRINT_POS; \
317 append_cmd(desc, CMD_##op | IMMEDIATE | len | options); \
318 append_data(desc, data, data_len); \
319}
320APPEND_CMD_PTR_TO_IMM2(key, KEY);
321
322#define APPEND_CMD_RAW_IMM(cmd, op, type) \
323static inline void append_##cmd##_imm_##type(u32 *desc, type immediate, \
324 u32 options) \
325{ \
326 PRINT_POS; \
327 append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(type)); \
328 append_cmd(desc, immediate); \
329}
330APPEND_CMD_RAW_IMM(load, LOAD, u32);