1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright 2013, Michael Ellerman, IBM Corporation.
4  */
5 
6 #define pr_fmt(fmt)	"powernv-rng: " fmt
7 
8 #include <linux/kernel.h>
9 #include <linux/of.h>
10 #include <linux/of_address.h>
11 #include <linux/of_platform.h>
12 #include <linux/slab.h>
13 #include <linux/smp.h>
14 #include <asm/archrandom.h>
15 #include <asm/cputable.h>
16 #include <asm/io.h>
17 #include <asm/prom.h>
18 #include <asm/machdep.h>
19 #include <asm/smp.h>
20 
21 #define DARN_ERR 0xFFFFFFFFFFFFFFFFul
22 
23 struct powernv_rng {
24 	void __iomem *regs;
25 	void __iomem *regs_real;
26 	unsigned long mask;
27 };
28 
29 static DEFINE_PER_CPU(struct powernv_rng *, powernv_rng);
30 
31 
32 int powernv_hwrng_present(void)
33 {
34 	struct powernv_rng *rng;
35 
36 	rng = get_cpu_var(powernv_rng);
37 	put_cpu_var(rng);
38 	return rng != NULL;
39 }
40 
41 static unsigned long rng_whiten(struct powernv_rng *rng, unsigned long val)
42 {
43 	unsigned long parity;
44 
45 	/* Calculate the parity of the value */
46 	asm (".machine push;   \
47 	      .machine power7; \
48 	      popcntd %0,%1;   \
49 	      .machine pop;"
50 	     : "=r" (parity) : "r" (val));
51 
52 	/* xor our value with the previous mask */
53 	val ^= rng->mask;
54 
55 	/* update the mask based on the parity of this value */
56 	rng->mask = (rng->mask << 1) | (parity & 1);
57 
58 	return val;
59 }
60 
61 int powernv_get_random_real_mode(unsigned long *v)
62 {
63 	struct powernv_rng *rng;
64 
65 	rng = raw_cpu_read(powernv_rng);
66 
67 	*v = rng_whiten(rng, __raw_rm_readq(rng->regs_real));
68 
69 	return 1;
70 }
71 
72 static int powernv_get_random_darn(unsigned long *v)
73 {
74 	unsigned long val;
75 
76 	/* Using DARN with L=1 - 64-bit conditioned random number */
77 	asm volatile(PPC_DARN(%0, 1) : "=r"(val));
78 
79 	if (val == DARN_ERR)
80 		return 0;
81 
82 	*v = val;
83 
84 	return 1;
85 }
86 
87 static int __init initialise_darn(void)
88 {
89 	unsigned long val;
90 	int i;
91 
92 	if (!cpu_has_feature(CPU_FTR_ARCH_300))
93 		return -ENODEV;
94 
95 	for (i = 0; i < 10; i++) {
96 		if (powernv_get_random_darn(&val)) {
97 			ppc_md.get_random_seed = powernv_get_random_darn;
98 			return 0;
99 		}
100 	}
101 
102 	pr_warn("Unable to use DARN for get_random_seed()\n");
103 
104 	return -EIO;
105 }
106 
107 int powernv_get_random_long(unsigned long *v)
108 {
109 	struct powernv_rng *rng;
110 
111 	rng = get_cpu_var(powernv_rng);
112 
113 	*v = rng_whiten(rng, in_be64(rng->regs));
114 
115 	put_cpu_var(rng);
116 
117 	return 1;
118 }
119 EXPORT_SYMBOL_GPL(powernv_get_random_long);
120 
121 static __init void rng_init_per_cpu(struct powernv_rng *rng,
122 				    struct device_node *dn)
123 {
124 	int chip_id, cpu;
125 
126 	chip_id = of_get_ibm_chip_id(dn);
127 	if (chip_id == -1)
128 		pr_warn("No ibm,chip-id found for %pOF.\n", dn);
129 
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;
134 		}
135 	}
136 }
137 
138 static __init int rng_create(struct device_node *dn)
139 {
140 	struct powernv_rng *rng;
141 	struct resource res;
142 	unsigned long val;
143 
144 	rng = kzalloc(sizeof(*rng), GFP_KERNEL);
145 	if (!rng)
146 		return -ENOMEM;
147 
148 	if (of_address_to_resource(dn, 0, &res)) {
149 		kfree(rng);
150 		return -ENXIO;
151 	}
152 
153 	rng->regs_real = (void __iomem *)res.start;
154 
155 	rng->regs = of_iomap(dn, 0);
156 	if (!rng->regs) {
157 		kfree(rng);
158 		return -ENXIO;
159 	}
160 
161 	val = in_be64(rng->regs);
162 	rng->mask = val;
163 
164 	rng_init_per_cpu(rng, dn);
165 
166 	pr_info_once("Registering arch random hook.\n");
167 
168 	ppc_md.get_random_seed = powernv_get_random_long;
169 
170 	return 0;
171 }
172 
173 static __init int rng_init(void)
174 {
175 	struct device_node *dn;
176 	int rc;
177 
178 	for_each_compatible_node(dn, NULL, "ibm,power-rng") {
179 		rc = rng_create(dn);
180 		if (rc) {
181 			pr_err("Failed creating rng for %pOF (%d).\n",
182 				dn, rc);
183 			continue;
184 		}
185 
186 		/* Create devices for hwrng driver */
187 		of_platform_device_create(dn, NULL, NULL);
188 	}
189 
190 	initialise_darn();
191 
192 	return 0;
193 }
194 machine_subsys_initcall(powernv, rng_init);
195