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