xref: /linux/drivers/cpuidle/dt_idle_states.c (revision b545465e22f5fec2862132c01a5d2abd3c4c4d50)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29f14da34SLorenzo Pieralisi /*
39f14da34SLorenzo Pieralisi  * DT idle states parsing code.
49f14da34SLorenzo Pieralisi  *
59f14da34SLorenzo Pieralisi  * Copyright (C) 2014 ARM Ltd.
69f14da34SLorenzo Pieralisi  * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
79f14da34SLorenzo Pieralisi  */
89f14da34SLorenzo Pieralisi 
99f14da34SLorenzo Pieralisi #define pr_fmt(fmt) "DT idle-states: " fmt
109f14da34SLorenzo Pieralisi 
119f14da34SLorenzo Pieralisi #include <linux/cpuidle.h>
129f14da34SLorenzo Pieralisi #include <linux/cpumask.h>
139f14da34SLorenzo Pieralisi #include <linux/errno.h>
149f14da34SLorenzo Pieralisi #include <linux/kernel.h>
159f14da34SLorenzo Pieralisi #include <linux/module.h>
169f14da34SLorenzo Pieralisi #include <linux/of.h>
179f14da34SLorenzo Pieralisi 
189f14da34SLorenzo Pieralisi #include "dt_idle_states.h"
199f14da34SLorenzo Pieralisi 
209f14da34SLorenzo Pieralisi static int init_state_node(struct cpuidle_state *idle_state,
21db10945cSJoseph Lo 			   const struct of_device_id *match_id,
229f14da34SLorenzo Pieralisi 			   struct device_node *state_node)
239f14da34SLorenzo Pieralisi {
249f14da34SLorenzo Pieralisi 	int err;
25c00bc5dfSLorenzo Pieralisi 	const char *desc;
269f14da34SLorenzo Pieralisi 
279f14da34SLorenzo Pieralisi 	/*
289f14da34SLorenzo Pieralisi 	 * CPUidle drivers are expected to initialize the const void *data
299f14da34SLorenzo Pieralisi 	 * pointer of the passed in struct of_device_id array to the idle
309f14da34SLorenzo Pieralisi 	 * state enter function.
319f14da34SLorenzo Pieralisi 	 */
329f14da34SLorenzo Pieralisi 	idle_state->enter = match_id->data;
33a94e502cSSudeep Holla 	/*
34a94e502cSSudeep Holla 	 * Since this is not a "coupled" state, it's safe to assume interrupts
35a94e502cSSudeep Holla 	 * won't be enabled when it exits allowing the tick to be frozen
3628ba086eSRafael J. Wysocki 	 * safely. So enter() can be also enter_s2idle() callback.
37a94e502cSSudeep Holla 	 */
3828ba086eSRafael J. Wysocki 	idle_state->enter_s2idle = match_id->data;
399f14da34SLorenzo Pieralisi 
409f14da34SLorenzo Pieralisi 	err = of_property_read_u32(state_node, "wakeup-latency-us",
419f14da34SLorenzo Pieralisi 				   &idle_state->exit_latency);
429f14da34SLorenzo Pieralisi 	if (err) {
439f14da34SLorenzo Pieralisi 		u32 entry_latency, exit_latency;
449f14da34SLorenzo Pieralisi 
459f14da34SLorenzo Pieralisi 		err = of_property_read_u32(state_node, "entry-latency-us",
469f14da34SLorenzo Pieralisi 					   &entry_latency);
479f14da34SLorenzo Pieralisi 		if (err) {
4884dc4141SRob Herring 			pr_debug(" * %pOF missing entry-latency-us property\n",
4984dc4141SRob Herring 				 state_node);
509f14da34SLorenzo Pieralisi 			return -EINVAL;
519f14da34SLorenzo Pieralisi 		}
529f14da34SLorenzo Pieralisi 
539f14da34SLorenzo Pieralisi 		err = of_property_read_u32(state_node, "exit-latency-us",
549f14da34SLorenzo Pieralisi 					   &exit_latency);
559f14da34SLorenzo Pieralisi 		if (err) {
5684dc4141SRob Herring 			pr_debug(" * %pOF missing exit-latency-us property\n",
5784dc4141SRob Herring 				 state_node);
589f14da34SLorenzo Pieralisi 			return -EINVAL;
599f14da34SLorenzo Pieralisi 		}
609f14da34SLorenzo Pieralisi 		/*
619f14da34SLorenzo Pieralisi 		 * If wakeup-latency-us is missing, default to entry+exit
629f14da34SLorenzo Pieralisi 		 * latencies as defined in idle states bindings
639f14da34SLorenzo Pieralisi 		 */
649f14da34SLorenzo Pieralisi 		idle_state->exit_latency = entry_latency + exit_latency;
659f14da34SLorenzo Pieralisi 	}
669f14da34SLorenzo Pieralisi 
679f14da34SLorenzo Pieralisi 	err = of_property_read_u32(state_node, "min-residency-us",
689f14da34SLorenzo Pieralisi 				   &idle_state->target_residency);
699f14da34SLorenzo Pieralisi 	if (err) {
7084dc4141SRob Herring 		pr_debug(" * %pOF missing min-residency-us property\n",
7184dc4141SRob Herring 			     state_node);
729f14da34SLorenzo Pieralisi 		return -EINVAL;
739f14da34SLorenzo Pieralisi 	}
749f14da34SLorenzo Pieralisi 
75c00bc5dfSLorenzo Pieralisi 	err = of_property_read_string(state_node, "idle-state-name", &desc);
76c00bc5dfSLorenzo Pieralisi 	if (err)
77c00bc5dfSLorenzo Pieralisi 		desc = state_node->name;
78c00bc5dfSLorenzo Pieralisi 
790c5ffc3dSPeter Zijlstra 	idle_state->flags = CPUIDLE_FLAG_RCU_IDLE;
809f14da34SLorenzo Pieralisi 	if (of_property_read_bool(state_node, "local-timer-stop"))
819f14da34SLorenzo Pieralisi 		idle_state->flags |= CPUIDLE_FLAG_TIMER_STOP;
829f14da34SLorenzo Pieralisi 	/*
839f14da34SLorenzo Pieralisi 	 * TODO:
849f14da34SLorenzo Pieralisi 	 *	replace with kstrdup and pointer assignment when name
859f14da34SLorenzo Pieralisi 	 *	and desc become string pointers
869f14da34SLorenzo Pieralisi 	 */
87*b545465eSJustin Stitt 	strscpy(idle_state->name, state_node->name, CPUIDLE_NAME_LEN);
88*b545465eSJustin Stitt 	strscpy(idle_state->desc, desc, CPUIDLE_DESC_LEN);
899f14da34SLorenzo Pieralisi 	return 0;
909f14da34SLorenzo Pieralisi }
919f14da34SLorenzo Pieralisi 
929f14da34SLorenzo Pieralisi /*
939f14da34SLorenzo Pieralisi  * Check that the idle state is uniform across all CPUs in the CPUidle driver
949f14da34SLorenzo Pieralisi  * cpumask
959f14da34SLorenzo Pieralisi  */
969f14da34SLorenzo Pieralisi static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
979f14da34SLorenzo Pieralisi 			     const cpumask_t *cpumask)
989f14da34SLorenzo Pieralisi {
999f14da34SLorenzo Pieralisi 	int cpu;
1009f14da34SLorenzo Pieralisi 	struct device_node *cpu_node, *curr_state_node;
1019f14da34SLorenzo Pieralisi 	bool valid = true;
1029f14da34SLorenzo Pieralisi 
1039f14da34SLorenzo Pieralisi 	/*
1049f14da34SLorenzo Pieralisi 	 * Compare idle state phandles for index idx on all CPUs in the
1059f14da34SLorenzo Pieralisi 	 * CPUidle driver cpumask. Start from next logical cpu following
1069f14da34SLorenzo Pieralisi 	 * cpumask_first(cpumask) since that's the CPU state_node was
1079f14da34SLorenzo Pieralisi 	 * retrieved from. If a mismatch is found bail out straight
1089f14da34SLorenzo Pieralisi 	 * away since we certainly hit a firmware misconfiguration.
1099f14da34SLorenzo Pieralisi 	 */
1109f14da34SLorenzo Pieralisi 	for (cpu = cpumask_next(cpumask_first(cpumask), cpumask);
1119f14da34SLorenzo Pieralisi 	     cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpumask)) {
1129f14da34SLorenzo Pieralisi 		cpu_node = of_cpu_device_node_get(cpu);
113778f173eSLina Iyer 		curr_state_node = of_get_cpu_state_node(cpu_node, idx);
1149f14da34SLorenzo Pieralisi 		if (state_node != curr_state_node)
1159f14da34SLorenzo Pieralisi 			valid = false;
1169f14da34SLorenzo Pieralisi 
1179f14da34SLorenzo Pieralisi 		of_node_put(curr_state_node);
1189f14da34SLorenzo Pieralisi 		of_node_put(cpu_node);
1199f14da34SLorenzo Pieralisi 		if (!valid)
1209f14da34SLorenzo Pieralisi 			break;
1219f14da34SLorenzo Pieralisi 	}
1229f14da34SLorenzo Pieralisi 
1239f14da34SLorenzo Pieralisi 	return valid;
1249f14da34SLorenzo Pieralisi }
1259f14da34SLorenzo Pieralisi 
1269f14da34SLorenzo Pieralisi /**
1279f14da34SLorenzo Pieralisi  * dt_init_idle_driver() - Parse the DT idle states and initialize the
1289f14da34SLorenzo Pieralisi  *			   idle driver states array
1299f14da34SLorenzo Pieralisi  * @drv:	  Pointer to CPU idle driver to be initialized
1309f14da34SLorenzo Pieralisi  * @matches:	  Array of of_device_id match structures to search in for
1319f14da34SLorenzo Pieralisi  *		  compatible idle state nodes. The data pointer for each valid
1329f14da34SLorenzo Pieralisi  *		  struct of_device_id entry in the matches array must point to
1339f14da34SLorenzo Pieralisi  *		  a function with the following signature, that corresponds to
1349f14da34SLorenzo Pieralisi  *		  the CPUidle state enter function signature:
1359f14da34SLorenzo Pieralisi  *
1369f14da34SLorenzo Pieralisi  *		  int (*)(struct cpuidle_device *dev,
1379f14da34SLorenzo Pieralisi  *			  struct cpuidle_driver *drv,
1389f14da34SLorenzo Pieralisi  *			  int index);
1399f14da34SLorenzo Pieralisi  *
1409f14da34SLorenzo Pieralisi  * @start_idx:    First idle state index to be initialized
1419f14da34SLorenzo Pieralisi  *
1429f14da34SLorenzo Pieralisi  * If DT idle states are detected and are valid the state count and states
1439f14da34SLorenzo Pieralisi  * array entries in the cpuidle driver are initialized accordingly starting
1449f14da34SLorenzo Pieralisi  * from index start_idx.
1459f14da34SLorenzo Pieralisi  *
1469f14da34SLorenzo Pieralisi  * Return: number of valid DT idle states parsed, <0 on failure
1479f14da34SLorenzo Pieralisi  */
1489f14da34SLorenzo Pieralisi int dt_init_idle_driver(struct cpuidle_driver *drv,
1499f14da34SLorenzo Pieralisi 			const struct of_device_id *matches,
1509f14da34SLorenzo Pieralisi 			unsigned int start_idx)
1519f14da34SLorenzo Pieralisi {
1529f14da34SLorenzo Pieralisi 	struct cpuidle_state *idle_state;
1539f14da34SLorenzo Pieralisi 	struct device_node *state_node, *cpu_node;
154db10945cSJoseph Lo 	const struct of_device_id *match_id;
1559f14da34SLorenzo Pieralisi 	int i, err = 0;
1569f14da34SLorenzo Pieralisi 	const cpumask_t *cpumask;
1579f14da34SLorenzo Pieralisi 	unsigned int state_idx = start_idx;
1589f14da34SLorenzo Pieralisi 
1599f14da34SLorenzo Pieralisi 	if (state_idx >= CPUIDLE_STATE_MAX)
1609f14da34SLorenzo Pieralisi 		return -EINVAL;
1619f14da34SLorenzo Pieralisi 	/*
1629f14da34SLorenzo Pieralisi 	 * We get the idle states for the first logical cpu in the
1639f14da34SLorenzo Pieralisi 	 * driver mask (or cpu_possible_mask if the driver cpumask is not set)
1649f14da34SLorenzo Pieralisi 	 * and we check through idle_state_valid() if they are uniform
1659f14da34SLorenzo Pieralisi 	 * across CPUs, otherwise we hit a firmware misconfiguration.
1669f14da34SLorenzo Pieralisi 	 */
1679f14da34SLorenzo Pieralisi 	cpumask = drv->cpumask ? : cpu_possible_mask;
1689f14da34SLorenzo Pieralisi 	cpu_node = of_cpu_device_node_get(cpumask_first(cpumask));
1699f14da34SLorenzo Pieralisi 
1709f14da34SLorenzo Pieralisi 	for (i = 0; ; i++) {
171778f173eSLina Iyer 		state_node = of_get_cpu_state_node(cpu_node, i);
1729f14da34SLorenzo Pieralisi 		if (!state_node)
1739f14da34SLorenzo Pieralisi 			break;
1749f14da34SLorenzo Pieralisi 
175db10945cSJoseph Lo 		match_id = of_match_node(matches, state_node);
176db10945cSJoseph Lo 		if (!match_id) {
177db10945cSJoseph Lo 			err = -ENODEV;
178db10945cSJoseph Lo 			break;
179db10945cSJoseph Lo 		}
180db10945cSJoseph Lo 
181b2cdd8e1SChristophe Jaillet 		if (!of_device_is_available(state_node)) {
182b2cdd8e1SChristophe Jaillet 			of_node_put(state_node);
18397735da0SLorenzo Pieralisi 			continue;
184b2cdd8e1SChristophe Jaillet 		}
18597735da0SLorenzo Pieralisi 
1869f14da34SLorenzo Pieralisi 		if (!idle_state_valid(state_node, i, cpumask)) {
18784dc4141SRob Herring 			pr_warn("%pOF idle state not valid, bailing out\n",
18884dc4141SRob Herring 				state_node);
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++];
199db10945cSJoseph Lo 		err = init_state_node(idle_state, match_id, state_node);
2009f14da34SLorenzo Pieralisi 		if (err) {
20184dc4141SRob Herring 			pr_err("Parsing idle state node %pOF failed with err %d\n",
20284dc4141SRob Herring 			       state_node, 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;
213cb51aeecSUlf Hansson 
214cb51aeecSUlf Hansson 	/* Set the number of total supported idle states. */
2159f14da34SLorenzo Pieralisi 	drv->state_count = state_idx;
2169f14da34SLorenzo Pieralisi 
2179f14da34SLorenzo Pieralisi 	/*
2189f14da34SLorenzo Pieralisi 	 * Return the number of present and valid DT idle states, which can
2199f14da34SLorenzo Pieralisi 	 * also be 0 on platforms with missing DT idle states or legacy DT
2209f14da34SLorenzo Pieralisi 	 * configuration predating the DT idle states bindings.
2219f14da34SLorenzo Pieralisi 	 */
222ee3c2c8aSUlf Hansson 	return state_idx - start_idx;
2239f14da34SLorenzo Pieralisi }
2249f14da34SLorenzo Pieralisi EXPORT_SYMBOL_GPL(dt_init_idle_driver);
225