This source file includes following definitions.
- init_state_node
- idle_state_valid
- dt_init_idle_driver
1
2
3
4
5
6
7
8
9 #define pr_fmt(fmt) "DT idle-states: " fmt
10
11 #include <linux/cpuidle.h>
12 #include <linux/cpumask.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/of_device.h>
18
19 #include "dt_idle_states.h"
20
21 static int init_state_node(struct cpuidle_state *idle_state,
22 const struct of_device_id *match_id,
23 struct device_node *state_node)
24 {
25 int err;
26 const char *desc;
27
28
29
30
31
32
33 idle_state->enter = match_id->data;
34
35
36
37
38
39 idle_state->enter_s2idle = match_id->data;
40
41 err = of_property_read_u32(state_node, "wakeup-latency-us",
42 &idle_state->exit_latency);
43 if (err) {
44 u32 entry_latency, exit_latency;
45
46 err = of_property_read_u32(state_node, "entry-latency-us",
47 &entry_latency);
48 if (err) {
49 pr_debug(" * %pOF missing entry-latency-us property\n",
50 state_node);
51 return -EINVAL;
52 }
53
54 err = of_property_read_u32(state_node, "exit-latency-us",
55 &exit_latency);
56 if (err) {
57 pr_debug(" * %pOF missing exit-latency-us property\n",
58 state_node);
59 return -EINVAL;
60 }
61
62
63
64
65 idle_state->exit_latency = entry_latency + exit_latency;
66 }
67
68 err = of_property_read_u32(state_node, "min-residency-us",
69 &idle_state->target_residency);
70 if (err) {
71 pr_debug(" * %pOF missing min-residency-us property\n",
72 state_node);
73 return -EINVAL;
74 }
75
76 err = of_property_read_string(state_node, "idle-state-name", &desc);
77 if (err)
78 desc = state_node->name;
79
80 idle_state->flags = 0;
81 if (of_property_read_bool(state_node, "local-timer-stop"))
82 idle_state->flags |= CPUIDLE_FLAG_TIMER_STOP;
83
84
85
86
87
88 strncpy(idle_state->name, state_node->name, CPUIDLE_NAME_LEN - 1);
89 strncpy(idle_state->desc, desc, CPUIDLE_DESC_LEN - 1);
90 return 0;
91 }
92
93
94
95
96
97 static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
98 const cpumask_t *cpumask)
99 {
100 int cpu;
101 struct device_node *cpu_node, *curr_state_node;
102 bool valid = true;
103
104
105
106
107
108
109
110
111 for (cpu = cpumask_next(cpumask_first(cpumask), cpumask);
112 cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpumask)) {
113 cpu_node = of_cpu_device_node_get(cpu);
114 curr_state_node = of_parse_phandle(cpu_node, "cpu-idle-states",
115 idx);
116 if (state_node != curr_state_node)
117 valid = false;
118
119 of_node_put(curr_state_node);
120 of_node_put(cpu_node);
121 if (!valid)
122 break;
123 }
124
125 return valid;
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 int dt_init_idle_driver(struct cpuidle_driver *drv,
151 const struct of_device_id *matches,
152 unsigned int start_idx)
153 {
154 struct cpuidle_state *idle_state;
155 struct device_node *state_node, *cpu_node;
156 const struct of_device_id *match_id;
157 int i, err = 0;
158 const cpumask_t *cpumask;
159 unsigned int state_idx = start_idx;
160
161 if (state_idx >= CPUIDLE_STATE_MAX)
162 return -EINVAL;
163
164
165
166
167
168
169 cpumask = drv->cpumask ? : cpu_possible_mask;
170 cpu_node = of_cpu_device_node_get(cpumask_first(cpumask));
171
172 for (i = 0; ; i++) {
173 state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i);
174 if (!state_node)
175 break;
176
177 match_id = of_match_node(matches, state_node);
178 if (!match_id) {
179 err = -ENODEV;
180 break;
181 }
182
183 if (!of_device_is_available(state_node)) {
184 of_node_put(state_node);
185 continue;
186 }
187
188 if (!idle_state_valid(state_node, i, cpumask)) {
189 pr_warn("%pOF idle state not valid, bailing out\n",
190 state_node);
191 err = -EINVAL;
192 break;
193 }
194
195 if (state_idx == CPUIDLE_STATE_MAX) {
196 pr_warn("State index reached static CPU idle driver states array size\n");
197 break;
198 }
199
200 idle_state = &drv->states[state_idx++];
201 err = init_state_node(idle_state, match_id, state_node);
202 if (err) {
203 pr_err("Parsing idle state node %pOF failed with err %d\n",
204 state_node, err);
205 err = -EINVAL;
206 break;
207 }
208 of_node_put(state_node);
209 }
210
211 of_node_put(state_node);
212 of_node_put(cpu_node);
213 if (err)
214 return err;
215
216
217
218
219 if (i)
220 drv->state_count = state_idx;
221
222
223
224
225
226
227 return i;
228 }
229 EXPORT_SYMBOL_GPL(dt_init_idle_driver);