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 rdseed_long(unsigned long *v)
37 asm volatile("rdseed %[out]"
39 : CC_OUT(c) (ok), [out] "=r" (*v));
44 * These are the generic interfaces; they must not be declared if the
45 * stubs in <linux/random.h> are to be invoked.
48 static inline size_t __must_check arch_get_random_longs(unsigned long *v, size_t max_longs)
50 return max_longs && static_cpu_has(X86_FEATURE_RDRAND) && rdrand_long(v) ? 1 : 0;
53 static inline size_t __must_check arch_get_random_seed_longs(unsigned long *v, size_t max_longs)
55 return max_longs && static_cpu_has(X86_FEATURE_RDSEED) && rdseed_long(v) ? 1 : 0;
59 void x86_init_rdrand(struct cpuinfo_x86 *c);
62 #endif /* ASM_X86_ARCHRANDOM_H */