linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Nicholas Piggin <npiggin@gmail.com>
To: linuxppc-dev@ozlabs.org, Paul Mackerras <paulus@ozlabs.org>
Subject: Re: [PATCH v2 6/9] powerpc/microwatt: Add support for hardware random number generator
Date: Sat, 19 Jun 2021 13:08:51 +1000	[thread overview]
Message-ID: <1624071936.oqwaldrt74.astroid@bobo.none> (raw)
In-Reply-To: <YMwXPHlV/ZleiQUY@thinks.paulus.ozlabs.org>

Excerpts from Paul Mackerras's message of June 18, 2021 1:47 pm:
> Microwatt's hardware RNG is accessed using the DARN instruction.
> 

I think we're getting a platforms/book3s soon with the VAS patches, 
might be a place to add the get_random_darn function.

Huh, DARN is unprivileged right? And yet we haven't wired it up in
pseries it still uses an hcall.

Anyway that's all stuff to sort out later.

Reviewed-by: Nicholas Piggin <npiggin@gmail.com>

> Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
> ---
>  arch/powerpc/platforms/microwatt/Kconfig  |  1 +
>  arch/powerpc/platforms/microwatt/Makefile |  2 +-
>  arch/powerpc/platforms/microwatt/rng.c    | 48 +++++++++++++++++++++++
>  3 files changed, 50 insertions(+), 1 deletion(-)
>  create mode 100644 arch/powerpc/platforms/microwatt/rng.c
> 
> diff --git a/arch/powerpc/platforms/microwatt/Kconfig b/arch/powerpc/platforms/microwatt/Kconfig
> index 50ed0cedb5f1..8f6a81978461 100644
> --- a/arch/powerpc/platforms/microwatt/Kconfig
> +++ b/arch/powerpc/platforms/microwatt/Kconfig
> @@ -7,6 +7,7 @@ config PPC_MICROWATT
>  	select PPC_ICP_NATIVE
>  	select PPC_NATIVE
>  	select PPC_UDBG_16550
> +	select ARCH_RANDOM
>  	help
>            This option enables support for FPGA-based Microwatt implementations.
>  
> diff --git a/arch/powerpc/platforms/microwatt/Makefile b/arch/powerpc/platforms/microwatt/Makefile
> index e6885b3b2ee7..116d6d3ad3f0 100644
> --- a/arch/powerpc/platforms/microwatt/Makefile
> +++ b/arch/powerpc/platforms/microwatt/Makefile
> @@ -1 +1 @@
> -obj-y	+= setup.o
> +obj-y	+= setup.o rng.o
> diff --git a/arch/powerpc/platforms/microwatt/rng.c b/arch/powerpc/platforms/microwatt/rng.c
> new file mode 100644
> index 000000000000..3d8ee6eb7dad
> --- /dev/null
> +++ b/arch/powerpc/platforms/microwatt/rng.c
> @@ -0,0 +1,48 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Derived from arch/powerpc/platforms/powernv/rng.c, which is:
> + * Copyright 2013, Michael Ellerman, IBM Corporation.
> + */
> +
> +#define pr_fmt(fmt)	"microwatt-rng: " fmt
> +
> +#include <linux/kernel.h>
> +#include <linux/smp.h>
> +#include <asm/archrandom.h>
> +#include <asm/cputable.h>
> +#include <asm/machdep.h>
> +
> +#define DARN_ERR 0xFFFFFFFFFFFFFFFFul
> +
> +int microwatt_get_random_darn(unsigned long *v)
> +{
> +	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;
> +}
> +
> +static __init int rng_init(void)
> +{
> +	unsigned long val;
> +	int i;
> +
> +	for (i = 0; i < 10; i++) {
> +		if (microwatt_get_random_darn(&val)) {
> +			ppc_md.get_random_seed = microwatt_get_random_darn;
> +			return 0;
> +		}
> +	}
> +
> +	pr_warn("Unable to use DARN for get_random_seed()\n");
> +
> +	return -EIO;
> +}
> +machine_subsys_initcall(, rng_init);
> -- 
> 2.31.1
> 
> 

  reply	other threads:[~2021-06-19  3:09 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-18  3:42 [PATCH v2 0/9] powerpc: Add support for Microwatt soft-core Paul Mackerras
2021-06-18  3:43 ` [PATCH v2 1/9] powerpc: Add Microwatt platform Paul Mackerras
2021-06-19  3:03   ` Nicholas Piggin
2021-06-18  3:44 ` [PATCH v2 2/9] powerpc: Add Microwatt device tree Paul Mackerras
2021-06-19 14:26   ` Segher Boessenkool
2021-06-20 12:08     ` Paul Mackerras
2021-06-21 13:54       ` Segher Boessenkool
2021-06-18  3:45 ` [PATCH v2 3/9] powerpc/microwatt: Populate platform bus from device-tree Paul Mackerras
2021-06-18  3:45 ` [PATCH v2 4/9] powerpc/xics: Add a native ICS backend for microwatt Paul Mackerras
2021-06-18  3:46 ` [PATCH v2 5/9] powerpc/microwatt: Use standard 16550 UART for console Paul Mackerras
2021-06-18  7:40   ` Nicholas Piggin
2021-06-18 12:12     ` Paul Mackerras
2021-06-19  2:58       ` Nicholas Piggin
2021-08-12 13:14   ` Christophe Leroy
2021-08-12 16:09     ` Segher Boessenkool
2021-06-18  3:47 ` [PATCH v2 6/9] powerpc/microwatt: Add support for hardware random number generator Paul Mackerras
2021-06-19  3:08   ` Nicholas Piggin [this message]
2021-06-19 14:36     ` Segher Boessenkool
2021-06-20  8:19       ` Nicholas Piggin
2021-06-18  3:48 ` [PATCH v2 7/9] powerpc/microwatt: Add microwatt_defconfig Paul Mackerras
2021-06-18  3:49 ` [PATCH v2 8/9] powerpc/boot: Fixup device-tree on little endian Paul Mackerras
2021-06-19  3:14   ` Nicholas Piggin
2021-06-18  3:49 ` [PATCH v2 9/9] powerpc/boot: Add a boot wrapper for Microwatt Paul Mackerras
2021-06-19  3:16   ` Nicholas Piggin
2021-06-19 14:45 ` [PATCH v2 0/9] powerpc: Add support for Microwatt soft-core Segher Boessenkool
2021-06-24 14:03 ` Michael Ellerman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1624071936.oqwaldrt74.astroid@bobo.none \
    --to=npiggin@gmail.com \
    --cc=linuxppc-dev@ozlabs.org \
    --cc=paulus@ozlabs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).