thermal.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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/platform_device.h>
  27. #include <linux/thermal.h>
  28. #include <linux/acpi.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/units.h>
  32. #include "internal.h"
  33. #define ACPI_THERMAL_CLASS "thermal_zone"
  34. #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
  35. #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
  36. #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
  37. #define ACPI_THERMAL_NOTIFY_DEVICES 0x82
  38. #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
  39. #define ACPI_THERMAL_NOTIFY_HOT 0xF1
  40. #define ACPI_THERMAL_MODE_ACTIVE 0x00
  41. #define ACPI_THERMAL_MAX_ACTIVE 10
  42. #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
  43. #define ACPI_THERMAL_TRIP_PASSIVE (-1)
  44. #define ACPI_THERMAL_MAX_NR_TRIPS (ACPI_THERMAL_MAX_ACTIVE + 3)
  45. /*
  46. * This exception is thrown out in two cases:
  47. * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
  48. * when re-evaluating the AML code.
  49. * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
  50. * We need to re-bind the cooling devices of a thermal zone when this occurs.
  51. */
  52. #define ACPI_THERMAL_TRIPS_EXCEPTION(tz, str) \
  53. do { \
  54. acpi_handle_info(tz->device->handle, \
  55. "ACPI thermal trip point %s changed\n" \
  56. "Please report to linux-acpi@vger.kernel.org\n", str); \
  57. } while (0)
  58. static int act;
  59. module_param(act, int, 0644);
  60. MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
  61. static int crt;
  62. module_param(crt, int, 0644);
  63. MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
  64. static int tzp;
  65. module_param(tzp, int, 0444);
  66. MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
  67. static int off;
  68. module_param(off, int, 0);
  69. MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
  70. static int psv;
  71. module_param(psv, int, 0644);
  72. MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
  73. static struct workqueue_struct *acpi_thermal_pm_queue;
  74. struct acpi_thermal_trip {
  75. unsigned long temp_dk;
  76. struct acpi_handle_list devices;
  77. };
  78. struct acpi_thermal_passive {
  79. struct acpi_thermal_trip trip;
  80. unsigned long tc1;
  81. unsigned long tc2;
  82. unsigned long delay;
  83. };
  84. struct acpi_thermal_active {
  85. struct acpi_thermal_trip trip;
  86. };
  87. struct acpi_thermal_trips {
  88. struct acpi_thermal_passive passive;
  89. struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
  90. };
  91. struct acpi_thermal {
  92. struct acpi_device *device;
  93. acpi_bus_id name;
  94. unsigned long temp_dk;
  95. unsigned long last_temp_dk;
  96. unsigned long polling_frequency;
  97. volatile u8 zombie;
  98. struct acpi_thermal_trips trips;
  99. struct thermal_zone_device *thermal_zone;
  100. int kelvin_offset; /* in millidegrees */
  101. struct work_struct thermal_check_work;
  102. struct mutex thermal_check_lock;
  103. refcount_t thermal_check_count;
  104. };
  105. /* --------------------------------------------------------------------------
  106. Thermal Zone Management
  107. -------------------------------------------------------------------------- */
  108. static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
  109. {
  110. acpi_status status = AE_OK;
  111. unsigned long long tmp;
  112. if (!tz)
  113. return -EINVAL;
  114. tz->last_temp_dk = tz->temp_dk;
  115. status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
  116. if (ACPI_FAILURE(status))
  117. return -ENODEV;
  118. tz->temp_dk = tmp;
  119. acpi_handle_debug(tz->device->handle, "Temperature is %lu dK\n",
  120. tz->temp_dk);
  121. return 0;
  122. }
  123. static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
  124. {
  125. acpi_status status = AE_OK;
  126. unsigned long long tmp;
  127. if (!tz)
  128. return -EINVAL;
  129. status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
  130. if (ACPI_FAILURE(status))
  131. return -ENODEV;
  132. tz->polling_frequency = tmp;
  133. acpi_handle_debug(tz->device->handle, "Polling frequency is %lu dS\n",
  134. tz->polling_frequency);
  135. return 0;
  136. }
  137. static int acpi_thermal_temp(struct acpi_thermal *tz, int temp_deci_k)
  138. {
  139. int temp;
  140. if (temp_deci_k == THERMAL_TEMP_INVALID)
  141. return THERMAL_TEMP_INVALID;
  142. temp = deci_kelvin_to_millicelsius_with_offset(temp_deci_k,
  143. tz->kelvin_offset);
  144. if (temp <= 0)
  145. return THERMAL_TEMP_INVALID;
  146. return temp;
  147. }
  148. static bool acpi_thermal_trip_valid(struct acpi_thermal_trip *acpi_trip)
  149. {
  150. return acpi_trip->temp_dk != THERMAL_TEMP_INVALID;
  151. }
  152. static int active_trip_index(struct acpi_thermal *tz,
  153. struct acpi_thermal_trip *acpi_trip)
  154. {
  155. struct acpi_thermal_active *active;
  156. active = container_of(acpi_trip, struct acpi_thermal_active, trip);
  157. return active - tz->trips.active;
  158. }
  159. static long get_passive_temp(struct acpi_thermal *tz)
  160. {
  161. int temp;
  162. if (acpi_passive_trip_temp(tz->device, &temp))
  163. return THERMAL_TEMP_INVALID;
  164. return temp;
  165. }
  166. static long get_active_temp(struct acpi_thermal *tz, int index)
  167. {
  168. int temp;
  169. if (acpi_active_trip_temp(tz->device, index, &temp))
  170. return THERMAL_TEMP_INVALID;
  171. /*
  172. * If an override has been provided, apply it so there are no active
  173. * trips with thresholds greater than the override.
  174. */
  175. if (act > 0) {
  176. unsigned long long override = celsius_to_deci_kelvin(act);
  177. if (temp > override)
  178. return override;
  179. }
  180. return temp;
  181. }
  182. static void acpi_thermal_update_trip(struct acpi_thermal *tz,
  183. const struct thermal_trip *trip)
  184. {
  185. struct acpi_thermal_trip *acpi_trip = trip->priv;
  186. if (trip->type == THERMAL_TRIP_PASSIVE) {
  187. if (psv > 0)
  188. return;
  189. acpi_trip->temp_dk = get_passive_temp(tz);
  190. } else {
  191. int index = active_trip_index(tz, acpi_trip);
  192. acpi_trip->temp_dk = get_active_temp(tz, index);
  193. }
  194. if (!acpi_thermal_trip_valid(acpi_trip))
  195. ACPI_THERMAL_TRIPS_EXCEPTION(tz, "state");
  196. }
  197. static bool update_trip_devices(struct acpi_thermal *tz,
  198. struct acpi_thermal_trip *acpi_trip,
  199. int index, bool compare)
  200. {
  201. struct acpi_handle_list devices = { 0 };
  202. char method[] = "_PSL";
  203. if (index != ACPI_THERMAL_TRIP_PASSIVE) {
  204. method[1] = 'A';
  205. method[2] = 'L';
  206. method[3] = '0' + index;
  207. }
  208. if (!acpi_evaluate_reference(tz->device->handle, method, NULL, &devices)) {
  209. acpi_handle_info(tz->device->handle, "%s evaluation failure\n", method);
  210. return false;
  211. }
  212. if (acpi_handle_list_equal(&acpi_trip->devices, &devices)) {
  213. acpi_handle_list_free(&devices);
  214. return true;
  215. }
  216. if (compare)
  217. ACPI_THERMAL_TRIPS_EXCEPTION(tz, "device");
  218. acpi_handle_list_replace(&acpi_trip->devices, &devices);
  219. return true;
  220. }
  221. static void acpi_thermal_update_trip_devices(struct acpi_thermal *tz,
  222. const struct thermal_trip *trip)
  223. {
  224. struct acpi_thermal_trip *acpi_trip = trip->priv;
  225. int index = trip->type == THERMAL_TRIP_PASSIVE ?
  226. ACPI_THERMAL_TRIP_PASSIVE : active_trip_index(tz, acpi_trip);
  227. if (update_trip_devices(tz, acpi_trip, index, true))
  228. return;
  229. acpi_trip->temp_dk = THERMAL_TEMP_INVALID;
  230. ACPI_THERMAL_TRIPS_EXCEPTION(tz, "state");
  231. }
  232. struct adjust_trip_data {
  233. struct acpi_thermal *tz;
  234. u32 event;
  235. };
  236. static int acpi_thermal_adjust_trip(struct thermal_trip *trip, void *data)
  237. {
  238. struct acpi_thermal_trip *acpi_trip = trip->priv;
  239. struct adjust_trip_data *atd = data;
  240. struct acpi_thermal *tz = atd->tz;
  241. int temp;
  242. if (!acpi_trip || !acpi_thermal_trip_valid(acpi_trip))
  243. return 0;
  244. if (atd->event == ACPI_THERMAL_NOTIFY_THRESHOLDS)
  245. acpi_thermal_update_trip(tz, trip);
  246. else
  247. acpi_thermal_update_trip_devices(tz, trip);
  248. if (acpi_thermal_trip_valid(acpi_trip))
  249. temp = acpi_thermal_temp(tz, acpi_trip->temp_dk);
  250. else
  251. temp = THERMAL_TEMP_INVALID;
  252. thermal_zone_set_trip_temp(tz->thermal_zone, trip, temp);
  253. return 0;
  254. }
  255. static void acpi_queue_thermal_check(struct acpi_thermal *tz)
  256. {
  257. if (!work_pending(&tz->thermal_check_work))
  258. queue_work(acpi_thermal_pm_queue, &tz->thermal_check_work);
  259. }
  260. static void acpi_thermal_trips_update(struct acpi_thermal *tz, u32 event)
  261. {
  262. struct adjust_trip_data atd = { .tz = tz, .event = event };
  263. struct acpi_device *adev = tz->device;
  264. /*
  265. * Use thermal_zone_for_each_trip() to carry out the trip points
  266. * update, so as to protect thermal_get_trend() from getting stale
  267. * trip point temperatures and to prevent thermal_zone_device_update()
  268. * invoked from acpi_thermal_check_fn() from producing inconsistent
  269. * results.
  270. */
  271. thermal_zone_for_each_trip(tz->thermal_zone,
  272. acpi_thermal_adjust_trip, &atd);
  273. acpi_queue_thermal_check(tz);
  274. acpi_bus_generate_netlink_event(adev->pnp.device_class,
  275. dev_name(&adev->dev), event, 0);
  276. }
  277. static int acpi_thermal_get_critical_trip(struct acpi_thermal *tz)
  278. {
  279. int temp;
  280. if (crt > 0) {
  281. temp = celsius_to_deci_kelvin(crt);
  282. goto set;
  283. }
  284. if (crt == -1) {
  285. acpi_handle_debug(tz->device->handle, "Critical threshold disabled\n");
  286. return THERMAL_TEMP_INVALID;
  287. }
  288. if (acpi_critical_trip_temp(tz->device, &temp))
  289. return THERMAL_TEMP_INVALID;
  290. if (temp <= 2732) {
  291. /*
  292. * Below zero (Celsius) values clearly aren't right for sure,
  293. * so discard them as invalid.
  294. */
  295. pr_info(FW_BUG "Invalid critical threshold (%d)\n", temp);
  296. return THERMAL_TEMP_INVALID;
  297. }
  298. set:
  299. acpi_handle_debug(tz->device->handle, "Critical threshold [%d]\n", temp);
  300. return temp;
  301. }
  302. static int acpi_thermal_get_hot_trip(struct acpi_thermal *tz)
  303. {
  304. int temp;
  305. if (acpi_hot_trip_temp(tz->device, &temp) || temp == THERMAL_TEMP_INVALID) {
  306. acpi_handle_debug(tz->device->handle, "No hot threshold\n");
  307. return THERMAL_TEMP_INVALID;
  308. }
  309. acpi_handle_debug(tz->device->handle, "Hot threshold [%d]\n", temp);
  310. return temp;
  311. }
  312. static bool passive_trip_params_init(struct acpi_thermal *tz)
  313. {
  314. unsigned long long tmp;
  315. acpi_status status;
  316. status = acpi_evaluate_integer(tz->device->handle, "_TC1", NULL, &tmp);
  317. if (ACPI_FAILURE(status))
  318. return false;
  319. tz->trips.passive.tc1 = tmp;
  320. status = acpi_evaluate_integer(tz->device->handle, "_TC2", NULL, &tmp);
  321. if (ACPI_FAILURE(status))
  322. return false;
  323. tz->trips.passive.tc2 = tmp;
  324. status = acpi_evaluate_integer(tz->device->handle, "_TFP", NULL, &tmp);
  325. if (ACPI_SUCCESS(status)) {
  326. tz->trips.passive.delay = tmp;
  327. return true;
  328. }
  329. status = acpi_evaluate_integer(tz->device->handle, "_TSP", NULL, &tmp);
  330. if (ACPI_FAILURE(status))
  331. return false;
  332. tz->trips.passive.delay = tmp * 100;
  333. return true;
  334. }
  335. static bool acpi_thermal_init_trip(struct acpi_thermal *tz, int index)
  336. {
  337. struct acpi_thermal_trip *acpi_trip;
  338. long temp;
  339. if (index == ACPI_THERMAL_TRIP_PASSIVE) {
  340. acpi_trip = &tz->trips.passive.trip;
  341. if (psv == -1)
  342. goto fail;
  343. if (!passive_trip_params_init(tz))
  344. goto fail;
  345. temp = psv > 0 ? celsius_to_deci_kelvin(psv) :
  346. get_passive_temp(tz);
  347. } else {
  348. acpi_trip = &tz->trips.active[index].trip;
  349. if (act == -1)
  350. goto fail;
  351. temp = get_active_temp(tz, index);
  352. }
  353. if (temp == THERMAL_TEMP_INVALID)
  354. goto fail;
  355. if (!update_trip_devices(tz, acpi_trip, index, false))
  356. goto fail;
  357. acpi_trip->temp_dk = temp;
  358. return true;
  359. fail:
  360. acpi_trip->temp_dk = THERMAL_TEMP_INVALID;
  361. return false;
  362. }
  363. static void acpi_thermal_get_trip_points(struct acpi_thermal *tz)
  364. {
  365. int i;
  366. acpi_thermal_init_trip(tz, ACPI_THERMAL_TRIP_PASSIVE);
  367. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  368. if (!acpi_thermal_init_trip(tz, i))
  369. break;
  370. }
  371. while (++i < ACPI_THERMAL_MAX_ACTIVE)
  372. tz->trips.active[i].trip.temp_dk = THERMAL_TEMP_INVALID;
  373. }
  374. /* sys I/F for generic thermal sysfs support */
  375. static int thermal_get_temp(struct thermal_zone_device *thermal, int *temp)
  376. {
  377. struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
  378. int result;
  379. if (!tz)
  380. return -EINVAL;
  381. result = acpi_thermal_get_temperature(tz);
  382. if (result)
  383. return result;
  384. *temp = deci_kelvin_to_millicelsius_with_offset(tz->temp_dk,
  385. tz->kelvin_offset);
  386. return 0;
  387. }
  388. static int thermal_get_trend(struct thermal_zone_device *thermal,
  389. const struct thermal_trip *trip,
  390. enum thermal_trend *trend)
  391. {
  392. struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
  393. struct acpi_thermal_trip *acpi_trip;
  394. int t;
  395. if (!tz || !trip)
  396. return -EINVAL;
  397. acpi_trip = trip->priv;
  398. if (!acpi_trip || !acpi_thermal_trip_valid(acpi_trip))
  399. return -EINVAL;
  400. switch (trip->type) {
  401. case THERMAL_TRIP_PASSIVE:
  402. t = tz->trips.passive.tc1 * (tz->temp_dk -
  403. tz->last_temp_dk) +
  404. tz->trips.passive.tc2 * (tz->temp_dk -
  405. acpi_trip->temp_dk);
  406. if (t > 0)
  407. *trend = THERMAL_TREND_RAISING;
  408. else if (t < 0)
  409. *trend = THERMAL_TREND_DROPPING;
  410. else
  411. *trend = THERMAL_TREND_STABLE;
  412. return 0;
  413. case THERMAL_TRIP_ACTIVE:
  414. t = acpi_thermal_temp(tz, tz->temp_dk);
  415. if (t <= trip->temperature)
  416. break;
  417. *trend = THERMAL_TREND_RAISING;
  418. return 0;
  419. default:
  420. break;
  421. }
  422. return -EINVAL;
  423. }
  424. static void acpi_thermal_zone_device_hot(struct thermal_zone_device *thermal)
  425. {
  426. struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
  427. acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
  428. dev_name(&tz->device->dev),
  429. ACPI_THERMAL_NOTIFY_HOT, 1);
  430. }
  431. static void acpi_thermal_zone_device_critical(struct thermal_zone_device *thermal)
  432. {
  433. struct acpi_thermal *tz = thermal_zone_device_priv(thermal);
  434. acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
  435. dev_name(&tz->device->dev),
  436. ACPI_THERMAL_NOTIFY_CRITICAL, 1);
  437. thermal_zone_device_critical(thermal);
  438. }
  439. static bool acpi_thermal_should_bind_cdev(struct thermal_zone_device *thermal,
  440. const struct thermal_trip *trip,
  441. struct thermal_cooling_device *cdev,
  442. struct cooling_spec *c)
  443. {
  444. struct acpi_thermal_trip *acpi_trip = trip->priv;
  445. struct acpi_device *cdev_adev = cdev->devdata;
  446. int i;
  447. /* Skip critical and hot trips. */
  448. if (!acpi_trip)
  449. return false;
  450. for (i = 0; i < acpi_trip->devices.count; i++) {
  451. acpi_handle handle = acpi_trip->devices.handles[i];
  452. if (acpi_fetch_acpi_dev(handle) == cdev_adev)
  453. return true;
  454. }
  455. return false;
  456. }
  457. static const struct thermal_zone_device_ops acpi_thermal_zone_ops = {
  458. .should_bind = acpi_thermal_should_bind_cdev,
  459. .get_temp = thermal_get_temp,
  460. .get_trend = thermal_get_trend,
  461. .hot = acpi_thermal_zone_device_hot,
  462. .critical = acpi_thermal_zone_device_critical,
  463. };
  464. static int acpi_thermal_zone_sysfs_add(struct acpi_thermal *tz)
  465. {
  466. struct device *tzdev = thermal_zone_device(tz->thermal_zone);
  467. int ret;
  468. ret = sysfs_create_link(&tz->device->dev.kobj,
  469. &tzdev->kobj, "thermal_zone");
  470. if (ret)
  471. return ret;
  472. ret = sysfs_create_link(&tzdev->kobj,
  473. &tz->device->dev.kobj, "device");
  474. if (ret)
  475. sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
  476. return ret;
  477. }
  478. static void acpi_thermal_zone_sysfs_remove(struct acpi_thermal *tz)
  479. {
  480. struct device *tzdev = thermal_zone_device(tz->thermal_zone);
  481. sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
  482. sysfs_remove_link(&tzdev->kobj, "device");
  483. }
  484. static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz,
  485. const struct thermal_trip *trip_table,
  486. unsigned int trip_count,
  487. int passive_delay)
  488. {
  489. int result;
  490. if (trip_count)
  491. tz->thermal_zone = thermal_zone_device_register_with_trips(
  492. "acpitz", trip_table, trip_count, tz,
  493. &acpi_thermal_zone_ops, NULL, passive_delay,
  494. tz->polling_frequency * 100);
  495. else
  496. tz->thermal_zone = thermal_tripless_zone_device_register(
  497. "acpitz", tz, &acpi_thermal_zone_ops, NULL);
  498. if (IS_ERR(tz->thermal_zone))
  499. return PTR_ERR(tz->thermal_zone);
  500. result = acpi_thermal_zone_sysfs_add(tz);
  501. if (result)
  502. goto unregister_tzd;
  503. result = thermal_zone_device_enable(tz->thermal_zone);
  504. if (result)
  505. goto remove_links;
  506. dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
  507. thermal_zone_device_id(tz->thermal_zone));
  508. return 0;
  509. remove_links:
  510. acpi_thermal_zone_sysfs_remove(tz);
  511. unregister_tzd:
  512. thermal_zone_device_unregister(tz->thermal_zone);
  513. return result;
  514. }
  515. static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
  516. {
  517. thermal_zone_device_disable(tz->thermal_zone);
  518. acpi_thermal_zone_sysfs_remove(tz);
  519. thermal_zone_device_unregister(tz->thermal_zone);
  520. tz->thermal_zone = NULL;
  521. }
  522. /* --------------------------------------------------------------------------
  523. Driver Interface
  524. -------------------------------------------------------------------------- */
  525. static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
  526. {
  527. struct acpi_thermal *tz = data;
  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(tz->device->handle,
  540. "Unsupported event [0x%x]\n", 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_probe(struct platform_device *pdev)
  618. {
  619. struct thermal_trip trip_table[ACPI_THERMAL_MAX_NR_TRIPS] = { 0 };
  620. struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
  621. struct acpi_thermal_trip *acpi_trip;
  622. struct thermal_trip *trip;
  623. struct acpi_thermal *tz;
  624. int crit_temp, hot_temp;
  625. int passive_delay = 0;
  626. int result;
  627. int i;
  628. if (!device)
  629. return -EINVAL;
  630. tz = kzalloc_obj(struct acpi_thermal);
  631. if (!tz)
  632. return -ENOMEM;
  633. platform_set_drvdata(pdev, tz);
  634. tz->device = device;
  635. strscpy(tz->name, device->pnp.bus_id);
  636. strscpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
  637. strscpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
  638. acpi_thermal_aml_dependency_fix(tz);
  639. /*
  640. * Set the cooling mode [_SCP] to active cooling. This needs to happen before
  641. * we retrieve the trip point values.
  642. */
  643. acpi_execute_simple_method(tz->device->handle, "_SCP", ACPI_THERMAL_MODE_ACTIVE);
  644. /* Get trip points [_ACi, _PSV, etc.] (required). */
  645. acpi_thermal_get_trip_points(tz);
  646. crit_temp = acpi_thermal_get_critical_trip(tz);
  647. hot_temp = acpi_thermal_get_hot_trip(tz);
  648. /* Get temperature [_TMP] (required). */
  649. result = acpi_thermal_get_temperature(tz);
  650. if (result)
  651. goto free_memory;
  652. /* Determine the default polling frequency [_TZP]. */
  653. if (tzp)
  654. tz->polling_frequency = tzp;
  655. else
  656. acpi_thermal_get_polling_frequency(tz);
  657. acpi_thermal_guess_offset(tz, crit_temp);
  658. trip = trip_table;
  659. if (crit_temp != THERMAL_TEMP_INVALID) {
  660. trip->type = THERMAL_TRIP_CRITICAL;
  661. trip->temperature = acpi_thermal_temp(tz, crit_temp);
  662. trip++;
  663. }
  664. if (hot_temp != THERMAL_TEMP_INVALID) {
  665. trip->type = THERMAL_TRIP_HOT;
  666. trip->temperature = acpi_thermal_temp(tz, hot_temp);
  667. trip++;
  668. }
  669. acpi_trip = &tz->trips.passive.trip;
  670. if (acpi_thermal_trip_valid(acpi_trip)) {
  671. passive_delay = tz->trips.passive.delay;
  672. trip->type = THERMAL_TRIP_PASSIVE;
  673. trip->temperature = acpi_thermal_temp(tz, acpi_trip->temp_dk);
  674. trip->priv = acpi_trip;
  675. trip++;
  676. }
  677. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  678. acpi_trip = &tz->trips.active[i].trip;
  679. if (!acpi_thermal_trip_valid(acpi_trip))
  680. break;
  681. trip->type = THERMAL_TRIP_ACTIVE;
  682. trip->temperature = acpi_thermal_temp(tz, acpi_trip->temp_dk);
  683. trip->priv = acpi_trip;
  684. trip++;
  685. }
  686. if (trip == trip_table)
  687. pr_warn(FW_BUG "No valid trip points!\n");
  688. result = acpi_thermal_register_thermal_zone(tz, trip_table,
  689. trip - trip_table,
  690. passive_delay);
  691. if (result)
  692. goto free_memory;
  693. refcount_set(&tz->thermal_check_count, 3);
  694. mutex_init(&tz->thermal_check_lock);
  695. INIT_WORK(&tz->thermal_check_work, acpi_thermal_check_fn);
  696. pr_info("%s [%s] (%ld C)\n", acpi_device_name(device),
  697. acpi_device_bid(device), deci_kelvin_to_celsius(tz->temp_dk));
  698. result = acpi_dev_install_notify_handler(device, ACPI_DEVICE_NOTIFY,
  699. acpi_thermal_notify, tz);
  700. if (result)
  701. goto flush_wq;
  702. return 0;
  703. flush_wq:
  704. flush_workqueue(acpi_thermal_pm_queue);
  705. acpi_thermal_unregister_thermal_zone(tz);
  706. free_memory:
  707. acpi_thermal_free_thermal_zone(tz);
  708. return result;
  709. }
  710. static void acpi_thermal_remove(struct platform_device *pdev)
  711. {
  712. struct acpi_thermal *tz = platform_get_drvdata(pdev);
  713. acpi_dev_remove_notify_handler(tz->device, ACPI_DEVICE_NOTIFY,
  714. acpi_thermal_notify);
  715. flush_workqueue(acpi_thermal_pm_queue);
  716. acpi_thermal_unregister_thermal_zone(tz);
  717. acpi_thermal_free_thermal_zone(tz);
  718. }
  719. #ifdef CONFIG_PM_SLEEP
  720. static int acpi_thermal_prepare(struct device *dev)
  721. {
  722. /* Make sure the previously queued thermal check work has been done */
  723. flush_workqueue(acpi_thermal_pm_queue);
  724. return 0;
  725. }
  726. static void acpi_thermal_complete(struct device *dev)
  727. {
  728. acpi_queue_thermal_check(dev_get_drvdata(dev));
  729. }
  730. static const struct dev_pm_ops acpi_thermal_pm_ops = {
  731. .prepare = acpi_thermal_prepare,
  732. .complete = acpi_thermal_complete,
  733. };
  734. #define ACPI_THERMAL_PM &acpi_thermal_pm_ops
  735. #else /* !CONFIG_PM_SLEEP */
  736. #define ACPI_THERMAL_PM NULL
  737. #endif /* CONFIG_PM_SLEEP */
  738. static const struct acpi_device_id thermal_device_ids[] = {
  739. {ACPI_THERMAL_HID, 0},
  740. {"", 0},
  741. };
  742. MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
  743. static struct platform_driver acpi_thermal_driver = {
  744. .probe = acpi_thermal_probe,
  745. .remove = acpi_thermal_remove,
  746. .driver = {
  747. .name = "acpi-thermal",
  748. .acpi_match_table = thermal_device_ids,
  749. .pm = ACPI_THERMAL_PM,
  750. },
  751. };
  752. static int thermal_act(const struct dmi_system_id *d)
  753. {
  754. if (act == 0) {
  755. pr_notice("%s detected: disabling all active thermal trip points\n",
  756. d->ident);
  757. act = -1;
  758. }
  759. return 0;
  760. }
  761. static int thermal_nocrt(const struct dmi_system_id *d)
  762. {
  763. pr_notice("%s detected: disabling all critical thermal trip point actions.\n",
  764. d->ident);
  765. crt = -1;
  766. return 0;
  767. }
  768. static int thermal_tzp(const struct dmi_system_id *d)
  769. {
  770. if (tzp == 0) {
  771. pr_notice("%s detected: enabling thermal zone polling\n",
  772. d->ident);
  773. tzp = 300; /* 300 dS = 30 Seconds */
  774. }
  775. return 0;
  776. }
  777. static int thermal_psv(const struct dmi_system_id *d)
  778. {
  779. if (psv == 0) {
  780. pr_notice("%s detected: disabling all passive thermal trip points\n",
  781. d->ident);
  782. psv = -1;
  783. }
  784. return 0;
  785. }
  786. static const struct dmi_system_id thermal_dmi_table[] __initconst = {
  787. /*
  788. * Award BIOS on this AOpen makes thermal control almost worthless.
  789. * http://bugzilla.kernel.org/show_bug.cgi?id=8842
  790. */
  791. {
  792. .callback = thermal_act,
  793. .ident = "AOpen i915GMm-HFS",
  794. .matches = {
  795. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  796. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  797. },
  798. },
  799. {
  800. .callback = thermal_psv,
  801. .ident = "AOpen i915GMm-HFS",
  802. .matches = {
  803. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  804. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  805. },
  806. },
  807. {
  808. .callback = thermal_tzp,
  809. .ident = "AOpen i915GMm-HFS",
  810. .matches = {
  811. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  812. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  813. },
  814. },
  815. {
  816. .callback = thermal_nocrt,
  817. .ident = "Gigabyte GA-7ZX",
  818. .matches = {
  819. DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
  820. DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
  821. },
  822. },
  823. {}
  824. };
  825. static int __init acpi_thermal_init(void)
  826. {
  827. int result;
  828. dmi_check_system(thermal_dmi_table);
  829. if (off) {
  830. pr_notice("thermal control disabled\n");
  831. return -ENODEV;
  832. }
  833. acpi_thermal_pm_queue = alloc_workqueue("acpi_thermal_pm",
  834. WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_PERCPU,
  835. 0);
  836. if (!acpi_thermal_pm_queue)
  837. return -ENODEV;
  838. result = platform_driver_register(&acpi_thermal_driver);
  839. if (result < 0) {
  840. destroy_workqueue(acpi_thermal_pm_queue);
  841. return -ENODEV;
  842. }
  843. return 0;
  844. }
  845. static void __exit acpi_thermal_exit(void)
  846. {
  847. platform_driver_unregister(&acpi_thermal_driver);
  848. destroy_workqueue(acpi_thermal_pm_queue);
  849. }
  850. module_init(acpi_thermal_init);
  851. module_exit(acpi_thermal_exit);
  852. MODULE_IMPORT_NS("ACPI_THERMAL");
  853. MODULE_AUTHOR("Paul Diefenbaugh");
  854. MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
  855. MODULE_LICENSE("GPL");