1/*-
2 * Copyright (c) 2018 Justin Hibbits
3 * Copyright (c) 2013 The FreeBSD Foundation
4 * Copyright (c) 2013 David E. O'Brien <obrien@NUXI.org>
5 * Copyright (c) 2012 Konstantin Belousov <kib@FreeBSD.org>
6 * All rights reserved.
7 *
8 * Portions of this software were developed by Konstantin Belousov
9 * under sponsorship from the FreeBSD Foundation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer
16 *    in this position and unchanged.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <sys/param.h>
35#include <sys/kernel.h>
36#include <sys/conf.h>
37#include <sys/lock.h>
38#include <sys/malloc.h>
39#include <sys/module.h>
40#include <sys/random.h>
41#include <sys/systm.h>
42
43#include <machine/cpu.h>
44#include <machine/md_var.h>
45
46#include <dev/random/randomdev.h>
47
48/*
49 * Power ISA 3.0 adds a "darn" instruction (Deliver A Random Number).  The RNG
50 * backing this instruction conforms to NIST SP800-90B and SP800-90C at the
51 * point of hardware design, and provides a minimum of 0.5 bits of entropy per
52 * bit.
53 */
54
55#define	RETRY_COUNT	10
56
57static u_int random_darn_read(void *, u_int);
58
59static struct random_source random_darn = {
60	.rs_ident = "PowerISA DARN random number generator",
61	.rs_source = RANDOM_PURE_DARN,
62	.rs_read = random_darn_read
63};
64
65static inline int
66darn_rng_store(u_long *buf)
67{
68	u_long rndval;
69	int retry;
70
71	for (retry = RETRY_COUNT; retry > 0; --retry) {
72		/* "DARN %rN, 1" instruction */
73		/*
74		 * Arguments for DARN: rN and "L", where "L" can be one of:
75		 * 0 - 32-bit conditional random number
76		 * 1 - Conditional random number (conditioned to remove bias)
77		 * 2 - Raw random number	 (unprocessed, may include bias)
78		 * 3 - Reserved
79		 */
80	    	__asm __volatile(".long 0x7c0105e6 | (%0 << 21)" :
81	    	    "+r"(rndval));
82		if (rndval != ~0)
83			break;
84	}
85
86	*buf = rndval;
87	return (retry);
88}
89
90/* It is required that buf length is a multiple of sizeof(u_long). */
91static u_int
92random_darn_read(void *buf, u_int c)
93{
94	u_long *b, rndval;
95	u_int count;
96
97	KASSERT(c % sizeof(*b) == 0, ("partial read %d", c));
98	b = buf;
99	for (count = c; count > 0; count -= sizeof(*b)) {
100		if (darn_rng_store(&rndval) == 0)
101			break;
102		*b++ = rndval;
103	}
104	return (c - count);
105}
106
107static int
108darn_modevent(module_t mod, int type, void *unused)
109{
110	int error = 0;
111
112	switch (type) {
113	case MOD_LOAD:
114		if (cpu_features2 & PPC_FEATURE2_DARN) {
115			random_source_register(&random_darn);
116			printf("random: fast provider: \"%s\"\n", random_darn.rs_ident);
117		}
118		break;
119
120	case MOD_UNLOAD:
121		if (cpu_features2 & PPC_FEATURE2_DARN)
122			random_source_deregister(&random_darn);
123		break;
124
125	case MOD_SHUTDOWN:
126		break;
127
128	default:
129		error = EOPNOTSUPP;
130		break;
131
132	}
133
134	return (error);
135}
136
137static moduledata_t darn_mod = {
138	"darn",
139	darn_modevent,
140	0
141};
142
143DECLARE_MODULE(darn, darn_mod, SI_SUB_RANDOM, SI_ORDER_FOURTH);
144MODULE_VERSION(darn, 1);
145MODULE_DEPEND(darn, random_harvestq, 1, 1, 1);
146