blob: f9cae9144acd17e1fca6e7533c9f794c8b3700fe [file] [log] [blame]
Ruchika Guptab9eebfa2014-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
39static inline int desc_len(u32 *desc)
40{
41 return *desc & HDR_DESCLEN_MASK;
42}
43
44static inline int desc_bytes(void *desc)
45{
46 return desc_len(desc) * CAAM_CMD_SZ;
47}
48
49static inline u32 *desc_end(u32 *desc)
50{
51 return desc + desc_len(desc);
52}
53
54static inline void init_desc(u32 *desc, u32 options)
55{
56 *desc = (options | HDR_ONE) + 1;
57}
58
59static inline void init_job_desc(u32 *desc, u32 options)
60{
61 init_desc(desc, CMD_DESC_HDR | options);
62}
63
64static inline void append_ptr(u32 *desc, dma_addr_t ptr)
65{
66 dma_addr_t *offset = (dma_addr_t *)desc_end(desc);
67
68 *offset = ptr;
69
70 (*desc) += CAAM_PTR_SZ / CAAM_CMD_SZ;
71}
72
73static inline void append_data(u32 *desc, void *data, int len)
74{
75 u32 *offset = desc_end(desc);
76
77 if (len) /* avoid sparse warning: memcpy with byte count of 0 */
78 memcpy(offset, data, len);
79
80 (*desc) += (len + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
81}
82
83static inline void append_cmd(u32 *desc, u32 command)
84{
85 u32 *cmd = desc_end(desc);
86
87 *cmd = command;
88
89 (*desc)++;
90}
91
92#define append_u32 append_cmd
93
94static inline void append_u64(u32 *desc, u64 data)
95{
96 u32 *offset = desc_end(desc);
97
98 *offset = upper_32_bits(data);
99 *(++offset) = lower_32_bits(data);
100
101 (*desc) += 2;
102}
103
104/* Write command without affecting header, and return pointer to next word */
105static inline u32 *write_cmd(u32 *desc, u32 command)
106{
107 *desc = command;
108
109 return desc + 1;
110}
111
112static inline void append_cmd_ptr(u32 *desc, dma_addr_t ptr, int len,
113 u32 command)
114{
115 append_cmd(desc, command | len);
116 append_ptr(desc, ptr);
117}
118
119/* Write length after pointer, rather than inside command */
120static inline void append_cmd_ptr_extlen(u32 *desc, dma_addr_t ptr,
121 unsigned int len, u32 command)
122{
123 append_cmd(desc, command);
124 if (!(command & (SQIN_RTO | SQIN_PRE)))
125 append_ptr(desc, ptr);
126 append_cmd(desc, len);
127}
128
129static inline void append_cmd_data(u32 *desc, void *data, int len,
130 u32 command)
131{
132 append_cmd(desc, command | IMMEDIATE | len);
133 append_data(desc, data, len);
134}
135
136#define APPEND_CMD_RET(cmd, op) \
137static inline u32 *append_##cmd(u32 *desc, u32 options) \
138{ \
139 u32 *cmd = desc_end(desc); \
140 PRINT_POS; \
141 append_cmd(desc, CMD_##op | options); \
142 return cmd; \
143}
144APPEND_CMD_RET(jump, JUMP)
145APPEND_CMD_RET(move, MOVE)
146
147static inline void set_jump_tgt_here(u32 *desc, u32 *jump_cmd)
148{
149 *jump_cmd = *jump_cmd | (desc_len(desc) - (jump_cmd - desc));
150}
151
152static inline void set_move_tgt_here(u32 *desc, u32 *move_cmd)
153{
154 *move_cmd &= ~MOVE_OFFSET_MASK;
155 *move_cmd = *move_cmd | ((desc_len(desc) << (MOVE_OFFSET_SHIFT + 2)) &
156 MOVE_OFFSET_MASK);
157}
158
159#define APPEND_CMD(cmd, op) \
160static inline void append_##cmd(u32 *desc, u32 options) \
161{ \
162 PRINT_POS; \
163 append_cmd(desc, CMD_##op | options); \
164}
165APPEND_CMD(operation, OPERATION)
166
167#define APPEND_CMD_LEN(cmd, op) \
168static inline void append_##cmd(u32 *desc, unsigned int len, u32 options) \
169{ \
170 PRINT_POS; \
171 append_cmd(desc, CMD_##op | len | options); \
172}
173APPEND_CMD_LEN(seq_store, SEQ_STORE)
174APPEND_CMD_LEN(seq_fifo_load, SEQ_FIFO_LOAD)
175APPEND_CMD_LEN(seq_fifo_store, SEQ_FIFO_STORE)
176
177#define APPEND_CMD_PTR(cmd, op) \
178static inline void append_##cmd(u32 *desc, dma_addr_t ptr, unsigned int len, \
179 u32 options) \
180{ \
181 PRINT_POS; \
182 append_cmd_ptr(desc, ptr, len, CMD_##op | options); \
183}
184APPEND_CMD_PTR(key, KEY)
185APPEND_CMD_PTR(load, LOAD)
186APPEND_CMD_PTR(fifo_load, FIFO_LOAD)
187APPEND_CMD_PTR(fifo_store, FIFO_STORE)
188
189static inline void append_store(u32 *desc, dma_addr_t ptr, unsigned int len,
190 u32 options)
191{
192 u32 cmd_src;
193
194 cmd_src = options & LDST_SRCDST_MASK;
195
196 append_cmd(desc, CMD_STORE | options | len);
197
198 /* The following options do not require pointer */
199 if (!(cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED ||
200 cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB ||
201 cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB_WE ||
202 cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED_WE))
203 append_ptr(desc, ptr);
204}
205
206#define APPEND_SEQ_PTR_INTLEN(cmd, op) \
207static inline void append_seq_##cmd##_ptr_intlen(u32 *desc, dma_addr_t ptr, \
208 unsigned int len, \
209 u32 options) \
210{ \
211 PRINT_POS; \
212 if (options & (SQIN_RTO | SQIN_PRE)) \
213 append_cmd(desc, CMD_SEQ_##op##_PTR | len | options); \
214 else \
215 append_cmd_ptr(desc, ptr, len, CMD_SEQ_##op##_PTR | options); \
216}
217APPEND_SEQ_PTR_INTLEN(in, IN)
218APPEND_SEQ_PTR_INTLEN(out, OUT)
219
220#define APPEND_CMD_PTR_TO_IMM(cmd, op) \
221static inline void append_##cmd##_as_imm(u32 *desc, void *data, \
222 unsigned int len, u32 options) \
223{ \
224 PRINT_POS; \
225 append_cmd_data(desc, data, len, CMD_##op | options); \
226}
227APPEND_CMD_PTR_TO_IMM(load, LOAD);
228APPEND_CMD_PTR_TO_IMM(fifo_load, FIFO_LOAD);
229
230#define APPEND_CMD_PTR_EXTLEN(cmd, op) \
231static inline void append_##cmd##_extlen(u32 *desc, dma_addr_t ptr, \
232 unsigned int len, u32 options) \
233{ \
234 PRINT_POS; \
235 append_cmd_ptr_extlen(desc, ptr, len, CMD_##op | SQIN_EXT | options); \
236}
237APPEND_CMD_PTR_EXTLEN(seq_in_ptr, SEQ_IN_PTR)
238APPEND_CMD_PTR_EXTLEN(seq_out_ptr, SEQ_OUT_PTR)
239
240/*
241 * Determine whether to store length internally or externally depending on
242 * the size of its type
243 */
244#define APPEND_CMD_PTR_LEN(cmd, op, type) \
245static inline void append_##cmd(u32 *desc, dma_addr_t ptr, \
246 type len, u32 options) \
247{ \
248 PRINT_POS; \
249 if (sizeof(type) > sizeof(u16)) \
250 append_##cmd##_extlen(desc, ptr, len, options); \
251 else \
252 append_##cmd##_intlen(desc, ptr, len, options); \
253}
254APPEND_CMD_PTR_LEN(seq_in_ptr, SEQ_IN_PTR, u32)
255APPEND_CMD_PTR_LEN(seq_out_ptr, SEQ_OUT_PTR, u32)
256
257/*
258 * 2nd variant for commands whose specified immediate length differs
259 * from length of immediate data provided, e.g., split keys
260 */
261#define APPEND_CMD_PTR_TO_IMM2(cmd, op) \
262static inline void append_##cmd##_as_imm(u32 *desc, void *data, \
263 unsigned int data_len, \
264 unsigned int len, u32 options) \
265{ \
266 PRINT_POS; \
267 append_cmd(desc, CMD_##op | IMMEDIATE | len | options); \
268 append_data(desc, data, data_len); \
269}
270APPEND_CMD_PTR_TO_IMM2(key, KEY);
271
272#define APPEND_CMD_RAW_IMM(cmd, op, type) \
273static inline void append_##cmd##_imm_##type(u32 *desc, type immediate, \
274 u32 options) \
275{ \
276 PRINT_POS; \
277 append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(type)); \
278 append_cmd(desc, immediate); \
279}
280APPEND_CMD_RAW_IMM(load, LOAD, u32);