piixpcib.c revision 1.13 1 /* $NetBSD: piixpcib.c,v 1.13 2008/04/04 22:48:58 cegger 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.13 2008/04/04 22:48:58 cegger 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 PMF_FN_PROTO);
88 static bool piixpcib_resume(device_t PMF_FN_PROTO);
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_dev(&sc->sc_dev, "can't map edge/level control registers\n");
154 return;
155 }
156
157 if (!pmf_device_register(self, piixpcib_suspend, piixpcib_resume))
158 aprint_error_dev(self, "couldn't establish power handler\n");
159
160 return;
161 }
162
163 static bool
164 piixpcib_suspend(device_t dv PMF_FN_ARGS)
165 {
166 struct piixpcib_softc *sc = device_private(dv);
167
168 /* capture PIRQX route control registers */
169 sc->sc_pirqrc = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PIIX4_PIRQRC);
170
171 /* capture edge/level control registers */
172 sc->sc_elcr[0] = bus_space_read_1(sc->sc_iot, sc->sc_ioh, 0);
173 sc->sc_elcr[1] = bus_space_read_1(sc->sc_iot, sc->sc_ioh, 1);
174
175 return true;
176 }
177
178 static bool
179 piixpcib_resume(device_t dv PMF_FN_ARGS)
180 {
181 struct piixpcib_softc *sc = device_private(dv);
182
183 /* restore PIRQX route control registers */
184 pci_conf_write(sc->sc_pc, sc->sc_pcitag, PIIX4_PIRQRC, sc->sc_pirqrc);
185
186 /* restore edge/level control registers */
187 bus_space_write_1(sc->sc_iot, sc->sc_ioh, 0, sc->sc_elcr[0]);
188 bus_space_write_1(sc->sc_iot, sc->sc_ioh, 1, sc->sc_elcr[1]);
189
190 return true;
191 }
192
193 /*
194 * Intel PIIX4 (SMI) SpeedStep support.
195 */
196
197 #define PIIXPCIB_GSIC 0x47534943
198 #define PIIXPCIB_GETOWNER 0
199 #define PIIXPCIB_GETSTATE 1
200 #define PIIXPCIB_SETSTATE 2
201 #define PIIXPCIB_GETFREQS 4
202
203 #define PIIXPCIB_SPEEDSTEP_HIGH 0
204 #define PIIXPCIB_SPEEDSTEP_LOW 1
205
206 static void
207 piixpcib_int15_gsic_call(int *sig, int *smicmd, int *cmd, int *smidata, int *flags)
208 {
209 struct bioscallregs regs;
210
211 memset(®s, 0, sizeof(struct bioscallregs));
212 regs.EAX = 0x0000e980; /* IST support */
213 regs.EDX = PIIXPCIB_GSIC;
214 bioscall(0x15, ®s);
215
216 if (regs.EAX == PIIXPCIB_GSIC) {
217 *sig = regs.EAX;
218 *smicmd = regs.EBX & 0xff;
219 *cmd = (regs.EBX >> 16) & 0xff;
220 *smidata = regs.ECX;
221 *flags = regs.EDX;
222 } else
223 *sig = *smicmd = *cmd = *smidata = *flags = -1;
224
225 return;
226 }
227
228 static int
229 piixpcib_set_ownership(struct piixpcib_softc *sc)
230 {
231 int rv;
232 paddr_t pmagic;
233 static char magic[] = "Copyright (c) 1999 Intel Corporation";
234
235 pmagic = vtophys((vaddr_t)magic);
236
237 __asm__ __volatile__(
238 "movl $0, %%edi\n\t"
239 "out %%al, (%%dx)\n"
240 : "=D" (rv)
241 : "a" (sc->sc_command),
242 "b" (0),
243 "c" (0),
244 "d" (sc->sc_smi_cmd),
245 "S" (pmagic)
246 );
247
248 return (rv ? ENXIO : 0);
249 }
250
251 static int
252 piixpcib_getset_state(struct piixpcib_softc *sc, int *state, int function)
253 {
254 int new;
255 int rv;
256 int eax;
257
258 #ifdef DIAGNOSTIC
259 if (function != PIIXPCIB_GETSTATE &&
260 function != PIIXPCIB_SETSTATE) {
261 aprint_error_dev(&sc->sc_dev, "GSI called with invalid function %d\n",
262 function);
263 return EINVAL;
264 }
265 #endif
266
267 __asm__ __volatile__(
268 "movl $0, %%edi\n\t"
269 "out %%al, (%%dx)\n"
270 : "=a" (eax),
271 "=b" (new),
272 "=D" (rv)
273 : "a" (sc->sc_command),
274 "b" (function),
275 "c" (*state),
276 "d" (sc->sc_smi_cmd),
277 "S" (0)
278 );
279
280 *state = new & 1;
281
282 switch (function) {
283 case PIIXPCIB_GETSTATE:
284 if (eax)
285 return ENXIO;
286 break;
287 case PIIXPCIB_SETSTATE:
288 if (rv)
289 return ENXIO;
290 break;
291 }
292
293 return 0;
294 }
295
296 static int
297 piixpcib_get(struct piixpcib_softc *sc)
298 {
299 int rv;
300 int state;
301
302 state = 0; /* XXX gcc */
303
304 rv = piixpcib_getset_state(sc, &state, PIIXPCIB_GETSTATE);
305 if (rv)
306 return rv;
307
308 return state & 1;
309 }
310
311 static int
312 piixpcib_set(struct piixpcib_softc *sc, int state)
313 {
314 int rv, s;
315 int try;
316
317 if (state != PIIXPCIB_SPEEDSTEP_HIGH &&
318 state != PIIXPCIB_SPEEDSTEP_LOW)
319 return ENXIO;
320 if (piixpcib_get(sc) == state)
321 return 0;
322
323 try = 5;
324
325 s = splhigh();
326
327 do {
328 rv = piixpcib_getset_state(sc, &state, PIIXPCIB_SETSTATE);
329 if (rv)
330 delay(200);
331 } while (rv && --try);
332
333 splx(s);
334
335 return rv;
336 }
337
338 static void
339 speedstep_configure(struct piixpcib_softc *sc,
340 struct pci_attach_args *pa)
341 {
342 const struct sysctlnode *node, *ssnode;
343 int sig, smicmd, cmd, smidata, flags;
344 int rv;
345
346 piixpcib_int15_gsic_call(&sig, &smicmd, &cmd, &smidata, &flags);
347
348 if (sig != -1) {
349 sc->sc_smi_cmd = smicmd;
350 sc->sc_smi_data = smidata;
351 if (cmd == 0x80) {
352 aprint_debug_dev(&sc->sc_dev, "GSIC returned cmd 0x80, should be 0x82\n");
353 cmd = 0x82;
354 }
355 sc->sc_command = (sig & 0xffffff00) | (cmd & 0xff);
356 sc->sc_flags = flags;
357 } else {
358 /* setup some defaults */
359 sc->sc_smi_cmd = 0xb2;
360 sc->sc_smi_data = 0xb3;
361 sc->sc_command = 0x47534982;
362 sc->sc_flags = 0;
363 }
364
365 if (piixpcib_set_ownership(sc) != 0) {
366 aprint_error_dev(&sc->sc_dev, "unable to claim ownership from the BIOS\n");
367 return; /* If we can't claim ownership from the BIOS, bail */
368 }
369
370 /* Put in machdep.speedstep_state (0 for low, 1 for high). */
371 if ((rv = sysctl_createv(NULL, 0, NULL, &node,
372 CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
373 NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL)) != 0)
374 goto err;
375
376 /* CTLFLAG_ANYWRITE? kernel option like EST? */
377 if ((rv = sysctl_createv(NULL, 0, &node, &ssnode,
378 CTLFLAG_READWRITE, CTLTYPE_INT, "speedstep_state", NULL,
379 speedstep_sysctl_helper, 0, NULL, 0, CTL_CREATE,
380 CTL_EOL)) != 0)
381 goto err;
382
383 /* XXX save the sc for IO tag/handle */
384 speedstep_cookie = sc;
385
386 aprint_verbose_dev(&sc->sc_dev, "SpeedStep SMI enabled\n");
387
388 return;
389
390 err:
391 aprint_normal("%s: sysctl_createv failed (rv = %d)\n", __func__, rv);
392
393 return;
394 }
395
396 /*
397 * get/set the SpeedStep state: 0 == low power, 1 == high power.
398 */
399 static int
400 speedstep_sysctl_helper(SYSCTLFN_ARGS)
401 {
402 struct sysctlnode node;
403 struct piixpcib_softc *sc;
404 uint8_t state, state2;
405 int ostate, nstate, error;
406
407 sc = speedstep_cookie;
408 error = 0;
409
410 state = piixpcib_get(sc);
411 if (state == PIIXPCIB_SPEEDSTEP_HIGH)
412 ostate = 1;
413 else
414 ostate = 0;
415 nstate = ostate;
416
417 node = *rnode;
418 node.sysctl_data = &nstate;
419
420 error = sysctl_lookup(SYSCTLFN_CALL(&node));
421 if (error || newp == NULL)
422 goto out;
423
424 /* Only two states are available */
425 if (nstate != 0 && nstate != 1) {
426 error = EINVAL;
427 goto out;
428 }
429
430 state2 = piixpcib_get(sc);
431 if (state2 == PIIXPCIB_SPEEDSTEP_HIGH)
432 ostate = 1;
433 else
434 ostate = 0;
435
436 if (ostate != nstate)
437 {
438 if (nstate == 0)
439 state2 = PIIXPCIB_SPEEDSTEP_LOW;
440 else
441 state2 = PIIXPCIB_SPEEDSTEP_HIGH;
442
443 error = piixpcib_set(sc, state2);
444 }
445 out:
446 return (error);
447 }
448