thermal.c 31 KB

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