From: Ulf Hansson <ulf.hans...@linaro.org>

Introduce two new genpd helper functions, of_genpd_attach|detach_cpu(),
which takes the CPU-number as an in-parameter.

To attach a CPU to a genpd, of_genpd_attach_cpu() starts by fetching the
struct device belonging to the CPU. Then it calls genpd_dev_pm_attach(),
which via DT tries to hook up the CPU device to its corresponding PM
domain. If it succeeds, of_genpd_attach_cpu() continues to prepare/enable
runtime PM of the device.

To detach a CPU from its PM domain, of_genpd_attach_cpu() reverse the
operations made from of_genpd_attach_cpu(). However, first it checks that
the CPU device has a valid PM domain pointer assigned, as to make sure it
belongs to genpd.

Cc: Lina Iyer <il...@codeaurora.org>
Co-developed-by: Lina Iyer <lina.i...@linaro.org>
Signed-off-by: Ulf Hansson <ulf.hans...@linaro.org>
[rpls...@codeaurora.org: Fix compilation issue]
Signed-off-by: Raju P.L.S.S.S.N <rpls...@codeaurora.org>
(am from https://patchwork.kernel.org/patch/10478167/)
---
 drivers/base/power/domain.c | 70 +++++++++++++++++++++++++++++++++++++++++++++
 include/linux/pm_domain.h   |  9 ++++++
 2 files changed, 79 insertions(+)

diff --git a/drivers/base/power/domain.c b/drivers/base/power/domain.c
index 4b57141..82973e3 100644
--- a/drivers/base/power/domain.c
+++ b/drivers/base/power/domain.c
@@ -6,6 +6,7 @@
  * This file is released under the GPLv2.
  */
 
+#include <linux/cpu.h>
 #include <linux/delay.h>
 #include <linux/kernel.h>
 #include <linux/io.h>
@@ -2398,6 +2399,75 @@ struct device *genpd_dev_pm_attach_by_name(struct device 
*dev, char *name)
        return genpd_dev_pm_attach_by_id(dev, index);
 }
 
+/*
+ * of_genpd_attach_cpu() - Attach a CPU to its PM domain
+ * @cpu: The CPU to be attached.
+ *
+ * Parses the OF node of the CPU's device, to find a PM domain specifier. If
+ * such is found, attaches the CPU's device to the retrieved pm_domain ops and
+ * enables runtime PM for it. This to allow the CPU to be power managed through
+ * its PM domain.
+ *
+ * Returns zero when successfully attached the CPU's device to its PM domain,
+ * else a negative error code.
+ */
+int of_genpd_attach_cpu(int cpu)
+{
+       struct device *dev = get_cpu_device(cpu);
+       int ret;
+
+       if (!dev) {
+               pr_warn("genpd: no dev for cpu%d\n", cpu);
+               return -ENODEV;
+       }
+
+       ret = genpd_dev_pm_attach(dev);
+       if (ret != 1) {
+               dev_warn(dev, "genpd: attach cpu failed %d\n", ret);
+               return ret < 0 ? ret : -ENODEV;
+       }
+
+       pm_runtime_irq_safe(dev);
+       pm_runtime_get_noresume(dev);
+       pm_runtime_set_active(dev);
+       pm_runtime_enable(dev);
+
+       dev_info(dev, "genpd: attached cpu\n");
+       return 0;
+}
+EXPORT_SYMBOL(of_genpd_attach_cpu);
+
+/**
+ * of_genpd_detach_cpu() - Detach a CPU from its PM domain
+ * @cpu: The CPU to be detached.
+ *
+ * Detach the CPU's device from its corresponding PM domain. If detaching is
+ * completed successfully, disable runtime PM and restore the runtime PM usage
+ * count for the CPU's device.
+ */
+void of_genpd_detach_cpu(int cpu)
+{
+       struct device *dev = get_cpu_device(cpu);
+
+       if (!dev) {
+               pr_warn("genpd: no dev for cpu%d\n", cpu);
+               return;
+       }
+
+       /* Check that the device is attached to a genpd. */
+       if (!(dev->pm_domain && dev->pm_domain->detach == genpd_dev_pm_detach))
+               return;
+
+       genpd_dev_pm_detach(dev, true);
+
+       pm_runtime_disable(dev);
+       pm_runtime_put_noidle(dev);
+       pm_runtime_reinit(dev);
+
+       dev_info(dev, "genpd: detached cpu\n");
+}
+EXPORT_SYMBOL(of_genpd_detach_cpu);
+
 static const struct of_device_id idle_state_match[] = {
        { .compatible = "domain-idle-state", },
        { }
diff --git a/include/linux/pm_domain.h b/include/linux/pm_domain.h
index 776c546..48850af 100644
--- a/include/linux/pm_domain.h
+++ b/include/linux/pm_domain.h
@@ -241,6 +241,8 @@ struct device *genpd_dev_pm_attach_by_id(struct device *dev,
                                         unsigned int index);
 struct device *genpd_dev_pm_attach_by_name(struct device *dev,
                                           char *name);
+int of_genpd_attach_cpu(int cpu);
+void of_genpd_detach_cpu(int cpu);
 #else /* !CONFIG_PM_GENERIC_DOMAINS_OF */
 static inline int of_genpd_add_provider_simple(struct device_node *np,
                                        struct generic_pm_domain *genpd)
@@ -298,6 +300,13 @@ static inline struct device 
*genpd_dev_pm_attach_by_name(struct device *dev,
        return NULL;
 }
 
+static inline int of_genpd_attach_cpu(int cpu)
+{
+       return -ENODEV;
+}
+
+static inline void of_genpd_detach_cpu(int cpu) {}
+
 static inline
 struct generic_pm_domain *of_genpd_remove_last(struct device_node *np)
 {
-- 
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member
of the Code Aurora Forum, hosted by The Linux Foundation.

Reply via email to