bcm2835_rng.c revision 1.11
1/*	$NetBSD: bcm2835_rng.c,v 1.11 2015/04/13 21:18:40 riastradh Exp $ */
2
3/*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jared D. McNeill
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: bcm2835_rng.c,v 1.11 2015/04/13 21:18:40 riastradh Exp $");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/device.h>
38#include <sys/kernel.h>
39#include <sys/bus.h>
40#include <sys/rndpool.h>
41#include <sys/rndsource.h>
42#include <sys/atomic.h>
43#include <sys/intr.h>
44
45#include <arm/broadcom/bcm_amba.h>
46#include <arm/broadcom/bcm2835reg.h>
47#include <arm/broadcom/bcm2835_intr.h>
48
49#define RNG_CTRL		0x00
50#define  RNG_CTRL_EN		__BIT(0)
51#define RNG_STATUS		0x04
52#define  RNG_STATUS_CNT		__BITS(31,24)
53#define RNG_DATA		0x08
54
55#define RNG_DATA_MAX		256
56
57struct bcm2835rng_softc {
58	device_t		sc_dev;
59
60	bus_space_tag_t		sc_iot;
61	bus_space_handle_t	sc_ioh;
62
63	kmutex_t		sc_intr_lock;
64	unsigned int		sc_bytes_wanted;
65	void 			*sc_sih;
66
67	kmutex_t		sc_rnd_lock;
68	krndsource_t		sc_rndsource;
69};
70
71static int bcmrng_match(device_t, cfdata_t, void *);
72static void bcmrng_attach(device_t, device_t, void *);
73static void bcmrng_get(struct bcm2835rng_softc *);
74static void bcmrng_get_cb(size_t, void *);
75static void bcmrng_get_intr(void *);
76
77CFATTACH_DECL_NEW(bcmrng_amba, sizeof(struct bcm2835rng_softc),
78    bcmrng_match, bcmrng_attach, NULL, NULL);
79
80/* ARGSUSED */
81static int
82bcmrng_match(device_t parent, cfdata_t match, void *aux)
83{
84	struct amba_attach_args *aaa = aux;
85
86	if (strcmp(aaa->aaa_name, "bcmrng") != 0)
87		return 0;
88
89	return 1;
90}
91
92static void
93bcmrng_attach(device_t parent, device_t self, void *aux)
94{
95	struct bcm2835rng_softc *sc = device_private(self);
96 	struct amba_attach_args *aaa = aux;
97	uint32_t ctrl;
98
99	aprint_naive("\n");
100	aprint_normal(": RNG\n");
101
102	sc->sc_dev = self;
103	sc->sc_iot = aaa->aaa_iot;
104
105	if (bus_space_map(aaa->aaa_iot, aaa->aaa_addr, BCM2835_RNG_SIZE, 0,
106	    &sc->sc_ioh)) {
107		aprint_error_dev(sc->sc_dev, "unable to map device\n");
108		goto fail0;
109	}
110
111	/* discard initial numbers, broadcom says they are "less random" */
112	bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS, 0x40000);
113
114	/* enable rng */
115	ctrl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL);
116	ctrl |= RNG_CTRL_EN;
117	bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL, ctrl);
118
119	/* set up a softint for adding data */
120	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SERIAL);
121	sc->sc_bytes_wanted = 0;
122	sc->sc_sih = softint_establish(SOFTINT_SERIAL|SOFTINT_MPSAFE,
123	    &bcmrng_get_intr, sc);
124	if (sc->sc_sih == NULL) {
125		aprint_error_dev(sc->sc_dev, "unable to establish softint");
126		goto fail1;
127	}
128
129	/* set up an rndsource */
130	mutex_init(&sc->sc_rnd_lock, MUTEX_DEFAULT, IPL_SERIAL);
131	rndsource_setcb(&sc->sc_rndsource, &bcmrng_get_cb, sc);
132	rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG,
133	    RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
134
135	/* get some initial entropy ASAP */
136	bcmrng_get_cb(RND_POOLBITS / NBBY, sc);
137
138	/* Success!  */
139	return;
140
141fail1:	mutex_destroy(&sc->sc_intr_lock);
142	bus_space_unmap(aaa->aaa_iot, sc->sc_ioh, BCM2835_RNG_SIZE);
143fail0:	return;
144}
145
146static void
147bcmrng_get(struct bcm2835rng_softc *sc)
148{
149	uint32_t status, cnt;
150	uint32_t buf[RNG_DATA_MAX]; /* 1k on the stack */
151
152	mutex_spin_enter(&sc->sc_intr_lock);
153	while (sc->sc_bytes_wanted) {
154		status = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS);
155		cnt = __SHIFTOUT(status, RNG_STATUS_CNT);
156		KASSERT(cnt < RNG_DATA_MAX);
157		if (cnt == 0)
158			continue;	/* XXX Busy-waiting seems wrong...  */
159		bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh, RNG_DATA, buf,
160		    cnt);
161
162		/*
163		 * This lock dance is necessary because rnd_add_data
164		 * may call bcmrng_get_cb which takes the intr lock.
165		 */
166		mutex_spin_exit(&sc->sc_intr_lock);
167		mutex_spin_enter(&sc->sc_rnd_lock);
168		rnd_add_data(&sc->sc_rndsource, buf, (cnt * 4),
169		    (cnt * 4 * NBBY));
170		mutex_spin_exit(&sc->sc_rnd_lock);
171		mutex_spin_enter(&sc->sc_intr_lock);
172		sc->sc_bytes_wanted -= MIN(sc->sc_bytes_wanted, (cnt * 4));
173	}
174	explicit_memset(buf, 0, sizeof(buf));
175	mutex_spin_exit(&sc->sc_intr_lock);
176}
177
178static void
179bcmrng_get_cb(size_t bytes_wanted, void *arg)
180{
181	struct bcm2835rng_softc *sc = arg;
182
183	/*
184	 * Deferring to a softint is necessary until the rnd(9) locking
185	 * is fixed.
186	 */
187	mutex_spin_enter(&sc->sc_intr_lock);
188	if (sc->sc_bytes_wanted == 0)
189		softint_schedule(sc->sc_sih);
190	if (bytes_wanted > (UINT_MAX - sc->sc_bytes_wanted))
191		sc->sc_bytes_wanted = UINT_MAX;
192	else
193		sc->sc_bytes_wanted += bytes_wanted;
194	mutex_spin_exit(&sc->sc_intr_lock);
195}
196
197static void
198bcmrng_get_intr(void *arg)
199{
200	struct bcm2835rng_softc *const sc = arg;
201
202	bcmrng_get(sc);
203}
204