thermal.c 29 KB

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