xref: /freebsd/sys/dev/acpica/acpi_thermal.c (revision 2be1a816b9ff69588e55be0a84cbe2a31efc0f2f)
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 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_acpi.h"
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/bus.h>
35 #include <sys/cpu.h>
36 #include <sys/kthread.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/bus.h>
40 #include <sys/proc.h>
41 #include <sys/reboot.h>
42 #include <sys/sysctl.h>
43 #include <sys/unistd.h>
44 #include <sys/power.h>
45 
46 #include "cpufreq_if.h"
47 
48 #include <contrib/dev/acpica/acpi.h>
49 #include <dev/acpica/acpivar.h>
50 
51 /* Hooks for the ACPI CA debugging infrastructure */
52 #define _COMPONENT	ACPI_THERMAL
53 ACPI_MODULE_NAME("THERMAL")
54 
55 #define TZ_ZEROC	2732
56 #define TZ_KELVTOC(x)	(((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10)
57 
58 #define TZ_NOTIFY_TEMPERATURE	0x80 /* Temperature changed. */
59 #define TZ_NOTIFY_LEVELS	0x81 /* Cooling levels changed. */
60 #define TZ_NOTIFY_DEVICES	0x82 /* Device lists changed. */
61 #define TZ_NOTIFY_CRITICAL	0xcc /* Fake notify that _CRT/_HOT reached. */
62 
63 /* Check for temperature changes every 10 seconds by default */
64 #define TZ_POLLRATE	10
65 
66 /* Make sure the reported temperature is valid for this number of polls. */
67 #define TZ_VALIDCHECKS	3
68 
69 /* Notify the user we will be shutting down in one more poll cycle. */
70 #define TZ_NOTIFYCOUNT	(TZ_VALIDCHECKS - 1)
71 
72 /* ACPI spec defines this */
73 #define TZ_NUMLEVELS	10
74 struct acpi_tz_zone {
75     int		ac[TZ_NUMLEVELS];
76     ACPI_BUFFER	al[TZ_NUMLEVELS];
77     int		crt;
78     int		hot;
79     ACPI_BUFFER	psl;
80     int		psv;
81     int		tc1;
82     int		tc2;
83     int		tsp;
84     int		tzp;
85 };
86 
87 struct acpi_tz_softc {
88     device_t			tz_dev;
89     ACPI_HANDLE			tz_handle;	/*Thermal zone handle*/
90     int				tz_temperature;	/*Current temperature*/
91     int				tz_active;	/*Current active cooling*/
92 #define TZ_ACTIVE_NONE		-1
93     int				tz_requested;	/*Minimum active cooling*/
94     int				tz_thflags;	/*Current temp-related flags*/
95 #define TZ_THFLAG_NONE		0
96 #define TZ_THFLAG_PSV		(1<<0)
97 #define TZ_THFLAG_HOT		(1<<2)
98 #define TZ_THFLAG_CRT		(1<<3)
99     int				tz_flags;
100 #define TZ_FLAG_NO_SCP		(1<<0)		/*No _SCP method*/
101 #define TZ_FLAG_GETPROFILE	(1<<1)		/*Get power_profile in timeout*/
102 #define TZ_FLAG_GETSETTINGS	(1<<2)		/*Get devs/setpoints*/
103     struct timespec		tz_cooling_started;
104 					/*Current cooling starting time*/
105 
106     struct sysctl_ctx_list	tz_sysctl_ctx;
107     struct sysctl_oid		*tz_sysctl_tree;
108     eventhandler_tag		tz_event;
109 
110     struct acpi_tz_zone 	tz_zone;	/*Thermal zone parameters*/
111     int				tz_validchecks;
112 
113     /* passive cooling */
114     struct proc			*tz_cooling_proc;
115     int				tz_cooling_proc_running;
116     int				tz_cooling_enabled;
117     int				tz_cooling_active;
118     int				tz_cooling_updated;
119     int				tz_cooling_saved_freq;
120 };
121 
122 #define CPUFREQ_MAX_LEVELS	64 /* XXX cpufreq should export this */
123 
124 static int	acpi_tz_probe(device_t dev);
125 static int	acpi_tz_attach(device_t dev);
126 static int	acpi_tz_establish(struct acpi_tz_softc *sc);
127 static void	acpi_tz_monitor(void *Context);
128 static void	acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg);
129 static void	acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg);
130 static void	acpi_tz_getparam(struct acpi_tz_softc *sc, char *node,
131 				 int *data);
132 static void	acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what);
133 static int	acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS);
134 static int	acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS);
135 static int	acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS);
136 static int	acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS);
137 static void	acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify,
138 				       void *context);
139 static void	acpi_tz_signal(struct acpi_tz_softc *sc, int flags);
140 static void	acpi_tz_timeout(struct acpi_tz_softc *sc, int flags);
141 static void	acpi_tz_power_profile(void *arg);
142 static void	acpi_tz_thread(void *arg);
143 static int	acpi_tz_cooling_is_available(struct acpi_tz_softc *sc);
144 static int	acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc);
145 
146 static device_method_t acpi_tz_methods[] = {
147     /* Device interface */
148     DEVMETHOD(device_probe,	acpi_tz_probe),
149     DEVMETHOD(device_attach,	acpi_tz_attach),
150 
151     {0, 0}
152 };
153 
154 static driver_t acpi_tz_driver = {
155     "acpi_tz",
156     acpi_tz_methods,
157     sizeof(struct acpi_tz_softc),
158 };
159 
160 static devclass_t acpi_tz_devclass;
161 DRIVER_MODULE(acpi_tz, acpi, acpi_tz_driver, acpi_tz_devclass, 0, 0);
162 MODULE_DEPEND(acpi_tz, acpi, 1, 1, 1);
163 
164 static struct sysctl_ctx_list	acpi_tz_sysctl_ctx;
165 static struct sysctl_oid	*acpi_tz_sysctl_tree;
166 
167 /* Minimum cooling run time */
168 static int			acpi_tz_min_runtime;
169 static int			acpi_tz_polling_rate = TZ_POLLRATE;
170 static int			acpi_tz_override;
171 
172 /* Timezone polling thread */
173 static struct proc		*acpi_tz_proc;
174 ACPI_LOCK_DECL(thermal, "ACPI thermal zone");
175 
176 static int			acpi_tz_cooling_unit = -1;
177 
178 static int
179 acpi_tz_probe(device_t dev)
180 {
181     int		result;
182 
183     if (acpi_get_type(dev) == ACPI_TYPE_THERMAL && !acpi_disabled("thermal")) {
184 	device_set_desc(dev, "Thermal Zone");
185 	result = -10;
186     } else
187 	result = ENXIO;
188     return (result);
189 }
190 
191 static int
192 acpi_tz_attach(device_t dev)
193 {
194     struct acpi_tz_softc	*sc;
195     struct acpi_softc		*acpi_sc;
196     int				error;
197     char			oidname[8];
198 
199     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
200 
201     sc = device_get_softc(dev);
202     sc->tz_dev = dev;
203     sc->tz_handle = acpi_get_handle(dev);
204     sc->tz_requested = TZ_ACTIVE_NONE;
205     sc->tz_active = TZ_ACTIVE_NONE;
206     sc->tz_thflags = TZ_THFLAG_NONE;
207     sc->tz_cooling_proc = NULL;
208     sc->tz_cooling_proc_running = FALSE;
209     sc->tz_cooling_active = FALSE;
210     sc->tz_cooling_updated = FALSE;
211     sc->tz_cooling_enabled = FALSE;
212 
213     /*
214      * Parse the current state of the thermal zone and build control
215      * structures.  We don't need to worry about interference with the
216      * control thread since we haven't fully attached this device yet.
217      */
218     if ((error = acpi_tz_establish(sc)) != 0)
219 	return (error);
220 
221     /*
222      * Register for any Notify events sent to this zone.
223      */
224     AcpiInstallNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
225 			     acpi_tz_notify_handler, sc);
226 
227     /*
228      * Create our sysctl nodes.
229      *
230      * XXX we need a mechanism for adding nodes under ACPI.
231      */
232     if (device_get_unit(dev) == 0) {
233 	acpi_sc = acpi_device_get_parent_softc(dev);
234 	sysctl_ctx_init(&acpi_tz_sysctl_ctx);
235 	acpi_tz_sysctl_tree = SYSCTL_ADD_NODE(&acpi_tz_sysctl_ctx,
236 			      SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
237 			      OID_AUTO, "thermal", CTLFLAG_RD, 0, "");
238 	SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
239 		       SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
240 		       OID_AUTO, "min_runtime", CTLFLAG_RW,
241 		       &acpi_tz_min_runtime, 0,
242 		       "minimum cooling run time in sec");
243 	SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
244 		       SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
245 		       OID_AUTO, "polling_rate", CTLFLAG_RW,
246 		       &acpi_tz_polling_rate, 0, "monitor polling rate");
247 	SYSCTL_ADD_INT(&acpi_tz_sysctl_ctx,
248 		       SYSCTL_CHILDREN(acpi_tz_sysctl_tree), OID_AUTO,
249 		       "user_override", CTLFLAG_RW, &acpi_tz_override, 0,
250 		       "allow override of thermal settings");
251     }
252     sysctl_ctx_init(&sc->tz_sysctl_ctx);
253     sprintf(oidname, "tz%d", device_get_unit(dev));
254     sc->tz_sysctl_tree = SYSCTL_ADD_NODE(&sc->tz_sysctl_ctx,
255 					 SYSCTL_CHILDREN(acpi_tz_sysctl_tree),
256 					 OID_AUTO, oidname, CTLFLAG_RD, 0, "");
257     SYSCTL_ADD_OPAQUE(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
258 		      OID_AUTO, "temperature", CTLFLAG_RD, &sc->tz_temperature,
259 		      sizeof(sc->tz_temperature), "IK",
260 		      "current thermal zone temperature");
261     SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
262 		    OID_AUTO, "active", CTLTYPE_INT | CTLFLAG_RW,
263 		    sc, 0, acpi_tz_active_sysctl, "I", "cooling is active");
264     SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
265 		    OID_AUTO, "passive_cooling", CTLTYPE_INT | CTLFLAG_RW,
266 		    sc, 0, acpi_tz_cooling_sysctl, "I",
267 		    "enable passive (speed reduction) cooling");
268 
269     SYSCTL_ADD_INT(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
270 		   OID_AUTO, "thermal_flags", CTLFLAG_RD,
271 		   &sc->tz_thflags, 0, "thermal zone flags");
272     SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
273 		    OID_AUTO, "_PSV", CTLTYPE_INT | CTLFLAG_RW,
274 		    sc, offsetof(struct acpi_tz_softc, tz_zone.psv),
275 		    acpi_tz_temp_sysctl, "IK", "passive cooling temp setpoint");
276     SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
277 		    OID_AUTO, "_HOT", CTLTYPE_INT | CTLFLAG_RW,
278 		    sc, offsetof(struct acpi_tz_softc, tz_zone.hot),
279 		    acpi_tz_temp_sysctl, "IK",
280 		    "too hot temp setpoint (suspend now)");
281     SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
282 		    OID_AUTO, "_CRT", CTLTYPE_INT | CTLFLAG_RW,
283 		    sc, offsetof(struct acpi_tz_softc, tz_zone.crt),
284 		    acpi_tz_temp_sysctl, "IK",
285 		    "critical temp setpoint (shutdown now)");
286     SYSCTL_ADD_OPAQUE(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
287 		      OID_AUTO, "_ACx", CTLFLAG_RD, &sc->tz_zone.ac,
288 		      sizeof(sc->tz_zone.ac), "IK", "");
289     SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
290 		    OID_AUTO, "_TC1", CTLTYPE_INT | CTLFLAG_RW,
291 		    sc, offsetof(struct acpi_tz_softc, tz_zone.tc1),
292 		    acpi_tz_passive_sysctl, "I",
293 		    "thermal constant 1 for passive cooling");
294     SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
295 		    OID_AUTO, "_TC2", CTLTYPE_INT | CTLFLAG_RW,
296 		    sc, offsetof(struct acpi_tz_softc, tz_zone.tc2),
297 		    acpi_tz_passive_sysctl, "I",
298 		    "thermal constant 2 for passive cooling");
299     SYSCTL_ADD_PROC(&sc->tz_sysctl_ctx, SYSCTL_CHILDREN(sc->tz_sysctl_tree),
300 		    OID_AUTO, "_TSP", CTLTYPE_INT | CTLFLAG_RW,
301 		    sc, offsetof(struct acpi_tz_softc, tz_zone.tsp),
302 		    acpi_tz_passive_sysctl, "I",
303 		    "thermal sampling period for passive cooling");
304 
305     /*
306      * Create thread to service all of the thermal zones.  Register
307      * our power profile event handler.
308      */
309     sc->tz_event = EVENTHANDLER_REGISTER(power_profile_change,
310 	acpi_tz_power_profile, sc, 0);
311     if (acpi_tz_proc == NULL) {
312 	error = kproc_create(acpi_tz_thread, NULL, &acpi_tz_proc,
313 	    RFHIGHPID, 0, "acpi_thermal");
314 	if (error != 0) {
315 	    device_printf(sc->tz_dev, "could not create thread - %d", error);
316 	    goto out;
317 	}
318     }
319 
320     /*
321      * Create a thread to handle passive cooling for 1st zone which
322      * has _PSV, _TSP, _TC1 and _TC2.  Users can enable it for other
323      * zones manually for now.
324      *
325      * XXX We enable only one zone to avoid multiple zones conflict
326      * with each other since cpufreq currently sets all CPUs to the
327      * given frequency whereas it's possible for different thermal
328      * zones to specify independent settings for multiple CPUs.
329      */
330     if (acpi_tz_cooling_unit < 0 && acpi_tz_cooling_is_available(sc))
331 	sc->tz_cooling_enabled = TRUE;
332     if (sc->tz_cooling_enabled) {
333 	error = acpi_tz_cooling_thread_start(sc);
334 	if (error != 0) {
335 	    sc->tz_cooling_enabled = FALSE;
336 	    goto out;
337 	}
338 	acpi_tz_cooling_unit = device_get_unit(dev);
339     }
340 
341     /*
342      * Flag the event handler for a manual invocation by our timeout.
343      * We defer it like this so that the rest of the subsystem has time
344      * to come up.  Don't bother evaluating/printing the temperature at
345      * this point; on many systems it'll be bogus until the EC is running.
346      */
347     sc->tz_flags |= TZ_FLAG_GETPROFILE;
348 
349 out:
350     if (error != 0) {
351 	EVENTHANDLER_DEREGISTER(power_profile_change, sc->tz_event);
352 	AcpiRemoveNotifyHandler(sc->tz_handle, ACPI_DEVICE_NOTIFY,
353 	    acpi_tz_notify_handler);
354 	sysctl_ctx_free(&sc->tz_sysctl_ctx);
355     }
356     return_VALUE (error);
357 }
358 
359 /*
360  * Parse the current state of this thermal zone and set up to use it.
361  *
362  * Note that we may have previous state, which will have to be discarded.
363  */
364 static int
365 acpi_tz_establish(struct acpi_tz_softc *sc)
366 {
367     ACPI_OBJECT	*obj;
368     int		i;
369     char	nbuf[8];
370 
371     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
372 
373     /* Erase any existing state. */
374     for (i = 0; i < TZ_NUMLEVELS; i++)
375 	if (sc->tz_zone.al[i].Pointer != NULL)
376 	    AcpiOsFree(sc->tz_zone.al[i].Pointer);
377     if (sc->tz_zone.psl.Pointer != NULL)
378 	AcpiOsFree(sc->tz_zone.psl.Pointer);
379 
380     /*
381      * XXX: We initialize only ACPI_BUFFER to avoid race condition
382      * with passive cooling thread which refers psv, tc1, tc2 and tsp.
383      */
384     bzero(sc->tz_zone.ac, sizeof(sc->tz_zone.ac));
385     bzero(sc->tz_zone.al, sizeof(sc->tz_zone.al));
386     bzero(&sc->tz_zone.psl, sizeof(sc->tz_zone.psl));
387 
388     /* Evaluate thermal zone parameters. */
389     for (i = 0; i < TZ_NUMLEVELS; i++) {
390 	sprintf(nbuf, "_AC%d", i);
391 	acpi_tz_getparam(sc, nbuf, &sc->tz_zone.ac[i]);
392 	sprintf(nbuf, "_AL%d", i);
393 	sc->tz_zone.al[i].Length = ACPI_ALLOCATE_BUFFER;
394 	sc->tz_zone.al[i].Pointer = NULL;
395 	AcpiEvaluateObject(sc->tz_handle, nbuf, NULL, &sc->tz_zone.al[i]);
396 	obj = (ACPI_OBJECT *)sc->tz_zone.al[i].Pointer;
397 	if (obj != NULL) {
398 	    /* Should be a package containing a list of power objects */
399 	    if (obj->Type != ACPI_TYPE_PACKAGE) {
400 		device_printf(sc->tz_dev, "%s has unknown type %d, rejecting\n",
401 			      nbuf, obj->Type);
402 		return_VALUE (ENXIO);
403 	    }
404 	}
405     }
406     acpi_tz_getparam(sc, "_CRT", &sc->tz_zone.crt);
407     acpi_tz_getparam(sc, "_HOT", &sc->tz_zone.hot);
408     sc->tz_zone.psl.Length = ACPI_ALLOCATE_BUFFER;
409     sc->tz_zone.psl.Pointer = NULL;
410     AcpiEvaluateObject(sc->tz_handle, "_PSL", NULL, &sc->tz_zone.psl);
411     acpi_tz_getparam(sc, "_PSV", &sc->tz_zone.psv);
412     acpi_tz_getparam(sc, "_TC1", &sc->tz_zone.tc1);
413     acpi_tz_getparam(sc, "_TC2", &sc->tz_zone.tc2);
414     acpi_tz_getparam(sc, "_TSP", &sc->tz_zone.tsp);
415     acpi_tz_getparam(sc, "_TZP", &sc->tz_zone.tzp);
416 
417     /*
418      * Sanity-check the values we've been given.
419      *
420      * XXX what do we do about systems that give us the same value for
421      *     more than one of these setpoints?
422      */
423     acpi_tz_sanity(sc, &sc->tz_zone.crt, "_CRT");
424     acpi_tz_sanity(sc, &sc->tz_zone.hot, "_HOT");
425     acpi_tz_sanity(sc, &sc->tz_zone.psv, "_PSV");
426     for (i = 0; i < TZ_NUMLEVELS; i++)
427 	acpi_tz_sanity(sc, &sc->tz_zone.ac[i], "_ACx");
428 
429     return_VALUE (0);
430 }
431 
432 static char *aclevel_string[] = {
433     "NONE", "_AC0", "_AC1", "_AC2", "_AC3", "_AC4",
434     "_AC5", "_AC6", "_AC7", "_AC8", "_AC9"
435 };
436 
437 static __inline const char *
438 acpi_tz_aclevel_string(int active)
439 {
440     if (active < -1 || active >= TZ_NUMLEVELS)
441 	return (aclevel_string[0]);
442 
443     return (aclevel_string[active + 1]);
444 }
445 
446 /*
447  * Get the current temperature.
448  */
449 static int
450 acpi_tz_get_temperature(struct acpi_tz_softc *sc)
451 {
452     int		temp;
453     ACPI_STATUS	status;
454     static char	*tmp_name = "_TMP";
455 
456     ACPI_FUNCTION_NAME ("acpi_tz_get_temperature");
457 
458     /* Evaluate the thermal zone's _TMP method. */
459     status = acpi_GetInteger(sc->tz_handle, tmp_name, &temp);
460     if (ACPI_FAILURE(status)) {
461 	ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
462 	    "error fetching current temperature -- %s\n",
463 	     AcpiFormatException(status));
464 	return (FALSE);
465     }
466 
467     /* Check it for validity. */
468     acpi_tz_sanity(sc, &temp, tmp_name);
469     if (temp == -1)
470 	return (FALSE);
471 
472     ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "got %d.%dC\n", TZ_KELVTOC(temp)));
473     sc->tz_temperature = temp;
474     return (TRUE);
475 }
476 
477 /*
478  * Evaluate the condition of a thermal zone, take appropriate actions.
479  */
480 static void
481 acpi_tz_monitor(void *Context)
482 {
483     struct acpi_tz_softc *sc;
484     struct	timespec curtime;
485     int		temp;
486     int		i;
487     int		newactive, newflags;
488 
489     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
490 
491     sc = (struct acpi_tz_softc *)Context;
492 
493     /* Get the current temperature. */
494     if (!acpi_tz_get_temperature(sc)) {
495 	/* XXX disable zone? go to max cooling? */
496 	return_VOID;
497     }
498     temp = sc->tz_temperature;
499 
500     /*
501      * Work out what we ought to be doing right now.
502      *
503      * Note that the _ACx levels sort from hot to cold.
504      */
505     newactive = TZ_ACTIVE_NONE;
506     for (i = TZ_NUMLEVELS - 1; i >= 0; i--) {
507 	if (sc->tz_zone.ac[i] != -1 && temp >= sc->tz_zone.ac[i]) {
508 	    newactive = i;
509 	    if (sc->tz_active != newactive) {
510 		ACPI_VPRINT(sc->tz_dev,
511 			    acpi_device_get_parent_softc(sc->tz_dev),
512 			    "_AC%d: temperature %d.%d >= setpoint %d.%d\n", i,
513 			    TZ_KELVTOC(temp), TZ_KELVTOC(sc->tz_zone.ac[i]));
514 	    }
515 	}
516     }
517 
518     /*
519      * We are going to get _ACx level down (colder side), but give a guaranteed
520      * minimum cooling run time if requested.
521      */
522     if (acpi_tz_min_runtime > 0 && sc->tz_active != TZ_ACTIVE_NONE &&
523 	(newactive == TZ_ACTIVE_NONE || newactive > sc->tz_active)) {
524 
525 	getnanotime(&curtime);
526 	timespecsub(&curtime, &sc->tz_cooling_started);
527 	if (curtime.tv_sec < acpi_tz_min_runtime)
528 	    newactive = sc->tz_active;
529     }
530 
531     /* Handle user override of active mode */
532     if (sc->tz_requested != TZ_ACTIVE_NONE && (newactive == TZ_ACTIVE_NONE
533         || sc->tz_requested < newactive))
534 	newactive = sc->tz_requested;
535 
536     /* update temperature-related flags */
537     newflags = TZ_THFLAG_NONE;
538     if (sc->tz_zone.psv != -1 && temp >= sc->tz_zone.psv)
539 	newflags |= TZ_THFLAG_PSV;
540     if (sc->tz_zone.hot != -1 && temp >= sc->tz_zone.hot)
541 	newflags |= TZ_THFLAG_HOT;
542     if (sc->tz_zone.crt != -1 && temp >= sc->tz_zone.crt)
543 	newflags |= TZ_THFLAG_CRT;
544 
545     /* If the active cooling state has changed, we have to switch things. */
546     if (newactive != sc->tz_active) {
547 	/* Turn off the cooling devices that are on, if any are */
548 	if (sc->tz_active != TZ_ACTIVE_NONE)
549 	    acpi_ForeachPackageObject(
550 		(ACPI_OBJECT *)sc->tz_zone.al[sc->tz_active].Pointer,
551 		acpi_tz_switch_cooler_off, sc);
552 
553 	/* Turn on cooling devices that are required, if any are */
554 	if (newactive != TZ_ACTIVE_NONE) {
555 	    acpi_ForeachPackageObject(
556 		(ACPI_OBJECT *)sc->tz_zone.al[newactive].Pointer,
557 		acpi_tz_switch_cooler_on, sc);
558 	}
559 	ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
560 		    "switched from %s to %s: %d.%dC\n",
561 		    acpi_tz_aclevel_string(sc->tz_active),
562 		    acpi_tz_aclevel_string(newactive), TZ_KELVTOC(temp));
563 	sc->tz_active = newactive;
564 	getnanotime(&sc->tz_cooling_started);
565     }
566 
567     /* XXX (de)activate any passive cooling that may be required. */
568 
569     /*
570      * If the temperature is at _HOT or _CRT, increment our event count.
571      * If it has occurred enough times, shutdown the system.  This is
572      * needed because some systems will report an invalid high temperature
573      * for one poll cycle.  It is suspected this is due to the embedded
574      * controller timing out.  A typical value is 138C for one cycle on
575      * a system that is otherwise 65C.
576      *
577      * If we're almost at that threshold, notify the user through devd(8).
578      */
579     if ((newflags & (TZ_THFLAG_HOT | TZ_THFLAG_CRT)) != 0) {
580 	sc->tz_validchecks++;
581 	if (sc->tz_validchecks == TZ_VALIDCHECKS) {
582 	    device_printf(sc->tz_dev,
583 		"WARNING - current temperature (%d.%dC) exceeds safe limits\n",
584 		TZ_KELVTOC(sc->tz_temperature));
585 	    shutdown_nice(RB_POWEROFF);
586 	} else if (sc->tz_validchecks == TZ_NOTIFYCOUNT)
587 	    acpi_UserNotify("Thermal", sc->tz_handle, TZ_NOTIFY_CRITICAL);
588     } else {
589 	sc->tz_validchecks = 0;
590     }
591     sc->tz_thflags = newflags;
592 
593     return_VOID;
594 }
595 
596 /*
597  * Given an object, verify that it's a reference to a device of some sort,
598  * and try to switch it off.
599  */
600 static void
601 acpi_tz_switch_cooler_off(ACPI_OBJECT *obj, void *arg)
602 {
603     ACPI_HANDLE			cooler;
604 
605     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
606 
607     cooler = acpi_GetReference(NULL, obj);
608     if (cooler == NULL) {
609 	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
610 	return_VOID;
611     }
612 
613     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s off\n",
614 		     acpi_name(cooler)));
615     acpi_pwr_switch_consumer(cooler, ACPI_STATE_D3);
616 
617     return_VOID;
618 }
619 
620 /*
621  * Given an object, verify that it's a reference to a device of some sort,
622  * and try to switch it on.
623  *
624  * XXX replication of off/on function code is bad.
625  */
626 static void
627 acpi_tz_switch_cooler_on(ACPI_OBJECT *obj, void *arg)
628 {
629     struct acpi_tz_softc	*sc = (struct acpi_tz_softc *)arg;
630     ACPI_HANDLE			cooler;
631     ACPI_STATUS			status;
632 
633     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
634 
635     cooler = acpi_GetReference(NULL, obj);
636     if (cooler == NULL) {
637 	ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get handle\n"));
638 	return_VOID;
639     }
640 
641     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "called to turn %s on\n",
642 		     acpi_name(cooler)));
643     status = acpi_pwr_switch_consumer(cooler, ACPI_STATE_D0);
644     if (ACPI_FAILURE(status)) {
645 	ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
646 		    "failed to activate %s - %s\n", acpi_name(cooler),
647 		    AcpiFormatException(status));
648     }
649 
650     return_VOID;
651 }
652 
653 /*
654  * Read/debug-print a parameter, default it to -1.
655  */
656 static void
657 acpi_tz_getparam(struct acpi_tz_softc *sc, char *node, int *data)
658 {
659 
660     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
661 
662     if (ACPI_FAILURE(acpi_GetInteger(sc->tz_handle, node, data))) {
663 	*data = -1;
664     } else {
665 	ACPI_DEBUG_PRINT((ACPI_DB_VALUES, "%s.%s = %d\n",
666 			 acpi_name(sc->tz_handle), node, *data));
667     }
668 
669     return_VOID;
670 }
671 
672 /*
673  * Sanity-check a temperature value.  Assume that setpoints
674  * should be between 0C and 200C.
675  */
676 static void
677 acpi_tz_sanity(struct acpi_tz_softc *sc, int *val, char *what)
678 {
679     if (*val != -1 && (*val < TZ_ZEROC || *val > TZ_ZEROC + 2000)) {
680 	device_printf(sc->tz_dev, "%s value is absurd, ignored (%d.%dC)\n",
681 		      what, TZ_KELVTOC(*val));
682 	*val = -1;
683     }
684 }
685 
686 /*
687  * Respond to a sysctl on the active state node.
688  */
689 static int
690 acpi_tz_active_sysctl(SYSCTL_HANDLER_ARGS)
691 {
692     struct acpi_tz_softc	*sc;
693     int				active;
694     int		 		error;
695 
696     sc = (struct acpi_tz_softc *)oidp->oid_arg1;
697     active = sc->tz_active;
698     error = sysctl_handle_int(oidp, &active, 0, req);
699 
700     /* Error or no new value */
701     if (error != 0 || req->newptr == NULL)
702 	return (error);
703     if (active < -1 || active >= TZ_NUMLEVELS)
704 	return (EINVAL);
705 
706     /* Set new preferred level and re-switch */
707     sc->tz_requested = active;
708     acpi_tz_signal(sc, 0);
709     return (0);
710 }
711 
712 static int
713 acpi_tz_cooling_sysctl(SYSCTL_HANDLER_ARGS)
714 {
715     struct acpi_tz_softc *sc;
716     int enabled, error;
717 
718     sc = (struct acpi_tz_softc *)oidp->oid_arg1;
719     enabled = sc->tz_cooling_enabled;
720     error = sysctl_handle_int(oidp, &enabled, 0, req);
721 
722     /* Error or no new value */
723     if (error != 0 || req->newptr == NULL)
724 	return (error);
725     if (enabled != TRUE && enabled != FALSE)
726 	return (EINVAL);
727 
728     if (enabled) {
729 	if (acpi_tz_cooling_is_available(sc))
730 	    error = acpi_tz_cooling_thread_start(sc);
731 	else
732 	    error = ENODEV;
733 	if (error)
734 	    enabled = FALSE;
735     }
736     sc->tz_cooling_enabled = enabled;
737     return (error);
738 }
739 
740 static int
741 acpi_tz_temp_sysctl(SYSCTL_HANDLER_ARGS)
742 {
743     struct acpi_tz_softc	*sc;
744     int				temp, *temp_ptr;
745     int		 		error;
746 
747     sc = oidp->oid_arg1;
748     temp_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
749     temp = *temp_ptr;
750     error = sysctl_handle_int(oidp, &temp, 0, req);
751 
752     /* Error or no new value */
753     if (error != 0 || req->newptr == NULL)
754 	return (error);
755 
756     /* Only allow changing settings if override is set. */
757     if (!acpi_tz_override)
758 	return (EPERM);
759 
760     /* Check user-supplied value for sanity. */
761     acpi_tz_sanity(sc, &temp, "user-supplied temp");
762     if (temp == -1)
763 	return (EINVAL);
764 
765     *temp_ptr = temp;
766     return (0);
767 }
768 
769 static int
770 acpi_tz_passive_sysctl(SYSCTL_HANDLER_ARGS)
771 {
772     struct acpi_tz_softc	*sc;
773     int				val, *val_ptr;
774     int				error;
775 
776     sc = oidp->oid_arg1;
777     val_ptr = (int *)((uintptr_t)sc + oidp->oid_arg2);
778     val = *val_ptr;
779     error = sysctl_handle_int(oidp, &val, 0, req);
780 
781     /* Error or no new value */
782     if (error != 0 || req->newptr == NULL)
783 	return (error);
784 
785     /* Only allow changing settings if override is set. */
786     if (!acpi_tz_override)
787 	return (EPERM);
788 
789     *val_ptr = val;
790     return (0);
791 }
792 
793 static void
794 acpi_tz_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
795 {
796     struct acpi_tz_softc	*sc = (struct acpi_tz_softc *)context;
797 
798     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
799 
800     switch (notify) {
801     case TZ_NOTIFY_TEMPERATURE:
802 	/* Temperature change occurred */
803 	acpi_tz_signal(sc, 0);
804 	break;
805     case TZ_NOTIFY_DEVICES:
806     case TZ_NOTIFY_LEVELS:
807 	/* Zone devices/setpoints changed */
808 	acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
809 	break;
810     default:
811 	ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
812 		    "unknown Notify event 0x%x\n", notify);
813 	break;
814     }
815 
816     acpi_UserNotify("Thermal", h, notify);
817 
818     return_VOID;
819 }
820 
821 static void
822 acpi_tz_signal(struct acpi_tz_softc *sc, int flags)
823 {
824     ACPI_LOCK(thermal);
825     sc->tz_flags |= flags;
826     ACPI_UNLOCK(thermal);
827     wakeup(&acpi_tz_proc);
828 }
829 
830 /*
831  * Notifies can be generated asynchronously but have also been seen to be
832  * triggered by other thermal methods.  One system generates a notify of
833  * 0x81 when the fan is turned on or off.  Another generates it when _SCP
834  * is called.  To handle these situations, we check the zone via
835  * acpi_tz_monitor() before evaluating changes to setpoints or the cooling
836  * policy.
837  */
838 static void
839 acpi_tz_timeout(struct acpi_tz_softc *sc, int flags)
840 {
841 
842     /* Check the current temperature and take action based on it */
843     acpi_tz_monitor(sc);
844 
845     /* If requested, get the power profile settings. */
846     if (flags & TZ_FLAG_GETPROFILE)
847 	acpi_tz_power_profile(sc);
848 
849     /*
850      * If requested, check for new devices/setpoints.  After finding them,
851      * check if we need to switch fans based on the new values.
852      */
853     if (flags & TZ_FLAG_GETSETTINGS) {
854 	acpi_tz_establish(sc);
855 	acpi_tz_monitor(sc);
856     }
857 
858     /* XXX passive cooling actions? */
859 }
860 
861 /*
862  * System power profile may have changed; fetch and notify the
863  * thermal zone accordingly.
864  *
865  * Since this can be called from an arbitrary eventhandler, it needs
866  * to get the ACPI lock itself.
867  */
868 static void
869 acpi_tz_power_profile(void *arg)
870 {
871     ACPI_STATUS			status;
872     struct acpi_tz_softc	*sc = (struct acpi_tz_softc *)arg;
873     int				state;
874 
875     state = power_profile_get_state();
876     if (state != POWER_PROFILE_PERFORMANCE && state != POWER_PROFILE_ECONOMY)
877 	return;
878 
879     /* check that we haven't decided there's no _SCP method */
880     if ((sc->tz_flags & TZ_FLAG_NO_SCP) == 0) {
881 
882 	/* Call _SCP to set the new profile */
883 	status = acpi_SetInteger(sc->tz_handle, "_SCP",
884 	    (state == POWER_PROFILE_PERFORMANCE) ? 0 : 1);
885 	if (ACPI_FAILURE(status)) {
886 	    if (status != AE_NOT_FOUND)
887 		ACPI_VPRINT(sc->tz_dev,
888 			    acpi_device_get_parent_softc(sc->tz_dev),
889 			    "can't evaluate %s._SCP - %s\n",
890 			    acpi_name(sc->tz_handle),
891 			    AcpiFormatException(status));
892 	    sc->tz_flags |= TZ_FLAG_NO_SCP;
893 	} else {
894 	    /* We have to re-evaluate the entire zone now */
895 	    acpi_tz_signal(sc, TZ_FLAG_GETSETTINGS);
896 	}
897     }
898 }
899 
900 /*
901  * Thermal zone monitor thread.
902  */
903 static void
904 acpi_tz_thread(void *arg)
905 {
906     device_t	*devs;
907     int		devcount, i;
908     int		flags;
909     struct acpi_tz_softc **sc;
910 
911     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
912 
913     devs = NULL;
914     devcount = 0;
915     sc = NULL;
916 
917     for (;;) {
918 	/* If the number of devices has changed, re-evaluate. */
919 	if (devclass_get_count(acpi_tz_devclass) != devcount) {
920 	    if (devs != NULL) {
921 		free(devs, M_TEMP);
922 		free(sc, M_TEMP);
923 	    }
924 	    devclass_get_devices(acpi_tz_devclass, &devs, &devcount);
925 	    sc = malloc(sizeof(struct acpi_tz_softc *) * devcount, M_TEMP,
926 			M_WAITOK | M_ZERO);
927 	    for (i = 0; i < devcount; i++)
928 		sc[i] = device_get_softc(devs[i]);
929 	}
930 
931 	/* Check for temperature events and act on them. */
932 	for (i = 0; i < devcount; i++) {
933 	    ACPI_LOCK(thermal);
934 	    flags = sc[i]->tz_flags;
935 	    sc[i]->tz_flags &= TZ_FLAG_NO_SCP;
936 	    ACPI_UNLOCK(thermal);
937 	    acpi_tz_timeout(sc[i], flags);
938 	}
939 
940 	/* If more work to do, don't go to sleep yet. */
941 	ACPI_LOCK(thermal);
942 	for (i = 0; i < devcount; i++) {
943 	    if (sc[i]->tz_flags & ~TZ_FLAG_NO_SCP)
944 		break;
945 	}
946 
947 	/*
948 	 * If we have no more work, sleep for a while, setting PDROP so that
949 	 * the mutex will not be reacquired.  Otherwise, drop the mutex and
950 	 * loop to handle more events.
951 	 */
952 	if (i == devcount)
953 	    msleep(&acpi_tz_proc, &thermal_mutex, PZERO | PDROP, "tzpoll",
954 		hz * acpi_tz_polling_rate);
955 	else
956 	    ACPI_UNLOCK(thermal);
957     }
958 }
959 
960 static int
961 acpi_tz_cpufreq_restore(struct acpi_tz_softc *sc)
962 {
963     device_t dev;
964     int error;
965 
966     if (!sc->tz_cooling_updated)
967 	return (0);
968     if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL)
969 	return (ENXIO);
970     ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
971 	"temperature %d.%dC: resuming previous clock speed (%d MHz)\n",
972 	TZ_KELVTOC(sc->tz_temperature), sc->tz_cooling_saved_freq);
973     error = CPUFREQ_SET(dev, NULL, CPUFREQ_PRIO_KERN);
974     if (error == 0)
975 	sc->tz_cooling_updated = FALSE;
976     return (error);
977 }
978 
979 static int
980 acpi_tz_cpufreq_update(struct acpi_tz_softc *sc, int req)
981 {
982     device_t dev;
983     struct cf_level *levels;
984     int num_levels, error, freq, desired_freq, perf, i;
985 
986     levels = malloc(CPUFREQ_MAX_LEVELS * sizeof(*levels), M_TEMP, M_NOWAIT);
987     if (levels == NULL)
988 	return (ENOMEM);
989 
990     /*
991      * Find the main device, cpufreq0.  We don't yet support independent
992      * CPU frequency control on SMP.
993      */
994     if ((dev = devclass_get_device(devclass_find("cpufreq"), 0)) == NULL) {
995 	error = ENXIO;
996 	goto out;
997     }
998 
999     /* Get the current frequency. */
1000     error = CPUFREQ_GET(dev, &levels[0]);
1001     if (error)
1002 	goto out;
1003     freq = levels[0].total_set.freq;
1004 
1005     /* Get the current available frequency levels. */
1006     num_levels = CPUFREQ_MAX_LEVELS;
1007     error = CPUFREQ_LEVELS(dev, levels, &num_levels);
1008     if (error) {
1009 	if (error == E2BIG)
1010 	    printf("cpufreq: need to increase CPUFREQ_MAX_LEVELS\n");
1011 	goto out;
1012     }
1013 
1014     /* Calculate the desired frequency as a percent of the max frequency. */
1015     perf = 100 * freq / levels[0].total_set.freq - req;
1016     if (perf < 0)
1017 	perf = 0;
1018     else if (perf > 100)
1019 	perf = 100;
1020     desired_freq = levels[0].total_set.freq * perf / 100;
1021 
1022     if (desired_freq < freq) {
1023 	/* Find the closest available frequency, rounding down. */
1024 	for (i = 0; i < num_levels; i++)
1025 	    if (levels[i].total_set.freq <= desired_freq)
1026 		break;
1027 
1028 	/* If we didn't find a relevant setting, use the lowest. */
1029 	if (i == num_levels)
1030 	    i--;
1031     } else {
1032 	/* If we didn't decrease frequency yet, don't increase it. */
1033 	if (!sc->tz_cooling_updated) {
1034 	    sc->tz_cooling_active = FALSE;
1035 	    goto out;
1036 	}
1037 
1038 	/* Use saved cpu frequency as maximum value. */
1039 	if (desired_freq > sc->tz_cooling_saved_freq)
1040 	    desired_freq = sc->tz_cooling_saved_freq;
1041 
1042 	/* Find the closest available frequency, rounding up. */
1043 	for (i = num_levels - 1; i >= 0; i--)
1044 	    if (levels[i].total_set.freq >= desired_freq)
1045 		break;
1046 
1047 	/* If we didn't find a relevant setting, use the highest. */
1048 	if (i == -1)
1049 	    i++;
1050 
1051 	/* If we're going to the highest frequency, restore the old setting. */
1052 	if (i == 0 || desired_freq == sc->tz_cooling_saved_freq) {
1053 	    error = acpi_tz_cpufreq_restore(sc);
1054 	    if (error == 0)
1055 		sc->tz_cooling_active = FALSE;
1056 	    goto out;
1057 	}
1058     }
1059 
1060     /* If we are going to a new frequency, activate it. */
1061     if (levels[i].total_set.freq != freq) {
1062 	ACPI_VPRINT(sc->tz_dev, acpi_device_get_parent_softc(sc->tz_dev),
1063 	    "temperature %d.%dC: %screasing clock speed "
1064 	    "from %d MHz to %d MHz\n",
1065 	    TZ_KELVTOC(sc->tz_temperature),
1066 	    (freq > levels[i].total_set.freq) ? "de" : "in",
1067 	    freq, levels[i].total_set.freq);
1068 	error = CPUFREQ_SET(dev, &levels[i], CPUFREQ_PRIO_KERN);
1069 	if (error == 0 && !sc->tz_cooling_updated) {
1070 	    sc->tz_cooling_saved_freq = freq;
1071 	    sc->tz_cooling_updated = TRUE;
1072 	}
1073     }
1074 
1075 out:
1076     if (levels)
1077 	free(levels, M_TEMP);
1078     return (error);
1079 }
1080 
1081 /*
1082  * Passive cooling thread; monitors current temperature according to the
1083  * cooling interval and calculates whether to scale back CPU frequency.
1084  */
1085 static void
1086 acpi_tz_cooling_thread(void *arg)
1087 {
1088     struct acpi_tz_softc *sc;
1089     int error, perf, curr_temp, prev_temp;
1090 
1091     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
1092 
1093     sc = (struct acpi_tz_softc *)arg;
1094 
1095     prev_temp = sc->tz_temperature;
1096     while (sc->tz_cooling_enabled) {
1097 	if (sc->tz_cooling_active)
1098 	    (void)acpi_tz_get_temperature(sc);
1099 	curr_temp = sc->tz_temperature;
1100 	if (curr_temp >= sc->tz_zone.psv)
1101 	    sc->tz_cooling_active = TRUE;
1102 	if (sc->tz_cooling_active) {
1103 	    perf = sc->tz_zone.tc1 * (curr_temp - prev_temp) +
1104 		   sc->tz_zone.tc2 * (curr_temp - sc->tz_zone.psv);
1105 	    perf /= 10;
1106 
1107 	    if (perf != 0) {
1108 		error = acpi_tz_cpufreq_update(sc, perf);
1109 
1110 		/*
1111 		 * If error and not simply a higher priority setting was
1112 		 * active, disable cooling.
1113 		 */
1114 		if (error != 0 && error != EPERM) {
1115 		    device_printf(sc->tz_dev,
1116 			"failed to set new freq, disabling passive cooling\n");
1117 		    sc->tz_cooling_enabled = FALSE;
1118 		}
1119 	    }
1120 	}
1121 	prev_temp = curr_temp;
1122 	tsleep(&sc->tz_cooling_proc, PZERO, "cooling",
1123 	    hz * sc->tz_zone.tsp / 10);
1124     }
1125     if (sc->tz_cooling_active) {
1126 	acpi_tz_cpufreq_restore(sc);
1127 	sc->tz_cooling_active = FALSE;
1128     }
1129     sc->tz_cooling_proc = NULL;
1130     ACPI_LOCK(thermal);
1131     sc->tz_cooling_proc_running = FALSE;
1132     ACPI_UNLOCK(thermal);
1133     kproc_exit(0);
1134 }
1135 
1136 /*
1137  * TODO: We ignore _PSL (list of cooling devices) since cpufreq enumerates
1138  * all CPUs for us.  However, it's possible in the future _PSL will
1139  * reference non-CPU devices so we may want to support it then.
1140  */
1141 static int
1142 acpi_tz_cooling_is_available(struct acpi_tz_softc *sc)
1143 {
1144     return (sc->tz_zone.tc1 != -1 && sc->tz_zone.tc2 != -1 &&
1145 	sc->tz_zone.tsp != -1 && sc->tz_zone.tsp != 0 &&
1146 	sc->tz_zone.psv != -1);
1147 }
1148 
1149 static int
1150 acpi_tz_cooling_thread_start(struct acpi_tz_softc *sc)
1151 {
1152     int error;
1153     char name[16];
1154 
1155     ACPI_LOCK(thermal);
1156     if (sc->tz_cooling_proc_running) {
1157 	ACPI_UNLOCK(thermal);
1158 	return (0);
1159     }
1160     sc->tz_cooling_proc_running = TRUE;
1161     ACPI_UNLOCK(thermal);
1162     error = 0;
1163     if (sc->tz_cooling_proc == NULL) {
1164 	snprintf(name, sizeof(name), "acpi_cooling%d",
1165 	    device_get_unit(sc->tz_dev));
1166 	error = kproc_create(acpi_tz_cooling_thread, sc,
1167 	    &sc->tz_cooling_proc, RFHIGHPID, 0, name);
1168 	if (error != 0) {
1169 	    device_printf(sc->tz_dev, "could not create thread - %d", error);
1170 	    ACPI_LOCK(thermal);
1171 	    sc->tz_cooling_proc_running = FALSE;
1172 	    ACPI_UNLOCK(thermal);
1173 	}
1174     }
1175     return (error);
1176 }
1177