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 
to_dtpm_cpu(struct dtpm * dtpm)37 static struct dtpm_cpu *to_dtpm_cpu(struct dtpm *dtpm)
38 {
39 	return container_of(dtpm, struct dtpm_cpu, dtpm);
40 }
41 
set_pd_power_limit(struct dtpm * dtpm,u64 power_limit)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 
scale_pd_power_uw(struct cpumask * pd_mask,u64 power)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 
get_pd_power_uw(struct dtpm * dtpm)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_domain *pd;
92 	struct cpumask *pd_mask;
93 	unsigned long freq;
94 	int i;
95 
96 	pd = em_cpu_get(dtpm_cpu->cpu);
97 
98 	pd_mask = em_span_cpus(pd);
99 
100 	freq = cpufreq_quick_get(dtpm_cpu->cpu);
101 
102 	for (i = 0; i < pd->nr_perf_states; i++) {
103 
104 		if (pd->table[i].frequency < freq)
105 			continue;
106 
107 		return scale_pd_power_uw(pd_mask, pd->table[i].power *
108 					 MICROWATT_PER_MILLIWATT);
109 	}
110 
111 	return 0;
112 }
113 
update_pd_power_uw(struct dtpm * dtpm)114 static int update_pd_power_uw(struct dtpm *dtpm)
115 {
116 	struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
117 	struct em_perf_domain *em = em_cpu_get(dtpm_cpu->cpu);
118 	struct cpumask cpus;
119 	int nr_cpus;
120 
121 	cpumask_and(&cpus, cpu_online_mask, to_cpumask(em->cpus));
122 	nr_cpus = cpumask_weight(&cpus);
123 
124 	dtpm->power_min = em->table[0].power;
125 	dtpm->power_min *= MICROWATT_PER_MILLIWATT;
126 	dtpm->power_min *= nr_cpus;
127 
128 	dtpm->power_max = em->table[em->nr_perf_states - 1].power;
129 	dtpm->power_max *= MICROWATT_PER_MILLIWATT;
130 	dtpm->power_max *= nr_cpus;
131 
132 	return 0;
133 }
134 
pd_release(struct dtpm * dtpm)135 static void pd_release(struct dtpm *dtpm)
136 {
137 	struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
138 	struct cpufreq_policy *policy;
139 
140 	if (freq_qos_request_active(&dtpm_cpu->qos_req))
141 		freq_qos_remove_request(&dtpm_cpu->qos_req);
142 
143 	policy = cpufreq_cpu_get(dtpm_cpu->cpu);
144 	if (policy) {
145 		for_each_cpu(dtpm_cpu->cpu, policy->related_cpus)
146 			per_cpu(dtpm_per_cpu, dtpm_cpu->cpu) = NULL;
147 	}
148 
149 	kfree(dtpm_cpu);
150 }
151 
152 static struct dtpm_ops dtpm_ops = {
153 	.set_power_uw	 = set_pd_power_limit,
154 	.get_power_uw	 = get_pd_power_uw,
155 	.update_power_uw = update_pd_power_uw,
156 	.release	 = pd_release,
157 };
158 
cpuhp_dtpm_cpu_offline(unsigned int cpu)159 static int cpuhp_dtpm_cpu_offline(unsigned int cpu)
160 {
161 	struct dtpm_cpu *dtpm_cpu;
162 
163 	dtpm_cpu = per_cpu(dtpm_per_cpu, cpu);
164 	if (dtpm_cpu)
165 		dtpm_update_power(&dtpm_cpu->dtpm);
166 
167 	return 0;
168 }
169 
cpuhp_dtpm_cpu_online(unsigned int cpu)170 static int cpuhp_dtpm_cpu_online(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 		return dtpm_update_power(&dtpm_cpu->dtpm);
177 
178 	return 0;
179 }
180 
__dtpm_cpu_setup(int cpu,struct dtpm * parent)181 static int __dtpm_cpu_setup(int cpu, struct dtpm *parent)
182 {
183 	struct dtpm_cpu *dtpm_cpu;
184 	struct cpufreq_policy *policy;
185 	struct em_perf_domain *pd;
186 	char name[CPUFREQ_NAME_LEN];
187 	int ret = -ENOMEM;
188 
189 	dtpm_cpu = per_cpu(dtpm_per_cpu, cpu);
190 	if (dtpm_cpu)
191 		return 0;
192 
193 	policy = cpufreq_cpu_get(cpu);
194 	if (!policy)
195 		return 0;
196 
197 	pd = em_cpu_get(cpu);
198 	if (!pd || em_is_artificial(pd))
199 		return -EINVAL;
200 
201 	dtpm_cpu = kzalloc(sizeof(*dtpm_cpu), GFP_KERNEL);
202 	if (!dtpm_cpu)
203 		return -ENOMEM;
204 
205 	dtpm_init(&dtpm_cpu->dtpm, &dtpm_ops);
206 	dtpm_cpu->cpu = cpu;
207 
208 	for_each_cpu(cpu, policy->related_cpus)
209 		per_cpu(dtpm_per_cpu, cpu) = dtpm_cpu;
210 
211 	snprintf(name, sizeof(name), "cpu%d-cpufreq", dtpm_cpu->cpu);
212 
213 	ret = dtpm_register(name, &dtpm_cpu->dtpm, parent);
214 	if (ret)
215 		goto out_kfree_dtpm_cpu;
216 
217 	ret = freq_qos_add_request(&policy->constraints,
218 				   &dtpm_cpu->qos_req, FREQ_QOS_MAX,
219 				   pd->table[pd->nr_perf_states - 1].frequency);
220 	if (ret)
221 		goto out_dtpm_unregister;
222 
223 	return 0;
224 
225 out_dtpm_unregister:
226 	dtpm_unregister(&dtpm_cpu->dtpm);
227 	dtpm_cpu = NULL;
228 
229 out_kfree_dtpm_cpu:
230 	for_each_cpu(cpu, policy->related_cpus)
231 		per_cpu(dtpm_per_cpu, cpu) = NULL;
232 	kfree(dtpm_cpu);
233 
234 	return ret;
235 }
236 
dtpm_cpu_setup(struct dtpm * dtpm,struct device_node * np)237 static int dtpm_cpu_setup(struct dtpm *dtpm, struct device_node *np)
238 {
239 	int cpu;
240 
241 	cpu = of_cpu_node_to_id(np);
242 	if (cpu < 0)
243 		return 0;
244 
245 	return __dtpm_cpu_setup(cpu, dtpm);
246 }
247 
dtpm_cpu_init(void)248 static int dtpm_cpu_init(void)
249 {
250 	int ret;
251 
252 	/*
253 	 * The callbacks at CPU hotplug time are calling
254 	 * dtpm_update_power() which in turns calls update_pd_power().
255 	 *
256 	 * The function update_pd_power() uses the online mask to
257 	 * figure out the power consumption limits.
258 	 *
259 	 * At CPUHP_AP_ONLINE_DYN, the CPU is present in the CPU
260 	 * online mask when the cpuhp_dtpm_cpu_online function is
261 	 * called, but the CPU is still in the online mask for the
262 	 * tear down callback. So the power can not be updated when
263 	 * the CPU is unplugged.
264 	 *
265 	 * At CPUHP_AP_DTPM_CPU_DEAD, the situation is the opposite as
266 	 * above. The CPU online mask is not up to date when the CPU
267 	 * is plugged in.
268 	 *
269 	 * For this reason, we need to call the online and offline
270 	 * callbacks at different moments when the CPU online mask is
271 	 * consistent with the power numbers we want to update.
272 	 */
273 	ret = cpuhp_setup_state(CPUHP_AP_DTPM_CPU_DEAD, "dtpm_cpu:offline",
274 				NULL, cpuhp_dtpm_cpu_offline);
275 	if (ret < 0)
276 		return ret;
277 
278 	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "dtpm_cpu:online",
279 				cpuhp_dtpm_cpu_online, NULL);
280 	if (ret < 0)
281 		return ret;
282 
283 	return 0;
284 }
285 
dtpm_cpu_exit(void)286 static void dtpm_cpu_exit(void)
287 {
288 	cpuhp_remove_state_nocalls(CPUHP_AP_ONLINE_DYN);
289 	cpuhp_remove_state_nocalls(CPUHP_AP_DTPM_CPU_DEAD);
290 }
291 
292 struct dtpm_subsys_ops dtpm_cpu_ops = {
293 	.name = KBUILD_MODNAME,
294 	.init = dtpm_cpu_init,
295 	.exit = dtpm_cpu_exit,
296 	.setup = dtpm_cpu_setup,
297 };
298