smccc_trng.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2022, Linaro Limited
  4. */
  5. #define LOG_CATEGORY UCLASS_RNG
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <linker_lists.h>
  9. #include <log.h>
  10. #include <rng.h>
  11. #include <dm/device_compat.h>
  12. #include <linux/arm-smccc.h>
  13. #include <linux/bitops.h>
  14. #include <linux/kernel.h>
  15. #include <linux/psci.h>
  16. #define DRIVER_NAME "smccc-trng"
  17. /**
  18. * Arm SMCCC TRNG firmware interface specification:
  19. * https://developer.arm.com/documentation/den0098/latest/
  20. */
  21. #define ARM_SMCCC_TRNG_VERSION 0x84000050
  22. #define ARM_SMCCC_TRNG_FEATURES 0x84000051
  23. #define ARM_SMCCC_TRNG_GET_UUID 0x84000052
  24. #define ARM_SMCCC_TRNG_RND_32 0x84000053
  25. #define ARM_SMCCC_TRNG_RND_64 0xC4000053
  26. #define ARM_SMCCC_RET_TRNG_SUCCESS ((ulong)0)
  27. #define ARM_SMCCC_RET_TRNG_NOT_SUPPORTED ((ulong)-1)
  28. #define ARM_SMCCC_RET_TRNG_INVALID_PARAMETER ((ulong)-2)
  29. #define ARM_SMCCC_RET_TRNG_NO_ENTROPY ((ulong)-3)
  30. #define TRNG_MAJOR_MASK GENMASK(30, 16)
  31. #define TRNG_MAJOR_SHIFT 16
  32. #define TRNG_MINOR_MASK GENMASK(15, 0)
  33. #define TRNG_MINOR_SHIFT 0
  34. #define TRNG_MAX_RND_64 (192 / 8)
  35. #define TRNG_MAX_RND_32 (96 / 8)
  36. /**
  37. * struct smccc_trng_priv - Private data for SMCCC TRNG support
  38. *
  39. * @smc64 - True if TRNG_RND_64 is supported, false if TRNG_RND_32 is supported
  40. */
  41. struct smccc_trng_priv {
  42. bool smc64;
  43. };
  44. /*
  45. * Copy random bytes from ulong SMCCC output register to target buffer
  46. * Defines 2 function flavors for whether ARM_SMCCC_TRNG_RND_32 or
  47. * ARM_SMCCC_TRNG_RND_64 was used to invoke the service.
  48. */
  49. static size_t smc32_copy_sample(u8 **ptr, size_t size, ulong *rnd)
  50. {
  51. size_t len = min(size, sizeof(u32));
  52. u32 sample = *rnd;
  53. memcpy(*ptr, &sample, len);
  54. *ptr += len;
  55. return size - len;
  56. }
  57. static size_t smc64_copy_sample(u8 **ptr, size_t size, ulong *rnd)
  58. {
  59. size_t len = min(size, sizeof(u64));
  60. u64 sample = *rnd;
  61. memcpy(*ptr, &sample, len);
  62. *ptr += len;
  63. return size - len;
  64. }
  65. static int smccc_trng_read(struct udevice *dev, void *data, size_t len)
  66. {
  67. struct psci_plat_data *smccc = dev_get_parent_plat(dev);
  68. struct smccc_trng_priv *priv = dev_get_priv(dev);
  69. struct arm_smccc_res res;
  70. u32 func_id;
  71. u8 *ptr = data;
  72. size_t rem = len;
  73. size_t max_sz;
  74. size_t (*copy_sample)(u8 **ptr, size_t size, ulong *rnd);
  75. if (priv->smc64) {
  76. copy_sample = smc64_copy_sample;
  77. func_id = ARM_SMCCC_TRNG_RND_64;
  78. max_sz = TRNG_MAX_RND_64;
  79. } else {
  80. copy_sample = smc32_copy_sample;
  81. func_id = ARM_SMCCC_TRNG_RND_32;
  82. max_sz = TRNG_MAX_RND_32;
  83. }
  84. while (rem) {
  85. size_t sz = min(rem, max_sz);
  86. smccc->invoke_fn(func_id, sz * 8, 0, 0, 0, 0, 0, 0, &res);
  87. switch (res.a0) {
  88. case ARM_SMCCC_RET_TRNG_SUCCESS:
  89. break;
  90. case ARM_SMCCC_RET_TRNG_NO_ENTROPY:
  91. continue;
  92. default:
  93. return -EIO;
  94. }
  95. rem -= sz;
  96. sz = copy_sample(&ptr, sz, &res.a3);
  97. if (sz)
  98. sz = copy_sample(&ptr, sz, &res.a2);
  99. if (sz)
  100. sz = copy_sample(&ptr, sz, &res.a1);
  101. }
  102. return 0;
  103. }
  104. static const struct dm_rng_ops smccc_trng_ops = {
  105. .read = smccc_trng_read,
  106. };
  107. static bool smccc_trng_is_supported(void (*invoke_fn)(unsigned long a0, unsigned long a1,
  108. unsigned long a2, unsigned long a3,
  109. unsigned long a4, unsigned long a5,
  110. unsigned long a6, unsigned long a7,
  111. struct arm_smccc_res *res))
  112. {
  113. struct arm_smccc_res res;
  114. (*invoke_fn)(ARM_SMCCC_ARCH_FEATURES, ARM_SMCCC_TRNG_VERSION, 0, 0, 0, 0, 0, 0, &res);
  115. if (res.a0 == ARM_SMCCC_RET_NOT_SUPPORTED)
  116. return false;
  117. (*invoke_fn)(ARM_SMCCC_TRNG_VERSION, 0, 0, 0, 0, 0, 0, 0, &res);
  118. if (res.a0 & BIT(31))
  119. return false;
  120. /* Test 64bit interface and fallback to 32bit interface */
  121. invoke_fn(ARM_SMCCC_TRNG_FEATURES, ARM_SMCCC_TRNG_RND_64,
  122. 0, 0, 0, 0, 0, 0, &res);
  123. if (res.a0 == ARM_SMCCC_RET_TRNG_NOT_SUPPORTED)
  124. invoke_fn(ARM_SMCCC_TRNG_FEATURES, ARM_SMCCC_TRNG_RND_32,
  125. 0, 0, 0, 0, 0, 0, &res);
  126. return res.a0 == ARM_SMCCC_RET_TRNG_SUCCESS;
  127. }
  128. ARM_SMCCC_FEATURE_DRIVER(smccc_trng) = {
  129. .driver_name = DRIVER_NAME,
  130. .is_supported = smccc_trng_is_supported,
  131. };
  132. static int smccc_trng_probe(struct udevice *dev)
  133. {
  134. struct psci_plat_data *smccc = dev_get_parent_plat(dev);
  135. struct smccc_trng_priv *priv = dev_get_priv(dev);
  136. struct arm_smccc_res res;
  137. if (!(smccc_trng_is_supported(smccc->invoke_fn)))
  138. return -ENODEV;
  139. /* At least one of 64bit and 32bit interfaces is available */
  140. smccc->invoke_fn(ARM_SMCCC_TRNG_FEATURES, ARM_SMCCC_TRNG_RND_64,
  141. 0, 0, 0, 0, 0, 0, &res);
  142. priv->smc64 = (res.a0 == ARM_SMCCC_RET_TRNG_SUCCESS);
  143. #ifdef DEBUG
  144. smccc->invoke_fn(ARM_SMCCC_TRNG_GET_UUID, 0, 0, 0, 0, 0, 0, 0, &res);
  145. if (res.a0 != ARM_SMCCC_RET_TRNG_NOT_SUPPORTED) {
  146. unsigned long uuid_a0 = res.a0;
  147. unsigned long uuid_a1 = res.a1;
  148. unsigned long uuid_a2 = res.a2;
  149. unsigned long uuid_a3 = res.a3;
  150. unsigned long major, minor;
  151. smccc->invoke_fn(ARM_SMCCC_TRNG_VERSION, 0, 0, 0, 0, 0, 0, 0, &res);
  152. major = (res.a0 & TRNG_MAJOR_MASK) >> TRNG_MAJOR_SHIFT;
  153. minor = (res.a0 & TRNG_MINOR_MASK) >> TRNG_MINOR_SHIFT;
  154. dev_dbg(dev, "Version %lu.%lu, UUID %08lx-%04lx-%04lx-%04lx-%04lx%08lx\n",
  155. major, minor, uuid_a0, uuid_a1 >> 16, uuid_a1 & GENMASK(16, 0),
  156. uuid_a2 >> 16, uuid_a2 & GENMASK(16, 0), uuid_a3);
  157. } else {
  158. dev_warn(dev, "Can't get TRNG UUID\n");
  159. }
  160. #endif
  161. return 0;
  162. }
  163. U_BOOT_DRIVER(smccc_trng) = {
  164. .name = DRIVER_NAME,
  165. .id = UCLASS_RNG,
  166. .ops = &smccc_trng_ops,
  167. .probe = smccc_trng_probe,
  168. .priv_auto = sizeof(struct smccc_trng_priv),
  169. };