xref: /freebsd/sys/dev/acpica/acpi_thermal.c (revision b52b9d56d4e96089873a75f9e29062eec19fabba)
1 /*-
2  * Copyright (c) 2000, 2001 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, 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  *	$FreeBSD$
28  */
29 
30 #include "opt_acpi.h"
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/kthread.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/bus.h>
37 #include <sys/proc.h>
38 #include <sys/reboot.h>
39 #include <sys/sysctl.h>
40 #include <sys/unistd.h>
41 #include <sys/power.h>
42 
43 #include "acpi.h"
44 
45 #include <dev/acpica/acpivar.h>
46 
47 /*
48  * Hooks for the ACPI CA debugging infrastructure
49  */
50 #define _COMPONENT	ACPI_THERMAL
51 ACPI_MODULE_NAME("THERMAL")
52 
53 #define TZ_ZEROC	2732
54 #define TZ_KELVTOC(x)	(((x) - TZ_ZEROC) / 10), (((x) - TZ_ZEROC) % 10)
55 
56 #define TZ_NOTIFY_TEMPERATURE	0x80
57 #define TZ_NOTIFY_DEVICES	0x81
58 #define TZ_NOTIFY_LEVELS	0x82
59 
60 #define TZ_POLLRATE	30		/* every 30 seconds by default */
61 
62 #define TZ_NUMLEVELS	10		/* defined by ACPI spec */
63 struct acpi_tz_zone {
64     int		ac[TZ_NUMLEVELS];
65     ACPI_BUFFER	al[TZ_NUMLEVELS];
66     int		crt;
67     int		hot;
68     ACPI_BUFFER	psl;
69     int		psv;
70     int		tc1;
71     int		tc2;
72     int		tsp;
73     int		tzp;
74 };
75 
76 
77 struct acpi_tz_softc {
78     device_t			tz_dev;			/* device handle */
79     ACPI_HANDLE			tz_handle;		/* thermal zone handle */
80     int				tz_temperature;		/* current temperature */
81     int				tz_active;		/* current active cooling */
82 #define TZ_ACTIVE_NONE		-1
83     int				tz_requested;		/* user-requested minimum active cooling */
84     int				tz_thflags;		/* current temperature-related flags */
85 #define TZ_THFLAG_NONE		0
86 #define TZ_THFLAG_PSV		(1<<0)
87 #define TZ_THFLAG_HOT		(1<<2)
88 #define TZ_THFLAG_CRT		(1<<3)
89     int				tz_flags;
90 #define TZ_FLAG_NO_SCP		(1<<0)			/* no _SCP method */
91 #define TZ_FLAG_GETPROFILE	(1<<1)			/* fetch power_profile in timeout */
92     struct timespec		tz_cooling_started;	/* current cooling starting time */
93 
94     struct sysctl_ctx_list	tz_sysctl_ctx;		/* sysctl tree */
95     struct sysctl_oid		*tz_sysctl_tree;
96 
97     struct acpi_tz_zone 	tz_zone;		/* thermal zone parameters */
98     int				tz_tmp_updating;
99 };
100 
101 static int	acpi_tz_probe(device_t dev);
102 static int	acpi_tz_attach(device_t dev);
103 static int	acpi_tz_establish(struct acpi_tz_softc *sc);
104 static void	acpi_tz_monitor(struct acpi_tz_softc *sc);
105 static void	acpi_tz_all_off(struct acpi_tz_softc *sc);
106 static void	acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg);
107 static void	acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg);
108 static void	acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, int *data);
109 static void	acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what);
110 static int	acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS);
111 static void	acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context);
112 static void	acpi_tz_timeout(struct acpi_tz_softc *sc);
113 static void	acpi_tz_power_profile(void *arg);
114 
115 static void	acpi_tz_thread(void *arg);
116 static struct proc *acpi_tz_proc;
117 
118 static device_method_t acpi_tz_methods[] = {
119     /* Device interface */
120     DEVMETHOD(device_probe,	acpi_tz_probe),
121     DEVMETHOD(device_attach,	acpi_tz_attach),
122 
123     {0, 0}
124 };
125 
126 static driver_t acpi_tz_driver = {
127     "acpi_tz",
128     acpi_tz_methods,
129     sizeof(struct acpi_tz_softc),
130 };
131 
132 static devclass_t acpi_tz_devclass;
133 DRIVER_MODULE(acpi_tz, acpi, acpi_tz_driver, acpi_tz_devclass, 0, 0);
134 
135 static struct sysctl_ctx_list	acpi_tz_sysctl_ctx;
136 static struct sysctl_oid	*acpi_tz_sysctl_tree;
137 
138 static int			acpi_tz_min_runtime = 0;/* minimum cooling run time */
139 static int			acpi_tz_polling_rate = TZ_POLLRATE;
140 
141 /*
142  * Match an ACPI thermal zone.
143  */
144 static int
145 acpi_tz_probe(device_t dev)
146 {
147     int		result;
148 
149     ACPI_LOCK;
150 
151     /* no FUNCTION_TRACE - too noisy */
152 
153     if ((acpi_get_type(dev) == ACPI_TYPE_THERMAL) &&
154 	!acpi_disabled("thermal")) {
155 	device_set_desc(dev, "thermal zone");
156 	result = -10;
157     } else {
158 	result = ENXIO;
159     }
160     ACPI_UNLOCK;
161     return(result);
162 }
163 
164 /*
165  * Attach to an ACPI thermal zone.
166  */
167 static int
168 acpi_tz_attach(device_t dev)
169 {
170     struct acpi_tz_softc	*sc;
171     struct acpi_softc		*acpi_sc;
172     int				error;
173     char			oidname[8];
174 
175     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
176 
177     ACPI_LOCK;
178 
179     sc = device_get_softc(dev);
180     sc->tz_dev = dev;
181     sc->tz_handle = acpi_get_handle(dev);
182     sc->tz_requested = TZ_ACTIVE_NONE;
183     sc->tz_tmp_updating = 0;
184 
185     /*
186      * Parse the current state of the thermal zone and build control
187      * structures.
188      */
189     if ((error = acpi_tz_establish(sc)) != 0)
190 	goto out;
191 
192     /*
193      * Register for any Notify events sent to this zone.
194      */
195     AcpiInstallNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
196 			     acpi_tz_notify_handler, sc);
197 
198     /*
199      * Create our sysctl nodes.
200      *
201      * XXX we need a mechanism for adding nodes under ACPI.
202      */
203     if (device_get_unit(dev) == 0) {
204 	acpi_sc = acpi_device_get_parent_softc(dev);
205 	sysctl_ctx_init(&acpi_tz_sysctl_ctx);
206 	acpi_tz_sysctl_tree = SYSCTL_ADD_NODE(&acpi_tz_sysctl_ctx,
207 					      SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
208 					      OID_AUTO, "thermal", CTLFLAG_RD, 0, "");
209 	SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
210 		       SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
211 		       OID_AUTO, "min_runtime", CTLFLAG_RD | CTLFLAG_RW,
212 		       &acpi_tz_min_runtime, 0, "minimum cooling run time in sec");
213 	SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
214 		       SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
215 		       OID_AUTO, "polling_rate", CTLFLAG_RD | CTLFLAG_RW,
216 		       &acpi_tz_polling_rate, 0, "monitor polling rate");
217     }
218     sysctl_ctx_init(&sc->tz_sysctl_ctx);
219     sprintf(oidname, "tz%d", device_get_unit(dev));
220     sc->tz_sysctl_tree = SYSCTL_ADD_NODE(&sc->tz_sysctl_ctx,
221 					 SYSCTL_CHILDREN(acpi_tz_sysctl_tree), OID_AUTO,
222 					 oidname, CTLFLAG_RD, 0, "");
223     SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
224 		   OID_AUTO, "temperature", CTLFLAG_RD,
225 		   &sc->tz_temperature, 0, "current thermal zone temperature");
226     SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
227 		    OID_AUTO, "active", CTLTYPE_INT | CTLFLAG_RW,
228 		    sc, 0, acpi_tz_active_sysctl, "I", "");
229 
230     SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
231 		   OID_AUTO, "thermal_flags", CTLFLAG_RD,
232 		   &sc->tz_thflags, 0, "thermal zone flags");
233     SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
234 		   OID_AUTO, "_PSV", CTLFLAG_RD,
235 		   &sc->tz_zone.psv, 0, "");
236     SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
237 		   OID_AUTO, "_HOT", CTLFLAG_RD,
238 		   &sc->tz_zone.hot, 0, "");
239     SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
240 		   OID_AUTO, "_CRT", CTLFLAG_RD,
241 		   &sc->tz_zone.crt, 0, "");
242     SYSCTL_ADD_OPAQUE(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
243 		      OID_AUTO, "_ACx", CTLFLAG_RD, &sc->tz_zone.ac,
244 		      sizeof(sc->tz_zone.ac), "I", "");
245 
246 
247     /*
248      * Register our power profile event handler, and flag it for a manual
249      * invocation by our timeout.  We defer it like this so that the rest
250      * of the subsystem has time to come up.
251      */
252     EVENTHANDLER_REGISTER(power_profile_change, acpi_tz_power_profile, sc, 0);
253     sc->tz_flags |= TZ_FLAG_GETPROFILE;
254 
255     /*
256      * Don't bother evaluating/printing the temperature at this point;
257      * on many systems it'll be bogus until the EC is running.
258      */
259 
260     /*
261      * Create our thread; we only need one, it will service all of the
262      * thermal zones.
263      */
264     if (acpi_tz_proc == NULL) {
265 	    error = kthread_create(acpi_tz_thread, NULL, &acpi_tz_proc,
266 				   RFHIGHPID, "acpi_thermal");
267 	    if (error != 0) {
268 		    device_printf(sc->tz_dev, "could not create thread - %d", error);
269 		    goto out;
270 	    }
271     }
272 
273  out:
274     ACPI_UNLOCK;
275 
276     return_VALUE(error);
277 }
278 
279 /*
280  * Parse the current state of this thermal zone and set up to use it.
281  *
282  * Note that we may have previous state, which will have to be discarded.
283  */
284 static int
285 acpi_tz_establish(struct acpi_tz_softc *sc)
286 {
287     ACPI_OBJECT	*obj;
288     int		i;
289     char	nbuf[8];
290 
291     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
292 
293     ACPI_ASSERTLOCK;
294 
295     /*
296      * Power everything off and erase any existing state.
297      */
298     acpi_tz_all_off(sc);
299     for (i = 0; i < TZ_NUMLEVELS; i++)
300 	if (sc->tz_zone.al[i].Pointer != NULL)
301 	    AcpiOsFree(sc->tz_zone.al[i].Pointer);
302     if (sc->tz_zone.psl.Pointer != NULL)
303 	AcpiOsFree(sc->tz_zone.psl.Pointer);
304     bzero(&sc->tz_zone, sizeof(sc->tz_zone));
305 
306     /*
307      * Evaluate thermal zone parameters.
308      */
309     for (i = 0; i < TZ_NUMLEVELS; i++) {
310 	sprintf(nbuf, "_AC%d", i);
311 	acpi_tz_getparam(sc, nbuf, &sc->tz_zone.ac[i]);
312 	sprintf(nbuf, "_AL%d", i);
313 	sc->tz_zone.al[i].Length = ACPI_ALLOCATE_BUFFER;
314 	sc->tz_zone.al[i].Pointer = NULL;
315 	AcpiEvaluateObject(sc->tz_handle, nbuf, NULL, &sc->tz_zone.al[i]);
316 	obj = (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer;
317 	if (obj != NULL) {
318 	    /* should be a package containing a list of power objects */
319 	    if (obj->Type != ACPI_TYPE_PACKAGE) {
320 		device_printf(sc->tz_dev, "%s has unknown object type %d, rejecting\n",
321 			      nbuf, obj->Type);
322 		return_VALUE(ENXIO);
323 	    }
324 	}
325     }
326     acpi_tz_getparam(sc, "_CRT", &sc->tz_zone.crt);
327     acpi_tz_getparam(sc, "_HOT", &sc->tz_zone.hot);
328     sc->tz_zone.psl.Length = ACPI_ALLOCATE_BUFFER;
329     sc->tz_zone.psl.Pointer = NULL;
330     AcpiEvaluateObject(sc->tz_handle, "_PSL", NULL, &sc->tz_zone.psl);
331     acpi_tz_getparam(sc, "_PSV", &sc->tz_zone.psv);
332     acpi_tz_getparam(sc, "_TC1", &sc->tz_zone.tc1);
333     acpi_tz_getparam(sc, "_TC2", &sc->tz_zone.tc2);
334     acpi_tz_getparam(sc, "_TSP", &sc->tz_zone.tsp);
335     acpi_tz_getparam(sc, "_TZP", &sc->tz_zone.tzp);
336 
337     /*
338      * Sanity-check the values we've been given.
339      *
340      * XXX what do we do about systems that give us the same value for
341      *     more than one of these setpoints?
342      */
343     acpi_tz_sanity(sc, &sc->tz_zone.crt, "_CRT");
344     acpi_tz_sanity(sc, &sc->tz_zone.hot, "_HOT");
345     acpi_tz_sanity(sc, &sc->tz_zone.psv, "_PSV");
346     for (i = 0; i < TZ_NUMLEVELS; i++)
347 	acpi_tz_sanity(sc, &sc->tz_zone.ac[i], "_ACx");
348 
349     /*
350      * Power off everything that we've just been given.
351      */
352     acpi_tz_all_off(sc);
353 
354     return_VALUE(0);
355 }
356 
357 static char	*aclevel_string[] =	{
358 	"NONE", "_AC0", "_AC1", "_AC2", "_AC3", "_AC4",
359 	"_AC5", "_AC6", "_AC7", "_AC8", "_AC9" };
360 
361 static __inline const char *
362 acpi_tz_aclevel_string(int active)
363 {
364 	if (active < -1 || active >= TZ_NUMLEVELS) {
365 		return (aclevel_string[0]);
366 	}
367 
368 	return (aclevel_string[active+1]);
369 }
370 
371 /*
372  * Evaluate the condition of a thermal zone, take appropriate actions.
373  */
374 static void
375 acpi_tz_monitor(struct acpi_tz_softc *sc)
376 {
377     int		temp;
378     int		i;
379     int		newactive, newflags;
380     struct	timespec curtime;
381     ACPI_STATUS	status;
382 
383     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
384 
385     ACPI_ASSERTLOCK;
386 
387     if (sc->tz_tmp_updating) {
388 	goto out;
389     }
390     sc->tz_tmp_updating = 1;
391 
392     /*
393      * Get the current temperature.
394      */
395     if (ACPI_FAILURE(status = acpi_EvaluateInteger(sc->tz_handle, "_TMP", &temp))) {
396 	ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
397 	    "error fetching current temperature -- %s\n",
398 	     AcpiFormatException(status));
399 	/* XXX disable zone? go to max cooling? */
400 	goto out;
401     }
402 
403     ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "got %d.%dC\n", TZ_KELVTOC(temp)));
404     sc->tz_temperature = temp;
405 
406     /*
407      * Work out what we ought to be doing right now.
408      *
409      * Note that the _ACx levels sort from hot to cold.
410      */
411     newactive = TZ_ACTIVE_NONE;
412     for (i = TZ_NUMLEVELS - 1; i >= 0; i--) {
413 	if ((sc->tz_zone.ac[i] != -1) && (temp >= sc->tz_zone.ac[i])) {
414 	    newactive = i;
415 	    if (sc->tz_active != newactive) {
416 		ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
417 		    "_AC%d: temperature %d.%d >= setpoint %d.%d\n", i,
418 		    TZ_KELVTOC(temp), TZ_KELVTOC(sc->tz_zone.ac[i]));
419 		getnanotime(&sc->tz_cooling_started);
420 	    }
421 	}
422     }
423 
424     /*
425      * We are going to get _ACx level down (colder side), but give a guaranteed
426      * minimum cooling run time if requested.
427      */
428     if (acpi_tz_min_runtime > 0 && sc->tz_active != TZ_ACTIVE_NONE &&
429 	(newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) {
430 	getnanotime(&curtime);
431 	timespecsub(&curtime, &sc->tz_cooling_started);
432 	if (curtime.tv_sec < acpi_tz_min_runtime) {
433 	    newactive = sc->tz_active;
434 	}
435     }
436 
437     /* handle user override of active mode */
438     if (sc->tz_requested > newactive)
439 	newactive = sc->tz_requested;
440 
441     /* update temperature-related flags */
442     newflags = TZ_THFLAG_NONE;
443     if ((sc->tz_zone.psv != -1) && (temp >= sc->tz_zone.psv))
444 	newflags |= TZ_THFLAG_PSV;
445     if ((sc->tz_zone.hot != -1) && (temp >= sc->tz_zone.hot))
446 	newflags |= TZ_THFLAG_HOT;
447     if ((sc->tz_zone.crt != -1) && (temp >= sc->tz_zone.crt))
448 	newflags |= TZ_THFLAG_CRT;
449 
450     /*
451      * If the active cooling state has changed, we have to switch things.
452      */
453     if (newactive != sc->tz_active) {
454 
455 	/* turn off the cooling devices that are on, if any are */
456 	if (sc->tz_active != TZ_ACTIVE_NONE)
457 	    acpi_ForeachPackageObject((ACPI_OBJECT *)sc->tz_zone.al[sc->tz_active].Pointer,
458 				      acpi_tz_switch_cooler_off, sc);
459 
460 	/* turn on cooling devices that are required, if any are */
461 	if (newactive != TZ_ACTIVE_NONE)
462 	    acpi_ForeachPackageObject((ACPI_OBJECT *)sc->tz_zone.al[newactive].Pointer,
463 				      acpi_tz_switch_cooler_on, sc);
464 	ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
465 	    "switched from %s to %s: %d.%dC\n",
466 	    acpi_tz_aclevel_string(sc->tz_active),
467 	    acpi_tz_aclevel_string(newactive), TZ_KELVTOC(temp));
468 	sc->tz_active = newactive;
469     }
470 
471     /*
472      * XXX (de)activate any passive cooling that may be required.
473      */
474 
475     /*
476      * If we have just become _HOT or _CRT, warn the user.
477      *
478      * We should actually shut down at this point, but it's not clear
479      * that some systems don't actually map _CRT to the same value as _AC0.
480      */
481     if ((newflags & (TZ_THFLAG_HOT | TZ_THFLAG_CRT)) &&
482 	!(sc->tz_thflags & (TZ_THFLAG_HOT | TZ_THFLAG_CRT))) {
483 	device_printf(sc->tz_dev, "WARNING - current temperature (%d.%dC) exceeds system limits\n",
484 		      TZ_KELVTOC(sc->tz_temperature));
485 	/* shutdown_nice(RB_POWEROFF);*/
486     }
487     sc->tz_thflags = newflags;
488 
489 out:
490     sc->tz_tmp_updating = 0;
491     return_VOID;
492 }
493 
494 /*
495  * Turn off all the cooling devices.
496  */
497 static void
498 acpi_tz_all_off(struct acpi_tz_softc *sc)
499 {
500     int		i;
501 
502     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
503 
504     ACPI_ASSERTLOCK;
505 
506     /*
507      * Scan all the _ALx objects, and turn them all off.
508      */
509     for (i = 0; i < TZ_NUMLEVELS; i++) {
510 	if (sc->tz_zone.al[i].Pointer == NULL)
511 	    continue;
512 	acpi_ForeachPackageObject((ACPI_OBJECT *)sc->tz_zone.al[i].Pointer,
513 				  acpi_tz_switch_cooler_off, sc);
514     }
515 
516     /*
517      * XXX revert any passive-cooling options.
518      */
519 
520     sc->tz_active = TZ_ACTIVE_NONE;
521     sc->tz_thflags = TZ_THFLAG_NONE;
522     return_VOID;
523 }
524 
525 /*
526  * Given an object, verify that it's a reference to a device of some sort,
527  * and try to switch it off.
528  */
529 static void
530 acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg)
531 {
532     ACPI_HANDLE		cooler;
533 
534     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
535 
536     ACPI_ASSERTLOCK;
537 
538     switch(obj->Type) {
539     case ACPI_TYPE_STRING:
540 	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s off\n", obj->String.Pointer));
541 
542 	/*
543 	 * Find the handle for the device and turn it off.
544 	 * The String object here seems to contain a fully-qualified path, so we
545 	 * don't have to search for it in our parents.
546 	 *
547 	 * XXX This may not always be the case.
548 	 */
549 	if (ACPI_SUCCESS(AcpiGetHandle(NULL, obj->String.Pointer, &cooler)))
550 	    acpi_pwr_switch_consumer(cooler, ACPI_STATE_D3);
551 	break;
552 
553     default:
554 	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to handle unsupported object type %d\n",
555 			  obj->Type));
556 	break;
557     }
558     return_VOID;
559 }
560 
561 /*
562  * Given an object, verify that it's a reference to a device of some sort,
563  * and try to switch it on.
564  *
565  * XXX replication of off/on function code is bad, mmmkay?
566  */
567 static void
568 acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg)
569 {
570     struct acpi_tz_softc	*sc = (struct acpi_tz_softc *)arg;
571     ACPI_HANDLE			cooler;
572     ACPI_STATUS			status;
573 
574     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
575 
576     ACPI_ASSERTLOCK;
577 
578     switch(obj->Type) {
579     case ACPI_TYPE_STRING:
580 	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s on\n", obj->String.Pointer));
581 
582 	/*
583 	 * Find the handle for the device and turn it off.
584 	 * The String object here seems to contain a fully-qualified path, so we
585 	 * don't have to search for it in our parents.
586 	 *
587 	 * XXX This may not always be the case.
588 	 */
589 	if (ACPI_SUCCESS(AcpiGetHandle(NULL, obj->String.Pointer, &cooler))) {
590 	    if (ACPI_FAILURE(status = acpi_pwr_switch_consumer(cooler, ACPI_STATE_D0))) {
591 		ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
592 		    "failed to activate %s - %s\n",
593 		    obj->String.Pointer, AcpiFormatException(status));
594 	    }
595 	} else {
596 	    ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
597 		"couldn't find %s\n", obj->String.Pointer);
598 	}
599 	break;
600 
601     default:
602 	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to handle unsupported object type %d\n",
603 			  obj->Type));
604 	break;
605     }
606 	return_VOID;
607 }
608 
609 /*
610  * Read/debug-print a parameter, default it to -1.
611  */
612 static void
613 acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, int *data)
614 {
615 
616     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
617 
618     ACPI_ASSERTLOCK;
619 
620     if (ACPI_FAILURE(acpi_EvaluateInteger(sc->tz_handle, node, data))) {
621 	*data = -1;
622     } else {
623 	ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "%s.%s = %d\n", acpi_name(sc->tz_handle),
624 			  node, *data));
625     }
626     return_VOID;
627 }
628 
629 /*
630  * Sanity-check a temperature value.  Assume that setpoints
631  * should be between 0C and 150C.
632  */
633 static void
634 acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what)
635 {
636     if ((*val != -1) && ((*val < TZ_ZEROC) || (*val > (TZ_ZEROC + 1500)))) {
637 	device_printf(sc->tz_dev, "%s value is absurd, ignored (%d.%dC)\n",
638 		      what, TZ_KELVTOC(*val));
639 	*val = -1;
640     }
641 }
642 
643 /*
644  * Respond to a sysctl on the active state node.
645  */
646 static int
647 acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS)
648 {
649     struct acpi_tz_softc	*sc;
650     int				active;
651     int		 		error;
652 
653     ACPI_LOCK;
654 
655     sc = (struct acpi_tz_softc *)oidp->oid_arg1;
656     active = sc->tz_active;
657     error = sysctl_handle_int(oidp, &active, 0, req);
658 
659     /* error or no new value */
660     if ((error != 0) || (req->newptr == NULL))
661 	goto out;
662 
663     /* range check */
664     if ((active < -1) || (active >= TZ_NUMLEVELS)) {
665 	error = EINVAL;
666 	goto out;
667     }
668 
669     /* set new preferred level and re-switch */
670     sc->tz_requested = active;
671     acpi_tz_monitor(sc);
672 
673  out:
674     ACPI_UNLOCK;
675     return(error);
676 }
677 
678 /*
679  * Respond to a Notify event sent to the zone.
680  */
681 static void
682 acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
683 {
684     struct acpi_tz_softc	*sc = (struct acpi_tz_softc *)context;
685 
686     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
687 
688     ACPI_ASSERTLOCK;
689 
690     switch(notify) {
691     case TZ_NOTIFY_TEMPERATURE:
692 	/* temperature change occurred */
693 	AcpiOsQueueForExecution(OSD_PRIORITY_HIGH, (OSD_EXECUTION_CALLBACK)acpi_tz_monitor, sc);
694 	break;
695     case TZ_NOTIFY_DEVICES:
696     case TZ_NOTIFY_LEVELS:
697 	/* zone devices/setpoints changed */
698 	AcpiOsQueueForExecution(OSD_PRIORITY_HIGH, (OSD_EXECUTION_CALLBACK)acpi_tz_establish, sc);
699 	break;
700     default:
701 	ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
702 	    "unknown Notify event 0x%x\n", notify);
703 	break;
704     }
705     return_VOID;
706 }
707 
708 /*
709  * Poll the thermal zone.
710  */
711 static void
712 acpi_tz_timeout(struct acpi_tz_softc *sc)
713 {
714 
715     /* do we need to get the power profile settings? */
716     if (sc->tz_flags & TZ_FLAG_GETPROFILE) {
717 	acpi_tz_power_profile((void *)sc);
718 	sc->tz_flags &= ~TZ_FLAG_GETPROFILE;
719     }
720 
721     ACPI_ASSERTLOCK;
722 
723     /* check the current temperature and take action based on it */
724     acpi_tz_monitor(sc);
725 
726     /* XXX passive cooling actions? */
727 }
728 
729 /*
730  * System power profile may have changed; fetch and notify the
731  * thermal zone accordingly.
732  *
733  * Since this can be called from an arbitrary eventhandler, it needs
734  * to get the ACPI lock itself.
735  */
736 static void
737 acpi_tz_power_profile(void *arg)
738 {
739     ACPI_OBJECT_LIST		args;
740     ACPI_OBJECT			obj;
741     ACPI_STATUS			status;
742     struct acpi_tz_softc	*sc = (struct acpi_tz_softc *)arg;
743     int				state;
744 
745     state = power_profile_get_state();
746     if (state != POWER_PROFILE_PERFORMANCE &&
747         state != POWER_PROFILE_ECONOMY) {
748         return;
749     }
750 
751     ACPI_LOCK;
752 
753     /* check that we haven't decided there's no _SCP method */
754     if (!(sc->tz_flags & TZ_FLAG_NO_SCP)) {
755 
756 	/* call _SCP to set the new profile */
757 	obj.Type = ACPI_TYPE_INTEGER;
758 	obj.Integer.Value = (state == POWER_PROFILE_PERFORMANCE) ? 0 : 1;
759 	args.Count = 1;
760 	args.Pointer = &obj;
761 	if (ACPI_FAILURE(status = AcpiEvaluateObject(sc->tz_handle, "_SCP", &args, NULL))) {
762 	    if (status != AE_NOT_FOUND)
763 		ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
764 		    "can't evaluate %s._SCP - %s\n", acpi_name(sc->tz_handle),
765 		    AcpiFormatException(status));
766 	    sc->tz_flags |= TZ_FLAG_NO_SCP;
767 	} else {
768 	    /* we have to re-evaluate the entire zone now */
769 	    AcpiOsQueueForExecution(OSD_PRIORITY_HIGH, (OSD_EXECUTION_CALLBACK)acpi_tz_establish, sc);
770 	}
771     }
772     ACPI_UNLOCK;
773 }
774 
775 /*
776  * Thermal zone monitor thread.
777  */
778 static void
779 acpi_tz_thread(void *arg)
780 {
781     device_t	*devs;
782     int		devcount, i;
783 
784     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
785 
786 
787     devs = NULL;
788     devcount = 0;
789 
790     for (;;) {
791 	tsleep(&acpi_tz_proc, PZERO, "nothing", hz * acpi_tz_polling_rate);
792 
793 	mtx_lock(&Giant);
794 
795 	if (devcount == 0)
796 	    devclass_get_devices(acpi_tz_devclass, &devs, &devcount);
797 
798 	ACPI_LOCK;
799 	for (i = 0; i < devcount; i++)
800 	    acpi_tz_timeout(device_get_softc(devs[i]));
801 	ACPI_UNLOCK;
802 
803 	mtx_unlock(&Giant);
804     }
805 }
806