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