Merge branch 'linux-linaro-lsk-v4.4' into linux-linaro-lsk-v4.4-android
[firefly-linux-kernel-4.4.55.git] / kernel / sched / energy.c
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 #define DEBUG
22
23 #include <linux/gfp.h>
24 #include <linux/of.h>
25 #include <linux/printk.h>
26 #include <linux/sched.h>
27 #include <linux/sched_energy.h>
28 #include <linux/stddef.h>
29
30 struct sched_group_energy *sge_array[NR_CPUS][NR_SD_LEVELS];
31
32 static void free_resources(void)
33 {
34         int cpu, sd_level;
35         struct sched_group_energy *sge;
36
37         for_each_possible_cpu(cpu) {
38                 for_each_possible_sd_level(sd_level) {
39                         sge = sge_array[cpu][sd_level];
40                         if (sge) {
41                                 kfree(sge->cap_states);
42                                 kfree(sge->idle_states);
43                                 kfree(sge);
44                         }
45                 }
46         }
47 }
48
49 void init_sched_energy_costs(void)
50 {
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;
57         const __be32 *val;
58
59         for_each_possible_cpu(cpu) {
60                 cn = of_get_cpu_node(cpu, NULL);
61                 if (!cn) {
62                         pr_warn("CPU device node missing for CPU %d\n", cpu);
63                         return;
64                 }
65
66                 if (!of_find_property(cn, "sched-energy-costs", NULL)) {
67                         pr_warn("CPU device node has no sched-energy-costs\n");
68                         return;
69                 }
70
71                 for_each_possible_sd_level(sd_level) {
72                         cp = of_parse_phandle(cn, "sched-energy-costs", sd_level);
73                         if (!cp)
74                                 break;
75
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");
79                                 goto out;
80                         }
81
82                         sge = kcalloc(1, sizeof(struct sched_group_energy),
83                                       GFP_NOWAIT);
84
85                         nstates = (prop->length / sizeof(u32)) / 2;
86                         cap_states = kcalloc(nstates,
87                                              sizeof(struct capacity_state),
88                                              GFP_NOWAIT);
89
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++);
93                         }
94
95                         sge->nr_cap_states = nstates;
96                         sge->cap_states = cap_states;
97
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");
101                                 goto out;
102                         }
103
104                         nstates = (prop->length / sizeof(u32));
105                         idle_states = kcalloc(nstates,
106                                               sizeof(struct idle_state),
107                                               GFP_NOWAIT);
108
109                         for (i = 0, val = prop->value; i < nstates; i++)
110                                 idle_states[i].power = be32_to_cpup(val++);
111
112                         sge->nr_idle_states = nstates;
113                         sge->idle_states = idle_states;
114
115                         sge_array[cpu][sd_level] = sge;
116                 }
117         }
118
119         pr_info("Sched-energy-costs installed from DT\n");
120         return;
121
122 out:
123         free_resources();
124 }