Skip to content

Commit c25769f

Browse files
paulusmackmpe
authored andcommitted
powerpc/microwatt: Add support for hardware random number generator
Microwatt's hardware RNG is accessed using the DARN instruction. Signed-off-by: Paul Mackerras <[email protected]> Reviewed-by: Nicholas Piggin <[email protected]> Reviewed-by: Segher Boessenkool <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/YMwXPHlV/[email protected]
1 parent 48b545b commit c25769f

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

arch/powerpc/platforms/microwatt/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ config PPC_MICROWATT
77
select PPC_ICP_NATIVE
88
select PPC_NATIVE
99
select PPC_UDBG_16550
10+
select ARCH_RANDOM
1011
help
1112
This option enables support for FPGA-based Microwatt implementations.
1213

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
obj-y += setup.o
1+
obj-y += setup.o rng.o
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Derived from arch/powerpc/platforms/powernv/rng.c, which is:
4+
* Copyright 2013, Michael Ellerman, IBM Corporation.
5+
*/
6+
7+
#define pr_fmt(fmt) "microwatt-rng: " fmt
8+
9+
#include <linux/kernel.h>
10+
#include <linux/smp.h>
11+
#include <asm/archrandom.h>
12+
#include <asm/cputable.h>
13+
#include <asm/machdep.h>
14+
15+
#define DARN_ERR 0xFFFFFFFFFFFFFFFFul
16+
17+
int microwatt_get_random_darn(unsigned long *v)
18+
{
19+
unsigned long val;
20+
21+
/* Using DARN with L=1 - 64-bit conditioned random number */
22+
asm volatile(PPC_DARN(%0, 1) : "=r"(val));
23+
24+
if (val == DARN_ERR)
25+
return 0;
26+
27+
*v = val;
28+
29+
return 1;
30+
}
31+
32+
static __init int rng_init(void)
33+
{
34+
unsigned long val;
35+
int i;
36+
37+
for (i = 0; i < 10; i++) {
38+
if (microwatt_get_random_darn(&val)) {
39+
ppc_md.get_random_seed = microwatt_get_random_darn;
40+
return 0;
41+
}
42+
}
43+
44+
pr_warn("Unable to use DARN for get_random_seed()\n");
45+
46+
return -EIO;
47+
}
48+
machine_subsys_initcall(, rng_init);

0 commit comments

Comments
 (0)