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