1 /*
2 * Obtain energy cost data from DT and populate relevant scheduler data
3 * structures.
4 *
5 * Copyright (C) 2015 ARM Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #define pr_fmt(fmt) "sched-energy: " fmt
20
21 #include <linux/gfp.h>
22 #include <linux/of.h>
23 #include <linux/printk.h>
24 #include <linux/sched.h>
25 #include <linux/sched/topology.h>
26 #include <linux/sched/energy.h>
27 #include <linux/stddef.h>
28 #include <linux/arch_topology.h>
29 #include <linux/cpu.h>
30 #include <linux/pm_opp.h>
31 #include <linux/platform_device.h>
32
33 #include "sched.h"
34
35 struct sched_group_energy *sge_array[NR_CPUS][NR_SD_LEVELS];
36
free_resources(void)37 static void free_resources(void)
38 {
39 int cpu, sd_level;
40 struct sched_group_energy *sge;
41
42 for_each_possible_cpu(cpu) {
43 for_each_possible_sd_level(sd_level) {
44 sge = sge_array[cpu][sd_level];
45 if (sge) {
46 kfree(sge->cap_states);
47 kfree(sge->idle_states);
48 kfree(sge);
49 }
50 }
51 }
52 }
53 static bool sge_ready;
54 static bool freq_energy_model;
55
check_max_cap_vs_cpu_scale(int cpu,struct sched_group_energy * sge)56 void check_max_cap_vs_cpu_scale(int cpu, struct sched_group_energy *sge)
57 {
58 unsigned long max_cap, cpu_scale;
59
60 max_cap = sge->cap_states[sge->nr_cap_states - 1].cap;
61 cpu_scale = topology_get_cpu_scale(NULL, cpu);
62
63 if (max_cap == cpu_scale)
64 return;
65
66 pr_warn("CPU%d max energy model capacity=%ld != cpu_scale=%ld\n", cpu,
67 max_cap, cpu_scale);
68 }
69
init_sched_energy_costs(void)70 void init_sched_energy_costs(void)
71 {
72 struct device_node *cn, *cp;
73 struct capacity_state *cap_states;
74 struct idle_state *idle_states;
75 struct sched_group_energy *sge;
76 const struct property *prop;
77 int sd_level, i, nstates, cpu;
78 const __be32 *val;
79
80 for_each_possible_cpu(cpu) {
81 cn = of_get_cpu_node(cpu, NULL);
82 if (!cn) {
83 pr_warn("CPU device node missing for CPU %d\n", cpu);
84 return;
85 }
86
87 if (!of_find_property(cn, "sched-energy-costs", NULL)) {
88 pr_warn("CPU device node has no sched-energy-costs\n");
89 return;
90 }
91 /* Check if the energy model contains frequency/power values */
92 if (of_find_property(cn, "freq-energy-model", NULL))
93 freq_energy_model = true;
94
95 for_each_possible_sd_level(sd_level) {
96 cp = of_parse_phandle(cn, "sched-energy-costs", sd_level);
97 if (!cp)
98 break;
99
100 prop = of_find_property(cp, "busy-cost-data", NULL);
101 if (!prop || !prop->value) {
102 pr_warn("No busy-cost data, skipping sched_energy init\n");
103 goto out;
104 }
105
106 sge = kcalloc(1, sizeof(struct sched_group_energy),
107 GFP_NOWAIT);
108
109 nstates = (prop->length / sizeof(u32)) / 2;
110 cap_states = kcalloc(nstates,
111 sizeof(struct capacity_state),
112 GFP_NOWAIT);
113
114 for (i = 0, val = prop->value; i < nstates; i++) {
115 if (freq_energy_model) {
116 /*
117 * Capacity values will be calculated later using
118 * frequency reported by OPP driver and cpu_uarch_scale
119 * values.
120 */
121 cap_states[i].frequency = be32_to_cpup(val++);
122 cap_states[i].cap = 0;
123 } else {
124 cap_states[i].frequency = 0;
125 cap_states[i].cap = be32_to_cpup(val++);
126 }
127 cap_states[i].power = be32_to_cpup(val++);
128 }
129
130 sge->nr_cap_states = nstates;
131 sge->cap_states = cap_states;
132
133 prop = of_find_property(cp, "idle-cost-data", NULL);
134 if (!prop || !prop->value) {
135 pr_warn("No idle-cost data, skipping sched_energy init\n");
136 goto out;
137 }
138
139 nstates = (prop->length / sizeof(u32));
140 idle_states = kcalloc(nstates,
141 sizeof(struct idle_state),
142 GFP_NOWAIT);
143
144 for (i = 0, val = prop->value; i < nstates; i++)
145 idle_states[i].power = be32_to_cpup(val++);
146
147 sge->nr_idle_states = nstates;
148 sge->idle_states = idle_states;
149
150 sge_array[cpu][sd_level] = sge;
151 }
152 if (!freq_energy_model)
153 check_max_cap_vs_cpu_scale(cpu, sge_array[cpu][SD_LEVEL0]);
154 }
155 sge_ready = true;
156 pr_info("Sched-energy-costs installed from DT\n");
157 return;
158
159 out:
160 free_resources();
161 }
162
sched_energy_probe(struct platform_device * pdev)163 static int sched_energy_probe(struct platform_device *pdev)
164 {
165 int cpu;
166 unsigned long *max_frequencies = NULL;
167 int ret;
168
169 if (!sge_ready)
170 return -EPROBE_DEFER;
171
172 if (!energy_aware() || !freq_energy_model)
173 return 0;
174
175 max_frequencies = kmalloc_array(nr_cpu_ids, sizeof(unsigned long),
176 GFP_KERNEL);
177 if (!max_frequencies) {
178 ret = -ENOMEM;
179 goto exit;
180 }
181
182 /*
183 * Find system max possible frequency and max frequencies for each
184 * CPUs.
185 */
186 for_each_possible_cpu(cpu) {
187 struct device *cpu_dev;
188 struct dev_pm_opp *opp;
189
190 cpu_dev = get_cpu_device(cpu);
191 if (IS_ERR_OR_NULL(cpu_dev)) {
192 if (!cpu_dev)
193 ret = -EINVAL;
194 else
195 ret = PTR_ERR(cpu_dev);
196 goto exit;
197 }
198
199 max_frequencies[cpu] = ULONG_MAX;
200
201 opp = dev_pm_opp_find_freq_floor(cpu_dev,
202 &max_frequencies[cpu]);
203 if (IS_ERR_OR_NULL(opp)) {
204 if (!opp || PTR_ERR(opp) == -ENODEV)
205 ret = -EPROBE_DEFER;
206 else
207 ret = PTR_ERR(opp);
208 goto exit;
209 }
210
211 /* Convert HZ to KHZ */
212 max_frequencies[cpu] /= 1000;
213 }
214
215 /* update capacity in energy model */
216 for_each_possible_cpu(cpu) {
217 unsigned long cpu_max_cap;
218 struct sched_group_energy *sge_l0, *sge;
219 cpu_max_cap = topology_get_cpu_scale(NULL, cpu);
220
221 /*
222 * All the cap_states have same frequency table so use
223 * SD_LEVEL0's.
224 */
225 sge_l0 = sge_array[cpu][SD_LEVEL0];
226 if (sge_l0 && sge_l0->nr_cap_states > 0) {
227 int i;
228 int ncapstates = sge_l0->nr_cap_states;
229
230 for (i = 0; i < ncapstates; i++) {
231 int sd_level;
232 unsigned long freq, cap;
233
234 /*
235 * Energy model can contain more frequency
236 * steps than actual for multiple speedbin
237 * support. Ceil the max capacity with actual
238 * one.
239 */
240 freq = min(sge_l0->cap_states[i].frequency,
241 max_frequencies[cpu]);
242 cap = DIV_ROUND_UP(cpu_max_cap * freq,
243 max_frequencies[cpu]);
244
245 for_each_possible_sd_level(sd_level) {
246 sge = sge_array[cpu][sd_level];
247 if (!sge)
248 break;
249 sge->cap_states[i].cap = cap;
250 }
251 dev_dbg(&pdev->dev,
252 "cpu=%d freq=%ld cap=%ld power_d0=%ld\n",
253 cpu, freq, sge_l0->cap_states[i].cap,
254 sge_l0->cap_states[i].power);
255 }
256
257 dev_info(&pdev->dev,
258 "cpu=%d [freq=%ld cap=%ld power_d0=%ld] -> [freq=%ld cap=%ld power_d0=%ld]\n",
259 cpu,
260 sge_l0->cap_states[0].frequency,
261 sge_l0->cap_states[0].cap,
262 sge_l0->cap_states[0].power,
263 sge_l0->cap_states[ncapstates - 1].frequency,
264 sge_l0->cap_states[ncapstates - 1].cap,
265 sge_l0->cap_states[ncapstates - 1].power
266 );
267 }
268 }
269
270 kfree(max_frequencies);
271 dev_info(&pdev->dev, "Sched-energy-costs capacity updated\n");
272 return 0;
273
274 exit:
275 if (ret != -EPROBE_DEFER)
276 dev_err(&pdev->dev, "error=%d\n", ret);
277 kfree(max_frequencies);
278 return ret;
279 }
280
281 static struct platform_driver energy_driver = {
282 .driver = {
283 .name = "sched-energy",
284 },
285 .probe = sched_energy_probe,
286 };
287
288 static struct platform_device energy_device = {
289 .name = "sched-energy",
290 };
291
sched_energy_init(void)292 static int __init sched_energy_init(void)
293 {
294 int ret;
295
296 ret = platform_device_register(&energy_device);
297 if (ret)
298 pr_err("%s device_register failed:%d\n", __func__, ret);
299 ret = platform_driver_register(&energy_driver);
300 if (ret) {
301 pr_err("%s driver_register failed:%d\n", __func__, ret);
302 platform_device_unregister(&energy_device);
303 }
304 return ret;
305 }
306 subsys_initcall(sched_energy_init);
307