desc_constr.h 7.2 KB

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