Linux 5.7.6
[linux/fpc-iii.git] / arch / x86 / include / asm / archrandom.h
blob7a4bb1bd4bdb84356d18e814b704b37f62fbf980
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
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>
8 */
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"
20 #ifdef CONFIG_X86_64
21 # define RDRAND_LONG ".byte 0x48,0x0f,0xc7,0xf0"
22 # define RDSEED_LONG ".byte 0x48,0x0f,0xc7,0xf8"
23 #else
24 # define RDRAND_LONG RDRAND_INT
25 # define RDSEED_LONG RDSEED_INT
26 #endif
28 /* Unconditional execution of RDRAND and RDSEED */
30 static inline bool __must_check rdrand_long(unsigned long *v)
32 bool ok;
33 unsigned int retry = RDRAND_RETRY_LOOPS;
34 do {
35 asm volatile(RDRAND_LONG
36 CC_SET(c)
37 : CC_OUT(c) (ok), "=a" (*v));
38 if (ok)
39 return true;
40 } while (--retry);
41 return false;
44 static inline bool __must_check rdrand_int(unsigned int *v)
46 bool ok;
47 unsigned int retry = RDRAND_RETRY_LOOPS;
48 do {
49 asm volatile(RDRAND_INT
50 CC_SET(c)
51 : CC_OUT(c) (ok), "=a" (*v));
52 if (ok)
53 return true;
54 } while (--retry);
55 return false;
58 static inline bool __must_check rdseed_long(unsigned long *v)
60 bool ok;
61 asm volatile(RDSEED_LONG
62 CC_SET(c)
63 : CC_OUT(c) (ok), "=a" (*v));
64 return ok;
67 static inline bool __must_check rdseed_int(unsigned int *v)
69 bool ok;
70 asm volatile(RDSEED_INT
71 CC_SET(c)
72 : CC_OUT(c) (ok), "=a" (*v));
73 return ok;
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 */