xref: /openbmc/linux/drivers/acpi/thermal.c (revision a2cab953)
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 int 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 == 1) {
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 						(tz->trips.active[i-2].temperature <
423 						celsius_to_deci_kelvin(act) ?
424 						tz->trips.active[i-2].temperature :
425 						celsius_to_deci_kelvin(act));
426 
427 				break;
428 			} else {
429 				tz->trips.active[i].temperature = tmp;
430 				tz->trips.active[i].flags.valid = 1;
431 			}
432 		}
433 
434 		name[2] = 'L';
435 		if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid) {
436 			memset(&devices, 0, sizeof(struct acpi_handle_list));
437 			status = acpi_evaluate_reference(tz->device->handle,
438 							 name, NULL, &devices);
439 			if (ACPI_FAILURE(status)) {
440 				acpi_handle_info(tz->device->handle,
441 						 "Invalid active%d threshold\n", i);
442 				tz->trips.active[i].flags.valid = 0;
443 			} else {
444 				tz->trips.active[i].flags.valid = 1;
445 			}
446 
447 			if (memcmp(&tz->trips.active[i].devices, &devices,
448 				   sizeof(struct acpi_handle_list))) {
449 				memcpy(&tz->trips.active[i].devices, &devices,
450 				       sizeof(struct acpi_handle_list));
451 				ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device");
452 			}
453 		}
454 		if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
455 			if (valid != tz->trips.active[i].flags.valid)
456 				ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "state");
457 
458 		if (!tz->trips.active[i].flags.valid)
459 			break;
460 	}
461 
462 	if (flag & ACPI_TRIPS_DEVICES) {
463 		memset(&devices, 0, sizeof(devices));
464 		status = acpi_evaluate_reference(tz->device->handle, "_TZD",
465 						 NULL, &devices);
466 		if (ACPI_SUCCESS(status) &&
467 		    memcmp(&tz->devices, &devices, sizeof(devices))) {
468 			tz->devices = devices;
469 			ACPI_THERMAL_TRIPS_EXCEPTION(flag, tz, "device");
470 		}
471 	}
472 
473 	return 0;
474 }
475 
476 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
477 {
478 	int i, valid, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
479 
480 	if (ret)
481 		return ret;
482 
483 	valid = tz->trips.critical.flags.valid |
484 		tz->trips.hot.flags.valid |
485 		tz->trips.passive.flags.valid;
486 
487 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
488 		valid |= tz->trips.active[i].flags.valid;
489 
490 	if (!valid) {
491 		pr_warn(FW_BUG "No valid trip found\n");
492 		return -ENODEV;
493 	}
494 	return 0;
495 }
496 
497 /* sys I/F for generic thermal sysfs support */
498 
499 static int thermal_get_temp(struct thermal_zone_device *thermal, int *temp)
500 {
501 	struct acpi_thermal *tz = thermal->devdata;
502 	int result;
503 
504 	if (!tz)
505 		return -EINVAL;
506 
507 	result = acpi_thermal_get_temperature(tz);
508 	if (result)
509 		return result;
510 
511 	*temp = deci_kelvin_to_millicelsius_with_offset(tz->temperature,
512 							tz->kelvin_offset);
513 	return 0;
514 }
515 
516 static int thermal_get_trip_type(struct thermal_zone_device *thermal,
517 				 int trip, enum thermal_trip_type *type)
518 {
519 	struct acpi_thermal *tz = thermal->devdata;
520 	int i;
521 
522 	if (!tz || trip < 0)
523 		return -EINVAL;
524 
525 	if (tz->trips.critical.flags.valid) {
526 		if (!trip) {
527 			*type = THERMAL_TRIP_CRITICAL;
528 			return 0;
529 		}
530 		trip--;
531 	}
532 
533 	if (tz->trips.hot.flags.valid) {
534 		if (!trip) {
535 			*type = THERMAL_TRIP_HOT;
536 			return 0;
537 		}
538 		trip--;
539 	}
540 
541 	if (tz->trips.passive.flags.valid) {
542 		if (!trip) {
543 			*type = THERMAL_TRIP_PASSIVE;
544 			return 0;
545 		}
546 		trip--;
547 	}
548 
549 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE && tz->trips.active[i].flags.valid; i++) {
550 		if (!trip) {
551 			*type = THERMAL_TRIP_ACTIVE;
552 			return 0;
553 		}
554 		trip--;
555 	}
556 
557 	return -EINVAL;
558 }
559 
560 static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
561 				 int trip, int *temp)
562 {
563 	struct acpi_thermal *tz = thermal->devdata;
564 	int i;
565 
566 	if (!tz || trip < 0)
567 		return -EINVAL;
568 
569 	if (tz->trips.critical.flags.valid) {
570 		if (!trip) {
571 			*temp = deci_kelvin_to_millicelsius_with_offset(
572 					tz->trips.critical.temperature,
573 					tz->kelvin_offset);
574 			return 0;
575 		}
576 		trip--;
577 	}
578 
579 	if (tz->trips.hot.flags.valid) {
580 		if (!trip) {
581 			*temp = deci_kelvin_to_millicelsius_with_offset(
582 					tz->trips.hot.temperature,
583 					tz->kelvin_offset);
584 			return 0;
585 		}
586 		trip--;
587 	}
588 
589 	if (tz->trips.passive.flags.valid) {
590 		if (!trip) {
591 			*temp = deci_kelvin_to_millicelsius_with_offset(
592 					tz->trips.passive.temperature,
593 					tz->kelvin_offset);
594 			return 0;
595 		}
596 		trip--;
597 	}
598 
599 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
600 		tz->trips.active[i].flags.valid; i++) {
601 		if (!trip) {
602 			*temp = deci_kelvin_to_millicelsius_with_offset(
603 					tz->trips.active[i].temperature,
604 					tz->kelvin_offset);
605 			return 0;
606 		}
607 		trip--;
608 	}
609 
610 	return -EINVAL;
611 }
612 
613 static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
614 				int *temperature)
615 {
616 	struct acpi_thermal *tz = thermal->devdata;
617 
618 	if (tz->trips.critical.flags.valid) {
619 		*temperature = deci_kelvin_to_millicelsius_with_offset(
620 					tz->trips.critical.temperature,
621 					tz->kelvin_offset);
622 		return 0;
623 	}
624 
625 	return -EINVAL;
626 }
627 
628 static int thermal_get_trend(struct thermal_zone_device *thermal,
629 			     int trip, enum thermal_trend *trend)
630 {
631 	struct acpi_thermal *tz = thermal->devdata;
632 	enum thermal_trip_type type;
633 	int i;
634 
635 	if (thermal_get_trip_type(thermal, trip, &type))
636 		return -EINVAL;
637 
638 	if (type == THERMAL_TRIP_ACTIVE) {
639 		int trip_temp;
640 		int temp = deci_kelvin_to_millicelsius_with_offset(
641 					tz->temperature, tz->kelvin_offset);
642 		if (thermal_get_trip_temp(thermal, trip, &trip_temp))
643 			return -EINVAL;
644 
645 		if (temp > trip_temp) {
646 			*trend = THERMAL_TREND_RAISING;
647 			return 0;
648 		} else {
649 			/* Fall back on default trend */
650 			return -EINVAL;
651 		}
652 	}
653 
654 	/*
655 	 * tz->temperature has already been updated by generic thermal layer,
656 	 * before this callback being invoked
657 	 */
658 	i = tz->trips.passive.tc1 * (tz->temperature - tz->last_temperature) +
659 	    tz->trips.passive.tc2 * (tz->temperature - tz->trips.passive.temperature);
660 
661 	if (i > 0)
662 		*trend = THERMAL_TREND_RAISING;
663 	else if (i < 0)
664 		*trend = THERMAL_TREND_DROPPING;
665 	else
666 		*trend = THERMAL_TREND_STABLE;
667 
668 	return 0;
669 }
670 
671 static void acpi_thermal_zone_device_hot(struct thermal_zone_device *thermal)
672 {
673 	struct acpi_thermal *tz = thermal->devdata;
674 
675 	acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
676 					dev_name(&tz->device->dev),
677 					ACPI_THERMAL_NOTIFY_HOT, 1);
678 }
679 
680 static void acpi_thermal_zone_device_critical(struct thermal_zone_device *thermal)
681 {
682 	struct acpi_thermal *tz = thermal->devdata;
683 
684 	acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
685 					dev_name(&tz->device->dev),
686 					ACPI_THERMAL_NOTIFY_CRITICAL, 1);
687 
688 	thermal_zone_device_critical(thermal);
689 }
690 
691 static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
692 					  struct thermal_cooling_device *cdev,
693 					  bool bind)
694 {
695 	struct acpi_device *device = cdev->devdata;
696 	struct acpi_thermal *tz = thermal->devdata;
697 	struct acpi_device *dev;
698 	acpi_handle handle;
699 	int i;
700 	int j;
701 	int trip = -1;
702 	int result = 0;
703 
704 	if (tz->trips.critical.flags.valid)
705 		trip++;
706 
707 	if (tz->trips.hot.flags.valid)
708 		trip++;
709 
710 	if (tz->trips.passive.flags.valid) {
711 		trip++;
712 		for (i = 0; i < tz->trips.passive.devices.count; i++) {
713 			handle = tz->trips.passive.devices.handles[i];
714 			dev = acpi_fetch_acpi_dev(handle);
715 			if (dev != device)
716 				continue;
717 
718 			if (bind)
719 				result = thermal_zone_bind_cooling_device(
720 						thermal, trip, cdev,
721 						THERMAL_NO_LIMIT,
722 						THERMAL_NO_LIMIT,
723 						THERMAL_WEIGHT_DEFAULT);
724 			else
725 				result =
726 					thermal_zone_unbind_cooling_device(
727 						thermal, trip, cdev);
728 
729 			if (result)
730 				goto failed;
731 		}
732 	}
733 
734 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
735 		if (!tz->trips.active[i].flags.valid)
736 			break;
737 
738 		trip++;
739 		for (j = 0; j < tz->trips.active[i].devices.count; j++) {
740 			handle = tz->trips.active[i].devices.handles[j];
741 			dev = acpi_fetch_acpi_dev(handle);
742 			if (dev != device)
743 				continue;
744 
745 			if (bind)
746 				result = thermal_zone_bind_cooling_device(
747 						thermal, trip, cdev,
748 						THERMAL_NO_LIMIT,
749 						THERMAL_NO_LIMIT,
750 						THERMAL_WEIGHT_DEFAULT);
751 			else
752 				result = thermal_zone_unbind_cooling_device(
753 						thermal, trip, cdev);
754 
755 			if (result)
756 				goto failed;
757 		}
758 	}
759 
760 failed:
761 	return result;
762 }
763 
764 static int
765 acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
766 				 struct thermal_cooling_device *cdev)
767 {
768 	return acpi_thermal_cooling_device_cb(thermal, cdev, true);
769 }
770 
771 static int
772 acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
773 				   struct thermal_cooling_device *cdev)
774 {
775 	return acpi_thermal_cooling_device_cb(thermal, cdev, false);
776 }
777 
778 static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
779 	.bind = acpi_thermal_bind_cooling_device,
780 	.unbind	= acpi_thermal_unbind_cooling_device,
781 	.get_temp = thermal_get_temp,
782 	.get_trip_type = thermal_get_trip_type,
783 	.get_trip_temp = thermal_get_trip_temp,
784 	.get_crit_temp = thermal_get_crit_temp,
785 	.get_trend = thermal_get_trend,
786 	.hot = acpi_thermal_zone_device_hot,
787 	.critical = acpi_thermal_zone_device_critical,
788 };
789 
790 static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
791 {
792 	int trips = 0;
793 	int result;
794 	acpi_status status;
795 	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 && tz->trips.active[i].flags.valid;
807 	     i++, trips++);
808 
809 	if (tz->trips.passive.flags.valid)
810 		tz->thermal_zone = thermal_zone_device_register("acpitz", trips, 0, tz,
811 								&acpi_thermal_zone_ops, NULL,
812 								tz->trips.passive.tsp * 100,
813 								tz->polling_frequency * 100);
814 	else
815 		tz->thermal_zone =
816 			thermal_zone_device_register("acpitz", trips, 0, tz,
817 						     &acpi_thermal_zone_ops, NULL,
818 						     0, tz->polling_frequency * 100);
819 
820 	if (IS_ERR(tz->thermal_zone))
821 		return -ENODEV;
822 
823 	result = sysfs_create_link(&tz->device->dev.kobj,
824 				   &tz->thermal_zone->device.kobj, "thermal_zone");
825 	if (result)
826 		goto unregister_tzd;
827 
828 	result = sysfs_create_link(&tz->thermal_zone->device.kobj,
829 				   &tz->device->dev.kobj, "device");
830 	if (result)
831 		goto remove_tz_link;
832 
833 	status =  acpi_bus_attach_private_data(tz->device->handle,
834 					       tz->thermal_zone);
835 	if (ACPI_FAILURE(status)) {
836 		result = -ENODEV;
837 		goto remove_dev_link;
838 	}
839 
840 	result = thermal_zone_device_enable(tz->thermal_zone);
841 	if (result)
842 		goto acpi_bus_detach;
843 
844 	dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
845 		 tz->thermal_zone->id);
846 
847 	return 0;
848 
849 acpi_bus_detach:
850 	acpi_bus_detach_private_data(tz->device->handle);
851 remove_dev_link:
852 	sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
853 remove_tz_link:
854 	sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
855 unregister_tzd:
856 	thermal_zone_device_unregister(tz->thermal_zone);
857 
858 	return result;
859 }
860 
861 static 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;
867 	acpi_bus_detach_private_data(tz->device->handle);
868 }
869 
870 
871 /* --------------------------------------------------------------------------
872                                  Driver Interface
873    -------------------------------------------------------------------------- */
874 
875 static 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 
881 static void acpi_thermal_notify(struct acpi_device *device, u32 event)
882 {
883 	struct acpi_thermal *tz = acpi_driver_data(device);
884 
885 	if (!tz)
886 		return;
887 
888 	switch (event) {
889 	case ACPI_THERMAL_NOTIFY_TEMPERATURE:
890 		acpi_queue_thermal_check(tz);
891 		break;
892 	case ACPI_THERMAL_NOTIFY_THRESHOLDS:
893 		acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
894 		acpi_queue_thermal_check(tz);
895 		acpi_bus_generate_netlink_event(device->pnp.device_class,
896 						dev_name(&device->dev), event, 0);
897 		break;
898 	case ACPI_THERMAL_NOTIFY_DEVICES:
899 		acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
900 		acpi_queue_thermal_check(tz);
901 		acpi_bus_generate_netlink_event(device->pnp.device_class,
902 						dev_name(&device->dev), event, 0);
903 		break;
904 	default:
905 		acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
906 				  event);
907 		break;
908 	}
909 }
910 
911 /*
912  * On some platforms, the AML code has dependency about
913  * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx.
914  * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after
915  *    /_CRT/_HOT/_PSV/_ACx, or else system will be power off.
916  * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0
917  *    if _TMP has never been evaluated.
918  *
919  * As this dependency is totally transparent to OS, evaluate
920  * all of them once, in the order of _CRT/_HOT/_PSV/_ACx,
921  * _TMP, before they are actually used.
922  */
923 static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz)
924 {
925 	acpi_handle handle = tz->device->handle;
926 	unsigned long long value;
927 	int i;
928 
929 	acpi_evaluate_integer(handle, "_CRT", NULL, &value);
930 	acpi_evaluate_integer(handle, "_HOT", NULL, &value);
931 	acpi_evaluate_integer(handle, "_PSV", NULL, &value);
932 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
933 		char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
934 		acpi_status status;
935 
936 		status = acpi_evaluate_integer(handle, name, NULL, &value);
937 		if (status == AE_NOT_FOUND)
938 			break;
939 	}
940 	acpi_evaluate_integer(handle, "_TMP", NULL, &value);
941 }
942 
943 static int acpi_thermal_get_info(struct acpi_thermal *tz)
944 {
945 	int result;
946 
947 	if (!tz)
948 		return -EINVAL;
949 
950 	acpi_thermal_aml_dependency_fix(tz);
951 
952 	/* Get trip points [_CRT, _PSV, etc.] (required) */
953 	result = acpi_thermal_get_trip_points(tz);
954 	if (result)
955 		return result;
956 
957 	/* Get temperature [_TMP] (required) */
958 	result = acpi_thermal_get_temperature(tz);
959 	if (result)
960 		return result;
961 
962 	/* Set the cooling mode [_SCP] to active cooling (default) */
963 	result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
964 	if (!result)
965 		tz->flags.cooling_mode = 1;
966 
967 	/* Get default polling frequency [_TZP] (optional) */
968 	if (tzp)
969 		tz->polling_frequency = tzp;
970 	else
971 		acpi_thermal_get_polling_frequency(tz);
972 
973 	return 0;
974 }
975 
976 /*
977  * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
978  * handles temperature values with a single decimal place. As a consequence,
979  * some implementations use an offset of 273.1 and others use an offset of
980  * 273.2. Try to find out which one is being used, to present the most
981  * accurate and visually appealing number.
982  *
983  * The heuristic below should work for all ACPI thermal zones which have a
984  * critical trip point with a value being a multiple of 0.5 degree Celsius.
985  */
986 static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
987 {
988 	if (tz->trips.critical.flags.valid &&
989 	    (tz->trips.critical.temperature % 5) == 1)
990 		tz->kelvin_offset = 273100;
991 	else
992 		tz->kelvin_offset = 273200;
993 }
994 
995 static void acpi_thermal_check_fn(struct work_struct *work)
996 {
997 	struct acpi_thermal *tz = container_of(work, struct acpi_thermal,
998 					       thermal_check_work);
999 
1000 	/*
1001 	 * In general, it is not sufficient to check the pending bit, because
1002 	 * subsequent instances of this function may be queued after one of them
1003 	 * has started running (e.g. if _TMP sleeps).  Avoid bailing out if just
1004 	 * one of them is running, though, because it may have done the actual
1005 	 * check some time ago, so allow at least one of them to block on the
1006 	 * mutex while another one is running the update.
1007 	 */
1008 	if (!refcount_dec_not_one(&tz->thermal_check_count))
1009 		return;
1010 
1011 	mutex_lock(&tz->thermal_check_lock);
1012 
1013 	thermal_zone_device_update(tz->thermal_zone, THERMAL_EVENT_UNSPECIFIED);
1014 
1015 	refcount_inc(&tz->thermal_check_count);
1016 
1017 	mutex_unlock(&tz->thermal_check_lock);
1018 }
1019 
1020 static int acpi_thermal_add(struct acpi_device *device)
1021 {
1022 	struct acpi_thermal *tz;
1023 	int result;
1024 
1025 	if (!device)
1026 		return -EINVAL;
1027 
1028 	tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1029 	if (!tz)
1030 		return -ENOMEM;
1031 
1032 	tz->device = device;
1033 	strcpy(tz->name, device->pnp.bus_id);
1034 	strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1035 	strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1036 	device->driver_data = tz;
1037 
1038 	result = acpi_thermal_get_info(tz);
1039 	if (result)
1040 		goto free_memory;
1041 
1042 	acpi_thermal_guess_offset(tz);
1043 
1044 	result = acpi_thermal_register_thermal_zone(tz);
1045 	if (result)
1046 		goto free_memory;
1047 
1048 	refcount_set(&tz->thermal_check_count, 3);
1049 	mutex_init(&tz->thermal_check_lock);
1050 	INIT_WORK(&tz->thermal_check_work, acpi_thermal_check_fn);
1051 
1052 	pr_info("%s [%s] (%ld C)\n", acpi_device_name(device),
1053 		acpi_device_bid(device), deci_kelvin_to_celsius(tz->temperature));
1054 	goto end;
1055 
1056 free_memory:
1057 	kfree(tz);
1058 end:
1059 	return result;
1060 }
1061 
1062 static int acpi_thermal_remove(struct acpi_device *device)
1063 {
1064 	struct acpi_thermal *tz;
1065 
1066 	if (!device || !acpi_driver_data(device))
1067 		return -EINVAL;
1068 
1069 	flush_workqueue(acpi_thermal_pm_queue);
1070 	tz = acpi_driver_data(device);
1071 
1072 	acpi_thermal_unregister_thermal_zone(tz);
1073 	kfree(tz);
1074 	return 0;
1075 }
1076 
1077 #ifdef CONFIG_PM_SLEEP
1078 static int acpi_thermal_suspend(struct device *dev)
1079 {
1080 	/* Make sure the previously queued thermal check work has been done */
1081 	flush_workqueue(acpi_thermal_pm_queue);
1082 	return 0;
1083 }
1084 
1085 static int acpi_thermal_resume(struct device *dev)
1086 {
1087 	struct acpi_thermal *tz;
1088 	int i, j, power_state, result;
1089 
1090 	if (!dev)
1091 		return -EINVAL;
1092 
1093 	tz = acpi_driver_data(to_acpi_device(dev));
1094 	if (!tz)
1095 		return -EINVAL;
1096 
1097 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1098 		if (!tz->trips.active[i].flags.valid)
1099 			break;
1100 
1101 		tz->trips.active[i].flags.enabled = 1;
1102 		for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1103 			result = acpi_bus_update_power(
1104 					tz->trips.active[i].devices.handles[j],
1105 					&power_state);
1106 			if (result || (power_state != ACPI_STATE_D0)) {
1107 				tz->trips.active[i].flags.enabled = 0;
1108 				break;
1109 			}
1110 		}
1111 		tz->state.active |= tz->trips.active[i].flags.enabled;
1112 	}
1113 
1114 	acpi_queue_thermal_check(tz);
1115 
1116 	return AE_OK;
1117 }
1118 #endif
1119 
1120 static int thermal_act(const struct dmi_system_id *d) {
1121 	if (act == 0) {
1122 		pr_notice("%s detected: disabling all active thermal trip points\n",
1123 			  d->ident);
1124 		act = -1;
1125 	}
1126 	return 0;
1127 }
1128 static int thermal_nocrt(const struct dmi_system_id *d) {
1129 	pr_notice("%s detected: disabling all critical thermal trip point actions.\n",
1130 		  d->ident);
1131 	nocrt = 1;
1132 	return 0;
1133 }
1134 static int thermal_tzp(const struct dmi_system_id *d) {
1135 	if (tzp == 0) {
1136 		pr_notice("%s detected: enabling thermal zone polling\n",
1137 			  d->ident);
1138 		tzp = 300;	/* 300 dS = 30 Seconds */
1139 	}
1140 	return 0;
1141 }
1142 static int thermal_psv(const struct dmi_system_id *d) {
1143 	if (psv == 0) {
1144 		pr_notice("%s detected: disabling all passive thermal trip points\n",
1145 			  d->ident);
1146 		psv = -1;
1147 	}
1148 	return 0;
1149 }
1150 
1151 static const struct dmi_system_id thermal_dmi_table[] __initconst = {
1152 	/*
1153 	 * Award BIOS on this AOpen makes thermal control almost worthless.
1154 	 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1155 	 */
1156 	{
1157 	 .callback = thermal_act,
1158 	 .ident = "AOpen i915GMm-HFS",
1159 	 .matches = {
1160 		DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1161 		DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1162 		},
1163 	},
1164 	{
1165 	 .callback = thermal_psv,
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_tzp,
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_nocrt,
1182 	 .ident = "Gigabyte GA-7ZX",
1183 	 .matches = {
1184 		DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1185 		DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1186 		},
1187 	},
1188 	{}
1189 };
1190 
1191 static int __init acpi_thermal_init(void)
1192 {
1193 	int result;
1194 
1195 	dmi_check_system(thermal_dmi_table);
1196 
1197 	if (off) {
1198 		pr_notice("thermal control disabled\n");
1199 		return -ENODEV;
1200 	}
1201 
1202 	acpi_thermal_pm_queue = alloc_workqueue("acpi_thermal_pm",
1203 						WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
1204 	if (!acpi_thermal_pm_queue)
1205 		return -ENODEV;
1206 
1207 	result = acpi_bus_register_driver(&acpi_thermal_driver);
1208 	if (result < 0) {
1209 		destroy_workqueue(acpi_thermal_pm_queue);
1210 		return -ENODEV;
1211 	}
1212 
1213 	return 0;
1214 }
1215 
1216 static void __exit acpi_thermal_exit(void)
1217 {
1218 	acpi_bus_unregister_driver(&acpi_thermal_driver);
1219 	destroy_workqueue(acpi_thermal_pm_queue);
1220 }
1221 
1222 module_init(acpi_thermal_init);
1223 module_exit(acpi_thermal_exit);
1224