blob: 383c7029d3cee04b60fa3de395cad5b683e7cf8c [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
4 *
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008 * This driver fully implements the ACPI thermal policy as described in the
9 * ACPI 2.0 Specification.
10 *
11 * TBD: 1. Implement passive cooling hysteresis.
12 * 2. Enhance passive cooling (CPU) states/limit interface to support
13 * concepts of 'multiple limiters', upper/lower limits, etc.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014 */
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/dmi.h>
19#include <linux/init.h>
20#include <linux/slab.h>
21#include <linux/types.h>
22#include <linux/jiffies.h>
23#include <linux/kmod.h>
24#include <linux/reboot.h>
25#include <linux/device.h>
26#include <linux/thermal.h>
27#include <linux/acpi.h>
28#include <linux/workqueue.h>
29#include <linux/uaccess.h>
30
31#define PREFIX "ACPI: "
32
33#define ACPI_THERMAL_CLASS "thermal_zone"
34#define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
35#define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
36#define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
37#define ACPI_THERMAL_NOTIFY_DEVICES 0x82
38#define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
39#define ACPI_THERMAL_NOTIFY_HOT 0xF1
40#define ACPI_THERMAL_MODE_ACTIVE 0x00
41
42#define ACPI_THERMAL_MAX_ACTIVE 10
43#define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
44
45#define _COMPONENT ACPI_THERMAL_COMPONENT
46ACPI_MODULE_NAME("thermal");
47
48MODULE_AUTHOR("Paul Diefenbaugh");
49MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
50MODULE_LICENSE("GPL");
51
52static int act;
53module_param(act, int, 0644);
54MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
55
56static int crt;
57module_param(crt, int, 0644);
58MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
59
60static int tzp;
61module_param(tzp, int, 0444);
62MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
63
64static int nocrt;
65module_param(nocrt, int, 0);
66MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
67
68static int off;
69module_param(off, int, 0);
70MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
71
72static int psv;
73module_param(psv, int, 0644);
74MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
75
76static struct workqueue_struct *acpi_thermal_pm_queue;
77
78static int acpi_thermal_add(struct acpi_device *device);
79static int acpi_thermal_remove(struct acpi_device *device);
80static void acpi_thermal_notify(struct acpi_device *device, u32 event);
81
82static const struct acpi_device_id thermal_device_ids[] = {
83 {ACPI_THERMAL_HID, 0},
84 {"", 0},
85};
86MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
87
88#ifdef CONFIG_PM_SLEEP
89static int acpi_thermal_suspend(struct device *dev);
90static int acpi_thermal_resume(struct device *dev);
91#else
92#define acpi_thermal_suspend NULL
93#define acpi_thermal_resume NULL
94#endif
95static SIMPLE_DEV_PM_OPS(acpi_thermal_pm, acpi_thermal_suspend, acpi_thermal_resume);
96
97static struct acpi_driver acpi_thermal_driver = {
98 .name = "thermal",
99 .class = ACPI_THERMAL_CLASS,
100 .ids = thermal_device_ids,
101 .ops = {
102 .add = acpi_thermal_add,
103 .remove = acpi_thermal_remove,
104 .notify = acpi_thermal_notify,
105 },
106 .drv.pm = &acpi_thermal_pm,
107};
108
109struct acpi_thermal_state {
110 u8 critical:1;
111 u8 hot:1;
112 u8 passive:1;
113 u8 active:1;
114 u8 reserved:4;
115 int active_index;
116};
117
118struct acpi_thermal_state_flags {
119 u8 valid:1;
120 u8 enabled:1;
121 u8 reserved:6;
122};
123
124struct acpi_thermal_critical {
125 struct acpi_thermal_state_flags flags;
126 unsigned long temperature;
127};
128
129struct acpi_thermal_hot {
130 struct acpi_thermal_state_flags flags;
131 unsigned long temperature;
132};
133
134struct acpi_thermal_passive {
135 struct acpi_thermal_state_flags flags;
136 unsigned long temperature;
137 unsigned long tc1;
138 unsigned long tc2;
139 unsigned long tsp;
140 struct acpi_handle_list devices;
141};
142
143struct acpi_thermal_active {
144 struct acpi_thermal_state_flags flags;
145 unsigned long temperature;
146 struct acpi_handle_list devices;
147};
148
149struct acpi_thermal_trips {
150 struct acpi_thermal_critical critical;
151 struct acpi_thermal_hot hot;
152 struct acpi_thermal_passive passive;
153 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
154};
155
156struct acpi_thermal_flags {
157 u8 cooling_mode:1; /* _SCP */
158 u8 devices:1; /* _TZD */
159 u8 reserved:6;
160};
161
162struct acpi_thermal {
163 struct acpi_device * device;
164 acpi_bus_id name;
165 unsigned long temperature;
166 unsigned long last_temperature;
167 unsigned long polling_frequency;
168 volatile u8 zombie;
169 struct acpi_thermal_flags flags;
170 struct acpi_thermal_state state;
171 struct acpi_thermal_trips trips;
172 struct acpi_handle_list devices;
173 struct thermal_zone_device *thermal_zone;
174 int tz_enabled;
175 int kelvin_offset;
176 struct work_struct thermal_check_work;
Olivier Deprez0e641232021-09-23 10:07:05 +0200177 struct mutex thermal_check_lock;
178 refcount_t thermal_check_count;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000179};
180
181/* --------------------------------------------------------------------------
182 Thermal Zone Management
183 -------------------------------------------------------------------------- */
184
185static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
186{
187 acpi_status status = AE_OK;
188 unsigned long long tmp;
189
190 if (!tz)
191 return -EINVAL;
192
193 tz->last_temperature = tz->temperature;
194
195 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
196 if (ACPI_FAILURE(status))
197 return -ENODEV;
198
199 tz->temperature = tmp;
200 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
201 tz->temperature));
202
203 return 0;
204}
205
206static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
207{
208 acpi_status status = AE_OK;
209 unsigned long long tmp;
210
211 if (!tz)
212 return -EINVAL;
213
214 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
215 if (ACPI_FAILURE(status))
216 return -ENODEV;
217
218 tz->polling_frequency = tmp;
219 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
220 tz->polling_frequency));
221
222 return 0;
223}
224
225static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
226{
227 if (!tz)
228 return -EINVAL;
229
David Brazdil0f672f62019-12-10 10:32:29 +0000230 if (ACPI_FAILURE(acpi_execute_simple_method(tz->device->handle,
231 "_SCP", mode)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000232 return -ENODEV;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000233
234 return 0;
235}
236
237#define ACPI_TRIPS_CRITICAL 0x01
238#define ACPI_TRIPS_HOT 0x02
239#define ACPI_TRIPS_PASSIVE 0x04
240#define ACPI_TRIPS_ACTIVE 0x08
241#define ACPI_TRIPS_DEVICES 0x10
242
243#define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
244#define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
245
246#define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
247 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
248 ACPI_TRIPS_DEVICES)
249
250/*
251 * This exception is thrown out in two cases:
252 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
253 * when re-evaluating the AML code.
254 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
255 * We need to re-bind the cooling devices of a thermal zone when this occurs.
256 */
257#define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
258do { \
259 if (flags != ACPI_TRIPS_INIT) \
260 ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
261 "ACPI thermal trip point %s changed\n" \
262 "Please send acpidump to linux-acpi@vger.kernel.org", str)); \
263} while (0)
264
265static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
266{
267 acpi_status status = AE_OK;
268 unsigned long long tmp;
269 struct acpi_handle_list devices;
270 int valid = 0;
271 int i;
272
273 /* Critical Shutdown */
274 if (flag & ACPI_TRIPS_CRITICAL) {
275 status = acpi_evaluate_integer(tz->device->handle,
276 "_CRT", NULL, &tmp);
277 tz->trips.critical.temperature = tmp;
278 /*
279 * Treat freezing temperatures as invalid as well; some
280 * BIOSes return really low values and cause reboots at startup.
281 * Below zero (Celsius) values clearly aren't right for sure..
282 * ... so lets discard those as invalid.
283 */
284 if (ACPI_FAILURE(status)) {
285 tz->trips.critical.flags.valid = 0;
286 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
287 "No critical threshold\n"));
288 } else if (tmp <= 2732) {
289 pr_warn(FW_BUG "Invalid critical threshold (%llu)\n",
290 tmp);
291 tz->trips.critical.flags.valid = 0;
292 } else {
293 tz->trips.critical.flags.valid = 1;
294 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
295 "Found critical threshold [%lu]\n",
296 tz->trips.critical.temperature));
297 }
298 if (tz->trips.critical.flags.valid == 1) {
299 if (crt == -1) {
300 tz->trips.critical.flags.valid = 0;
301 } else if (crt > 0) {
302 unsigned long crt_k = CELSIUS_TO_DECI_KELVIN(crt);
303 /*
304 * Allow override critical threshold
305 */
306 if (crt_k > tz->trips.critical.temperature)
307 pr_warn(PREFIX "Critical threshold %d C\n",
308 crt);
309 tz->trips.critical.temperature = crt_k;
310 }
311 }
312 }
313
314 /* Critical Sleep (optional) */
315 if (flag & ACPI_TRIPS_HOT) {
316 status = acpi_evaluate_integer(tz->device->handle,
317 "_HOT", NULL, &tmp);
318 if (ACPI_FAILURE(status)) {
319 tz->trips.hot.flags.valid = 0;
320 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
321 "No hot threshold\n"));
322 } else {
323 tz->trips.hot.temperature = tmp;
324 tz->trips.hot.flags.valid = 1;
325 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
326 "Found hot threshold [%lu]\n",
327 tz->trips.hot.temperature));
328 }
329 }
330
331 /* Passive (optional) */
332 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
333 (flag == ACPI_TRIPS_INIT)) {
334 valid = tz->trips.passive.flags.valid;
335 if (psv == -1) {
336 status = AE_SUPPORT;
337 } else if (psv > 0) {
338 tmp = CELSIUS_TO_DECI_KELVIN(psv);
339 status = AE_OK;
340 } else {
341 status = acpi_evaluate_integer(tz->device->handle,
342 "_PSV", NULL, &tmp);
343 }
344
345 if (ACPI_FAILURE(status))
346 tz->trips.passive.flags.valid = 0;
347 else {
348 tz->trips.passive.temperature = tmp;
349 tz->trips.passive.flags.valid = 1;
350 if (flag == ACPI_TRIPS_INIT) {
351 status = acpi_evaluate_integer(
352 tz->device->handle, "_TC1",
353 NULL, &tmp);
354 if (ACPI_FAILURE(status))
355 tz->trips.passive.flags.valid = 0;
356 else
357 tz->trips.passive.tc1 = tmp;
358 status = acpi_evaluate_integer(
359 tz->device->handle, "_TC2",
360 NULL, &tmp);
361 if (ACPI_FAILURE(status))
362 tz->trips.passive.flags.valid = 0;
363 else
364 tz->trips.passive.tc2 = tmp;
365 status = acpi_evaluate_integer(
366 tz->device->handle, "_TSP",
367 NULL, &tmp);
368 if (ACPI_FAILURE(status))
369 tz->trips.passive.flags.valid = 0;
370 else
371 tz->trips.passive.tsp = tmp;
372 }
373 }
374 }
375 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
376 memset(&devices, 0, sizeof(struct acpi_handle_list));
377 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
378 NULL, &devices);
379 if (ACPI_FAILURE(status)) {
380 pr_warn(PREFIX "Invalid passive threshold\n");
381 tz->trips.passive.flags.valid = 0;
382 }
383 else
384 tz->trips.passive.flags.valid = 1;
385
386 if (memcmp(&tz->trips.passive.devices, &devices,
387 sizeof(struct acpi_handle_list))) {
388 memcpy(&tz->trips.passive.devices, &devices,
389 sizeof(struct acpi_handle_list));
390 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
391 }
392 }
393 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
394 if (valid != tz->trips.passive.flags.valid)
395 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
396 }
397
398 /* Active (optional) */
399 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
400 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
401 valid = tz->trips.active[i].flags.valid;
402
403 if (act == -1)
404 break; /* disable all active trip points */
405
406 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
407 tz->trips.active[i].flags.valid)) {
408 status = acpi_evaluate_integer(tz->device->handle,
409 name, NULL, &tmp);
410 if (ACPI_FAILURE(status)) {
411 tz->trips.active[i].flags.valid = 0;
412 if (i == 0)
413 break;
414 if (act <= 0)
415 break;
416 if (i == 1)
417 tz->trips.active[0].temperature =
418 CELSIUS_TO_DECI_KELVIN(act);
419 else
420 /*
421 * Don't allow override higher than
422 * the next higher trip point
423 */
424 tz->trips.active[i - 1].temperature =
425 (tz->trips.active[i - 2].temperature <
426 CELSIUS_TO_DECI_KELVIN(act) ?
427 tz->trips.active[i - 2].temperature :
428 CELSIUS_TO_DECI_KELVIN(act));
429 break;
430 } else {
431 tz->trips.active[i].temperature = tmp;
432 tz->trips.active[i].flags.valid = 1;
433 }
434 }
435
436 name[2] = 'L';
437 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
438 memset(&devices, 0, sizeof(struct acpi_handle_list));
439 status = acpi_evaluate_reference(tz->device->handle,
440 name, NULL, &devices);
441 if (ACPI_FAILURE(status)) {
442 pr_warn(PREFIX "Invalid active%d threshold\n",
443 i);
444 tz->trips.active[i].flags.valid = 0;
445 }
446 else
447 tz->trips.active[i].flags.valid = 1;
448
449 if (memcmp(&tz->trips.active[i].devices, &devices,
450 sizeof(struct acpi_handle_list))) {
451 memcpy(&tz->trips.active[i].devices, &devices,
452 sizeof(struct acpi_handle_list));
453 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
454 }
455 }
456 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
457 if (valid != tz->trips.active[i].flags.valid)
458 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
459
460 if (!tz->trips.active[i].flags.valid)
461 break;
462 }
463
David Brazdil0f672f62019-12-10 10:32:29 +0000464 if (flag & ACPI_TRIPS_DEVICES) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000465 memset(&devices, 0, sizeof(devices));
466 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
467 NULL, &devices);
468 if (ACPI_SUCCESS(status)
469 && memcmp(&tz->devices, &devices, sizeof(devices))) {
470 tz->devices = devices;
471 ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
472 }
473 }
474
475 return 0;
476}
477
478static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
479{
480 int i, valid, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
481
482 if (ret)
483 return ret;
484
485 valid = tz->trips.critical.flags.valid |
486 tz->trips.hot.flags.valid |
487 tz->trips.passive.flags.valid;
488
489 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
490 valid |= tz->trips.active[i].flags.valid;
491
492 if (!valid) {
493 pr_warn(FW_BUG "No valid trip found\n");
494 return -ENODEV;
495 }
496 return 0;
497}
498
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000499/* sys I/F for generic thermal sysfs support */
500
501static int thermal_get_temp(struct thermal_zone_device *thermal, int *temp)
502{
503 struct acpi_thermal *tz = thermal->devdata;
504 int result;
505
506 if (!tz)
507 return -EINVAL;
508
509 result = acpi_thermal_get_temperature(tz);
510 if (result)
511 return result;
512
513 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(tz->temperature,
514 tz->kelvin_offset);
515 return 0;
516}
517
518static int thermal_get_mode(struct thermal_zone_device *thermal,
519 enum thermal_device_mode *mode)
520{
521 struct acpi_thermal *tz = thermal->devdata;
522
523 if (!tz)
524 return -EINVAL;
525
526 *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
527 THERMAL_DEVICE_DISABLED;
528
529 return 0;
530}
531
Olivier Deprez0e641232021-09-23 10:07:05 +0200532static void acpi_thermal_check_fn(struct work_struct *work);
533
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000534static int thermal_set_mode(struct thermal_zone_device *thermal,
535 enum thermal_device_mode mode)
536{
537 struct acpi_thermal *tz = thermal->devdata;
538 int enable;
539
540 if (!tz)
541 return -EINVAL;
542
543 /*
544 * enable/disable thermal management from ACPI thermal driver
545 */
546 if (mode == THERMAL_DEVICE_ENABLED)
547 enable = 1;
548 else if (mode == THERMAL_DEVICE_DISABLED) {
549 enable = 0;
550 pr_warn("thermal zone will be disabled\n");
551 } else
552 return -EINVAL;
553
554 if (enable != tz->tz_enabled) {
555 tz->tz_enabled = enable;
556 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
557 "%s kernel ACPI thermal control\n",
558 tz->tz_enabled ? "Enable" : "Disable"));
Olivier Deprez0e641232021-09-23 10:07:05 +0200559 acpi_thermal_check_fn(&tz->thermal_check_work);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000560 }
561 return 0;
562}
563
564static int thermal_get_trip_type(struct thermal_zone_device *thermal,
565 int trip, enum thermal_trip_type *type)
566{
567 struct acpi_thermal *tz = thermal->devdata;
568 int i;
569
570 if (!tz || trip < 0)
571 return -EINVAL;
572
573 if (tz->trips.critical.flags.valid) {
574 if (!trip) {
575 *type = THERMAL_TRIP_CRITICAL;
576 return 0;
577 }
578 trip--;
579 }
580
581 if (tz->trips.hot.flags.valid) {
582 if (!trip) {
583 *type = THERMAL_TRIP_HOT;
584 return 0;
585 }
586 trip--;
587 }
588
589 if (tz->trips.passive.flags.valid) {
590 if (!trip) {
591 *type = THERMAL_TRIP_PASSIVE;
592 return 0;
593 }
594 trip--;
595 }
596
597 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
598 tz->trips.active[i].flags.valid; i++) {
599 if (!trip) {
600 *type = THERMAL_TRIP_ACTIVE;
601 return 0;
602 }
603 trip--;
604 }
605
606 return -EINVAL;
607}
608
609static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
610 int trip, int *temp)
611{
612 struct acpi_thermal *tz = thermal->devdata;
613 int i;
614
615 if (!tz || trip < 0)
616 return -EINVAL;
617
618 if (tz->trips.critical.flags.valid) {
619 if (!trip) {
620 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
621 tz->trips.critical.temperature,
622 tz->kelvin_offset);
623 return 0;
624 }
625 trip--;
626 }
627
628 if (tz->trips.hot.flags.valid) {
629 if (!trip) {
630 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
631 tz->trips.hot.temperature,
632 tz->kelvin_offset);
633 return 0;
634 }
635 trip--;
636 }
637
638 if (tz->trips.passive.flags.valid) {
639 if (!trip) {
640 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
641 tz->trips.passive.temperature,
642 tz->kelvin_offset);
643 return 0;
644 }
645 trip--;
646 }
647
648 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
649 tz->trips.active[i].flags.valid; i++) {
650 if (!trip) {
651 *temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
652 tz->trips.active[i].temperature,
653 tz->kelvin_offset);
654 return 0;
655 }
656 trip--;
657 }
658
659 return -EINVAL;
660}
661
662static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
663 int *temperature)
664{
665 struct acpi_thermal *tz = thermal->devdata;
666
667 if (tz->trips.critical.flags.valid) {
668 *temperature = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
669 tz->trips.critical.temperature,
670 tz->kelvin_offset);
671 return 0;
672 } else
673 return -EINVAL;
674}
675
676static int thermal_get_trend(struct thermal_zone_device *thermal,
677 int trip, enum thermal_trend *trend)
678{
679 struct acpi_thermal *tz = thermal->devdata;
680 enum thermal_trip_type type;
681 int i;
682
683 if (thermal_get_trip_type(thermal, trip, &type))
684 return -EINVAL;
685
686 if (type == THERMAL_TRIP_ACTIVE) {
687 int trip_temp;
688 int temp = DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(
689 tz->temperature, tz->kelvin_offset);
690 if (thermal_get_trip_temp(thermal, trip, &trip_temp))
691 return -EINVAL;
692
693 if (temp > trip_temp) {
694 *trend = THERMAL_TREND_RAISING;
695 return 0;
696 } else {
697 /* Fall back on default trend */
698 return -EINVAL;
699 }
700 }
701
702 /*
703 * tz->temperature has already been updated by generic thermal layer,
704 * before this callback being invoked
705 */
706 i = (tz->trips.passive.tc1 * (tz->temperature - tz->last_temperature))
707 + (tz->trips.passive.tc2
708 * (tz->temperature - tz->trips.passive.temperature));
709
710 if (i > 0)
711 *trend = THERMAL_TREND_RAISING;
712 else if (i < 0)
713 *trend = THERMAL_TREND_DROPPING;
714 else
715 *trend = THERMAL_TREND_STABLE;
716 return 0;
717}
718
719
720static int thermal_notify(struct thermal_zone_device *thermal, int trip,
721 enum thermal_trip_type trip_type)
722{
723 u8 type = 0;
724 struct acpi_thermal *tz = thermal->devdata;
725
726 if (trip_type == THERMAL_TRIP_CRITICAL)
727 type = ACPI_THERMAL_NOTIFY_CRITICAL;
728 else if (trip_type == THERMAL_TRIP_HOT)
729 type = ACPI_THERMAL_NOTIFY_HOT;
730 else
731 return 0;
732
733 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
734 dev_name(&tz->device->dev), type, 1);
735
736 if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
737 return 1;
738
739 return 0;
740}
741
742static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
743 struct thermal_cooling_device *cdev,
744 bool bind)
745{
746 struct acpi_device *device = cdev->devdata;
747 struct acpi_thermal *tz = thermal->devdata;
748 struct acpi_device *dev;
749 acpi_status status;
750 acpi_handle handle;
751 int i;
752 int j;
753 int trip = -1;
754 int result = 0;
755
756 if (tz->trips.critical.flags.valid)
757 trip++;
758
759 if (tz->trips.hot.flags.valid)
760 trip++;
761
762 if (tz->trips.passive.flags.valid) {
763 trip++;
764 for (i = 0; i < tz->trips.passive.devices.count;
765 i++) {
766 handle = tz->trips.passive.devices.handles[i];
767 status = acpi_bus_get_device(handle, &dev);
768 if (ACPI_FAILURE(status) || dev != device)
769 continue;
770 if (bind)
771 result =
772 thermal_zone_bind_cooling_device
773 (thermal, trip, cdev,
774 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT,
775 THERMAL_WEIGHT_DEFAULT);
776 else
777 result =
778 thermal_zone_unbind_cooling_device
779 (thermal, trip, cdev);
780 if (result)
781 goto failed;
782 }
783 }
784
785 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
786 if (!tz->trips.active[i].flags.valid)
787 break;
788 trip++;
789 for (j = 0;
790 j < tz->trips.active[i].devices.count;
791 j++) {
792 handle = tz->trips.active[i].devices.handles[j];
793 status = acpi_bus_get_device(handle, &dev);
794 if (ACPI_FAILURE(status) || dev != device)
795 continue;
796 if (bind)
797 result = thermal_zone_bind_cooling_device
798 (thermal, trip, cdev,
799 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT,
800 THERMAL_WEIGHT_DEFAULT);
801 else
802 result = thermal_zone_unbind_cooling_device
803 (thermal, trip, cdev);
804 if (result)
805 goto failed;
806 }
807 }
808
809 for (i = 0; i < tz->devices.count; i++) {
810 handle = tz->devices.handles[i];
811 status = acpi_bus_get_device(handle, &dev);
812 if (ACPI_SUCCESS(status) && (dev == device)) {
813 if (bind)
814 result = thermal_zone_bind_cooling_device
815 (thermal, THERMAL_TRIPS_NONE,
816 cdev, THERMAL_NO_LIMIT,
817 THERMAL_NO_LIMIT,
818 THERMAL_WEIGHT_DEFAULT);
819 else
820 result = thermal_zone_unbind_cooling_device
821 (thermal, THERMAL_TRIPS_NONE,
822 cdev);
823 if (result)
824 goto failed;
825 }
826 }
827
828failed:
829 return result;
830}
831
832static int
833acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
834 struct thermal_cooling_device *cdev)
835{
836 return acpi_thermal_cooling_device_cb(thermal, cdev, true);
837}
838
839static int
840acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
841 struct thermal_cooling_device *cdev)
842{
843 return acpi_thermal_cooling_device_cb(thermal, cdev, false);
844}
845
846static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
847 .bind = acpi_thermal_bind_cooling_device,
848 .unbind = acpi_thermal_unbind_cooling_device,
849 .get_temp = thermal_get_temp,
850 .get_mode = thermal_get_mode,
851 .set_mode = thermal_set_mode,
852 .get_trip_type = thermal_get_trip_type,
853 .get_trip_temp = thermal_get_trip_temp,
854 .get_crit_temp = thermal_get_crit_temp,
855 .get_trend = thermal_get_trend,
856 .notify = thermal_notify,
857};
858
859static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
860{
861 int trips = 0;
862 int result;
863 acpi_status status;
864 int i;
865
866 if (tz->trips.critical.flags.valid)
867 trips++;
868
869 if (tz->trips.hot.flags.valid)
870 trips++;
871
872 if (tz->trips.passive.flags.valid)
873 trips++;
874
875 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
876 tz->trips.active[i].flags.valid; i++, trips++);
877
878 if (tz->trips.passive.flags.valid)
879 tz->thermal_zone =
880 thermal_zone_device_register("acpitz", trips, 0, tz,
881 &acpi_thermal_zone_ops, NULL,
882 tz->trips.passive.tsp*100,
883 tz->polling_frequency*100);
884 else
885 tz->thermal_zone =
886 thermal_zone_device_register("acpitz", trips, 0, tz,
887 &acpi_thermal_zone_ops, NULL,
888 0, tz->polling_frequency*100);
889 if (IS_ERR(tz->thermal_zone))
890 return -ENODEV;
891
892 result = sysfs_create_link(&tz->device->dev.kobj,
893 &tz->thermal_zone->device.kobj, "thermal_zone");
894 if (result)
895 return result;
896
897 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
898 &tz->device->dev.kobj, "device");
899 if (result)
900 return result;
901
902 status = acpi_bus_attach_private_data(tz->device->handle,
903 tz->thermal_zone);
904 if (ACPI_FAILURE(status))
905 return -ENODEV;
906
907 tz->tz_enabled = 1;
908
909 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
910 tz->thermal_zone->id);
911 return 0;
912}
913
914static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
915{
916 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
917 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
918 thermal_zone_device_unregister(tz->thermal_zone);
919 tz->thermal_zone = NULL;
920 acpi_bus_detach_private_data(tz->device->handle);
921}
922
923
924/* --------------------------------------------------------------------------
925 Driver Interface
926 -------------------------------------------------------------------------- */
927
Olivier Deprez0e641232021-09-23 10:07:05 +0200928static void acpi_queue_thermal_check(struct acpi_thermal *tz)
929{
930 if (!work_pending(&tz->thermal_check_work))
931 queue_work(acpi_thermal_pm_queue, &tz->thermal_check_work);
932}
933
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000934static void acpi_thermal_notify(struct acpi_device *device, u32 event)
935{
936 struct acpi_thermal *tz = acpi_driver_data(device);
937
938
939 if (!tz)
940 return;
941
942 switch (event) {
943 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
Olivier Deprez0e641232021-09-23 10:07:05 +0200944 acpi_queue_thermal_check(tz);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000945 break;
946 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
947 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
Olivier Deprez0e641232021-09-23 10:07:05 +0200948 acpi_queue_thermal_check(tz);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000949 acpi_bus_generate_netlink_event(device->pnp.device_class,
950 dev_name(&device->dev), event, 0);
951 break;
952 case ACPI_THERMAL_NOTIFY_DEVICES:
953 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
Olivier Deprez0e641232021-09-23 10:07:05 +0200954 acpi_queue_thermal_check(tz);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000955 acpi_bus_generate_netlink_event(device->pnp.device_class,
956 dev_name(&device->dev), event, 0);
957 break;
958 default:
959 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
960 "Unsupported event [0x%x]\n", event));
961 break;
962 }
963}
964
965/*
966 * On some platforms, the AML code has dependency about
967 * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx.
968 * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after
969 * /_CRT/_HOT/_PSV/_ACx, or else system will be power off.
970 * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0
971 * if _TMP has never been evaluated.
972 *
973 * As this dependency is totally transparent to OS, evaluate
974 * all of them once, in the order of _CRT/_HOT/_PSV/_ACx,
975 * _TMP, before they are actually used.
976 */
977static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz)
978{
979 acpi_handle handle = tz->device->handle;
980 unsigned long long value;
981 int i;
982
983 acpi_evaluate_integer(handle, "_CRT", NULL, &value);
984 acpi_evaluate_integer(handle, "_HOT", NULL, &value);
985 acpi_evaluate_integer(handle, "_PSV", NULL, &value);
986 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
987 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
988 acpi_status status;
989
990 status = acpi_evaluate_integer(handle, name, NULL, &value);
991 if (status == AE_NOT_FOUND)
992 break;
993 }
994 acpi_evaluate_integer(handle, "_TMP", NULL, &value);
995}
996
997static int acpi_thermal_get_info(struct acpi_thermal *tz)
998{
999 int result = 0;
1000
1001
1002 if (!tz)
1003 return -EINVAL;
1004
1005 acpi_thermal_aml_dependency_fix(tz);
1006
1007 /* Get trip points [_CRT, _PSV, etc.] (required) */
1008 result = acpi_thermal_get_trip_points(tz);
1009 if (result)
1010 return result;
1011
1012 /* Get temperature [_TMP] (required) */
1013 result = acpi_thermal_get_temperature(tz);
1014 if (result)
1015 return result;
1016
1017 /* Set the cooling mode [_SCP] to active cooling (default) */
1018 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
1019 if (!result)
1020 tz->flags.cooling_mode = 1;
1021
1022 /* Get default polling frequency [_TZP] (optional) */
1023 if (tzp)
1024 tz->polling_frequency = tzp;
1025 else
1026 acpi_thermal_get_polling_frequency(tz);
1027
1028 return 0;
1029}
1030
1031/*
1032 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
1033 * handles temperature values with a single decimal place. As a consequence,
1034 * some implementations use an offset of 273.1 and others use an offset of
1035 * 273.2. Try to find out which one is being used, to present the most
1036 * accurate and visually appealing number.
1037 *
1038 * The heuristic below should work for all ACPI thermal zones which have a
1039 * critical trip point with a value being a multiple of 0.5 degree Celsius.
1040 */
1041static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
1042{
1043 if (tz->trips.critical.flags.valid &&
1044 (tz->trips.critical.temperature % 5) == 1)
1045 tz->kelvin_offset = 2731;
1046 else
1047 tz->kelvin_offset = 2732;
1048}
1049
1050static void acpi_thermal_check_fn(struct work_struct *work)
1051{
1052 struct acpi_thermal *tz = container_of(work, struct acpi_thermal,
1053 thermal_check_work);
Olivier Deprez0e641232021-09-23 10:07:05 +02001054
1055 if (!tz->tz_enabled)
1056 return;
1057 /*
1058 * In general, it is not sufficient to check the pending bit, because
1059 * subsequent instances of this function may be queued after one of them
1060 * has started running (e.g. if _TMP sleeps). Avoid bailing out if just
1061 * one of them is running, though, because it may have done the actual
1062 * check some time ago, so allow at least one of them to block on the
1063 * mutex while another one is running the update.
1064 */
1065 if (!refcount_dec_not_one(&tz->thermal_check_count))
1066 return;
1067
1068 mutex_lock(&tz->thermal_check_lock);
1069
1070 thermal_zone_device_update(tz->thermal_zone, THERMAL_EVENT_UNSPECIFIED);
1071
1072 refcount_inc(&tz->thermal_check_count);
1073
1074 mutex_unlock(&tz->thermal_check_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001075}
1076
1077static int acpi_thermal_add(struct acpi_device *device)
1078{
1079 int result = 0;
1080 struct acpi_thermal *tz = NULL;
1081
1082
1083 if (!device)
1084 return -EINVAL;
1085
1086 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1087 if (!tz)
1088 return -ENOMEM;
1089
1090 tz->device = device;
1091 strcpy(tz->name, device->pnp.bus_id);
1092 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1093 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1094 device->driver_data = tz;
1095
1096 result = acpi_thermal_get_info(tz);
1097 if (result)
1098 goto free_memory;
1099
1100 acpi_thermal_guess_offset(tz);
1101
1102 result = acpi_thermal_register_thermal_zone(tz);
1103 if (result)
1104 goto free_memory;
1105
Olivier Deprez0e641232021-09-23 10:07:05 +02001106 refcount_set(&tz->thermal_check_count, 3);
1107 mutex_init(&tz->thermal_check_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001108 INIT_WORK(&tz->thermal_check_work, acpi_thermal_check_fn);
1109
1110 pr_info(PREFIX "%s [%s] (%ld C)\n", acpi_device_name(device),
1111 acpi_device_bid(device), DECI_KELVIN_TO_CELSIUS(tz->temperature));
1112 goto end;
1113
1114free_memory:
1115 kfree(tz);
1116end:
1117 return result;
1118}
1119
1120static int acpi_thermal_remove(struct acpi_device *device)
1121{
1122 struct acpi_thermal *tz = NULL;
1123
1124 if (!device || !acpi_driver_data(device))
1125 return -EINVAL;
1126
1127 flush_workqueue(acpi_thermal_pm_queue);
1128 tz = acpi_driver_data(device);
1129
1130 acpi_thermal_unregister_thermal_zone(tz);
1131 kfree(tz);
1132 return 0;
1133}
1134
1135#ifdef CONFIG_PM_SLEEP
1136static int acpi_thermal_suspend(struct device *dev)
1137{
1138 /* Make sure the previously queued thermal check work has been done */
1139 flush_workqueue(acpi_thermal_pm_queue);
1140 return 0;
1141}
1142
1143static int acpi_thermal_resume(struct device *dev)
1144{
1145 struct acpi_thermal *tz;
1146 int i, j, power_state, result;
1147
1148 if (!dev)
1149 return -EINVAL;
1150
1151 tz = acpi_driver_data(to_acpi_device(dev));
1152 if (!tz)
1153 return -EINVAL;
1154
1155 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1156 if (!(&tz->trips.active[i]))
1157 break;
1158 if (!tz->trips.active[i].flags.valid)
1159 break;
1160 tz->trips.active[i].flags.enabled = 1;
1161 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1162 result = acpi_bus_update_power(
1163 tz->trips.active[i].devices.handles[j],
1164 &power_state);
1165 if (result || (power_state != ACPI_STATE_D0)) {
1166 tz->trips.active[i].flags.enabled = 0;
1167 break;
1168 }
1169 }
1170 tz->state.active |= tz->trips.active[i].flags.enabled;
1171 }
1172
Olivier Deprez0e641232021-09-23 10:07:05 +02001173 acpi_queue_thermal_check(tz);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001174
1175 return AE_OK;
1176}
1177#endif
1178
1179static int thermal_act(const struct dmi_system_id *d) {
1180
1181 if (act == 0) {
1182 pr_notice(PREFIX "%s detected: "
1183 "disabling all active thermal trip points\n", d->ident);
1184 act = -1;
1185 }
1186 return 0;
1187}
1188static int thermal_nocrt(const struct dmi_system_id *d) {
1189
1190 pr_notice(PREFIX "%s detected: "
1191 "disabling all critical thermal trip point actions.\n", d->ident);
1192 nocrt = 1;
1193 return 0;
1194}
1195static int thermal_tzp(const struct dmi_system_id *d) {
1196
1197 if (tzp == 0) {
1198 pr_notice(PREFIX "%s detected: "
1199 "enabling thermal zone polling\n", d->ident);
1200 tzp = 300; /* 300 dS = 30 Seconds */
1201 }
1202 return 0;
1203}
1204static int thermal_psv(const struct dmi_system_id *d) {
1205
1206 if (psv == 0) {
1207 pr_notice(PREFIX "%s detected: "
1208 "disabling all passive thermal trip points\n", d->ident);
1209 psv = -1;
1210 }
1211 return 0;
1212}
1213
1214static const struct dmi_system_id thermal_dmi_table[] __initconst = {
1215 /*
1216 * Award BIOS on this AOpen makes thermal control almost worthless.
1217 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1218 */
1219 {
1220 .callback = thermal_act,
1221 .ident = "AOpen i915GMm-HFS",
1222 .matches = {
1223 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1224 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1225 },
1226 },
1227 {
1228 .callback = thermal_psv,
1229 .ident = "AOpen i915GMm-HFS",
1230 .matches = {
1231 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1232 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1233 },
1234 },
1235 {
1236 .callback = thermal_tzp,
1237 .ident = "AOpen i915GMm-HFS",
1238 .matches = {
1239 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1240 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1241 },
1242 },
1243 {
1244 .callback = thermal_nocrt,
1245 .ident = "Gigabyte GA-7ZX",
1246 .matches = {
1247 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1248 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1249 },
1250 },
1251 {}
1252};
1253
1254static int __init acpi_thermal_init(void)
1255{
1256 int result = 0;
1257
1258 dmi_check_system(thermal_dmi_table);
1259
1260 if (off) {
1261 pr_notice(PREFIX "thermal control disabled\n");
1262 return -ENODEV;
1263 }
1264
1265 acpi_thermal_pm_queue = alloc_workqueue("acpi_thermal_pm",
1266 WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
1267 if (!acpi_thermal_pm_queue)
1268 return -ENODEV;
1269
1270 result = acpi_bus_register_driver(&acpi_thermal_driver);
1271 if (result < 0) {
1272 destroy_workqueue(acpi_thermal_pm_queue);
1273 return -ENODEV;
1274 }
1275
1276 return 0;
1277}
1278
1279static void __exit acpi_thermal_exit(void)
1280{
1281 acpi_bus_unregister_driver(&acpi_thermal_driver);
1282 destroy_workqueue(acpi_thermal_pm_queue);
1283
1284 return;
1285}
1286
1287module_init(acpi_thermal_init);
1288module_exit(acpi_thermal_exit);