2 * Obtain energy cost data from DT and populate relevant scheduler data
5 * Copyright (C) 2015 ARM Ltd.
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.
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.
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/>.
19 #define pr_fmt(fmt) "sched-energy: " fmt
23 #include <linux/gfp.h>
25 #include <linux/printk.h>
26 #include <linux/sched.h>
27 #include <linux/sched_energy.h>
28 #include <linux/stddef.h>
30 struct sched_group_energy *sge_array[NR_CPUS][NR_SD_LEVELS];
32 static void free_resources(void)
35 struct sched_group_energy *sge;
37 for_each_possible_cpu(cpu) {
38 for_each_possible_sd_level(sd_level) {
39 sge = sge_array[cpu][sd_level];
41 kfree(sge->cap_states);
42 kfree(sge->idle_states);
49 void init_sched_energy_costs(void)
51 struct device_node *cn, *cp;
52 struct capacity_state *cap_states;
53 struct idle_state *idle_states;
54 struct sched_group_energy *sge;
55 const struct property *prop;
56 int sd_level, i, nstates, cpu;
59 for_each_possible_cpu(cpu) {
60 cn = of_get_cpu_node(cpu, NULL);
62 pr_warn("CPU device node missing for CPU %d\n", cpu);
66 if (!of_find_property(cn, "sched-energy-costs", NULL)) {
67 pr_warn("CPU device node has no sched-energy-costs\n");
71 for_each_possible_sd_level(sd_level) {
72 cp = of_parse_phandle(cn, "sched-energy-costs", sd_level);
76 prop = of_find_property(cp, "busy-cost-data", NULL);
77 if (!prop || !prop->value) {
78 pr_warn("No busy-cost data, skipping sched_energy init\n");
82 sge = kcalloc(1, sizeof(struct sched_group_energy),
85 nstates = (prop->length / sizeof(u32)) / 2;
86 cap_states = kcalloc(nstates,
87 sizeof(struct capacity_state),
90 for (i = 0, val = prop->value; i < nstates; i++) {
91 cap_states[i].cap = be32_to_cpup(val++);
92 cap_states[i].power = be32_to_cpup(val++);
95 sge->nr_cap_states = nstates;
96 sge->cap_states = cap_states;
98 prop = of_find_property(cp, "idle-cost-data", NULL);
99 if (!prop || !prop->value) {
100 pr_warn("No idle-cost data, skipping sched_energy init\n");
104 nstates = (prop->length / sizeof(u32));
105 idle_states = kcalloc(nstates,
106 sizeof(struct idle_state),
109 for (i = 0, val = prop->value; i < nstates; i++)
110 idle_states[i].power = be32_to_cpup(val++);
112 sge->nr_idle_states = nstates;
113 sge->idle_states = idle_states;
115 sge_array[cpu][sd_level] = sge;
119 pr_info("Sched-energy-costs installed from DT\n");