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