From patchwork Thu Aug 25 20:03:16 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lina Iyer X-Patchwork-Id: 9299891 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 71B8C607D8 for ; Thu, 25 Aug 2016 20:15:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6250828F69 for ; Thu, 25 Aug 2016 20:15:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 55492291F1; Thu, 25 Aug 2016 20:15:10 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 9F90528F69 for ; Thu, 25 Aug 2016 20:15:09 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1bd11p-0006Ab-Oz; Thu, 25 Aug 2016 20:13:33 +0000 Received: from casper.infradead.org ([2001:770:15f::2]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1bd11R-00057K-UM for linux-arm-kernel@bombadil.infradead.org; Thu, 25 Aug 2016 20:13:10 +0000 Received: from mail-pa0-x235.google.com ([2607:f8b0:400e:c03::235]) by casper.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1bd0sx-0005xX-Ip for linux-arm-kernel@lists.infradead.org; Thu, 25 Aug 2016 20:04:25 +0000 Received: by mail-pa0-x235.google.com with SMTP id fi15so19725116pac.1 for ; Thu, 25 Aug 2016 13:04:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=gXEGqQUPAO3aKAtRQKl5SQRCk4rVDGjrDQq1P7fYe5U=; b=C5hW+vq2pwVu045X++TCNg+SIdqvtwc2+M5zLLUkuu8ZsS3wdrODQUHFcdLpu94sa6 oyXat1H9mgl8bDeWft5G/pnwLKQYARzPesCduYxLAa7+9evacPBNsdGSW39kDYHRNMA1 SmTe2lD+9ft5Jb05Qxo6FyyMJ7AtbTIZzN9mk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=gXEGqQUPAO3aKAtRQKl5SQRCk4rVDGjrDQq1P7fYe5U=; b=V+aFFtnaus2DvzGkC9CHwd858Il4MpkOA/QcCflTggrNZXh9OBBCKIbqea+fWJ+OwB UX+8eePYwnwCy4jaCBE2aMwukv9bChOywXl4qEH8AOxLv4LWyClw8TU5Lx5dqt5Ujbbp UfX75K4hjLmaQrIhND7PjnGE45bHGCLg9SqqnnHHR4RQSdA2XGUD5i0AtI2R1yhrHVkp eez7hJfHYNwyFQOKGOTIgIUs1hOHtjrEZSm2HFwXs4qmI1fxBrSYG0JWPqLK84P+432d vY4L5C4H2avMAYYbC4ynQlam/M4lVyXogpY8F4Z0/4OYYURDdES+rv/fZYATYfP2b6GZ QQng== X-Gm-Message-State: AE9vXwMbxazViNqo3GlbwNviB6Gzd2TMjDrKigNcI7gv/WEtgE7caMGELWvUZMVxSe+1jgdJ X-Received: by 10.66.193.163 with SMTP id hp3mr19742416pac.73.1472155431461; Thu, 25 Aug 2016 13:03:51 -0700 (PDT) Received: from ubuntu.localdomain (i-global254.qualcomm.com. [199.106.103.254]) by smtp.gmail.com with ESMTPSA id u1sm22841644pfu.12.2016.08.25.13.03.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 25 Aug 2016 13:03:50 -0700 (PDT) From: Lina Iyer To: ulf.hansson@linaro.org, khilman@kernel.org, rjw@rjwysocki.net, linux-pm@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH v4 07/16] PM / cpu_domains: Initialize CPU PM domains from DT Date: Thu, 25 Aug 2016 14:03:16 -0600 Message-Id: <1472155405-41841-8-git-send-email-lina.iyer@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1472155405-41841-1-git-send-email-lina.iyer@linaro.org> References: <1472155405-41841-1-git-send-email-lina.iyer@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160825_210423_842457_1A66900A X-CRM114-Status: GOOD ( 27.51 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: lorenzo.pieralisi@arm.com, Juri.Lelli@arm.com, linux-arm-msm@vger.kernel.org, sboyd@codeaurora.org, brendan.jackman@arm.com, sudeep.holla@arm.com, andy.gross@linaro.org, Lina Iyer MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Add helper functions to parse DT and initialize the CPU PM domains and attach CPU to their respective domains using information provided in the DT. For each CPU in the DT, we identify the domain provider; initialize and register the PM domain if isn't already registered and attach all the CPU devices to the domain. Usually, when there are multiple clusters of CPUs, there is a top level coherency domain that is dependent on these individual domains. All domains thus created are marked IRQ safe automatically and therefore may be powered down when the CPUs in the domain are powered down by cpuidle. Cc: Kevin Hilman Suggested-by: Ulf Hansson Signed-off-by: Lina Iyer --- drivers/base/power/cpu_domains.c | 190 +++++++++++++++++++++++++++++++++++++++ include/linux/cpu_domains.h | 18 ++++ 2 files changed, 208 insertions(+) diff --git a/drivers/base/power/cpu_domains.c b/drivers/base/power/cpu_domains.c index 73e493b..8bf61e2 100644 --- a/drivers/base/power/cpu_domains.c +++ b/drivers/base/power/cpu_domains.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -189,3 +190,192 @@ fail: return ERR_PTR(ret); } EXPORT_SYMBOL(cpu_pd_init); + +static struct generic_pm_domain *alloc_genpd(const char *name) +{ + struct generic_pm_domain *genpd; + + genpd = kzalloc(sizeof(*genpd), GFP_KERNEL); + if (!genpd) + return ERR_PTR(-ENOMEM); + + genpd->name = kstrndup(name, CPU_PD_NAME_MAX, GFP_KERNEL); + if (!genpd->name) { + kfree(genpd); + return ERR_PTR(-ENOMEM); + } + + return genpd; +} + +/** + * of_init_cpu_pm_domain() - Initialize a CPU PM domain from a device node + * + * @dn: The domain provider's device node + * @ops: The power_on/_off callbacks for the domain + * + * Returns the generic_pm_domain (genpd) pointer to the domain on success + */ +static struct generic_pm_domain *of_init_cpu_pm_domain(struct device_node *dn, + const struct cpu_pd_ops *ops) +{ + struct cpu_pm_domain *pd = NULL; + struct generic_pm_domain *genpd = NULL; + int ret = -ENOMEM; + + if (!of_device_is_available(dn)) + return ERR_PTR(-ENODEV); + + genpd = alloc_genpd(dn->full_name); + if (IS_ERR(genpd)) + return genpd; + + genpd->of_node = dn; + + /* Populate platform specific states from DT */ + if (ops->populate_state_data) { + struct device_node *np; + int i; + + /* Initialize the arm,idle-state properties */ + ret = pm_genpd_of_parse_power_states(genpd); + if (ret) { + pr_warn("%s domain states not initialized (%d)\n", + dn->full_name, ret); + goto fail; + } + for (i = 0; i < genpd->state_count; i++) { + ret = ops->populate_state_data(genpd->states[i].of_node, + &genpd->states[i].param); + of_node_put(np); + if (ret) + goto fail; + } + } + + genpd = cpu_pd_init(genpd, ops); + if (IS_ERR(genpd)) + goto fail; + + ret = of_genpd_add_provider_simple(dn, genpd); + if (ret) + pr_warn("Unable to add genpd %s as provider\n", + pd->genpd->name); + + return genpd; +fail: + kfree(genpd->name); + kfree(genpd); + if (pd) + kfree(pd->cpus); + kfree(pd); + return ERR_PTR(ret); +} + +static struct generic_pm_domain *of_get_cpu_domain(struct device_node *dn, + const struct cpu_pd_ops *ops, int cpu) +{ + struct of_phandle_args args; + struct generic_pm_domain *genpd, *parent; + int ret; + + /* Do we have this domain? If not, create the domain */ + args.np = dn; + args.args_count = 0; + + genpd = of_genpd_get_from_provider(&args); + if (!IS_ERR(genpd)) + return genpd; + + genpd = of_init_cpu_pm_domain(dn, ops); + if (IS_ERR(genpd)) + return genpd; + + /* Is there a domain provider for this domain? */ + ret = of_parse_phandle_with_args(dn, "power-domains", + "#power-domain-cells", 0, &args); + if (ret < 0) + goto skip_parent; + + /* Find its parent and attach this domain to it, recursively */ + parent = of_get_cpu_domain(args.np, ops, cpu); + if (IS_ERR(parent)) + goto skip_parent; + + ret = cpu_pd_attach_domain(parent, genpd); + if (ret) + pr_err("Unable to attach domain %s to parent %s\n", + genpd->name, parent->name); + +skip_parent: + of_node_put(dn); + return genpd; +} + +/** + * of_setup_cpu_pd_single() - Setup the PM domains for a CPU + * + * @cpu: The CPU for which the PM domain is to be set up. + * @ops: The PM domain suspend/resume ops for the CPU's domain + * + * If the CPU PM domain exists already, then the CPU is attached to + * that CPU PD. If it doesn't, the domain is created, the @ops are + * set for power_on/power_off callbacks and then the CPU is attached + * to that domain. If the domain was created outside this framework, + * then we do not attach the CPU to the domain. + */ +int of_setup_cpu_pd_single(int cpu, const struct cpu_pd_ops *ops) +{ + + struct device_node *dn, *np; + struct generic_pm_domain *genpd; + struct cpu_pm_domain *cpu_pd; + + np = of_get_cpu_node(cpu, NULL); + if (!np) + return -ENODEV; + + dn = of_parse_phandle(np, "power-domains", 0); + of_node_put(np); + if (!dn) + return -ENODEV; + + /* Find the genpd for this CPU, create if not found */ + genpd = of_get_cpu_domain(dn, ops, cpu); + of_node_put(dn); + if (IS_ERR(genpd)) + return PTR_ERR(genpd); + + cpu_pd = to_cpu_pd(genpd); + if (!cpu_pd) { + pr_err("%s: Genpd was created outside CPU PM domains\n", + __func__); + return -ENOENT; + } + + return cpu_pd_attach_cpu(genpd, cpu); +} +EXPORT_SYMBOL(of_setup_cpu_pd_single); + +/** + * of_setup_cpu_pd() - Setup the PM domains for all CPUs + * + * @ops: The PM domain suspend/resume ops for all the domains + * + * Setup the CPU PM domain and attach all possible CPUs to their respective + * domains. The domains are created if not already and then attached. + */ +int of_setup_cpu_pd(const struct cpu_pd_ops *ops) +{ + int cpu; + int ret; + + for_each_possible_cpu(cpu) { + ret = of_setup_cpu_pd_single(cpu, ops); + if (ret) + break; + } + + return ret; +} +EXPORT_SYMBOL(of_setup_cpu_pd); diff --git a/include/linux/cpu_domains.h b/include/linux/cpu_domains.h index 3a0a027..736d9e6 100644 --- a/include/linux/cpu_domains.h +++ b/include/linux/cpu_domains.h @@ -14,8 +14,10 @@ #include struct cpumask; +struct device_node; struct cpu_pd_ops { + int (*populate_state_data)(struct device_node *n, u32 *param); int (*power_off)(u32 state_idx, u32 param, const struct cpumask *mask); int (*power_on)(void); }; @@ -46,4 +48,20 @@ static inline int cpu_pd_attach_cpu(struct generic_pm_domain *genpd, int cpu) #endif /* CONFIG_PM_GENERIC_DOMAINS */ +#ifdef CONFIG_PM_GENERIC_DOMAINS_OF + +int of_setup_cpu_pd_single(int cpu, const struct cpu_pd_ops *ops); + +int of_setup_cpu_pd(const struct cpu_pd_ops *ops); + +#else + +static inline int of_setup_cpu_pd_single(int cpu, const struct cpu_pd_ops *ops) +{ return -ENODEV; } + +static inline int of_setup_cpu_pd(const struct cpu_pd_ops *ops) +{ return -ENODEV; } + +#endif /* CONFIG_PM_GENERIC_DOMAINS_OF */ + #endif /* __CPU_DOMAINS_H__ */