thermal.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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_get_power(tz->trips.active[i].devices.
  904. handles[j], &power_state);
  905. if (result || (power_state != ACPI_STATE_D0)) {
  906. tz->trips.active[i].flags.enabled = 0;
  907. break;
  908. }
  909. }
  910. tz->state.active |= tz->trips.active[i].flags.enabled;
  911. }
  912. acpi_thermal_check(tz);
  913. return AE_OK;
  914. }
  915. static int thermal_act(const struct dmi_system_id *d) {
  916. if (act == 0) {
  917. printk(KERN_NOTICE "ACPI: %s detected: "
  918. "disabling all active thermal trip points\n", d->ident);
  919. act = -1;
  920. }
  921. return 0;
  922. }
  923. static int thermal_nocrt(const struct dmi_system_id *d) {
  924. printk(KERN_NOTICE "ACPI: %s detected: "
  925. "disabling all critical thermal trip point actions.\n", d->ident);
  926. nocrt = 1;
  927. return 0;
  928. }
  929. static int thermal_tzp(const struct dmi_system_id *d) {
  930. if (tzp == 0) {
  931. printk(KERN_NOTICE "ACPI: %s detected: "
  932. "enabling thermal zone polling\n", d->ident);
  933. tzp = 300; /* 300 dS = 30 Seconds */
  934. }
  935. return 0;
  936. }
  937. static int thermal_psv(const struct dmi_system_id *d) {
  938. if (psv == 0) {
  939. printk(KERN_NOTICE "ACPI: %s detected: "
  940. "disabling all passive thermal trip points\n", d->ident);
  941. psv = -1;
  942. }
  943. return 0;
  944. }
  945. static struct dmi_system_id thermal_dmi_table[] __initdata = {
  946. /*
  947. * Award BIOS on this AOpen makes thermal control almost worthless.
  948. * http://bugzilla.kernel.org/show_bug.cgi?id=8842
  949. */
  950. {
  951. .callback = thermal_act,
  952. .ident = "AOpen i915GMm-HFS",
  953. .matches = {
  954. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  955. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  956. },
  957. },
  958. {
  959. .callback = thermal_psv,
  960. .ident = "AOpen i915GMm-HFS",
  961. .matches = {
  962. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  963. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  964. },
  965. },
  966. {
  967. .callback = thermal_tzp,
  968. .ident = "AOpen i915GMm-HFS",
  969. .matches = {
  970. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  971. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  972. },
  973. },
  974. {
  975. .callback = thermal_nocrt,
  976. .ident = "Gigabyte GA-7ZX",
  977. .matches = {
  978. DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
  979. DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
  980. },
  981. },
  982. {}
  983. };
  984. static int __init acpi_thermal_init(void)
  985. {
  986. int result = 0;
  987. dmi_check_system(thermal_dmi_table);
  988. if (off) {
  989. printk(KERN_NOTICE "ACPI: thermal control disabled\n");
  990. return -ENODEV;
  991. }
  992. result = acpi_bus_register_driver(&acpi_thermal_driver);
  993. if (result < 0)
  994. return -ENODEV;
  995. return 0;
  996. }
  997. static void __exit acpi_thermal_exit(void)
  998. {
  999. acpi_bus_unregister_driver(&acpi_thermal_driver);
  1000. return;
  1001. }
  1002. module_init(acpi_thermal_init);
  1003. module_exit(acpi_thermal_exit);