thermal.c 46 KB

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