1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2013, Michael Ellerman, IBM Corporation.
6 #define pr_fmt(fmt) "powernv-rng: " fmt
8 #include <linux/kernel.h>
10 #include <linux/of_address.h>
11 #include <linux/of_platform.h>
12 #include <linux/slab.h>
13 #include <linux/smp.h>
14 #include <asm/archrandom.h>
15 #include <asm/cputable.h>
18 #include <asm/machdep.h>
22 #define DARN_ERR 0xFFFFFFFFFFFFFFFFul
26 void __iomem *regs_real;
30 static DEFINE_PER_CPU(struct powernv_rng *, powernv_rng);
32 int powernv_hwrng_present(void)
34 struct powernv_rng *rng;
36 rng = get_cpu_var(powernv_rng);
41 static unsigned long rng_whiten(struct powernv_rng *rng, unsigned long val)
45 /* Calculate the parity of the value */
46 asm (".machine push; \
50 : "=r" (parity) : "r" (val));
52 /* xor our value with the previous mask */
55 /* update the mask based on the parity of this value */
56 rng->mask = (rng->mask << 1) | (parity & 1);
61 int powernv_get_random_real_mode(unsigned long *v)
63 struct powernv_rng *rng;
65 rng = raw_cpu_read(powernv_rng);
69 *v = rng_whiten(rng, __raw_rm_readq(rng->regs_real));
74 static int powernv_get_random_darn(unsigned long *v)
78 /* Using DARN with L=1 - 64-bit conditioned random number */
79 asm volatile(PPC_DARN(%0, 1) : "=r"(val));
89 static int initialise_darn(void)
94 if (!cpu_has_feature(CPU_FTR_ARCH_300))
97 for (i = 0; i < 10; i++) {
98 if (powernv_get_random_darn(&val)) {
99 ppc_md.get_random_seed = powernv_get_random_darn;
106 int powernv_get_random_long(unsigned long *v)
108 struct powernv_rng *rng;
110 rng = get_cpu_var(powernv_rng);
112 *v = rng_whiten(rng, in_be64(rng->regs));
118 EXPORT_SYMBOL_GPL(powernv_get_random_long);
120 static __init void rng_init_per_cpu(struct powernv_rng *rng,
121 struct device_node *dn)
125 chip_id = of_get_ibm_chip_id(dn);
127 pr_warn("No ibm,chip-id found for %pOF.\n", dn);
129 for_each_possible_cpu(cpu) {
130 if (per_cpu(powernv_rng, cpu) == NULL ||
131 cpu_to_chip_id(cpu) == chip_id) {
132 per_cpu(powernv_rng, cpu) = rng;
137 static __init int rng_create(struct device_node *dn)
139 struct powernv_rng *rng;
143 rng = kzalloc(sizeof(*rng), GFP_KERNEL);
147 if (of_address_to_resource(dn, 0, &res)) {
152 rng->regs_real = (void __iomem *)res.start;
154 rng->regs = of_iomap(dn, 0);
160 val = in_be64(rng->regs);
163 rng_init_per_cpu(rng, dn);
165 ppc_md.get_random_seed = powernv_get_random_long;
170 static int __init pnv_get_random_long_early(unsigned long *v)
172 struct device_node *dn;
174 if (!slab_is_available())
177 if (cmpxchg(&ppc_md.get_random_seed, pnv_get_random_long_early,
178 NULL) != pnv_get_random_long_early)
181 for_each_compatible_node(dn, NULL, "ibm,power-rng")
184 if (!ppc_md.get_random_seed)
186 return ppc_md.get_random_seed(v);
189 void __init pnv_rng_init(void)
191 struct device_node *dn;
193 /* Prefer darn over the rest. */
194 if (!initialise_darn())
197 dn = of_find_compatible_node(NULL, NULL, "ibm,power-rng");
199 ppc_md.get_random_seed = pnv_get_random_long_early;
204 static int __init pnv_rng_late_init(void)
206 struct device_node *dn;
209 /* In case it wasn't called during init for some other reason. */
210 if (ppc_md.get_random_seed == pnv_get_random_long_early)
211 pnv_get_random_long_early(&v);
213 if (ppc_md.get_random_seed == powernv_get_random_long) {
214 for_each_compatible_node(dn, NULL, "ibm,power-rng")
215 of_platform_device_create(dn, NULL, NULL);
220 machine_subsys_initcall(powernv, pnv_rng_late_init);