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>
21 #include <asm/machdep.h>
30 static DEFINE_PER_CPU(struct powernv_rng
*, powernv_rng
);
33 static unsigned long rng_whiten(struct powernv_rng
*rng
, unsigned long val
)
37 /* Calculate the parity of the value */
38 asm ("popcntd %0,%1" : "=r" (parity
) : "r" (val
));
40 /* xor our value with the previous mask */
43 /* update the mask based on the parity of this value */
44 rng
->mask
= (rng
->mask
<< 1) | (parity
& 1);
49 int powernv_get_random_long(unsigned long *v
)
51 struct powernv_rng
*rng
;
53 rng
= get_cpu_var(powernv_rng
);
55 *v
= rng_whiten(rng
, in_be64(rng
->regs
));
61 EXPORT_SYMBOL_GPL(powernv_get_random_long
);
63 static __init
void rng_init_per_cpu(struct powernv_rng
*rng
,
64 struct device_node
*dn
)
68 chip_id
= of_get_ibm_chip_id(dn
);
70 pr_warn("No ibm,chip-id found for %s.\n", dn
->full_name
);
72 for_each_possible_cpu(cpu
) {
73 if (per_cpu(powernv_rng
, cpu
) == NULL
||
74 cpu_to_chip_id(cpu
) == chip_id
) {
75 per_cpu(powernv_rng
, cpu
) = rng
;
80 static __init
int rng_create(struct device_node
*dn
)
82 struct powernv_rng
*rng
;
85 rng
= kzalloc(sizeof(*rng
), GFP_KERNEL
);
89 rng
->regs
= of_iomap(dn
, 0);
95 val
= in_be64(rng
->regs
);
98 rng_init_per_cpu(rng
, dn
);
100 pr_info_once("Registering arch random hook.\n");
102 ppc_md
.get_random_long
= powernv_get_random_long
;
107 static __init
int rng_init(void)
109 struct device_node
*dn
;
112 for_each_compatible_node(dn
, NULL
, "ibm,power-rng") {
115 pr_err("Failed creating rng for %s (%d).\n",
120 /* Create devices for hwrng driver */
121 of_platform_device_create(dn
, NULL
, NULL
);
126 subsys_initcall(rng_init
);
This page took 0.049851 seconds and 5 git commands to generate.