thermal.c 47 KB

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