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