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