Home | History | Annotate | Line # | Download | only in acpi
acpi_tz.c revision 1.64
      1  1.64    jruoho /* $NetBSD: acpi_tz.c,v 1.64 2010/04/14 19:27:28 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.64    jruoho __KERNEL_RCSID(0, "$NetBSD: acpi_tz.c,v 1.64 2010/04/14 19:27:28 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.1  jmcneill 	rv = AcpiInstallNotifyHandler(sc->sc_devnode->ad_handle,
    215  1.57    jruoho 	    ACPI_DEVICE_NOTIFY, acpitz_notify_handler, self);
    216  1.57    jruoho 
    217  1.57    jruoho 	if (ACPI_FAILURE(rv))
    218   1.1  jmcneill 		return;
    219   1.1  jmcneill 
    220  1.33   xtraeme 	callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
    221  1.33   xtraeme 	callout_setfunc(&sc->sc_callout, acpitz_tick, self);
    222   1.1  jmcneill 
    223  1.33   xtraeme 	acpitz_init_envsys(self);
    224  1.25     joerg 
    225  1.32  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    226  1.32  jmcneill 		aprint_error(": couldn't establish power handler\n");
    227  1.32  jmcneill 
    228  1.25     joerg 	callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10);
    229   1.1  jmcneill }
    230   1.1  jmcneill 
    231  1.11     kochi static void
    232  1.14    martin acpitz_get_zone_quiet(void *opaque)
    233  1.14    martin {
    234  1.14    martin 	acpitz_get_zone(opaque, 0);
    235  1.14    martin }
    236  1.14    martin 
    237  1.14    martin static void
    238   1.1  jmcneill acpitz_get_status(void *opaque)
    239   1.1  jmcneill {
    240  1.33   xtraeme 	device_t dv = opaque;
    241  1.33   xtraeme 	struct acpitz_softc *sc = device_private(dv);
    242  1.64    jruoho 	uint32_t tmp, active, fmin, fmax, fcurrent;
    243  1.13     kochi 	int i, flags;
    244  1.13     kochi 
    245  1.13     kochi 	sc->sc_zone_expire--;
    246  1.13     kochi 	if (sc->sc_zone_expire <= 0) {
    247  1.13     kochi 		sc->sc_zone_expire = ATZ_ZONE_EXPIRE / sc->sc_zone.tzp;
    248  1.13     kochi 		if (sc->sc_flags & ATZ_F_VERBOSE)
    249  1.33   xtraeme 			printf("%s: force refetch zone\n", device_xname(dv));
    250  1.34   xtraeme 		acpitz_get_zone(dv, 0);
    251  1.13     kochi 	}
    252   1.3   mycroft 
    253  1.63    jruoho 	if (acpitz_get_integer(dv, "_TMP", &tmp) != 0)
    254   1.3   mycroft 		return;
    255  1.49    cegger 
    256  1.49    cegger 	sc->sc_zone.prevtmp = sc->sc_zone.tmp;
    257  1.13     kochi 	sc->sc_zone.tmp = tmp;
    258  1.49    cegger 	if (sc->sc_first)
    259  1.49    cegger 		sc->sc_zone.prevtmp = tmp;
    260  1.13     kochi 	/* XXX sanity check for tmp here? */
    261   1.1  jmcneill 
    262  1.49    cegger 	if (acpitz_get_fanspeed(dv, &fmin, &fmax, &fcurrent) == 0) {
    263  1.49    cegger 		if (fcurrent != ATZ_TMP_INVALID)
    264  1.49    cegger 			sc->sc_zone.fancurrent = fcurrent;
    265  1.49    cegger 	}
    266  1.49    cegger 
    267   1.7     soren 	/*
    268  1.13     kochi 	 * The temperature unit for envsys(4) is microKelvin, so convert to
    269   1.7     soren 	 * that from ACPI's microKelvin. Also, the ACPI specification assumes
    270  1.13     kochi 	 * that K = C + 273.2 rather than the nominal 273.15 used by envsys(4),
    271   1.7     soren 	 * so we correct for that too.
    272   1.7     soren 	 */
    273  1.49    cegger 	sc->sc_temp_sensor.value_cur = ATZ2UKELVIN(sc->sc_zone.tmp);
    274  1.49    cegger 	sc->sc_temp_sensor.state = ENVSYS_SVALID;
    275  1.49    cegger 
    276  1.49    cegger 	sc->sc_fan_sensor.value_cur = sc->sc_zone.fancurrent;
    277  1.49    cegger 	sc->sc_fan_sensor.state = ENVSYS_SVALID;
    278   1.1  jmcneill 
    279   1.1  jmcneill 	if (sc->sc_flags & ATZ_F_VERBOSE)
    280  1.33   xtraeme 		acpitz_print_status(dv);
    281   1.1  jmcneill 
    282  1.13     kochi 	if (sc->sc_flags & ATZ_F_PASSIVEONLY) {
    283  1.13     kochi 		/* Passive Cooling: XXX not yet */
    284  1.13     kochi 
    285  1.13     kochi 	} else {
    286  1.13     kochi 		/* Active Cooling */
    287  1.13     kochi 
    288  1.13     kochi 		/* temperature threshold: _AC0 > ... > _AC9 */
    289  1.13     kochi 		active = ATZ_ACTIVE_NONE;
    290  1.13     kochi 		for (i = ATZ_NLEVELS - 1; i >= 0; i--) {
    291  1.13     kochi 			if (sc->sc_zone.ac[i] == ATZ_TMP_INVALID)
    292  1.13     kochi 				continue;
    293  1.13     kochi 
    294  1.13     kochi 			/* we want to keep highest cooling mode in 'active' */
    295  1.13     kochi 			if (sc->sc_zone.ac[i] <= tmp)
    296  1.13     kochi 				active = i;
    297  1.13     kochi 		}
    298  1.13     kochi 
    299  1.33   xtraeme 		flags = sc->sc_flags &
    300  1.33   xtraeme 		    ~(ATZ_F_CRITICAL|ATZ_F_HOT|ATZ_F_PASSIVE);
    301  1.13     kochi 		if (sc->sc_zone.psv != ATZ_TMP_INVALID &&
    302  1.13     kochi 		    tmp >= sc->sc_zone.psv)
    303  1.13     kochi 			flags |= ATZ_F_PASSIVE;
    304  1.13     kochi 		if (sc->sc_zone.hot != ATZ_TMP_INVALID &&
    305  1.13     kochi 		    tmp >= sc->sc_zone.hot)
    306  1.13     kochi 			flags |= ATZ_F_HOT;
    307  1.13     kochi 		if (sc->sc_zone.crt != ATZ_TMP_INVALID &&
    308  1.13     kochi 		    tmp >= sc->sc_zone.crt)
    309  1.13     kochi 			flags |= ATZ_F_CRITICAL;
    310  1.13     kochi 
    311  1.13     kochi 		if (flags != sc->sc_flags) {
    312  1.13     kochi 			int changed = (sc->sc_flags ^ flags) & flags;
    313  1.13     kochi 			sc->sc_flags = flags;
    314  1.22   xtraeme 			if (changed & ATZ_F_CRITICAL) {
    315  1.49    cegger 				sc->sc_temp_sensor.state = ENVSYS_SCRITOVER;
    316  1.39  pgoyette 				aprint_debug_dev(dv,
    317  1.33   xtraeme 				    "zone went critical at temp %sC\n",
    318  1.13     kochi 				    acpitz_celcius_string(tmp));
    319  1.22   xtraeme 			} else if (changed & ATZ_F_HOT) {
    320  1.49    cegger 				sc->sc_temp_sensor.state = ENVSYS_SCRITOVER;
    321  1.39  pgoyette 				aprint_debug_dev(dv,
    322  1.33   xtraeme 				    "zone went hot at temp %sC\n",
    323  1.13     kochi 				    acpitz_celcius_string(tmp));
    324  1.22   xtraeme 			}
    325  1.13     kochi 		}
    326  1.13     kochi 
    327  1.13     kochi 		/* power on fans */
    328  1.13     kochi 		if (sc->sc_active != active) {
    329  1.13     kochi 			if (sc->sc_active != ATZ_ACTIVE_NONE)
    330  1.13     kochi 				acpitz_power_zone(sc, sc->sc_active, 0);
    331  1.13     kochi 
    332  1.13     kochi 			if (active != ATZ_ACTIVE_NONE) {
    333  1.13     kochi 				if (sc->sc_flags & ATZ_F_VERBOSE)
    334  1.13     kochi 					printf("%s: active cooling level %u\n",
    335  1.33   xtraeme 					    device_xname(dv), active);
    336  1.13     kochi 				acpitz_power_zone(sc, active, 1);
    337  1.52  pgoyette 			} else if (sc->sc_flags & ATZ_F_VERBOSE)
    338  1.52  pgoyette 				printf("%s: no active cooling level\n",
    339  1.52  pgoyette 				    device_xname(dv));
    340  1.13     kochi 
    341  1.13     kochi 			sc->sc_active = active;
    342  1.13     kochi 		}
    343  1.13     kochi 	}
    344  1.13     kochi 
    345   1.1  jmcneill 	return;
    346   1.1  jmcneill }
    347   1.1  jmcneill 
    348  1.13     kochi static char *
    349  1.13     kochi acpitz_celcius_string(int dk)
    350  1.13     kochi {
    351  1.13     kochi 	static char buf[10];
    352  1.59  pgoyette 	int dc;
    353  1.13     kochi 
    354  1.59  pgoyette 	dc = abs(dk - ATZ_ZEROC);
    355  1.59  pgoyette 	snprintf(buf, sizeof(buf), "%s%d.%d", (dk >= ATZ_ZEROC)?"":"-",
    356  1.59  pgoyette 		dc / 10, dc % 10);
    357  1.13     kochi 
    358  1.13     kochi 	return buf;
    359  1.13     kochi }
    360  1.13     kochi 
    361   1.1  jmcneill static void
    362  1.33   xtraeme acpitz_print_status(device_t dv)
    363   1.1  jmcneill {
    364  1.33   xtraeme 	struct acpitz_softc *sc = device_private(dv);
    365   1.1  jmcneill 
    366  1.33   xtraeme 	printf("%s: zone temperature is now %sC\n", device_xname(dv),
    367  1.13     kochi 	    acpitz_celcius_string(sc->sc_zone.tmp));
    368  1.49    cegger 	if (sc->sc_have_fan) {
    369  1.49    cegger 		printf("%s: fan rpm %u\n", device_xname(dv),
    370  1.49    cegger 		    sc->sc_zone.fancurrent);
    371  1.49    cegger 	}
    372   1.1  jmcneill 
    373   1.1  jmcneill 	return;
    374   1.1  jmcneill }
    375   1.1  jmcneill 
    376  1.13     kochi static ACPI_STATUS
    377  1.13     kochi acpitz_switch_cooler(ACPI_OBJECT *obj, void *arg)
    378  1.13     kochi {
    379  1.13     kochi 	ACPI_HANDLE cooler;
    380  1.13     kochi 	ACPI_STATUS rv;
    381  1.13     kochi 	int pwr_state, flag;
    382  1.13     kochi 
    383  1.13     kochi 	flag = *(int *)arg;
    384  1.13     kochi 
    385  1.13     kochi 	if (flag)
    386  1.13     kochi 		pwr_state = ACPI_STATE_D0;
    387  1.13     kochi 	else
    388  1.13     kochi 		pwr_state = ACPI_STATE_D3;
    389  1.13     kochi 
    390  1.56    jruoho 	rv = acpi_eval_reference_handle(obj, &cooler);
    391  1.56    jruoho 	if (ACPI_FAILURE(rv)) {
    392  1.56    jruoho 		aprint_error("%s: failed to get handle\n", __func__);
    393  1.56    jruoho 		return rv;
    394  1.13     kochi 	}
    395  1.13     kochi 
    396  1.13     kochi 	rv = acpi_pwr_switch_consumer(cooler, pwr_state);
    397  1.55    jruoho 	if (rv != AE_BAD_PARAMETER && ACPI_FAILURE(rv))
    398  1.55    jruoho 		aprint_error("%s: failed to change state for %s: %s\n",
    399  1.55    jruoho 		    __func__, acpi_name(cooler), AcpiFormatException(rv));
    400  1.13     kochi 
    401  1.13     kochi 	return AE_OK;
    402  1.13     kochi }
    403  1.13     kochi 
    404  1.13     kochi /*
    405  1.13     kochi  * acpitz_power_zone:
    406  1.13     kochi  *	power on or off the i:th part of the zone zone
    407  1.13     kochi  */
    408  1.13     kochi static void
    409  1.13     kochi acpitz_power_zone(struct acpitz_softc *sc, int i, int on)
    410  1.13     kochi {
    411  1.13     kochi 	KASSERT(i >= 0 && i < ATZ_NLEVELS);
    412  1.13     kochi 
    413  1.13     kochi 	acpi_foreach_package_object(sc->sc_zone.al[i].Pointer,
    414  1.13     kochi 	    acpitz_switch_cooler, &on);
    415  1.13     kochi }
    416  1.13     kochi 
    417  1.13     kochi 
    418  1.13     kochi /*
    419  1.13     kochi  * acpitz_power_off:
    420  1.13     kochi  *	power off parts of the zone
    421  1.13     kochi  */
    422  1.13     kochi static void
    423  1.13     kochi acpitz_power_off(struct acpitz_softc *sc)
    424  1.13     kochi {
    425  1.13     kochi 	int i;
    426  1.13     kochi 
    427  1.13     kochi 	for (i = 0 ; i < ATZ_NLEVELS; i++) {
    428  1.13     kochi 		if (sc->sc_zone.al[i].Pointer == NULL)
    429  1.13     kochi 			continue;
    430  1.13     kochi 		acpitz_power_zone(sc, i, 0);
    431  1.13     kochi 	}
    432  1.13     kochi 	sc->sc_active = ATZ_ACTIVE_NONE;
    433  1.13     kochi 	sc->sc_flags &= ~(ATZ_F_CRITICAL|ATZ_F_HOT|ATZ_F_PASSIVE);
    434  1.13     kochi }
    435  1.13     kochi 
    436  1.13     kochi static void
    437  1.14    martin acpitz_get_zone(void *opaque, int verbose)
    438  1.13     kochi {
    439  1.33   xtraeme 	device_t dv = opaque;
    440  1.33   xtraeme 	struct acpitz_softc *sc = device_private(dv);
    441  1.13     kochi 	ACPI_STATUS rv;
    442  1.13     kochi 	char buf[8];
    443  1.13     kochi 	int i, valid_levels;
    444  1.13     kochi 
    445  1.29  jmcneill 	if (!sc->sc_first) {
    446  1.13     kochi 		acpitz_power_off(sc);
    447  1.13     kochi 
    448  1.13     kochi 		for (i = 0; i < ATZ_NLEVELS; i++) {
    449  1.13     kochi 			if (sc->sc_zone.al[i].Pointer != NULL)
    450  1.47   mlelstv 				ACPI_FREE(sc->sc_zone.al[i].Pointer);
    451  1.13     kochi 			sc->sc_zone.al[i].Pointer = NULL;
    452  1.13     kochi 		}
    453  1.63    jruoho 	}
    454  1.13     kochi 
    455  1.13     kochi 	valid_levels = 0;
    456  1.13     kochi 
    457  1.13     kochi 	for (i = 0; i < ATZ_NLEVELS; i++) {
    458  1.13     kochi 		ACPI_OBJECT *obj;
    459  1.13     kochi 
    460  1.13     kochi 		snprintf(buf, sizeof(buf), "_AC%d", i);
    461  1.33   xtraeme 		if (acpitz_get_integer(dv, buf, &sc->sc_zone.ac[i]))
    462  1.13     kochi 			continue;
    463  1.13     kochi 
    464  1.13     kochi 		snprintf(buf, sizeof(buf), "_AL%d", i);
    465  1.13     kochi 		rv = acpi_eval_struct(sc->sc_devnode->ad_handle, buf,
    466  1.13     kochi 		    &sc->sc_zone.al[i]);
    467  1.13     kochi 		if (ACPI_FAILURE(rv)) {
    468  1.13     kochi 			sc->sc_zone.al[i].Pointer = NULL;
    469  1.13     kochi 			continue;
    470  1.13     kochi 		}
    471  1.13     kochi 
    472  1.13     kochi 		obj = sc->sc_zone.al[i].Pointer;
    473  1.13     kochi 		if (obj != NULL) {
    474  1.13     kochi 			if (obj->Type != ACPI_TYPE_PACKAGE) {
    475  1.29  jmcneill 				aprint_error("%d not package\n", i);
    476  1.47   mlelstv 				ACPI_FREE(obj);
    477  1.13     kochi 				sc->sc_zone.al[i].Pointer = NULL;
    478  1.13     kochi 				continue;
    479  1.13     kochi 			}
    480  1.13     kochi 		}
    481  1.13     kochi 
    482  1.29  jmcneill 		if (sc->sc_first)
    483  1.29  jmcneill 			aprint_normal(" active cooling level %d: %sC", i,
    484  1.13     kochi 			    acpitz_celcius_string(sc->sc_zone.ac[i]));
    485  1.13     kochi 
    486  1.13     kochi 		valid_levels++;
    487  1.13     kochi 	}
    488  1.13     kochi 
    489  1.33   xtraeme 	acpitz_get_integer(dv, "_TMP", &sc->sc_zone.tmp);
    490  1.33   xtraeme 	acpitz_get_integer(dv, "_CRT", &sc->sc_zone.crt);
    491  1.33   xtraeme 	acpitz_get_integer(dv, "_HOT", &sc->sc_zone.hot);
    492  1.54    jruoho 	acpitz_get_integer(dv, "_PSV", &sc->sc_zone.psv);
    493  1.54    jruoho 	acpitz_get_integer(dv, "_TC1", &sc->sc_zone.tc1);
    494  1.54    jruoho 	acpitz_get_integer(dv, "_TC2", &sc->sc_zone.tc2);
    495  1.54    jruoho 
    496  1.54    jruoho #if 0
    497  1.47   mlelstv 	sc->sc_zone.psl.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
    498  1.13     kochi 	sc->sc_zone.psl.Pointer = NULL;
    499  1.49    cegger 	AcpiEvaluateObject(sc->sc_devnode->ad_handle,
    500  1.49    cegger 	    "_PSL", NULL, &sc->sc_zone.psl);
    501  1.54    jruoho #endif
    502  1.13     kochi 
    503  1.48    cegger 	/* ACPI spec: If _RTV is not present or present and zero,
    504  1.48    cegger 	 * values are absolute. */
    505  1.48    cegger 	acpitz_get_integer(dv, "_RTV", &sc->sc_zone.rtv);
    506  1.48    cegger 	if (sc->sc_zone.rtv == ATZ_TMP_INVALID)
    507  1.48    cegger 		sc->sc_zone.rtv = 0;
    508  1.48    cegger 
    509  1.48    cegger 
    510  1.13     kochi 	acpitz_sane_temp(&sc->sc_zone.tmp);
    511  1.13     kochi 	acpitz_sane_temp(&sc->sc_zone.crt);
    512  1.13     kochi 	acpitz_sane_temp(&sc->sc_zone.hot);
    513  1.13     kochi 	acpitz_sane_temp(&sc->sc_zone.psv);
    514  1.13     kochi 
    515  1.63    jruoho 	if (verbose != 0) {
    516  1.63    jruoho 		aprint_verbose_dev(dv, "");
    517  1.63    jruoho 
    518  1.14    martin 		if (sc->sc_zone.crt != ATZ_TMP_INVALID)
    519  1.63    jruoho 			aprint_verbose("critical %s C",
    520  1.14    martin 			    acpitz_celcius_string(sc->sc_zone.crt));
    521  1.63    jruoho 
    522  1.14    martin 		if (sc->sc_zone.hot != ATZ_TMP_INVALID)
    523  1.63    jruoho 			aprint_verbose(" hot %s C",
    524  1.14    martin 			    acpitz_celcius_string(sc->sc_zone.hot));
    525  1.63    jruoho 
    526  1.14    martin 		if (sc->sc_zone.psv != ATZ_TMP_INVALID)
    527  1.63    jruoho 			aprint_normal(" passive %s C",
    528  1.59  pgoyette 			    acpitz_celcius_string(sc->sc_zone.psv));
    529  1.29  jmcneill 	}
    530  1.29  jmcneill 
    531  1.29  jmcneill 	if (valid_levels == 0) {
    532  1.29  jmcneill 		sc->sc_flags |= ATZ_F_PASSIVEONLY;
    533  1.63    jruoho 
    534  1.29  jmcneill 		if (sc->sc_first)
    535  1.63    jruoho 			aprint_verbose(", passive cooling");
    536  1.29  jmcneill 	}
    537  1.63    jruoho 
    538  1.63    jruoho 	if (verbose != 0)
    539  1.63    jruoho 		aprint_verbose("\n");
    540  1.13     kochi 
    541  1.13     kochi 	for (i = 0; i < ATZ_NLEVELS; i++)
    542  1.13     kochi 		acpitz_sane_temp(&sc->sc_zone.ac[i]);
    543  1.13     kochi 
    544  1.13     kochi 	acpitz_power_off(sc);
    545  1.29  jmcneill 	sc->sc_first = 0;
    546  1.13     kochi }
    547  1.13     kochi 
    548  1.11     kochi static void
    549  1.64    jruoho acpitz_notify_handler(ACPI_HANDLE hdl, uint32_t notify, void *opaque)
    550   1.1  jmcneill {
    551  1.33   xtraeme 	device_t dv = opaque;
    552  1.15     kochi 	ACPI_OSD_EXEC_CALLBACK func = NULL;
    553  1.13     kochi 	const char *name;
    554  1.46  jmcneill 	ACPI_STATUS rv;
    555   1.1  jmcneill 
    556   1.1  jmcneill 	switch (notify) {
    557   1.1  jmcneill 	case ACPI_NOTIFY_ThermalZoneStatusChanged:
    558  1.13     kochi 		func = acpitz_get_status;
    559  1.13     kochi 		name = "status check";
    560  1.13     kochi 		break;
    561   1.1  jmcneill 	case ACPI_NOTIFY_ThermalZoneTripPointsChanged:
    562  1.13     kochi 	case ACPI_NOTIFY_DeviceListsChanged:
    563  1.14    martin 		func = acpitz_get_zone_quiet;
    564  1.13     kochi 		name = "get zone";
    565   1.1  jmcneill 		break;
    566   1.1  jmcneill 	default:
    567  1.33   xtraeme 		aprint_debug_dev(dv,
    568  1.33   xtraeme 		    "received unhandled notify message 0x%x\n", notify);
    569  1.13     kochi 		return;
    570   1.1  jmcneill 	}
    571   1.1  jmcneill 
    572  1.13     kochi 	KASSERT(func != NULL);
    573  1.13     kochi 
    574  1.33   xtraeme 	rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, func, dv);
    575  1.46  jmcneill 	if (ACPI_FAILURE(rv))
    576  1.33   xtraeme 		aprint_debug_dev(dv, "unable to queue %s\n", name);
    577   1.1  jmcneill }
    578   1.1  jmcneill 
    579   1.1  jmcneill static void
    580  1.64    jruoho acpitz_sane_temp(uint32_t *tmp)
    581  1.13     kochi {
    582  1.13     kochi 	/* Sane temperatures are beteen 0 and 150 C */
    583  1.13     kochi 	if (*tmp < ATZ_ZEROC || *tmp > ATZ_ZEROC + 1500)
    584  1.13     kochi 		*tmp = ATZ_TMP_INVALID;
    585  1.13     kochi }
    586  1.13     kochi 
    587  1.13     kochi static int
    588  1.64    jruoho acpitz_get_integer(device_t dv, const char *cm, uint32_t *val)
    589  1.13     kochi {
    590  1.33   xtraeme 	struct acpitz_softc *sc = device_private(dv);
    591  1.13     kochi 	ACPI_STATUS rv;
    592  1.13     kochi 	ACPI_INTEGER tmp;
    593  1.13     kochi 
    594  1.13     kochi 	rv = acpi_eval_integer(sc->sc_devnode->ad_handle, cm, &tmp);
    595  1.63    jruoho 
    596  1.13     kochi 	if (ACPI_FAILURE(rv)) {
    597  1.13     kochi 		*val = ATZ_TMP_INVALID;
    598  1.63    jruoho 
    599  1.63    jruoho 		if (rv != AE_NOT_FOUND)
    600  1.63    jruoho 			aprint_debug_dev(dv, "failed to evaluate %s: %s\n",
    601  1.63    jruoho 			    cm, AcpiFormatException(rv));
    602  1.63    jruoho 
    603  1.13     kochi 		return 1;
    604  1.13     kochi 	}
    605  1.13     kochi 
    606  1.13     kochi 	*val = tmp;
    607  1.13     kochi 
    608  1.13     kochi 	return 0;
    609  1.13     kochi }
    610  1.13     kochi 
    611  1.49    cegger static int
    612  1.49    cegger acpitz_get_fanspeed(device_t dv,
    613  1.64    jruoho     uint32_t *fanmin, uint32_t *fanmax, uint32_t *fancurrent)
    614  1.49    cegger {
    615  1.49    cegger 	struct acpitz_softc *sc = device_private(dv);
    616  1.49    cegger 	ACPI_STATUS rv;
    617  1.49    cegger 	ACPI_HANDLE handle;
    618  1.49    cegger 	ACPI_INTEGER fmin, fmax, fcurr;
    619  1.49    cegger 	int rc = 0;
    620  1.49    cegger 
    621  1.49    cegger 	handle = sc->sc_devnode->ad_handle;
    622  1.49    cegger 	rv = acpi_eval_integer(handle, "FMIN", &fmin);
    623  1.49    cegger 	if (ACPI_FAILURE(rv)) {
    624  1.49    cegger 		fmin = ATZ_TMP_INVALID;
    625  1.49    cegger 		rc = 1;
    626  1.49    cegger 	}
    627  1.49    cegger 	rv = acpi_eval_integer(handle, "FMAX", &fmax);
    628  1.49    cegger 	if (ACPI_FAILURE(rv)) {
    629  1.49    cegger 		fmax = ATZ_TMP_INVALID;
    630  1.49    cegger 		rc = 1;
    631  1.49    cegger 	}
    632  1.49    cegger 	rv = acpi_eval_integer(handle, "FRSP", &fcurr);
    633  1.49    cegger 	if (ACPI_FAILURE(rv)) {
    634  1.49    cegger 		fcurr = ATZ_TMP_INVALID;
    635  1.49    cegger 		rc = 1;
    636  1.49    cegger 	}
    637  1.49    cegger 
    638  1.49    cegger 	if (fanmin)
    639  1.49    cegger 		*fanmin = fmin;
    640  1.49    cegger 	if (fanmax)
    641  1.49    cegger 		*fanmax = fmax;
    642  1.49    cegger 	if (fancurrent)
    643  1.49    cegger 		*fancurrent = fcurr;
    644  1.49    cegger 	return rc;
    645  1.49    cegger }
    646  1.49    cegger 
    647  1.49    cegger #ifdef notyet
    648  1.49    cegger static ACPI_STATUS
    649  1.64    jruoho acpitz_set_fanspeed(device_t dv, uint32_t fanspeed)
    650  1.49    cegger {
    651  1.49    cegger 	struct acpitz_softc *sc = device_private(dv);
    652  1.49    cegger 	ACPI_STATUS rv;
    653  1.49    cegger 	ACPI_HANDLE handle;
    654  1.49    cegger 	handle = sc->sc_devnode->ad_handle;
    655  1.49    cegger 
    656  1.49    cegger 	rv = acpi_eval_set_integer(handle, "FSSP", fanspeed);
    657  1.49    cegger 	if (ACPI_FAILURE(rv))
    658  1.49    cegger 		aprint_debug_dev(dv, "failed to set fanspeed to %u rpm: %s\n",
    659  1.49    cegger 			fanspeed, AcpiFormatException(rv));
    660  1.49    cegger 	return rv;
    661  1.49    cegger }
    662  1.49    cegger #endif
    663  1.49    cegger 
    664  1.13     kochi static void
    665   1.1  jmcneill acpitz_tick(void *opaque)
    666   1.1  jmcneill {
    667  1.33   xtraeme 	device_t dv = opaque;
    668  1.33   xtraeme 	struct acpitz_softc *sc = device_private(dv);
    669   1.1  jmcneill 
    670  1.33   xtraeme 	AcpiOsExecute(OSL_NOTIFY_HANDLER, acpitz_get_status, dv);
    671   1.1  jmcneill 
    672  1.25     joerg 	callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10);
    673   1.1  jmcneill }
    674   1.1  jmcneill 
    675   1.1  jmcneill static void
    676  1.33   xtraeme acpitz_init_envsys(device_t dv)
    677   1.1  jmcneill {
    678  1.33   xtraeme 	struct acpitz_softc *sc = device_private(dv);
    679  1.33   xtraeme 
    680  1.27   xtraeme 	sc->sc_sme = sysmon_envsys_create();
    681  1.42  pgoyette 	sc->sc_sme->sme_get_limits = acpitz_get_limits;
    682  1.42  pgoyette 	sc->sc_sme->sme_cookie = sc;
    683  1.42  pgoyette 	sc->sc_sme->sme_name = device_xname(dv);
    684  1.42  pgoyette 	sc->sc_sme->sme_flags = SME_DISABLE_REFRESH;
    685  1.49    cegger 
    686  1.49    cegger 	sc->sc_temp_sensor.flags = ENVSYS_FMONLIMITS | ENVSYS_FMONNOTSUPP;
    687  1.49    cegger 	sc->sc_temp_sensor.units = ENVSYS_STEMP;
    688  1.49    cegger 	strlcpy(sc->sc_temp_sensor.desc,
    689  1.49    cegger 	    sc->sc_zone.name, sizeof(sc->sc_temp_sensor.desc));
    690  1.49    cegger 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_temp_sensor))
    691  1.42  pgoyette 		goto out;
    692  1.42  pgoyette 
    693  1.49    cegger 	if (sc->sc_have_fan) {
    694  1.49    cegger 		sc->sc_fan_sensor.flags =
    695  1.49    cegger 		    ENVSYS_FMONLIMITS | ENVSYS_FMONNOTSUPP;
    696  1.49    cegger 		sc->sc_fan_sensor.units = ENVSYS_SFANRPM;
    697  1.49    cegger 		strlcpy(sc->sc_fan_sensor.desc,
    698  1.49    cegger 		    "FAN", sizeof(sc->sc_fan_sensor.desc));
    699  1.49    cegger 		if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_fan_sensor))
    700  1.49    cegger 			/* ignore error because fan sensor is optional */
    701  1.49    cegger 			aprint_error_dev(dv, "unable to attach fan sensor\n");
    702  1.49    cegger 	}
    703  1.49    cegger 
    704  1.42  pgoyette 	/* hook into sysmon */
    705  1.42  pgoyette 	if (sysmon_envsys_register(sc->sc_sme) == 0)
    706  1.27   xtraeme 		return;
    707  1.49    cegger 
    708  1.42  pgoyette out:
    709  1.42  pgoyette 	aprint_error_dev(dv, "unable to register with sysmon\n");
    710  1.42  pgoyette 	sysmon_envsys_destroy(sc->sc_sme);
    711  1.42  pgoyette }
    712  1.42  pgoyette 
    713  1.42  pgoyette static void
    714  1.42  pgoyette acpitz_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
    715  1.58  pgoyette 		  sysmon_envsys_lim_t *limits, uint32_t *props)
    716  1.42  pgoyette {
    717  1.42  pgoyette 	struct acpitz_softc *sc = sme->sme_cookie;
    718  1.42  pgoyette 	int i;
    719   1.1  jmcneill 
    720  1.49    cegger 	switch (edata->units) {
    721  1.49    cegger 	case ENVSYS_STEMP:
    722  1.58  pgoyette 		*props = 0;
    723  1.49    cegger 		if (sc->sc_zone.hot != ATZ_TMP_INVALID) {
    724  1.58  pgoyette 			*props |= PROP_CRITMAX;
    725  1.49    cegger 			limits->sel_critmax = ATZ2UKELVIN(sc->sc_zone.hot);
    726  1.49    cegger 		} else if (sc->sc_zone.crt != ATZ_TMP_INVALID) {
    727  1.58  pgoyette 			*props |= PROP_CRITMAX;
    728  1.49    cegger 			limits->sel_critmax = ATZ2UKELVIN(sc->sc_zone.crt);
    729  1.49    cegger 		}
    730  1.49    cegger 		for (i = 0; i < ATZ_NLEVELS; i++) {
    731  1.49    cegger 			if (sc->sc_zone.ac[i] != ATZ_TMP_INVALID) {
    732  1.51  pgoyette 				limits->sel_warnmax =
    733  1.49    cegger 				    ATZ2UKELVIN(sc->sc_zone.ac[i]);
    734  1.58  pgoyette 				*props |= PROP_WARNMAX;
    735  1.49    cegger 				break;
    736  1.49    cegger 			}
    737  1.49    cegger 		}
    738  1.49    cegger 		break;
    739  1.49    cegger 
    740  1.49    cegger 	case ENVSYS_SFANRPM:
    741  1.58  pgoyette 		*props = 0;
    742  1.49    cegger 		if (sc->sc_zone.fanmin != ATZ_TMP_INVALID) {
    743  1.58  pgoyette 			*props |= PROP_WARNMIN;
    744  1.49    cegger 			limits->sel_warnmin = sc->sc_zone.fanmin;
    745  1.49    cegger 			sc->sc_fan_sensor.flags |= ENVSYS_FVALID_MIN;
    746  1.49    cegger 		}
    747  1.49    cegger 		if (sc->sc_zone.fanmax != ATZ_TMP_INVALID) {
    748  1.58  pgoyette 			*props |= PROP_WARNMAX;
    749  1.49    cegger 			limits->sel_warnmax = sc->sc_zone.fanmax;
    750  1.49    cegger 			sc->sc_fan_sensor.flags |= ENVSYS_FVALID_MAX;
    751  1.42  pgoyette 		}
    752  1.49    cegger 		break;
    753  1.49    cegger 	}
    754   1.1  jmcneill }
    755