1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2020 Linaro Limited
5 * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
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.
11 * The association between the power and the performance state, allows
12 * to set the power of the CPU at the OPP granularity.
14 * The CPU hotplug is supported and the power numbers will be updated
15 * if a CPU is hot plugged / unplugged.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
25 #include <linux/pm_qos.h>
26 #include <linux/slab.h>
27 #include <linux/units.h>
31 struct freq_qos_request qos_req;
35 static DEFINE_PER_CPU(struct dtpm_cpu *, dtpm_per_cpu);
37 static struct dtpm_cpu *to_dtpm_cpu(struct dtpm *dtpm)
39 return container_of(dtpm, struct dtpm_cpu, dtpm);
42 static u64 set_pd_power_limit(struct dtpm *dtpm, u64 power_limit)
44 struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
45 struct em_perf_domain *pd = em_cpu_get(dtpm_cpu->cpu);
51 cpumask_and(&cpus, cpu_online_mask, to_cpumask(pd->cpus));
52 nr_cpus = cpumask_weight(&cpus);
54 for (i = 0; i < pd->nr_perf_states; i++) {
56 power = pd->table[i].power * MICROWATT_PER_MILLIWATT * nr_cpus;
58 if (power > power_limit)
62 freq = pd->table[i - 1].frequency;
64 freq_qos_update_request(&dtpm_cpu->qos_req, freq);
66 power_limit = pd->table[i - 1].power *
67 MICROWATT_PER_MILLIWATT * nr_cpus;
72 static u64 scale_pd_power_uw(struct cpumask *pd_mask, u64 power)
74 unsigned long max, sum_util = 0;
78 * The capacity is the same for all CPUs belonging to
79 * the same perf domain.
81 max = arch_scale_cpu_capacity(cpumask_first(pd_mask));
83 for_each_cpu_and(cpu, pd_mask, cpu_online_mask)
84 sum_util += sched_cpu_util(cpu);
86 return (power * ((sum_util << 10) / max)) >> 10;
89 static u64 get_pd_power_uw(struct dtpm *dtpm)
91 struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
92 struct em_perf_domain *pd;
93 struct cpumask *pd_mask;
97 pd = em_cpu_get(dtpm_cpu->cpu);
99 pd_mask = em_span_cpus(pd);
101 freq = cpufreq_quick_get(dtpm_cpu->cpu);
103 for (i = 0; i < pd->nr_perf_states; i++) {
105 if (pd->table[i].frequency < freq)
108 return scale_pd_power_uw(pd_mask, pd->table[i].power *
109 MICROWATT_PER_MILLIWATT);
115 static int update_pd_power_uw(struct dtpm *dtpm)
117 struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
118 struct em_perf_domain *em = em_cpu_get(dtpm_cpu->cpu);
122 cpumask_and(&cpus, cpu_online_mask, to_cpumask(em->cpus));
123 nr_cpus = cpumask_weight(&cpus);
125 dtpm->power_min = em->table[0].power;
126 dtpm->power_min *= MICROWATT_PER_MILLIWATT;
127 dtpm->power_min *= nr_cpus;
129 dtpm->power_max = em->table[em->nr_perf_states - 1].power;
130 dtpm->power_max *= MICROWATT_PER_MILLIWATT;
131 dtpm->power_max *= nr_cpus;
136 static void pd_release(struct dtpm *dtpm)
138 struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
139 struct cpufreq_policy *policy;
141 if (freq_qos_request_active(&dtpm_cpu->qos_req))
142 freq_qos_remove_request(&dtpm_cpu->qos_req);
144 policy = cpufreq_cpu_get(dtpm_cpu->cpu);
146 for_each_cpu(dtpm_cpu->cpu, policy->related_cpus)
147 per_cpu(dtpm_per_cpu, dtpm_cpu->cpu) = NULL;
153 static struct dtpm_ops dtpm_ops = {
154 .set_power_uw = set_pd_power_limit,
155 .get_power_uw = get_pd_power_uw,
156 .update_power_uw = update_pd_power_uw,
157 .release = pd_release,
160 static int cpuhp_dtpm_cpu_offline(unsigned int cpu)
162 struct dtpm_cpu *dtpm_cpu;
164 dtpm_cpu = per_cpu(dtpm_per_cpu, cpu);
166 dtpm_update_power(&dtpm_cpu->dtpm);
171 static int cpuhp_dtpm_cpu_online(unsigned int cpu)
173 struct dtpm_cpu *dtpm_cpu;
175 dtpm_cpu = per_cpu(dtpm_per_cpu, cpu);
177 return dtpm_update_power(&dtpm_cpu->dtpm);
182 static int __dtpm_cpu_setup(int cpu, struct dtpm *parent)
184 struct dtpm_cpu *dtpm_cpu;
185 struct cpufreq_policy *policy;
186 struct em_perf_domain *pd;
187 char name[CPUFREQ_NAME_LEN];
190 dtpm_cpu = per_cpu(dtpm_per_cpu, cpu);
194 policy = cpufreq_cpu_get(cpu);
198 pd = em_cpu_get(cpu);
199 if (!pd || em_is_artificial(pd))
202 dtpm_cpu = kzalloc(sizeof(*dtpm_cpu), GFP_KERNEL);
206 dtpm_init(&dtpm_cpu->dtpm, &dtpm_ops);
209 for_each_cpu(cpu, policy->related_cpus)
210 per_cpu(dtpm_per_cpu, cpu) = dtpm_cpu;
212 snprintf(name, sizeof(name), "cpu%d-cpufreq", dtpm_cpu->cpu);
214 ret = dtpm_register(name, &dtpm_cpu->dtpm, parent);
216 goto out_kfree_dtpm_cpu;
218 ret = freq_qos_add_request(&policy->constraints,
219 &dtpm_cpu->qos_req, FREQ_QOS_MAX,
220 pd->table[pd->nr_perf_states - 1].frequency);
222 goto out_dtpm_unregister;
227 dtpm_unregister(&dtpm_cpu->dtpm);
231 for_each_cpu(cpu, policy->related_cpus)
232 per_cpu(dtpm_per_cpu, cpu) = NULL;
238 static int dtpm_cpu_setup(struct dtpm *dtpm, struct device_node *np)
242 cpu = of_cpu_node_to_id(np);
246 return __dtpm_cpu_setup(cpu, dtpm);
249 static int dtpm_cpu_init(void)
254 * The callbacks at CPU hotplug time are calling
255 * dtpm_update_power() which in turns calls update_pd_power().
257 * The function update_pd_power() uses the online mask to
258 * figure out the power consumption limits.
260 * At CPUHP_AP_ONLINE_DYN, the CPU is present in the CPU
261 * online mask when the cpuhp_dtpm_cpu_online function is
262 * called, but the CPU is still in the online mask for the
263 * tear down callback. So the power can not be updated when
264 * the CPU is unplugged.
266 * At CPUHP_AP_DTPM_CPU_DEAD, the situation is the opposite as
267 * above. The CPU online mask is not up to date when the CPU
270 * For this reason, we need to call the online and offline
271 * callbacks at different moments when the CPU online mask is
272 * consistent with the power numbers we want to update.
274 ret = cpuhp_setup_state(CPUHP_AP_DTPM_CPU_DEAD, "dtpm_cpu:offline",
275 NULL, cpuhp_dtpm_cpu_offline);
279 ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "dtpm_cpu:online",
280 cpuhp_dtpm_cpu_online, NULL);
287 static void dtpm_cpu_exit(void)
289 cpuhp_remove_state_nocalls(CPUHP_AP_ONLINE_DYN);
290 cpuhp_remove_state_nocalls(CPUHP_AP_DTPM_CPU_DEAD);
293 struct dtpm_subsys_ops dtpm_cpu_ops = {
294 .name = KBUILD_MODNAME,
295 .init = dtpm_cpu_init,
296 .exit = dtpm_cpu_exit,
297 .setup = dtpm_cpu_setup,