thermal.c 29 KB

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