xref: /openbmc/linux/arch/s390/include/asm/archrandom.h (revision f44fa887)
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 	return false;
31 }
32 
33 static inline bool arch_has_random_seed(void)
34 {
35 	if (static_branch_likely(&s390_arch_random_available))
36 		return true;
37 	return false;
38 }
39 
40 static inline bool arch_get_random_long(unsigned long *v)
41 {
42 	return false;
43 }
44 
45 static inline bool arch_get_random_int(unsigned int *v)
46 {
47 	return false;
48 }
49 
50 static inline bool arch_get_random_seed_long(unsigned long *v)
51 {
52 	if (static_branch_likely(&s390_arch_random_available)) {
53 		s390_arch_random_generate((u8 *)v, sizeof(*v));
54 		return true;
55 	}
56 	return false;
57 }
58 
59 static inline bool arch_get_random_seed_int(unsigned int *v)
60 {
61 	if (static_branch_likely(&s390_arch_random_available)) {
62 		s390_arch_random_generate((u8 *)v, sizeof(*v));
63 		return true;
64 	}
65 	return false;
66 }
67 
68 #endif /* CONFIG_ARCH_RANDOM */
69 #endif /* _ASM_S390_ARCHRANDOM_H */
70