Home | History | Annotate | Line # | Download | only in acpi
acpi_tz.c revision 1.8
      1 /* $NetBSD: acpi_tz.c,v 1.8 2004/02/02 10:36:19 soren 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.8 2004/02/02 10:36:19 soren 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 	rv = acpi_eval_integer(sc->sc_devnode->ad_handle, "_TZP",
    156 	    &sc->sc_zone.tzp);
    157 	if (ACPI_FAILURE(rv)) {
    158 		printf("%s: unable to get polling interval; using default of",
    159 		    sc->sc_dev.dv_xname);
    160 		sc->sc_zone.tzp = ATZ_TZP_RATE;
    161 	} else {
    162 		printf("%s: polling interval is", sc->sc_dev.dv_xname);
    163 	}
    164 	printf(" %d.%ds\n", sc->sc_zone.tzp / 10, sc->sc_zone.tsp % 10);
    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 (ACPI_FAILURE(rv)) {
    175 		printf("%s: unable to install SYSTEM NOTIFY handler: %s\n",
    176 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    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 (ACPI_FAILURE(rv)) {
    196 		printf("%s: failed to evaluate _TMP: %s\n",
    197 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    198 		return;
    199 	}
    200 
    201 	/*
    202 	 * The temperature unit for envsys(9) is microKelvin, so convert to
    203 	 * that from ACPI's microKelvin. Also, the ACPI specification assumes
    204 	 * that K = C + 273.2 rather than the nominal 273.15 used by envsys(9),
    205 	 * so we correct for that too.
    206 	 */
    207 	sc->sc_data[ATZ_SENSOR_TEMP].cur.data_us =
    208 	    sc->sc_zone.tmp * 100000 - 50000;
    209 	sc->sc_data[ATZ_SENSOR_TEMP].validflags |= ENVSYS_FCURVALID;
    210 
    211 	if (sc->sc_flags & ATZ_F_VERBOSE)
    212 		acpitz_print_status(sc);
    213 
    214 	return;
    215 }
    216 
    217 static void
    218 acpitz_print_status(struct acpitz_softc *sc)
    219 {
    220 
    221 	printf("%s: zone temperature is now %d K\n", sc->sc_dev.dv_xname,
    222 	    sc->sc_zone.tmp / 10);
    223 
    224 	return;
    225 }
    226 
    227 void
    228 acpitz_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
    229 {
    230 	struct acpitz_softc *sc = opaque;
    231 	int rv;
    232 
    233 	switch (notify) {
    234 	case ACPI_NOTIFY_ThermalZoneStatusChanged:
    235 	case ACPI_NOTIFY_ThermalZoneTripPointsChanged:
    236 		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
    237 		    acpitz_get_status, sc);
    238 		if (ACPI_FAILURE(rv)) {
    239 			printf("%s: unable to queue status check: %s\n",
    240 			    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    241 		}
    242 		break;
    243 	default:
    244 		printf("%s: received unhandled notify message 0x%x\n",
    245 		    sc->sc_dev.dv_xname, notify);
    246 		break;
    247 	}
    248 
    249 	return;
    250 }
    251 
    252 static void
    253 acpitz_tick(void *opaque)
    254 {
    255 	struct acpitz_softc *sc = opaque;
    256 
    257 	callout_reset(&sc->sc_callout, (sc->sc_zone.tzp / 10) * hz,
    258 	    acpitz_tick, opaque);
    259 	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpitz_get_status, sc);
    260 
    261 	return;
    262 }
    263 
    264 static void
    265 acpitz_init_envsys(struct acpitz_softc *sc)
    266 {
    267 	int i;
    268 
    269 	simple_lock_init(&sc->sc_slock);
    270 
    271 	for (i = 0; i < ATZ_NUMSENSORS; i++) {
    272 		sc->sc_data[i].sensor = sc->sc_info[i].sensor = i;
    273 		sc->sc_data[i].validflags = ENVSYS_FVALID;
    274 		sc->sc_info[i].validflags = ENVSYS_FVALID;
    275 		sc->sc_data[i].warnflags = ENVSYS_WARN_OK;
    276 	}
    277 #define INITDATA(index, unit, string) \
    278 	sc->sc_data[index].units = unit;				   \
    279 	sc->sc_info[index].units = unit;				   \
    280 	snprintf(sc->sc_info[index].desc, sizeof(sc->sc_info[index].desc), \
    281 	    "%s %s", sc->sc_dev.dv_xname, string);
    282 
    283 	INITDATA(ATZ_SENSOR_TEMP, ENVSYS_STEMP, "temperature");
    284 
    285 	/* hook into sysmon */
    286 	sc->sc_sysmon.sme_ranges = acpitz_ranges;
    287 	sc->sc_sysmon.sme_sensor_info = sc->sc_info;
    288 	sc->sc_sysmon.sme_sensor_data = sc->sc_data;
    289 	sc->sc_sysmon.sme_cookie = sc;
    290 	sc->sc_sysmon.sme_gtredata = acpitz_gtredata;
    291 	sc->sc_sysmon.sme_streinfo = acpitz_streinfo;
    292 	sc->sc_sysmon.sme_nsensors = ATZ_NUMSENSORS;
    293 	sc->sc_sysmon.sme_envsys_version = 1000;
    294 
    295 	if (sysmon_envsys_register(&sc->sc_sysmon))
    296 		printf("%s: unable to register with sysmon\n",
    297 		    sc->sc_dev.dv_xname);
    298 }
    299 
    300 int
    301 acpitz_gtredata(struct sysmon_envsys *sme, struct envsys_tre_data *tred)
    302 {
    303 	struct acpitz_softc *sc = sme->sme_cookie;
    304 
    305 	simple_lock(&sc->sc_slock);
    306 
    307 	*tred = sc->sc_data[tred->sensor];
    308 
    309 	simple_unlock(&sc->sc_slock);
    310 
    311 	return 0;
    312 }
    313 
    314 int
    315 acpitz_streinfo(struct sysmon_envsys *sme, struct envsys_basic_info *binfo)
    316 {
    317 
    318 	/* XXX not implemented */
    319 	binfo->validflags = 0;
    320 
    321 	return 0;
    322 }
    323