2 * OF idle states parsing code.
4 * Copyright (C) 2014 ARM Ltd.
5 * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
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.
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>
20 #include <linux/slab.h>
22 #include "of_idle_states.h"
25 struct list_head list;
26 struct device_node *node;
30 static struct list_head head __initdata = LIST_HEAD_INIT(head);
32 static bool __init state_cpu_valid(struct device_node *state_node,
33 struct device_node *cpu_node)
36 struct device_node *cpu_state;
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);
44 of_node_put(cpu_state);
49 static bool __init state_cpus_valid(const cpumask_t *cpus,
50 struct device_node *state_node)
53 struct device_node *cpu_node;
56 * Check if state is valid on driver cpumask cpus
58 for_each_cpu(cpu, cpus) {
59 cpu_node = of_get_cpu_node(cpu, NULL);
62 pr_err("Missing device node for CPU %d\n", cpu);
66 if (!state_cpu_valid(state_node, cpu_node))
73 static int __init state_cmp(void *priv, struct list_head *a,
76 struct state_elem *ela, *elb;
78 ela = container_of(a, struct state_elem, list);
79 elb = container_of(b, struct state_elem, list);
81 return ela->val - elb->val;
84 static int __init add_state_node(cpumask_t *cpumask,
85 struct device_node *state_node)
87 struct state_elem *el;
90 pr_debug(" * %s...\n", state_node->full_name);
92 if (!state_cpus_valid(cpumask, state_node))
95 * Parse just the value required to sort the states.
97 if (of_property_read_u32(state_node, "min-residency-us",
99 pr_debug(" * %s missing min-residency-us property\n",
100 state_node->full_name);
104 el = kmalloc(sizeof(*el), GFP_KERNEL);
106 pr_err("%s failed to allocate memory\n", __func__);
110 el->node = state_node;
112 list_add_tail(&el->list, &head);
117 static void __init init_state_node(struct cpuidle_driver *drv,
118 struct device_node *state_node,
121 struct cpuidle_state *idle_state;
123 pr_debug(" * %s...\n", state_node->full_name);
125 idle_state = &drv->states[*cnt];
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);
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);
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.
153 idle_state->flags = CPUIDLE_FLAG_TIME_VALID | CPUIDLE_FLAG_TIMER_STOP;
155 strncpy(idle_state->name, state_node->name, CPUIDLE_NAME_LEN);
156 strncpy(idle_state->desc, state_node->name, CPUIDLE_NAME_LEN);
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)
165 struct state_elem *el;
166 struct list_head *curr, *tmp;
167 unsigned int cnt = start_idx;
169 list_for_each_entry(el, &head, list) {
171 * Check if the init function has to fill the
172 * state_nodes array on behalf of the CPUidle driver.
175 state_nodes[cnt] = el->node;
177 * cnt is updated on return if a state was added.
179 init_state_node(drv, el->node, &cnt);
181 if (cnt == CPUIDLE_STATE_MAX) {
182 pr_warn("State index reached static CPU idle state limit\n");
187 drv->state_count = cnt;
189 list_for_each_safe(curr, tmp, &head) {
191 kfree(container_of(curr, struct state_elem, list));
195 * If no idle states are detected, return an error and let the idle
196 * driver initialization fail accordingly.
198 return (cnt > start_idx) ? 0 : -ENODATA;
201 static void __init add_idle_states(struct cpuidle_driver *drv,
202 struct device_node *idle_states)
204 struct device_node *state_node;
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);
213 * If memory allocation fails, better bail out.
214 * Initialized nodes are freed at initialization
215 * completion in of_init_idle_driver().
217 if ((add_state_node(drv->cpumask, state_node) == -ENOMEM))
221 * Sort the states list before initializing the CPUidle driver
224 list_sort(NULL, &head, state_cmp);
228 * of_init_idle_driver - Parse the DT idle states and initialize the
229 * idle driver states array
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
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.
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)
251 struct device_node *idle_states_node;
254 if (start_idx >= CPUIDLE_STATE_MAX) {
255 pr_warn("State index exceeds static CPU idle driver states array size\n");
259 if (WARN(init_nodes && !state_nodes,
260 "Requested nodes stashing in an invalid nodes container\n"))
263 idle_states_node = of_find_node_by_path("/cpus/idle-states");
264 if (!idle_states_node)
267 add_idle_states(drv, idle_states_node);
269 ret = init_idle_states(drv, state_nodes, start_idx, init_nodes);
271 of_node_put(idle_states_node);