1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * This file is part of the Linux kernel.
5 * Copyright (c) 2011-2014, Intel Corporation
10 #ifndef ASM_X86_ARCHRANDOM_H
11 #define ASM_X86_ARCHRANDOM_H
13 #include <asm/processor.h>
14 #include <asm/cpufeature.h>
16 #define RDRAND_RETRY_LOOPS 10
18 /* Unconditional execution of RDRAND and RDSEED */
20 static inline bool __must_check rdrand_long(unsigned long *v)
23 unsigned int retry = RDRAND_RETRY_LOOPS;
25 asm volatile("rdrand %[out]"
27 : CC_OUT(c) (ok), [out] "=r" (*v));
34 static inline bool __must_check rdrand_int(unsigned int *v)
37 unsigned int retry = RDRAND_RETRY_LOOPS;
39 asm volatile("rdrand %[out]"
41 : CC_OUT(c) (ok), [out] "=r" (*v));
48 static inline bool __must_check rdseed_long(unsigned long *v)
51 asm volatile("rdseed %[out]"
53 : CC_OUT(c) (ok), [out] "=r" (*v));
57 static inline bool __must_check rdseed_int(unsigned int *v)
60 asm volatile("rdseed %[out]"
62 : CC_OUT(c) (ok), [out] "=r" (*v));
67 * These are the generic interfaces; they must not be declared if the
68 * stubs in <linux/random.h> are to be invoked,
69 * i.e. CONFIG_ARCH_RANDOM is not defined.
71 #ifdef CONFIG_ARCH_RANDOM
73 static inline bool __must_check arch_get_random_long(unsigned long *v)
75 return static_cpu_has(X86_FEATURE_RDRAND) ? rdrand_long(v) : false;
78 static inline bool __must_check arch_get_random_int(unsigned int *v)
80 return static_cpu_has(X86_FEATURE_RDRAND) ? rdrand_int(v) : false;
83 static inline bool __must_check arch_get_random_seed_long(unsigned long *v)
85 return static_cpu_has(X86_FEATURE_RDSEED) ? rdseed_long(v) : false;
88 static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
90 return static_cpu_has(X86_FEATURE_RDSEED) ? rdseed_int(v) : false;
93 extern void x86_init_rdrand(struct cpuinfo_x86 *c);
95 #else /* !CONFIG_ARCH_RANDOM */
97 static inline void x86_init_rdrand(struct cpuinfo_x86 *c) { }
99 #endif /* !CONFIG_ARCH_RANDOM */
101 #endif /* ASM_X86_ARCHRANDOM_H */