acpi_tz.c revision 1.36 1 /* $NetBSD: acpi_tz.c,v 1.36 2008/04/14 00:30:30 jmcneill 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.36 2008/04/14 00:30:30 jmcneill 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/mutex.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 #define ATZ_F_CRITICAL 0x02 /* zone critical */
54 #define ATZ_F_HOT 0x04 /* zone hot */
55 #define ATZ_F_PASSIVE 0x08 /* zone passive cooling */
56 #define ATZ_F_PASSIVEONLY 0x10 /* zone is passive cooling only */
57
58 /* no active cooling level */
59 #define ATZ_ACTIVE_NONE -1
60
61 /* constants */
62 #define ATZ_TZP_RATE 300 /* default if no _TZP CM present (30 secs) */
63 #define ATZ_NLEVELS 10 /* number of cooling levels, from ACPI spec */
64 #define ATZ_ZEROC 2732 /* 0C in tenths degrees Kelvin */
65 #define ATZ_TMP_INVALID 0xffffffff /* invalid temperature */
66 #define ATZ_ZONE_EXPIRE 9000 /* zone info refetch interval (15min) */
67
68 /* sensor indexes */
69 #define ATZ_SENSOR_TEMP 0 /* thermal zone temperature */
70
71 static int acpitz_match(device_t, cfdata_t, void *);
72 static void acpitz_attach(device_t, device_t, void *);
73
74 /*
75 * ACPI Temperature Zone information. Note all temperatures are reported
76 * in tenths of degrees Kelvin
77 */
78 struct acpitz_zone {
79 /* Active cooling temperature threshold */
80 UINT32 ac[ATZ_NLEVELS];
81 /* Package of references to all active cooling devices for a level */
82 ACPI_BUFFER al[ATZ_NLEVELS];
83 /* Critical temperature threshold for system shutdown */
84 UINT32 crt;
85 /* Critical temperature threshold for S4 sleep */
86 UINT32 hot;
87 /* Package of references to processor objects for passive cooling */
88 ACPI_BUFFER psl;
89 /* Passive cooling temperature threshold */
90 UINT32 psv;
91 /* Thermal constants for use in passive cooling formulas */
92 UINT32 tc1, tc2;
93 /* Current temperature of the thermal zone */
94 UINT32 tmp;
95 /* Thermal sampling period for passive cooling, in tenths of seconds */
96 UINT32 tsp;
97 /* Package of references to devices in this TZ (optional) */
98 ACPI_BUFFER tzd;
99 /* Recommended TZ polling frequency, in tenths of seconds */
100 UINT32 tzp;
101 };
102
103 struct acpitz_softc {
104 struct acpi_devnode *sc_devnode;
105 struct acpitz_zone sc_zone;
106 struct callout sc_callout;
107 struct sysmon_envsys *sc_sme;
108 envsys_data_t sc_sensor;
109 kmutex_t sc_mtx;
110 int sc_active; /* active cooling level */
111 int sc_flags;
112 int sc_rate; /* tz poll rate */
113 int sc_zone_expire;
114
115 int sc_first;
116 };
117
118 static void acpitz_get_status(void *);
119 static void acpitz_get_zone(void *, int);
120 static void acpitz_get_zone_quiet(void *);
121 static char *acpitz_celcius_string(int);
122 static void acpitz_print_status(device_t);
123 static void acpitz_power_off(struct acpitz_softc *);
124 static void acpitz_power_zone(struct acpitz_softc *, int, int);
125 static void acpitz_sane_temp(UINT32 *tmp);
126 static ACPI_STATUS
127 acpitz_switch_cooler(ACPI_OBJECT *, void *);
128 static void acpitz_notify_handler(ACPI_HANDLE, UINT32, void *);
129 static int acpitz_get_integer(device_t, const char *, UINT32 *);
130 static void acpitz_tick(void *);
131 static void acpitz_init_envsys(device_t);
132
133 CFATTACH_DECL_NEW(acpitz, sizeof(struct acpitz_softc), acpitz_match,
134 acpitz_attach, NULL, NULL);
135
136 /*
137 * acpitz_match: autoconf(9) match routine
138 */
139 static int
140 acpitz_match(device_t parent, cfdata_t match, void *aux)
141 {
142 struct acpi_attach_args *aa = aux;
143
144 if (aa->aa_node->ad_type != ACPI_TYPE_THERMAL)
145 return 0;
146
147 return 1;
148 }
149
150 /*
151 * acpitz_attach: autoconf(9) attach routine
152 */
153 static void
154 acpitz_attach(device_t parent, device_t self, void *aux)
155 {
156 struct acpitz_softc *sc = device_private(self);
157 struct acpi_attach_args *aa = aux;
158 ACPI_STATUS rv;
159 ACPI_INTEGER v;
160
161 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
162
163 #if 0
164 sc->sc_flags = ATZ_F_VERBOSE;
165 #endif
166 sc->sc_devnode = aa->aa_node;
167
168 aprint_naive("\n");
169
170 rv = acpi_eval_integer(sc->sc_devnode->ad_handle, "_TZP", &v);
171 if (ACPI_FAILURE(rv))
172 sc->sc_zone.tzp = ATZ_TZP_RATE;
173 else
174 sc->sc_zone.tzp = v;
175
176 aprint_debug(" sample rate %d.%ds\n",
177 sc->sc_zone.tzp / 10, sc->sc_zone.tzp % 10);
178
179 /* XXX a value of 0 means "polling is not necessary" */
180 if (sc->sc_zone.tzp == 0)
181 sc->sc_zone.tzp = ATZ_TZP_RATE;
182
183 sc->sc_zone_expire = ATZ_ZONE_EXPIRE / sc->sc_zone.tzp;
184 sc->sc_first = 1;
185
186 acpitz_get_zone(self, 1);
187 acpitz_get_status(self);
188
189 rv = AcpiInstallNotifyHandler(sc->sc_devnode->ad_handle,
190 ACPI_SYSTEM_NOTIFY, acpitz_notify_handler, self);
191 if (ACPI_FAILURE(rv)) {
192 aprint_error(": unable to install SYSTEM NOTIFY handler: %s\n",
193 AcpiFormatException(rv));
194 return;
195 }
196
197 callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
198 callout_setfunc(&sc->sc_callout, acpitz_tick, self);
199
200 acpitz_init_envsys(self);
201
202 if (!pmf_device_register(self, NULL, NULL))
203 aprint_error(": couldn't establish power handler\n");
204
205 callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10);
206 }
207
208 static void
209 acpitz_get_zone_quiet(void *opaque)
210 {
211 acpitz_get_zone(opaque, 0);
212 }
213
214 static void
215 acpitz_get_status(void *opaque)
216 {
217 device_t dv = opaque;
218 struct acpitz_softc *sc = device_private(dv);
219 UINT32 tmp, active;
220 int i, flags;
221
222 sc->sc_zone_expire--;
223 if (sc->sc_zone_expire <= 0) {
224 sc->sc_zone_expire = ATZ_ZONE_EXPIRE / sc->sc_zone.tzp;
225 if (sc->sc_flags & ATZ_F_VERBOSE)
226 printf("%s: force refetch zone\n", device_xname(dv));
227 acpitz_get_zone(dv, 0);
228 }
229
230 if (acpitz_get_integer(dv, "_TMP", &tmp)) {
231 aprint_error_dev(dv, "failed to evaluate _TMP\n");
232 return;
233 }
234 sc->sc_zone.tmp = tmp;
235 /* XXX sanity check for tmp here? */
236
237 /*
238 * The temperature unit for envsys(4) is microKelvin, so convert to
239 * that from ACPI's microKelvin. Also, the ACPI specification assumes
240 * that K = C + 273.2 rather than the nominal 273.15 used by envsys(4),
241 * so we correct for that too.
242 */
243 sc->sc_sensor.value_cur = sc->sc_zone.tmp * 100000 - 50000;
244 sc->sc_sensor.state = ENVSYS_SVALID;
245
246 if (sc->sc_flags & ATZ_F_VERBOSE)
247 acpitz_print_status(dv);
248
249 if (sc->sc_flags & ATZ_F_PASSIVEONLY) {
250 /* Passive Cooling: XXX not yet */
251
252 } else {
253 /* Active Cooling */
254
255 /* temperature threshold: _AC0 > ... > _AC9 */
256 active = ATZ_ACTIVE_NONE;
257 for (i = ATZ_NLEVELS - 1; i >= 0; i--) {
258 if (sc->sc_zone.ac[i] == ATZ_TMP_INVALID)
259 continue;
260
261 /* we want to keep highest cooling mode in 'active' */
262 if (sc->sc_zone.ac[i] <= tmp)
263 active = i;
264 }
265
266 flags = sc->sc_flags &
267 ~(ATZ_F_CRITICAL|ATZ_F_HOT|ATZ_F_PASSIVE);
268 if (sc->sc_zone.psv != ATZ_TMP_INVALID &&
269 tmp >= sc->sc_zone.psv)
270 flags |= ATZ_F_PASSIVE;
271 if (sc->sc_zone.hot != ATZ_TMP_INVALID &&
272 tmp >= sc->sc_zone.hot)
273 flags |= ATZ_F_HOT;
274 if (sc->sc_zone.crt != ATZ_TMP_INVALID &&
275 tmp >= sc->sc_zone.crt)
276 flags |= ATZ_F_CRITICAL;
277
278 if (flags != sc->sc_flags) {
279 int changed = (sc->sc_flags ^ flags) & flags;
280 sc->sc_flags = flags;
281 if (changed & ATZ_F_CRITICAL) {
282 sc->sc_sensor.state = ENVSYS_SCRITICAL;
283 aprint_normal_dev(dv,
284 "zone went critical at temp %sC\n",
285 acpitz_celcius_string(tmp));
286 } else if (changed & ATZ_F_HOT) {
287 sc->sc_sensor.state = ENVSYS_SWARNOVER;
288 aprint_normal_dev(dv,
289 "zone went hot at temp %sC\n",
290 acpitz_celcius_string(tmp));
291 }
292 }
293
294 /* power on fans */
295 if (sc->sc_active != active) {
296 if (sc->sc_active != ATZ_ACTIVE_NONE)
297 acpitz_power_zone(sc, sc->sc_active, 0);
298
299 if (active != ATZ_ACTIVE_NONE) {
300 if (sc->sc_flags & ATZ_F_VERBOSE)
301 printf("%s: active cooling level %u\n",
302 device_xname(dv), active);
303 acpitz_power_zone(sc, active, 1);
304 }
305
306 sc->sc_active = active;
307 }
308 }
309
310 return;
311 }
312
313 static char *
314 acpitz_celcius_string(int dk)
315 {
316 static char buf[10];
317
318 snprintf(buf, sizeof(buf), "%d.%d", (dk - ATZ_ZEROC) / 10,
319 (dk - ATZ_ZEROC) % 10);
320
321 return buf;
322 }
323
324 static void
325 acpitz_print_status(device_t dv)
326 {
327 struct acpitz_softc *sc = device_private(dv);
328
329 printf("%s: zone temperature is now %sC\n", device_xname(dv),
330 acpitz_celcius_string(sc->sc_zone.tmp));
331
332 return;
333 }
334
335 static ACPI_STATUS
336 acpitz_switch_cooler(ACPI_OBJECT *obj, void *arg)
337 {
338 ACPI_HANDLE cooler;
339 ACPI_STATUS rv;
340 int pwr_state, flag;
341
342 flag = *(int *)arg;
343
344 if (flag)
345 pwr_state = ACPI_STATE_D0;
346 else
347 pwr_state = ACPI_STATE_D3;
348
349 switch(obj->Type) {
350 case ACPI_TYPE_LOCAL_REFERENCE:
351 case ACPI_TYPE_ANY:
352 cooler = obj->Reference.Handle;
353 break;
354 case ACPI_TYPE_STRING:
355 rv = AcpiGetHandle(NULL, obj->String.Pointer, &cooler);
356 if (ACPI_FAILURE(rv)) {
357 printf("failed to get handler from %s\n",
358 obj->String.Pointer);
359 return rv;
360 }
361 break;
362 default:
363 printf("unknown power type: %d\n", obj->Type);
364 return AE_OK;
365 }
366
367 rv = acpi_pwr_switch_consumer(cooler, pwr_state);
368 if (rv != AE_BAD_PARAMETER && ACPI_FAILURE(rv)) {
369 printf("failed to change state for %s: %s\n",
370 acpi_name(obj->Reference.Handle),
371 AcpiFormatException(rv));
372 }
373
374 return AE_OK;
375 }
376
377 /*
378 * acpitz_power_zone:
379 * power on or off the i:th part of the zone zone
380 */
381 static void
382 acpitz_power_zone(struct acpitz_softc *sc, int i, int on)
383 {
384 KASSERT(i >= 0 && i < ATZ_NLEVELS);
385
386 acpi_foreach_package_object(sc->sc_zone.al[i].Pointer,
387 acpitz_switch_cooler, &on);
388 }
389
390
391 /*
392 * acpitz_power_off:
393 * power off parts of the zone
394 */
395 static void
396 acpitz_power_off(struct acpitz_softc *sc)
397 {
398 int i;
399
400 for (i = 0 ; i < ATZ_NLEVELS; i++) {
401 if (sc->sc_zone.al[i].Pointer == NULL)
402 continue;
403 acpitz_power_zone(sc, i, 0);
404 }
405 sc->sc_active = ATZ_ACTIVE_NONE;
406 sc->sc_flags &= ~(ATZ_F_CRITICAL|ATZ_F_HOT|ATZ_F_PASSIVE);
407 }
408
409 static void
410 acpitz_get_zone(void *opaque, int verbose)
411 {
412 device_t dv = opaque;
413 struct acpitz_softc *sc = device_private(dv);
414 ACPI_STATUS rv;
415 char buf[8];
416 int i, valid_levels;
417
418 if (!sc->sc_first) {
419 acpitz_power_off(sc);
420
421 for (i = 0; i < ATZ_NLEVELS; i++) {
422 if (sc->sc_zone.al[i].Pointer != NULL)
423 AcpiOsFree(sc->sc_zone.al[i].Pointer);
424 sc->sc_zone.al[i].Pointer = NULL;
425 }
426 } else
427 aprint_normal(":");
428
429 valid_levels = 0;
430
431 for (i = 0; i < ATZ_NLEVELS; i++) {
432 ACPI_OBJECT *obj;
433
434 snprintf(buf, sizeof(buf), "_AC%d", i);
435 if (acpitz_get_integer(dv, buf, &sc->sc_zone.ac[i]))
436 continue;
437
438 snprintf(buf, sizeof(buf), "_AL%d", i);
439 rv = acpi_eval_struct(sc->sc_devnode->ad_handle, buf,
440 &sc->sc_zone.al[i]);
441 if (ACPI_FAILURE(rv)) {
442 sc->sc_zone.al[i].Pointer = NULL;
443 continue;
444 }
445
446 obj = sc->sc_zone.al[i].Pointer;
447 if (obj != NULL) {
448 if (obj->Type != ACPI_TYPE_PACKAGE) {
449 aprint_error("%d not package\n", i);
450 AcpiOsFree(obj);
451 sc->sc_zone.al[i].Pointer = NULL;
452 continue;
453 }
454 }
455
456 if (sc->sc_first)
457 aprint_normal(" active cooling level %d: %sC", i,
458 acpitz_celcius_string(sc->sc_zone.ac[i]));
459
460 valid_levels++;
461 }
462
463 acpitz_get_integer(dv, "_TMP", &sc->sc_zone.tmp);
464 acpitz_get_integer(dv, "_CRT", &sc->sc_zone.crt);
465 acpitz_get_integer(dv, "_HOT", &sc->sc_zone.hot);
466 sc->sc_zone.psl.Length = ACPI_ALLOCATE_BUFFER;
467 sc->sc_zone.psl.Pointer = NULL;
468 AcpiEvaluateObject(sc, "_PSL", NULL, &sc->sc_zone.psl);
469 acpitz_get_integer(dv, "_PSV", &sc->sc_zone.psv);
470 acpitz_get_integer(dv, "_TC1", &sc->sc_zone.tc1);
471 acpitz_get_integer(dv, "_TC2", &sc->sc_zone.tc2);
472
473 acpitz_sane_temp(&sc->sc_zone.tmp);
474 acpitz_sane_temp(&sc->sc_zone.crt);
475 acpitz_sane_temp(&sc->sc_zone.hot);
476 acpitz_sane_temp(&sc->sc_zone.psv);
477
478 if (verbose) {
479 if (sc->sc_zone.crt != ATZ_TMP_INVALID)
480 aprint_normal(" critical %sC",
481 acpitz_celcius_string(sc->sc_zone.crt));
482 if (sc->sc_zone.hot != ATZ_TMP_INVALID)
483 aprint_normal(" hot %sC",
484 acpitz_celcius_string(sc->sc_zone.hot));
485 if (sc->sc_zone.psv != ATZ_TMP_INVALID)
486 aprint_normal(" passive %sC",
487 acpitz_celcius_string(sc->sc_zone.tmp));
488 }
489
490 if (valid_levels == 0) {
491 sc->sc_flags |= ATZ_F_PASSIVEONLY;
492 if (sc->sc_first)
493 aprint_normal(", passive cooling");
494 }
495 if (verbose)
496 aprint_normal("\n");
497
498 for (i = 0; i < ATZ_NLEVELS; i++)
499 acpitz_sane_temp(&sc->sc_zone.ac[i]);
500
501 acpitz_power_off(sc);
502 sc->sc_first = 0;
503 }
504
505
506 static void
507 acpitz_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
508 {
509 device_t dv = opaque;
510 ACPI_OSD_EXEC_CALLBACK func = NULL;
511 const char *name;
512 int rv;
513
514 switch (notify) {
515 case ACPI_NOTIFY_ThermalZoneStatusChanged:
516 func = acpitz_get_status;
517 name = "status check";
518 break;
519 case ACPI_NOTIFY_ThermalZoneTripPointsChanged:
520 case ACPI_NOTIFY_DeviceListsChanged:
521 func = acpitz_get_zone_quiet;
522 name = "get zone";
523 break;
524 default:
525 aprint_debug_dev(dv,
526 "received unhandled notify message 0x%x\n", notify);
527 return;
528 }
529
530 KASSERT(func != NULL);
531
532 rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, func, dv);
533 if (rv != AE_OK)
534 aprint_debug_dev(dv, "unable to queue %s\n", name);
535 }
536
537 static void
538 acpitz_sane_temp(UINT32 *tmp)
539 {
540 /* Sane temperatures are beteen 0 and 150 C */
541 if (*tmp < ATZ_ZEROC || *tmp > ATZ_ZEROC + 1500)
542 *tmp = ATZ_TMP_INVALID;
543 }
544
545 static int
546 acpitz_get_integer(device_t dv, const char *cm, UINT32 *val)
547 {
548 struct acpitz_softc *sc = device_private(dv);
549 ACPI_STATUS rv;
550 ACPI_INTEGER tmp;
551
552 rv = acpi_eval_integer(sc->sc_devnode->ad_handle, cm, &tmp);
553 if (ACPI_FAILURE(rv)) {
554 #ifdef ACPI_DEBUG
555 aprint_debug_dev(dv, "failed to evaluate %s: %s\n",
556 cm, AcpiFormatException(rv));
557 #endif
558 *val = ATZ_TMP_INVALID;
559 return 1;
560 }
561
562 *val = tmp;
563
564 return 0;
565 }
566
567 static void
568 acpitz_tick(void *opaque)
569 {
570 device_t dv = opaque;
571 struct acpitz_softc *sc = device_private(dv);
572
573 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpitz_get_status, dv);
574
575 callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10);
576 }
577
578 static void
579 acpitz_init_envsys(device_t dv)
580 {
581 struct acpitz_softc *sc = device_private(dv);
582
583 sc->sc_sme = sysmon_envsys_create();
584 sc->sc_sensor.monitor = true;
585 sc->sc_sensor.flags = (ENVSYS_FMONCRITICAL|ENVSYS_FMONWARNOVER);
586 strlcpy(sc->sc_sensor.desc, "temperature", sizeof(sc->sc_sensor.desc));
587 if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
588 sysmon_envsys_destroy(sc->sc_sme);
589 return;
590 }
591
592 /* hook into sysmon */
593 sc->sc_sme->sme_name = device_xname(dv);
594 sc->sc_sme->sme_flags = SME_DISABLE_REFRESH;
595
596 if (sysmon_envsys_register(sc->sc_sme)) {
597 aprint_error_dev(dv, "unable to register with sysmon\n");
598 sysmon_envsys_destroy(sc->sc_sme);
599 }
600 }
601