arm_smccc_trng.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Randomness driver for the ARM SMCCC TRNG Firmware Interface
  4. * https://developer.arm.com/documentation/den0098/latest/
  5. *
  6. * Copyright (C) 2020 Arm Ltd.
  7. *
  8. * The ARM TRNG firmware interface specifies a protocol to read entropy
  9. * from a higher exception level, to abstract from any machine specific
  10. * implemenations and allow easier use in hypervisors.
  11. *
  12. * The firmware interface is realised using the SMCCC specification.
  13. */
  14. #include <linux/bits.h>
  15. #include <linux/device.h>
  16. #include <linux/hw_random.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/arm-smccc.h>
  20. #ifdef CONFIG_ARM64
  21. #define ARM_SMCCC_TRNG_RND ARM_SMCCC_TRNG_RND64
  22. #define MAX_BITS_PER_CALL (3 * 64UL)
  23. #else
  24. #define ARM_SMCCC_TRNG_RND ARM_SMCCC_TRNG_RND32
  25. #define MAX_BITS_PER_CALL (3 * 32UL)
  26. #endif
  27. /* We don't want to allow the firmware to stall us forever. */
  28. #define SMCCC_TRNG_MAX_TRIES 20
  29. #define SMCCC_RET_TRNG_INVALID_PARAMETER -2
  30. #define SMCCC_RET_TRNG_NO_ENTROPY -3
  31. static int copy_from_registers(char *buf, struct arm_smccc_res *res,
  32. size_t bytes)
  33. {
  34. unsigned int chunk, copied;
  35. if (bytes == 0)
  36. return 0;
  37. chunk = min(bytes, sizeof(long));
  38. memcpy(buf, &res->a3, chunk);
  39. copied = chunk;
  40. if (copied >= bytes)
  41. return copied;
  42. chunk = min((bytes - copied), sizeof(long));
  43. memcpy(&buf[copied], &res->a2, chunk);
  44. copied += chunk;
  45. if (copied >= bytes)
  46. return copied;
  47. chunk = min((bytes - copied), sizeof(long));
  48. memcpy(&buf[copied], &res->a1, chunk);
  49. return copied + chunk;
  50. }
  51. static int smccc_trng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  52. {
  53. struct arm_smccc_res res;
  54. u8 *buf = data;
  55. unsigned int copied = 0;
  56. int tries = 0;
  57. while (copied < max) {
  58. size_t bits = min_t(size_t, (max - copied) * BITS_PER_BYTE,
  59. MAX_BITS_PER_CALL);
  60. arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_RND, bits, &res);
  61. switch ((int)res.a0) {
  62. case SMCCC_RET_SUCCESS:
  63. copied += copy_from_registers(buf + copied, &res,
  64. bits / BITS_PER_BYTE);
  65. tries = 0;
  66. break;
  67. case SMCCC_RET_TRNG_NO_ENTROPY:
  68. if (!wait)
  69. return copied;
  70. tries++;
  71. if (tries >= SMCCC_TRNG_MAX_TRIES)
  72. return copied;
  73. cond_resched();
  74. break;
  75. default:
  76. return -EIO;
  77. }
  78. }
  79. return copied;
  80. }
  81. static int smccc_trng_probe(struct platform_device *pdev)
  82. {
  83. struct hwrng *trng;
  84. trng = devm_kzalloc(&pdev->dev, sizeof(*trng), GFP_KERNEL);
  85. if (!trng)
  86. return -ENOMEM;
  87. trng->name = "smccc_trng";
  88. trng->read = smccc_trng_read;
  89. return devm_hwrng_register(&pdev->dev, trng);
  90. }
  91. static struct platform_driver smccc_trng_driver = {
  92. .driver = {
  93. .name = "smccc_trng",
  94. },
  95. .probe = smccc_trng_probe,
  96. };
  97. module_platform_driver(smccc_trng_driver);
  98. MODULE_ALIAS("platform:smccc_trng");
  99. MODULE_AUTHOR("Andre Przywara");
  100. MODULE_DESCRIPTION("Arm SMCCC TRNG firmware interface support");
  101. MODULE_LICENSE("GPL");