desc_constr.h 8.4 KB

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