2 * This file is part of the Linux kernel.
4 * Copyright (c) 2011-2014, Intel Corporation
5 * Authors: Fenghua Yu <fenghua.yu@intel.com>,
6 * H. Peter Anvin <hpa@linux.intel.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
23 #ifndef ASM_X86_ARCHRANDOM_H
24 #define ASM_X86_ARCHRANDOM_H
26 #include <asm/processor.h>
27 #include <asm/cpufeature.h>
29 #define RDRAND_RETRY_LOOPS 10
31 #define RDRAND_INT ".byte 0x0f,0xc7,0xf0"
32 #define RDSEED_INT ".byte 0x0f,0xc7,0xf8"
34 # define RDRAND_LONG ".byte 0x48,0x0f,0xc7,0xf0"
35 # define RDSEED_LONG ".byte 0x48,0x0f,0xc7,0xf8"
37 # define RDRAND_LONG RDRAND_INT
38 # define RDSEED_LONG RDSEED_INT
41 /* Unconditional execution of RDRAND and RDSEED */
43 static inline bool rdrand_long(unsigned long *v)
46 unsigned int retry = RDRAND_RETRY_LOOPS;
48 asm volatile(RDRAND_LONG
50 : CC_OUT(c) (ok), "=a" (*v));
57 static inline bool rdrand_int(unsigned int *v)
60 unsigned int retry = RDRAND_RETRY_LOOPS;
62 asm volatile(RDRAND_INT
64 : CC_OUT(c) (ok), "=a" (*v));
71 static inline bool rdseed_long(unsigned long *v)
74 asm volatile(RDSEED_LONG
76 : CC_OUT(c) (ok), "=a" (*v));
80 static inline bool rdseed_int(unsigned int *v)
83 asm volatile(RDSEED_INT
85 : CC_OUT(c) (ok), "=a" (*v));
90 * These are the generic interfaces; they must not be declared if the
91 * stubs in <linux/random.h> are to be invoked,
92 * i.e. CONFIG_ARCH_RANDOM is not defined.
94 #ifdef CONFIG_ARCH_RANDOM
96 static inline bool arch_get_random_long(unsigned long *v)
98 return static_cpu_has(X86_FEATURE_RDRAND) ? rdrand_long(v) : false;
101 static inline bool arch_get_random_int(unsigned int *v)
103 return static_cpu_has(X86_FEATURE_RDRAND) ? rdrand_int(v) : false;
106 static inline bool arch_get_random_seed_long(unsigned long *v)
108 return static_cpu_has(X86_FEATURE_RDSEED) ? rdseed_long(v) : false;
111 static inline bool arch_get_random_seed_int(unsigned int *v)
113 return static_cpu_has(X86_FEATURE_RDSEED) ? rdseed_int(v) : false;
116 extern void x86_init_rdrand(struct cpuinfo_x86 *c);
118 #else /* !CONFIG_ARCH_RANDOM */
120 static inline void x86_init_rdrand(struct cpuinfo_x86 *c) { }
122 #endif /* !CONFIG_ARCH_RANDOM */
124 #endif /* ASM_X86_ARCHRANDOM_H */