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/init.h> 37 #include <linux/types.h> 38 #include <linux/proc_fs.h> 39 #include <linux/timer.h> 40 #include <linux/jiffies.h> 41 #include <linux/kmod.h> 42 #include <linux/seq_file.h> 43 #include <linux/reboot.h> 44 #include <asm/uaccess.h> 45 46 #include <acpi/acpi_bus.h> 47 #include <acpi/acpi_drivers.h> 48 49 #define ACPI_THERMAL_COMPONENT 0x04000000 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 KELVIN_TO_CELSIUS(t) (long)(((long)t-2732>=0) ? ((long)t-2732+5)/10 : ((long)t-2732-5)/10) 68 #define CELSIUS_TO_KELVIN(t) ((t+273)*10) 69 70 #define _COMPONENT ACPI_THERMAL_COMPONENT 71 ACPI_MODULE_NAME("thermal"); 72 73 MODULE_AUTHOR("Paul Diefenbaugh"); 74 MODULE_DESCRIPTION("ACPI Thermal Zone Driver"); 75 MODULE_LICENSE("GPL"); 76 77 static int tzp; 78 module_param(tzp, int, 0); 79 MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.\n"); 80 81 static int acpi_thermal_add(struct acpi_device *device); 82 static int acpi_thermal_remove(struct acpi_device *device, int type); 83 static int acpi_thermal_resume(struct acpi_device *device); 84 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file); 85 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file); 86 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file); 87 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file); 88 static ssize_t acpi_thermal_write_cooling_mode(struct file *, 89 const char __user *, size_t, 90 loff_t *); 91 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file); 92 static ssize_t acpi_thermal_write_polling(struct file *, const char __user *, 93 size_t, loff_t *); 94 95 static const struct acpi_device_id thermal_device_ids[] = { 96 {ACPI_THERMAL_HID, 0}, 97 {"", 0}, 98 }; 99 MODULE_DEVICE_TABLE(acpi, thermal_device_ids); 100 101 static struct acpi_driver acpi_thermal_driver = { 102 .name = "thermal", 103 .class = ACPI_THERMAL_CLASS, 104 .ids = thermal_device_ids, 105 .ops = { 106 .add = acpi_thermal_add, 107 .remove = acpi_thermal_remove, 108 .resume = acpi_thermal_resume, 109 }, 110 }; 111 112 struct acpi_thermal_state { 113 u8 critical:1; 114 u8 hot:1; 115 u8 passive:1; 116 u8 active:1; 117 u8 reserved:4; 118 int active_index; 119 }; 120 121 struct acpi_thermal_state_flags { 122 u8 valid:1; 123 u8 enabled:1; 124 u8 reserved:6; 125 }; 126 127 struct acpi_thermal_critical { 128 struct acpi_thermal_state_flags flags; 129 unsigned long temperature; 130 }; 131 132 struct acpi_thermal_hot { 133 struct acpi_thermal_state_flags flags; 134 unsigned long temperature; 135 }; 136 137 struct acpi_thermal_passive { 138 struct acpi_thermal_state_flags flags; 139 unsigned long temperature; 140 unsigned long tc1; 141 unsigned long tc2; 142 unsigned long tsp; 143 struct acpi_handle_list devices; 144 }; 145 146 struct acpi_thermal_active { 147 struct acpi_thermal_state_flags flags; 148 unsigned long temperature; 149 struct acpi_handle_list devices; 150 }; 151 152 struct acpi_thermal_trips { 153 struct acpi_thermal_critical critical; 154 struct acpi_thermal_hot hot; 155 struct acpi_thermal_passive passive; 156 struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE]; 157 }; 158 159 struct acpi_thermal_flags { 160 u8 cooling_mode:1; /* _SCP */ 161 u8 devices:1; /* _TZD */ 162 u8 reserved:6; 163 }; 164 165 struct acpi_thermal { 166 struct acpi_device * device; 167 acpi_bus_id name; 168 unsigned long temperature; 169 unsigned long last_temperature; 170 unsigned long polling_frequency; 171 volatile u8 zombie; 172 struct acpi_thermal_flags flags; 173 struct acpi_thermal_state state; 174 struct acpi_thermal_trips trips; 175 struct acpi_handle_list devices; 176 struct timer_list timer; 177 }; 178 179 static const struct file_operations acpi_thermal_state_fops = { 180 .open = acpi_thermal_state_open_fs, 181 .read = seq_read, 182 .llseek = seq_lseek, 183 .release = single_release, 184 }; 185 186 static const struct file_operations acpi_thermal_temp_fops = { 187 .open = acpi_thermal_temp_open_fs, 188 .read = seq_read, 189 .llseek = seq_lseek, 190 .release = single_release, 191 }; 192 193 static const struct file_operations acpi_thermal_trip_fops = { 194 .open = acpi_thermal_trip_open_fs, 195 .read = seq_read, 196 .llseek = seq_lseek, 197 .release = single_release, 198 }; 199 200 static const struct file_operations acpi_thermal_cooling_fops = { 201 .open = acpi_thermal_cooling_open_fs, 202 .read = seq_read, 203 .write = acpi_thermal_write_cooling_mode, 204 .llseek = seq_lseek, 205 .release = single_release, 206 }; 207 208 static const struct file_operations acpi_thermal_polling_fops = { 209 .open = acpi_thermal_polling_open_fs, 210 .read = seq_read, 211 .write = acpi_thermal_write_polling, 212 .llseek = seq_lseek, 213 .release = single_release, 214 }; 215 216 /* -------------------------------------------------------------------------- 217 Thermal Zone Management 218 -------------------------------------------------------------------------- */ 219 220 static int acpi_thermal_get_temperature(struct acpi_thermal *tz) 221 { 222 acpi_status status = AE_OK; 223 224 225 if (!tz) 226 return -EINVAL; 227 228 tz->last_temperature = tz->temperature; 229 230 status = 231 acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tz->temperature); 232 if (ACPI_FAILURE(status)) 233 return -ENODEV; 234 235 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n", 236 tz->temperature)); 237 238 return 0; 239 } 240 241 static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz) 242 { 243 acpi_status status = AE_OK; 244 245 246 if (!tz) 247 return -EINVAL; 248 249 status = 250 acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, 251 &tz->polling_frequency); 252 if (ACPI_FAILURE(status)) 253 return -ENODEV; 254 255 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n", 256 tz->polling_frequency)); 257 258 return 0; 259 } 260 261 static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds) 262 { 263 264 if (!tz) 265 return -EINVAL; 266 267 tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */ 268 269 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 270 "Polling frequency set to %lu seconds\n", 271 tz->polling_frequency/10)); 272 273 return 0; 274 } 275 276 static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode) 277 { 278 acpi_status status = AE_OK; 279 union acpi_object arg0 = { ACPI_TYPE_INTEGER }; 280 struct acpi_object_list arg_list = { 1, &arg0 }; 281 acpi_handle handle = NULL; 282 283 284 if (!tz) 285 return -EINVAL; 286 287 status = acpi_get_handle(tz->device->handle, "_SCP", &handle); 288 if (ACPI_FAILURE(status)) { 289 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n")); 290 return -ENODEV; 291 } 292 293 arg0.integer.value = mode; 294 295 status = acpi_evaluate_object(handle, NULL, &arg_list, NULL); 296 if (ACPI_FAILURE(status)) 297 return -ENODEV; 298 299 return 0; 300 } 301 302 static int acpi_thermal_get_trip_points(struct acpi_thermal *tz) 303 { 304 acpi_status status = AE_OK; 305 int i = 0; 306 307 308 if (!tz) 309 return -EINVAL; 310 311 /* Critical Shutdown (required) */ 312 313 status = acpi_evaluate_integer(tz->device->handle, "_CRT", NULL, 314 &tz->trips.critical.temperature); 315 if (ACPI_FAILURE(status)) { 316 tz->trips.critical.flags.valid = 0; 317 ACPI_EXCEPTION((AE_INFO, status, "No critical threshold")); 318 return -ENODEV; 319 } else { 320 tz->trips.critical.flags.valid = 1; 321 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 322 "Found critical threshold [%lu]\n", 323 tz->trips.critical.temperature)); 324 } 325 326 /* Critical Sleep (optional) */ 327 328 status = 329 acpi_evaluate_integer(tz->device->handle, "_HOT", NULL, 330 &tz->trips.hot.temperature); 331 if (ACPI_FAILURE(status)) { 332 tz->trips.hot.flags.valid = 0; 333 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No hot threshold\n")); 334 } else { 335 tz->trips.hot.flags.valid = 1; 336 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found hot threshold [%lu]\n", 337 tz->trips.hot.temperature)); 338 } 339 340 /* Passive: Processors (optional) */ 341 342 status = 343 acpi_evaluate_integer(tz->device->handle, "_PSV", NULL, 344 &tz->trips.passive.temperature); 345 if (ACPI_FAILURE(status)) { 346 tz->trips.passive.flags.valid = 0; 347 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No passive threshold\n")); 348 } else { 349 tz->trips.passive.flags.valid = 1; 350 351 status = 352 acpi_evaluate_integer(tz->device->handle, "_TC1", NULL, 353 &tz->trips.passive.tc1); 354 if (ACPI_FAILURE(status)) 355 tz->trips.passive.flags.valid = 0; 356 357 status = 358 acpi_evaluate_integer(tz->device->handle, "_TC2", NULL, 359 &tz->trips.passive.tc2); 360 if (ACPI_FAILURE(status)) 361 tz->trips.passive.flags.valid = 0; 362 363 status = 364 acpi_evaluate_integer(tz->device->handle, "_TSP", NULL, 365 &tz->trips.passive.tsp); 366 if (ACPI_FAILURE(status)) 367 tz->trips.passive.flags.valid = 0; 368 369 status = 370 acpi_evaluate_reference(tz->device->handle, "_PSL", NULL, 371 &tz->trips.passive.devices); 372 if (ACPI_FAILURE(status)) 373 tz->trips.passive.flags.valid = 0; 374 375 if (!tz->trips.passive.flags.valid) 376 printk(KERN_WARNING PREFIX "Invalid passive threshold\n"); 377 else 378 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 379 "Found passive threshold [%lu]\n", 380 tz->trips.passive.temperature)); 381 } 382 383 /* Active: Fans, etc. (optional) */ 384 385 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) { 386 387 char name[5] = { '_', 'A', 'C', ('0' + i), '\0' }; 388 389 status = 390 acpi_evaluate_integer(tz->device->handle, name, NULL, 391 &tz->trips.active[i].temperature); 392 if (ACPI_FAILURE(status)) 393 break; 394 395 name[2] = 'L'; 396 status = 397 acpi_evaluate_reference(tz->device->handle, name, NULL, 398 &tz->trips.active[i].devices); 399 if (ACPI_SUCCESS(status)) { 400 tz->trips.active[i].flags.valid = 1; 401 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 402 "Found active threshold [%d]:[%lu]\n", 403 i, tz->trips.active[i].temperature)); 404 } else 405 ACPI_EXCEPTION((AE_INFO, status, 406 "Invalid active threshold [%d]", i)); 407 } 408 409 return 0; 410 } 411 412 static int acpi_thermal_get_devices(struct acpi_thermal *tz) 413 { 414 acpi_status status = AE_OK; 415 416 417 if (!tz) 418 return -EINVAL; 419 420 status = 421 acpi_evaluate_reference(tz->device->handle, "_TZD", NULL, &tz->devices); 422 if (ACPI_FAILURE(status)) 423 return -ENODEV; 424 425 return 0; 426 } 427 428 static int acpi_thermal_critical(struct acpi_thermal *tz) 429 { 430 if (!tz || !tz->trips.critical.flags.valid) 431 return -EINVAL; 432 433 if (tz->temperature >= tz->trips.critical.temperature) { 434 printk(KERN_WARNING PREFIX "Critical trip point\n"); 435 tz->trips.critical.flags.enabled = 1; 436 } else if (tz->trips.critical.flags.enabled) 437 tz->trips.critical.flags.enabled = 0; 438 439 printk(KERN_EMERG 440 "Critical temperature reached (%ld C), shutting down.\n", 441 KELVIN_TO_CELSIUS(tz->temperature)); 442 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL, 443 tz->trips.critical.flags.enabled); 444 445 orderly_poweroff(true); 446 447 return 0; 448 } 449 450 static int acpi_thermal_hot(struct acpi_thermal *tz) 451 { 452 if (!tz || !tz->trips.hot.flags.valid) 453 return -EINVAL; 454 455 if (tz->temperature >= tz->trips.hot.temperature) { 456 printk(KERN_WARNING PREFIX "Hot trip point\n"); 457 tz->trips.hot.flags.enabled = 1; 458 } else if (tz->trips.hot.flags.enabled) 459 tz->trips.hot.flags.enabled = 0; 460 461 acpi_bus_generate_event(tz->device, ACPI_THERMAL_NOTIFY_HOT, 462 tz->trips.hot.flags.enabled); 463 464 /* TBD: Call user-mode "sleep(S4)" function */ 465 466 return 0; 467 } 468 469 static void acpi_thermal_passive(struct acpi_thermal *tz) 470 { 471 int result = 1; 472 struct acpi_thermal_passive *passive = NULL; 473 int trend = 0; 474 int i = 0; 475 476 477 if (!tz || !tz->trips.passive.flags.valid) 478 return; 479 480 passive = &(tz->trips.passive); 481 482 /* 483 * Above Trip? 484 * ----------- 485 * Calculate the thermal trend (using the passive cooling equation) 486 * and modify the performance limit for all passive cooling devices 487 * accordingly. Note that we assume symmetry. 488 */ 489 if (tz->temperature >= passive->temperature) { 490 trend = 491 (passive->tc1 * (tz->temperature - tz->last_temperature)) + 492 (passive->tc2 * (tz->temperature - passive->temperature)); 493 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 494 "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n", 495 trend, passive->tc1, tz->temperature, 496 tz->last_temperature, passive->tc2, 497 tz->temperature, passive->temperature)); 498 passive->flags.enabled = 1; 499 /* Heating up? */ 500 if (trend > 0) 501 for (i = 0; i < passive->devices.count; i++) 502 acpi_processor_set_thermal_limit(passive-> 503 devices. 504 handles[i], 505 ACPI_PROCESSOR_LIMIT_INCREMENT); 506 /* Cooling off? */ 507 else if (trend < 0) { 508 for (i = 0; i < passive->devices.count; i++) 509 /* 510 * assume that we are on highest 511 * freq/lowest thrott and can leave 512 * passive mode, even in error case 513 */ 514 if (!acpi_processor_set_thermal_limit 515 (passive->devices.handles[i], 516 ACPI_PROCESSOR_LIMIT_DECREMENT)) 517 result = 0; 518 /* 519 * Leave cooling mode, even if the temp might 520 * higher than trip point This is because some 521 * machines might have long thermal polling 522 * frequencies (tsp) defined. We will fall back 523 * into passive mode in next cycle (probably quicker) 524 */ 525 if (result) { 526 passive->flags.enabled = 0; 527 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 528 "Disabling passive cooling, still above threshold," 529 " but we are cooling down\n")); 530 } 531 } 532 return; 533 } 534 535 /* 536 * Below Trip? 537 * ----------- 538 * Implement passive cooling hysteresis to slowly increase performance 539 * and avoid thrashing around the passive trip point. Note that we 540 * assume symmetry. 541 */ 542 if (!passive->flags.enabled) 543 return; 544 for (i = 0; i < passive->devices.count; i++) 545 if (!acpi_processor_set_thermal_limit 546 (passive->devices.handles[i], 547 ACPI_PROCESSOR_LIMIT_DECREMENT)) 548 result = 0; 549 if (result) { 550 passive->flags.enabled = 0; 551 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 552 "Disabling passive cooling (zone is cool)\n")); 553 } 554 } 555 556 static void acpi_thermal_active(struct acpi_thermal *tz) 557 { 558 int result = 0; 559 struct acpi_thermal_active *active = NULL; 560 int i = 0; 561 int j = 0; 562 unsigned long maxtemp = 0; 563 564 565 if (!tz) 566 return; 567 568 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) { 569 active = &(tz->trips.active[i]); 570 if (!active || !active->flags.valid) 571 break; 572 if (tz->temperature >= active->temperature) { 573 /* 574 * Above Threshold? 575 * ---------------- 576 * If not already enabled, turn ON all cooling devices 577 * associated with this active threshold. 578 */ 579 if (active->temperature > maxtemp) 580 tz->state.active_index = i; 581 maxtemp = active->temperature; 582 if (active->flags.enabled) 583 continue; 584 for (j = 0; j < active->devices.count; j++) { 585 result = 586 acpi_bus_set_power(active->devices. 587 handles[j], 588 ACPI_STATE_D0); 589 if (result) { 590 printk(KERN_WARNING PREFIX 591 "Unable to turn cooling device [%p] 'on'\n", 592 active->devices. 593 handles[j]); 594 continue; 595 } 596 active->flags.enabled = 1; 597 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 598 "Cooling device [%p] now 'on'\n", 599 active->devices.handles[j])); 600 } 601 continue; 602 } 603 if (!active->flags.enabled) 604 continue; 605 /* 606 * Below Threshold? 607 * ---------------- 608 * Turn OFF all cooling devices associated with this 609 * threshold. 610 */ 611 for (j = 0; j < active->devices.count; j++) { 612 result = acpi_bus_set_power(active->devices.handles[j], 613 ACPI_STATE_D3); 614 if (result) { 615 printk(KERN_WARNING PREFIX 616 "Unable to turn cooling device [%p] 'off'\n", 617 active->devices.handles[j]); 618 continue; 619 } 620 active->flags.enabled = 0; 621 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 622 "Cooling device [%p] now 'off'\n", 623 active->devices.handles[j])); 624 } 625 } 626 } 627 628 static void acpi_thermal_check(void *context); 629 630 static void acpi_thermal_run(unsigned long data) 631 { 632 struct acpi_thermal *tz = (struct acpi_thermal *)data; 633 if (!tz->zombie) 634 acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data); 635 } 636 637 static void acpi_thermal_check(void *data) 638 { 639 int result = 0; 640 struct acpi_thermal *tz = data; 641 unsigned long sleep_time = 0; 642 int i = 0; 643 struct acpi_thermal_state state; 644 645 646 if (!tz) { 647 printk(KERN_ERR PREFIX "Invalid (NULL) context\n"); 648 return; 649 } 650 651 state = tz->state; 652 653 result = acpi_thermal_get_temperature(tz); 654 if (result) 655 return; 656 657 memset(&tz->state, 0, sizeof(tz->state)); 658 659 /* 660 * Check Trip Points 661 * ----------------- 662 * Compare the current temperature to the trip point values to see 663 * if we've entered one of the thermal policy states. Note that 664 * this function determines when a state is entered, but the 665 * individual policy decides when it is exited (e.g. hysteresis). 666 */ 667 if (tz->trips.critical.flags.valid) 668 state.critical |= 669 (tz->temperature >= tz->trips.critical.temperature); 670 if (tz->trips.hot.flags.valid) 671 state.hot |= (tz->temperature >= tz->trips.hot.temperature); 672 if (tz->trips.passive.flags.valid) 673 state.passive |= 674 (tz->temperature >= tz->trips.passive.temperature); 675 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) 676 if (tz->trips.active[i].flags.valid) 677 state.active |= 678 (tz->temperature >= 679 tz->trips.active[i].temperature); 680 681 /* 682 * Invoke Policy 683 * ------------- 684 * Separated from the above check to allow individual policy to 685 * determine when to exit a given state. 686 */ 687 if (state.critical) 688 acpi_thermal_critical(tz); 689 if (state.hot) 690 acpi_thermal_hot(tz); 691 if (state.passive) 692 acpi_thermal_passive(tz); 693 if (state.active) 694 acpi_thermal_active(tz); 695 696 /* 697 * Calculate State 698 * --------------- 699 * Again, separated from the above two to allow independent policy 700 * decisions. 701 */ 702 tz->state.critical = tz->trips.critical.flags.enabled; 703 tz->state.hot = tz->trips.hot.flags.enabled; 704 tz->state.passive = tz->trips.passive.flags.enabled; 705 tz->state.active = 0; 706 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) 707 tz->state.active |= tz->trips.active[i].flags.enabled; 708 709 /* 710 * Calculate Sleep Time 711 * -------------------- 712 * If we're in the passive state, use _TSP's value. Otherwise 713 * use the default polling frequency (e.g. _TZP). If no polling 714 * frequency is specified then we'll wait forever (at least until 715 * a thermal event occurs). Note that _TSP and _TZD values are 716 * given in 1/10th seconds (we must covert to milliseconds). 717 */ 718 if (tz->state.passive) 719 sleep_time = tz->trips.passive.tsp * 100; 720 else if (tz->polling_frequency > 0) 721 sleep_time = tz->polling_frequency * 100; 722 723 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n", 724 tz->name, tz->temperature, sleep_time)); 725 726 /* 727 * Schedule Next Poll 728 * ------------------ 729 */ 730 if (!sleep_time) { 731 if (timer_pending(&(tz->timer))) 732 del_timer(&(tz->timer)); 733 } else { 734 if (timer_pending(&(tz->timer))) 735 mod_timer(&(tz->timer), 736 jiffies + (HZ * sleep_time) / 1000); 737 else { 738 tz->timer.data = (unsigned long)tz; 739 tz->timer.function = acpi_thermal_run; 740 tz->timer.expires = jiffies + (HZ * sleep_time) / 1000; 741 add_timer(&(tz->timer)); 742 } 743 } 744 745 return; 746 } 747 748 /* -------------------------------------------------------------------------- 749 FS Interface (/proc) 750 -------------------------------------------------------------------------- */ 751 752 static struct proc_dir_entry *acpi_thermal_dir; 753 754 static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset) 755 { 756 struct acpi_thermal *tz = seq->private; 757 758 759 if (!tz) 760 goto end; 761 762 seq_puts(seq, "state: "); 763 764 if (!tz->state.critical && !tz->state.hot && !tz->state.passive 765 && !tz->state.active) 766 seq_puts(seq, "ok\n"); 767 else { 768 if (tz->state.critical) 769 seq_puts(seq, "critical "); 770 if (tz->state.hot) 771 seq_puts(seq, "hot "); 772 if (tz->state.passive) 773 seq_puts(seq, "passive "); 774 if (tz->state.active) 775 seq_printf(seq, "active[%d]", tz->state.active_index); 776 seq_puts(seq, "\n"); 777 } 778 779 end: 780 return 0; 781 } 782 783 static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file) 784 { 785 return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data); 786 } 787 788 static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset) 789 { 790 int result = 0; 791 struct acpi_thermal *tz = seq->private; 792 793 794 if (!tz) 795 goto end; 796 797 result = acpi_thermal_get_temperature(tz); 798 if (result) 799 goto end; 800 801 seq_printf(seq, "temperature: %ld C\n", 802 KELVIN_TO_CELSIUS(tz->temperature)); 803 804 end: 805 return 0; 806 } 807 808 static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file) 809 { 810 return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data); 811 } 812 813 static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset) 814 { 815 struct acpi_thermal *tz = seq->private; 816 struct acpi_device *device; 817 acpi_status status; 818 819 int i = 0; 820 int j = 0; 821 822 823 if (!tz) 824 goto end; 825 826 if (tz->trips.critical.flags.valid) 827 seq_printf(seq, "critical (S5): %ld C\n", 828 KELVIN_TO_CELSIUS(tz->trips.critical.temperature)); 829 830 if (tz->trips.hot.flags.valid) 831 seq_printf(seq, "hot (S4): %ld C\n", 832 KELVIN_TO_CELSIUS(tz->trips.hot.temperature)); 833 834 if (tz->trips.passive.flags.valid) { 835 seq_printf(seq, 836 "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=", 837 KELVIN_TO_CELSIUS(tz->trips.passive.temperature), 838 tz->trips.passive.tc1, tz->trips.passive.tc2, 839 tz->trips.passive.tsp); 840 for (j = 0; j < tz->trips.passive.devices.count; j++) { 841 status = acpi_bus_get_device(tz->trips.passive.devices. 842 handles[j], &device); 843 seq_printf(seq, "%4.4s ", status ? "" : 844 acpi_device_bid(device)); 845 } 846 seq_puts(seq, "\n"); 847 } 848 849 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) { 850 if (!(tz->trips.active[i].flags.valid)) 851 break; 852 seq_printf(seq, "active[%d]: %ld C: devices=", 853 i, 854 KELVIN_TO_CELSIUS(tz->trips.active[i].temperature)); 855 for (j = 0; j < tz->trips.active[i].devices.count; j++){ 856 status = acpi_bus_get_device(tz->trips.active[i]. 857 devices.handles[j], 858 &device); 859 seq_printf(seq, "%4.4s ", status ? "" : 860 acpi_device_bid(device)); 861 } 862 seq_puts(seq, "\n"); 863 } 864 865 end: 866 return 0; 867 } 868 869 static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file) 870 { 871 return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data); 872 } 873 874 static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset) 875 { 876 struct acpi_thermal *tz = seq->private; 877 878 879 if (!tz) 880 goto end; 881 882 if (!tz->flags.cooling_mode) 883 seq_puts(seq, "<setting not supported>\n"); 884 else 885 seq_puts(seq, "0 - Active; 1 - Passive\n"); 886 887 end: 888 return 0; 889 } 890 891 static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file) 892 { 893 return single_open(file, acpi_thermal_cooling_seq_show, 894 PDE(inode)->data); 895 } 896 897 static ssize_t 898 acpi_thermal_write_cooling_mode(struct file *file, 899 const char __user * buffer, 900 size_t count, loff_t * ppos) 901 { 902 struct seq_file *m = file->private_data; 903 struct acpi_thermal *tz = m->private; 904 int result = 0; 905 char mode_string[12] = { '\0' }; 906 907 908 if (!tz || (count > sizeof(mode_string) - 1)) 909 return -EINVAL; 910 911 if (!tz->flags.cooling_mode) 912 return -ENODEV; 913 914 if (copy_from_user(mode_string, buffer, count)) 915 return -EFAULT; 916 917 mode_string[count] = '\0'; 918 919 result = acpi_thermal_set_cooling_mode(tz, 920 simple_strtoul(mode_string, NULL, 921 0)); 922 if (result) 923 return result; 924 925 acpi_thermal_check(tz); 926 927 return count; 928 } 929 930 static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset) 931 { 932 struct acpi_thermal *tz = seq->private; 933 934 935 if (!tz) 936 goto end; 937 938 if (!tz->polling_frequency) { 939 seq_puts(seq, "<polling disabled>\n"); 940 goto end; 941 } 942 943 seq_printf(seq, "polling frequency: %lu seconds\n", 944 (tz->polling_frequency / 10)); 945 946 end: 947 return 0; 948 } 949 950 static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file) 951 { 952 return single_open(file, acpi_thermal_polling_seq_show, 953 PDE(inode)->data); 954 } 955 956 static ssize_t 957 acpi_thermal_write_polling(struct file *file, 958 const char __user * buffer, 959 size_t count, loff_t * ppos) 960 { 961 struct seq_file *m = file->private_data; 962 struct acpi_thermal *tz = m->private; 963 int result = 0; 964 char polling_string[12] = { '\0' }; 965 int seconds = 0; 966 967 968 if (!tz || (count > sizeof(polling_string) - 1)) 969 return -EINVAL; 970 971 if (copy_from_user(polling_string, buffer, count)) 972 return -EFAULT; 973 974 polling_string[count] = '\0'; 975 976 seconds = simple_strtoul(polling_string, NULL, 0); 977 978 result = acpi_thermal_set_polling(tz, seconds); 979 if (result) 980 return result; 981 982 acpi_thermal_check(tz); 983 984 return count; 985 } 986 987 static int acpi_thermal_add_fs(struct acpi_device *device) 988 { 989 struct proc_dir_entry *entry = NULL; 990 991 992 if (!acpi_device_dir(device)) { 993 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), 994 acpi_thermal_dir); 995 if (!acpi_device_dir(device)) 996 return -ENODEV; 997 acpi_device_dir(device)->owner = THIS_MODULE; 998 } 999 1000 /* 'state' [R] */ 1001 entry = create_proc_entry(ACPI_THERMAL_FILE_STATE, 1002 S_IRUGO, acpi_device_dir(device)); 1003 if (!entry) 1004 return -ENODEV; 1005 else { 1006 entry->proc_fops = &acpi_thermal_state_fops; 1007 entry->data = acpi_driver_data(device); 1008 entry->owner = THIS_MODULE; 1009 } 1010 1011 /* 'temperature' [R] */ 1012 entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE, 1013 S_IRUGO, acpi_device_dir(device)); 1014 if (!entry) 1015 return -ENODEV; 1016 else { 1017 entry->proc_fops = &acpi_thermal_temp_fops; 1018 entry->data = acpi_driver_data(device); 1019 entry->owner = THIS_MODULE; 1020 } 1021 1022 /* 'trip_points' [R/W] */ 1023 entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS, 1024 S_IFREG | S_IRUGO | S_IWUSR, 1025 acpi_device_dir(device)); 1026 if (!entry) 1027 return -ENODEV; 1028 else { 1029 entry->proc_fops = &acpi_thermal_trip_fops; 1030 entry->data = acpi_driver_data(device); 1031 entry->owner = THIS_MODULE; 1032 } 1033 1034 /* 'cooling_mode' [R/W] */ 1035 entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE, 1036 S_IFREG | S_IRUGO | S_IWUSR, 1037 acpi_device_dir(device)); 1038 if (!entry) 1039 return -ENODEV; 1040 else { 1041 entry->proc_fops = &acpi_thermal_cooling_fops; 1042 entry->data = acpi_driver_data(device); 1043 entry->owner = THIS_MODULE; 1044 } 1045 1046 /* 'polling_frequency' [R/W] */ 1047 entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ, 1048 S_IFREG | S_IRUGO | S_IWUSR, 1049 acpi_device_dir(device)); 1050 if (!entry) 1051 return -ENODEV; 1052 else { 1053 entry->proc_fops = &acpi_thermal_polling_fops; 1054 entry->data = acpi_driver_data(device); 1055 entry->owner = THIS_MODULE; 1056 } 1057 1058 return 0; 1059 } 1060 1061 static int acpi_thermal_remove_fs(struct acpi_device *device) 1062 { 1063 1064 if (acpi_device_dir(device)) { 1065 remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ, 1066 acpi_device_dir(device)); 1067 remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE, 1068 acpi_device_dir(device)); 1069 remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS, 1070 acpi_device_dir(device)); 1071 remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE, 1072 acpi_device_dir(device)); 1073 remove_proc_entry(ACPI_THERMAL_FILE_STATE, 1074 acpi_device_dir(device)); 1075 remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir); 1076 acpi_device_dir(device) = NULL; 1077 } 1078 1079 return 0; 1080 } 1081 1082 /* -------------------------------------------------------------------------- 1083 Driver Interface 1084 -------------------------------------------------------------------------- */ 1085 1086 static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data) 1087 { 1088 struct acpi_thermal *tz = data; 1089 struct acpi_device *device = NULL; 1090 1091 1092 if (!tz) 1093 return; 1094 1095 device = tz->device; 1096 1097 switch (event) { 1098 case ACPI_THERMAL_NOTIFY_TEMPERATURE: 1099 acpi_thermal_check(tz); 1100 break; 1101 case ACPI_THERMAL_NOTIFY_THRESHOLDS: 1102 acpi_thermal_get_trip_points(tz); 1103 acpi_thermal_check(tz); 1104 acpi_bus_generate_event(device, event, 0); 1105 break; 1106 case ACPI_THERMAL_NOTIFY_DEVICES: 1107 if (tz->flags.devices) 1108 acpi_thermal_get_devices(tz); 1109 acpi_bus_generate_event(device, event, 0); 1110 break; 1111 default: 1112 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 1113 "Unsupported event [0x%x]\n", event)); 1114 break; 1115 } 1116 1117 return; 1118 } 1119 1120 static int acpi_thermal_get_info(struct acpi_thermal *tz) 1121 { 1122 int result = 0; 1123 1124 1125 if (!tz) 1126 return -EINVAL; 1127 1128 /* Get temperature [_TMP] (required) */ 1129 result = acpi_thermal_get_temperature(tz); 1130 if (result) 1131 return result; 1132 1133 /* Get trip points [_CRT, _PSV, etc.] (required) */ 1134 result = acpi_thermal_get_trip_points(tz); 1135 if (result) 1136 return result; 1137 1138 /* Set the cooling mode [_SCP] to active cooling (default) */ 1139 result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE); 1140 if (!result) 1141 tz->flags.cooling_mode = 1; 1142 1143 /* Get default polling frequency [_TZP] (optional) */ 1144 if (tzp) 1145 tz->polling_frequency = tzp; 1146 else 1147 acpi_thermal_get_polling_frequency(tz); 1148 1149 /* Get devices in this thermal zone [_TZD] (optional) */ 1150 result = acpi_thermal_get_devices(tz); 1151 if (!result) 1152 tz->flags.devices = 1; 1153 1154 return 0; 1155 } 1156 1157 static int acpi_thermal_add(struct acpi_device *device) 1158 { 1159 int result = 0; 1160 acpi_status status = AE_OK; 1161 struct acpi_thermal *tz = NULL; 1162 1163 1164 if (!device) 1165 return -EINVAL; 1166 1167 tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL); 1168 if (!tz) 1169 return -ENOMEM; 1170 1171 tz->device = device; 1172 strcpy(tz->name, device->pnp.bus_id); 1173 strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME); 1174 strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS); 1175 acpi_driver_data(device) = tz; 1176 1177 result = acpi_thermal_get_info(tz); 1178 if (result) 1179 goto end; 1180 1181 result = acpi_thermal_add_fs(device); 1182 if (result) 1183 goto end; 1184 1185 init_timer(&tz->timer); 1186 1187 acpi_thermal_check(tz); 1188 1189 status = acpi_install_notify_handler(device->handle, 1190 ACPI_DEVICE_NOTIFY, 1191 acpi_thermal_notify, tz); 1192 if (ACPI_FAILURE(status)) { 1193 result = -ENODEV; 1194 goto end; 1195 } 1196 1197 printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n", 1198 acpi_device_name(device), acpi_device_bid(device), 1199 KELVIN_TO_CELSIUS(tz->temperature)); 1200 1201 end: 1202 if (result) { 1203 acpi_thermal_remove_fs(device); 1204 kfree(tz); 1205 } 1206 1207 return result; 1208 } 1209 1210 static int acpi_thermal_remove(struct acpi_device *device, int type) 1211 { 1212 acpi_status status = AE_OK; 1213 struct acpi_thermal *tz = NULL; 1214 1215 1216 if (!device || !acpi_driver_data(device)) 1217 return -EINVAL; 1218 1219 tz = acpi_driver_data(device); 1220 1221 /* avoid timer adding new defer task */ 1222 tz->zombie = 1; 1223 /* wait for running timer (on other CPUs) finish */ 1224 del_timer_sync(&(tz->timer)); 1225 /* synchronize deferred task */ 1226 acpi_os_wait_events_complete(NULL); 1227 /* deferred task may reinsert timer */ 1228 del_timer_sync(&(tz->timer)); 1229 1230 status = acpi_remove_notify_handler(device->handle, 1231 ACPI_DEVICE_NOTIFY, 1232 acpi_thermal_notify); 1233 1234 /* Terminate policy */ 1235 if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) { 1236 tz->trips.passive.flags.enabled = 0; 1237 acpi_thermal_passive(tz); 1238 } 1239 if (tz->trips.active[0].flags.valid 1240 && tz->trips.active[0].flags.enabled) { 1241 tz->trips.active[0].flags.enabled = 0; 1242 acpi_thermal_active(tz); 1243 } 1244 1245 acpi_thermal_remove_fs(device); 1246 1247 kfree(tz); 1248 return 0; 1249 } 1250 1251 static int acpi_thermal_resume(struct acpi_device *device) 1252 { 1253 struct acpi_thermal *tz = NULL; 1254 int i, j, power_state, result; 1255 1256 1257 if (!device || !acpi_driver_data(device)) 1258 return -EINVAL; 1259 1260 tz = acpi_driver_data(device); 1261 1262 for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) { 1263 if (!(&tz->trips.active[i])) 1264 break; 1265 if (!tz->trips.active[i].flags.valid) 1266 break; 1267 tz->trips.active[i].flags.enabled = 1; 1268 for (j = 0; j < tz->trips.active[i].devices.count; j++) { 1269 result = acpi_bus_get_power(tz->trips.active[i].devices. 1270 handles[j], &power_state); 1271 if (result || (power_state != ACPI_STATE_D0)) { 1272 tz->trips.active[i].flags.enabled = 0; 1273 break; 1274 } 1275 } 1276 tz->state.active |= tz->trips.active[i].flags.enabled; 1277 } 1278 1279 acpi_thermal_check(tz); 1280 1281 return AE_OK; 1282 } 1283 1284 static int __init acpi_thermal_init(void) 1285 { 1286 int result = 0; 1287 1288 1289 acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir); 1290 if (!acpi_thermal_dir) 1291 return -ENODEV; 1292 acpi_thermal_dir->owner = THIS_MODULE; 1293 1294 result = acpi_bus_register_driver(&acpi_thermal_driver); 1295 if (result < 0) { 1296 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir); 1297 return -ENODEV; 1298 } 1299 1300 return 0; 1301 } 1302 1303 static void __exit acpi_thermal_exit(void) 1304 { 1305 1306 acpi_bus_unregister_driver(&acpi_thermal_driver); 1307 1308 remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir); 1309 1310 return; 1311 } 1312 1313 module_init(acpi_thermal_init); 1314 module_exit(acpi_thermal_exit); 1315