18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * This file is part of the Linux kernel.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2011-2014, Intel Corporation
68c2ecf20Sopenharmony_ci * Authors: Fenghua Yu <fenghua.yu@intel.com>,
78c2ecf20Sopenharmony_ci *          H. Peter Anvin <hpa@linux.intel.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef ASM_X86_ARCHRANDOM_H
118c2ecf20Sopenharmony_ci#define ASM_X86_ARCHRANDOM_H
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <asm/processor.h>
148c2ecf20Sopenharmony_ci#include <asm/cpufeature.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#define RDRAND_RETRY_LOOPS	10
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/* Unconditional execution of RDRAND and RDSEED */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic inline bool __must_check rdrand_long(unsigned long *v)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	bool ok;
238c2ecf20Sopenharmony_ci	unsigned int retry = RDRAND_RETRY_LOOPS;
248c2ecf20Sopenharmony_ci	do {
258c2ecf20Sopenharmony_ci		asm volatile("rdrand %[out]"
268c2ecf20Sopenharmony_ci			     CC_SET(c)
278c2ecf20Sopenharmony_ci			     : CC_OUT(c) (ok), [out] "=r" (*v));
288c2ecf20Sopenharmony_ci		if (ok)
298c2ecf20Sopenharmony_ci			return true;
308c2ecf20Sopenharmony_ci	} while (--retry);
318c2ecf20Sopenharmony_ci	return false;
328c2ecf20Sopenharmony_ci}
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic inline bool __must_check rdrand_int(unsigned int *v)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	bool ok;
378c2ecf20Sopenharmony_ci	unsigned int retry = RDRAND_RETRY_LOOPS;
388c2ecf20Sopenharmony_ci	do {
398c2ecf20Sopenharmony_ci		asm volatile("rdrand %[out]"
408c2ecf20Sopenharmony_ci			     CC_SET(c)
418c2ecf20Sopenharmony_ci			     : CC_OUT(c) (ok), [out] "=r" (*v));
428c2ecf20Sopenharmony_ci		if (ok)
438c2ecf20Sopenharmony_ci			return true;
448c2ecf20Sopenharmony_ci	} while (--retry);
458c2ecf20Sopenharmony_ci	return false;
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic inline bool __must_check rdseed_long(unsigned long *v)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	bool ok;
518c2ecf20Sopenharmony_ci	asm volatile("rdseed %[out]"
528c2ecf20Sopenharmony_ci		     CC_SET(c)
538c2ecf20Sopenharmony_ci		     : CC_OUT(c) (ok), [out] "=r" (*v));
548c2ecf20Sopenharmony_ci	return ok;
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic inline bool __must_check rdseed_int(unsigned int *v)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	bool ok;
608c2ecf20Sopenharmony_ci	asm volatile("rdseed %[out]"
618c2ecf20Sopenharmony_ci		     CC_SET(c)
628c2ecf20Sopenharmony_ci		     : CC_OUT(c) (ok), [out] "=r" (*v));
638c2ecf20Sopenharmony_ci	return ok;
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/*
678c2ecf20Sopenharmony_ci * These are the generic interfaces; they must not be declared if the
688c2ecf20Sopenharmony_ci * stubs in <linux/random.h> are to be invoked,
698c2ecf20Sopenharmony_ci * i.e. CONFIG_ARCH_RANDOM is not defined.
708c2ecf20Sopenharmony_ci */
718c2ecf20Sopenharmony_ci#ifdef CONFIG_ARCH_RANDOM
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic inline bool __must_check arch_get_random_long(unsigned long *v)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	return static_cpu_has(X86_FEATURE_RDRAND) ? rdrand_long(v) : false;
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic inline bool __must_check arch_get_random_int(unsigned int *v)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	return static_cpu_has(X86_FEATURE_RDRAND) ? rdrand_int(v) : false;
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic inline bool __must_check arch_get_random_seed_long(unsigned long *v)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	return static_cpu_has(X86_FEATURE_RDSEED) ? rdseed_long(v) : false;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic inline bool __must_check arch_get_random_seed_int(unsigned int *v)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	return static_cpu_has(X86_FEATURE_RDSEED) ? rdseed_int(v) : false;
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ciextern void x86_init_rdrand(struct cpuinfo_x86 *c);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci#else  /* !CONFIG_ARCH_RANDOM */
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic inline void x86_init_rdrand(struct cpuinfo_x86 *c) { }
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#endif  /* !CONFIG_ARCH_RANDOM */
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci#endif /* ASM_X86_ARCHRANDOM_H */
102