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