Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'for-next/rng' into for-next/core

Add support for the TRNG firmware call introduced by Arm spec DEN0098.

* for-next/rng:
arm64: Add support for SMCCC TRNG entropy source
firmware: smccc: Introduce SMCCC TRNG framework
firmware: smccc: Add SMCCC TRNG function call IDs

+119 -10
+10
arch/arm/include/asm/archrandom.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #ifndef _ASM_ARCHRANDOM_H 3 + #define _ASM_ARCHRANDOM_H 4 + 5 + static inline bool __init smccc_probe_trng(void) 6 + { 7 + return false; 8 + } 9 + 10 + #endif /* _ASM_ARCHRANDOM_H */
+72 -10
arch/arm64/include/asm/archrandom.h
··· 4 4 5 5 #ifdef CONFIG_ARCH_RANDOM 6 6 7 + #include <linux/arm-smccc.h> 7 8 #include <linux/bug.h> 8 9 #include <linux/kernel.h> 9 10 #include <asm/cpufeature.h> 11 + 12 + #define ARM_SMCCC_TRNG_MIN_VERSION 0x10000UL 13 + 14 + extern bool smccc_trng_available; 15 + 16 + static inline bool __init smccc_probe_trng(void) 17 + { 18 + struct arm_smccc_res res; 19 + 20 + arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_VERSION, &res); 21 + if ((s32)res.a0 < 0) 22 + return false; 23 + 24 + return res.a0 >= ARM_SMCCC_TRNG_MIN_VERSION; 25 + } 10 26 11 27 static inline bool __arm64_rndr(unsigned long *v) 12 28 { ··· 54 38 55 39 static inline bool __must_check arch_get_random_seed_long(unsigned long *v) 56 40 { 41 + struct arm_smccc_res res; 42 + 43 + /* 44 + * We prefer the SMCCC call, since its semantics (return actual 45 + * hardware backed entropy) is closer to the idea behind this 46 + * function here than what even the RNDRSS register provides 47 + * (the output of a pseudo RNG freshly seeded by a TRNG). 48 + */ 49 + if (smccc_trng_available) { 50 + arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_RND64, 64, &res); 51 + if ((int)res.a0 >= 0) { 52 + *v = res.a3; 53 + return true; 54 + } 55 + } 56 + 57 57 /* 58 58 * Only support the generic interface after we have detected 59 59 * the system wide capability, avoiding complexity with the 60 60 * cpufeature code and with potential scheduling between CPUs 61 61 * with and without the feature. 62 62 */ 63 - if (!cpus_have_const_cap(ARM64_HAS_RNG)) 64 - return false; 63 + if (cpus_have_const_cap(ARM64_HAS_RNG) && __arm64_rndr(v)) 64 + return true; 65 65 66 - return __arm64_rndr(v); 66 + return false; 67 67 } 68 - 69 68 70 69 static inline bool __must_check arch_get_random_seed_int(unsigned int *v) 71 70 { 71 + struct arm_smccc_res res; 72 72 unsigned long val; 73 - bool ok = arch_get_random_seed_long(&val); 74 73 75 - *v = val; 76 - return ok; 74 + if (smccc_trng_available) { 75 + arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_RND64, 32, &res); 76 + if ((int)res.a0 >= 0) { 77 + *v = res.a3 & GENMASK(31, 0); 78 + return true; 79 + } 80 + } 81 + 82 + if (cpus_have_const_cap(ARM64_HAS_RNG)) { 83 + if (__arm64_rndr(&val)) { 84 + *v = val; 85 + return true; 86 + } 87 + } 88 + 89 + return false; 77 90 } 78 91 79 92 static inline bool __init __early_cpu_has_rndr(void) ··· 117 72 { 118 73 WARN_ON(system_state != SYSTEM_BOOTING); 119 74 120 - if (!__early_cpu_has_rndr()) 121 - return false; 75 + if (smccc_trng_available) { 76 + struct arm_smccc_res res; 122 77 123 - return __arm64_rndr(v); 78 + arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_RND64, 64, &res); 79 + if ((int)res.a0 >= 0) { 80 + *v = res.a3; 81 + return true; 82 + } 83 + } 84 + 85 + if (__early_cpu_has_rndr() && __arm64_rndr(v)) 86 + return true; 87 + 88 + return false; 124 89 } 125 90 #define arch_get_random_seed_long_early arch_get_random_seed_long_early 91 + 92 + #else /* !CONFIG_ARCH_RANDOM */ 93 + 94 + static inline bool __init smccc_probe_trng(void) 95 + { 96 + return false; 97 + } 126 98 127 99 #endif /* CONFIG_ARCH_RANDOM */ 128 100 #endif /* _ASM_ARCHRANDOM_H */
+6
drivers/firmware/smccc/smccc.c
··· 5 5 6 6 #define pr_fmt(fmt) "smccc: " fmt 7 7 8 + #include <linux/cache.h> 8 9 #include <linux/init.h> 9 10 #include <linux/arm-smccc.h> 11 + #include <asm/archrandom.h> 10 12 11 13 static u32 smccc_version = ARM_SMCCC_VERSION_1_0; 12 14 static enum arm_smccc_conduit smccc_conduit = SMCCC_CONDUIT_NONE; 15 + 16 + bool __ro_after_init smccc_trng_available = false; 13 17 14 18 void __init arm_smccc_version_init(u32 version, enum arm_smccc_conduit conduit) 15 19 { 16 20 smccc_version = version; 17 21 smccc_conduit = conduit; 22 + 23 + smccc_trng_available = smccc_probe_trng(); 18 24 } 19 25 20 26 enum arm_smccc_conduit arm_smccc_1_1_get_conduit(void)
+31
include/linux/arm-smccc.h
··· 102 102 ARM_SMCCC_OWNER_STANDARD_HYP, \ 103 103 0x21) 104 104 105 + /* TRNG entropy source calls (defined by ARM DEN0098) */ 106 + #define ARM_SMCCC_TRNG_VERSION \ 107 + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ 108 + ARM_SMCCC_SMC_32, \ 109 + ARM_SMCCC_OWNER_STANDARD, \ 110 + 0x50) 111 + 112 + #define ARM_SMCCC_TRNG_FEATURES \ 113 + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ 114 + ARM_SMCCC_SMC_32, \ 115 + ARM_SMCCC_OWNER_STANDARD, \ 116 + 0x51) 117 + 118 + #define ARM_SMCCC_TRNG_GET_UUID \ 119 + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ 120 + ARM_SMCCC_SMC_32, \ 121 + ARM_SMCCC_OWNER_STANDARD, \ 122 + 0x52) 123 + 124 + #define ARM_SMCCC_TRNG_RND32 \ 125 + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ 126 + ARM_SMCCC_SMC_32, \ 127 + ARM_SMCCC_OWNER_STANDARD, \ 128 + 0x53) 129 + 130 + #define ARM_SMCCC_TRNG_RND64 \ 131 + ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ 132 + ARM_SMCCC_SMC_64, \ 133 + ARM_SMCCC_OWNER_STANDARD, \ 134 + 0x53) 135 + 105 136 /* 106 137 * Return codes defined in ARM DEN 0070A 107 138 * ARM DEN 0070A is now merged/consolidated into ARM DEN 0028 C