dtpm_cpu.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2020 Linaro Limited
  4. *
  5. * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
  6. *
  7. * The DTPM CPU is based on the energy model. It hooks the CPU in the
  8. * DTPM tree which in turns update the power number by propagating the
  9. * power number from the CPU energy model information to the parents.
  10. *
  11. * The association between the power and the performance state, allows
  12. * to set the power of the CPU at the OPP granularity.
  13. *
  14. * The CPU hotplug is supported and the power numbers will be updated
  15. * if a CPU is hot plugged / unplugged.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/cpumask.h>
  19. #include <linux/cpufreq.h>
  20. #include <linux/cpuhotplug.h>
  21. #include <linux/dtpm.h>
  22. #include <linux/energy_model.h>
  23. #include <linux/of.h>
  24. #include <linux/pm_qos.h>
  25. #include <linux/slab.h>
  26. struct dtpm_cpu {
  27. struct dtpm dtpm;
  28. struct freq_qos_request qos_req;
  29. int cpu;
  30. };
  31. static DEFINE_PER_CPU(struct dtpm_cpu *, dtpm_per_cpu);
  32. static struct dtpm_cpu *to_dtpm_cpu(struct dtpm *dtpm)
  33. {
  34. return container_of(dtpm, struct dtpm_cpu, dtpm);
  35. }
  36. static u64 set_pd_power_limit(struct dtpm *dtpm, u64 power_limit)
  37. {
  38. struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
  39. struct em_perf_domain *pd = em_cpu_get(dtpm_cpu->cpu);
  40. struct em_perf_state *table;
  41. unsigned long freq;
  42. u64 power;
  43. int i, nr_cpus;
  44. nr_cpus = cpumask_weight_and(cpu_online_mask, to_cpumask(pd->cpus));
  45. rcu_read_lock();
  46. table = em_perf_state_from_pd(pd);
  47. for (i = 0; i < pd->nr_perf_states; i++) {
  48. power = table[i].power * nr_cpus;
  49. if (power > power_limit)
  50. break;
  51. }
  52. freq = table[i - 1].frequency;
  53. power_limit = table[i - 1].power * nr_cpus;
  54. rcu_read_unlock();
  55. freq_qos_update_request(&dtpm_cpu->qos_req, freq);
  56. return power_limit;
  57. }
  58. static u64 scale_pd_power_uw(struct cpumask *pd_mask, u64 power)
  59. {
  60. unsigned long max, sum_util = 0;
  61. int cpu;
  62. /*
  63. * The capacity is the same for all CPUs belonging to
  64. * the same perf domain.
  65. */
  66. max = arch_scale_cpu_capacity(cpumask_first(pd_mask));
  67. for_each_cpu_and(cpu, pd_mask, cpu_online_mask)
  68. sum_util += sched_cpu_util(cpu);
  69. return (power * ((sum_util << 10) / max)) >> 10;
  70. }
  71. static u64 get_pd_power_uw(struct dtpm *dtpm)
  72. {
  73. struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
  74. struct em_perf_state *table;
  75. struct em_perf_domain *pd;
  76. struct cpumask *pd_mask;
  77. unsigned long freq;
  78. u64 power = 0;
  79. int i;
  80. pd = em_cpu_get(dtpm_cpu->cpu);
  81. if (!pd)
  82. return 0;
  83. pd_mask = em_span_cpus(pd);
  84. freq = cpufreq_quick_get(dtpm_cpu->cpu);
  85. rcu_read_lock();
  86. table = em_perf_state_from_pd(pd);
  87. for (i = 0; i < pd->nr_perf_states; i++) {
  88. if (table[i].frequency < freq)
  89. continue;
  90. power = scale_pd_power_uw(pd_mask, table[i].power);
  91. break;
  92. }
  93. rcu_read_unlock();
  94. return power;
  95. }
  96. static int update_pd_power_uw(struct dtpm *dtpm)
  97. {
  98. struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
  99. struct em_perf_domain *em = em_cpu_get(dtpm_cpu->cpu);
  100. struct em_perf_state *table;
  101. int nr_cpus;
  102. nr_cpus = cpumask_weight_and(cpu_online_mask, to_cpumask(em->cpus));
  103. rcu_read_lock();
  104. table = em_perf_state_from_pd(em);
  105. dtpm->power_min = table[0].power;
  106. dtpm->power_min *= nr_cpus;
  107. dtpm->power_max = table[em->nr_perf_states - 1].power;
  108. dtpm->power_max *= nr_cpus;
  109. rcu_read_unlock();
  110. return 0;
  111. }
  112. static void pd_release(struct dtpm *dtpm)
  113. {
  114. struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
  115. struct cpufreq_policy *policy;
  116. if (freq_qos_request_active(&dtpm_cpu->qos_req))
  117. freq_qos_remove_request(&dtpm_cpu->qos_req);
  118. policy = cpufreq_cpu_get(dtpm_cpu->cpu);
  119. if (policy) {
  120. for_each_cpu(dtpm_cpu->cpu, policy->related_cpus)
  121. per_cpu(dtpm_per_cpu, dtpm_cpu->cpu) = NULL;
  122. cpufreq_cpu_put(policy);
  123. }
  124. kfree(dtpm_cpu);
  125. }
  126. static struct dtpm_ops dtpm_ops = {
  127. .set_power_uw = set_pd_power_limit,
  128. .get_power_uw = get_pd_power_uw,
  129. .update_power_uw = update_pd_power_uw,
  130. .release = pd_release,
  131. };
  132. static int cpuhp_dtpm_cpu_offline(unsigned int cpu)
  133. {
  134. struct dtpm_cpu *dtpm_cpu;
  135. dtpm_cpu = per_cpu(dtpm_per_cpu, cpu);
  136. if (dtpm_cpu)
  137. dtpm_update_power(&dtpm_cpu->dtpm);
  138. return 0;
  139. }
  140. static int cpuhp_dtpm_cpu_online(unsigned int cpu)
  141. {
  142. struct dtpm_cpu *dtpm_cpu;
  143. dtpm_cpu = per_cpu(dtpm_per_cpu, cpu);
  144. if (dtpm_cpu)
  145. return dtpm_update_power(&dtpm_cpu->dtpm);
  146. return 0;
  147. }
  148. static int __dtpm_cpu_setup(int cpu, struct dtpm *parent)
  149. {
  150. struct dtpm_cpu *dtpm_cpu;
  151. struct cpufreq_policy *policy;
  152. struct em_perf_state *table;
  153. struct em_perf_domain *pd;
  154. char name[CPUFREQ_NAME_LEN];
  155. int ret = -ENOMEM;
  156. dtpm_cpu = per_cpu(dtpm_per_cpu, cpu);
  157. if (dtpm_cpu)
  158. return 0;
  159. policy = cpufreq_cpu_get(cpu);
  160. if (!policy)
  161. return 0;
  162. pd = em_cpu_get(cpu);
  163. if (!pd || em_is_artificial(pd)) {
  164. ret = -EINVAL;
  165. goto release_policy;
  166. }
  167. dtpm_cpu = kzalloc_obj(*dtpm_cpu);
  168. if (!dtpm_cpu) {
  169. ret = -ENOMEM;
  170. goto release_policy;
  171. }
  172. dtpm_init(&dtpm_cpu->dtpm, &dtpm_ops);
  173. dtpm_cpu->cpu = cpu;
  174. for_each_cpu(cpu, policy->related_cpus)
  175. per_cpu(dtpm_per_cpu, cpu) = dtpm_cpu;
  176. snprintf(name, sizeof(name), "cpu%d-cpufreq", dtpm_cpu->cpu);
  177. ret = dtpm_register(name, &dtpm_cpu->dtpm, parent);
  178. if (ret)
  179. goto out_kfree_dtpm_cpu;
  180. rcu_read_lock();
  181. table = em_perf_state_from_pd(pd);
  182. ret = freq_qos_add_request(&policy->constraints,
  183. &dtpm_cpu->qos_req, FREQ_QOS_MAX,
  184. table[pd->nr_perf_states - 1].frequency);
  185. rcu_read_unlock();
  186. if (ret < 0)
  187. goto out_dtpm_unregister;
  188. cpufreq_cpu_put(policy);
  189. return 0;
  190. out_dtpm_unregister:
  191. dtpm_unregister(&dtpm_cpu->dtpm);
  192. dtpm_cpu = NULL;
  193. out_kfree_dtpm_cpu:
  194. for_each_cpu(cpu, policy->related_cpus)
  195. per_cpu(dtpm_per_cpu, cpu) = NULL;
  196. kfree(dtpm_cpu);
  197. release_policy:
  198. cpufreq_cpu_put(policy);
  199. return ret;
  200. }
  201. static int dtpm_cpu_setup(struct dtpm *dtpm, struct device_node *np)
  202. {
  203. int cpu;
  204. cpu = of_cpu_node_to_id(np);
  205. if (cpu < 0)
  206. return 0;
  207. return __dtpm_cpu_setup(cpu, dtpm);
  208. }
  209. static int dtpm_cpu_init(void)
  210. {
  211. int ret;
  212. /*
  213. * The callbacks at CPU hotplug time are calling
  214. * dtpm_update_power() which in turns calls update_pd_power().
  215. *
  216. * The function update_pd_power() uses the online mask to
  217. * figure out the power consumption limits.
  218. *
  219. * At CPUHP_AP_ONLINE_DYN, the CPU is present in the CPU
  220. * online mask when the cpuhp_dtpm_cpu_online function is
  221. * called, but the CPU is still in the online mask for the
  222. * tear down callback. So the power can not be updated when
  223. * the CPU is unplugged.
  224. *
  225. * At CPUHP_AP_DTPM_CPU_DEAD, the situation is the opposite as
  226. * above. The CPU online mask is not up to date when the CPU
  227. * is plugged in.
  228. *
  229. * For this reason, we need to call the online and offline
  230. * callbacks at different moments when the CPU online mask is
  231. * consistent with the power numbers we want to update.
  232. */
  233. ret = cpuhp_setup_state(CPUHP_AP_DTPM_CPU_DEAD, "dtpm_cpu:offline",
  234. NULL, cpuhp_dtpm_cpu_offline);
  235. if (ret < 0)
  236. return ret;
  237. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "dtpm_cpu:online",
  238. cpuhp_dtpm_cpu_online, NULL);
  239. if (ret < 0)
  240. return ret;
  241. return 0;
  242. }
  243. static void dtpm_cpu_exit(void)
  244. {
  245. cpuhp_remove_state_nocalls(CPUHP_AP_ONLINE_DYN);
  246. cpuhp_remove_state_nocalls(CPUHP_AP_DTPM_CPU_DEAD);
  247. }
  248. struct dtpm_subsys_ops dtpm_cpu_ops = {
  249. .name = KBUILD_MODNAME,
  250. .init = dtpm_cpu_init,
  251. .exit = dtpm_cpu_exit,
  252. .setup = dtpm_cpu_setup,
  253. };