sun8i-ce-trng.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * sun8i-ce-trng.c - hardware cryptographic offloader for
  4. * Allwinner H3/A64/H5/H2+/H6/R40 SoC
  5. *
  6. * Copyright (C) 2015-2020 Corentin Labbe <[email protected]>
  7. *
  8. * This file handle the TRNG
  9. *
  10. * You could find a link for the datasheet in Documentation/arm/sunxi.rst
  11. */
  12. #include "sun8i-ce.h"
  13. #include <linux/dma-mapping.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/hw_random.h>
  16. /*
  17. * Note that according to the algorithm ID, 2 versions of the TRNG exists,
  18. * The first present in H3/H5/R40/A64 and the second present in H6.
  19. * This file adds support for both, but only the second is working
  20. * reliabily according to rngtest.
  21. **/
  22. static int sun8i_ce_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  23. {
  24. struct sun8i_ce_dev *ce;
  25. dma_addr_t dma_dst;
  26. int err = 0;
  27. int flow = 3;
  28. unsigned int todo;
  29. struct sun8i_ce_flow *chan;
  30. struct ce_task *cet;
  31. u32 common;
  32. void *d;
  33. ce = container_of(rng, struct sun8i_ce_dev, trng);
  34. /* round the data length to a multiple of 32*/
  35. todo = max + 32;
  36. todo -= todo % 32;
  37. d = kzalloc(todo, GFP_KERNEL | GFP_DMA);
  38. if (!d)
  39. return -ENOMEM;
  40. #ifdef CONFIG_CRYPTO_DEV_SUN8I_CE_DEBUG
  41. ce->hwrng_stat_req++;
  42. ce->hwrng_stat_bytes += todo;
  43. #endif
  44. dma_dst = dma_map_single(ce->dev, d, todo, DMA_FROM_DEVICE);
  45. if (dma_mapping_error(ce->dev, dma_dst)) {
  46. dev_err(ce->dev, "Cannot DMA MAP DST\n");
  47. err = -EFAULT;
  48. goto err_dst;
  49. }
  50. err = pm_runtime_resume_and_get(ce->dev);
  51. if (err < 0)
  52. goto err_pm;
  53. mutex_lock(&ce->rnglock);
  54. chan = &ce->chanlist[flow];
  55. cet = &chan->tl[0];
  56. memset(cet, 0, sizeof(struct ce_task));
  57. cet->t_id = cpu_to_le32(flow);
  58. common = ce->variant->trng | CE_COMM_INT;
  59. cet->t_common_ctl = cpu_to_le32(common);
  60. /* recent CE (H6) need length in bytes, in word otherwise */
  61. if (ce->variant->trng_t_dlen_in_bytes)
  62. cet->t_dlen = cpu_to_le32(todo);
  63. else
  64. cet->t_dlen = cpu_to_le32(todo / 4);
  65. cet->t_sym_ctl = 0;
  66. cet->t_asym_ctl = 0;
  67. cet->t_dst[0].addr = cpu_to_le32(dma_dst);
  68. cet->t_dst[0].len = cpu_to_le32(todo / 4);
  69. ce->chanlist[flow].timeout = todo;
  70. err = sun8i_ce_run_task(ce, 3, "TRNG");
  71. mutex_unlock(&ce->rnglock);
  72. pm_runtime_put(ce->dev);
  73. err_pm:
  74. dma_unmap_single(ce->dev, dma_dst, todo, DMA_FROM_DEVICE);
  75. if (!err) {
  76. memcpy(data, d, max);
  77. err = max;
  78. }
  79. err_dst:
  80. kfree_sensitive(d);
  81. return err;
  82. }
  83. int sun8i_ce_hwrng_register(struct sun8i_ce_dev *ce)
  84. {
  85. int ret;
  86. if (ce->variant->trng == CE_ID_NOTSUPP) {
  87. dev_info(ce->dev, "TRNG not supported\n");
  88. return 0;
  89. }
  90. ce->trng.name = "sun8i Crypto Engine TRNG";
  91. ce->trng.read = sun8i_ce_trng_read;
  92. ce->trng.quality = 1000;
  93. ret = hwrng_register(&ce->trng);
  94. if (ret)
  95. dev_err(ce->dev, "Fail to register the TRNG\n");
  96. return ret;
  97. }
  98. void sun8i_ce_hwrng_unregister(struct sun8i_ce_dev *ce)
  99. {
  100. if (ce->variant->trng == CE_ID_NOTSUPP)
  101. return;
  102. hwrng_unregister(&ce->trng);
  103. }