thermal.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  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 <linux/thermal.h>
  44. #include <linux/acpi.h>
  45. #include <asm/uaccess.h>
  46. #define PREFIX "ACPI: "
  47. #define ACPI_THERMAL_CLASS "thermal_zone"
  48. #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
  49. #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
  50. #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
  51. #define ACPI_THERMAL_NOTIFY_DEVICES 0x82
  52. #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
  53. #define ACPI_THERMAL_NOTIFY_HOT 0xF1
  54. #define ACPI_THERMAL_MODE_ACTIVE 0x00
  55. #define ACPI_THERMAL_MAX_ACTIVE 10
  56. #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
  57. #define _COMPONENT ACPI_THERMAL_COMPONENT
  58. ACPI_MODULE_NAME("thermal");
  59. MODULE_AUTHOR("Paul Diefenbaugh");
  60. MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
  61. MODULE_LICENSE("GPL");
  62. static int act;
  63. module_param(act, int, 0644);
  64. MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
  65. static int crt;
  66. module_param(crt, int, 0644);
  67. MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
  68. static int tzp;
  69. module_param(tzp, int, 0444);
  70. MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
  71. static int nocrt;
  72. module_param(nocrt, int, 0);
  73. MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
  74. static int off;
  75. module_param(off, int, 0);
  76. MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
  77. static int psv;
  78. module_param(psv, int, 0644);
  79. MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
  80. static int acpi_thermal_add(struct acpi_device *device);
  81. static int acpi_thermal_remove(struct acpi_device *device);
  82. static void acpi_thermal_notify(struct acpi_device *device, u32 event);
  83. static const struct acpi_device_id thermal_device_ids[] = {
  84. {ACPI_THERMAL_HID, 0},
  85. {"", 0},
  86. };
  87. MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
  88. #ifdef CONFIG_PM_SLEEP
  89. static int acpi_thermal_resume(struct device *dev);
  90. #else
  91. #define acpi_thermal_resume NULL
  92. #endif
  93. static SIMPLE_DEV_PM_OPS(acpi_thermal_pm, NULL, acpi_thermal_resume);
  94. static struct acpi_driver acpi_thermal_driver = {
  95. .name = "thermal",
  96. .class = ACPI_THERMAL_CLASS,
  97. .ids = thermal_device_ids,
  98. .ops = {
  99. .add = acpi_thermal_add,
  100. .remove = acpi_thermal_remove,
  101. .notify = acpi_thermal_notify,
  102. },
  103. .drv.pm = &acpi_thermal_pm,
  104. };
  105. struct acpi_thermal_state {
  106. u8 critical:1;
  107. u8 hot:1;
  108. u8 passive:1;
  109. u8 active:1;
  110. u8 reserved:4;
  111. int active_index;
  112. };
  113. struct acpi_thermal_state_flags {
  114. u8 valid:1;
  115. u8 enabled:1;
  116. u8 reserved:6;
  117. };
  118. struct acpi_thermal_critical {
  119. struct acpi_thermal_state_flags flags;
  120. unsigned long temperature;
  121. };
  122. struct acpi_thermal_hot {
  123. struct acpi_thermal_state_flags flags;
  124. unsigned long temperature;
  125. };
  126. struct acpi_thermal_passive {
  127. struct acpi_thermal_state_flags flags;
  128. unsigned long temperature;
  129. unsigned long tc1;
  130. unsigned long tc2;
  131. unsigned long tsp;
  132. struct acpi_handle_list devices;
  133. };
  134. struct acpi_thermal_active {
  135. struct acpi_thermal_state_flags flags;
  136. unsigned long temperature;
  137. struct acpi_handle_list devices;
  138. };
  139. struct acpi_thermal_trips {
  140. struct acpi_thermal_critical critical;
  141. struct acpi_thermal_hot hot;
  142. struct acpi_thermal_passive passive;
  143. struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
  144. };
  145. struct acpi_thermal_flags {
  146. u8 cooling_mode:1; /* _SCP */
  147. u8 devices:1; /* _TZD */
  148. u8 reserved:6;
  149. };
  150. struct acpi_thermal {
  151. struct acpi_device * device;
  152. acpi_bus_id name;
  153. unsigned long temperature;
  154. unsigned long last_temperature;
  155. unsigned long polling_frequency;
  156. volatile u8 zombie;
  157. struct acpi_thermal_flags flags;
  158. struct acpi_thermal_state state;
  159. struct acpi_thermal_trips trips;
  160. struct acpi_handle_list devices;
  161. struct thermal_zone_device *thermal_zone;
  162. int tz_enabled;
  163. int kelvin_offset;
  164. };
  165. /* --------------------------------------------------------------------------
  166. Thermal Zone Management
  167. -------------------------------------------------------------------------- */
  168. static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
  169. {
  170. acpi_status status = AE_OK;
  171. unsigned long long tmp;
  172. if (!tz)
  173. return -EINVAL;
  174. tz->last_temperature = tz->temperature;
  175. status = acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tmp);
  176. if (ACPI_FAILURE(status))
  177. return -ENODEV;
  178. tz->temperature = tmp;
  179. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
  180. tz->temperature));
  181. return 0;
  182. }
  183. static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
  184. {
  185. acpi_status status = AE_OK;
  186. unsigned long long tmp;
  187. if (!tz)
  188. return -EINVAL;
  189. status = acpi_evaluate_integer(tz->device->handle, "_TZP", NULL, &tmp);
  190. if (ACPI_FAILURE(status))
  191. return -ENODEV;
  192. tz->polling_frequency = tmp;
  193. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
  194. tz->polling_frequency));
  195. return 0;
  196. }
  197. static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
  198. {
  199. if (!tz)
  200. return -EINVAL;
  201. if (!acpi_has_method(tz->device->handle, "_SCP")) {
  202. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
  203. return -ENODEV;
  204. } else if (ACPI_FAILURE(acpi_execute_simple_method(tz->device->handle,
  205. "_SCP", mode))) {
  206. return -ENODEV;
  207. }
  208. return 0;
  209. }
  210. #define ACPI_TRIPS_CRITICAL 0x01
  211. #define ACPI_TRIPS_HOT 0x02
  212. #define ACPI_TRIPS_PASSIVE 0x04
  213. #define ACPI_TRIPS_ACTIVE 0x08
  214. #define ACPI_TRIPS_DEVICES 0x10
  215. #define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
  216. #define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
  217. #define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
  218. ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
  219. ACPI_TRIPS_DEVICES)
  220. /*
  221. * This exception is thrown out in two cases:
  222. * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
  223. * when re-evaluating the AML code.
  224. * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
  225. * We need to re-bind the cooling devices of a thermal zone when this occurs.
  226. */
  227. #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
  228. do { \
  229. if (flags != ACPI_TRIPS_INIT) \
  230. ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
  231. "ACPI thermal trip point %s changed\n" \
  232. "Please send acpidump to linux-acpi@vger.kernel.org", str)); \
  233. } while (0)
  234. static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
  235. {
  236. acpi_status status = AE_OK;
  237. unsigned long long tmp;
  238. struct acpi_handle_list devices;
  239. int valid = 0;
  240. int i;
  241. /* Critical Shutdown */
  242. if (flag & ACPI_TRIPS_CRITICAL) {
  243. status = acpi_evaluate_integer(tz->device->handle,
  244. "_CRT", NULL, &tmp);
  245. tz->trips.critical.temperature = tmp;
  246. /*
  247. * Treat freezing temperatures as invalid as well; some
  248. * BIOSes return really low values and cause reboots at startup.
  249. * Below zero (Celsius) values clearly aren't right for sure..
  250. * ... so lets discard those as invalid.
  251. */
  252. if (ACPI_FAILURE(status)) {
  253. tz->trips.critical.flags.valid = 0;
  254. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  255. "No critical threshold\n"));
  256. } else if (tmp <= 2732) {
  257. pr_warn(FW_BUG "Invalid critical threshold (%llu)\n",
  258. tmp);
  259. tz->trips.critical.flags.valid = 0;
  260. } else {
  261. tz->trips.critical.flags.valid = 1;
  262. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  263. "Found critical threshold [%lu]\n",
  264. tz->trips.critical.temperature));
  265. }
  266. if (tz->trips.critical.flags.valid == 1) {
  267. if (crt == -1) {
  268. tz->trips.critical.flags.valid = 0;
  269. } else if (crt > 0) {
  270. unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
  271. /*
  272. * Allow override critical threshold
  273. */
  274. if (crt_k > tz->trips.critical.temperature)
  275. pr_warn(PREFIX "Critical threshold %d C\n",
  276. crt);
  277. tz->trips.critical.temperature = crt_k;
  278. }
  279. }
  280. }
  281. /* Critical Sleep (optional) */
  282. if (flag & ACPI_TRIPS_HOT) {
  283. status = acpi_evaluate_integer(tz->device->handle,
  284. "_HOT", NULL, &tmp);
  285. if (ACPI_FAILURE(status)) {
  286. tz->trips.hot.flags.valid = 0;
  287. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  288. "No hot threshold\n"));
  289. } else {
  290. tz->trips.hot.temperature = tmp;
  291. tz->trips.hot.flags.valid = 1;
  292. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  293. "Found hot threshold [%lu]\n",
  294. tz->trips.critical.temperature));
  295. }
  296. }
  297. /* Passive (optional) */
  298. if (((flag & ACPI_TRIPS_PASSIVE) && tz->trips.passive.flags.valid) ||
  299. (flag == ACPI_TRIPS_INIT)) {
  300. valid = tz->trips.passive.flags.valid;
  301. if (psv == -1) {
  302. status = AE_SUPPORT;
  303. } else if (psv > 0) {
  304. tmp = CELSIUS_TO_KELVIN(psv);
  305. status = AE_OK;
  306. } else {
  307. status = acpi_evaluate_integer(tz->device->handle,
  308. "_PSV", NULL, &tmp);
  309. }
  310. if (ACPI_FAILURE(status))
  311. tz->trips.passive.flags.valid = 0;
  312. else {
  313. tz->trips.passive.temperature = tmp;
  314. tz->trips.passive.flags.valid = 1;
  315. if (flag == ACPI_TRIPS_INIT) {
  316. status = acpi_evaluate_integer(
  317. tz->device->handle, "_TC1",
  318. NULL, &tmp);
  319. if (ACPI_FAILURE(status))
  320. tz->trips.passive.flags.valid = 0;
  321. else
  322. tz->trips.passive.tc1 = tmp;
  323. status = acpi_evaluate_integer(
  324. tz->device->handle, "_TC2",
  325. NULL, &tmp);
  326. if (ACPI_FAILURE(status))
  327. tz->trips.passive.flags.valid = 0;
  328. else
  329. tz->trips.passive.tc2 = tmp;
  330. status = acpi_evaluate_integer(
  331. tz->device->handle, "_TSP",
  332. NULL, &tmp);
  333. if (ACPI_FAILURE(status))
  334. tz->trips.passive.flags.valid = 0;
  335. else
  336. tz->trips.passive.tsp = tmp;
  337. }
  338. }
  339. }
  340. if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
  341. memset(&devices, 0, sizeof(struct acpi_handle_list));
  342. status = acpi_evaluate_reference(tz->device->handle, "_PSL",
  343. NULL, &devices);
  344. if (ACPI_FAILURE(status)) {
  345. pr_warn(PREFIX "Invalid passive threshold\n");
  346. tz->trips.passive.flags.valid = 0;
  347. }
  348. else
  349. tz->trips.passive.flags.valid = 1;
  350. if (memcmp(&tz->trips.passive.devices, &devices,
  351. sizeof(struct acpi_handle_list))) {
  352. memcpy(&tz->trips.passive.devices, &devices,
  353. sizeof(struct acpi_handle_list));
  354. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
  355. }
  356. }
  357. if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
  358. if (valid != tz->trips.passive.flags.valid)
  359. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
  360. }
  361. /* Active (optional) */
  362. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  363. char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
  364. valid = tz->trips.active[i].flags.valid;
  365. if (act == -1)
  366. break; /* disable all active trip points */
  367. if ((flag == ACPI_TRIPS_INIT) || ((flag & ACPI_TRIPS_ACTIVE) &&
  368. tz->trips.active[i].flags.valid)) {
  369. status = acpi_evaluate_integer(tz->device->handle,
  370. name, NULL, &tmp);
  371. if (ACPI_FAILURE(status)) {
  372. tz->trips.active[i].flags.valid = 0;
  373. if (i == 0)
  374. break;
  375. if (act <= 0)
  376. break;
  377. if (i == 1)
  378. tz->trips.active[0].temperature =
  379. CELSIUS_TO_KELVIN(act);
  380. else
  381. /*
  382. * Don't allow override higher than
  383. * the next higher trip point
  384. */
  385. tz->trips.active[i - 1].temperature =
  386. (tz->trips.active[i - 2].temperature <
  387. CELSIUS_TO_KELVIN(act) ?
  388. tz->trips.active[i - 2].temperature :
  389. CELSIUS_TO_KELVIN(act));
  390. break;
  391. } else {
  392. tz->trips.active[i].temperature = tmp;
  393. tz->trips.active[i].flags.valid = 1;
  394. }
  395. }
  396. name[2] = 'L';
  397. if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
  398. memset(&devices, 0, sizeof(struct acpi_handle_list));
  399. status = acpi_evaluate_reference(tz->device->handle,
  400. name, NULL, &devices);
  401. if (ACPI_FAILURE(status)) {
  402. pr_warn(PREFIX "Invalid active%d threshold\n",
  403. i);
  404. tz->trips.active[i].flags.valid = 0;
  405. }
  406. else
  407. tz->trips.active[i].flags.valid = 1;
  408. if (memcmp(&tz->trips.active[i].devices, &devices,
  409. sizeof(struct acpi_handle_list))) {
  410. memcpy(&tz->trips.active[i].devices, &devices,
  411. sizeof(struct acpi_handle_list));
  412. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
  413. }
  414. }
  415. if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
  416. if (valid != tz->trips.active[i].flags.valid)
  417. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
  418. if (!tz->trips.active[i].flags.valid)
  419. break;
  420. }
  421. if ((flag & ACPI_TRIPS_DEVICES)
  422. && acpi_has_method(tz->device->handle, "_TZD")) {
  423. memset(&devices, 0, sizeof(devices));
  424. status = acpi_evaluate_reference(tz->device->handle, "_TZD",
  425. NULL, &devices);
  426. if (ACPI_SUCCESS(status)
  427. && memcmp(&tz->devices, &devices, sizeof(devices))) {
  428. tz->devices = devices;
  429. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
  430. }
  431. }
  432. return 0;
  433. }
  434. static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
  435. {
  436. int i, valid, ret = acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
  437. if (ret)
  438. return ret;
  439. valid = tz->trips.critical.flags.valid |
  440. tz->trips.hot.flags.valid |
  441. tz->trips.passive.flags.valid;
  442. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
  443. valid |= tz->trips.active[i].flags.valid;
  444. if (!valid) {
  445. pr_warn(FW_BUG "No valid trip found\n");
  446. return -ENODEV;
  447. }
  448. return 0;
  449. }
  450. static void acpi_thermal_check(void *data)
  451. {
  452. struct acpi_thermal *tz = data;
  453. if (!tz->tz_enabled)
  454. return;
  455. thermal_zone_device_update(tz->thermal_zone);
  456. }
  457. /* sys I/F for generic thermal sysfs support */
  458. #define KELVIN_TO_MILLICELSIUS(t, off) (((t) - (off)) * 100)
  459. static int thermal_get_temp(struct thermal_zone_device *thermal,
  460. unsigned long *temp)
  461. {
  462. struct acpi_thermal *tz = thermal->devdata;
  463. int result;
  464. if (!tz)
  465. return -EINVAL;
  466. result = acpi_thermal_get_temperature(tz);
  467. if (result)
  468. return result;
  469. *temp = KELVIN_TO_MILLICELSIUS(tz->temperature, tz->kelvin_offset);
  470. return 0;
  471. }
  472. static int thermal_get_mode(struct thermal_zone_device *thermal,
  473. enum thermal_device_mode *mode)
  474. {
  475. struct acpi_thermal *tz = thermal->devdata;
  476. if (!tz)
  477. return -EINVAL;
  478. *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
  479. THERMAL_DEVICE_DISABLED;
  480. return 0;
  481. }
  482. static int thermal_set_mode(struct thermal_zone_device *thermal,
  483. enum thermal_device_mode mode)
  484. {
  485. struct acpi_thermal *tz = thermal->devdata;
  486. int enable;
  487. if (!tz)
  488. return -EINVAL;
  489. /*
  490. * enable/disable thermal management from ACPI thermal driver
  491. */
  492. if (mode == THERMAL_DEVICE_ENABLED)
  493. enable = 1;
  494. else if (mode == THERMAL_DEVICE_DISABLED) {
  495. enable = 0;
  496. pr_warn("thermal zone will be disabled\n");
  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 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);