thermal.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  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/types.h>
  38. #include <linux/proc_fs.h>
  39. #include <linux/timer.h>
  40. #include <linux/jiffies.h>
  41. #include <linux/kmod.h>
  42. #include <linux/seq_file.h>
  43. #include <linux/reboot.h>
  44. #include <asm/uaccess.h>
  45. #include <linux/thermal.h>
  46. #include <acpi/acpi_bus.h>
  47. #include <acpi/acpi_drivers.h>
  48. #define ACPI_THERMAL_COMPONENT 0x04000000
  49. #define ACPI_THERMAL_CLASS "thermal_zone"
  50. #define ACPI_THERMAL_DEVICE_NAME "Thermal Zone"
  51. #define ACPI_THERMAL_FILE_STATE "state"
  52. #define ACPI_THERMAL_FILE_TEMPERATURE "temperature"
  53. #define ACPI_THERMAL_FILE_TRIP_POINTS "trip_points"
  54. #define ACPI_THERMAL_FILE_COOLING_MODE "cooling_mode"
  55. #define ACPI_THERMAL_FILE_POLLING_FREQ "polling_frequency"
  56. #define ACPI_THERMAL_NOTIFY_TEMPERATURE 0x80
  57. #define ACPI_THERMAL_NOTIFY_THRESHOLDS 0x81
  58. #define ACPI_THERMAL_NOTIFY_DEVICES 0x82
  59. #define ACPI_THERMAL_NOTIFY_CRITICAL 0xF0
  60. #define ACPI_THERMAL_NOTIFY_HOT 0xF1
  61. #define ACPI_THERMAL_MODE_ACTIVE 0x00
  62. #define ACPI_THERMAL_MAX_ACTIVE 10
  63. #define ACPI_THERMAL_MAX_LIMIT_STR_LEN 65
  64. #define _COMPONENT ACPI_THERMAL_COMPONENT
  65. ACPI_MODULE_NAME("thermal");
  66. MODULE_AUTHOR("Paul Diefenbaugh");
  67. MODULE_DESCRIPTION("ACPI Thermal Zone Driver");
  68. MODULE_LICENSE("GPL");
  69. static int act;
  70. module_param(act, int, 0644);
  71. MODULE_PARM_DESC(act, "Disable or override all lowest active trip points.");
  72. static int crt;
  73. module_param(crt, int, 0644);
  74. MODULE_PARM_DESC(crt, "Disable or lower all critical trip points.");
  75. static int tzp;
  76. module_param(tzp, int, 0444);
  77. MODULE_PARM_DESC(tzp, "Thermal zone polling frequency, in 1/10 seconds.");
  78. static int nocrt;
  79. module_param(nocrt, int, 0);
  80. MODULE_PARM_DESC(nocrt, "Set to take no action upon ACPI thermal zone critical trips points.");
  81. static int off;
  82. module_param(off, int, 0);
  83. MODULE_PARM_DESC(off, "Set to disable ACPI thermal support.");
  84. static int psv;
  85. module_param(psv, int, 0644);
  86. MODULE_PARM_DESC(psv, "Disable or override all passive trip points.");
  87. static int acpi_thermal_add(struct acpi_device *device);
  88. static int acpi_thermal_remove(struct acpi_device *device, int type);
  89. static int acpi_thermal_resume(struct acpi_device *device);
  90. static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file);
  91. static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file);
  92. static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file);
  93. static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file);
  94. static ssize_t acpi_thermal_write_cooling_mode(struct file *,
  95. const char __user *, size_t,
  96. loff_t *);
  97. static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file);
  98. static ssize_t acpi_thermal_write_polling(struct file *, const char __user *,
  99. size_t, loff_t *);
  100. static const struct acpi_device_id thermal_device_ids[] = {
  101. {ACPI_THERMAL_HID, 0},
  102. {"", 0},
  103. };
  104. MODULE_DEVICE_TABLE(acpi, thermal_device_ids);
  105. static struct acpi_driver acpi_thermal_driver = {
  106. .name = "thermal",
  107. .class = ACPI_THERMAL_CLASS,
  108. .ids = thermal_device_ids,
  109. .ops = {
  110. .add = acpi_thermal_add,
  111. .remove = acpi_thermal_remove,
  112. .resume = acpi_thermal_resume,
  113. },
  114. };
  115. struct acpi_thermal_state {
  116. u8 critical:1;
  117. u8 hot:1;
  118. u8 passive:1;
  119. u8 active:1;
  120. u8 reserved:4;
  121. int active_index;
  122. };
  123. struct acpi_thermal_state_flags {
  124. u8 valid:1;
  125. u8 enabled:1;
  126. u8 reserved:6;
  127. };
  128. struct acpi_thermal_critical {
  129. struct acpi_thermal_state_flags flags;
  130. unsigned long temperature;
  131. };
  132. struct acpi_thermal_hot {
  133. struct acpi_thermal_state_flags flags;
  134. unsigned long temperature;
  135. };
  136. struct acpi_thermal_passive {
  137. struct acpi_thermal_state_flags flags;
  138. unsigned long temperature;
  139. unsigned long tc1;
  140. unsigned long tc2;
  141. unsigned long tsp;
  142. struct acpi_handle_list devices;
  143. };
  144. struct acpi_thermal_active {
  145. struct acpi_thermal_state_flags flags;
  146. unsigned long temperature;
  147. struct acpi_handle_list devices;
  148. };
  149. struct acpi_thermal_trips {
  150. struct acpi_thermal_critical critical;
  151. struct acpi_thermal_hot hot;
  152. struct acpi_thermal_passive passive;
  153. struct acpi_thermal_active active[ACPI_THERMAL_MAX_ACTIVE];
  154. };
  155. struct acpi_thermal_flags {
  156. u8 cooling_mode:1; /* _SCP */
  157. u8 devices:1; /* _TZD */
  158. u8 reserved:6;
  159. };
  160. struct acpi_thermal {
  161. struct acpi_device * device;
  162. acpi_bus_id name;
  163. unsigned long temperature;
  164. unsigned long last_temperature;
  165. unsigned long polling_frequency;
  166. volatile u8 zombie;
  167. struct acpi_thermal_flags flags;
  168. struct acpi_thermal_state state;
  169. struct acpi_thermal_trips trips;
  170. struct acpi_handle_list devices;
  171. struct timer_list timer;
  172. struct thermal_zone_device *thermal_zone;
  173. int tz_enabled;
  174. struct mutex lock;
  175. };
  176. static const struct file_operations acpi_thermal_state_fops = {
  177. .open = acpi_thermal_state_open_fs,
  178. .read = seq_read,
  179. .llseek = seq_lseek,
  180. .release = single_release,
  181. };
  182. static const struct file_operations acpi_thermal_temp_fops = {
  183. .open = acpi_thermal_temp_open_fs,
  184. .read = seq_read,
  185. .llseek = seq_lseek,
  186. .release = single_release,
  187. };
  188. static const struct file_operations acpi_thermal_trip_fops = {
  189. .open = acpi_thermal_trip_open_fs,
  190. .read = seq_read,
  191. .llseek = seq_lseek,
  192. .release = single_release,
  193. };
  194. static const struct file_operations acpi_thermal_cooling_fops = {
  195. .open = acpi_thermal_cooling_open_fs,
  196. .read = seq_read,
  197. .write = acpi_thermal_write_cooling_mode,
  198. .llseek = seq_lseek,
  199. .release = single_release,
  200. };
  201. static const struct file_operations acpi_thermal_polling_fops = {
  202. .open = acpi_thermal_polling_open_fs,
  203. .read = seq_read,
  204. .write = acpi_thermal_write_polling,
  205. .llseek = seq_lseek,
  206. .release = single_release,
  207. };
  208. /* --------------------------------------------------------------------------
  209. Thermal Zone Management
  210. -------------------------------------------------------------------------- */
  211. static int acpi_thermal_get_temperature(struct acpi_thermal *tz)
  212. {
  213. acpi_status status = AE_OK;
  214. if (!tz)
  215. return -EINVAL;
  216. tz->last_temperature = tz->temperature;
  217. status =
  218. acpi_evaluate_integer(tz->device->handle, "_TMP", NULL, &tz->temperature);
  219. if (ACPI_FAILURE(status))
  220. return -ENODEV;
  221. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Temperature is %lu dK\n",
  222. tz->temperature));
  223. return 0;
  224. }
  225. static int acpi_thermal_get_polling_frequency(struct acpi_thermal *tz)
  226. {
  227. acpi_status status = AE_OK;
  228. if (!tz)
  229. return -EINVAL;
  230. status =
  231. acpi_evaluate_integer(tz->device->handle, "_TZP", NULL,
  232. &tz->polling_frequency);
  233. if (ACPI_FAILURE(status))
  234. return -ENODEV;
  235. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Polling frequency is %lu dS\n",
  236. tz->polling_frequency));
  237. return 0;
  238. }
  239. static int acpi_thermal_set_polling(struct acpi_thermal *tz, int seconds)
  240. {
  241. if (!tz)
  242. return -EINVAL;
  243. tz->polling_frequency = seconds * 10; /* Convert value to deci-seconds */
  244. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  245. "Polling frequency set to %lu seconds\n",
  246. tz->polling_frequency/10));
  247. return 0;
  248. }
  249. static int acpi_thermal_set_cooling_mode(struct acpi_thermal *tz, int mode)
  250. {
  251. acpi_status status = AE_OK;
  252. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  253. struct acpi_object_list arg_list = { 1, &arg0 };
  254. acpi_handle handle = NULL;
  255. if (!tz)
  256. return -EINVAL;
  257. status = acpi_get_handle(tz->device->handle, "_SCP", &handle);
  258. if (ACPI_FAILURE(status)) {
  259. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_SCP not present\n"));
  260. return -ENODEV;
  261. }
  262. arg0.integer.value = mode;
  263. status = acpi_evaluate_object(handle, NULL, &arg_list, NULL);
  264. if (ACPI_FAILURE(status))
  265. return -ENODEV;
  266. return 0;
  267. }
  268. #define ACPI_TRIPS_CRITICAL 0x01
  269. #define ACPI_TRIPS_HOT 0x02
  270. #define ACPI_TRIPS_PASSIVE 0x04
  271. #define ACPI_TRIPS_ACTIVE 0x08
  272. #define ACPI_TRIPS_DEVICES 0x10
  273. #define ACPI_TRIPS_REFRESH_THRESHOLDS (ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE)
  274. #define ACPI_TRIPS_REFRESH_DEVICES ACPI_TRIPS_DEVICES
  275. #define ACPI_TRIPS_INIT (ACPI_TRIPS_CRITICAL | ACPI_TRIPS_HOT | \
  276. ACPI_TRIPS_PASSIVE | ACPI_TRIPS_ACTIVE | \
  277. ACPI_TRIPS_DEVICES)
  278. /*
  279. * This exception is thrown out in two cases:
  280. * 1.An invalid trip point becomes invalid or a valid trip point becomes invalid
  281. * when re-evaluating the AML code.
  282. * 2.TODO: Devices listed in _PSL, _ALx, _TZD may change.
  283. * We need to re-bind the cooling devices of a thermal zone when this occurs.
  284. */
  285. #define ACPI_THERMAL_TRIPS_EXCEPTION(flags, str) \
  286. do { \
  287. if (flags != ACPI_TRIPS_INIT) \
  288. ACPI_EXCEPTION((AE_INFO, AE_ERROR, \
  289. "ACPI thermal trip point %s changed\n" \
  290. "Please send acpidump to linux-acpi@vger.kernel.org\n", str)); \
  291. } while (0)
  292. static int acpi_thermal_trips_update(struct acpi_thermal *tz, int flag)
  293. {
  294. acpi_status status = AE_OK;
  295. struct acpi_handle_list devices;
  296. int valid = 0;
  297. int i;
  298. /* Critical Shutdown (required) */
  299. if (flag & ACPI_TRIPS_CRITICAL) {
  300. status = acpi_evaluate_integer(tz->device->handle,
  301. "_CRT", NULL, &tz->trips.critical.temperature);
  302. if (ACPI_FAILURE(status)) {
  303. tz->trips.critical.flags.valid = 0;
  304. ACPI_EXCEPTION((AE_INFO, status,
  305. "No critical threshold"));
  306. return -ENODEV;
  307. } else {
  308. tz->trips.critical.flags.valid = 1;
  309. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  310. "Found critical threshold [%lu]\n",
  311. tz->trips.critical.temperature));
  312. }
  313. if (tz->trips.critical.flags.valid == 1) {
  314. if (crt == -1) {
  315. tz->trips.critical.flags.valid = 0;
  316. } else if (crt > 0) {
  317. unsigned long crt_k = CELSIUS_TO_KELVIN(crt);
  318. /*
  319. * Allow override to lower critical threshold
  320. */
  321. if (crt_k < tz->trips.critical.temperature)
  322. tz->trips.critical.temperature = crt_k;
  323. }
  324. }
  325. }
  326. /* Critical Sleep (optional) */
  327. if (flag & ACPI_TRIPS_HOT) {
  328. status = acpi_evaluate_integer(tz->device->handle,
  329. "_HOT", NULL, &tz->trips.hot.temperature);
  330. if (ACPI_FAILURE(status)) {
  331. tz->trips.hot.flags.valid = 0;
  332. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  333. "No hot threshold\n"));
  334. } else {
  335. tz->trips.hot.flags.valid = 1;
  336. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  337. "Found hot threshold [%lu]\n",
  338. tz->trips.critical.temperature));
  339. }
  340. }
  341. /* Passive (optional) */
  342. if (flag & ACPI_TRIPS_PASSIVE) {
  343. valid = tz->trips.passive.flags.valid;
  344. if (psv == -1) {
  345. status = AE_SUPPORT;
  346. } else if (psv > 0) {
  347. tz->trips.passive.temperature = CELSIUS_TO_KELVIN(psv);
  348. status = AE_OK;
  349. } else {
  350. status = acpi_evaluate_integer(tz->device->handle,
  351. "_PSV", NULL, &tz->trips.passive.temperature);
  352. }
  353. if (ACPI_FAILURE(status))
  354. tz->trips.passive.flags.valid = 0;
  355. else {
  356. tz->trips.passive.flags.valid = 1;
  357. if (flag == ACPI_TRIPS_INIT) {
  358. status = acpi_evaluate_integer(
  359. tz->device->handle, "_TC1",
  360. NULL, &tz->trips.passive.tc1);
  361. if (ACPI_FAILURE(status))
  362. tz->trips.passive.flags.valid = 0;
  363. status = acpi_evaluate_integer(
  364. tz->device->handle, "_TC2",
  365. NULL, &tz->trips.passive.tc2);
  366. if (ACPI_FAILURE(status))
  367. tz->trips.passive.flags.valid = 0;
  368. status = acpi_evaluate_integer(
  369. tz->device->handle, "_TSP",
  370. NULL, &tz->trips.passive.tsp);
  371. if (ACPI_FAILURE(status))
  372. tz->trips.passive.flags.valid = 0;
  373. }
  374. }
  375. }
  376. if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.passive.flags.valid) {
  377. memset(&devices, 0, sizeof(struct acpi_handle_list));
  378. status = acpi_evaluate_reference(tz->device->handle, "_PSL",
  379. NULL, &devices);
  380. if (ACPI_FAILURE(status))
  381. tz->trips.passive.flags.valid = 0;
  382. else
  383. tz->trips.passive.flags.valid = 1;
  384. if (memcmp(&tz->trips.passive.devices, &devices,
  385. sizeof(struct acpi_handle_list))) {
  386. memcpy(&tz->trips.passive.devices, &devices,
  387. sizeof(struct acpi_handle_list));
  388. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
  389. }
  390. }
  391. if ((flag & ACPI_TRIPS_PASSIVE) || (flag & ACPI_TRIPS_DEVICES)) {
  392. if (valid != tz->trips.passive.flags.valid)
  393. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
  394. }
  395. /* Active (optional) */
  396. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  397. char name[5] = { '_', 'A', 'C', ('0' + i), '\0' };
  398. valid = tz->trips.active[i].flags.valid;
  399. if (act == -1)
  400. break; /* disable all active trip points */
  401. if (flag & ACPI_TRIPS_ACTIVE) {
  402. status = acpi_evaluate_integer(tz->device->handle,
  403. name, NULL, &tz->trips.active[i].temperature);
  404. if (ACPI_FAILURE(status)) {
  405. tz->trips.active[i].flags.valid = 0;
  406. if (i == 0)
  407. break;
  408. if (act <= 0)
  409. break;
  410. if (i == 1)
  411. tz->trips.active[0].temperature =
  412. CELSIUS_TO_KELVIN(act);
  413. else
  414. /*
  415. * Don't allow override higher than
  416. * the next higher trip point
  417. */
  418. tz->trips.active[i - 1].temperature =
  419. (tz->trips.active[i - 2].temperature <
  420. CELSIUS_TO_KELVIN(act) ?
  421. tz->trips.active[i - 2].temperature :
  422. CELSIUS_TO_KELVIN(act));
  423. break;
  424. } else
  425. tz->trips.active[i].flags.valid = 1;
  426. }
  427. name[2] = 'L';
  428. if ((flag & ACPI_TRIPS_DEVICES) && tz->trips.active[i].flags.valid ) {
  429. memset(&devices, 0, sizeof(struct acpi_handle_list));
  430. status = acpi_evaluate_reference(tz->device->handle,
  431. name, NULL, &devices);
  432. if (ACPI_FAILURE(status))
  433. tz->trips.active[i].flags.valid = 0;
  434. else
  435. tz->trips.active[i].flags.valid = 1;
  436. if (memcmp(&tz->trips.active[i].devices, &devices,
  437. sizeof(struct acpi_handle_list))) {
  438. memcpy(&tz->trips.active[i].devices, &devices,
  439. sizeof(struct acpi_handle_list));
  440. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
  441. }
  442. }
  443. if ((flag & ACPI_TRIPS_ACTIVE) || (flag & ACPI_TRIPS_DEVICES))
  444. if (valid != tz->trips.active[i].flags.valid)
  445. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "state");
  446. if (!tz->trips.active[i].flags.valid)
  447. break;
  448. }
  449. if (flag & ACPI_TRIPS_DEVICES) {
  450. memset(&devices, 0, sizeof(struct acpi_handle_list));
  451. status = acpi_evaluate_reference(tz->device->handle, "_TZD",
  452. NULL, &devices);
  453. if (memcmp(&tz->devices, &devices,
  454. sizeof(struct acpi_handle_list))) {
  455. memcpy(&tz->devices, &devices,
  456. sizeof(struct acpi_handle_list));
  457. ACPI_THERMAL_TRIPS_EXCEPTION(flag, "device");
  458. }
  459. }
  460. return 0;
  461. }
  462. static int acpi_thermal_get_trip_points(struct acpi_thermal *tz)
  463. {
  464. return acpi_thermal_trips_update(tz, ACPI_TRIPS_INIT);
  465. }
  466. static int acpi_thermal_critical(struct acpi_thermal *tz)
  467. {
  468. if (!tz || !tz->trips.critical.flags.valid)
  469. return -EINVAL;
  470. if (tz->temperature >= tz->trips.critical.temperature) {
  471. printk(KERN_WARNING PREFIX "Critical trip point\n");
  472. tz->trips.critical.flags.enabled = 1;
  473. } else if (tz->trips.critical.flags.enabled)
  474. tz->trips.critical.flags.enabled = 0;
  475. acpi_bus_generate_proc_event(tz->device, ACPI_THERMAL_NOTIFY_CRITICAL,
  476. tz->trips.critical.flags.enabled);
  477. acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
  478. tz->device->dev.bus_id,
  479. ACPI_THERMAL_NOTIFY_CRITICAL,
  480. tz->trips.critical.flags.enabled);
  481. /* take no action if nocrt is set */
  482. if(!nocrt) {
  483. printk(KERN_EMERG
  484. "Critical temperature reached (%ld C), shutting down.\n",
  485. KELVIN_TO_CELSIUS(tz->temperature));
  486. orderly_poweroff(true);
  487. }
  488. return 0;
  489. }
  490. static int acpi_thermal_hot(struct acpi_thermal *tz)
  491. {
  492. if (!tz || !tz->trips.hot.flags.valid)
  493. return -EINVAL;
  494. if (tz->temperature >= tz->trips.hot.temperature) {
  495. printk(KERN_WARNING PREFIX "Hot trip point\n");
  496. tz->trips.hot.flags.enabled = 1;
  497. } else if (tz->trips.hot.flags.enabled)
  498. tz->trips.hot.flags.enabled = 0;
  499. acpi_bus_generate_proc_event(tz->device, ACPI_THERMAL_NOTIFY_HOT,
  500. tz->trips.hot.flags.enabled);
  501. acpi_bus_generate_netlink_event(tz->device->pnp.device_class,
  502. tz->device->dev.bus_id,
  503. ACPI_THERMAL_NOTIFY_HOT,
  504. tz->trips.hot.flags.enabled);
  505. /* TBD: Call user-mode "sleep(S4)" function if nocrt is cleared */
  506. return 0;
  507. }
  508. static void acpi_thermal_passive(struct acpi_thermal *tz)
  509. {
  510. int result = 1;
  511. struct acpi_thermal_passive *passive = NULL;
  512. int trend = 0;
  513. int i = 0;
  514. if (!tz || !tz->trips.passive.flags.valid)
  515. return;
  516. passive = &(tz->trips.passive);
  517. /*
  518. * Above Trip?
  519. * -----------
  520. * Calculate the thermal trend (using the passive cooling equation)
  521. * and modify the performance limit for all passive cooling devices
  522. * accordingly. Note that we assume symmetry.
  523. */
  524. if (tz->temperature >= passive->temperature) {
  525. trend =
  526. (passive->tc1 * (tz->temperature - tz->last_temperature)) +
  527. (passive->tc2 * (tz->temperature - passive->temperature));
  528. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  529. "trend[%d]=(tc1[%lu]*(tmp[%lu]-last[%lu]))+(tc2[%lu]*(tmp[%lu]-psv[%lu]))\n",
  530. trend, passive->tc1, tz->temperature,
  531. tz->last_temperature, passive->tc2,
  532. tz->temperature, passive->temperature));
  533. passive->flags.enabled = 1;
  534. /* Heating up? */
  535. if (trend > 0)
  536. for (i = 0; i < passive->devices.count; i++)
  537. acpi_processor_set_thermal_limit(passive->
  538. devices.
  539. handles[i],
  540. ACPI_PROCESSOR_LIMIT_INCREMENT);
  541. /* Cooling off? */
  542. else if (trend < 0) {
  543. for (i = 0; i < passive->devices.count; i++)
  544. /*
  545. * assume that we are on highest
  546. * freq/lowest thrott and can leave
  547. * passive mode, even in error case
  548. */
  549. if (!acpi_processor_set_thermal_limit
  550. (passive->devices.handles[i],
  551. ACPI_PROCESSOR_LIMIT_DECREMENT))
  552. result = 0;
  553. /*
  554. * Leave cooling mode, even if the temp might
  555. * higher than trip point This is because some
  556. * machines might have long thermal polling
  557. * frequencies (tsp) defined. We will fall back
  558. * into passive mode in next cycle (probably quicker)
  559. */
  560. if (result) {
  561. passive->flags.enabled = 0;
  562. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  563. "Disabling passive cooling, still above threshold,"
  564. " but we are cooling down\n"));
  565. }
  566. }
  567. return;
  568. }
  569. /*
  570. * Below Trip?
  571. * -----------
  572. * Implement passive cooling hysteresis to slowly increase performance
  573. * and avoid thrashing around the passive trip point. Note that we
  574. * assume symmetry.
  575. */
  576. if (!passive->flags.enabled)
  577. return;
  578. for (i = 0; i < passive->devices.count; i++)
  579. if (!acpi_processor_set_thermal_limit
  580. (passive->devices.handles[i],
  581. ACPI_PROCESSOR_LIMIT_DECREMENT))
  582. result = 0;
  583. if (result) {
  584. passive->flags.enabled = 0;
  585. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  586. "Disabling passive cooling (zone is cool)\n"));
  587. }
  588. }
  589. static void acpi_thermal_active(struct acpi_thermal *tz)
  590. {
  591. int result = 0;
  592. struct acpi_thermal_active *active = NULL;
  593. int i = 0;
  594. int j = 0;
  595. unsigned long maxtemp = 0;
  596. if (!tz)
  597. return;
  598. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  599. active = &(tz->trips.active[i]);
  600. if (!active || !active->flags.valid)
  601. break;
  602. if (tz->temperature >= active->temperature) {
  603. /*
  604. * Above Threshold?
  605. * ----------------
  606. * If not already enabled, turn ON all cooling devices
  607. * associated with this active threshold.
  608. */
  609. if (active->temperature > maxtemp)
  610. tz->state.active_index = i;
  611. maxtemp = active->temperature;
  612. if (active->flags.enabled)
  613. continue;
  614. for (j = 0; j < active->devices.count; j++) {
  615. result =
  616. acpi_bus_set_power(active->devices.
  617. handles[j],
  618. ACPI_STATE_D0);
  619. if (result) {
  620. printk(KERN_WARNING PREFIX
  621. "Unable to turn cooling device [%p] 'on'\n",
  622. active->devices.
  623. handles[j]);
  624. continue;
  625. }
  626. active->flags.enabled = 1;
  627. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  628. "Cooling device [%p] now 'on'\n",
  629. active->devices.handles[j]));
  630. }
  631. continue;
  632. }
  633. if (!active->flags.enabled)
  634. continue;
  635. /*
  636. * Below Threshold?
  637. * ----------------
  638. * Turn OFF all cooling devices associated with this
  639. * threshold.
  640. */
  641. for (j = 0; j < active->devices.count; j++) {
  642. result = acpi_bus_set_power(active->devices.handles[j],
  643. ACPI_STATE_D3);
  644. if (result) {
  645. printk(KERN_WARNING PREFIX
  646. "Unable to turn cooling device [%p] 'off'\n",
  647. active->devices.handles[j]);
  648. continue;
  649. }
  650. active->flags.enabled = 0;
  651. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  652. "Cooling device [%p] now 'off'\n",
  653. active->devices.handles[j]));
  654. }
  655. }
  656. }
  657. static void acpi_thermal_check(void *context);
  658. static void acpi_thermal_run(unsigned long data)
  659. {
  660. struct acpi_thermal *tz = (struct acpi_thermal *)data;
  661. if (!tz->zombie)
  662. acpi_os_execute(OSL_GPE_HANDLER, acpi_thermal_check, (void *)data);
  663. }
  664. static void acpi_thermal_check(void *data)
  665. {
  666. int result = 0;
  667. struct acpi_thermal *tz = data;
  668. unsigned long sleep_time = 0;
  669. unsigned long timeout_jiffies = 0;
  670. int i = 0;
  671. struct acpi_thermal_state state;
  672. if (!tz) {
  673. printk(KERN_ERR PREFIX "Invalid (NULL) context\n");
  674. return;
  675. }
  676. /* Check if someone else is already running */
  677. if (!mutex_trylock(&tz->lock))
  678. return;
  679. state = tz->state;
  680. result = acpi_thermal_get_temperature(tz);
  681. if (result)
  682. goto unlock;
  683. if (!tz->tz_enabled)
  684. goto unlock;
  685. memset(&tz->state, 0, sizeof(tz->state));
  686. /*
  687. * Check Trip Points
  688. * -----------------
  689. * Compare the current temperature to the trip point values to see
  690. * if we've entered one of the thermal policy states. Note that
  691. * this function determines when a state is entered, but the
  692. * individual policy decides when it is exited (e.g. hysteresis).
  693. */
  694. if (tz->trips.critical.flags.valid)
  695. state.critical |=
  696. (tz->temperature >= tz->trips.critical.temperature);
  697. if (tz->trips.hot.flags.valid)
  698. state.hot |= (tz->temperature >= tz->trips.hot.temperature);
  699. if (tz->trips.passive.flags.valid)
  700. state.passive |=
  701. (tz->temperature >= tz->trips.passive.temperature);
  702. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
  703. if (tz->trips.active[i].flags.valid)
  704. state.active |=
  705. (tz->temperature >=
  706. tz->trips.active[i].temperature);
  707. /*
  708. * Invoke Policy
  709. * -------------
  710. * Separated from the above check to allow individual policy to
  711. * determine when to exit a given state.
  712. */
  713. if (state.critical)
  714. acpi_thermal_critical(tz);
  715. if (state.hot)
  716. acpi_thermal_hot(tz);
  717. if (state.passive)
  718. acpi_thermal_passive(tz);
  719. if (state.active)
  720. acpi_thermal_active(tz);
  721. /*
  722. * Calculate State
  723. * ---------------
  724. * Again, separated from the above two to allow independent policy
  725. * decisions.
  726. */
  727. tz->state.critical = tz->trips.critical.flags.enabled;
  728. tz->state.hot = tz->trips.hot.flags.enabled;
  729. tz->state.passive = tz->trips.passive.flags.enabled;
  730. tz->state.active = 0;
  731. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++)
  732. tz->state.active |= tz->trips.active[i].flags.enabled;
  733. /*
  734. * Calculate Sleep Time
  735. * --------------------
  736. * If we're in the passive state, use _TSP's value. Otherwise
  737. * use the default polling frequency (e.g. _TZP). If no polling
  738. * frequency is specified then we'll wait forever (at least until
  739. * a thermal event occurs). Note that _TSP and _TZD values are
  740. * given in 1/10th seconds (we must covert to milliseconds).
  741. */
  742. if (tz->state.passive) {
  743. sleep_time = tz->trips.passive.tsp * 100;
  744. timeout_jiffies = jiffies + (HZ * sleep_time) / 1000;
  745. } else if (tz->polling_frequency > 0) {
  746. sleep_time = tz->polling_frequency * 100;
  747. timeout_jiffies = round_jiffies(jiffies + (HZ * sleep_time) / 1000);
  748. }
  749. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: temperature[%lu] sleep[%lu]\n",
  750. tz->name, tz->temperature, sleep_time));
  751. /*
  752. * Schedule Next Poll
  753. * ------------------
  754. */
  755. if (!sleep_time) {
  756. if (timer_pending(&(tz->timer)))
  757. del_timer(&(tz->timer));
  758. } else {
  759. if (timer_pending(&(tz->timer)))
  760. mod_timer(&(tz->timer), timeout_jiffies);
  761. else {
  762. tz->timer.data = (unsigned long)tz;
  763. tz->timer.function = acpi_thermal_run;
  764. tz->timer.expires = timeout_jiffies;
  765. add_timer(&(tz->timer));
  766. }
  767. }
  768. unlock:
  769. mutex_unlock(&tz->lock);
  770. }
  771. /* sys I/F for generic thermal sysfs support */
  772. #define KELVIN_TO_MILLICELSIUS(t) (t * 100 - 273200)
  773. static int thermal_get_temp(struct thermal_zone_device *thermal, char *buf)
  774. {
  775. struct acpi_thermal *tz = thermal->devdata;
  776. if (!tz)
  777. return -EINVAL;
  778. return sprintf(buf, "%ld\n", KELVIN_TO_MILLICELSIUS(tz->temperature));
  779. }
  780. static const char enabled[] = "kernel";
  781. static const char disabled[] = "user";
  782. static int thermal_get_mode(struct thermal_zone_device *thermal,
  783. char *buf)
  784. {
  785. struct acpi_thermal *tz = thermal->devdata;
  786. if (!tz)
  787. return -EINVAL;
  788. return sprintf(buf, "%s\n", tz->tz_enabled ?
  789. enabled : disabled);
  790. }
  791. static int thermal_set_mode(struct thermal_zone_device *thermal,
  792. const char *buf)
  793. {
  794. struct acpi_thermal *tz = thermal->devdata;
  795. int enable;
  796. if (!tz)
  797. return -EINVAL;
  798. /*
  799. * enable/disable thermal management from ACPI thermal driver
  800. */
  801. if (!strncmp(buf, enabled, sizeof enabled - 1))
  802. enable = 1;
  803. else if (!strncmp(buf, disabled, sizeof disabled - 1))
  804. enable = 0;
  805. else
  806. return -EINVAL;
  807. if (enable != tz->tz_enabled) {
  808. tz->tz_enabled = enable;
  809. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  810. "%s ACPI thermal control\n",
  811. tz->tz_enabled ? enabled : disabled));
  812. acpi_thermal_check(tz);
  813. }
  814. return 0;
  815. }
  816. static int thermal_get_trip_type(struct thermal_zone_device *thermal,
  817. int trip, char *buf)
  818. {
  819. struct acpi_thermal *tz = thermal->devdata;
  820. int i;
  821. if (!tz || trip < 0)
  822. return -EINVAL;
  823. if (tz->trips.critical.flags.valid) {
  824. if (!trip)
  825. return sprintf(buf, "critical\n");
  826. trip--;
  827. }
  828. if (tz->trips.hot.flags.valid) {
  829. if (!trip)
  830. return sprintf(buf, "hot\n");
  831. trip--;
  832. }
  833. if (tz->trips.passive.flags.valid) {
  834. if (!trip)
  835. return sprintf(buf, "passive\n");
  836. trip--;
  837. }
  838. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  839. tz->trips.active[i].flags.valid; i++) {
  840. if (!trip)
  841. return sprintf(buf, "active%d\n", i);
  842. trip--;
  843. }
  844. return -EINVAL;
  845. }
  846. static int thermal_get_trip_temp(struct thermal_zone_device *thermal,
  847. int trip, char *buf)
  848. {
  849. struct acpi_thermal *tz = thermal->devdata;
  850. int i;
  851. if (!tz || trip < 0)
  852. return -EINVAL;
  853. if (tz->trips.critical.flags.valid) {
  854. if (!trip)
  855. return sprintf(buf, "%ld\n", KELVIN_TO_MILLICELSIUS(
  856. tz->trips.critical.temperature));
  857. trip--;
  858. }
  859. if (tz->trips.hot.flags.valid) {
  860. if (!trip)
  861. return sprintf(buf, "%ld\n", KELVIN_TO_MILLICELSIUS(
  862. tz->trips.hot.temperature));
  863. trip--;
  864. }
  865. if (tz->trips.passive.flags.valid) {
  866. if (!trip)
  867. return sprintf(buf, "%ld\n", KELVIN_TO_MILLICELSIUS(
  868. tz->trips.passive.temperature));
  869. trip--;
  870. }
  871. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  872. tz->trips.active[i].flags.valid; i++) {
  873. if (!trip)
  874. return sprintf(buf, "%ld\n", KELVIN_TO_MILLICELSIUS(
  875. tz->trips.active[i].temperature));
  876. trip--;
  877. }
  878. return -EINVAL;
  879. }
  880. typedef int (*cb)(struct thermal_zone_device *, int,
  881. struct thermal_cooling_device *);
  882. static int acpi_thermal_cooling_device_cb(struct thermal_zone_device *thermal,
  883. struct thermal_cooling_device *cdev,
  884. cb action)
  885. {
  886. struct acpi_device *device = cdev->devdata;
  887. struct acpi_thermal *tz = thermal->devdata;
  888. struct acpi_device *dev;
  889. acpi_status status;
  890. acpi_handle handle;
  891. int i;
  892. int j;
  893. int trip = -1;
  894. int result = 0;
  895. if (tz->trips.critical.flags.valid)
  896. trip++;
  897. if (tz->trips.hot.flags.valid)
  898. trip++;
  899. if (tz->trips.passive.flags.valid) {
  900. trip++;
  901. for (i = 0; i < tz->trips.passive.devices.count;
  902. i++) {
  903. handle = tz->trips.passive.devices.handles[i];
  904. status = acpi_bus_get_device(handle, &dev);
  905. if (ACPI_SUCCESS(status) && (dev == device)) {
  906. result = action(thermal, trip, cdev);
  907. if (result)
  908. goto failed;
  909. }
  910. }
  911. }
  912. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  913. if (!tz->trips.active[i].flags.valid)
  914. break;
  915. trip++;
  916. for (j = 0;
  917. j < tz->trips.active[i].devices.count;
  918. j++) {
  919. handle = tz->trips.active[i].devices.handles[j];
  920. status = acpi_bus_get_device(handle, &dev);
  921. if (ACPI_SUCCESS(status) && (dev == device)) {
  922. result = action(thermal, trip, cdev);
  923. if (result)
  924. goto failed;
  925. }
  926. }
  927. }
  928. for (i = 0; i < tz->devices.count; i++) {
  929. handle = tz->devices.handles[i];
  930. status = acpi_bus_get_device(handle, &dev);
  931. if (ACPI_SUCCESS(status) && (dev == device)) {
  932. result = action(thermal, -1, cdev);
  933. if (result)
  934. goto failed;
  935. }
  936. }
  937. failed:
  938. return result;
  939. }
  940. static int
  941. acpi_thermal_bind_cooling_device(struct thermal_zone_device *thermal,
  942. struct thermal_cooling_device *cdev)
  943. {
  944. return acpi_thermal_cooling_device_cb(thermal, cdev,
  945. thermal_zone_bind_cooling_device);
  946. }
  947. static int
  948. acpi_thermal_unbind_cooling_device(struct thermal_zone_device *thermal,
  949. struct thermal_cooling_device *cdev)
  950. {
  951. return acpi_thermal_cooling_device_cb(thermal, cdev,
  952. thermal_zone_unbind_cooling_device);
  953. }
  954. static struct thermal_zone_device_ops acpi_thermal_zone_ops = {
  955. .bind = acpi_thermal_bind_cooling_device,
  956. .unbind = acpi_thermal_unbind_cooling_device,
  957. .get_temp = thermal_get_temp,
  958. .get_mode = thermal_get_mode,
  959. .set_mode = thermal_set_mode,
  960. .get_trip_type = thermal_get_trip_type,
  961. .get_trip_temp = thermal_get_trip_temp,
  962. };
  963. static int acpi_thermal_register_thermal_zone(struct acpi_thermal *tz)
  964. {
  965. int trips = 0;
  966. int result;
  967. acpi_status status;
  968. int i;
  969. if (tz->trips.critical.flags.valid)
  970. trips++;
  971. if (tz->trips.hot.flags.valid)
  972. trips++;
  973. if (tz->trips.passive.flags.valid)
  974. trips++;
  975. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE &&
  976. tz->trips.active[i].flags.valid; i++, trips++);
  977. tz->thermal_zone = thermal_zone_device_register("ACPI thermal zone",
  978. trips, tz, &acpi_thermal_zone_ops);
  979. if (IS_ERR(tz->thermal_zone))
  980. return -ENODEV;
  981. result = sysfs_create_link(&tz->device->dev.kobj,
  982. &tz->thermal_zone->device.kobj, "thermal_zone");
  983. if (result)
  984. return result;
  985. result = sysfs_create_link(&tz->thermal_zone->device.kobj,
  986. &tz->device->dev.kobj, "device");
  987. if (result)
  988. return result;
  989. status = acpi_attach_data(tz->device->handle,
  990. acpi_bus_private_data_handler,
  991. tz->thermal_zone);
  992. if (ACPI_FAILURE(status)) {
  993. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  994. "Error attaching device data\n"));
  995. return -ENODEV;
  996. }
  997. tz->tz_enabled = 1;
  998. printk(KERN_INFO PREFIX "%s is registered as thermal_zone%d\n",
  999. tz->device->dev.bus_id, tz->thermal_zone->id);
  1000. return 0;
  1001. }
  1002. static void acpi_thermal_unregister_thermal_zone(struct acpi_thermal *tz)
  1003. {
  1004. sysfs_remove_link(&tz->device->dev.kobj, "thermal_zone");
  1005. sysfs_remove_link(&tz->thermal_zone->device.kobj, "device");
  1006. thermal_zone_device_unregister(tz->thermal_zone);
  1007. tz->thermal_zone = NULL;
  1008. acpi_detach_data(tz->device->handle, acpi_bus_private_data_handler);
  1009. }
  1010. /* --------------------------------------------------------------------------
  1011. FS Interface (/proc)
  1012. -------------------------------------------------------------------------- */
  1013. static struct proc_dir_entry *acpi_thermal_dir;
  1014. static int acpi_thermal_state_seq_show(struct seq_file *seq, void *offset)
  1015. {
  1016. struct acpi_thermal *tz = seq->private;
  1017. if (!tz)
  1018. goto end;
  1019. seq_puts(seq, "state: ");
  1020. if (!tz->state.critical && !tz->state.hot && !tz->state.passive
  1021. && !tz->state.active)
  1022. seq_puts(seq, "ok\n");
  1023. else {
  1024. if (tz->state.critical)
  1025. seq_puts(seq, "critical ");
  1026. if (tz->state.hot)
  1027. seq_puts(seq, "hot ");
  1028. if (tz->state.passive)
  1029. seq_puts(seq, "passive ");
  1030. if (tz->state.active)
  1031. seq_printf(seq, "active[%d]", tz->state.active_index);
  1032. seq_puts(seq, "\n");
  1033. }
  1034. end:
  1035. return 0;
  1036. }
  1037. static int acpi_thermal_state_open_fs(struct inode *inode, struct file *file)
  1038. {
  1039. return single_open(file, acpi_thermal_state_seq_show, PDE(inode)->data);
  1040. }
  1041. static int acpi_thermal_temp_seq_show(struct seq_file *seq, void *offset)
  1042. {
  1043. int result = 0;
  1044. struct acpi_thermal *tz = seq->private;
  1045. if (!tz)
  1046. goto end;
  1047. result = acpi_thermal_get_temperature(tz);
  1048. if (result)
  1049. goto end;
  1050. seq_printf(seq, "temperature: %ld C\n",
  1051. KELVIN_TO_CELSIUS(tz->temperature));
  1052. end:
  1053. return 0;
  1054. }
  1055. static int acpi_thermal_temp_open_fs(struct inode *inode, struct file *file)
  1056. {
  1057. return single_open(file, acpi_thermal_temp_seq_show, PDE(inode)->data);
  1058. }
  1059. static int acpi_thermal_trip_seq_show(struct seq_file *seq, void *offset)
  1060. {
  1061. struct acpi_thermal *tz = seq->private;
  1062. struct acpi_device *device;
  1063. acpi_status status;
  1064. int i = 0;
  1065. int j = 0;
  1066. if (!tz)
  1067. goto end;
  1068. if (tz->trips.critical.flags.valid)
  1069. seq_printf(seq, "critical (S5): %ld C%s",
  1070. KELVIN_TO_CELSIUS(tz->trips.critical.temperature),
  1071. nocrt ? " <disabled>\n" : "\n");
  1072. if (tz->trips.hot.flags.valid)
  1073. seq_printf(seq, "hot (S4): %ld C%s",
  1074. KELVIN_TO_CELSIUS(tz->trips.hot.temperature),
  1075. nocrt ? " <disabled>\n" : "\n");
  1076. if (tz->trips.passive.flags.valid) {
  1077. seq_printf(seq,
  1078. "passive: %ld C: tc1=%lu tc2=%lu tsp=%lu devices=",
  1079. KELVIN_TO_CELSIUS(tz->trips.passive.temperature),
  1080. tz->trips.passive.tc1, tz->trips.passive.tc2,
  1081. tz->trips.passive.tsp);
  1082. for (j = 0; j < tz->trips.passive.devices.count; j++) {
  1083. status = acpi_bus_get_device(tz->trips.passive.devices.
  1084. handles[j], &device);
  1085. seq_printf(seq, "%4.4s ", status ? "" :
  1086. acpi_device_bid(device));
  1087. }
  1088. seq_puts(seq, "\n");
  1089. }
  1090. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  1091. if (!(tz->trips.active[i].flags.valid))
  1092. break;
  1093. seq_printf(seq, "active[%d]: %ld C: devices=",
  1094. i,
  1095. KELVIN_TO_CELSIUS(tz->trips.active[i].temperature));
  1096. for (j = 0; j < tz->trips.active[i].devices.count; j++){
  1097. status = acpi_bus_get_device(tz->trips.active[i].
  1098. devices.handles[j],
  1099. &device);
  1100. seq_printf(seq, "%4.4s ", status ? "" :
  1101. acpi_device_bid(device));
  1102. }
  1103. seq_puts(seq, "\n");
  1104. }
  1105. end:
  1106. return 0;
  1107. }
  1108. static int acpi_thermal_trip_open_fs(struct inode *inode, struct file *file)
  1109. {
  1110. return single_open(file, acpi_thermal_trip_seq_show, PDE(inode)->data);
  1111. }
  1112. static int acpi_thermal_cooling_seq_show(struct seq_file *seq, void *offset)
  1113. {
  1114. struct acpi_thermal *tz = seq->private;
  1115. if (!tz)
  1116. goto end;
  1117. if (!tz->flags.cooling_mode)
  1118. seq_puts(seq, "<setting not supported>\n");
  1119. else
  1120. seq_puts(seq, "0 - Active; 1 - Passive\n");
  1121. end:
  1122. return 0;
  1123. }
  1124. static int acpi_thermal_cooling_open_fs(struct inode *inode, struct file *file)
  1125. {
  1126. return single_open(file, acpi_thermal_cooling_seq_show,
  1127. PDE(inode)->data);
  1128. }
  1129. static ssize_t
  1130. acpi_thermal_write_cooling_mode(struct file *file,
  1131. const char __user * buffer,
  1132. size_t count, loff_t * ppos)
  1133. {
  1134. struct seq_file *m = file->private_data;
  1135. struct acpi_thermal *tz = m->private;
  1136. int result = 0;
  1137. char mode_string[12] = { '\0' };
  1138. if (!tz || (count > sizeof(mode_string) - 1))
  1139. return -EINVAL;
  1140. if (!tz->flags.cooling_mode)
  1141. return -ENODEV;
  1142. if (copy_from_user(mode_string, buffer, count))
  1143. return -EFAULT;
  1144. mode_string[count] = '\0';
  1145. result = acpi_thermal_set_cooling_mode(tz,
  1146. simple_strtoul(mode_string, NULL,
  1147. 0));
  1148. if (result)
  1149. return result;
  1150. acpi_thermal_check(tz);
  1151. return count;
  1152. }
  1153. static int acpi_thermal_polling_seq_show(struct seq_file *seq, void *offset)
  1154. {
  1155. struct acpi_thermal *tz = seq->private;
  1156. if (!tz)
  1157. goto end;
  1158. if (!tz->polling_frequency) {
  1159. seq_puts(seq, "<polling disabled>\n");
  1160. goto end;
  1161. }
  1162. seq_printf(seq, "polling frequency: %lu seconds\n",
  1163. (tz->polling_frequency / 10));
  1164. end:
  1165. return 0;
  1166. }
  1167. static int acpi_thermal_polling_open_fs(struct inode *inode, struct file *file)
  1168. {
  1169. return single_open(file, acpi_thermal_polling_seq_show,
  1170. PDE(inode)->data);
  1171. }
  1172. static ssize_t
  1173. acpi_thermal_write_polling(struct file *file,
  1174. const char __user * buffer,
  1175. size_t count, loff_t * ppos)
  1176. {
  1177. struct seq_file *m = file->private_data;
  1178. struct acpi_thermal *tz = m->private;
  1179. int result = 0;
  1180. char polling_string[12] = { '\0' };
  1181. int seconds = 0;
  1182. if (!tz || (count > sizeof(polling_string) - 1))
  1183. return -EINVAL;
  1184. if (copy_from_user(polling_string, buffer, count))
  1185. return -EFAULT;
  1186. polling_string[count] = '\0';
  1187. seconds = simple_strtoul(polling_string, NULL, 0);
  1188. result = acpi_thermal_set_polling(tz, seconds);
  1189. if (result)
  1190. return result;
  1191. acpi_thermal_check(tz);
  1192. return count;
  1193. }
  1194. static int acpi_thermal_add_fs(struct acpi_device *device)
  1195. {
  1196. struct proc_dir_entry *entry = NULL;
  1197. if (!acpi_device_dir(device)) {
  1198. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  1199. acpi_thermal_dir);
  1200. if (!acpi_device_dir(device))
  1201. return -ENODEV;
  1202. acpi_device_dir(device)->owner = THIS_MODULE;
  1203. }
  1204. /* 'state' [R] */
  1205. entry = create_proc_entry(ACPI_THERMAL_FILE_STATE,
  1206. S_IRUGO, acpi_device_dir(device));
  1207. if (!entry)
  1208. return -ENODEV;
  1209. else {
  1210. entry->proc_fops = &acpi_thermal_state_fops;
  1211. entry->data = acpi_driver_data(device);
  1212. entry->owner = THIS_MODULE;
  1213. }
  1214. /* 'temperature' [R] */
  1215. entry = create_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
  1216. S_IRUGO, acpi_device_dir(device));
  1217. if (!entry)
  1218. return -ENODEV;
  1219. else {
  1220. entry->proc_fops = &acpi_thermal_temp_fops;
  1221. entry->data = acpi_driver_data(device);
  1222. entry->owner = THIS_MODULE;
  1223. }
  1224. /* 'trip_points' [R] */
  1225. entry = create_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
  1226. S_IRUGO,
  1227. acpi_device_dir(device));
  1228. if (!entry)
  1229. return -ENODEV;
  1230. else {
  1231. entry->proc_fops = &acpi_thermal_trip_fops;
  1232. entry->data = acpi_driver_data(device);
  1233. entry->owner = THIS_MODULE;
  1234. }
  1235. /* 'cooling_mode' [R/W] */
  1236. entry = create_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
  1237. S_IFREG | S_IRUGO | S_IWUSR,
  1238. acpi_device_dir(device));
  1239. if (!entry)
  1240. return -ENODEV;
  1241. else {
  1242. entry->proc_fops = &acpi_thermal_cooling_fops;
  1243. entry->data = acpi_driver_data(device);
  1244. entry->owner = THIS_MODULE;
  1245. }
  1246. /* 'polling_frequency' [R/W] */
  1247. entry = create_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
  1248. S_IFREG | S_IRUGO | S_IWUSR,
  1249. acpi_device_dir(device));
  1250. if (!entry)
  1251. return -ENODEV;
  1252. else {
  1253. entry->proc_fops = &acpi_thermal_polling_fops;
  1254. entry->data = acpi_driver_data(device);
  1255. entry->owner = THIS_MODULE;
  1256. }
  1257. return 0;
  1258. }
  1259. static int acpi_thermal_remove_fs(struct acpi_device *device)
  1260. {
  1261. if (acpi_device_dir(device)) {
  1262. remove_proc_entry(ACPI_THERMAL_FILE_POLLING_FREQ,
  1263. acpi_device_dir(device));
  1264. remove_proc_entry(ACPI_THERMAL_FILE_COOLING_MODE,
  1265. acpi_device_dir(device));
  1266. remove_proc_entry(ACPI_THERMAL_FILE_TRIP_POINTS,
  1267. acpi_device_dir(device));
  1268. remove_proc_entry(ACPI_THERMAL_FILE_TEMPERATURE,
  1269. acpi_device_dir(device));
  1270. remove_proc_entry(ACPI_THERMAL_FILE_STATE,
  1271. acpi_device_dir(device));
  1272. remove_proc_entry(acpi_device_bid(device), acpi_thermal_dir);
  1273. acpi_device_dir(device) = NULL;
  1274. }
  1275. return 0;
  1276. }
  1277. /* --------------------------------------------------------------------------
  1278. Driver Interface
  1279. -------------------------------------------------------------------------- */
  1280. static void acpi_thermal_notify(acpi_handle handle, u32 event, void *data)
  1281. {
  1282. struct acpi_thermal *tz = data;
  1283. struct acpi_device *device = NULL;
  1284. if (!tz)
  1285. return;
  1286. device = tz->device;
  1287. switch (event) {
  1288. case ACPI_THERMAL_NOTIFY_TEMPERATURE:
  1289. acpi_thermal_check(tz);
  1290. break;
  1291. case ACPI_THERMAL_NOTIFY_THRESHOLDS:
  1292. acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_THRESHOLDS);
  1293. acpi_thermal_check(tz);
  1294. acpi_bus_generate_proc_event(device, event, 0);
  1295. acpi_bus_generate_netlink_event(device->pnp.device_class,
  1296. device->dev.bus_id, event, 0);
  1297. break;
  1298. case ACPI_THERMAL_NOTIFY_DEVICES:
  1299. acpi_thermal_trips_update(tz, ACPI_TRIPS_REFRESH_DEVICES);
  1300. acpi_thermal_check(tz);
  1301. acpi_bus_generate_proc_event(device, event, 0);
  1302. acpi_bus_generate_netlink_event(device->pnp.device_class,
  1303. device->dev.bus_id, event, 0);
  1304. break;
  1305. default:
  1306. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1307. "Unsupported event [0x%x]\n", event));
  1308. break;
  1309. }
  1310. return;
  1311. }
  1312. static int acpi_thermal_get_info(struct acpi_thermal *tz)
  1313. {
  1314. int result = 0;
  1315. if (!tz)
  1316. return -EINVAL;
  1317. /* Get temperature [_TMP] (required) */
  1318. result = acpi_thermal_get_temperature(tz);
  1319. if (result)
  1320. return result;
  1321. /* Get trip points [_CRT, _PSV, etc.] (required) */
  1322. result = acpi_thermal_get_trip_points(tz);
  1323. if (result)
  1324. return result;
  1325. /* Set the cooling mode [_SCP] to active cooling (default) */
  1326. result = acpi_thermal_set_cooling_mode(tz, ACPI_THERMAL_MODE_ACTIVE);
  1327. if (!result)
  1328. tz->flags.cooling_mode = 1;
  1329. /* Get default polling frequency [_TZP] (optional) */
  1330. if (tzp)
  1331. tz->polling_frequency = tzp;
  1332. else
  1333. acpi_thermal_get_polling_frequency(tz);
  1334. return 0;
  1335. }
  1336. static int acpi_thermal_add(struct acpi_device *device)
  1337. {
  1338. int result = 0;
  1339. acpi_status status = AE_OK;
  1340. struct acpi_thermal *tz = NULL;
  1341. if (!device)
  1342. return -EINVAL;
  1343. tz = kzalloc(sizeof(struct acpi_thermal), GFP_KERNEL);
  1344. if (!tz)
  1345. return -ENOMEM;
  1346. tz->device = device;
  1347. strcpy(tz->name, device->pnp.bus_id);
  1348. strcpy(acpi_device_name(device), ACPI_THERMAL_DEVICE_NAME);
  1349. strcpy(acpi_device_class(device), ACPI_THERMAL_CLASS);
  1350. acpi_driver_data(device) = tz;
  1351. mutex_init(&tz->lock);
  1352. result = acpi_thermal_get_info(tz);
  1353. if (result)
  1354. goto free_memory;
  1355. result = acpi_thermal_register_thermal_zone(tz);
  1356. if (result)
  1357. goto free_memory;
  1358. result = acpi_thermal_add_fs(device);
  1359. if (result)
  1360. goto unregister_thermal_zone;
  1361. init_timer(&tz->timer);
  1362. acpi_thermal_check(tz);
  1363. status = acpi_install_notify_handler(device->handle,
  1364. ACPI_DEVICE_NOTIFY,
  1365. acpi_thermal_notify, tz);
  1366. if (ACPI_FAILURE(status)) {
  1367. result = -ENODEV;
  1368. goto remove_fs;
  1369. }
  1370. printk(KERN_INFO PREFIX "%s [%s] (%ld C)\n",
  1371. acpi_device_name(device), acpi_device_bid(device),
  1372. KELVIN_TO_CELSIUS(tz->temperature));
  1373. goto end;
  1374. remove_fs:
  1375. acpi_thermal_remove_fs(device);
  1376. unregister_thermal_zone:
  1377. thermal_zone_device_unregister(tz->thermal_zone);
  1378. free_memory:
  1379. kfree(tz);
  1380. end:
  1381. return result;
  1382. }
  1383. static int acpi_thermal_remove(struct acpi_device *device, int type)
  1384. {
  1385. acpi_status status = AE_OK;
  1386. struct acpi_thermal *tz = NULL;
  1387. if (!device || !acpi_driver_data(device))
  1388. return -EINVAL;
  1389. tz = acpi_driver_data(device);
  1390. /* avoid timer adding new defer task */
  1391. tz->zombie = 1;
  1392. /* wait for running timer (on other CPUs) finish */
  1393. del_timer_sync(&(tz->timer));
  1394. /* synchronize deferred task */
  1395. acpi_os_wait_events_complete(NULL);
  1396. /* deferred task may reinsert timer */
  1397. del_timer_sync(&(tz->timer));
  1398. status = acpi_remove_notify_handler(device->handle,
  1399. ACPI_DEVICE_NOTIFY,
  1400. acpi_thermal_notify);
  1401. /* Terminate policy */
  1402. if (tz->trips.passive.flags.valid && tz->trips.passive.flags.enabled) {
  1403. tz->trips.passive.flags.enabled = 0;
  1404. acpi_thermal_passive(tz);
  1405. }
  1406. if (tz->trips.active[0].flags.valid
  1407. && tz->trips.active[0].flags.enabled) {
  1408. tz->trips.active[0].flags.enabled = 0;
  1409. acpi_thermal_active(tz);
  1410. }
  1411. acpi_thermal_remove_fs(device);
  1412. acpi_thermal_unregister_thermal_zone(tz);
  1413. mutex_destroy(&tz->lock);
  1414. kfree(tz);
  1415. return 0;
  1416. }
  1417. static int acpi_thermal_resume(struct acpi_device *device)
  1418. {
  1419. struct acpi_thermal *tz = NULL;
  1420. int i, j, power_state, result;
  1421. if (!device || !acpi_driver_data(device))
  1422. return -EINVAL;
  1423. tz = acpi_driver_data(device);
  1424. for (i = 0; i < ACPI_THERMAL_MAX_ACTIVE; i++) {
  1425. if (!(&tz->trips.active[i]))
  1426. break;
  1427. if (!tz->trips.active[i].flags.valid)
  1428. break;
  1429. tz->trips.active[i].flags.enabled = 1;
  1430. for (j = 0; j < tz->trips.active[i].devices.count; j++) {
  1431. result = acpi_bus_get_power(tz->trips.active[i].devices.
  1432. handles[j], &power_state);
  1433. if (result || (power_state != ACPI_STATE_D0)) {
  1434. tz->trips.active[i].flags.enabled = 0;
  1435. break;
  1436. }
  1437. }
  1438. tz->state.active |= tz->trips.active[i].flags.enabled;
  1439. }
  1440. acpi_thermal_check(tz);
  1441. return AE_OK;
  1442. }
  1443. #ifdef CONFIG_DMI
  1444. static int thermal_act(const struct dmi_system_id *d) {
  1445. if (act == 0) {
  1446. printk(KERN_NOTICE "ACPI: %s detected: "
  1447. "disabling all active thermal trip points\n", d->ident);
  1448. act = -1;
  1449. }
  1450. return 0;
  1451. }
  1452. static int thermal_nocrt(const struct dmi_system_id *d) {
  1453. printk(KERN_NOTICE "ACPI: %s detected: "
  1454. "disabling all critical thermal trip point actions.\n", d->ident);
  1455. nocrt = 1;
  1456. return 0;
  1457. }
  1458. static int thermal_tzp(const struct dmi_system_id *d) {
  1459. if (tzp == 0) {
  1460. printk(KERN_NOTICE "ACPI: %s detected: "
  1461. "enabling thermal zone polling\n", d->ident);
  1462. tzp = 300; /* 300 dS = 30 Seconds */
  1463. }
  1464. return 0;
  1465. }
  1466. static int thermal_psv(const struct dmi_system_id *d) {
  1467. if (psv == 0) {
  1468. printk(KERN_NOTICE "ACPI: %s detected: "
  1469. "disabling all passive thermal trip points\n", d->ident);
  1470. psv = -1;
  1471. }
  1472. return 0;
  1473. }
  1474. static struct dmi_system_id thermal_dmi_table[] __initdata = {
  1475. /*
  1476. * Award BIOS on this AOpen makes thermal control almost worthless.
  1477. * http://bugzilla.kernel.org/show_bug.cgi?id=8842
  1478. */
  1479. {
  1480. .callback = thermal_act,
  1481. .ident = "AOpen i915GMm-HFS",
  1482. .matches = {
  1483. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  1484. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  1485. },
  1486. },
  1487. {
  1488. .callback = thermal_psv,
  1489. .ident = "AOpen i915GMm-HFS",
  1490. .matches = {
  1491. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  1492. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  1493. },
  1494. },
  1495. {
  1496. .callback = thermal_tzp,
  1497. .ident = "AOpen i915GMm-HFS",
  1498. .matches = {
  1499. DMI_MATCH(DMI_BOARD_VENDOR, "AOpen"),
  1500. DMI_MATCH(DMI_BOARD_NAME, "i915GMm-HFS"),
  1501. },
  1502. },
  1503. {
  1504. .callback = thermal_nocrt,
  1505. .ident = "Gigabyte GA-7ZX",
  1506. .matches = {
  1507. DMI_MATCH(DMI_BOARD_VENDOR, "Gigabyte Technology Co., Ltd."),
  1508. DMI_MATCH(DMI_BOARD_NAME, "7ZX"),
  1509. },
  1510. },
  1511. {}
  1512. };
  1513. #endif /* CONFIG_DMI */
  1514. static int __init acpi_thermal_init(void)
  1515. {
  1516. int result = 0;
  1517. dmi_check_system(thermal_dmi_table);
  1518. if (off) {
  1519. printk(KERN_NOTICE "ACPI: thermal control disabled\n");
  1520. return -ENODEV;
  1521. }
  1522. acpi_thermal_dir = proc_mkdir(ACPI_THERMAL_CLASS, acpi_root_dir);
  1523. if (!acpi_thermal_dir)
  1524. return -ENODEV;
  1525. acpi_thermal_dir->owner = THIS_MODULE;
  1526. result = acpi_bus_register_driver(&acpi_thermal_driver);
  1527. if (result < 0) {
  1528. remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
  1529. return -ENODEV;
  1530. }
  1531. return 0;
  1532. }
  1533. static void __exit acpi_thermal_exit(void)
  1534. {
  1535. acpi_bus_unregister_driver(&acpi_thermal_driver);
  1536. remove_proc_entry(ACPI_THERMAL_CLASS, acpi_root_dir);
  1537. return;
  1538. }
  1539. module_init(acpi_thermal_init);
  1540. module_exit(acpi_thermal_exit);