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