thermal.c 47 KB

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