xref: /linux/drivers/cpuidle/dt_idle_states.c (revision cb51aeec147334d25fa8336d3334ea92e2134052)
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 #include <linux/of_device.h>
189f14da34SLorenzo Pieralisi 
199f14da34SLorenzo Pieralisi #include "dt_idle_states.h"
209f14da34SLorenzo Pieralisi 
219f14da34SLorenzo Pieralisi static int init_state_node(struct cpuidle_state *idle_state,
22db10945cSJoseph Lo 			   const struct of_device_id *match_id,
239f14da34SLorenzo Pieralisi 			   struct device_node *state_node)
249f14da34SLorenzo Pieralisi {
259f14da34SLorenzo Pieralisi 	int err;
26c00bc5dfSLorenzo Pieralisi 	const char *desc;
279f14da34SLorenzo Pieralisi 
289f14da34SLorenzo Pieralisi 	/*
299f14da34SLorenzo Pieralisi 	 * CPUidle drivers are expected to initialize the const void *data
309f14da34SLorenzo Pieralisi 	 * pointer of the passed in struct of_device_id array to the idle
319f14da34SLorenzo Pieralisi 	 * state enter function.
329f14da34SLorenzo Pieralisi 	 */
339f14da34SLorenzo Pieralisi 	idle_state->enter = match_id->data;
34a94e502cSSudeep Holla 	/*
35a94e502cSSudeep Holla 	 * Since this is not a "coupled" state, it's safe to assume interrupts
36a94e502cSSudeep Holla 	 * won't be enabled when it exits allowing the tick to be frozen
3728ba086eSRafael J. Wysocki 	 * safely. So enter() can be also enter_s2idle() callback.
38a94e502cSSudeep Holla 	 */
3928ba086eSRafael J. Wysocki 	idle_state->enter_s2idle = match_id->data;
409f14da34SLorenzo Pieralisi 
419f14da34SLorenzo Pieralisi 	err = of_property_read_u32(state_node, "wakeup-latency-us",
429f14da34SLorenzo Pieralisi 				   &idle_state->exit_latency);
439f14da34SLorenzo Pieralisi 	if (err) {
449f14da34SLorenzo Pieralisi 		u32 entry_latency, exit_latency;
459f14da34SLorenzo Pieralisi 
469f14da34SLorenzo Pieralisi 		err = of_property_read_u32(state_node, "entry-latency-us",
479f14da34SLorenzo Pieralisi 					   &entry_latency);
489f14da34SLorenzo Pieralisi 		if (err) {
4984dc4141SRob Herring 			pr_debug(" * %pOF missing entry-latency-us property\n",
5084dc4141SRob Herring 				 state_node);
519f14da34SLorenzo Pieralisi 			return -EINVAL;
529f14da34SLorenzo Pieralisi 		}
539f14da34SLorenzo Pieralisi 
549f14da34SLorenzo Pieralisi 		err = of_property_read_u32(state_node, "exit-latency-us",
559f14da34SLorenzo Pieralisi 					   &exit_latency);
569f14da34SLorenzo Pieralisi 		if (err) {
5784dc4141SRob Herring 			pr_debug(" * %pOF missing exit-latency-us property\n",
5884dc4141SRob Herring 				 state_node);
599f14da34SLorenzo Pieralisi 			return -EINVAL;
609f14da34SLorenzo Pieralisi 		}
619f14da34SLorenzo Pieralisi 		/*
629f14da34SLorenzo Pieralisi 		 * If wakeup-latency-us is missing, default to entry+exit
639f14da34SLorenzo Pieralisi 		 * latencies as defined in idle states bindings
649f14da34SLorenzo Pieralisi 		 */
659f14da34SLorenzo Pieralisi 		idle_state->exit_latency = entry_latency + exit_latency;
669f14da34SLorenzo Pieralisi 	}
679f14da34SLorenzo Pieralisi 
689f14da34SLorenzo Pieralisi 	err = of_property_read_u32(state_node, "min-residency-us",
699f14da34SLorenzo Pieralisi 				   &idle_state->target_residency);
709f14da34SLorenzo Pieralisi 	if (err) {
7184dc4141SRob Herring 		pr_debug(" * %pOF missing min-residency-us property\n",
7284dc4141SRob Herring 			     state_node);
739f14da34SLorenzo Pieralisi 		return -EINVAL;
749f14da34SLorenzo Pieralisi 	}
759f14da34SLorenzo Pieralisi 
76c00bc5dfSLorenzo Pieralisi 	err = of_property_read_string(state_node, "idle-state-name", &desc);
77c00bc5dfSLorenzo Pieralisi 	if (err)
78c00bc5dfSLorenzo Pieralisi 		desc = state_node->name;
79c00bc5dfSLorenzo Pieralisi 
80b82b6ccaSDaniel Lezcano 	idle_state->flags = 0;
819f14da34SLorenzo Pieralisi 	if (of_property_read_bool(state_node, "local-timer-stop"))
829f14da34SLorenzo Pieralisi 		idle_state->flags |= CPUIDLE_FLAG_TIMER_STOP;
839f14da34SLorenzo Pieralisi 	/*
849f14da34SLorenzo Pieralisi 	 * TODO:
859f14da34SLorenzo Pieralisi 	 *	replace with kstrdup and pointer assignment when name
869f14da34SLorenzo Pieralisi 	 *	and desc become string pointers
879f14da34SLorenzo Pieralisi 	 */
889f14da34SLorenzo Pieralisi 	strncpy(idle_state->name, state_node->name, CPUIDLE_NAME_LEN - 1);
89c00bc5dfSLorenzo Pieralisi 	strncpy(idle_state->desc, desc, CPUIDLE_DESC_LEN - 1);
909f14da34SLorenzo Pieralisi 	return 0;
919f14da34SLorenzo Pieralisi }
929f14da34SLorenzo Pieralisi 
939f14da34SLorenzo Pieralisi /*
949f14da34SLorenzo Pieralisi  * Check that the idle state is uniform across all CPUs in the CPUidle driver
959f14da34SLorenzo Pieralisi  * cpumask
969f14da34SLorenzo Pieralisi  */
979f14da34SLorenzo Pieralisi static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
989f14da34SLorenzo Pieralisi 			     const cpumask_t *cpumask)
999f14da34SLorenzo Pieralisi {
1009f14da34SLorenzo Pieralisi 	int cpu;
1019f14da34SLorenzo Pieralisi 	struct device_node *cpu_node, *curr_state_node;
1029f14da34SLorenzo Pieralisi 	bool valid = true;
1039f14da34SLorenzo Pieralisi 
1049f14da34SLorenzo Pieralisi 	/*
1059f14da34SLorenzo Pieralisi 	 * Compare idle state phandles for index idx on all CPUs in the
1069f14da34SLorenzo Pieralisi 	 * CPUidle driver cpumask. Start from next logical cpu following
1079f14da34SLorenzo Pieralisi 	 * cpumask_first(cpumask) since that's the CPU state_node was
1089f14da34SLorenzo Pieralisi 	 * retrieved from. If a mismatch is found bail out straight
1099f14da34SLorenzo Pieralisi 	 * away since we certainly hit a firmware misconfiguration.
1109f14da34SLorenzo Pieralisi 	 */
1119f14da34SLorenzo Pieralisi 	for (cpu = cpumask_next(cpumask_first(cpumask), cpumask);
1129f14da34SLorenzo Pieralisi 	     cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpumask)) {
1139f14da34SLorenzo Pieralisi 		cpu_node = of_cpu_device_node_get(cpu);
114778f173eSLina Iyer 		curr_state_node = of_get_cpu_state_node(cpu_node, idx);
1159f14da34SLorenzo Pieralisi 		if (state_node != curr_state_node)
1169f14da34SLorenzo Pieralisi 			valid = false;
1179f14da34SLorenzo Pieralisi 
1189f14da34SLorenzo Pieralisi 		of_node_put(curr_state_node);
1199f14da34SLorenzo Pieralisi 		of_node_put(cpu_node);
1209f14da34SLorenzo Pieralisi 		if (!valid)
1219f14da34SLorenzo Pieralisi 			break;
1229f14da34SLorenzo Pieralisi 	}
1239f14da34SLorenzo Pieralisi 
1249f14da34SLorenzo Pieralisi 	return valid;
1259f14da34SLorenzo Pieralisi }
1269f14da34SLorenzo Pieralisi 
1279f14da34SLorenzo Pieralisi /**
1289f14da34SLorenzo Pieralisi  * dt_init_idle_driver() - Parse the DT idle states and initialize the
1299f14da34SLorenzo Pieralisi  *			   idle driver states array
1309f14da34SLorenzo Pieralisi  * @drv:	  Pointer to CPU idle driver to be initialized
1319f14da34SLorenzo Pieralisi  * @matches:	  Array of of_device_id match structures to search in for
1329f14da34SLorenzo Pieralisi  *		  compatible idle state nodes. The data pointer for each valid
1339f14da34SLorenzo Pieralisi  *		  struct of_device_id entry in the matches array must point to
1349f14da34SLorenzo Pieralisi  *		  a function with the following signature, that corresponds to
1359f14da34SLorenzo Pieralisi  *		  the CPUidle state enter function signature:
1369f14da34SLorenzo Pieralisi  *
1379f14da34SLorenzo Pieralisi  *		  int (*)(struct cpuidle_device *dev,
1389f14da34SLorenzo Pieralisi  *			  struct cpuidle_driver *drv,
1399f14da34SLorenzo Pieralisi  *			  int index);
1409f14da34SLorenzo Pieralisi  *
1419f14da34SLorenzo Pieralisi  * @start_idx:    First idle state index to be initialized
1429f14da34SLorenzo Pieralisi  *
1439f14da34SLorenzo Pieralisi  * If DT idle states are detected and are valid the state count and states
1449f14da34SLorenzo Pieralisi  * array entries in the cpuidle driver are initialized accordingly starting
1459f14da34SLorenzo Pieralisi  * from index start_idx.
1469f14da34SLorenzo Pieralisi  *
1479f14da34SLorenzo Pieralisi  * Return: number of valid DT idle states parsed, <0 on failure
1489f14da34SLorenzo Pieralisi  */
1499f14da34SLorenzo Pieralisi int dt_init_idle_driver(struct cpuidle_driver *drv,
1509f14da34SLorenzo Pieralisi 			const struct of_device_id *matches,
1519f14da34SLorenzo Pieralisi 			unsigned int start_idx)
1529f14da34SLorenzo Pieralisi {
1539f14da34SLorenzo Pieralisi 	struct cpuidle_state *idle_state;
1549f14da34SLorenzo Pieralisi 	struct device_node *state_node, *cpu_node;
155db10945cSJoseph Lo 	const struct of_device_id *match_id;
1569f14da34SLorenzo Pieralisi 	int i, err = 0;
1579f14da34SLorenzo Pieralisi 	const cpumask_t *cpumask;
1589f14da34SLorenzo Pieralisi 	unsigned int state_idx = start_idx;
1599f14da34SLorenzo Pieralisi 
1609f14da34SLorenzo Pieralisi 	if (state_idx >= CPUIDLE_STATE_MAX)
1619f14da34SLorenzo Pieralisi 		return -EINVAL;
1629f14da34SLorenzo Pieralisi 	/*
1639f14da34SLorenzo Pieralisi 	 * We get the idle states for the first logical cpu in the
1649f14da34SLorenzo Pieralisi 	 * driver mask (or cpu_possible_mask if the driver cpumask is not set)
1659f14da34SLorenzo Pieralisi 	 * and we check through idle_state_valid() if they are uniform
1669f14da34SLorenzo Pieralisi 	 * across CPUs, otherwise we hit a firmware misconfiguration.
1679f14da34SLorenzo Pieralisi 	 */
1689f14da34SLorenzo Pieralisi 	cpumask = drv->cpumask ? : cpu_possible_mask;
1699f14da34SLorenzo Pieralisi 	cpu_node = of_cpu_device_node_get(cpumask_first(cpumask));
1709f14da34SLorenzo Pieralisi 
1719f14da34SLorenzo Pieralisi 	for (i = 0; ; i++) {
172778f173eSLina Iyer 		state_node = of_get_cpu_state_node(cpu_node, i);
1739f14da34SLorenzo Pieralisi 		if (!state_node)
1749f14da34SLorenzo Pieralisi 			break;
1759f14da34SLorenzo Pieralisi 
176db10945cSJoseph Lo 		match_id = of_match_node(matches, state_node);
177db10945cSJoseph Lo 		if (!match_id) {
178db10945cSJoseph Lo 			err = -ENODEV;
179db10945cSJoseph Lo 			break;
180db10945cSJoseph Lo 		}
181db10945cSJoseph Lo 
182b2cdd8e1SChristophe Jaillet 		if (!of_device_is_available(state_node)) {
183b2cdd8e1SChristophe Jaillet 			of_node_put(state_node);
18497735da0SLorenzo Pieralisi 			continue;
185b2cdd8e1SChristophe Jaillet 		}
18697735da0SLorenzo Pieralisi 
1879f14da34SLorenzo Pieralisi 		if (!idle_state_valid(state_node, i, cpumask)) {
18884dc4141SRob Herring 			pr_warn("%pOF idle state not valid, bailing out\n",
18984dc4141SRob Herring 				state_node);
1909f14da34SLorenzo Pieralisi 			err = -EINVAL;
1919f14da34SLorenzo Pieralisi 			break;
1929f14da34SLorenzo Pieralisi 		}
1939f14da34SLorenzo Pieralisi 
1949f14da34SLorenzo Pieralisi 		if (state_idx == CPUIDLE_STATE_MAX) {
1959f14da34SLorenzo Pieralisi 			pr_warn("State index reached static CPU idle driver states array size\n");
1969f14da34SLorenzo Pieralisi 			break;
1979f14da34SLorenzo Pieralisi 		}
1989f14da34SLorenzo Pieralisi 
1999f14da34SLorenzo Pieralisi 		idle_state = &drv->states[state_idx++];
200db10945cSJoseph Lo 		err = init_state_node(idle_state, match_id, state_node);
2019f14da34SLorenzo Pieralisi 		if (err) {
20284dc4141SRob Herring 			pr_err("Parsing idle state node %pOF failed with err %d\n",
20384dc4141SRob Herring 			       state_node, err);
2049f14da34SLorenzo Pieralisi 			err = -EINVAL;
2059f14da34SLorenzo Pieralisi 			break;
2069f14da34SLorenzo Pieralisi 		}
2079f14da34SLorenzo Pieralisi 		of_node_put(state_node);
2089f14da34SLorenzo Pieralisi 	}
2099f14da34SLorenzo Pieralisi 
2109f14da34SLorenzo Pieralisi 	of_node_put(state_node);
2119f14da34SLorenzo Pieralisi 	of_node_put(cpu_node);
2129f14da34SLorenzo Pieralisi 	if (err)
2139f14da34SLorenzo Pieralisi 		return err;
214*cb51aeecSUlf Hansson 
215*cb51aeecSUlf Hansson 	/* Set the number of total supported idle states. */
2169f14da34SLorenzo Pieralisi 	drv->state_count = state_idx;
2179f14da34SLorenzo Pieralisi 
2189f14da34SLorenzo Pieralisi 	/*
2199f14da34SLorenzo Pieralisi 	 * Return the number of present and valid DT idle states, which can
2209f14da34SLorenzo Pieralisi 	 * also be 0 on platforms with missing DT idle states or legacy DT
2219f14da34SLorenzo Pieralisi 	 * configuration predating the DT idle states bindings.
2229f14da34SLorenzo Pieralisi 	 */
223ee3c2c8aSUlf Hansson 	return state_idx - start_idx;
2249f14da34SLorenzo Pieralisi }
2259f14da34SLorenzo Pieralisi EXPORT_SYMBOL_GPL(dt_init_idle_driver);
226