thermal.c 29 KB

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