1*9f14da34SLorenzo Pieralisi /* 2*9f14da34SLorenzo Pieralisi * DT idle states parsing code. 3*9f14da34SLorenzo Pieralisi * 4*9f14da34SLorenzo Pieralisi * Copyright (C) 2014 ARM Ltd. 5*9f14da34SLorenzo Pieralisi * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com> 6*9f14da34SLorenzo Pieralisi * 7*9f14da34SLorenzo Pieralisi * This program is free software; you can redistribute it and/or modify 8*9f14da34SLorenzo Pieralisi * it under the terms of the GNU General Public License version 2 as 9*9f14da34SLorenzo Pieralisi * published by the Free Software Foundation. 10*9f14da34SLorenzo Pieralisi */ 11*9f14da34SLorenzo Pieralisi 12*9f14da34SLorenzo Pieralisi #define pr_fmt(fmt) "DT idle-states: " fmt 13*9f14da34SLorenzo Pieralisi 14*9f14da34SLorenzo Pieralisi #include <linux/cpuidle.h> 15*9f14da34SLorenzo Pieralisi #include <linux/cpumask.h> 16*9f14da34SLorenzo Pieralisi #include <linux/errno.h> 17*9f14da34SLorenzo Pieralisi #include <linux/kernel.h> 18*9f14da34SLorenzo Pieralisi #include <linux/module.h> 19*9f14da34SLorenzo Pieralisi #include <linux/of.h> 20*9f14da34SLorenzo Pieralisi #include <linux/of_device.h> 21*9f14da34SLorenzo Pieralisi 22*9f14da34SLorenzo Pieralisi #include "dt_idle_states.h" 23*9f14da34SLorenzo Pieralisi 24*9f14da34SLorenzo Pieralisi static int init_state_node(struct cpuidle_state *idle_state, 25*9f14da34SLorenzo Pieralisi const struct of_device_id *matches, 26*9f14da34SLorenzo Pieralisi struct device_node *state_node) 27*9f14da34SLorenzo Pieralisi { 28*9f14da34SLorenzo Pieralisi int err; 29*9f14da34SLorenzo Pieralisi const struct of_device_id *match_id; 30*9f14da34SLorenzo Pieralisi 31*9f14da34SLorenzo Pieralisi match_id = of_match_node(matches, state_node); 32*9f14da34SLorenzo Pieralisi if (!match_id) 33*9f14da34SLorenzo Pieralisi return -ENODEV; 34*9f14da34SLorenzo Pieralisi /* 35*9f14da34SLorenzo Pieralisi * CPUidle drivers are expected to initialize the const void *data 36*9f14da34SLorenzo Pieralisi * pointer of the passed in struct of_device_id array to the idle 37*9f14da34SLorenzo Pieralisi * state enter function. 38*9f14da34SLorenzo Pieralisi */ 39*9f14da34SLorenzo Pieralisi idle_state->enter = match_id->data; 40*9f14da34SLorenzo Pieralisi 41*9f14da34SLorenzo Pieralisi err = of_property_read_u32(state_node, "wakeup-latency-us", 42*9f14da34SLorenzo Pieralisi &idle_state->exit_latency); 43*9f14da34SLorenzo Pieralisi if (err) { 44*9f14da34SLorenzo Pieralisi u32 entry_latency, exit_latency; 45*9f14da34SLorenzo Pieralisi 46*9f14da34SLorenzo Pieralisi err = of_property_read_u32(state_node, "entry-latency-us", 47*9f14da34SLorenzo Pieralisi &entry_latency); 48*9f14da34SLorenzo Pieralisi if (err) { 49*9f14da34SLorenzo Pieralisi pr_debug(" * %s missing entry-latency-us property\n", 50*9f14da34SLorenzo Pieralisi state_node->full_name); 51*9f14da34SLorenzo Pieralisi return -EINVAL; 52*9f14da34SLorenzo Pieralisi } 53*9f14da34SLorenzo Pieralisi 54*9f14da34SLorenzo Pieralisi err = of_property_read_u32(state_node, "exit-latency-us", 55*9f14da34SLorenzo Pieralisi &exit_latency); 56*9f14da34SLorenzo Pieralisi if (err) { 57*9f14da34SLorenzo Pieralisi pr_debug(" * %s missing exit-latency-us property\n", 58*9f14da34SLorenzo Pieralisi state_node->full_name); 59*9f14da34SLorenzo Pieralisi return -EINVAL; 60*9f14da34SLorenzo Pieralisi } 61*9f14da34SLorenzo Pieralisi /* 62*9f14da34SLorenzo Pieralisi * If wakeup-latency-us is missing, default to entry+exit 63*9f14da34SLorenzo Pieralisi * latencies as defined in idle states bindings 64*9f14da34SLorenzo Pieralisi */ 65*9f14da34SLorenzo Pieralisi idle_state->exit_latency = entry_latency + exit_latency; 66*9f14da34SLorenzo Pieralisi } 67*9f14da34SLorenzo Pieralisi 68*9f14da34SLorenzo Pieralisi err = of_property_read_u32(state_node, "min-residency-us", 69*9f14da34SLorenzo Pieralisi &idle_state->target_residency); 70*9f14da34SLorenzo Pieralisi if (err) { 71*9f14da34SLorenzo Pieralisi pr_debug(" * %s missing min-residency-us property\n", 72*9f14da34SLorenzo Pieralisi state_node->full_name); 73*9f14da34SLorenzo Pieralisi return -EINVAL; 74*9f14da34SLorenzo Pieralisi } 75*9f14da34SLorenzo Pieralisi 76*9f14da34SLorenzo Pieralisi idle_state->flags = CPUIDLE_FLAG_TIME_VALID; 77*9f14da34SLorenzo Pieralisi if (of_property_read_bool(state_node, "local-timer-stop")) 78*9f14da34SLorenzo Pieralisi idle_state->flags |= CPUIDLE_FLAG_TIMER_STOP; 79*9f14da34SLorenzo Pieralisi /* 80*9f14da34SLorenzo Pieralisi * TODO: 81*9f14da34SLorenzo Pieralisi * replace with kstrdup and pointer assignment when name 82*9f14da34SLorenzo Pieralisi * and desc become string pointers 83*9f14da34SLorenzo Pieralisi */ 84*9f14da34SLorenzo Pieralisi strncpy(idle_state->name, state_node->name, CPUIDLE_NAME_LEN - 1); 85*9f14da34SLorenzo Pieralisi strncpy(idle_state->desc, state_node->name, CPUIDLE_DESC_LEN - 1); 86*9f14da34SLorenzo Pieralisi return 0; 87*9f14da34SLorenzo Pieralisi } 88*9f14da34SLorenzo Pieralisi 89*9f14da34SLorenzo Pieralisi /* 90*9f14da34SLorenzo Pieralisi * Check that the idle state is uniform across all CPUs in the CPUidle driver 91*9f14da34SLorenzo Pieralisi * cpumask 92*9f14da34SLorenzo Pieralisi */ 93*9f14da34SLorenzo Pieralisi static bool idle_state_valid(struct device_node *state_node, unsigned int idx, 94*9f14da34SLorenzo Pieralisi const cpumask_t *cpumask) 95*9f14da34SLorenzo Pieralisi { 96*9f14da34SLorenzo Pieralisi int cpu; 97*9f14da34SLorenzo Pieralisi struct device_node *cpu_node, *curr_state_node; 98*9f14da34SLorenzo Pieralisi bool valid = true; 99*9f14da34SLorenzo Pieralisi 100*9f14da34SLorenzo Pieralisi /* 101*9f14da34SLorenzo Pieralisi * Compare idle state phandles for index idx on all CPUs in the 102*9f14da34SLorenzo Pieralisi * CPUidle driver cpumask. Start from next logical cpu following 103*9f14da34SLorenzo Pieralisi * cpumask_first(cpumask) since that's the CPU state_node was 104*9f14da34SLorenzo Pieralisi * retrieved from. If a mismatch is found bail out straight 105*9f14da34SLorenzo Pieralisi * away since we certainly hit a firmware misconfiguration. 106*9f14da34SLorenzo Pieralisi */ 107*9f14da34SLorenzo Pieralisi for (cpu = cpumask_next(cpumask_first(cpumask), cpumask); 108*9f14da34SLorenzo Pieralisi cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpumask)) { 109*9f14da34SLorenzo Pieralisi cpu_node = of_cpu_device_node_get(cpu); 110*9f14da34SLorenzo Pieralisi curr_state_node = of_parse_phandle(cpu_node, "cpu-idle-states", 111*9f14da34SLorenzo Pieralisi idx); 112*9f14da34SLorenzo Pieralisi if (state_node != curr_state_node) 113*9f14da34SLorenzo Pieralisi valid = false; 114*9f14da34SLorenzo Pieralisi 115*9f14da34SLorenzo Pieralisi of_node_put(curr_state_node); 116*9f14da34SLorenzo Pieralisi of_node_put(cpu_node); 117*9f14da34SLorenzo Pieralisi if (!valid) 118*9f14da34SLorenzo Pieralisi break; 119*9f14da34SLorenzo Pieralisi } 120*9f14da34SLorenzo Pieralisi 121*9f14da34SLorenzo Pieralisi return valid; 122*9f14da34SLorenzo Pieralisi } 123*9f14da34SLorenzo Pieralisi 124*9f14da34SLorenzo Pieralisi /** 125*9f14da34SLorenzo Pieralisi * dt_init_idle_driver() - Parse the DT idle states and initialize the 126*9f14da34SLorenzo Pieralisi * idle driver states array 127*9f14da34SLorenzo Pieralisi * @drv: Pointer to CPU idle driver to be initialized 128*9f14da34SLorenzo Pieralisi * @matches: Array of of_device_id match structures to search in for 129*9f14da34SLorenzo Pieralisi * compatible idle state nodes. The data pointer for each valid 130*9f14da34SLorenzo Pieralisi * struct of_device_id entry in the matches array must point to 131*9f14da34SLorenzo Pieralisi * a function with the following signature, that corresponds to 132*9f14da34SLorenzo Pieralisi * the CPUidle state enter function signature: 133*9f14da34SLorenzo Pieralisi * 134*9f14da34SLorenzo Pieralisi * int (*)(struct cpuidle_device *dev, 135*9f14da34SLorenzo Pieralisi * struct cpuidle_driver *drv, 136*9f14da34SLorenzo Pieralisi * int index); 137*9f14da34SLorenzo Pieralisi * 138*9f14da34SLorenzo Pieralisi * @start_idx: First idle state index to be initialized 139*9f14da34SLorenzo Pieralisi * 140*9f14da34SLorenzo Pieralisi * If DT idle states are detected and are valid the state count and states 141*9f14da34SLorenzo Pieralisi * array entries in the cpuidle driver are initialized accordingly starting 142*9f14da34SLorenzo Pieralisi * from index start_idx. 143*9f14da34SLorenzo Pieralisi * 144*9f14da34SLorenzo Pieralisi * Return: number of valid DT idle states parsed, <0 on failure 145*9f14da34SLorenzo Pieralisi */ 146*9f14da34SLorenzo Pieralisi int dt_init_idle_driver(struct cpuidle_driver *drv, 147*9f14da34SLorenzo Pieralisi const struct of_device_id *matches, 148*9f14da34SLorenzo Pieralisi unsigned int start_idx) 149*9f14da34SLorenzo Pieralisi { 150*9f14da34SLorenzo Pieralisi struct cpuidle_state *idle_state; 151*9f14da34SLorenzo Pieralisi struct device_node *state_node, *cpu_node; 152*9f14da34SLorenzo Pieralisi int i, err = 0; 153*9f14da34SLorenzo Pieralisi const cpumask_t *cpumask; 154*9f14da34SLorenzo Pieralisi unsigned int state_idx = start_idx; 155*9f14da34SLorenzo Pieralisi 156*9f14da34SLorenzo Pieralisi if (state_idx >= CPUIDLE_STATE_MAX) 157*9f14da34SLorenzo Pieralisi return -EINVAL; 158*9f14da34SLorenzo Pieralisi /* 159*9f14da34SLorenzo Pieralisi * We get the idle states for the first logical cpu in the 160*9f14da34SLorenzo Pieralisi * driver mask (or cpu_possible_mask if the driver cpumask is not set) 161*9f14da34SLorenzo Pieralisi * and we check through idle_state_valid() if they are uniform 162*9f14da34SLorenzo Pieralisi * across CPUs, otherwise we hit a firmware misconfiguration. 163*9f14da34SLorenzo Pieralisi */ 164*9f14da34SLorenzo Pieralisi cpumask = drv->cpumask ? : cpu_possible_mask; 165*9f14da34SLorenzo Pieralisi cpu_node = of_cpu_device_node_get(cpumask_first(cpumask)); 166*9f14da34SLorenzo Pieralisi 167*9f14da34SLorenzo Pieralisi for (i = 0; ; i++) { 168*9f14da34SLorenzo Pieralisi state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i); 169*9f14da34SLorenzo Pieralisi if (!state_node) 170*9f14da34SLorenzo Pieralisi break; 171*9f14da34SLorenzo Pieralisi 172*9f14da34SLorenzo Pieralisi if (!idle_state_valid(state_node, i, cpumask)) { 173*9f14da34SLorenzo Pieralisi pr_warn("%s idle state not valid, bailing out\n", 174*9f14da34SLorenzo Pieralisi state_node->full_name); 175*9f14da34SLorenzo Pieralisi err = -EINVAL; 176*9f14da34SLorenzo Pieralisi break; 177*9f14da34SLorenzo Pieralisi } 178*9f14da34SLorenzo Pieralisi 179*9f14da34SLorenzo Pieralisi if (state_idx == CPUIDLE_STATE_MAX) { 180*9f14da34SLorenzo Pieralisi pr_warn("State index reached static CPU idle driver states array size\n"); 181*9f14da34SLorenzo Pieralisi break; 182*9f14da34SLorenzo Pieralisi } 183*9f14da34SLorenzo Pieralisi 184*9f14da34SLorenzo Pieralisi idle_state = &drv->states[state_idx++]; 185*9f14da34SLorenzo Pieralisi err = init_state_node(idle_state, matches, state_node); 186*9f14da34SLorenzo Pieralisi if (err) { 187*9f14da34SLorenzo Pieralisi pr_err("Parsing idle state node %s failed with err %d\n", 188*9f14da34SLorenzo Pieralisi state_node->full_name, err); 189*9f14da34SLorenzo Pieralisi err = -EINVAL; 190*9f14da34SLorenzo Pieralisi break; 191*9f14da34SLorenzo Pieralisi } 192*9f14da34SLorenzo Pieralisi of_node_put(state_node); 193*9f14da34SLorenzo Pieralisi } 194*9f14da34SLorenzo Pieralisi 195*9f14da34SLorenzo Pieralisi of_node_put(state_node); 196*9f14da34SLorenzo Pieralisi of_node_put(cpu_node); 197*9f14da34SLorenzo Pieralisi if (err) 198*9f14da34SLorenzo Pieralisi return err; 199*9f14da34SLorenzo Pieralisi /* 200*9f14da34SLorenzo Pieralisi * Update the driver state count only if some valid DT idle states 201*9f14da34SLorenzo Pieralisi * were detected 202*9f14da34SLorenzo Pieralisi */ 203*9f14da34SLorenzo Pieralisi if (i) 204*9f14da34SLorenzo Pieralisi drv->state_count = state_idx; 205*9f14da34SLorenzo Pieralisi 206*9f14da34SLorenzo Pieralisi /* 207*9f14da34SLorenzo Pieralisi * Return the number of present and valid DT idle states, which can 208*9f14da34SLorenzo Pieralisi * also be 0 on platforms with missing DT idle states or legacy DT 209*9f14da34SLorenzo Pieralisi * configuration predating the DT idle states bindings. 210*9f14da34SLorenzo Pieralisi */ 211*9f14da34SLorenzo Pieralisi return i; 212*9f14da34SLorenzo Pieralisi } 213*9f14da34SLorenzo Pieralisi EXPORT_SYMBOL_GPL(dt_init_idle_driver); 214