desc_constr.h 8.4 KB

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