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