desc_constr.h 15 KB

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