1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_ARCHRANDOM_H
3 #define _ASM_ARCHRANDOM_H
5 #ifdef CONFIG_ARCH_RANDOM
8 #include <linux/kernel.h>
9 #include <asm/cpufeature.h>
11 static inline bool __arm64_rndr(unsigned long *v)
16 * Reads of RNDR set PSTATE.NZCV to 0b0000 on success,
17 * and set PSTATE.NZCV to 0b0100 otherwise.
20 __mrs_s("%0", SYS_RNDR_EL0) "\n"
22 : "=r" (*v), "=r" (ok)
29 static inline bool __must_check arch_get_random_long(unsigned long *v)
34 static inline bool __must_check arch_get_random_int(unsigned int *v)
39 static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
42 * Only support the generic interface after we have detected
43 * the system wide capability, avoiding complexity with the
44 * cpufeature code and with potential scheduling between CPUs
45 * with and without the feature.
47 if (!cpus_have_const_cap(ARM64_HAS_RNG))
50 return __arm64_rndr(v);
54 static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
57 bool ok = arch_get_random_seed_long(&val);
63 static inline bool __init __early_cpu_has_rndr(void)
65 /* Open code as we run prior to the first call to cpufeature. */
66 unsigned long ftr = read_sysreg_s(SYS_ID_AA64ISAR0_EL1);
67 return (ftr >> ID_AA64ISAR0_RNDR_SHIFT) & 0xf;
70 static inline bool __init __must_check
71 arch_get_random_seed_long_early(unsigned long *v)
73 WARN_ON(system_state != SYSTEM_BOOTING);
75 if (!__early_cpu_has_rndr())
78 return __arm64_rndr(v);
80 #define arch_get_random_seed_long_early arch_get_random_seed_long_early
82 #endif /* CONFIG_ARCH_RANDOM */
83 #endif /* _ASM_ARCHRANDOM_H */