2019-05-27 06:55:01 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2013-10-11 03:07:57 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013, Michael Ellerman, IBM Corporation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define pr_fmt(fmt) "powernv-rng: " fmt
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/of.h>
|
2013-10-28 08:34:41 +00:00
|
|
|
#include <linux/of_address.h>
|
2013-10-11 03:07:57 +00:00
|
|
|
#include <linux/of_platform.h>
|
|
|
|
#include <linux/slab.h>
|
2013-10-28 08:34:41 +00:00
|
|
|
#include <linux/smp.h>
|
2013-10-11 03:07:57 +00:00
|
|
|
#include <asm/archrandom.h>
|
2017-08-04 01:12:18 +00:00
|
|
|
#include <asm/cputable.h>
|
2013-10-11 03:07:57 +00:00
|
|
|
#include <asm/io.h>
|
2013-10-28 08:34:41 +00:00
|
|
|
#include <asm/prom.h>
|
2013-10-11 03:07:57 +00:00
|
|
|
#include <asm/machdep.h>
|
2013-11-20 00:05:01 +00:00
|
|
|
#include <asm/smp.h>
|
2022-06-21 14:08:49 +00:00
|
|
|
#include "powernv.h"
|
2013-10-11 03:07:57 +00:00
|
|
|
|
2017-08-04 01:12:18 +00:00
|
|
|
#define DARN_ERR 0xFFFFFFFFFFFFFFFFul
|
2013-10-11 03:07:57 +00:00
|
|
|
|
2022-07-27 14:32:19 +00:00
|
|
|
struct pnv_rng {
|
2013-10-11 03:07:57 +00:00
|
|
|
void __iomem *regs;
|
2015-03-20 09:39:41 +00:00
|
|
|
void __iomem *regs_real;
|
2013-10-11 03:07:57 +00:00
|
|
|
unsigned long mask;
|
|
|
|
};
|
|
|
|
|
2022-07-27 14:32:19 +00:00
|
|
|
static DEFINE_PER_CPU(struct pnv_rng *, pnv_rng);
|
2013-10-11 03:07:57 +00:00
|
|
|
|
2022-07-27 14:32:19 +00:00
|
|
|
static unsigned long rng_whiten(struct pnv_rng *rng, unsigned long val)
|
2013-10-11 03:07:57 +00:00
|
|
|
{
|
|
|
|
unsigned long parity;
|
|
|
|
|
|
|
|
/* Calculate the parity of the value */
|
2022-02-24 16:22:14 +00:00
|
|
|
asm (".machine push; \
|
|
|
|
.machine power7; \
|
|
|
|
popcntd %0,%1; \
|
|
|
|
.machine pop;"
|
|
|
|
: "=r" (parity) : "r" (val));
|
2013-10-11 03:07:57 +00:00
|
|
|
|
|
|
|
/* xor our value with the previous mask */
|
|
|
|
val ^= rng->mask;
|
|
|
|
|
|
|
|
/* update the mask based on the parity of this value */
|
|
|
|
rng->mask = (rng->mask << 1) | (parity & 1);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2022-07-27 14:32:19 +00:00
|
|
|
static int pnv_get_random_darn(unsigned long *v)
|
2017-08-04 01:12:18 +00:00
|
|
|
{
|
|
|
|
unsigned long val;
|
|
|
|
|
|
|
|
/* Using DARN with L=1 - 64-bit conditioned random number */
|
|
|
|
asm volatile(PPC_DARN(%0, 1) : "=r"(val));
|
|
|
|
|
|
|
|
if (val == DARN_ERR)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*v = val;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-12-16 22:00:26 +00:00
|
|
|
static int __init initialise_darn(void)
|
2017-08-04 01:12:18 +00:00
|
|
|
{
|
|
|
|
unsigned long val;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!cpu_has_feature(CPU_FTR_ARCH_300))
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
for (i = 0; i < 10; i++) {
|
2022-07-27 14:32:19 +00:00
|
|
|
if (pnv_get_random_darn(&val)) {
|
|
|
|
ppc_md.get_random_seed = pnv_get_random_darn;
|
2017-08-04 01:12:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2022-07-27 14:32:19 +00:00
|
|
|
int pnv_get_random_long(unsigned long *v)
|
2013-10-11 03:07:57 +00:00
|
|
|
{
|
2022-07-27 14:32:19 +00:00
|
|
|
struct pnv_rng *rng;
|
2013-10-11 03:07:57 +00:00
|
|
|
|
2022-07-27 14:32:18 +00:00
|
|
|
if (mfmsr() & MSR_DR) {
|
2022-07-27 14:32:19 +00:00
|
|
|
rng = get_cpu_var(pnv_rng);
|
2022-07-27 14:32:18 +00:00
|
|
|
*v = rng_whiten(rng, in_be64(rng->regs));
|
|
|
|
put_cpu_var(rng);
|
|
|
|
} else {
|
2022-07-27 14:32:19 +00:00
|
|
|
rng = raw_cpu_read(pnv_rng);
|
2022-07-27 14:32:18 +00:00
|
|
|
*v = rng_whiten(rng, __raw_rm_readq(rng->regs_real));
|
|
|
|
}
|
2013-10-11 03:07:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2022-07-27 14:32:19 +00:00
|
|
|
EXPORT_SYMBOL_GPL(pnv_get_random_long);
|
2013-10-11 03:07:57 +00:00
|
|
|
|
2022-07-27 14:32:19 +00:00
|
|
|
static __init void rng_init_per_cpu(struct pnv_rng *rng,
|
2013-10-11 03:07:57 +00:00
|
|
|
struct device_node *dn)
|
|
|
|
{
|
|
|
|
int chip_id, cpu;
|
|
|
|
|
|
|
|
chip_id = of_get_ibm_chip_id(dn);
|
|
|
|
if (chip_id == -1)
|
2017-08-21 15:16:47 +00:00
|
|
|
pr_warn("No ibm,chip-id found for %pOF.\n", dn);
|
2013-10-11 03:07:57 +00:00
|
|
|
|
|
|
|
for_each_possible_cpu(cpu) {
|
2022-07-27 14:32:19 +00:00
|
|
|
if (per_cpu(pnv_rng, cpu) == NULL ||
|
2013-10-11 03:07:57 +00:00
|
|
|
cpu_to_chip_id(cpu) == chip_id) {
|
2022-07-27 14:32:19 +00:00
|
|
|
per_cpu(pnv_rng, cpu) = rng;
|
2013-10-11 03:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static __init int rng_create(struct device_node *dn)
|
|
|
|
{
|
2022-07-27 14:32:19 +00:00
|
|
|
struct pnv_rng *rng;
|
2015-03-20 09:39:41 +00:00
|
|
|
struct resource res;
|
2013-10-11 03:07:57 +00:00
|
|
|
unsigned long val;
|
|
|
|
|
|
|
|
rng = kzalloc(sizeof(*rng), GFP_KERNEL);
|
|
|
|
if (!rng)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2015-03-20 09:39:41 +00:00
|
|
|
if (of_address_to_resource(dn, 0, &res)) {
|
|
|
|
kfree(rng);
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
rng->regs_real = (void __iomem *)res.start;
|
|
|
|
|
2013-10-11 03:07:57 +00:00
|
|
|
rng->regs = of_iomap(dn, 0);
|
|
|
|
if (!rng->regs) {
|
|
|
|
kfree(rng);
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
val = in_be64(rng->regs);
|
|
|
|
rng->mask = val;
|
|
|
|
|
|
|
|
rng_init_per_cpu(rng, dn);
|
|
|
|
|
2022-07-27 14:32:19 +00:00
|
|
|
ppc_md.get_random_seed = pnv_get_random_long;
|
2013-10-11 03:07:57 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-21 14:08:49 +00:00
|
|
|
static int __init pnv_get_random_long_early(unsigned long *v)
|
2013-10-11 03:07:57 +00:00
|
|
|
{
|
|
|
|
struct device_node *dn;
|
2022-06-21 14:08:49 +00:00
|
|
|
|
|
|
|
if (!slab_is_available())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (cmpxchg(&ppc_md.get_random_seed, pnv_get_random_long_early,
|
|
|
|
NULL) != pnv_get_random_long_early)
|
|
|
|
return 0;
|
2013-10-11 03:07:57 +00:00
|
|
|
|
2022-06-30 12:16:54 +00:00
|
|
|
for_each_compatible_node(dn, NULL, "ibm,power-rng")
|
|
|
|
rng_create(dn);
|
2013-10-11 03:07:57 +00:00
|
|
|
|
2022-06-21 14:08:49 +00:00
|
|
|
if (!ppc_md.get_random_seed)
|
|
|
|
return 0;
|
|
|
|
return ppc_md.get_random_seed(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void __init pnv_rng_init(void)
|
|
|
|
{
|
|
|
|
struct device_node *dn;
|
2017-08-04 01:12:18 +00:00
|
|
|
|
2022-06-21 14:08:49 +00:00
|
|
|
/* Prefer darn over the rest. */
|
|
|
|
if (!initialise_darn())
|
|
|
|
return;
|
|
|
|
|
|
|
|
dn = of_find_compatible_node(NULL, NULL, "ibm,power-rng");
|
|
|
|
if (dn)
|
|
|
|
ppc_md.get_random_seed = pnv_get_random_long_early;
|
|
|
|
|
|
|
|
of_node_put(dn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init pnv_rng_late_init(void)
|
|
|
|
{
|
2022-06-30 12:16:54 +00:00
|
|
|
struct device_node *dn;
|
2022-06-21 14:08:49 +00:00
|
|
|
unsigned long v;
|
2022-06-30 12:16:54 +00:00
|
|
|
|
2022-06-21 14:08:49 +00:00
|
|
|
/* In case it wasn't called during init for some other reason. */
|
|
|
|
if (ppc_md.get_random_seed == pnv_get_random_long_early)
|
|
|
|
pnv_get_random_long_early(&v);
|
2022-06-30 12:16:54 +00:00
|
|
|
|
2022-07-27 14:32:19 +00:00
|
|
|
if (ppc_md.get_random_seed == pnv_get_random_long) {
|
2022-06-30 12:16:54 +00:00
|
|
|
for_each_compatible_node(dn, NULL, "ibm,power-rng")
|
|
|
|
of_platform_device_create(dn, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2013-10-11 03:07:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2022-06-21 14:08:49 +00:00
|
|
|
machine_subsys_initcall(powernv, pnv_rng_late_init);
|