thermal_core.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * thermal.c - Generic Thermal Management Sysfs support.
  4. *
  5. * Copyright (C) 2008 Intel Corp
  6. * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
  7. * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <linux/export.h>
  13. #include <linux/slab.h>
  14. #include <linux/kdev_t.h>
  15. #include <linux/idr.h>
  16. #include <linux/list_sort.h>
  17. #include <linux/thermal.h>
  18. #include <linux/reboot.h>
  19. #include <linux/string.h>
  20. #include <linux/of.h>
  21. #include <linux/suspend.h>
  22. #define CREATE_TRACE_POINTS
  23. #include "thermal_trace.h"
  24. #include "thermal_core.h"
  25. #include "thermal_hwmon.h"
  26. static DEFINE_IDA(thermal_tz_ida);
  27. static DEFINE_IDA(thermal_cdev_ida);
  28. static LIST_HEAD(thermal_tz_list);
  29. static LIST_HEAD(thermal_cdev_list);
  30. static LIST_HEAD(thermal_governor_list);
  31. static DEFINE_MUTEX(thermal_list_lock);
  32. static DEFINE_MUTEX(thermal_governor_lock);
  33. static struct thermal_governor *def_governor;
  34. static bool thermal_pm_suspended;
  35. /*
  36. * Governor section: set of functions to handle thermal governors
  37. *
  38. * Functions to help in the life cycle of thermal governors within
  39. * the thermal core and by the thermal governor code.
  40. */
  41. static struct thermal_governor *__find_governor(const char *name)
  42. {
  43. struct thermal_governor *pos;
  44. if (!name || !name[0])
  45. return def_governor;
  46. list_for_each_entry(pos, &thermal_governor_list, governor_list)
  47. if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
  48. return pos;
  49. return NULL;
  50. }
  51. /**
  52. * bind_previous_governor() - bind the previous governor of the thermal zone
  53. * @tz: a valid pointer to a struct thermal_zone_device
  54. * @failed_gov_name: the name of the governor that failed to register
  55. *
  56. * Register the previous governor of the thermal zone after a new
  57. * governor has failed to be bound.
  58. */
  59. static void bind_previous_governor(struct thermal_zone_device *tz,
  60. const char *failed_gov_name)
  61. {
  62. if (tz->governor && tz->governor->bind_to_tz) {
  63. if (tz->governor->bind_to_tz(tz)) {
  64. dev_err(&tz->device,
  65. "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
  66. failed_gov_name, tz->governor->name, tz->type);
  67. tz->governor = NULL;
  68. }
  69. }
  70. }
  71. /**
  72. * thermal_set_governor() - Switch to another governor
  73. * @tz: a valid pointer to a struct thermal_zone_device
  74. * @new_gov: pointer to the new governor
  75. *
  76. * Change the governor of thermal zone @tz.
  77. *
  78. * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
  79. */
  80. static int thermal_set_governor(struct thermal_zone_device *tz,
  81. struct thermal_governor *new_gov)
  82. {
  83. int ret = 0;
  84. if (tz->governor && tz->governor->unbind_from_tz)
  85. tz->governor->unbind_from_tz(tz);
  86. if (new_gov && new_gov->bind_to_tz) {
  87. ret = new_gov->bind_to_tz(tz);
  88. if (ret) {
  89. bind_previous_governor(tz, new_gov->name);
  90. return ret;
  91. }
  92. }
  93. tz->governor = new_gov;
  94. return ret;
  95. }
  96. int thermal_register_governor(struct thermal_governor *governor)
  97. {
  98. int err;
  99. const char *name;
  100. struct thermal_zone_device *pos;
  101. if (!governor)
  102. return -EINVAL;
  103. mutex_lock(&thermal_governor_lock);
  104. err = -EBUSY;
  105. if (!__find_governor(governor->name)) {
  106. bool match_default;
  107. err = 0;
  108. list_add(&governor->governor_list, &thermal_governor_list);
  109. match_default = !strncmp(governor->name,
  110. DEFAULT_THERMAL_GOVERNOR,
  111. THERMAL_NAME_LENGTH);
  112. if (!def_governor && match_default)
  113. def_governor = governor;
  114. }
  115. mutex_lock(&thermal_list_lock);
  116. list_for_each_entry(pos, &thermal_tz_list, node) {
  117. /*
  118. * only thermal zones with specified tz->tzp->governor_name
  119. * may run with tz->govenor unset
  120. */
  121. if (pos->governor)
  122. continue;
  123. name = pos->tzp->governor_name;
  124. if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
  125. int ret;
  126. ret = thermal_set_governor(pos, governor);
  127. if (ret)
  128. dev_err(&pos->device,
  129. "Failed to set governor %s for thermal zone %s: %d\n",
  130. governor->name, pos->type, ret);
  131. }
  132. }
  133. mutex_unlock(&thermal_list_lock);
  134. mutex_unlock(&thermal_governor_lock);
  135. return err;
  136. }
  137. void thermal_unregister_governor(struct thermal_governor *governor)
  138. {
  139. struct thermal_zone_device *pos;
  140. if (!governor)
  141. return;
  142. mutex_lock(&thermal_governor_lock);
  143. if (!__find_governor(governor->name))
  144. goto exit;
  145. mutex_lock(&thermal_list_lock);
  146. list_for_each_entry(pos, &thermal_tz_list, node) {
  147. if (!strncasecmp(pos->governor->name, governor->name,
  148. THERMAL_NAME_LENGTH))
  149. thermal_set_governor(pos, NULL);
  150. }
  151. mutex_unlock(&thermal_list_lock);
  152. list_del(&governor->governor_list);
  153. exit:
  154. mutex_unlock(&thermal_governor_lock);
  155. }
  156. int thermal_zone_device_set_policy(struct thermal_zone_device *tz,
  157. char *policy)
  158. {
  159. struct thermal_governor *gov;
  160. int ret = -EINVAL;
  161. mutex_lock(&thermal_governor_lock);
  162. mutex_lock(&tz->lock);
  163. gov = __find_governor(strim(policy));
  164. if (!gov)
  165. goto exit;
  166. ret = thermal_set_governor(tz, gov);
  167. exit:
  168. mutex_unlock(&tz->lock);
  169. mutex_unlock(&thermal_governor_lock);
  170. thermal_notify_tz_gov_change(tz, policy);
  171. return ret;
  172. }
  173. int thermal_build_list_of_policies(char *buf)
  174. {
  175. struct thermal_governor *pos;
  176. ssize_t count = 0;
  177. mutex_lock(&thermal_governor_lock);
  178. list_for_each_entry(pos, &thermal_governor_list, governor_list) {
  179. count += sysfs_emit_at(buf, count, "%s ", pos->name);
  180. }
  181. count += sysfs_emit_at(buf, count, "\n");
  182. mutex_unlock(&thermal_governor_lock);
  183. return count;
  184. }
  185. static void __init thermal_unregister_governors(void)
  186. {
  187. struct thermal_governor **governor;
  188. for_each_governor_table(governor)
  189. thermal_unregister_governor(*governor);
  190. }
  191. static int __init thermal_register_governors(void)
  192. {
  193. int ret = 0;
  194. struct thermal_governor **governor;
  195. for_each_governor_table(governor) {
  196. ret = thermal_register_governor(*governor);
  197. if (ret) {
  198. pr_err("Failed to register governor: '%s'",
  199. (*governor)->name);
  200. break;
  201. }
  202. pr_info("Registered thermal governor '%s'",
  203. (*governor)->name);
  204. }
  205. if (ret) {
  206. struct thermal_governor **gov;
  207. for_each_governor_table(gov) {
  208. if (gov == governor)
  209. break;
  210. thermal_unregister_governor(*gov);
  211. }
  212. }
  213. return ret;
  214. }
  215. static int __thermal_zone_device_set_mode(struct thermal_zone_device *tz,
  216. enum thermal_device_mode mode)
  217. {
  218. if (tz->ops.change_mode) {
  219. int ret;
  220. ret = tz->ops.change_mode(tz, mode);
  221. if (ret)
  222. return ret;
  223. }
  224. tz->mode = mode;
  225. return 0;
  226. }
  227. static void thermal_zone_broken_disable(struct thermal_zone_device *tz)
  228. {
  229. struct thermal_trip_desc *td;
  230. dev_err(&tz->device, "Unable to get temperature, disabling!\n");
  231. /*
  232. * This function only runs for enabled thermal zones, so no need to
  233. * check for the current mode.
  234. */
  235. __thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
  236. thermal_notify_tz_disable(tz);
  237. for_each_trip_desc(tz, td) {
  238. if (td->trip.type == THERMAL_TRIP_CRITICAL &&
  239. td->trip.temperature > THERMAL_TEMP_INVALID) {
  240. dev_crit(&tz->device,
  241. "Disabled thermal zone with critical trip point\n");
  242. return;
  243. }
  244. }
  245. }
  246. /*
  247. * Zone update section: main control loop applied to each zone while monitoring
  248. * in polling mode. The monitoring is done using a workqueue.
  249. * Same update may be done on a zone by calling thermal_zone_device_update().
  250. *
  251. * An update means:
  252. * - Non-critical trips will invoke the governor responsible for that zone;
  253. * - Hot trips will produce a notification to userspace;
  254. * - Critical trip point will cause a system shutdown.
  255. */
  256. static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
  257. unsigned long delay)
  258. {
  259. if (delay > HZ)
  260. delay = round_jiffies_relative(delay);
  261. mod_delayed_work(system_freezable_power_efficient_wq, &tz->poll_queue, delay);
  262. }
  263. static void thermal_zone_recheck(struct thermal_zone_device *tz, int error)
  264. {
  265. if (error == -EAGAIN) {
  266. thermal_zone_device_set_polling(tz, THERMAL_RECHECK_DELAY);
  267. return;
  268. }
  269. /*
  270. * Print the message once to reduce log noise. It will be followed by
  271. * another one if the temperature cannot be determined after multiple
  272. * attempts.
  273. */
  274. if (tz->recheck_delay_jiffies == THERMAL_RECHECK_DELAY)
  275. dev_info(&tz->device, "Temperature check failed (%d)\n", error);
  276. thermal_zone_device_set_polling(tz, tz->recheck_delay_jiffies);
  277. tz->recheck_delay_jiffies += max(tz->recheck_delay_jiffies >> 1, 1ULL);
  278. if (tz->recheck_delay_jiffies > THERMAL_MAX_RECHECK_DELAY) {
  279. thermal_zone_broken_disable(tz);
  280. /*
  281. * Restore the original recheck delay value to allow the thermal
  282. * zone to try to recover when it is reenabled by user space.
  283. */
  284. tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY;
  285. }
  286. }
  287. static void monitor_thermal_zone(struct thermal_zone_device *tz)
  288. {
  289. if (tz->passive > 0 && tz->passive_delay_jiffies)
  290. thermal_zone_device_set_polling(tz, tz->passive_delay_jiffies);
  291. else if (tz->polling_delay_jiffies)
  292. thermal_zone_device_set_polling(tz, tz->polling_delay_jiffies);
  293. }
  294. static struct thermal_governor *thermal_get_tz_governor(struct thermal_zone_device *tz)
  295. {
  296. if (tz->governor)
  297. return tz->governor;
  298. return def_governor;
  299. }
  300. void thermal_governor_update_tz(struct thermal_zone_device *tz,
  301. enum thermal_notify_event reason)
  302. {
  303. if (!tz->governor || !tz->governor->update_tz)
  304. return;
  305. tz->governor->update_tz(tz, reason);
  306. }
  307. static void thermal_zone_device_halt(struct thermal_zone_device *tz, bool shutdown)
  308. {
  309. /*
  310. * poweroff_delay_ms must be a carefully profiled positive value.
  311. * Its a must for forced_emergency_poweroff_work to be scheduled.
  312. */
  313. int poweroff_delay_ms = CONFIG_THERMAL_EMERGENCY_POWEROFF_DELAY_MS;
  314. const char *msg = "Temperature too high";
  315. dev_emerg(&tz->device, "%s: critical temperature reached\n", tz->type);
  316. if (shutdown)
  317. hw_protection_shutdown(msg, poweroff_delay_ms);
  318. else
  319. hw_protection_reboot(msg, poweroff_delay_ms);
  320. }
  321. void thermal_zone_device_critical(struct thermal_zone_device *tz)
  322. {
  323. thermal_zone_device_halt(tz, true);
  324. }
  325. EXPORT_SYMBOL(thermal_zone_device_critical);
  326. void thermal_zone_device_critical_reboot(struct thermal_zone_device *tz)
  327. {
  328. thermal_zone_device_halt(tz, false);
  329. }
  330. static void handle_critical_trips(struct thermal_zone_device *tz,
  331. const struct thermal_trip *trip)
  332. {
  333. trace_thermal_zone_trip(tz, thermal_zone_trip_id(tz, trip), trip->type);
  334. if (trip->type == THERMAL_TRIP_CRITICAL)
  335. tz->ops.critical(tz);
  336. else if (tz->ops.hot)
  337. tz->ops.hot(tz);
  338. }
  339. static void handle_thermal_trip(struct thermal_zone_device *tz,
  340. struct thermal_trip_desc *td,
  341. struct list_head *way_up_list,
  342. struct list_head *way_down_list)
  343. {
  344. const struct thermal_trip *trip = &td->trip;
  345. int old_threshold;
  346. if (trip->temperature == THERMAL_TEMP_INVALID)
  347. return;
  348. /*
  349. * If the trip temperature or hysteresis has been updated recently,
  350. * the threshold needs to be computed again using the new values.
  351. * However, its initial value still reflects the old ones and that
  352. * is what needs to be compared with the previous zone temperature
  353. * to decide which action to take.
  354. */
  355. old_threshold = td->threshold;
  356. td->threshold = trip->temperature;
  357. if (tz->last_temperature >= old_threshold &&
  358. tz->last_temperature != THERMAL_TEMP_INIT) {
  359. /*
  360. * Mitigation is under way, so it needs to stop if the zone
  361. * temperature falls below the low temperature of the trip.
  362. * In that case, the trip temperature becomes the new threshold.
  363. */
  364. if (tz->temperature < trip->temperature - trip->hysteresis) {
  365. list_add(&td->notify_list_node, way_down_list);
  366. td->notify_temp = trip->temperature - trip->hysteresis;
  367. if (trip->type == THERMAL_TRIP_PASSIVE) {
  368. tz->passive--;
  369. WARN_ON(tz->passive < 0);
  370. }
  371. } else {
  372. td->threshold -= trip->hysteresis;
  373. }
  374. } else if (tz->temperature >= trip->temperature) {
  375. /*
  376. * There is no mitigation under way, so it needs to be started
  377. * if the zone temperature exceeds the trip one. The new
  378. * threshold is then set to the low temperature of the trip.
  379. */
  380. list_add_tail(&td->notify_list_node, way_up_list);
  381. td->notify_temp = trip->temperature;
  382. td->threshold -= trip->hysteresis;
  383. if (trip->type == THERMAL_TRIP_PASSIVE)
  384. tz->passive++;
  385. else if (trip->type == THERMAL_TRIP_CRITICAL ||
  386. trip->type == THERMAL_TRIP_HOT)
  387. handle_critical_trips(tz, trip);
  388. }
  389. }
  390. static void thermal_zone_device_check(struct work_struct *work)
  391. {
  392. struct thermal_zone_device *tz = container_of(work, struct
  393. thermal_zone_device,
  394. poll_queue.work);
  395. thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
  396. }
  397. static void thermal_zone_device_init(struct thermal_zone_device *tz)
  398. {
  399. struct thermal_trip_desc *td;
  400. INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check);
  401. tz->temperature = THERMAL_TEMP_INIT;
  402. tz->passive = 0;
  403. tz->prev_low_trip = -INT_MAX;
  404. tz->prev_high_trip = INT_MAX;
  405. for_each_trip_desc(tz, td) {
  406. struct thermal_instance *instance;
  407. list_for_each_entry(instance, &td->thermal_instances, trip_node)
  408. instance->initialized = false;
  409. }
  410. }
  411. static void thermal_governor_trip_crossed(struct thermal_governor *governor,
  412. struct thermal_zone_device *tz,
  413. const struct thermal_trip *trip,
  414. bool crossed_up)
  415. {
  416. if (trip->type == THERMAL_TRIP_HOT || trip->type == THERMAL_TRIP_CRITICAL)
  417. return;
  418. if (governor->trip_crossed)
  419. governor->trip_crossed(tz, trip, crossed_up);
  420. }
  421. static void thermal_trip_crossed(struct thermal_zone_device *tz,
  422. const struct thermal_trip *trip,
  423. struct thermal_governor *governor,
  424. bool crossed_up)
  425. {
  426. if (crossed_up) {
  427. thermal_notify_tz_trip_up(tz, trip);
  428. thermal_debug_tz_trip_up(tz, trip);
  429. } else {
  430. thermal_notify_tz_trip_down(tz, trip);
  431. thermal_debug_tz_trip_down(tz, trip);
  432. }
  433. thermal_governor_trip_crossed(governor, tz, trip, crossed_up);
  434. }
  435. static int thermal_trip_notify_cmp(void *not_used, const struct list_head *a,
  436. const struct list_head *b)
  437. {
  438. struct thermal_trip_desc *tda = container_of(a, struct thermal_trip_desc,
  439. notify_list_node);
  440. struct thermal_trip_desc *tdb = container_of(b, struct thermal_trip_desc,
  441. notify_list_node);
  442. return tda->notify_temp - tdb->notify_temp;
  443. }
  444. void __thermal_zone_device_update(struct thermal_zone_device *tz,
  445. enum thermal_notify_event event)
  446. {
  447. struct thermal_governor *governor = thermal_get_tz_governor(tz);
  448. struct thermal_trip_desc *td;
  449. LIST_HEAD(way_down_list);
  450. LIST_HEAD(way_up_list);
  451. int low = -INT_MAX, high = INT_MAX;
  452. int temp, ret;
  453. if (tz->state != TZ_STATE_READY || tz->mode != THERMAL_DEVICE_ENABLED)
  454. return;
  455. ret = __thermal_zone_get_temp(tz, &temp);
  456. if (ret) {
  457. thermal_zone_recheck(tz, ret);
  458. return;
  459. } else if (temp <= THERMAL_TEMP_INVALID) {
  460. /*
  461. * Special case: No valid temperature value is available, but
  462. * the zone owner does not want the core to do anything about
  463. * it. Continue regular zone polling if needed, so that this
  464. * function can be called again, but skip everything else.
  465. */
  466. goto monitor;
  467. }
  468. tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY;
  469. tz->last_temperature = tz->temperature;
  470. tz->temperature = temp;
  471. trace_thermal_temperature(tz);
  472. thermal_genl_sampling_temp(tz->id, temp);
  473. tz->notify_event = event;
  474. for_each_trip_desc(tz, td) {
  475. handle_thermal_trip(tz, td, &way_up_list, &way_down_list);
  476. if (td->threshold <= tz->temperature && td->threshold > low)
  477. low = td->threshold;
  478. if (td->threshold >= tz->temperature && td->threshold < high)
  479. high = td->threshold;
  480. }
  481. thermal_zone_set_trips(tz, low, high);
  482. list_sort(NULL, &way_up_list, thermal_trip_notify_cmp);
  483. list_for_each_entry(td, &way_up_list, notify_list_node)
  484. thermal_trip_crossed(tz, &td->trip, governor, true);
  485. list_sort(NULL, &way_down_list, thermal_trip_notify_cmp);
  486. list_for_each_entry_reverse(td, &way_down_list, notify_list_node)
  487. thermal_trip_crossed(tz, &td->trip, governor, false);
  488. if (governor->manage)
  489. governor->manage(tz);
  490. thermal_debug_update_trip_stats(tz);
  491. monitor:
  492. monitor_thermal_zone(tz);
  493. }
  494. static int thermal_zone_device_set_mode(struct thermal_zone_device *tz,
  495. enum thermal_device_mode mode)
  496. {
  497. int ret;
  498. mutex_lock(&tz->lock);
  499. /* do nothing if mode isn't changing */
  500. if (mode == tz->mode) {
  501. mutex_unlock(&tz->lock);
  502. return 0;
  503. }
  504. ret = __thermal_zone_device_set_mode(tz, mode);
  505. if (ret) {
  506. mutex_unlock(&tz->lock);
  507. return ret;
  508. }
  509. __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
  510. mutex_unlock(&tz->lock);
  511. if (mode == THERMAL_DEVICE_ENABLED)
  512. thermal_notify_tz_enable(tz);
  513. else
  514. thermal_notify_tz_disable(tz);
  515. return 0;
  516. }
  517. int thermal_zone_device_enable(struct thermal_zone_device *tz)
  518. {
  519. return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_ENABLED);
  520. }
  521. EXPORT_SYMBOL_GPL(thermal_zone_device_enable);
  522. int thermal_zone_device_disable(struct thermal_zone_device *tz)
  523. {
  524. return thermal_zone_device_set_mode(tz, THERMAL_DEVICE_DISABLED);
  525. }
  526. EXPORT_SYMBOL_GPL(thermal_zone_device_disable);
  527. static bool thermal_zone_is_present(struct thermal_zone_device *tz)
  528. {
  529. return !list_empty(&tz->node);
  530. }
  531. void thermal_zone_device_update(struct thermal_zone_device *tz,
  532. enum thermal_notify_event event)
  533. {
  534. mutex_lock(&tz->lock);
  535. if (thermal_zone_is_present(tz))
  536. __thermal_zone_device_update(tz, event);
  537. mutex_unlock(&tz->lock);
  538. }
  539. EXPORT_SYMBOL_GPL(thermal_zone_device_update);
  540. void thermal_zone_trip_down(struct thermal_zone_device *tz,
  541. const struct thermal_trip *trip)
  542. {
  543. thermal_trip_crossed(tz, trip, thermal_get_tz_governor(tz), false);
  544. }
  545. int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
  546. void *data)
  547. {
  548. struct thermal_governor *gov;
  549. int ret = 0;
  550. mutex_lock(&thermal_governor_lock);
  551. list_for_each_entry(gov, &thermal_governor_list, governor_list) {
  552. ret = cb(gov, data);
  553. if (ret)
  554. break;
  555. }
  556. mutex_unlock(&thermal_governor_lock);
  557. return ret;
  558. }
  559. int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
  560. void *), void *data)
  561. {
  562. struct thermal_cooling_device *cdev;
  563. int ret = 0;
  564. mutex_lock(&thermal_list_lock);
  565. list_for_each_entry(cdev, &thermal_cdev_list, node) {
  566. ret = cb(cdev, data);
  567. if (ret)
  568. break;
  569. }
  570. mutex_unlock(&thermal_list_lock);
  571. return ret;
  572. }
  573. int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
  574. void *data)
  575. {
  576. struct thermal_zone_device *tz;
  577. int ret = 0;
  578. mutex_lock(&thermal_list_lock);
  579. list_for_each_entry(tz, &thermal_tz_list, node) {
  580. ret = cb(tz, data);
  581. if (ret)
  582. break;
  583. }
  584. mutex_unlock(&thermal_list_lock);
  585. return ret;
  586. }
  587. struct thermal_zone_device *thermal_zone_get_by_id(int id)
  588. {
  589. struct thermal_zone_device *tz, *match = NULL;
  590. mutex_lock(&thermal_list_lock);
  591. list_for_each_entry(tz, &thermal_tz_list, node) {
  592. if (tz->id == id) {
  593. get_device(&tz->device);
  594. match = tz;
  595. break;
  596. }
  597. }
  598. mutex_unlock(&thermal_list_lock);
  599. return match;
  600. }
  601. /*
  602. * Device management section: cooling devices, zones devices, and binding
  603. *
  604. * Set of functions provided by the thermal core for:
  605. * - cooling devices lifecycle: registration, unregistration,
  606. * binding, and unbinding.
  607. * - thermal zone devices lifecycle: registration, unregistration,
  608. * binding, and unbinding.
  609. */
  610. /**
  611. * thermal_bind_cdev_to_trip - bind a cooling device to a thermal zone
  612. * @tz: pointer to struct thermal_zone_device
  613. * @trip: trip point the cooling devices is associated with in this zone.
  614. * @cdev: pointer to struct thermal_cooling_device
  615. * @cool_spec: cooling specification for @trip and @cdev
  616. *
  617. * This interface function bind a thermal cooling device to the certain trip
  618. * point of a thermal zone device.
  619. * This function is usually called in the thermal zone device .bind callback.
  620. *
  621. * Return: 0 on success, the proper error value otherwise.
  622. */
  623. static int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz,
  624. struct thermal_trip *trip,
  625. struct thermal_cooling_device *cdev,
  626. struct cooling_spec *cool_spec)
  627. {
  628. struct thermal_trip_desc *td = trip_to_trip_desc(trip);
  629. struct thermal_instance *dev, *instance;
  630. bool upper_no_limit;
  631. int result;
  632. /* lower default 0, upper default max_state */
  633. if (cool_spec->lower == THERMAL_NO_LIMIT)
  634. cool_spec->lower = 0;
  635. if (cool_spec->upper == THERMAL_NO_LIMIT) {
  636. cool_spec->upper = cdev->max_state;
  637. upper_no_limit = true;
  638. } else {
  639. upper_no_limit = false;
  640. }
  641. if (cool_spec->lower > cool_spec->upper || cool_spec->upper > cdev->max_state)
  642. return -EINVAL;
  643. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  644. if (!dev)
  645. return -ENOMEM;
  646. dev->cdev = cdev;
  647. dev->trip = trip;
  648. dev->upper = cool_spec->upper;
  649. dev->upper_no_limit = upper_no_limit;
  650. dev->lower = cool_spec->lower;
  651. dev->target = THERMAL_NO_TARGET;
  652. dev->weight = cool_spec->weight;
  653. result = ida_alloc(&tz->ida, GFP_KERNEL);
  654. if (result < 0)
  655. goto free_mem;
  656. dev->id = result;
  657. sprintf(dev->name, "cdev%d", dev->id);
  658. result =
  659. sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
  660. if (result)
  661. goto release_ida;
  662. snprintf(dev->attr_name, sizeof(dev->attr_name), "cdev%d_trip_point",
  663. dev->id);
  664. sysfs_attr_init(&dev->attr.attr);
  665. dev->attr.attr.name = dev->attr_name;
  666. dev->attr.attr.mode = 0444;
  667. dev->attr.show = trip_point_show;
  668. result = device_create_file(&tz->device, &dev->attr);
  669. if (result)
  670. goto remove_symbol_link;
  671. snprintf(dev->weight_attr_name, sizeof(dev->weight_attr_name),
  672. "cdev%d_weight", dev->id);
  673. sysfs_attr_init(&dev->weight_attr.attr);
  674. dev->weight_attr.attr.name = dev->weight_attr_name;
  675. dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
  676. dev->weight_attr.show = weight_show;
  677. dev->weight_attr.store = weight_store;
  678. result = device_create_file(&tz->device, &dev->weight_attr);
  679. if (result)
  680. goto remove_trip_file;
  681. mutex_lock(&cdev->lock);
  682. list_for_each_entry(instance, &td->thermal_instances, trip_node)
  683. if (instance->cdev == cdev) {
  684. result = -EEXIST;
  685. break;
  686. }
  687. if (!result) {
  688. list_add_tail(&dev->trip_node, &td->thermal_instances);
  689. list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
  690. atomic_set(&tz->need_update, 1);
  691. thermal_governor_update_tz(tz, THERMAL_TZ_BIND_CDEV);
  692. }
  693. mutex_unlock(&cdev->lock);
  694. if (!result)
  695. return 0;
  696. device_remove_file(&tz->device, &dev->weight_attr);
  697. remove_trip_file:
  698. device_remove_file(&tz->device, &dev->attr);
  699. remove_symbol_link:
  700. sysfs_remove_link(&tz->device.kobj, dev->name);
  701. release_ida:
  702. ida_free(&tz->ida, dev->id);
  703. free_mem:
  704. kfree(dev);
  705. return result;
  706. }
  707. /**
  708. * thermal_unbind_cdev_from_trip - unbind a cooling device from a thermal zone.
  709. * @tz: pointer to a struct thermal_zone_device.
  710. * @trip: trip point the cooling devices is associated with in this zone.
  711. * @cdev: pointer to a struct thermal_cooling_device.
  712. *
  713. * This interface function unbind a thermal cooling device from the certain
  714. * trip point of a thermal zone device.
  715. * This function is usually called in the thermal zone device .unbind callback.
  716. */
  717. static void thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz,
  718. struct thermal_trip *trip,
  719. struct thermal_cooling_device *cdev)
  720. {
  721. struct thermal_trip_desc *td = trip_to_trip_desc(trip);
  722. struct thermal_instance *pos, *next;
  723. mutex_lock(&cdev->lock);
  724. list_for_each_entry_safe(pos, next, &td->thermal_instances, trip_node) {
  725. if (pos->cdev == cdev) {
  726. list_del(&pos->trip_node);
  727. list_del(&pos->cdev_node);
  728. thermal_governor_update_tz(tz, THERMAL_TZ_UNBIND_CDEV);
  729. mutex_unlock(&cdev->lock);
  730. goto unbind;
  731. }
  732. }
  733. mutex_unlock(&cdev->lock);
  734. return;
  735. unbind:
  736. device_remove_file(&tz->device, &pos->weight_attr);
  737. device_remove_file(&tz->device, &pos->attr);
  738. sysfs_remove_link(&tz->device.kobj, pos->name);
  739. ida_free(&tz->ida, pos->id);
  740. kfree(pos);
  741. }
  742. static void thermal_release(struct device *dev)
  743. {
  744. struct thermal_zone_device *tz;
  745. struct thermal_cooling_device *cdev;
  746. if (!strncmp(dev_name(dev), "thermal_zone",
  747. sizeof("thermal_zone") - 1)) {
  748. tz = to_thermal_zone(dev);
  749. thermal_zone_destroy_device_groups(tz);
  750. mutex_destroy(&tz->lock);
  751. complete(&tz->removal);
  752. } else if (!strncmp(dev_name(dev), "cooling_device",
  753. sizeof("cooling_device") - 1)) {
  754. cdev = to_cooling_device(dev);
  755. thermal_cooling_device_destroy_sysfs(cdev);
  756. kfree_const(cdev->type);
  757. ida_free(&thermal_cdev_ida, cdev->id);
  758. kfree(cdev);
  759. }
  760. }
  761. static struct class *thermal_class;
  762. static inline
  763. void print_bind_err_msg(struct thermal_zone_device *tz,
  764. const struct thermal_trip *trip,
  765. struct thermal_cooling_device *cdev, int ret)
  766. {
  767. dev_err(&tz->device, "binding cdev %s to trip %d failed: %d\n",
  768. cdev->type, thermal_zone_trip_id(tz, trip), ret);
  769. }
  770. static void thermal_zone_cdev_bind(struct thermal_zone_device *tz,
  771. struct thermal_cooling_device *cdev)
  772. {
  773. struct thermal_trip_desc *td;
  774. if (!tz->ops.should_bind)
  775. return;
  776. mutex_lock(&tz->lock);
  777. for_each_trip_desc(tz, td) {
  778. struct thermal_trip *trip = &td->trip;
  779. struct cooling_spec c = {
  780. .upper = THERMAL_NO_LIMIT,
  781. .lower = THERMAL_NO_LIMIT,
  782. .weight = THERMAL_WEIGHT_DEFAULT
  783. };
  784. int ret;
  785. if (!tz->ops.should_bind(tz, trip, cdev, &c))
  786. continue;
  787. ret = thermal_bind_cdev_to_trip(tz, trip, cdev, &c);
  788. if (ret)
  789. print_bind_err_msg(tz, trip, cdev, ret);
  790. }
  791. mutex_unlock(&tz->lock);
  792. }
  793. /**
  794. * __thermal_cooling_device_register() - register a new thermal cooling device
  795. * @np: a pointer to a device tree node.
  796. * @type: the thermal cooling device type.
  797. * @devdata: device private data.
  798. * @ops: standard thermal cooling devices callbacks.
  799. *
  800. * This interface function adds a new thermal cooling device (fan/processor/...)
  801. * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
  802. * to all the thermal zone devices registered at the same time.
  803. * It also gives the opportunity to link the cooling device to a device tree
  804. * node, so that it can be bound to a thermal zone created out of device tree.
  805. *
  806. * Return: a pointer to the created struct thermal_cooling_device or an
  807. * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
  808. */
  809. static struct thermal_cooling_device *
  810. __thermal_cooling_device_register(struct device_node *np,
  811. const char *type, void *devdata,
  812. const struct thermal_cooling_device_ops *ops)
  813. {
  814. struct thermal_cooling_device *cdev;
  815. struct thermal_zone_device *pos = NULL;
  816. unsigned long current_state;
  817. int id, ret;
  818. if (!ops || !ops->get_max_state || !ops->get_cur_state ||
  819. !ops->set_cur_state)
  820. return ERR_PTR(-EINVAL);
  821. if (!thermal_class)
  822. return ERR_PTR(-ENODEV);
  823. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  824. if (!cdev)
  825. return ERR_PTR(-ENOMEM);
  826. ret = ida_alloc(&thermal_cdev_ida, GFP_KERNEL);
  827. if (ret < 0)
  828. goto out_kfree_cdev;
  829. cdev->id = ret;
  830. id = ret;
  831. cdev->type = kstrdup_const(type ? type : "", GFP_KERNEL);
  832. if (!cdev->type) {
  833. ret = -ENOMEM;
  834. goto out_ida_remove;
  835. }
  836. mutex_init(&cdev->lock);
  837. INIT_LIST_HEAD(&cdev->thermal_instances);
  838. cdev->np = np;
  839. cdev->ops = ops;
  840. cdev->updated = false;
  841. cdev->device.class = thermal_class;
  842. cdev->devdata = devdata;
  843. ret = cdev->ops->get_max_state(cdev, &cdev->max_state);
  844. if (ret)
  845. goto out_cdev_type;
  846. /*
  847. * The cooling device's current state is only needed for debug
  848. * initialization below, so a failure to get it does not cause
  849. * the entire cooling device initialization to fail. However,
  850. * the debug will not work for the device if its initial state
  851. * cannot be determined and drivers are responsible for ensuring
  852. * that this will not happen.
  853. */
  854. ret = cdev->ops->get_cur_state(cdev, &current_state);
  855. if (ret)
  856. current_state = ULONG_MAX;
  857. thermal_cooling_device_setup_sysfs(cdev);
  858. ret = dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
  859. if (ret)
  860. goto out_cooling_dev;
  861. ret = device_register(&cdev->device);
  862. if (ret) {
  863. /* thermal_release() handles rest of the cleanup */
  864. put_device(&cdev->device);
  865. return ERR_PTR(ret);
  866. }
  867. if (current_state <= cdev->max_state)
  868. thermal_debug_cdev_add(cdev, current_state);
  869. /* Add 'this' new cdev to the global cdev list */
  870. mutex_lock(&thermal_list_lock);
  871. list_add(&cdev->node, &thermal_cdev_list);
  872. /* Update binding information for 'this' new cdev */
  873. list_for_each_entry(pos, &thermal_tz_list, node)
  874. thermal_zone_cdev_bind(pos, cdev);
  875. list_for_each_entry(pos, &thermal_tz_list, node)
  876. if (atomic_cmpxchg(&pos->need_update, 1, 0))
  877. thermal_zone_device_update(pos,
  878. THERMAL_EVENT_UNSPECIFIED);
  879. mutex_unlock(&thermal_list_lock);
  880. return cdev;
  881. out_cooling_dev:
  882. thermal_cooling_device_destroy_sysfs(cdev);
  883. out_cdev_type:
  884. kfree_const(cdev->type);
  885. out_ida_remove:
  886. ida_free(&thermal_cdev_ida, id);
  887. out_kfree_cdev:
  888. kfree(cdev);
  889. return ERR_PTR(ret);
  890. }
  891. /**
  892. * thermal_cooling_device_register() - register a new thermal cooling device
  893. * @type: the thermal cooling device type.
  894. * @devdata: device private data.
  895. * @ops: standard thermal cooling devices callbacks.
  896. *
  897. * This interface function adds a new thermal cooling device (fan/processor/...)
  898. * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
  899. * to all the thermal zone devices registered at the same time.
  900. *
  901. * Return: a pointer to the created struct thermal_cooling_device or an
  902. * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
  903. */
  904. struct thermal_cooling_device *
  905. thermal_cooling_device_register(const char *type, void *devdata,
  906. const struct thermal_cooling_device_ops *ops)
  907. {
  908. return __thermal_cooling_device_register(NULL, type, devdata, ops);
  909. }
  910. EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
  911. /**
  912. * thermal_of_cooling_device_register() - register an OF thermal cooling device
  913. * @np: a pointer to a device tree node.
  914. * @type: the thermal cooling device type.
  915. * @devdata: device private data.
  916. * @ops: standard thermal cooling devices callbacks.
  917. *
  918. * This function will register a cooling device with device tree node reference.
  919. * This interface function adds a new thermal cooling device (fan/processor/...)
  920. * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
  921. * to all the thermal zone devices registered at the same time.
  922. *
  923. * Return: a pointer to the created struct thermal_cooling_device or an
  924. * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
  925. */
  926. struct thermal_cooling_device *
  927. thermal_of_cooling_device_register(struct device_node *np,
  928. const char *type, void *devdata,
  929. const struct thermal_cooling_device_ops *ops)
  930. {
  931. return __thermal_cooling_device_register(np, type, devdata, ops);
  932. }
  933. EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
  934. static void thermal_cooling_device_release(struct device *dev, void *res)
  935. {
  936. thermal_cooling_device_unregister(
  937. *(struct thermal_cooling_device **)res);
  938. }
  939. /**
  940. * devm_thermal_of_cooling_device_register() - register an OF thermal cooling
  941. * device
  942. * @dev: a valid struct device pointer of a sensor device.
  943. * @np: a pointer to a device tree node.
  944. * @type: the thermal cooling device type.
  945. * @devdata: device private data.
  946. * @ops: standard thermal cooling devices callbacks.
  947. *
  948. * This function will register a cooling device with device tree node reference.
  949. * This interface function adds a new thermal cooling device (fan/processor/...)
  950. * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
  951. * to all the thermal zone devices registered at the same time.
  952. *
  953. * Return: a pointer to the created struct thermal_cooling_device or an
  954. * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
  955. */
  956. struct thermal_cooling_device *
  957. devm_thermal_of_cooling_device_register(struct device *dev,
  958. struct device_node *np,
  959. const char *type, void *devdata,
  960. const struct thermal_cooling_device_ops *ops)
  961. {
  962. struct thermal_cooling_device **ptr, *tcd;
  963. ptr = devres_alloc(thermal_cooling_device_release, sizeof(*ptr),
  964. GFP_KERNEL);
  965. if (!ptr)
  966. return ERR_PTR(-ENOMEM);
  967. tcd = __thermal_cooling_device_register(np, type, devdata, ops);
  968. if (IS_ERR(tcd)) {
  969. devres_free(ptr);
  970. return tcd;
  971. }
  972. *ptr = tcd;
  973. devres_add(dev, ptr);
  974. return tcd;
  975. }
  976. EXPORT_SYMBOL_GPL(devm_thermal_of_cooling_device_register);
  977. static bool thermal_cooling_device_present(struct thermal_cooling_device *cdev)
  978. {
  979. struct thermal_cooling_device *pos = NULL;
  980. list_for_each_entry(pos, &thermal_cdev_list, node) {
  981. if (pos == cdev)
  982. return true;
  983. }
  984. return false;
  985. }
  986. /**
  987. * thermal_cooling_device_update - Update a cooling device object
  988. * @cdev: Target cooling device.
  989. *
  990. * Update @cdev to reflect a change of the underlying hardware or platform.
  991. *
  992. * Must be called when the maximum cooling state of @cdev becomes invalid and so
  993. * its .get_max_state() callback needs to be run to produce the new maximum
  994. * cooling state value.
  995. */
  996. void thermal_cooling_device_update(struct thermal_cooling_device *cdev)
  997. {
  998. struct thermal_instance *ti;
  999. unsigned long state;
  1000. if (IS_ERR_OR_NULL(cdev))
  1001. return;
  1002. /*
  1003. * Hold thermal_list_lock throughout the update to prevent the device
  1004. * from going away while being updated.
  1005. */
  1006. mutex_lock(&thermal_list_lock);
  1007. if (!thermal_cooling_device_present(cdev))
  1008. goto unlock_list;
  1009. /*
  1010. * Update under the cdev lock to prevent the state from being set beyond
  1011. * the new limit concurrently.
  1012. */
  1013. mutex_lock(&cdev->lock);
  1014. if (cdev->ops->get_max_state(cdev, &cdev->max_state))
  1015. goto unlock;
  1016. thermal_cooling_device_stats_reinit(cdev);
  1017. list_for_each_entry(ti, &cdev->thermal_instances, cdev_node) {
  1018. if (ti->upper == cdev->max_state)
  1019. continue;
  1020. if (ti->upper < cdev->max_state) {
  1021. if (ti->upper_no_limit)
  1022. ti->upper = cdev->max_state;
  1023. continue;
  1024. }
  1025. ti->upper = cdev->max_state;
  1026. if (ti->lower > ti->upper)
  1027. ti->lower = ti->upper;
  1028. if (ti->target == THERMAL_NO_TARGET)
  1029. continue;
  1030. if (ti->target > ti->upper)
  1031. ti->target = ti->upper;
  1032. }
  1033. if (cdev->ops->get_cur_state(cdev, &state) || state > cdev->max_state)
  1034. goto unlock;
  1035. thermal_cooling_device_stats_update(cdev, state);
  1036. unlock:
  1037. mutex_unlock(&cdev->lock);
  1038. unlock_list:
  1039. mutex_unlock(&thermal_list_lock);
  1040. }
  1041. EXPORT_SYMBOL_GPL(thermal_cooling_device_update);
  1042. static void thermal_zone_cdev_unbind(struct thermal_zone_device *tz,
  1043. struct thermal_cooling_device *cdev)
  1044. {
  1045. struct thermal_trip_desc *td;
  1046. mutex_lock(&tz->lock);
  1047. for_each_trip_desc(tz, td)
  1048. thermal_unbind_cdev_from_trip(tz, &td->trip, cdev);
  1049. mutex_unlock(&tz->lock);
  1050. }
  1051. /**
  1052. * thermal_cooling_device_unregister - removes a thermal cooling device
  1053. * @cdev: the thermal cooling device to remove.
  1054. *
  1055. * thermal_cooling_device_unregister() must be called when a registered
  1056. * thermal cooling device is no longer needed.
  1057. */
  1058. void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
  1059. {
  1060. struct thermal_zone_device *tz;
  1061. if (!cdev)
  1062. return;
  1063. thermal_debug_cdev_remove(cdev);
  1064. mutex_lock(&thermal_list_lock);
  1065. if (!thermal_cooling_device_present(cdev)) {
  1066. mutex_unlock(&thermal_list_lock);
  1067. return;
  1068. }
  1069. list_del(&cdev->node);
  1070. /* Unbind all thermal zones associated with 'this' cdev */
  1071. list_for_each_entry(tz, &thermal_tz_list, node)
  1072. thermal_zone_cdev_unbind(tz, cdev);
  1073. mutex_unlock(&thermal_list_lock);
  1074. device_unregister(&cdev->device);
  1075. }
  1076. EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
  1077. int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp)
  1078. {
  1079. const struct thermal_trip_desc *td;
  1080. int ret = -EINVAL;
  1081. if (tz->ops.get_crit_temp)
  1082. return tz->ops.get_crit_temp(tz, temp);
  1083. mutex_lock(&tz->lock);
  1084. for_each_trip_desc(tz, td) {
  1085. const struct thermal_trip *trip = &td->trip;
  1086. if (trip->type == THERMAL_TRIP_CRITICAL) {
  1087. *temp = trip->temperature;
  1088. ret = 0;
  1089. break;
  1090. }
  1091. }
  1092. mutex_unlock(&tz->lock);
  1093. return ret;
  1094. }
  1095. EXPORT_SYMBOL_GPL(thermal_zone_get_crit_temp);
  1096. static void thermal_zone_init_complete(struct thermal_zone_device *tz)
  1097. {
  1098. mutex_lock(&tz->lock);
  1099. tz->state &= ~TZ_STATE_FLAG_INIT;
  1100. /*
  1101. * If system suspend or resume is in progress at this point, the
  1102. * new thermal zone needs to be marked as suspended because
  1103. * thermal_pm_notify() has run already.
  1104. */
  1105. if (thermal_pm_suspended)
  1106. tz->state |= TZ_STATE_FLAG_SUSPENDED;
  1107. __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
  1108. mutex_unlock(&tz->lock);
  1109. }
  1110. /**
  1111. * thermal_zone_device_register_with_trips() - register a new thermal zone device
  1112. * @type: the thermal zone device type
  1113. * @trips: a pointer to an array of thermal trips
  1114. * @num_trips: the number of trip points the thermal zone support
  1115. * @devdata: private device data
  1116. * @ops: standard thermal zone device callbacks
  1117. * @tzp: thermal zone platform parameters
  1118. * @passive_delay: number of milliseconds to wait between polls when
  1119. * performing passive cooling
  1120. * @polling_delay: number of milliseconds to wait between polls when checking
  1121. * whether trip points have been crossed (0 for interrupt
  1122. * driven systems)
  1123. *
  1124. * This interface function adds a new thermal zone device (sensor) to
  1125. * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
  1126. * thermal cooling devices registered at the same time.
  1127. * thermal_zone_device_unregister() must be called when the device is no
  1128. * longer needed. The passive cooling depends on the .get_trend() return value.
  1129. *
  1130. * Return: a pointer to the created struct thermal_zone_device or an
  1131. * in case of error, an ERR_PTR. Caller must check return value with
  1132. * IS_ERR*() helpers.
  1133. */
  1134. struct thermal_zone_device *
  1135. thermal_zone_device_register_with_trips(const char *type,
  1136. const struct thermal_trip *trips,
  1137. int num_trips, void *devdata,
  1138. const struct thermal_zone_device_ops *ops,
  1139. const struct thermal_zone_params *tzp,
  1140. unsigned int passive_delay,
  1141. unsigned int polling_delay)
  1142. {
  1143. const struct thermal_trip *trip = trips;
  1144. struct thermal_cooling_device *cdev;
  1145. struct thermal_zone_device *tz;
  1146. struct thermal_trip_desc *td;
  1147. int id;
  1148. int result;
  1149. struct thermal_governor *governor;
  1150. if (!type || strlen(type) == 0) {
  1151. pr_err("No thermal zone type defined\n");
  1152. return ERR_PTR(-EINVAL);
  1153. }
  1154. if (strlen(type) >= THERMAL_NAME_LENGTH) {
  1155. pr_err("Thermal zone name (%s) too long, should be under %d chars\n",
  1156. type, THERMAL_NAME_LENGTH);
  1157. return ERR_PTR(-EINVAL);
  1158. }
  1159. if (num_trips < 0) {
  1160. pr_err("Incorrect number of thermal trips\n");
  1161. return ERR_PTR(-EINVAL);
  1162. }
  1163. if (!ops || !ops->get_temp) {
  1164. pr_err("Thermal zone device ops not defined or invalid\n");
  1165. return ERR_PTR(-EINVAL);
  1166. }
  1167. if (num_trips > 0 && !trips)
  1168. return ERR_PTR(-EINVAL);
  1169. if (polling_delay && passive_delay > polling_delay)
  1170. return ERR_PTR(-EINVAL);
  1171. if (!thermal_class)
  1172. return ERR_PTR(-ENODEV);
  1173. tz = kzalloc(struct_size(tz, trips, num_trips), GFP_KERNEL);
  1174. if (!tz)
  1175. return ERR_PTR(-ENOMEM);
  1176. if (tzp) {
  1177. tz->tzp = kmemdup(tzp, sizeof(*tzp), GFP_KERNEL);
  1178. if (!tz->tzp) {
  1179. result = -ENOMEM;
  1180. goto free_tz;
  1181. }
  1182. }
  1183. INIT_LIST_HEAD(&tz->node);
  1184. ida_init(&tz->ida);
  1185. mutex_init(&tz->lock);
  1186. init_completion(&tz->removal);
  1187. init_completion(&tz->resume);
  1188. id = ida_alloc(&thermal_tz_ida, GFP_KERNEL);
  1189. if (id < 0) {
  1190. result = id;
  1191. goto free_tzp;
  1192. }
  1193. tz->id = id;
  1194. strscpy(tz->type, type, sizeof(tz->type));
  1195. tz->ops = *ops;
  1196. if (!tz->ops.critical)
  1197. tz->ops.critical = thermal_zone_device_critical;
  1198. tz->device.class = thermal_class;
  1199. tz->devdata = devdata;
  1200. tz->num_trips = num_trips;
  1201. for_each_trip_desc(tz, td) {
  1202. td->trip = *trip++;
  1203. INIT_LIST_HEAD(&td->thermal_instances);
  1204. /*
  1205. * Mark all thresholds as invalid to start with even though
  1206. * this only matters for the trips that start as invalid and
  1207. * become valid later.
  1208. */
  1209. td->threshold = INT_MAX;
  1210. }
  1211. tz->polling_delay_jiffies = msecs_to_jiffies(polling_delay);
  1212. tz->passive_delay_jiffies = msecs_to_jiffies(passive_delay);
  1213. tz->recheck_delay_jiffies = THERMAL_RECHECK_DELAY;
  1214. tz->state = TZ_STATE_FLAG_INIT;
  1215. /* sys I/F */
  1216. /* Add nodes that are always present via .groups */
  1217. result = thermal_zone_create_device_groups(tz);
  1218. if (result)
  1219. goto remove_id;
  1220. /* A new thermal zone needs to be updated anyway. */
  1221. atomic_set(&tz->need_update, 1);
  1222. result = dev_set_name(&tz->device, "thermal_zone%d", tz->id);
  1223. if (result) {
  1224. thermal_zone_destroy_device_groups(tz);
  1225. goto remove_id;
  1226. }
  1227. thermal_zone_device_init(tz);
  1228. result = device_register(&tz->device);
  1229. if (result)
  1230. goto release_device;
  1231. /* Update 'this' zone's governor information */
  1232. mutex_lock(&thermal_governor_lock);
  1233. if (tz->tzp)
  1234. governor = __find_governor(tz->tzp->governor_name);
  1235. else
  1236. governor = def_governor;
  1237. result = thermal_set_governor(tz, governor);
  1238. if (result) {
  1239. mutex_unlock(&thermal_governor_lock);
  1240. goto unregister;
  1241. }
  1242. mutex_unlock(&thermal_governor_lock);
  1243. if (!tz->tzp || !tz->tzp->no_hwmon) {
  1244. result = thermal_add_hwmon_sysfs(tz);
  1245. if (result)
  1246. goto unregister;
  1247. }
  1248. mutex_lock(&thermal_list_lock);
  1249. mutex_lock(&tz->lock);
  1250. list_add_tail(&tz->node, &thermal_tz_list);
  1251. mutex_unlock(&tz->lock);
  1252. /* Bind cooling devices for this zone */
  1253. list_for_each_entry(cdev, &thermal_cdev_list, node)
  1254. thermal_zone_cdev_bind(tz, cdev);
  1255. thermal_zone_init_complete(tz);
  1256. mutex_unlock(&thermal_list_lock);
  1257. thermal_notify_tz_create(tz);
  1258. thermal_debug_tz_add(tz);
  1259. return tz;
  1260. unregister:
  1261. device_del(&tz->device);
  1262. release_device:
  1263. put_device(&tz->device);
  1264. remove_id:
  1265. ida_free(&thermal_tz_ida, id);
  1266. free_tzp:
  1267. kfree(tz->tzp);
  1268. free_tz:
  1269. kfree(tz);
  1270. return ERR_PTR(result);
  1271. }
  1272. EXPORT_SYMBOL_GPL(thermal_zone_device_register_with_trips);
  1273. struct thermal_zone_device *thermal_tripless_zone_device_register(
  1274. const char *type,
  1275. void *devdata,
  1276. const struct thermal_zone_device_ops *ops,
  1277. const struct thermal_zone_params *tzp)
  1278. {
  1279. return thermal_zone_device_register_with_trips(type, NULL, 0, devdata,
  1280. ops, tzp, 0, 0);
  1281. }
  1282. EXPORT_SYMBOL_GPL(thermal_tripless_zone_device_register);
  1283. void *thermal_zone_device_priv(struct thermal_zone_device *tzd)
  1284. {
  1285. return tzd->devdata;
  1286. }
  1287. EXPORT_SYMBOL_GPL(thermal_zone_device_priv);
  1288. const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
  1289. {
  1290. return tzd->type;
  1291. }
  1292. EXPORT_SYMBOL_GPL(thermal_zone_device_type);
  1293. int thermal_zone_device_id(struct thermal_zone_device *tzd)
  1294. {
  1295. return tzd->id;
  1296. }
  1297. EXPORT_SYMBOL_GPL(thermal_zone_device_id);
  1298. struct device *thermal_zone_device(struct thermal_zone_device *tzd)
  1299. {
  1300. return &tzd->device;
  1301. }
  1302. EXPORT_SYMBOL_GPL(thermal_zone_device);
  1303. /**
  1304. * thermal_zone_device_unregister - removes the registered thermal zone device
  1305. * @tz: the thermal zone device to remove
  1306. */
  1307. void thermal_zone_device_unregister(struct thermal_zone_device *tz)
  1308. {
  1309. struct thermal_cooling_device *cdev;
  1310. struct thermal_zone_device *pos = NULL;
  1311. if (!tz)
  1312. return;
  1313. thermal_debug_tz_remove(tz);
  1314. mutex_lock(&thermal_list_lock);
  1315. list_for_each_entry(pos, &thermal_tz_list, node)
  1316. if (pos == tz)
  1317. break;
  1318. if (pos != tz) {
  1319. /* thermal zone device not found */
  1320. mutex_unlock(&thermal_list_lock);
  1321. return;
  1322. }
  1323. mutex_lock(&tz->lock);
  1324. list_del(&tz->node);
  1325. mutex_unlock(&tz->lock);
  1326. /* Unbind all cdevs associated with 'this' thermal zone */
  1327. list_for_each_entry(cdev, &thermal_cdev_list, node)
  1328. thermal_zone_cdev_unbind(tz, cdev);
  1329. mutex_unlock(&thermal_list_lock);
  1330. cancel_delayed_work_sync(&tz->poll_queue);
  1331. thermal_set_governor(tz, NULL);
  1332. thermal_remove_hwmon_sysfs(tz);
  1333. ida_free(&thermal_tz_ida, tz->id);
  1334. ida_destroy(&tz->ida);
  1335. device_del(&tz->device);
  1336. put_device(&tz->device);
  1337. thermal_notify_tz_delete(tz);
  1338. wait_for_completion(&tz->removal);
  1339. kfree(tz->tzp);
  1340. kfree(tz);
  1341. }
  1342. EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
  1343. /**
  1344. * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
  1345. * @name: thermal zone name to fetch the temperature
  1346. *
  1347. * When only one zone is found with the passed name, returns a reference to it.
  1348. *
  1349. * Return: On success returns a reference to an unique thermal zone with
  1350. * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
  1351. * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
  1352. */
  1353. struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
  1354. {
  1355. struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
  1356. unsigned int found = 0;
  1357. if (!name)
  1358. goto exit;
  1359. mutex_lock(&thermal_list_lock);
  1360. list_for_each_entry(pos, &thermal_tz_list, node)
  1361. if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
  1362. found++;
  1363. ref = pos;
  1364. }
  1365. mutex_unlock(&thermal_list_lock);
  1366. /* nothing has been found, thus an error code for it */
  1367. if (found == 0)
  1368. ref = ERR_PTR(-ENODEV);
  1369. else if (found > 1)
  1370. /* Success only when an unique zone is found */
  1371. ref = ERR_PTR(-EEXIST);
  1372. exit:
  1373. return ref;
  1374. }
  1375. EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
  1376. static void thermal_zone_device_resume(struct work_struct *work)
  1377. {
  1378. struct thermal_zone_device *tz;
  1379. tz = container_of(work, struct thermal_zone_device, poll_queue.work);
  1380. mutex_lock(&tz->lock);
  1381. tz->state &= ~(TZ_STATE_FLAG_SUSPENDED | TZ_STATE_FLAG_RESUMING);
  1382. thermal_debug_tz_resume(tz);
  1383. thermal_zone_device_init(tz);
  1384. thermal_governor_update_tz(tz, THERMAL_TZ_RESUME);
  1385. __thermal_zone_device_update(tz, THERMAL_TZ_RESUME);
  1386. complete(&tz->resume);
  1387. mutex_unlock(&tz->lock);
  1388. }
  1389. static void thermal_zone_pm_prepare(struct thermal_zone_device *tz)
  1390. {
  1391. mutex_lock(&tz->lock);
  1392. if (tz->state & TZ_STATE_FLAG_RESUMING) {
  1393. /*
  1394. * thermal_zone_device_resume() queued up for this zone has not
  1395. * acquired the lock yet, so release it to let the function run
  1396. * and wait util it has done the work.
  1397. */
  1398. mutex_unlock(&tz->lock);
  1399. wait_for_completion(&tz->resume);
  1400. mutex_lock(&tz->lock);
  1401. }
  1402. tz->state |= TZ_STATE_FLAG_SUSPENDED;
  1403. mutex_unlock(&tz->lock);
  1404. }
  1405. static void thermal_zone_pm_complete(struct thermal_zone_device *tz)
  1406. {
  1407. mutex_lock(&tz->lock);
  1408. cancel_delayed_work(&tz->poll_queue);
  1409. reinit_completion(&tz->resume);
  1410. tz->state |= TZ_STATE_FLAG_RESUMING;
  1411. /*
  1412. * Replace the work function with the resume one, which will restore the
  1413. * original work function and schedule the polling work if needed.
  1414. */
  1415. INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_resume);
  1416. /* Queue up the work without a delay. */
  1417. mod_delayed_work(system_freezable_power_efficient_wq, &tz->poll_queue, 0);
  1418. mutex_unlock(&tz->lock);
  1419. }
  1420. static int thermal_pm_notify(struct notifier_block *nb,
  1421. unsigned long mode, void *_unused)
  1422. {
  1423. struct thermal_zone_device *tz;
  1424. switch (mode) {
  1425. case PM_HIBERNATION_PREPARE:
  1426. case PM_RESTORE_PREPARE:
  1427. case PM_SUSPEND_PREPARE:
  1428. mutex_lock(&thermal_list_lock);
  1429. thermal_pm_suspended = true;
  1430. list_for_each_entry(tz, &thermal_tz_list, node)
  1431. thermal_zone_pm_prepare(tz);
  1432. mutex_unlock(&thermal_list_lock);
  1433. break;
  1434. case PM_POST_HIBERNATION:
  1435. case PM_POST_RESTORE:
  1436. case PM_POST_SUSPEND:
  1437. mutex_lock(&thermal_list_lock);
  1438. thermal_pm_suspended = false;
  1439. list_for_each_entry(tz, &thermal_tz_list, node)
  1440. thermal_zone_pm_complete(tz);
  1441. mutex_unlock(&thermal_list_lock);
  1442. break;
  1443. default:
  1444. break;
  1445. }
  1446. return 0;
  1447. }
  1448. static struct notifier_block thermal_pm_nb = {
  1449. .notifier_call = thermal_pm_notify,
  1450. /*
  1451. * Run at the lowest priority to avoid interference between the thermal
  1452. * zone resume work items spawned by thermal_pm_notify() and the other
  1453. * PM notifiers.
  1454. */
  1455. .priority = INT_MIN,
  1456. };
  1457. static int __init thermal_init(void)
  1458. {
  1459. int result;
  1460. thermal_debug_init();
  1461. result = thermal_netlink_init();
  1462. if (result)
  1463. goto error;
  1464. result = thermal_register_governors();
  1465. if (result)
  1466. goto unregister_netlink;
  1467. thermal_class = kzalloc(sizeof(*thermal_class), GFP_KERNEL);
  1468. if (!thermal_class) {
  1469. result = -ENOMEM;
  1470. goto unregister_governors;
  1471. }
  1472. thermal_class->name = "thermal";
  1473. thermal_class->dev_release = thermal_release;
  1474. result = class_register(thermal_class);
  1475. if (result) {
  1476. kfree(thermal_class);
  1477. thermal_class = NULL;
  1478. goto unregister_governors;
  1479. }
  1480. result = register_pm_notifier(&thermal_pm_nb);
  1481. if (result)
  1482. pr_warn("Thermal: Can not register suspend notifier, return %d\n",
  1483. result);
  1484. return 0;
  1485. unregister_governors:
  1486. thermal_unregister_governors();
  1487. unregister_netlink:
  1488. thermal_netlink_exit();
  1489. error:
  1490. mutex_destroy(&thermal_list_lock);
  1491. mutex_destroy(&thermal_governor_lock);
  1492. return result;
  1493. }
  1494. postcore_initcall(thermal_init);