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 19 #include <linux/cpumask.h> 20 #include <linux/cpufreq.h> 21 #include <linux/cpuhotplug.h> 22 #include <linux/dtpm.h> 23 #include <linux/energy_model.h> 24 #include <linux/of.h> 25 #include <linux/pm_qos.h> 26 #include <linux/slab.h> 27 28 struct dtpm_cpu { 29 struct dtpm dtpm; 30 struct freq_qos_request qos_req; 31 int cpu; 32 }; 33 34 static DEFINE_PER_CPU(struct dtpm_cpu *, dtpm_per_cpu); 35 36 static struct dtpm_cpu *to_dtpm_cpu(struct dtpm *dtpm) 37 { 38 return container_of(dtpm, struct dtpm_cpu, dtpm); 39 } 40 41 static u64 set_pd_power_limit(struct dtpm *dtpm, u64 power_limit) 42 { 43 struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm); 44 struct em_perf_domain *pd = em_cpu_get(dtpm_cpu->cpu); 45 struct cpumask cpus; 46 unsigned long freq; 47 u64 power; 48 int i, nr_cpus; 49 50 cpumask_and(&cpus, cpu_online_mask, to_cpumask(pd->cpus)); 51 nr_cpus = cpumask_weight(&cpus); 52 53 for (i = 0; i < pd->nr_perf_states; i++) { 54 55 power = pd->table[i].power * nr_cpus; 56 57 if (power > power_limit) 58 break; 59 } 60 61 freq = pd->table[i - 1].frequency; 62 63 freq_qos_update_request(&dtpm_cpu->qos_req, freq); 64 65 power_limit = pd->table[i - 1].power * nr_cpus; 66 67 return power_limit; 68 } 69 70 static u64 scale_pd_power_uw(struct cpumask *pd_mask, u64 power) 71 { 72 unsigned long max, sum_util = 0; 73 int cpu; 74 75 /* 76 * The capacity is the same for all CPUs belonging to 77 * the same perf domain. 78 */ 79 max = arch_scale_cpu_capacity(cpumask_first(pd_mask)); 80 81 for_each_cpu_and(cpu, pd_mask, cpu_online_mask) 82 sum_util += sched_cpu_util(cpu); 83 84 return (power * ((sum_util << 10) / max)) >> 10; 85 } 86 87 static u64 get_pd_power_uw(struct dtpm *dtpm) 88 { 89 struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm); 90 struct em_perf_domain *pd; 91 struct cpumask *pd_mask; 92 unsigned long freq; 93 int i; 94 95 pd = em_cpu_get(dtpm_cpu->cpu); 96 97 pd_mask = em_span_cpus(pd); 98 99 freq = cpufreq_quick_get(dtpm_cpu->cpu); 100 101 for (i = 0; i < pd->nr_perf_states; i++) { 102 103 if (pd->table[i].frequency < freq) 104 continue; 105 106 return scale_pd_power_uw(pd_mask, pd->table[i].power); 107 } 108 109 return 0; 110 } 111 112 static int update_pd_power_uw(struct dtpm *dtpm) 113 { 114 struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm); 115 struct em_perf_domain *em = em_cpu_get(dtpm_cpu->cpu); 116 struct cpumask cpus; 117 int nr_cpus; 118 119 cpumask_and(&cpus, cpu_online_mask, to_cpumask(em->cpus)); 120 nr_cpus = cpumask_weight(&cpus); 121 122 dtpm->power_min = em->table[0].power; 123 dtpm->power_min *= nr_cpus; 124 125 dtpm->power_max = em->table[em->nr_perf_states - 1].power; 126 dtpm->power_max *= nr_cpus; 127 128 return 0; 129 } 130 131 static void pd_release(struct dtpm *dtpm) 132 { 133 struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm); 134 struct cpufreq_policy *policy; 135 136 if (freq_qos_request_active(&dtpm_cpu->qos_req)) 137 freq_qos_remove_request(&dtpm_cpu->qos_req); 138 139 policy = cpufreq_cpu_get(dtpm_cpu->cpu); 140 if (policy) { 141 for_each_cpu(dtpm_cpu->cpu, policy->related_cpus) 142 per_cpu(dtpm_per_cpu, dtpm_cpu->cpu) = NULL; 143 } 144 145 kfree(dtpm_cpu); 146 } 147 148 static struct dtpm_ops dtpm_ops = { 149 .set_power_uw = set_pd_power_limit, 150 .get_power_uw = get_pd_power_uw, 151 .update_power_uw = update_pd_power_uw, 152 .release = pd_release, 153 }; 154 155 static int cpuhp_dtpm_cpu_offline(unsigned int cpu) 156 { 157 struct dtpm_cpu *dtpm_cpu; 158 159 dtpm_cpu = per_cpu(dtpm_per_cpu, cpu); 160 if (dtpm_cpu) 161 dtpm_update_power(&dtpm_cpu->dtpm); 162 163 return 0; 164 } 165 166 static int cpuhp_dtpm_cpu_online(unsigned int cpu) 167 { 168 struct dtpm_cpu *dtpm_cpu; 169 170 dtpm_cpu = per_cpu(dtpm_per_cpu, cpu); 171 if (dtpm_cpu) 172 return dtpm_update_power(&dtpm_cpu->dtpm); 173 174 return 0; 175 } 176 177 static int __dtpm_cpu_setup(int cpu, struct dtpm *parent) 178 { 179 struct dtpm_cpu *dtpm_cpu; 180 struct cpufreq_policy *policy; 181 struct em_perf_domain *pd; 182 char name[CPUFREQ_NAME_LEN]; 183 int ret = -ENOMEM; 184 185 dtpm_cpu = per_cpu(dtpm_per_cpu, cpu); 186 if (dtpm_cpu) 187 return 0; 188 189 policy = cpufreq_cpu_get(cpu); 190 if (!policy) 191 return 0; 192 193 pd = em_cpu_get(cpu); 194 if (!pd || em_is_artificial(pd)) 195 return -EINVAL; 196 197 dtpm_cpu = kzalloc(sizeof(*dtpm_cpu), GFP_KERNEL); 198 if (!dtpm_cpu) 199 return -ENOMEM; 200 201 dtpm_init(&dtpm_cpu->dtpm, &dtpm_ops); 202 dtpm_cpu->cpu = cpu; 203 204 for_each_cpu(cpu, policy->related_cpus) 205 per_cpu(dtpm_per_cpu, cpu) = dtpm_cpu; 206 207 snprintf(name, sizeof(name), "cpu%d-cpufreq", dtpm_cpu->cpu); 208 209 ret = dtpm_register(name, &dtpm_cpu->dtpm, parent); 210 if (ret) 211 goto out_kfree_dtpm_cpu; 212 213 ret = freq_qos_add_request(&policy->constraints, 214 &dtpm_cpu->qos_req, FREQ_QOS_MAX, 215 pd->table[pd->nr_perf_states - 1].frequency); 216 if (ret) 217 goto out_dtpm_unregister; 218 219 return 0; 220 221 out_dtpm_unregister: 222 dtpm_unregister(&dtpm_cpu->dtpm); 223 dtpm_cpu = NULL; 224 225 out_kfree_dtpm_cpu: 226 for_each_cpu(cpu, policy->related_cpus) 227 per_cpu(dtpm_per_cpu, cpu) = NULL; 228 kfree(dtpm_cpu); 229 230 return ret; 231 } 232 233 static int dtpm_cpu_setup(struct dtpm *dtpm, struct device_node *np) 234 { 235 int cpu; 236 237 cpu = of_cpu_node_to_id(np); 238 if (cpu < 0) 239 return 0; 240 241 return __dtpm_cpu_setup(cpu, dtpm); 242 } 243 244 static int dtpm_cpu_init(void) 245 { 246 int ret; 247 248 /* 249 * The callbacks at CPU hotplug time are calling 250 * dtpm_update_power() which in turns calls update_pd_power(). 251 * 252 * The function update_pd_power() uses the online mask to 253 * figure out the power consumption limits. 254 * 255 * At CPUHP_AP_ONLINE_DYN, the CPU is present in the CPU 256 * online mask when the cpuhp_dtpm_cpu_online function is 257 * called, but the CPU is still in the online mask for the 258 * tear down callback. So the power can not be updated when 259 * the CPU is unplugged. 260 * 261 * At CPUHP_AP_DTPM_CPU_DEAD, the situation is the opposite as 262 * above. The CPU online mask is not up to date when the CPU 263 * is plugged in. 264 * 265 * For this reason, we need to call the online and offline 266 * callbacks at different moments when the CPU online mask is 267 * consistent with the power numbers we want to update. 268 */ 269 ret = cpuhp_setup_state(CPUHP_AP_DTPM_CPU_DEAD, "dtpm_cpu:offline", 270 NULL, cpuhp_dtpm_cpu_offline); 271 if (ret < 0) 272 return ret; 273 274 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "dtpm_cpu:online", 275 cpuhp_dtpm_cpu_online, NULL); 276 if (ret < 0) 277 return ret; 278 279 return 0; 280 } 281 282 static void dtpm_cpu_exit(void) 283 { 284 cpuhp_remove_state_nocalls(CPUHP_AP_ONLINE_DYN); 285 cpuhp_remove_state_nocalls(CPUHP_AP_DTPM_CPU_DEAD); 286 } 287 288 struct dtpm_subsys_ops dtpm_cpu_ops = { 289 .name = KBUILD_MODNAME, 290 .init = dtpm_cpu_init, 291 .exit = dtpm_cpu_exit, 292 .setup = dtpm_cpu_setup, 293 }; 294