desc_constr.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * caam descriptor construction helper functions
  3. *
  4. * Copyright 2008-2012 Freescale Semiconductor, Inc.
  5. */
  6. #ifndef DESC_CONSTR_H
  7. #define DESC_CONSTR_H
  8. #include "desc.h"
  9. #include "regs.h"
  10. #define IMMEDIATE (1 << 23)
  11. #define CAAM_CMD_SZ sizeof(u32)
  12. #define CAAM_PTR_SZ sizeof(dma_addr_t)
  13. #define CAAM_DESC_BYTES_MAX (CAAM_CMD_SZ * MAX_CAAM_DESCSIZE)
  14. #define DESC_JOB_IO_LEN (CAAM_CMD_SZ * 5 + CAAM_PTR_SZ * 3)
  15. #ifdef DEBUG
  16. #define PRINT_POS do { printk(KERN_DEBUG "%02d: %s\n", desc_len(desc),\
  17. &__func__[sizeof("append")]); } while (0)
  18. #else
  19. #define PRINT_POS
  20. #endif
  21. #define SET_OK_NO_PROP_ERRORS (IMMEDIATE | LDST_CLASS_DECO | \
  22. LDST_SRCDST_WORD_DECOCTRL | \
  23. (LDOFF_CHG_SHARE_OK_NO_PROP << \
  24. LDST_OFFSET_SHIFT))
  25. #define DISABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
  26. LDST_SRCDST_WORD_DECOCTRL | \
  27. (LDOFF_DISABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
  28. #define ENABLE_AUTO_INFO_FIFO (IMMEDIATE | LDST_CLASS_DECO | \
  29. LDST_SRCDST_WORD_DECOCTRL | \
  30. (LDOFF_ENABLE_AUTO_NFIFO << LDST_OFFSET_SHIFT))
  31. extern bool caam_little_end;
  32. static inline int desc_len(u32 * const desc)
  33. {
  34. return caam32_to_cpu(*desc) & HDR_DESCLEN_MASK;
  35. }
  36. static inline int desc_bytes(void * const desc)
  37. {
  38. return desc_len(desc) * CAAM_CMD_SZ;
  39. }
  40. static inline u32 *desc_end(u32 * const desc)
  41. {
  42. return desc + desc_len(desc);
  43. }
  44. static inline void *sh_desc_pdb(u32 * const desc)
  45. {
  46. return desc + 1;
  47. }
  48. static inline void init_desc(u32 * const desc, u32 options)
  49. {
  50. *desc = cpu_to_caam32((options | HDR_ONE) + 1);
  51. }
  52. static inline void init_sh_desc(u32 * const desc, u32 options)
  53. {
  54. PRINT_POS;
  55. init_desc(desc, CMD_SHARED_DESC_HDR | options);
  56. }
  57. static inline void init_sh_desc_pdb(u32 * const desc, u32 options,
  58. size_t pdb_bytes)
  59. {
  60. u32 pdb_len = (pdb_bytes + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
  61. init_sh_desc(desc, (((pdb_len + 1) << HDR_START_IDX_SHIFT) + pdb_len) |
  62. options);
  63. }
  64. static inline void init_job_desc(u32 * const desc, u32 options)
  65. {
  66. init_desc(desc, CMD_DESC_HDR | options);
  67. }
  68. static inline void init_job_desc_pdb(u32 * const desc, u32 options,
  69. size_t pdb_bytes)
  70. {
  71. u32 pdb_len = (pdb_bytes + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ;
  72. init_job_desc(desc, (((pdb_len + 1) << HDR_START_IDX_SHIFT)) | options);
  73. }
  74. static inline void append_ptr(u32 * const desc, dma_addr_t ptr)
  75. {
  76. dma_addr_t *offset = (dma_addr_t *)desc_end(desc);
  77. *offset = cpu_to_caam_dma(ptr);
  78. (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) +
  79. CAAM_PTR_SZ / CAAM_CMD_SZ);
  80. }
  81. static inline void init_job_desc_shared(u32 * const desc, dma_addr_t ptr,
  82. int len, 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. static inline void append_data(u32 * const desc, void *data, int len)
  90. {
  91. u32 *offset = desc_end(desc);
  92. if (len) /* avoid sparse warning: memcpy with byte count of 0 */
  93. memcpy(offset, data, len);
  94. (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) +
  95. (len + CAAM_CMD_SZ - 1) / CAAM_CMD_SZ);
  96. }
  97. static inline void append_cmd(u32 * const desc, u32 command)
  98. {
  99. u32 *cmd = desc_end(desc);
  100. *cmd = cpu_to_caam32(command);
  101. (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) + 1);
  102. }
  103. #define append_u32 append_cmd
  104. static inline void append_u64(u32 * const desc, u64 data)
  105. {
  106. u32 *offset = desc_end(desc);
  107. /* Only 32-bit alignment is guaranteed in descriptor buffer */
  108. if (caam_little_end) {
  109. *offset = cpu_to_caam32(lower_32_bits(data));
  110. *(++offset) = cpu_to_caam32(upper_32_bits(data));
  111. } else {
  112. *offset = cpu_to_caam32(upper_32_bits(data));
  113. *(++offset) = cpu_to_caam32(lower_32_bits(data));
  114. }
  115. (*desc) = cpu_to_caam32(caam32_to_cpu(*desc) + 2);
  116. }
  117. /* Write command without affecting header, and return pointer to next word */
  118. static inline u32 *write_cmd(u32 * const desc, u32 command)
  119. {
  120. *desc = cpu_to_caam32(command);
  121. return desc + 1;
  122. }
  123. static inline void append_cmd_ptr(u32 * const desc, dma_addr_t ptr, int len,
  124. u32 command)
  125. {
  126. append_cmd(desc, command | len);
  127. append_ptr(desc, ptr);
  128. }
  129. /* Write length after pointer, rather than inside command */
  130. static inline void append_cmd_ptr_extlen(u32 * const desc, dma_addr_t ptr,
  131. unsigned int len, u32 command)
  132. {
  133. append_cmd(desc, command);
  134. if (!(command & (SQIN_RTO | SQIN_PRE)))
  135. append_ptr(desc, ptr);
  136. append_cmd(desc, len);
  137. }
  138. static inline void append_cmd_data(u32 * const desc, void *data, int len,
  139. u32 command)
  140. {
  141. append_cmd(desc, command | IMMEDIATE | len);
  142. append_data(desc, data, len);
  143. }
  144. #define APPEND_CMD_RET(cmd, op) \
  145. static inline u32 *append_##cmd(u32 * const desc, u32 options) \
  146. { \
  147. u32 *cmd = desc_end(desc); \
  148. PRINT_POS; \
  149. append_cmd(desc, CMD_##op | options); \
  150. return cmd; \
  151. }
  152. APPEND_CMD_RET(jump, JUMP)
  153. APPEND_CMD_RET(move, MOVE)
  154. static inline void set_jump_tgt_here(u32 * const desc, u32 *jump_cmd)
  155. {
  156. *jump_cmd = cpu_to_caam32(caam32_to_cpu(*jump_cmd) |
  157. (desc_len(desc) - (jump_cmd - desc)));
  158. }
  159. static inline void set_move_tgt_here(u32 * const desc, u32 *move_cmd)
  160. {
  161. u32 val = caam32_to_cpu(*move_cmd);
  162. val &= ~MOVE_OFFSET_MASK;
  163. val |= (desc_len(desc) << (MOVE_OFFSET_SHIFT + 2)) & MOVE_OFFSET_MASK;
  164. *move_cmd = cpu_to_caam32(val);
  165. }
  166. #define APPEND_CMD(cmd, op) \
  167. static inline void append_##cmd(u32 * const desc, u32 options) \
  168. { \
  169. PRINT_POS; \
  170. append_cmd(desc, CMD_##op | options); \
  171. }
  172. APPEND_CMD(operation, OPERATION)
  173. #define APPEND_CMD_LEN(cmd, op) \
  174. static inline void append_##cmd(u32 * const desc, unsigned int len, \
  175. u32 options) \
  176. { \
  177. PRINT_POS; \
  178. append_cmd(desc, CMD_##op | len | options); \
  179. }
  180. APPEND_CMD_LEN(seq_load, SEQ_LOAD)
  181. APPEND_CMD_LEN(seq_store, SEQ_STORE)
  182. APPEND_CMD_LEN(seq_fifo_load, SEQ_FIFO_LOAD)
  183. APPEND_CMD_LEN(seq_fifo_store, SEQ_FIFO_STORE)
  184. #define APPEND_CMD_PTR(cmd, op) \
  185. static inline void append_##cmd(u32 * const desc, dma_addr_t ptr, \
  186. unsigned int len, u32 options) \
  187. { \
  188. PRINT_POS; \
  189. append_cmd_ptr(desc, ptr, len, CMD_##op | options); \
  190. }
  191. APPEND_CMD_PTR(key, KEY)
  192. APPEND_CMD_PTR(load, LOAD)
  193. APPEND_CMD_PTR(fifo_load, FIFO_LOAD)
  194. APPEND_CMD_PTR(fifo_store, FIFO_STORE)
  195. static inline void append_store(u32 * const desc, dma_addr_t ptr,
  196. unsigned int len, u32 options)
  197. {
  198. u32 cmd_src;
  199. cmd_src = options & LDST_SRCDST_MASK;
  200. append_cmd(desc, CMD_STORE | options | len);
  201. /* The following options do not require pointer */
  202. if (!(cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED ||
  203. cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB ||
  204. cmd_src == LDST_SRCDST_WORD_DESCBUF_JOB_WE ||
  205. cmd_src == LDST_SRCDST_WORD_DESCBUF_SHARED_WE))
  206. append_ptr(desc, ptr);
  207. }
  208. #define APPEND_SEQ_PTR_INTLEN(cmd, op) \
  209. static inline void append_seq_##cmd##_ptr_intlen(u32 * const desc, \
  210. dma_addr_t ptr, \
  211. unsigned int len, \
  212. u32 options) \
  213. { \
  214. PRINT_POS; \
  215. if (options & (SQIN_RTO | SQIN_PRE)) \
  216. append_cmd(desc, CMD_SEQ_##op##_PTR | len | options); \
  217. else \
  218. append_cmd_ptr(desc, ptr, len, CMD_SEQ_##op##_PTR | options); \
  219. }
  220. APPEND_SEQ_PTR_INTLEN(in, IN)
  221. APPEND_SEQ_PTR_INTLEN(out, OUT)
  222. #define APPEND_CMD_PTR_TO_IMM(cmd, op) \
  223. static inline void append_##cmd##_as_imm(u32 * const desc, void *data, \
  224. unsigned int len, u32 options) \
  225. { \
  226. PRINT_POS; \
  227. append_cmd_data(desc, data, len, CMD_##op | options); \
  228. }
  229. APPEND_CMD_PTR_TO_IMM(load, LOAD);
  230. APPEND_CMD_PTR_TO_IMM(fifo_load, FIFO_LOAD);
  231. #define APPEND_CMD_PTR_EXTLEN(cmd, op) \
  232. static inline void append_##cmd##_extlen(u32 * const desc, dma_addr_t ptr, \
  233. unsigned int len, u32 options) \
  234. { \
  235. PRINT_POS; \
  236. append_cmd_ptr_extlen(desc, ptr, len, CMD_##op | SQIN_EXT | options); \
  237. }
  238. APPEND_CMD_PTR_EXTLEN(seq_in_ptr, SEQ_IN_PTR)
  239. APPEND_CMD_PTR_EXTLEN(seq_out_ptr, SEQ_OUT_PTR)
  240. /*
  241. * Determine whether to store length internally or externally depending on
  242. * the size of its type
  243. */
  244. #define APPEND_CMD_PTR_LEN(cmd, op, type) \
  245. static inline void append_##cmd(u32 * const desc, dma_addr_t ptr, \
  246. type len, u32 options) \
  247. { \
  248. PRINT_POS; \
  249. if (sizeof(type) > sizeof(u16)) \
  250. append_##cmd##_extlen(desc, ptr, len, options); \
  251. else \
  252. append_##cmd##_intlen(desc, ptr, len, options); \
  253. }
  254. APPEND_CMD_PTR_LEN(seq_in_ptr, SEQ_IN_PTR, u32)
  255. APPEND_CMD_PTR_LEN(seq_out_ptr, SEQ_OUT_PTR, u32)
  256. /*
  257. * 2nd variant for commands whose specified immediate length differs
  258. * from length of immediate data provided, e.g., split keys
  259. */
  260. #define APPEND_CMD_PTR_TO_IMM2(cmd, op) \
  261. static inline void append_##cmd##_as_imm(u32 * const desc, void *data, \
  262. unsigned int data_len, \
  263. unsigned int len, u32 options) \
  264. { \
  265. PRINT_POS; \
  266. append_cmd(desc, CMD_##op | IMMEDIATE | len | options); \
  267. append_data(desc, data, data_len); \
  268. }
  269. APPEND_CMD_PTR_TO_IMM2(key, KEY);
  270. #define APPEND_CMD_RAW_IMM(cmd, op, type) \
  271. static inline void append_##cmd##_imm_##type(u32 * const desc, type immediate, \
  272. u32 options) \
  273. { \
  274. PRINT_POS; \
  275. append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(type)); \
  276. append_cmd(desc, immediate); \
  277. }
  278. APPEND_CMD_RAW_IMM(load, LOAD, u32);
  279. /*
  280. * ee - endianness
  281. * size - size of immediate type in bytes
  282. */
  283. #define APPEND_CMD_RAW_IMM2(cmd, op, ee, size) \
  284. static inline void append_##cmd##_imm_##ee##size(u32 *desc, \
  285. u##size immediate, \
  286. u32 options) \
  287. { \
  288. __##ee##size data = cpu_to_##ee##size(immediate); \
  289. PRINT_POS; \
  290. append_cmd(desc, CMD_##op | IMMEDIATE | options | sizeof(data)); \
  291. append_data(desc, &data, sizeof(data)); \
  292. }
  293. APPEND_CMD_RAW_IMM2(load, LOAD, be, 32);
  294. /*
  295. * Append math command. Only the last part of destination and source need to
  296. * be specified
  297. */
  298. #define APPEND_MATH(op, desc, dest, src_0, src_1, len) \
  299. append_cmd(desc, CMD_MATH | MATH_FUN_##op | MATH_DEST_##dest | \
  300. MATH_SRC0_##src_0 | MATH_SRC1_##src_1 | (u32)len);
  301. #define append_math_add(desc, dest, src0, src1, len) \
  302. APPEND_MATH(ADD, desc, dest, src0, src1, len)
  303. #define append_math_sub(desc, dest, src0, src1, len) \
  304. APPEND_MATH(SUB, desc, dest, src0, src1, len)
  305. #define append_math_add_c(desc, dest, src0, src1, len) \
  306. APPEND_MATH(ADDC, desc, dest, src0, src1, len)
  307. #define append_math_sub_b(desc, dest, src0, src1, len) \
  308. APPEND_MATH(SUBB, desc, dest, src0, src1, len)
  309. #define append_math_and(desc, dest, src0, src1, len) \
  310. APPEND_MATH(AND, desc, dest, src0, src1, len)
  311. #define append_math_or(desc, dest, src0, src1, len) \
  312. APPEND_MATH(OR, desc, dest, src0, src1, len)
  313. #define append_math_xor(desc, dest, src0, src1, len) \
  314. APPEND_MATH(XOR, desc, dest, src0, src1, len)
  315. #define append_math_lshift(desc, dest, src0, src1, len) \
  316. APPEND_MATH(LSHIFT, desc, dest, src0, src1, len)
  317. #define append_math_rshift(desc, dest, src0, src1, len) \
  318. APPEND_MATH(RSHIFT, desc, dest, src0, src1, len)
  319. #define append_math_ldshift(desc, dest, src0, src1, len) \
  320. APPEND_MATH(SHLD, desc, dest, src0, src1, len)
  321. /* Exactly one source is IMM. Data is passed in as u32 value */
  322. #define APPEND_MATH_IMM_u32(op, desc, dest, src_0, src_1, data) \
  323. do { \
  324. APPEND_MATH(op, desc, dest, src_0, src_1, CAAM_CMD_SZ); \
  325. append_cmd(desc, data); \
  326. } while (0)
  327. #define append_math_add_imm_u32(desc, dest, src0, src1, data) \
  328. APPEND_MATH_IMM_u32(ADD, desc, dest, src0, src1, data)
  329. #define append_math_sub_imm_u32(desc, dest, src0, src1, data) \
  330. APPEND_MATH_IMM_u32(SUB, desc, dest, src0, src1, data)
  331. #define append_math_add_c_imm_u32(desc, dest, src0, src1, data) \
  332. APPEND_MATH_IMM_u32(ADDC, desc, dest, src0, src1, data)
  333. #define append_math_sub_b_imm_u32(desc, dest, src0, src1, data) \
  334. APPEND_MATH_IMM_u32(SUBB, desc, dest, src0, src1, data)
  335. #define append_math_and_imm_u32(desc, dest, src0, src1, data) \
  336. APPEND_MATH_IMM_u32(AND, desc, dest, src0, src1, data)
  337. #define append_math_or_imm_u32(desc, dest, src0, src1, data) \
  338. APPEND_MATH_IMM_u32(OR, desc, dest, src0, src1, data)
  339. #define append_math_xor_imm_u32(desc, dest, src0, src1, data) \
  340. APPEND_MATH_IMM_u32(XOR, desc, dest, src0, src1, data)
  341. #define append_math_lshift_imm_u32(desc, dest, src0, src1, data) \
  342. APPEND_MATH_IMM_u32(LSHIFT, desc, dest, src0, src1, data)
  343. #define append_math_rshift_imm_u32(desc, dest, src0, src1, data) \
  344. APPEND_MATH_IMM_u32(RSHIFT, desc, dest, src0, src1, data)
  345. /* Exactly one source is IMM. Data is passed in as u64 value */
  346. #define APPEND_MATH_IMM_u64(op, desc, dest, src_0, src_1, data) \
  347. do { \
  348. u32 upper = (data >> 16) >> 16; \
  349. APPEND_MATH(op, desc, dest, src_0, src_1, CAAM_CMD_SZ * 2 | \
  350. (upper ? 0 : MATH_IFB)); \
  351. if (upper) \
  352. append_u64(desc, data); \
  353. else \
  354. append_u32(desc, lower_32_bits(data)); \
  355. } while (0)
  356. #define append_math_add_imm_u64(desc, dest, src0, src1, data) \
  357. APPEND_MATH_IMM_u64(ADD, desc, dest, src0, src1, data)
  358. #define append_math_sub_imm_u64(desc, dest, src0, src1, data) \
  359. APPEND_MATH_IMM_u64(SUB, desc, dest, src0, src1, data)
  360. #define append_math_add_c_imm_u64(desc, dest, src0, src1, data) \
  361. APPEND_MATH_IMM_u64(ADDC, desc, dest, src0, src1, data)
  362. #define append_math_sub_b_imm_u64(desc, dest, src0, src1, data) \
  363. APPEND_MATH_IMM_u64(SUBB, desc, dest, src0, src1, data)
  364. #define append_math_and_imm_u64(desc, dest, src0, src1, data) \
  365. APPEND_MATH_IMM_u64(AND, desc, dest, src0, src1, data)
  366. #define append_math_or_imm_u64(desc, dest, src0, src1, data) \
  367. APPEND_MATH_IMM_u64(OR, desc, dest, src0, src1, data)
  368. #define append_math_xor_imm_u64(desc, dest, src0, src1, data) \
  369. APPEND_MATH_IMM_u64(XOR, desc, dest, src0, src1, data)
  370. #define append_math_lshift_imm_u64(desc, dest, src0, src1, data) \
  371. APPEND_MATH_IMM_u64(LSHIFT, desc, dest, src0, src1, data)
  372. #define append_math_rshift_imm_u64(desc, dest, src0, src1, data) \
  373. APPEND_MATH_IMM_u64(RSHIFT, desc, dest, src0, src1, data)
  374. /**
  375. * struct alginfo - Container for algorithm details
  376. * @algtype: algorithm selector; for valid values, see documentation of the
  377. * functions where it is used.
  378. * @keylen: length of the provided algorithm key, in bytes
  379. * @keylen_pad: padded length of the provided algorithm key, in bytes
  380. * @key: address where algorithm key resides; virtual address if key_inline
  381. * is true, dma (bus) address if key_inline is false.
  382. * @key_inline: true - key can be inlined in the descriptor; false - key is
  383. * referenced by the descriptor
  384. */
  385. struct alginfo {
  386. u32 algtype;
  387. unsigned int keylen;
  388. unsigned int keylen_pad;
  389. union {
  390. dma_addr_t key_dma;
  391. void *key_virt;
  392. };
  393. bool key_inline;
  394. };
  395. /**
  396. * desc_inline_query() - Provide indications on which data items can be inlined
  397. * and which shall be referenced in a shared descriptor.
  398. * @sd_base_len: Shared descriptor base length - bytes consumed by the commands,
  399. * excluding the data items to be inlined (or corresponding
  400. * pointer if an item is not inlined). Each cnstr_* function that
  401. * generates descriptors should have a define mentioning
  402. * corresponding length.
  403. * @jd_len: Maximum length of the job descriptor(s) that will be used
  404. * together with the shared descriptor.
  405. * @data_len: Array of lengths of the data items trying to be inlined
  406. * @inl_mask: 32bit mask with bit x = 1 if data item x can be inlined, 0
  407. * otherwise.
  408. * @count: Number of data items (size of @data_len array); must be <= 32
  409. *
  410. * Return: 0 if data can be inlined / referenced, negative value if not. If 0,
  411. * check @inl_mask for details.
  412. */
  413. static inline int desc_inline_query(unsigned int sd_base_len,
  414. unsigned int jd_len, unsigned int *data_len,
  415. u32 *inl_mask, unsigned int count)
  416. {
  417. int rem_bytes = (int)(CAAM_DESC_BYTES_MAX - sd_base_len - jd_len);
  418. unsigned int i;
  419. *inl_mask = 0;
  420. for (i = 0; (i < count) && (rem_bytes > 0); i++) {
  421. if (rem_bytes - (int)(data_len[i] +
  422. (count - i - 1) * CAAM_PTR_SZ) >= 0) {
  423. rem_bytes -= data_len[i];
  424. *inl_mask |= (1 << i);
  425. } else {
  426. rem_bytes -= CAAM_PTR_SZ;
  427. }
  428. }
  429. return (rem_bytes >= 0) ? 0 : -1;
  430. }
  431. #endif /* DESC_CONSTR_H */