Index | Thread | Search

From:
Mark Kettenis <mark.kettenis@xs4all.nl>
Subject:
Re: mtrng(4): MediaTek random number generator
To:
James Hastings <moosetek4@gmail.com>
Cc:
moosetek4@gmail.com,tech@openbsd.org
Date:
Wed, 12 Feb 2025 17:57:37 +0100

Download raw body.

Thread
> From: James Hastings <moosetek4@gmail.com>
> Date: Tue, 11 Feb 2025 04:37:09 -0500 (EST)
> 
> Add support for the 32-bit random number generator on MediaTek SoCs.
> 
> ok?

One small nit.  Otherwise ok kettenis@

> Index: sys/dev/fdt/mtrng.c
> ===================================================================
> RCS file: sys/dev/fdt/mtrng.c
> diff -N sys/dev/fdt/mtrng.c
> --- /dev/null	1 Jan 1970 00:00:00 -0000
> +++ sys/dev/fdt/mtrng.c	11 Feb 2025 06:08:45 -0000
> @@ -0,0 +1,105 @@
> +/*	$OpenBSD$	*/
> +/*
> + * Copyright (c) 2025 James Hastings <hastings@openbsd.org>
> + *
> + * Permission to use, copy, modify, and distribute this software for any
> + * purpose with or without fee is hereby granted, provided that the above
> + * copyright notice and this permission notice appear in all copies.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
> + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
> + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
> + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
> + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
> + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
> + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
> + */
> +
> +#include <sys/param.h>
> +#include <sys/systm.h>
> +#include <sys/device.h>
> +#include <sys/timeout.h>
> +
> +#include <machine/bus.h>
> +#include <machine/fdt.h>
> +
> +#include <dev/ofw/openfirm.h>
> +#include <dev/ofw/ofw_clock.h>
> +#include <dev/ofw/fdt.h>
> +
> +/* Registers */
> +#define RNG_CONF		0x00
> +#define  RNG_READY		(1U << 31)
> +#define  RNG_EN			(1U <<  0)

You have an extra space here after the "<<".

> +#define RNG_DATA		0x08
> +
> +struct mtrng_softc {
> +	struct device		sc_dev;
> +	bus_space_tag_t		sc_iot;
> +	bus_space_handle_t	sc_ioh;
> +
> +	struct timeout		sc_to;
> +};
> +
> +int	mtrng_match(struct device *, void *, void *);
> +void	mtrng_attach(struct device *, struct device *, void *);
> +
> +const struct cfattach	mtrng_ca = {
> +	sizeof (struct mtrng_softc), mtrng_match, mtrng_attach
> +};
> +
> +struct cfdriver mtrng_cd = {
> +	NULL, "mtrng", DV_DULL
> +};
> +
> +void	mtrng_rnd(void *);
> +
> +int
> +mtrng_match(struct device *parent, void *match, void *aux)
> +{
> +	struct fdt_attach_args *faa = aux;
> +
> +	return OF_is_compatible(faa->fa_node, "mediatek,mt7623-rng");
> +}
> +
> +void
> +mtrng_attach(struct device *parent, struct device *self, void *aux)
> +{
> +	struct mtrng_softc *sc = (struct mtrng_softc *)self;
> +	struct fdt_attach_args *faa = aux;
> +
> +	if (faa->fa_nreg < 1) {
> +		printf(": no registers\n");
> +		return;
> +	}
> +
> +	sc->sc_iot = faa->fa_iot;
> +	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
> +	    faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
> +		printf(": can't map registers\n");
> +		return;
> +	}
> +
> +	clock_enable_all(faa->fa_node);
> +
> +	bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_CONF, RNG_EN);
> +
> +	printf("\n");
> +
> +	timeout_set(&sc->sc_to, mtrng_rnd, sc);
> +	mtrng_rnd(sc);
> +}
> +
> +void
> +mtrng_rnd(void *arg)
> +{
> +	struct mtrng_softc *sc = arg;
> +	uint32_t sta;
> +
> +	sta = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_CONF);
> +	if ((sta & RNG_READY) == RNG_READY)
> +		enqueue_randomness(bus_space_read_4(sc->sc_iot,
> +		    sc->sc_ioh, RNG_DATA));
> +
> +	timeout_add_sec(&sc->sc_to, 1);
> +}
> Index: sys/dev/fdt/files.fdt
> ===================================================================
> RCS file: /cvs/src/sys/dev/fdt/files.fdt,v
> retrieving revision 1.205
> diff -u -p -r1.205 files.fdt
> --- sys/dev/fdt/files.fdt	30 Jan 2025 00:26:44 -0000	1.205
> +++ sys/dev/fdt/files.fdt	11 Feb 2025 06:08:45 -0000
> @@ -340,6 +340,10 @@ device	mtintc
>  attach	mtintc at fdt
>  file	dev/fdt/mtintc.c		mtintc
>  
> +device	mtrng
> +attach	mtrng at fdt
> +file	dev/fdt/mtrng.c			mtrng
> +
>  device	rkanxdp
>  attach	rkanxdp at fdt
>  file	dev/fdt/rkanxdp.c		rkanxdp
> Index: share/man/man4/mtrng.4
> ===================================================================
> RCS file: share/man/man4/mtrng.4
> diff -N share/man/man4/mtrng.4
> --- /dev/null	1 Jan 1970 00:00:00 -0000
> +++ share/man/man4/mtrng.4	11 Feb 2025 06:08:45 -0000
> @@ -0,0 +1,47 @@
> +.\"	$OpenBSD$
> +.\"
> +.\" Copyright (c) 2025 James Hastings <hastings@openbsd.org>
> +.\"
> +.\" Permission to use, copy, modify, and distribute this software for any
> +.\" purpose with or without fee is hereby granted, provided that the above
> +.\" copyright notice and this permission notice appear in all copies.
> +.\"
> +.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
> +.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
> +.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
> +.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
> +.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
> +.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
> +.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
> +.\"
> +.Dd $Mdocdate$
> +.Dt MTRNG 4
> +.Os
> +.Sh NAME
> +.Nm mtrng
> +.Nd MediaTek random number generator
> +.Sh SYNOPSIS
> +.Cd "mtrng* at fdt?"
> +.Sh DESCRIPTION
> +The
> +.Nm
> +driver provides support for the random number generator found on
> +MediaTek SoCs.
> +.Pp
> +It feeds the random subsystem's entropy pool with 32 bits of
> +random data every second.
> +.Sh SEE ALSO
> +.Xr intro 4 ,
> +.Xr random 4 ,
> +.Xr arc4random 9
> +.Sh HISTORY
> +The
> +.Nm
> +driver first appeared in
> +.Ox 7.7 .
> +.Sh AUTHORS
> +.An -nosplit
> +The
> +.Nm
> +driver was written by
> +.An James Hastings Aq Mt hastings@openbsd.org .
> Index: share/man/man4/Makefile
> ===================================================================
> RCS file: /cvs/src/share/man/man4/Makefile,v
> retrieving revision 1.858
> diff -u -p -r1.858 Makefile
> --- share/man/man4/Makefile	30 Jan 2025 00:31:55 -0000	1.858
> +++ share/man/man4/Makefile	11 Feb 2025 06:08:45 -0000
> @@ -59,7 +59,7 @@ MAN=	aac.4 abcrtc.4 abl.4 ac97.4 acphy.4
>  	mcprtc.4 mcx.4 midi.4 mii.4 mfi.4 mfii.4 mfokrtc.4 \
>  	mlphy.4 moscom.4 mos.4 mpe.4 mpath.4 mpi.4 mpii.4 \
>  	mpip.4 mpu.4 msk.4 mpw.4 msts.4 mtd.4 mtdphy.4 \
> -	mtintc.4 mtio.4 mtw.4 mue.4 \
> +	mtintc.4 mtio.4 mtrng.4 mtw.4 mue.4 \
>  	multicast.4 mvclock.4 mvdog.4 mvgicp.4 mvgpio.4 mvicu.4 mviic.4 \
>  	mvkpcie.4 mvneta.4 mvpinctrl.4 mvpp.4 mvrng.4 mvrtc.4 mvspi.4 \
>  	mvtemp.4 mvsw.4 mvuart.4 myx.4 \
> Index: sys/arch/arm64/conf/GENERIC
> ===================================================================
> RCS file: /cvs/src/sys/arch/arm64/conf/GENERIC,v
> retrieving revision 1.291
> diff -u -p -r1.291 GENERIC
> --- sys/arch/arm64/conf/GENERIC	30 Jan 2025 07:32:04 -0000	1.291
> +++ sys/arch/arm64/conf/GENERIC	11 Feb 2025 06:08:46 -0000
> @@ -274,6 +274,7 @@ hitemp*		at fdt?
>  
>  # MediaTek SoCs
>  mtintc*		at fdt?
> +mtrng*		at fdt?
>  
>  # Marvell SoCs
>  mvclock*	at fdt? early 1
> Index: sys/arch/arm64/conf/RAMDISK
> ===================================================================
> RCS file: /cvs/src/sys/arch/arm64/conf/RAMDISK,v
> retrieving revision 1.220
> diff -u -p -r1.220 RAMDISK
> --- sys/arch/arm64/conf/RAMDISK	30 Jan 2025 07:32:04 -0000	1.220
> +++ sys/arch/arm64/conf/RAMDISK	11 Feb 2025 06:08:46 -0000
> @@ -206,6 +206,7 @@ hireset*	at fdt? early 1
>  
>  # MediaTek SoCs
>  mtintc*		at fdt?
> +mtrng*		at fdt?
>  
>  # Marvell SoCs
>  mvclock*	at fdt? early 1
> Index: sys/arch/armv7/conf/GENERIC
> ===================================================================
> RCS file: /cvs/src/sys/arch/armv7/conf/GENERIC,v
> retrieving revision 1.143
> diff -u -p -r1.143 GENERIC
> --- sys/arch/armv7/conf/GENERIC	30 Jan 2025 07:32:17 -0000	1.143
> +++ sys/arch/armv7/conf/GENERIC	11 Feb 2025 06:08:46 -0000
> @@ -186,6 +186,7 @@ usb*		at dwctwo?
>  
>  # MediaTek SoCs
>  mtintc*		at fdt?
> +mtrng*		at fdt?
>  
>  # Marvell SoC
>  mvacc*		at fdt? early 1
> Index: sys/arch/armv7/conf/RAMDISK
> ===================================================================
> RCS file: /cvs/src/sys/arch/armv7/conf/RAMDISK,v
> retrieving revision 1.131
> diff -u -p -r1.131 RAMDISK
> --- sys/arch/armv7/conf/RAMDISK	30 Jan 2025 07:32:17 -0000	1.131
> +++ sys/arch/armv7/conf/RAMDISK	11 Feb 2025 06:08:46 -0000
> @@ -172,6 +172,7 @@ usb*		at dwctwo?
>  
>  # MediaTek SoCs
>  mtintc*		at fdt?
> +mtrng*		at fdt?
>  
>  # Marvell SoC
>  mvacc*		at fdt? early 1
> 
>