blob: 95105db642b9823dbdcab603c516d7f6edf1df6e [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +010016#define pr_fmt(fmt) "ACPI: thermal: " fmt
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/module.h>
Len Brown0b5bfa12007-08-12 00:13:02 -040020#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/types.h>
Tim Schmielaucd354f12007-02-14 00:33:14 -080024#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/kmod.h>
Jeremy Fitzhardinge10a0a8d2007-07-17 18:37:02 -070026#include <linux/reboot.h>
Stephen Rothwellf6f5c452009-03-03 12:47:17 +110027#include <linux/device.h>
Zhang Rui3f655ef2008-01-17 15:51:11 +080028#include <linux/thermal.h>
Lv Zheng8b484632013-12-03 08:49:16 +080029#include <linux/acpi.h>
Aaron Lua59ffb22014-03-04 14:24:46 +080030#include <linux/workqueue.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080031#include <linux/uaccess.h>
Akinobu Mita7f49a5c2020-01-30 22:15:33 -080032#include <linux/units.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define ACPI_THERMAL_CLASS "thermal_zone"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#define ACPI_THERMAL_MAX_ACTIVE 10
44#define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
45
Thomas Renninger1cbf4c52004-09-16 11:07:00 -040046MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050047MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070048MODULE_LICENSE("GPL");
49
Len Brownf8707ec2007-08-12 00:12:54 -040050static int act;
51module_param(act, int, 0644);
Len Brown3c1d36d2007-08-14 15:12:56 -040052MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
Len Brownf8707ec2007-08-12 00:12:54 -040053
Len Brownc52a7412007-08-14 15:49:32 -040054static int crt;
55module_param(crt, int, 0644);
56MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static int tzp;
Len Brown730ff342007-08-12 00:12:26 -040059module_param(tzp, int, 0444);
Len Brown3c1d36d2007-08-14 15:12:56 -040060MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Len Brownf5487142007-08-12 00:12:44 -040062static int nocrt;
63module_param(nocrt, int, 0);
Len Brown8c99fdc2007-08-20 18:46:50 -040064MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
Len Brownf5487142007-08-12 00:12:44 -040065
Len Brown72b33ef2007-08-12 00:12:17 -040066static int off;
67module_param(off, int, 0);
Len Brown3c1d36d2007-08-14 15:12:56 -040068MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
Len Brown72b33ef2007-08-12 00:12:17 -040069
Len Browna70cdc52007-08-12 00:12:35 -040070static int psv;
71module_param(psv, int, 0644);
Len Brown3c1d36d2007-08-14 15:12:56 -040072MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
Len Browna70cdc52007-08-12 00:12:35 -040073
Aaron Lua59ffb22014-03-04 14:24:46 +080074static struct workqueue_struct *acpi_thermal_pm_queue;
75
Len Brown4be44fc2005-08-05 00:44:28 -040076static int acpi_thermal_add(struct acpi_device *device);
Rafael J. Wysocki51fac832013-01-24 00:24:48 +010077static int acpi_thermal_remove(struct acpi_device *device);
Bjorn Helgaas342d5502009-04-07 15:37:06 +000078static void acpi_thermal_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Thomas Renninger1ba90e32007-07-23 14:44:41 +020080static const struct acpi_device_id thermal_device_ids[] = {
81 {ACPI_THERMAL_HID, 0},
82 {"", 0},
83};
84MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
85
Rafael J. Wysocki90692402012-08-09 23:00:02 +020086#ifdef CONFIG_PM_SLEEP
Aaron Lua59ffb22014-03-04 14:24:46 +080087static int acpi_thermal_suspend(struct device *dev);
Rafael J. Wysocki167cffb2012-06-27 23:26:18 +020088static int acpi_thermal_resume(struct device *dev);
Shuah Khanfae9e2a2014-02-12 20:19:10 -070089#else
Aaron Lua59ffb22014-03-04 14:24:46 +080090#define acpi_thermal_suspend NULL
Shuah Khanfae9e2a2014-02-12 20:19:10 -070091#define acpi_thermal_resume NULL
Rafael J. Wysocki90692402012-08-09 23:00:02 +020092#endif
Aaron Lua59ffb22014-03-04 14:24:46 +080093static SIMPLE_DEV_PM_OPS(acpi_thermal_pm, acpi_thermal_suspend, acpi_thermal_resume);
Rafael J. Wysocki167cffb2012-06-27 23:26:18 +020094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static struct acpi_driver acpi_thermal_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050096 .name = "thermal",
Len Brown4be44fc2005-08-05 00:44:28 -040097 .class = ACPI_THERMAL_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020098 .ids = thermal_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040099 .ops = {
100 .add = acpi_thermal_add,
101 .remove = acpi_thermal_remove,
Bjorn Helgaas342d5502009-04-07 15:37:06 +0000102 .notify = acpi_thermal_notify,
Len Brown4be44fc2005-08-05 00:44:28 -0400103 },
Rafael J. Wysocki167cffb2012-06-27 23:26:18 +0200104 .drv.pm = &acpi_thermal_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
107struct acpi_thermal_state {
Len Brown4be44fc2005-08-05 00:44:28 -0400108 u8 critical:1;
109 u8 hot:1;
110 u8 passive:1;
111 u8 active:1;
112 u8 reserved:4;
113 int active_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
116struct acpi_thermal_state_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400117 u8 valid:1;
118 u8 enabled:1;
119 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
122struct acpi_thermal_critical {
123 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400124 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
127struct acpi_thermal_hot {
128 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400129 unsigned long temperature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
132struct acpi_thermal_passive {
133 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400134 unsigned long temperature;
135 unsigned long tc1;
136 unsigned long tc2;
137 unsigned long tsp;
138 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
141struct acpi_thermal_active {
142 struct acpi_thermal_state_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400143 unsigned long temperature;
144 struct acpi_handle_list devices;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
147struct acpi_thermal_trips {
148 struct acpi_thermal_critical critical;
Len Brown4be44fc2005-08-05 00:44:28 -0400149 struct acpi_thermal_hot hot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct acpi_thermal_passive passive;
151 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
152};
153
154struct acpi_thermal_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400155 u8 cooling_mode:1; /* _SCP */
156 u8 devices:1; /* _TZD */
157 u8 reserved:6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
160struct acpi_thermal {
Patrick Mochel8348e1b2006-05-19 16:54:40 -0400161 struct acpi_device * device;
Len Brown4be44fc2005-08-05 00:44:28 -0400162 acpi_bus_id name;
163 unsigned long temperature;
164 unsigned long last_temperature;
165 unsigned long polling_frequency;
Len Brown4be44fc2005-08-05 00:44:28 -0400166 volatile u8 zombie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 struct acpi_thermal_flags flags;
168 struct acpi_thermal_state state;
169 struct acpi_thermal_trips trips;
Len Brown4be44fc2005-08-05 00:44:28 -0400170 struct acpi_handle_list devices;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800171 struct thermal_zone_device *thermal_zone;
Akinobu Mita7f49a5c2020-01-30 22:15:33 -0800172 int kelvin_offset; /* in millidegrees */
Aaron Lua59ffb22014-03-04 14:24:46 +0800173 struct work_struct thermal_check_work;
Rafael J. Wysocki81b704d32021-01-14 19:34:22 +0100174 struct mutex thermal_check_lock;
175 refcount_t thermal_check_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/* --------------------------------------------------------------------------
179 Thermal Zone Management
180 -------------------------------------------------------------------------- */
181
Len Brown4be44fc2005-08-05 00:44:28 -0400182static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Len Brown4be44fc2005-08-05 00:44:28 -0400184 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400185 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400188 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 tz->last_temperature = tz->temperature;
191
Matthew Wilcox27663c52008-10-10 02:22:59 -0400192 status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400194 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Matthew Wilcox27663c52008-10-10 02:22:59 -0400196 tz->temperature = tmp;
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100197
198 acpi_handle_debug(tz->device->handle, "Temperature is %lu dK\n",
199 tz->temperature);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Patrick Mocheld550d982006-06-27 00:41:40 -0400201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Len Brown4be44fc2005-08-05 00:44:28 -0400204static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Len Brown4be44fc2005-08-05 00:44:28 -0400206 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400207 unsigned long long tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400210 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Matthew Wilcox27663c52008-10-10 02:22:59 -0400212 status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400214 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Matthew Wilcox27663c52008-10-10 02:22:59 -0400216 tz->polling_frequency = tmp;
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100217 acpi_handle_debug(tz->device->handle, "Polling frequency is %lu dS\n",
218 tz->polling_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Patrick Mocheld550d982006-06-27 00:41:40 -0400220 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Len Brown4be44fc2005-08-05 00:44:28 -0400223static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400226 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Kelsey Skunberge88c7402019-07-21 20:35:30 -0600228 if (ACPI_FAILURE(acpi_execute_simple_method(tz->device->handle,
229 "_SCP", mode)))
Patrick Mocheld550d982006-06-27 00:41:40 -0400230 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Patrick Mocheld550d982006-06-27 00:41:40 -0400232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Zhang Ruice44e192008-01-17 15:51:25 +0800235#define ACPI_TRIPS_CRITICAL 0x01
236#define ACPI_TRIPS_HOT 0x02
237#define ACPI_TRIPS_PASSIVE 0x04
238#define ACPI_TRIPS_ACTIVE 0x08
239#define ACPI_TRIPS_DEVICES 0x10
240
241#define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
242#define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
243
244#define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
245 ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
246 ACPI_TRIPS_DEVICES)
247
248/*
249 * This exception is thrown out in two cases:
250 * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
251 * when re-evaluating the AML code.
252 * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
253 * We need to re-bind the cooling devices of a thermal zone when this occurs.
254 */
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100255#define ACPI_THERMAL_TRIPS_EXCEPTION(flags, tz, str) \
Zhang Ruice44e192008-01-17 15:51:25 +0800256do { \
257 if (flags != ACPI_TRIPS_INIT) \
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100258 acpi_handle_info(tz->device->handle, \
Zhang Ruice44e192008-01-17 15:51:25 +0800259 "ACPI thermal trip point %s changed\n" \
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100260 "Please report to linux-acpi@vger.kernel.org\n", str); \
Zhang Ruice44e192008-01-17 15:51:25 +0800261} while (0)
262
263static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Len Brown4be44fc2005-08-05 00:44:28 -0400265 acpi_status status = AE_OK;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400266 unsigned long long tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800267 struct acpi_handle_list devices;
268 int valid = 0;
269 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Thomas Renningerfa809452010-02-20 11:44:27 +0100271 /* Critical Shutdown */
Zhang Ruice44e192008-01-17 15:51:25 +0800272 if (flag & ACPI_TRIPS_CRITICAL) {
273 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400274 "_CRT", NULL, &tmp);
275 tz->trips.critical.temperature = tmp;
Arjan van de Vena39a2d72008-05-19 15:55:15 -0700276 /*
277 * Treat freezing temperatures as invalid as well; some
278 * BIOSes return really low values and cause reboots at startup.
Adam Buchbinderb731d7b2009-03-13 12:15:26 -0400279 * Below zero (Celsius) values clearly aren't right for sure..
Arjan van de Vena39a2d72008-05-19 15:55:15 -0700280 * ... so lets discard those as invalid.
281 */
Thomas Renningerfa809452010-02-20 11:44:27 +0100282 if (ACPI_FAILURE(status)) {
Len Brownc52a7412007-08-14 15:49:32 -0400283 tz->trips.critical.flags.valid = 0;
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100284 acpi_handle_debug(tz->device->handle,
285 "No critical threshold\n");
Thomas Renningerfa809452010-02-20 11:44:27 +0100286 } else if (tmp <= 2732) {
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100287 pr_info(FW_BUG "Invalid critical threshold (%llu)\n",
Andy Shevchenko766a8a62013-09-13 18:15:46 +0300288 tmp);
Thomas Renningerfa809452010-02-20 11:44:27 +0100289 tz->trips.critical.flags.valid = 0;
Zhang Ruice44e192008-01-17 15:51:25 +0800290 } else {
291 tz->trips.critical.flags.valid = 1;
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100292 acpi_handle_debug(tz->device->handle,
Thomas Renningerfa809452010-02-20 11:44:27 +0100293 "Found critical threshold [%lu]\n",
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100294 tz->trips.critical.temperature);
Zhang Ruice44e192008-01-17 15:51:25 +0800295 }
296 if (tz->trips.critical.flags.valid == 1) {
297 if (crt == -1) {
298 tz->trips.critical.flags.valid = 0;
299 } else if (crt > 0) {
Akinobu Mita7f49a5c2020-01-30 22:15:33 -0800300 unsigned long crt_k = celsius_to_deci_kelvin(crt);
301
Zhang Ruice44e192008-01-17 15:51:25 +0800302 /*
Zhang Rui22a94d72008-10-17 02:41:20 -0400303 * Allow override critical threshold
Zhang Ruice44e192008-01-17 15:51:25 +0800304 */
Zhang Rui22a94d72008-10-17 02:41:20 -0400305 if (crt_k > tz->trips.critical.temperature)
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100306 pr_info("Critical threshold %d C\n", crt);
307
Zhang Rui22a94d72008-10-17 02:41:20 -0400308 tz->trips.critical.temperature = crt_k;
Zhang Ruice44e192008-01-17 15:51:25 +0800309 }
Len Brownc52a7412007-08-14 15:49:32 -0400310 }
311 }
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 /* Critical Sleep (optional) */
Zhang Ruice44e192008-01-17 15:51:25 +0800314 if (flag & ACPI_TRIPS_HOT) {
Len Browna70cdc52007-08-12 00:12:35 -0400315 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400316 "_HOT", NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800317 if (ACPI_FAILURE(status)) {
318 tz->trips.hot.flags.valid = 0;
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100319 acpi_handle_debug(tz->device->handle,
320 "No hot threshold\n");
Zhang Ruice44e192008-01-17 15:51:25 +0800321 } else {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400322 tz->trips.hot.temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800323 tz->trips.hot.flags.valid = 1;
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100324 acpi_handle_debug(tz->device->handle,
325 "Found hot threshold [%lu]\n",
326 tz->trips.hot.temperature);
Zhang Ruice44e192008-01-17 15:51:25 +0800327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329
Zhang Ruice44e192008-01-17 15:51:25 +0800330 /* Passive (optional) */
Zhang Rui0e4240d2009-01-16 12:53:42 -0500331 if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
332 (flag == ACPI_TRIPS_INIT)) {
Zhang Ruice44e192008-01-17 15:51:25 +0800333 valid = tz->trips.passive.flags.valid;
334 if (psv == -1) {
335 status = AE_SUPPORT;
336 } else if (psv > 0) {
Akinobu Mita7f49a5c2020-01-30 22:15:33 -0800337 tmp = celsius_to_deci_kelvin(psv);
Zhang Ruice44e192008-01-17 15:51:25 +0800338 status = AE_OK;
339 } else {
340 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400341 "_PSV", NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Zhang Ruice44e192008-01-17 15:51:25 +0800344 if (ACPI_FAILURE(status))
345 tz->trips.passive.flags.valid = 0;
346 else {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400347 tz->trips.passive.temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800348 tz->trips.passive.flags.valid = 1;
349 if (flag == ACPI_TRIPS_INIT) {
350 status = acpi_evaluate_integer(
351 tz->device->handle, "_TC1",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400352 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800353 if (ACPI_FAILURE(status))
354 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400355 else
356 tz->trips.passive.tc1 = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800357 status = acpi_evaluate_integer(
358 tz->device->handle, "_TC2",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400359 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800360 if (ACPI_FAILURE(status))
361 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400362 else
363 tz->trips.passive.tc2 = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800364 status = acpi_evaluate_integer(
365 tz->device->handle, "_TSP",
Matthew Wilcox27663c52008-10-10 02:22:59 -0400366 NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800367 if (ACPI_FAILURE(status))
368 tz->trips.passive.flags.valid = 0;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400369 else
370 tz->trips.passive.tsp = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800371 }
372 }
373 }
374 if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
375 memset(&devices, 0, sizeof(struct acpi_handle_list));
376 status = acpi_evaluate_reference(tz->device->handle, "_PSL",
377 NULL, &devices);
Zhang Rui0e4240d2009-01-16 12:53:42 -0500378 if (ACPI_FAILURE(status)) {
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100379 acpi_handle_info(tz->device->handle,
380 "Invalid passive threshold\n");
Zhang Ruice44e192008-01-17 15:51:25 +0800381 tz->trips.passive.flags.valid = 0;
Zhang Rui0e4240d2009-01-16 12:53:42 -0500382 }
Zhang Ruice44e192008-01-17 15:51:25 +0800383 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));
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100390 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device");
Zhang Ruice44e192008-01-17 15:51:25 +0800391 }
392 }
393 if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
394 if (valid != tz->trips.passive.flags.valid)
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100395 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "state");
Zhang Ruice44e192008-01-17 15:51:25 +0800396 }
397
398 /* Active (optional) */
Len Brown4be44fc2005-08-05 00:44:28 -0400399 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400400 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
Zhang Ruice44e192008-01-17 15:51:25 +0800401 valid = tz->trips.active[i].flags.valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Len Brownf8707ec2007-08-12 00:12:54 -0400403 if (act == -1)
Zhang Ruice44e192008-01-17 15:51:25 +0800404 break; /* disable all active trip points */
Len Brownf8707ec2007-08-12 00:12:54 -0400405
Zhang Rui0e4240d2009-01-16 12:53:42 -0500406 if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
407 tz->trips.active[i].flags.valid)) {
Zhang Ruice44e192008-01-17 15:51:25 +0800408 status = acpi_evaluate_integer(tz->device->handle,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400409 name, NULL, &tmp);
Zhang Ruice44e192008-01-17 15:51:25 +0800410 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 =
Akinobu Mita7f49a5c2020-01-30 22:15:33 -0800418 celsius_to_deci_kelvin(act);
Zhang Ruice44e192008-01-17 15:51:25 +0800419 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 <
Akinobu Mita7f49a5c2020-01-30 22:15:33 -0800426 celsius_to_deci_kelvin(act) ?
Zhang Ruice44e192008-01-17 15:51:25 +0800427 tz->trips.active[i - 2].temperature :
Akinobu Mita7f49a5c2020-01-30 22:15:33 -0800428 celsius_to_deci_kelvin(act));
Len Brownf8707ec2007-08-12 00:12:54 -0400429 break;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400430 } else {
431 tz->trips.active[i].temperature = tmp;
Zhang Ruice44e192008-01-17 15:51:25 +0800432 tz->trips.active[i].flags.valid = 1;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400433 }
Len Brownf8707ec2007-08-12 00:12:54 -0400434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 name[2] = 'L';
Zhang Ruice44e192008-01-17 15:51:25 +0800437 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);
Zhang Rui0e4240d2009-01-16 12:53:42 -0500441 if (ACPI_FAILURE(status)) {
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100442 acpi_handle_info(tz->device->handle,
443 "Invalid active%d threshold\n", i);
Zhang Ruice44e192008-01-17 15:51:25 +0800444 tz->trips.active[i].flags.valid = 0;
Zhang Rui0e4240d2009-01-16 12:53:42 -0500445 }
Zhang Ruice44e192008-01-17 15:51:25 +0800446 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));
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100453 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device");
Zhang Ruice44e192008-01-17 15:51:25 +0800454 }
455 }
456 if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
457 if (valid != tz->trips.active[i].flags.valid)
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100458 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "state");
Zhang Ruice44e192008-01-17 15:51:25 +0800459
460 if (!tz->trips.active[i].flags.valid)
461 break;
462 }
463
Kelsey Skunberge88c7402019-07-21 20:35:30 -0600464 if (flag & ACPI_TRIPS_DEVICES) {
Lan Tianyu668e0202013-08-27 16:29:31 +0800465 memset(&devices, 0, sizeof(devices));
Zhang Ruice44e192008-01-17 15:51:25 +0800466 status = acpi_evaluate_reference(tz->device->handle, "_TZD",
467 NULL, &devices);
Lan Tianyu668e0202013-08-27 16:29:31 +0800468 if (ACPI_SUCCESS(status)
469 && memcmp(&tz->devices, &devices, sizeof(devices))) {
470 tz->devices = devices;
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100471 ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device");
Zhang Ruice44e192008-01-17 15:51:25 +0800472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
Patrick Mocheld550d982006-06-27 00:41:40 -0400475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Zhang Ruice44e192008-01-17 15:51:25 +0800478static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Thomas Renninger8b7ef6d2010-02-16 22:55:51 +0100480 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) {
Andy Shevchenko766a8a62013-09-13 18:15:46 +0300493 pr_warn(FW_BUG "No valid trip found\n");
Thomas Renninger8b7ef6d2010-02-16 22:55:51 +0100494 return -ENODEV;
495 }
496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Zhang Rui3f655ef2008-01-17 15:51:11 +0800499/* sys I/F for generic thermal sysfs support */
Zhang, Rui5e012762008-02-28 07:51:30 +0800500
Sascha Hauer17e83512015-07-24 08:12:54 +0200501static int thermal_get_temp(struct thermal_zone_device *thermal, int *temp)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800502{
503 struct acpi_thermal *tz = thermal->devdata;
Zhang, Rui76ecb4f2008-04-10 16:20:23 +0800504 int result;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800505
506 if (!tz)
507 return -EINVAL;
508
Zhang, Rui76ecb4f2008-04-10 16:20:23 +0800509 result = acpi_thermal_get_temperature(tz);
510 if (result)
511 return result;
512
Akinobu Mita7f49a5c2020-01-30 22:15:33 -0800513 *temp = deci_kelvin_to_millicelsius_with_offset(tz->temperature,
Aaron Lu7b83fd92014-03-25 10:40:09 +0800514 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000515 return 0;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800516}
517
Zhang Rui3f655ef2008-01-17 15:51:11 +0800518static int thermal_get_trip_type(struct thermal_zone_device *thermal,
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000519 int trip, enum thermal_trip_type *type)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800520{
521 struct acpi_thermal *tz = thermal->devdata;
522 int i;
523
524 if (!tz || trip < 0)
525 return -EINVAL;
526
527 if (tz->trips.critical.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000528 if (!trip) {
529 *type = THERMAL_TRIP_CRITICAL;
530 return 0;
531 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800532 trip--;
533 }
534
535 if (tz->trips.hot.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000536 if (!trip) {
537 *type = THERMAL_TRIP_HOT;
538 return 0;
539 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800540 trip--;
541 }
542
543 if (tz->trips.passive.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000544 if (!trip) {
545 *type = THERMAL_TRIP_PASSIVE;
546 return 0;
547 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800548 trip--;
549 }
550
551 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
552 tz->trips.active[i].flags.valid; i++) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000553 if (!trip) {
554 *type = THERMAL_TRIP_ACTIVE;
555 return 0;
556 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800557 trip--;
558 }
559
560 return -EINVAL;
561}
562
563static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
Sascha Hauer17e83512015-07-24 08:12:54 +0200564 int trip, int *temp)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800565{
566 struct acpi_thermal *tz = thermal->devdata;
567 int i;
568
569 if (!tz || trip < 0)
570 return -EINVAL;
571
572 if (tz->trips.critical.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000573 if (!trip) {
Akinobu Mita7f49a5c2020-01-30 22:15:33 -0800574 *temp = deci_kelvin_to_millicelsius_with_offset(
Jean Delvare13614e32009-04-06 16:01:46 +0200575 tz->trips.critical.temperature,
576 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000577 return 0;
578 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800579 trip--;
580 }
581
582 if (tz->trips.hot.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000583 if (!trip) {
Akinobu Mita7f49a5c2020-01-30 22:15:33 -0800584 *temp = deci_kelvin_to_millicelsius_with_offset(
Jean Delvare13614e32009-04-06 16:01:46 +0200585 tz->trips.hot.temperature,
586 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000587 return 0;
588 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800589 trip--;
590 }
591
592 if (tz->trips.passive.flags.valid) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000593 if (!trip) {
Akinobu Mita7f49a5c2020-01-30 22:15:33 -0800594 *temp = deci_kelvin_to_millicelsius_with_offset(
Jean Delvare13614e32009-04-06 16:01:46 +0200595 tz->trips.passive.temperature,
596 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000597 return 0;
598 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800599 trip--;
600 }
601
602 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
603 tz->trips.active[i].flags.valid; i++) {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000604 if (!trip) {
Akinobu Mita7f49a5c2020-01-30 22:15:33 -0800605 *temp = deci_kelvin_to_millicelsius_with_offset(
Jean Delvare13614e32009-04-06 16:01:46 +0200606 tz->trips.active[i].temperature,
607 tz->kelvin_offset);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000608 return 0;
609 }
Zhang Rui3f655ef2008-01-17 15:51:11 +0800610 trip--;
611 }
612
613 return -EINVAL;
614}
615
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800616static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
Sascha Hauer17e83512015-07-24 08:12:54 +0200617 int *temperature)
618{
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800619 struct acpi_thermal *tz = thermal->devdata;
620
621 if (tz->trips.critical.flags.valid) {
Akinobu Mita7f49a5c2020-01-30 22:15:33 -0800622 *temperature = deci_kelvin_to_millicelsius_with_offset(
Jean Delvare13614e32009-04-06 16:01:46 +0200623 tz->trips.critical.temperature,
624 tz->kelvin_offset);
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800625 return 0;
626 } else
627 return -EINVAL;
628}
629
Zhang Rui601f3d42012-06-27 09:54:33 +0800630static int thermal_get_trend(struct thermal_zone_device *thermal,
631 int trip, enum thermal_trend *trend)
632{
633 struct acpi_thermal *tz = thermal->devdata;
634 enum thermal_trip_type type;
635 int i;
636
637 if (thermal_get_trip_type(thermal, trip, &type))
638 return -EINVAL;
639
Zhang Rui4ae46be2012-06-27 10:05:39 +0800640 if (type == THERMAL_TRIP_ACTIVE) {
Sascha Hauer17e83512015-07-24 08:12:54 +0200641 int trip_temp;
Akinobu Mita7f49a5c2020-01-30 22:15:33 -0800642 int temp = deci_kelvin_to_millicelsius_with_offset(
Aaron Lu7b83fd92014-03-25 10:40:09 +0800643 tz->temperature, tz->kelvin_offset);
Zhang Rui94a40932013-04-26 09:19:53 +0000644 if (thermal_get_trip_temp(thermal, trip, &trip_temp))
645 return -EINVAL;
646
647 if (temp > trip_temp) {
648 *trend = THERMAL_TREND_RAISING;
649 return 0;
650 } else {
651 /* Fall back on default trend */
652 return -EINVAL;
653 }
Zhang Rui4ae46be2012-06-27 10:05:39 +0800654 }
Zhang Rui601f3d42012-06-27 09:54:33 +0800655
656 /*
657 * tz->temperature has already been updated by generic thermal layer,
658 * before this callback being invoked
659 */
660 i = (tz->trips.passive.tc1 * (tz->temperature - tz->last_temperature))
661 + (tz->trips.passive.tc2
662 * (tz->temperature - tz->trips.passive.temperature));
663
664 if (i > 0)
665 *trend = THERMAL_TREND_RAISING;
666 else if (i < 0)
667 *trend = THERMAL_TREND_DROPPING;
668 else
669 *trend = THERMAL_TREND_STABLE;
670 return 0;
671}
672
Daniel Lezcanoa73cb202020-12-10 13:15:12 +0100673static void acpi_thermal_zone_device_hot(struct thermal_zone_device *thermal)
Matthew Garrettb1569e92008-12-03 17:55:32 +0000674{
Matthew Garrettb1569e92008-12-03 17:55:32 +0000675 struct acpi_thermal *tz = thermal->devdata;
676
Daniel Lezcanoa73cb202020-12-10 13:15:12 +0100677 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
678 dev_name(&tz->device->dev),
679 ACPI_THERMAL_NOTIFY_HOT, 1);
680}
681
682static void acpi_thermal_zone_device_critical(struct thermal_zone_device *thermal)
683{
684 struct acpi_thermal *tz = thermal->devdata;
Matthew Garrettb1569e92008-12-03 17:55:32 +0000685
Matthew Garrettb1569e92008-12-03 17:55:32 +0000686 acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
Daniel Lezcanoa73cb202020-12-10 13:15:12 +0100687 dev_name(&tz->device->dev),
688 ACPI_THERMAL_NOTIFY_CRITICAL, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000689
Daniel Lezcanoa73cb202020-12-10 13:15:12 +0100690 thermal_zone_device_critical(thermal);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000691}
692
Zhang Rui3f655ef2008-01-17 15:51:11 +0800693static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
694 struct thermal_cooling_device *cdev,
Zhang Rui9d998422012-06-26 16:35:57 +0800695 bool bind)
Zhang Rui3f655ef2008-01-17 15:51:11 +0800696{
697 struct acpi_device *device = cdev->devdata;
698 struct acpi_thermal *tz = thermal->devdata;
Zhang Rui653a00c2008-01-17 15:51:18 +0800699 struct acpi_device *dev;
700 acpi_status status;
701 acpi_handle handle;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800702 int i;
703 int j;
704 int trip = -1;
705 int result = 0;
706
707 if (tz->trips.critical.flags.valid)
708 trip++;
709
710 if (tz->trips.hot.flags.valid)
711 trip++;
712
713 if (tz->trips.passive.flags.valid) {
714 trip++;
715 for (i = 0; i < tz->trips.passive.devices.count;
716 i++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800717 handle = tz->trips.passive.devices.handles[i];
718 status = acpi_bus_get_device(handle, &dev);
Zhang Rui9d998422012-06-26 16:35:57 +0800719 if (ACPI_FAILURE(status) || dev != device)
720 continue;
721 if (bind)
722 result =
723 thermal_zone_bind_cooling_device
724 (thermal, trip, cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000725 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT,
726 THERMAL_WEIGHT_DEFAULT);
Zhang Rui9d998422012-06-26 16:35:57 +0800727 else
728 result =
729 thermal_zone_unbind_cooling_device
730 (thermal, trip, cdev);
731 if (result)
732 goto failed;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800733 }
734 }
735
736 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
737 if (!tz->trips.active[i].flags.valid)
738 break;
739 trip++;
740 for (j = 0;
741 j < tz->trips.active[i].devices.count;
742 j++) {
Zhang Rui653a00c2008-01-17 15:51:18 +0800743 handle = tz->trips.active[i].devices.handles[j];
744 status = acpi_bus_get_device(handle, &dev);
Zhang Rui9d998422012-06-26 16:35:57 +0800745 if (ACPI_FAILURE(status) || dev != device)
746 continue;
747 if (bind)
748 result = thermal_zone_bind_cooling_device
749 (thermal, trip, cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000750 THERMAL_NO_LIMIT, THERMAL_NO_LIMIT,
751 THERMAL_WEIGHT_DEFAULT);
Zhang Rui9d998422012-06-26 16:35:57 +0800752 else
753 result = thermal_zone_unbind_cooling_device
754 (thermal, trip, cdev);
755 if (result)
756 goto failed;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800757 }
758 }
759
Zhang Rui3f655ef2008-01-17 15:51:11 +0800760failed:
761 return result;
762}
763
764static int
765acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
766 struct thermal_cooling_device *cdev)
767{
Zhang Rui9d998422012-06-26 16:35:57 +0800768 return acpi_thermal_cooling_device_cb(thermal, cdev, true);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800769}
770
771static int
772acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
773 struct thermal_cooling_device *cdev)
774{
Zhang Rui9d998422012-06-26 16:35:57 +0800775 return acpi_thermal_cooling_device_cb(thermal, cdev, false);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800776}
777
Emil Goodeec9c9c22014-01-07 17:24:55 +0100778static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
Zhang Rui3f655ef2008-01-17 15:51:11 +0800779 .bind = acpi_thermal_bind_cooling_device,
780 .unbind = acpi_thermal_unbind_cooling_device,
781 .get_temp = thermal_get_temp,
Zhang Rui3f655ef2008-01-17 15:51:11 +0800782 .get_trip_type = thermal_get_trip_type,
783 .get_trip_temp = thermal_get_trip_temp,
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800784 .get_crit_temp = thermal_get_crit_temp,
Zhang Rui601f3d42012-06-27 09:54:33 +0800785 .get_trend = thermal_get_trend,
Daniel Lezcanoa73cb202020-12-10 13:15:12 +0100786 .hot = acpi_thermal_zone_device_hot,
787 .critical = acpi_thermal_zone_device_critical,
Zhang Rui3f655ef2008-01-17 15:51:11 +0800788};
789
790static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
791{
792 int trips = 0;
793 int result;
Zhang Rui20733932008-01-17 15:51:21 +0800794 acpi_status status;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800795 int i;
796
797 if (tz->trips.critical.flags.valid)
798 trips++;
799
800 if (tz->trips.hot.flags.valid)
801 trips++;
802
803 if (tz->trips.passive.flags.valid)
804 trips++;
805
806 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
807 tz->trips.active[i].flags.valid; i++, trips++);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000808
809 if (tz->trips.passive.flags.valid)
810 tz->thermal_zone =
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800811 thermal_zone_device_register("acpitz", trips, 0, tz,
Durgadoss R50125a92012-09-18 11:04:56 +0530812 &acpi_thermal_zone_ops, NULL,
Matthew Garrettb1569e92008-12-03 17:55:32 +0000813 tz->trips.passive.tsp*100,
814 tz->polling_frequency*100);
815 else
816 tz->thermal_zone =
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800817 thermal_zone_device_register("acpitz", trips, 0, tz,
Durgadoss R50125a92012-09-18 11:04:56 +0530818 &acpi_thermal_zone_ops, NULL,
819 0, tz->polling_frequency*100);
Krzysztof Heltbb070e42008-04-08 17:41:52 -0700820 if (IS_ERR(tz->thermal_zone))
Zhang Rui3f655ef2008-01-17 15:51:11 +0800821 return -ENODEV;
822
823 result = sysfs_create_link(&tz->device->dev.kobj,
824 &tz->thermal_zone->device.kobj, "thermal_zone");
825 if (result)
Andrzej Pietrasiewicz172066c2020-06-29 14:29:15 +0200826 goto unregister_tzd;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800827
828 result = sysfs_create_link(&tz->thermal_zone->device.kobj,
829 &tz->device->dev.kobj, "device");
830 if (result)
Andrzej Pietrasiewicz172066c2020-06-29 14:29:15 +0200831 goto remove_tz_link;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800832
Lan Tianyue6f8a4d2014-05-20 20:59:22 +0800833 status = acpi_bus_attach_private_data(tz->device->handle,
834 tz->thermal_zone);
Andrzej Pietrasiewicz172066c2020-06-29 14:29:15 +0200835 if (ACPI_FAILURE(status)) {
836 result = -ENODEV;
837 goto remove_dev_link;
838 }
Zhang Rui20733932008-01-17 15:51:21 +0800839
Andrzej Pietrasiewicz7f4957b2020-06-29 14:29:21 +0200840 result = thermal_zone_device_enable(tz->thermal_zone);
841 if (result)
842 goto acpi_bus_detach;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800843
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200844 dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
845 tz->thermal_zone->id);
Andrzej Pietrasiewicz172066c2020-06-29 14:29:15 +0200846
Zhang Rui3f655ef2008-01-17 15:51:11 +0800847 return 0;
Andrzej Pietrasiewicz172066c2020-06-29 14:29:15 +0200848
Andrzej Pietrasiewicz7f4957b2020-06-29 14:29:21 +0200849acpi_bus_detach:
850 acpi_bus_detach_private_data(tz->device->handle);
Andrzej Pietrasiewicz172066c2020-06-29 14:29:15 +0200851remove_dev_link:
852 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
853remove_tz_link:
854 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
855unregister_tzd:
856 thermal_zone_device_unregister(tz->thermal_zone);
857
858 return result;
Zhang Rui3f655ef2008-01-17 15:51:11 +0800859}
860
861static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
862{
863 sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
864 sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
865 thermal_zone_device_unregister(tz->thermal_zone);
866 tz->thermal_zone = NULL;
Lan Tianyue6f8a4d2014-05-20 20:59:22 +0800867 acpi_bus_detach_private_data(tz->device->handle);
Zhang Rui3f655ef2008-01-17 15:51:11 +0800868}
869
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/* --------------------------------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 Driver Interface
873 -------------------------------------------------------------------------- */
874
Rafael J. Wysocki81b704d32021-01-14 19:34:22 +0100875static void acpi_queue_thermal_check(struct acpi_thermal *tz)
876{
877 if (!work_pending(&tz->thermal_check_work))
878 queue_work(acpi_thermal_pm_queue, &tz->thermal_check_work);
879}
880
Bjorn Helgaas342d5502009-04-07 15:37:06 +0000881static void acpi_thermal_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Bjorn Helgaas342d5502009-04-07 15:37:06 +0000883 struct acpi_thermal *tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400887 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 switch (event) {
890 case ACPI_THERMAL_NOTIFY_TEMPERATURE:
Rafael J. Wysocki81b704d32021-01-14 19:34:22 +0100891 acpi_queue_thermal_check(tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 break;
893 case ACPI_THERMAL_NOTIFY_THRESHOLDS:
Zhang Ruice44e192008-01-17 15:51:25 +0800894 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
Rafael J. Wysocki81b704d32021-01-14 19:34:22 +0100895 acpi_queue_thermal_check(tz);
Zhang Rui962ce8c2007-08-23 01:24:31 +0800896 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +0100897 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 break;
899 case ACPI_THERMAL_NOTIFY_DEVICES:
Zhang Ruice44e192008-01-17 15:51:25 +0800900 acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
Rafael J. Wysocki81b704d32021-01-14 19:34:22 +0100901 acpi_queue_thermal_check(tz);
Zhang Rui962ce8c2007-08-23 01:24:31 +0800902 acpi_bus_generate_netlink_event(device->pnp.device_class,
Kay Sievers07944692008-10-30 01:18:59 +0100903 dev_name(&device->dev), event, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 break;
905 default:
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +0100906 acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
907 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 break;
909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Zhang Rui261cba22012-11-27 20:42:11 +0100912/*
913 * On some platforms, the AML code has dependency about
914 * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx.
915 * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after
916 * /_CRT/_HOT/_PSV/_ACx, or else system will be power off.
917 * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0
918 * if _TMP has never been evaluated.
919 *
920 * As this dependency is totally transparent to OS, evaluate
921 * all of them once, in the order of _CRT/_HOT/_PSV/_ACx,
922 * _TMP, before they are actually used.
923 */
924static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz)
925{
926 acpi_handle handle = tz->device->handle;
927 unsigned long long value;
928 int i;
929
930 acpi_evaluate_integer(handle, "_CRT", NULL, &value);
931 acpi_evaluate_integer(handle, "_HOT", NULL, &value);
932 acpi_evaluate_integer(handle, "_PSV", NULL, &value);
933 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
934 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
935 acpi_status status;
936
937 status = acpi_evaluate_integer(handle, name, NULL, &value);
938 if (status == AE_NOT_FOUND)
939 break;
940 }
941 acpi_evaluate_integer(handle, "_TMP", NULL, &value);
942}
943
Len Brown4be44fc2005-08-05 00:44:28 -0400944static int acpi_thermal_get_info(struct acpi_thermal *tz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Len Brown4be44fc2005-08-05 00:44:28 -0400946 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -0400950 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Zhang Rui261cba22012-11-27 20:42:11 +0100952 acpi_thermal_aml_dependency_fix(tz);
953
Matthew Garrett9bcb8112012-02-01 10:26:54 -0500954 /* Get trip points [_CRT, _PSV, etc.] (required) */
955 result = acpi_thermal_get_trip_points(tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400957 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Matthew Garrett9bcb8112012-02-01 10:26:54 -0500959 /* Get temperature [_TMP] (required) */
960 result = acpi_thermal_get_temperature(tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400962 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 /* Set the cooling mode [_SCP] to active cooling (default) */
965 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
Len Brown4be44fc2005-08-05 00:44:28 -0400966 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 tz->flags.cooling_mode = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 /* Get default polling frequency [_TZP] (optional) */
970 if (tzp)
971 tz->polling_frequency = tzp;
972 else
973 acpi_thermal_get_polling_frequency(tz);
974
Patrick Mocheld550d982006-06-27 00:41:40 -0400975 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
Jean Delvare13614e32009-04-06 16:01:46 +0200978/*
979 * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
980 * handles temperature values with a single decimal place. As a consequence,
981 * some implementations use an offset of 273.1 and others use an offset of
982 * 273.2. Try to find out which one is being used, to present the most
983 * accurate and visually appealing number.
984 *
985 * The heuristic below should work for all ACPI thermal zones which have a
986 * critical trip point with a value being a multiple of 0.5 degree Celsius.
987 */
988static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
989{
990 if (tz->trips.critical.flags.valid &&
991 (tz->trips.critical.temperature % 5) == 1)
Akinobu Mita7f49a5c2020-01-30 22:15:33 -0800992 tz->kelvin_offset = 273100;
Jean Delvare13614e32009-04-06 16:01:46 +0200993 else
Akinobu Mita7f49a5c2020-01-30 22:15:33 -0800994 tz->kelvin_offset = 273200;
Jean Delvare13614e32009-04-06 16:01:46 +0200995}
996
Aaron Lua59ffb22014-03-04 14:24:46 +0800997static void acpi_thermal_check_fn(struct work_struct *work)
998{
999 struct acpi_thermal *tz = container_of(work, struct acpi_thermal,
1000 thermal_check_work);
Rafael J. Wysocki81b704d32021-01-14 19:34:22 +01001001
1002 /*
1003 * In general, it is not sufficient to check the pending bit, because
1004 * subsequent instances of this function may be queued after one of them
1005 * has started running (e.g. if _TMP sleeps). Avoid bailing out if just
1006 * one of them is running, though, because it may have done the actual
1007 * check some time ago, so allow at least one of them to block on the
1008 * mutex while another one is running the update.
1009 */
1010 if (!refcount_dec_not_one(&tz->thermal_check_count))
1011 return;
1012
1013 mutex_lock(&tz->thermal_check_lock);
1014
1015 thermal_zone_device_update(tz->thermal_zone, THERMAL_EVENT_UNSPECIFIED);
1016
1017 refcount_inc(&tz->thermal_check_count);
1018
1019 mutex_unlock(&tz->thermal_check_lock);
Aaron Lua59ffb22014-03-04 14:24:46 +08001020}
1021
Len Brown4be44fc2005-08-05 00:44:28 -04001022static int acpi_thermal_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Len Brown4be44fc2005-08-05 00:44:28 -04001024 int result = 0;
Len Brown4be44fc2005-08-05 00:44:28 -04001025 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001029 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Burman Yan36bcbec2006-12-19 12:56:11 -08001031 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (!tz)
Patrick Mocheld550d982006-06-27 00:41:40 -04001033 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Patrick Mochel8348e1b2006-05-19 16:54:40 -04001035 tz->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 strcpy(tz->name, device->pnp.bus_id);
1037 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1038 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001039 device->driver_data = tz;
Zhang Rui3f655ef2008-01-17 15:51:11 +08001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 result = acpi_thermal_get_info(tz);
1042 if (result)
Zhang Rui3f655ef2008-01-17 15:51:11 +08001043 goto free_memory;
1044
Jean Delvare13614e32009-04-06 16:01:46 +02001045 acpi_thermal_guess_offset(tz);
1046
Zhang Rui3f655ef2008-01-17 15:51:11 +08001047 result = acpi_thermal_register_thermal_zone(tz);
1048 if (result)
1049 goto free_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Rafael J. Wysocki81b704d32021-01-14 19:34:22 +01001051 refcount_set(&tz->thermal_check_count, 3);
1052 mutex_init(&tz->thermal_check_lock);
Aaron Lua59ffb22014-03-04 14:24:46 +08001053 INIT_WORK(&tz->thermal_check_work, acpi_thermal_check_fn);
1054
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +01001055 pr_info("%s [%s] (%ld C)\n", acpi_device_name(device),
Akinobu Mita7f49a5c2020-01-30 22:15:33 -08001056 acpi_device_bid(device), deci_kelvin_to_celsius(tz->temperature));
Zhang Rui3f655ef2008-01-17 15:51:11 +08001057 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Zhang Rui3f655ef2008-01-17 15:51:11 +08001059free_memory:
1060 kfree(tz);
1061end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001062 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001065static int acpi_thermal_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Len Brown4be44fc2005-08-05 00:44:28 -04001067 struct acpi_thermal *tz = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04001070 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Aaron Lua59ffb22014-03-04 14:24:46 +08001072 flush_workqueue(acpi_thermal_pm_queue);
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001073 tz = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Zhang Rui3f655ef2008-01-17 15:51:11 +08001075 acpi_thermal_unregister_thermal_zone(tz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 kfree(tz);
Patrick Mocheld550d982006-06-27 00:41:40 -04001077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Rafael J. Wysocki90692402012-08-09 23:00:02 +02001080#ifdef CONFIG_PM_SLEEP
Aaron Lua59ffb22014-03-04 14:24:46 +08001081static int acpi_thermal_suspend(struct device *dev)
1082{
1083 /* Make sure the previously queued thermal check work has been done */
1084 flush_workqueue(acpi_thermal_pm_queue);
1085 return 0;
1086}
1087
Rafael J. Wysocki167cffb2012-06-27 23:26:18 +02001088static int acpi_thermal_resume(struct device *dev)
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001089{
Rafael J. Wysocki167cffb2012-06-27 23:26:18 +02001090 struct acpi_thermal *tz;
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001091 int i, j, power_state, result;
1092
Rafael J. Wysocki167cffb2012-06-27 23:26:18 +02001093 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001094 return -EINVAL;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001095
Rafael J. Wysocki167cffb2012-06-27 23:26:18 +02001096 tz = acpi_driver_data(to_acpi_device(dev));
1097 if (!tz)
1098 return -EINVAL;
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001099
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001100 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001101 if (!(&tz->trips.active[i]))
1102 break;
1103 if (!tz->trips.active[i].flags.valid)
1104 break;
1105 tz->trips.active[i].flags.enabled = 1;
1106 for (j = 0; j < tz->trips.active[i].devices.count; j++) {
Rafael J. Wysocki488a76c2010-11-25 00:11:24 +01001107 result = acpi_bus_update_power(
1108 tz->trips.active[i].devices.handles[j],
1109 &power_state);
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001110 if (result || (power_state != ACPI_STATE_D0)) {
1111 tz->trips.active[i].flags.enabled = 0;
1112 break;
1113 }
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001114 }
Konstantin Karasyovb1028c52007-02-16 02:23:07 -05001115 tz->state.active |= tz->trips.active[i].flags.enabled;
Konstantin Karasyovbed936f2006-07-10 04:44:26 -07001116 }
1117
Rafael J. Wysocki81b704d32021-01-14 19:34:22 +01001118 acpi_queue_thermal_check(tz);
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001119
1120 return AE_OK;
1121}
Rafael J. Wysocki90692402012-08-09 23:00:02 +02001122#endif
Konstantin Karasyov74ce14682006-05-08 08:32:00 -04001123
Jeff Garzik18552562007-10-03 15:15:40 -04001124static int thermal_act(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001125
1126 if (act == 0) {
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +01001127 pr_notice("%s detected: disabling all active thermal trip points\n",
1128 d->ident);
Len Brown0b5bfa12007-08-12 00:13:02 -04001129 act = -1;
1130 }
1131 return 0;
1132}
Jeff Garzik18552562007-10-03 15:15:40 -04001133static int thermal_nocrt(const struct dmi_system_id *d) {
Len Brown8c99fdc2007-08-20 18:46:50 -04001134
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +01001135 pr_notice("%s detected: disabling all critical thermal trip point actions.\n",
1136 d->ident);
Len Brown8c99fdc2007-08-20 18:46:50 -04001137 nocrt = 1;
1138 return 0;
1139}
Jeff Garzik18552562007-10-03 15:15:40 -04001140static int thermal_tzp(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001141
1142 if (tzp == 0) {
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +01001143 pr_notice("%s detected: enabling thermal zone polling\n",
1144 d->ident);
Len Brown0b5bfa12007-08-12 00:13:02 -04001145 tzp = 300; /* 300 dS = 30 Seconds */
1146 }
1147 return 0;
1148}
Jeff Garzik18552562007-10-03 15:15:40 -04001149static int thermal_psv(const struct dmi_system_id *d) {
Len Brown0b5bfa12007-08-12 00:13:02 -04001150
1151 if (psv == 0) {
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +01001152 pr_notice("%s detected: disabling all passive thermal trip points\n",
1153 d->ident);
Len Brown0b5bfa12007-08-12 00:13:02 -04001154 psv = -1;
1155 }
1156 return 0;
1157}
1158
Christoph Hellwig6faadbb2017-09-14 11:59:30 +02001159static const struct dmi_system_id thermal_dmi_table[] __initconst = {
Len Brown0b5bfa12007-08-12 00:13:02 -04001160 /*
1161 * Award BIOS on this AOpen makes thermal control almost worthless.
1162 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1163 */
1164 {
1165 .callback = thermal_act,
1166 .ident = "AOpen i915GMm-HFS",
1167 .matches = {
1168 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1169 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1170 },
1171 },
1172 {
1173 .callback = thermal_psv,
1174 .ident = "AOpen i915GMm-HFS",
1175 .matches = {
1176 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1177 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1178 },
1179 },
1180 {
1181 .callback = thermal_tzp,
1182 .ident = "AOpen i915GMm-HFS",
1183 .matches = {
1184 DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1185 DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1186 },
1187 },
Len Brown8c99fdc2007-08-20 18:46:50 -04001188 {
1189 .callback = thermal_nocrt,
1190 .ident = "Gigabyte GA-7ZX",
1191 .matches = {
1192 DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1193 DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1194 },
1195 },
Len Brown0b5bfa12007-08-12 00:13:02 -04001196 {}
1197};
Len Brown0b5bfa12007-08-12 00:13:02 -04001198
Len Brown4be44fc2005-08-05 00:44:28 -04001199static int __init acpi_thermal_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Len Brown4be44fc2005-08-05 00:44:28 -04001201 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Len Brown0b5bfa12007-08-12 00:13:02 -04001203 dmi_check_system(thermal_dmi_table);
1204
Len Brown72b33ef2007-08-12 00:12:17 -04001205 if (off) {
Rafael J. Wysockif86b15a2021-02-03 19:49:21 +01001206 pr_notice("thermal control disabled\n");
Len Brown72b33ef2007-08-12 00:12:17 -04001207 return -ENODEV;
1208 }
Zhang Rui43d9f872010-07-15 10:46:44 +08001209
Bhaktipriya Shridhare053dc92016-06-07 08:45:40 +05301210 acpi_thermal_pm_queue = alloc_workqueue("acpi_thermal_pm",
1211 WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
Aaron Lua59ffb22014-03-04 14:24:46 +08001212 if (!acpi_thermal_pm_queue)
Patrick Mocheld550d982006-06-27 00:41:40 -04001213 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Aaron Lua59ffb22014-03-04 14:24:46 +08001215 result = acpi_bus_register_driver(&acpi_thermal_driver);
1216 if (result < 0) {
1217 destroy_workqueue(acpi_thermal_pm_queue);
1218 return -ENODEV;
1219 }
1220
Patrick Mocheld550d982006-06-27 00:41:40 -04001221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
Len Brown4be44fc2005-08-05 00:44:28 -04001224static void __exit acpi_thermal_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 acpi_bus_unregister_driver(&acpi_thermal_driver);
Aaron Lu2807bd12014-05-26 14:34:07 +02001227 destroy_workqueue(acpi_thermal_pm_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Patrick Mocheld550d982006-06-27 00:41:40 -04001229 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232module_init(acpi_thermal_init);
1233module_exit(acpi_thermal_exit);