diff mbox

[v9,5/8] drivers: cpuidle: implement DT based idle states infrastructure

Message ID 1409938498-17984-6-git-send-email-lorenzo.pieralisi@arm.com
State Accepted
Commit 9f14da345599c14b329cf5ac9499ad322056dd32
Headers show

Commit Message

Lorenzo Pieralisi Sept. 5, 2014, 5:34 p.m. UTC
On most common ARM systems, the low-power states a CPU can be put into are
not discoverable in HW and require device tree bindings to describe
power down suspend operations and idle states parameters.

In order to enable DT based idle states and configure idle drivers, this
patch implements the bulk infrastructure required to parse the device tree
idle states bindings and initialize the corresponding CPUidle driver states
data.

The parsing API accepts a start index that defines the first idle state
that should be initialized by the parsing code in order to give new and
legacy driver flexibility over which states should be parsed using the
new DT mechanism.

The idle states node(s) is obtained from the phandle list of the first cpu
in the driver cpumask;  the kernel checks that the idle state node phandle
is the same for all CPUs in the driver cpumask before declaring the idle state
as valid and start parsing its content.

The idle state enter function pointer is initialized through DT match
structures passed in by the CPUidle driver, so that ARM legacy code can
cope with platform specific idle entry method based on compatible
string matching and the code used to initialize the enter function pointer
can be moved to the DT generic layer.

Acked-by: Catalin Marinas <catalin.marinas@arm.com>
Acked-by: Daniel Lezcano <daniel.lezcano@linaro.org>
Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
---
 drivers/cpuidle/Kconfig          |   3 +
 drivers/cpuidle/Makefile         |   1 +
 drivers/cpuidle/dt_idle_states.c | 213 +++++++++++++++++++++++++++++++++++++++
 drivers/cpuidle/dt_idle_states.h |   7 ++
 4 files changed, 224 insertions(+)
 create mode 100644 drivers/cpuidle/dt_idle_states.c
 create mode 100644 drivers/cpuidle/dt_idle_states.h

Comments

Lorenzo Pieralisi Sept. 5, 2014, 9:29 p.m. UTC | #1
On Fri, Sep 05, 2014 at 09:00:52PM +0100, Kevin Hilman wrote:
> Lorenzo Pieralisi <lorenzo.pieralisi@arm.com> writes:
> 
> > On most common ARM systems, the low-power states a CPU can be put into are
> > not discoverable in HW and require device tree bindings to describe
> > power down suspend operations and idle states parameters.
> >
> > In order to enable DT based idle states and configure idle drivers, this
> > patch implements the bulk infrastructure required to parse the device tree
> > idle states bindings and initialize the corresponding CPUidle driver states
> > data.
> >
> > The parsing API accepts a start index that defines the first idle state
> > that should be initialized by the parsing code in order to give new and
> > legacy driver flexibility over which states should be parsed using the
> > new DT mechanism.
> >
> > The idle states node(s) is obtained from the phandle list of the first cpu
> > in the driver cpumask;  the kernel checks that the idle state node phandle
> > is the same for all CPUs in the driver cpumask before declaring the idle state
> > as valid and start parsing its content.
> >
> > The idle state enter function pointer is initialized through DT match
> > structures passed in by the CPUidle driver, so that ARM legacy code can
> > cope with platform specific idle entry method based on compatible
> > string matching and the code used to initialize the enter function pointer
> > can be moved to the DT generic layer.
> >
> > Acked-by: Catalin Marinas <catalin.marinas@arm.com>
> > Acked-by: Daniel Lezcano <daniel.lezcano@linaro.org>
> > Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
> 
> [...]
> 
> > +	idle_state->flags = CPUIDLE_FLAG_TIME_VALID;
> > +	if (of_property_read_bool(state_node, "local-timer-stop"))
> > +		idle_state->flags |= CPUIDLE_FLAG_TIMER_STOP;
> > +	/*
> > +	 * TODO:
> > +	 *	replace with kstrdup and pointer assignment when name
> > +	 *	and desc become string pointers
> > +	 */
> > +	strncpy(idle_state->name, state_node->name, CPUIDLE_NAME_LEN - 1);
> > +	strncpy(idle_state->desc, state_node->name, CPUIDLE_DESC_LEN - 1);
> 
> This is a very minor concern, and shouldn't hold back this series,
> but...
> 
> I was playing with this series in order to test out the qcom cpuidle
> driver from Lina, and noticed that the state name and descriptions were
> not terribly helpful:
> 
> /sys/devices/system/cpu/cpu0/cpuidle # cat state?/name
> cpu-idle-state-
> cpu-idle-state-
> /sys/devices/system/cpu/cpu0/cpuidle # cat state?/desc
> cpu-idle-state-0
> cpu-idle-state-1
> 
> Turns out these strings come from the node name itself, and truncated in
> the case of state->name, but this can be fixed in the DTS itself (c.f.
> reply to Lina's driver.)
> 
> However, seeing that the node name is used to populate both the
> state->name and ->, made me wonder if there should better way to set the
> state->desc field to a more useful string.  Tools like powertop actually
> use that field and it can be quite useful.

Well, the truncation problem will be solved when those strings will be
kdup'ed, so for the name I think there is not a problem, copying the
state node is fine waiting for those strings to become pointers.

For desc, there are four options:

(1) enumerating idle states (but that's worse than copying the name into
    desc since on ARM idle-state{1,2,3...} means nothing)
(2) copying the idle state node compatible string into desc
(3) Add an optional property to the DT bindings to describe the state
(4) Leave code as it is

(3) I am not extremely keen at this stage to re-patch the DT bindings,
it has been an awful lot of work to make everyone agree so I would avoid
any changes, I hope you understand (and I am not even sure DT maintainers
would accept that, so even less keen on changing the DT bindings at this
stage).

(2) I am not sure it will clarify the description much.

(1) I would rule it out. So either we accept that the name can be
extended in length (that's going to be the case since we will
dynamically allocate the string so there will be no truncation, to
a reasonable extent) so (4) is fine, or we merge this code and I
will take care of pushing for (3) in a separate patch and copy the resulting
description into desc (if that change does not get NACK'ed).

I would really want to see this code in the mailine asap since it is
groundwork for all future CPUidle generalisation, I hope that what I am
saying above is acceptable, please let me know what you think.

Lorenzo

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Mark Brown Sept. 5, 2014, 9:36 p.m. UTC | #2
On Fri, Sep 05, 2014 at 10:29:08PM +0100, Lorenzo Pieralisi wrote:
> On Fri, Sep 05, 2014 at 09:00:52PM +0100, Kevin Hilman wrote:

> (3) Add an optional property to the DT bindings to describe the state

> (3) I am not extremely keen at this stage to re-patch the DT bindings,
> it has been an awful lot of work to make everyone agree so I would avoid
> any changes, I hope you understand (and I am not even sure DT maintainers
> would accept that, so even less keen on changing the DT bindings at this
> stage).

Just do it as an incremental patch if you're worried - we've got
precendents for this like regulator-name which exists solely to make
diagnostics more descriptive.
Kevin Hilman Sept. 8, 2014, 3:58 p.m. UTC | #3
Lorenzo Pieralisi <lorenzo.pieralisi@arm.com> writes:

> On Fri, Sep 05, 2014 at 09:00:52PM +0100, Kevin Hilman wrote:
>> Lorenzo Pieralisi <lorenzo.pieralisi@arm.com> writes:
>> 
>> > On most common ARM systems, the low-power states a CPU can be put into are
>> > not discoverable in HW and require device tree bindings to describe
>> > power down suspend operations and idle states parameters.
>> >
>> > In order to enable DT based idle states and configure idle drivers, this
>> > patch implements the bulk infrastructure required to parse the device tree
>> > idle states bindings and initialize the corresponding CPUidle driver states
>> > data.
>> >
>> > The parsing API accepts a start index that defines the first idle state
>> > that should be initialized by the parsing code in order to give new and
>> > legacy driver flexibility over which states should be parsed using the
>> > new DT mechanism.
>> >
>> > The idle states node(s) is obtained from the phandle list of the first cpu
>> > in the driver cpumask;  the kernel checks that the idle state node phandle
>> > is the same for all CPUs in the driver cpumask before declaring the idle state
>> > as valid and start parsing its content.
>> >
>> > The idle state enter function pointer is initialized through DT match
>> > structures passed in by the CPUidle driver, so that ARM legacy code can
>> > cope with platform specific idle entry method based on compatible
>> > string matching and the code used to initialize the enter function pointer
>> > can be moved to the DT generic layer.
>> >
>> > Acked-by: Catalin Marinas <catalin.marinas@arm.com>
>> > Acked-by: Daniel Lezcano <daniel.lezcano@linaro.org>
>> > Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
>> 
>> [...]
>> 
>> > +	idle_state->flags = CPUIDLE_FLAG_TIME_VALID;
>> > +	if (of_property_read_bool(state_node, "local-timer-stop"))
>> > +		idle_state->flags |= CPUIDLE_FLAG_TIMER_STOP;
>> > +	/*
>> > +	 * TODO:
>> > +	 *	replace with kstrdup and pointer assignment when name
>> > +	 *	and desc become string pointers
>> > +	 */
>> > +	strncpy(idle_state->name, state_node->name, CPUIDLE_NAME_LEN - 1);
>> > +	strncpy(idle_state->desc, state_node->name, CPUIDLE_DESC_LEN - 1);
>> 
>> This is a very minor concern, and shouldn't hold back this series,
>> but...
>> 
>> I was playing with this series in order to test out the qcom cpuidle
>> driver from Lina, and noticed that the state name and descriptions were
>> not terribly helpful:
>> 
>> /sys/devices/system/cpu/cpu0/cpuidle # cat state?/name
>> cpu-idle-state-
>> cpu-idle-state-
>> /sys/devices/system/cpu/cpu0/cpuidle # cat state?/desc
>> cpu-idle-state-0
>> cpu-idle-state-1
>> 
>> Turns out these strings come from the node name itself, and truncated in
>> the case of state->name, but this can be fixed in the DTS itself (c.f.
>> reply to Lina's driver.)
>> 
>> However, seeing that the node name is used to populate both the
>> state->name and ->, made me wonder if there should better way to set the
>> state->desc field to a more useful string.  Tools like powertop actually
>> use that field and it can be quite useful.
>
> Well, the truncation problem will be solved when those strings will be
> kdup'ed, so for the name I think there is not a problem, copying the
> state node is fine waiting for those strings to become pointers.
>
> For desc, there are four options:
>
> (1) enumerating idle states (but that's worse than copying the name into
>     desc since on ARM idle-state{1,2,3...} means nothing)
> (2) copying the idle state node compatible string into desc
> (3) Add an optional property to the DT bindings to describe the state
> (4) Leave code as it is
>
> (3) I am not extremely keen at this stage to re-patch the DT bindings,
> it has been an awful lot of work to make everyone agree so I would avoid
> any changes, I hope you understand (and I am not even sure DT maintainers
> would accept that, so even less keen on changing the DT bindings at this
> stage).
>
> (2) I am not sure it will clarify the description much.
>
> (1) I would rule it out. So either we accept that the name can be
> extended in length (that's going to be the case since we will
> dynamically allocate the string so there will be no truncation, to
> a reasonable extent) so (4) is fine, or we merge this code and I
> will take care of pushing for (3) in a separate patch and copy the resulting
> description into desc (if that change does not get NACK'ed).
>
> I would really want to see this code in the mailine asap since it is
> groundwork for all future CPUidle generalisation, I hope that what I am
> saying above is acceptable, please let me know what you think.

Agreed, as I stated when I rasied this issue, it's a very minor concern
and I don't think it should hold back this series.

After this series is merged, I think approach (3) is probably the best
and should be done as a follow-up patch/series.

Kevin
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Lorenzo Pieralisi Sept. 9, 2014, 3:51 p.m. UTC | #4
On Mon, Sep 08, 2014 at 04:58:53PM +0100, Kevin Hilman wrote:
> Lorenzo Pieralisi <lorenzo.pieralisi@arm.com> writes:
> 
> > On Fri, Sep 05, 2014 at 09:00:52PM +0100, Kevin Hilman wrote:
> >> Lorenzo Pieralisi <lorenzo.pieralisi@arm.com> writes:
> >> 
> >> > On most common ARM systems, the low-power states a CPU can be put into are
> >> > not discoverable in HW and require device tree bindings to describe
> >> > power down suspend operations and idle states parameters.
> >> >
> >> > In order to enable DT based idle states and configure idle drivers, this
> >> > patch implements the bulk infrastructure required to parse the device tree
> >> > idle states bindings and initialize the corresponding CPUidle driver states
> >> > data.
> >> >
> >> > The parsing API accepts a start index that defines the first idle state
> >> > that should be initialized by the parsing code in order to give new and
> >> > legacy driver flexibility over which states should be parsed using the
> >> > new DT mechanism.
> >> >
> >> > The idle states node(s) is obtained from the phandle list of the first cpu
> >> > in the driver cpumask;  the kernel checks that the idle state node phandle
> >> > is the same for all CPUs in the driver cpumask before declaring the idle state
> >> > as valid and start parsing its content.
> >> >
> >> > The idle state enter function pointer is initialized through DT match
> >> > structures passed in by the CPUidle driver, so that ARM legacy code can
> >> > cope with platform specific idle entry method based on compatible
> >> > string matching and the code used to initialize the enter function pointer
> >> > can be moved to the DT generic layer.
> >> >
> >> > Acked-by: Catalin Marinas <catalin.marinas@arm.com>
> >> > Acked-by: Daniel Lezcano <daniel.lezcano@linaro.org>
> >> > Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
> >> 
> >> [...]
> >> 
> >> > +	idle_state->flags = CPUIDLE_FLAG_TIME_VALID;
> >> > +	if (of_property_read_bool(state_node, "local-timer-stop"))
> >> > +		idle_state->flags |= CPUIDLE_FLAG_TIMER_STOP;
> >> > +	/*
> >> > +	 * TODO:
> >> > +	 *	replace with kstrdup and pointer assignment when name
> >> > +	 *	and desc become string pointers
> >> > +	 */
> >> > +	strncpy(idle_state->name, state_node->name, CPUIDLE_NAME_LEN - 1);
> >> > +	strncpy(idle_state->desc, state_node->name, CPUIDLE_DESC_LEN - 1);
> >> 
> >> This is a very minor concern, and shouldn't hold back this series,
> >> but...
> >> 
> >> I was playing with this series in order to test out the qcom cpuidle
> >> driver from Lina, and noticed that the state name and descriptions were
> >> not terribly helpful:
> >> 
> >> /sys/devices/system/cpu/cpu0/cpuidle # cat state?/name
> >> cpu-idle-state-
> >> cpu-idle-state-
> >> /sys/devices/system/cpu/cpu0/cpuidle # cat state?/desc
> >> cpu-idle-state-0
> >> cpu-idle-state-1
> >> 
> >> Turns out these strings come from the node name itself, and truncated in
> >> the case of state->name, but this can be fixed in the DTS itself (c.f.
> >> reply to Lina's driver.)
> >> 
> >> However, seeing that the node name is used to populate both the
> >> state->name and ->, made me wonder if there should better way to set the
> >> state->desc field to a more useful string.  Tools like powertop actually
> >> use that field and it can be quite useful.
> >
> > Well, the truncation problem will be solved when those strings will be
> > kdup'ed, so for the name I think there is not a problem, copying the
> > state node is fine waiting for those strings to become pointers.
> >
> > For desc, there are four options:
> >
> > (1) enumerating idle states (but that's worse than copying the name into
> >     desc since on ARM idle-state{1,2,3...} means nothing)
> > (2) copying the idle state node compatible string into desc
> > (3) Add an optional property to the DT bindings to describe the state
> > (4) Leave code as it is
> >
> > (3) I am not extremely keen at this stage to re-patch the DT bindings,
> > it has been an awful lot of work to make everyone agree so I would avoid
> > any changes, I hope you understand (and I am not even sure DT maintainers
> > would accept that, so even less keen on changing the DT bindings at this
> > stage).
> >
> > (2) I am not sure it will clarify the description much.
> >
> > (1) I would rule it out. So either we accept that the name can be
> > extended in length (that's going to be the case since we will
> > dynamically allocate the string so there will be no truncation, to
> > a reasonable extent) so (4) is fine, or we merge this code and I
> > will take care of pushing for (3) in a separate patch and copy the resulting
> > description into desc (if that change does not get NACK'ed).
> >
> > I would really want to see this code in the mailine asap since it is
> > groundwork for all future CPUidle generalisation, I hope that what I am
> > saying above is acceptable, please let me know what you think.
> 
> Agreed, as I stated when I rasied this issue, it's a very minor concern
> and I don't think it should hold back this series.
> 
> After this series is merged, I think approach (3) is probably the best
> and should be done as a follow-up patch/series.

I agree and that's what I will do, thank you Kevin (and Mark).

Lorenzo

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/cpuidle/Kconfig b/drivers/cpuidle/Kconfig
index 32748c3..8deb934 100644
--- a/drivers/cpuidle/Kconfig
+++ b/drivers/cpuidle/Kconfig
@@ -25,6 +25,9 @@  config CPU_IDLE_GOV_MENU
 	bool "Menu governor (for tickless system)"
 	default y
 
+config DT_IDLE_STATES
+	bool
+
 menu "ARM CPU Idle Drivers"
 depends on ARM
 source "drivers/cpuidle/Kconfig.arm"
diff --git a/drivers/cpuidle/Makefile b/drivers/cpuidle/Makefile
index 11edb31..002b653 100644
--- a/drivers/cpuidle/Makefile
+++ b/drivers/cpuidle/Makefile
@@ -4,6 +4,7 @@ 
 
 obj-y += cpuidle.o driver.o governor.o sysfs.o governors/
 obj-$(CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED) += coupled.o
+obj-$(CONFIG_DT_IDLE_STATES)		  += dt_idle_states.o
 
 ##################################################################################
 # ARM SoC drivers
diff --git a/drivers/cpuidle/dt_idle_states.c b/drivers/cpuidle/dt_idle_states.c
new file mode 100644
index 0000000..52f4d11
--- /dev/null
+++ b/drivers/cpuidle/dt_idle_states.c
@@ -0,0 +1,213 @@ 
+/*
+ * DT idle states parsing code.
+ *
+ * Copyright (C) 2014 ARM Ltd.
+ * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#define pr_fmt(fmt) "DT idle-states: " fmt
+
+#include <linux/cpuidle.h>
+#include <linux/cpumask.h>
+#include <linux/errno.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+
+#include "dt_idle_states.h"
+
+static int init_state_node(struct cpuidle_state *idle_state,
+			   const struct of_device_id *matches,
+			   struct device_node *state_node)
+{
+	int err;
+	const struct of_device_id *match_id;
+
+	match_id = of_match_node(matches, state_node);
+	if (!match_id)
+		return -ENODEV;
+	/*
+	 * CPUidle drivers are expected to initialize the const void *data
+	 * pointer of the passed in struct of_device_id array to the idle
+	 * state enter function.
+	 */
+	idle_state->enter = match_id->data;
+
+	err = of_property_read_u32(state_node, "wakeup-latency-us",
+				   &idle_state->exit_latency);
+	if (err) {
+		u32 entry_latency, exit_latency;
+
+		err = of_property_read_u32(state_node, "entry-latency-us",
+					   &entry_latency);
+		if (err) {
+			pr_debug(" * %s missing entry-latency-us property\n",
+				 state_node->full_name);
+			return -EINVAL;
+		}
+
+		err = of_property_read_u32(state_node, "exit-latency-us",
+					   &exit_latency);
+		if (err) {
+			pr_debug(" * %s missing exit-latency-us property\n",
+				 state_node->full_name);
+			return -EINVAL;
+		}
+		/*
+		 * If wakeup-latency-us is missing, default to entry+exit
+		 * latencies as defined in idle states bindings
+		 */
+		idle_state->exit_latency = entry_latency + exit_latency;
+	}
+
+	err = of_property_read_u32(state_node, "min-residency-us",
+				   &idle_state->target_residency);
+	if (err) {
+		pr_debug(" * %s missing min-residency-us property\n",
+			     state_node->full_name);
+		return -EINVAL;
+	}
+
+	idle_state->flags = CPUIDLE_FLAG_TIME_VALID;
+	if (of_property_read_bool(state_node, "local-timer-stop"))
+		idle_state->flags |= CPUIDLE_FLAG_TIMER_STOP;
+	/*
+	 * TODO:
+	 *	replace with kstrdup and pointer assignment when name
+	 *	and desc become string pointers
+	 */
+	strncpy(idle_state->name, state_node->name, CPUIDLE_NAME_LEN - 1);
+	strncpy(idle_state->desc, state_node->name, CPUIDLE_DESC_LEN - 1);
+	return 0;
+}
+
+/*
+ * Check that the idle state is uniform across all CPUs in the CPUidle driver
+ * cpumask
+ */
+static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
+			     const cpumask_t *cpumask)
+{
+	int cpu;
+	struct device_node *cpu_node, *curr_state_node;
+	bool valid = true;
+
+	/*
+	 * Compare idle state phandles for index idx on all CPUs in the
+	 * CPUidle driver cpumask. Start from next logical cpu following
+	 * cpumask_first(cpumask) since that's the CPU state_node was
+	 * retrieved from. If a mismatch is found bail out straight
+	 * away since we certainly hit a firmware misconfiguration.
+	 */
+	for (cpu = cpumask_next(cpumask_first(cpumask), cpumask);
+	     cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpumask)) {
+		cpu_node = of_cpu_device_node_get(cpu);
+		curr_state_node = of_parse_phandle(cpu_node, "cpu-idle-states",
+						   idx);
+		if (state_node != curr_state_node)
+			valid = false;
+
+		of_node_put(curr_state_node);
+		of_node_put(cpu_node);
+		if (!valid)
+			break;
+	}
+
+	return valid;
+}
+
+/**
+ * dt_init_idle_driver() - Parse the DT idle states and initialize the
+ *			   idle driver states array
+ * @drv:	  Pointer to CPU idle driver to be initialized
+ * @matches:	  Array of of_device_id match structures to search in for
+ *		  compatible idle state nodes. The data pointer for each valid
+ *		  struct of_device_id entry in the matches array must point to
+ *		  a function with the following signature, that corresponds to
+ *		  the CPUidle state enter function signature:
+ *
+ *		  int (*)(struct cpuidle_device *dev,
+ *			  struct cpuidle_driver *drv,
+ *			  int index);
+ *
+ * @start_idx:    First idle state index to be initialized
+ *
+ * If DT idle states are detected and are valid the state count and states
+ * array entries in the cpuidle driver are initialized accordingly starting
+ * from index start_idx.
+ *
+ * Return: number of valid DT idle states parsed, <0 on failure
+ */
+int dt_init_idle_driver(struct cpuidle_driver *drv,
+			const struct of_device_id *matches,
+			unsigned int start_idx)
+{
+	struct cpuidle_state *idle_state;
+	struct device_node *state_node, *cpu_node;
+	int i, err = 0;
+	const cpumask_t *cpumask;
+	unsigned int state_idx = start_idx;
+
+	if (state_idx >= CPUIDLE_STATE_MAX)
+		return -EINVAL;
+	/*
+	 * We get the idle states for the first logical cpu in the
+	 * driver mask (or cpu_possible_mask if the driver cpumask is not set)
+	 * and we check through idle_state_valid() if they are uniform
+	 * across CPUs, otherwise we hit a firmware misconfiguration.
+	 */
+	cpumask = drv->cpumask ? : cpu_possible_mask;
+	cpu_node = of_cpu_device_node_get(cpumask_first(cpumask));
+
+	for (i = 0; ; i++) {
+		state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i);
+		if (!state_node)
+			break;
+
+		if (!idle_state_valid(state_node, i, cpumask)) {
+			pr_warn("%s idle state not valid, bailing out\n",
+				state_node->full_name);
+			err = -EINVAL;
+			break;
+		}
+
+		if (state_idx == CPUIDLE_STATE_MAX) {
+			pr_warn("State index reached static CPU idle driver states array size\n");
+			break;
+		}
+
+		idle_state = &drv->states[state_idx++];
+		err = init_state_node(idle_state, matches, state_node);
+		if (err) {
+			pr_err("Parsing idle state node %s failed with err %d\n",
+			       state_node->full_name, err);
+			err = -EINVAL;
+			break;
+		}
+		of_node_put(state_node);
+	}
+
+	of_node_put(state_node);
+	of_node_put(cpu_node);
+	if (err)
+		return err;
+	/*
+	 * Update the driver state count only if some valid DT idle states
+	 * were detected
+	 */
+	if (i)
+		drv->state_count = state_idx;
+
+	/*
+	 * Return the number of present and valid DT idle states, which can
+	 * also be 0 on platforms with missing DT idle states or legacy DT
+	 * configuration predating the DT idle states bindings.
+	 */
+	return i;
+}
+EXPORT_SYMBOL_GPL(dt_init_idle_driver);
diff --git a/drivers/cpuidle/dt_idle_states.h b/drivers/cpuidle/dt_idle_states.h
new file mode 100644
index 0000000..4818134
--- /dev/null
+++ b/drivers/cpuidle/dt_idle_states.h
@@ -0,0 +1,7 @@ 
+#ifndef __DT_IDLE_STATES
+#define __DT_IDLE_STATES
+
+int dt_init_idle_driver(struct cpuidle_driver *drv,
+			const struct of_device_id *matches,
+			unsigned int start_idx);
+#endif