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