LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Ulf Hansson <ulf.hansson@linaro.org>
To: "Rafael J . Wysocki" <rjw@rjwysocki.net>,
	Sudeep Holla <sudeep.holla@arm.com>,
	Lorenzo Pieralisi <Lorenzo.Pieralisi@arm.com>,
	linux-pm@vger.kernel.org
Cc: Kevin Hilman <khilman@kernel.org>,
	Lina Iyer <ilina@codeaurora.org>,
	Lina Iyer <lina.iyer@linaro.org>,
	Ulf Hansson <ulf.hansson@linaro.org>,
	Rob Herring <robh+dt@kernel.org>,
	Daniel Lezcano <daniel.lezcano@linaro.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Stephen Boyd <sboyd@kernel.org>, Juri Lelli <juri.lelli@arm.com>,
	Geert Uytterhoeven <geert+renesas@glider.be>,
	linux-arm-kernel@lists.infradead.org,
	linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v6 19/25] drivers: firmware: psci: Add support for PM domains using genpd
Date: Wed, 14 Mar 2018 17:58:29 +0100	[thread overview]
Message-ID: <1521046715-30683-20-git-send-email-ulf.hansson@linaro.org> (raw)
In-Reply-To: <1521046715-30683-1-git-send-email-ulf.hansson@linaro.org>

When the hierarchical layout is used in DT, as to describe the PM topology
for the CPUs, which are managed by PSCI, we want to be able to initialize
and setup the corresponding PM domain data structures.

Let's make this possible via adding a new file, psci_pm_domains.c and
implement the needed interface towards the generic PM domain (aka genpd).
Share a helper function, psci_dt_init_pm_domains(), which the regular PSCI
firmware driver may call when it needs to initialize the PM topology using
genpd.

In principle, the implementation consists of allocating/initializing the
genpd data structures, parsing the domain-idle states DT bindings via
calling of_genpd_parse_idle_states() and to call pm_genpd_init() for the
allocated genpds.

Finally, one genpd OF provider is added per genpd. Via DT, this enables
devices, including CPU devices, to be attached to the created genpds.

Cc: Lina Iyer <ilina@codeaurora.org>
Co-developed-by: Lina Iyer <lina.iyer@linaro.org>
Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
---
 drivers/firmware/Makefile         |   2 +-
 drivers/firmware/psci.h           |   6 ++
 drivers/firmware/psci_pm_domain.c | 180 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 187 insertions(+), 1 deletion(-)
 create mode 100644 drivers/firmware/psci_pm_domain.c

diff --git a/drivers/firmware/Makefile b/drivers/firmware/Makefile
index b248238..877260e 100644
--- a/drivers/firmware/Makefile
+++ b/drivers/firmware/Makefile
@@ -2,7 +2,7 @@
 #
 # Makefile for the linux kernel.
 #
-obj-$(CONFIG_ARM_PSCI_FW)	+= psci.o
+obj-$(CONFIG_ARM_PSCI_FW)	+= psci.o psci_pm_domain.o
 obj-$(CONFIG_ARM_PSCI_CHECKER)	+= psci_checker.o
 obj-$(CONFIG_ARM_SCPI_PROTOCOL)	+= arm_scpi.o
 obj-$(CONFIG_ARM_SCPI_POWER_DOMAIN) += scpi_pm_domain.o
diff --git a/drivers/firmware/psci.h b/drivers/firmware/psci.h
index a2b4be5..8b6fe51 100644
--- a/drivers/firmware/psci.h
+++ b/drivers/firmware/psci.h
@@ -10,4 +10,10 @@ void psci_set_domain_state(u32 state);
 
 int psci_dt_parse_state_node(struct device_node *np, u32 *state);
 
+#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
+int psci_dt_init_pm_domains(struct device_node *np);
+#else
+static inline int psci_dt_init_pm_domains(struct device_node *np) { return 0; }
+#endif
+
 #endif /* __PSCI_H */
diff --git a/drivers/firmware/psci_pm_domain.c b/drivers/firmware/psci_pm_domain.c
new file mode 100644
index 0000000..f54819e
--- /dev/null
+++ b/drivers/firmware/psci_pm_domain.c
@@ -0,0 +1,180 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * PM domains for CPUs via genpd - managed by PSCI.
+ *
+ * Copyright (C) 2018 Linaro Ltd.
+ * Author: Ulf Hansson <ulf.hansson@linaro.org>
+ *
+ */
+
+#define pr_fmt(fmt) "psci: " fmt
+
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/pm_domain.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+
+#include "psci.h"
+
+#ifdef CONFIG_PM_GENERIC_DOMAINS_OF
+static int psci_pd_power_off(struct generic_pm_domain *pd)
+{
+	struct genpd_power_state *state = &pd->states[pd->state_idx];
+	u32 *pd_state;
+	u32 composite_pd_state;
+
+	if (!state->data)
+		return 0;
+
+	pd_state = state->data;
+	composite_pd_state = *pd_state | psci_get_domain_state();
+	psci_set_domain_state(composite_pd_state);
+
+	return 0;
+}
+
+static int psci_dt_parse_pd_states(struct genpd_power_state *states,
+				   int state_count)
+{
+	int i, err;
+	u32 *psci_states;
+
+	if (!state_count)
+		return 0;
+
+	psci_states = kcalloc(state_count, sizeof(psci_states), GFP_KERNEL);
+	if (!psci_states)
+		return -ENOMEM;
+
+	for (i = 0; i < state_count; i++) {
+		err = psci_dt_parse_state_node(to_of_node(states[i].fwnode),
+					       &psci_states[i]);
+		if (err) {
+			kfree(psci_states);
+			return err;
+		}
+	}
+
+	for (i = 0; i < state_count; i++)
+		states[i].data = &psci_states[i];
+
+	return 0;
+}
+
+static int psci_dt_init_genpd(struct device_node *np,
+			      struct genpd_power_state *states,
+			      unsigned int state_count)
+{
+	struct generic_pm_domain *pd;
+	struct dev_power_governor *pd_gov;
+	int ret = -ENOMEM;
+
+	pd = kzalloc(sizeof(*pd), GFP_KERNEL);
+	if (!pd)
+		return -ENOMEM;
+
+	pd->name = kasprintf(GFP_KERNEL, "%pOF", np);
+	if (!pd->name)
+		goto free_pd;
+
+	pd->name = kbasename(pd->name);
+	pd->power_off = psci_pd_power_off;
+	pd->states = states;
+	pd->state_count = state_count;
+	pd->flags |= GENPD_FLAG_IRQ_SAFE | GENPD_FLAG_CPU_DOMAIN;
+
+	/* Use governor for CPU PM domains if it has some states to manage. */
+	pd_gov = state_count > 0 ? &pm_domain_cpu_gov : NULL;
+
+	ret = pm_genpd_init(pd, pd_gov, false);
+	if (ret)
+		goto free_name;
+
+	ret = of_genpd_add_provider_simple(np, pd);
+	if (ret)
+		goto remove_pd;
+
+	pr_info("init PM domain %s\n", pd->name);
+	return 0;
+
+remove_pd:
+	pm_genpd_remove(pd);
+free_name:
+	kfree(pd->name);
+free_pd:
+	kfree(pd);
+	pr_err("failed to init PM domain ret=%d %pOF\n", ret, np);
+	return ret;
+}
+
+static int psci_dt_set_genpd_topology(struct device_node *np)
+{
+	struct device_node *node;
+	struct of_phandle_args child, parent;
+	int ret;
+
+	for_each_child_of_node(np, node) {
+		if (of_parse_phandle_with_args(node, "power-domains",
+					       "#power-domain-cells", 0,
+					       &parent))
+			continue;
+
+		child.np = node;
+		child.args_count = 0;
+
+		ret = of_genpd_add_subdomain(&parent, &child);
+		of_node_put(parent.np);
+		if (ret) {
+			of_node_put(node);
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
+int psci_dt_init_pm_domains(struct device_node *np)
+{
+	struct device_node *node;
+	struct genpd_power_state *states;
+	int state_count;
+	int pd_count = 0;
+	int ret;
+
+	/* Parse child nodes for "#power-domain-cells". */
+	for_each_child_of_node(np, node) {
+		if (!of_find_property(node, "#power-domain-cells", NULL))
+			continue;
+
+		ret = of_genpd_parse_idle_states(node, &states, &state_count);
+		if (ret)
+			goto err_put;
+
+		ret = psci_dt_parse_pd_states(states, state_count);
+		if (ret)
+			goto err_put;
+
+		ret = psci_dt_init_genpd(node, states, state_count);
+		if (ret)
+			goto err_put;
+
+		pd_count++;
+	}
+
+	if (!pd_count)
+		return 0;
+
+	ret = psci_dt_set_genpd_topology(np);
+	if (ret)
+		goto err_msg;
+
+	return pd_count;
+
+err_put:
+	of_node_put(node);
+err_msg:
+	pr_err("failed to create PM domains ret=%d\n", ret);
+	return ret;
+}
+#endif
-- 
2.7.4

  parent reply	other threads:[~2018-03-14 17:01 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-14 16:58 [PATCH v6 00/25] PM / Domains: Support hierarchical CPU arrangement (PSCI/ARM) Ulf Hansson
2018-03-14 16:58 ` [PATCH v6 01/25] PM / Domains: Don't treat zero found compatible idle states as an error Ulf Hansson
2018-03-14 16:58 ` [PATCH v6 02/25] PM / Domains: Deal with multiple states but no governor in genpd Ulf Hansson
2018-03-14 16:58 ` [PATCH v6 03/25] PM / Domains: Add generic data pointer to genpd_power_state struct Ulf Hansson
2018-03-14 16:58 ` [PATCH v6 04/25] PM / Domains: Add support for CPU devices to genpd Ulf Hansson
2018-03-14 16:58 ` [PATCH v6 05/25] PM / Domains: Add helper functions to attach/detach CPUs to/from genpd Ulf Hansson
2018-03-14 16:58 ` [PATCH v6 06/25] timer: Export next wakeup time of a CPU Ulf Hansson
2018-03-14 17:19   ` Mark Rutland
2018-03-14 16:58 ` [PATCH v6 07/25] PM / Domains: Add genpd governor for CPUs Ulf Hansson
2018-03-14 16:58 ` [PATCH v6 08/25] PM / Domains: Extend genpd CPU governor to cope with QoS constraints Ulf Hansson
2018-03-14 16:58 ` [PATCH v6 09/25] kernel/cpu_pm: Manage runtime PM in the idle path for CPUs Ulf Hansson
2018-03-14 16:58 ` [PATCH v6 10/25] dt: psci: Update DT bindings to support hierarchical PSCI states Ulf Hansson
2018-03-14 16:58 ` [PATCH v6 11/25] of: base: Add of_get_cpu_state_node() to get idle states for a CPU node Ulf Hansson
2018-03-18 12:51   ` Rob Herring
2018-03-14 16:58 ` [PATCH v6 12/25] cpuidle: dt: Support hierarchical CPU idle states Ulf Hansson
2018-03-14 16:58 ` [PATCH v6 13/25] drivers: firmware: psci: Split psci_dt_cpu_init_idle() Ulf Hansson
2018-03-14 16:58 ` [PATCH v6 14/25] drivers: firmware: psci: Support hierarchical CPU idle states Ulf Hansson
2018-03-14 16:58 ` [PATCH v6 15/25] drivers: firmware: psci: Simplify error path of psci_dt_init() Ulf Hansson
2018-03-14 17:25   ` Mark Rutland
2018-03-14 16:58 ` [PATCH v6 16/25] drivers: firmware: psci: Announce support for OS initiated suspend mode Ulf Hansson
2018-03-14 16:58 ` [PATCH v6 17/25] drivers: firmware: psci: Prepare to use " Ulf Hansson
2018-03-14 16:58 ` [PATCH v6 18/25] drivers: firmware: psci: Share a few internal PSCI functions Ulf Hansson
2018-03-14 17:30   ` Mark Rutland
2018-03-14 16:58 ` Ulf Hansson [this message]
2018-03-14 16:58 ` [PATCH v6 20/25] drivers: firmware: psci: Introduce psci_dt_topology_init() Ulf Hansson
2018-03-14 17:38   ` Mark Rutland
2018-04-10  7:19     ` Ulf Hansson
2018-04-10 11:10       ` Mark Rutland
2018-04-10 12:25         ` Ulf Hansson
2018-04-18 11:32           ` Mark Rutland
2018-03-14 16:58 ` [PATCH v6 21/25] drivers: firmware: psci: Try to attach CPU devices to their PM domains Ulf Hansson
2018-03-14 16:58 ` [PATCH v6 22/25] drivers: firmware: psci: Deal with CPU hotplug when using OSI mode Ulf Hansson
2018-03-14 16:58 ` [PATCH v6 23/25] MAINTAINERS: Update files for PSCI Ulf Hansson
2018-03-14 17:40   ` Mark Rutland
2018-03-14 16:58 ` [PATCH v6 24/25] arm64: kernel: Respect the hierarchical CPU topology in DT " Ulf Hansson
2018-03-14 16:58 ` [PATCH v6 25/25] arm64: dts: Convert to the hierarchical CPU topology layout for MSM8916 Ulf Hansson
2018-03-14 17:19 ` [PATCH v6 00/25] PM / Domains: Support hierarchical CPU arrangement (PSCI/ARM) Mark Rutland
2018-03-15 14:19   ` Ulf Hansson
2018-03-15 11:00 ` Geert Uytterhoeven
2018-03-15 13:14   ` Ulf Hansson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1521046715-30683-20-git-send-email-ulf.hansson@linaro.org \
    --to=ulf.hansson@linaro.org \
    --cc=Lorenzo.Pieralisi@arm.com \
    --cc=daniel.lezcano@linaro.org \
    --cc=geert+renesas@glider.be \
    --cc=ilina@codeaurora.org \
    --cc=juri.lelli@arm.com \
    --cc=khilman@kernel.org \
    --cc=lina.iyer@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=rjw@rjwysocki.net \
    --cc=robh+dt@kernel.org \
    --cc=sboyd@kernel.org \
    --cc=sudeep.holla@arm.com \
    --cc=tglx@linutronix.de \
    --cc=vincent.guittot@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).