thermal.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * acpi_thermal.c - ACPI Thermal Zone Driver ($Revision: 41 $)
  4. *
  5. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  7. *
  8. * This driver fully implements the ACPI thermal policy as described in the
  9. * ACPI 2.0 Specification.
  10. *
  11. * TBD: 1. Implement passive cooling hysteresis.
  12. * 2. Enhance passive cooling (CPU) states/limit interface to support
  13. * concepts of 'multiple limiters', upper/lower limits, etc.
  14. */
  15. #define pr_fmt(fmt) "ACPI: thermal: " fmt
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/dmi.h>
  19. #include <linux/init.h>
  20. #include <linux/slab.h>
  21. #include <linux/types.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/kmod.h>
  24. #include <linux/reboot.h>
  25. #include <linux/device.h>
  26. #include <linux/thermal.h>
  27. #include <linux/acpi.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/units.h>
  31. #include "internal.h"
  32. #define ACPI_THERMAL_CLASS "thermal_zone"
  33. #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
  34. #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
  35. #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
  36. #define ACPI_THERMAL_NOTIFY_DEVICES 0x82
  37. #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
  38. #define ACPI_THERMAL_NOTIFY_HOT 0xF1
  39. #define ACPI_THERMAL_MODE_ACTIVE 0x00
  40. #define ACPI_THERMAL_MAX_ACTIVE 10
  41. #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
  42. #define ACPI_THERMAL_TRIP_PASSIVE (-1)
  43. #define ACPI_THERMAL_MAX_NR_TRIPS (ACPI_THERMAL_MAX_ACTIVE + 3)
  44. /*
  45. * This exception is thrown out in two cases:
  46. * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
  47. * when re-evaluating the AML code.
  48. * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
  49. * We need to re-bind the cooling devices of a thermal zone when this occurs.
  50. */
  51. #define ACPI_THERMAL_TRIPS_EXCEPTION(tz, str) \
  52. do { \
  53. acpi_handle_info(tz->device->handle, \
  54. "ACPI thermal trip point %s changed\n" \
  55. "Please report to linux-acpi@vger.kernel.org\n", str); \
  56. } while (0)
  57. static int act;
  58. module_param(act, int, 0644);
  59. MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
  60. static int crt;
  61. module_param(crt, int, 0644);
  62. MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
  63. static int tzp;
  64. module_param(tzp, int, 0444);
  65. MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
  66. static int off;
  67. module_param(off, int, 0);
  68. MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
  69. static int psv;
  70. module_param(psv, int, 0644);
  71. MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
  72. static struct workqueue_struct *acpi_thermal_pm_queue;
  73. struct acpi_thermal_trip {
  74. unsigned long temp_dk;
  75. struct acpi_handle_list devices;
  76. };
  77. struct acpi_thermal_passive {
  78. struct acpi_thermal_trip trip;
  79. unsigned long tc1;
  80. unsigned long tc2;
  81. unsigned long delay;
  82. };
  83. struct acpi_thermal_active {
  84. struct acpi_thermal_trip trip;
  85. };
  86. struct acpi_thermal_trips {
  87. struct acpi_thermal_passive passive;
  88. struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
  89. };
  90. struct acpi_thermal {
  91. struct acpi_device *device;
  92. acpi_bus_id name;
  93. unsigned long temp_dk;
  94. unsigned long last_temp_dk;
  95. unsigned long polling_frequency;
  96. volatile u8 zombie;
  97. struct acpi_thermal_trips trips;
  98. struct thermal_zone_device *thermal_zone;
  99. int kelvin_offset; /* in millidegrees */
  100. struct work_struct thermal_check_work;
  101. struct mutex thermal_check_lock;
  102. refcount_t thermal_check_count;
  103. };
  104. /* --------------------------------------------------------------------------
  105. Thermal Zone Management
  106. -------------------------------------------------------------------------- */
  107. static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
  108. {
  109. acpi_status status = AE_OK;
  110. unsigned long long tmp;
  111. if (!tz)
  112. return -EINVAL;
  113. tz->last_temp_dk = tz->temp_dk;
  114. status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
  115. if (ACPI_FAILURE(status))
  116. return -ENODEV;
  117. tz->temp_dk = tmp;
  118. acpi_handle_debug(tz->device->handle, "Temperature is %lu dK\n",
  119. tz->temp_dk);
  120. return 0;
  121. }
  122. static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
  123. {
  124. acpi_status status = AE_OK;
  125. unsigned long long tmp;
  126. if (!tz)
  127. return -EINVAL;
  128. status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
  129. if (ACPI_FAILURE(status))
  130. return -ENODEV;
  131. tz->polling_frequency = tmp;
  132. acpi_handle_debug(tz->device->handle, "Polling frequency is %lu dS\n",
  133. tz->polling_frequency);
  134. return 0;
  135. }
  136. static int acpi_thermal_temp(struct acpi_thermal *tz, int temp_deci_k)
  137. {
  138. int temp;
  139. if (temp_deci_k == THERMAL_TEMP_INVALID)
  140. return THERMAL_TEMP_INVALID;
  141. temp = deci_kelvin_to_millicelsius_with_offset(temp_deci_k,
  142. tz->kelvin_offset);
  143. if (temp <= 0)
  144. return THERMAL_TEMP_INVALID;
  145. return temp;
  146. }
  147. static bool acpi_thermal_trip_valid(struct acpi_thermal_trip *acpi_trip)
  148. {
  149. return acpi_trip->temp_dk != THERMAL_TEMP_INVALID;
  150. }
  151. static int active_trip_index(struct acpi_thermal *tz,
  152. struct acpi_thermal_trip *acpi_trip)
  153. {
  154. struct acpi_thermal_active *active;
  155. active = container_of(acpi_trip, struct acpi_thermal_active, trip);
  156. return active - tz->trips.active;
  157. }
  158. static long get_passive_temp(struct acpi_thermal *tz)
  159. {
  160. int temp;
  161. if (acpi_passive_trip_temp(tz->device, &temp))
  162. return THERMAL_TEMP_INVALID;
  163. return temp;
  164. }
  165. static long get_active_temp(struct acpi_thermal *tz, int index)
  166. {
  167. int temp;
  168. if (acpi_active_trip_temp(tz->device, index, &temp))
  169. return THERMAL_TEMP_INVALID;
  170. /*
  171. * If an override has been provided, apply it so there are no active
  172. * trips with thresholds greater than the override.
  173. */
  174. if (act > 0) {
  175. unsigned long long override = celsius_to_deci_kelvin(act);
  176. if (temp > override)
  177. return override;
  178. }
  179. return temp;
  180. }
  181. static void acpi_thermal_update_trip(struct acpi_thermal *tz,
  182. const struct thermal_trip *trip)
  183. {
  184. struct acpi_thermal_trip *acpi_trip = trip->priv;
  185. if (trip->type == THERMAL_TRIP_PASSIVE) {
  186. if (psv > 0)
  187. return;
  188. acpi_trip->temp_dk = get_passive_temp(tz);
  189. } else {
  190. int index = active_trip_index(tz, acpi_trip);
  191. acpi_trip->temp_dk = get_active_temp(tz, index);
  192. }
  193. if (!acpi_thermal_trip_valid(acpi_trip))
  194. ACPI_THERMAL_TRIPS_EXCEPTION(tz, "state");
  195. }
  196. static bool update_trip_devices(struct acpi_thermal *tz,
  197. struct acpi_thermal_trip *acpi_trip,
  198. int index, bool compare)
  199. {
  200. struct acpi_handle_list devices = { 0 };
  201. char method[] = "_PSL";
  202. if (index != ACPI_THERMAL_TRIP_PASSIVE) {
  203. method[1] = 'A';
  204. method[2] = 'L';
  205. method[3] = '0' + index;
  206. }
  207. if (!acpi_evaluate_reference(tz->device->handle, method, NULL, &devices)) {
  208. acpi_handle_info(tz->device->handle, "%s evaluation failure\n", method);
  209. return false;
  210. }
  211. if (acpi_handle_list_equal(&acpi_trip->devices, &devices)) {
  212. acpi_handle_list_free(&devices);
  213. return true;
  214. }
  215. if (compare)
  216. ACPI_THERMAL_TRIPS_EXCEPTION(tz, "device");
  217. acpi_handle_list_replace(&acpi_trip->devices, &devices);
  218. return true;
  219. }
  220. static void acpi_thermal_update_trip_devices(struct acpi_thermal *tz,
  221. const struct thermal_trip *trip)
  222. {
  223. struct acpi_thermal_trip *acpi_trip = trip->priv;
  224. int index = trip->type == THERMAL_TRIP_PASSIVE ?
  225. ACPI_THERMAL_TRIP_PASSIVE : active_trip_index(tz, acpi_trip);
  226. if (update_trip_devices(tz, acpi_trip, index, true))
  227. return;
  228. acpi_trip->temp_dk = THERMAL_TEMP_INVALID;
  229. ACPI_THERMAL_TRIPS_EXCEPTION(tz, "state");
  230. }
  231. struct adjust_trip_data {
  232. struct acpi_thermal *tz;
  233. u32 event;
  234. };
  235. static int acpi_thermal_adjust_trip(struct thermal_trip *trip, void *data)
  236. {
  237. struct acpi_thermal_trip *acpi_trip = trip->priv;
  238. struct adjust_trip_data *atd = data;
  239. struct acpi_thermal *tz = atd->tz;
  240. int temp;
  241. if (!acpi_trip || !acpi_thermal_trip_valid(acpi_trip))
  242. return 0;
  243. if (atd->event == ACPI_THERMAL_NOTIFY_THRESHOLDS)
  244. acpi_thermal_update_trip(tz, trip);
  245. else
  246. acpi_thermal_update_trip_devices(tz, trip);
  247. if (acpi_thermal_trip_valid(acpi_trip))
  248. temp = acpi_thermal_temp(tz, acpi_trip->temp_dk);
  249. else
  250. temp = THERMAL_TEMP_INVALID;
  251. thermal_zone_set_trip_temp(tz->thermal_zone, trip, temp);
  252. return 0;
  253. }
  254. static void acpi_queue_thermal_check(struct acpi_thermal *tz)
  255. {
  256. if (!work_pending(&tz->thermal_check_work))
  257. queue_work(acpi_thermal_pm_queue, &tz->thermal_check_work);
  258. }
  259. static void acpi_thermal_trips_update(struct acpi_thermal *tz, u32 event)
  260. {
  261. struct adjust_trip_data atd = { .tz = tz, .event = event };
  262. struct acpi_device *adev = tz->device;
  263. /*
  264. * Use thermal_zone_for_each_trip() to carry out the trip points
  265. * update, so as to protect thermal_get_trend() from getting stale
  266. * trip point temperatures and to prevent thermal_zone_device_update()
  267. * invoked from acpi_thermal_check_fn() from producing inconsistent
  268. * results.
  269. */
  270. thermal_zone_for_each_trip(tz->thermal_zone,
  271. acpi_thermal_adjust_trip, &atd);
  272. acpi_queue_thermal_check(tz);
  273. acpi_bus_generate_netlink_event(adev->pnp.device_class,
  274. dev_name(&adev->dev), event, 0);
  275. }
  276. static int acpi_thermal_get_critical_trip(struct acpi_thermal *tz)
  277. {
  278. int temp;
  279. if (crt > 0) {
  280. temp = celsius_to_deci_kelvin(crt);
  281. goto set;
  282. }
  283. if (crt == -1) {
  284. acpi_handle_debug(tz->device->handle, "Critical threshold disabled\n");
  285. return THERMAL_TEMP_INVALID;
  286. }
  287. if (acpi_critical_trip_temp(tz->device, &temp))
  288. return THERMAL_TEMP_INVALID;
  289. if (temp <= 2732) {
  290. /*
  291. * Below zero (Celsius) values clearly aren't right for sure,
  292. * so discard them as invalid.
  293. */
  294. pr_info(FW_BUG "Invalid critical threshold (%d)\n", temp);
  295. return THERMAL_TEMP_INVALID;
  296. }
  297. set:
  298. acpi_handle_debug(tz->device->handle, "Critical threshold [%d]\n", temp);
  299. return temp;
  300. }
  301. static int acpi_thermal_get_hot_trip(struct acpi_thermal *tz)
  302. {
  303. int temp;
  304. if (acpi_hot_trip_temp(tz->device, &temp) || temp == THERMAL_TEMP_INVALID) {
  305. acpi_handle_debug(tz->device->handle, "No hot threshold\n");
  306. return THERMAL_TEMP_INVALID;
  307. }
  308. acpi_handle_debug(tz->device->handle, "Hot threshold [%d]\n", temp);
  309. return temp;
  310. }
  311. static bool passive_trip_params_init(struct acpi_thermal *tz)
  312. {
  313. unsigned long long tmp;
  314. acpi_status status;
  315. status = acpi_evaluate_integer(tz->device->handle, "_TC1", NULL, &tmp);
  316. if (ACPI_FAILURE(status))
  317. return false;
  318. tz->trips.passive.tc1 = tmp;
  319. status = acpi_evaluate_integer(tz->device->handle, "_TC2", NULL, &tmp);
  320. if (ACPI_FAILURE(status))
  321. return false;
  322. tz->trips.passive.tc2 = tmp;
  323. status = acpi_evaluate_integer(tz->device->handle, "_TFP", NULL, &tmp);
  324. if (ACPI_SUCCESS(status)) {
  325. tz->trips.passive.delay = tmp;
  326. return true;
  327. }
  328. status = acpi_evaluate_integer(tz->device->handle, "_TSP", NULL, &tmp);
  329. if (ACPI_FAILURE(status))
  330. return false;
  331. tz->trips.passive.delay = tmp * 100;
  332. return true;
  333. }
  334. static bool acpi_thermal_init_trip(struct acpi_thermal *tz, int index)
  335. {
  336. struct acpi_thermal_trip *acpi_trip;
  337. long temp;
  338. if (index == ACPI_THERMAL_TRIP_PASSIVE) {
  339. acpi_trip = &tz->trips.passive.trip;
  340. if (psv == -1)
  341. goto fail;
  342. if (!passive_trip_params_init(tz))
  343. goto fail;
  344. temp = psv > 0 ? celsius_to_deci_kelvin(psv) :
  345. get_passive_temp(tz);
  346. } else {
  347. acpi_trip = &tz->trips.active[index].trip;
  348. if (act == -1)
  349. goto fail;
  350. temp = get_active_temp(tz, index);
  351. }
  352. if (temp == THERMAL_TEMP_INVALID)
  353. goto fail;
  354. if (!update_trip_devices(tz, acpi_trip, index, false))
  355. goto fail;
  356. acpi_trip->temp_dk = temp;
  357. return true;
  358. fail:
  359. acpi_trip->temp_dk = THERMAL_TEMP_INVALID;
  360. return false;
  361. }
  362. static void acpi_thermal_get_trip_points(struct acpi_thermal *tz)
  363. {
  364. int i;
  365. acpi_thermal_init_trip(tz, ACPI_THERMAL_TRIP_PASSIVE);
  366. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  367. if (!acpi_thermal_init_trip(tz, i))
  368. break;
  369. }
  370. while (++i < ACPI_THERMAL_MAX_ACTIVE)
  371. tz->trips.active[i].trip.temp_dk = THERMAL_TEMP_INVALID;
  372. }
  373. /* sys I/F for generic thermal sysfs support */
  374. static int thermal_get_temp(struct thermal_zone_device *thermal, int *temp)
  375. {
  376. struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
  377. int result;
  378. if (!tz)
  379. return -EINVAL;
  380. result = acpi_thermal_get_temperature(tz);
  381. if (result)
  382. return result;
  383. *temp = deci_kelvin_to_millicelsius_with_offset(tz->temp_dk,
  384. tz->kelvin_offset);
  385. return 0;
  386. }
  387. static int thermal_get_trend(struct thermal_zone_device *thermal,
  388. const struct thermal_trip *trip,
  389. enum thermal_trend *trend)
  390. {
  391. struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
  392. struct acpi_thermal_trip *acpi_trip;
  393. int t;
  394. if (!tz || !trip)
  395. return -EINVAL;
  396. acpi_trip = trip->priv;
  397. if (!acpi_trip || !acpi_thermal_trip_valid(acpi_trip))
  398. return -EINVAL;
  399. switch (trip->type) {
  400. case THERMAL_TRIP_PASSIVE:
  401. t = tz->trips.passive.tc1 * (tz->temp_dk -
  402. tz->last_temp_dk) +
  403. tz->trips.passive.tc2 * (tz->temp_dk -
  404. acpi_trip->temp_dk);
  405. if (t > 0)
  406. *trend = THERMAL_TREND_RAISING;
  407. else if (t < 0)
  408. *trend = THERMAL_TREND_DROPPING;
  409. else
  410. *trend = THERMAL_TREND_STABLE;
  411. return 0;
  412. case THERMAL_TRIP_ACTIVE:
  413. t = acpi_thermal_temp(tz, tz->temp_dk);
  414. if (t <= trip->temperature)
  415. break;
  416. *trend = THERMAL_TREND_RAISING;
  417. return 0;
  418. default:
  419. break;
  420. }
  421. return -EINVAL;
  422. }
  423. static void acpi_thermal_zone_device_hot(struct thermal_zone_device *thermal)
  424. {
  425. struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
  426. acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
  427. dev_name(&tz->device->dev),
  428. ACPI_THERMAL_NOTIFY_HOT, 1);
  429. }
  430. static void acpi_thermal_zone_device_critical(struct thermal_zone_device *thermal)
  431. {
  432. struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
  433. acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
  434. dev_name(&tz->device->dev),
  435. ACPI_THERMAL_NOTIFY_CRITICAL, 1);
  436. thermal_zone_device_critical(thermal);
  437. }
  438. static bool acpi_thermal_should_bind_cdev(struct thermal_zone_device *thermal,
  439. const struct thermal_trip *trip,
  440. struct thermal_cooling_device *cdev,
  441. struct cooling_spec *c)
  442. {
  443. struct acpi_thermal_trip *acpi_trip = trip->priv;
  444. struct acpi_device *cdev_adev = cdev->devdata;
  445. int i;
  446. /* Skip critical and hot trips. */
  447. if (!acpi_trip)
  448. return false;
  449. for (i = 0; i < acpi_trip->devices.count; i++) {
  450. acpi_handle handle = acpi_trip->devices.handles[i];
  451. if (acpi_fetch_acpi_dev(handle) == cdev_adev)
  452. return true;
  453. }
  454. return false;
  455. }
  456. static const struct thermal_zone_device_ops acpi_thermal_zone_ops = {
  457. .should_bind = acpi_thermal_should_bind_cdev,
  458. .get_temp = thermal_get_temp,
  459. .get_trend = thermal_get_trend,
  460. .hot = acpi_thermal_zone_device_hot,
  461. .critical = acpi_thermal_zone_device_critical,
  462. };
  463. static int acpi_thermal_zone_sysfs_add(struct acpi_thermal *tz)
  464. {
  465. struct device *tzdev = thermal_zone_device(tz->thermal_zone);
  466. int ret;
  467. ret = sysfs_create_link(&tz->device->dev.kobj,
  468. &tzdev->kobj, "thermal_zone");
  469. if (ret)
  470. return ret;
  471. ret = sysfs_create_link(&tzdev->kobj,
  472. &tz->device->dev.kobj, "device");
  473. if (ret)
  474. sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
  475. return ret;
  476. }
  477. static void acpi_thermal_zone_sysfs_remove(struct acpi_thermal *tz)
  478. {
  479. struct device *tzdev = thermal_zone_device(tz->thermal_zone);
  480. sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
  481. sysfs_remove_link(&tzdev->kobj, "device");
  482. }
  483. static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz,
  484. const struct thermal_trip *trip_table,
  485. unsigned int trip_count,
  486. int passive_delay)
  487. {
  488. int result;
  489. if (trip_count)
  490. tz->thermal_zone = thermal_zone_device_register_with_trips(
  491. "acpitz", trip_table, trip_count, tz,
  492. &acpi_thermal_zone_ops, NULL, passive_delay,
  493. tz->polling_frequency * 100);
  494. else
  495. tz->thermal_zone = thermal_tripless_zone_device_register(
  496. "acpitz", tz, &acpi_thermal_zone_ops, NULL);
  497. if (IS_ERR(tz->thermal_zone))
  498. return PTR_ERR(tz->thermal_zone);
  499. result = acpi_thermal_zone_sysfs_add(tz);
  500. if (result)
  501. goto unregister_tzd;
  502. result = thermal_zone_device_enable(tz->thermal_zone);
  503. if (result)
  504. goto remove_links;
  505. dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
  506. thermal_zone_device_id(tz->thermal_zone));
  507. return 0;
  508. remove_links:
  509. acpi_thermal_zone_sysfs_remove(tz);
  510. unregister_tzd:
  511. thermal_zone_device_unregister(tz->thermal_zone);
  512. return result;
  513. }
  514. static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
  515. {
  516. thermal_zone_device_disable(tz->thermal_zone);
  517. acpi_thermal_zone_sysfs_remove(tz);
  518. thermal_zone_device_unregister(tz->thermal_zone);
  519. tz->thermal_zone = NULL;
  520. }
  521. /* --------------------------------------------------------------------------
  522. Driver Interface
  523. -------------------------------------------------------------------------- */
  524. static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
  525. {
  526. struct acpi_device *device = data;
  527. struct acpi_thermal *tz = acpi_driver_data(device);
  528. if (!tz)
  529. return;
  530. switch (event) {
  531. case ACPI_THERMAL_NOTIFY_TEMPERATURE:
  532. acpi_queue_thermal_check(tz);
  533. break;
  534. case ACPI_THERMAL_NOTIFY_THRESHOLDS:
  535. case ACPI_THERMAL_NOTIFY_DEVICES:
  536. acpi_thermal_trips_update(tz, event);
  537. break;
  538. default:
  539. acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
  540. event);
  541. break;
  542. }
  543. }
  544. /*
  545. * On some platforms, the AML code has dependency about
  546. * the evaluating order of _TMP and _CRT/_HOT/_PSV/_ACx.
  547. * 1. On HP Pavilion G4-1016tx, _TMP must be invoked after
  548. * /_CRT/_HOT/_PSV/_ACx, or else system will be power off.
  549. * 2. On HP Compaq 6715b/6715s, the return value of _PSV is 0
  550. * if _TMP has never been evaluated.
  551. *
  552. * As this dependency is totally transparent to OS, evaluate
  553. * all of them once, in the order of _CRT/_HOT/_PSV/_ACx,
  554. * _TMP, before they are actually used.
  555. */
  556. static void acpi_thermal_aml_dependency_fix(struct acpi_thermal *tz)
  557. {
  558. acpi_handle handle = tz->device->handle;
  559. unsigned long long value;
  560. int i;
  561. acpi_evaluate_integer(handle, "_CRT", NULL, &value);
  562. acpi_evaluate_integer(handle, "_HOT", NULL, &value);
  563. acpi_evaluate_integer(handle, "_PSV", NULL, &value);
  564. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  565. char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
  566. acpi_status status;
  567. status = acpi_evaluate_integer(handle, name, NULL, &value);
  568. if (status == AE_NOT_FOUND)
  569. break;
  570. }
  571. acpi_evaluate_integer(handle, "_TMP", NULL, &value);
  572. }
  573. /*
  574. * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
  575. * handles temperature values with a single decimal place. As a consequence,
  576. * some implementations use an offset of 273.1 and others use an offset of
  577. * 273.2. Try to find out which one is being used, to present the most
  578. * accurate and visually appealing number.
  579. *
  580. * The heuristic below should work for all ACPI thermal zones which have a
  581. * critical trip point with a value being a multiple of 0.5 degree Celsius.
  582. */
  583. static void acpi_thermal_guess_offset(struct acpi_thermal *tz, long crit_temp)
  584. {
  585. if (crit_temp != THERMAL_TEMP_INVALID && crit_temp % 5 == 1)
  586. tz->kelvin_offset = 273100;
  587. else
  588. tz->kelvin_offset = 273200;
  589. }
  590. static void acpi_thermal_check_fn(struct work_struct *work)
  591. {
  592. struct acpi_thermal *tz = container_of(work, struct acpi_thermal,
  593. thermal_check_work);
  594. /*
  595. * In general, it is not sufficient to check the pending bit, because
  596. * subsequent instances of this function may be queued after one of them
  597. * has started running (e.g. if _TMP sleeps). Avoid bailing out if just
  598. * one of them is running, though, because it may have done the actual
  599. * check some time ago, so allow at least one of them to block on the
  600. * mutex while another one is running the update.
  601. */
  602. if (!refcount_dec_not_one(&tz->thermal_check_count))
  603. return;
  604. mutex_lock(&tz->thermal_check_lock);
  605. thermal_zone_device_update(tz->thermal_zone, THERMAL_EVENT_UNSPECIFIED);
  606. refcount_inc(&tz->thermal_check_count);
  607. mutex_unlock(&tz->thermal_check_lock);
  608. }
  609. static void acpi_thermal_free_thermal_zone(struct acpi_thermal *tz)
  610. {
  611. int i;
  612. acpi_handle_list_free(&tz->trips.passive.trip.devices);
  613. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
  614. acpi_handle_list_free(&tz->trips.active[i].trip.devices);
  615. kfree(tz);
  616. }
  617. static int acpi_thermal_add(struct acpi_device *device)
  618. {
  619. struct thermal_trip trip_table[ACPI_THERMAL_MAX_NR_TRIPS] = { 0 };
  620. struct acpi_thermal_trip *acpi_trip;
  621. struct thermal_trip *trip;
  622. struct acpi_thermal *tz;
  623. int crit_temp, hot_temp;
  624. int passive_delay = 0;
  625. int result;
  626. int i;
  627. if (!device)
  628. return -EINVAL;
  629. tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
  630. if (!tz)
  631. return -ENOMEM;
  632. tz->device = device;
  633. strcpy(tz->name, device->pnp.bus_id);
  634. strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
  635. strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
  636. device->driver_data = tz;
  637. acpi_thermal_aml_dependency_fix(tz);
  638. /*
  639. * Set the cooling mode [_SCP] to active cooling. This needs to happen before
  640. * we retrieve the trip point values.
  641. */
  642. acpi_execute_simple_method(tz->device->handle, "_SCP", ACPI_THERMAL_MODE_ACTIVE);
  643. /* Get trip points [_ACi, _PSV, etc.] (required). */
  644. acpi_thermal_get_trip_points(tz);
  645. crit_temp = acpi_thermal_get_critical_trip(tz);
  646. hot_temp = acpi_thermal_get_hot_trip(tz);
  647. /* Get temperature [_TMP] (required). */
  648. result = acpi_thermal_get_temperature(tz);
  649. if (result)
  650. goto free_memory;
  651. /* Determine the default polling frequency [_TZP]. */
  652. if (tzp)
  653. tz->polling_frequency = tzp;
  654. else
  655. acpi_thermal_get_polling_frequency(tz);
  656. acpi_thermal_guess_offset(tz, crit_temp);
  657. trip = trip_table;
  658. if (crit_temp != THERMAL_TEMP_INVALID) {
  659. trip->type = THERMAL_TRIP_CRITICAL;
  660. trip->temperature = acpi_thermal_temp(tz, crit_temp);
  661. trip++;
  662. }
  663. if (hot_temp != THERMAL_TEMP_INVALID) {
  664. trip->type = THERMAL_TRIP_HOT;
  665. trip->temperature = acpi_thermal_temp(tz, hot_temp);
  666. trip++;
  667. }
  668. acpi_trip = &tz->trips.passive.trip;
  669. if (acpi_thermal_trip_valid(acpi_trip)) {
  670. passive_delay = tz->trips.passive.delay;
  671. trip->type = THERMAL_TRIP_PASSIVE;
  672. trip->temperature = acpi_thermal_temp(tz, acpi_trip->temp_dk);
  673. trip->priv = acpi_trip;
  674. trip++;
  675. }
  676. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  677. acpi_trip = &tz->trips.active[i].trip;
  678. if (!acpi_thermal_trip_valid(acpi_trip))
  679. break;
  680. trip->type = THERMAL_TRIP_ACTIVE;
  681. trip->temperature = acpi_thermal_temp(tz, acpi_trip->temp_dk);
  682. trip->priv = acpi_trip;
  683. trip++;
  684. }
  685. if (trip == trip_table)
  686. pr_warn(FW_BUG "No valid trip points!\n");
  687. result = acpi_thermal_register_thermal_zone(tz, trip_table,
  688. trip - trip_table,
  689. passive_delay);
  690. if (result)
  691. goto free_memory;
  692. refcount_set(&tz->thermal_check_count, 3);
  693. mutex_init(&tz->thermal_check_lock);
  694. INIT_WORK(&tz->thermal_check_work, acpi_thermal_check_fn);
  695. pr_info("%s [%s] (%ld C)\n", acpi_device_name(device),
  696. acpi_device_bid(device), deci_kelvin_to_celsius(tz->temp_dk));
  697. result = acpi_dev_install_notify_handler(device, ACPI_DEVICE_NOTIFY,
  698. acpi_thermal_notify, device);
  699. if (result)
  700. goto flush_wq;
  701. return 0;
  702. flush_wq:
  703. flush_workqueue(acpi_thermal_pm_queue);
  704. acpi_thermal_unregister_thermal_zone(tz);
  705. free_memory:
  706. acpi_thermal_free_thermal_zone(tz);
  707. return result;
  708. }
  709. static void acpi_thermal_remove(struct acpi_device *device)
  710. {
  711. struct acpi_thermal *tz;
  712. if (!device || !acpi_driver_data(device))
  713. return;
  714. tz = acpi_driver_data(device);
  715. acpi_dev_remove_notify_handler(device, ACPI_DEVICE_NOTIFY,
  716. acpi_thermal_notify);
  717. flush_workqueue(acpi_thermal_pm_queue);
  718. acpi_thermal_unregister_thermal_zone(tz);
  719. acpi_thermal_free_thermal_zone(tz);
  720. }
  721. #ifdef CONFIG_PM_SLEEP
  722. static int acpi_thermal_suspend(struct device *dev)
  723. {
  724. /* Make sure the previously queued thermal check work has been done */
  725. flush_workqueue(acpi_thermal_pm_queue);
  726. return 0;
  727. }
  728. static int acpi_thermal_resume(struct device *dev)
  729. {
  730. struct acpi_thermal *tz;
  731. int i, j, power_state;
  732. if (!dev)
  733. return -EINVAL;
  734. tz = acpi_driver_data(to_acpi_device(dev));
  735. if (!tz)
  736. return -EINVAL;
  737. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  738. struct acpi_thermal_trip *acpi_trip = &tz->trips.active[i].trip;
  739. if (!acpi_thermal_trip_valid(acpi_trip))
  740. break;
  741. for (j = 0; j < acpi_trip->devices.count; j++) {
  742. acpi_bus_update_power(acpi_trip->devices.handles[j],
  743. &power_state);
  744. }
  745. }
  746. acpi_queue_thermal_check(tz);
  747. return AE_OK;
  748. }
  749. #else
  750. #define acpi_thermal_suspend NULL
  751. #define acpi_thermal_resume NULL
  752. #endif
  753. static SIMPLE_DEV_PM_OPS(acpi_thermal_pm, acpi_thermal_suspend, acpi_thermal_resume);
  754. static const struct acpi_device_id thermal_device_ids[] = {
  755. {ACPI_THERMAL_HID, 0},
  756. {"", 0},
  757. };
  758. MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
  759. static struct acpi_driver acpi_thermal_driver = {
  760. .name = "thermal",
  761. .class = ACPI_THERMAL_CLASS,
  762. .ids = thermal_device_ids,
  763. .ops = {
  764. .add = acpi_thermal_add,
  765. .remove = acpi_thermal_remove,
  766. },
  767. .drv.pm = &acpi_thermal_pm,
  768. };
  769. static int thermal_act(const struct dmi_system_id *d)
  770. {
  771. if (act == 0) {
  772. pr_notice("%s detected: disabling all active thermal trip points\n",
  773. d->ident);
  774. act = -1;
  775. }
  776. return 0;
  777. }
  778. static int thermal_nocrt(const struct dmi_system_id *d)
  779. {
  780. pr_notice("%s detected: disabling all critical thermal trip point actions.\n",
  781. d->ident);
  782. crt = -1;
  783. return 0;
  784. }
  785. static int thermal_tzp(const struct dmi_system_id *d)
  786. {
  787. if (tzp == 0) {
  788. pr_notice("%s detected: enabling thermal zone polling\n",
  789. d->ident);
  790. tzp = 300; /* 300 dS = 30 Seconds */
  791. }
  792. return 0;
  793. }
  794. static int thermal_psv(const struct dmi_system_id *d)
  795. {
  796. if (psv == 0) {
  797. pr_notice("%s detected: disabling all passive thermal trip points\n",
  798. d->ident);
  799. psv = -1;
  800. }
  801. return 0;
  802. }
  803. static const struct dmi_system_id thermal_dmi_table[] __initconst = {
  804. /*
  805. * Award BIOS on this AOpen makes thermal control almost worthless.
  806. * http://bugzilla.kernel.org/show_bug.cgi?id=8842
  807. */
  808. {
  809. .callback = thermal_act,
  810. .ident = "AOpen i915GMm-HFS",
  811. .matches = {
  812. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  813. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  814. },
  815. },
  816. {
  817. .callback = thermal_psv,
  818. .ident = "AOpen i915GMm-HFS",
  819. .matches = {
  820. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  821. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  822. },
  823. },
  824. {
  825. .callback = thermal_tzp,
  826. .ident = "AOpen i915GMm-HFS",
  827. .matches = {
  828. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  829. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  830. },
  831. },
  832. {
  833. .callback = thermal_nocrt,
  834. .ident = "Gigabyte GA-7ZX",
  835. .matches = {
  836. DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
  837. DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
  838. },
  839. },
  840. {}
  841. };
  842. static int __init acpi_thermal_init(void)
  843. {
  844. int result;
  845. dmi_check_system(thermal_dmi_table);
  846. if (off) {
  847. pr_notice("thermal control disabled\n");
  848. return -ENODEV;
  849. }
  850. acpi_thermal_pm_queue = alloc_workqueue("acpi_thermal_pm",
  851. WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
  852. if (!acpi_thermal_pm_queue)
  853. return -ENODEV;
  854. result = acpi_bus_register_driver(&acpi_thermal_driver);
  855. if (result < 0) {
  856. destroy_workqueue(acpi_thermal_pm_queue);
  857. return -ENODEV;
  858. }
  859. return 0;
  860. }
  861. static void __exit acpi_thermal_exit(void)
  862. {
  863. acpi_bus_unregister_driver(&acpi_thermal_driver);
  864. destroy_workqueue(acpi_thermal_pm_queue);
  865. }
  866. module_init(acpi_thermal_init);
  867. module_exit(acpi_thermal_exit);
  868. MODULE_IMPORT_NS(ACPI_THERMAL);
  869. MODULE_AUTHOR("Paul Diefenbaugh");
  870. MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
  871. MODULE_LICENSE("GPL");