1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * This file is part of the Linux kernel.
5 * Copyright (c) 2011-2014, Intel Corporation
6 * Authors: Fenghua Yu <fenghua.yu@intel.com>,
7 * H. Peter Anvin <hpa@linux.intel.com>
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 #define RDRAND_INT ".byte 0x0f,0xc7,0xf0"
19 #define RDSEED_INT ".byte 0x0f,0xc7,0xf8"
21 # define RDRAND_LONG ".byte 0x48,0x0f,0xc7,0xf0"
22 # define RDSEED_LONG ".byte 0x48,0x0f,0xc7,0xf8"
24 # define RDRAND_LONG RDRAND_INT
25 # define RDSEED_LONG RDSEED_INT
28 /* Unconditional execution of RDRAND and RDSEED */
30 static inline bool __must_check
rdrand_long(unsigned long *v
)
33 unsigned int retry
= RDRAND_RETRY_LOOPS
;
35 asm volatile(RDRAND_LONG
37 : CC_OUT(c
) (ok
), "=a" (*v
));
44 static inline bool __must_check
rdrand_int(unsigned int *v
)
47 unsigned int retry
= RDRAND_RETRY_LOOPS
;
49 asm volatile(RDRAND_INT
51 : CC_OUT(c
) (ok
), "=a" (*v
));
58 static inline bool __must_check
rdseed_long(unsigned long *v
)
61 asm volatile(RDSEED_LONG
63 : CC_OUT(c
) (ok
), "=a" (*v
));
67 static inline bool __must_check
rdseed_int(unsigned int *v
)
70 asm volatile(RDSEED_INT
72 : CC_OUT(c
) (ok
), "=a" (*v
));
77 * These are the generic interfaces; they must not be declared if the
78 * stubs in <linux/random.h> are to be invoked,
79 * i.e. CONFIG_ARCH_RANDOM is not defined.
81 #ifdef CONFIG_ARCH_RANDOM
83 static inline bool __must_check
arch_get_random_long(unsigned long *v
)
85 return static_cpu_has(X86_FEATURE_RDRAND
) ? rdrand_long(v
) : false;
88 static inline bool __must_check
arch_get_random_int(unsigned int *v
)
90 return static_cpu_has(X86_FEATURE_RDRAND
) ? rdrand_int(v
) : false;
93 static inline bool __must_check
arch_get_random_seed_long(unsigned long *v
)
95 return static_cpu_has(X86_FEATURE_RDSEED
) ? rdseed_long(v
) : false;
98 static inline bool __must_check
arch_get_random_seed_int(unsigned int *v
)
100 return static_cpu_has(X86_FEATURE_RDSEED
) ? rdseed_int(v
) : false;
103 extern void x86_init_rdrand(struct cpuinfo_x86
*c
);
105 #else /* !CONFIG_ARCH_RANDOM */
107 static inline void x86_init_rdrand(struct cpuinfo_x86
*c
) { }
109 #endif /* !CONFIG_ARCH_RANDOM */
111 #endif /* ASM_X86_ARCHRANDOM_H */