2 * Copyright 2013, Michael Ellerman, IBM Corporation.
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.
10 #define pr_fmt(fmt) "powernv-rng: " fmt
12 #include <linux/kernel.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>
22 #include <asm/machdep.h>
26 #define DARN_ERR 0xFFFFFFFFFFFFFFFFul
30 void __iomem *regs_real;
34 static DEFINE_PER_CPU(struct powernv_rng *, powernv_rng);
36 int powernv_hwrng_present(void)
38 struct powernv_rng *rng;
40 rng = get_cpu_var(powernv_rng);
45 static unsigned long rng_whiten(struct powernv_rng *rng, unsigned long val)
49 /* Calculate the parity of the value */
50 asm (".machine push; \
54 : "=r" (parity) : "r" (val));
56 /* xor our value with the previous mask */
59 /* update the mask based on the parity of this value */
60 rng->mask = (rng->mask << 1) | (parity & 1);
65 int powernv_get_random_real_mode(unsigned long *v)
67 struct powernv_rng *rng;
69 rng = raw_cpu_read(powernv_rng);
73 *v = rng_whiten(rng, __raw_rm_readq(rng->regs_real));
78 int powernv_get_random_darn(unsigned long *v)
82 /* Using DARN with L=1 - 64-bit conditioned random number */
83 asm volatile(PPC_DARN(%0, 1) : "=r"(val));
93 static int initialise_darn(void)
98 if (!cpu_has_feature(CPU_FTR_ARCH_300))
101 for (i = 0; i < 10; i++) {
102 if (powernv_get_random_darn(&val)) {
103 ppc_md.get_random_seed = powernv_get_random_darn;
110 int powernv_get_random_long(unsigned long *v)
112 struct powernv_rng *rng;
114 rng = get_cpu_var(powernv_rng);
116 *v = rng_whiten(rng, in_be64(rng->regs));
122 EXPORT_SYMBOL_GPL(powernv_get_random_long);
124 static __init void rng_init_per_cpu(struct powernv_rng *rng,
125 struct device_node *dn)
129 chip_id = of_get_ibm_chip_id(dn);
131 pr_warn("No ibm,chip-id found for %pOF.\n", dn);
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;
141 static __init int rng_create(struct device_node *dn)
143 struct powernv_rng *rng;
147 rng = kzalloc(sizeof(*rng), GFP_KERNEL);
151 if (of_address_to_resource(dn, 0, &res)) {
156 rng->regs_real = (void __iomem *)res.start;
158 rng->regs = of_iomap(dn, 0);
164 val = in_be64(rng->regs);
167 rng_init_per_cpu(rng, dn);
169 ppc_md.get_random_seed = powernv_get_random_long;
174 static int __init pnv_get_random_long_early(unsigned long *v)
176 struct device_node *dn;
178 if (!slab_is_available())
181 if (cmpxchg(&ppc_md.get_random_seed, pnv_get_random_long_early,
182 NULL) != pnv_get_random_long_early)
185 for_each_compatible_node(dn, NULL, "ibm,power-rng")
188 if (!ppc_md.get_random_seed)
190 return ppc_md.get_random_seed(v);
193 void __init pnv_rng_init(void)
195 struct device_node *dn;
197 /* Prefer darn over the rest. */
198 if (!initialise_darn())
201 dn = of_find_compatible_node(NULL, NULL, "ibm,power-rng");
203 ppc_md.get_random_seed = pnv_get_random_long_early;
208 static int __init pnv_rng_late_init(void)
210 struct device_node *dn;
213 /* In case it wasn't called during init for some other reason. */
214 if (ppc_md.get_random_seed == pnv_get_random_long_early)
215 pnv_get_random_long_early(&v);
217 if (ppc_md.get_random_seed == powernv_get_random_long) {
218 for_each_compatible_node(dn, NULL, "ibm,power-rng")
219 of_platform_device_create(dn, NULL, NULL);
224 machine_subsys_initcall(powernv, pnv_rng_late_init);