core.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/dma-mapping.h>
  7. #include <linux/interconnect.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/module.h>
  10. #include <linux/mod_devicetable.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/types.h>
  14. #include <crypto/algapi.h>
  15. #include <crypto/internal/hash.h>
  16. #include "core.h"
  17. #include "cipher.h"
  18. #include "sha.h"
  19. #include "aead.h"
  20. #define QCE_MAJOR_VERSION5 0x05
  21. #define QCE_QUEUE_LENGTH 1
  22. #define QCE_DEFAULT_MEM_BANDWIDTH 393600
  23. static const struct qce_algo_ops *qce_ops[] = {
  24. #ifdef CONFIG_CRYPTO_DEV_QCE_SKCIPHER
  25. &skcipher_ops,
  26. #endif
  27. #ifdef CONFIG_CRYPTO_DEV_QCE_SHA
  28. &ahash_ops,
  29. #endif
  30. #ifdef CONFIG_CRYPTO_DEV_QCE_AEAD
  31. &aead_ops,
  32. #endif
  33. };
  34. static void qce_unregister_algs(struct qce_device *qce)
  35. {
  36. const struct qce_algo_ops *ops;
  37. int i;
  38. for (i = 0; i < ARRAY_SIZE(qce_ops); i++) {
  39. ops = qce_ops[i];
  40. ops->unregister_algs(qce);
  41. }
  42. }
  43. static int qce_register_algs(struct qce_device *qce)
  44. {
  45. const struct qce_algo_ops *ops;
  46. int i, j, ret = -ENODEV;
  47. for (i = 0; i < ARRAY_SIZE(qce_ops); i++) {
  48. ops = qce_ops[i];
  49. ret = ops->register_algs(qce);
  50. if (ret) {
  51. for (j = i - 1; j >= 0; j--)
  52. ops->unregister_algs(qce);
  53. return ret;
  54. }
  55. }
  56. return 0;
  57. }
  58. static int qce_handle_request(struct crypto_async_request *async_req)
  59. {
  60. int ret = -EINVAL, i;
  61. const struct qce_algo_ops *ops;
  62. u32 type = crypto_tfm_alg_type(async_req->tfm);
  63. for (i = 0; i < ARRAY_SIZE(qce_ops); i++) {
  64. ops = qce_ops[i];
  65. if (type != ops->type)
  66. continue;
  67. ret = ops->async_req_handle(async_req);
  68. break;
  69. }
  70. return ret;
  71. }
  72. static int qce_handle_queue(struct qce_device *qce,
  73. struct crypto_async_request *req)
  74. {
  75. struct crypto_async_request *async_req, *backlog;
  76. unsigned long flags;
  77. int ret = 0, err;
  78. spin_lock_irqsave(&qce->lock, flags);
  79. if (req)
  80. ret = crypto_enqueue_request(&qce->queue, req);
  81. /* busy, do not dequeue request */
  82. if (qce->req) {
  83. spin_unlock_irqrestore(&qce->lock, flags);
  84. return ret;
  85. }
  86. backlog = crypto_get_backlog(&qce->queue);
  87. async_req = crypto_dequeue_request(&qce->queue);
  88. if (async_req)
  89. qce->req = async_req;
  90. spin_unlock_irqrestore(&qce->lock, flags);
  91. if (!async_req)
  92. return ret;
  93. if (backlog) {
  94. spin_lock_bh(&qce->lock);
  95. crypto_request_complete(backlog, -EINPROGRESS);
  96. spin_unlock_bh(&qce->lock);
  97. }
  98. err = qce_handle_request(async_req);
  99. if (err) {
  100. qce->result = err;
  101. tasklet_schedule(&qce->done_tasklet);
  102. }
  103. return ret;
  104. }
  105. static void qce_tasklet_req_done(unsigned long data)
  106. {
  107. struct qce_device *qce = (struct qce_device *)data;
  108. struct crypto_async_request *req;
  109. unsigned long flags;
  110. spin_lock_irqsave(&qce->lock, flags);
  111. req = qce->req;
  112. qce->req = NULL;
  113. spin_unlock_irqrestore(&qce->lock, flags);
  114. if (req)
  115. crypto_request_complete(req, qce->result);
  116. qce_handle_queue(qce, NULL);
  117. }
  118. static int qce_async_request_enqueue(struct qce_device *qce,
  119. struct crypto_async_request *req)
  120. {
  121. return qce_handle_queue(qce, req);
  122. }
  123. static void qce_async_request_done(struct qce_device *qce, int ret)
  124. {
  125. qce->result = ret;
  126. tasklet_schedule(&qce->done_tasklet);
  127. }
  128. static int qce_check_version(struct qce_device *qce)
  129. {
  130. u32 major, minor, step;
  131. qce_get_version(qce, &major, &minor, &step);
  132. /*
  133. * the driver does not support v5 with minor 0 because it has special
  134. * alignment requirements.
  135. */
  136. if (major != QCE_MAJOR_VERSION5 || minor == 0)
  137. return -ENODEV;
  138. qce->burst_size = QCE_BAM_BURST_SIZE;
  139. /*
  140. * Rx and tx pipes are treated as a pair inside CE.
  141. * Pipe pair number depends on the actual BAM dma pipe
  142. * that is used for transfers. The BAM dma pipes are passed
  143. * from the device tree and used to derive the pipe pair
  144. * id in the CE driver as follows.
  145. * BAM dma pipes(rx, tx) CE pipe pair id
  146. * 0,1 0
  147. * 2,3 1
  148. * 4,5 2
  149. * 6,7 3
  150. * ...
  151. */
  152. qce->pipe_pair_id = qce->dma.rxchan->chan_id >> 1;
  153. dev_dbg(qce->dev, "Crypto device found, version %d.%d.%d\n",
  154. major, minor, step);
  155. return 0;
  156. }
  157. static int qce_crypto_probe(struct platform_device *pdev)
  158. {
  159. struct device *dev = &pdev->dev;
  160. struct qce_device *qce;
  161. int ret;
  162. qce = devm_kzalloc(dev, sizeof(*qce), GFP_KERNEL);
  163. if (!qce)
  164. return -ENOMEM;
  165. qce->dev = dev;
  166. platform_set_drvdata(pdev, qce);
  167. qce->base = devm_platform_ioremap_resource(pdev, 0);
  168. if (IS_ERR(qce->base))
  169. return PTR_ERR(qce->base);
  170. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  171. if (ret < 0)
  172. return ret;
  173. qce->core = devm_clk_get_optional(qce->dev, "core");
  174. if (IS_ERR(qce->core))
  175. return PTR_ERR(qce->core);
  176. qce->iface = devm_clk_get_optional(qce->dev, "iface");
  177. if (IS_ERR(qce->iface))
  178. return PTR_ERR(qce->iface);
  179. qce->bus = devm_clk_get_optional(qce->dev, "bus");
  180. if (IS_ERR(qce->bus))
  181. return PTR_ERR(qce->bus);
  182. qce->mem_path = devm_of_icc_get(qce->dev, "memory");
  183. if (IS_ERR(qce->mem_path))
  184. return PTR_ERR(qce->mem_path);
  185. ret = icc_set_bw(qce->mem_path, QCE_DEFAULT_MEM_BANDWIDTH, QCE_DEFAULT_MEM_BANDWIDTH);
  186. if (ret)
  187. return ret;
  188. ret = clk_prepare_enable(qce->core);
  189. if (ret)
  190. goto err_mem_path_disable;
  191. ret = clk_prepare_enable(qce->iface);
  192. if (ret)
  193. goto err_clks_core;
  194. ret = clk_prepare_enable(qce->bus);
  195. if (ret)
  196. goto err_clks_iface;
  197. ret = qce_dma_request(qce->dev, &qce->dma);
  198. if (ret)
  199. goto err_clks;
  200. ret = qce_check_version(qce);
  201. if (ret)
  202. goto err_dma;
  203. spin_lock_init(&qce->lock);
  204. tasklet_init(&qce->done_tasklet, qce_tasklet_req_done,
  205. (unsigned long)qce);
  206. crypto_init_queue(&qce->queue, QCE_QUEUE_LENGTH);
  207. qce->async_req_enqueue = qce_async_request_enqueue;
  208. qce->async_req_done = qce_async_request_done;
  209. ret = qce_register_algs(qce);
  210. if (ret)
  211. goto err_dma;
  212. return 0;
  213. err_dma:
  214. qce_dma_release(&qce->dma);
  215. err_clks:
  216. clk_disable_unprepare(qce->bus);
  217. err_clks_iface:
  218. clk_disable_unprepare(qce->iface);
  219. err_clks_core:
  220. clk_disable_unprepare(qce->core);
  221. err_mem_path_disable:
  222. icc_set_bw(qce->mem_path, 0, 0);
  223. return ret;
  224. }
  225. static void qce_crypto_remove(struct platform_device *pdev)
  226. {
  227. struct qce_device *qce = platform_get_drvdata(pdev);
  228. tasklet_kill(&qce->done_tasklet);
  229. qce_unregister_algs(qce);
  230. qce_dma_release(&qce->dma);
  231. clk_disable_unprepare(qce->bus);
  232. clk_disable_unprepare(qce->iface);
  233. clk_disable_unprepare(qce->core);
  234. }
  235. static const struct of_device_id qce_crypto_of_match[] = {
  236. { .compatible = "qcom,crypto-v5.1", },
  237. { .compatible = "qcom,crypto-v5.4", },
  238. { .compatible = "qcom,qce", },
  239. {}
  240. };
  241. MODULE_DEVICE_TABLE(of, qce_crypto_of_match);
  242. static struct platform_driver qce_crypto_driver = {
  243. .probe = qce_crypto_probe,
  244. .remove_new = qce_crypto_remove,
  245. .driver = {
  246. .name = KBUILD_MODNAME,
  247. .of_match_table = qce_crypto_of_match,
  248. },
  249. };
  250. module_platform_driver(qce_crypto_driver);
  251. MODULE_LICENSE("GPL v2");
  252. MODULE_DESCRIPTION("Qualcomm crypto engine driver");
  253. MODULE_ALIAS("platform:" KBUILD_MODNAME);
  254. MODULE_AUTHOR("The Linux Foundation");