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