GNU Linux-libre 4.9.333-gnu1
[releases.git] / arch / powerpc / platforms / powernv / rng.c
1 /*
2  * Copyright 2013, Michael Ellerman, IBM Corporation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version
7  * 2 of the License, or (at your option) any later version.
8  */
9
10 #define pr_fmt(fmt)     "powernv-rng: " fmt
11
12 #include <linux/kernel.h>
13 #include <linux/of.h>
14 #include <linux/of_address.h>
15 #include <linux/of_platform.h>
16 #include <linux/slab.h>
17 #include <linux/smp.h>
18 #include <asm/archrandom.h>
19 #include <asm/cputable.h>
20 #include <asm/io.h>
21 #include <asm/prom.h>
22 #include <asm/machdep.h>
23 #include <asm/smp.h>
24 #include "powernv.h"
25
26 #define DARN_ERR 0xFFFFFFFFFFFFFFFFul
27
28 struct powernv_rng {
29         void __iomem *regs;
30         void __iomem *regs_real;
31         unsigned long mask;
32 };
33
34 static DEFINE_PER_CPU(struct powernv_rng *, powernv_rng);
35
36 int powernv_hwrng_present(void)
37 {
38         struct powernv_rng *rng;
39
40         rng = get_cpu_var(powernv_rng);
41         put_cpu_var(rng);
42         return rng != NULL;
43 }
44
45 static unsigned long rng_whiten(struct powernv_rng *rng, unsigned long val)
46 {
47         unsigned long parity;
48
49         /* Calculate the parity of the value */
50         asm (".machine push;   \
51               .machine power7; \
52               popcntd %0,%1;   \
53               .machine pop;"
54              : "=r" (parity) : "r" (val));
55
56         /* xor our value with the previous mask */
57         val ^= rng->mask;
58
59         /* update the mask based on the parity of this value */
60         rng->mask = (rng->mask << 1) | (parity & 1);
61
62         return val;
63 }
64
65 int powernv_get_random_real_mode(unsigned long *v)
66 {
67         struct powernv_rng *rng;
68
69         rng = raw_cpu_read(powernv_rng);
70         if (!rng)
71                 return 0;
72
73         *v = rng_whiten(rng, in_rm64(rng->regs_real));
74
75         return 1;
76 }
77
78 static int powernv_get_random_darn(unsigned long *v)
79 {
80         unsigned long val;
81
82         /* Using DARN with L=1 - 64-bit conditioned random number */
83         asm volatile(PPC_DARN(%0, 1) : "=r"(val));
84
85         if (val == DARN_ERR)
86                 return 0;
87
88         *v = val;
89
90         return 1;
91 }
92
93 static int __init initialise_darn(void)
94 {
95         unsigned long val;
96         int i;
97
98         if (!cpu_has_feature(CPU_FTR_ARCH_300))
99                 return -ENODEV;
100
101         for (i = 0; i < 10; i++) {
102                 if (powernv_get_random_darn(&val)) {
103                         ppc_md.get_random_seed = powernv_get_random_darn;
104                         return 0;
105                 }
106         }
107         return -EIO;
108 }
109
110 int powernv_get_random_long(unsigned long *v)
111 {
112         struct powernv_rng *rng;
113
114         rng = get_cpu_var(powernv_rng);
115
116         *v = rng_whiten(rng, in_be64(rng->regs));
117
118         put_cpu_var(rng);
119
120         return 1;
121 }
122 EXPORT_SYMBOL_GPL(powernv_get_random_long);
123
124 static __init void rng_init_per_cpu(struct powernv_rng *rng,
125                                     struct device_node *dn)
126 {
127         int chip_id, cpu;
128
129         chip_id = of_get_ibm_chip_id(dn);
130         if (chip_id == -1)
131                 pr_warn("No ibm,chip-id found for %pOF.\n", dn);
132
133         for_each_possible_cpu(cpu) {
134                 if (per_cpu(powernv_rng, cpu) == NULL ||
135                     cpu_to_chip_id(cpu) == chip_id) {
136                         per_cpu(powernv_rng, cpu) = rng;
137                 }
138         }
139 }
140
141 static __init int rng_create(struct device_node *dn)
142 {
143         struct powernv_rng *rng;
144         struct resource res;
145         unsigned long val;
146
147         rng = kzalloc(sizeof(*rng), GFP_KERNEL);
148         if (!rng)
149                 return -ENOMEM;
150
151         if (of_address_to_resource(dn, 0, &res)) {
152                 kfree(rng);
153                 return -ENXIO;
154         }
155
156         rng->regs_real = (void __iomem *)res.start;
157
158         rng->regs = of_iomap(dn, 0);
159         if (!rng->regs) {
160                 kfree(rng);
161                 return -ENXIO;
162         }
163
164         val = in_be64(rng->regs);
165         rng->mask = val;
166
167         rng_init_per_cpu(rng, dn);
168
169         ppc_md.get_random_seed = powernv_get_random_long;
170
171         return 0;
172 }
173
174 static int __init pnv_get_random_long_early(unsigned long *v)
175 {
176         struct device_node *dn;
177
178         if (!slab_is_available())
179                 return 0;
180
181         if (cmpxchg(&ppc_md.get_random_seed, pnv_get_random_long_early,
182                     NULL) != pnv_get_random_long_early)
183                 return 0;
184
185         for_each_compatible_node(dn, NULL, "ibm,power-rng") {
186                 if (rng_create(dn))
187                         continue;
188                 /* Create devices for hwrng driver */
189                 of_platform_device_create(dn, NULL, NULL);
190         }
191
192         if (!ppc_md.get_random_seed)
193                 return 0;
194         return ppc_md.get_random_seed(v);
195 }
196
197 void __init pnv_rng_init(void)
198 {
199         struct device_node *dn;
200
201         /* Prefer darn over the rest. */
202         if (!initialise_darn())
203                 return;
204
205         dn = of_find_compatible_node(NULL, NULL, "ibm,power-rng");
206         if (dn)
207                 ppc_md.get_random_seed = pnv_get_random_long_early;
208
209         of_node_put(dn);
210 }
211
212 static int __init pnv_rng_late_init(void)
213 {
214         unsigned long v;
215         /* In case it wasn't called during init for some other reason. */
216         if (ppc_md.get_random_seed == pnv_get_random_long_early)
217                 pnv_get_random_long_early(&v);
218         return 0;
219 }
220 machine_subsys_initcall(powernv, pnv_rng_late_init);