Home | History | Annotate | Line # | Download | only in acpi
acpi_tz.c revision 1.65
      1  1.65    jruoho /* $NetBSD: acpi_tz.c,v 1.65 2010/04/15 07:02:24 jruoho 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.65    jruoho __KERNEL_RCSID(0, "$NetBSD: acpi_tz.c,v 1.65 2010/04/15 07:02:24 jruoho Exp $");
     34   1.1  jmcneill 
     35   1.1  jmcneill #include <sys/param.h>
     36   1.1  jmcneill #include <sys/device.h>
     37   1.1  jmcneill #include <sys/callout.h>
     38  1.61    jruoho #include <sys/kernel.h>
     39  1.61    jruoho #include <sys/systm.h>
     40   1.1  jmcneill 
     41   1.1  jmcneill #include <dev/acpi/acpireg.h>
     42   1.1  jmcneill #include <dev/acpi/acpivar.h>
     43   1.1  jmcneill 
     44  1.47   mlelstv #define _COMPONENT          ACPI_TZ_COMPONENT
     45  1.47   mlelstv ACPI_MODULE_NAME            ("acpi_tz")
     46  1.47   mlelstv 
     47   1.1  jmcneill /* flags */
     48   1.1  jmcneill #define ATZ_F_VERBOSE		0x01	/* show events to console */
     49  1.13     kochi #define ATZ_F_CRITICAL		0x02	/* zone critical */
     50  1.13     kochi #define ATZ_F_HOT		0x04	/* zone hot */
     51  1.13     kochi #define ATZ_F_PASSIVE		0x08	/* zone passive cooling */
     52  1.13     kochi #define ATZ_F_PASSIVEONLY	0x10	/* zone is passive cooling only */
     53  1.13     kochi 
     54  1.13     kochi /* no active cooling level */
     55  1.13     kochi #define ATZ_ACTIVE_NONE -1
     56   1.1  jmcneill 
     57   1.1  jmcneill /* constants */
     58   1.1  jmcneill #define ATZ_TZP_RATE	300	/* default if no _TZP CM present (30 secs) */
     59   1.1  jmcneill #define ATZ_NLEVELS	10	/* number of cooling levels, from ACPI spec */
     60  1.60  pgoyette #define ATZ_ZEROC	2732	/* 0C, measured in 0.1 Kelvin */
     61  1.13     kochi #define ATZ_TMP_INVALID	0xffffffff	/* invalid temperature */
     62  1.13     kochi #define ATZ_ZONE_EXPIRE	9000	/* zone info refetch interval (15min) */
     63   1.1  jmcneill 
     64   1.1  jmcneill /* sensor indexes */
     65   1.1  jmcneill #define ATZ_SENSOR_TEMP	0	/* thermal zone temperature */
     66   1.1  jmcneill 
     67  1.33   xtraeme static int	acpitz_match(device_t, cfdata_t, void *);
     68  1.33   xtraeme static void	acpitz_attach(device_t, device_t, void *);
     69   1.1  jmcneill 
     70   1.1  jmcneill /*
     71   1.1  jmcneill  * ACPI Temperature Zone information. Note all temperatures are reported
     72  1.60  pgoyette  * in 0.1 Kelvin, and that the ACPI specification assumes that
     73  1.60  pgoyette  * K = C + 273.2 rather than the nominal 273.15 used by envsys(4).
     74  1.45  pgoyette  * So define an appropriate conversion.
     75   1.1  jmcneill  */
     76  1.45  pgoyette 
     77  1.45  pgoyette #define	ATZ2UKELVIN(t) ((t) * 100000 - 50000)
     78  1.45  pgoyette 
     79   1.1  jmcneill struct acpitz_zone {
     80   1.1  jmcneill 	/* Active cooling temperature threshold */
     81  1.64    jruoho 	uint32_t ac[ATZ_NLEVELS];
     82   1.1  jmcneill 	/* Package of references to all active cooling devices for a level */
     83   1.1  jmcneill 	ACPI_BUFFER al[ATZ_NLEVELS];
     84   1.1  jmcneill 	/* Critical temperature threshold for system shutdown */
     85  1.64    jruoho 	uint32_t crt;
     86   1.1  jmcneill 	/* Critical temperature threshold for S4 sleep */
     87  1.64    jruoho 	uint32_t hot;
     88   1.1  jmcneill 	/* Package of references to processor objects for passive cooling */
     89   1.1  jmcneill 	ACPI_BUFFER psl;
     90  1.48    cegger 	/* Conveys if temperatures are absolute or relative values. */
     91  1.64    jruoho 	uint32_t rtv;
     92   1.1  jmcneill 	/* Passive cooling temperature threshold */
     93  1.64    jruoho 	uint32_t psv;
     94   1.1  jmcneill 	/* Thermal constants for use in passive cooling formulas */
     95  1.64    jruoho 	uint32_t tc1, tc2;
     96   1.1  jmcneill 	/* Current temperature of the thermal zone */
     97  1.64    jruoho 	uint32_t prevtmp, tmp;
     98   1.1  jmcneill 	/* Thermal sampling period for passive cooling, in tenths of seconds */
     99  1.64    jruoho 	uint32_t tsp;
    100   1.1  jmcneill 	/* Package of references to devices in this TZ (optional) */
    101   1.1  jmcneill 	ACPI_BUFFER tzd;
    102   1.1  jmcneill 	/* Recommended TZ polling frequency, in tenths of seconds */
    103  1.64    jruoho 	uint32_t tzp;
    104  1.49    cegger 	/* Thermal zone name */
    105  1.49    cegger 	char *name;
    106  1.49    cegger 	/* FAN min, max, current rpms */
    107  1.64    jruoho 	uint32_t fanmin, fanmax, fancurrent;
    108   1.1  jmcneill };
    109   1.1  jmcneill 
    110   1.1  jmcneill struct acpitz_softc {
    111   1.1  jmcneill 	struct acpi_devnode *sc_devnode;
    112   1.1  jmcneill 	struct acpitz_zone sc_zone;
    113   1.1  jmcneill 	struct callout sc_callout;
    114  1.27   xtraeme 	struct sysmon_envsys *sc_sme;
    115  1.49    cegger 	envsys_data_t sc_temp_sensor;
    116  1.49    cegger 	envsys_data_t sc_fan_sensor;
    117  1.13     kochi 	int sc_active;		/* active cooling level */
    118   1.1  jmcneill 	int sc_flags;
    119   1.1  jmcneill 	int sc_rate;		/* tz poll rate */
    120  1.13     kochi 	int sc_zone_expire;
    121  1.29  jmcneill 
    122  1.29  jmcneill 	int sc_first;
    123  1.49    cegger 	int sc_have_fan;	/* FAN sensor is optional */
    124   1.1  jmcneill };
    125   1.1  jmcneill 
    126  1.11     kochi static void	acpitz_get_status(void *);
    127  1.14    martin static void	acpitz_get_zone(void *, int);
    128  1.14    martin static void	acpitz_get_zone_quiet(void *);
    129  1.33   xtraeme static char	*acpitz_celcius_string(int);
    130  1.33   xtraeme static void	acpitz_print_status(device_t);
    131  1.13     kochi static void	acpitz_power_off(struct acpitz_softc *);
    132  1.13     kochi static void	acpitz_power_zone(struct acpitz_softc *, int, int);
    133  1.64    jruoho static void	acpitz_sane_temp(uint32_t *tmp);
    134  1.13     kochi static ACPI_STATUS
    135  1.13     kochi 		acpitz_switch_cooler(ACPI_OBJECT *, void *);
    136  1.64    jruoho static void	acpitz_notify_handler(ACPI_HANDLE, uint32_t, void *);
    137  1.64    jruoho static int	acpitz_get_integer(device_t, const char *, uint32_t *);
    138   1.1  jmcneill static void	acpitz_tick(void *);
    139  1.33   xtraeme static void	acpitz_init_envsys(device_t);
    140  1.42  pgoyette static void	acpitz_get_limits(struct sysmon_envsys *, envsys_data_t *,
    141  1.58  pgoyette 				  sysmon_envsys_lim_t *, uint32_t *);
    142  1.64    jruoho static int	acpitz_get_fanspeed(device_t, uint32_t *,
    143  1.64    jruoho 				    uint32_t *, uint32_t *);
    144  1.49    cegger #ifdef notyet
    145  1.49    cegger static ACPI_STATUS
    146  1.64    jruoho 		acpitz_set_fanspeed(device_t, uint32_t);
    147  1.49    cegger #endif
    148   1.1  jmcneill 
    149  1.33   xtraeme CFATTACH_DECL_NEW(acpitz, sizeof(struct acpitz_softc), acpitz_match,
    150   1.1  jmcneill     acpitz_attach, NULL, NULL);
    151   1.1  jmcneill 
    152   1.1  jmcneill /*
    153   1.1  jmcneill  * acpitz_match: autoconf(9) match routine
    154   1.1  jmcneill  */
    155  1.11     kochi static int
    156  1.33   xtraeme acpitz_match(device_t parent, cfdata_t match, void *aux)
    157   1.1  jmcneill {
    158   1.1  jmcneill 	struct acpi_attach_args *aa = aux;
    159   1.1  jmcneill 
    160   1.1  jmcneill 	if (aa->aa_node->ad_type != ACPI_TYPE_THERMAL)
    161   1.1  jmcneill 		return 0;
    162   1.1  jmcneill 
    163   1.1  jmcneill 	return 1;
    164   1.1  jmcneill }
    165   1.1  jmcneill 
    166   1.1  jmcneill /*
    167   1.1  jmcneill  * acpitz_attach: autoconf(9) attach routine
    168   1.1  jmcneill  */
    169  1.11     kochi static void
    170  1.33   xtraeme acpitz_attach(device_t parent, device_t self, void *aux)
    171   1.1  jmcneill {
    172  1.33   xtraeme 	struct acpitz_softc *sc = device_private(self);
    173   1.1  jmcneill 	struct acpi_attach_args *aa = aux;
    174   1.1  jmcneill 	ACPI_STATUS rv;
    175   1.9    martin 	ACPI_INTEGER v;
    176   1.1  jmcneill 
    177   1.1  jmcneill #if 0
    178   1.1  jmcneill 	sc->sc_flags = ATZ_F_VERBOSE;
    179   1.1  jmcneill #endif
    180   1.1  jmcneill 	sc->sc_devnode = aa->aa_node;
    181   1.1  jmcneill 
    182  1.29  jmcneill 	aprint_naive("\n");
    183  1.63    jruoho 	aprint_normal(": ACPI Thermal Zone\n");
    184   1.1  jmcneill 
    185   1.9    martin 	rv = acpi_eval_integer(sc->sc_devnode->ad_handle, "_TZP", &v);
    186  1.31  jmcneill 	if (ACPI_FAILURE(rv))
    187   1.1  jmcneill 		sc->sc_zone.tzp = ATZ_TZP_RATE;
    188  1.31  jmcneill 	else
    189   1.9    martin 		sc->sc_zone.tzp = v;
    190  1.31  jmcneill 
    191  1.63    jruoho 	aprint_debug_dev(self, "sample rate %d.%ds\n",
    192  1.31  jmcneill 	    sc->sc_zone.tzp / 10, sc->sc_zone.tzp % 10);
    193   1.3   mycroft 
    194   1.1  jmcneill 	/* XXX a value of 0 means "polling is not necessary" */
    195   1.1  jmcneill 	if (sc->sc_zone.tzp == 0)
    196   1.1  jmcneill 		sc->sc_zone.tzp = ATZ_TZP_RATE;
    197  1.10     kochi 
    198  1.13     kochi 	sc->sc_zone_expire = ATZ_ZONE_EXPIRE / sc->sc_zone.tzp;
    199  1.29  jmcneill 	sc->sc_first = 1;
    200  1.49    cegger 	sc->sc_have_fan = 0;
    201  1.49    cegger 	if (acpitz_get_fanspeed(self,
    202  1.49    cegger 	    &sc->sc_zone.fanmin, &sc->sc_zone.fanmax, &sc->sc_zone.fancurrent)
    203  1.49    cegger 	    == 0)
    204  1.49    cegger 		sc->sc_have_fan = 1;
    205  1.13     kochi 
    206  1.49    cegger 	rv = acpi_eval_string(sc->sc_devnode->ad_handle,
    207  1.49    cegger 	    "REGN", &sc->sc_zone.name);
    208  1.49    cegger 	if (ACPI_FAILURE(rv))
    209  1.49    cegger 		sc->sc_zone.name = __UNCONST("temperature");
    210  1.63    jruoho 
    211  1.33   xtraeme 	acpitz_get_zone(self, 1);
    212  1.33   xtraeme 	acpitz_get_status(self);
    213   1.1  jmcneill 
    214  1.65    jruoho 	(void)acpi_register_notify(sc->sc_devnode, acpitz_notify_handler);
    215   1.1  jmcneill 
    216  1.33   xtraeme 	callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
    217  1.33   xtraeme 	callout_setfunc(&sc->sc_callout, acpitz_tick, self);
    218   1.1  jmcneill 
    219  1.33   xtraeme 	acpitz_init_envsys(self);
    220  1.25     joerg 
    221  1.32  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    222  1.32  jmcneill 		aprint_error(": couldn't establish power handler\n");
    223  1.32  jmcneill 
    224  1.25     joerg 	callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10);
    225   1.1  jmcneill }
    226   1.1  jmcneill 
    227  1.11     kochi static void
    228  1.14    martin acpitz_get_zone_quiet(void *opaque)
    229  1.14    martin {
    230  1.14    martin 	acpitz_get_zone(opaque, 0);
    231  1.14    martin }
    232  1.14    martin 
    233  1.14    martin static void
    234   1.1  jmcneill acpitz_get_status(void *opaque)
    235   1.1  jmcneill {
    236  1.33   xtraeme 	device_t dv = opaque;
    237  1.33   xtraeme 	struct acpitz_softc *sc = device_private(dv);
    238  1.64    jruoho 	uint32_t tmp, active, fmin, fmax, fcurrent;
    239  1.13     kochi 	int i, flags;
    240  1.13     kochi 
    241  1.13     kochi 	sc->sc_zone_expire--;
    242  1.13     kochi 	if (sc->sc_zone_expire <= 0) {
    243  1.13     kochi 		sc->sc_zone_expire = ATZ_ZONE_EXPIRE / sc->sc_zone.tzp;
    244  1.13     kochi 		if (sc->sc_flags & ATZ_F_VERBOSE)
    245  1.33   xtraeme 			printf("%s: force refetch zone\n", device_xname(dv));
    246  1.34   xtraeme 		acpitz_get_zone(dv, 0);
    247  1.13     kochi 	}
    248   1.3   mycroft 
    249  1.63    jruoho 	if (acpitz_get_integer(dv, "_TMP", &tmp) != 0)
    250   1.3   mycroft 		return;
    251  1.49    cegger 
    252  1.49    cegger 	sc->sc_zone.prevtmp = sc->sc_zone.tmp;
    253  1.13     kochi 	sc->sc_zone.tmp = tmp;
    254  1.49    cegger 	if (sc->sc_first)
    255  1.49    cegger 		sc->sc_zone.prevtmp = tmp;
    256  1.13     kochi 	/* XXX sanity check for tmp here? */
    257   1.1  jmcneill 
    258  1.49    cegger 	if (acpitz_get_fanspeed(dv, &fmin, &fmax, &fcurrent) == 0) {
    259  1.49    cegger 		if (fcurrent != ATZ_TMP_INVALID)
    260  1.49    cegger 			sc->sc_zone.fancurrent = fcurrent;
    261  1.49    cegger 	}
    262  1.49    cegger 
    263   1.7     soren 	/*
    264  1.13     kochi 	 * The temperature unit for envsys(4) is microKelvin, so convert to
    265   1.7     soren 	 * that from ACPI's microKelvin. Also, the ACPI specification assumes
    266  1.13     kochi 	 * that K = C + 273.2 rather than the nominal 273.15 used by envsys(4),
    267   1.7     soren 	 * so we correct for that too.
    268   1.7     soren 	 */
    269  1.49    cegger 	sc->sc_temp_sensor.value_cur = ATZ2UKELVIN(sc->sc_zone.tmp);
    270  1.49    cegger 	sc->sc_temp_sensor.state = ENVSYS_SVALID;
    271  1.49    cegger 
    272  1.49    cegger 	sc->sc_fan_sensor.value_cur = sc->sc_zone.fancurrent;
    273  1.49    cegger 	sc->sc_fan_sensor.state = ENVSYS_SVALID;
    274   1.1  jmcneill 
    275   1.1  jmcneill 	if (sc->sc_flags & ATZ_F_VERBOSE)
    276  1.33   xtraeme 		acpitz_print_status(dv);
    277   1.1  jmcneill 
    278  1.13     kochi 	if (sc->sc_flags & ATZ_F_PASSIVEONLY) {
    279  1.13     kochi 		/* Passive Cooling: XXX not yet */
    280  1.13     kochi 
    281  1.13     kochi 	} else {
    282  1.13     kochi 		/* Active Cooling */
    283  1.13     kochi 
    284  1.13     kochi 		/* temperature threshold: _AC0 > ... > _AC9 */
    285  1.13     kochi 		active = ATZ_ACTIVE_NONE;
    286  1.13     kochi 		for (i = ATZ_NLEVELS - 1; i >= 0; i--) {
    287  1.13     kochi 			if (sc->sc_zone.ac[i] == ATZ_TMP_INVALID)
    288  1.13     kochi 				continue;
    289  1.13     kochi 
    290  1.13     kochi 			/* we want to keep highest cooling mode in 'active' */
    291  1.13     kochi 			if (sc->sc_zone.ac[i] <= tmp)
    292  1.13     kochi 				active = i;
    293  1.13     kochi 		}
    294  1.13     kochi 
    295  1.33   xtraeme 		flags = sc->sc_flags &
    296  1.33   xtraeme 		    ~(ATZ_F_CRITICAL|ATZ_F_HOT|ATZ_F_PASSIVE);
    297  1.13     kochi 		if (sc->sc_zone.psv != ATZ_TMP_INVALID &&
    298  1.13     kochi 		    tmp >= sc->sc_zone.psv)
    299  1.13     kochi 			flags |= ATZ_F_PASSIVE;
    300  1.13     kochi 		if (sc->sc_zone.hot != ATZ_TMP_INVALID &&
    301  1.13     kochi 		    tmp >= sc->sc_zone.hot)
    302  1.13     kochi 			flags |= ATZ_F_HOT;
    303  1.13     kochi 		if (sc->sc_zone.crt != ATZ_TMP_INVALID &&
    304  1.13     kochi 		    tmp >= sc->sc_zone.crt)
    305  1.13     kochi 			flags |= ATZ_F_CRITICAL;
    306  1.13     kochi 
    307  1.13     kochi 		if (flags != sc->sc_flags) {
    308  1.13     kochi 			int changed = (sc->sc_flags ^ flags) & flags;
    309  1.13     kochi 			sc->sc_flags = flags;
    310  1.22   xtraeme 			if (changed & ATZ_F_CRITICAL) {
    311  1.49    cegger 				sc->sc_temp_sensor.state = ENVSYS_SCRITOVER;
    312  1.39  pgoyette 				aprint_debug_dev(dv,
    313  1.33   xtraeme 				    "zone went critical at temp %sC\n",
    314  1.13     kochi 				    acpitz_celcius_string(tmp));
    315  1.22   xtraeme 			} else if (changed & ATZ_F_HOT) {
    316  1.49    cegger 				sc->sc_temp_sensor.state = ENVSYS_SCRITOVER;
    317  1.39  pgoyette 				aprint_debug_dev(dv,
    318  1.33   xtraeme 				    "zone went hot at temp %sC\n",
    319  1.13     kochi 				    acpitz_celcius_string(tmp));
    320  1.22   xtraeme 			}
    321  1.13     kochi 		}
    322  1.13     kochi 
    323  1.13     kochi 		/* power on fans */
    324  1.13     kochi 		if (sc->sc_active != active) {
    325  1.13     kochi 			if (sc->sc_active != ATZ_ACTIVE_NONE)
    326  1.13     kochi 				acpitz_power_zone(sc, sc->sc_active, 0);
    327  1.13     kochi 
    328  1.13     kochi 			if (active != ATZ_ACTIVE_NONE) {
    329  1.13     kochi 				if (sc->sc_flags & ATZ_F_VERBOSE)
    330  1.13     kochi 					printf("%s: active cooling level %u\n",
    331  1.33   xtraeme 					    device_xname(dv), active);
    332  1.13     kochi 				acpitz_power_zone(sc, active, 1);
    333  1.52  pgoyette 			} else if (sc->sc_flags & ATZ_F_VERBOSE)
    334  1.52  pgoyette 				printf("%s: no active cooling level\n",
    335  1.52  pgoyette 				    device_xname(dv));
    336  1.13     kochi 
    337  1.13     kochi 			sc->sc_active = active;
    338  1.13     kochi 		}
    339  1.13     kochi 	}
    340  1.13     kochi 
    341   1.1  jmcneill 	return;
    342   1.1  jmcneill }
    343   1.1  jmcneill 
    344  1.13     kochi static char *
    345  1.13     kochi acpitz_celcius_string(int dk)
    346  1.13     kochi {
    347  1.13     kochi 	static char buf[10];
    348  1.59  pgoyette 	int dc;
    349  1.13     kochi 
    350  1.59  pgoyette 	dc = abs(dk - ATZ_ZEROC);
    351  1.59  pgoyette 	snprintf(buf, sizeof(buf), "%s%d.%d", (dk >= ATZ_ZEROC)?"":"-",
    352  1.59  pgoyette 		dc / 10, dc % 10);
    353  1.13     kochi 
    354  1.13     kochi 	return buf;
    355  1.13     kochi }
    356  1.13     kochi 
    357   1.1  jmcneill static void
    358  1.33   xtraeme acpitz_print_status(device_t dv)
    359   1.1  jmcneill {
    360  1.33   xtraeme 	struct acpitz_softc *sc = device_private(dv);
    361   1.1  jmcneill 
    362  1.33   xtraeme 	printf("%s: zone temperature is now %sC\n", device_xname(dv),
    363  1.13     kochi 	    acpitz_celcius_string(sc->sc_zone.tmp));
    364  1.49    cegger 	if (sc->sc_have_fan) {
    365  1.49    cegger 		printf("%s: fan rpm %u\n", device_xname(dv),
    366  1.49    cegger 		    sc->sc_zone.fancurrent);
    367  1.49    cegger 	}
    368   1.1  jmcneill 
    369   1.1  jmcneill 	return;
    370   1.1  jmcneill }
    371   1.1  jmcneill 
    372  1.13     kochi static ACPI_STATUS
    373  1.13     kochi acpitz_switch_cooler(ACPI_OBJECT *obj, void *arg)
    374  1.13     kochi {
    375  1.13     kochi 	ACPI_HANDLE cooler;
    376  1.13     kochi 	ACPI_STATUS rv;
    377  1.13     kochi 	int pwr_state, flag;
    378  1.13     kochi 
    379  1.13     kochi 	flag = *(int *)arg;
    380  1.13     kochi 
    381  1.13     kochi 	if (flag)
    382  1.13     kochi 		pwr_state = ACPI_STATE_D0;
    383  1.13     kochi 	else
    384  1.13     kochi 		pwr_state = ACPI_STATE_D3;
    385  1.13     kochi 
    386  1.56    jruoho 	rv = acpi_eval_reference_handle(obj, &cooler);
    387  1.56    jruoho 	if (ACPI_FAILURE(rv)) {
    388  1.56    jruoho 		aprint_error("%s: failed to get handle\n", __func__);
    389  1.56    jruoho 		return rv;
    390  1.13     kochi 	}
    391  1.13     kochi 
    392  1.13     kochi 	rv = acpi_pwr_switch_consumer(cooler, pwr_state);
    393  1.55    jruoho 	if (rv != AE_BAD_PARAMETER && ACPI_FAILURE(rv))
    394  1.55    jruoho 		aprint_error("%s: failed to change state for %s: %s\n",
    395  1.55    jruoho 		    __func__, acpi_name(cooler), AcpiFormatException(rv));
    396  1.13     kochi 
    397  1.13     kochi 	return AE_OK;
    398  1.13     kochi }
    399  1.13     kochi 
    400  1.13     kochi /*
    401  1.13     kochi  * acpitz_power_zone:
    402  1.13     kochi  *	power on or off the i:th part of the zone zone
    403  1.13     kochi  */
    404  1.13     kochi static void
    405  1.13     kochi acpitz_power_zone(struct acpitz_softc *sc, int i, int on)
    406  1.13     kochi {
    407  1.13     kochi 	KASSERT(i >= 0 && i < ATZ_NLEVELS);
    408  1.13     kochi 
    409  1.13     kochi 	acpi_foreach_package_object(sc->sc_zone.al[i].Pointer,
    410  1.13     kochi 	    acpitz_switch_cooler, &on);
    411  1.13     kochi }
    412  1.13     kochi 
    413  1.13     kochi 
    414  1.13     kochi /*
    415  1.13     kochi  * acpitz_power_off:
    416  1.13     kochi  *	power off parts of the zone
    417  1.13     kochi  */
    418  1.13     kochi static void
    419  1.13     kochi acpitz_power_off(struct acpitz_softc *sc)
    420  1.13     kochi {
    421  1.13     kochi 	int i;
    422  1.13     kochi 
    423  1.13     kochi 	for (i = 0 ; i < ATZ_NLEVELS; i++) {
    424  1.13     kochi 		if (sc->sc_zone.al[i].Pointer == NULL)
    425  1.13     kochi 			continue;
    426  1.13     kochi 		acpitz_power_zone(sc, i, 0);
    427  1.13     kochi 	}
    428  1.13     kochi 	sc->sc_active = ATZ_ACTIVE_NONE;
    429  1.13     kochi 	sc->sc_flags &= ~(ATZ_F_CRITICAL|ATZ_F_HOT|ATZ_F_PASSIVE);
    430  1.13     kochi }
    431  1.13     kochi 
    432  1.13     kochi static void
    433  1.14    martin acpitz_get_zone(void *opaque, int verbose)
    434  1.13     kochi {
    435  1.33   xtraeme 	device_t dv = opaque;
    436  1.33   xtraeme 	struct acpitz_softc *sc = device_private(dv);
    437  1.13     kochi 	ACPI_STATUS rv;
    438  1.13     kochi 	char buf[8];
    439  1.13     kochi 	int i, valid_levels;
    440  1.13     kochi 
    441  1.29  jmcneill 	if (!sc->sc_first) {
    442  1.13     kochi 		acpitz_power_off(sc);
    443  1.13     kochi 
    444  1.13     kochi 		for (i = 0; i < ATZ_NLEVELS; i++) {
    445  1.13     kochi 			if (sc->sc_zone.al[i].Pointer != NULL)
    446  1.47   mlelstv 				ACPI_FREE(sc->sc_zone.al[i].Pointer);
    447  1.13     kochi 			sc->sc_zone.al[i].Pointer = NULL;
    448  1.13     kochi 		}
    449  1.63    jruoho 	}
    450  1.13     kochi 
    451  1.13     kochi 	valid_levels = 0;
    452  1.13     kochi 
    453  1.13     kochi 	for (i = 0; i < ATZ_NLEVELS; i++) {
    454  1.13     kochi 		ACPI_OBJECT *obj;
    455  1.13     kochi 
    456  1.13     kochi 		snprintf(buf, sizeof(buf), "_AC%d", i);
    457  1.33   xtraeme 		if (acpitz_get_integer(dv, buf, &sc->sc_zone.ac[i]))
    458  1.13     kochi 			continue;
    459  1.13     kochi 
    460  1.13     kochi 		snprintf(buf, sizeof(buf), "_AL%d", i);
    461  1.13     kochi 		rv = acpi_eval_struct(sc->sc_devnode->ad_handle, buf,
    462  1.13     kochi 		    &sc->sc_zone.al[i]);
    463  1.13     kochi 		if (ACPI_FAILURE(rv)) {
    464  1.13     kochi 			sc->sc_zone.al[i].Pointer = NULL;
    465  1.13     kochi 			continue;
    466  1.13     kochi 		}
    467  1.13     kochi 
    468  1.13     kochi 		obj = sc->sc_zone.al[i].Pointer;
    469  1.13     kochi 		if (obj != NULL) {
    470  1.13     kochi 			if (obj->Type != ACPI_TYPE_PACKAGE) {
    471  1.29  jmcneill 				aprint_error("%d not package\n", i);
    472  1.47   mlelstv 				ACPI_FREE(obj);
    473  1.13     kochi 				sc->sc_zone.al[i].Pointer = NULL;
    474  1.13     kochi 				continue;
    475  1.13     kochi 			}
    476  1.13     kochi 		}
    477  1.13     kochi 
    478  1.29  jmcneill 		if (sc->sc_first)
    479  1.29  jmcneill 			aprint_normal(" active cooling level %d: %sC", i,
    480  1.13     kochi 			    acpitz_celcius_string(sc->sc_zone.ac[i]));
    481  1.13     kochi 
    482  1.13     kochi 		valid_levels++;
    483  1.13     kochi 	}
    484  1.13     kochi 
    485  1.33   xtraeme 	acpitz_get_integer(dv, "_TMP", &sc->sc_zone.tmp);
    486  1.33   xtraeme 	acpitz_get_integer(dv, "_CRT", &sc->sc_zone.crt);
    487  1.33   xtraeme 	acpitz_get_integer(dv, "_HOT", &sc->sc_zone.hot);
    488  1.54    jruoho 	acpitz_get_integer(dv, "_PSV", &sc->sc_zone.psv);
    489  1.54    jruoho 	acpitz_get_integer(dv, "_TC1", &sc->sc_zone.tc1);
    490  1.54    jruoho 	acpitz_get_integer(dv, "_TC2", &sc->sc_zone.tc2);
    491  1.54    jruoho 
    492  1.54    jruoho #if 0
    493  1.47   mlelstv 	sc->sc_zone.psl.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    494  1.13     kochi 	sc->sc_zone.psl.Pointer = NULL;
    495  1.49    cegger 	AcpiEvaluateObject(sc->sc_devnode->ad_handle,
    496  1.49    cegger 	    "_PSL", NULL, &sc->sc_zone.psl);
    497  1.54    jruoho #endif
    498  1.13     kochi 
    499  1.48    cegger 	/* ACPI spec: If _RTV is not present or present and zero,
    500  1.48    cegger 	 * values are absolute. */
    501  1.48    cegger 	acpitz_get_integer(dv, "_RTV", &sc->sc_zone.rtv);
    502  1.48    cegger 	if (sc->sc_zone.rtv == ATZ_TMP_INVALID)
    503  1.48    cegger 		sc->sc_zone.rtv = 0;
    504  1.48    cegger 
    505  1.48    cegger 
    506  1.13     kochi 	acpitz_sane_temp(&sc->sc_zone.tmp);
    507  1.13     kochi 	acpitz_sane_temp(&sc->sc_zone.crt);
    508  1.13     kochi 	acpitz_sane_temp(&sc->sc_zone.hot);
    509  1.13     kochi 	acpitz_sane_temp(&sc->sc_zone.psv);
    510  1.13     kochi 
    511  1.63    jruoho 	if (verbose != 0) {
    512  1.63    jruoho 		aprint_verbose_dev(dv, "");
    513  1.63    jruoho 
    514  1.14    martin 		if (sc->sc_zone.crt != ATZ_TMP_INVALID)
    515  1.63    jruoho 			aprint_verbose("critical %s C",
    516  1.14    martin 			    acpitz_celcius_string(sc->sc_zone.crt));
    517  1.63    jruoho 
    518  1.14    martin 		if (sc->sc_zone.hot != ATZ_TMP_INVALID)
    519  1.63    jruoho 			aprint_verbose(" hot %s C",
    520  1.14    martin 			    acpitz_celcius_string(sc->sc_zone.hot));
    521  1.63    jruoho 
    522  1.14    martin 		if (sc->sc_zone.psv != ATZ_TMP_INVALID)
    523  1.63    jruoho 			aprint_normal(" passive %s C",
    524  1.59  pgoyette 			    acpitz_celcius_string(sc->sc_zone.psv));
    525  1.29  jmcneill 	}
    526  1.29  jmcneill 
    527  1.29  jmcneill 	if (valid_levels == 0) {
    528  1.29  jmcneill 		sc->sc_flags |= ATZ_F_PASSIVEONLY;
    529  1.63    jruoho 
    530  1.29  jmcneill 		if (sc->sc_first)
    531  1.63    jruoho 			aprint_verbose(", passive cooling");
    532  1.29  jmcneill 	}
    533  1.63    jruoho 
    534  1.63    jruoho 	if (verbose != 0)
    535  1.63    jruoho 		aprint_verbose("\n");
    536  1.13     kochi 
    537  1.13     kochi 	for (i = 0; i < ATZ_NLEVELS; i++)
    538  1.13     kochi 		acpitz_sane_temp(&sc->sc_zone.ac[i]);
    539  1.13     kochi 
    540  1.13     kochi 	acpitz_power_off(sc);
    541  1.29  jmcneill 	sc->sc_first = 0;
    542  1.13     kochi }
    543  1.13     kochi 
    544  1.11     kochi static void
    545  1.64    jruoho acpitz_notify_handler(ACPI_HANDLE hdl, uint32_t notify, void *opaque)
    546   1.1  jmcneill {
    547  1.33   xtraeme 	device_t dv = opaque;
    548  1.15     kochi 	ACPI_OSD_EXEC_CALLBACK func = NULL;
    549  1.13     kochi 	const char *name;
    550  1.46  jmcneill 	ACPI_STATUS rv;
    551   1.1  jmcneill 
    552   1.1  jmcneill 	switch (notify) {
    553   1.1  jmcneill 	case ACPI_NOTIFY_ThermalZoneStatusChanged:
    554  1.13     kochi 		func = acpitz_get_status;
    555  1.13     kochi 		name = "status check";
    556  1.13     kochi 		break;
    557   1.1  jmcneill 	case ACPI_NOTIFY_ThermalZoneTripPointsChanged:
    558  1.13     kochi 	case ACPI_NOTIFY_DeviceListsChanged:
    559  1.14    martin 		func = acpitz_get_zone_quiet;
    560  1.13     kochi 		name = "get zone";
    561   1.1  jmcneill 		break;
    562   1.1  jmcneill 	default:
    563  1.33   xtraeme 		aprint_debug_dev(dv,
    564  1.33   xtraeme 		    "received unhandled notify message 0x%x\n", notify);
    565  1.13     kochi 		return;
    566   1.1  jmcneill 	}
    567   1.1  jmcneill 
    568  1.13     kochi 	KASSERT(func != NULL);
    569  1.13     kochi 
    570  1.33   xtraeme 	rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, func, dv);
    571  1.46  jmcneill 	if (ACPI_FAILURE(rv))
    572  1.33   xtraeme 		aprint_debug_dev(dv, "unable to queue %s\n", name);
    573   1.1  jmcneill }
    574   1.1  jmcneill 
    575   1.1  jmcneill static void
    576  1.64    jruoho acpitz_sane_temp(uint32_t *tmp)
    577  1.13     kochi {
    578  1.13     kochi 	/* Sane temperatures are beteen 0 and 150 C */
    579  1.13     kochi 	if (*tmp < ATZ_ZEROC || *tmp > ATZ_ZEROC + 1500)
    580  1.13     kochi 		*tmp = ATZ_TMP_INVALID;
    581  1.13     kochi }
    582  1.13     kochi 
    583  1.13     kochi static int
    584  1.64    jruoho acpitz_get_integer(device_t dv, const char *cm, uint32_t *val)
    585  1.13     kochi {
    586  1.33   xtraeme 	struct acpitz_softc *sc = device_private(dv);
    587  1.13     kochi 	ACPI_STATUS rv;
    588  1.13     kochi 	ACPI_INTEGER tmp;
    589  1.13     kochi 
    590  1.13     kochi 	rv = acpi_eval_integer(sc->sc_devnode->ad_handle, cm, &tmp);
    591  1.63    jruoho 
    592  1.13     kochi 	if (ACPI_FAILURE(rv)) {
    593  1.13     kochi 		*val = ATZ_TMP_INVALID;
    594  1.63    jruoho 
    595  1.63    jruoho 		if (rv != AE_NOT_FOUND)
    596  1.63    jruoho 			aprint_debug_dev(dv, "failed to evaluate %s: %s\n",
    597  1.63    jruoho 			    cm, AcpiFormatException(rv));
    598  1.63    jruoho 
    599  1.13     kochi 		return 1;
    600  1.13     kochi 	}
    601  1.13     kochi 
    602  1.13     kochi 	*val = tmp;
    603  1.13     kochi 
    604  1.13     kochi 	return 0;
    605  1.13     kochi }
    606  1.13     kochi 
    607  1.49    cegger static int
    608  1.49    cegger acpitz_get_fanspeed(device_t dv,
    609  1.64    jruoho     uint32_t *fanmin, uint32_t *fanmax, uint32_t *fancurrent)
    610  1.49    cegger {
    611  1.49    cegger 	struct acpitz_softc *sc = device_private(dv);
    612  1.49    cegger 	ACPI_STATUS rv;
    613  1.49    cegger 	ACPI_HANDLE handle;
    614  1.49    cegger 	ACPI_INTEGER fmin, fmax, fcurr;
    615  1.49    cegger 	int rc = 0;
    616  1.49    cegger 
    617  1.49    cegger 	handle = sc->sc_devnode->ad_handle;
    618  1.49    cegger 	rv = acpi_eval_integer(handle, "FMIN", &fmin);
    619  1.49    cegger 	if (ACPI_FAILURE(rv)) {
    620  1.49    cegger 		fmin = ATZ_TMP_INVALID;
    621  1.49    cegger 		rc = 1;
    622  1.49    cegger 	}
    623  1.49    cegger 	rv = acpi_eval_integer(handle, "FMAX", &fmax);
    624  1.49    cegger 	if (ACPI_FAILURE(rv)) {
    625  1.49    cegger 		fmax = ATZ_TMP_INVALID;
    626  1.49    cegger 		rc = 1;
    627  1.49    cegger 	}
    628  1.49    cegger 	rv = acpi_eval_integer(handle, "FRSP", &fcurr);
    629  1.49    cegger 	if (ACPI_FAILURE(rv)) {
    630  1.49    cegger 		fcurr = ATZ_TMP_INVALID;
    631  1.49    cegger 		rc = 1;
    632  1.49    cegger 	}
    633  1.49    cegger 
    634  1.49    cegger 	if (fanmin)
    635  1.49    cegger 		*fanmin = fmin;
    636  1.49    cegger 	if (fanmax)
    637  1.49    cegger 		*fanmax = fmax;
    638  1.49    cegger 	if (fancurrent)
    639  1.49    cegger 		*fancurrent = fcurr;
    640  1.49    cegger 	return rc;
    641  1.49    cegger }
    642  1.49    cegger 
    643  1.49    cegger #ifdef notyet
    644  1.49    cegger static ACPI_STATUS
    645  1.64    jruoho acpitz_set_fanspeed(device_t dv, uint32_t fanspeed)
    646  1.49    cegger {
    647  1.49    cegger 	struct acpitz_softc *sc = device_private(dv);
    648  1.49    cegger 	ACPI_STATUS rv;
    649  1.49    cegger 	ACPI_HANDLE handle;
    650  1.49    cegger 	handle = sc->sc_devnode->ad_handle;
    651  1.49    cegger 
    652  1.49    cegger 	rv = acpi_eval_set_integer(handle, "FSSP", fanspeed);
    653  1.49    cegger 	if (ACPI_FAILURE(rv))
    654  1.49    cegger 		aprint_debug_dev(dv, "failed to set fanspeed to %u rpm: %s\n",
    655  1.49    cegger 			fanspeed, AcpiFormatException(rv));
    656  1.49    cegger 	return rv;
    657  1.49    cegger }
    658  1.49    cegger #endif
    659  1.49    cegger 
    660  1.13     kochi static void
    661   1.1  jmcneill acpitz_tick(void *opaque)
    662   1.1  jmcneill {
    663  1.33   xtraeme 	device_t dv = opaque;
    664  1.33   xtraeme 	struct acpitz_softc *sc = device_private(dv);
    665   1.1  jmcneill 
    666  1.33   xtraeme 	AcpiOsExecute(OSL_NOTIFY_HANDLER, acpitz_get_status, dv);
    667   1.1  jmcneill 
    668  1.25     joerg 	callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10);
    669   1.1  jmcneill }
    670   1.1  jmcneill 
    671   1.1  jmcneill static void
    672  1.33   xtraeme acpitz_init_envsys(device_t dv)
    673   1.1  jmcneill {
    674  1.33   xtraeme 	struct acpitz_softc *sc = device_private(dv);
    675  1.33   xtraeme 
    676  1.27   xtraeme 	sc->sc_sme = sysmon_envsys_create();
    677  1.42  pgoyette 	sc->sc_sme->sme_get_limits = acpitz_get_limits;
    678  1.42  pgoyette 	sc->sc_sme->sme_cookie = sc;
    679  1.42  pgoyette 	sc->sc_sme->sme_name = device_xname(dv);
    680  1.42  pgoyette 	sc->sc_sme->sme_flags = SME_DISABLE_REFRESH;
    681  1.49    cegger 
    682  1.49    cegger 	sc->sc_temp_sensor.flags = ENVSYS_FMONLIMITS | ENVSYS_FMONNOTSUPP;
    683  1.49    cegger 	sc->sc_temp_sensor.units = ENVSYS_STEMP;
    684  1.49    cegger 	strlcpy(sc->sc_temp_sensor.desc,
    685  1.49    cegger 	    sc->sc_zone.name, sizeof(sc->sc_temp_sensor.desc));
    686  1.49    cegger 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_temp_sensor))
    687  1.42  pgoyette 		goto out;
    688  1.42  pgoyette 
    689  1.49    cegger 	if (sc->sc_have_fan) {
    690  1.49    cegger 		sc->sc_fan_sensor.flags =
    691  1.49    cegger 		    ENVSYS_FMONLIMITS | ENVSYS_FMONNOTSUPP;
    692  1.49    cegger 		sc->sc_fan_sensor.units = ENVSYS_SFANRPM;
    693  1.49    cegger 		strlcpy(sc->sc_fan_sensor.desc,
    694  1.49    cegger 		    "FAN", sizeof(sc->sc_fan_sensor.desc));
    695  1.49    cegger 		if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_fan_sensor))
    696  1.49    cegger 			/* ignore error because fan sensor is optional */
    697  1.49    cegger 			aprint_error_dev(dv, "unable to attach fan sensor\n");
    698  1.49    cegger 	}
    699  1.49    cegger 
    700  1.42  pgoyette 	/* hook into sysmon */
    701  1.42  pgoyette 	if (sysmon_envsys_register(sc->sc_sme) == 0)
    702  1.27   xtraeme 		return;
    703  1.49    cegger 
    704  1.42  pgoyette out:
    705  1.42  pgoyette 	aprint_error_dev(dv, "unable to register with sysmon\n");
    706  1.42  pgoyette 	sysmon_envsys_destroy(sc->sc_sme);
    707  1.42  pgoyette }
    708  1.42  pgoyette 
    709  1.42  pgoyette static void
    710  1.42  pgoyette acpitz_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    711  1.58  pgoyette 		  sysmon_envsys_lim_t *limits, uint32_t *props)
    712  1.42  pgoyette {
    713  1.42  pgoyette 	struct acpitz_softc *sc = sme->sme_cookie;
    714  1.42  pgoyette 	int i;
    715   1.1  jmcneill 
    716  1.49    cegger 	switch (edata->units) {
    717  1.49    cegger 	case ENVSYS_STEMP:
    718  1.58  pgoyette 		*props = 0;
    719  1.49    cegger 		if (sc->sc_zone.hot != ATZ_TMP_INVALID) {
    720  1.58  pgoyette 			*props |= PROP_CRITMAX;
    721  1.49    cegger 			limits->sel_critmax = ATZ2UKELVIN(sc->sc_zone.hot);
    722  1.49    cegger 		} else if (sc->sc_zone.crt != ATZ_TMP_INVALID) {
    723  1.58  pgoyette 			*props |= PROP_CRITMAX;
    724  1.49    cegger 			limits->sel_critmax = ATZ2UKELVIN(sc->sc_zone.crt);
    725  1.49    cegger 		}
    726  1.49    cegger 		for (i = 0; i < ATZ_NLEVELS; i++) {
    727  1.49    cegger 			if (sc->sc_zone.ac[i] != ATZ_TMP_INVALID) {
    728  1.51  pgoyette 				limits->sel_warnmax =
    729  1.49    cegger 				    ATZ2UKELVIN(sc->sc_zone.ac[i]);
    730  1.58  pgoyette 				*props |= PROP_WARNMAX;
    731  1.49    cegger 				break;
    732  1.49    cegger 			}
    733  1.49    cegger 		}
    734  1.49    cegger 		break;
    735  1.49    cegger 
    736  1.49    cegger 	case ENVSYS_SFANRPM:
    737  1.58  pgoyette 		*props = 0;
    738  1.49    cegger 		if (sc->sc_zone.fanmin != ATZ_TMP_INVALID) {
    739  1.58  pgoyette 			*props |= PROP_WARNMIN;
    740  1.49    cegger 			limits->sel_warnmin = sc->sc_zone.fanmin;
    741  1.49    cegger 			sc->sc_fan_sensor.flags |= ENVSYS_FVALID_MIN;
    742  1.49    cegger 		}
    743  1.49    cegger 		if (sc->sc_zone.fanmax != ATZ_TMP_INVALID) {
    744  1.58  pgoyette 			*props |= PROP_WARNMAX;
    745  1.49    cegger 			limits->sel_warnmax = sc->sc_zone.fanmax;
    746  1.49    cegger 			sc->sc_fan_sensor.flags |= ENVSYS_FVALID_MAX;
    747  1.42  pgoyette 		}
    748  1.49    cegger 		break;
    749  1.49    cegger 	}
    750   1.1  jmcneill }
    751