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