blob: ea11a33e7fff4383e6f333e52393e0b984c3549d [file] [log] [blame]
Lorenzo Pieralisi9f14da32014-02-14 14:28:39 +00001/*
2 * DT 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#define pr_fmt(fmt) "DT idle-states: " fmt
13
14#include <linux/cpuidle.h>
15#include <linux/cpumask.h>
16#include <linux/errno.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/of_device.h>
21
22#include "dt_idle_states.h"
23
24static int init_state_node(struct cpuidle_state *idle_state,
25 const struct of_device_id *matches,
26 struct device_node *state_node)
27{
28 int err;
29 const struct of_device_id *match_id;
Lorenzo Pieralisic00bc5d2014-10-15 16:57:34 +010030 const char *desc;
Lorenzo Pieralisi9f14da32014-02-14 14:28:39 +000031
32 match_id = of_match_node(matches, state_node);
33 if (!match_id)
34 return -ENODEV;
35 /*
36 * CPUidle drivers are expected to initialize the const void *data
37 * pointer of the passed in struct of_device_id array to the idle
38 * state enter function.
39 */
40 idle_state->enter = match_id->data;
41
42 err = of_property_read_u32(state_node, "wakeup-latency-us",
43 &idle_state->exit_latency);
44 if (err) {
45 u32 entry_latency, exit_latency;
46
47 err = of_property_read_u32(state_node, "entry-latency-us",
48 &entry_latency);
49 if (err) {
50 pr_debug(" * %s missing entry-latency-us property\n",
51 state_node->full_name);
52 return -EINVAL;
53 }
54
55 err = of_property_read_u32(state_node, "exit-latency-us",
56 &exit_latency);
57 if (err) {
58 pr_debug(" * %s missing exit-latency-us property\n",
59 state_node->full_name);
60 return -EINVAL;
61 }
62 /*
63 * If wakeup-latency-us is missing, default to entry+exit
64 * latencies as defined in idle states bindings
65 */
66 idle_state->exit_latency = entry_latency + exit_latency;
67 }
68
69 err = of_property_read_u32(state_node, "min-residency-us",
70 &idle_state->target_residency);
71 if (err) {
72 pr_debug(" * %s missing min-residency-us property\n",
73 state_node->full_name);
74 return -EINVAL;
75 }
76
Lorenzo Pieralisic00bc5d2014-10-15 16:57:34 +010077 err = of_property_read_string(state_node, "idle-state-name", &desc);
78 if (err)
79 desc = state_node->name;
80
Daniel Lezcanob82b6cc2014-11-12 16:03:50 +010081 idle_state->flags = 0;
Lorenzo Pieralisi9f14da32014-02-14 14:28:39 +000082 if (of_property_read_bool(state_node, "local-timer-stop"))
83 idle_state->flags |= CPUIDLE_FLAG_TIMER_STOP;
84 /*
85 * TODO:
86 * replace with kstrdup and pointer assignment when name
87 * and desc become string pointers
88 */
89 strncpy(idle_state->name, state_node->name, CPUIDLE_NAME_LEN - 1);
Lorenzo Pieralisic00bc5d2014-10-15 16:57:34 +010090 strncpy(idle_state->desc, desc, CPUIDLE_DESC_LEN - 1);
Lorenzo Pieralisi9f14da32014-02-14 14:28:39 +000091 return 0;
92}
93
94/*
95 * Check that the idle state is uniform across all CPUs in the CPUidle driver
96 * cpumask
97 */
98static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
99 const cpumask_t *cpumask)
100{
101 int cpu;
102 struct device_node *cpu_node, *curr_state_node;
103 bool valid = true;
104
105 /*
106 * Compare idle state phandles for index idx on all CPUs in the
107 * CPUidle driver cpumask. Start from next logical cpu following
108 * cpumask_first(cpumask) since that's the CPU state_node was
109 * retrieved from. If a mismatch is found bail out straight
110 * away since we certainly hit a firmware misconfiguration.
111 */
112 for (cpu = cpumask_next(cpumask_first(cpumask), cpumask);
113 cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpumask)) {
114 cpu_node = of_cpu_device_node_get(cpu);
115 curr_state_node = of_parse_phandle(cpu_node, "cpu-idle-states",
116 idx);
117 if (state_node != curr_state_node)
118 valid = false;
119
120 of_node_put(curr_state_node);
121 of_node_put(cpu_node);
122 if (!valid)
123 break;
124 }
125
126 return valid;
127}
128
129/**
130 * dt_init_idle_driver() - Parse the DT idle states and initialize the
131 * idle driver states array
132 * @drv: Pointer to CPU idle driver to be initialized
133 * @matches: Array of of_device_id match structures to search in for
134 * compatible idle state nodes. The data pointer for each valid
135 * struct of_device_id entry in the matches array must point to
136 * a function with the following signature, that corresponds to
137 * the CPUidle state enter function signature:
138 *
139 * int (*)(struct cpuidle_device *dev,
140 * struct cpuidle_driver *drv,
141 * int index);
142 *
143 * @start_idx: First idle state index to be initialized
144 *
145 * If DT idle states are detected and are valid the state count and states
146 * array entries in the cpuidle driver are initialized accordingly starting
147 * from index start_idx.
148 *
149 * Return: number of valid DT idle states parsed, <0 on failure
150 */
151int dt_init_idle_driver(struct cpuidle_driver *drv,
152 const struct of_device_id *matches,
153 unsigned int start_idx)
154{
155 struct cpuidle_state *idle_state;
156 struct device_node *state_node, *cpu_node;
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 * We get the idle states for the first logical cpu in the
165 * driver mask (or cpu_possible_mask if the driver cpumask is not set)
166 * and we check through idle_state_valid() if they are uniform
167 * across CPUs, otherwise we hit a firmware misconfiguration.
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
Christophe Jailletb22081f2017-06-11 14:28:54 +0200177 if (!of_device_is_available(state_node)) {
178 of_node_put(state_node);
Lorenzo Pieralisi97735da02014-10-15 16:50:52 +0100179 continue;
Christophe Jailletb22081f2017-06-11 14:28:54 +0200180 }
Lorenzo Pieralisi97735da02014-10-15 16:50:52 +0100181
Lorenzo Pieralisi9f14da32014-02-14 14:28:39 +0000182 if (!idle_state_valid(state_node, i, cpumask)) {
183 pr_warn("%s idle state not valid, bailing out\n",
184 state_node->full_name);
185 err = -EINVAL;
186 break;
187 }
188
189 if (state_idx == CPUIDLE_STATE_MAX) {
190 pr_warn("State index reached static CPU idle driver states array size\n");
191 break;
192 }
193
194 idle_state = &drv->states[state_idx++];
195 err = init_state_node(idle_state, matches, state_node);
196 if (err) {
197 pr_err("Parsing idle state node %s failed with err %d\n",
198 state_node->full_name, err);
199 err = -EINVAL;
200 break;
201 }
202 of_node_put(state_node);
203 }
204
205 of_node_put(state_node);
206 of_node_put(cpu_node);
207 if (err)
208 return err;
209 /*
210 * Update the driver state count only if some valid DT idle states
211 * were detected
212 */
213 if (i)
214 drv->state_count = state_idx;
215
216 /*
217 * Return the number of present and valid DT idle states, which can
218 * also be 0 on platforms with missing DT idle states or legacy DT
219 * configuration predating the DT idle states bindings.
220 */
221 return i;
222}
223EXPORT_SYMBOL_GPL(dt_init_idle_driver);