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