From patchwork Tue Jan 12 06:58:32 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Viresh Kumar X-Patchwork-Id: 59601 Delivered-To: patch@linaro.org Received: by 10.112.130.2 with SMTP id oa2csp2562809lbb; Mon, 11 Jan 2016 22:58:39 -0800 (PST) X-Received: by 10.98.9.75 with SMTP id e72mr31621005pfd.29.1452581919695; Mon, 11 Jan 2016 22:58:39 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 2si34184247pfj.77.2016.01.11.22.58.39; Mon, 11 Jan 2016 22:58:39 -0800 (PST) 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; 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; dkim=neutral (body hash did not verify) header.i=@linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753363AbcALG6i (ORCPT + 11 others); Tue, 12 Jan 2016 01:58:38 -0500 Received: from mail-pa0-f46.google.com ([209.85.220.46]:33096 "EHLO mail-pa0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752187AbcALG6h (ORCPT ); Tue, 12 Jan 2016 01:58:37 -0500 Received: by mail-pa0-f46.google.com with SMTP id cy9so334906367pac.0 for ; Mon, 11 Jan 2016 22:58:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; bh=OXclH22I9VmmcL0aDiRakg6XZo6ip/z1j8mR4T7YpMI=; b=chyUggVklpiOpf8ClPLGm9TdeDB1MJZJkmPdh2q04G1GyhlOjFO0MgkdAz+Hi1GQTd 8IFwAZltK5BhAN4I2p73vG+4ah/7+zQ0SmWB1S6/afvE8N/9Q+Ca8lvfeqjsFfTv6tq3 jDJTAr6MvdZG41rv71lNcZ29oqQUwoHAFgSvw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-type:content-disposition:in-reply-to :user-agent; bh=OXclH22I9VmmcL0aDiRakg6XZo6ip/z1j8mR4T7YpMI=; b=La4WKSdSKY81kDMGQ8awWTxF7khEXkrq2MyPLizX6dTxWGWeFD6+2Wpk3YNeOUt3aR VtY1gYiikwwG9DvilaXUBuizIghvqT3THiUG4DPMS2W5MNhTB2ViZypKgsWEjg6yz4Bj CmthwLvyvwaWm5BaaWvozgMjhryOTkdW1li/BWJ7d3IBRqa8XqEtu2fbzJMmYSsIpVl7 rAYDZGKyIG1OrhdN1cRBa8FYIDAefkjDA2q9PExVhMyIw7CYifBcRqR5IHPcdkcGRiKt Ls4q4kfexDp2+IyBJ36zskRknrAWFm+kAZ29A4k1OfkZpXfCODShq4nQTw2ClxOyyK2u T6bA== X-Gm-Message-State: ALoCoQnfwXJSCkEwfXLuBZhFBX0eturj8IubDp9kFcGbAIc7Z9uweDzduwmBj22wKdjjj+A6VgHyRlrHMcjWzARsy8HHuVtTKw== X-Received: by 10.66.161.133 with SMTP id xs5mr184987393pab.70.1452581916986; Mon, 11 Jan 2016 22:58:36 -0800 (PST) Received: from localhost ([122.172.46.246]) by smtp.gmail.com with ESMTPSA id 75sm27682975pfj.20.2016.01.11.22.58.35 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Mon, 11 Jan 2016 22:58:36 -0800 (PST) Date: Tue, 12 Jan 2016 12:28:32 +0530 From: Viresh Kumar To: Stephen Boyd Cc: Rafael Wysocki , linaro-kernel@lists.linaro.org, linux-pm@vger.kernel.org, nm@ti.com Subject: Re: [PATCH 08/17] PM / OPP: Add dev_pm_opp_set_rate() Message-ID: <20160112065832.GN1084@ubuntu> References: <5824076a64ad4215cfc63c238e8b8947bc996e87.1450777582.git.viresh.kumar@linaro.org> <20160112014010.GM22188@codeaurora.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20160112014010.GM22188@codeaurora.org> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org On 11-01-16, 17:40, Stephen Boyd wrote: > > + ret = regulator_set_voltage_triplet(reg, opp->u_volt_min, opp->u_volt, > > This can't be called under an RCU read lock. > > > + rcu_read_lock(); > > This lock is held way too long. > > > + freq = clk_round_rate(clk, target_freq); > > This can't be called under RCU. > > > + if ((long)freq <= 0) > > + freq = target_freq; > > + > > + old_freq = clk_get_rate(clk); > > Same for this. Some of these I figured out earlier (after sending the series), and fixed them by dropping rcu lock at certain points. But the problem is that we will be using opp/clk/reg here for almost the complete routine and the lock must be acquired for all the time. (Untested for now) -------------------------8<------------------------- Subject: [PATCH] PM / OPP: Add dev_pm_opp_set_rate() This adds a routine, dev_pm_opp_set_rate(), responsible for configuring power-supply and clock source for an OPP. The OPP is found by matching against the target_freq passed to the routine. This shall replace similar code present in most of the OPP users and help simplify them a lot. Signed-off-by: Viresh Kumar --- drivers/base/power/opp/core.c | 155 ++++++++++++++++++++++++++++++++++++++++++ include/linux/pm_opp.h | 6 ++ 2 files changed, 161 insertions(+) -- viresh -- 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/opp/core.c b/drivers/base/power/opp/core.c index 2b1e8cb3897f..ca6b31ab5c87 100644 --- a/drivers/base/power/opp/core.c +++ b/drivers/base/power/opp/core.c @@ -527,6 +527,161 @@ struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, } EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); +static int _set_opp_voltage(struct device *dev, struct regulator *reg, + unsigned long u_volt, unsigned long u_volt_min, + unsigned long u_volt_max) +{ + int ret; + + /* Regulator not be available for device */ + if (IS_ERR(reg)) { + dev_dbg(dev, "%s: regulator not available: %ld\n", __func__, + PTR_ERR(reg)); + return 0; + } + + dev_dbg(dev, "%s: voltages (mV): %lu %lu %lu\n", __func__, u_volt_min, + u_volt, u_volt_max); + + ret = regulator_set_voltage_triplet(reg, u_volt_min, u_volt, + u_volt_max); + if (ret) + dev_err(dev, "%s: failed to set voltage (%lu %lu %lu mV): %d\n", + __func__, u_volt_min, u_volt, u_volt_max, ret); + + return ret; +} + +/** + * dev_pm_opp_set_rate() - Configure new OPP based on frequency + * @dev: device for which we do this operation + * @target_freq: frequency to achieve + * + * This configures the power-supplies and clock source to the levels specified + * by the OPP corresponding to the target_freq. + * + * Locking: This function internally uses mutex locks to keep the integrity of + * the internal data structures. Callers should ensure that this function is + * *NOT* called under RCU protection or in contexts where mutex cannot be + * locked. + */ +int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) +{ + struct device_opp *dev_opp; + struct dev_pm_opp *old_opp, *opp; + struct regulator *reg; + struct clk *clk; + unsigned long freq, old_freq; + unsigned long u_volt, u_volt_min, u_volt_max; + unsigned long ou_volt, ou_volt_min, ou_volt_max; + int ret; + + if (unlikely(!target_freq)) { + dev_err(dev, "%s: Invalid target frequency %lu\n", __func__, + target_freq); + return -EINVAL; + } + + /* + * Hold our list modification lock here as clk/regulator routines called + * below can possibly take another mutex, which isn't allowed within rcu + * locks. + */ + mutex_lock(&dev_opp_list_lock); + + dev_opp = _find_device_opp(dev); + if (IS_ERR(dev_opp)) { + dev_err(dev, "%s: device opp doesn't exist\n", __func__); + ret = PTR_ERR(dev_opp); + goto unlock; + } + + clk = dev_opp->clk; + if (IS_ERR(clk)) { + dev_err(dev, "%s: No clock available for the device\n", + __func__); + ret = -EINVAL; + goto unlock; + } + + freq = clk_round_rate(clk, target_freq); + if ((long)freq <= 0) + freq = target_freq; + + old_freq = clk_get_rate(clk); + + /* Return early if nothing to do */ + if (old_freq == freq) { + dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n", + __func__, freq); + ret = 0; + goto unlock; + } + + reg = dev_opp->regulator; + old_opp = dev_pm_opp_find_freq_ceil(dev, &old_freq); + opp = dev_pm_opp_find_freq_ceil(dev, &freq); + + if (IS_ERR(opp)) { + ret = PTR_ERR(opp); + dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", + __func__, freq, ret); + goto unlock; + } + + u_volt = opp->u_volt; + u_volt_min = opp->u_volt_min; + u_volt_max = opp->u_volt_max; + + ou_volt = old_opp->u_volt; + ou_volt_min = old_opp->u_volt_min; + ou_volt_max = old_opp->u_volt_max; + + /* Scaling up? Scale voltage before frequency */ + if (freq > old_freq) { + ret = _set_opp_voltage(dev, reg, u_volt, u_volt_min, + u_volt_max); + if (ret) + goto restore_voltage; + } + + /* Change frequency */ + + dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", + __func__, old_freq, freq); + + ret = clk_set_rate(clk, freq); + if (ret) { + dev_err(dev, "%s: failed to set clock rate: %d\n", __func__, + ret); + goto restore_voltage; + } + + /* Scaling down? Scale voltage after frequency */ + if (freq < old_freq) { + ret = _set_opp_voltage(dev, reg, u_volt, u_volt_min, + u_volt_max); + if (ret) + goto restore_freq; + } + + mutex_unlock(&dev_opp_list_lock); + + return 0; + +restore_freq: + if (clk_set_rate(clk, old_freq)) + dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n", + __func__, old_freq); +restore_voltage: + /* This shouldn't harm if the voltages weren't updated earlier */ + _set_opp_voltage(dev, reg, ou_volt, ou_volt_min, ou_volt_max); +unlock: + mutex_unlock(&dev_opp_list_lock); + return ret; +} +EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); + /* List-dev Helpers */ static void _kfree_list_dev_rcu(struct rcu_head *head) { diff --git a/include/linux/pm_opp.h b/include/linux/pm_opp.h index 59da3d9e11ea..cccaf4a29e9f 100644 --- a/include/linux/pm_opp.h +++ b/include/linux/pm_opp.h @@ -64,6 +64,7 @@ int dev_pm_opp_set_prop_name(struct device *dev, const char *name); void dev_pm_opp_put_prop_name(struct device *dev); int dev_pm_opp_set_regulator(struct device *dev, const char *name); void dev_pm_opp_put_regulator(struct device *dev); +int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq); #else static inline unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) { @@ -172,6 +173,11 @@ static inline int dev_pm_opp_set_regulator(struct device *dev, const char *name) static inline void dev_pm_opp_put_regulator(struct device *dev) {} +static inline int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) +{ + return -EINVAL; +} + #endif /* CONFIG_PM_OPP */ #if defined(CONFIG_PM_OPP) && defined(CONFIG_OF)