blob: 4ea93b03a2ab149b4137db8f236b44af209097ac [file] [log] [blame]
Ruchika Guptaac1b2692014-10-15 11:35:30 +05301/*
2 * caam descriptor construction helper functions
3 *
4 * Copyright 2008-2014 Freescale Semiconductor, Inc.
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 *
8 * Based on desc_constr.h file in linux drivers/crypto/caam
9 */
10
11#include <linux/compat.h>
12#include "desc.h"
13
14#define IMMEDIATE (1 << 23)
15#define CAAM_CMD_SZ sizeof(u32)
16#define CAAM_PTR_SZ sizeof(dma_addr_t)
17#define CAAM_DESC_BYTES_MAX (CAAM_CMD_SZ * MAX_CAAM_DESCSIZE)
18#define DESC_JOB_IO_LEN (CAAM_CMD_SZ * 5 + CAAM_PTR_SZ * 3)
19
20#ifdef DEBUG
21#define PRINT_POS do { printf("%02d: %s\n", desc_len(desc),\
22 &__func__[sizeof("append")]); \
23 } while (0)
24#else
25#define PRINT_POS
26#endif
27
28#define SET_OK_NO_PROP_ERRORS (IMMEDIATE | LDST_CLASS_DECO | \
29 LDST_SRCDST_WORD_DECOCTRL | \
30 (LDOFF_CHG_SHARE_OK_NO_PROP << \
31 LDST_OFFSET_SHIFT))
32#define DISABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
33 LDST_SRCDST_WORD_DECOCTRL | \
34 (LDOFF_DISABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
35#define ENABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
36 LDST_SRCDST_WORD_DECOCTRL | \
37 (LDOFF_ENABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
38
Aneesh Bansal43421822015-10-29 22:58:03 +053039#ifdef CONFIG_PHYS_64BIT
40union ptr_addr_t {
41 u64 m_whole;
42 struct {
43#ifdef CONFIG_SYS_FSL_SEC_LE
44 u32 low;
45 u32 high;
46#elif defined(CONFIG_SYS_FSL_SEC_BE)
47 u32 high;
48 u32 low;
49#else
50#error Neither CONFIG_SYS_FSL_SEC_LE nor CONFIG_SYS_FSL_SEC_BE is defined
51#endif
52 } m_halfs;
53};
54#endif
55
Aneesh Bansala2ecfff2016-02-15 15:12:56 +053056static inline void pdb_add_ptr(dma_addr_t *offset, dma_addr_t ptr)
57{
58#ifdef CONFIG_PHYS_64BIT
59 /* The Position of low and high part of 64 bit address
60 * will depend on the endianness of CAAM Block */
61 union ptr_addr_t *ptr_addr = (union ptr_addr_t *)offset;
62 ptr_addr->m_halfs.high = (u32)(ptr >> 32);
63 ptr_addr->m_halfs.low = (u32)ptr;
64#else
65 *offset = ptr;
66#endif
67}
68
Ruchika Guptaac1b2692014-10-15 11:35:30 +053069static inline int desc_len(u32 *desc)
70{
71 return *desc & HDR_DESCLEN_MASK;
72}
73
74static inline int desc_bytes(void *desc)
75{
76 return desc_len(desc) * CAAM_CMD_SZ;
77}
78
79static inline u32 *desc_end(u32 *desc)
80{
81 return desc + desc_len(desc);
82}
83
Aneesh Bansala2ecfff2016-02-15 15:12:56 +053084static inline void *desc_pdb(u32 *desc)
85{
86 return desc + 1;
87}
88
Ruchika Guptaac1b2692014-10-15 11:35:30 +053089static inline void init_desc(u32 *desc, u32 options)
90{
91 *desc = (options | HDR_ONE) + 1;
92}
93
94static inline void init_job_desc(u32 *desc, u32 options)
95{
96 init_desc(desc, CMD_DESC_HDR | options);
97}
98
Aneesh Bansala2ecfff2016-02-15 15:12:56 +053099static inline void init_job_desc_pdb(u32 *desc, u32 options, size_t pdb_bytes)
100{
101 u32 pdb_len = (pdb_bytes + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
102
103 init_job_desc(desc,
104 (((pdb_len + 1) << HDR_START_IDX_SHIFT) + pdb_len) |
105 options);
106}
107
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530108static inline void append_ptr(u32 *desc, dma_addr_t ptr)
109{
110 dma_addr_t *offset = (dma_addr_t *)desc_end(desc);
111
Aneesh Bansal43421822015-10-29 22:58:03 +0530112#ifdef CONFIG_PHYS_64BIT
113 /* The Position of low and high part of 64 bit address
114 * will depend on the endianness of CAAM Block */
115 union ptr_addr_t ptr_addr;
116 ptr_addr.m_halfs.high = (u32)(ptr >> 32);
117 ptr_addr.m_halfs.low = (u32)ptr;
118 *offset = ptr_addr.m_whole;
119#else
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530120 *offset = ptr;
Aneesh Bansal43421822015-10-29 22:58:03 +0530121#endif
Ruchika Guptaac1b2692014-10-15 11:35:30 +0530122
123 (*desc) += CAAM_PTR_SZ / CAAM_CMD_SZ;
124}
125
126static inline void append_data(u32 *desc, void *data, int len)
127{
128 u32 *offset = desc_end(desc);
129
130 if (len) /* avoid sparse warning: memcpy with byte count of 0 */
131 memcpy(offset, data, len);
132
133 (*desc) += (len + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
134}
135
136static inline void append_cmd(u32 *desc, u32 command)
137{
138 u32 *cmd = desc_end(desc);
139
140 *cmd = command;
141
142 (*desc)++;
143}
144
145#define append_u32 append_cmd
146
147static inline void append_u64(u32 *desc, u64 data)
148{
149 u32 *offset = desc_end(desc);
150
151 *offset = upper_32_bits(data);
152 *(++offset) = lower_32_bits(data);
153
154 (*desc) += 2;
155}
156
157/* Write command without affecting header, and return pointer to next word */
158static inline u32 *write_cmd(u32 *desc, u32 command)
159{
160 *desc = command;
161
162 return desc + 1;
163}
164
165static inline void append_cmd_ptr(u32 *desc, dma_addr_t ptr, int len,
166 u32 command)
167{
168 append_cmd(desc, command | len);
169 append_ptr(desc, ptr);
170}
171
172/* Write length after pointer, rather than inside command */
173static inline void append_cmd_ptr_extlen(u32 *desc, dma_addr_t ptr,
174 unsigned int len, u32 command)
175{
176 append_cmd(desc, command);
177 if (!(command & (SQIN_RTO | SQIN_PRE)))
178 append_ptr(desc, ptr);
179 append_cmd(desc, len);
180}
181
182static inline void append_cmd_data(u32 *desc, void *data, int len,
183 u32 command)
184{
185 append_cmd(desc, command | IMMEDIATE | len);
186 append_data(desc, data, len);
187}
188
189#define APPEND_CMD_RET(cmd, op) \
190static inline u32 *append_##cmd(u32 *desc, u32 options) \
191{ \
192 u32 *cmd = desc_end(desc); \
193 PRINT_POS; \
194 append_cmd(desc, CMD_##op | options); \
195 return cmd; \
196}
197APPEND_CMD_RET(jump, JUMP)
198APPEND_CMD_RET(move, MOVE)
199
200static inline void set_jump_tgt_here(u32 *desc, u32 *jump_cmd)
201{
202 *jump_cmd = *jump_cmd | (desc_len(desc) - (jump_cmd - desc));
203}
204
205static inline void set_move_tgt_here(u32 *desc, u32 *move_cmd)
206{
207 *move_cmd &= ~MOVE_OFFSET_MASK;
208 *move_cmd = *move_cmd | ((desc_len(desc) << (MOVE_OFFSET_SHIFT + 2)) &
209 MOVE_OFFSET_MASK);
210}
211
212#define APPEND_CMD(cmd, op) \
213static inline void append_##cmd(u32 *desc, u32 options) \
214{ \
215 PRINT_POS; \
216 append_cmd(desc, CMD_##op | options); \
217}
218APPEND_CMD(operation, OPERATION)
219
220#define APPEND_CMD_LEN(cmd, op) \
221static inline void append_##cmd(u32 *desc, unsigned int len, u32 options) \
222{ \
223 PRINT_POS; \
224 append_cmd(desc, CMD_##op | len | options); \
225}
226APPEND_CMD_LEN(seq_store, SEQ_STORE)
227APPEND_CMD_LEN(seq_fifo_load, SEQ_FIFO_LOAD)
228APPEND_CMD_LEN(seq_fifo_store, SEQ_FIFO_STORE)
229
230#define APPEND_CMD_PTR(cmd, op) \
231static inline void append_##cmd(u32 *desc, dma_addr_t ptr, unsigned int len, \
232 u32 options) \
233{ \
234 PRINT_POS; \
235 append_cmd_ptr(desc, ptr, len, CMD_##op | options); \
236}
237APPEND_CMD_PTR(key, KEY)
238APPEND_CMD_PTR(load, LOAD)
239APPEND_CMD_PTR(fifo_load, FIFO_LOAD)
240APPEND_CMD_PTR(fifo_store, FIFO_STORE)
241
242static inline void append_store(u32 *desc, dma_addr_t ptr, unsigned int len,
243 u32 options)
244{
245 u32 cmd_src;
246
247 cmd_src = options & LDST_SRCDST_MASK;
248
249 append_cmd(desc, CMD_STORE | options | len);
250
251 /* The following options do not require pointer */
252 if (!(cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED ||
253 cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB ||
254 cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB_WE ||
255 cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED_WE))
256 append_ptr(desc, ptr);
257}
258
259#define APPEND_SEQ_PTR_INTLEN(cmd, op) \
260static inline void append_seq_##cmd##_ptr_intlen(u32 *desc, dma_addr_t ptr, \
261 unsigned int len, \
262 u32 options) \
263{ \
264 PRINT_POS; \
265 if (options & (SQIN_RTO | SQIN_PRE)) \
266 append_cmd(desc, CMD_SEQ_##op##_PTR | len | options); \
267 else \
268 append_cmd_ptr(desc, ptr, len, CMD_SEQ_##op##_PTR | options); \
269}
270APPEND_SEQ_PTR_INTLEN(in, IN)
271APPEND_SEQ_PTR_INTLEN(out, OUT)
272
273#define APPEND_CMD_PTR_TO_IMM(cmd, op) \
274static inline void append_##cmd##_as_imm(u32 *desc, void *data, \
275 unsigned int len, u32 options) \
276{ \
277 PRINT_POS; \
278 append_cmd_data(desc, data, len, CMD_##op | options); \
279}
280APPEND_CMD_PTR_TO_IMM(load, LOAD);
281APPEND_CMD_PTR_TO_IMM(fifo_load, FIFO_LOAD);
282
283#define APPEND_CMD_PTR_EXTLEN(cmd, op) \
284static inline void append_##cmd##_extlen(u32 *desc, dma_addr_t ptr, \
285 unsigned int len, u32 options) \
286{ \
287 PRINT_POS; \
288 append_cmd_ptr_extlen(desc, ptr, len, CMD_##op | SQIN_EXT | options); \
289}
290APPEND_CMD_PTR_EXTLEN(seq_in_ptr, SEQ_IN_PTR)
291APPEND_CMD_PTR_EXTLEN(seq_out_ptr, SEQ_OUT_PTR)
292
293/*
294 * Determine whether to store length internally or externally depending on
295 * the size of its type
296 */
297#define APPEND_CMD_PTR_LEN(cmd, op, type) \
298static inline void append_##cmd(u32 *desc, dma_addr_t ptr, \
299 type len, u32 options) \
300{ \
301 PRINT_POS; \
302 if (sizeof(type) > sizeof(u16)) \
303 append_##cmd##_extlen(desc, ptr, len, options); \
304 else \
305 append_##cmd##_intlen(desc, ptr, len, options); \
306}
307APPEND_CMD_PTR_LEN(seq_in_ptr, SEQ_IN_PTR, u32)
308APPEND_CMD_PTR_LEN(seq_out_ptr, SEQ_OUT_PTR, u32)
309
310/*
311 * 2nd variant for commands whose specified immediate length differs
312 * from length of immediate data provided, e.g., split keys
313 */
314#define APPEND_CMD_PTR_TO_IMM2(cmd, op) \
315static inline void append_##cmd##_as_imm(u32 *desc, void *data, \
316 unsigned int data_len, \
317 unsigned int len, u32 options) \
318{ \
319 PRINT_POS; \
320 append_cmd(desc, CMD_##op | IMMEDIATE | len | options); \
321 append_data(desc, data, data_len); \
322}
323APPEND_CMD_PTR_TO_IMM2(key, KEY);
324
325#define APPEND_CMD_RAW_IMM(cmd, op, type) \
326static inline void append_##cmd##_imm_##type(u32 *desc, type immediate, \
327 u32 options) \
328{ \
329 PRINT_POS; \
330 append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(type)); \
331 append_cmd(desc, immediate); \
332}
333APPEND_CMD_RAW_IMM(load, LOAD, u32);