1 /*        $NetBSD: amdpm.c,v 1.43 2022/03/19 11:37:06 riastradh Exp $ */
2 
3 /*-
4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Enami Tsugutomo.
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: amdpm.c,v 1.43 2022/03/19 11:37:06 riastradh Exp $");
34 
35 #include "opt_amdpm.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 #include <sys/callout.h>
42 #include <sys/rndsource.h>
43 #include <sys/mutex.h>
44 
45 #include <sys/bus.h>
46 #include <dev/ic/acpipmtimer.h>
47 
48 #include <dev/i2c/i2cvar.h>
49 
50 #include <dev/pci/pcivar.h>
51 #include <dev/pci/pcireg.h>
52 #include <dev/pci/pcidevs.h>
53 
54 #include <dev/pci/amdpmreg.h>
55 #include <dev/pci/amdpmvar.h>
56 #include <dev/pci/amdpm_smbusreg.h>
57 
58 static void         amdpm_rnd_callout(void *);
59 static void         amdpm_rnd_callout_locked(void *);
60 
61 #ifdef AMDPM_RND_COUNTERS
62 #define   AMDPM_RNDCNT_INCR(ev)         (ev)->ev_count++
63 #endif
64 
65 static int
amdpm_match(device_t parent,cfdata_t match,void * aux)66 amdpm_match(device_t parent, cfdata_t match, void *aux)
67 {
68           struct pci_attach_args *pa = aux;
69 
70           if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD) {
71                     switch (PCI_PRODUCT(pa->pa_id)) {
72                     case PCI_PRODUCT_AMD_PBC768_PMC:
73                     case PCI_PRODUCT_AMD_PBC8111_ACPI:
74                               return (1);
75                     }
76           }
77           if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NVIDIA) {
78                     switch (PCI_PRODUCT(pa->pa_id)) {
79                     case PCI_PRODUCT_NVIDIA_XBOX_SMBUS:
80                               return (1);
81                     }
82           }
83 
84           return (0);
85 }
86 
87 static void
amdpm_rnd_get(size_t bytes,void * priv)88 amdpm_rnd_get(size_t bytes, void *priv)
89 {
90           struct amdpm_softc *sc = priv;
91 
92           mutex_enter(&sc->sc_mutex);
93           sc->sc_rnd_need = bytes;
94           amdpm_rnd_callout_locked(sc);
95           mutex_exit(&sc->sc_mutex);
96 }
97 
98 static void
amdpm_attach(device_t parent,device_t self,void * aux)99 amdpm_attach(device_t parent, device_t self, void *aux)
100 {
101           struct amdpm_softc *sc = device_private(self);
102           struct pci_attach_args *pa = aux;
103           pcireg_t confreg, pmptrreg;
104           u_int32_t pmreg;
105           int i;
106 
107           pci_aprint_devinfo(pa, NULL);
108 
109           sc->sc_dev = self;
110 
111           if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NVIDIA_XBOX_SMBUS)
112                     sc->sc_nforce = 1;
113           else
114                     sc->sc_nforce = 0;
115 
116           sc->sc_pc = pa->pa_pc;
117           sc->sc_tag = pa->pa_tag;
118           sc->sc_iot = pa->pa_iot;
119           sc->sc_pa = pa;
120 
121           confreg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
122           /* enable pm i/o space for AMD-8111 and nForce */
123           if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC8111_ACPI ||
124               sc->sc_nforce)
125                     confreg |= AMDPM_PMIOEN;
126 
127           /* Enable random number generation for everyone */
128           pci_conf_write(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG,
129               confreg | AMDPM_RNGEN);
130           confreg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
131 
132           if ((confreg & AMDPM_PMIOEN) == 0) {
133                     aprint_error_dev(self, "PMxx space isn't enabled\n");
134                     return;
135           }
136 
137           if (sc->sc_nforce) {
138                     pmptrreg = pci_conf_read(pa->pa_pc, pa->pa_tag, NFORCE_PMPTR);
139                     aprint_normal_dev(self, "power management at 0x%04x\n",
140                         NFORCE_PMBASE(pmptrreg));
141                     if (bus_space_map(sc->sc_iot, NFORCE_PMBASE(pmptrreg),
142                         AMDPM_PMSIZE, 0, &sc->sc_ioh)) {
143                               aprint_error_dev(self, "failed to map PMxx space\n");
144                               return;
145                     }
146           } else {
147                     pmptrreg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_PMPTR);
148                     if (bus_space_map(sc->sc_iot, AMDPM_PMBASE(pmptrreg),
149                         AMDPM_PMSIZE, 0, &sc->sc_ioh)) {
150                               aprint_error_dev(self, "failed to map PMxx space\n");
151                               return;
152                     }
153           }
154 
155           /* don't attach a timecounter on nforce boards */
156           if ((confreg & AMDPM_TMRRST) == 0 && (confreg & AMDPM_STOPTMR) == 0 &&
157               !sc->sc_nforce) {
158                     acpipmtimer_attach(self, sc->sc_iot, sc->sc_ioh,
159                       AMDPM_TMR, ((confreg & AMDPM_TMR32) ? ACPIPMT_32BIT : 0));
160           }
161 
162           mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_SOFTSERIAL);
163 
164           /* try to attach devices on the smbus */
165           if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC8111_ACPI ||
166               PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC768_PMC ||
167               sc->sc_nforce) {
168                     amdpm_smbus_attach(sc);
169           }
170 
171           if (confreg & AMDPM_RNGEN) {
172                     /* Check to see if we can read data from the RNG. */
173                     (void) bus_space_read_4(sc->sc_iot, sc->sc_ioh,
174                         AMDPM_RNGDATA);
175                     for (i = 0; i < 1000; i++) {
176                               pmreg = bus_space_read_4(sc->sc_iot,
177                                   sc->sc_ioh, AMDPM_RNGSTAT);
178                               if (pmreg & AMDPM_RNGDONE)
179                                         break;
180                               delay(1);
181                     }
182                     if ((pmreg & AMDPM_RNGDONE) != 0) {
183                               aprint_normal_dev(self, ""
184                                   "random number generator enabled (apprx. %dms)\n",
185                                   i);
186                               callout_init(&sc->sc_rnd_ch, CALLOUT_MPSAFE);
187 #ifdef AMDPM_RND_COUNTERS
188                               evcnt_attach_dynamic(&sc->sc_rnd_hits, EVCNT_TYPE_MISC,
189                                   NULL, device_xname(self), "rnd hits");
190                               evcnt_attach_dynamic(&sc->sc_rnd_miss, EVCNT_TYPE_MISC,
191                                   NULL, device_xname(self), "rnd miss");
192                               for (i = 0; i < 256; i++) {
193                                         evcnt_attach_dynamic(&sc->sc_rnd_data[i],
194                                             EVCNT_TYPE_MISC, NULL, device_xname(self),
195                                             "rnd data");
196                               }
197 #endif
198                               rndsource_setcb(&sc->sc_rnd_source,
199                                                   amdpm_rnd_get, sc);
200                               rnd_attach_source(&sc->sc_rnd_source,
201                                   device_xname(self), RND_TYPE_RNG,
202                                   RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
203                     }
204           }
205 }
206 
207 CFATTACH_DECL_NEW(amdpm, sizeof(struct amdpm_softc),
208     amdpm_match, amdpm_attach, NULL, NULL);
209 
210 static void
amdpm_rnd_callout_locked(void * v)211 amdpm_rnd_callout_locked(void *v)
212 {
213           struct amdpm_softc *sc = v;
214           u_int32_t rngreg;
215 #ifdef AMDPM_RND_COUNTERS
216           int i;
217 #endif
218 
219           if (sc->sc_rnd_need < 1) {
220                     callout_stop(&sc->sc_rnd_ch);
221                     return;
222           }
223 
224           if ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGSTAT) &
225               AMDPM_RNGDONE) != 0) {
226                     rngreg = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
227                         AMDPM_RNGDATA);
228                     rnd_add_data(&sc->sc_rnd_source, &rngreg,
229                         sizeof(rngreg), sizeof(rngreg) * NBBY);
230                     sc->sc_rnd_need -= sizeof(rngreg);
231 #ifdef AMDPM_RND_COUNTERS
232                     AMDPM_RNDCNT_INCR(&sc->sc_rnd_hits);
233                     for (i = 0; i < sizeof(rngreg); i++, rngreg >>= NBBY)
234                               AMDPM_RNDCNT_INCR(&sc->sc_rnd_data[rngreg & 0xff]);
235 #endif
236           }
237 #ifdef AMDPM_RND_COUNTERS
238           else
239                     AMDPM_RNDCNT_INCR(&sc->sc_rnd_miss);
240 #endif
241           if (sc->sc_rnd_need > 0) {
242                     callout_reset(&sc->sc_rnd_ch, 1, amdpm_rnd_callout, sc);
243           }
244 }
245 
246 static void
amdpm_rnd_callout(void * v)247 amdpm_rnd_callout(void *v)
248 {
249           struct amdpm_softc *sc = v;
250 
251           mutex_enter(&sc->sc_mutex);
252           amdpm_rnd_callout_locked(v);
253           mutex_exit(&sc->sc_mutex);
254 }
255