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