From patchwork Thu Aug 4 23:04:53 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lina Iyer X-Patchwork-Id: 73328 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp1636819qga; Thu, 4 Aug 2016 16:06:02 -0700 (PDT) X-Received: by 10.98.11.133 with SMTP id 5mr128519568pfl.160.1470351959983; Thu, 04 Aug 2016 16:05:59 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y80si16817659pfi.205.2016.08.04.16.05.59; Thu, 04 Aug 2016 16:05:59 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-pm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: best guess record for domain of linux-pm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1759170AbcHDXFz (ORCPT + 14 others); Thu, 4 Aug 2016 19:05:55 -0400 Received: from mail-pa0-f52.google.com ([209.85.220.52]:36279 "EHLO mail-pa0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1759140AbcHDXFt (ORCPT ); Thu, 4 Aug 2016 19:05:49 -0400 Received: by mail-pa0-f52.google.com with SMTP id pp5so86666790pac.3 for ; Thu, 04 Aug 2016 16:05:48 -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=UeLwiINLkZjTrndnohI6kcYXWc481OfXesF8Ppu4J30=; b=QMhWRBRHwWRs3wNGSMpe4kV6GfPSEo83RcLVtP2W//f2nxfnswf577vFee83RddUf5 528gLvk9P4ugzmS/SegrH4Bxdz6JDCk1eoahvLL76U0MtCCZAn2+yiXfrJkVLV19vjhl rrfIio7peQhu3/WvIPtfV6DWD9Nj8BgqU6JwQ= 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=UeLwiINLkZjTrndnohI6kcYXWc481OfXesF8Ppu4J30=; b=AcwjoqTHG5xsqO48Q9z4Witnlz9zPBmrT2YrYnkr1WdRbF6GvV/yjWOkEeIltXm0GR RJuLV2YG+l2Yg3bWxF8wVdx5T8bB1a/iJttZDPP6MSLpJ8Cxs0iNlDioCiHf79eMDGCa mpA6mOrpXXr2aCWvBxI6G0VtCKOj31eyWaSgxG8SMjilN4WlOJtx8olCQaSPipyQzjnb twBK8kkbGwB2g29ZEGC5Jl1aP5G2/tX8/rOMkhCBND+XPgdPRyrYXCvkJ/hkjwCssP08 r0XHcHww7vRj19/CsvFwqpN6HCjG68I0+6NuThUhNapvsJ+y2KPHFcl0Z8LVz+GrwBJN Uj2g== X-Gm-Message-State: AEkooutDmWICMZ1DwYWYxtbaWigTlOLsMbUxFOlKNPxbDwbkKUFFkf5IcQaHwloLRsyKfC02 X-Received: by 10.66.146.69 with SMTP id ta5mr131912320pab.157.1470351948082; Thu, 04 Aug 2016 16:05:48 -0700 (PDT) Received: from ubuntu.localdomain (i-global254.qualcomm.com. [199.106.103.254]) by smtp.gmail.com with ESMTPSA id x66sm22736214pfb.86.2016.08.04.16.05.45 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 04 Aug 2016 16:05:46 -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 Cc: andy.gross@linaro.org, sboyd@codeaurora.org, linux-arm-msm@vger.kernel.org, Lina Iyer , Daniel Lezcano , Lorenzo Pieralisi Subject: [PATCH v3 06/15] PM / cpu_domains: Setup PM domains for CPUs/clusters Date: Thu, 4 Aug 2016 17:04:53 -0600 Message-Id: <1470351902-43103-7-git-send-email-lina.iyer@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1470351902-43103-1-git-send-email-lina.iyer@linaro.org> References: <1470351902-43103-1-git-send-email-lina.iyer@linaro.org> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Define and add Generic PM domains (genpd) for CPU clusters. Many new SoCs group CPUs as clusters. Clusters share common resources like power rails, caches, VFP, Coresight etc. When all CPUs in the cluster are idle, these shared resources may also be put in their idle state. CPUs may be associated with their domain providers in DT. The domains in turn may be associated with their providers. This is clean way to model the cluster hierarchy like that of ARM's big.little architecture. The current patch expects OF support to initialize and setup CPU PM domains. 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. Reading DT, initializing Generic PM domains, attaching CPUs to it domains are common functionalities across ARM SoCs. Provide a common set of APIs to setup PM domains for CPU clusters and its parents. The platform drivers may just call of_setup_cpu_pd() to do a single step setup of CPU domains. Cc: Ulf Hansson Cc: Daniel Lezcano Cc: Lorenzo Pieralisi Suggested-by: Kevin Hilman Signed-off-by: Lina Iyer --- drivers/base/power/Makefile | 1 + drivers/base/power/cpu_domains.c | 310 +++++++++++++++++++++++++++++++++++++++ include/linux/cpu_domains.h | 39 +++++ 3 files changed, 350 insertions(+) create mode 100644 drivers/base/power/cpu_domains.c create mode 100644 include/linux/cpu_domains.h -- 2.7.4 -- To unsubscribe from this list: send the line "unsubscribe linux-pm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/base/power/Makefile b/drivers/base/power/Makefile index 5998c53..9883e89 100644 --- a/drivers/base/power/Makefile +++ b/drivers/base/power/Makefile @@ -3,6 +3,7 @@ obj-$(CONFIG_PM_SLEEP) += main.o wakeup.o obj-$(CONFIG_PM_TRACE_RTC) += trace.o obj-$(CONFIG_PM_OPP) += opp/ obj-$(CONFIG_PM_GENERIC_DOMAINS) += domain.o domain_governor.o +obj-$(CONFIG_PM_GENERIC_DOMAINS_OF) += cpu_domains.o obj-$(CONFIG_HAVE_CLK) += clock_ops.o ccflags-$(CONFIG_DEBUG_DRIVER) := -DDEBUG diff --git a/drivers/base/power/cpu_domains.c b/drivers/base/power/cpu_domains.c new file mode 100644 index 0000000..a25a77e --- /dev/null +++ b/drivers/base/power/cpu_domains.c @@ -0,0 +1,310 @@ +/* + * drivers/base/power/cpu_domains.c - Helper functions to create CPU PM domains. + * + * Copyright (C) 2016 Linaro Ltd. + * + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define CPU_PD_NAME_MAX 36 + +struct cpu_pm_domain { + struct list_head link; + struct cpu_pd_ops ops; + struct generic_pm_domain *genpd; + struct cpu_pm_domain *parent; + cpumask_var_t cpus; +}; + +/* List of CPU PM domains we care about */ +static LIST_HEAD(of_cpu_pd_list); +static DEFINE_MUTEX(cpu_pd_list_lock); + +static inline +struct cpu_pm_domain *to_cpu_pd(struct generic_pm_domain *d) +{ + struct cpu_pm_domain *pd; + struct cpu_pm_domain *res = NULL; + + rcu_read_lock(); + list_for_each_entry_rcu(pd, &of_cpu_pd_list, link) + if (pd->genpd == d) { + res = pd; + break; + } + rcu_read_unlock(); + + return res; +} + +static int cpu_pd_attach_cpu(struct cpu_pm_domain *cpu_pd, int cpu) +{ + int ret; + struct device *cpu_dev; + + cpu_dev = get_cpu_device(cpu); + if (!cpu_dev) { + pr_warn("%s: Unable to get device for CPU%d\n", + __func__, cpu); + return -ENODEV; + } + + ret = genpd_dev_pm_attach(cpu_dev); + if (ret) + dev_warn(cpu_dev, + "%s: Unable to attach to power-domain: %d\n", + __func__, ret); + else + dev_dbg(cpu_dev, "Attached to domain\n"); + + while (!ret && cpu_pd) { + cpumask_set_cpu(cpu, cpu_pd->cpus); + cpu_pd = cpu_pd->parent; + }; + + return ret; +} + +static int cpu_pd_power_on(struct generic_pm_domain *genpd) +{ + struct cpu_pm_domain *pd = to_cpu_pd(genpd); + + return pd->ops.power_on ? pd->ops.power_on() : 0; +} + +static int cpu_pd_power_off(struct generic_pm_domain *genpd) +{ + struct cpu_pm_domain *pd = to_cpu_pd(genpd); + + return pd->ops.power_off ? pd->ops.power_off(genpd->state_idx, + genpd->states[genpd->state_idx].param, + pd->cpus) : 0; +} + +/** + * 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 = kzalloc(sizeof(*genpd), GFP_KERNEL); + if (!genpd) + goto fail; + + genpd->name = kstrndup(dn->full_name, CPU_PD_NAME_MAX, GFP_KERNEL); + if (!genpd->name) + goto fail; + + pd = kzalloc(sizeof(*pd), GFP_KERNEL); + if (!pd) + goto fail; + + if (!zalloc_cpumask_var(&pd->cpus, GFP_KERNEL)) + goto fail; + + genpd->power_off = cpu_pd_power_off; + genpd->power_on = cpu_pd_power_on; + genpd->flags |= GENPD_FLAG_IRQ_SAFE; + genpd->of_node = dn; + pd->genpd = genpd; + pd->ops.power_on = ops->power_on; + pd->ops.power_off = ops->power_off; + + INIT_LIST_HEAD_RCU(&pd->link); + mutex_lock(&cpu_pd_list_lock); + list_add_rcu(&pd->link, &of_cpu_pd_list); + mutex_unlock(&cpu_pd_list_lock); + + /* 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; + } + } + + /* Register the CPU genpd */ + pr_debug("adding %s as CPU PM domain\n", pd->genpd->name); + ret = pm_genpd_init(pd->genpd, &simple_qos_governor, false); + if (ret) { + pr_err("Unable to initialize domain %s\n", dn->full_name); + goto fail; + } + + ret = of_genpd_add_provider_simple(dn, pd->genpd); + if (ret) + pr_warn("Unable to add genpd %s as provider\n", + pd->genpd->name); + + return pd->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; + struct cpu_pm_domain *cpu_pd, *parent_cpu_pd; + 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 = pm_genpd_add_subdomain(parent, genpd); + if (ret) { + pr_err("%s: Unable to add sub-domain (%s)\n err: %d", + __func__, genpd->name, ret); + return ERR_PTR(ret); + } + + /* + * Reference parent domain for easy access. + * Note: We could be attached to a domain that is not a + * CPU PM domain in that case don't reference the parent. + */ + cpu_pd = to_cpu_pd(genpd); + parent_cpu_pd = to_cpu_pd(parent); + + if (cpu_pd && parent_cpu_pd) + cpu_pd->parent = parent_cpu_pd; + +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(cpu_pd, 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 new file mode 100644 index 0000000..de17f80 --- /dev/null +++ b/include/linux/cpu_domains.h @@ -0,0 +1,39 @@ +/* + * include/linux/cpu_domains.h + * + * Copyright (C) 2016 Linaro Ltd. + * + * 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. + */ + +#ifndef __CPU_DOMAINS_H__ +#define __CPU_DOMAINS_H__ + +#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); +}; + +#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__ */