]>
git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - arch/powerpc/platforms/powernv/rng.c
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 ("popcntd %0,%1" : "=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
);
67 *v
= rng_whiten(rng
, __raw_rm_readq(rng
->regs_real
));
72 int powernv_get_random_darn(unsigned long *v
)
76 /* Using DARN with L=1 - 64-bit conditioned random number */
77 asm volatile(PPC_DARN(%0, 1) : "=r"(val
));
87 static int initialise_darn(void)
92 if (!cpu_has_feature(CPU_FTR_ARCH_300
))
95 for (i
= 0; i
< 10; i
++) {
96 if (powernv_get_random_darn(&val
)) {
97 ppc_md
.get_random_seed
= powernv_get_random_darn
;
102 pr_warn("Unable to use DARN for get_random_seed()\n");
107 int powernv_get_random_long(unsigned long *v
)
109 struct powernv_rng
*rng
;
111 rng
= get_cpu_var(powernv_rng
);
113 *v
= rng_whiten(rng
, in_be64(rng
->regs
));
119 EXPORT_SYMBOL_GPL(powernv_get_random_long
);
121 static __init
void rng_init_per_cpu(struct powernv_rng
*rng
,
122 struct device_node
*dn
)
126 chip_id
= of_get_ibm_chip_id(dn
);
128 pr_warn("No ibm,chip-id found for %pOF.\n", dn
);
130 for_each_possible_cpu(cpu
) {
131 if (per_cpu(powernv_rng
, cpu
) == NULL
||
132 cpu_to_chip_id(cpu
) == chip_id
) {
133 per_cpu(powernv_rng
, cpu
) = rng
;
138 static __init
int rng_create(struct device_node
*dn
)
140 struct powernv_rng
*rng
;
144 rng
= kzalloc(sizeof(*rng
), GFP_KERNEL
);
148 if (of_address_to_resource(dn
, 0, &res
)) {
153 rng
->regs_real
= (void __iomem
*)res
.start
;
155 rng
->regs
= of_iomap(dn
, 0);
161 val
= in_be64(rng
->regs
);
164 rng_init_per_cpu(rng
, dn
);
166 pr_info_once("Registering arch random hook.\n");
168 ppc_md
.get_random_seed
= powernv_get_random_long
;
173 static __init
int rng_init(void)
175 struct device_node
*dn
;
178 for_each_compatible_node(dn
, NULL
, "ibm,power-rng") {
181 pr_err("Failed creating rng for %pOF (%d).\n",
186 /* Create devices for hwrng driver */
187 of_platform_device_create(dn
, NULL
, NULL
);
194 machine_subsys_initcall(powernv
, rng_init
);