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 * 64)
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 DISABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
22 				LDST_SRCDST_WORD_DECOCTRL | \
23 				(LDOFF_DISABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
24 #define ENABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
25 			       LDST_SRCDST_WORD_DECOCTRL | \
26 			       (LDOFF_ENABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
27 
28 static inline int desc_len(u32 *desc)
29 {
30 	return *desc & HDR_DESCLEN_MASK;
31 }
32 
33 static inline int desc_bytes(void *desc)
34 {
35 	return desc_len(desc) * CAAM_CMD_SZ;
36 }
37 
38 static inline u32 *desc_end(u32 *desc)
39 {
40 	return desc + desc_len(desc);
41 }
42 
43 static inline void *sh_desc_pdb(u32 *desc)
44 {
45 	return desc + 1;
46 }
47 
48 static inline void init_desc(u32 *desc, u32 options)
49 {
50 	*desc = options | HDR_ONE | 1;
51 }
52 
53 static inline void init_sh_desc(u32 *desc, u32 options)
54 {
55 	PRINT_POS;
56 	init_desc(desc, CMD_SHARED_DESC_HDR | options);
57 }
58 
59 static inline void init_sh_desc_pdb(u32 *desc, u32 options, size_t pdb_bytes)
60 {
61 	u32 pdb_len = pdb_bytes / CAAM_CMD_SZ + 1;
62 
63 	init_sh_desc(desc, ((pdb_len << HDR_START_IDX_SHIFT) + pdb_len) |
64 		     options);
65 }
66 
67 static inline void init_job_desc(u32 *desc, u32 options)
68 {
69 	init_desc(desc, CMD_DESC_HDR | options);
70 }
71 
72 static inline void append_ptr(u32 *desc, dma_addr_t ptr)
73 {
74 	dma_addr_t *offset = (dma_addr_t *)desc_end(desc);
75 
76 	*offset = ptr;
77 
78 	(*desc) += CAAM_PTR_SZ / CAAM_CMD_SZ;
79 }
80 
81 static inline void init_job_desc_shared(u32 *desc, dma_addr_t ptr, int len,
82 					u32 options)
83 {
84 	PRINT_POS;
85 	init_job_desc(desc, HDR_SHARED | options |
86 		      (len << HDR_START_IDX_SHIFT));
87 	append_ptr(desc, ptr);
88 }
89 
90 static inline void append_data(u32 *desc, void *data, int len)
91 {
92 	u32 *offset = desc_end(desc);
93 
94 	if (len) /* avoid sparse warning: memcpy with byte count of 0 */
95 		memcpy(offset, data, len);
96 
97 	(*desc) += (len + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
98 }
99 
100 static inline void append_cmd(u32 *desc, u32 command)
101 {
102 	u32 *cmd = desc_end(desc);
103 
104 	*cmd = command;
105 
106 	(*desc)++;
107 }
108 
109 static inline void append_cmd_ptr(u32 *desc, dma_addr_t ptr, int len,
110 				  u32 command)
111 {
112 	append_cmd(desc, command | len);
113 	append_ptr(desc, ptr);
114 }
115 
116 static inline void append_cmd_data(u32 *desc, void *data, int len,
117 				   u32 command)
118 {
119 	append_cmd(desc, command | IMMEDIATE | len);
120 	append_data(desc, data, len);
121 }
122 
123 static inline u32 *append_jump(u32 *desc, u32 options)
124 {
125 	u32 *cmd = desc_end(desc);
126 
127 	PRINT_POS;
128 	append_cmd(desc, CMD_JUMP | options);
129 
130 	return cmd;
131 }
132 
133 static inline void set_jump_tgt_here(u32 *desc, u32 *jump_cmd)
134 {
135 	*jump_cmd = *jump_cmd | (desc_len(desc) - (jump_cmd - desc));
136 }
137 
138 #define APPEND_CMD(cmd, op) \
139 static inline void append_##cmd(u32 *desc, u32 options) \
140 { \
141 	PRINT_POS; \
142 	append_cmd(desc, CMD_##op | options); \
143 }
144 APPEND_CMD(operation, OPERATION)
145 APPEND_CMD(move, MOVE)
146 
147 #define APPEND_CMD_LEN(cmd, op) \
148 static inline void append_##cmd(u32 *desc, unsigned int len, u32 options) \
149 { \
150 	PRINT_POS; \
151 	append_cmd(desc, CMD_##op | len | options); \
152 }
153 APPEND_CMD_LEN(seq_store, SEQ_STORE)
154 APPEND_CMD_LEN(seq_fifo_load, SEQ_FIFO_LOAD)
155 APPEND_CMD_LEN(seq_fifo_store, SEQ_FIFO_STORE)
156 
157 #define APPEND_CMD_PTR(cmd, op) \
158 static inline void append_##cmd(u32 *desc, dma_addr_t ptr, unsigned int len, \
159 				u32 options) \
160 { \
161 	PRINT_POS; \
162 	append_cmd_ptr(desc, ptr, len, CMD_##op | options); \
163 }
164 APPEND_CMD_PTR(key, KEY)
165 APPEND_CMD_PTR(seq_in_ptr, SEQ_IN_PTR)
166 APPEND_CMD_PTR(seq_out_ptr, SEQ_OUT_PTR)
167 APPEND_CMD_PTR(load, LOAD)
168 APPEND_CMD_PTR(store, STORE)
169 APPEND_CMD_PTR(fifo_load, FIFO_LOAD)
170 APPEND_CMD_PTR(fifo_store, FIFO_STORE)
171 
172 #define APPEND_CMD_PTR_TO_IMM(cmd, op) \
173 static inline void append_##cmd##_as_imm(u32 *desc, void *data, \
174 					 unsigned int len, u32 options) \
175 { \
176 	PRINT_POS; \
177 	append_cmd_data(desc, data, len, CMD_##op | options); \
178 }
179 APPEND_CMD_PTR_TO_IMM(load, LOAD);
180 APPEND_CMD_PTR_TO_IMM(fifo_load, FIFO_LOAD);
181 
182 /*
183  * 2nd variant for commands whose specified immediate length differs
184  * from length of immediate data provided, e.g., split keys
185  */
186 #define APPEND_CMD_PTR_TO_IMM2(cmd, op) \
187 static inline void append_##cmd##_as_imm(u32 *desc, void *data, \
188 					 unsigned int data_len, \
189 					 unsigned int len, u32 options) \
190 { \
191 	PRINT_POS; \
192 	append_cmd(desc, CMD_##op | IMMEDIATE | len | options); \
193 	append_data(desc, data, data_len); \
194 }
195 APPEND_CMD_PTR_TO_IMM2(key, KEY);
196 
197 #define APPEND_CMD_RAW_IMM(cmd, op, type) \
198 static inline void append_##cmd##_imm_##type(u32 *desc, type immediate, \
199 					     u32 options) \
200 { \
201 	PRINT_POS; \
202 	append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(type)); \
203 	append_cmd(desc, immediate); \
204 }
205 APPEND_CMD_RAW_IMM(load, LOAD, u32);
206