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