xref: /linux/drivers/cpuidle/dt_idle_states.c (revision a94e502c22b61072f808f53392a8433bc948b03d)
19f14da34SLorenzo Pieralisi /*
29f14da34SLorenzo Pieralisi  * DT idle states parsing code.
39f14da34SLorenzo Pieralisi  *
49f14da34SLorenzo Pieralisi  * Copyright (C) 2014 ARM Ltd.
59f14da34SLorenzo Pieralisi  * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
69f14da34SLorenzo Pieralisi  *
79f14da34SLorenzo Pieralisi  * This program is free software; you can redistribute it and/or modify
89f14da34SLorenzo Pieralisi  * it under the terms of the GNU General Public License version 2 as
99f14da34SLorenzo Pieralisi  * published by the Free Software Foundation.
109f14da34SLorenzo Pieralisi  */
119f14da34SLorenzo Pieralisi 
129f14da34SLorenzo Pieralisi #define pr_fmt(fmt) "DT idle-states: " fmt
139f14da34SLorenzo Pieralisi 
149f14da34SLorenzo Pieralisi #include <linux/cpuidle.h>
159f14da34SLorenzo Pieralisi #include <linux/cpumask.h>
169f14da34SLorenzo Pieralisi #include <linux/errno.h>
179f14da34SLorenzo Pieralisi #include <linux/kernel.h>
189f14da34SLorenzo Pieralisi #include <linux/module.h>
199f14da34SLorenzo Pieralisi #include <linux/of.h>
209f14da34SLorenzo Pieralisi #include <linux/of_device.h>
219f14da34SLorenzo Pieralisi 
229f14da34SLorenzo Pieralisi #include "dt_idle_states.h"
239f14da34SLorenzo Pieralisi 
249f14da34SLorenzo Pieralisi static int init_state_node(struct cpuidle_state *idle_state,
259f14da34SLorenzo Pieralisi 			   const struct of_device_id *matches,
269f14da34SLorenzo Pieralisi 			   struct device_node *state_node)
279f14da34SLorenzo Pieralisi {
289f14da34SLorenzo Pieralisi 	int err;
299f14da34SLorenzo Pieralisi 	const struct of_device_id *match_id;
30c00bc5dfSLorenzo Pieralisi 	const char *desc;
319f14da34SLorenzo Pieralisi 
329f14da34SLorenzo Pieralisi 	match_id = of_match_node(matches, state_node);
339f14da34SLorenzo Pieralisi 	if (!match_id)
349f14da34SLorenzo Pieralisi 		return -ENODEV;
359f14da34SLorenzo Pieralisi 	/*
369f14da34SLorenzo Pieralisi 	 * CPUidle drivers are expected to initialize the const void *data
379f14da34SLorenzo Pieralisi 	 * pointer of the passed in struct of_device_id array to the idle
389f14da34SLorenzo Pieralisi 	 * state enter function.
399f14da34SLorenzo Pieralisi 	 */
409f14da34SLorenzo Pieralisi 	idle_state->enter = match_id->data;
41*a94e502cSSudeep Holla 	/*
42*a94e502cSSudeep Holla 	 * Since this is not a "coupled" state, it's safe to assume interrupts
43*a94e502cSSudeep Holla 	 * won't be enabled when it exits allowing the tick to be frozen
44*a94e502cSSudeep Holla 	 * safely. So enter() can be also enter_freeze() callback.
45*a94e502cSSudeep Holla 	 */
46*a94e502cSSudeep Holla 	idle_state->enter_freeze = match_id->data;
479f14da34SLorenzo Pieralisi 
489f14da34SLorenzo Pieralisi 	err = of_property_read_u32(state_node, "wakeup-latency-us",
499f14da34SLorenzo Pieralisi 				   &idle_state->exit_latency);
509f14da34SLorenzo Pieralisi 	if (err) {
519f14da34SLorenzo Pieralisi 		u32 entry_latency, exit_latency;
529f14da34SLorenzo Pieralisi 
539f14da34SLorenzo Pieralisi 		err = of_property_read_u32(state_node, "entry-latency-us",
549f14da34SLorenzo Pieralisi 					   &entry_latency);
559f14da34SLorenzo Pieralisi 		if (err) {
569f14da34SLorenzo Pieralisi 			pr_debug(" * %s missing entry-latency-us property\n",
579f14da34SLorenzo Pieralisi 				 state_node->full_name);
589f14da34SLorenzo Pieralisi 			return -EINVAL;
599f14da34SLorenzo Pieralisi 		}
609f14da34SLorenzo Pieralisi 
619f14da34SLorenzo Pieralisi 		err = of_property_read_u32(state_node, "exit-latency-us",
629f14da34SLorenzo Pieralisi 					   &exit_latency);
639f14da34SLorenzo Pieralisi 		if (err) {
649f14da34SLorenzo Pieralisi 			pr_debug(" * %s missing exit-latency-us property\n",
659f14da34SLorenzo Pieralisi 				 state_node->full_name);
669f14da34SLorenzo Pieralisi 			return -EINVAL;
679f14da34SLorenzo Pieralisi 		}
689f14da34SLorenzo Pieralisi 		/*
699f14da34SLorenzo Pieralisi 		 * If wakeup-latency-us is missing, default to entry+exit
709f14da34SLorenzo Pieralisi 		 * latencies as defined in idle states bindings
719f14da34SLorenzo Pieralisi 		 */
729f14da34SLorenzo Pieralisi 		idle_state->exit_latency = entry_latency + exit_latency;
739f14da34SLorenzo Pieralisi 	}
749f14da34SLorenzo Pieralisi 
759f14da34SLorenzo Pieralisi 	err = of_property_read_u32(state_node, "min-residency-us",
769f14da34SLorenzo Pieralisi 				   &idle_state->target_residency);
779f14da34SLorenzo Pieralisi 	if (err) {
789f14da34SLorenzo Pieralisi 		pr_debug(" * %s missing min-residency-us property\n",
799f14da34SLorenzo Pieralisi 			     state_node->full_name);
809f14da34SLorenzo Pieralisi 		return -EINVAL;
819f14da34SLorenzo Pieralisi 	}
829f14da34SLorenzo Pieralisi 
83c00bc5dfSLorenzo Pieralisi 	err = of_property_read_string(state_node, "idle-state-name", &desc);
84c00bc5dfSLorenzo Pieralisi 	if (err)
85c00bc5dfSLorenzo Pieralisi 		desc = state_node->name;
86c00bc5dfSLorenzo Pieralisi 
87b82b6ccaSDaniel Lezcano 	idle_state->flags = 0;
889f14da34SLorenzo Pieralisi 	if (of_property_read_bool(state_node, "local-timer-stop"))
899f14da34SLorenzo Pieralisi 		idle_state->flags |= CPUIDLE_FLAG_TIMER_STOP;
909f14da34SLorenzo Pieralisi 	/*
919f14da34SLorenzo Pieralisi 	 * TODO:
929f14da34SLorenzo Pieralisi 	 *	replace with kstrdup and pointer assignment when name
939f14da34SLorenzo Pieralisi 	 *	and desc become string pointers
949f14da34SLorenzo Pieralisi 	 */
959f14da34SLorenzo Pieralisi 	strncpy(idle_state->name, state_node->name, CPUIDLE_NAME_LEN - 1);
96c00bc5dfSLorenzo Pieralisi 	strncpy(idle_state->desc, desc, CPUIDLE_DESC_LEN - 1);
979f14da34SLorenzo Pieralisi 	return 0;
989f14da34SLorenzo Pieralisi }
999f14da34SLorenzo Pieralisi 
1009f14da34SLorenzo Pieralisi /*
1019f14da34SLorenzo Pieralisi  * Check that the idle state is uniform across all CPUs in the CPUidle driver
1029f14da34SLorenzo Pieralisi  * cpumask
1039f14da34SLorenzo Pieralisi  */
1049f14da34SLorenzo Pieralisi static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
1059f14da34SLorenzo Pieralisi 			     const cpumask_t *cpumask)
1069f14da34SLorenzo Pieralisi {
1079f14da34SLorenzo Pieralisi 	int cpu;
1089f14da34SLorenzo Pieralisi 	struct device_node *cpu_node, *curr_state_node;
1099f14da34SLorenzo Pieralisi 	bool valid = true;
1109f14da34SLorenzo Pieralisi 
1119f14da34SLorenzo Pieralisi 	/*
1129f14da34SLorenzo Pieralisi 	 * Compare idle state phandles for index idx on all CPUs in the
1139f14da34SLorenzo Pieralisi 	 * CPUidle driver cpumask. Start from next logical cpu following
1149f14da34SLorenzo Pieralisi 	 * cpumask_first(cpumask) since that's the CPU state_node was
1159f14da34SLorenzo Pieralisi 	 * retrieved from. If a mismatch is found bail out straight
1169f14da34SLorenzo Pieralisi 	 * away since we certainly hit a firmware misconfiguration.
1179f14da34SLorenzo Pieralisi 	 */
1189f14da34SLorenzo Pieralisi 	for (cpu = cpumask_next(cpumask_first(cpumask), cpumask);
1199f14da34SLorenzo Pieralisi 	     cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpumask)) {
1209f14da34SLorenzo Pieralisi 		cpu_node = of_cpu_device_node_get(cpu);
1219f14da34SLorenzo Pieralisi 		curr_state_node = of_parse_phandle(cpu_node, "cpu-idle-states",
1229f14da34SLorenzo Pieralisi 						   idx);
1239f14da34SLorenzo Pieralisi 		if (state_node != curr_state_node)
1249f14da34SLorenzo Pieralisi 			valid = false;
1259f14da34SLorenzo Pieralisi 
1269f14da34SLorenzo Pieralisi 		of_node_put(curr_state_node);
1279f14da34SLorenzo Pieralisi 		of_node_put(cpu_node);
1289f14da34SLorenzo Pieralisi 		if (!valid)
1299f14da34SLorenzo Pieralisi 			break;
1309f14da34SLorenzo Pieralisi 	}
1319f14da34SLorenzo Pieralisi 
1329f14da34SLorenzo Pieralisi 	return valid;
1339f14da34SLorenzo Pieralisi }
1349f14da34SLorenzo Pieralisi 
1359f14da34SLorenzo Pieralisi /**
1369f14da34SLorenzo Pieralisi  * dt_init_idle_driver() - Parse the DT idle states and initialize the
1379f14da34SLorenzo Pieralisi  *			   idle driver states array
1389f14da34SLorenzo Pieralisi  * @drv:	  Pointer to CPU idle driver to be initialized
1399f14da34SLorenzo Pieralisi  * @matches:	  Array of of_device_id match structures to search in for
1409f14da34SLorenzo Pieralisi  *		  compatible idle state nodes. The data pointer for each valid
1419f14da34SLorenzo Pieralisi  *		  struct of_device_id entry in the matches array must point to
1429f14da34SLorenzo Pieralisi  *		  a function with the following signature, that corresponds to
1439f14da34SLorenzo Pieralisi  *		  the CPUidle state enter function signature:
1449f14da34SLorenzo Pieralisi  *
1459f14da34SLorenzo Pieralisi  *		  int (*)(struct cpuidle_device *dev,
1469f14da34SLorenzo Pieralisi  *			  struct cpuidle_driver *drv,
1479f14da34SLorenzo Pieralisi  *			  int index);
1489f14da34SLorenzo Pieralisi  *
1499f14da34SLorenzo Pieralisi  * @start_idx:    First idle state index to be initialized
1509f14da34SLorenzo Pieralisi  *
1519f14da34SLorenzo Pieralisi  * If DT idle states are detected and are valid the state count and states
1529f14da34SLorenzo Pieralisi  * array entries in the cpuidle driver are initialized accordingly starting
1539f14da34SLorenzo Pieralisi  * from index start_idx.
1549f14da34SLorenzo Pieralisi  *
1559f14da34SLorenzo Pieralisi  * Return: number of valid DT idle states parsed, <0 on failure
1569f14da34SLorenzo Pieralisi  */
1579f14da34SLorenzo Pieralisi int dt_init_idle_driver(struct cpuidle_driver *drv,
1589f14da34SLorenzo Pieralisi 			const struct of_device_id *matches,
1599f14da34SLorenzo Pieralisi 			unsigned int start_idx)
1609f14da34SLorenzo Pieralisi {
1619f14da34SLorenzo Pieralisi 	struct cpuidle_state *idle_state;
1629f14da34SLorenzo Pieralisi 	struct device_node *state_node, *cpu_node;
1639f14da34SLorenzo Pieralisi 	int i, err = 0;
1649f14da34SLorenzo Pieralisi 	const cpumask_t *cpumask;
1659f14da34SLorenzo Pieralisi 	unsigned int state_idx = start_idx;
1669f14da34SLorenzo Pieralisi 
1679f14da34SLorenzo Pieralisi 	if (state_idx >= CPUIDLE_STATE_MAX)
1689f14da34SLorenzo Pieralisi 		return -EINVAL;
1699f14da34SLorenzo Pieralisi 	/*
1709f14da34SLorenzo Pieralisi 	 * We get the idle states for the first logical cpu in the
1719f14da34SLorenzo Pieralisi 	 * driver mask (or cpu_possible_mask if the driver cpumask is not set)
1729f14da34SLorenzo Pieralisi 	 * and we check through idle_state_valid() if they are uniform
1739f14da34SLorenzo Pieralisi 	 * across CPUs, otherwise we hit a firmware misconfiguration.
1749f14da34SLorenzo Pieralisi 	 */
1759f14da34SLorenzo Pieralisi 	cpumask = drv->cpumask ? : cpu_possible_mask;
1769f14da34SLorenzo Pieralisi 	cpu_node = of_cpu_device_node_get(cpumask_first(cpumask));
1779f14da34SLorenzo Pieralisi 
1789f14da34SLorenzo Pieralisi 	for (i = 0; ; i++) {
1799f14da34SLorenzo Pieralisi 		state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i);
1809f14da34SLorenzo Pieralisi 		if (!state_node)
1819f14da34SLorenzo Pieralisi 			break;
1829f14da34SLorenzo Pieralisi 
18397735da0SLorenzo Pieralisi 		if (!of_device_is_available(state_node))
18497735da0SLorenzo Pieralisi 			continue;
18597735da0SLorenzo Pieralisi 
1869f14da34SLorenzo Pieralisi 		if (!idle_state_valid(state_node, i, cpumask)) {
1879f14da34SLorenzo Pieralisi 			pr_warn("%s idle state not valid, bailing out\n",
1889f14da34SLorenzo Pieralisi 				state_node->full_name);
1899f14da34SLorenzo Pieralisi 			err = -EINVAL;
1909f14da34SLorenzo Pieralisi 			break;
1919f14da34SLorenzo Pieralisi 		}
1929f14da34SLorenzo Pieralisi 
1939f14da34SLorenzo Pieralisi 		if (state_idx == CPUIDLE_STATE_MAX) {
1949f14da34SLorenzo Pieralisi 			pr_warn("State index reached static CPU idle driver states array size\n");
1959f14da34SLorenzo Pieralisi 			break;
1969f14da34SLorenzo Pieralisi 		}
1979f14da34SLorenzo Pieralisi 
1989f14da34SLorenzo Pieralisi 		idle_state = &drv->states[state_idx++];
1999f14da34SLorenzo Pieralisi 		err = init_state_node(idle_state, matches, state_node);
2009f14da34SLorenzo Pieralisi 		if (err) {
2019f14da34SLorenzo Pieralisi 			pr_err("Parsing idle state node %s failed with err %d\n",
2029f14da34SLorenzo Pieralisi 			       state_node->full_name, err);
2039f14da34SLorenzo Pieralisi 			err = -EINVAL;
2049f14da34SLorenzo Pieralisi 			break;
2059f14da34SLorenzo Pieralisi 		}
2069f14da34SLorenzo Pieralisi 		of_node_put(state_node);
2079f14da34SLorenzo Pieralisi 	}
2089f14da34SLorenzo Pieralisi 
2099f14da34SLorenzo Pieralisi 	of_node_put(state_node);
2109f14da34SLorenzo Pieralisi 	of_node_put(cpu_node);
2119f14da34SLorenzo Pieralisi 	if (err)
2129f14da34SLorenzo Pieralisi 		return err;
2139f14da34SLorenzo Pieralisi 	/*
2149f14da34SLorenzo Pieralisi 	 * Update the driver state count only if some valid DT idle states
2159f14da34SLorenzo Pieralisi 	 * were detected
2169f14da34SLorenzo Pieralisi 	 */
2179f14da34SLorenzo Pieralisi 	if (i)
2189f14da34SLorenzo Pieralisi 		drv->state_count = state_idx;
2199f14da34SLorenzo Pieralisi 
2209f14da34SLorenzo Pieralisi 	/*
2219f14da34SLorenzo Pieralisi 	 * Return the number of present and valid DT idle states, which can
2229f14da34SLorenzo Pieralisi 	 * also be 0 on platforms with missing DT idle states or legacy DT
2239f14da34SLorenzo Pieralisi 	 * configuration predating the DT idle states bindings.
2249f14da34SLorenzo Pieralisi 	 */
2259f14da34SLorenzo Pieralisi 	return i;
2269f14da34SLorenzo Pieralisi }
2279f14da34SLorenzo Pieralisi EXPORT_SYMBOL_GPL(dt_init_idle_driver);
228