amdccp.c revision 1.4 1 1.4 riastrad /* $NetBSD: amdccp.c,v 1.4 2022/03/19 11:37:06 riastradh Exp $ */
2 1.1 jakllsch
3 1.1 jakllsch /*
4 1.1 jakllsch * Copyright (c) 2018 Jonathan A. Kollasch
5 1.1 jakllsch * All rights reserved.
6 1.1 jakllsch *
7 1.1 jakllsch * Redistribution and use in source and binary forms, with or without
8 1.1 jakllsch * modification, are permitted provided that the following conditions
9 1.1 jakllsch * are met:
10 1.1 jakllsch * 1. Redistributions of source code must retain the above copyright
11 1.1 jakllsch * notice, this list of conditions and the following disclaimer.
12 1.1 jakllsch * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jakllsch * notice, this list of conditions and the following disclaimer in the
14 1.1 jakllsch * documentation and/or other materials provided with the distribution.
15 1.1 jakllsch *
16 1.1 jakllsch * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 1.1 jakllsch * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jakllsch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jakllsch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 1.1 jakllsch * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 1.1 jakllsch * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 1.1 jakllsch * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 1.1 jakllsch * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 1.1 jakllsch * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 1.1 jakllsch * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 1.1 jakllsch * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 jakllsch */
28 1.1 jakllsch
29 1.1 jakllsch #include <sys/cdefs.h>
30 1.1 jakllsch
31 1.4 riastrad __KERNEL_RCSID(0, "$NetBSD: amdccp.c,v 1.4 2022/03/19 11:37:06 riastradh Exp $");
32 1.1 jakllsch
33 1.1 jakllsch #include <sys/param.h>
34 1.1 jakllsch #include <sys/systm.h>
35 1.1 jakllsch #include <sys/device.h>
36 1.1 jakllsch #include <sys/bus.h>
37 1.1 jakllsch #include <sys/rndsource.h>
38 1.1 jakllsch
39 1.1 jakllsch #include <dev/ic/amdccpvar.h>
40 1.1 jakllsch
41 1.1 jakllsch static uint32_t amdccp_rnd_read(struct amdccp_softc *);
42 1.1 jakllsch static void amdccp_rnd_callback(size_t, void *);
43 1.1 jakllsch
44 1.1 jakllsch void
45 1.1 jakllsch amdccp_common_attach(struct amdccp_softc *sc)
46 1.1 jakllsch {
47 1.4 riastrad
48 1.4 riastrad mutex_init(&sc->sc_rndlock, MUTEX_DEFAULT, IPL_SOFTSERIAL);
49 1.1 jakllsch rndsource_setcb(&sc->sc_rndsource, amdccp_rnd_callback, sc);
50 1.1 jakllsch rnd_attach_source(&sc->sc_rndsource, device_xname(sc->sc_dev),
51 1.1 jakllsch RND_TYPE_RNG, RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
52 1.1 jakllsch }
53 1.1 jakllsch
54 1.1 jakllsch static uint32_t
55 1.1 jakllsch amdccp_rnd_read(struct amdccp_softc *sc)
56 1.1 jakllsch {
57 1.1 jakllsch size_t retries = 5;
58 1.1 jakllsch uint32_t bits;
59 1.1 jakllsch
60 1.1 jakllsch /* TRNG register reads 0x0 when bits aren't valid */
61 1.1 jakllsch while (retries--) {
62 1.1 jakllsch bits = bus_space_read_4(sc->sc_bst, sc->sc_bsh, CCP_TRNG_REG);
63 1.1 jakllsch if (bits != 0x0)
64 1.1 jakllsch break;
65 1.1 jakllsch }
66 1.1 jakllsch
67 1.1 jakllsch return bits;
68 1.1 jakllsch }
69 1.1 jakllsch
70 1.1 jakllsch static void
71 1.1 jakllsch amdccp_rnd_callback(size_t bytes_wanted, void *priv)
72 1.1 jakllsch {
73 1.1 jakllsch struct amdccp_softc * const sc = priv;
74 1.1 jakllsch uint32_t buf[128];
75 1.1 jakllsch size_t cnt;
76 1.1 jakllsch
77 1.1 jakllsch mutex_enter(&sc->sc_rndlock);
78 1.1 jakllsch while (bytes_wanted) {
79 1.1 jakllsch const size_t nbytes = MIN(bytes_wanted, sizeof(buf));
80 1.1 jakllsch const size_t nwords = howmany(nbytes, sizeof(buf[0]));
81 1.3 mrg
82 1.1 jakllsch for (cnt = 0; cnt < nwords; cnt++) {
83 1.1 jakllsch buf[cnt] = amdccp_rnd_read(sc);
84 1.1 jakllsch if (buf[cnt] == 0) {
85 1.1 jakllsch break;
86 1.1 jakllsch }
87 1.1 jakllsch }
88 1.3 mrg if (cnt == 0) {
89 1.3 mrg break;
90 1.3 mrg }
91 1.3 mrg
92 1.1 jakllsch const size_t cntby = cnt * sizeof(buf[0]);
93 1.3 mrg
94 1.1 jakllsch rnd_add_data_sync(&sc->sc_rndsource, buf, cntby, cntby * NBBY);
95 1.1 jakllsch bytes_wanted -= MIN(bytes_wanted, cntby);
96 1.1 jakllsch }
97 1.1 jakllsch explicit_memset(buf, 0, sizeof(buf));
98 1.1 jakllsch mutex_exit(&sc->sc_rndlock);
99 1.1 jakllsch }
100