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