1 /** $MirOS: src/sys/dev/pci/amdpm.c,v 1.2 2005/03/06 21:27:47 tg Exp $ */
2 /* $OpenBSD: amdpm.c,v 1.3 2002/11/04 17:12:34 fgsch Exp $ */
3
4 /*-
5 * Copyright (c) 2002 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Enami Tsugutomo.
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 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/device.h>
44 #include <sys/timeout.h>
45
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcidevs.h>
49
50 #include <dev/rndvar.h>
51 #include <dev/pci/amdpmreg.h>
52
53 struct amdpm_softc {
54 struct device sc_dev;
55
56 pci_chipset_tag_t sc_pc;
57 pcitag_t sc_tag;
58
59 bus_space_tag_t sc_iot;
60 bus_space_handle_t sc_ioh; /* PMxx space */
61
62 struct timeout sc_rnd_ch;
63 #ifdef AMDPM_RND_COUNTERS
64 struct evcnt sc_rnd_hits;
65 struct evcnt sc_rnd_miss;
66 struct evcnt sc_rnd_data[256];
67 #endif
68 };
69
70 int amdpm_match(struct device *, void *, void *);
71 void amdpm_attach(struct device *, struct device *, void *);
72 void amdpm_rnd_callout(void *);
73
74 struct cfattach amdpm_ca = {
75 sizeof(struct amdpm_softc), amdpm_match, amdpm_attach
76 };
77
78 struct cfdriver amdpm_cd = {
79 NULL, "amdpm", DV_DULL
80 };
81
82 #ifdef AMDPM_RND_COUNTERS
83 #define AMDPM_RNDCNT_INCR(ev) (ev)->ev_count++
84 #else
85 #define AMDPM_RNDCNT_INCR(ev) /* nothing */
86 #endif
87
88 int
amdpm_match(struct device * parent,void * match,void * aux)89 amdpm_match(struct device *parent, void *match, void *aux)
90 {
91 struct pci_attach_args *pa = aux;
92
93 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD &&
94 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC768_PMC)
95 return (1);
96 return (0);
97 }
98
99 void
amdpm_attach(struct device * parent,struct device * self,void * aux)100 amdpm_attach(struct device *parent, struct device *self, void *aux)
101 {
102 struct amdpm_softc *sc = (struct amdpm_softc *) self;
103 struct pci_attach_args *pa = aux;
104 struct timeval tv1, tv2;
105 pcireg_t reg;
106 int i;
107
108 sc->sc_pc = pa->pa_pc;
109 sc->sc_tag = pa->pa_tag;
110 sc->sc_iot = pa->pa_iot;
111
112 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
113 if ((reg & AMDPM_PMIOEN) == 0) {
114 printf(": PMxx space isn't enabled\n");
115 return;
116 }
117 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_PMPTR);
118 if (bus_space_map(sc->sc_iot, AMDPM_PMBASE(reg), AMDPM_PMSIZE,
119 0, &sc->sc_ioh)) {
120 printf(": failed to map PMxx space\n");
121 return;
122 }
123
124 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
125 if (reg & AMDPM_RNGEN) {
126 /* Check to see if we can read data from the RNG. */
127 (void) bus_space_read_4(sc->sc_iot, sc->sc_ioh,
128 AMDPM_RNGDATA);
129 /* benchmark the RNG */
130 microtime(&tv1);
131 for (i = 2 * 1024; i--; ) {
132 while(!(bus_space_read_1(sc->sc_iot, sc->sc_ioh,
133 AMDPM_RNGSTAT) & AMDPM_RNGDONE))
134 ;
135 (void) bus_space_read_4(sc->sc_iot, sc->sc_ioh,
136 AMDPM_RNGDATA);
137 }
138 microtime(&tv2);
139
140 timersub(&tv2, &tv1, &tv1);
141 if (tv1.tv_sec)
142 tv1.tv_usec += 1000000 * tv1.tv_sec;
143 printf(": rng active, %ld Kib/sec", 8 * 1048576 / tv1.tv_usec);
144
145 #ifdef AMDPM_RND_COUNTERS
146 evcnt_attach_dynamic(&sc->sc_rnd_hits, EVCNT_TYPE_MISC,
147 NULL, sc->sc_dev.dv_xname, "rnd hits");
148 evcnt_attach_dynamic(&sc->sc_rnd_miss, EVCNT_TYPE_MISC,
149 NULL, sc->sc_dev.dv_xname, "rnd miss");
150 for (i = 0; i < 256; i++) {
151 evcnt_attach_dynamic(&sc->sc_rnd_data[i],
152 EVCNT_TYPE_MISC, NULL, sc->sc_dev.dv_xname,
153 "rnd data");
154 }
155 #endif
156 timeout_set(&sc->sc_rnd_ch, amdpm_rnd_callout, sc);
157 amdpm_rnd_callout(sc);
158 }
159 }
160
161 void
amdpm_rnd_callout(void * v)162 amdpm_rnd_callout(void *v)
163 {
164 struct amdpm_softc *sc = v;
165 u_int32_t reg;
166 #ifdef AMDPM_RND_COUNTERS
167 int i;
168 #endif
169
170 if ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGSTAT) &
171 AMDPM_RNGDONE) != 0) {
172 reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGDATA);
173 add_true_randomness(reg);
174 #ifdef AMDPM_RND_COUNTERS
175 AMDPM_RNDCNT_INCR(&sc->sc_rnd_hits);
176 for (i = 0; i < sizeof(reg); i++, reg >>= NBBY)
177 AMDPM_RNDCNT_INCR(&sc->sc_rnd_data[reg & 0xff]);
178 #endif
179 } else
180 AMDPM_RNDCNT_INCR(&sc->sc_rnd_miss);
181 timeout_add(&sc->sc_rnd_ch, 1);
182 }
183