xref: /netbsd-src/sys/dev/pci/amdpm.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: amdpm.c,v 1.42 2020/05/30 10:27:29 jdolecek 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.42 2020/05/30 10:27:29 jdolecek 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
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
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
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 	/* XXX this mutex is IPL_VM because it can be taken by rnd_getmore() */
163 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM);
164 
165 	/* try to attach devices on the smbus */
166 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC8111_ACPI ||
167 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC768_PMC ||
168 	    sc->sc_nforce) {
169 		amdpm_smbus_attach(sc);
170 	}
171 
172 	if (confreg & AMDPM_RNGEN) {
173 		/* Check to see if we can read data from the RNG. */
174 		(void) bus_space_read_4(sc->sc_iot, sc->sc_ioh,
175 		    AMDPM_RNGDATA);
176 		for (i = 0; i < 1000; i++) {
177 			pmreg = bus_space_read_4(sc->sc_iot,
178 			    sc->sc_ioh, AMDPM_RNGSTAT);
179 			if (pmreg & AMDPM_RNGDONE)
180 				break;
181 			delay(1);
182 		}
183 		if ((pmreg & AMDPM_RNGDONE) != 0) {
184 			aprint_normal_dev(self, ""
185 			    "random number generator enabled (apprx. %dms)\n",
186 			    i);
187 			callout_init(&sc->sc_rnd_ch, CALLOUT_MPSAFE);
188 #ifdef AMDPM_RND_COUNTERS
189 			evcnt_attach_dynamic(&sc->sc_rnd_hits, EVCNT_TYPE_MISC,
190 			    NULL, device_xname(self), "rnd hits");
191 			evcnt_attach_dynamic(&sc->sc_rnd_miss, EVCNT_TYPE_MISC,
192 			    NULL, device_xname(self), "rnd miss");
193 			for (i = 0; i < 256; i++) {
194 				evcnt_attach_dynamic(&sc->sc_rnd_data[i],
195 				    EVCNT_TYPE_MISC, NULL, device_xname(self),
196 				    "rnd data");
197 			}
198 #endif
199 			rndsource_setcb(&sc->sc_rnd_source,
200 					amdpm_rnd_get, sc);
201 			rnd_attach_source(&sc->sc_rnd_source,
202 			    device_xname(self), RND_TYPE_RNG,
203 			    RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
204 		}
205 	}
206 }
207 
208 CFATTACH_DECL_NEW(amdpm, sizeof(struct amdpm_softc),
209     amdpm_match, amdpm_attach, NULL, NULL);
210 
211 static void
212 amdpm_rnd_callout_locked(void *v)
213 {
214 	struct amdpm_softc *sc = v;
215 	u_int32_t rngreg;
216 #ifdef AMDPM_RND_COUNTERS
217 	int i;
218 #endif
219 
220 	if (sc->sc_rnd_need < 1) {
221 		callout_stop(&sc->sc_rnd_ch);
222 		return;
223 	}
224 
225 	if ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGSTAT) &
226 	    AMDPM_RNGDONE) != 0) {
227 		rngreg = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
228 		    AMDPM_RNGDATA);
229 		rnd_add_data(&sc->sc_rnd_source, &rngreg,
230 		    sizeof(rngreg), sizeof(rngreg) * NBBY);
231 		sc->sc_rnd_need -= sizeof(rngreg);
232 #ifdef AMDPM_RND_COUNTERS
233 		AMDPM_RNDCNT_INCR(&sc->sc_rnd_hits);
234 		for (i = 0; i < sizeof(rngreg); i++, rngreg >>= NBBY)
235 			AMDPM_RNDCNT_INCR(&sc->sc_rnd_data[rngreg & 0xff]);
236 #endif
237 	}
238 #ifdef AMDPM_RND_COUNTERS
239 	else
240 		AMDPM_RNDCNT_INCR(&sc->sc_rnd_miss);
241 #endif
242 	if (sc->sc_rnd_need > 0) {
243 		callout_reset(&sc->sc_rnd_ch, 1, amdpm_rnd_callout, sc);
244 	}
245 }
246 
247 static void
248 amdpm_rnd_callout(void *v)
249 {
250 	struct amdpm_softc *sc = v;
251 
252 	mutex_enter(&sc->sc_mutex);
253 	amdpm_rnd_callout_locked(v);
254 	mutex_exit(&sc->sc_mutex);
255 }
256