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