piixpcib.c revision 1.11 1 /* $NetBSD: piixpcib.c,v 1.11 2007/12/09 20:27:48 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.11 2007/12/09 20:27:48 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 #include <i386/pci/piixreg.h>
63
64 #define PIIX4_PIRQRC 0x60
65
66 struct piixpcib_softc {
67 struct device sc_dev;
68
69 pci_chipset_tag_t sc_pc;
70 pcitag_t sc_pcitag;
71
72 int sc_smi_cmd;
73 int sc_smi_data;
74 int sc_command;
75 int sc_flags;
76
77 bus_space_tag_t sc_iot;
78 bus_space_handle_t sc_ioh;
79
80 pcireg_t sc_pirqrc;
81 uint8_t sc_elcr[2];
82 };
83
84 static int piixpcibmatch(struct device *, struct cfdata *, void *);
85 static void piixpcibattach(struct device *, struct device *, void *);
86
87 static bool piixpcib_suspend(device_t);
88 static bool piixpcib_resume(device_t);
89
90 static void speedstep_configure(struct piixpcib_softc *,
91 struct pci_attach_args *);
92 static int speedstep_sysctl_helper(SYSCTLFN_ARGS);
93
94 struct piixpcib_softc *speedstep_cookie; /* XXX */
95
96 /* Defined in arch/i386/pci/pcib.c. */
97 extern void pcibattach(struct device *, struct device *, void *);
98
99 CFATTACH_DECL(piixpcib, sizeof(struct piixpcib_softc),
100 piixpcibmatch, piixpcibattach, NULL, NULL);
101
102 /*
103 * Autoconf callbacks.
104 */
105 static int
106 piixpcibmatch(struct device *parent, struct cfdata *match,
107 void *aux)
108 {
109 struct pci_attach_args *pa;
110
111 pa = (struct pci_attach_args *)aux;
112
113 /* We are ISA bridge, of course */
114 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE ||
115 (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA &&
116 PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_MISC)) {
117 return 0;
118 }
119
120 /* Matches only Intel PIIX4 */
121 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL) {
122 switch (PCI_PRODUCT(pa->pa_id)) {
123 case PCI_PRODUCT_INTEL_82371AB_ISA: /* PIIX4 */
124 case PCI_PRODUCT_INTEL_82440MX_PMC: /* PIIX4 in MX440 */
125 return 10;
126 }
127 }
128
129 return 0;
130 }
131
132 static void
133 piixpcibattach(struct device *parent, struct device *self, void *aux)
134 {
135 struct pci_attach_args *pa;
136 struct piixpcib_softc *sc;
137
138 pa = (struct pci_attach_args *)aux;
139 sc = (struct piixpcib_softc *)self;
140
141 sc->sc_pc = pa->pa_pc;
142 sc->sc_pcitag = pa->pa_tag;
143 sc->sc_iot = pa->pa_iot;
144
145 pcibattach(parent, self, aux);
146
147 /* Set up SpeedStep. */
148 speedstep_configure(sc, pa);
149
150 /* Map edge/level control registers */
151 if (bus_space_map(sc->sc_iot, PIIX_REG_ELCR, PIIX_REG_ELCR_SIZE, 0,
152 &sc->sc_ioh)) {
153 aprint_error("%s: can't map edge/level control registers\n",
154 sc->sc_dev.dv_xname);
155 return;
156 }
157
158 if (!pmf_device_register(self, piixpcib_suspend, piixpcib_resume))
159 aprint_error_dev(self, "couldn't establish power handler\n");
160
161 return;
162 }
163
164 static bool
165 piixpcib_suspend(device_t dv)
166 {
167 struct piixpcib_softc *sc = device_private(dv);
168
169 /* capture PIRQX route control registers */
170 sc->sc_pirqrc = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PIIX4_PIRQRC);
171
172 /* capture edge/level control registers */
173 sc->sc_elcr[0] = bus_space_read_1(sc->sc_iot, sc->sc_ioh, 0);
174 sc->sc_elcr[1] = bus_space_read_1(sc->sc_iot, sc->sc_ioh, 1);
175
176 return true;
177 }
178
179 static bool
180 piixpcib_resume(device_t dv)
181 {
182 struct piixpcib_softc *sc = device_private(dv);
183
184 /* restore PIRQX route control registers */
185 pci_conf_write(sc->sc_pc, sc->sc_pcitag, PIIX4_PIRQRC, sc->sc_pirqrc);
186
187 /* restore edge/level control registers */
188 bus_space_write_1(sc->sc_iot, sc->sc_ioh, 0, sc->sc_elcr[0]);
189 bus_space_write_1(sc->sc_iot, sc->sc_ioh, 1, sc->sc_elcr[1]);
190
191 return true;
192 }
193
194 /*
195 * Intel PIIX4 (SMI) SpeedStep support.
196 */
197
198 #define PIIXPCIB_GSIC 0x47534943
199 #define PIIXPCIB_GETOWNER 0
200 #define PIIXPCIB_GETSTATE 1
201 #define PIIXPCIB_SETSTATE 2
202 #define PIIXPCIB_GETFREQS 4
203
204 #define PIIXPCIB_SPEEDSTEP_HIGH 0
205 #define PIIXPCIB_SPEEDSTEP_LOW 1
206
207 static void
208 piixpcib_int15_gsic_call(int *sig, int *smicmd, int *cmd, int *smidata, int *flags)
209 {
210 struct bioscallregs regs;
211
212 memset(®s, 0, sizeof(struct bioscallregs));
213 regs.EAX = 0x0000e980; /* IST support */
214 regs.EDX = PIIXPCIB_GSIC;
215 bioscall(0x15, ®s);
216
217 if (regs.EAX == PIIXPCIB_GSIC) {
218 *sig = regs.EAX;
219 *smicmd = regs.EBX & 0xff;
220 *cmd = (regs.EBX >> 16) & 0xff;
221 *smidata = regs.ECX;
222 *flags = regs.EDX;
223 } else
224 *sig = *smicmd = *cmd = *smidata = *flags = -1;
225
226 return;
227 }
228
229 static int
230 piixpcib_set_ownership(struct piixpcib_softc *sc)
231 {
232 int rv;
233 paddr_t pmagic;
234 static char magic[] = "Copyright (c) 1999 Intel Corporation";
235
236 pmagic = vtophys((vaddr_t)magic);
237
238 __asm__ __volatile__(
239 "movl $0, %%edi\n\t"
240 "out %%al, (%%dx)\n"
241 : "=D" (rv)
242 : "a" (sc->sc_command),
243 "b" (0),
244 "c" (0),
245 "d" (sc->sc_smi_cmd),
246 "S" (pmagic)
247 );
248
249 return (rv ? ENXIO : 0);
250 }
251
252 static int
253 piixpcib_getset_state(struct piixpcib_softc *sc, int *state, int function)
254 {
255 int new;
256 int rv;
257 int eax;
258
259 #ifdef DIAGNOSTIC
260 if (function != PIIXPCIB_GETSTATE &&
261 function != PIIXPCIB_SETSTATE) {
262 aprint_error("%s: GSI called with invalid function %d\n",
263 sc->sc_dev.dv_xname, function);
264 return EINVAL;
265 }
266 #endif
267
268 __asm__ __volatile__(
269 "movl $0, %%edi\n\t"
270 "out %%al, (%%dx)\n"
271 : "=a" (eax),
272 "=b" (new),
273 "=D" (rv)
274 : "a" (sc->sc_command),
275 "b" (function),
276 "c" (*state),
277 "d" (sc->sc_smi_cmd),
278 "S" (0)
279 );
280
281 *state = new & 1;
282
283 switch (function) {
284 case PIIXPCIB_GETSTATE:
285 if (eax)
286 return ENXIO;
287 break;
288 case PIIXPCIB_SETSTATE:
289 if (rv)
290 return ENXIO;
291 break;
292 }
293
294 return 0;
295 }
296
297 static int
298 piixpcib_get(struct piixpcib_softc *sc)
299 {
300 int rv;
301 int state;
302
303 state = 0; /* XXX gcc */
304
305 rv = piixpcib_getset_state(sc, &state, PIIXPCIB_GETSTATE);
306 if (rv)
307 return rv;
308
309 return state & 1;
310 }
311
312 static int
313 piixpcib_set(struct piixpcib_softc *sc, int state)
314 {
315 int rv, s;
316 int try;
317
318 if (state != PIIXPCIB_SPEEDSTEP_HIGH &&
319 state != PIIXPCIB_SPEEDSTEP_LOW)
320 return ENXIO;
321 if (piixpcib_get(sc) == state)
322 return 0;
323
324 try = 5;
325
326 s = splhigh();
327
328 do {
329 rv = piixpcib_getset_state(sc, &state, PIIXPCIB_SETSTATE);
330 if (rv)
331 delay(200);
332 } while (rv && --try);
333
334 splx(s);
335
336 return rv;
337 }
338
339 static void
340 speedstep_configure(struct piixpcib_softc *sc,
341 struct pci_attach_args *pa)
342 {
343 const struct sysctlnode *node, *ssnode;
344 int sig, smicmd, cmd, smidata, flags;
345 int rv;
346
347 piixpcib_int15_gsic_call(&sig, &smicmd, &cmd, &smidata, &flags);
348
349 if (sig != -1) {
350 sc->sc_smi_cmd = smicmd;
351 sc->sc_smi_data = smidata;
352 if (cmd == 0x80) {
353 aprint_debug("%s: GSIC returned cmd 0x80, should be 0x82\n",
354 sc->sc_dev.dv_xname);
355 cmd = 0x82;
356 }
357 sc->sc_command = (sig & 0xffffff00) | (cmd & 0xff);
358 sc->sc_flags = flags;
359 } else {
360 /* setup some defaults */
361 sc->sc_smi_cmd = 0xb2;
362 sc->sc_smi_data = 0xb3;
363 sc->sc_command = 0x47534982;
364 sc->sc_flags = 0;
365 }
366
367 if (piixpcib_set_ownership(sc) != 0) {
368 aprint_error("%s: unable to claim ownership from the BIOS\n",
369 sc->sc_dev.dv_xname);
370 return; /* If we can't claim ownership from the BIOS, bail */
371 }
372
373 /* Put in machdep.speedstep_state (0 for low, 1 for high). */
374 if ((rv = sysctl_createv(NULL, 0, NULL, &node,
375 CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
376 NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL)) != 0)
377 goto err;
378
379 /* CTLFLAG_ANYWRITE? kernel option like EST? */
380 if ((rv = sysctl_createv(NULL, 0, &node, &ssnode,
381 CTLFLAG_READWRITE, CTLTYPE_INT, "speedstep_state", NULL,
382 speedstep_sysctl_helper, 0, NULL, 0, CTL_CREATE,
383 CTL_EOL)) != 0)
384 goto err;
385
386 /* XXX save the sc for IO tag/handle */
387 speedstep_cookie = sc;
388
389 aprint_verbose("%s: SpeedStep SMI enabled\n", sc->sc_dev.dv_xname);
390
391 return;
392
393 err:
394 aprint_normal("%s: sysctl_createv failed (rv = %d)\n", __func__, rv);
395
396 return;
397 }
398
399 /*
400 * get/set the SpeedStep state: 0 == low power, 1 == high power.
401 */
402 static int
403 speedstep_sysctl_helper(SYSCTLFN_ARGS)
404 {
405 struct sysctlnode node;
406 struct piixpcib_softc *sc;
407 uint8_t state, state2;
408 int ostate, nstate, error;
409
410 sc = speedstep_cookie;
411 error = 0;
412
413 state = piixpcib_get(sc);
414 if (state == PIIXPCIB_SPEEDSTEP_HIGH)
415 ostate = 1;
416 else
417 ostate = 0;
418 nstate = ostate;
419
420 node = *rnode;
421 node.sysctl_data = &nstate;
422
423 error = sysctl_lookup(SYSCTLFN_CALL(&node));
424 if (error || newp == NULL)
425 goto out;
426
427 /* Only two states are available */
428 if (nstate != 0 && nstate != 1) {
429 error = EINVAL;
430 goto out;
431 }
432
433 state2 = piixpcib_get(sc);
434 if (state2 == PIIXPCIB_SPEEDSTEP_HIGH)
435 ostate = 1;
436 else
437 ostate = 0;
438
439 if (ostate != nstate)
440 {
441 if (nstate == 0)
442 state2 = PIIXPCIB_SPEEDSTEP_LOW;
443 else
444 state2 = PIIXPCIB_SPEEDSTEP_HIGH;
445
446 error = piixpcib_set(sc, state2);
447 }
448 out:
449 return (error);
450 }
451