1 /*
2  * caam descriptor construction helper functions
3  *
4  * Copyright 2008-2011 Freescale Semiconductor, Inc.
5  */
6 
7 #include "desc.h"
8 
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 
14 #ifdef DEBUG
15 #define PRINT_POS do { printk(KERN_DEBUG "%02d: %s\n", desc_len(desc),\
16 			      &__func__[sizeof("append")]); } while (0)
17 #else
18 #define PRINT_POS
19 #endif
20 
21 #define SET_OK_PROP_ERRORS (IMMEDIATE | LDST_CLASS_DECO | \
22 			    LDST_SRCDST_WORD_DECOCTRL | \
23 			    (LDOFF_CHG_SHARE_OK_PROP << LDST_OFFSET_SHIFT))
24 #define DISABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
25 				LDST_SRCDST_WORD_DECOCTRL | \
26 				(LDOFF_DISABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
27 #define ENABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
28 			       LDST_SRCDST_WORD_DECOCTRL | \
29 			       (LDOFF_ENABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
30 
31 static inline int desc_len(u32 *desc)
32 {
33 	return *desc & HDR_DESCLEN_MASK;
34 }
35 
36 static inline int desc_bytes(void *desc)
37 {
38 	return desc_len(desc) * CAAM_CMD_SZ;
39 }
40 
41 static inline u32 *desc_end(u32 *desc)
42 {
43 	return desc + desc_len(desc);
44 }
45 
46 static inline void *sh_desc_pdb(u32 *desc)
47 {
48 	return desc + 1;
49 }
50 
51 static inline void init_desc(u32 *desc, u32 options)
52 {
53 	*desc = options | HDR_ONE | 1;
54 }
55 
56 static inline void init_sh_desc(u32 *desc, u32 options)
57 {
58 	PRINT_POS;
59 	init_desc(desc, CMD_SHARED_DESC_HDR | options);
60 }
61 
62 static inline void init_sh_desc_pdb(u32 *desc, u32 options, size_t pdb_bytes)
63 {
64 	u32 pdb_len = pdb_bytes / CAAM_CMD_SZ + 1;
65 
66 	init_sh_desc(desc, ((pdb_len << HDR_START_IDX_SHIFT) + pdb_len) |
67 		     options);
68 }
69 
70 static inline void init_job_desc(u32 *desc, u32 options)
71 {
72 	init_desc(desc, CMD_DESC_HDR | options);
73 }
74 
75 static inline void append_ptr(u32 *desc, dma_addr_t ptr)
76 {
77 	dma_addr_t *offset = (dma_addr_t *)desc_end(desc);
78 
79 	*offset = ptr;
80 
81 	(*desc) += CAAM_PTR_SZ / CAAM_CMD_SZ;
82 }
83 
84 static inline void init_job_desc_shared(u32 *desc, dma_addr_t ptr, int len,
85 					u32 options)
86 {
87 	PRINT_POS;
88 	init_job_desc(desc, HDR_SHARED | options |
89 		      (len << HDR_START_IDX_SHIFT));
90 	append_ptr(desc, ptr);
91 }
92 
93 static inline void append_data(u32 *desc, void *data, int len)
94 {
95 	u32 *offset = desc_end(desc);
96 
97 	if (len) /* avoid sparse warning: memcpy with byte count of 0 */
98 		memcpy(offset, data, len);
99 
100 	(*desc) += (len + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
101 }
102 
103 static inline void append_cmd(u32 *desc, u32 command)
104 {
105 	u32 *cmd = desc_end(desc);
106 
107 	*cmd = command;
108 
109 	(*desc)++;
110 }
111 
112 static 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 static inline void append_cmd_data(u32 *desc, void *data, int len,
120 				   u32 command)
121 {
122 	append_cmd(desc, command | IMMEDIATE | len);
123 	append_data(desc, data, len);
124 }
125 
126 static inline u32 *append_jump(u32 *desc, u32 options)
127 {
128 	u32 *cmd = desc_end(desc);
129 
130 	PRINT_POS;
131 	append_cmd(desc, CMD_JUMP | options);
132 
133 	return cmd;
134 }
135 
136 static inline void set_jump_tgt_here(u32 *desc, u32 *jump_cmd)
137 {
138 	*jump_cmd = *jump_cmd | (desc_len(desc) - (jump_cmd - desc));
139 }
140 
141 #define APPEND_CMD(cmd, op) \
142 static inline void append_##cmd(u32 *desc, u32 options) \
143 { \
144 	PRINT_POS; \
145 	append_cmd(desc, CMD_##op | options); \
146 }
147 APPEND_CMD(operation, OPERATION)
148 APPEND_CMD(move, MOVE)
149 
150 #define APPEND_CMD_LEN(cmd, op) \
151 static inline void append_##cmd(u32 *desc, unsigned int len, u32 options) \
152 { \
153 	PRINT_POS; \
154 	append_cmd(desc, CMD_##op | len | options); \
155 }
156 APPEND_CMD_LEN(seq_store, SEQ_STORE)
157 APPEND_CMD_LEN(seq_fifo_load, SEQ_FIFO_LOAD)
158 APPEND_CMD_LEN(seq_fifo_store, SEQ_FIFO_STORE)
159 
160 #define APPEND_CMD_PTR(cmd, op) \
161 static inline void append_##cmd(u32 *desc, dma_addr_t ptr, unsigned int len, \
162 				u32 options) \
163 { \
164 	PRINT_POS; \
165 	append_cmd_ptr(desc, ptr, len, CMD_##op | options); \
166 }
167 APPEND_CMD_PTR(key, KEY)
168 APPEND_CMD_PTR(seq_in_ptr, SEQ_IN_PTR)
169 APPEND_CMD_PTR(seq_out_ptr, SEQ_OUT_PTR)
170 APPEND_CMD_PTR(load, LOAD)
171 APPEND_CMD_PTR(store, STORE)
172 APPEND_CMD_PTR(fifo_load, FIFO_LOAD)
173 APPEND_CMD_PTR(fifo_store, FIFO_STORE)
174 
175 #define APPEND_CMD_PTR_TO_IMM(cmd, op) \
176 static inline void append_##cmd##_as_imm(u32 *desc, void *data, \
177 					 unsigned int len, u32 options) \
178 { \
179 	PRINT_POS; \
180 	append_cmd_data(desc, data, len, CMD_##op | options); \
181 }
182 APPEND_CMD_PTR_TO_IMM(load, LOAD);
183 APPEND_CMD_PTR_TO_IMM(fifo_load, FIFO_LOAD);
184 
185 /*
186  * 2nd variant for commands whose specified immediate length differs
187  * from length of immediate data provided, e.g., split keys
188  */
189 #define APPEND_CMD_PTR_TO_IMM2(cmd, op) \
190 static inline void append_##cmd##_as_imm(u32 *desc, void *data, \
191 					 unsigned int data_len, \
192 					 unsigned int len, u32 options) \
193 { \
194 	PRINT_POS; \
195 	append_cmd(desc, CMD_##op | IMMEDIATE | len | options); \
196 	append_data(desc, data, data_len); \
197 }
198 APPEND_CMD_PTR_TO_IMM2(key, KEY);
199 
200 #define APPEND_CMD_RAW_IMM(cmd, op, type) \
201 static inline void append_##cmd##_imm_##type(u32 *desc, type immediate, \
202 					     u32 options) \
203 { \
204 	PRINT_POS; \
205 	append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(type)); \
206 	append_cmd(desc, immediate); \
207 }
208 APPEND_CMD_RAW_IMM(load, LOAD, u32);
209 
210 /*
211  * Append math command. Only the last part of destination and source need to
212  * be specified
213  */
214 #define APPEND_MATH(op, desc, dest, src_0, src_1, len) \
215 append_cmd(desc, CMD_MATH | MATH_FUN_##op | MATH_DEST_##dest | \
216 	   MATH_SRC0_##src_0 | MATH_SRC1_##src_1 | (u32) (len & MATH_LEN_MASK));
217 
218 #define append_math_add(desc, dest, src0, src1, len) \
219 	APPEND_MATH(ADD, desc, dest, src0, src1, len)
220 #define append_math_sub(desc, dest, src0, src1, len) \
221 	APPEND_MATH(SUB, desc, dest, src0, src1, len)
222 #define append_math_add_c(desc, dest, src0, src1, len) \
223 	APPEND_MATH(ADDC, desc, dest, src0, src1, len)
224 #define append_math_sub_b(desc, dest, src0, src1, len) \
225 	APPEND_MATH(SUBB, desc, dest, src0, src1, len)
226 #define append_math_and(desc, dest, src0, src1, len) \
227 	APPEND_MATH(AND, desc, dest, src0, src1, len)
228 #define append_math_or(desc, dest, src0, src1, len) \
229 	APPEND_MATH(OR, desc, dest, src0, src1, len)
230 #define append_math_xor(desc, dest, src0, src1, len) \
231 	APPEND_MATH(XOR, desc, dest, src0, src1, len)
232 #define append_math_lshift(desc, dest, src0, src1, len) \
233 	APPEND_MATH(LSHIFT, desc, dest, src0, src1, len)
234 #define append_math_rshift(desc, dest, src0, src1, len) \
235 	APPEND_MATH(RSHIFT, desc, dest, src0, src1, len)
236 
237 /* Exactly one source is IMM. Data is passed in as u32 value */
238 #define APPEND_MATH_IMM_u32(op, desc, dest, src_0, src_1, data) \
239 do { \
240 	APPEND_MATH(op, desc, dest, src_0, src_1, CAAM_CMD_SZ); \
241 	append_cmd(desc, data); \
242 } while (0);
243 
244 #define append_math_add_imm_u32(desc, dest, src0, src1, data) \
245 	APPEND_MATH_IMM_u32(ADD, desc, dest, src0, src1, data)
246 #define append_math_sub_imm_u32(desc, dest, src0, src1, data) \
247 	APPEND_MATH_IMM_u32(SUB, desc, dest, src0, src1, data)
248 #define append_math_add_c_imm_u32(desc, dest, src0, src1, data) \
249 	APPEND_MATH_IMM_u32(ADDC, desc, dest, src0, src1, data)
250 #define append_math_sub_b_imm_u32(desc, dest, src0, src1, data) \
251 	APPEND_MATH_IMM_u32(SUBB, desc, dest, src0, src1, data)
252 #define append_math_and_imm_u32(desc, dest, src0, src1, data) \
253 	APPEND_MATH_IMM_u32(AND, desc, dest, src0, src1, data)
254 #define append_math_or_imm_u32(desc, dest, src0, src1, data) \
255 	APPEND_MATH_IMM_u32(OR, desc, dest, src0, src1, data)
256 #define append_math_xor_imm_u32(desc, dest, src0, src1, data) \
257 	APPEND_MATH_IMM_u32(XOR, desc, dest, src0, src1, data)
258 #define append_math_lshift_imm_u32(desc, dest, src0, src1, data) \
259 	APPEND_MATH_IMM_u32(LSHIFT, desc, dest, src0, src1, data)
260 #define append_math_rshift_imm_u32(desc, dest, src0, src1, data) \
261 	APPEND_MATH_IMM_u32(RSHIFT, desc, dest, src0, src1, data)
262