qti_cpufreq_cdev.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "%s:%s " fmt, KBUILD_MODNAME, __func__
  7. #include <linux/cpu.h>
  8. #include <linux/cpufreq.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm_qos.h>
  13. #include <linux/slab.h>
  14. #include <linux/thermal.h>
  15. #include <linux/workqueue.h>
  16. #define CPUFREQ_CDEV_NAME "cpu%d"
  17. #define CPUFREQ_CDEV "qcom-cpufreq-cdev"
  18. struct cpufreq_cdev_device {
  19. struct list_head node;
  20. struct thermal_cooling_device *cdev;
  21. int cpu;
  22. unsigned long cur_state;
  23. unsigned long max_state;
  24. unsigned int *freq_table;
  25. struct freq_qos_request qos_max_freq_req;
  26. char cdev_name[THERMAL_NAME_LENGTH];
  27. struct cpufreq_policy *policy;
  28. struct work_struct reg_work;
  29. };
  30. static DEFINE_MUTEX(qti_cpufreq_cdev_lock);
  31. static LIST_HEAD(qti_cpufreq_cdev_list);
  32. static enum cpuhp_state cpu_hp_online;
  33. static unsigned int state_to_cpufreq(struct cpufreq_cdev_device *cdev_data,
  34. unsigned long state)
  35. {
  36. return cdev_data->freq_table ?
  37. cdev_data->freq_table[state] : UINT_MAX;
  38. }
  39. static int cpufreq_cdev_set_state(struct thermal_cooling_device *cdev,
  40. unsigned long state)
  41. {
  42. struct cpufreq_cdev_device *cdev_data = cdev->devdata;
  43. int ret = 0;
  44. unsigned int freq;
  45. if (state > cdev_data->max_state)
  46. return -EINVAL;
  47. if (state == cdev_data->cur_state)
  48. return 0;
  49. if (freq_qos_request_active(&cdev_data->qos_max_freq_req)) {
  50. freq = state_to_cpufreq(cdev_data, state);
  51. pr_debug("cdev:%s Limit:%u\n", cdev->type, freq);
  52. ret = freq_qos_update_request(&cdev_data->qos_max_freq_req,
  53. freq);
  54. if (ret < 0) {
  55. pr_err("Error placing qos request:%u. cdev:%s err:%d\n",
  56. freq, cdev->type, ret);
  57. return ret;
  58. }
  59. }
  60. cdev_data->cur_state = state;
  61. return 0;
  62. }
  63. static int cpufreq_cdev_get_state(struct thermal_cooling_device *cdev,
  64. unsigned long *state)
  65. {
  66. struct cpufreq_cdev_device *cdev_data = cdev->devdata;
  67. *state = cdev_data->cur_state;
  68. return 0;
  69. }
  70. static int cpufreq_cdev_get_max_state(struct thermal_cooling_device *cdev,
  71. unsigned long *state)
  72. {
  73. struct cpufreq_cdev_device *cdev_data = cdev->devdata;
  74. *state = cdev_data->max_state;
  75. return 0;
  76. }
  77. static struct thermal_cooling_device_ops cpufreq_cdev_ops = {
  78. .set_cur_state = cpufreq_cdev_set_state,
  79. .get_cur_state = cpufreq_cdev_get_state,
  80. .get_max_state = cpufreq_cdev_get_max_state,
  81. };
  82. static void cpufreq_cdev_register(struct work_struct *work)
  83. {
  84. struct cpufreq_cdev_device *cdev_data = container_of(work,
  85. struct cpufreq_cdev_device, reg_work);
  86. struct cpufreq_policy *policy = NULL;
  87. int freq_count = 0, i;
  88. policy = cpufreq_cpu_get(cdev_data->cpu);
  89. if (!policy) {
  90. pr_err("No policy for CPU:%d\n", cdev_data->cpu);
  91. return;
  92. }
  93. freq_count = cpufreq_table_count_valid_entries(policy);
  94. if (!freq_count) {
  95. pr_debug("CPU%d freq policy table count error%d\n",
  96. cdev_data->cpu, freq_count);
  97. goto error_exit;
  98. }
  99. cdev_data->freq_table = kmalloc_array(freq_count,
  100. sizeof(*cdev_data->freq_table),
  101. GFP_KERNEL);
  102. if (!cdev_data->freq_table)
  103. goto error_exit;
  104. for (i = 0; i < freq_count; i++) {
  105. if (policy->freq_table_sorted ==
  106. CPUFREQ_TABLE_SORTED_ASCENDING)
  107. cdev_data->freq_table[i] =
  108. policy->freq_table[freq_count - i - 1].frequency;
  109. else
  110. cdev_data->freq_table[i] =
  111. policy->freq_table[i].frequency;
  112. }
  113. freq_count--;
  114. cdev_data->policy = policy;
  115. cdev_data->max_state = freq_count;
  116. cdev_data->cur_state = 0;
  117. freq_qos_add_request(&policy->constraints,
  118. &cdev_data->qos_max_freq_req, FREQ_QOS_MAX,
  119. state_to_cpufreq(cdev_data, 0));
  120. cdev_data->cdev = thermal_cooling_device_register(cdev_data->cdev_name,
  121. cdev_data, &cpufreq_cdev_ops);
  122. if (IS_ERR(cdev_data->cdev)) {
  123. pr_err("Cdev register failed for %s, ret:%d\n",
  124. cdev_data->cdev_name, PTR_ERR(cdev_data->cdev));
  125. freq_qos_remove_request(&cdev_data->qos_max_freq_req);
  126. goto error_exit;
  127. }
  128. pr_debug("Cdev %s registered\n", cdev_data->cdev_name);
  129. return;
  130. error_exit:
  131. if (policy)
  132. cpufreq_cpu_put(policy);
  133. if (cdev_data->cdev)
  134. cdev_data->cdev = NULL;
  135. kfree(cdev_data->freq_table);
  136. }
  137. static int cpufreq_cdev_hp_online(unsigned int online_cpu)
  138. {
  139. struct cpufreq_cdev_device *cdev_data;
  140. mutex_lock(&qti_cpufreq_cdev_lock);
  141. list_for_each_entry(cdev_data, &qti_cpufreq_cdev_list, node) {
  142. if (cdev_data->cpu != online_cpu || cdev_data->cdev)
  143. continue;
  144. queue_work(system_highpri_wq, &cdev_data->reg_work);
  145. }
  146. mutex_unlock(&qti_cpufreq_cdev_lock);
  147. return 0;
  148. }
  149. static int cpufreq_cdev_probe(struct platform_device *pdev)
  150. {
  151. struct cpufreq_cdev_device *cdev_data;
  152. struct device_node *np = pdev->dev.of_node, *cpu_phandle = NULL;
  153. struct device_node *subsys_np = NULL;
  154. struct device *dev = &pdev->dev;
  155. struct device *cpu_dev;
  156. int cpu = 0, ret = 0;
  157. int cpu_count;
  158. struct of_phandle_iterator it;
  159. mutex_lock(&qti_cpufreq_cdev_lock);
  160. for_each_available_child_of_node(np, subsys_np) {
  161. of_phandle_iterator_init(&it, subsys_np, "qcom,cpus", NULL, 0);
  162. cpu_count = 0;
  163. while (of_phandle_iterator_next(&it) == 0) {
  164. cpu_phandle = it.node;
  165. for_each_possible_cpu(cpu) {
  166. cpu_dev = get_cpu_device(cpu);
  167. if (cpu_dev && (cpu_dev->of_node == cpu_phandle)) {
  168. cpu_count++;
  169. break;
  170. }
  171. }
  172. if (cpu_count)
  173. break;
  174. }
  175. if (!cpu_count)
  176. continue;
  177. cdev_data = devm_kzalloc(dev, sizeof(*cdev_data), GFP_KERNEL);
  178. if (!cdev_data) {
  179. mutex_unlock(&qti_cpufreq_cdev_lock);
  180. return -ENOMEM;
  181. }
  182. cdev_data->cpu = cpu;
  183. snprintf(cdev_data->cdev_name, THERMAL_NAME_LENGTH,
  184. subsys_np->name);
  185. INIT_WORK(&cdev_data->reg_work,
  186. cpufreq_cdev_register);
  187. list_add(&cdev_data->node, &qti_cpufreq_cdev_list);
  188. }
  189. mutex_unlock(&qti_cpufreq_cdev_lock);
  190. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "thermal-cpu/cdev:online",
  191. cpufreq_cdev_hp_online, NULL);
  192. if (ret < 0)
  193. return ret;
  194. cpu_hp_online = ret;
  195. return 0;
  196. }
  197. static int cpufreq_cdev_remove(struct platform_device *pdev)
  198. {
  199. struct cpufreq_cdev_device *cdev_data;
  200. mutex_lock(&qti_cpufreq_cdev_lock);
  201. if (cpu_hp_online) {
  202. cpuhp_remove_state_nocalls(cpu_hp_online);
  203. cpu_hp_online = 0;
  204. }
  205. list_for_each_entry(cdev_data, &qti_cpufreq_cdev_list, node) {
  206. if (!cdev_data->cdev)
  207. continue;
  208. thermal_cooling_device_unregister(cdev_data->cdev);
  209. if (freq_qos_request_active(&cdev_data->qos_max_freq_req))
  210. freq_qos_remove_request(&cdev_data->qos_max_freq_req);
  211. cdev_data->cdev = NULL;
  212. cpufreq_cpu_put(cdev_data->policy);
  213. kfree(cdev_data->freq_table);
  214. }
  215. mutex_unlock(&qti_cpufreq_cdev_lock);
  216. return 0;
  217. }
  218. static const struct of_device_id cpufreq_cdev_match[] = {
  219. {.compatible = "qcom,cpufreq-cdev"},
  220. {}
  221. };
  222. static struct platform_driver cpufreq_cdev_driver = {
  223. .probe = cpufreq_cdev_probe,
  224. .remove = cpufreq_cdev_remove,
  225. .driver = {
  226. .name = CPUFREQ_CDEV,
  227. .of_match_table = cpufreq_cdev_match,
  228. },
  229. };
  230. static int __init cpufreq_cdev_init(void)
  231. {
  232. return platform_driver_register(&cpufreq_cdev_driver);
  233. }
  234. module_init(cpufreq_cdev_init);
  235. static void __exit cpufreq_cdev_exit(void)
  236. {
  237. platform_driver_unregister(&cpufreq_cdev_driver);
  238. }
  239. module_exit(cpufreq_cdev_exit);
  240. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. cpufreq cooling driver");
  241. MODULE_LICENSE("GPL");