blob: 80db43944afefe927cec6e09ed088bffb635f45d [file] [log] [blame]
Michael Ellermana4da0d52013-10-11 14:07:57 +11001/*
2 * Copyright 2013, Michael Ellerman, IBM Corporation.
3 *
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.
8 */
9
10#define pr_fmt(fmt) "powernv-rng: " fmt
11
12#include <linux/kernel.h>
13#include <linux/of.h>
Stephen Rothwell1400b422013-10-28 19:34:41 +110014#include <linux/of_address.h>
Michael Ellermana4da0d52013-10-11 14:07:57 +110015#include <linux/of_platform.h>
16#include <linux/slab.h>
Stephen Rothwell1400b422013-10-28 19:34:41 +110017#include <linux/smp.h>
Michael Ellermana4da0d52013-10-11 14:07:57 +110018#include <asm/archrandom.h>
19#include <asm/io.h>
Stephen Rothwell1400b422013-10-28 19:34:41 +110020#include <asm/prom.h>
Michael Ellermana4da0d52013-10-11 14:07:57 +110021#include <asm/machdep.h>
Michael Ellerman3eb906c2013-11-20 11:05:01 +110022#include <asm/smp.h>
Michael Ellermana4da0d52013-10-11 14:07:57 +110023
24
25struct powernv_rng {
26 void __iomem *regs;
27 unsigned long mask;
28};
29
30static DEFINE_PER_CPU(struct powernv_rng *, powernv_rng);
31
32
33static unsigned long rng_whiten(struct powernv_rng *rng, unsigned long val)
34{
35 unsigned long parity;
36
37 /* Calculate the parity of the value */
38 asm ("popcntd %0,%1" : "=r" (parity) : "r" (val));
39
40 /* xor our value with the previous mask */
41 val ^= rng->mask;
42
43 /* update the mask based on the parity of this value */
44 rng->mask = (rng->mask << 1) | (parity & 1);
45
46 return val;
47}
48
49int powernv_get_random_long(unsigned long *v)
50{
51 struct powernv_rng *rng;
52
53 rng = get_cpu_var(powernv_rng);
54
55 *v = rng_whiten(rng, in_be64(rng->regs));
56
57 put_cpu_var(rng);
58
59 return 1;
60}
61EXPORT_SYMBOL_GPL(powernv_get_random_long);
62
63static __init void rng_init_per_cpu(struct powernv_rng *rng,
64 struct device_node *dn)
65{
66 int chip_id, cpu;
67
68 chip_id = of_get_ibm_chip_id(dn);
69 if (chip_id == -1)
70 pr_warn("No ibm,chip-id found for %s.\n", dn->full_name);
71
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;
76 }
77 }
78}
79
80static __init int rng_create(struct device_node *dn)
81{
82 struct powernv_rng *rng;
83 unsigned long val;
84
85 rng = kzalloc(sizeof(*rng), GFP_KERNEL);
86 if (!rng)
87 return -ENOMEM;
88
89 rng->regs = of_iomap(dn, 0);
90 if (!rng->regs) {
91 kfree(rng);
92 return -ENXIO;
93 }
94
95 val = in_be64(rng->regs);
96 rng->mask = val;
97
98 rng_init_per_cpu(rng, dn);
99
100 pr_info_once("Registering arch random hook.\n");
101
102 ppc_md.get_random_long = powernv_get_random_long;
103
104 return 0;
105}
106
107static __init int rng_init(void)
108{
109 struct device_node *dn;
110 int rc;
111
112 for_each_compatible_node(dn, NULL, "ibm,power-rng") {
113 rc = rng_create(dn);
114 if (rc) {
115 pr_err("Failed creating rng for %s (%d).\n",
116 dn->full_name, rc);
117 continue;
118 }
119
120 /* Create devices for hwrng driver */
121 of_platform_device_create(dn, NULL, NULL);
122 }
123
124 return 0;
125}
Michael Ellermanb14726c2014-07-15 22:22:24 +1000126machine_subsys_initcall(powernv, rng_init);