thermal.c 46 KB

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