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 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 const char enabled[] = "kernel";
  480. static const char disabled[] = "user";
  481. static int thermal_get_mode(struct thermal_zone_device *thermal,
  482. enum thermal_device_mode *mode)
  483. {
  484. struct acpi_thermal *tz = thermal->devdata;
  485. if (!tz)
  486. return -EINVAL;
  487. *mode = tz->tz_enabled ? THERMAL_DEVICE_ENABLED :
  488. THERMAL_DEVICE_DISABLED;
  489. return 0;
  490. }
  491. static int thermal_set_mode(struct thermal_zone_device *thermal,
  492. enum thermal_device_mode mode)
  493. {
  494. struct acpi_thermal *tz = thermal->devdata;
  495. int enable;
  496. if (!tz)
  497. return -EINVAL;
  498. /*
  499. * enable/disable thermal management from ACPI thermal driver
  500. */
  501. if (mode == THERMAL_DEVICE_ENABLED)
  502. enable = 1;
  503. else if (mode == THERMAL_DEVICE_DISABLED)
  504. enable = 0;
  505. else
  506. return -EINVAL;
  507. if (enable != tz->tz_enabled) {
  508. tz->tz_enabled = enable;
  509. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  510. "%s ACPI thermal control\n",
  511. tz->tz_enabled ? enabled : disabled));
  512. acpi_thermal_check(tz);
  513. }
  514. return 0;
  515. }
  516. static int thermal_get_trip_type(struct thermal_zone_device *thermal,
  517. int trip, enum thermal_trip_type *type)
  518. {
  519. struct acpi_thermal *tz = thermal->devdata;
  520. int i;
  521. if (!tz || trip < 0)
  522. return -EINVAL;
  523. if (tz->trips.critical.flags.valid) {
  524. if (!trip) {
  525. *type = THERMAL_TRIP_CRITICAL;
  526. return 0;
  527. }
  528. trip--;
  529. }
  530. if (tz->trips.hot.flags.valid) {
  531. if (!trip) {
  532. *type = THERMAL_TRIP_HOT;
  533. return 0;
  534. }
  535. trip--;
  536. }
  537. if (tz->trips.passive.flags.valid) {
  538. if (!trip) {
  539. *type = THERMAL_TRIP_PASSIVE;
  540. return 0;
  541. }
  542. trip--;
  543. }
  544. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  545. tz->trips.active[i].flags.valid; i++) {
  546. if (!trip) {
  547. *type = THERMAL_TRIP_ACTIVE;
  548. return 0;
  549. }
  550. trip--;
  551. }
  552. return -EINVAL;
  553. }
  554. static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
  555. int trip, unsigned long *temp)
  556. {
  557. struct acpi_thermal *tz = thermal->devdata;
  558. int i;
  559. if (!tz || trip < 0)
  560. return -EINVAL;
  561. if (tz->trips.critical.flags.valid) {
  562. if (!trip) {
  563. *temp = KELVIN_TO_MILLICELSIUS(
  564. tz->trips.critical.temperature,
  565. tz->kelvin_offset);
  566. return 0;
  567. }
  568. trip--;
  569. }
  570. if (tz->trips.hot.flags.valid) {
  571. if (!trip) {
  572. *temp = KELVIN_TO_MILLICELSIUS(
  573. tz->trips.hot.temperature,
  574. tz->kelvin_offset);
  575. return 0;
  576. }
  577. trip--;
  578. }
  579. if (tz->trips.passive.flags.valid) {
  580. if (!trip) {
  581. *temp = KELVIN_TO_MILLICELSIUS(
  582. tz->trips.passive.temperature,
  583. tz->kelvin_offset);
  584. return 0;
  585. }
  586. trip--;
  587. }
  588. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  589. tz->trips.active[i].flags.valid; i++) {
  590. if (!trip) {
  591. *temp = KELVIN_TO_MILLICELSIUS(
  592. tz->trips.active[i].temperature,
  593. tz->kelvin_offset);
  594. return 0;
  595. }
  596. trip--;
  597. }
  598. return -EINVAL;
  599. }
  600. static int thermal_get_crit_temp(struct thermal_zone_device *thermal,
  601. unsigned long *temperature) {
  602. struct acpi_thermal *tz = thermal->devdata;
  603. if (tz->trips.critical.flags.valid) {
  604. *temperature = KELVIN_TO_MILLICELSIUS(
  605. tz->trips.critical.temperature,
  606. tz->kelvin_offset);
  607. return 0;
  608. } else
  609. return -EINVAL;
  610. }
  611. static int thermal_notify(struct thermal_zone_device *thermal, int trip,
  612. enum thermal_trip_type trip_type)
  613. {
  614. u8 type = 0;
  615. struct acpi_thermal *tz = thermal->devdata;
  616. if (trip_type == THERMAL_TRIP_CRITICAL)
  617. type = ACPI_THERMAL_NOTIFY_CRITICAL;
  618. else if (trip_type == THERMAL_TRIP_HOT)
  619. type = ACPI_THERMAL_NOTIFY_HOT;
  620. else
  621. return 0;
  622. acpi_bus_generate_proc_event(tz->device, type, 1);
  623. acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
  624. dev_name(&tz->device->dev), type, 1);
  625. if (trip_type == THERMAL_TRIP_CRITICAL && nocrt)
  626. return 1;
  627. return 0;
  628. }
  629. typedef int (*cb)(struct thermal_zone_device *, int,
  630. struct thermal_cooling_device *);
  631. static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
  632. struct thermal_cooling_device *cdev,
  633. cb action)
  634. {
  635. struct acpi_device *device = cdev->devdata;
  636. struct acpi_thermal *tz = thermal->devdata;
  637. struct acpi_device *dev;
  638. acpi_status status;
  639. acpi_handle handle;
  640. int i;
  641. int j;
  642. int trip = -1;
  643. int result = 0;
  644. if (tz->trips.critical.flags.valid)
  645. trip++;
  646. if (tz->trips.hot.flags.valid)
  647. trip++;
  648. if (tz->trips.passive.flags.valid) {
  649. trip++;
  650. for (i = 0; i < tz->trips.passive.devices.count;
  651. i++) {
  652. handle = tz->trips.passive.devices.handles[i];
  653. status = acpi_bus_get_device(handle, &dev);
  654. if (ACPI_SUCCESS(status) && (dev == device)) {
  655. result = action(thermal, trip, cdev);
  656. if (result)
  657. goto failed;
  658. }
  659. }
  660. }
  661. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  662. if (!tz->trips.active[i].flags.valid)
  663. break;
  664. trip++;
  665. for (j = 0;
  666. j < tz->trips.active[i].devices.count;
  667. j++) {
  668. handle = tz->trips.active[i].devices.handles[j];
  669. status = acpi_bus_get_device(handle, &dev);
  670. if (ACPI_SUCCESS(status) && (dev == device)) {
  671. result = action(thermal, trip, cdev);
  672. if (result)
  673. goto failed;
  674. }
  675. }
  676. }
  677. for (i = 0; i < tz->devices.count; i++) {
  678. handle = tz->devices.handles[i];
  679. status = acpi_bus_get_device(handle, &dev);
  680. if (ACPI_SUCCESS(status) && (dev == device)) {
  681. result = action(thermal, -1, cdev);
  682. if (result)
  683. goto failed;
  684. }
  685. }
  686. failed:
  687. return result;
  688. }
  689. static int
  690. acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
  691. struct thermal_cooling_device *cdev)
  692. {
  693. return acpi_thermal_cooling_device_cb(thermal, cdev,
  694. thermal_zone_bind_cooling_device);
  695. }
  696. static int
  697. acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
  698. struct thermal_cooling_device *cdev)
  699. {
  700. return acpi_thermal_cooling_device_cb(thermal, cdev,
  701. thermal_zone_unbind_cooling_device);
  702. }
  703. static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
  704. .bind = acpi_thermal_bind_cooling_device,
  705. .unbind = acpi_thermal_unbind_cooling_device,
  706. .get_temp = thermal_get_temp,
  707. .get_mode = thermal_get_mode,
  708. .set_mode = thermal_set_mode,
  709. .get_trip_type = thermal_get_trip_type,
  710. .get_trip_temp = thermal_get_trip_temp,
  711. .get_crit_temp = thermal_get_crit_temp,
  712. .notify = thermal_notify,
  713. };
  714. static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
  715. {
  716. int trips = 0;
  717. int result;
  718. acpi_status status;
  719. int i;
  720. if (tz->trips.critical.flags.valid)
  721. trips++;
  722. if (tz->trips.hot.flags.valid)
  723. trips++;
  724. if (tz->trips.passive.flags.valid)
  725. trips++;
  726. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  727. tz->trips.active[i].flags.valid; i++, trips++);
  728. if (tz->trips.passive.flags.valid)
  729. tz->thermal_zone =
  730. thermal_zone_device_register("acpitz", trips, tz,
  731. &acpi_thermal_zone_ops,
  732. tz->trips.passive.tc1,
  733. tz->trips.passive.tc2,
  734. tz->trips.passive.tsp*100,
  735. tz->polling_frequency*100);
  736. else
  737. tz->thermal_zone =
  738. thermal_zone_device_register("acpitz", trips, tz,
  739. &acpi_thermal_zone_ops,
  740. 0, 0, 0,
  741. tz->polling_frequency*100);
  742. if (IS_ERR(tz->thermal_zone))
  743. return -ENODEV;
  744. result = sysfs_create_link(&tz->device->dev.kobj,
  745. &tz->thermal_zone->device.kobj, "thermal_zone");
  746. if (result)
  747. return result;
  748. result = sysfs_create_link(&tz->thermal_zone->device.kobj,
  749. &tz->device->dev.kobj, "device");
  750. if (result)
  751. return result;
  752. status = acpi_attach_data(tz->device->handle,
  753. acpi_bus_private_data_handler,
  754. tz->thermal_zone);
  755. if (ACPI_FAILURE(status)) {
  756. printk(KERN_ERR PREFIX
  757. "Error attaching device data\n");
  758. return -ENODEV;
  759. }
  760. tz->tz_enabled = 1;
  761. dev_info(&tz->device->dev, "registered as thermal_zone%d\n",
  762. tz->thermal_zone->id);
  763. return 0;
  764. }
  765. static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
  766. {
  767. sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
  768. sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
  769. thermal_zone_device_unregister(tz->thermal_zone);
  770. tz->thermal_zone = NULL;
  771. acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
  772. }
  773. /* --------------------------------------------------------------------------
  774. Driver Interface
  775. -------------------------------------------------------------------------- */
  776. static void acpi_thermal_notify(struct acpi_device *device, u32 event)
  777. {
  778. struct acpi_thermal *tz = acpi_driver_data(device);
  779. if (!tz)
  780. return;
  781. switch (event) {
  782. case ACPI_THERMAL_NOTIFY_TEMPERATURE:
  783. acpi_thermal_check(tz);
  784. break;
  785. case ACPI_THERMAL_NOTIFY_THRESHOLDS:
  786. acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
  787. acpi_thermal_check(tz);
  788. acpi_bus_generate_proc_event(device, event, 0);
  789. acpi_bus_generate_netlink_event(device->pnp.device_class,
  790. dev_name(&device->dev), event, 0);
  791. break;
  792. case ACPI_THERMAL_NOTIFY_DEVICES:
  793. acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
  794. acpi_thermal_check(tz);
  795. acpi_bus_generate_proc_event(device, event, 0);
  796. acpi_bus_generate_netlink_event(device->pnp.device_class,
  797. dev_name(&device->dev), event, 0);
  798. break;
  799. default:
  800. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  801. "Unsupported event [0x%x]\n", event));
  802. break;
  803. }
  804. }
  805. static int acpi_thermal_get_info(struct acpi_thermal *tz)
  806. {
  807. int result = 0;
  808. if (!tz)
  809. return -EINVAL;
  810. /* Get temperature [_TMP] (required) */
  811. result = acpi_thermal_get_temperature(tz);
  812. if (result)
  813. return result;
  814. /* Get trip points [_CRT, _PSV, etc.] (required) */
  815. result = acpi_thermal_get_trip_points(tz);
  816. if (result)
  817. return result;
  818. /* Set the cooling mode [_SCP] to active cooling (default) */
  819. result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
  820. if (!result)
  821. tz->flags.cooling_mode = 1;
  822. /* Get default polling frequency [_TZP] (optional) */
  823. if (tzp)
  824. tz->polling_frequency = tzp;
  825. else
  826. acpi_thermal_get_polling_frequency(tz);
  827. return 0;
  828. }
  829. /*
  830. * The exact offset between Kelvin and degree Celsius is 273.15. However ACPI
  831. * handles temperature values with a single decimal place. As a consequence,
  832. * some implementations use an offset of 273.1 and others use an offset of
  833. * 273.2. Try to find out which one is being used, to present the most
  834. * accurate and visually appealing number.
  835. *
  836. * The heuristic below should work for all ACPI thermal zones which have a
  837. * critical trip point with a value being a multiple of 0.5 degree Celsius.
  838. */
  839. static void acpi_thermal_guess_offset(struct acpi_thermal *tz)
  840. {
  841. if (tz->trips.critical.flags.valid &&
  842. (tz->trips.critical.temperature % 5) == 1)
  843. tz->kelvin_offset = 2731;
  844. else
  845. tz->kelvin_offset = 2732;
  846. }
  847. static int acpi_thermal_add(struct acpi_device *device)
  848. {
  849. int result = 0;
  850. struct acpi_thermal *tz = NULL;
  851. if (!device)
  852. return -EINVAL;
  853. tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
  854. if (!tz)
  855. return -ENOMEM;
  856. tz->device = device;
  857. strcpy(tz->name, device->pnp.bus_id);
  858. strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
  859. strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
  860. device->driver_data = tz;
  861. mutex_init(&tz->lock);
  862. result = acpi_thermal_get_info(tz);
  863. if (result)
  864. goto free_memory;
  865. acpi_thermal_guess_offset(tz);
  866. result = acpi_thermal_register_thermal_zone(tz);
  867. if (result)
  868. goto free_memory;
  869. printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
  870. acpi_device_name(device), acpi_device_bid(device),
  871. KELVIN_TO_CELSIUS(tz->temperature));
  872. goto end;
  873. free_memory:
  874. kfree(tz);
  875. end:
  876. return result;
  877. }
  878. static int acpi_thermal_remove(struct acpi_device *device, int type)
  879. {
  880. struct acpi_thermal *tz = NULL;
  881. if (!device || !acpi_driver_data(device))
  882. return -EINVAL;
  883. tz = acpi_driver_data(device);
  884. acpi_thermal_unregister_thermal_zone(tz);
  885. mutex_destroy(&tz->lock);
  886. kfree(tz);
  887. return 0;
  888. }
  889. static int acpi_thermal_resume(struct acpi_device *device)
  890. {
  891. struct acpi_thermal *tz = NULL;
  892. int i, j, power_state, result;
  893. if (!device || !acpi_driver_data(device))
  894. return -EINVAL;
  895. tz = acpi_driver_data(device);
  896. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  897. if (!(&tz->trips.active[i]))
  898. break;
  899. if (!tz->trips.active[i].flags.valid)
  900. break;
  901. tz->trips.active[i].flags.enabled = 1;
  902. for (j = 0; j < tz->trips.active[i].devices.count; j++) {
  903. result = acpi_bus_update_power(
  904. tz->trips.active[i].devices.handles[j],
  905. &power_state);
  906. if (result || (power_state != ACPI_STATE_D0)) {
  907. tz->trips.active[i].flags.enabled = 0;
  908. break;
  909. }
  910. }
  911. tz->state.active |= tz->trips.active[i].flags.enabled;
  912. }
  913. acpi_thermal_check(tz);
  914. return AE_OK;
  915. }
  916. static int thermal_act(const struct dmi_system_id *d) {
  917. if (act == 0) {
  918. printk(KERN_NOTICE "ACPI: %s detected: "
  919. "disabling all active thermal trip points\n", d->ident);
  920. act = -1;
  921. }
  922. return 0;
  923. }
  924. static int thermal_nocrt(const struct dmi_system_id *d) {
  925. printk(KERN_NOTICE "ACPI: %s detected: "
  926. "disabling all critical thermal trip point actions.\n", d->ident);
  927. nocrt = 1;
  928. return 0;
  929. }
  930. static int thermal_tzp(const struct dmi_system_id *d) {
  931. if (tzp == 0) {
  932. printk(KERN_NOTICE "ACPI: %s detected: "
  933. "enabling thermal zone polling\n", d->ident);
  934. tzp = 300; /* 300 dS = 30 Seconds */
  935. }
  936. return 0;
  937. }
  938. static int thermal_psv(const struct dmi_system_id *d) {
  939. if (psv == 0) {
  940. printk(KERN_NOTICE "ACPI: %s detected: "
  941. "disabling all passive thermal trip points\n", d->ident);
  942. psv = -1;
  943. }
  944. return 0;
  945. }
  946. static struct dmi_system_id thermal_dmi_table[] __initdata = {
  947. /*
  948. * Award BIOS on this AOpen makes thermal control almost worthless.
  949. * http://bugzilla.kernel.org/show_bug.cgi?id=8842
  950. */
  951. {
  952. .callback = thermal_act,
  953. .ident = "AOpen i915GMm-HFS",
  954. .matches = {
  955. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  956. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  957. },
  958. },
  959. {
  960. .callback = thermal_psv,
  961. .ident = "AOpen i915GMm-HFS",
  962. .matches = {
  963. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  964. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  965. },
  966. },
  967. {
  968. .callback = thermal_tzp,
  969. .ident = "AOpen i915GMm-HFS",
  970. .matches = {
  971. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  972. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  973. },
  974. },
  975. {
  976. .callback = thermal_nocrt,
  977. .ident = "Gigabyte GA-7ZX",
  978. .matches = {
  979. DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
  980. DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
  981. },
  982. },
  983. {}
  984. };
  985. static int __init acpi_thermal_init(void)
  986. {
  987. int result = 0;
  988. dmi_check_system(thermal_dmi_table);
  989. if (off) {
  990. printk(KERN_NOTICE "ACPI: thermal control disabled\n");
  991. return -ENODEV;
  992. }
  993. result = acpi_bus_register_driver(&acpi_thermal_driver);
  994. if (result < 0)
  995. return -ENODEV;
  996. return 0;
  997. }
  998. static void __exit acpi_thermal_exit(void)
  999. {
  1000. acpi_bus_unregister_driver(&acpi_thermal_driver);
  1001. return;
  1002. }
  1003. module_init(acpi_thermal_init);
  1004. module_exit(acpi_thermal_exit);