1 // SPDX-License-Identifier: GPL-2.0
3 * Hardware Random Number Generator support.
4 * Cavium Thunder, Marvell OcteonTx/Tx2 processor families.
6 * Copyright (C) 2016 Cavium, Inc.
9 #include <linux/hw_random.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/pci_ids.h>
15 #include <asm/arch_timer.h>
18 #define PCI_DEVID_CAVIUM_RNG_PF 0xA018
19 #define PCI_DEVID_CAVIUM_RNG_VF 0xA033
21 #define HEALTH_STATUS_REG 0x38
24 #define PCI_DEVICE_ID_RST_OTX2 0xA085
25 #define RST_BOOT_REG 0x1600ULL
26 #define CLOCK_BASE_RATE 50000000ULL
27 #define MSEC_TO_NSEC(x) (x * 1000000)
32 void __iomem
*pf_regbase
;
39 static inline bool is_octeontx(struct pci_dev
*pdev
)
41 if (midr_is_cpu_model_range(read_cpuid_id(), MIDR_THUNDERX_83XX
,
42 MIDR_CPU_VAR_REV(0, 0),
43 MIDR_CPU_VAR_REV(3, 0)) ||
44 midr_is_cpu_model_range(read_cpuid_id(), MIDR_THUNDERX_81XX
,
45 MIDR_CPU_VAR_REV(0, 0),
46 MIDR_CPU_VAR_REV(3, 0)) ||
47 midr_is_cpu_model_range(read_cpuid_id(), MIDR_THUNDERX
,
48 MIDR_CPU_VAR_REV(0, 0),
49 MIDR_CPU_VAR_REV(3, 0)))
55 static u64
rng_get_coprocessor_clkrate(void)
57 u64 ret
= CLOCK_BASE_RATE
* 16; /* Assume 800Mhz as default */
61 pdev
= pci_get_device(PCI_VENDOR_ID_CAVIUM
,
62 PCI_DEVICE_ID_RST_OTX2
, NULL
);
66 base
= pci_ioremap_bar(pdev
, 0);
70 /* RST: PNR_MUL * 50Mhz gives clockrate */
71 ret
= CLOCK_BASE_RATE
* ((readq(base
+ RST_BOOT_REG
) >> 33) & 0x3F);
82 static int check_rng_health(struct cavium_rng
*rng
)
84 u64 cur_err
, cur_time
;
89 /* Skip checking health for OcteonTx */
93 status
= readq(rng
->pf_regbase
+ HEALTH_STATUS_REG
);
94 if (status
& BIT_ULL(0)) {
95 dev_err(&rng
->pdev
->dev
, "HWRNG: Startup health test failed\n");
103 cur_time
= arch_timer_read_counter();
105 /* RNM_HEALTH_STATUS[CYCLES_SINCE_HEALTH_FAILURE]
106 * Number of coprocessor cycles times 2 since the last failure.
107 * This field doesn't get cleared/updated until another failure.
110 cur_err
= (cycles
* 1000000000) / rng
->clock_rate
; /* In nanosec */
112 /* Ignore errors that happenned a long time ago, these
113 * are most likely false positive errors.
115 if (cur_err
> MSEC_TO_NSEC(10)) {
121 if (rng
->prev_error
) {
122 /* Calculate time elapsed since last error
123 * '1' tick of CNTVCT is 10ns, since it runs at 100Mhz.
125 time_elapsed
= (cur_time
- rng
->prev_time
) * 10;
126 time_elapsed
+= rng
->prev_error
;
128 /* Check if current error is a new one or the old one itself.
129 * If error is a new one then consider there is a persistent
130 * issue with entropy, declare hardware failure.
132 if (cur_err
< time_elapsed
) {
133 dev_err(&rng
->pdev
->dev
, "HWRNG failure detected\n");
134 rng
->prev_error
= cur_err
;
135 rng
->prev_time
= cur_time
;
140 rng
->prev_error
= cur_err
;
141 rng
->prev_time
= cur_time
;
145 /* Read data from the RNG unit */
146 static int cavium_rng_read(struct hwrng
*rng
, void *dat
, size_t max
, bool wait
)
148 struct cavium_rng
*p
= container_of(rng
, struct cavium_rng
, ops
);
149 unsigned int size
= max
;
152 err
= check_rng_health(p
);
157 *((u64
*)dat
) = readq(p
->result
);
162 *((u8
*)dat
) = readb(p
->result
);
169 static int cavium_map_pf_regs(struct cavium_rng
*rng
)
171 struct pci_dev
*pdev
;
173 /* Health status is not supported on 83xx, skip mapping PF CSRs */
174 if (is_octeontx(rng
->pdev
)) {
175 rng
->pf_regbase
= NULL
;
179 pdev
= pci_get_device(PCI_VENDOR_ID_CAVIUM
,
180 PCI_DEVID_CAVIUM_RNG_PF
, NULL
);
182 pr_err("Cannot find RNG PF device\n");
186 rng
->pf_regbase
= ioremap(pci_resource_start(pdev
, 0),
187 pci_resource_len(pdev
, 0));
188 if (!rng
->pf_regbase
) {
189 dev_err(&pdev
->dev
, "Failed to map PF CSR region\n");
196 /* Get co-processor clock rate */
197 rng
->clock_rate
= rng_get_coprocessor_clkrate();
202 /* Map Cavium RNG to an HWRNG object */
203 static int cavium_rng_probe_vf(struct pci_dev
*pdev
,
204 const struct pci_device_id
*id
)
206 struct cavium_rng
*rng
;
209 rng
= devm_kzalloc(&pdev
->dev
, sizeof(*rng
), GFP_KERNEL
);
215 /* Map the RNG result */
216 rng
->result
= pcim_iomap(pdev
, 0, 0);
218 dev_err(&pdev
->dev
, "Error iomap failed retrieving result.\n");
222 rng
->ops
.name
= devm_kasprintf(&pdev
->dev
, GFP_KERNEL
,
223 "cavium-rng-%s", dev_name(&pdev
->dev
));
227 rng
->ops
.read
= cavium_rng_read
;
229 pci_set_drvdata(pdev
, rng
);
231 /* Health status is available only at PF, hence map PF registers. */
232 ret
= cavium_map_pf_regs(rng
);
236 ret
= devm_hwrng_register(&pdev
->dev
, &rng
->ops
);
238 dev_err(&pdev
->dev
, "Error registering device as HWRNG.\n");
246 static void cavium_rng_remove_vf(struct pci_dev
*pdev
)
248 struct cavium_rng
*rng
;
250 rng
= pci_get_drvdata(pdev
);
251 iounmap(rng
->pf_regbase
);
254 static const struct pci_device_id cavium_rng_vf_id_table
[] = {
255 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM
, PCI_DEVID_CAVIUM_RNG_VF
) },
258 MODULE_DEVICE_TABLE(pci
, cavium_rng_vf_id_table
);
260 static struct pci_driver cavium_rng_vf_driver
= {
261 .name
= "cavium_rng_vf",
262 .id_table
= cavium_rng_vf_id_table
,
263 .probe
= cavium_rng_probe_vf
,
264 .remove
= cavium_rng_remove_vf
,
266 module_pci_driver(cavium_rng_vf_driver
);
268 MODULE_AUTHOR("Omer Khaliq <okhaliq@caviumnetworks.com>");
269 MODULE_DESCRIPTION("Cavium ThunderX Random Number Generator VF support");
270 MODULE_LICENSE("GPL v2");