From patchwork Thu Apr 11 18:17:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulf Hansson X-Patchwork-Id: 162071 Delivered-To: patches@linaro.org Received: by 2002:a02:c6d8:0:0:0:0:0 with SMTP id r24csp7741901jan; Thu, 11 Apr 2019 11:17:40 -0700 (PDT) X-Received: by 2002:ac2:5a01:: with SMTP id q1mr27933828lfn.147.1555006660849; Thu, 11 Apr 2019 11:17:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555006660; cv=none; d=google.com; s=arc-20160816; b=rAeRjK5VxyWL0QbXPTsyDMoZVfwpl6Kk8zt9plJJPT1uIygz7r2HN5lai3ZXj9SdGO SG8IHmS0itLLD5VpPKRFbTeyBjUBfADpb87R+WDlKhRwIpI+JA5zkdLEicY5DnD/eMAD GDIbA16pmqgxTAwfbX72RFHRZCRACDIZU57RQXqzs/Ms6ZM7EXCX0HSbF2/Nhsr76PBB Y9xzVehRfh/4fN5iJlLrZg++ElJuO51hvro+uc3An4yvkvS3HlRwiGJ8/fTRutNYDL/0 vY1+X+p596+m5QEvMIPpBJyz2rqnkAa6UzCEQRyUJIonnSEdc4OkCDptx2okzb1gdkIJ ew8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:date:subject:cc:to:from:dkim-signature; bh=pf7QbFgAhcYW1LW0/5PkllmLvAODcFP2rewRBiSGZHI=; b=KN7z2sO7VpMX3xS9yCD/aFjMAxWsoh2JDxZlVszgxuLcEQLYeCOSAOkuSsNdSdAhla +6TYepA/sZ95JVTReiHNQqj87MYFxM3QCcLEkQjcp4EXTRISLfJJ+wr8g1h50hVHXNIr 7ZYOEupUI3YFdgPmGE8eY2+KJlGHGCJhRbeDP3c+uUHEtoRPooNZLH19Sje4rxit719c v5miQ08Jusst63RNUo8hc7P0P/6FL7F7fPEpV8ifXPtkepwiCbva6MAdJhRmnE/ea9nh q+OqGinpNWrHPMVUZiaivs25+bu91hOA5Wguy3Qg0KrHYz07j4JCr7c573x9j+ftr8V4 NF0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zNmdNA0r; spf=pass (google.com: domain of ulf.hansson@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=ulf.hansson@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id h7sor4063730lfc.51.2019.04.11.11.17.40 for (Google Transport Security); Thu, 11 Apr 2019 11:17:40 -0700 (PDT) Received-SPF: pass (google.com: domain of ulf.hansson@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zNmdNA0r; spf=pass (google.com: domain of ulf.hansson@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=ulf.hansson@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=pf7QbFgAhcYW1LW0/5PkllmLvAODcFP2rewRBiSGZHI=; b=zNmdNA0riuf18zmSkT7G9Qx0jX7jsxoGGM/+DgLaKSfnMVOvTHOMMKH9y+NmA7eA07 0eIpvywenadsD4RjFSkCTF6GoQDu8Edwtm+CKhJ6y+PoO1+hNGcJpJ0EM4+2LUqGjwmW n4fU6KjMJxN75eq61398vodHLyEYfJLj/YjT9/S0gYFvr0WchaExF9Y/JYZFF/rOk2/g oVrI55JCqd2mZVfmwo7oDevmL1c6jh8ZhPQjZPl3g1tiwjPGhB98Ux0XboRQ9zHZ4WWh kkWz3Zs8LaAREk40JttskePAP145ypSMzIpBAIjlJvgZVAJVzL+uQOSeSpY1lR68Jg41 Yiow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=pf7QbFgAhcYW1LW0/5PkllmLvAODcFP2rewRBiSGZHI=; b=CY89x4sxjuRVaVHJZV2xgFzwU8tMYxdbN4OO3PwowtNhyhsMczA1+1Ii1T1zECYW6t JJuvUDp59NWsuTXba3YuJZACt0tWmn3X35lmcz6KLFSbQI3qtqU+sYCTYazMrxkuM8B/ AhatQNoc2+62OaVz/5TaBKI8fqerXyJHeaYC7fjyqt58hFK/agNXzml+sWCtiAyjcx7a /cc6BmyS0iMuZOWU5+h4y3N4P/bZaMoKKsR5S0CzKLKaHHRPHzsLZ4u9wLHpQPrrKLNE 48jsB1wwkpKy55oQALqpF3RqUUEGEi4Jlx795aBG50iiyM7SF4gBJxfeZFasc/T+WLdu 9fbg== X-Gm-Message-State: APjAAAWtPuIeB0dbEb+yDepWl6YGR3h50wdLj3iGtrVLWjkBWfXo6a5z sV4/5EmLAc1dpA+2ohyU2oazSGP4 X-Google-Smtp-Source: APXvYqwEGuLmkApqIn6LxQZ4v+k520TCQHVuG0cD0K4tdPNQtoJDRox1x9GFJsn1Etr6xdO4nvYxZg== X-Received: by 2002:ac2:4ad1:: with SMTP id m17mr10432536lfp.129.1555006660332; Thu, 11 Apr 2019 11:17:40 -0700 (PDT) Return-Path: Received: from localhost.localdomain (h-158-174-22-210.NA.cust.bahnhof.se. [158.174.22.210]) by smtp.gmail.com with ESMTPSA id d13sm1555715lfa.40.2019.04.11.11.17.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Apr 2019 11:17:39 -0700 (PDT) From: Ulf Hansson To: "Rafael J . Wysocki" , linux-pm@vger.kernel.org Cc: Frederic Weisbecker , Thomas Gleixner , Sudeep Holla , Lorenzo Pieralisi , Mark Rutland , Daniel Lezcano , "Raju P . L . S . S . S . N" , Stephen Boyd , Tony Lindgren , Kevin Hilman , Lina Iyer , Ulf Hansson , Viresh Kumar , Vincent Guittot , Geert Uytterhoeven , linux-arm-kernel@lists.infradead.org, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, "Rafael J . Wysocki" Subject: [PATCH v14 4/4] PM / Domains: Add genpd governor for CPUs Date: Thu, 11 Apr 2019 20:17:33 +0200 Message-Id: <20190411181733.21437-1-ulf.hansson@linaro.org> X-Mailer: git-send-email 2.17.1 After some preceding changes, PM domains managed by genpd may contain CPU devices, so idle state residency values should be taken into account during the state selection process. [The residency value is the minimum amount of time to be spent by a CPU (or a group of CPUs) in an idle state in order to save more energy than could be saved by picking up a shallower idle state.] For this purpose, add a new genpd governor, pm_domain_cpu_gov, to be used for selecting idle states of PM domains with CPU devices attached either directly or through subdomains. The new governor computes the minimum expected idle duration for all online CPUs attached to a PM domain and its subdomains. Next, it finds the deepest idle state whose target residency is within the expected idle duration and selects it as the target idle state of the domain. It should be noted that the minimum expected idle duration computation is based on the closest timer event information stored in the per-CPU variables cpuidle_devices for all of the CPUs in the domain. That needs to be revisited in future, as obviously there are other reasons why a CPU may be woken up from idle. Co-developed-by: Lina Iyer Acked-by: Daniel Lezcano Signed-off-by: Ulf Hansson [ rjw: Changelog ] Signed-off-by: Rafael J. Wysocki --- Changes in v4: - Fix build error when CONFIG_CPU_IDLE is unset. --- drivers/base/power/domain_governor.c | 67 +++++++++++++++++++++++++++- include/linux/pm_domain.h | 4 ++ 2 files changed, 70 insertions(+), 1 deletion(-) -- 2.17.1 diff --git a/drivers/base/power/domain_governor.c b/drivers/base/power/domain_governor.c index 4d07e38a8247..7912bc957244 100644 --- a/drivers/base/power/domain_governor.c +++ b/drivers/base/power/domain_governor.c @@ -10,6 +10,9 @@ #include #include #include +#include +#include +#include static int dev_update_qos_constraint(struct device *dev, void *data) { @@ -210,8 +213,10 @@ static bool default_power_down_ok(struct dev_pm_domain *pd) struct generic_pm_domain *genpd = pd_to_genpd(pd); struct gpd_link *link; - if (!genpd->max_off_time_changed) + if (!genpd->max_off_time_changed) { + genpd->state_idx = genpd->cached_power_down_state_idx; return genpd->cached_power_down_ok; + } /* * We have to invalidate the cached results for the masters, so @@ -236,6 +241,7 @@ static bool default_power_down_ok(struct dev_pm_domain *pd) genpd->state_idx--; } + genpd->cached_power_down_state_idx = genpd->state_idx; return genpd->cached_power_down_ok; } @@ -244,6 +250,65 @@ static bool always_on_power_down_ok(struct dev_pm_domain *domain) return false; } +#ifdef CONFIG_CPU_IDLE +static bool cpu_power_down_ok(struct dev_pm_domain *pd) +{ + struct generic_pm_domain *genpd = pd_to_genpd(pd); + struct cpuidle_device *dev; + ktime_t domain_wakeup, next_hrtimer; + s64 idle_duration_ns; + int cpu, i; + + /* Validate dev PM QoS constraints. */ + if (!default_power_down_ok(pd)) + return false; + + if (!(genpd->flags & GENPD_FLAG_CPU_DOMAIN)) + return true; + + /* + * Find the next wakeup for any of the online CPUs within the PM domain + * and its subdomains. Note, we only need the genpd->cpus, as it already + * contains a mask of all CPUs from subdomains. + */ + domain_wakeup = ktime_set(KTIME_SEC_MAX, 0); + for_each_cpu_and(cpu, genpd->cpus, cpu_online_mask) { + dev = per_cpu(cpuidle_devices, cpu); + if (dev) { + next_hrtimer = READ_ONCE(dev->next_hrtimer); + if (ktime_before(next_hrtimer, domain_wakeup)) + domain_wakeup = next_hrtimer; + } + } + + /* The minimum idle duration is from now - until the next wakeup. */ + idle_duration_ns = ktime_to_ns(ktime_sub(domain_wakeup, ktime_get())); + if (idle_duration_ns <= 0) + return false; + + /* + * Find the deepest idle state that has its residency value satisfied + * and by also taking into account the power off latency for the state. + * Start at the state picked by the dev PM QoS constraint validation. + */ + i = genpd->state_idx; + do { + if (idle_duration_ns >= (genpd->states[i].residency_ns + + genpd->states[i].power_off_latency_ns)) { + genpd->state_idx = i; + return true; + } + } while (--i >= 0); + + return false; +} + +struct dev_power_governor pm_domain_cpu_gov = { + .suspend_ok = default_suspend_ok, + .power_down_ok = cpu_power_down_ok, +}; +#endif + struct dev_power_governor simple_qos_governor = { .suspend_ok = default_suspend_ok, .power_down_ok = default_power_down_ok, diff --git a/include/linux/pm_domain.h b/include/linux/pm_domain.h index a6e251fe9deb..bc82e74560ee 100644 --- a/include/linux/pm_domain.h +++ b/include/linux/pm_domain.h @@ -118,6 +118,7 @@ struct generic_pm_domain { s64 max_off_time_ns; /* Maximum allowed "suspended" time. */ bool max_off_time_changed; bool cached_power_down_ok; + bool cached_power_down_state_idx; int (*attach_dev)(struct generic_pm_domain *domain, struct device *dev); void (*detach_dev)(struct generic_pm_domain *domain, @@ -202,6 +203,9 @@ int dev_pm_genpd_set_performance_state(struct device *dev, unsigned int state); extern struct dev_power_governor simple_qos_governor; extern struct dev_power_governor pm_domain_always_on_gov; +#ifdef CONFIG_CPU_IDLE +extern struct dev_power_governor pm_domain_cpu_gov; +#endif #else static inline struct generic_pm_domain_data *dev_gpd_data(struct device *dev)