1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * This file is part of the Linux kernel. 4 * 5 * Copyright (c) 2011-2014, Intel Corporation 6 * Authors: Fenghua Yu <fenghua.yu@intel.com>, 7 * H. Peter Anvin <hpa@linux.intel.com> 8 */ 9 10 #ifndef ASM_X86_ARCHRANDOM_H 11 #define ASM_X86_ARCHRANDOM_H 12 13 #include <asm/processor.h> 14 #include <asm/cpufeature.h> 15 16 #define RDRAND_RETRY_LOOPS 10 17 18 /* Unconditional execution of RDRAND and RDSEED */ 19 rdrand_long(unsigned long * v)20static inline bool __must_check rdrand_long(unsigned long *v) 21 { 22 bool ok; 23 unsigned int retry = RDRAND_RETRY_LOOPS; 24 do { 25 asm volatile("rdrand %[out]" 26 : "=@ccc" (ok), [out] "=r" (*v)); 27 if (ok) 28 return true; 29 } while (--retry); 30 return false; 31 } 32 rdseed_long(unsigned long * v)33static inline bool __must_check rdseed_long(unsigned long *v) 34 { 35 bool ok; 36 asm volatile("rdseed %[out]" 37 : "=@ccc" (ok), [out] "=r" (*v)); 38 return ok; 39 } 40 41 /* 42 * These are the generic interfaces; they must not be declared if the 43 * stubs in <linux/random.h> are to be invoked. 44 */ 45 arch_get_random_longs(unsigned long * v,size_t max_longs)46static inline size_t __must_check arch_get_random_longs(unsigned long *v, size_t max_longs) 47 { 48 return max_longs && static_cpu_has(X86_FEATURE_RDRAND) && rdrand_long(v) ? 1 : 0; 49 } 50 arch_get_random_seed_longs(unsigned long * v,size_t max_longs)51static inline size_t __must_check arch_get_random_seed_longs(unsigned long *v, size_t max_longs) 52 { 53 return max_longs && static_cpu_has(X86_FEATURE_RDSEED) && rdseed_long(v) ? 1 : 0; 54 } 55 56 #ifndef CONFIG_UML 57 void x86_init_rdrand(struct cpuinfo_x86 *c); 58 #endif 59 60 #endif /* ASM_X86_ARCHRANDOM_H */ 61