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>
25 #define DARN_ERR 0xFFFFFFFFFFFFFFFFul
29 void __iomem *regs_real;
33 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);
71 *v = rng_whiten(rng, __raw_rm_readq(rng->regs_real));
76 int powernv_get_random_darn(unsigned long *v)
80 /* Using DARN with L=1 - 64-bit conditioned random number */
81 asm volatile(PPC_DARN(%0, 1) : "=r"(val));
91 static int initialise_darn(void)
96 if (!cpu_has_feature(CPU_FTR_ARCH_300))
99 for (i = 0; i < 10; i++) {
100 if (powernv_get_random_darn(&val)) {
101 ppc_md.get_random_seed = powernv_get_random_darn;
106 pr_warn("Unable to use DARN for get_random_seed()\n");
111 int powernv_get_random_long(unsigned long *v)
113 struct powernv_rng *rng;
115 rng = get_cpu_var(powernv_rng);
117 *v = rng_whiten(rng, in_be64(rng->regs));
123 EXPORT_SYMBOL_GPL(powernv_get_random_long);
125 static __init void rng_init_per_cpu(struct powernv_rng *rng,
126 struct device_node *dn)
130 chip_id = of_get_ibm_chip_id(dn);
132 pr_warn("No ibm,chip-id found for %pOF.\n", dn);
134 for_each_possible_cpu(cpu) {
135 if (per_cpu(powernv_rng, cpu) == NULL ||
136 cpu_to_chip_id(cpu) == chip_id) {
137 per_cpu(powernv_rng, cpu) = rng;
142 static __init int rng_create(struct device_node *dn)
144 struct powernv_rng *rng;
148 rng = kzalloc(sizeof(*rng), GFP_KERNEL);
152 if (of_address_to_resource(dn, 0, &res)) {
157 rng->regs_real = (void __iomem *)res.start;
159 rng->regs = of_iomap(dn, 0);
165 val = in_be64(rng->regs);
168 rng_init_per_cpu(rng, dn);
170 pr_info_once("Registering arch random hook.\n");
172 ppc_md.get_random_seed = powernv_get_random_long;
177 static __init int rng_init(void)
179 struct device_node *dn;
182 for_each_compatible_node(dn, NULL, "ibm,power-rng") {
185 pr_err("Failed creating rng for %pOF (%d).\n",
190 /* Create devices for hwrng driver */
191 of_platform_device_create(dn, NULL, NULL);
198 machine_subsys_initcall(powernv, rng_init);