Home | History | Annotate | Line # | Download | only in acpi
acpi_tz.c revision 1.11
      1  1.11     kochi /* $NetBSD: acpi_tz.c,v 1.11 2004/05/01 12:03:48 kochi 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.11     kochi __KERNEL_RCSID(0, "$NetBSD: acpi_tz.c,v 1.11 2004/05/01 12:03:48 kochi Exp $");
     34   1.1  jmcneill 
     35   1.1  jmcneill #include <sys/param.h>
     36   1.1  jmcneill #include <sys/systm.h>
     37   1.1  jmcneill #include <sys/kernel.h>
     38   1.1  jmcneill #include <sys/errno.h>
     39   1.1  jmcneill #include <sys/ioctl.h>
     40   1.1  jmcneill #include <sys/syslog.h>
     41   1.1  jmcneill #include <sys/device.h>
     42   1.1  jmcneill #include <sys/callout.h>
     43   1.1  jmcneill #include <sys/proc.h>
     44   1.1  jmcneill #include <sys/lock.h>
     45   1.1  jmcneill #include <dev/sysmon/sysmonvar.h>
     46   1.1  jmcneill 
     47   1.1  jmcneill #include <dev/acpi/acpica.h>
     48   1.1  jmcneill #include <dev/acpi/acpireg.h>
     49   1.1  jmcneill #include <dev/acpi/acpivar.h>
     50   1.1  jmcneill 
     51   1.1  jmcneill /* flags */
     52   1.1  jmcneill #define ATZ_F_VERBOSE		0x01	/* show events to console */
     53   1.1  jmcneill 
     54   1.1  jmcneill /* constants */
     55   1.1  jmcneill #define ATZ_TZP_RATE	300	/* default if no _TZP CM present (30 secs) */
     56   1.1  jmcneill #define ATZ_NLEVELS	10	/* number of cooling levels, from ACPI spec */
     57   1.1  jmcneill 
     58   1.1  jmcneill /* sensor indexes */
     59   1.1  jmcneill #define ATZ_SENSOR_TEMP	0	/* thermal zone temperature */
     60   1.1  jmcneill #define ATZ_NUMSENSORS	1	/* number of sensors */
     61   1.1  jmcneill 
     62  1.11     kochi static const struct envsys_range acpitz_ranges[] = {
     63   1.1  jmcneill 	{ 0, 1,	ATZ_SENSOR_TEMP },
     64   1.1  jmcneill };
     65   1.1  jmcneill 
     66  1.11     kochi static int	acpitz_match(struct device *, struct cfdata *, void *);
     67  1.11     kochi static void	acpitz_attach(struct device *, struct device *, void *);
     68   1.1  jmcneill 
     69   1.1  jmcneill /*
     70   1.1  jmcneill  * ACPI Temperature Zone information. Note all temperatures are reported
     71   1.1  jmcneill  * in tenths of degrees Kelvin
     72   1.1  jmcneill  */
     73   1.1  jmcneill struct acpitz_zone {
     74   1.1  jmcneill 	/* Active cooling temperature threshold */
     75   1.1  jmcneill 	UINT32 ac[ATZ_NLEVELS];
     76   1.1  jmcneill 	/* Package of references to all active cooling devices for a level */
     77   1.1  jmcneill 	ACPI_BUFFER al[ATZ_NLEVELS];
     78   1.1  jmcneill 	/* Critical temperature threshold for system shutdown */
     79   1.1  jmcneill 	UINT32 crt;
     80   1.1  jmcneill 	/* Critical temperature threshold for S4 sleep */
     81   1.1  jmcneill 	UINT32 hot;
     82   1.1  jmcneill 	/* Package of references to processor objects for passive cooling */
     83   1.1  jmcneill 	ACPI_BUFFER psl;
     84   1.1  jmcneill 	/* Passive cooling temperature threshold */
     85   1.1  jmcneill 	UINT32 psv;
     86   1.1  jmcneill 	/* Thermal constants for use in passive cooling formulas */
     87   1.1  jmcneill 	UINT32 tc1, tc2;
     88   1.1  jmcneill 	/* Current temperature of the thermal zone */
     89   1.1  jmcneill 	UINT32 tmp;
     90   1.1  jmcneill 	/* Thermal sampling period for passive cooling, in tenths of seconds */
     91   1.1  jmcneill 	UINT32 tsp;
     92   1.1  jmcneill 	/* Package of references to devices in this TZ (optional) */
     93   1.1  jmcneill 	ACPI_BUFFER tzd;
     94   1.1  jmcneill 	/* Recommended TZ polling frequency, in tenths of seconds */
     95   1.1  jmcneill 	UINT32 tzp;
     96   1.1  jmcneill };
     97   1.1  jmcneill 
     98   1.1  jmcneill struct acpitz_softc {
     99   1.1  jmcneill 	struct device sc_dev;
    100   1.1  jmcneill 	struct acpi_devnode *sc_devnode;
    101   1.1  jmcneill 	struct acpitz_zone sc_zone;
    102   1.1  jmcneill 	struct callout sc_callout;
    103   1.1  jmcneill 	struct envsys_tre_data sc_data[ATZ_NUMSENSORS];
    104   1.1  jmcneill 	struct envsys_basic_info sc_info[ATZ_NUMSENSORS];
    105   1.1  jmcneill 	struct sysmon_envsys sc_sysmon;
    106   1.1  jmcneill 	struct simplelock sc_slock;
    107   1.1  jmcneill 	int sc_flags;
    108   1.1  jmcneill 	int sc_rate;		/* tz poll rate */
    109   1.1  jmcneill };
    110   1.1  jmcneill 
    111  1.11     kochi static void	acpitz_get_status(void *);
    112   1.1  jmcneill static void	acpitz_print_status(struct acpitz_softc *);
    113  1.11     kochi static void	acpitz_notify_handler(ACPI_HANDLE, UINT32, void *);
    114   1.1  jmcneill static void	acpitz_tick(void *);
    115   1.1  jmcneill static void	acpitz_init_envsys(struct acpitz_softc *);
    116   1.1  jmcneill static int	acpitz_gtredata(struct sysmon_envsys *,
    117   1.1  jmcneill 				struct envsys_tre_data *);
    118   1.1  jmcneill static int	acpitz_streinfo(struct sysmon_envsys *,
    119   1.1  jmcneill 				struct envsys_basic_info *);
    120   1.1  jmcneill 
    121   1.1  jmcneill CFATTACH_DECL(acpitz, sizeof(struct acpitz_softc), acpitz_match,
    122   1.1  jmcneill     acpitz_attach, NULL, NULL);
    123   1.1  jmcneill 
    124   1.1  jmcneill /*
    125   1.1  jmcneill  * acpitz_match: autoconf(9) match routine
    126   1.1  jmcneill  */
    127  1.11     kochi static int
    128   1.1  jmcneill acpitz_match(struct device *parent, struct cfdata *match, void *aux)
    129   1.1  jmcneill {
    130   1.1  jmcneill 	struct acpi_attach_args *aa = aux;
    131   1.1  jmcneill 
    132   1.1  jmcneill 	if (aa->aa_node->ad_type != ACPI_TYPE_THERMAL)
    133   1.1  jmcneill 		return 0;
    134   1.1  jmcneill 
    135   1.1  jmcneill 	return 1;
    136   1.1  jmcneill }
    137   1.1  jmcneill 
    138   1.1  jmcneill /*
    139   1.1  jmcneill  * acpitz_attach: autoconf(9) attach routine
    140   1.1  jmcneill  */
    141  1.11     kochi static void
    142   1.1  jmcneill acpitz_attach(struct device *parent, struct device *self, void *aux)
    143   1.1  jmcneill {
    144   1.1  jmcneill 	struct acpitz_softc *sc = (struct acpitz_softc *)self;
    145   1.1  jmcneill 	struct acpi_attach_args *aa = aux;
    146   1.1  jmcneill 	ACPI_STATUS rv;
    147   1.9    martin 	ACPI_INTEGER v;
    148   1.1  jmcneill 
    149   1.1  jmcneill #if 0
    150   1.1  jmcneill 	sc->sc_flags = ATZ_F_VERBOSE;
    151   1.1  jmcneill #endif
    152   1.1  jmcneill 	sc->sc_devnode = aa->aa_node;
    153   1.1  jmcneill 
    154   1.1  jmcneill 	printf(": ACPI Thermal Zone\n");
    155   1.1  jmcneill 
    156   1.9    martin 	rv = acpi_eval_integer(sc->sc_devnode->ad_handle, "_TZP", &v);
    157   1.6   mycroft 	if (ACPI_FAILURE(rv)) {
    158   1.4   mycroft 		printf("%s: unable to get polling interval; using default of",
    159   1.4   mycroft 		    sc->sc_dev.dv_xname);
    160   1.1  jmcneill 		sc->sc_zone.tzp = ATZ_TZP_RATE;
    161   1.3   mycroft 	} else {
    162   1.9    martin 		sc->sc_zone.tzp = v;
    163   1.4   mycroft 		printf("%s: polling interval is", sc->sc_dev.dv_xname);
    164   1.1  jmcneill 	}
    165   1.4   mycroft 	printf(" %d.%ds\n", sc->sc_zone.tzp / 10, sc->sc_zone.tsp % 10);
    166   1.3   mycroft 
    167   1.1  jmcneill 	/* XXX a value of 0 means "polling is not necessary" */
    168   1.1  jmcneill 	if (sc->sc_zone.tzp == 0)
    169   1.1  jmcneill 		sc->sc_zone.tzp = ATZ_TZP_RATE;
    170  1.10     kochi 
    171   1.1  jmcneill 	acpitz_get_status(sc);
    172   1.1  jmcneill 
    173   1.1  jmcneill 	rv = AcpiInstallNotifyHandler(sc->sc_devnode->ad_handle,
    174   1.1  jmcneill 	    ACPI_SYSTEM_NOTIFY, acpitz_notify_handler, sc);
    175   1.6   mycroft 	if (ACPI_FAILURE(rv)) {
    176   1.5   mycroft 		printf("%s: unable to install SYSTEM NOTIFY handler: %s\n",
    177   1.5   mycroft 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    178   1.1  jmcneill 		return;
    179   1.1  jmcneill 	}
    180   1.1  jmcneill 
    181   1.1  jmcneill 	callout_init(&sc->sc_callout);
    182   1.1  jmcneill 	callout_reset(&sc->sc_callout, (sc->sc_zone.tzp / 10) * hz,
    183   1.1  jmcneill 	    acpitz_tick, sc);
    184   1.1  jmcneill 
    185   1.1  jmcneill 	acpitz_init_envsys(sc);
    186   1.1  jmcneill }
    187   1.1  jmcneill 
    188  1.11     kochi static void
    189   1.1  jmcneill acpitz_get_status(void *opaque)
    190   1.1  jmcneill {
    191   1.1  jmcneill 	struct acpitz_softc *sc = opaque;
    192   1.3   mycroft 	ACPI_STATUS rv;
    193   1.9    martin 	ACPI_INTEGER v;
    194   1.3   mycroft 
    195   1.9    martin 	rv = acpi_eval_integer(sc->sc_devnode->ad_handle, "_TMP", &v);
    196   1.6   mycroft 	if (ACPI_FAILURE(rv)) {
    197   1.5   mycroft 		printf("%s: failed to evaluate _TMP: %s\n",
    198   1.5   mycroft 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    199   1.3   mycroft 		return;
    200   1.3   mycroft 	}
    201   1.9    martin 	sc->sc_zone.tmp = v;
    202   1.1  jmcneill 
    203   1.7     soren 	/*
    204   1.7     soren 	 * The temperature unit for envsys(9) is microKelvin, so convert to
    205   1.7     soren 	 * that from ACPI's microKelvin. Also, the ACPI specification assumes
    206   1.8     soren 	 * that K = C + 273.2 rather than the nominal 273.15 used by envsys(9),
    207   1.7     soren 	 * so we correct for that too.
    208   1.7     soren 	 */
    209   1.7     soren 	sc->sc_data[ATZ_SENSOR_TEMP].cur.data_us =
    210   1.8     soren 	    sc->sc_zone.tmp * 100000 - 50000;
    211   1.3   mycroft 	sc->sc_data[ATZ_SENSOR_TEMP].validflags |= ENVSYS_FCURVALID;
    212   1.1  jmcneill 
    213   1.1  jmcneill 	if (sc->sc_flags & ATZ_F_VERBOSE)
    214   1.1  jmcneill 		acpitz_print_status(sc);
    215   1.1  jmcneill 
    216   1.1  jmcneill 	return;
    217   1.1  jmcneill }
    218   1.1  jmcneill 
    219   1.1  jmcneill static void
    220   1.1  jmcneill acpitz_print_status(struct acpitz_softc *sc)
    221   1.1  jmcneill {
    222   1.1  jmcneill 
    223   1.1  jmcneill 	printf("%s: zone temperature is now %d K\n", sc->sc_dev.dv_xname,
    224   1.1  jmcneill 	    sc->sc_zone.tmp / 10);
    225   1.1  jmcneill 
    226   1.1  jmcneill 	return;
    227   1.1  jmcneill }
    228   1.1  jmcneill 
    229  1.11     kochi static void
    230   1.1  jmcneill acpitz_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
    231   1.1  jmcneill {
    232   1.1  jmcneill 	struct acpitz_softc *sc = opaque;
    233   1.1  jmcneill 	int rv;
    234   1.1  jmcneill 
    235   1.1  jmcneill 	switch (notify) {
    236   1.1  jmcneill 	case ACPI_NOTIFY_ThermalZoneStatusChanged:
    237   1.1  jmcneill 	case ACPI_NOTIFY_ThermalZoneTripPointsChanged:
    238   1.1  jmcneill 		rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
    239   1.1  jmcneill 		    acpitz_get_status, sc);
    240   1.6   mycroft 		if (ACPI_FAILURE(rv)) {
    241   1.5   mycroft 			printf("%s: unable to queue status check: %s\n",
    242   1.5   mycroft 			    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    243   1.1  jmcneill 		}
    244   1.1  jmcneill 		break;
    245   1.1  jmcneill 	default:
    246   1.5   mycroft 		printf("%s: received unhandled notify message 0x%x\n",
    247   1.1  jmcneill 		    sc->sc_dev.dv_xname, notify);
    248   1.1  jmcneill 		break;
    249   1.1  jmcneill 	}
    250   1.1  jmcneill 
    251   1.1  jmcneill 	return;
    252   1.1  jmcneill }
    253   1.1  jmcneill 
    254   1.1  jmcneill static void
    255   1.1  jmcneill acpitz_tick(void *opaque)
    256   1.1  jmcneill {
    257   1.1  jmcneill 	struct acpitz_softc *sc = opaque;
    258   1.1  jmcneill 
    259   1.1  jmcneill 	callout_reset(&sc->sc_callout, (sc->sc_zone.tzp / 10) * hz,
    260   1.1  jmcneill 	    acpitz_tick, opaque);
    261   1.1  jmcneill 	AcpiOsQueueForExecution(OSD_PRIORITY_LO, acpitz_get_status, sc);
    262   1.1  jmcneill 
    263   1.1  jmcneill 	return;
    264   1.1  jmcneill }
    265   1.1  jmcneill 
    266   1.1  jmcneill static void
    267   1.1  jmcneill acpitz_init_envsys(struct acpitz_softc *sc)
    268   1.1  jmcneill {
    269   1.1  jmcneill 	int i;
    270   1.1  jmcneill 
    271   1.1  jmcneill 	simple_lock_init(&sc->sc_slock);
    272   1.1  jmcneill 
    273   1.1  jmcneill 	for (i = 0; i < ATZ_NUMSENSORS; i++) {
    274   1.1  jmcneill 		sc->sc_data[i].sensor = sc->sc_info[i].sensor = i;
    275   1.3   mycroft 		sc->sc_data[i].validflags = ENVSYS_FVALID;
    276   1.1  jmcneill 		sc->sc_info[i].validflags = ENVSYS_FVALID;
    277   1.1  jmcneill 		sc->sc_data[i].warnflags = ENVSYS_WARN_OK;
    278   1.1  jmcneill 	}
    279   1.1  jmcneill #define INITDATA(index, unit, string) \
    280   1.1  jmcneill 	sc->sc_data[index].units = unit;				   \
    281   1.1  jmcneill 	sc->sc_info[index].units = unit;				   \
    282   1.1  jmcneill 	snprintf(sc->sc_info[index].desc, sizeof(sc->sc_info[index].desc), \
    283   1.1  jmcneill 	    "%s %s", sc->sc_dev.dv_xname, string);
    284   1.1  jmcneill 
    285   1.1  jmcneill 	INITDATA(ATZ_SENSOR_TEMP, ENVSYS_STEMP, "temperature");
    286   1.1  jmcneill 
    287   1.1  jmcneill 	/* hook into sysmon */
    288   1.1  jmcneill 	sc->sc_sysmon.sme_ranges = acpitz_ranges;
    289   1.1  jmcneill 	sc->sc_sysmon.sme_sensor_info = sc->sc_info;
    290   1.1  jmcneill 	sc->sc_sysmon.sme_sensor_data = sc->sc_data;
    291   1.1  jmcneill 	sc->sc_sysmon.sme_cookie = sc;
    292   1.1  jmcneill 	sc->sc_sysmon.sme_gtredata = acpitz_gtredata;
    293   1.1  jmcneill 	sc->sc_sysmon.sme_streinfo = acpitz_streinfo;
    294   1.1  jmcneill 	sc->sc_sysmon.sme_nsensors = ATZ_NUMSENSORS;
    295   1.1  jmcneill 	sc->sc_sysmon.sme_envsys_version = 1000;
    296   1.1  jmcneill 
    297   1.1  jmcneill 	if (sysmon_envsys_register(&sc->sc_sysmon))
    298   1.1  jmcneill 		printf("%s: unable to register with sysmon\n",
    299   1.1  jmcneill 		    sc->sc_dev.dv_xname);
    300   1.1  jmcneill }
    301   1.1  jmcneill 
    302   1.1  jmcneill int
    303   1.1  jmcneill acpitz_gtredata(struct sysmon_envsys *sme, struct envsys_tre_data *tred)
    304   1.1  jmcneill {
    305   1.1  jmcneill 	struct acpitz_softc *sc = sme->sme_cookie;
    306   1.1  jmcneill 
    307   1.1  jmcneill 	simple_lock(&sc->sc_slock);
    308   1.1  jmcneill 
    309   1.1  jmcneill 	*tred = sc->sc_data[tred->sensor];
    310   1.1  jmcneill 
    311   1.1  jmcneill 	simple_unlock(&sc->sc_slock);
    312   1.1  jmcneill 
    313   1.1  jmcneill 	return 0;
    314   1.1  jmcneill }
    315   1.1  jmcneill 
    316  1.11     kochi static int
    317   1.1  jmcneill acpitz_streinfo(struct sysmon_envsys *sme, struct envsys_basic_info *binfo)
    318   1.1  jmcneill {
    319   1.1  jmcneill 
    320   1.1  jmcneill 	/* XXX not implemented */
    321   1.1  jmcneill 	binfo->validflags = 0;
    322   1.1  jmcneill 
    323   1.1  jmcneill 	return 0;
    324   1.1  jmcneill }
    325