piixpcib.c revision 1.5 1 /* $NetBSD: piixpcib.c,v 1.5 2006/06/19 02:30:35 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2004, 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Minoura Makoto, Matthew R. Green, and Jared D. McNeill.
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Intel PIIX4 PCI-ISA bridge device driver with CPU frequency scaling support
41 *
42 * Based on the FreeBSD 'smist' cpufreq driver by Bruno Ducrot
43 */
44
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: piixpcib.c,v 1.5 2006/06/19 02:30:35 jmcneill Exp $");
47
48 #include <sys/types.h>
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/device.h>
52 #include <sys/sysctl.h>
53 #include <machine/bus.h>
54
55 #include <machine/frame.h>
56 #include <machine/bioscall.h>
57
58 #include <dev/pci/pcivar.h>
59 #include <dev/pci/pcireg.h>
60 #include <dev/pci/pcidevs.h>
61
62 #define PIIX4_PIRQRCA 0x60
63 #define PIIX4_PIRQRCB 0x61
64 #define PIIX4_PIRQRCC 0x62
65 #define PIIX4_PIRQRCD 0x63
66
67 struct piixpcib_softc {
68 struct device sc_dev;
69
70 pci_chipset_tag_t sc_pc;
71 pcitag_t sc_pcitag;
72
73 int sc_smi_cmd;
74 int sc_smi_data;
75 int sc_command;
76 int sc_flags;
77
78 void *sc_powerhook;
79 struct pci_conf_state sc_pciconf;
80 };
81
82 static int piixpcibmatch(struct device *, struct cfdata *, void *);
83 static void piixpcibattach(struct device *, struct device *, void *);
84
85 static void piixpcib_powerhook(int, void *);
86
87 static void speedstep_configure(struct piixpcib_softc *,
88 struct pci_attach_args *);
89 static int speedstep_sysctl_helper(SYSCTLFN_ARGS);
90
91 struct piixpcib_softc *speedstep_cookie; /* XXX */
92
93 /* Defined in arch/i386/pci/pcib.c. */
94 extern void pcibattach(struct device *, struct device *, void *);
95
96 CFATTACH_DECL(piixpcib, sizeof(struct piixpcib_softc),
97 piixpcibmatch, piixpcibattach, NULL, NULL);
98
99 /*
100 * Autoconf callbacks.
101 */
102 static int
103 piixpcibmatch(struct device *parent, struct cfdata *match, void *aux)
104 {
105 struct pci_attach_args *pa;
106
107 pa = (struct pci_attach_args *)aux;
108
109 /* We are ISA bridge, of course */
110 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE ||
111 (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA &&
112 PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_MISC)) {
113 return 0;
114 }
115
116 /* Matches only Intel PIIX4 */
117 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL) {
118 switch (PCI_PRODUCT(pa->pa_id)) {
119 case PCI_PRODUCT_INTEL_82371AB_ISA: /* PIIX4 */
120 case PCI_PRODUCT_INTEL_82440MX_PMC: /* PIIX4 in MX440 */
121 return 10;
122 }
123 }
124
125 return 0;
126 }
127
128 static void
129 piixpcibattach(struct device *parent, struct device *self, void *aux)
130 {
131 struct pci_attach_args *pa;
132 struct piixpcib_softc *sc;
133
134 pa = (struct pci_attach_args *)aux;
135 sc = (struct piixpcib_softc *)self;
136
137 sc->sc_pc = pa->pa_pc;
138 sc->sc_pcitag = pa->pa_tag;
139
140 pcibattach(parent, self, aux);
141
142 /* Set up SpeedStep. */
143 speedstep_configure(sc, pa);
144
145 sc->sc_powerhook = powerhook_establish(piixpcib_powerhook, sc);
146 if (sc->sc_powerhook == NULL)
147 aprint_error("%s: can't establish powerhook\n",
148 sc->sc_dev.dv_xname);
149
150 return;
151 }
152
153 static void
154 piixpcib_powerhook(int why, void *opaque)
155 {
156 struct piixpcib_softc *sc;
157 pci_chipset_tag_t pc;
158 pcitag_t tag;
159
160 sc = (struct piixpcib_softc *)opaque;
161 pc = sc->sc_pc;
162 tag = sc->sc_pcitag;
163
164 switch (why) {
165 case PWR_SUSPEND:
166 pci_conf_capture(pc, tag, &sc->sc_pciconf);
167 break;
168 case PWR_RESUME:
169 pci_conf_restore(pc, tag, &sc->sc_pciconf);
170 break;
171 }
172
173 return;
174 }
175
176 /*
177 * Intel PIIX4 (SMI) SpeedStep support.
178 */
179
180 #define PIIXPCIB_GSIC 0x47534943
181 #define PIIXPCIB_GETOWNER 0
182 #define PIIXPCIB_GETSTATE 1
183 #define PIIXPCIB_SETSTATE 2
184 #define PIIXPCIB_GETFREQS 4
185
186 #define PIIXPCIB_SPEEDSTEP_HIGH 0
187 #define PIIXPCIB_SPEEDSTEP_LOW 1
188
189 static void
190 piixpcib_int15_gsic_call(int *sig, int *smicmd, int *cmd, int *smidata, int *flags)
191 {
192 struct bioscallregs regs;
193
194 memset(®s, 0, sizeof(struct bioscallregs));
195 regs.EAX = 0x0000e980; /* IST support */
196 regs.EDX = PIIXPCIB_GSIC;
197 bioscall(0x15, ®s);
198
199 if (regs.EAX == PIIXPCIB_GSIC) {
200 *sig = regs.EAX;
201 *smicmd = regs.EBX & 0xff;
202 *cmd = (regs.EBX >> 16) & 0xff;
203 *smidata = regs.ECX;
204 *flags = regs.EDX;
205 } else
206 *sig = *smicmd = *cmd = *smidata = *flags = -1;
207
208 return;
209 }
210
211 static int
212 piixpcib_set_ownership(struct piixpcib_softc *sc)
213 {
214 int rv;
215 paddr_t pmagic;
216 static char magic[] = "Copyright (c) 1999 Intel Corporation";
217
218 pmagic = vtophys((vaddr_t)magic);
219
220 __asm__ __volatile__(
221 "movl $0, %%edi\n\t"
222 "out %%al, (%%dx)\n"
223 : "=D" (rv)
224 : "a" (sc->sc_command),
225 "b" (0),
226 "c" (0),
227 "d" (sc->sc_smi_cmd),
228 "S" (pmagic)
229 );
230
231 return (rv ? ENXIO : 0);
232 }
233
234 static int
235 piixpcib_getset_state(struct piixpcib_softc *sc, int *state, int function)
236 {
237 int new;
238 int rv;
239 int eax;
240
241 #ifdef DIAGNOSTIC
242 if (function != PIIXPCIB_GETSTATE &&
243 function != PIIXPCIB_SETSTATE) {
244 aprint_error("%s: GSI called with invalid function %d\n",
245 sc->sc_dev.dv_xname, function);
246 return EINVAL;
247 }
248 #endif
249
250 __asm__ __volatile__(
251 "movl $0, %%edi\n\t"
252 "out %%al, (%%dx)\n"
253 : "=a" (eax),
254 "=b" (new),
255 "=D" (rv)
256 : "a" (sc->sc_command),
257 "b" (function),
258 "c" (*state),
259 "d" (sc->sc_smi_cmd),
260 "S" (0)
261 );
262
263 *state = new & 1;
264
265 switch (function) {
266 case PIIXPCIB_GETSTATE:
267 if (eax)
268 return ENXIO;
269 break;
270 case PIIXPCIB_SETSTATE:
271 if (rv)
272 return ENXIO;
273 break;
274 }
275
276 return 0;
277 }
278
279 static int
280 piixpcib_get(struct piixpcib_softc *sc)
281 {
282 int rv;
283 int state;
284
285 state = 0; /* XXX gcc */
286
287 rv = piixpcib_getset_state(sc, &state, PIIXPCIB_GETSTATE);
288 if (rv)
289 return rv;
290
291 return state & 1;
292 }
293
294 static int
295 piixpcib_set(struct piixpcib_softc *sc, int state)
296 {
297 int rv, s;
298 int try;
299
300 if (state != PIIXPCIB_SPEEDSTEP_HIGH &&
301 state != PIIXPCIB_SPEEDSTEP_LOW)
302 return ENXIO;
303 if (piixpcib_get(sc) == state)
304 return 0;
305
306 try = 5;
307
308 s = splhigh();
309
310 do {
311 rv = piixpcib_getset_state(sc, &state, PIIXPCIB_SETSTATE);
312 if (rv)
313 delay(200);
314 } while (rv && --try);
315
316 splx(s);
317
318 return rv;
319 }
320
321 static void
322 speedstep_configure(struct piixpcib_softc *sc, struct pci_attach_args *pa)
323 {
324 const struct sysctlnode *node, *ssnode;
325 int sig, smicmd, cmd, smidata, flags;
326 int rv;
327
328 piixpcib_int15_gsic_call(&sig, &smicmd, &cmd, &smidata, &flags);
329
330 if (sig != -1) {
331 sc->sc_smi_cmd = smicmd;
332 sc->sc_smi_data = smidata;
333 if (cmd == 0x80) {
334 aprint_debug("%s: GSIC returned cmd 0x80, should be 0x82\n",
335 sc->sc_dev.dv_xname);
336 cmd = 0x82;
337 }
338 sc->sc_command = (sig & 0xffffff00) | (cmd & 0xff);
339 sc->sc_flags = flags;
340 } else {
341 /* setup some defaults */
342 sc->sc_smi_cmd = 0xb2;
343 sc->sc_smi_data = 0xb3;
344 sc->sc_command = 0x47534982;
345 sc->sc_flags = 0;
346 }
347
348 if (piixpcib_set_ownership(sc) != 0) {
349 aprint_error("%s: unable to claim ownership from the BIOS\n",
350 sc->sc_dev.dv_xname);
351 return; /* If we can't claim ownership from the BIOS, bail */
352 }
353
354 /* Put in machdep.speedstep_state (0 for low, 1 for high). */
355 if ((rv = sysctl_createv(NULL, 0, NULL, &node,
356 CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
357 NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL)) != 0)
358 goto err;
359
360 /* CTLFLAG_ANYWRITE? kernel option like EST? */
361 if ((rv = sysctl_createv(NULL, 0, &node, &ssnode,
362 CTLFLAG_READWRITE, CTLTYPE_INT, "speedstep_state", NULL,
363 speedstep_sysctl_helper, 0, NULL, 0, CTL_CREATE,
364 CTL_EOL)) != 0)
365 goto err;
366
367 /* XXX save the sc for IO tag/handle */
368 speedstep_cookie = sc;
369
370 aprint_verbose("%s: SpeedStep SMI enabled\n", sc->sc_dev.dv_xname);
371
372 return;
373
374 err:
375 aprint_normal("%s: sysctl_createv failed (rv = %d)\n", __func__, rv);
376
377 return;
378 }
379
380 /*
381 * get/set the SpeedStep state: 0 == low power, 1 == high power.
382 */
383 static int
384 speedstep_sysctl_helper(SYSCTLFN_ARGS)
385 {
386 struct sysctlnode node;
387 struct piixpcib_softc *sc;
388 uint8_t state, state2;
389 int ostate, nstate, error;
390
391 sc = speedstep_cookie;
392 error = 0;
393
394 state = piixpcib_get(sc);
395 if (state == PIIXPCIB_SPEEDSTEP_HIGH)
396 ostate = 1;
397 else
398 ostate = 0;
399 nstate = ostate;
400
401 node = *rnode;
402 node.sysctl_data = &nstate;
403
404 error = sysctl_lookup(SYSCTLFN_CALL(&node));
405 if (error || newp == NULL)
406 goto out;
407
408 /* Only two states are available */
409 if (nstate != 0 && nstate != 1) {
410 error = EINVAL;
411 goto out;
412 }
413
414 state2 = piixpcib_get(sc);
415 if (state2 == PIIXPCIB_SPEEDSTEP_HIGH)
416 ostate = 1;
417 else
418 ostate = 0;
419
420 if (ostate != nstate)
421 {
422 if (nstate == 0)
423 state2 = PIIXPCIB_SPEEDSTEP_LOW;
424 else
425 state2 = PIIXPCIB_SPEEDSTEP_HIGH;
426
427 error = piixpcib_set(sc, state2);
428 }
429 out:
430 return (error);
431 }
432