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