thermal_core.c 50 KB

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