1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Kernel interface for the s390 arch_random_* functions
5 * Copyright IBM Corp. 2017
7 * Author: Harald Freudenberger <freude@de.ibm.com>
11 #ifndef _ASM_S390_ARCHRANDOM_H
12 #define _ASM_S390_ARCHRANDOM_H
14 #ifdef CONFIG_ARCH_RANDOM
16 #include <linux/static_key.h>
17 #include <linux/atomic.h>
18 #include <asm/cpacf.h>
20 DECLARE_STATIC_KEY_FALSE(s390_arch_random_available);
21 extern atomic64_t s390_arch_random_counter;
23 static void s390_arch_random_generate(u8 *buf, unsigned int nbytes)
25 cpacf_trng(NULL, 0, buf, nbytes);
26 atomic64_add(nbytes, &s390_arch_random_counter);
29 static inline bool arch_has_random(void)
31 if (static_branch_likely(&s390_arch_random_available))
36 static inline bool arch_has_random_seed(void)
38 return arch_has_random();
41 static inline bool arch_get_random_long(unsigned long *v)
43 if (static_branch_likely(&s390_arch_random_available)) {
44 s390_arch_random_generate((u8 *)v, sizeof(*v));
50 static inline bool arch_get_random_int(unsigned int *v)
52 if (static_branch_likely(&s390_arch_random_available)) {
53 s390_arch_random_generate((u8 *)v, sizeof(*v));
59 static inline bool arch_get_random_seed_long(unsigned long *v)
61 return arch_get_random_long(v);
64 static inline bool arch_get_random_seed_int(unsigned int *v)
66 return arch_get_random_int(v);
69 #endif /* CONFIG_ARCH_RANDOM */
70 #endif /* _ASM_S390_ARCHRANDOM_H */