xref: /openbmc/linux/drivers/powercap/dtpm_cpu.c (revision d2cdc6ad)
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/pm_qos.h>
25 #include <linux/slab.h>
26 #include <linux/units.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 cpumask cpus;
46 	unsigned long freq;
47 	u64 power;
48 	int i, nr_cpus;
49 
50 	cpumask_and(&cpus, cpu_online_mask, to_cpumask(pd->cpus));
51 	nr_cpus = cpumask_weight(&cpus);
52 
53 	for (i = 0; i < pd->nr_perf_states; i++) {
54 
55 		power = pd->table[i].power * MICROWATT_PER_MILLIWATT * nr_cpus;
56 
57 		if (power > power_limit)
58 			break;
59 	}
60 
61 	freq = pd->table[i - 1].frequency;
62 
63 	freq_qos_update_request(&dtpm_cpu->qos_req, freq);
64 
65 	power_limit = pd->table[i - 1].power *
66 		MICROWATT_PER_MILLIWATT * nr_cpus;
67 
68 	return power_limit;
69 }
70 
71 static u64 get_pd_power_uw(struct dtpm *dtpm)
72 {
73 	struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
74 	struct em_perf_domain *pd;
75 	struct cpumask cpus;
76 	unsigned long freq;
77 	int i, nr_cpus;
78 
79 	pd = em_cpu_get(dtpm_cpu->cpu);
80 	freq = cpufreq_quick_get(dtpm_cpu->cpu);
81 
82 	cpumask_and(&cpus, cpu_online_mask, to_cpumask(pd->cpus));
83 	nr_cpus = cpumask_weight(&cpus);
84 
85 	for (i = 0; i < pd->nr_perf_states; i++) {
86 
87 		if (pd->table[i].frequency < freq)
88 			continue;
89 
90 		return pd->table[i].power *
91 			MICROWATT_PER_MILLIWATT * nr_cpus;
92 	}
93 
94 	return 0;
95 }
96 
97 static int update_pd_power_uw(struct dtpm *dtpm)
98 {
99 	struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
100 	struct em_perf_domain *em = em_cpu_get(dtpm_cpu->cpu);
101 	struct cpumask cpus;
102 	int nr_cpus;
103 
104 	cpumask_and(&cpus, cpu_online_mask, to_cpumask(em->cpus));
105 	nr_cpus = cpumask_weight(&cpus);
106 
107 	dtpm->power_min = em->table[0].power;
108 	dtpm->power_min *= MICROWATT_PER_MILLIWATT;
109 	dtpm->power_min *= nr_cpus;
110 
111 	dtpm->power_max = em->table[em->nr_perf_states - 1].power;
112 	dtpm->power_max *= MICROWATT_PER_MILLIWATT;
113 	dtpm->power_max *= nr_cpus;
114 
115 	return 0;
116 }
117 
118 static void pd_release(struct dtpm *dtpm)
119 {
120 	struct dtpm_cpu *dtpm_cpu = to_dtpm_cpu(dtpm);
121 
122 	if (freq_qos_request_active(&dtpm_cpu->qos_req))
123 		freq_qos_remove_request(&dtpm_cpu->qos_req);
124 
125 	kfree(dtpm_cpu);
126 }
127 
128 static struct dtpm_ops dtpm_ops = {
129 	.set_power_uw	 = set_pd_power_limit,
130 	.get_power_uw	 = get_pd_power_uw,
131 	.update_power_uw = update_pd_power_uw,
132 	.release	 = pd_release,
133 };
134 
135 static int cpuhp_dtpm_cpu_offline(unsigned int cpu)
136 {
137 	struct em_perf_domain *pd;
138 	struct dtpm_cpu *dtpm_cpu;
139 
140 	pd = em_cpu_get(cpu);
141 	if (!pd)
142 		return -EINVAL;
143 
144 	dtpm_cpu = per_cpu(dtpm_per_cpu, cpu);
145 
146 	return dtpm_update_power(&dtpm_cpu->dtpm);
147 }
148 
149 static int cpuhp_dtpm_cpu_online(unsigned int cpu)
150 {
151 	struct dtpm_cpu *dtpm_cpu;
152 	struct cpufreq_policy *policy;
153 	struct em_perf_domain *pd;
154 	char name[CPUFREQ_NAME_LEN];
155 	int ret = -ENOMEM;
156 
157 	policy = cpufreq_cpu_get(cpu);
158 	if (!policy)
159 		return 0;
160 
161 	pd = em_cpu_get(cpu);
162 	if (!pd)
163 		return -EINVAL;
164 
165 	dtpm_cpu = per_cpu(dtpm_per_cpu, cpu);
166 	if (dtpm_cpu)
167 		return dtpm_update_power(&dtpm_cpu->dtpm);
168 
169 	dtpm_cpu = kzalloc(sizeof(*dtpm_cpu), GFP_KERNEL);
170 	if (!dtpm_cpu)
171 		return -ENOMEM;
172 
173 	dtpm_init(&dtpm_cpu->dtpm, &dtpm_ops);
174 	dtpm_cpu->cpu = cpu;
175 
176 	for_each_cpu(cpu, policy->related_cpus)
177 		per_cpu(dtpm_per_cpu, cpu) = dtpm_cpu;
178 
179 	snprintf(name, sizeof(name), "cpu%d-cpufreq", dtpm_cpu->cpu);
180 
181 	ret = dtpm_register(name, &dtpm_cpu->dtpm, NULL);
182 	if (ret)
183 		goto out_kfree_dtpm_cpu;
184 
185 	ret = freq_qos_add_request(&policy->constraints,
186 				   &dtpm_cpu->qos_req, FREQ_QOS_MAX,
187 				   pd->table[pd->nr_perf_states - 1].frequency);
188 	if (ret)
189 		goto out_dtpm_unregister;
190 
191 	return 0;
192 
193 out_dtpm_unregister:
194 	dtpm_unregister(&dtpm_cpu->dtpm);
195 	dtpm_cpu = NULL;
196 
197 out_kfree_dtpm_cpu:
198 	for_each_cpu(cpu, policy->related_cpus)
199 		per_cpu(dtpm_per_cpu, cpu) = NULL;
200 	kfree(dtpm_cpu);
201 
202 	return ret;
203 }
204 
205 static int __init dtpm_cpu_init(void)
206 {
207 	int ret;
208 
209 	/*
210 	 * The callbacks at CPU hotplug time are calling
211 	 * dtpm_update_power() which in turns calls update_pd_power().
212 	 *
213 	 * The function update_pd_power() uses the online mask to
214 	 * figure out the power consumption limits.
215 	 *
216 	 * At CPUHP_AP_ONLINE_DYN, the CPU is present in the CPU
217 	 * online mask when the cpuhp_dtpm_cpu_online function is
218 	 * called, but the CPU is still in the online mask for the
219 	 * tear down callback. So the power can not be updated when
220 	 * the CPU is unplugged.
221 	 *
222 	 * At CPUHP_AP_DTPM_CPU_DEAD, the situation is the opposite as
223 	 * above. The CPU online mask is not up to date when the CPU
224 	 * is plugged in.
225 	 *
226 	 * For this reason, we need to call the online and offline
227 	 * callbacks at different moments when the CPU online mask is
228 	 * consistent with the power numbers we want to update.
229 	 */
230 	ret = cpuhp_setup_state(CPUHP_AP_DTPM_CPU_DEAD, "dtpm_cpu:offline",
231 				NULL, cpuhp_dtpm_cpu_offline);
232 	if (ret < 0)
233 		return ret;
234 
235 	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "dtpm_cpu:online",
236 				cpuhp_dtpm_cpu_online, NULL);
237 	if (ret < 0)
238 		return ret;
239 
240 	return 0;
241 }
242 
243 DTPM_DECLARE(dtpm_cpu, dtpm_cpu_init);
244