tdma.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Provide TDMA helper functions used by cipher and hash algorithm
  3. * implementations.
  4. *
  5. * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
  6. * Author: Arnaud Ebalard <arno@natisbad.org>
  7. *
  8. * This work is based on an initial version written by
  9. * Sebastian Andrzej Siewior < sebastian at breakpoint dot cc >
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License version 2 as published
  13. * by the Free Software Foundation.
  14. */
  15. #include "cesa.h"
  16. bool mv_cesa_req_dma_iter_next_transfer(struct mv_cesa_dma_iter *iter,
  17. struct mv_cesa_sg_dma_iter *sgiter,
  18. unsigned int len)
  19. {
  20. if (!sgiter->sg)
  21. return false;
  22. sgiter->op_offset += len;
  23. sgiter->offset += len;
  24. if (sgiter->offset == sg_dma_len(sgiter->sg)) {
  25. if (sg_is_last(sgiter->sg))
  26. return false;
  27. sgiter->offset = 0;
  28. sgiter->sg = sg_next(sgiter->sg);
  29. }
  30. if (sgiter->op_offset == iter->op_len)
  31. return false;
  32. return true;
  33. }
  34. void mv_cesa_dma_step(struct mv_cesa_req *dreq)
  35. {
  36. struct mv_cesa_engine *engine = dreq->engine;
  37. writel_relaxed(0, engine->regs + CESA_SA_CFG);
  38. mv_cesa_set_int_mask(engine, CESA_SA_INT_ACC0_IDMA_DONE);
  39. writel_relaxed(CESA_TDMA_DST_BURST_128B | CESA_TDMA_SRC_BURST_128B |
  40. CESA_TDMA_NO_BYTE_SWAP | CESA_TDMA_EN,
  41. engine->regs + CESA_TDMA_CONTROL);
  42. writel_relaxed(CESA_SA_CFG_ACT_CH0_IDMA | CESA_SA_CFG_MULTI_PKT |
  43. CESA_SA_CFG_CH0_W_IDMA | CESA_SA_CFG_PARA_DIS,
  44. engine->regs + CESA_SA_CFG);
  45. writel_relaxed(dreq->chain.first->cur_dma,
  46. engine->regs + CESA_TDMA_NEXT_ADDR);
  47. BUG_ON(readl(engine->regs + CESA_SA_CMD) &
  48. CESA_SA_CMD_EN_CESA_SA_ACCL0);
  49. writel(CESA_SA_CMD_EN_CESA_SA_ACCL0, engine->regs + CESA_SA_CMD);
  50. }
  51. void mv_cesa_dma_cleanup(struct mv_cesa_req *dreq)
  52. {
  53. struct mv_cesa_tdma_desc *tdma;
  54. for (tdma = dreq->chain.first; tdma;) {
  55. struct mv_cesa_tdma_desc *old_tdma = tdma;
  56. u32 type = tdma->flags & CESA_TDMA_TYPE_MSK;
  57. if (type == CESA_TDMA_OP)
  58. dma_pool_free(cesa_dev->dma->op_pool, tdma->op,
  59. le32_to_cpu(tdma->src));
  60. tdma = tdma->next;
  61. dma_pool_free(cesa_dev->dma->tdma_desc_pool, old_tdma,
  62. old_tdma->cur_dma);
  63. }
  64. dreq->chain.first = NULL;
  65. dreq->chain.last = NULL;
  66. }
  67. void mv_cesa_dma_prepare(struct mv_cesa_req *dreq,
  68. struct mv_cesa_engine *engine)
  69. {
  70. struct mv_cesa_tdma_desc *tdma;
  71. for (tdma = dreq->chain.first; tdma; tdma = tdma->next) {
  72. if (tdma->flags & CESA_TDMA_DST_IN_SRAM)
  73. tdma->dst = cpu_to_le32(tdma->dst + engine->sram_dma);
  74. if (tdma->flags & CESA_TDMA_SRC_IN_SRAM)
  75. tdma->src = cpu_to_le32(tdma->src + engine->sram_dma);
  76. if ((tdma->flags & CESA_TDMA_TYPE_MSK) == CESA_TDMA_OP)
  77. mv_cesa_adjust_op(engine, tdma->op);
  78. }
  79. }
  80. void mv_cesa_tdma_chain(struct mv_cesa_engine *engine,
  81. struct mv_cesa_req *dreq)
  82. {
  83. if (engine->chain.first == NULL && engine->chain.last == NULL) {
  84. engine->chain.first = dreq->chain.first;
  85. engine->chain.last = dreq->chain.last;
  86. } else {
  87. struct mv_cesa_tdma_desc *last;
  88. last = engine->chain.last;
  89. last->next = dreq->chain.first;
  90. engine->chain.last = dreq->chain.last;
  91. /*
  92. * Break the DMA chain if the CESA_TDMA_BREAK_CHAIN is set on
  93. * the last element of the current chain, or if the request
  94. * being queued needs the IV regs to be set before lauching
  95. * the request.
  96. */
  97. if (!(last->flags & CESA_TDMA_BREAK_CHAIN) &&
  98. !(dreq->chain.first->flags & CESA_TDMA_SET_STATE))
  99. last->next_dma = dreq->chain.first->cur_dma;
  100. }
  101. }
  102. int mv_cesa_tdma_process(struct mv_cesa_engine *engine, u32 status)
  103. {
  104. struct crypto_async_request *req = NULL;
  105. struct mv_cesa_tdma_desc *tdma = NULL, *next = NULL;
  106. dma_addr_t tdma_cur;
  107. int res = 0;
  108. tdma_cur = readl(engine->regs + CESA_TDMA_CUR);
  109. for (tdma = engine->chain.first; tdma; tdma = next) {
  110. spin_lock_bh(&engine->lock);
  111. next = tdma->next;
  112. spin_unlock_bh(&engine->lock);
  113. if (tdma->flags & CESA_TDMA_END_OF_REQ) {
  114. struct crypto_async_request *backlog = NULL;
  115. struct mv_cesa_ctx *ctx;
  116. u32 current_status;
  117. spin_lock_bh(&engine->lock);
  118. /*
  119. * if req is NULL, this means we're processing the
  120. * request in engine->req.
  121. */
  122. if (!req)
  123. req = engine->req;
  124. else
  125. req = mv_cesa_dequeue_req_locked(engine,
  126. &backlog);
  127. /* Re-chaining to the next request */
  128. engine->chain.first = tdma->next;
  129. tdma->next = NULL;
  130. /* If this is the last request, clear the chain */
  131. if (engine->chain.first == NULL)
  132. engine->chain.last = NULL;
  133. spin_unlock_bh(&engine->lock);
  134. ctx = crypto_tfm_ctx(req->tfm);
  135. current_status = (tdma->cur_dma == tdma_cur) ?
  136. status : CESA_SA_INT_ACC0_IDMA_DONE;
  137. res = ctx->ops->process(req, current_status);
  138. ctx->ops->complete(req);
  139. if (res == 0)
  140. mv_cesa_engine_enqueue_complete_request(engine,
  141. req);
  142. if (backlog)
  143. backlog->complete(backlog, -EINPROGRESS);
  144. }
  145. if (res || tdma->cur_dma == tdma_cur)
  146. break;
  147. }
  148. /* Save the last request in error to engine->req, so that the core
  149. * knows which request was fautly */
  150. if (res) {
  151. spin_lock_bh(&engine->lock);
  152. engine->req = req;
  153. spin_unlock_bh(&engine->lock);
  154. }
  155. return res;
  156. }
  157. static struct mv_cesa_tdma_desc *
  158. mv_cesa_dma_add_desc(struct mv_cesa_tdma_chain *chain, gfp_t flags)
  159. {
  160. struct mv_cesa_tdma_desc *new_tdma = NULL;
  161. dma_addr_t dma_handle;
  162. new_tdma = dma_pool_zalloc(cesa_dev->dma->tdma_desc_pool, flags,
  163. &dma_handle);
  164. if (!new_tdma)
  165. return ERR_PTR(-ENOMEM);
  166. new_tdma->cur_dma = dma_handle;
  167. if (chain->last) {
  168. chain->last->next_dma = cpu_to_le32(dma_handle);
  169. chain->last->next = new_tdma;
  170. } else {
  171. chain->first = new_tdma;
  172. }
  173. chain->last = new_tdma;
  174. return new_tdma;
  175. }
  176. int mv_cesa_dma_add_result_op(struct mv_cesa_tdma_chain *chain, dma_addr_t src,
  177. u32 size, u32 flags, gfp_t gfp_flags)
  178. {
  179. struct mv_cesa_tdma_desc *tdma, *op_desc;
  180. tdma = mv_cesa_dma_add_desc(chain, gfp_flags);
  181. if (IS_ERR(tdma))
  182. return PTR_ERR(tdma);
  183. /* We re-use an existing op_desc object to retrieve the context
  184. * and result instead of allocating a new one.
  185. * There is at least one object of this type in a CESA crypto
  186. * req, just pick the first one in the chain.
  187. */
  188. for (op_desc = chain->first; op_desc; op_desc = op_desc->next) {
  189. u32 type = op_desc->flags & CESA_TDMA_TYPE_MSK;
  190. if (type == CESA_TDMA_OP)
  191. break;
  192. }
  193. if (!op_desc)
  194. return -EIO;
  195. tdma->byte_cnt = cpu_to_le32(size | BIT(31));
  196. tdma->src = src;
  197. tdma->dst = op_desc->src;
  198. tdma->op = op_desc->op;
  199. flags &= (CESA_TDMA_DST_IN_SRAM | CESA_TDMA_SRC_IN_SRAM);
  200. tdma->flags = flags | CESA_TDMA_RESULT;
  201. return 0;
  202. }
  203. struct mv_cesa_op_ctx *mv_cesa_dma_add_op(struct mv_cesa_tdma_chain *chain,
  204. const struct mv_cesa_op_ctx *op_templ,
  205. bool skip_ctx,
  206. gfp_t flags)
  207. {
  208. struct mv_cesa_tdma_desc *tdma;
  209. struct mv_cesa_op_ctx *op;
  210. dma_addr_t dma_handle;
  211. unsigned int size;
  212. tdma = mv_cesa_dma_add_desc(chain, flags);
  213. if (IS_ERR(tdma))
  214. return ERR_CAST(tdma);
  215. op = dma_pool_alloc(cesa_dev->dma->op_pool, flags, &dma_handle);
  216. if (!op)
  217. return ERR_PTR(-ENOMEM);
  218. *op = *op_templ;
  219. size = skip_ctx ? sizeof(op->desc) : sizeof(*op);
  220. tdma = chain->last;
  221. tdma->op = op;
  222. tdma->byte_cnt = cpu_to_le32(size | BIT(31));
  223. tdma->src = cpu_to_le32(dma_handle);
  224. tdma->dst = CESA_SA_CFG_SRAM_OFFSET;
  225. tdma->flags = CESA_TDMA_DST_IN_SRAM | CESA_TDMA_OP;
  226. return op;
  227. }
  228. int mv_cesa_dma_add_data_transfer(struct mv_cesa_tdma_chain *chain,
  229. dma_addr_t dst, dma_addr_t src, u32 size,
  230. u32 flags, gfp_t gfp_flags)
  231. {
  232. struct mv_cesa_tdma_desc *tdma;
  233. tdma = mv_cesa_dma_add_desc(chain, gfp_flags);
  234. if (IS_ERR(tdma))
  235. return PTR_ERR(tdma);
  236. tdma->byte_cnt = cpu_to_le32(size | BIT(31));
  237. tdma->src = src;
  238. tdma->dst = dst;
  239. flags &= (CESA_TDMA_DST_IN_SRAM | CESA_TDMA_SRC_IN_SRAM);
  240. tdma->flags = flags | CESA_TDMA_DATA;
  241. return 0;
  242. }
  243. int mv_cesa_dma_add_dummy_launch(struct mv_cesa_tdma_chain *chain, gfp_t flags)
  244. {
  245. struct mv_cesa_tdma_desc *tdma;
  246. tdma = mv_cesa_dma_add_desc(chain, flags);
  247. return PTR_ERR_OR_ZERO(tdma);
  248. }
  249. int mv_cesa_dma_add_dummy_end(struct mv_cesa_tdma_chain *chain, gfp_t flags)
  250. {
  251. struct mv_cesa_tdma_desc *tdma;
  252. tdma = mv_cesa_dma_add_desc(chain, flags);
  253. if (IS_ERR(tdma))
  254. return PTR_ERR(tdma);
  255. tdma->byte_cnt = cpu_to_le32(BIT(31));
  256. return 0;
  257. }
  258. int mv_cesa_dma_add_op_transfers(struct mv_cesa_tdma_chain *chain,
  259. struct mv_cesa_dma_iter *dma_iter,
  260. struct mv_cesa_sg_dma_iter *sgiter,
  261. gfp_t gfp_flags)
  262. {
  263. u32 flags = sgiter->dir == DMA_TO_DEVICE ?
  264. CESA_TDMA_DST_IN_SRAM : CESA_TDMA_SRC_IN_SRAM;
  265. unsigned int len;
  266. do {
  267. dma_addr_t dst, src;
  268. int ret;
  269. len = mv_cesa_req_dma_iter_transfer_len(dma_iter, sgiter);
  270. if (sgiter->dir == DMA_TO_DEVICE) {
  271. dst = CESA_SA_DATA_SRAM_OFFSET + sgiter->op_offset;
  272. src = sg_dma_address(sgiter->sg) + sgiter->offset;
  273. } else {
  274. dst = sg_dma_address(sgiter->sg) + sgiter->offset;
  275. src = CESA_SA_DATA_SRAM_OFFSET + sgiter->op_offset;
  276. }
  277. ret = mv_cesa_dma_add_data_transfer(chain, dst, src, len,
  278. flags, gfp_flags);
  279. if (ret)
  280. return ret;
  281. } while (mv_cesa_req_dma_iter_next_transfer(dma_iter, sgiter, len));
  282. return 0;
  283. }