cpu_hotplug.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/thermal.h>
  9. #include <linux/err.h>
  10. #include <linux/slab.h>
  11. #include <linux/cpu.h>
  12. #include <linux/device.h>
  13. #if IS_ENABLED(CONFIG_SEC_PM_LOG)
  14. #include <linux/sec_pm_log.h>
  15. #endif
  16. #define CPU_HOTPLUG_LEVEL 1
  17. struct cpu_hot_cdev {
  18. struct list_head node;
  19. int cpu_id;
  20. bool cpu_hot_state;
  21. bool cpu_cur_state;
  22. struct thermal_cooling_device *cdev;
  23. struct device_node *np;
  24. char cdev_name[THERMAL_NAME_LENGTH];
  25. struct work_struct reg_work;
  26. struct work_struct exec_work;
  27. };
  28. static enum cpuhp_state cpu_hp_online;
  29. static DEFINE_MUTEX(cpu_hot_lock);
  30. static LIST_HEAD(cpu_hot_cdev_list);
  31. static int cpu_hot_hp_online(unsigned int online_cpu)
  32. {
  33. struct cpu_hot_cdev *cpu_hot_cdev;
  34. int ret = 0;
  35. mutex_lock(&cpu_hot_lock);
  36. list_for_each_entry(cpu_hot_cdev, &cpu_hot_cdev_list, node) {
  37. if (online_cpu != cpu_hot_cdev->cpu_id)
  38. continue;
  39. if (cpu_hot_cdev->cdev) {
  40. if (cpu_hot_cdev->cpu_hot_state) {
  41. pr_debug("Offline CPU:%d\n",
  42. online_cpu);
  43. ret = NOTIFY_BAD;
  44. }
  45. } else
  46. queue_work(system_highpri_wq, &cpu_hot_cdev->reg_work);
  47. break;
  48. }
  49. mutex_unlock(&cpu_hot_lock);
  50. return ret;
  51. }
  52. /**
  53. * cpu_hot_set_cur_state - callback function to set the current cooling
  54. * state.
  55. * @cdev: thermal cooling device pointer.
  56. * @state: set this variable to the current cooling state.
  57. *
  58. * Callback for the thermal cooling device to change the cpu hotplug
  59. * current cooling state.
  60. *
  61. * Return: 0 on success, an error code otherwise.
  62. */
  63. static int cpu_hot_set_cur_state(struct thermal_cooling_device *cdev,
  64. unsigned long state)
  65. {
  66. struct cpu_hot_cdev *cpu_hot_cdev = cdev->devdata;
  67. if (cpu_hot_cdev->cpu_id == -1)
  68. return -ENODEV;
  69. /* Request state should be less than max_level */
  70. if (state > CPU_HOTPLUG_LEVEL)
  71. return -EINVAL;
  72. state = !!state;
  73. /* Check if the old cooling action is same as new cooling action */
  74. if (cpu_hot_cdev->cpu_hot_state == state)
  75. return 0;
  76. mutex_lock(&cpu_hot_lock);
  77. cpu_hot_cdev->cpu_hot_state = state;
  78. mutex_unlock(&cpu_hot_lock);
  79. queue_work(system_highpri_wq, &cpu_hot_cdev->exec_work);
  80. return 0;
  81. }
  82. /**
  83. * cpu_hot_get_cur_state - callback function to get the current cooling
  84. * state.
  85. * @cdev: thermal cooling device pointer.
  86. * @state: fill this variable with the current cooling state.
  87. *
  88. * Callback for the thermal cooling device to return the cpu hotplug
  89. * current cooling state.
  90. *
  91. * Return: 0 on success, an error code otherwise.
  92. */
  93. static int cpu_hot_get_cur_state(struct thermal_cooling_device *cdev,
  94. unsigned long *state)
  95. {
  96. struct cpu_hot_cdev *cpu_hot_cdev = cdev->devdata;
  97. mutex_lock(&cpu_hot_lock);
  98. *state = (cpu_hot_cdev->cpu_hot_state) ?
  99. CPU_HOTPLUG_LEVEL : 0;
  100. mutex_unlock(&cpu_hot_lock);
  101. return 0;
  102. }
  103. /**
  104. * cpu_hot_get_max_state - callback function to get the max cooling state.
  105. * @cdev: thermal cooling device pointer.
  106. * @state: fill this variable with the max cooling state.
  107. *
  108. * Callback for the thermal cooling device to return the cpu
  109. * hotplug max cooling state.
  110. *
  111. * Return: 0 on success, an error code otherwise.
  112. */
  113. static int cpu_hot_get_max_state(struct thermal_cooling_device *cdev,
  114. unsigned long *state)
  115. {
  116. *state = CPU_HOTPLUG_LEVEL;
  117. return 0;
  118. }
  119. static struct thermal_cooling_device_ops cpu_hot_cooling_ops = {
  120. .get_max_state = cpu_hot_get_max_state,
  121. .get_cur_state = cpu_hot_get_cur_state,
  122. .set_cur_state = cpu_hot_set_cur_state,
  123. };
  124. static void cpu_hot_execute_cdev(struct work_struct *work)
  125. {
  126. struct cpu_hot_cdev *cpu_hot_cdev =
  127. container_of(work, struct cpu_hot_cdev, exec_work);
  128. int ret = 0, cpu = 0;
  129. mutex_lock(&cpu_hot_lock);
  130. cpu = cpu_hot_cdev->cpu_id;
  131. if (cpu_hot_cdev->cpu_hot_state == CPU_HOTPLUG_LEVEL) {
  132. if (!cpu_hot_cdev->cpu_cur_state)
  133. goto unlock_exit;
  134. mutex_unlock(&cpu_hot_lock);
  135. ret = remove_cpu(cpu);
  136. mutex_lock(&cpu_hot_lock);
  137. if (ret < 0)
  138. pr_err("CPU:%d offline error:%d\n", cpu, ret);
  139. else {
  140. #if IS_ENABLED(CONFIG_SEC_PM_LOG)
  141. ss_thermal_print("offline cpu%d\n", cpu);
  142. #endif
  143. cpu_hot_cdev->cpu_cur_state = false;
  144. }
  145. } else {
  146. if (cpu_hot_cdev->cpu_cur_state)
  147. goto unlock_exit;
  148. mutex_unlock(&cpu_hot_lock);
  149. ret = add_cpu(cpu);
  150. mutex_lock(&cpu_hot_lock);
  151. if (ret)
  152. pr_err("CPU:%d online error:%d\n", cpu, ret);
  153. else {
  154. #if IS_ENABLED(CONFIG_SEC_PM_LOG)
  155. ss_thermal_print("online cpu%d\n", cpu);
  156. #endif
  157. cpu_hot_cdev->cpu_cur_state = true;
  158. }
  159. }
  160. unlock_exit:
  161. mutex_unlock(&cpu_hot_lock);
  162. }
  163. static void cpu_hot_register_cdev(struct work_struct *work)
  164. {
  165. struct cpu_hot_cdev *cpu_hot_cdev =
  166. container_of(work, struct cpu_hot_cdev, reg_work);
  167. int ret = 0;
  168. cpu_hot_cdev->cdev = thermal_of_cooling_device_register(
  169. cpu_hot_cdev->np,
  170. cpu_hot_cdev->cdev_name,
  171. cpu_hot_cdev,
  172. &cpu_hot_cooling_ops);
  173. if (IS_ERR(cpu_hot_cdev->cdev)) {
  174. ret = PTR_ERR(cpu_hot_cdev->cdev);
  175. pr_err("Cooling register failed for %s, ret:%d\n",
  176. cpu_hot_cdev->cdev_name, ret);
  177. cpu_hot_cdev->cdev = NULL;
  178. return;
  179. }
  180. pr_debug("Cooling device [%s] registered.\n", cpu_hot_cdev->cdev_name);
  181. }
  182. static int cpu_hot_probe(struct platform_device *pdev)
  183. {
  184. int ret = 0, cpu = 0;
  185. struct device_node *dev_phandle, *subsys_np = NULL;
  186. struct device *cpu_dev;
  187. struct cpu_hot_cdev *cpu_hot_cdev = NULL;
  188. struct device_node *np = pdev->dev.of_node;
  189. const char *alias;
  190. INIT_LIST_HEAD(&cpu_hot_cdev_list);
  191. for_each_available_child_of_node(np, subsys_np) {
  192. cpu_hot_cdev = devm_kzalloc(&pdev->dev,
  193. sizeof(*cpu_hot_cdev), GFP_KERNEL);
  194. if (!cpu_hot_cdev) {
  195. of_node_put(subsys_np);
  196. return -ENOMEM;
  197. }
  198. cpu_hot_cdev->cpu_id = -1;
  199. cpu_hot_cdev->cpu_hot_state = false;
  200. cpu_hot_cdev->cpu_cur_state = true;
  201. cpu_hot_cdev->cdev = NULL;
  202. cpu_hot_cdev->np = subsys_np;
  203. dev_phandle = of_parse_phandle(subsys_np, "qcom,cpu", 0);
  204. for_each_possible_cpu(cpu) {
  205. cpu_dev = get_cpu_device(cpu);
  206. if (cpu_dev && cpu_dev->of_node == dev_phandle) {
  207. cpu_hot_cdev->cpu_id = cpu;
  208. break;
  209. }
  210. }
  211. if (cpu_hot_cdev->cpu_id == -1) {
  212. dev_err(&pdev->dev, "Invalid CPU phandle\n");
  213. continue;
  214. }
  215. ret = of_property_read_string(subsys_np,
  216. "qcom,cdev-alias", &alias);
  217. if (ret)
  218. snprintf(cpu_hot_cdev->cdev_name, THERMAL_NAME_LENGTH, "cpu-hotplug%d",
  219. cpu_hot_cdev->cpu_id);
  220. else
  221. strscpy(cpu_hot_cdev->cdev_name, alias,
  222. THERMAL_NAME_LENGTH);
  223. INIT_WORK(&cpu_hot_cdev->reg_work,
  224. cpu_hot_register_cdev);
  225. INIT_WORK(&cpu_hot_cdev->exec_work,
  226. cpu_hot_execute_cdev);
  227. list_add(&cpu_hot_cdev->node, &cpu_hot_cdev_list);
  228. }
  229. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "cpu-hotplug/cdev:online",
  230. cpu_hot_hp_online, NULL);
  231. if (ret < 0)
  232. return ret;
  233. cpu_hp_online = ret;
  234. return 0;
  235. }
  236. static int cpu_hot_remove(struct platform_device *pdev)
  237. {
  238. struct cpu_hot_cdev *cpu_hot_cdev = NULL, *next = NULL;
  239. int ret = 0;
  240. if (cpu_hp_online) {
  241. cpuhp_remove_state_nocalls(cpu_hp_online);
  242. cpu_hp_online = 0;
  243. }
  244. mutex_lock(&cpu_hot_lock);
  245. list_for_each_entry_safe(cpu_hot_cdev, next, &cpu_hot_cdev_list, node) {
  246. if (!cpu_hot_cdev->cdev)
  247. goto loop_skip;
  248. thermal_cooling_device_unregister(cpu_hot_cdev->cdev);
  249. if (cpu_hot_cdev->cpu_hot_state) {
  250. cpu_hot_cdev->cpu_hot_state = false;
  251. ret = add_cpu(cpu_hot_cdev->cpu_id);
  252. if (ret)
  253. pr_err("CPU:%d online error:%d\n",
  254. cpu_hot_cdev->cpu_id, ret);
  255. }
  256. loop_skip:
  257. list_del(&cpu_hot_cdev->node);
  258. }
  259. mutex_unlock(&cpu_hot_lock);
  260. return 0;
  261. }
  262. static const struct of_device_id cpu_hot_match[] = {
  263. { .compatible = "qcom,cpu-hotplug", },
  264. {},
  265. };
  266. static struct platform_driver cpu_hot_driver = {
  267. .probe = cpu_hot_probe,
  268. .remove = cpu_hot_remove,
  269. .driver = {
  270. .name = KBUILD_MODNAME,
  271. .of_match_table = cpu_hot_match,
  272. },
  273. };
  274. module_platform_driver(cpu_hot_driver);
  275. MODULE_DESCRIPTION("CPU Hotplug cooling device driver");
  276. MODULE_LICENSE("GPL");