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. pr_warn("thermal zone is disabled \n");
  454. return;
  455. }
  456. thermal_zone_device_update(tz->thermal_zone);
  457. }
  458. /* sys I/F for generic thermal sysfs support */
  459. #define KELVIN_TO_MILLICELSIUS(t, off) (((t) - (off)) * 100)
  460. static int thermal_get_temp(struct thermal_zone_device *thermal,
  461. unsigned long *temp)
  462. {
  463. struct acpi_thermal *tz = thermal->devdata;
  464. int result;
  465. if (!tz)
  466. return -EINVAL;
  467. result = acpi_thermal_get_temperature(tz);
  468. if (result)
  469. return result;
  470. *temp = KELVIN_TO_MILLICELSIUS(tz->temperature, tz->kelvin_offset);
  471. return 0;
  472. }
  473. static int thermal_get_mode(struct thermal_zone_device *thermal,
  474. enum thermal_device_mode *mode)
  475. {
  476. struct acpi_thermal *tz = thermal->devdata;
  477. if (!tz)
  478. return -EINVAL;
  479. *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
  480. THERMAL_DEVICE_DISABLED;
  481. return 0;
  482. }
  483. static int thermal_set_mode(struct thermal_zone_device *thermal,
  484. enum thermal_device_mode mode)
  485. {
  486. struct acpi_thermal *tz = thermal->devdata;
  487. int enable;
  488. if (!tz)
  489. return -EINVAL;
  490. /*
  491. * enable/disable thermal management from ACPI thermal driver
  492. */
  493. if (mode == THERMAL_DEVICE_ENABLED)
  494. enable = 1;
  495. else if (mode == THERMAL_DEVICE_DISABLED)
  496. enable = 0;
  497. else
  498. return -EINVAL;
  499. if (enable != tz->tz_enabled) {
  500. tz->tz_enabled = enable;
  501. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  502. "%s kernel ACPI thermal control\n",
  503. tz->tz_enabled ? "Enable" : "Disable"));
  504. acpi_thermal_check(tz);
  505. }
  506. return 0;
  507. }
  508. static int thermal_get_trip_type(struct thermal_zone_device *thermal,
  509. int trip, enum thermal_trip_type *type)
  510. {
  511. struct acpi_thermal *tz = thermal->devdata;
  512. int i;
  513. if (!tz || trip < 0)
  514. return -EINVAL;
  515. if (tz->trips.critical.flags.valid) {
  516. if (!trip) {
  517. *type = THERMAL_TRIP_CRITICAL;
  518. return 0;
  519. }
  520. trip--;
  521. }
  522. if (tz->trips.hot.flags.valid) {
  523. if (!trip) {
  524. *type = THERMAL_TRIP_HOT;
  525. return 0;
  526. }
  527. trip--;
  528. }
  529. if (tz->trips.passive.flags.valid) {
  530. if (!trip) {
  531. *type = THERMAL_TRIP_PASSIVE;
  532. return 0;
  533. }
  534. trip--;
  535. }
  536. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  537. tz->trips.active[i].flags.valid; i++) {
  538. if (!trip) {
  539. *type = THERMAL_TRIP_ACTIVE;
  540. return 0;
  541. }
  542. trip--;
  543. }
  544. return -EINVAL;
  545. }
  546. static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
  547. int trip, unsigned long *temp)
  548. {
  549. struct acpi_thermal *tz = thermal->devdata;
  550. int i;
  551. if (!tz || trip < 0)
  552. return -EINVAL;
  553. if (tz->trips.critical.flags.valid) {
  554. if (!trip) {
  555. *temp = KELVIN_TO_MILLICELSIUS(
  556. tz->trips.critical.temperature,
  557. tz->kelvin_offset);
  558. return 0;
  559. }
  560. trip--;
  561. }
  562. if (tz->trips.hot.flags.valid) {
  563. if (!trip) {
  564. *temp = KELVIN_TO_MILLICELSIUS(
  565. tz->trips.hot.temperature,
  566. tz->kelvin_offset);
  567. return 0;
  568. }
  569. trip--;
  570. }
  571. if (tz->trips.passive.flags.valid) {
  572. if (!trip) {
  573. *temp = KELVIN_TO_MILLICELSIUS(
  574. tz->trips.passive.temperature,
  575. tz->kelvin_offset);
  576. return 0;
  577. }
  578. trip--;
  579. }
  580. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  581. tz->trips.active[i].flags.valid; i++) {
  582. if (!trip) {
  583. *temp = KELVIN_TO_MILLICELSIUS(
  584. tz->trips.active[i].temperature,
  585. tz->kelvin_offset);
  586. return 0;
  587. }
  588. trip--;
  589. }
  590. return -EINVAL;
  591. }
  592. static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
  593. unsigned long *temperature) {
  594. struct acpi_thermal *tz = thermal->devdata;
  595. if (tz->trips.critical.flags.valid) {
  596. *temperature = KELVIN_TO_MILLICELSIUS(
  597. tz->trips.critical.temperature,
  598. tz->kelvin_offset);
  599. return 0;
  600. } else
  601. return -EINVAL;
  602. }
  603. static int thermal_get_trend(struct thermal_zone_device *thermal,
  604. int trip, enum thermal_trend *trend)
  605. {
  606. struct acpi_thermal *tz = thermal->devdata;
  607. enum thermal_trip_type type;
  608. int i;
  609. if (thermal_get_trip_type(thermal, trip, &type))
  610. return -EINVAL;
  611. if (type == THERMAL_TRIP_ACTIVE) {
  612. unsigned long trip_temp;
  613. unsigned long temp = KELVIN_TO_MILLICELSIUS(tz->temperature,
  614. tz->kelvin_offset);
  615. if (thermal_get_trip_temp(thermal, trip, &trip_temp))
  616. return -EINVAL;
  617. if (temp > trip_temp) {
  618. *trend = THERMAL_TREND_RAISING;
  619. return 0;
  620. } else {
  621. /* Fall back on default trend */
  622. return -EINVAL;
  623. }
  624. }
  625. /*
  626. * tz->temperature has already been updated by generic thermal layer,
  627. * before this callback being invoked
  628. */
  629. i = (tz->trips.passive.tc1 * (tz->temperature - tz->last_temperature))
  630. + (tz->trips.passive.tc2
  631. * (tz->temperature - tz->trips.passive.temperature));
  632. if (i > 0)
  633. *trend = THERMAL_TREND_RAISING;
  634. else if (i < 0)
  635. *trend = THERMAL_TREND_DROPPING;
  636. else
  637. *trend = THERMAL_TREND_STABLE;
  638. return 0;
  639. }
  640. static int thermal_notify(struct thermal_zone_device *thermal, int trip,
  641. enum thermal_trip_type trip_type)
  642. {
  643. u8 type = 0;
  644. struct acpi_thermal *tz = thermal->devdata;
  645. if (trip_type == THERMAL_TRIP_CRITICAL)
  646. type = ACPI_THERMAL_NOTIFY_CRITICAL;
  647. else if (trip_type == THERMAL_TRIP_HOT)
  648. type = ACPI_THERMAL_NOTIFY_HOT;
  649. else
  650. return 0;
  651. acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
  652. dev_name(&tz->device->dev), type, 1);
  653. if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
  654. return 1;
  655. return 0;
  656. }
  657. static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
  658. struct thermal_cooling_device *cdev,
  659. bool bind)
  660. {
  661. struct acpi_device *device = cdev->devdata;
  662. struct acpi_thermal *tz = thermal->devdata;
  663. struct acpi_device *dev;
  664. acpi_status status;
  665. acpi_handle handle;
  666. int i;
  667. int j;
  668. int trip = -1;
  669. int result = 0;
  670. if (tz->trips.critical.flags.valid)
  671. trip++;
  672. if (tz->trips.hot.flags.valid)
  673. trip++;
  674. if (tz->trips.passive.flags.valid) {
  675. trip++;
  676. for (i = 0; i < tz->trips.passive.devices.count;
  677. i++) {
  678. handle = tz->trips.passive.devices.handles[i];
  679. status = acpi_bus_get_device(handle, &dev);
  680. if (ACPI_FAILURE(status) || dev != device)
  681. continue;
  682. if (bind)
  683. result =
  684. thermal_zone_bind_cooling_device
  685. (thermal, trip, cdev,
  686. THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
  687. else
  688. result =
  689. thermal_zone_unbind_cooling_device
  690. (thermal, trip, cdev);
  691. if (result)
  692. goto failed;
  693. }
  694. }
  695. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  696. if (!tz->trips.active[i].flags.valid)
  697. break;
  698. trip++;
  699. for (j = 0;
  700. j < tz->trips.active[i].devices.count;
  701. j++) {
  702. handle = tz->trips.active[i].devices.handles[j];
  703. status = acpi_bus_get_device(handle, &dev);
  704. if (ACPI_FAILURE(status) || dev != device)
  705. continue;
  706. if (bind)
  707. result = thermal_zone_bind_cooling_device
  708. (thermal, trip, cdev,
  709. THERMAL_NO_LIMIT, THERMAL_NO_LIMIT);
  710. else
  711. result = thermal_zone_unbind_cooling_device
  712. (thermal, trip, cdev);
  713. if (result)
  714. goto failed;
  715. }
  716. }
  717. for (i = 0; i < tz->devices.count; i++) {
  718. handle = tz->devices.handles[i];
  719. status = acpi_bus_get_device(handle, &dev);
  720. if (ACPI_SUCCESS(status) && (dev == device)) {
  721. if (bind)
  722. result = thermal_zone_bind_cooling_device
  723. (thermal, THERMAL_TRIPS_NONE,
  724. cdev, THERMAL_NO_LIMIT,
  725. THERMAL_NO_LIMIT);
  726. else
  727. result = thermal_zone_unbind_cooling_device
  728. (thermal, THERMAL_TRIPS_NONE,
  729. cdev);
  730. if (result)
  731. goto failed;
  732. }
  733. }
  734. failed:
  735. return result;
  736. }
  737. static int
  738. acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
  739. struct thermal_cooling_device *cdev)
  740. {
  741. return acpi_thermal_cooling_device_cb(thermal, cdev, true);
  742. }
  743. static int
  744. acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
  745. struct thermal_cooling_device *cdev)
  746. {
  747. return acpi_thermal_cooling_device_cb(thermal, cdev, false);
  748. }
  749. static const struct thermal_zone_device_ops acpi_thermal_zone_ops = {
  750. .bind = acpi_thermal_bind_cooling_device,
  751. .unbind = acpi_thermal_unbind_cooling_device,
  752. .get_temp = thermal_get_temp,
  753. .get_mode = thermal_get_mode,
  754. .set_mode = thermal_set_mode,
  755. .get_trip_type = thermal_get_trip_type,
  756. .get_trip_temp = thermal_get_trip_temp,
  757. .get_crit_temp = thermal_get_crit_temp,
  758. .get_trend = thermal_get_trend,
  759. .notify = thermal_notify,
  760. };
  761. static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
  762. {
  763. int trips = 0;
  764. int result;
  765. acpi_status status;
  766. int i;
  767. if (tz->trips.critical.flags.valid)
  768. trips++;
  769. if (tz->trips.hot.flags.valid)
  770. trips++;
  771. if (tz->trips.passive.flags.valid)
  772. trips++;
  773. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  774. tz->trips.active[i].flags.valid; i++, trips++);
  775. if (tz->trips.passive.flags.valid)
  776. tz->thermal_zone =
  777. thermal_zone_device_register("acpitz", trips, 0, tz,
  778. &acpi_thermal_zone_ops, NULL,
  779. tz->trips.passive.tsp*100,
  780. tz->polling_frequency*100);
  781. else
  782. tz->thermal_zone =
  783. thermal_zone_device_register("acpitz", trips, 0, tz,
  784. &acpi_thermal_zone_ops, NULL,
  785. 0, tz->polling_frequency*100);
  786. if (IS_ERR(tz->thermal_zone))
  787. return -ENODEV;
  788. result = sysfs_create_link(&tz->device->dev.kobj,
  789. &tz->thermal_zone->device.kobj, "thermal_zone");
  790. if (result)
  791. return result;
  792. result = sysfs_create_link(&tz->thermal_zone->device.kobj,
  793. &tz->device->dev.kobj, "device");
  794. if (result)
  795. return result;
  796. status = acpi_attach_data(tz->device->handle,
  797. acpi_bus_private_data_handler,
  798. tz->thermal_zone);
  799. if (ACPI_FAILURE(status)) {
  800. pr_err(PREFIX "Error attaching device data\n");
  801. return -ENODEV;
  802. }
  803. tz->tz_enabled = 1;
  804. dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
  805. tz->thermal_zone->id);
  806. return 0;
  807. }
  808. static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
  809. {
  810. sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
  811. sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
  812. thermal_zone_device_unregister(tz->thermal_zone);
  813. tz->thermal_zone = NULL;
  814. acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
  815. }
  816. /* --------------------------------------------------------------------------
  817. Driver Interface
  818. -------------------------------------------------------------------------- */
  819. static void acpi_thermal_notify(struct acpi_device *device, u32 event)
  820. {
  821. struct acpi_thermal *tz = acpi_driver_data(device);
  822. if (!tz)
  823. return;
  824. switch (event) {
  825. case ACPI_THERMAL_NOTIFY_TEMPERATURE:
  826. acpi_thermal_check(tz);
  827. break;
  828. case ACPI_THERMAL_NOTIFY_THRESHOLDS:
  829. acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
  830. acpi_thermal_check(tz);
  831. acpi_bus_generate_netlink_event(device->pnp.device_class,
  832. dev_name(&device->dev), event, 0);
  833. break;
  834. case ACPI_THERMAL_NOTIFY_DEVICES:
  835. acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
  836. acpi_thermal_check(tz);
  837. acpi_bus_generate_netlink_event(device->pnp.device_class,
  838. dev_name(&device->dev), event, 0);
  839. break;
  840. default:
  841. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  842. "Unsupported event [0x%x]\n", event));
  843. break;
  844. }
  845. }
  846. /*
  847. * On some platforms, the AML code has dependency about
  848. * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx.
  849. * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after
  850. * /_CRT/_HOT/_PSV/_ACx, or else system will be power off.
  851. * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0
  852. * if _TMP has never been evaluated.
  853. *
  854. * As this dependency is totally transparent to OS, evaluate
  855. * all of them once, in the order of _CRT/_HOT/_PSV/_ACx,
  856. * _TMP, before they are actually used.
  857. */
  858. static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz)
  859. {
  860. acpi_handle handle = tz->device->handle;
  861. unsigned long long value;
  862. int i;
  863. acpi_evaluate_integer(handle, "_CRT", NULL, &value);
  864. acpi_evaluate_integer(handle, "_HOT", NULL, &value);
  865. acpi_evaluate_integer(handle, "_PSV", NULL, &value);
  866. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  867. char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
  868. acpi_status status;
  869. status = acpi_evaluate_integer(handle, name, NULL, &value);
  870. if (status == AE_NOT_FOUND)
  871. break;
  872. }
  873. acpi_evaluate_integer(handle, "_TMP", NULL, &value);
  874. }
  875. static int acpi_thermal_get_info(struct acpi_thermal *tz)
  876. {
  877. int result = 0;
  878. if (!tz)
  879. return -EINVAL;
  880. acpi_thermal_aml_dependency_fix(tz);
  881. /* Get trip points [_CRT, _PSV, etc.] (required) */
  882. result = acpi_thermal_get_trip_points(tz);
  883. if (result)
  884. return result;
  885. /* Get temperature [_TMP] (required) */
  886. result = acpi_thermal_get_temperature(tz);
  887. if (result)
  888. return result;
  889. /* Set the cooling mode [_SCP] to active cooling (default) */
  890. result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
  891. if (!result)
  892. tz->flags.cooling_mode = 1;
  893. /* Get default polling frequency [_TZP] (optional) */
  894. if (tzp)
  895. tz->polling_frequency = tzp;
  896. else
  897. acpi_thermal_get_polling_frequency(tz);
  898. return 0;
  899. }
  900. /*
  901. * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
  902. * handles temperature values with a single decimal place. As a consequence,
  903. * some implementations use an offset of 273.1 and others use an offset of
  904. * 273.2. Try to find out which one is being used, to present the most
  905. * accurate and visually appealing number.
  906. *
  907. * The heuristic below should work for all ACPI thermal zones which have a
  908. * critical trip point with a value being a multiple of 0.5 degree Celsius.
  909. */
  910. static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
  911. {
  912. if (tz->trips.critical.flags.valid &&
  913. (tz->trips.critical.temperature % 5) == 1)
  914. tz->kelvin_offset = 2731;
  915. else
  916. tz->kelvin_offset = 2732;
  917. }
  918. static int acpi_thermal_add(struct acpi_device *device)
  919. {
  920. int result = 0;
  921. struct acpi_thermal *tz = NULL;
  922. if (!device)
  923. return -EINVAL;
  924. tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
  925. if (!tz)
  926. return -ENOMEM;
  927. tz->device = device;
  928. strcpy(tz->name, device->pnp.bus_id);
  929. strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
  930. strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
  931. device->driver_data = tz;
  932. result = acpi_thermal_get_info(tz);
  933. if (result)
  934. goto free_memory;
  935. acpi_thermal_guess_offset(tz);
  936. result = acpi_thermal_register_thermal_zone(tz);
  937. if (result)
  938. goto free_memory;
  939. pr_info(PREFIX "%s [%s] (%ld C)\n", acpi_device_name(device),
  940. acpi_device_bid(device), KELVIN_TO_CELSIUS(tz->temperature));
  941. goto end;
  942. free_memory:
  943. kfree(tz);
  944. end:
  945. return result;
  946. }
  947. static int acpi_thermal_remove(struct acpi_device *device)
  948. {
  949. struct acpi_thermal *tz = NULL;
  950. if (!device || !acpi_driver_data(device))
  951. return -EINVAL;
  952. tz = acpi_driver_data(device);
  953. acpi_thermal_unregister_thermal_zone(tz);
  954. kfree(tz);
  955. return 0;
  956. }
  957. #ifdef CONFIG_PM_SLEEP
  958. static int acpi_thermal_resume(struct device *dev)
  959. {
  960. struct acpi_thermal *tz;
  961. int i, j, power_state, result;
  962. if (!dev)
  963. return -EINVAL;
  964. tz = acpi_driver_data(to_acpi_device(dev));
  965. if (!tz)
  966. return -EINVAL;
  967. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  968. if (!(&tz->trips.active[i]))
  969. break;
  970. if (!tz->trips.active[i].flags.valid)
  971. break;
  972. tz->trips.active[i].flags.enabled = 1;
  973. for (j = 0; j < tz->trips.active[i].devices.count; j++) {
  974. result = acpi_bus_update_power(
  975. tz->trips.active[i].devices.handles[j],
  976. &power_state);
  977. if (result || (power_state != ACPI_STATE_D0)) {
  978. tz->trips.active[i].flags.enabled = 0;
  979. break;
  980. }
  981. }
  982. tz->state.active |= tz->trips.active[i].flags.enabled;
  983. }
  984. acpi_thermal_check(tz);
  985. return AE_OK;
  986. }
  987. #endif
  988. static int thermal_act(const struct dmi_system_id *d) {
  989. if (act == 0) {
  990. pr_notice(PREFIX "%s detected: "
  991. "disabling all active thermal trip points\n", d->ident);
  992. act = -1;
  993. }
  994. return 0;
  995. }
  996. static int thermal_nocrt(const struct dmi_system_id *d) {
  997. pr_notice(PREFIX "%s detected: "
  998. "disabling all critical thermal trip point actions.\n", d->ident);
  999. nocrt = 1;
  1000. return 0;
  1001. }
  1002. static int thermal_tzp(const struct dmi_system_id *d) {
  1003. if (tzp == 0) {
  1004. pr_notice(PREFIX "%s detected: "
  1005. "enabling thermal zone polling\n", d->ident);
  1006. tzp = 300; /* 300 dS = 30 Seconds */
  1007. }
  1008. return 0;
  1009. }
  1010. static int thermal_psv(const struct dmi_system_id *d) {
  1011. if (psv == 0) {
  1012. pr_notice(PREFIX "%s detected: "
  1013. "disabling all passive thermal trip points\n", d->ident);
  1014. psv = -1;
  1015. }
  1016. return 0;
  1017. }
  1018. static struct dmi_system_id thermal_dmi_table[] __initdata = {
  1019. /*
  1020. * Award BIOS on this AOpen makes thermal control almost worthless.
  1021. * http://bugzilla.kernel.org/show_bug.cgi?id=8842
  1022. */
  1023. {
  1024. .callback = thermal_act,
  1025. .ident = "AOpen i915GMm-HFS",
  1026. .matches = {
  1027. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  1028. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  1029. },
  1030. },
  1031. {
  1032. .callback = thermal_psv,
  1033. .ident = "AOpen i915GMm-HFS",
  1034. .matches = {
  1035. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  1036. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  1037. },
  1038. },
  1039. {
  1040. .callback = thermal_tzp,
  1041. .ident = "AOpen i915GMm-HFS",
  1042. .matches = {
  1043. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  1044. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  1045. },
  1046. },
  1047. {
  1048. .callback = thermal_nocrt,
  1049. .ident = "Gigabyte GA-7ZX",
  1050. .matches = {
  1051. DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
  1052. DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
  1053. },
  1054. },
  1055. {}
  1056. };
  1057. static int __init acpi_thermal_init(void)
  1058. {
  1059. int result = 0;
  1060. dmi_check_system(thermal_dmi_table);
  1061. if (off) {
  1062. pr_notice(PREFIX "thermal control disabled\n");
  1063. return -ENODEV;
  1064. }
  1065. result = acpi_bus_register_driver(&acpi_thermal_driver);
  1066. if (result < 0)
  1067. return -ENODEV;
  1068. return 0;
  1069. }
  1070. static void __exit acpi_thermal_exit(void)
  1071. {
  1072. acpi_bus_unregister_driver(&acpi_thermal_driver);
  1073. return;
  1074. }
  1075. module_init(acpi_thermal_init);
  1076. module_exit(acpi_thermal_exit);