]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/acpi/thermal.c
ACPI: thermal: create "thermal.act=" to disable or override active trip point
[mirror_ubuntu-jammy-kernel.git] / drivers / acpi / thermal.c
1 /*
2 * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 *
25 * This driver fully implements the ACPI thermal policy as described in the
26 * ACPI 2.0 Specification.
27 *
28 * TBD: 1. Implement passive cooling hysteresis.
29 * 2. Enhance passive cooling (CPU) states/limit interface to support
30 * concepts of 'multiple limiters', upper/lower limits, etc.
31 *
32 */
33
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/types.h>
38 #include <linux/proc_fs.h>
39 #include <linux/timer.h>
40 #include <linux/jiffies.h>
41 #include <linux/kmod.h>
42 #include <linux/seq_file.h>
43 #include <linux/reboot.h>
44 #include <asm/uaccess.h>
45
46 #include <acpi/acpi_bus.h>
47 #include <acpi/acpi_drivers.h>
48
49 #define ACPI_THERMAL_COMPONENT 0x04000000
50 #define ACPI_THERMAL_CLASS "thermal_zone"
51 #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
52 #define ACPI_THERMAL_FILE_STATE "state"
53 #define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
54 #define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
55 #define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
56 #define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
57 #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
58 #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
59 #define ACPI_THERMAL_NOTIFY_DEVICES 0x82
60 #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
61 #define ACPI_THERMAL_NOTIFY_HOT 0xF1
62 #define ACPI_THERMAL_MODE_ACTIVE 0x00
63
64 #define ACPI_THERMAL_MAX_ACTIVE 10
65 #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
66
67 #define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732>=0) ? ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
68 #define CELSIUS_TO_KELVIN(t) ((t+273)*10)
69
70 #define _COMPONENT ACPI_THERMAL_COMPONENT
71 ACPI_MODULE_NAME("thermal");
72
73 MODULE_AUTHOR("Paul Diefenbaugh");
74 MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
75 MODULE_LICENSE("GPL");
76
77 static int act;
78 module_param(act, int, 0644);
79 MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.\n");
80
81 static int tzp;
82 module_param(tzp, int, 0444);
83 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n");
84
85 static int nocrt;
86 module_param(nocrt, int, 0);
87 MODULE_PARM_DESC(nocrt, "Set to disable action on ACPI thermal zone critical and hot trips.\n");
88
89 static int off;
90 module_param(off, int, 0);
91 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.\n");
92
93 static int psv;
94 module_param(psv, int, 0644);
95 MODULE_PARM_DESC(psv, "Disable or override all passive trip points.\n");
96
97 static int acpi_thermal_add(struct acpi_device *device);
98 static int acpi_thermal_remove(struct acpi_device *device, int type);
99 static int acpi_thermal_resume(struct acpi_device *device);
100 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
101 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
102 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
103 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
104 static ssize_t acpi_thermal_write_cooling_mode(struct file *,
105 const char __user *, size_t,
106 loff_t *);
107 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
108 static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
109 size_t, loff_t *);
110
111 static const struct acpi_device_id thermal_device_ids[] = {
112 {ACPI_THERMAL_HID, 0},
113 {"", 0},
114 };
115 MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
116
117 static struct acpi_driver acpi_thermal_driver = {
118 .name = "thermal",
119 .class = ACPI_THERMAL_CLASS,
120 .ids = thermal_device_ids,
121 .ops = {
122 .add = acpi_thermal_add,
123 .remove = acpi_thermal_remove,
124 .resume = acpi_thermal_resume,
125 },
126 };
127
128 struct acpi_thermal_state {
129 u8 critical:1;
130 u8 hot:1;
131 u8 passive:1;
132 u8 active:1;
133 u8 reserved:4;
134 int active_index;
135 };
136
137 struct acpi_thermal_state_flags {
138 u8 valid:1;
139 u8 enabled:1;
140 u8 reserved:6;
141 };
142
143 struct acpi_thermal_critical {
144 struct acpi_thermal_state_flags flags;
145 unsigned long temperature;
146 };
147
148 struct acpi_thermal_hot {
149 struct acpi_thermal_state_flags flags;
150 unsigned long temperature;
151 };
152
153 struct acpi_thermal_passive {
154 struct acpi_thermal_state_flags flags;
155 unsigned long temperature;
156 unsigned long tc1;
157 unsigned long tc2;
158 unsigned long tsp;
159 struct acpi_handle_list devices;
160 };
161
162 struct acpi_thermal_active {
163 struct acpi_thermal_state_flags flags;
164 unsigned long temperature;
165 struct acpi_handle_list devices;
166 };
167
168 struct acpi_thermal_trips {
169 struct acpi_thermal_critical critical;
170 struct acpi_thermal_hot hot;
171 struct acpi_thermal_passive passive;
172 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
173 };
174
175 struct acpi_thermal_flags {
176 u8 cooling_mode:1; /* _SCP */
177 u8 devices:1; /* _TZD */
178 u8 reserved:6;
179 };
180
181 struct acpi_thermal {
182 struct acpi_device * device;
183 acpi_bus_id name;
184 unsigned long temperature;
185 unsigned long last_temperature;
186 unsigned long polling_frequency;
187 volatile u8 zombie;
188 struct acpi_thermal_flags flags;
189 struct acpi_thermal_state state;
190 struct acpi_thermal_trips trips;
191 struct acpi_handle_list devices;
192 struct timer_list timer;
193 };
194
195 static const struct file_operations acpi_thermal_state_fops = {
196 .open = acpi_thermal_state_open_fs,
197 .read = seq_read,
198 .llseek = seq_lseek,
199 .release = single_release,
200 };
201
202 static const struct file_operations acpi_thermal_temp_fops = {
203 .open = acpi_thermal_temp_open_fs,
204 .read = seq_read,
205 .llseek = seq_lseek,
206 .release = single_release,
207 };
208
209 static const struct file_operations acpi_thermal_trip_fops = {
210 .open = acpi_thermal_trip_open_fs,
211 .read = seq_read,
212 .llseek = seq_lseek,
213 .release = single_release,
214 };
215
216 static const struct file_operations acpi_thermal_cooling_fops = {
217 .open = acpi_thermal_cooling_open_fs,
218 .read = seq_read,
219 .write = acpi_thermal_write_cooling_mode,
220 .llseek = seq_lseek,
221 .release = single_release,
222 };
223
224 static const struct file_operations acpi_thermal_polling_fops = {
225 .open = acpi_thermal_polling_open_fs,
226 .read = seq_read,
227 .write = acpi_thermal_write_polling,
228 .llseek = seq_lseek,
229 .release = single_release,
230 };
231
232 /* --------------------------------------------------------------------------
233 Thermal Zone Management
234 -------------------------------------------------------------------------- */
235
236 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
237 {
238 acpi_status status = AE_OK;
239
240
241 if (!tz)
242 return -EINVAL;
243
244 tz->last_temperature = tz->temperature;
245
246 status =
247 acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tz->temperature);
248 if (ACPI_FAILURE(status))
249 return -ENODEV;
250
251 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
252 tz->temperature));
253
254 return 0;
255 }
256
257 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
258 {
259 acpi_status status = AE_OK;
260
261
262 if (!tz)
263 return -EINVAL;
264
265 status =
266 acpi_evaluate_integer(tz->device->handle, "_TZP", NULL,
267 &tz->polling_frequency);
268 if (ACPI_FAILURE(status))
269 return -ENODEV;
270
271 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
272 tz->polling_frequency));
273
274 return 0;
275 }
276
277 static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
278 {
279
280 if (!tz)
281 return -EINVAL;
282
283 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
284
285 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
286 "Polling frequency set to %lu seconds\n",
287 tz->polling_frequency/10));
288
289 return 0;
290 }
291
292 static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
293 {
294 acpi_status status = AE_OK;
295 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
296 struct acpi_object_list arg_list = { 1, &arg0 };
297 acpi_handle handle = NULL;
298
299
300 if (!tz)
301 return -EINVAL;
302
303 status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
304 if (ACPI_FAILURE(status)) {
305 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
306 return -ENODEV;
307 }
308
309 arg0.integer.value = mode;
310
311 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
312 if (ACPI_FAILURE(status))
313 return -ENODEV;
314
315 return 0;
316 }
317
318 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
319 {
320 acpi_status status = AE_OK;
321 int i = 0;
322
323
324 if (!tz)
325 return -EINVAL;
326
327 /* Critical Shutdown (required) */
328
329 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL,
330 &tz->trips.critical.temperature);
331 if (ACPI_FAILURE(status)) {
332 tz->trips.critical.flags.valid = 0;
333 ACPI_EXCEPTION((AE_INFO, status, "No critical threshold"));
334 return -ENODEV;
335 } else {
336 tz->trips.critical.flags.valid = 1;
337 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
338 "Found critical threshold [%lu]\n",
339 tz->trips.critical.temperature));
340 }
341
342 /* Critical Sleep (optional) */
343
344 status =
345 acpi_evaluate_integer(tz->device->handle, "_HOT", NULL,
346 &tz->trips.hot.temperature);
347 if (ACPI_FAILURE(status)) {
348 tz->trips.hot.flags.valid = 0;
349 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n"));
350 } else {
351 tz->trips.hot.flags.valid = 1;
352 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n",
353 tz->trips.hot.temperature));
354 }
355
356 /* Passive: Processors (optional) */
357
358 if (psv == -1) {
359 status = AE_SUPPORT;
360 } else if (psv > 0) {
361 tz->trips.passive.temperature = CELSIUS_TO_KELVIN(psv);
362 status = AE_OK;
363 } else {
364 status = acpi_evaluate_integer(tz->device->handle,
365 "_PSV", NULL, &tz->trips.passive.temperature);
366 }
367
368 if (ACPI_FAILURE(status)) {
369 tz->trips.passive.flags.valid = 0;
370 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n"));
371 } else {
372 tz->trips.passive.flags.valid = 1;
373
374 status =
375 acpi_evaluate_integer(tz->device->handle, "_TC1", NULL,
376 &tz->trips.passive.tc1);
377 if (ACPI_FAILURE(status))
378 tz->trips.passive.flags.valid = 0;
379
380 status =
381 acpi_evaluate_integer(tz->device->handle, "_TC2", NULL,
382 &tz->trips.passive.tc2);
383 if (ACPI_FAILURE(status))
384 tz->trips.passive.flags.valid = 0;
385
386 status =
387 acpi_evaluate_integer(tz->device->handle, "_TSP", NULL,
388 &tz->trips.passive.tsp);
389 if (ACPI_FAILURE(status))
390 tz->trips.passive.flags.valid = 0;
391
392 status =
393 acpi_evaluate_reference(tz->device->handle, "_PSL", NULL,
394 &tz->trips.passive.devices);
395 if (ACPI_FAILURE(status))
396 tz->trips.passive.flags.valid = 0;
397
398 if (!tz->trips.passive.flags.valid)
399 printk(KERN_WARNING PREFIX "Invalid passive threshold\n");
400 else
401 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
402 "Found passive threshold [%lu]\n",
403 tz->trips.passive.temperature));
404 }
405
406 /* Active: Fans, etc. (optional) */
407
408 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
409
410 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
411
412 if (act == -1)
413 break; /* disable all active trip points */
414
415 status = acpi_evaluate_integer(tz->device->handle,
416 name, NULL, &tz->trips.active[i].temperature);
417
418 if (ACPI_FAILURE(status)) {
419 if (i == 0) /* no active trip points */
420 break;
421 if (act <= 0) /* no override requested */
422 break;
423 if (i == 1) { /* 1 trip point */
424 tz->trips.active[0].temperature =
425 CELSIUS_TO_KELVIN(act);
426 } else { /* multiple trips */
427 /*
428 * Don't allow override higher than
429 * the next higher trip point
430 */
431 tz->trips.active[i - 1].temperature =
432 (tz->trips.active[i - 2].temperature <
433 CELSIUS_TO_KELVIN(act) ?
434 tz->trips.active[i - 2].temperature :
435 CELSIUS_TO_KELVIN(act));
436 }
437 break;
438 }
439
440 name[2] = 'L';
441 status =
442 acpi_evaluate_reference(tz->device->handle, name, NULL,
443 &tz->trips.active[i].devices);
444 if (ACPI_SUCCESS(status)) {
445 tz->trips.active[i].flags.valid = 1;
446 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
447 "Found active threshold [%d]:[%lu]\n",
448 i, tz->trips.active[i].temperature));
449 } else
450 ACPI_EXCEPTION((AE_INFO, status,
451 "Invalid active threshold [%d]", i));
452 }
453
454 return 0;
455 }
456
457 static int acpi_thermal_get_devices(struct acpi_thermal *tz)
458 {
459 acpi_status status = AE_OK;
460
461
462 if (!tz)
463 return -EINVAL;
464
465 status =
466 acpi_evaluate_reference(tz->device->handle, "_TZD", NULL, &tz->devices);
467 if (ACPI_FAILURE(status))
468 return -ENODEV;
469
470 return 0;
471 }
472
473 static int acpi_thermal_critical(struct acpi_thermal *tz)
474 {
475 if (!tz || !tz->trips.critical.flags.valid || nocrt)
476 return -EINVAL;
477
478 if (tz->temperature >= tz->trips.critical.temperature) {
479 printk(KERN_WARNING PREFIX "Critical trip point\n");
480 tz->trips.critical.flags.enabled = 1;
481 } else if (tz->trips.critical.flags.enabled)
482 tz->trips.critical.flags.enabled = 0;
483
484 printk(KERN_EMERG
485 "Critical temperature reached (%ld C), shutting down.\n",
486 KELVIN_TO_CELSIUS(tz->temperature));
487 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
488 tz->trips.critical.flags.enabled);
489
490 orderly_poweroff(true);
491
492 return 0;
493 }
494
495 static int acpi_thermal_hot(struct acpi_thermal *tz)
496 {
497 if (!tz || !tz->trips.hot.flags.valid || nocrt)
498 return -EINVAL;
499
500 if (tz->temperature >= tz->trips.hot.temperature) {
501 printk(KERN_WARNING PREFIX "Hot trip point\n");
502 tz->trips.hot.flags.enabled = 1;
503 } else if (tz->trips.hot.flags.enabled)
504 tz->trips.hot.flags.enabled = 0;
505
506 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
507 tz->trips.hot.flags.enabled);
508
509 /* TBD: Call user-mode "sleep(S4)" function */
510
511 return 0;
512 }
513
514 static void acpi_thermal_passive(struct acpi_thermal *tz)
515 {
516 int result = 1;
517 struct acpi_thermal_passive *passive = NULL;
518 int trend = 0;
519 int i = 0;
520
521
522 if (!tz || !tz->trips.passive.flags.valid)
523 return;
524
525 passive = &(tz->trips.passive);
526
527 /*
528 * Above Trip?
529 * -----------
530 * Calculate the thermal trend (using the passive cooling equation)
531 * and modify the performance limit for all passive cooling devices
532 * accordingly. Note that we assume symmetry.
533 */
534 if (tz->temperature >= passive->temperature) {
535 trend =
536 (passive->tc1 * (tz->temperature - tz->last_temperature)) +
537 (passive->tc2 * (tz->temperature - passive->temperature));
538 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
539 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
540 trend, passive->tc1, tz->temperature,
541 tz->last_temperature, passive->tc2,
542 tz->temperature, passive->temperature));
543 passive->flags.enabled = 1;
544 /* Heating up? */
545 if (trend > 0)
546 for (i = 0; i < passive->devices.count; i++)
547 acpi_processor_set_thermal_limit(passive->
548 devices.
549 handles[i],
550 ACPI_PROCESSOR_LIMIT_INCREMENT);
551 /* Cooling off? */
552 else if (trend < 0) {
553 for (i = 0; i < passive->devices.count; i++)
554 /*
555 * assume that we are on highest
556 * freq/lowest thrott and can leave
557 * passive mode, even in error case
558 */
559 if (!acpi_processor_set_thermal_limit
560 (passive->devices.handles[i],
561 ACPI_PROCESSOR_LIMIT_DECREMENT))
562 result = 0;
563 /*
564 * Leave cooling mode, even if the temp might
565 * higher than trip point This is because some
566 * machines might have long thermal polling
567 * frequencies (tsp) defined. We will fall back
568 * into passive mode in next cycle (probably quicker)
569 */
570 if (result) {
571 passive->flags.enabled = 0;
572 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
573 "Disabling passive cooling, still above threshold,"
574 " but we are cooling down\n"));
575 }
576 }
577 return;
578 }
579
580 /*
581 * Below Trip?
582 * -----------
583 * Implement passive cooling hysteresis to slowly increase performance
584 * and avoid thrashing around the passive trip point. Note that we
585 * assume symmetry.
586 */
587 if (!passive->flags.enabled)
588 return;
589 for (i = 0; i < passive->devices.count; i++)
590 if (!acpi_processor_set_thermal_limit
591 (passive->devices.handles[i],
592 ACPI_PROCESSOR_LIMIT_DECREMENT))
593 result = 0;
594 if (result) {
595 passive->flags.enabled = 0;
596 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
597 "Disabling passive cooling (zone is cool)\n"));
598 }
599 }
600
601 static void acpi_thermal_active(struct acpi_thermal *tz)
602 {
603 int result = 0;
604 struct acpi_thermal_active *active = NULL;
605 int i = 0;
606 int j = 0;
607 unsigned long maxtemp = 0;
608
609
610 if (!tz)
611 return;
612
613 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
614 active = &(tz->trips.active[i]);
615 if (!active || !active->flags.valid)
616 break;
617 if (tz->temperature >= active->temperature) {
618 /*
619 * Above Threshold?
620 * ----------------
621 * If not already enabled, turn ON all cooling devices
622 * associated with this active threshold.
623 */
624 if (active->temperature > maxtemp)
625 tz->state.active_index = i;
626 maxtemp = active->temperature;
627 if (active->flags.enabled)
628 continue;
629 for (j = 0; j < active->devices.count; j++) {
630 result =
631 acpi_bus_set_power(active->devices.
632 handles[j],
633 ACPI_STATE_D0);
634 if (result) {
635 printk(KERN_WARNING PREFIX
636 "Unable to turn cooling device [%p] 'on'\n",
637 active->devices.
638 handles[j]);
639 continue;
640 }
641 active->flags.enabled = 1;
642 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
643 "Cooling device [%p] now 'on'\n",
644 active->devices.handles[j]));
645 }
646 continue;
647 }
648 if (!active->flags.enabled)
649 continue;
650 /*
651 * Below Threshold?
652 * ----------------
653 * Turn OFF all cooling devices associated with this
654 * threshold.
655 */
656 for (j = 0; j < active->devices.count; j++) {
657 result = acpi_bus_set_power(active->devices.handles[j],
658 ACPI_STATE_D3);
659 if (result) {
660 printk(KERN_WARNING PREFIX
661 "Unable to turn cooling device [%p] 'off'\n",
662 active->devices.handles[j]);
663 continue;
664 }
665 active->flags.enabled = 0;
666 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
667 "Cooling device [%p] now 'off'\n",
668 active->devices.handles[j]));
669 }
670 }
671 }
672
673 static void acpi_thermal_check(void *context);
674
675 static void acpi_thermal_run(unsigned long data)
676 {
677 struct acpi_thermal *tz = (struct acpi_thermal *)data;
678 if (!tz->zombie)
679 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
680 }
681
682 static void acpi_thermal_check(void *data)
683 {
684 int result = 0;
685 struct acpi_thermal *tz = data;
686 unsigned long sleep_time = 0;
687 int i = 0;
688 struct acpi_thermal_state state;
689
690
691 if (!tz) {
692 printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
693 return;
694 }
695
696 state = tz->state;
697
698 result = acpi_thermal_get_temperature(tz);
699 if (result)
700 return;
701
702 memset(&tz->state, 0, sizeof(tz->state));
703
704 /*
705 * Check Trip Points
706 * -----------------
707 * Compare the current temperature to the trip point values to see
708 * if we've entered one of the thermal policy states. Note that
709 * this function determines when a state is entered, but the
710 * individual policy decides when it is exited (e.g. hysteresis).
711 */
712 if (tz->trips.critical.flags.valid)
713 state.critical |=
714 (tz->temperature >= tz->trips.critical.temperature);
715 if (tz->trips.hot.flags.valid)
716 state.hot |= (tz->temperature >= tz->trips.hot.temperature);
717 if (tz->trips.passive.flags.valid)
718 state.passive |=
719 (tz->temperature >= tz->trips.passive.temperature);
720 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
721 if (tz->trips.active[i].flags.valid)
722 state.active |=
723 (tz->temperature >=
724 tz->trips.active[i].temperature);
725
726 /*
727 * Invoke Policy
728 * -------------
729 * Separated from the above check to allow individual policy to
730 * determine when to exit a given state.
731 */
732 if (state.critical)
733 acpi_thermal_critical(tz);
734 if (state.hot)
735 acpi_thermal_hot(tz);
736 if (state.passive)
737 acpi_thermal_passive(tz);
738 if (state.active)
739 acpi_thermal_active(tz);
740
741 /*
742 * Calculate State
743 * ---------------
744 * Again, separated from the above two to allow independent policy
745 * decisions.
746 */
747 tz->state.critical = tz->trips.critical.flags.enabled;
748 tz->state.hot = tz->trips.hot.flags.enabled;
749 tz->state.passive = tz->trips.passive.flags.enabled;
750 tz->state.active = 0;
751 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
752 tz->state.active |= tz->trips.active[i].flags.enabled;
753
754 /*
755 * Calculate Sleep Time
756 * --------------------
757 * If we're in the passive state, use _TSP's value. Otherwise
758 * use the default polling frequency (e.g. _TZP). If no polling
759 * frequency is specified then we'll wait forever (at least until
760 * a thermal event occurs). Note that _TSP and _TZD values are
761 * given in 1/10th seconds (we must covert to milliseconds).
762 */
763 if (tz->state.passive)
764 sleep_time = tz->trips.passive.tsp * 100;
765 else if (tz->polling_frequency > 0)
766 sleep_time = tz->polling_frequency * 100;
767
768 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
769 tz->name, tz->temperature, sleep_time));
770
771 /*
772 * Schedule Next Poll
773 * ------------------
774 */
775 if (!sleep_time) {
776 if (timer_pending(&(tz->timer)))
777 del_timer(&(tz->timer));
778 } else {
779 if (timer_pending(&(tz->timer)))
780 mod_timer(&(tz->timer),
781 jiffies + (HZ * sleep_time) / 1000);
782 else {
783 tz->timer.data = (unsigned long)tz;
784 tz->timer.function = acpi_thermal_run;
785 tz->timer.expires = jiffies + (HZ * sleep_time) / 1000;
786 add_timer(&(tz->timer));
787 }
788 }
789
790 return;
791 }
792
793 /* --------------------------------------------------------------------------
794 FS Interface (/proc)
795 -------------------------------------------------------------------------- */
796
797 static struct proc_dir_entry *acpi_thermal_dir;
798
799 static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
800 {
801 struct acpi_thermal *tz = seq->private;
802
803
804 if (!tz)
805 goto end;
806
807 seq_puts(seq, "state: ");
808
809 if (!tz->state.critical && !tz->state.hot && !tz->state.passive
810 && !tz->state.active)
811 seq_puts(seq, "ok\n");
812 else {
813 if (tz->state.critical)
814 seq_puts(seq, "critical ");
815 if (tz->state.hot)
816 seq_puts(seq, "hot ");
817 if (tz->state.passive)
818 seq_puts(seq, "passive ");
819 if (tz->state.active)
820 seq_printf(seq, "active[%d]", tz->state.active_index);
821 seq_puts(seq, "\n");
822 }
823
824 end:
825 return 0;
826 }
827
828 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
829 {
830 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
831 }
832
833 static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
834 {
835 int result = 0;
836 struct acpi_thermal *tz = seq->private;
837
838
839 if (!tz)
840 goto end;
841
842 result = acpi_thermal_get_temperature(tz);
843 if (result)
844 goto end;
845
846 seq_printf(seq, "temperature: %ld C\n",
847 KELVIN_TO_CELSIUS(tz->temperature));
848
849 end:
850 return 0;
851 }
852
853 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
854 {
855 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
856 }
857
858 static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
859 {
860 struct acpi_thermal *tz = seq->private;
861 struct acpi_device *device;
862 acpi_status status;
863
864 int i = 0;
865 int j = 0;
866
867
868 if (!tz)
869 goto end;
870
871 if (tz->trips.critical.flags.valid)
872 seq_printf(seq, "critical (S5): %ld C%s",
873 KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
874 nocrt ? " <disabled>\n" : "\n");
875
876 if (tz->trips.hot.flags.valid)
877 seq_printf(seq, "hot (S4): %ld C%s",
878 KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
879 nocrt ? " <disabled>\n" : "\n");
880
881 if (tz->trips.passive.flags.valid) {
882 seq_printf(seq,
883 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
884 KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
885 tz->trips.passive.tc1, tz->trips.passive.tc2,
886 tz->trips.passive.tsp);
887 for (j = 0; j < tz->trips.passive.devices.count; j++) {
888 status = acpi_bus_get_device(tz->trips.passive.devices.
889 handles[j], &device);
890 seq_printf(seq, "%4.4s ", status ? "" :
891 acpi_device_bid(device));
892 }
893 seq_puts(seq, "\n");
894 }
895
896 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
897 if (!(tz->trips.active[i].flags.valid))
898 break;
899 seq_printf(seq, "active[%d]: %ld C: devices=",
900 i,
901 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
902 for (j = 0; j < tz->trips.active[i].devices.count; j++){
903 status = acpi_bus_get_device(tz->trips.active[i].
904 devices.handles[j],
905 &device);
906 seq_printf(seq, "%4.4s ", status ? "" :
907 acpi_device_bid(device));
908 }
909 seq_puts(seq, "\n");
910 }
911
912 end:
913 return 0;
914 }
915
916 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
917 {
918 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
919 }
920
921 static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
922 {
923 struct acpi_thermal *tz = seq->private;
924
925
926 if (!tz)
927 goto end;
928
929 if (!tz->flags.cooling_mode)
930 seq_puts(seq, "<setting not supported>\n");
931 else
932 seq_puts(seq, "0 - Active; 1 - Passive\n");
933
934 end:
935 return 0;
936 }
937
938 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
939 {
940 return single_open(file, acpi_thermal_cooling_seq_show,
941 PDE(inode)->data);
942 }
943
944 static ssize_t
945 acpi_thermal_write_cooling_mode(struct file *file,
946 const char __user * buffer,
947 size_t count, loff_t * ppos)
948 {
949 struct seq_file *m = file->private_data;
950 struct acpi_thermal *tz = m->private;
951 int result = 0;
952 char mode_string[12] = { '\0' };
953
954
955 if (!tz || (count > sizeof(mode_string) - 1))
956 return -EINVAL;
957
958 if (!tz->flags.cooling_mode)
959 return -ENODEV;
960
961 if (copy_from_user(mode_string, buffer, count))
962 return -EFAULT;
963
964 mode_string[count] = '\0';
965
966 result = acpi_thermal_set_cooling_mode(tz,
967 simple_strtoul(mode_string, NULL,
968 0));
969 if (result)
970 return result;
971
972 acpi_thermal_check(tz);
973
974 return count;
975 }
976
977 static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
978 {
979 struct acpi_thermal *tz = seq->private;
980
981
982 if (!tz)
983 goto end;
984
985 if (!tz->polling_frequency) {
986 seq_puts(seq, "<polling disabled>\n");
987 goto end;
988 }
989
990 seq_printf(seq, "polling frequency: %lu seconds\n",
991 (tz->polling_frequency / 10));
992
993 end:
994 return 0;
995 }
996
997 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
998 {
999 return single_open(file, acpi_thermal_polling_seq_show,
1000 PDE(inode)->data);
1001 }
1002
1003 static ssize_t
1004 acpi_thermal_write_polling(struct file *file,
1005 const char __user * buffer,
1006 size_t count, loff_t * ppos)
1007 {
1008 struct seq_file *m = file->private_data;
1009 struct acpi_thermal *tz = m->private;
1010 int result = 0;
1011 char polling_string[12] = { '\0' };
1012 int seconds = 0;
1013
1014
1015 if (!tz || (count > sizeof(polling_string) - 1))
1016 return -EINVAL;
1017
1018 if (copy_from_user(polling_string, buffer, count))
1019 return -EFAULT;
1020
1021 polling_string[count] = '\0';
1022
1023 seconds = simple_strtoul(polling_string, NULL, 0);
1024
1025 result = acpi_thermal_set_polling(tz, seconds);
1026 if (result)
1027 return result;
1028
1029 acpi_thermal_check(tz);
1030
1031 return count;
1032 }
1033
1034 static int acpi_thermal_add_fs(struct acpi_device *device)
1035 {
1036 struct proc_dir_entry *entry = NULL;
1037
1038
1039 if (!acpi_device_dir(device)) {
1040 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1041 acpi_thermal_dir);
1042 if (!acpi_device_dir(device))
1043 return -ENODEV;
1044 acpi_device_dir(device)->owner = THIS_MODULE;
1045 }
1046
1047 /* 'state' [R] */
1048 entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
1049 S_IRUGO, acpi_device_dir(device));
1050 if (!entry)
1051 return -ENODEV;
1052 else {
1053 entry->proc_fops = &acpi_thermal_state_fops;
1054 entry->data = acpi_driver_data(device);
1055 entry->owner = THIS_MODULE;
1056 }
1057
1058 /* 'temperature' [R] */
1059 entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1060 S_IRUGO, acpi_device_dir(device));
1061 if (!entry)
1062 return -ENODEV;
1063 else {
1064 entry->proc_fops = &acpi_thermal_temp_fops;
1065 entry->data = acpi_driver_data(device);
1066 entry->owner = THIS_MODULE;
1067 }
1068
1069 /* 'trip_points' [R/W] */
1070 entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1071 S_IFREG | S_IRUGO | S_IWUSR,
1072 acpi_device_dir(device));
1073 if (!entry)
1074 return -ENODEV;
1075 else {
1076 entry->proc_fops = &acpi_thermal_trip_fops;
1077 entry->data = acpi_driver_data(device);
1078 entry->owner = THIS_MODULE;
1079 }
1080
1081 /* 'cooling_mode' [R/W] */
1082 entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1083 S_IFREG | S_IRUGO | S_IWUSR,
1084 acpi_device_dir(device));
1085 if (!entry)
1086 return -ENODEV;
1087 else {
1088 entry->proc_fops = &acpi_thermal_cooling_fops;
1089 entry->data = acpi_driver_data(device);
1090 entry->owner = THIS_MODULE;
1091 }
1092
1093 /* 'polling_frequency' [R/W] */
1094 entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1095 S_IFREG | S_IRUGO | S_IWUSR,
1096 acpi_device_dir(device));
1097 if (!entry)
1098 return -ENODEV;
1099 else {
1100 entry->proc_fops = &acpi_thermal_polling_fops;
1101 entry->data = acpi_driver_data(device);
1102 entry->owner = THIS_MODULE;
1103 }
1104
1105 return 0;
1106 }
1107
1108 static int acpi_thermal_remove_fs(struct acpi_device *device)
1109 {
1110
1111 if (acpi_device_dir(device)) {
1112 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1113 acpi_device_dir(device));
1114 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1115 acpi_device_dir(device));
1116 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1117 acpi_device_dir(device));
1118 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1119 acpi_device_dir(device));
1120 remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1121 acpi_device_dir(device));
1122 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1123 acpi_device_dir(device) = NULL;
1124 }
1125
1126 return 0;
1127 }
1128
1129 /* --------------------------------------------------------------------------
1130 Driver Interface
1131 -------------------------------------------------------------------------- */
1132
1133 static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
1134 {
1135 struct acpi_thermal *tz = data;
1136 struct acpi_device *device = NULL;
1137
1138
1139 if (!tz)
1140 return;
1141
1142 device = tz->device;
1143
1144 switch (event) {
1145 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1146 acpi_thermal_check(tz);
1147 break;
1148 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1149 acpi_thermal_get_trip_points(tz);
1150 acpi_thermal_check(tz);
1151 acpi_bus_generate_event(device, event, 0);
1152 break;
1153 case ACPI_THERMAL_NOTIFY_DEVICES:
1154 if (tz->flags.devices)
1155 acpi_thermal_get_devices(tz);
1156 acpi_bus_generate_event(device, event, 0);
1157 break;
1158 default:
1159 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1160 "Unsupported event [0x%x]\n", event));
1161 break;
1162 }
1163
1164 return;
1165 }
1166
1167 static int acpi_thermal_get_info(struct acpi_thermal *tz)
1168 {
1169 int result = 0;
1170
1171
1172 if (!tz)
1173 return -EINVAL;
1174
1175 /* Get temperature [_TMP] (required) */
1176 result = acpi_thermal_get_temperature(tz);
1177 if (result)
1178 return result;
1179
1180 /* Get trip points [_CRT, _PSV, etc.] (required) */
1181 result = acpi_thermal_get_trip_points(tz);
1182 if (result)
1183 return result;
1184
1185 /* Set the cooling mode [_SCP] to active cooling (default) */
1186 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
1187 if (!result)
1188 tz->flags.cooling_mode = 1;
1189
1190 /* Get default polling frequency [_TZP] (optional) */
1191 if (tzp)
1192 tz->polling_frequency = tzp;
1193 else
1194 acpi_thermal_get_polling_frequency(tz);
1195
1196 /* Get devices in this thermal zone [_TZD] (optional) */
1197 result = acpi_thermal_get_devices(tz);
1198 if (!result)
1199 tz->flags.devices = 1;
1200
1201 return 0;
1202 }
1203
1204 static int acpi_thermal_add(struct acpi_device *device)
1205 {
1206 int result = 0;
1207 acpi_status status = AE_OK;
1208 struct acpi_thermal *tz = NULL;
1209
1210
1211 if (!device)
1212 return -EINVAL;
1213
1214 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1215 if (!tz)
1216 return -ENOMEM;
1217
1218 tz->device = device;
1219 strcpy(tz->name, device->pnp.bus_id);
1220 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1221 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1222 acpi_driver_data(device) = tz;
1223
1224 result = acpi_thermal_get_info(tz);
1225 if (result)
1226 goto end;
1227
1228 result = acpi_thermal_add_fs(device);
1229 if (result)
1230 goto end;
1231
1232 init_timer(&tz->timer);
1233
1234 acpi_thermal_check(tz);
1235
1236 status = acpi_install_notify_handler(device->handle,
1237 ACPI_DEVICE_NOTIFY,
1238 acpi_thermal_notify, tz);
1239 if (ACPI_FAILURE(status)) {
1240 result = -ENODEV;
1241 goto end;
1242 }
1243
1244 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
1245 acpi_device_name(device), acpi_device_bid(device),
1246 KELVIN_TO_CELSIUS(tz->temperature));
1247
1248 end:
1249 if (result) {
1250 acpi_thermal_remove_fs(device);
1251 kfree(tz);
1252 }
1253
1254 return result;
1255 }
1256
1257 static int acpi_thermal_remove(struct acpi_device *device, int type)
1258 {
1259 acpi_status status = AE_OK;
1260 struct acpi_thermal *tz = NULL;
1261
1262
1263 if (!device || !acpi_driver_data(device))
1264 return -EINVAL;
1265
1266 tz = acpi_driver_data(device);
1267
1268 /* avoid timer adding new defer task */
1269 tz->zombie = 1;
1270 /* wait for running timer (on other CPUs) finish */
1271 del_timer_sync(&(tz->timer));
1272 /* synchronize deferred task */
1273 acpi_os_wait_events_complete(NULL);
1274 /* deferred task may reinsert timer */
1275 del_timer_sync(&(tz->timer));
1276
1277 status = acpi_remove_notify_handler(device->handle,
1278 ACPI_DEVICE_NOTIFY,
1279 acpi_thermal_notify);
1280
1281 /* Terminate policy */
1282 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
1283 tz->trips.passive.flags.enabled = 0;
1284 acpi_thermal_passive(tz);
1285 }
1286 if (tz->trips.active[0].flags.valid
1287 && tz->trips.active[0].flags.enabled) {
1288 tz->trips.active[0].flags.enabled = 0;
1289 acpi_thermal_active(tz);
1290 }
1291
1292 acpi_thermal_remove_fs(device);
1293
1294 kfree(tz);
1295 return 0;
1296 }
1297
1298 static int acpi_thermal_resume(struct acpi_device *device)
1299 {
1300 struct acpi_thermal *tz = NULL;
1301 int i, j, power_state, result;
1302
1303
1304 if (!device || !acpi_driver_data(device))
1305 return -EINVAL;
1306
1307 tz = acpi_driver_data(device);
1308
1309 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1310 if (!(&tz->trips.active[i]))
1311 break;
1312 if (!tz->trips.active[i].flags.valid)
1313 break;
1314 tz->trips.active[i].flags.enabled = 1;
1315 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1316 result = acpi_bus_get_power(tz->trips.active[i].devices.
1317 handles[j], &power_state);
1318 if (result || (power_state != ACPI_STATE_D0)) {
1319 tz->trips.active[i].flags.enabled = 0;
1320 break;
1321 }
1322 }
1323 tz->state.active |= tz->trips.active[i].flags.enabled;
1324 }
1325
1326 acpi_thermal_check(tz);
1327
1328 return AE_OK;
1329 }
1330
1331 static int __init acpi_thermal_init(void)
1332 {
1333 int result = 0;
1334
1335 if (off) {
1336 printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1337 return -ENODEV;
1338 }
1339 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1340 if (!acpi_thermal_dir)
1341 return -ENODEV;
1342 acpi_thermal_dir->owner = THIS_MODULE;
1343
1344 result = acpi_bus_register_driver(&acpi_thermal_driver);
1345 if (result < 0) {
1346 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1347 return -ENODEV;
1348 }
1349
1350 return 0;
1351 }
1352
1353 static void __exit acpi_thermal_exit(void)
1354 {
1355
1356 acpi_bus_unregister_driver(&acpi_thermal_driver);
1357
1358 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1359
1360 return;
1361 }
1362
1363 module_init(acpi_thermal_init);
1364 module_exit(acpi_thermal_exit);