thermal.c 46 KB

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