1 /* 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 24 static int init_state_node(struct cpuidle_state *idle_state, 25 const struct of_device_id *match_id, 26 struct device_node *state_node) 27 { 28 int err; 29 const char *desc; 30 31 /* 32 * CPUidle drivers are expected to initialize the const void *data 33 * pointer of the passed in struct of_device_id array to the idle 34 * state enter function. 35 */ 36 idle_state->enter = match_id->data; 37 /* 38 * Since this is not a "coupled" state, it's safe to assume interrupts 39 * won't be enabled when it exits allowing the tick to be frozen 40 * safely. So enter() can be also enter_s2idle() callback. 41 */ 42 idle_state->enter_s2idle = match_id->data; 43 44 err = of_property_read_u32(state_node, "wakeup-latency-us", 45 &idle_state->exit_latency); 46 if (err) { 47 u32 entry_latency, exit_latency; 48 49 err = of_property_read_u32(state_node, "entry-latency-us", 50 &entry_latency); 51 if (err) { 52 pr_debug(" * %pOF missing entry-latency-us property\n", 53 state_node); 54 return -EINVAL; 55 } 56 57 err = of_property_read_u32(state_node, "exit-latency-us", 58 &exit_latency); 59 if (err) { 60 pr_debug(" * %pOF missing exit-latency-us property\n", 61 state_node); 62 return -EINVAL; 63 } 64 /* 65 * If wakeup-latency-us is missing, default to entry+exit 66 * latencies as defined in idle states bindings 67 */ 68 idle_state->exit_latency = entry_latency + exit_latency; 69 } 70 71 err = of_property_read_u32(state_node, "min-residency-us", 72 &idle_state->target_residency); 73 if (err) { 74 pr_debug(" * %pOF missing min-residency-us property\n", 75 state_node); 76 return -EINVAL; 77 } 78 79 err = of_property_read_string(state_node, "idle-state-name", &desc); 80 if (err) 81 desc = state_node->name; 82 83 idle_state->flags = 0; 84 if (of_property_read_bool(state_node, "local-timer-stop")) 85 idle_state->flags |= CPUIDLE_FLAG_TIMER_STOP; 86 /* 87 * TODO: 88 * replace with kstrdup and pointer assignment when name 89 * and desc become string pointers 90 */ 91 strncpy(idle_state->name, state_node->name, CPUIDLE_NAME_LEN - 1); 92 strncpy(idle_state->desc, desc, CPUIDLE_DESC_LEN - 1); 93 return 0; 94 } 95 96 /* 97 * Check that the idle state is uniform across all CPUs in the CPUidle driver 98 * cpumask 99 */ 100 static bool idle_state_valid(struct device_node *state_node, unsigned int idx, 101 const cpumask_t *cpumask) 102 { 103 int cpu; 104 struct device_node *cpu_node, *curr_state_node; 105 bool valid = true; 106 107 /* 108 * Compare idle state phandles for index idx on all CPUs in the 109 * CPUidle driver cpumask. Start from next logical cpu following 110 * cpumask_first(cpumask) since that's the CPU state_node was 111 * retrieved from. If a mismatch is found bail out straight 112 * away since we certainly hit a firmware misconfiguration. 113 */ 114 for (cpu = cpumask_next(cpumask_first(cpumask), cpumask); 115 cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpumask)) { 116 cpu_node = of_cpu_device_node_get(cpu); 117 curr_state_node = of_parse_phandle(cpu_node, "cpu-idle-states", 118 idx); 119 if (state_node != curr_state_node) 120 valid = false; 121 122 of_node_put(curr_state_node); 123 of_node_put(cpu_node); 124 if (!valid) 125 break; 126 } 127 128 return valid; 129 } 130 131 /** 132 * dt_init_idle_driver() - Parse the DT idle states and initialize the 133 * idle driver states array 134 * @drv: Pointer to CPU idle driver to be initialized 135 * @matches: Array of of_device_id match structures to search in for 136 * compatible idle state nodes. The data pointer for each valid 137 * struct of_device_id entry in the matches array must point to 138 * a function with the following signature, that corresponds to 139 * the CPUidle state enter function signature: 140 * 141 * int (*)(struct cpuidle_device *dev, 142 * struct cpuidle_driver *drv, 143 * int index); 144 * 145 * @start_idx: First idle state index to be initialized 146 * 147 * If DT idle states are detected and are valid the state count and states 148 * array entries in the cpuidle driver are initialized accordingly starting 149 * from index start_idx. 150 * 151 * Return: number of valid DT idle states parsed, <0 on failure 152 */ 153 int dt_init_idle_driver(struct cpuidle_driver *drv, 154 const struct of_device_id *matches, 155 unsigned int start_idx) 156 { 157 struct cpuidle_state *idle_state; 158 struct device_node *state_node, *cpu_node; 159 const struct of_device_id *match_id; 160 int i, err = 0; 161 const cpumask_t *cpumask; 162 unsigned int state_idx = start_idx; 163 164 if (state_idx >= CPUIDLE_STATE_MAX) 165 return -EINVAL; 166 /* 167 * We get the idle states for the first logical cpu in the 168 * driver mask (or cpu_possible_mask if the driver cpumask is not set) 169 * and we check through idle_state_valid() if they are uniform 170 * across CPUs, otherwise we hit a firmware misconfiguration. 171 */ 172 cpumask = drv->cpumask ? : cpu_possible_mask; 173 cpu_node = of_cpu_device_node_get(cpumask_first(cpumask)); 174 175 for (i = 0; ; i++) { 176 state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i); 177 if (!state_node) 178 break; 179 180 match_id = of_match_node(matches, state_node); 181 if (!match_id) { 182 err = -ENODEV; 183 break; 184 } 185 186 if (!of_device_is_available(state_node)) { 187 of_node_put(state_node); 188 continue; 189 } 190 191 if (!idle_state_valid(state_node, i, cpumask)) { 192 pr_warn("%pOF idle state not valid, bailing out\n", 193 state_node); 194 err = -EINVAL; 195 break; 196 } 197 198 if (state_idx == CPUIDLE_STATE_MAX) { 199 pr_warn("State index reached static CPU idle driver states array size\n"); 200 break; 201 } 202 203 idle_state = &drv->states[state_idx++]; 204 err = init_state_node(idle_state, match_id, state_node); 205 if (err) { 206 pr_err("Parsing idle state node %pOF failed with err %d\n", 207 state_node, err); 208 err = -EINVAL; 209 break; 210 } 211 of_node_put(state_node); 212 } 213 214 of_node_put(state_node); 215 of_node_put(cpu_node); 216 if (err) 217 return err; 218 /* 219 * Update the driver state count only if some valid DT idle states 220 * were detected 221 */ 222 if (i) 223 drv->state_count = state_idx; 224 225 /* 226 * Return the number of present and valid DT idle states, which can 227 * also be 0 on platforms with missing DT idle states or legacy DT 228 * configuration predating the DT idle states bindings. 229 */ 230 return i; 231 } 232 EXPORT_SYMBOL_GPL(dt_init_idle_driver); 233