thermal_pause.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "%s:%s " fmt, KBUILD_MODNAME, __func__
  6. #include <linux/module.h>
  7. #include <linux/thermal.h>
  8. #include <linux/err.h>
  9. #include <linux/slab.h>
  10. #include <linux/cpu.h>
  11. #include <linux/of_device.h>
  12. #include <linux/suspend.h>
  13. #include <linux/cpumask.h>
  14. #include <linux/sched/walt.h>
  15. #include "thermal_zone_internal.h"
  16. #if IS_ENABLED(CONFIG_SEC_PM_LOG)
  17. #include <linux/sec_pm_log.h>
  18. #endif
  19. enum thermal_pause_levels {
  20. THERMAL_NO_CPU_PAUSE,
  21. THERMAL_GROUP_CPU_PAUSE,
  22. /* define new pause levels above this line */
  23. MAX_THERMAL_PAUSE_LEVEL
  24. };
  25. #define THERMAL_PAUSE_RETRY_COUNT 5
  26. struct thermal_pause_cdev {
  27. struct list_head node;
  28. cpumask_t cpu_mask;
  29. enum thermal_pause_levels thermal_pause_level;
  30. enum thermal_pause_levels thermal_pause_req;
  31. struct thermal_cooling_device *cdev;
  32. struct device_node *np;
  33. char cdev_name[THERMAL_NAME_LENGTH];
  34. struct work_struct reg_work;
  35. struct work_struct pause_update_work;
  36. };
  37. static DEFINE_MUTEX(cpus_pause_lock);
  38. static LIST_HEAD(thermal_pause_cdev_list);
  39. static struct cpumask cpus_in_max_cooling_level;
  40. static enum cpuhp_state cpu_hp_online;
  41. static BLOCKING_NOTIFIER_HEAD(thermal_pause_notifier);
  42. void thermal_pause_notifier_register(struct notifier_block *n)
  43. {
  44. blocking_notifier_chain_register(&thermal_pause_notifier, n);
  45. }
  46. EXPORT_SYMBOL(thermal_pause_notifier_register);
  47. void thermal_pause_notifier_unregister(struct notifier_block *n)
  48. {
  49. blocking_notifier_chain_unregister(&thermal_pause_notifier, n);
  50. }
  51. EXPORT_SYMBOL(thermal_pause_notifier_unregister);
  52. const struct cpumask *thermal_paused_cpumask(void)
  53. {
  54. return &cpus_in_max_cooling_level;
  55. }
  56. EXPORT_SYMBOL(thermal_paused_cpumask);
  57. static int thermal_pause_hp_online(unsigned int online_cpu)
  58. {
  59. struct thermal_pause_cdev *thermal_pause_cdev;
  60. int ret = 0;
  61. pr_debug("online entry CPU:%d\n", online_cpu);
  62. mutex_lock(&cpus_pause_lock);
  63. list_for_each_entry(thermal_pause_cdev, &thermal_pause_cdev_list, node) {
  64. if (cpumask_test_cpu(online_cpu, &thermal_pause_cdev->cpu_mask)
  65. && !thermal_pause_cdev->cdev)
  66. queue_work(system_highpri_wq,
  67. &thermal_pause_cdev->reg_work);
  68. }
  69. mutex_unlock(&cpus_pause_lock);
  70. pr_debug("online exit CPU:%d\n", online_cpu);
  71. return ret;
  72. }
  73. /**
  74. * thermal_pause_work - work function to pause a group of cpus at
  75. * the specified level.
  76. *
  77. * @thermal_pasue_cdev: the cdev currently being processed
  78. *
  79. * Function to handle setting the current cpus paused by
  80. * this driver for the mask specified in the device.
  81. * it assumes the mutex is locked upon entrance.
  82. */
  83. static int thermal_pause_work(struct thermal_pause_cdev *thermal_pause_cdev)
  84. {
  85. int cpu = 0;
  86. int ret = -EBUSY;
  87. cpumask_t cpus_to_pause, cpus_to_notify;
  88. cpumask_copy(&cpus_to_pause, &thermal_pause_cdev->cpu_mask);
  89. pr_debug("Pause:%*pbl\n", cpumask_pr_args(&thermal_pause_cdev->cpu_mask));
  90. #if IS_ENABLED(CONFIG_SEC_PM_LOG)
  91. ss_thermal_print("Pause:%*pbl\n", cpumask_pr_args(&thermal_pause_cdev->cpu_mask));
  92. #endif
  93. mutex_unlock(&cpus_pause_lock);
  94. ret = walt_pause_cpus(&cpus_to_pause, PAUSE_THERMAL);
  95. mutex_lock(&cpus_pause_lock);
  96. if (ret == 0) {
  97. /* remove CPUs that have already been notified */
  98. cpumask_andnot(&cpus_to_notify, &thermal_pause_cdev->cpu_mask,
  99. &cpus_in_max_cooling_level);
  100. for_each_cpu(cpu, &cpus_to_notify)
  101. blocking_notifier_call_chain(&thermal_pause_notifier, 1,
  102. (void *)(long)cpu);
  103. /* track CPUs currently in cooling level */
  104. cpumask_or(&cpus_in_max_cooling_level,
  105. &cpus_in_max_cooling_level,
  106. &thermal_pause_cdev->cpu_mask);
  107. } else {
  108. /* Failure. These cpus not paused by thermal */
  109. pr_err("Error pausing CPU:%*pbl. err:%d\n",
  110. cpumask_pr_args(&thermal_pause_cdev->cpu_mask), ret);
  111. }
  112. return ret;
  113. }
  114. /**
  115. * thermal_resume_work - work function to unpause a
  116. * group of cpus in the mask for this cdev
  117. *
  118. * @thermal_pasue_cdev: the cdev currently being processed
  119. *
  120. * Function to handle enabling the group of cpus in the cdev.
  121. * This is performed as a work function to avoid conflicts
  122. * between a hotplug event invoking a pause cooling device,
  123. * and a thermal event invoking a pause cooling device.
  124. */
  125. static int thermal_resume_work(struct thermal_pause_cdev *thermal_pause_cdev)
  126. {
  127. int cpu = 0;
  128. int ret = -ENODEV;
  129. cpumask_t cpus_to_unpause, new_paused_cpus, cpus_to_notify;
  130. struct thermal_pause_cdev *cdev;
  131. cpumask_copy(&cpus_to_unpause, &thermal_pause_cdev->cpu_mask);
  132. pr_debug("Unpause:%*pbl\n", cpumask_pr_args(&cpus_to_unpause));
  133. #if IS_ENABLED(CONFIG_SEC_PM_LOG)
  134. ss_thermal_print("Unpause:%*pbl\n", cpumask_pr_args(&thermal_pause_cdev->cpu_mask));
  135. #endif
  136. mutex_unlock(&cpus_pause_lock);
  137. ret = walt_resume_cpus(&cpus_to_unpause, PAUSE_THERMAL);
  138. mutex_lock(&cpus_pause_lock);
  139. if (ret == 0) {
  140. /* gather up the cpus paused state from all the cdevs */
  141. cpumask_clear(&new_paused_cpus);
  142. list_for_each_entry(cdev, &thermal_pause_cdev_list, node) {
  143. if (!cdev->thermal_pause_level || cdev == thermal_pause_cdev)
  144. continue;
  145. cpumask_or(&new_paused_cpus, &new_paused_cpus, &cdev->cpu_mask);
  146. }
  147. /* remove CPUs that will remain paused */
  148. cpumask_andnot(&cpus_to_notify, &cpus_in_max_cooling_level, &new_paused_cpus);
  149. /* Notify for each CPU that we intended to resume */
  150. for_each_cpu(cpu, &cpus_to_notify)
  151. blocking_notifier_call_chain(&thermal_pause_notifier, 0,
  152. (void *)(long)cpu);
  153. /* update the cpus cooling mask */
  154. cpumask_copy(&cpus_in_max_cooling_level, &new_paused_cpus);
  155. } else {
  156. /* Failure. Ref-count for cpus controlled by thermal still set */
  157. pr_err("Error resuming CPU:%*pbl. err:%d\n",
  158. cpumask_pr_args(&thermal_pause_cdev->cpu_mask), ret);
  159. }
  160. return ret;
  161. }
  162. /**
  163. * thermal_pause_set_update_work: Enforce Requested State
  164. *
  165. * @work: the work structure for this work
  166. *
  167. * Enforce the most recent requested cooling state, if
  168. * it is a mismatch with the current state. Since the
  169. * request is made in a different context from the
  170. * enforcement, it is possible to have an updated request
  171. * after completing the resume/pause request.
  172. *
  173. * Handle a post-operation mismatch between the cooling state
  174. * and the requested state.
  175. *
  176. * This is performed as a work function to avoid conflicts
  177. * between a hotplug event invoking a pause cooling device,
  178. * and a thermal event invoking a pause cooling device.
  179. */
  180. static void thermal_pause_update_work(struct work_struct *work)
  181. {
  182. int ret = 0;
  183. int retcnt = THERMAL_PAUSE_RETRY_COUNT;
  184. struct thermal_pause_cdev *thermal_pause_cdev =
  185. container_of(work, struct thermal_pause_cdev, pause_update_work);
  186. mutex_lock(&cpus_pause_lock);
  187. retry:
  188. if (thermal_pause_cdev->thermal_pause_req != thermal_pause_cdev->thermal_pause_level) {
  189. if (thermal_pause_cdev->thermal_pause_req == THERMAL_NO_CPU_PAUSE) {
  190. ret = thermal_resume_work(thermal_pause_cdev);
  191. if (ret >= 0)
  192. thermal_pause_cdev->thermal_pause_level = THERMAL_NO_CPU_PAUSE;
  193. } else {
  194. ret = thermal_pause_work(thermal_pause_cdev);
  195. if (ret >= 0)
  196. thermal_pause_cdev->thermal_pause_level = THERMAL_GROUP_CPU_PAUSE;
  197. }
  198. if (ret < 0 && retcnt > 0) {
  199. retcnt--;
  200. goto retry;
  201. }
  202. }
  203. /*
  204. * if the pause/resume operation itself failed (and failed THERMAL_PAUSE_RETRY_COUNT
  205. * times) then ret will be negative here. Do not repeatedly retry if pause itself
  206. * failed, because this can happen indefinitely.
  207. *
  208. * If instead the pause request has been toggled back and forth many times by
  209. * the thermal framework, this can be handled here.
  210. */
  211. if (ret >= 0 &&
  212. thermal_pause_cdev->thermal_pause_req != thermal_pause_cdev->thermal_pause_level) {
  213. pr_debug("Pause: requested state changed while workfn running\n");
  214. retcnt = THERMAL_PAUSE_RETRY_COUNT;
  215. goto retry;
  216. }
  217. mutex_unlock(&cpus_pause_lock);
  218. }
  219. /**
  220. * thermal_pause_set_cur_state - callback function to set the current cooling
  221. * level.
  222. * @cdev: thermal cooling device pointer.
  223. * @level: set this variable to the current cooling level.
  224. *
  225. * Callback for the thermal cooling device to change the cpu pause
  226. * current cooling level, by making a requested and queueing the
  227. * work to be done.
  228. *
  229. * Return: 0 on success, an error code otherwise.
  230. */
  231. static int thermal_pause_set_cur_state(struct thermal_cooling_device *cdev,
  232. unsigned long level)
  233. {
  234. struct thermal_pause_cdev *thermal_pause_cdev = cdev->devdata;
  235. if (level >= MAX_THERMAL_PAUSE_LEVEL)
  236. return -EINVAL;
  237. mutex_lock(&cpus_pause_lock);
  238. if (level)
  239. thermal_pause_cdev->thermal_pause_req = THERMAL_GROUP_CPU_PAUSE;
  240. else
  241. thermal_pause_cdev->thermal_pause_req = THERMAL_NO_CPU_PAUSE;
  242. queue_work(system_highpri_wq, &thermal_pause_cdev->pause_update_work);
  243. mutex_unlock(&cpus_pause_lock);
  244. return 0;
  245. }
  246. /**
  247. * thermal_pause_get_cur_state - callback function to get the current cooling
  248. * state.
  249. * @cdev: thermal cooling device pointer.
  250. * @state: fill this variable with the current cooling state.
  251. *
  252. * Callback for the thermal cooling device to return the cpu pause
  253. * current cooling level
  254. *
  255. * Return: 0 on success, an error code otherwise.
  256. */
  257. static int thermal_pause_get_cur_state(struct thermal_cooling_device *cdev,
  258. unsigned long *level)
  259. {
  260. struct thermal_pause_cdev *thermal_pause_cdev = cdev->devdata;
  261. *level = thermal_pause_cdev->thermal_pause_level;
  262. return 0;
  263. }
  264. /**
  265. * thermal_pause_get_max_state - callback function to get the max cooling state.
  266. * @cdev: thermal cooling device pointer.
  267. * @level: fill this variable with the max cooling level
  268. *
  269. * Callback for the thermal cooling device to return the cpu
  270. * pause max cooling state.
  271. *
  272. * Return: 0 on success, an error code otherwise.
  273. */
  274. static int thermal_pause_get_max_state(struct thermal_cooling_device *cdev,
  275. unsigned long *level)
  276. {
  277. *level = MAX_THERMAL_PAUSE_LEVEL - 1;
  278. return 0;
  279. }
  280. static struct thermal_cooling_device_ops thermal_pause_cooling_ops = {
  281. .get_max_state = thermal_pause_get_max_state,
  282. .get_cur_state = thermal_pause_get_cur_state,
  283. .set_cur_state = thermal_pause_set_cur_state,
  284. };
  285. static void thermal_pause_register_cdev(struct work_struct *work)
  286. {
  287. struct thermal_pause_cdev *thermal_pause_cdev =
  288. container_of(work, struct thermal_pause_cdev, reg_work);
  289. int ret = 0;
  290. cpumask_t cpus_online;
  291. cpumask_and(&cpus_online,
  292. &thermal_pause_cdev->cpu_mask,
  293. cpu_online_mask);
  294. if (!cpumask_equal(&thermal_pause_cdev->cpu_mask, &cpus_online))
  295. return;
  296. thermal_pause_cdev->cdev = thermal_of_cooling_device_register(
  297. thermal_pause_cdev->np,
  298. thermal_pause_cdev->cdev_name,
  299. thermal_pause_cdev,
  300. &thermal_pause_cooling_ops);
  301. if (IS_ERR(thermal_pause_cdev->cdev)) {
  302. ret = PTR_ERR(thermal_pause_cdev->cdev);
  303. pr_err("Cooling register failed for %s, ret:%d\n",
  304. thermal_pause_cdev->cdev_name, ret);
  305. thermal_pause_cdev->cdev = NULL;
  306. return;
  307. }
  308. pr_debug("Cooling device [%s] registered.\n",
  309. thermal_pause_cdev->cdev_name);
  310. }
  311. static int thermal_pause_probe(struct platform_device *pdev)
  312. {
  313. int ret = 0, cpu = 0;
  314. struct device_node *subsys_np = NULL, *cpu_phandle = NULL;
  315. struct device *cpu_dev;
  316. struct thermal_pause_cdev *thermal_pause_cdev = NULL;
  317. struct device_node *np = pdev->dev.of_node;
  318. struct of_phandle_iterator it;
  319. cpumask_t cpu_mask;
  320. unsigned long mask = 0;
  321. const char *alias;
  322. /*
  323. * cpu pause is first thermal cooling device driver
  324. * which modeprobe in early boot up, hence just register
  325. * for vendor hook to disable cooling stats
  326. */
  327. thermal_vendor_hooks_init();
  328. INIT_LIST_HEAD(&thermal_pause_cdev_list);
  329. cpumask_clear(&cpus_in_max_cooling_level);
  330. for_each_available_child_of_node(np, subsys_np) {
  331. cpumask_clear(&cpu_mask);
  332. mask = 0;
  333. of_phandle_iterator_init(&it, subsys_np, "qcom,cpus", NULL, 0);
  334. while (of_phandle_iterator_next(&it) == 0) {
  335. cpu_phandle = it.node;
  336. for_each_possible_cpu(cpu) {
  337. cpu_dev = get_cpu_device(cpu);
  338. if (cpu_dev && cpu_dev->of_node
  339. == cpu_phandle) {
  340. cpumask_set_cpu(cpu, &cpu_mask);
  341. mask = mask | BIT(cpu);
  342. break;
  343. }
  344. }
  345. }
  346. if (cpumask_empty(&cpu_mask))
  347. continue;
  348. thermal_pause_cdev = devm_kzalloc(&pdev->dev,
  349. sizeof(*thermal_pause_cdev), GFP_KERNEL);
  350. if (!thermal_pause_cdev) {
  351. of_node_put(subsys_np);
  352. return -ENOMEM;
  353. }
  354. ret = of_property_read_string(subsys_np,
  355. "qcom,cdev-alias", &alias);
  356. if (ret)
  357. snprintf(thermal_pause_cdev->cdev_name, THERMAL_NAME_LENGTH,
  358. "thermal-pause-%X", mask);
  359. else
  360. strscpy(thermal_pause_cdev->cdev_name, alias,
  361. THERMAL_NAME_LENGTH);
  362. thermal_pause_cdev->thermal_pause_level = false;
  363. thermal_pause_cdev->cdev = NULL;
  364. thermal_pause_cdev->np = subsys_np;
  365. cpumask_copy(&thermal_pause_cdev->cpu_mask, &cpu_mask);
  366. INIT_WORK(&thermal_pause_cdev->reg_work, thermal_pause_register_cdev);
  367. INIT_WORK(&thermal_pause_cdev->pause_update_work, thermal_pause_update_work);
  368. list_add(&thermal_pause_cdev->node, &thermal_pause_cdev_list);
  369. }
  370. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "thermal-pause/cdev:online",
  371. thermal_pause_hp_online, NULL);
  372. if (ret < 0)
  373. return ret;
  374. cpu_hp_online = ret;
  375. return 0;
  376. }
  377. static int thermal_pause_remove(struct platform_device *pdev)
  378. {
  379. struct thermal_pause_cdev *thermal_pause_cdev = NULL, *next = NULL;
  380. int ret = 0;
  381. thermal_vendor_hooks_exit();
  382. if (cpu_hp_online) {
  383. cpuhp_remove_state_nocalls(cpu_hp_online);
  384. cpu_hp_online = 0;
  385. }
  386. mutex_lock(&cpus_pause_lock);
  387. list_for_each_entry_safe(thermal_pause_cdev, next,
  388. &thermal_pause_cdev_list, node) {
  389. /* for each asserted cooling device, resume the CPUs */
  390. if (thermal_pause_cdev->thermal_pause_level) {
  391. thermal_pause_cdev->thermal_pause_req = THERMAL_NO_CPU_PAUSE;
  392. queue_work(system_highpri_wq, &thermal_pause_cdev->pause_update_work);
  393. }
  394. if (thermal_pause_cdev->cdev)
  395. thermal_cooling_device_unregister(
  396. thermal_pause_cdev->cdev);
  397. list_del(&thermal_pause_cdev->node);
  398. }
  399. mutex_unlock(&cpus_pause_lock);
  400. /* if the resume failed, thermal still controls the CPUs.
  401. * ensure that the error is passed to the caller.
  402. */
  403. return ret;
  404. }
  405. static const struct of_device_id thermal_pause_match[] = {
  406. { .compatible = "qcom,thermal-pause", },
  407. {},
  408. };
  409. static struct platform_driver thermal_pause_driver = {
  410. .probe = thermal_pause_probe,
  411. .remove = thermal_pause_remove,
  412. .driver = {
  413. .name = KBUILD_MODNAME,
  414. .of_match_table = thermal_pause_match,
  415. },
  416. };
  417. module_platform_driver(thermal_pause_driver);
  418. MODULE_LICENSE("GPL v2");