Home | History | Annotate | Line # | Download | only in acpi
acpi_tz.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: acpi_tz.c,v 1.1 2003/01/04 05:36:03 jmcneill 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.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: acpi_tz.c,v 1.1 2003/01/04 05:36:03 jmcneill 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 int	acpitz_get_integer(struct acpitz_softc *, char *, UINT32 *);
    115  1.1  jmcneill static void	acpitz_tick(void *);
    116  1.1  jmcneill static void	acpitz_init_envsys(struct acpitz_softc *);
    117  1.1  jmcneill static int	acpitz_gtredata(struct sysmon_envsys *,
    118  1.1  jmcneill 				struct envsys_tre_data *);
    119  1.1  jmcneill static int	acpitz_streinfo(struct sysmon_envsys *,
    120  1.1  jmcneill 				struct envsys_basic_info *);
    121  1.1  jmcneill 
    122  1.1  jmcneill CFATTACH_DECL(acpitz, sizeof(struct acpitz_softc), acpitz_match,
    123  1.1  jmcneill     acpitz_attach, NULL, NULL);
    124  1.1  jmcneill 
    125  1.1  jmcneill /*
    126  1.1  jmcneill  * acpitz_match: autoconf(9) match routine
    127  1.1  jmcneill  */
    128  1.1  jmcneill int
    129  1.1  jmcneill acpitz_match(struct device *parent, struct cfdata *match, void *aux)
    130  1.1  jmcneill {
    131  1.1  jmcneill 	struct acpi_attach_args *aa = aux;
    132  1.1  jmcneill 
    133  1.1  jmcneill 	if (aa->aa_node->ad_type != ACPI_TYPE_THERMAL)
    134  1.1  jmcneill 		return 0;
    135  1.1  jmcneill 
    136  1.1  jmcneill 	return 1;
    137  1.1  jmcneill }
    138  1.1  jmcneill 
    139  1.1  jmcneill /*
    140  1.1  jmcneill  * acpitz_attach: autoconf(9) attach routine
    141  1.1  jmcneill  */
    142  1.1  jmcneill void
    143  1.1  jmcneill acpitz_attach(struct device *parent, struct device *self, void *aux)
    144  1.1  jmcneill {
    145  1.1  jmcneill 	struct acpitz_softc *sc = (struct acpitz_softc *)self;
    146  1.1  jmcneill 	struct acpi_attach_args *aa = aux;
    147  1.1  jmcneill 	ACPI_STATUS rv;
    148  1.1  jmcneill 
    149  1.1  jmcneill #if 0
    150  1.1  jmcneill 	sc->sc_flags = ATZ_F_VERBOSE;
    151  1.1  jmcneill #endif
    152  1.1  jmcneill 	sc->sc_devnode = aa->aa_node;
    153  1.1  jmcneill 
    154  1.1  jmcneill 	printf(": ACPI Thermal Zone\n");
    155  1.1  jmcneill 
    156  1.1  jmcneill 	if (acpitz_get_integer(sc, "_TZP", &sc->sc_zone.tzp)) {
    157  1.1  jmcneill 		printf("%s: unable to get poll rate, using default\n",
    158  1.1  jmcneill 		    sc->sc_dev.dv_xname);
    159  1.1  jmcneill 		sc->sc_zone.tzp = ATZ_TZP_RATE;
    160  1.1  jmcneill 	}
    161  1.1  jmcneill 	/* XXX a value of 0 means "polling is not necessary" */
    162  1.1  jmcneill 	if (sc->sc_zone.tzp == 0)
    163  1.1  jmcneill 		sc->sc_zone.tzp = ATZ_TZP_RATE;
    164  1.1  jmcneill 
    165  1.1  jmcneill 	acpitz_get_status(sc);
    166  1.1  jmcneill 
    167  1.1  jmcneill 	rv = AcpiInstallNotifyHandler(sc->sc_devnode->ad_handle,
    168  1.1  jmcneill 	    ACPI_DEVICE_NOTIFY, acpitz_notify_handler, sc);
    169  1.1  jmcneill 	if (rv != AE_OK) {
    170  1.1  jmcneill 		printf("%s: unable to install device notify handler\n",
    171  1.1  jmcneill 		    sc->sc_dev.dv_xname);
    172  1.1  jmcneill 		return;
    173  1.1  jmcneill 	}
    174  1.1  jmcneill 
    175  1.1  jmcneill 	rv = AcpiInstallNotifyHandler(sc->sc_devnode->ad_handle,
    176  1.1  jmcneill 	    ACPI_SYSTEM_NOTIFY, acpitz_notify_handler, sc);
    177  1.1  jmcneill 	if (rv != AE_OK) {
    178  1.1  jmcneill 		printf("%s: unable to install system notify handler\n",
    179  1.1  jmcneill 		    sc->sc_dev.dv_xname);
    180  1.1  jmcneill 		return;
    181  1.1  jmcneill 	}
    182  1.1  jmcneill 
    183  1.1  jmcneill 	callout_init(&sc->sc_callout);
    184  1.1  jmcneill 	callout_reset(&sc->sc_callout, (sc->sc_zone.tzp / 10) * hz,
    185  1.1  jmcneill 	    acpitz_tick, sc);
    186  1.1  jmcneill 
    187  1.1  jmcneill 	acpitz_init_envsys(sc);
    188  1.1  jmcneill }
    189  1.1  jmcneill 
    190  1.1  jmcneill void
    191  1.1  jmcneill acpitz_get_status(void *opaque)
    192  1.1  jmcneill {
    193  1.1  jmcneill 	struct acpitz_softc *sc = opaque;
    194  1.1  jmcneill 
    195  1.1  jmcneill 	acpitz_get_integer(sc, "_TMP", &sc->sc_zone.tmp);
    196  1.1  jmcneill 	sc->sc_data[ATZ_SENSOR_TEMP].cur.data_us = sc->sc_zone.tmp * 100000;
    197  1.1  jmcneill 
    198  1.1  jmcneill 	if (sc->sc_flags & ATZ_F_VERBOSE)
    199  1.1  jmcneill 		acpitz_print_status(sc);
    200  1.1  jmcneill 
    201  1.1  jmcneill 	return;
    202  1.1  jmcneill }
    203  1.1  jmcneill 
    204  1.1  jmcneill static void
    205  1.1  jmcneill acpitz_print_status(struct acpitz_softc *sc)
    206  1.1  jmcneill {
    207  1.1  jmcneill 
    208  1.1  jmcneill 	printf("%s: zone temperature is now %d K\n", sc->sc_dev.dv_xname,
    209  1.1  jmcneill 	    sc->sc_zone.tmp / 10);
    210  1.1  jmcneill 
    211  1.1  jmcneill 	return;
    212  1.1  jmcneill }
    213  1.1  jmcneill 
    214  1.1  jmcneill void
    215  1.1  jmcneill acpitz_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
    216  1.1  jmcneill {
    217  1.1  jmcneill 	struct acpitz_softc *sc = opaque;
    218  1.1  jmcneill 	int rv;
    219  1.1  jmcneill 
    220  1.1  jmcneill 	switch (notify) {
    221  1.1  jmcneill 	case ACPI_NOTIFY_ThermalZoneStatusChanged:
    222  1.1  jmcneill 	case ACPI_NOTIFY_ThermalZoneTripPointsChanged:
    223  1.1  jmcneill 		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
    224  1.1  jmcneill 		    acpitz_get_status, sc);
    225  1.1  jmcneill 		if (rv != AE_OK) {
    226  1.1  jmcneill 			printf("%s: unable to queue status check\n",
    227  1.1  jmcneill 			    sc->sc_dev.dv_xname);
    228  1.1  jmcneill 		}
    229  1.1  jmcneill 		break;
    230  1.1  jmcneill 	default:
    231  1.1  jmcneill 		printf("%s: received unhandled notify message %d\n",
    232  1.1  jmcneill 		    sc->sc_dev.dv_xname, notify);
    233  1.1  jmcneill 		break;
    234  1.1  jmcneill 	}
    235  1.1  jmcneill 
    236  1.1  jmcneill 	return;
    237  1.1  jmcneill }
    238  1.1  jmcneill 
    239  1.1  jmcneill int
    240  1.1  jmcneill acpitz_get_integer(struct acpitz_softc *sc, char *cm, UINT32 *rv)
    241  1.1  jmcneill {
    242  1.1  jmcneill 	ACPI_STATUS status;
    243  1.1  jmcneill 
    244  1.1  jmcneill 	status = acpi_eval_integer(sc->sc_devnode->ad_handle, cm, rv);
    245  1.1  jmcneill 	if (status != AE_OK) {
    246  1.1  jmcneill #ifdef ACPI_DEBUG
    247  1.1  jmcneill 		printf("%s: failed to evalulate %s: %x\n", sc->sc_dev.dv_xname,
    248  1.1  jmcneill 		    cm, status);
    249  1.1  jmcneill #endif
    250  1.1  jmcneill 		return 1;
    251  1.1  jmcneill 	}
    252  1.1  jmcneill 
    253  1.1  jmcneill 	return 0;
    254  1.1  jmcneill }
    255  1.1  jmcneill 
    256  1.1  jmcneill static void
    257  1.1  jmcneill acpitz_tick(void *opaque)
    258  1.1  jmcneill {
    259  1.1  jmcneill 	struct acpitz_softc *sc = opaque;
    260  1.1  jmcneill 
    261  1.1  jmcneill 	callout_reset(&sc->sc_callout, (sc->sc_zone.tzp / 10) * hz,
    262  1.1  jmcneill 	    acpitz_tick, opaque);
    263  1.1  jmcneill 	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpitz_get_status, sc);
    264  1.1  jmcneill 
    265  1.1  jmcneill 	return;
    266  1.1  jmcneill }
    267  1.1  jmcneill 
    268  1.1  jmcneill static void
    269  1.1  jmcneill acpitz_init_envsys(struct acpitz_softc *sc)
    270  1.1  jmcneill {
    271  1.1  jmcneill 	int i;
    272  1.1  jmcneill 
    273  1.1  jmcneill 	simple_lock_init(&sc->sc_slock);
    274  1.1  jmcneill 
    275  1.1  jmcneill 	for (i = 0; i < ATZ_NUMSENSORS; i++) {
    276  1.1  jmcneill 		sc->sc_data[i].sensor = sc->sc_info[i].sensor = i;
    277  1.1  jmcneill 		sc->sc_data[i].validflags = (ENVSYS_FVALID | ENVSYS_FCURVALID);
    278  1.1  jmcneill 		sc->sc_info[i].validflags = ENVSYS_FVALID;
    279  1.1  jmcneill 		sc->sc_data[i].warnflags = ENVSYS_WARN_OK;
    280  1.1  jmcneill 	}
    281  1.1  jmcneill #define INITDATA(index, unit, string) \
    282  1.1  jmcneill 	sc->sc_data[index].units = unit;				   \
    283  1.1  jmcneill 	sc->sc_info[index].units = unit;				   \
    284  1.1  jmcneill 	snprintf(sc->sc_info[index].desc, sizeof(sc->sc_info[index].desc), \
    285  1.1  jmcneill 	    "%s %s", sc->sc_dev.dv_xname, string);
    286  1.1  jmcneill 
    287  1.1  jmcneill 	INITDATA(ATZ_SENSOR_TEMP, ENVSYS_STEMP, "temperature");
    288  1.1  jmcneill 
    289  1.1  jmcneill 	/* hook into sysmon */
    290  1.1  jmcneill 	sc->sc_sysmon.sme_ranges = acpitz_ranges;
    291  1.1  jmcneill 	sc->sc_sysmon.sme_sensor_info = sc->sc_info;
    292  1.1  jmcneill 	sc->sc_sysmon.sme_sensor_data = sc->sc_data;
    293  1.1  jmcneill 	sc->sc_sysmon.sme_cookie = sc;
    294  1.1  jmcneill 	sc->sc_sysmon.sme_gtredata = acpitz_gtredata;
    295  1.1  jmcneill 	sc->sc_sysmon.sme_streinfo = acpitz_streinfo;
    296  1.1  jmcneill 	sc->sc_sysmon.sme_nsensors = ATZ_NUMSENSORS;
    297  1.1  jmcneill 	sc->sc_sysmon.sme_envsys_version = 1000;
    298  1.1  jmcneill 
    299  1.1  jmcneill 	if (sysmon_envsys_register(&sc->sc_sysmon))
    300  1.1  jmcneill 		printf("%s: unable to register with sysmon\n",
    301  1.1  jmcneill 		    sc->sc_dev.dv_xname);
    302  1.1  jmcneill }
    303  1.1  jmcneill 
    304  1.1  jmcneill int
    305  1.1  jmcneill acpitz_gtredata(struct sysmon_envsys *sme, struct envsys_tre_data *tred)
    306  1.1  jmcneill {
    307  1.1  jmcneill 	struct acpitz_softc *sc = sme->sme_cookie;
    308  1.1  jmcneill 
    309  1.1  jmcneill 	simple_lock(&sc->sc_slock);
    310  1.1  jmcneill 
    311  1.1  jmcneill 	*tred = sc->sc_data[tred->sensor];
    312  1.1  jmcneill 
    313  1.1  jmcneill 	simple_unlock(&sc->sc_slock);
    314  1.1  jmcneill 
    315  1.1  jmcneill 	return 0;
    316  1.1  jmcneill }
    317  1.1  jmcneill 
    318  1.1  jmcneill int
    319  1.1  jmcneill acpitz_streinfo(struct sysmon_envsys *sme, struct envsys_basic_info *binfo)
    320  1.1  jmcneill {
    321  1.1  jmcneill 
    322  1.1  jmcneill 	/* XXX not implemented */
    323  1.1  jmcneill 	binfo->validflags = 0;
    324  1.1  jmcneill 
    325  1.1  jmcneill 	return 0;
    326  1.1  jmcneill }
    327