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 pnv_rng *, pnv_rng);
32 static unsigned long rng_whiten(struct pnv_rng *rng, unsigned long val)
36 /* Calculate the parity of the value */
37 asm (".machine push; \
41 : "=r" (parity) : "r" (val));
43 /* xor our value with the previous mask */
46 /* update the mask based on the parity of this value */
47 rng->mask = (rng->mask << 1) | (parity & 1);
52 static int pnv_get_random_darn(unsigned long *v)
56 /* Using DARN with L=1 - 64-bit conditioned random number */
57 asm volatile(PPC_DARN(%0, 1) : "=r"(val));
67 static int __init initialise_darn(void)
72 if (!cpu_has_feature(CPU_FTR_ARCH_300))
75 for (i = 0; i < 10; i++) {
76 if (pnv_get_random_darn(&val)) {
77 ppc_md.get_random_seed = pnv_get_random_darn;
84 int pnv_get_random_long(unsigned long *v)
88 if (mfmsr() & MSR_DR) {
89 rng = get_cpu_var(pnv_rng);
90 *v = rng_whiten(rng, in_be64(rng->regs));
93 rng = raw_cpu_read(pnv_rng);
94 *v = rng_whiten(rng, __raw_rm_readq(rng->regs_real));
98 EXPORT_SYMBOL_GPL(pnv_get_random_long);
100 static __init void rng_init_per_cpu(struct pnv_rng *rng,
101 struct device_node *dn)
105 chip_id = of_get_ibm_chip_id(dn);
107 pr_warn("No ibm,chip-id found for %pOF.\n", dn);
109 for_each_possible_cpu(cpu) {
110 if (per_cpu(pnv_rng, cpu) == NULL ||
111 cpu_to_chip_id(cpu) == chip_id) {
112 per_cpu(pnv_rng, cpu) = rng;
117 static __init int rng_create(struct device_node *dn)
123 rng = kzalloc(sizeof(*rng), GFP_KERNEL);
127 if (of_address_to_resource(dn, 0, &res)) {
132 rng->regs_real = (void __iomem *)res.start;
134 rng->regs = of_iomap(dn, 0);
140 val = in_be64(rng->regs);
143 rng_init_per_cpu(rng, dn);
145 ppc_md.get_random_seed = pnv_get_random_long;
150 static int __init pnv_get_random_long_early(unsigned long *v)
152 struct device_node *dn;
154 if (!slab_is_available())
157 if (cmpxchg(&ppc_md.get_random_seed, pnv_get_random_long_early,
158 NULL) != pnv_get_random_long_early)
161 for_each_compatible_node(dn, NULL, "ibm,power-rng")
164 if (!ppc_md.get_random_seed)
166 return ppc_md.get_random_seed(v);
169 void __init pnv_rng_init(void)
171 struct device_node *dn;
173 /* Prefer darn over the rest. */
174 if (!initialise_darn())
177 dn = of_find_compatible_node(NULL, NULL, "ibm,power-rng");
179 ppc_md.get_random_seed = pnv_get_random_long_early;
184 static int __init pnv_rng_late_init(void)
186 struct device_node *dn;
189 /* In case it wasn't called during init for some other reason. */
190 if (ppc_md.get_random_seed == pnv_get_random_long_early)
191 pnv_get_random_long_early(&v);
193 if (ppc_md.get_random_seed == pnv_get_random_long) {
194 for_each_compatible_node(dn, NULL, "ibm,power-rng")
195 of_platform_device_create(dn, NULL, NULL);
200 machine_subsys_initcall(powernv, pnv_rng_late_init);