xref: /linux/drivers/cpuidle/dt_idle_states.c (revision b82b6cca488074da3852e8a54fde1d9f74bf1557)
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;
309f14da34SLorenzo Pieralisi 
319f14da34SLorenzo Pieralisi 	match_id = of_match_node(matches, state_node);
329f14da34SLorenzo Pieralisi 	if (!match_id)
339f14da34SLorenzo Pieralisi 		return -ENODEV;
349f14da34SLorenzo Pieralisi 	/*
359f14da34SLorenzo Pieralisi 	 * CPUidle drivers are expected to initialize the const void *data
369f14da34SLorenzo Pieralisi 	 * pointer of the passed in struct of_device_id array to the idle
379f14da34SLorenzo Pieralisi 	 * state enter function.
389f14da34SLorenzo Pieralisi 	 */
399f14da34SLorenzo Pieralisi 	idle_state->enter = 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) {
499f14da34SLorenzo Pieralisi 			pr_debug(" * %s missing entry-latency-us property\n",
509f14da34SLorenzo Pieralisi 				 state_node->full_name);
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) {
579f14da34SLorenzo Pieralisi 			pr_debug(" * %s missing exit-latency-us property\n",
589f14da34SLorenzo Pieralisi 				 state_node->full_name);
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) {
719f14da34SLorenzo Pieralisi 		pr_debug(" * %s missing min-residency-us property\n",
729f14da34SLorenzo Pieralisi 			     state_node->full_name);
739f14da34SLorenzo Pieralisi 		return -EINVAL;
749f14da34SLorenzo Pieralisi 	}
759f14da34SLorenzo Pieralisi 
76*b82b6ccaSDaniel Lezcano 	idle_state->flags = 0;
779f14da34SLorenzo Pieralisi 	if (of_property_read_bool(state_node, "local-timer-stop"))
789f14da34SLorenzo Pieralisi 		idle_state->flags |= CPUIDLE_FLAG_TIMER_STOP;
799f14da34SLorenzo Pieralisi 	/*
809f14da34SLorenzo Pieralisi 	 * TODO:
819f14da34SLorenzo Pieralisi 	 *	replace with kstrdup and pointer assignment when name
829f14da34SLorenzo Pieralisi 	 *	and desc become string pointers
839f14da34SLorenzo Pieralisi 	 */
849f14da34SLorenzo Pieralisi 	strncpy(idle_state->name, state_node->name, CPUIDLE_NAME_LEN - 1);
859f14da34SLorenzo Pieralisi 	strncpy(idle_state->desc, state_node->name, CPUIDLE_DESC_LEN - 1);
869f14da34SLorenzo Pieralisi 	return 0;
879f14da34SLorenzo Pieralisi }
889f14da34SLorenzo Pieralisi 
899f14da34SLorenzo Pieralisi /*
909f14da34SLorenzo Pieralisi  * Check that the idle state is uniform across all CPUs in the CPUidle driver
919f14da34SLorenzo Pieralisi  * cpumask
929f14da34SLorenzo Pieralisi  */
939f14da34SLorenzo Pieralisi static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
949f14da34SLorenzo Pieralisi 			     const cpumask_t *cpumask)
959f14da34SLorenzo Pieralisi {
969f14da34SLorenzo Pieralisi 	int cpu;
979f14da34SLorenzo Pieralisi 	struct device_node *cpu_node, *curr_state_node;
989f14da34SLorenzo Pieralisi 	bool valid = true;
999f14da34SLorenzo Pieralisi 
1009f14da34SLorenzo Pieralisi 	/*
1019f14da34SLorenzo Pieralisi 	 * Compare idle state phandles for index idx on all CPUs in the
1029f14da34SLorenzo Pieralisi 	 * CPUidle driver cpumask. Start from next logical cpu following
1039f14da34SLorenzo Pieralisi 	 * cpumask_first(cpumask) since that's the CPU state_node was
1049f14da34SLorenzo Pieralisi 	 * retrieved from. If a mismatch is found bail out straight
1059f14da34SLorenzo Pieralisi 	 * away since we certainly hit a firmware misconfiguration.
1069f14da34SLorenzo Pieralisi 	 */
1079f14da34SLorenzo Pieralisi 	for (cpu = cpumask_next(cpumask_first(cpumask), cpumask);
1089f14da34SLorenzo Pieralisi 	     cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpumask)) {
1099f14da34SLorenzo Pieralisi 		cpu_node = of_cpu_device_node_get(cpu);
1109f14da34SLorenzo Pieralisi 		curr_state_node = of_parse_phandle(cpu_node, "cpu-idle-states",
1119f14da34SLorenzo Pieralisi 						   idx);
1129f14da34SLorenzo Pieralisi 		if (state_node != curr_state_node)
1139f14da34SLorenzo Pieralisi 			valid = false;
1149f14da34SLorenzo Pieralisi 
1159f14da34SLorenzo Pieralisi 		of_node_put(curr_state_node);
1169f14da34SLorenzo Pieralisi 		of_node_put(cpu_node);
1179f14da34SLorenzo Pieralisi 		if (!valid)
1189f14da34SLorenzo Pieralisi 			break;
1199f14da34SLorenzo Pieralisi 	}
1209f14da34SLorenzo Pieralisi 
1219f14da34SLorenzo Pieralisi 	return valid;
1229f14da34SLorenzo Pieralisi }
1239f14da34SLorenzo Pieralisi 
1249f14da34SLorenzo Pieralisi /**
1259f14da34SLorenzo Pieralisi  * dt_init_idle_driver() - Parse the DT idle states and initialize the
1269f14da34SLorenzo Pieralisi  *			   idle driver states array
1279f14da34SLorenzo Pieralisi  * @drv:	  Pointer to CPU idle driver to be initialized
1289f14da34SLorenzo Pieralisi  * @matches:	  Array of of_device_id match structures to search in for
1299f14da34SLorenzo Pieralisi  *		  compatible idle state nodes. The data pointer for each valid
1309f14da34SLorenzo Pieralisi  *		  struct of_device_id entry in the matches array must point to
1319f14da34SLorenzo Pieralisi  *		  a function with the following signature, that corresponds to
1329f14da34SLorenzo Pieralisi  *		  the CPUidle state enter function signature:
1339f14da34SLorenzo Pieralisi  *
1349f14da34SLorenzo Pieralisi  *		  int (*)(struct cpuidle_device *dev,
1359f14da34SLorenzo Pieralisi  *			  struct cpuidle_driver *drv,
1369f14da34SLorenzo Pieralisi  *			  int index);
1379f14da34SLorenzo Pieralisi  *
1389f14da34SLorenzo Pieralisi  * @start_idx:    First idle state index to be initialized
1399f14da34SLorenzo Pieralisi  *
1409f14da34SLorenzo Pieralisi  * If DT idle states are detected and are valid the state count and states
1419f14da34SLorenzo Pieralisi  * array entries in the cpuidle driver are initialized accordingly starting
1429f14da34SLorenzo Pieralisi  * from index start_idx.
1439f14da34SLorenzo Pieralisi  *
1449f14da34SLorenzo Pieralisi  * Return: number of valid DT idle states parsed, <0 on failure
1459f14da34SLorenzo Pieralisi  */
1469f14da34SLorenzo Pieralisi int dt_init_idle_driver(struct cpuidle_driver *drv,
1479f14da34SLorenzo Pieralisi 			const struct of_device_id *matches,
1489f14da34SLorenzo Pieralisi 			unsigned int start_idx)
1499f14da34SLorenzo Pieralisi {
1509f14da34SLorenzo Pieralisi 	struct cpuidle_state *idle_state;
1519f14da34SLorenzo Pieralisi 	struct device_node *state_node, *cpu_node;
1529f14da34SLorenzo Pieralisi 	int i, err = 0;
1539f14da34SLorenzo Pieralisi 	const cpumask_t *cpumask;
1549f14da34SLorenzo Pieralisi 	unsigned int state_idx = start_idx;
1559f14da34SLorenzo Pieralisi 
1569f14da34SLorenzo Pieralisi 	if (state_idx >= CPUIDLE_STATE_MAX)
1579f14da34SLorenzo Pieralisi 		return -EINVAL;
1589f14da34SLorenzo Pieralisi 	/*
1599f14da34SLorenzo Pieralisi 	 * We get the idle states for the first logical cpu in the
1609f14da34SLorenzo Pieralisi 	 * driver mask (or cpu_possible_mask if the driver cpumask is not set)
1619f14da34SLorenzo Pieralisi 	 * and we check through idle_state_valid() if they are uniform
1629f14da34SLorenzo Pieralisi 	 * across CPUs, otherwise we hit a firmware misconfiguration.
1639f14da34SLorenzo Pieralisi 	 */
1649f14da34SLorenzo Pieralisi 	cpumask = drv->cpumask ? : cpu_possible_mask;
1659f14da34SLorenzo Pieralisi 	cpu_node = of_cpu_device_node_get(cpumask_first(cpumask));
1669f14da34SLorenzo Pieralisi 
1679f14da34SLorenzo Pieralisi 	for (i = 0; ; i++) {
1689f14da34SLorenzo Pieralisi 		state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i);
1699f14da34SLorenzo Pieralisi 		if (!state_node)
1709f14da34SLorenzo Pieralisi 			break;
1719f14da34SLorenzo Pieralisi 
1729f14da34SLorenzo Pieralisi 		if (!idle_state_valid(state_node, i, cpumask)) {
1739f14da34SLorenzo Pieralisi 			pr_warn("%s idle state not valid, bailing out\n",
1749f14da34SLorenzo Pieralisi 				state_node->full_name);
1759f14da34SLorenzo Pieralisi 			err = -EINVAL;
1769f14da34SLorenzo Pieralisi 			break;
1779f14da34SLorenzo Pieralisi 		}
1789f14da34SLorenzo Pieralisi 
1799f14da34SLorenzo Pieralisi 		if (state_idx == CPUIDLE_STATE_MAX) {
1809f14da34SLorenzo Pieralisi 			pr_warn("State index reached static CPU idle driver states array size\n");
1819f14da34SLorenzo Pieralisi 			break;
1829f14da34SLorenzo Pieralisi 		}
1839f14da34SLorenzo Pieralisi 
1849f14da34SLorenzo Pieralisi 		idle_state = &drv->states[state_idx++];
1859f14da34SLorenzo Pieralisi 		err = init_state_node(idle_state, matches, state_node);
1869f14da34SLorenzo Pieralisi 		if (err) {
1879f14da34SLorenzo Pieralisi 			pr_err("Parsing idle state node %s failed with err %d\n",
1889f14da34SLorenzo Pieralisi 			       state_node->full_name, err);
1899f14da34SLorenzo Pieralisi 			err = -EINVAL;
1909f14da34SLorenzo Pieralisi 			break;
1919f14da34SLorenzo Pieralisi 		}
1929f14da34SLorenzo Pieralisi 		of_node_put(state_node);
1939f14da34SLorenzo Pieralisi 	}
1949f14da34SLorenzo Pieralisi 
1959f14da34SLorenzo Pieralisi 	of_node_put(state_node);
1969f14da34SLorenzo Pieralisi 	of_node_put(cpu_node);
1979f14da34SLorenzo Pieralisi 	if (err)
1989f14da34SLorenzo Pieralisi 		return err;
1999f14da34SLorenzo Pieralisi 	/*
2009f14da34SLorenzo Pieralisi 	 * Update the driver state count only if some valid DT idle states
2019f14da34SLorenzo Pieralisi 	 * were detected
2029f14da34SLorenzo Pieralisi 	 */
2039f14da34SLorenzo Pieralisi 	if (i)
2049f14da34SLorenzo Pieralisi 		drv->state_count = state_idx;
2059f14da34SLorenzo Pieralisi 
2069f14da34SLorenzo Pieralisi 	/*
2079f14da34SLorenzo Pieralisi 	 * Return the number of present and valid DT idle states, which can
2089f14da34SLorenzo Pieralisi 	 * also be 0 on platforms with missing DT idle states or legacy DT
2099f14da34SLorenzo Pieralisi 	 * configuration predating the DT idle states bindings.
2109f14da34SLorenzo Pieralisi 	 */
2119f14da34SLorenzo Pieralisi 	return i;
2129f14da34SLorenzo Pieralisi }
2139f14da34SLorenzo Pieralisi EXPORT_SYMBOL_GPL(dt_init_idle_driver);
214