Home | History | Annotate | Line # | Download | only in acpi
acpi_tz.c revision 1.7
      1  1.7     soren /* $NetBSD: acpi_tz.c,v 1.7 2004/02/02 07:58:11 soren Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*
      4  1.1  jmcneill  * Copyright (c) 2003 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. The name of the author may not be used to endorse or promote products
     13  1.1  jmcneill  *    derived from this software without specific prior written permission.
     14  1.1  jmcneill  *
     15  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     20  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     21  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     22  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     23  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  jmcneill  * SUCH DAMAGE.
     26  1.1  jmcneill  */
     27  1.1  jmcneill 
     28  1.1  jmcneill /*
     29  1.1  jmcneill  * ACPI Thermal Zone driver
     30  1.1  jmcneill  */
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/cdefs.h>
     33  1.7     soren __KERNEL_RCSID(0, "$NetBSD: acpi_tz.c,v 1.7 2004/02/02 07:58:11 soren Exp $");
     34  1.1  jmcneill 
     35  1.1  jmcneill #include <sys/param.h>
     36  1.1  jmcneill #include <sys/systm.h>
     37  1.1  jmcneill #include <sys/kernel.h>
     38  1.1  jmcneill #include <sys/errno.h>
     39  1.1  jmcneill #include <sys/ioctl.h>
     40  1.1  jmcneill #include <sys/syslog.h>
     41  1.1  jmcneill #include <sys/device.h>
     42  1.1  jmcneill #include <sys/callout.h>
     43  1.1  jmcneill #include <sys/proc.h>
     44  1.1  jmcneill #include <sys/lock.h>
     45  1.1  jmcneill #include <dev/sysmon/sysmonvar.h>
     46  1.1  jmcneill 
     47  1.1  jmcneill #include <dev/acpi/acpica.h>
     48  1.1  jmcneill #include <dev/acpi/acpireg.h>
     49  1.1  jmcneill #include <dev/acpi/acpivar.h>
     50  1.1  jmcneill 
     51  1.1  jmcneill /* flags */
     52  1.1  jmcneill #define ATZ_F_VERBOSE		0x01	/* show events to console */
     53  1.1  jmcneill 
     54  1.1  jmcneill /* constants */
     55  1.1  jmcneill #define ATZ_TZP_RATE	300	/* default if no _TZP CM present (30 secs) */
     56  1.1  jmcneill #define ATZ_NLEVELS	10	/* number of cooling levels, from ACPI spec */
     57  1.1  jmcneill 
     58  1.1  jmcneill /* sensor indexes */
     59  1.1  jmcneill #define ATZ_SENSOR_TEMP	0	/* thermal zone temperature */
     60  1.1  jmcneill #define ATZ_NUMSENSORS	1	/* number of sensors */
     61  1.1  jmcneill 
     62  1.1  jmcneill const struct envsys_range acpitz_ranges[] = {
     63  1.1  jmcneill 	{ 0, 1,	ATZ_SENSOR_TEMP },
     64  1.1  jmcneill };
     65  1.1  jmcneill 
     66  1.1  jmcneill int	acpitz_match(struct device *, struct cfdata *, void *);
     67  1.1  jmcneill void	acpitz_attach(struct device *, struct device *, void *);
     68  1.1  jmcneill 
     69  1.1  jmcneill /*
     70  1.1  jmcneill  * ACPI Temperature Zone information. Note all temperatures are reported
     71  1.1  jmcneill  * in tenths of degrees Kelvin
     72  1.1  jmcneill  */
     73  1.1  jmcneill struct acpitz_zone {
     74  1.1  jmcneill 	/* Active cooling temperature threshold */
     75  1.1  jmcneill 	UINT32 ac[ATZ_NLEVELS];
     76  1.1  jmcneill 	/* Package of references to all active cooling devices for a level */
     77  1.1  jmcneill 	ACPI_BUFFER al[ATZ_NLEVELS];
     78  1.1  jmcneill 	/* Critical temperature threshold for system shutdown */
     79  1.1  jmcneill 	UINT32 crt;
     80  1.1  jmcneill 	/* Critical temperature threshold for S4 sleep */
     81  1.1  jmcneill 	UINT32 hot;
     82  1.1  jmcneill 	/* Package of references to processor objects for passive cooling */
     83  1.1  jmcneill 	ACPI_BUFFER psl;
     84  1.1  jmcneill 	/* Passive cooling temperature threshold */
     85  1.1  jmcneill 	UINT32 psv;
     86  1.1  jmcneill 	/* Thermal constants for use in passive cooling formulas */
     87  1.1  jmcneill 	UINT32 tc1, tc2;
     88  1.1  jmcneill 	/* Current temperature of the thermal zone */
     89  1.1  jmcneill 	UINT32 tmp;
     90  1.1  jmcneill 	/* Thermal sampling period for passive cooling, in tenths of seconds */
     91  1.1  jmcneill 	UINT32 tsp;
     92  1.1  jmcneill 	/* Package of references to devices in this TZ (optional) */
     93  1.1  jmcneill 	ACPI_BUFFER tzd;
     94  1.1  jmcneill 	/* Recommended TZ polling frequency, in tenths of seconds */
     95  1.1  jmcneill 	UINT32 tzp;
     96  1.1  jmcneill };
     97  1.1  jmcneill 
     98  1.1  jmcneill struct acpitz_softc {
     99  1.1  jmcneill 	struct device sc_dev;
    100  1.1  jmcneill 	struct acpi_devnode *sc_devnode;
    101  1.1  jmcneill 	struct acpitz_zone sc_zone;
    102  1.1  jmcneill 	struct callout sc_callout;
    103  1.1  jmcneill 	struct envsys_tre_data sc_data[ATZ_NUMSENSORS];
    104  1.1  jmcneill 	struct envsys_basic_info sc_info[ATZ_NUMSENSORS];
    105  1.1  jmcneill 	struct sysmon_envsys sc_sysmon;
    106  1.1  jmcneill 	struct simplelock sc_slock;
    107  1.1  jmcneill 	int sc_flags;
    108  1.1  jmcneill 	int sc_rate;		/* tz poll rate */
    109  1.1  jmcneill };
    110  1.1  jmcneill 
    111  1.1  jmcneill void	acpitz_get_status(void *);
    112  1.1  jmcneill static void	acpitz_print_status(struct acpitz_softc *);
    113  1.1  jmcneill void	acpitz_notify_handler(ACPI_HANDLE, UINT32, void *);
    114  1.1  jmcneill static void	acpitz_tick(void *);
    115  1.1  jmcneill static void	acpitz_init_envsys(struct acpitz_softc *);
    116  1.1  jmcneill static int	acpitz_gtredata(struct sysmon_envsys *,
    117  1.1  jmcneill 				struct envsys_tre_data *);
    118  1.1  jmcneill static int	acpitz_streinfo(struct sysmon_envsys *,
    119  1.1  jmcneill 				struct envsys_basic_info *);
    120  1.1  jmcneill 
    121  1.1  jmcneill CFATTACH_DECL(acpitz, sizeof(struct acpitz_softc), acpitz_match,
    122  1.1  jmcneill     acpitz_attach, NULL, NULL);
    123  1.1  jmcneill 
    124  1.1  jmcneill /*
    125  1.1  jmcneill  * acpitz_match: autoconf(9) match routine
    126  1.1  jmcneill  */
    127  1.1  jmcneill int
    128  1.1  jmcneill acpitz_match(struct device *parent, struct cfdata *match, void *aux)
    129  1.1  jmcneill {
    130  1.1  jmcneill 	struct acpi_attach_args *aa = aux;
    131  1.1  jmcneill 
    132  1.1  jmcneill 	if (aa->aa_node->ad_type != ACPI_TYPE_THERMAL)
    133  1.1  jmcneill 		return 0;
    134  1.1  jmcneill 
    135  1.1  jmcneill 	return 1;
    136  1.1  jmcneill }
    137  1.1  jmcneill 
    138  1.1  jmcneill /*
    139  1.1  jmcneill  * acpitz_attach: autoconf(9) attach routine
    140  1.1  jmcneill  */
    141  1.1  jmcneill void
    142  1.1  jmcneill acpitz_attach(struct device *parent, struct device *self, void *aux)
    143  1.1  jmcneill {
    144  1.1  jmcneill 	struct acpitz_softc *sc = (struct acpitz_softc *)self;
    145  1.1  jmcneill 	struct acpi_attach_args *aa = aux;
    146  1.1  jmcneill 	ACPI_STATUS rv;
    147  1.1  jmcneill 
    148  1.1  jmcneill #if 0
    149  1.1  jmcneill 	sc->sc_flags = ATZ_F_VERBOSE;
    150  1.1  jmcneill #endif
    151  1.1  jmcneill 	sc->sc_devnode = aa->aa_node;
    152  1.1  jmcneill 
    153  1.1  jmcneill 	printf(": ACPI Thermal Zone\n");
    154  1.1  jmcneill 
    155  1.6   mycroft 	rv = acpi_eval_integer(sc->sc_devnode->ad_handle, "_TZP",
    156  1.6   mycroft 	    &sc->sc_zone.tzp);
    157  1.6   mycroft 	if (ACPI_FAILURE(rv)) {
    158  1.4   mycroft 		printf("%s: unable to get polling interval; using default of",
    159  1.4   mycroft 		    sc->sc_dev.dv_xname);
    160  1.1  jmcneill 		sc->sc_zone.tzp = ATZ_TZP_RATE;
    161  1.3   mycroft 	} else {
    162  1.4   mycroft 		printf("%s: polling interval is", sc->sc_dev.dv_xname);
    163  1.1  jmcneill 	}
    164  1.4   mycroft 	printf(" %d.%ds\n", sc->sc_zone.tzp / 10, sc->sc_zone.tsp % 10);
    165  1.3   mycroft 
    166  1.1  jmcneill 	/* XXX a value of 0 means "polling is not necessary" */
    167  1.1  jmcneill 	if (sc->sc_zone.tzp == 0)
    168  1.1  jmcneill 		sc->sc_zone.tzp = ATZ_TZP_RATE;
    169  1.1  jmcneill 
    170  1.1  jmcneill 	acpitz_get_status(sc);
    171  1.1  jmcneill 
    172  1.1  jmcneill 	rv = AcpiInstallNotifyHandler(sc->sc_devnode->ad_handle,
    173  1.1  jmcneill 	    ACPI_SYSTEM_NOTIFY, acpitz_notify_handler, sc);
    174  1.6   mycroft 	if (ACPI_FAILURE(rv)) {
    175  1.5   mycroft 		printf("%s: unable to install SYSTEM NOTIFY handler: %s\n",
    176  1.5   mycroft 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    177  1.1  jmcneill 		return;
    178  1.1  jmcneill 	}
    179  1.1  jmcneill 
    180  1.1  jmcneill 	callout_init(&sc->sc_callout);
    181  1.1  jmcneill 	callout_reset(&sc->sc_callout, (sc->sc_zone.tzp / 10) * hz,
    182  1.1  jmcneill 	    acpitz_tick, sc);
    183  1.1  jmcneill 
    184  1.1  jmcneill 	acpitz_init_envsys(sc);
    185  1.1  jmcneill }
    186  1.1  jmcneill 
    187  1.1  jmcneill void
    188  1.1  jmcneill acpitz_get_status(void *opaque)
    189  1.1  jmcneill {
    190  1.1  jmcneill 	struct acpitz_softc *sc = opaque;
    191  1.3   mycroft 	ACPI_STATUS rv;
    192  1.3   mycroft 
    193  1.3   mycroft 	rv = acpi_eval_integer(sc->sc_devnode->ad_handle, "_TMP",
    194  1.3   mycroft 	    &sc->sc_zone.tmp);
    195  1.6   mycroft 	if (ACPI_FAILURE(rv)) {
    196  1.5   mycroft 		printf("%s: failed to evaluate _TMP: %s\n",
    197  1.5   mycroft 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    198  1.3   mycroft 		return;
    199  1.3   mycroft 	}
    200  1.1  jmcneill 
    201  1.7     soren 	/*
    202  1.7     soren 	 * The temperature unit for envsys(9) is microKelvin, so convert to
    203  1.7     soren 	 * that from ACPI's microKelvin. Also, the ACPI specification assumes
    204  1.7     soren 	 * that K = C + 273.2 rather than the nominal 273.16 used by envsys(9),
    205  1.7     soren 	 * so we correct for that too.
    206  1.7     soren 	 */
    207  1.7     soren 	sc->sc_data[ATZ_SENSOR_TEMP].cur.data_us =
    208  1.7     soren 	    sc->sc_zone.tmp * 100000 - 40000;
    209  1.3   mycroft 	sc->sc_data[ATZ_SENSOR_TEMP].validflags |= ENVSYS_FCURVALID;
    210  1.1  jmcneill 
    211  1.1  jmcneill 	if (sc->sc_flags & ATZ_F_VERBOSE)
    212  1.1  jmcneill 		acpitz_print_status(sc);
    213  1.1  jmcneill 
    214  1.1  jmcneill 	return;
    215  1.1  jmcneill }
    216  1.1  jmcneill 
    217  1.1  jmcneill static void
    218  1.1  jmcneill acpitz_print_status(struct acpitz_softc *sc)
    219  1.1  jmcneill {
    220  1.1  jmcneill 
    221  1.1  jmcneill 	printf("%s: zone temperature is now %d K\n", sc->sc_dev.dv_xname,
    222  1.1  jmcneill 	    sc->sc_zone.tmp / 10);
    223  1.1  jmcneill 
    224  1.1  jmcneill 	return;
    225  1.1  jmcneill }
    226  1.1  jmcneill 
    227  1.1  jmcneill void
    228  1.1  jmcneill acpitz_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
    229  1.1  jmcneill {
    230  1.1  jmcneill 	struct acpitz_softc *sc = opaque;
    231  1.1  jmcneill 	int rv;
    232  1.1  jmcneill 
    233  1.1  jmcneill 	switch (notify) {
    234  1.1  jmcneill 	case ACPI_NOTIFY_ThermalZoneStatusChanged:
    235  1.1  jmcneill 	case ACPI_NOTIFY_ThermalZoneTripPointsChanged:
    236  1.1  jmcneill 		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
    237  1.1  jmcneill 		    acpitz_get_status, sc);
    238  1.6   mycroft 		if (ACPI_FAILURE(rv)) {
    239  1.5   mycroft 			printf("%s: unable to queue status check: %s\n",
    240  1.5   mycroft 			    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    241  1.1  jmcneill 		}
    242  1.1  jmcneill 		break;
    243  1.1  jmcneill 	default:
    244  1.5   mycroft 		printf("%s: received unhandled notify message 0x%x\n",
    245  1.1  jmcneill 		    sc->sc_dev.dv_xname, notify);
    246  1.1  jmcneill 		break;
    247  1.1  jmcneill 	}
    248  1.1  jmcneill 
    249  1.1  jmcneill 	return;
    250  1.1  jmcneill }
    251  1.1  jmcneill 
    252  1.1  jmcneill static void
    253  1.1  jmcneill acpitz_tick(void *opaque)
    254  1.1  jmcneill {
    255  1.1  jmcneill 	struct acpitz_softc *sc = opaque;
    256  1.1  jmcneill 
    257  1.1  jmcneill 	callout_reset(&sc->sc_callout, (sc->sc_zone.tzp / 10) * hz,
    258  1.1  jmcneill 	    acpitz_tick, opaque);
    259  1.1  jmcneill 	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpitz_get_status, sc);
    260  1.1  jmcneill 
    261  1.1  jmcneill 	return;
    262  1.1  jmcneill }
    263  1.1  jmcneill 
    264  1.1  jmcneill static void
    265  1.1  jmcneill acpitz_init_envsys(struct acpitz_softc *sc)
    266  1.1  jmcneill {
    267  1.1  jmcneill 	int i;
    268  1.1  jmcneill 
    269  1.1  jmcneill 	simple_lock_init(&sc->sc_slock);
    270  1.1  jmcneill 
    271  1.1  jmcneill 	for (i = 0; i < ATZ_NUMSENSORS; i++) {
    272  1.1  jmcneill 		sc->sc_data[i].sensor = sc->sc_info[i].sensor = i;
    273  1.3   mycroft 		sc->sc_data[i].validflags = ENVSYS_FVALID;
    274  1.1  jmcneill 		sc->sc_info[i].validflags = ENVSYS_FVALID;
    275  1.1  jmcneill 		sc->sc_data[i].warnflags = ENVSYS_WARN_OK;
    276  1.1  jmcneill 	}
    277  1.1  jmcneill #define INITDATA(index, unit, string) \
    278  1.1  jmcneill 	sc->sc_data[index].units = unit;				   \
    279  1.1  jmcneill 	sc->sc_info[index].units = unit;				   \
    280  1.1  jmcneill 	snprintf(sc->sc_info[index].desc, sizeof(sc->sc_info[index].desc), \
    281  1.1  jmcneill 	    "%s %s", sc->sc_dev.dv_xname, string);
    282  1.1  jmcneill 
    283  1.1  jmcneill 	INITDATA(ATZ_SENSOR_TEMP, ENVSYS_STEMP, "temperature");
    284  1.1  jmcneill 
    285  1.1  jmcneill 	/* hook into sysmon */
    286  1.1  jmcneill 	sc->sc_sysmon.sme_ranges = acpitz_ranges;
    287  1.1  jmcneill 	sc->sc_sysmon.sme_sensor_info = sc->sc_info;
    288  1.1  jmcneill 	sc->sc_sysmon.sme_sensor_data = sc->sc_data;
    289  1.1  jmcneill 	sc->sc_sysmon.sme_cookie = sc;
    290  1.1  jmcneill 	sc->sc_sysmon.sme_gtredata = acpitz_gtredata;
    291  1.1  jmcneill 	sc->sc_sysmon.sme_streinfo = acpitz_streinfo;
    292  1.1  jmcneill 	sc->sc_sysmon.sme_nsensors = ATZ_NUMSENSORS;
    293  1.1  jmcneill 	sc->sc_sysmon.sme_envsys_version = 1000;
    294  1.1  jmcneill 
    295  1.1  jmcneill 	if (sysmon_envsys_register(&sc->sc_sysmon))
    296  1.1  jmcneill 		printf("%s: unable to register with sysmon\n",
    297  1.1  jmcneill 		    sc->sc_dev.dv_xname);
    298  1.1  jmcneill }
    299  1.1  jmcneill 
    300  1.1  jmcneill int
    301  1.1  jmcneill acpitz_gtredata(struct sysmon_envsys *sme, struct envsys_tre_data *tred)
    302  1.1  jmcneill {
    303  1.1  jmcneill 	struct acpitz_softc *sc = sme->sme_cookie;
    304  1.1  jmcneill 
    305  1.1  jmcneill 	simple_lock(&sc->sc_slock);
    306  1.1  jmcneill 
    307  1.1  jmcneill 	*tred = sc->sc_data[tred->sensor];
    308  1.1  jmcneill 
    309  1.1  jmcneill 	simple_unlock(&sc->sc_slock);
    310  1.1  jmcneill 
    311  1.1  jmcneill 	return 0;
    312  1.1  jmcneill }
    313  1.1  jmcneill 
    314  1.1  jmcneill int
    315  1.1  jmcneill acpitz_streinfo(struct sysmon_envsys *sme, struct envsys_basic_info *binfo)
    316  1.1  jmcneill {
    317  1.1  jmcneill 
    318  1.1  jmcneill 	/* XXX not implemented */
    319  1.1  jmcneill 	binfo->validflags = 0;
    320  1.1  jmcneill 
    321  1.1  jmcneill 	return 0;
    322  1.1  jmcneill }
    323