xref: /openbmc/linux/arch/s390/include/asm/archrandom.h (revision 90a53e44)
1 /*
2  * Kernel interface for the s390 arch_random_* functions
3  *
4  * Copyright IBM Corp. 2017
5  *
6  * Author: Harald Freudenberger <freude@de.ibm.com>
7  *
8  */
9 
10 #ifndef _ASM_S390_ARCHRANDOM_H
11 #define _ASM_S390_ARCHRANDOM_H
12 
13 #ifdef CONFIG_ARCH_RANDOM
14 
15 #include <linux/static_key.h>
16 #include <linux/atomic.h>
17 #include <asm/cpacf.h>
18 
19 DECLARE_STATIC_KEY_FALSE(s390_arch_random_available);
20 extern atomic64_t s390_arch_random_counter;
21 
22 static void s390_arch_random_generate(u8 *buf, unsigned int nbytes)
23 {
24 	cpacf_trng(NULL, 0, buf, nbytes);
25 	atomic64_add(nbytes, &s390_arch_random_counter);
26 }
27 
28 static inline bool arch_has_random(void)
29 {
30 	if (static_branch_likely(&s390_arch_random_available))
31 		return true;
32 	return false;
33 }
34 
35 static inline bool arch_has_random_seed(void)
36 {
37 	return arch_has_random();
38 }
39 
40 static inline bool arch_get_random_long(unsigned long *v)
41 {
42 	if (static_branch_likely(&s390_arch_random_available)) {
43 		s390_arch_random_generate((u8 *)v, sizeof(*v));
44 		return true;
45 	}
46 	return false;
47 }
48 
49 static inline bool arch_get_random_int(unsigned int *v)
50 {
51 	if (static_branch_likely(&s390_arch_random_available)) {
52 		s390_arch_random_generate((u8 *)v, sizeof(*v));
53 		return true;
54 	}
55 	return false;
56 }
57 
58 static inline bool arch_get_random_seed_long(unsigned long *v)
59 {
60 	return arch_get_random_long(v);
61 }
62 
63 static inline bool arch_get_random_seed_int(unsigned int *v)
64 {
65 	return arch_get_random_int(v);
66 }
67 
68 #endif /* CONFIG_ARCH_RANDOM */
69 #endif /* _ASM_S390_ARCHRANDOM_H */
70