rk3368: add extra name for grf,sgrf,pmu-grf syscon node
[firefly-linux-kernel-4.4.55.git] / drivers / cpuidle / of_idle_states.c
1 /*
2  * OF idle states parsing code.
3  *
4  * Copyright (C) 2014 ARM Ltd.
5  * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
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
12 #include <linux/cpuidle.h>
13 #include <linux/cpumask.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/list_sort.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/slab.h>
21
22 #include "of_idle_states.h"
23
24 struct state_elem {
25         struct list_head list;
26         struct device_node *node;
27         int val;
28 };
29
30 static struct list_head head __initdata = LIST_HEAD_INIT(head);
31
32 static bool __init state_cpu_valid(struct device_node *state_node,
33                                    struct device_node *cpu_node)
34 {
35         int i = 0;
36         struct device_node *cpu_state;
37
38         while ((cpu_state = of_parse_phandle(cpu_node,
39                                              "cpu-idle-states", i++))) {
40                 if (cpu_state && state_node == cpu_state) {
41                         of_node_put(cpu_state);
42                         return true;
43                 }
44                 of_node_put(cpu_state);
45         }
46         return false;
47 }
48
49 static bool __init state_cpus_valid(const cpumask_t *cpus,
50                                     struct device_node *state_node)
51 {
52         int cpu;
53         struct device_node *cpu_node;
54
55         /*
56          * Check if state is valid on driver cpumask cpus
57          */
58         for_each_cpu(cpu, cpus) {
59                 cpu_node = of_get_cpu_node(cpu, NULL);
60
61                 if (!cpu_node) {
62                         pr_err("Missing device node for CPU %d\n", cpu);
63                         return false;
64                 }
65
66                 if (!state_cpu_valid(state_node, cpu_node))
67                         return false;
68         }
69
70         return true;
71 }
72
73 static int __init state_cmp(void *priv, struct list_head *a,
74                             struct list_head *b)
75 {
76         struct state_elem *ela, *elb;
77
78         ela = container_of(a, struct state_elem, list);
79         elb = container_of(b, struct state_elem, list);
80
81         return ela->val - elb->val;
82 }
83
84 static int __init add_state_node(cpumask_t *cpumask,
85                                  struct device_node *state_node)
86 {
87         struct state_elem *el;
88         u32 val;
89
90         pr_debug(" * %s...\n", state_node->full_name);
91
92         if (!state_cpus_valid(cpumask, state_node))
93                 return -EINVAL;
94         /*
95          * Parse just the value required to sort the states.
96          */
97         if (of_property_read_u32(state_node, "min-residency-us",
98                                  &val)) {
99                 pr_debug(" * %s missing min-residency-us property\n",
100                          state_node->full_name);
101                 return -EINVAL;
102         }
103
104         el = kmalloc(sizeof(*el), GFP_KERNEL);
105         if (!el) {
106                 pr_err("%s failed to allocate memory\n", __func__);
107                 return -ENOMEM;
108         }
109
110         el->node = state_node;
111         el->val = val;
112         list_add_tail(&el->list, &head);
113
114         return 0;
115 }
116
117 static void __init init_state_node(struct cpuidle_driver *drv,
118                                    struct device_node *state_node,
119                                    int *cnt)
120 {
121         struct cpuidle_state *idle_state;
122
123         pr_debug(" * %s...\n", state_node->full_name);
124
125         idle_state = &drv->states[*cnt];
126
127         if (of_property_read_u32(state_node, "exit-latency-us",
128                                  &idle_state->exit_latency)) {
129                 pr_debug(" * %s missing exit-latency-us property\n",
130                              state_node->full_name);
131                 return;
132         }
133
134         if (of_property_read_u32(state_node, "min-residency-us",
135                                  &idle_state->target_residency)) {
136                 pr_debug(" * %s missing min-residency-us property\n",
137                              state_node->full_name);
138                 return;
139         }
140         /*
141          * It is unknown to the idle driver if and when the tick_device
142          * loses context when the CPU enters the idle states. To solve
143          * this issue the tick device must be linked to a power domain
144          * so that the idle driver can check on which states the device
145          * loses its context. Current code takes the conservative choice
146          * of defining the idle state as one where the tick device always
147          * loses its context. On platforms where tick device never loses
148          * its context (ie it is not a C3STOP device) this turns into
149          * a nop. On platforms where the tick device does lose context in some
150          * states, this code can be optimized, when power domain specifications
151          * for ARM CPUs are finalized.
152          */
153         idle_state->flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TIMER_STOP;
154
155         strncpy(idle_state->name, state_node->name, CPUIDLE_NAME_LEN);
156         strncpy(idle_state->desc, state_node->name, CPUIDLE_NAME_LEN);
157
158         (*cnt)++;
159 }
160
161 static int __init init_idle_states(struct cpuidle_driver *drv,
162                                    struct device_node *state_nodes[],
163                                    unsigned int start_idx, bool init_nodes)
164 {
165         struct state_elem *el;
166         struct list_head *curr, *tmp;
167         unsigned int cnt = start_idx;
168
169         list_for_each_entry(el, &head, list) {
170                 /*
171                  * Check if the init function has to fill the
172                  * state_nodes array on behalf of the CPUidle driver.
173                  */
174                 if (init_nodes)
175                         state_nodes[cnt] = el->node;
176                 /*
177                  * cnt is updated on return if a state was added.
178                  */
179                 init_state_node(drv, el->node, &cnt);
180
181                 if (cnt == CPUIDLE_STATE_MAX) {
182                         pr_warn("State index reached static CPU idle state limit\n");
183                         break;
184                 }
185         }
186
187         drv->state_count = cnt;
188
189         list_for_each_safe(curr, tmp, &head) {
190                 list_del(curr);
191                 kfree(container_of(curr, struct state_elem, list));
192         }
193
194         /*
195          * If no idle states are detected, return an error and let the idle
196          * driver initialization fail accordingly.
197          */
198         return (cnt > start_idx) ? 0 : -ENODATA;
199 }
200
201 static void __init add_idle_states(struct cpuidle_driver *drv,
202                                    struct device_node *idle_states)
203 {
204         struct device_node *state_node;
205
206         for_each_child_of_node(idle_states, state_node) {
207                 if ((!of_device_is_compatible(state_node, "arm,idle-state"))) {
208                         pr_warn(" * %s: children of /cpus/idle-states must be \"arm,idle-state\" compatible\n",
209                                      state_node->full_name);
210                         continue;
211                 }
212                 /*
213                  * If memory allocation fails, better bail out.
214                  * Initialized nodes are freed at initialization
215                  * completion in of_init_idle_driver().
216                  */
217                 if ((add_state_node(drv->cpumask, state_node) == -ENOMEM))
218                         break;
219         }
220         /*
221          * Sort the states list before initializing the CPUidle driver
222          * states array.
223          */
224         list_sort(NULL, &head, state_cmp);
225 }
226
227 /*
228  * of_init_idle_driver - Parse the DT idle states and initialize the
229  *                       idle driver states array
230  *
231  * @drv:          Pointer to CPU idle driver to be initialized
232  * @state_nodes:  Array of struct device_nodes to be initialized if
233  *                init_nodes == true. Must be sized CPUIDLE_STATE_MAX
234  * @start_idx:    First idle state index to be initialized
235  * @init_nodes:   Boolean to request device nodes initialization
236  *
237  * Returns:
238  *      0 on success
239  *      <0 on failure
240  *
241  *      On success the states array in the cpuidle driver contains
242  *      initialized entries in the states array, starting from index start_idx.
243  *      If init_nodes == true, on success the state_nodes array is initialized
244  *      with idle state DT node pointers, starting from index start_idx,
245  *      in a 1:1 relation with the idle driver states array.
246  */
247 int __init of_init_idle_driver(struct cpuidle_driver *drv,
248                                struct device_node *state_nodes[],
249                                unsigned int start_idx, bool init_nodes)
250 {
251         struct device_node *idle_states_node;
252         int ret;
253
254         if (start_idx >= CPUIDLE_STATE_MAX) {
255                 pr_warn("State index exceeds static CPU idle driver states array size\n");
256                 return -EINVAL;
257         }
258
259         if (WARN(init_nodes && !state_nodes,
260                 "Requested nodes stashing in an invalid nodes container\n"))
261                 return -EINVAL;
262
263         idle_states_node = of_find_node_by_path("/cpus/idle-states");
264         if (!idle_states_node)
265                 return -ENOENT;
266
267         add_idle_states(drv, idle_states_node);
268
269         ret = init_idle_states(drv, state_nodes, start_idx, init_nodes);
270
271         of_node_put(idle_states_node);
272
273         return ret;
274 }