1*844cc63cShannken /* $NetBSD: piixpcib.c,v 1.24 2019/10/18 15:00:15 hannken Exp $ */
23ab993c8Sjmcneill
33ab993c8Sjmcneill /*-
43ab993c8Sjmcneill * Copyright (c) 2004, 2006 The NetBSD Foundation, Inc.
53ab993c8Sjmcneill * All rights reserved.
63ab993c8Sjmcneill *
73ab993c8Sjmcneill * This code is derived from software contributed to The NetBSD Foundation
83ab993c8Sjmcneill * by Minoura Makoto, Matthew R. Green, and Jared D. McNeill.
93ab993c8Sjmcneill *
103ab993c8Sjmcneill * Redistribution and use in source and binary forms, with or without
113ab993c8Sjmcneill * modification, are permitted provided that the following conditions
123ab993c8Sjmcneill * are met:
133ab993c8Sjmcneill * 1. Redistributions of source code must retain the above copyright
143ab993c8Sjmcneill * notice, this list of conditions and the following disclaimer.
153ab993c8Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
163ab993c8Sjmcneill * notice, this list of conditions and the following disclaimer in the
173ab993c8Sjmcneill * documentation and/or other materials provided with the distribution.
183ab993c8Sjmcneill *
193ab993c8Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
203ab993c8Sjmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
213ab993c8Sjmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
223ab993c8Sjmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
233ab993c8Sjmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
243ab993c8Sjmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
253ab993c8Sjmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
263ab993c8Sjmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
273ab993c8Sjmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
283ab993c8Sjmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
293ab993c8Sjmcneill * POSSIBILITY OF SUCH DAMAGE.
303ab993c8Sjmcneill */
313ab993c8Sjmcneill
323ab993c8Sjmcneill /*
333ab993c8Sjmcneill * Intel PIIX4 PCI-ISA bridge device driver with CPU frequency scaling support
343ab993c8Sjmcneill *
353ab993c8Sjmcneill * Based on the FreeBSD 'smist' cpufreq driver by Bruno Ducrot
363ab993c8Sjmcneill */
373ab993c8Sjmcneill
383ab993c8Sjmcneill #include <sys/cdefs.h>
39*844cc63cShannken __KERNEL_RCSID(0, "$NetBSD: piixpcib.c,v 1.24 2019/10/18 15:00:15 hannken Exp $");
403ab993c8Sjmcneill
413ab993c8Sjmcneill #include <sys/types.h>
423ab993c8Sjmcneill #include <sys/param.h>
433ab993c8Sjmcneill #include <sys/systm.h>
443ab993c8Sjmcneill #include <sys/device.h>
453ab993c8Sjmcneill #include <sys/sysctl.h>
4652f5d050Sdyoung #include <sys/bus.h>
473ab993c8Sjmcneill
483ab993c8Sjmcneill #include <machine/frame.h>
493ab993c8Sjmcneill #include <machine/bioscall.h>
50*844cc63cShannken #include <machine/bootinfo.h>
513ab993c8Sjmcneill
523ab993c8Sjmcneill #include <dev/pci/pcivar.h>
533ab993c8Sjmcneill #include <dev/pci/pcireg.h>
543ab993c8Sjmcneill #include <dev/pci/pcidevs.h>
553ab993c8Sjmcneill
56f86d875aSjmcneill #include <i386/pci/piixreg.h>
57d2849ba3Smartin #include <x86/pci/pcibvar.h>
58f86d875aSjmcneill
59f86d875aSjmcneill #define PIIX4_PIRQRC 0x60
60686aef28Sjmcneill
613ab993c8Sjmcneill struct piixpcib_softc {
62d2849ba3Smartin /* we call pcibattach() which assumes our softc starts like this: */
633ab993c8Sjmcneill
64d2849ba3Smartin struct pcib_softc sc_pcib;
65d2849ba3Smartin
66d2849ba3Smartin device_t sc_dev;
67686aef28Sjmcneill
683ab993c8Sjmcneill int sc_smi_cmd;
693ab993c8Sjmcneill int sc_smi_data;
703ab993c8Sjmcneill int sc_command;
713ab993c8Sjmcneill int sc_flags;
72686aef28Sjmcneill
73f86d875aSjmcneill bus_space_tag_t sc_iot;
74f86d875aSjmcneill bus_space_handle_t sc_ioh;
75f86d875aSjmcneill
76f86d875aSjmcneill pcireg_t sc_pirqrc;
77f86d875aSjmcneill uint8_t sc_elcr[2];
783ab993c8Sjmcneill };
793ab993c8Sjmcneill
80ed38b748Sxtraeme static int piixpcibmatch(device_t, cfdata_t, void *);
81ed38b748Sxtraeme static void piixpcibattach(device_t, device_t, void *);
823ab993c8Sjmcneill
83c1b390d4Sdyoung static bool piixpcib_suspend(device_t, const pmf_qual_t *);
84c1b390d4Sdyoung static bool piixpcib_resume(device_t, const pmf_qual_t *);
85686aef28Sjmcneill
863ab993c8Sjmcneill static void speedstep_configure(struct piixpcib_softc *,
8738e973e4Sdyoung const struct pci_attach_args *);
883ab993c8Sjmcneill static int speedstep_sysctl_helper(SYSCTLFN_ARGS);
893ab993c8Sjmcneill
90d2849ba3Smartin static struct piixpcib_softc *speedstep_cookie; /* XXX */
913ab993c8Sjmcneill
92ed38b748Sxtraeme CFATTACH_DECL_NEW(piixpcib, sizeof(struct piixpcib_softc),
933ab993c8Sjmcneill piixpcibmatch, piixpcibattach, NULL, NULL);
943ab993c8Sjmcneill
953ab993c8Sjmcneill /*
963ab993c8Sjmcneill * Autoconf callbacks.
973ab993c8Sjmcneill */
983ab993c8Sjmcneill static int
piixpcibmatch(device_t parent,cfdata_t match,void * aux)99ed38b748Sxtraeme piixpcibmatch(device_t parent, cfdata_t match, void *aux)
1003ab993c8Sjmcneill {
101ed38b748Sxtraeme struct pci_attach_args *pa = aux;
1023ab993c8Sjmcneill
1033ab993c8Sjmcneill /* We are ISA bridge, of course */
1043ab993c8Sjmcneill if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE ||
1050f4d7745Sjdc (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA &&
1060f4d7745Sjdc PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_MISC)) {
1073ab993c8Sjmcneill return 0;
1083ab993c8Sjmcneill }
1093ab993c8Sjmcneill
1103ab993c8Sjmcneill /* Matches only Intel PIIX4 */
1113ab993c8Sjmcneill if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL) {
1123ab993c8Sjmcneill switch (PCI_PRODUCT(pa->pa_id)) {
1133ab993c8Sjmcneill case PCI_PRODUCT_INTEL_82371AB_ISA: /* PIIX4 */
1143ab993c8Sjmcneill case PCI_PRODUCT_INTEL_82440MX_PMC: /* PIIX4 in MX440 */
1153ab993c8Sjmcneill return 10;
1163ab993c8Sjmcneill }
1173ab993c8Sjmcneill }
1183ab993c8Sjmcneill
1193ab993c8Sjmcneill return 0;
1203ab993c8Sjmcneill }
1213ab993c8Sjmcneill
1223ab993c8Sjmcneill static void
piixpcibattach(device_t parent,device_t self,void * aux)123ed38b748Sxtraeme piixpcibattach(device_t parent, device_t self, void *aux)
1243ab993c8Sjmcneill {
125ed38b748Sxtraeme struct pci_attach_args *pa = aux;
126ed38b748Sxtraeme struct piixpcib_softc *sc = device_private(self);
1273ab993c8Sjmcneill
128ed38b748Sxtraeme sc->sc_dev = self;
129f86d875aSjmcneill sc->sc_iot = pa->pa_iot;
130686aef28Sjmcneill
1313ab993c8Sjmcneill pcibattach(parent, self, aux);
1323ab993c8Sjmcneill
1333ab993c8Sjmcneill /* Set up SpeedStep. */
1343ab993c8Sjmcneill speedstep_configure(sc, pa);
1353ab993c8Sjmcneill
136f86d875aSjmcneill /* Map edge/level control registers */
137f86d875aSjmcneill if (bus_space_map(sc->sc_iot, PIIX_REG_ELCR, PIIX_REG_ELCR_SIZE, 0,
138f86d875aSjmcneill &sc->sc_ioh)) {
1390e374985Smsaitoh aprint_error_dev(self,
1400e374985Smsaitoh "can't map edge/level control registers\n");
141f86d875aSjmcneill return;
142f86d875aSjmcneill }
143f86d875aSjmcneill
1444c1d81b2Sjmcneill if (!pmf_device_register(self, piixpcib_suspend, piixpcib_resume))
1454c1d81b2Sjmcneill aprint_error_dev(self, "couldn't establish power handler\n");
146686aef28Sjmcneill }
147686aef28Sjmcneill
1484c1d81b2Sjmcneill static bool
piixpcib_suspend(device_t dv,const pmf_qual_t * qual)149c1b390d4Sdyoung piixpcib_suspend(device_t dv, const pmf_qual_t *qual)
150686aef28Sjmcneill {
1514c1d81b2Sjmcneill struct piixpcib_softc *sc = device_private(dv);
1523c27eb63Sjmcneill
1533c27eb63Sjmcneill /* capture PIRQX route control registers */
154d2849ba3Smartin sc->sc_pirqrc = pci_conf_read(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag,
155d2849ba3Smartin PIIX4_PIRQRC);
156f86d875aSjmcneill
157f86d875aSjmcneill /* capture edge/level control registers */
158f86d875aSjmcneill sc->sc_elcr[0] = bus_space_read_1(sc->sc_iot, sc->sc_ioh, 0);
159f86d875aSjmcneill sc->sc_elcr[1] = bus_space_read_1(sc->sc_iot, sc->sc_ioh, 1);
1604c1d81b2Sjmcneill
1614c1d81b2Sjmcneill return true;
1624c1d81b2Sjmcneill }
1634c1d81b2Sjmcneill
1644c1d81b2Sjmcneill static bool
piixpcib_resume(device_t dv,const pmf_qual_t * qual)165c1b390d4Sdyoung piixpcib_resume(device_t dv, const pmf_qual_t *qual)
1664c1d81b2Sjmcneill {
1674c1d81b2Sjmcneill struct piixpcib_softc *sc = device_private(dv);
1683c27eb63Sjmcneill
1693c27eb63Sjmcneill /* restore PIRQX route control registers */
170d2849ba3Smartin pci_conf_write(sc->sc_pcib.sc_pc, sc->sc_pcib.sc_tag, PIIX4_PIRQRC,
171d2849ba3Smartin sc->sc_pirqrc);
172f86d875aSjmcneill
173f86d875aSjmcneill /* restore edge/level control registers */
174f86d875aSjmcneill bus_space_write_1(sc->sc_iot, sc->sc_ioh, 0, sc->sc_elcr[0]);
175f86d875aSjmcneill bus_space_write_1(sc->sc_iot, sc->sc_ioh, 1, sc->sc_elcr[1]);
176686aef28Sjmcneill
1774c1d81b2Sjmcneill return true;
1783ab993c8Sjmcneill }
1793ab993c8Sjmcneill
1803ab993c8Sjmcneill /*
1813ab993c8Sjmcneill * Intel PIIX4 (SMI) SpeedStep support.
1823ab993c8Sjmcneill */
1833ab993c8Sjmcneill
1843ab993c8Sjmcneill #define PIIXPCIB_GSIC 0x47534943
1853ab993c8Sjmcneill #define PIIXPCIB_GETOWNER 0
1863ab993c8Sjmcneill #define PIIXPCIB_GETSTATE 1
1873ab993c8Sjmcneill #define PIIXPCIB_SETSTATE 2
1883ab993c8Sjmcneill #define PIIXPCIB_GETFREQS 4
1893ab993c8Sjmcneill
1903ab993c8Sjmcneill #define PIIXPCIB_SPEEDSTEP_HIGH 0
1913ab993c8Sjmcneill #define PIIXPCIB_SPEEDSTEP_LOW 1
1923ab993c8Sjmcneill
1933ab993c8Sjmcneill static void
piixpcib_int15_gsic_call(int * sig,int * smicmd,int * cmd,int * smidata,int * flags)1940e374985Smsaitoh piixpcib_int15_gsic_call(int *sig, int *smicmd, int *cmd, int *smidata,
1950e374985Smsaitoh int *flags)
1963ab993c8Sjmcneill {
1973ab993c8Sjmcneill struct bioscallregs regs;
1983ab993c8Sjmcneill
199eca8efa3Smanu /* No bioscall with EFI */
200eca8efa3Smanu if (lookup_bootinfo(BTINFO_EFI) != NULL) {
201eca8efa3Smanu *sig = *smicmd = *cmd = *smidata = *flags = -1;
202eca8efa3Smanu return;
203eca8efa3Smanu }
204eca8efa3Smanu
2053ab993c8Sjmcneill memset(®s, 0, sizeof(struct bioscallregs));
2063ab993c8Sjmcneill regs.EAX = 0x0000e980; /* IST support */
2073ab993c8Sjmcneill regs.EDX = PIIXPCIB_GSIC;
2083ab993c8Sjmcneill bioscall(0x15, ®s);
2093ab993c8Sjmcneill
2103ab993c8Sjmcneill if (regs.EAX == PIIXPCIB_GSIC) {
2113ab993c8Sjmcneill *sig = regs.EAX;
2123ab993c8Sjmcneill *smicmd = regs.EBX & 0xff;
2133ab993c8Sjmcneill *cmd = (regs.EBX >> 16) & 0xff;
2143ab993c8Sjmcneill *smidata = regs.ECX;
2153ab993c8Sjmcneill *flags = regs.EDX;
2163ab993c8Sjmcneill } else
2173ab993c8Sjmcneill *sig = *smicmd = *cmd = *smidata = *flags = -1;
2183ab993c8Sjmcneill
2193ab993c8Sjmcneill return;
2203ab993c8Sjmcneill }
2213ab993c8Sjmcneill
2223ab993c8Sjmcneill static int
piixpcib_set_ownership(struct piixpcib_softc * sc)2233ab993c8Sjmcneill piixpcib_set_ownership(struct piixpcib_softc *sc)
2243ab993c8Sjmcneill {
2253ab993c8Sjmcneill int rv;
226da702eb9Sjym u_long pmagic;
2273ab993c8Sjmcneill static char magic[] = "Copyright (c) 1999 Intel Corporation";
2283ab993c8Sjmcneill
2293ab993c8Sjmcneill pmagic = vtophys((vaddr_t)magic);
2303ab993c8Sjmcneill
2313ab993c8Sjmcneill __asm__ __volatile__(
2323ab993c8Sjmcneill "movl $0, %%edi\n\t"
2333ab993c8Sjmcneill "out %%al, (%%dx)\n"
2343ab993c8Sjmcneill : "=D" (rv)
2353ab993c8Sjmcneill : "a" (sc->sc_command),
2363ab993c8Sjmcneill "b" (0),
2373ab993c8Sjmcneill "c" (0),
2383ab993c8Sjmcneill "d" (sc->sc_smi_cmd),
2393ab993c8Sjmcneill "S" (pmagic)
2403ab993c8Sjmcneill );
2413ab993c8Sjmcneill
2423ab993c8Sjmcneill return (rv ? ENXIO : 0);
2433ab993c8Sjmcneill }
2443ab993c8Sjmcneill
2453ab993c8Sjmcneill static int
piixpcib_getset_state(struct piixpcib_softc * sc,int * state,int function)2463ab993c8Sjmcneill piixpcib_getset_state(struct piixpcib_softc *sc, int *state, int function)
2473ab993c8Sjmcneill {
2483ab993c8Sjmcneill int new;
2493ab993c8Sjmcneill int rv;
2503ab993c8Sjmcneill int eax;
2513ab993c8Sjmcneill
2523ab993c8Sjmcneill #ifdef DIAGNOSTIC
2533ab993c8Sjmcneill if (function != PIIXPCIB_GETSTATE &&
2543ab993c8Sjmcneill function != PIIXPCIB_SETSTATE) {
2550e374985Smsaitoh aprint_error_dev(sc->sc_dev,
2560e374985Smsaitoh "GSI called with invalid function %d\n", function);
2573ab993c8Sjmcneill return EINVAL;
2583ab993c8Sjmcneill }
2593ab993c8Sjmcneill #endif
2603ab993c8Sjmcneill
2613ab993c8Sjmcneill __asm__ __volatile__(
2623ab993c8Sjmcneill "movl $0, %%edi\n\t"
2633ab993c8Sjmcneill "out %%al, (%%dx)\n"
2643ab993c8Sjmcneill : "=a" (eax),
2653ab993c8Sjmcneill "=b" (new),
2663ab993c8Sjmcneill "=D" (rv)
2673ab993c8Sjmcneill : "a" (sc->sc_command),
2683ab993c8Sjmcneill "b" (function),
2693ab993c8Sjmcneill "c" (*state),
2703ab993c8Sjmcneill "d" (sc->sc_smi_cmd),
2713ab993c8Sjmcneill "S" (0)
2723ab993c8Sjmcneill );
2733ab993c8Sjmcneill
2743ab993c8Sjmcneill *state = new & 1;
2753ab993c8Sjmcneill
2763ab993c8Sjmcneill switch (function) {
2773ab993c8Sjmcneill case PIIXPCIB_GETSTATE:
2783ab993c8Sjmcneill if (eax)
2793ab993c8Sjmcneill return ENXIO;
2803ab993c8Sjmcneill break;
2813ab993c8Sjmcneill case PIIXPCIB_SETSTATE:
2823ab993c8Sjmcneill if (rv)
2833ab993c8Sjmcneill return ENXIO;
2843ab993c8Sjmcneill break;
2853ab993c8Sjmcneill }
2863ab993c8Sjmcneill
2873ab993c8Sjmcneill return 0;
2883ab993c8Sjmcneill }
2893ab993c8Sjmcneill
2903ab993c8Sjmcneill static int
piixpcib_get(struct piixpcib_softc * sc)2913ab993c8Sjmcneill piixpcib_get(struct piixpcib_softc *sc)
2923ab993c8Sjmcneill {
2933ab993c8Sjmcneill int rv;
2943ab993c8Sjmcneill int state;
2953ab993c8Sjmcneill
296911a6956Smrg state = 0; /* XXX gcc */
297911a6956Smrg
2983ab993c8Sjmcneill rv = piixpcib_getset_state(sc, &state, PIIXPCIB_GETSTATE);
2993ab993c8Sjmcneill if (rv)
3003ab993c8Sjmcneill return rv;
3013ab993c8Sjmcneill
3023ab993c8Sjmcneill return state & 1;
3033ab993c8Sjmcneill }
3043ab993c8Sjmcneill
3053ab993c8Sjmcneill static int
piixpcib_set(struct piixpcib_softc * sc,int state)3063ab993c8Sjmcneill piixpcib_set(struct piixpcib_softc *sc, int state)
3073ab993c8Sjmcneill {
3083ab993c8Sjmcneill int rv, s;
3093ab993c8Sjmcneill int try;
3103ab993c8Sjmcneill
3113ab993c8Sjmcneill if (state != PIIXPCIB_SPEEDSTEP_HIGH &&
3123ab993c8Sjmcneill state != PIIXPCIB_SPEEDSTEP_LOW)
3133ab993c8Sjmcneill return ENXIO;
3143ab993c8Sjmcneill if (piixpcib_get(sc) == state)
3153ab993c8Sjmcneill return 0;
3163ab993c8Sjmcneill
3173ab993c8Sjmcneill try = 5;
3183ab993c8Sjmcneill
3193ab993c8Sjmcneill s = splhigh();
3203ab993c8Sjmcneill
3213ab993c8Sjmcneill do {
3223ab993c8Sjmcneill rv = piixpcib_getset_state(sc, &state, PIIXPCIB_SETSTATE);
3233ab993c8Sjmcneill if (rv)
3243ab993c8Sjmcneill delay(200);
3253ab993c8Sjmcneill } while (rv && --try);
3263ab993c8Sjmcneill
3273ab993c8Sjmcneill splx(s);
3283ab993c8Sjmcneill
3293ab993c8Sjmcneill return rv;
3303ab993c8Sjmcneill }
3313ab993c8Sjmcneill
3323ab993c8Sjmcneill static void
speedstep_configure(struct piixpcib_softc * sc,const struct pci_attach_args * pa)3334d595fd7Schristos speedstep_configure(struct piixpcib_softc *sc,
33438e973e4Sdyoung const struct pci_attach_args *pa)
3353ab993c8Sjmcneill {
3363ab993c8Sjmcneill const struct sysctlnode *node, *ssnode;
3373ab993c8Sjmcneill int sig, smicmd, cmd, smidata, flags;
3383ab993c8Sjmcneill int rv;
3393ab993c8Sjmcneill
3403ab993c8Sjmcneill piixpcib_int15_gsic_call(&sig, &smicmd, &cmd, &smidata, &flags);
3413ab993c8Sjmcneill
3423ab993c8Sjmcneill if (sig != -1) {
3433ab993c8Sjmcneill sc->sc_smi_cmd = smicmd;
3443ab993c8Sjmcneill sc->sc_smi_data = smidata;
3453ab993c8Sjmcneill if (cmd == 0x80) {
3460e374985Smsaitoh aprint_debug_dev(sc->sc_dev,
3470e374985Smsaitoh "GSIC returned cmd 0x80, should be 0x82\n");
3483ab993c8Sjmcneill cmd = 0x82;
3493ab993c8Sjmcneill }
3503ab993c8Sjmcneill sc->sc_command = (sig & 0xffffff00) | (cmd & 0xff);
3513ab993c8Sjmcneill sc->sc_flags = flags;
3523ab993c8Sjmcneill } else {
3533ab993c8Sjmcneill /* setup some defaults */
3543ab993c8Sjmcneill sc->sc_smi_cmd = 0xb2;
3553ab993c8Sjmcneill sc->sc_smi_data = 0xb3;
3563ab993c8Sjmcneill sc->sc_command = 0x47534982;
3573ab993c8Sjmcneill sc->sc_flags = 0;
3583ab993c8Sjmcneill }
3593ab993c8Sjmcneill
3603ab993c8Sjmcneill if (piixpcib_set_ownership(sc) != 0) {
3610e374985Smsaitoh aprint_error_dev(sc->sc_dev,
3620e374985Smsaitoh "unable to claim ownership from the BIOS\n");
3633ab993c8Sjmcneill return; /* If we can't claim ownership from the BIOS, bail */
3643ab993c8Sjmcneill }
3653ab993c8Sjmcneill
3663ab993c8Sjmcneill /* Put in machdep.speedstep_state (0 for low, 1 for high). */
3673ab993c8Sjmcneill if ((rv = sysctl_createv(NULL, 0, NULL, &node,
3683ab993c8Sjmcneill CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
3693ab993c8Sjmcneill NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL)) != 0)
3703ab993c8Sjmcneill goto err;
3713ab993c8Sjmcneill
3723ab993c8Sjmcneill /* CTLFLAG_ANYWRITE? kernel option like EST? */
3733ab993c8Sjmcneill if ((rv = sysctl_createv(NULL, 0, &node, &ssnode,
3743ab993c8Sjmcneill CTLFLAG_READWRITE, CTLTYPE_INT, "speedstep_state", NULL,
3753ab993c8Sjmcneill speedstep_sysctl_helper, 0, NULL, 0, CTL_CREATE,
3763ab993c8Sjmcneill CTL_EOL)) != 0)
3773ab993c8Sjmcneill goto err;
3783ab993c8Sjmcneill
3793ab993c8Sjmcneill /* XXX save the sc for IO tag/handle */
3803ab993c8Sjmcneill speedstep_cookie = sc;
3813ab993c8Sjmcneill
382ed38b748Sxtraeme aprint_verbose_dev(sc->sc_dev, "SpeedStep SMI enabled\n");
3833ab993c8Sjmcneill return;
3843ab993c8Sjmcneill
3853ab993c8Sjmcneill err:
3863ab993c8Sjmcneill aprint_normal("%s: sysctl_createv failed (rv = %d)\n", __func__, rv);
3873ab993c8Sjmcneill }
3883ab993c8Sjmcneill
3893ab993c8Sjmcneill /*
3903ab993c8Sjmcneill * get/set the SpeedStep state: 0 == low power, 1 == high power.
3913ab993c8Sjmcneill */
3923ab993c8Sjmcneill static int
speedstep_sysctl_helper(SYSCTLFN_ARGS)3933ab993c8Sjmcneill speedstep_sysctl_helper(SYSCTLFN_ARGS)
3943ab993c8Sjmcneill {
3953ab993c8Sjmcneill struct sysctlnode node;
3963ab993c8Sjmcneill struct piixpcib_softc *sc;
3973ab993c8Sjmcneill uint8_t state, state2;
3983ab993c8Sjmcneill int ostate, nstate, error;
3993ab993c8Sjmcneill
4003ab993c8Sjmcneill sc = speedstep_cookie;
4013ab993c8Sjmcneill error = 0;
4023ab993c8Sjmcneill
4033ab993c8Sjmcneill state = piixpcib_get(sc);
4043ab993c8Sjmcneill if (state == PIIXPCIB_SPEEDSTEP_HIGH)
4053ab993c8Sjmcneill ostate = 1;
4063ab993c8Sjmcneill else
4073ab993c8Sjmcneill ostate = 0;
4083ab993c8Sjmcneill nstate = ostate;
4093ab993c8Sjmcneill
4103ab993c8Sjmcneill node = *rnode;
4113ab993c8Sjmcneill node.sysctl_data = &nstate;
4123ab993c8Sjmcneill
4133ab993c8Sjmcneill error = sysctl_lookup(SYSCTLFN_CALL(&node));
4143ab993c8Sjmcneill if (error || newp == NULL)
4153ab993c8Sjmcneill goto out;
4163ab993c8Sjmcneill
4173ab993c8Sjmcneill /* Only two states are available */
4183ab993c8Sjmcneill if (nstate != 0 && nstate != 1) {
4193ab993c8Sjmcneill error = EINVAL;
4203ab993c8Sjmcneill goto out;
4213ab993c8Sjmcneill }
4223ab993c8Sjmcneill
4233ab993c8Sjmcneill state2 = piixpcib_get(sc);
4243ab993c8Sjmcneill if (state2 == PIIXPCIB_SPEEDSTEP_HIGH)
4253ab993c8Sjmcneill ostate = 1;
4263ab993c8Sjmcneill else
4273ab993c8Sjmcneill ostate = 0;
4283ab993c8Sjmcneill
4293ab993c8Sjmcneill if (ostate != nstate)
4303ab993c8Sjmcneill {
4313ab993c8Sjmcneill if (nstate == 0)
4323ab993c8Sjmcneill state2 = PIIXPCIB_SPEEDSTEP_LOW;
4333ab993c8Sjmcneill else
4343ab993c8Sjmcneill state2 = PIIXPCIB_SPEEDSTEP_HIGH;
4353ab993c8Sjmcneill
4363ab993c8Sjmcneill error = piixpcib_set(sc, state2);
4373ab993c8Sjmcneill }
4383ab993c8Sjmcneill out:
4393ab993c8Sjmcneill return (error);
4403ab993c8Sjmcneill }
441