acpi_tz.c revision 1.35 1 /* $NetBSD: acpi_tz.c,v 1.35 2008/03/29 13:33:12 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.35 2008/03/29 13:33:12 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_ANY:
351 cooler = obj->Reference.Handle;
352 break;
353 case ACPI_TYPE_STRING:
354 rv = AcpiGetHandle(NULL, obj->String.Pointer, &cooler);
355 if (ACPI_FAILURE(rv)) {
356 printf("failed to get handler from %s\n",
357 obj->String.Pointer);
358 return rv;
359 }
360 break;
361 default:
362 printf("unknown power type: %d\n", obj->Type);
363 return AE_OK;
364 }
365
366 rv = acpi_pwr_switch_consumer(cooler, pwr_state);
367 if (rv != AE_BAD_PARAMETER && ACPI_FAILURE(rv)) {
368 printf("failed to change state for %s: %s\n",
369 acpi_name(obj->Reference.Handle),
370 AcpiFormatException(rv));
371 }
372
373 return AE_OK;
374 }
375
376 /*
377 * acpitz_power_zone:
378 * power on or off the i:th part of the zone zone
379 */
380 static void
381 acpitz_power_zone(struct acpitz_softc *sc, int i, int on)
382 {
383 KASSERT(i >= 0 && i < ATZ_NLEVELS);
384
385 acpi_foreach_package_object(sc->sc_zone.al[i].Pointer,
386 acpitz_switch_cooler, &on);
387 }
388
389
390 /*
391 * acpitz_power_off:
392 * power off parts of the zone
393 */
394 static void
395 acpitz_power_off(struct acpitz_softc *sc)
396 {
397 int i;
398
399 for (i = 0 ; i < ATZ_NLEVELS; i++) {
400 if (sc->sc_zone.al[i].Pointer == NULL)
401 continue;
402 acpitz_power_zone(sc, i, 0);
403 }
404 sc->sc_active = ATZ_ACTIVE_NONE;
405 sc->sc_flags &= ~(ATZ_F_CRITICAL|ATZ_F_HOT|ATZ_F_PASSIVE);
406 }
407
408 static void
409 acpitz_get_zone(void *opaque, int verbose)
410 {
411 device_t dv = opaque;
412 struct acpitz_softc *sc = device_private(dv);
413 ACPI_STATUS rv;
414 char buf[8];
415 int i, valid_levels;
416
417 if (!sc->sc_first) {
418 acpitz_power_off(sc);
419
420 for (i = 0; i < ATZ_NLEVELS; i++) {
421 if (sc->sc_zone.al[i].Pointer != NULL)
422 AcpiOsFree(sc->sc_zone.al[i].Pointer);
423 sc->sc_zone.al[i].Pointer = NULL;
424 }
425 } else
426 aprint_normal(":");
427
428 valid_levels = 0;
429
430 for (i = 0; i < ATZ_NLEVELS; i++) {
431 ACPI_OBJECT *obj;
432
433 snprintf(buf, sizeof(buf), "_AC%d", i);
434 if (acpitz_get_integer(dv, buf, &sc->sc_zone.ac[i]))
435 continue;
436
437 snprintf(buf, sizeof(buf), "_AL%d", i);
438 rv = acpi_eval_struct(sc->sc_devnode->ad_handle, buf,
439 &sc->sc_zone.al[i]);
440 if (ACPI_FAILURE(rv)) {
441 sc->sc_zone.al[i].Pointer = NULL;
442 continue;
443 }
444
445 obj = sc->sc_zone.al[i].Pointer;
446 if (obj != NULL) {
447 if (obj->Type != ACPI_TYPE_PACKAGE) {
448 aprint_error("%d not package\n", i);
449 AcpiOsFree(obj);
450 sc->sc_zone.al[i].Pointer = NULL;
451 continue;
452 }
453 }
454
455 if (sc->sc_first)
456 aprint_normal(" active cooling level %d: %sC", i,
457 acpitz_celcius_string(sc->sc_zone.ac[i]));
458
459 valid_levels++;
460 }
461
462 acpitz_get_integer(dv, "_TMP", &sc->sc_zone.tmp);
463 acpitz_get_integer(dv, "_CRT", &sc->sc_zone.crt);
464 acpitz_get_integer(dv, "_HOT", &sc->sc_zone.hot);
465 sc->sc_zone.psl.Length = ACPI_ALLOCATE_BUFFER;
466 sc->sc_zone.psl.Pointer = NULL;
467 AcpiEvaluateObject(sc, "_PSL", NULL, &sc->sc_zone.psl);
468 acpitz_get_integer(dv, "_PSV", &sc->sc_zone.psv);
469 acpitz_get_integer(dv, "_TC1", &sc->sc_zone.tc1);
470 acpitz_get_integer(dv, "_TC2", &sc->sc_zone.tc2);
471
472 acpitz_sane_temp(&sc->sc_zone.tmp);
473 acpitz_sane_temp(&sc->sc_zone.crt);
474 acpitz_sane_temp(&sc->sc_zone.hot);
475 acpitz_sane_temp(&sc->sc_zone.psv);
476
477 if (verbose) {
478 if (sc->sc_zone.crt != ATZ_TMP_INVALID)
479 aprint_normal(" critical %sC",
480 acpitz_celcius_string(sc->sc_zone.crt));
481 if (sc->sc_zone.hot != ATZ_TMP_INVALID)
482 aprint_normal(" hot %sC",
483 acpitz_celcius_string(sc->sc_zone.hot));
484 if (sc->sc_zone.psv != ATZ_TMP_INVALID)
485 aprint_normal(" passive %sC",
486 acpitz_celcius_string(sc->sc_zone.tmp));
487 }
488
489 if (valid_levels == 0) {
490 sc->sc_flags |= ATZ_F_PASSIVEONLY;
491 if (sc->sc_first)
492 aprint_normal(", passive cooling");
493 }
494 if (verbose)
495 aprint_normal("\n");
496
497 for (i = 0; i < ATZ_NLEVELS; i++)
498 acpitz_sane_temp(&sc->sc_zone.ac[i]);
499
500 acpitz_power_off(sc);
501 sc->sc_first = 0;
502 }
503
504
505 static void
506 acpitz_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
507 {
508 device_t dv = opaque;
509 ACPI_OSD_EXEC_CALLBACK func = NULL;
510 const char *name;
511 int rv;
512
513 switch (notify) {
514 case ACPI_NOTIFY_ThermalZoneStatusChanged:
515 func = acpitz_get_status;
516 name = "status check";
517 break;
518 case ACPI_NOTIFY_ThermalZoneTripPointsChanged:
519 case ACPI_NOTIFY_DeviceListsChanged:
520 func = acpitz_get_zone_quiet;
521 name = "get zone";
522 break;
523 default:
524 aprint_debug_dev(dv,
525 "received unhandled notify message 0x%x\n", notify);
526 return;
527 }
528
529 KASSERT(func != NULL);
530
531 rv = AcpiOsExecute(OSL_NOTIFY_HANDLER, func, dv);
532 if (rv != AE_OK)
533 aprint_debug_dev(dv, "unable to queue %s\n", name);
534 }
535
536 static void
537 acpitz_sane_temp(UINT32 *tmp)
538 {
539 /* Sane temperatures are beteen 0 and 150 C */
540 if (*tmp < ATZ_ZEROC || *tmp > ATZ_ZEROC + 1500)
541 *tmp = ATZ_TMP_INVALID;
542 }
543
544 static int
545 acpitz_get_integer(device_t dv, const char *cm, UINT32 *val)
546 {
547 struct acpitz_softc *sc = device_private(dv);
548 ACPI_STATUS rv;
549 ACPI_INTEGER tmp;
550
551 rv = acpi_eval_integer(sc->sc_devnode->ad_handle, cm, &tmp);
552 if (ACPI_FAILURE(rv)) {
553 #ifdef ACPI_DEBUG
554 aprint_debug_dev(dv, "failed to evaluate %s: %s\n",
555 cm, AcpiFormatException(rv));
556 #endif
557 *val = ATZ_TMP_INVALID;
558 return 1;
559 }
560
561 *val = tmp;
562
563 return 0;
564 }
565
566 static void
567 acpitz_tick(void *opaque)
568 {
569 device_t dv = opaque;
570 struct acpitz_softc *sc = device_private(dv);
571
572 AcpiOsExecute(OSL_NOTIFY_HANDLER, acpitz_get_status, dv);
573
574 callout_schedule(&sc->sc_callout, sc->sc_zone.tzp * hz / 10);
575 }
576
577 static void
578 acpitz_init_envsys(device_t dv)
579 {
580 struct acpitz_softc *sc = device_private(dv);
581
582 sc->sc_sme = sysmon_envsys_create();
583 sc->sc_sensor.monitor = true;
584 sc->sc_sensor.flags = (ENVSYS_FMONCRITICAL|ENVSYS_FMONWARNOVER);
585 strlcpy(sc->sc_sensor.desc, "temperature", sizeof(sc->sc_sensor.desc));
586 if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
587 sysmon_envsys_destroy(sc->sc_sme);
588 return;
589 }
590
591 /* hook into sysmon */
592 sc->sc_sme->sme_name = device_xname(dv);
593 sc->sc_sme->sme_flags = SME_DISABLE_REFRESH;
594
595 if (sysmon_envsys_register(sc->sc_sme)) {
596 aprint_error_dev(dv, "unable to register with sysmon\n");
597 sysmon_envsys_destroy(sc->sc_sme);
598 }
599 }
600