xref: /openbmc/linux/drivers/acpi/thermal.c (revision b8bb76713ec50df2f11efee386e16f93d51e1076)
1 /*
2  *  acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  *
25  *  This driver fully implements the ACPI thermal policy as described in the
26  *  ACPI 2.0 Specification.
27  *
28  *  TBD: 1. Implement passive cooling hysteresis.
29  *       2. Enhance passive cooling (CPU) states/limit interface to support
30  *          concepts of 'multiple limiters', upper/lower limits, etc.
31  *
32  */
33 
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/dmi.h>
37 #include <linux/init.h>
38 #include <linux/types.h>
39 #include <linux/proc_fs.h>
40 #include <linux/timer.h>
41 #include <linux/jiffies.h>
42 #include <linux/kmod.h>
43 #include <linux/seq_file.h>
44 #include <linux/reboot.h>
45 #include <asm/uaccess.h>
46 #include <linux/thermal.h>
47 #include <acpi/acpi_bus.h>
48 #include <acpi/acpi_drivers.h>
49 
50 #define ACPI_THERMAL_CLASS		"thermal_zone"
51 #define ACPI_THERMAL_DEVICE_NAME	"Thermal Zone"
52 #define ACPI_THERMAL_FILE_STATE		"state"
53 #define ACPI_THERMAL_FILE_TEMPERATURE	"temperature"
54 #define ACPI_THERMAL_FILE_TRIP_POINTS	"trip_points"
55 #define ACPI_THERMAL_FILE_COOLING_MODE	"cooling_mode"
56 #define ACPI_THERMAL_FILE_POLLING_FREQ	"polling_frequency"
57 #define ACPI_THERMAL_NOTIFY_TEMPERATURE	0x80
58 #define ACPI_THERMAL_NOTIFY_THRESHOLDS	0x81
59 #define ACPI_THERMAL_NOTIFY_DEVICES	0x82
60 #define ACPI_THERMAL_NOTIFY_CRITICAL	0xF0
61 #define ACPI_THERMAL_NOTIFY_HOT		0xF1
62 #define ACPI_THERMAL_MODE_ACTIVE	0x00
63 
64 #define ACPI_THERMAL_MAX_ACTIVE	10
65 #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
66 
67 #define _COMPONENT		ACPI_THERMAL_COMPONENT
68 ACPI_MODULE_NAME("thermal");
69 
70 MODULE_AUTHOR("Paul Diefenbaugh");
71 MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
72 MODULE_LICENSE("GPL");
73 
74 static int act;
75 module_param(act, int, 0644);
76 MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
77 
78 static int crt;
79 module_param(crt, int, 0644);
80 MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
81 
82 static int tzp;
83 module_param(tzp, int, 0444);
84 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
85 
86 static int nocrt;
87 module_param(nocrt, int, 0);
88 MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
89 
90 static int off;
91 module_param(off, int, 0);
92 MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
93 
94 static int psv;
95 module_param(psv, int, 0644);
96 MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
97 
98 static int acpi_thermal_add(struct acpi_device *device);
99 static int acpi_thermal_remove(struct acpi_device *device, int type);
100 static int acpi_thermal_resume(struct acpi_device *device);
101 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
102 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
103 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
104 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
105 static ssize_t acpi_thermal_write_cooling_mode(struct file *,
106 					       const char __user *, size_t,
107 					       loff_t *);
108 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
109 static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
110 					  size_t, loff_t *);
111 
112 static const struct acpi_device_id  thermal_device_ids[] = {
113 	{ACPI_THERMAL_HID, 0},
114 	{"", 0},
115 };
116 MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
117 
118 static struct acpi_driver acpi_thermal_driver = {
119 	.name = "thermal",
120 	.class = ACPI_THERMAL_CLASS,
121 	.ids = thermal_device_ids,
122 	.ops = {
123 		.add = acpi_thermal_add,
124 		.remove = acpi_thermal_remove,
125 		.resume = acpi_thermal_resume,
126 		},
127 };
128 
129 struct acpi_thermal_state {
130 	u8 critical:1;
131 	u8 hot:1;
132 	u8 passive:1;
133 	u8 active:1;
134 	u8 reserved:4;
135 	int active_index;
136 };
137 
138 struct acpi_thermal_state_flags {
139 	u8 valid:1;
140 	u8 enabled:1;
141 	u8 reserved:6;
142 };
143 
144 struct acpi_thermal_critical {
145 	struct acpi_thermal_state_flags flags;
146 	unsigned long temperature;
147 };
148 
149 struct acpi_thermal_hot {
150 	struct acpi_thermal_state_flags flags;
151 	unsigned long temperature;
152 };
153 
154 struct acpi_thermal_passive {
155 	struct acpi_thermal_state_flags flags;
156 	unsigned long temperature;
157 	unsigned long tc1;
158 	unsigned long tc2;
159 	unsigned long tsp;
160 	struct acpi_handle_list devices;
161 };
162 
163 struct acpi_thermal_active {
164 	struct acpi_thermal_state_flags flags;
165 	unsigned long temperature;
166 	struct acpi_handle_list devices;
167 };
168 
169 struct acpi_thermal_trips {
170 	struct acpi_thermal_critical critical;
171 	struct acpi_thermal_hot hot;
172 	struct acpi_thermal_passive passive;
173 	struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
174 };
175 
176 struct acpi_thermal_flags {
177 	u8 cooling_mode:1;	/* _SCP */
178 	u8 devices:1;		/* _TZD */
179 	u8 reserved:6;
180 };
181 
182 struct acpi_thermal {
183 	struct acpi_device * device;
184 	acpi_bus_id name;
185 	unsigned long temperature;
186 	unsigned long last_temperature;
187 	unsigned long polling_frequency;
188 	volatile u8 zombie;
189 	struct acpi_thermal_flags flags;
190 	struct acpi_thermal_state state;
191 	struct acpi_thermal_trips trips;
192 	struct acpi_handle_list devices;
193 	struct timer_list timer;
194 	struct thermal_zone_device *thermal_zone;
195 	int tz_enabled;
196 	struct mutex lock;
197 };
198 
199 static const struct file_operations acpi_thermal_state_fops = {
200 	.owner = THIS_MODULE,
201 	.open = acpi_thermal_state_open_fs,
202 	.read = seq_read,
203 	.llseek = seq_lseek,
204 	.release = single_release,
205 };
206 
207 static const struct file_operations acpi_thermal_temp_fops = {
208 	.owner = THIS_MODULE,
209 	.open = acpi_thermal_temp_open_fs,
210 	.read = seq_read,
211 	.llseek = seq_lseek,
212 	.release = single_release,
213 };
214 
215 static const struct file_operations acpi_thermal_trip_fops = {
216 	.owner = THIS_MODULE,
217 	.open = acpi_thermal_trip_open_fs,
218 	.read = seq_read,
219 	.llseek = seq_lseek,
220 	.release = single_release,
221 };
222 
223 static const struct file_operations acpi_thermal_cooling_fops = {
224 	.owner = THIS_MODULE,
225 	.open = acpi_thermal_cooling_open_fs,
226 	.read = seq_read,
227 	.write = acpi_thermal_write_cooling_mode,
228 	.llseek = seq_lseek,
229 	.release = single_release,
230 };
231 
232 static const struct file_operations acpi_thermal_polling_fops = {
233 	.owner = THIS_MODULE,
234 	.open = acpi_thermal_polling_open_fs,
235 	.read = seq_read,
236 	.write = acpi_thermal_write_polling,
237 	.llseek = seq_lseek,
238 	.release = single_release,
239 };
240 
241 /* --------------------------------------------------------------------------
242                              Thermal Zone Management
243    -------------------------------------------------------------------------- */
244 
245 static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
246 {
247 	acpi_status status = AE_OK;
248 	unsigned long long tmp;
249 
250 	if (!tz)
251 		return -EINVAL;
252 
253 	tz->last_temperature = tz->temperature;
254 
255 	status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
256 	if (ACPI_FAILURE(status))
257 		return -ENODEV;
258 
259 	tz->temperature = tmp;
260 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
261 			  tz->temperature));
262 
263 	return 0;
264 }
265 
266 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
267 {
268 	acpi_status status = AE_OK;
269 	unsigned long long tmp;
270 
271 	if (!tz)
272 		return -EINVAL;
273 
274 	status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
275 	if (ACPI_FAILURE(status))
276 		return -ENODEV;
277 
278 	tz->polling_frequency = tmp;
279 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
280 			  tz->polling_frequency));
281 
282 	return 0;
283 }
284 
285 static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
286 {
287 
288 	if (!tz)
289 		return -EINVAL;
290 
291 	tz->polling_frequency = seconds * 10;	/* Convert value to deci-seconds */
292 
293 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
294 			  "Polling frequency set to %lu seconds\n",
295 			  tz->polling_frequency/10));
296 
297 	return 0;
298 }
299 
300 static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
301 {
302 	acpi_status status = AE_OK;
303 	union acpi_object arg0 = { ACPI_TYPE_INTEGER };
304 	struct acpi_object_list arg_list = { 1, &arg0 };
305 	acpi_handle handle = NULL;
306 
307 
308 	if (!tz)
309 		return -EINVAL;
310 
311 	status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
312 	if (ACPI_FAILURE(status)) {
313 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
314 		return -ENODEV;
315 	}
316 
317 	arg0.integer.value = mode;
318 
319 	status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
320 	if (ACPI_FAILURE(status))
321 		return -ENODEV;
322 
323 	return 0;
324 }
325 
326 #define ACPI_TRIPS_CRITICAL	0x01
327 #define ACPI_TRIPS_HOT		0x02
328 #define ACPI_TRIPS_PASSIVE	0x04
329 #define ACPI_TRIPS_ACTIVE	0x08
330 #define ACPI_TRIPS_DEVICES	0x10
331 
332 #define ACPI_TRIPS_REFRESH_THRESHOLDS	(ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
333 #define ACPI_TRIPS_REFRESH_DEVICES	ACPI_TRIPS_DEVICES
334 
335 #define ACPI_TRIPS_INIT      (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT |	\
336 			      ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE |	\
337 			      ACPI_TRIPS_DEVICES)
338 
339 /*
340  * This exception is thrown out in two cases:
341  * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
342  *   when re-evaluating the AML code.
343  * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
344  *   We need to re-bind the cooling devices of a thermal zone when this occurs.
345  */
346 #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str)	\
347 do {	\
348 	if (flags != ACPI_TRIPS_INIT)	\
349 		ACPI_EXCEPTION((AE_INFO, AE_ERROR,	\
350 		"ACPI thermal trip point %s changed\n"	\
351 		"Please send acpidump to linux-acpi@vger.kernel.org\n", str)); \
352 } while (0)
353 
354 static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
355 {
356 	acpi_status status = AE_OK;
357 	unsigned long long tmp;
358 	struct acpi_handle_list devices;
359 	int valid = 0;
360 	int i;
361 
362 	/* Critical Shutdown (required) */
363 	if (flag & ACPI_TRIPS_CRITICAL) {
364 		status = acpi_evaluate_integer(tz->device->handle,
365 				"_CRT", NULL, &tmp);
366 		tz->trips.critical.temperature = tmp;
367 		/*
368 		 * Treat freezing temperatures as invalid as well; some
369 		 * BIOSes return really low values and cause reboots at startup.
370 		 * Below zero (Celcius) values clearly aren't right for sure..
371 		 * ... so lets discard those as invalid.
372 		 */
373 		if (ACPI_FAILURE(status) ||
374 				tz->trips.critical.temperature <= 2732) {
375 			tz->trips.critical.flags.valid = 0;
376 			ACPI_EXCEPTION((AE_INFO, status,
377 					"No or invalid critical threshold"));
378 			return -ENODEV;
379 		} else {
380 			tz->trips.critical.flags.valid = 1;
381 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
382 					"Found critical threshold [%lu]\n",
383 					tz->trips.critical.temperature));
384 		}
385 		if (tz->trips.critical.flags.valid == 1) {
386 			if (crt == -1) {
387 				tz->trips.critical.flags.valid = 0;
388 			} else if (crt > 0) {
389 				unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
390 				/*
391 				 * Allow override critical threshold
392 				 */
393 				if (crt_k > tz->trips.critical.temperature)
394 					printk(KERN_WARNING PREFIX
395 						"Critical threshold %d C\n", crt);
396 				tz->trips.critical.temperature = crt_k;
397 			}
398 		}
399 	}
400 
401 	/* Critical Sleep (optional) */
402 	if (flag & ACPI_TRIPS_HOT) {
403 		status = acpi_evaluate_integer(tz->device->handle,
404 				"_HOT", NULL, &tmp);
405 		if (ACPI_FAILURE(status)) {
406 			tz->trips.hot.flags.valid = 0;
407 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
408 					"No hot threshold\n"));
409 		} else {
410 			tz->trips.hot.temperature = tmp;
411 			tz->trips.hot.flags.valid = 1;
412 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
413 					"Found hot threshold [%lu]\n",
414 					tz->trips.critical.temperature));
415 		}
416 	}
417 
418 	/* Passive (optional) */
419 	if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
420 		(flag == ACPI_TRIPS_INIT)) {
421 		valid = tz->trips.passive.flags.valid;
422 		if (psv == -1) {
423 			status = AE_SUPPORT;
424 		} else if (psv > 0) {
425 			tmp = CELSIUS_TO_KELVIN(psv);
426 			status = AE_OK;
427 		} else {
428 			status = acpi_evaluate_integer(tz->device->handle,
429 				"_PSV", NULL, &tmp);
430 		}
431 
432 		if (ACPI_FAILURE(status))
433 			tz->trips.passive.flags.valid = 0;
434 		else {
435 			tz->trips.passive.temperature = tmp;
436 			tz->trips.passive.flags.valid = 1;
437 			if (flag == ACPI_TRIPS_INIT) {
438 				status = acpi_evaluate_integer(
439 						tz->device->handle, "_TC1",
440 						NULL, &tmp);
441 				if (ACPI_FAILURE(status))
442 					tz->trips.passive.flags.valid = 0;
443 				else
444 					tz->trips.passive.tc1 = tmp;
445 				status = acpi_evaluate_integer(
446 						tz->device->handle, "_TC2",
447 						NULL, &tmp);
448 				if (ACPI_FAILURE(status))
449 					tz->trips.passive.flags.valid = 0;
450 				else
451 					tz->trips.passive.tc2 = tmp;
452 				status = acpi_evaluate_integer(
453 						tz->device->handle, "_TSP",
454 						NULL, &tmp);
455 				if (ACPI_FAILURE(status))
456 					tz->trips.passive.flags.valid = 0;
457 				else
458 					tz->trips.passive.tsp = tmp;
459 			}
460 		}
461 	}
462 	if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
463 		memset(&devices, 0, sizeof(struct acpi_handle_list));
464 		status = acpi_evaluate_reference(tz->device->handle, "_PSL",
465 							NULL, &devices);
466 		if (ACPI_FAILURE(status)) {
467 			printk(KERN_WARNING PREFIX
468 				"Invalid passive threshold\n");
469 			tz->trips.passive.flags.valid = 0;
470 		}
471 		else
472 			tz->trips.passive.flags.valid = 1;
473 
474 		if (memcmp(&tz->trips.passive.devices, &devices,
475 				sizeof(struct acpi_handle_list))) {
476 			memcpy(&tz->trips.passive.devices, &devices,
477 				sizeof(struct acpi_handle_list));
478 			ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
479 		}
480 	}
481 	if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
482 		if (valid != tz->trips.passive.flags.valid)
483 				ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
484 	}
485 
486 	/* Active (optional) */
487 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
488 		char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
489 		valid = tz->trips.active[i].flags.valid;
490 
491 		if (act == -1)
492 			break; /* disable all active trip points */
493 
494 		if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
495 			tz->trips.active[i].flags.valid)) {
496 			status = acpi_evaluate_integer(tz->device->handle,
497 							name, NULL, &tmp);
498 			if (ACPI_FAILURE(status)) {
499 				tz->trips.active[i].flags.valid = 0;
500 				if (i == 0)
501 					break;
502 				if (act <= 0)
503 					break;
504 				if (i == 1)
505 					tz->trips.active[0].temperature =
506 						CELSIUS_TO_KELVIN(act);
507 				else
508 					/*
509 					 * Don't allow override higher than
510 					 * the next higher trip point
511 					 */
512 					tz->trips.active[i - 1].temperature =
513 						(tz->trips.active[i - 2].temperature <
514 						CELSIUS_TO_KELVIN(act) ?
515 						tz->trips.active[i - 2].temperature :
516 						CELSIUS_TO_KELVIN(act));
517 				break;
518 			} else {
519 				tz->trips.active[i].temperature = tmp;
520 				tz->trips.active[i].flags.valid = 1;
521 			}
522 		}
523 
524 		name[2] = 'L';
525 		if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
526 			memset(&devices, 0, sizeof(struct acpi_handle_list));
527 			status = acpi_evaluate_reference(tz->device->handle,
528 						name, NULL, &devices);
529 			if (ACPI_FAILURE(status)) {
530 				printk(KERN_WARNING PREFIX
531 					"Invalid active%d threshold\n", i);
532 				tz->trips.active[i].flags.valid = 0;
533 			}
534 			else
535 				tz->trips.active[i].flags.valid = 1;
536 
537 			if (memcmp(&tz->trips.active[i].devices, &devices,
538 					sizeof(struct acpi_handle_list))) {
539 				memcpy(&tz->trips.active[i].devices, &devices,
540 					sizeof(struct acpi_handle_list));
541 				ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
542 			}
543 		}
544 		if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
545 			if (valid != tz->trips.active[i].flags.valid)
546 				ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
547 
548 		if (!tz->trips.active[i].flags.valid)
549 			break;
550 	}
551 
552 	if (flag & ACPI_TRIPS_DEVICES) {
553 		memset(&devices, 0, sizeof(struct acpi_handle_list));
554 		status = acpi_evaluate_reference(tz->device->handle, "_TZD",
555 						NULL, &devices);
556 		if (memcmp(&tz->devices, &devices,
557 				sizeof(struct acpi_handle_list))) {
558 			memcpy(&tz->devices, &devices,
559 				sizeof(struct acpi_handle_list));
560 			ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
561 		}
562 	}
563 
564 	return 0;
565 }
566 
567 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
568 {
569 	return acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
570 }
571 
572 static int acpi_thermal_critical(struct acpi_thermal *tz)
573 {
574 	if (!tz || !tz->trips.critical.flags.valid)
575 		return -EINVAL;
576 
577 	if (tz->temperature >= tz->trips.critical.temperature) {
578 		printk(KERN_WARNING PREFIX "Critical trip point\n");
579 		tz->trips.critical.flags.enabled = 1;
580 	} else if (tz->trips.critical.flags.enabled)
581 		tz->trips.critical.flags.enabled = 0;
582 
583 	acpi_bus_generate_proc_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
584 				tz->trips.critical.flags.enabled);
585 	acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
586 					  dev_name(&tz->device->dev),
587 					  ACPI_THERMAL_NOTIFY_CRITICAL,
588 					  tz->trips.critical.flags.enabled);
589 
590 	/* take no action if nocrt is set */
591 	if(!nocrt) {
592 		printk(KERN_EMERG
593 			"Critical temperature reached (%ld C), shutting down.\n",
594 			KELVIN_TO_CELSIUS(tz->temperature));
595 		orderly_poweroff(true);
596 	}
597 
598 	return 0;
599 }
600 
601 static int acpi_thermal_hot(struct acpi_thermal *tz)
602 {
603 	if (!tz || !tz->trips.hot.flags.valid)
604 		return -EINVAL;
605 
606 	if (tz->temperature >= tz->trips.hot.temperature) {
607 		printk(KERN_WARNING PREFIX "Hot trip point\n");
608 		tz->trips.hot.flags.enabled = 1;
609 	} else if (tz->trips.hot.flags.enabled)
610 		tz->trips.hot.flags.enabled = 0;
611 
612 	acpi_bus_generate_proc_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
613 				tz->trips.hot.flags.enabled);
614 	acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
615 					  dev_name(&tz->device->dev),
616 					  ACPI_THERMAL_NOTIFY_HOT,
617 					  tz->trips.hot.flags.enabled);
618 
619 	/* TBD: Call user-mode "sleep(S4)" function if nocrt is cleared */
620 
621 	return 0;
622 }
623 
624 static void acpi_thermal_passive(struct acpi_thermal *tz)
625 {
626 	int result = 1;
627 	struct acpi_thermal_passive *passive = NULL;
628 	int trend = 0;
629 	int i = 0;
630 
631 
632 	if (!tz || !tz->trips.passive.flags.valid)
633 		return;
634 
635 	passive = &(tz->trips.passive);
636 
637 	/*
638 	 * Above Trip?
639 	 * -----------
640 	 * Calculate the thermal trend (using the passive cooling equation)
641 	 * and modify the performance limit for all passive cooling devices
642 	 * accordingly.  Note that we assume symmetry.
643 	 */
644 	if (tz->temperature >= passive->temperature) {
645 		trend =
646 		    (passive->tc1 * (tz->temperature - tz->last_temperature)) +
647 		    (passive->tc2 * (tz->temperature - passive->temperature));
648 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
649 				  "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
650 				  trend, passive->tc1, tz->temperature,
651 				  tz->last_temperature, passive->tc2,
652 				  tz->temperature, passive->temperature));
653 		passive->flags.enabled = 1;
654 		/* Heating up? */
655 		if (trend > 0)
656 			for (i = 0; i < passive->devices.count; i++)
657 				acpi_processor_set_thermal_limit(passive->
658 								 devices.
659 								 handles[i],
660 								 ACPI_PROCESSOR_LIMIT_INCREMENT);
661 		/* Cooling off? */
662 		else if (trend < 0) {
663 			for (i = 0; i < passive->devices.count; i++)
664 				/*
665 				 * assume that we are on highest
666 				 * freq/lowest thrott and can leave
667 				 * passive mode, even in error case
668 				 */
669 				if (!acpi_processor_set_thermal_limit
670 				    (passive->devices.handles[i],
671 				     ACPI_PROCESSOR_LIMIT_DECREMENT))
672 					result = 0;
673 			/*
674 			 * Leave cooling mode, even if the temp might
675 			 * higher than trip point This is because some
676 			 * machines might have long thermal polling
677 			 * frequencies (tsp) defined. We will fall back
678 			 * into passive mode in next cycle (probably quicker)
679 			 */
680 			if (result) {
681 				passive->flags.enabled = 0;
682 				ACPI_DEBUG_PRINT((ACPI_DB_INFO,
683 						  "Disabling passive cooling, still above threshold,"
684 						  " but we are cooling down\n"));
685 			}
686 		}
687 		return;
688 	}
689 
690 	/*
691 	 * Below Trip?
692 	 * -----------
693 	 * Implement passive cooling hysteresis to slowly increase performance
694 	 * and avoid thrashing around the passive trip point.  Note that we
695 	 * assume symmetry.
696 	 */
697 	if (!passive->flags.enabled)
698 		return;
699 	for (i = 0; i < passive->devices.count; i++)
700 		if (!acpi_processor_set_thermal_limit
701 		    (passive->devices.handles[i],
702 		     ACPI_PROCESSOR_LIMIT_DECREMENT))
703 			result = 0;
704 	if (result) {
705 		passive->flags.enabled = 0;
706 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
707 				  "Disabling passive cooling (zone is cool)\n"));
708 	}
709 }
710 
711 static void acpi_thermal_active(struct acpi_thermal *tz)
712 {
713 	int result = 0;
714 	struct acpi_thermal_active *active = NULL;
715 	int i = 0;
716 	int j = 0;
717 	unsigned long maxtemp = 0;
718 
719 
720 	if (!tz)
721 		return;
722 
723 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
724 		active = &(tz->trips.active[i]);
725 		if (!active || !active->flags.valid)
726 			break;
727 		if (tz->temperature >= active->temperature) {
728 			/*
729 			 * Above Threshold?
730 			 * ----------------
731 			 * If not already enabled, turn ON all cooling devices
732 			 * associated with this active threshold.
733 			 */
734 			if (active->temperature > maxtemp)
735 				tz->state.active_index = i;
736 			maxtemp = active->temperature;
737 			if (active->flags.enabled)
738 				continue;
739 			for (j = 0; j < active->devices.count; j++) {
740 				result =
741 				    acpi_bus_set_power(active->devices.
742 						       handles[j],
743 						       ACPI_STATE_D0);
744 				if (result) {
745 					printk(KERN_WARNING PREFIX
746 						      "Unable to turn cooling device [%p] 'on'\n",
747 						      active->devices.
748 						      handles[j]);
749 					continue;
750 				}
751 				active->flags.enabled = 1;
752 				ACPI_DEBUG_PRINT((ACPI_DB_INFO,
753 						  "Cooling device [%p] now 'on'\n",
754 						  active->devices.handles[j]));
755 			}
756 			continue;
757 		}
758 		if (!active->flags.enabled)
759 			continue;
760 		/*
761 		 * Below Threshold?
762 		 * ----------------
763 		 * Turn OFF all cooling devices associated with this
764 		 * threshold.
765 		 */
766 		for (j = 0; j < active->devices.count; j++) {
767 			result = acpi_bus_set_power(active->devices.handles[j],
768 						    ACPI_STATE_D3);
769 			if (result) {
770 				printk(KERN_WARNING PREFIX
771 					      "Unable to turn cooling device [%p] 'off'\n",
772 					      active->devices.handles[j]);
773 				continue;
774 			}
775 			active->flags.enabled = 0;
776 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
777 					  "Cooling device [%p] now 'off'\n",
778 					  active->devices.handles[j]));
779 		}
780 	}
781 }
782 
783 static void acpi_thermal_check(void *context);
784 
785 static void acpi_thermal_run(unsigned long data)
786 {
787 	struct acpi_thermal *tz = (struct acpi_thermal *)data;
788 	if (!tz->zombie)
789 		acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
790 }
791 
792 static void acpi_thermal_active_off(void *data)
793 {
794 	int result = 0;
795 	struct acpi_thermal *tz = data;
796 	int i = 0;
797 	int j = 0;
798 	struct acpi_thermal_active *active = NULL;
799 
800 	if (!tz) {
801 		printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
802 		return;
803 	}
804 
805 	result = acpi_thermal_get_temperature(tz);
806 	if (result)
807 		return;
808 
809 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
810 		active = &(tz->trips.active[i]);
811 		if (!active || !active->flags.valid)
812 			break;
813 		if (tz->temperature >= active->temperature) {
814 			/*
815 			 * If the thermal temperature is greater than the
816 			 * active threshod, unnecessary to turn off the
817 			 * the active cooling device.
818 			 */
819 			continue;
820 		}
821 		/*
822 		 * Below Threshold?
823 		 * ----------------
824 		 * Turn OFF all cooling devices associated with this
825 		 * threshold.
826 		 */
827 		for (j = 0; j < active->devices.count; j++)
828 			result = acpi_bus_set_power(active->devices.handles[j],
829 						    ACPI_STATE_D3);
830 	}
831 }
832 
833 static void acpi_thermal_check(void *data)
834 {
835 	int result = 0;
836 	struct acpi_thermal *tz = data;
837 	unsigned long sleep_time = 0;
838 	unsigned long timeout_jiffies = 0;
839 	int i = 0;
840 	struct acpi_thermal_state state;
841 
842 
843 	if (!tz) {
844 		printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
845 		return;
846 	}
847 
848 	/* Check if someone else is already running */
849 	if (!mutex_trylock(&tz->lock))
850 		return;
851 
852 	state = tz->state;
853 
854 	result = acpi_thermal_get_temperature(tz);
855 	if (result)
856 		goto unlock;
857 
858 	if (!tz->tz_enabled)
859 		goto unlock;
860 
861 	memset(&tz->state, 0, sizeof(tz->state));
862 
863 	/*
864 	 * Check Trip Points
865 	 * -----------------
866 	 * Compare the current temperature to the trip point values to see
867 	 * if we've entered one of the thermal policy states.  Note that
868 	 * this function determines when a state is entered, but the
869 	 * individual policy decides when it is exited (e.g. hysteresis).
870 	 */
871 	if (tz->trips.critical.flags.valid)
872 		state.critical |=
873 		    (tz->temperature >= tz->trips.critical.temperature);
874 	if (tz->trips.hot.flags.valid)
875 		state.hot |= (tz->temperature >= tz->trips.hot.temperature);
876 	if (tz->trips.passive.flags.valid)
877 		state.passive |=
878 		    (tz->temperature >= tz->trips.passive.temperature);
879 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
880 		if (tz->trips.active[i].flags.valid)
881 			state.active |=
882 			    (tz->temperature >=
883 			     tz->trips.active[i].temperature);
884 
885 	/*
886 	 * Invoke Policy
887 	 * -------------
888 	 * Separated from the above check to allow individual policy to
889 	 * determine when to exit a given state.
890 	 */
891 	if (state.critical)
892 		acpi_thermal_critical(tz);
893 	if (state.hot)
894 		acpi_thermal_hot(tz);
895 	if (state.passive)
896 		acpi_thermal_passive(tz);
897 	if (state.active)
898 		acpi_thermal_active(tz);
899 
900 	/*
901 	 * Calculate State
902 	 * ---------------
903 	 * Again, separated from the above two to allow independent policy
904 	 * decisions.
905 	 */
906 	tz->state.critical = tz->trips.critical.flags.enabled;
907 	tz->state.hot = tz->trips.hot.flags.enabled;
908 	tz->state.passive = tz->trips.passive.flags.enabled;
909 	tz->state.active = 0;
910 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
911 		tz->state.active |= tz->trips.active[i].flags.enabled;
912 
913 	/*
914 	 * Calculate Sleep Time
915 	 * --------------------
916 	 * If we're in the passive state, use _TSP's value.  Otherwise
917 	 * use the default polling frequency (e.g. _TZP).  If no polling
918 	 * frequency is specified then we'll wait forever (at least until
919 	 * a thermal event occurs).  Note that _TSP and _TZD values are
920 	 * given in 1/10th seconds (we must covert to milliseconds).
921 	 */
922 	if (tz->state.passive) {
923 		sleep_time = tz->trips.passive.tsp * 100;
924 		timeout_jiffies =  jiffies + (HZ * sleep_time) / 1000;
925 	} else if (tz->polling_frequency > 0) {
926 		sleep_time = tz->polling_frequency * 100;
927 		timeout_jiffies =  round_jiffies(jiffies + (HZ * sleep_time) / 1000);
928 	}
929 
930 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
931 			  tz->name, tz->temperature, sleep_time));
932 
933 	/*
934 	 * Schedule Next Poll
935 	 * ------------------
936 	 */
937 	if (!sleep_time) {
938 		if (timer_pending(&(tz->timer)))
939 			del_timer(&(tz->timer));
940 	} else {
941 		if (timer_pending(&(tz->timer)))
942 			mod_timer(&(tz->timer), timeout_jiffies);
943 		else {
944 			tz->timer.data = (unsigned long)tz;
945 			tz->timer.function = acpi_thermal_run;
946 			tz->timer.expires = timeout_jiffies;
947 			add_timer(&(tz->timer));
948 		}
949 	}
950       unlock:
951 	mutex_unlock(&tz->lock);
952 }
953 
954 /* sys I/F for generic thermal sysfs support */
955 #define KELVIN_TO_MILLICELSIUS(t) (t * 100 - 273200)
956 
957 static int thermal_get_temp(struct thermal_zone_device *thermal, char *buf)
958 {
959 	struct acpi_thermal *tz = thermal->devdata;
960 	int result;
961 
962 	if (!tz)
963 		return -EINVAL;
964 
965 	result = acpi_thermal_get_temperature(tz);
966 	if (result)
967 		return result;
968 
969 	return sprintf(buf, "%ld\n", KELVIN_TO_MILLICELSIUS(tz->temperature));
970 }
971 
972 static const char enabled[] = "kernel";
973 static const char disabled[] = "user";
974 static int thermal_get_mode(struct thermal_zone_device *thermal,
975 				char *buf)
976 {
977 	struct acpi_thermal *tz = thermal->devdata;
978 
979 	if (!tz)
980 		return -EINVAL;
981 
982 	return sprintf(buf, "%s\n", tz->tz_enabled ?
983 			enabled : disabled);
984 }
985 
986 static int thermal_set_mode(struct thermal_zone_device *thermal,
987 				const char *buf)
988 {
989 	struct acpi_thermal *tz = thermal->devdata;
990 	int enable;
991 
992 	if (!tz)
993 		return -EINVAL;
994 
995 	/*
996 	 * enable/disable thermal management from ACPI thermal driver
997 	 */
998 	if (!strncmp(buf, enabled, sizeof enabled - 1))
999 		enable = 1;
1000 	else if (!strncmp(buf, disabled, sizeof disabled - 1))
1001 		enable = 0;
1002 	else
1003 		return -EINVAL;
1004 
1005 	if (enable != tz->tz_enabled) {
1006 		tz->tz_enabled = enable;
1007 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1008 			"%s ACPI thermal control\n",
1009 			tz->tz_enabled ? enabled : disabled));
1010 		acpi_thermal_check(tz);
1011 	}
1012 	return 0;
1013 }
1014 
1015 static int thermal_get_trip_type(struct thermal_zone_device *thermal,
1016 				 int trip, char *buf)
1017 {
1018 	struct acpi_thermal *tz = thermal->devdata;
1019 	int i;
1020 
1021 	if (!tz || trip < 0)
1022 		return -EINVAL;
1023 
1024 	if (tz->trips.critical.flags.valid) {
1025 		if (!trip)
1026 			return sprintf(buf, "critical\n");
1027 		trip--;
1028 	}
1029 
1030 	if (tz->trips.hot.flags.valid) {
1031 		if (!trip)
1032 			return sprintf(buf, "hot\n");
1033 		trip--;
1034 	}
1035 
1036 	if (tz->trips.passive.flags.valid) {
1037 		if (!trip)
1038 			return sprintf(buf, "passive\n");
1039 		trip--;
1040 	}
1041 
1042 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
1043 		tz->trips.active[i].flags.valid; i++) {
1044 		if (!trip)
1045 			return sprintf(buf, "active%d\n", i);
1046 		trip--;
1047 	}
1048 
1049 	return -EINVAL;
1050 }
1051 
1052 static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
1053 				 int trip, char *buf)
1054 {
1055 	struct acpi_thermal *tz = thermal->devdata;
1056 	int i;
1057 
1058 	if (!tz || trip < 0)
1059 		return -EINVAL;
1060 
1061 	if (tz->trips.critical.flags.valid) {
1062 		if (!trip)
1063 			return sprintf(buf, "%ld\n", KELVIN_TO_MILLICELSIUS(
1064 				tz->trips.critical.temperature));
1065 		trip--;
1066 	}
1067 
1068 	if (tz->trips.hot.flags.valid) {
1069 		if (!trip)
1070 			return sprintf(buf, "%ld\n", KELVIN_TO_MILLICELSIUS(
1071 					tz->trips.hot.temperature));
1072 		trip--;
1073 	}
1074 
1075 	if (tz->trips.passive.flags.valid) {
1076 		if (!trip)
1077 			return sprintf(buf, "%ld\n", KELVIN_TO_MILLICELSIUS(
1078 					tz->trips.passive.temperature));
1079 		trip--;
1080 	}
1081 
1082 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
1083 		tz->trips.active[i].flags.valid; i++) {
1084 		if (!trip)
1085 			return sprintf(buf, "%ld\n", KELVIN_TO_MILLICELSIUS(
1086 					tz->trips.active[i].temperature));
1087 		trip--;
1088 	}
1089 
1090 	return -EINVAL;
1091 }
1092 
1093 static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
1094 				unsigned long *temperature) {
1095 	struct acpi_thermal *tz = thermal->devdata;
1096 
1097 	if (tz->trips.critical.flags.valid) {
1098 		*temperature = KELVIN_TO_MILLICELSIUS(
1099 				tz->trips.critical.temperature);
1100 		return 0;
1101 	} else
1102 		return -EINVAL;
1103 }
1104 
1105 typedef int (*cb)(struct thermal_zone_device *, int,
1106 		  struct thermal_cooling_device *);
1107 static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
1108 					struct thermal_cooling_device *cdev,
1109 					cb action)
1110 {
1111 	struct acpi_device *device = cdev->devdata;
1112 	struct acpi_thermal *tz = thermal->devdata;
1113 	struct acpi_device *dev;
1114 	acpi_status status;
1115 	acpi_handle handle;
1116 	int i;
1117 	int j;
1118 	int trip = -1;
1119 	int result = 0;
1120 
1121 	if (tz->trips.critical.flags.valid)
1122 		trip++;
1123 
1124 	if (tz->trips.hot.flags.valid)
1125 		trip++;
1126 
1127 	if (tz->trips.passive.flags.valid) {
1128 		trip++;
1129 		for (i = 0; i < tz->trips.passive.devices.count;
1130 		    i++) {
1131 			handle = tz->trips.passive.devices.handles[i];
1132 			status = acpi_bus_get_device(handle, &dev);
1133 			if (ACPI_SUCCESS(status) && (dev == device)) {
1134 				result = action(thermal, trip, cdev);
1135 				if (result)
1136 					goto failed;
1137 			}
1138 		}
1139 	}
1140 
1141 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1142 		if (!tz->trips.active[i].flags.valid)
1143 			break;
1144 		trip++;
1145 		for (j = 0;
1146 		    j < tz->trips.active[i].devices.count;
1147 		    j++) {
1148 			handle = tz->trips.active[i].devices.handles[j];
1149 			status = acpi_bus_get_device(handle, &dev);
1150 			if (ACPI_SUCCESS(status) && (dev == device)) {
1151 				result = action(thermal, trip, cdev);
1152 				if (result)
1153 					goto failed;
1154 			}
1155 		}
1156 	}
1157 
1158 	for (i = 0; i < tz->devices.count; i++) {
1159 		handle = tz->devices.handles[i];
1160 		status = acpi_bus_get_device(handle, &dev);
1161 		if (ACPI_SUCCESS(status) && (dev == device)) {
1162 			result = action(thermal, -1, cdev);
1163 			if (result)
1164 				goto failed;
1165 		}
1166 	}
1167 
1168 failed:
1169 	return result;
1170 }
1171 
1172 static int
1173 acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
1174 					struct thermal_cooling_device *cdev)
1175 {
1176 	return acpi_thermal_cooling_device_cb(thermal, cdev,
1177 				thermal_zone_bind_cooling_device);
1178 }
1179 
1180 static int
1181 acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
1182 					struct thermal_cooling_device *cdev)
1183 {
1184 	return acpi_thermal_cooling_device_cb(thermal, cdev,
1185 				thermal_zone_unbind_cooling_device);
1186 }
1187 
1188 static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
1189 	.bind = acpi_thermal_bind_cooling_device,
1190 	.unbind	= acpi_thermal_unbind_cooling_device,
1191 	.get_temp = thermal_get_temp,
1192 	.get_mode = thermal_get_mode,
1193 	.set_mode = thermal_set_mode,
1194 	.get_trip_type = thermal_get_trip_type,
1195 	.get_trip_temp = thermal_get_trip_temp,
1196 	.get_crit_temp = thermal_get_crit_temp,
1197 };
1198 
1199 static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
1200 {
1201 	int trips = 0;
1202 	int result;
1203 	acpi_status status;
1204 	int i;
1205 
1206 	if (tz->trips.critical.flags.valid)
1207 		trips++;
1208 
1209 	if (tz->trips.hot.flags.valid)
1210 		trips++;
1211 
1212 	if (tz->trips.passive.flags.valid)
1213 		trips++;
1214 
1215 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
1216 			tz->trips.active[i].flags.valid; i++, trips++);
1217 	tz->thermal_zone = thermal_zone_device_register("acpitz",
1218 					trips, tz, &acpi_thermal_zone_ops);
1219 	if (IS_ERR(tz->thermal_zone))
1220 		return -ENODEV;
1221 
1222 	result = sysfs_create_link(&tz->device->dev.kobj,
1223 				   &tz->thermal_zone->device.kobj, "thermal_zone");
1224 	if (result)
1225 		return result;
1226 
1227 	result = sysfs_create_link(&tz->thermal_zone->device.kobj,
1228 				   &tz->device->dev.kobj, "device");
1229 	if (result)
1230 		return result;
1231 
1232 	status = acpi_attach_data(tz->device->handle,
1233 				  acpi_bus_private_data_handler,
1234 				  tz->thermal_zone);
1235 	if (ACPI_FAILURE(status)) {
1236 		printk(KERN_ERR PREFIX
1237 				"Error attaching device data\n");
1238 		return -ENODEV;
1239 	}
1240 
1241 	tz->tz_enabled = 1;
1242 
1243 	dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
1244 		 tz->thermal_zone->id);
1245 	return 0;
1246 }
1247 
1248 static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
1249 {
1250 	sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
1251 	sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
1252 	thermal_zone_device_unregister(tz->thermal_zone);
1253 	tz->thermal_zone = NULL;
1254 	acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
1255 }
1256 
1257 
1258 /* --------------------------------------------------------------------------
1259                               FS Interface (/proc)
1260    -------------------------------------------------------------------------- */
1261 
1262 static struct proc_dir_entry *acpi_thermal_dir;
1263 
1264 static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
1265 {
1266 	struct acpi_thermal *tz = seq->private;
1267 
1268 
1269 	if (!tz)
1270 		goto end;
1271 
1272 	seq_puts(seq, "state:                   ");
1273 
1274 	if (!tz->state.critical && !tz->state.hot && !tz->state.passive
1275 	    && !tz->state.active)
1276 		seq_puts(seq, "ok\n");
1277 	else {
1278 		if (tz->state.critical)
1279 			seq_puts(seq, "critical ");
1280 		if (tz->state.hot)
1281 			seq_puts(seq, "hot ");
1282 		if (tz->state.passive)
1283 			seq_puts(seq, "passive ");
1284 		if (tz->state.active)
1285 			seq_printf(seq, "active[%d]", tz->state.active_index);
1286 		seq_puts(seq, "\n");
1287 	}
1288 
1289       end:
1290 	return 0;
1291 }
1292 
1293 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
1294 {
1295 	return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
1296 }
1297 
1298 static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
1299 {
1300 	int result = 0;
1301 	struct acpi_thermal *tz = seq->private;
1302 
1303 
1304 	if (!tz)
1305 		goto end;
1306 
1307 	result = acpi_thermal_get_temperature(tz);
1308 	if (result)
1309 		goto end;
1310 
1311 	seq_printf(seq, "temperature:             %ld C\n",
1312 		   KELVIN_TO_CELSIUS(tz->temperature));
1313 
1314       end:
1315 	return 0;
1316 }
1317 
1318 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
1319 {
1320 	return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
1321 }
1322 
1323 static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
1324 {
1325 	struct acpi_thermal *tz = seq->private;
1326 	struct acpi_device *device;
1327 	acpi_status status;
1328 
1329 	int i = 0;
1330 	int j = 0;
1331 
1332 
1333 	if (!tz)
1334 		goto end;
1335 
1336 	if (tz->trips.critical.flags.valid)
1337 		seq_printf(seq, "critical (S5):           %ld C%s",
1338 			   KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
1339 			   nocrt ? " <disabled>\n" : "\n");
1340 
1341 	if (tz->trips.hot.flags.valid)
1342 		seq_printf(seq, "hot (S4):                %ld C%s",
1343 			   KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
1344 			   nocrt ? " <disabled>\n" : "\n");
1345 
1346 	if (tz->trips.passive.flags.valid) {
1347 		seq_printf(seq,
1348 			   "passive:                 %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
1349 			   KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
1350 			   tz->trips.passive.tc1, tz->trips.passive.tc2,
1351 			   tz->trips.passive.tsp);
1352 		for (j = 0; j < tz->trips.passive.devices.count; j++) {
1353 			status = acpi_bus_get_device(tz->trips.passive.devices.
1354 						     handles[j], &device);
1355 			seq_printf(seq, "%4.4s ", status ? "" :
1356 				   acpi_device_bid(device));
1357 		}
1358 		seq_puts(seq, "\n");
1359 	}
1360 
1361 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1362 		if (!(tz->trips.active[i].flags.valid))
1363 			break;
1364 		seq_printf(seq, "active[%d]:               %ld C: devices=",
1365 			   i,
1366 			   KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
1367 		for (j = 0; j < tz->trips.active[i].devices.count; j++){
1368 			status = acpi_bus_get_device(tz->trips.active[i].
1369 						     devices.handles[j],
1370 						     &device);
1371 			seq_printf(seq, "%4.4s ", status ? "" :
1372 				   acpi_device_bid(device));
1373 		}
1374 		seq_puts(seq, "\n");
1375 	}
1376 
1377       end:
1378 	return 0;
1379 }
1380 
1381 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
1382 {
1383 	return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
1384 }
1385 
1386 static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
1387 {
1388 	struct acpi_thermal *tz = seq->private;
1389 
1390 
1391 	if (!tz)
1392 		goto end;
1393 
1394 	if (!tz->flags.cooling_mode)
1395 		seq_puts(seq, "<setting not supported>\n");
1396 	else
1397 		seq_puts(seq, "0 - Active; 1 - Passive\n");
1398 
1399       end:
1400 	return 0;
1401 }
1402 
1403 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
1404 {
1405 	return single_open(file, acpi_thermal_cooling_seq_show,
1406 			   PDE(inode)->data);
1407 }
1408 
1409 static ssize_t
1410 acpi_thermal_write_cooling_mode(struct file *file,
1411 				const char __user * buffer,
1412 				size_t count, loff_t * ppos)
1413 {
1414 	struct seq_file *m = file->private_data;
1415 	struct acpi_thermal *tz = m->private;
1416 	int result = 0;
1417 	char mode_string[12] = { '\0' };
1418 
1419 
1420 	if (!tz || (count > sizeof(mode_string) - 1))
1421 		return -EINVAL;
1422 
1423 	if (!tz->flags.cooling_mode)
1424 		return -ENODEV;
1425 
1426 	if (copy_from_user(mode_string, buffer, count))
1427 		return -EFAULT;
1428 
1429 	mode_string[count] = '\0';
1430 
1431 	result = acpi_thermal_set_cooling_mode(tz,
1432 					       simple_strtoul(mode_string, NULL,
1433 							      0));
1434 	if (result)
1435 		return result;
1436 
1437 	acpi_thermal_check(tz);
1438 
1439 	return count;
1440 }
1441 
1442 static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
1443 {
1444 	struct acpi_thermal *tz = seq->private;
1445 
1446 
1447 	if (!tz)
1448 		goto end;
1449 
1450 	if (!tz->polling_frequency) {
1451 		seq_puts(seq, "<polling disabled>\n");
1452 		goto end;
1453 	}
1454 
1455 	seq_printf(seq, "polling frequency:       %lu seconds\n",
1456 		   (tz->polling_frequency / 10));
1457 
1458       end:
1459 	return 0;
1460 }
1461 
1462 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
1463 {
1464 	return single_open(file, acpi_thermal_polling_seq_show,
1465 			   PDE(inode)->data);
1466 }
1467 
1468 static ssize_t
1469 acpi_thermal_write_polling(struct file *file,
1470 			   const char __user * buffer,
1471 			   size_t count, loff_t * ppos)
1472 {
1473 	struct seq_file *m = file->private_data;
1474 	struct acpi_thermal *tz = m->private;
1475 	int result = 0;
1476 	char polling_string[12] = { '\0' };
1477 	int seconds = 0;
1478 
1479 
1480 	if (!tz || (count > sizeof(polling_string) - 1))
1481 		return -EINVAL;
1482 
1483 	if (copy_from_user(polling_string, buffer, count))
1484 		return -EFAULT;
1485 
1486 	polling_string[count] = '\0';
1487 
1488 	seconds = simple_strtoul(polling_string, NULL, 0);
1489 
1490 	result = acpi_thermal_set_polling(tz, seconds);
1491 	if (result)
1492 		return result;
1493 
1494 	acpi_thermal_check(tz);
1495 
1496 	return count;
1497 }
1498 
1499 static int acpi_thermal_add_fs(struct acpi_device *device)
1500 {
1501 	struct proc_dir_entry *entry = NULL;
1502 
1503 
1504 	if (!acpi_device_dir(device)) {
1505 		acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1506 						     acpi_thermal_dir);
1507 		if (!acpi_device_dir(device))
1508 			return -ENODEV;
1509 	}
1510 
1511 	/* 'state' [R] */
1512 	entry = proc_create_data(ACPI_THERMAL_FILE_STATE,
1513 				 S_IRUGO, acpi_device_dir(device),
1514 				 &acpi_thermal_state_fops,
1515 				 acpi_driver_data(device));
1516 	if (!entry)
1517 		return -ENODEV;
1518 
1519 	/* 'temperature' [R] */
1520 	entry = proc_create_data(ACPI_THERMAL_FILE_TEMPERATURE,
1521 				 S_IRUGO, acpi_device_dir(device),
1522 				 &acpi_thermal_temp_fops,
1523 				 acpi_driver_data(device));
1524 	if (!entry)
1525 		return -ENODEV;
1526 
1527 	/* 'trip_points' [R] */
1528 	entry = proc_create_data(ACPI_THERMAL_FILE_TRIP_POINTS,
1529 				 S_IRUGO,
1530 				 acpi_device_dir(device),
1531 				 &acpi_thermal_trip_fops,
1532 				 acpi_driver_data(device));
1533 	if (!entry)
1534 		return -ENODEV;
1535 
1536 	/* 'cooling_mode' [R/W] */
1537 	entry = proc_create_data(ACPI_THERMAL_FILE_COOLING_MODE,
1538 				 S_IFREG | S_IRUGO | S_IWUSR,
1539 				 acpi_device_dir(device),
1540 				 &acpi_thermal_cooling_fops,
1541 				 acpi_driver_data(device));
1542 	if (!entry)
1543 		return -ENODEV;
1544 
1545 	/* 'polling_frequency' [R/W] */
1546 	entry = proc_create_data(ACPI_THERMAL_FILE_POLLING_FREQ,
1547 				 S_IFREG | S_IRUGO | S_IWUSR,
1548 				 acpi_device_dir(device),
1549 				 &acpi_thermal_polling_fops,
1550 				 acpi_driver_data(device));
1551 	if (!entry)
1552 		return -ENODEV;
1553 	return 0;
1554 }
1555 
1556 static int acpi_thermal_remove_fs(struct acpi_device *device)
1557 {
1558 
1559 	if (acpi_device_dir(device)) {
1560 		remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
1561 				  acpi_device_dir(device));
1562 		remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
1563 				  acpi_device_dir(device));
1564 		remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
1565 				  acpi_device_dir(device));
1566 		remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
1567 				  acpi_device_dir(device));
1568 		remove_proc_entry(ACPI_THERMAL_FILE_STATE,
1569 				  acpi_device_dir(device));
1570 		remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
1571 		acpi_device_dir(device) = NULL;
1572 	}
1573 
1574 	return 0;
1575 }
1576 
1577 /* --------------------------------------------------------------------------
1578                                  Driver Interface
1579    -------------------------------------------------------------------------- */
1580 
1581 static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
1582 {
1583 	struct acpi_thermal *tz = data;
1584 	struct acpi_device *device = NULL;
1585 
1586 
1587 	if (!tz)
1588 		return;
1589 
1590 	device = tz->device;
1591 
1592 	switch (event) {
1593 	case ACPI_THERMAL_NOTIFY_TEMPERATURE:
1594 		acpi_thermal_check(tz);
1595 		break;
1596 	case ACPI_THERMAL_NOTIFY_THRESHOLDS:
1597 		acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
1598 		acpi_thermal_check(tz);
1599 		acpi_bus_generate_proc_event(device, event, 0);
1600 		acpi_bus_generate_netlink_event(device->pnp.device_class,
1601 						  dev_name(&device->dev), event, 0);
1602 		break;
1603 	case ACPI_THERMAL_NOTIFY_DEVICES:
1604 		acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
1605 		acpi_thermal_check(tz);
1606 		acpi_bus_generate_proc_event(device, event, 0);
1607 		acpi_bus_generate_netlink_event(device->pnp.device_class,
1608 						  dev_name(&device->dev), event, 0);
1609 		break;
1610 	default:
1611 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1612 				  "Unsupported event [0x%x]\n", event));
1613 		break;
1614 	}
1615 
1616 	return;
1617 }
1618 
1619 static int acpi_thermal_get_info(struct acpi_thermal *tz)
1620 {
1621 	int result = 0;
1622 
1623 
1624 	if (!tz)
1625 		return -EINVAL;
1626 
1627 	/* Get temperature [_TMP] (required) */
1628 	result = acpi_thermal_get_temperature(tz);
1629 	if (result)
1630 		return result;
1631 
1632 	/* Get trip points [_CRT, _PSV, etc.] (required) */
1633 	result = acpi_thermal_get_trip_points(tz);
1634 	if (result)
1635 		return result;
1636 
1637 	/* Set the cooling mode [_SCP] to active cooling (default) */
1638 	result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
1639 	if (!result)
1640 		tz->flags.cooling_mode = 1;
1641 
1642 	/* Get default polling frequency [_TZP] (optional) */
1643 	if (tzp)
1644 		tz->polling_frequency = tzp;
1645 	else
1646 		acpi_thermal_get_polling_frequency(tz);
1647 
1648 	return 0;
1649 }
1650 
1651 static int acpi_thermal_add(struct acpi_device *device)
1652 {
1653 	int result = 0;
1654 	acpi_status status = AE_OK;
1655 	struct acpi_thermal *tz = NULL;
1656 
1657 
1658 	if (!device)
1659 		return -EINVAL;
1660 
1661 	tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
1662 	if (!tz)
1663 		return -ENOMEM;
1664 
1665 	tz->device = device;
1666 	strcpy(tz->name, device->pnp.bus_id);
1667 	strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
1668 	strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
1669 	device->driver_data = tz;
1670 	mutex_init(&tz->lock);
1671 
1672 
1673 	result = acpi_thermal_get_info(tz);
1674 	if (result)
1675 		goto free_memory;
1676 
1677 	result = acpi_thermal_register_thermal_zone(tz);
1678 	if (result)
1679 		goto free_memory;
1680 
1681 	result = acpi_thermal_add_fs(device);
1682 	if (result)
1683 		goto unregister_thermal_zone;
1684 
1685 	init_timer(&tz->timer);
1686 
1687 	acpi_thermal_active_off(tz);
1688 
1689 	acpi_thermal_check(tz);
1690 
1691 	status = acpi_install_notify_handler(device->handle,
1692 					     ACPI_DEVICE_NOTIFY,
1693 					     acpi_thermal_notify, tz);
1694 	if (ACPI_FAILURE(status)) {
1695 		result = -ENODEV;
1696 		goto remove_fs;
1697 	}
1698 
1699 	printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
1700 	       acpi_device_name(device), acpi_device_bid(device),
1701 	       KELVIN_TO_CELSIUS(tz->temperature));
1702 	goto end;
1703 
1704 remove_fs:
1705 	acpi_thermal_remove_fs(device);
1706 unregister_thermal_zone:
1707 	thermal_zone_device_unregister(tz->thermal_zone);
1708 free_memory:
1709 	kfree(tz);
1710 end:
1711 	return result;
1712 }
1713 
1714 static int acpi_thermal_remove(struct acpi_device *device, int type)
1715 {
1716 	acpi_status status = AE_OK;
1717 	struct acpi_thermal *tz = NULL;
1718 
1719 
1720 	if (!device || !acpi_driver_data(device))
1721 		return -EINVAL;
1722 
1723 	tz = acpi_driver_data(device);
1724 
1725 	/* avoid timer adding new defer task */
1726 	tz->zombie = 1;
1727 	/* wait for running timer (on other CPUs) finish */
1728 	del_timer_sync(&(tz->timer));
1729 	/* synchronize deferred task */
1730 	acpi_os_wait_events_complete(NULL);
1731 	/* deferred task may reinsert timer */
1732 	del_timer_sync(&(tz->timer));
1733 
1734 	status = acpi_remove_notify_handler(device->handle,
1735 					    ACPI_DEVICE_NOTIFY,
1736 					    acpi_thermal_notify);
1737 
1738 	/* Terminate policy */
1739 	if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
1740 		tz->trips.passive.flags.enabled = 0;
1741 		acpi_thermal_passive(tz);
1742 	}
1743 	if (tz->trips.active[0].flags.valid
1744 	    && tz->trips.active[0].flags.enabled) {
1745 		tz->trips.active[0].flags.enabled = 0;
1746 		acpi_thermal_active(tz);
1747 	}
1748 
1749 	acpi_thermal_remove_fs(device);
1750 	acpi_thermal_unregister_thermal_zone(tz);
1751 	mutex_destroy(&tz->lock);
1752 	kfree(tz);
1753 	return 0;
1754 }
1755 
1756 static int acpi_thermal_resume(struct acpi_device *device)
1757 {
1758 	struct acpi_thermal *tz = NULL;
1759 	int i, j, power_state, result;
1760 
1761 
1762 	if (!device || !acpi_driver_data(device))
1763 		return -EINVAL;
1764 
1765 	tz = acpi_driver_data(device);
1766 
1767 	for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
1768 		if (!(&tz->trips.active[i]))
1769 			break;
1770 		if (!tz->trips.active[i].flags.valid)
1771 			break;
1772 		tz->trips.active[i].flags.enabled = 1;
1773 		for (j = 0; j < tz->trips.active[i].devices.count; j++) {
1774 			result = acpi_bus_get_power(tz->trips.active[i].devices.
1775 			    handles[j], &power_state);
1776 			if (result || (power_state != ACPI_STATE_D0)) {
1777 				tz->trips.active[i].flags.enabled = 0;
1778 				break;
1779 			}
1780 		}
1781 		tz->state.active |= tz->trips.active[i].flags.enabled;
1782 	}
1783 
1784 	acpi_thermal_check(tz);
1785 
1786 	return AE_OK;
1787 }
1788 
1789 static int thermal_act(const struct dmi_system_id *d) {
1790 
1791 	if (act == 0) {
1792 		printk(KERN_NOTICE "ACPI: %s detected: "
1793 			"disabling all active thermal trip points\n", d->ident);
1794 		act = -1;
1795 	}
1796 	return 0;
1797 }
1798 static int thermal_nocrt(const struct dmi_system_id *d) {
1799 
1800 	printk(KERN_NOTICE "ACPI: %s detected: "
1801 		"disabling all critical thermal trip point actions.\n", d->ident);
1802 	nocrt = 1;
1803 	return 0;
1804 }
1805 static int thermal_tzp(const struct dmi_system_id *d) {
1806 
1807 	if (tzp == 0) {
1808 		printk(KERN_NOTICE "ACPI: %s detected: "
1809 			"enabling thermal zone polling\n", d->ident);
1810 		tzp = 300;	/* 300 dS = 30 Seconds */
1811 	}
1812 	return 0;
1813 }
1814 static int thermal_psv(const struct dmi_system_id *d) {
1815 
1816 	if (psv == 0) {
1817 		printk(KERN_NOTICE "ACPI: %s detected: "
1818 			"disabling all passive thermal trip points\n", d->ident);
1819 		psv = -1;
1820 	}
1821 	return 0;
1822 }
1823 
1824 static struct dmi_system_id thermal_dmi_table[] __initdata = {
1825 	/*
1826 	 * Award BIOS on this AOpen makes thermal control almost worthless.
1827 	 * http://bugzilla.kernel.org/show_bug.cgi?id=8842
1828 	 */
1829 	{
1830 	 .callback = thermal_act,
1831 	 .ident = "AOpen i915GMm-HFS",
1832 	 .matches = {
1833 		DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1834 		DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1835 		},
1836 	},
1837 	{
1838 	 .callback = thermal_psv,
1839 	 .ident = "AOpen i915GMm-HFS",
1840 	 .matches = {
1841 		DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1842 		DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1843 		},
1844 	},
1845 	{
1846 	 .callback = thermal_tzp,
1847 	 .ident = "AOpen i915GMm-HFS",
1848 	 .matches = {
1849 		DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
1850 		DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
1851 		},
1852 	},
1853 	{
1854 	 .callback = thermal_nocrt,
1855 	 .ident = "Gigabyte GA-7ZX",
1856 	 .matches = {
1857 		DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
1858 		DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
1859 		},
1860 	},
1861 	{}
1862 };
1863 
1864 static int __init acpi_thermal_init(void)
1865 {
1866 	int result = 0;
1867 
1868 	dmi_check_system(thermal_dmi_table);
1869 
1870 	if (off) {
1871 		printk(KERN_NOTICE "ACPI: thermal control disabled\n");
1872 		return -ENODEV;
1873 	}
1874 	acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
1875 	if (!acpi_thermal_dir)
1876 		return -ENODEV;
1877 
1878 	result = acpi_bus_register_driver(&acpi_thermal_driver);
1879 	if (result < 0) {
1880 		remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1881 		return -ENODEV;
1882 	}
1883 
1884 	return 0;
1885 }
1886 
1887 static void __exit acpi_thermal_exit(void)
1888 {
1889 
1890 	acpi_bus_unregister_driver(&acpi_thermal_driver);
1891 
1892 	remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
1893 
1894 	return;
1895 }
1896 
1897 module_init(acpi_thermal_init);
1898 module_exit(acpi_thermal_exit);
1899