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