thermal.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/dmi.h>
  36. #include <linux/init.h>
  37. #include <linux/slab.h>
  38. #include <linux/types.h>
  39. #include <linux/jiffies.h>
  40. #include <linux/kmod.h>
  41. #include <linux/reboot.h>
  42. #include <linux/device.h>
  43. #include <asm/uaccess.h>
  44. #include <linux/thermal.h>
  45. #include <acpi/acpi_bus.h>
  46. #include <acpi/acpi_drivers.h>
  47. #define PREFIX "ACPI: "
  48. #define ACPI_THERMAL_CLASS "thermal_zone"
  49. #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
  50. #define ACPI_THERMAL_FILE_STATE "state"
  51. #define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
  52. #define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
  53. #define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
  54. #define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
  55. #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
  56. #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
  57. #define ACPI_THERMAL_NOTIFY_DEVICES 0x82
  58. #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
  59. #define ACPI_THERMAL_NOTIFY_HOT 0xF1
  60. #define ACPI_THERMAL_MODE_ACTIVE 0x00
  61. #define ACPI_THERMAL_MAX_ACTIVE 10
  62. #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
  63. #define _COMPONENT ACPI_THERMAL_COMPONENT
  64. ACPI_MODULE_NAME("thermal");
  65. MODULE_AUTHOR("Paul Diefenbaugh");
  66. MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
  67. MODULE_LICENSE("GPL");
  68. static int act;
  69. module_param(act, int, 0644);
  70. MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
  71. static int crt;
  72. module_param(crt, int, 0644);
  73. MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
  74. static int tzp;
  75. module_param(tzp, int, 0444);
  76. MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
  77. static int nocrt;
  78. module_param(nocrt, int, 0);
  79. MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
  80. static int off;
  81. module_param(off, int, 0);
  82. MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
  83. static int psv;
  84. module_param(psv, int, 0644);
  85. MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
  86. static int acpi_thermal_add(struct acpi_device *device);
  87. static int acpi_thermal_remove(struct acpi_device *device, int type);
  88. static void acpi_thermal_notify(struct acpi_device *device, u32 event);
  89. static const struct acpi_device_id thermal_device_ids[] = {
  90. {ACPI_THERMAL_HID, 0},
  91. {"", 0},
  92. };
  93. MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
  94. static int acpi_thermal_resume(struct device *dev);
  95. static SIMPLE_DEV_PM_OPS(acpi_thermal_pm, NULL, acpi_thermal_resume);
  96. static struct acpi_driver acpi_thermal_driver = {
  97. .name = "thermal",
  98. .class = ACPI_THERMAL_CLASS,
  99. .ids = thermal_device_ids,
  100. .ops = {
  101. .add = acpi_thermal_add,
  102. .remove = acpi_thermal_remove,
  103. .notify = acpi_thermal_notify,
  104. },
  105. .drv.pm = &acpi_thermal_pm,
  106. };
  107. struct acpi_thermal_state {
  108. u8 critical:1;
  109. u8 hot:1;
  110. u8 passive:1;
  111. u8 active:1;
  112. u8 reserved:4;
  113. int active_index;
  114. };
  115. struct acpi_thermal_state_flags {
  116. u8 valid:1;
  117. u8 enabled:1;
  118. u8 reserved:6;
  119. };
  120. struct acpi_thermal_critical {
  121. struct acpi_thermal_state_flags flags;
  122. unsigned long temperature;
  123. };
  124. struct acpi_thermal_hot {
  125. struct acpi_thermal_state_flags flags;
  126. unsigned long temperature;
  127. };
  128. struct acpi_thermal_passive {
  129. struct acpi_thermal_state_flags flags;
  130. unsigned long temperature;
  131. unsigned long tc1;
  132. unsigned long tc2;
  133. unsigned long tsp;
  134. struct acpi_handle_list devices;
  135. };
  136. struct acpi_thermal_active {
  137. struct acpi_thermal_state_flags flags;
  138. unsigned long temperature;
  139. struct acpi_handle_list devices;
  140. };
  141. struct acpi_thermal_trips {
  142. struct acpi_thermal_critical critical;
  143. struct acpi_thermal_hot hot;
  144. struct acpi_thermal_passive passive;
  145. struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
  146. };
  147. struct acpi_thermal_flags {
  148. u8 cooling_mode:1; /* _SCP */
  149. u8 devices:1; /* _TZD */
  150. u8 reserved:6;
  151. };
  152. struct acpi_thermal {
  153. struct acpi_device * device;
  154. acpi_bus_id name;
  155. unsigned long temperature;
  156. unsigned long last_temperature;
  157. unsigned long polling_frequency;
  158. volatile u8 zombie;
  159. struct acpi_thermal_flags flags;
  160. struct acpi_thermal_state state;
  161. struct acpi_thermal_trips trips;
  162. struct acpi_handle_list devices;
  163. struct thermal_zone_device *thermal_zone;
  164. int tz_enabled;
  165. int kelvin_offset;
  166. struct mutex lock;
  167. };
  168. /* --------------------------------------------------------------------------
  169. Thermal Zone Management
  170. -------------------------------------------------------------------------- */
  171. static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
  172. {
  173. acpi_status status = AE_OK;
  174. unsigned long long tmp;
  175. if (!tz)
  176. return -EINVAL;
  177. tz->last_temperature = tz->temperature;
  178. status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
  179. if (ACPI_FAILURE(status))
  180. return -ENODEV;
  181. tz->temperature = tmp;
  182. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
  183. tz->temperature));
  184. return 0;
  185. }
  186. static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
  187. {
  188. acpi_status status = AE_OK;
  189. unsigned long long tmp;
  190. if (!tz)
  191. return -EINVAL;
  192. status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
  193. if (ACPI_FAILURE(status))
  194. return -ENODEV;
  195. tz->polling_frequency = tmp;
  196. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
  197. tz->polling_frequency));
  198. return 0;
  199. }
  200. static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
  201. {
  202. acpi_status status = AE_OK;
  203. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  204. struct acpi_object_list arg_list = { 1, &arg0 };
  205. acpi_handle handle = NULL;
  206. if (!tz)
  207. return -EINVAL;
  208. status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
  209. if (ACPI_FAILURE(status)) {
  210. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
  211. return -ENODEV;
  212. }
  213. arg0.integer.value = mode;
  214. status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
  215. if (ACPI_FAILURE(status))
  216. return -ENODEV;
  217. return 0;
  218. }
  219. #define ACPI_TRIPS_CRITICAL 0x01
  220. #define ACPI_TRIPS_HOT 0x02
  221. #define ACPI_TRIPS_PASSIVE 0x04
  222. #define ACPI_TRIPS_ACTIVE 0x08
  223. #define ACPI_TRIPS_DEVICES 0x10
  224. #define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
  225. #define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
  226. #define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
  227. ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
  228. ACPI_TRIPS_DEVICES)
  229. /*
  230. * This exception is thrown out in two cases:
  231. * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
  232. * when re-evaluating the AML code.
  233. * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
  234. * We need to re-bind the cooling devices of a thermal zone when this occurs.
  235. */
  236. #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
  237. do { \
  238. if (flags != ACPI_TRIPS_INIT) \
  239. ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
  240. "ACPI thermal trip point %s changed\n" \
  241. "Please send acpidump to linux-acpi@vger.kernel.org\n", str)); \
  242. } while (0)
  243. static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
  244. {
  245. acpi_status status = AE_OK;
  246. unsigned long long tmp;
  247. struct acpi_handle_list devices;
  248. int valid = 0;
  249. int i;
  250. /* Critical Shutdown */
  251. if (flag & ACPI_TRIPS_CRITICAL) {
  252. status = acpi_evaluate_integer(tz->device->handle,
  253. "_CRT", NULL, &tmp);
  254. tz->trips.critical.temperature = tmp;
  255. /*
  256. * Treat freezing temperatures as invalid as well; some
  257. * BIOSes return really low values and cause reboots at startup.
  258. * Below zero (Celsius) values clearly aren't right for sure..
  259. * ... so lets discard those as invalid.
  260. */
  261. if (ACPI_FAILURE(status)) {
  262. tz->trips.critical.flags.valid = 0;
  263. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  264. "No critical threshold\n"));
  265. } else if (tmp <= 2732) {
  266. printk(KERN_WARNING FW_BUG "Invalid critical threshold "
  267. "(%llu)\n", tmp);
  268. tz->trips.critical.flags.valid = 0;
  269. } else {
  270. tz->trips.critical.flags.valid = 1;
  271. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  272. "Found critical threshold [%lu]\n",
  273. tz->trips.critical.temperature));
  274. }
  275. if (tz->trips.critical.flags.valid == 1) {
  276. if (crt == -1) {
  277. tz->trips.critical.flags.valid = 0;
  278. } else if (crt > 0) {
  279. unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
  280. /*
  281. * Allow override critical threshold
  282. */
  283. if (crt_k > tz->trips.critical.temperature)
  284. printk(KERN_WARNING PREFIX
  285. "Critical threshold %d C\n", crt);
  286. tz->trips.critical.temperature = crt_k;
  287. }
  288. }
  289. }
  290. /* Critical Sleep (optional) */
  291. if (flag & ACPI_TRIPS_HOT) {
  292. status = acpi_evaluate_integer(tz->device->handle,
  293. "_HOT", NULL, &tmp);
  294. if (ACPI_FAILURE(status)) {
  295. tz->trips.hot.flags.valid = 0;
  296. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  297. "No hot threshold\n"));
  298. } else {
  299. tz->trips.hot.temperature = tmp;
  300. tz->trips.hot.flags.valid = 1;
  301. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  302. "Found hot threshold [%lu]\n",
  303. tz->trips.critical.temperature));
  304. }
  305. }
  306. /* Passive (optional) */
  307. if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
  308. (flag == ACPI_TRIPS_INIT)) {
  309. valid = tz->trips.passive.flags.valid;
  310. if (psv == -1) {
  311. status = AE_SUPPORT;
  312. } else if (psv > 0) {
  313. tmp = CELSIUS_TO_KELVIN(psv);
  314. status = AE_OK;
  315. } else {
  316. status = acpi_evaluate_integer(tz->device->handle,
  317. "_PSV", NULL, &tmp);
  318. }
  319. if (ACPI_FAILURE(status))
  320. tz->trips.passive.flags.valid = 0;
  321. else {
  322. tz->trips.passive.temperature = tmp;
  323. tz->trips.passive.flags.valid = 1;
  324. if (flag == ACPI_TRIPS_INIT) {
  325. status = acpi_evaluate_integer(
  326. tz->device->handle, "_TC1",
  327. NULL, &tmp);
  328. if (ACPI_FAILURE(status))
  329. tz->trips.passive.flags.valid = 0;
  330. else
  331. tz->trips.passive.tc1 = tmp;
  332. status = acpi_evaluate_integer(
  333. tz->device->handle, "_TC2",
  334. NULL, &tmp);
  335. if (ACPI_FAILURE(status))
  336. tz->trips.passive.flags.valid = 0;
  337. else
  338. tz->trips.passive.tc2 = tmp;
  339. status = acpi_evaluate_integer(
  340. tz->device->handle, "_TSP",
  341. NULL, &tmp);
  342. if (ACPI_FAILURE(status))
  343. tz->trips.passive.flags.valid = 0;
  344. else
  345. tz->trips.passive.tsp = tmp;
  346. }
  347. }
  348. }
  349. if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
  350. memset(&devices, 0, sizeof(struct acpi_handle_list));
  351. status = acpi_evaluate_reference(tz->device->handle, "_PSL",
  352. NULL, &devices);
  353. if (ACPI_FAILURE(status)) {
  354. printk(KERN_WARNING PREFIX
  355. "Invalid passive threshold\n");
  356. tz->trips.passive.flags.valid = 0;
  357. }
  358. else
  359. tz->trips.passive.flags.valid = 1;
  360. if (memcmp(&tz->trips.passive.devices, &devices,
  361. sizeof(struct acpi_handle_list))) {
  362. memcpy(&tz->trips.passive.devices, &devices,
  363. sizeof(struct acpi_handle_list));
  364. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
  365. }
  366. }
  367. if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
  368. if (valid != tz->trips.passive.flags.valid)
  369. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
  370. }
  371. /* Active (optional) */
  372. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  373. char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
  374. valid = tz->trips.active[i].flags.valid;
  375. if (act == -1)
  376. break; /* disable all active trip points */
  377. if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
  378. tz->trips.active[i].flags.valid)) {
  379. status = acpi_evaluate_integer(tz->device->handle,
  380. name, NULL, &tmp);
  381. if (ACPI_FAILURE(status)) {
  382. tz->trips.active[i].flags.valid = 0;
  383. if (i == 0)
  384. break;
  385. if (act <= 0)
  386. break;
  387. if (i == 1)
  388. tz->trips.active[0].temperature =
  389. CELSIUS_TO_KELVIN(act);
  390. else
  391. /*
  392. * Don't allow override higher than
  393. * the next higher trip point
  394. */
  395. tz->trips.active[i - 1].temperature =
  396. (tz->trips.active[i - 2].temperature <
  397. CELSIUS_TO_KELVIN(act) ?
  398. tz->trips.active[i - 2].temperature :
  399. CELSIUS_TO_KELVIN(act));
  400. break;
  401. } else {
  402. tz->trips.active[i].temperature = tmp;
  403. tz->trips.active[i].flags.valid = 1;
  404. }
  405. }
  406. name[2] = 'L';
  407. if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
  408. memset(&devices, 0, sizeof(struct acpi_handle_list));
  409. status = acpi_evaluate_reference(tz->device->handle,
  410. name, NULL, &devices);
  411. if (ACPI_FAILURE(status)) {
  412. printk(KERN_WARNING PREFIX
  413. "Invalid active%d threshold\n", i);
  414. tz->trips.active[i].flags.valid = 0;
  415. }
  416. else
  417. tz->trips.active[i].flags.valid = 1;
  418. if (memcmp(&tz->trips.active[i].devices, &devices,
  419. sizeof(struct acpi_handle_list))) {
  420. memcpy(&tz->trips.active[i].devices, &devices,
  421. sizeof(struct acpi_handle_list));
  422. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
  423. }
  424. }
  425. if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
  426. if (valid != tz->trips.active[i].flags.valid)
  427. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
  428. if (!tz->trips.active[i].flags.valid)
  429. break;
  430. }
  431. if (flag & ACPI_TRIPS_DEVICES) {
  432. memset(&devices, 0, sizeof(struct acpi_handle_list));
  433. status = acpi_evaluate_reference(tz->device->handle, "_TZD",
  434. NULL, &devices);
  435. if (memcmp(&tz->devices, &devices,
  436. sizeof(struct acpi_handle_list))) {
  437. memcpy(&tz->devices, &devices,
  438. sizeof(struct acpi_handle_list));
  439. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
  440. }
  441. }
  442. return 0;
  443. }
  444. static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
  445. {
  446. int i, valid, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
  447. if (ret)
  448. return ret;
  449. valid = tz->trips.critical.flags.valid |
  450. tz->trips.hot.flags.valid |
  451. tz->trips.passive.flags.valid;
  452. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
  453. valid |= tz->trips.active[i].flags.valid;
  454. if (!valid) {
  455. printk(KERN_WARNING FW_BUG "No valid trip found\n");
  456. return -ENODEV;
  457. }
  458. return 0;
  459. }
  460. static void acpi_thermal_check(void *data)
  461. {
  462. struct acpi_thermal *tz = data;
  463. thermal_zone_device_update(tz->thermal_zone);
  464. }
  465. /* sys I/F for generic thermal sysfs support */
  466. #define KELVIN_TO_MILLICELSIUS(t, off) (((t) - (off)) * 100)
  467. static int thermal_get_temp(struct thermal_zone_device *thermal,
  468. unsigned long *temp)
  469. {
  470. struct acpi_thermal *tz = thermal->devdata;
  471. int result;
  472. if (!tz)
  473. return -EINVAL;
  474. result = acpi_thermal_get_temperature(tz);
  475. if (result)
  476. return result;
  477. *temp = KELVIN_TO_MILLICELSIUS(tz->temperature, tz->kelvin_offset);
  478. return 0;
  479. }
  480. static const char enabled[] = "kernel";
  481. static const char disabled[] = "user";
  482. static int thermal_get_mode(struct thermal_zone_device *thermal,
  483. enum thermal_device_mode *mode)
  484. {
  485. struct acpi_thermal *tz = thermal->devdata;
  486. if (!tz)
  487. return -EINVAL;
  488. *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
  489. THERMAL_DEVICE_DISABLED;
  490. return 0;
  491. }
  492. static int thermal_set_mode(struct thermal_zone_device *thermal,
  493. enum thermal_device_mode mode)
  494. {
  495. struct acpi_thermal *tz = thermal->devdata;
  496. int enable;
  497. if (!tz)
  498. return -EINVAL;
  499. /*
  500. * enable/disable thermal management from ACPI thermal driver
  501. */
  502. if (mode == THERMAL_DEVICE_ENABLED)
  503. enable = 1;
  504. else if (mode == THERMAL_DEVICE_DISABLED)
  505. enable = 0;
  506. else
  507. return -EINVAL;
  508. if (enable != tz->tz_enabled) {
  509. tz->tz_enabled = enable;
  510. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  511. "%s ACPI thermal control\n",
  512. tz->tz_enabled ? enabled : disabled));
  513. acpi_thermal_check(tz);
  514. }
  515. return 0;
  516. }
  517. static int thermal_get_trip_type(struct thermal_zone_device *thermal,
  518. int trip, enum thermal_trip_type *type)
  519. {
  520. struct acpi_thermal *tz = thermal->devdata;
  521. int i;
  522. if (!tz || trip < 0)
  523. return -EINVAL;
  524. if (tz->trips.critical.flags.valid) {
  525. if (!trip) {
  526. *type = THERMAL_TRIP_CRITICAL;
  527. return 0;
  528. }
  529. trip--;
  530. }
  531. if (tz->trips.hot.flags.valid) {
  532. if (!trip) {
  533. *type = THERMAL_TRIP_HOT;
  534. return 0;
  535. }
  536. trip--;
  537. }
  538. if (tz->trips.passive.flags.valid) {
  539. if (!trip) {
  540. *type = THERMAL_TRIP_PASSIVE;
  541. return 0;
  542. }
  543. trip--;
  544. }
  545. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  546. tz->trips.active[i].flags.valid; i++) {
  547. if (!trip) {
  548. *type = THERMAL_TRIP_ACTIVE;
  549. return 0;
  550. }
  551. trip--;
  552. }
  553. return -EINVAL;
  554. }
  555. static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
  556. int trip, unsigned long *temp)
  557. {
  558. struct acpi_thermal *tz = thermal->devdata;
  559. int i;
  560. if (!tz || trip < 0)
  561. return -EINVAL;
  562. if (tz->trips.critical.flags.valid) {
  563. if (!trip) {
  564. *temp = KELVIN_TO_MILLICELSIUS(
  565. tz->trips.critical.temperature,
  566. tz->kelvin_offset);
  567. return 0;
  568. }
  569. trip--;
  570. }
  571. if (tz->trips.hot.flags.valid) {
  572. if (!trip) {
  573. *temp = KELVIN_TO_MILLICELSIUS(
  574. tz->trips.hot.temperature,
  575. tz->kelvin_offset);
  576. return 0;
  577. }
  578. trip--;
  579. }
  580. if (tz->trips.passive.flags.valid) {
  581. if (!trip) {
  582. *temp = KELVIN_TO_MILLICELSIUS(
  583. tz->trips.passive.temperature,
  584. tz->kelvin_offset);
  585. return 0;
  586. }
  587. trip--;
  588. }
  589. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  590. tz->trips.active[i].flags.valid; i++) {
  591. if (!trip) {
  592. *temp = KELVIN_TO_MILLICELSIUS(
  593. tz->trips.active[i].temperature,
  594. tz->kelvin_offset);
  595. return 0;
  596. }
  597. trip--;
  598. }
  599. return -EINVAL;
  600. }
  601. static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
  602. unsigned long *temperature) {
  603. struct acpi_thermal *tz = thermal->devdata;
  604. if (tz->trips.critical.flags.valid) {
  605. *temperature = KELVIN_TO_MILLICELSIUS(
  606. tz->trips.critical.temperature,
  607. tz->kelvin_offset);
  608. return 0;
  609. } else
  610. return -EINVAL;
  611. }
  612. static int thermal_notify(struct thermal_zone_device *thermal, int trip,
  613. enum thermal_trip_type trip_type)
  614. {
  615. u8 type = 0;
  616. struct acpi_thermal *tz = thermal->devdata;
  617. if (trip_type == THERMAL_TRIP_CRITICAL)
  618. type = ACPI_THERMAL_NOTIFY_CRITICAL;
  619. else if (trip_type == THERMAL_TRIP_HOT)
  620. type = ACPI_THERMAL_NOTIFY_HOT;
  621. else
  622. return 0;
  623. acpi_bus_generate_proc_event(tz->device, type, 1);
  624. acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
  625. dev_name(&tz->device->dev), type, 1);
  626. if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
  627. return 1;
  628. return 0;
  629. }
  630. typedef int (*cb)(struct thermal_zone_device *, int,
  631. struct thermal_cooling_device *);
  632. static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
  633. struct thermal_cooling_device *cdev,
  634. cb action)
  635. {
  636. struct acpi_device *device = cdev->devdata;
  637. struct acpi_thermal *tz = thermal->devdata;
  638. struct acpi_device *dev;
  639. acpi_status status;
  640. acpi_handle handle;
  641. int i;
  642. int j;
  643. int trip = -1;
  644. int result = 0;
  645. if (tz->trips.critical.flags.valid)
  646. trip++;
  647. if (tz->trips.hot.flags.valid)
  648. trip++;
  649. if (tz->trips.passive.flags.valid) {
  650. trip++;
  651. for (i = 0; i < tz->trips.passive.devices.count;
  652. i++) {
  653. handle = tz->trips.passive.devices.handles[i];
  654. status = acpi_bus_get_device(handle, &dev);
  655. if (ACPI_SUCCESS(status) && (dev == device)) {
  656. result = action(thermal, trip, cdev);
  657. if (result)
  658. goto failed;
  659. }
  660. }
  661. }
  662. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  663. if (!tz->trips.active[i].flags.valid)
  664. break;
  665. trip++;
  666. for (j = 0;
  667. j < tz->trips.active[i].devices.count;
  668. j++) {
  669. handle = tz->trips.active[i].devices.handles[j];
  670. status = acpi_bus_get_device(handle, &dev);
  671. if (ACPI_SUCCESS(status) && (dev == device)) {
  672. result = action(thermal, trip, cdev);
  673. if (result)
  674. goto failed;
  675. }
  676. }
  677. }
  678. for (i = 0; i < tz->devices.count; i++) {
  679. handle = tz->devices.handles[i];
  680. status = acpi_bus_get_device(handle, &dev);
  681. if (ACPI_SUCCESS(status) && (dev == device)) {
  682. result = action(thermal, -1, cdev);
  683. if (result)
  684. goto failed;
  685. }
  686. }
  687. failed:
  688. return result;
  689. }
  690. static int
  691. acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
  692. struct thermal_cooling_device *cdev)
  693. {
  694. return acpi_thermal_cooling_device_cb(thermal, cdev,
  695. thermal_zone_bind_cooling_device);
  696. }
  697. static int
  698. acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
  699. struct thermal_cooling_device *cdev)
  700. {
  701. return acpi_thermal_cooling_device_cb(thermal, cdev,
  702. thermal_zone_unbind_cooling_device);
  703. }
  704. static const struct thermal_zone_device_ops acpi_thermal_zone_ops = {
  705. .bind = acpi_thermal_bind_cooling_device,
  706. .unbind = acpi_thermal_unbind_cooling_device,
  707. .get_temp = thermal_get_temp,
  708. .get_mode = thermal_get_mode,
  709. .set_mode = thermal_set_mode,
  710. .get_trip_type = thermal_get_trip_type,
  711. .get_trip_temp = thermal_get_trip_temp,
  712. .get_crit_temp = thermal_get_crit_temp,
  713. .notify = thermal_notify,
  714. };
  715. static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
  716. {
  717. int trips = 0;
  718. int result;
  719. acpi_status status;
  720. int i;
  721. if (tz->trips.critical.flags.valid)
  722. trips++;
  723. if (tz->trips.hot.flags.valid)
  724. trips++;
  725. if (tz->trips.passive.flags.valid)
  726. trips++;
  727. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  728. tz->trips.active[i].flags.valid; i++, trips++);
  729. if (tz->trips.passive.flags.valid)
  730. tz->thermal_zone =
  731. thermal_zone_device_register("acpitz", trips, tz,
  732. &acpi_thermal_zone_ops,
  733. tz->trips.passive.tc1,
  734. tz->trips.passive.tc2,
  735. tz->trips.passive.tsp*100,
  736. tz->polling_frequency*100);
  737. else
  738. tz->thermal_zone =
  739. thermal_zone_device_register("acpitz", trips, tz,
  740. &acpi_thermal_zone_ops,
  741. 0, 0, 0,
  742. tz->polling_frequency*100);
  743. if (IS_ERR(tz->thermal_zone))
  744. return -ENODEV;
  745. result = sysfs_create_link(&tz->device->dev.kobj,
  746. &tz->thermal_zone->device.kobj, "thermal_zone");
  747. if (result)
  748. return result;
  749. result = sysfs_create_link(&tz->thermal_zone->device.kobj,
  750. &tz->device->dev.kobj, "device");
  751. if (result)
  752. return result;
  753. status = acpi_attach_data(tz->device->handle,
  754. acpi_bus_private_data_handler,
  755. tz->thermal_zone);
  756. if (ACPI_FAILURE(status)) {
  757. printk(KERN_ERR PREFIX
  758. "Error attaching device data\n");
  759. return -ENODEV;
  760. }
  761. tz->tz_enabled = 1;
  762. dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
  763. tz->thermal_zone->id);
  764. return 0;
  765. }
  766. static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
  767. {
  768. sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
  769. sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
  770. thermal_zone_device_unregister(tz->thermal_zone);
  771. tz->thermal_zone = NULL;
  772. acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
  773. }
  774. /* --------------------------------------------------------------------------
  775. Driver Interface
  776. -------------------------------------------------------------------------- */
  777. static void acpi_thermal_notify(struct acpi_device *device, u32 event)
  778. {
  779. struct acpi_thermal *tz = acpi_driver_data(device);
  780. if (!tz)
  781. return;
  782. switch (event) {
  783. case ACPI_THERMAL_NOTIFY_TEMPERATURE:
  784. acpi_thermal_check(tz);
  785. break;
  786. case ACPI_THERMAL_NOTIFY_THRESHOLDS:
  787. acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
  788. acpi_thermal_check(tz);
  789. acpi_bus_generate_proc_event(device, event, 0);
  790. acpi_bus_generate_netlink_event(device->pnp.device_class,
  791. dev_name(&device->dev), event, 0);
  792. break;
  793. case ACPI_THERMAL_NOTIFY_DEVICES:
  794. acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
  795. acpi_thermal_check(tz);
  796. acpi_bus_generate_proc_event(device, event, 0);
  797. acpi_bus_generate_netlink_event(device->pnp.device_class,
  798. dev_name(&device->dev), event, 0);
  799. break;
  800. default:
  801. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  802. "Unsupported event [0x%x]\n", event));
  803. break;
  804. }
  805. }
  806. static int acpi_thermal_get_info(struct acpi_thermal *tz)
  807. {
  808. int result = 0;
  809. if (!tz)
  810. return -EINVAL;
  811. /* Get trip points [_CRT, _PSV, etc.] (required) */
  812. result = acpi_thermal_get_trip_points(tz);
  813. if (result)
  814. return result;
  815. /* Get temperature [_TMP] (required) */
  816. result = acpi_thermal_get_temperature(tz);
  817. if (result)
  818. return result;
  819. /* Set the cooling mode [_SCP] to active cooling (default) */
  820. result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
  821. if (!result)
  822. tz->flags.cooling_mode = 1;
  823. /* Get default polling frequency [_TZP] (optional) */
  824. if (tzp)
  825. tz->polling_frequency = tzp;
  826. else
  827. acpi_thermal_get_polling_frequency(tz);
  828. return 0;
  829. }
  830. /*
  831. * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
  832. * handles temperature values with a single decimal place. As a consequence,
  833. * some implementations use an offset of 273.1 and others use an offset of
  834. * 273.2. Try to find out which one is being used, to present the most
  835. * accurate and visually appealing number.
  836. *
  837. * The heuristic below should work for all ACPI thermal zones which have a
  838. * critical trip point with a value being a multiple of 0.5 degree Celsius.
  839. */
  840. static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
  841. {
  842. if (tz->trips.critical.flags.valid &&
  843. (tz->trips.critical.temperature % 5) == 1)
  844. tz->kelvin_offset = 2731;
  845. else
  846. tz->kelvin_offset = 2732;
  847. }
  848. static int acpi_thermal_add(struct acpi_device *device)
  849. {
  850. int result = 0;
  851. struct acpi_thermal *tz = NULL;
  852. if (!device)
  853. return -EINVAL;
  854. tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
  855. if (!tz)
  856. return -ENOMEM;
  857. tz->device = device;
  858. strcpy(tz->name, device->pnp.bus_id);
  859. strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
  860. strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
  861. device->driver_data = tz;
  862. mutex_init(&tz->lock);
  863. result = acpi_thermal_get_info(tz);
  864. if (result)
  865. goto free_memory;
  866. acpi_thermal_guess_offset(tz);
  867. result = acpi_thermal_register_thermal_zone(tz);
  868. if (result)
  869. goto free_memory;
  870. printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
  871. acpi_device_name(device), acpi_device_bid(device),
  872. KELVIN_TO_CELSIUS(tz->temperature));
  873. goto end;
  874. free_memory:
  875. kfree(tz);
  876. end:
  877. return result;
  878. }
  879. static int acpi_thermal_remove(struct acpi_device *device, int type)
  880. {
  881. struct acpi_thermal *tz = NULL;
  882. if (!device || !acpi_driver_data(device))
  883. return -EINVAL;
  884. tz = acpi_driver_data(device);
  885. acpi_thermal_unregister_thermal_zone(tz);
  886. mutex_destroy(&tz->lock);
  887. kfree(tz);
  888. return 0;
  889. }
  890. static int acpi_thermal_resume(struct device *dev)
  891. {
  892. struct acpi_thermal *tz;
  893. int i, j, power_state, result;
  894. if (!dev)
  895. return -EINVAL;
  896. tz = acpi_driver_data(to_acpi_device(dev));
  897. if (!tz)
  898. return -EINVAL;
  899. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  900. if (!(&tz->trips.active[i]))
  901. break;
  902. if (!tz->trips.active[i].flags.valid)
  903. break;
  904. tz->trips.active[i].flags.enabled = 1;
  905. for (j = 0; j < tz->trips.active[i].devices.count; j++) {
  906. result = acpi_bus_update_power(
  907. tz->trips.active[i].devices.handles[j],
  908. &power_state);
  909. if (result || (power_state != ACPI_STATE_D0)) {
  910. tz->trips.active[i].flags.enabled = 0;
  911. break;
  912. }
  913. }
  914. tz->state.active |= tz->trips.active[i].flags.enabled;
  915. }
  916. acpi_thermal_check(tz);
  917. return AE_OK;
  918. }
  919. static int thermal_act(const struct dmi_system_id *d) {
  920. if (act == 0) {
  921. printk(KERN_NOTICE "ACPI: %s detected: "
  922. "disabling all active thermal trip points\n", d->ident);
  923. act = -1;
  924. }
  925. return 0;
  926. }
  927. static int thermal_nocrt(const struct dmi_system_id *d) {
  928. printk(KERN_NOTICE "ACPI: %s detected: "
  929. "disabling all critical thermal trip point actions.\n", d->ident);
  930. nocrt = 1;
  931. return 0;
  932. }
  933. static int thermal_tzp(const struct dmi_system_id *d) {
  934. if (tzp == 0) {
  935. printk(KERN_NOTICE "ACPI: %s detected: "
  936. "enabling thermal zone polling\n", d->ident);
  937. tzp = 300; /* 300 dS = 30 Seconds */
  938. }
  939. return 0;
  940. }
  941. static int thermal_psv(const struct dmi_system_id *d) {
  942. if (psv == 0) {
  943. printk(KERN_NOTICE "ACPI: %s detected: "
  944. "disabling all passive thermal trip points\n", d->ident);
  945. psv = -1;
  946. }
  947. return 0;
  948. }
  949. static struct dmi_system_id thermal_dmi_table[] __initdata = {
  950. /*
  951. * Award BIOS on this AOpen makes thermal control almost worthless.
  952. * http://bugzilla.kernel.org/show_bug.cgi?id=8842
  953. */
  954. {
  955. .callback = thermal_act,
  956. .ident = "AOpen i915GMm-HFS",
  957. .matches = {
  958. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  959. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  960. },
  961. },
  962. {
  963. .callback = thermal_psv,
  964. .ident = "AOpen i915GMm-HFS",
  965. .matches = {
  966. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  967. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  968. },
  969. },
  970. {
  971. .callback = thermal_tzp,
  972. .ident = "AOpen i915GMm-HFS",
  973. .matches = {
  974. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  975. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  976. },
  977. },
  978. {
  979. .callback = thermal_nocrt,
  980. .ident = "Gigabyte GA-7ZX",
  981. .matches = {
  982. DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
  983. DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
  984. },
  985. },
  986. {}
  987. };
  988. static int __init acpi_thermal_init(void)
  989. {
  990. int result = 0;
  991. dmi_check_system(thermal_dmi_table);
  992. if (off) {
  993. printk(KERN_NOTICE "ACPI: thermal control disabled\n");
  994. return -ENODEV;
  995. }
  996. result = acpi_bus_register_driver(&acpi_thermal_driver);
  997. if (result < 0)
  998. return -ENODEV;
  999. return 0;
  1000. }
  1001. static void __exit acpi_thermal_exit(void)
  1002. {
  1003. acpi_bus_unregister_driver(&acpi_thermal_driver);
  1004. return;
  1005. }
  1006. module_init(acpi_thermal_init);
  1007. module_exit(acpi_thermal_exit);