core.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/cleanup.h>
  6. #include <linux/clk.h>
  7. #include <linux/device.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/interconnect.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/module.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/types.h>
  15. #include <crypto/algapi.h>
  16. #include <crypto/internal/hash.h>
  17. #include "core.h"
  18. #include "cipher.h"
  19. #include "sha.h"
  20. #include "aead.h"
  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(void *data)
  35. {
  36. const struct qce_algo_ops *ops;
  37. struct qce_device *qce = data;
  38. int i;
  39. for (i = 0; i < ARRAY_SIZE(qce_ops); i++) {
  40. ops = qce_ops[i];
  41. ops->unregister_algs(qce);
  42. }
  43. }
  44. static int devm_qce_register_algs(struct qce_device *qce)
  45. {
  46. const struct qce_algo_ops *ops;
  47. int i, j, ret = -ENODEV;
  48. for (i = 0; i < ARRAY_SIZE(qce_ops); i++) {
  49. ops = qce_ops[i];
  50. ret = ops->register_algs(qce);
  51. if (ret) {
  52. for (j = i - 1; j >= 0; j--)
  53. ops->unregister_algs(qce);
  54. return ret;
  55. }
  56. }
  57. return devm_add_action_or_reset(qce->dev, qce_unregister_algs, qce);
  58. }
  59. static int qce_handle_request(struct crypto_async_request *async_req)
  60. {
  61. int ret = -EINVAL, i;
  62. const struct qce_algo_ops *ops;
  63. u32 type = crypto_tfm_alg_type(async_req->tfm);
  64. for (i = 0; i < ARRAY_SIZE(qce_ops); i++) {
  65. ops = qce_ops[i];
  66. if (type != ops->type)
  67. continue;
  68. ret = ops->async_req_handle(async_req);
  69. break;
  70. }
  71. return ret;
  72. }
  73. static int qce_handle_queue(struct qce_device *qce,
  74. struct crypto_async_request *req)
  75. {
  76. struct crypto_async_request *async_req, *backlog;
  77. int ret = 0, err;
  78. scoped_guard(mutex, &qce->lock) {
  79. if (req)
  80. ret = crypto_enqueue_request(&qce->queue, req);
  81. /* busy, do not dequeue request */
  82. if (qce->req)
  83. return ret;
  84. backlog = crypto_get_backlog(&qce->queue);
  85. async_req = crypto_dequeue_request(&qce->queue);
  86. if (async_req)
  87. qce->req = async_req;
  88. }
  89. if (!async_req)
  90. return ret;
  91. if (backlog) {
  92. scoped_guard(mutex, &qce->lock)
  93. crypto_request_complete(backlog, -EINPROGRESS);
  94. }
  95. err = qce_handle_request(async_req);
  96. if (err) {
  97. qce->result = err;
  98. schedule_work(&qce->done_work);
  99. }
  100. return ret;
  101. }
  102. static void qce_req_done_work(struct work_struct *work)
  103. {
  104. struct qce_device *qce = container_of(work, struct qce_device,
  105. done_work);
  106. struct crypto_async_request *req;
  107. scoped_guard(mutex, &qce->lock) {
  108. req = qce->req;
  109. qce->req = NULL;
  110. }
  111. if (req)
  112. crypto_request_complete(req, qce->result);
  113. qce_handle_queue(qce, NULL);
  114. }
  115. static int qce_async_request_enqueue(struct qce_device *qce,
  116. struct crypto_async_request *req)
  117. {
  118. return qce_handle_queue(qce, req);
  119. }
  120. static void qce_async_request_done(struct qce_device *qce, int ret)
  121. {
  122. qce->result = ret;
  123. schedule_work(&qce->done_work);
  124. }
  125. static int qce_check_version(struct qce_device *qce)
  126. {
  127. u32 major, minor, step;
  128. qce_get_version(qce, &major, &minor, &step);
  129. /*
  130. * the driver does not support v5 with minor 0 because it has special
  131. * alignment requirements.
  132. */
  133. if (major == 5 && minor == 0)
  134. return -ENODEV;
  135. qce->burst_size = QCE_BAM_BURST_SIZE;
  136. /*
  137. * Rx and tx pipes are treated as a pair inside CE.
  138. * Pipe pair number depends on the actual BAM dma pipe
  139. * that is used for transfers. The BAM dma pipes are passed
  140. * from the device tree and used to derive the pipe pair
  141. * id in the CE driver as follows.
  142. * BAM dma pipes(rx, tx) CE pipe pair id
  143. * 0,1 0
  144. * 2,3 1
  145. * 4,5 2
  146. * 6,7 3
  147. * ...
  148. */
  149. qce->pipe_pair_id = qce->dma.rxchan->chan_id >> 1;
  150. dev_dbg(qce->dev, "Crypto device found, version %d.%d.%d\n",
  151. major, minor, step);
  152. return 0;
  153. }
  154. static int qce_crypto_probe(struct platform_device *pdev)
  155. {
  156. struct device *dev = &pdev->dev;
  157. struct qce_device *qce;
  158. int ret;
  159. qce = devm_kzalloc(dev, sizeof(*qce), GFP_KERNEL);
  160. if (!qce)
  161. return -ENOMEM;
  162. qce->dev = dev;
  163. platform_set_drvdata(pdev, qce);
  164. qce->base = devm_platform_ioremap_resource(pdev, 0);
  165. if (IS_ERR(qce->base))
  166. return PTR_ERR(qce->base);
  167. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  168. if (ret < 0)
  169. return ret;
  170. qce->core = devm_clk_get_optional_enabled(qce->dev, "core");
  171. if (IS_ERR(qce->core))
  172. return PTR_ERR(qce->core);
  173. qce->iface = devm_clk_get_optional_enabled(qce->dev, "iface");
  174. if (IS_ERR(qce->iface))
  175. return PTR_ERR(qce->iface);
  176. qce->bus = devm_clk_get_optional_enabled(qce->dev, "bus");
  177. if (IS_ERR(qce->bus))
  178. return PTR_ERR(qce->bus);
  179. qce->mem_path = devm_of_icc_get(qce->dev, "memory");
  180. if (IS_ERR(qce->mem_path))
  181. return PTR_ERR(qce->mem_path);
  182. ret = icc_set_bw(qce->mem_path, QCE_DEFAULT_MEM_BANDWIDTH, QCE_DEFAULT_MEM_BANDWIDTH);
  183. if (ret)
  184. return ret;
  185. ret = devm_qce_dma_request(qce->dev, &qce->dma);
  186. if (ret)
  187. return ret;
  188. ret = qce_check_version(qce);
  189. if (ret)
  190. return ret;
  191. ret = devm_mutex_init(qce->dev, &qce->lock);
  192. if (ret)
  193. return ret;
  194. INIT_WORK(&qce->done_work, qce_req_done_work);
  195. crypto_init_queue(&qce->queue, QCE_QUEUE_LENGTH);
  196. qce->async_req_enqueue = qce_async_request_enqueue;
  197. qce->async_req_done = qce_async_request_done;
  198. return devm_qce_register_algs(qce);
  199. }
  200. static const struct of_device_id qce_crypto_of_match[] = {
  201. { .compatible = "qcom,crypto-v5.1", },
  202. { .compatible = "qcom,crypto-v5.4", },
  203. { .compatible = "qcom,qce", },
  204. {}
  205. };
  206. MODULE_DEVICE_TABLE(of, qce_crypto_of_match);
  207. static struct platform_driver qce_crypto_driver = {
  208. .probe = qce_crypto_probe,
  209. .driver = {
  210. .name = KBUILD_MODNAME,
  211. .of_match_table = qce_crypto_of_match,
  212. },
  213. };
  214. module_platform_driver(qce_crypto_driver);
  215. MODULE_LICENSE("GPL v2");
  216. MODULE_DESCRIPTION("Qualcomm crypto engine driver");
  217. MODULE_ALIAS("platform:" KBUILD_MODNAME);
  218. MODULE_AUTHOR("The Linux Foundation");