From patchwork Sun Feb 23 04:19:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 11398629 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A5C5F17D5 for ; Sun, 23 Feb 2020 04:20:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 84866217F4 for ; Sun, 23 Feb 2020 04:20:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=sholland.org header.i=@sholland.org header.b="XJ2snZnj"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="faVC0fIX" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726884AbgBWETw (ORCPT ); Sat, 22 Feb 2020 23:19:52 -0500 Received: from out4-smtp.messagingengine.com ([66.111.4.28]:33427 "EHLO out4-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726983AbgBWETw (ORCPT ); Sat, 22 Feb 2020 23:19:52 -0500 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 589E721B03; Sat, 22 Feb 2020 23:19:50 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Sat, 22 Feb 2020 23:19:50 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sholland.org; h= from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; s=fm2; bh=29jUBwoXvuarTIYCv2K9/Ed7RP zIOgoOw2xOPHEd28A=; b=XJ2snZnj7a6mcv05PkQ04IqGL1WPX0jE1k35GsuDnz a9/i+DS+fjwR3zHY83HTaHSMuL0Af3zkoS8uId802BhCe5n9BR2zOz/Nd6H1g6NG TTNvxfyvDTXn3c1gmkwi/SBf66R2uVrUdlvoGR0GEfwm1G9q21ZU4kqXcE/4rbJa OWWOxzqXb0qsPndCs7cYXhfwsC9C1lYwEnL7318RD+x/Mj/mIix6FQuVjxie0dOc Xbz/V2ZFrt5ps4l2+Kj7TKIxGMWNu3X03EwS0NI+/NCPh5/0GQw/hHnpEOTc8bvv vrTYciL4YR6cg0H0/qX5OfrcVMcLb3bgcgK3BckPHAeA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :message-id:mime-version:subject:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=29jUBwoXvuarTIYCv 2K9/Ed7RPzIOgoOw2xOPHEd28A=; b=faVC0fIXMmvtXXZl/KlJS2hCmrK5IpCBx fGOUndeFr0+bdNkAgGWKsNgDmpqXEvvz8Nd+ooSl9ZgtjShM5GH5G+rrdvPTrAm9 GU0lib2mNA6Q1gGI055etM/t5YqjZYsYvFZUjQqSAxn6TfzCmQvGk6rFtfqrSLtT CXLYvqRWfmXFf8Lp0Zg/wQx0zh39SWMCjQiKWY007OAVwx9Lyq1i2r9661p2V63n WDgiowjswROxKDIEyrKGpFQj68vdis3Y3PIuJxrD4vNiUwXhIlTErhKRfUXTdecb QPc/dsS/bpGQm++SinIFO185Q45T9YG8UEBLmbjTJvnyLku3wUBQA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrkeejgdeiiecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefhvffufffkofgggfestdekredtredttdenucfhrhhomhepufgrmhhuvghlucfj ohhllhgrnhguuceoshgrmhhuvghlsehshhholhhlrghnugdrohhrgheqnecukfhppeejtd drudefhedrudegkedrudehudenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhep mhgrihhlfhhrohhmpehsrghmuhgvlhesshhhohhllhgrnhgurdhorhhg X-ME-Proxy: Received: from titanium.stl.sholland.net (70-135-148-151.lightspeed.stlsmo.sbcglobal.net [70.135.148.151]) by mail.messagingengine.com (Postfix) with ESMTPA id CE9C7328005E; Sat, 22 Feb 2020 23:19:48 -0500 (EST) From: Samuel Holland To: Stephen Boyd , Michael Turquette , Maxime Ripard , Chen-Yu Tsai , Andy Gross , Bjorn Andersson Cc: linux-arm-msm@vger.kernel.org, linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org, Samuel Holland Subject: [PATCH 1/2] clk: Implement protected-clocks for all OF clock providers Date: Sat, 22 Feb 2020 22:19:47 -0600 Message-Id: <20200223041948.3218-1-samuel@sholland.org> X-Mailer: git-send-email 2.24.1 MIME-Version: 1.0 Sender: linux-clk-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-clk@vger.kernel.org This is a generic implementation of the "protected-clocks" property from the common clock binding. It allows firmware to inform the OS about clocks that must not be disabled while the OS is running. This implementation comes with some caveats: 1) Clocks that have CLK_IS_CRITICAL in their init data are prepared/ enabled before they are attached to the clock tree. protected-clocks are only protected once the clock provider is added, which is generally after all of the clocks it provides have been registered. This leaves a window of opportunity where something could disable or modify the clock, such as a driver running on another CPU, or the clock core itself. There is a comment to this effect in __clk_core_init(): /* * Enable CLK_IS_CRITICAL clocks so newly added critical clocks * don't get accidentally disabled when walking the orphan tree and * reparenting clocks */ Similarly, these clocks will be enabled after they are first reparented, unlike other CLK_IS_CRITICAL clocks. See the comment in clk_core_reparent_orphans_nolock(): /* * We need to use __clk_set_parent_before() and _after() to * to properly migrate any prepare/enable count of the orphan * clock. This is important for CLK_IS_CRITICAL clocks, which * are enabled during init but might not have a parent yet. */ Ideally we could detect protected clocks before they are reparented, but there are two problems with that: a) From the clock core's perspective, hw->init is const. b) The clock core doesn't see the device_node until __clk_register is called on the first clock. So the only "race-free" way to detect protected-clocks is to do it in the middle of __clk_register, between when core->flags is initialized and calling __clk_core_init(). That requires scanning the device tree again for each clock, which is part of why I didn't do it that way. 2) __clk_protect needs to be idempotent, for two reasons: a) Clocks with CLK_IS_CRITICAL in their init data are already prepared/enabled, and we don't want to prepare/enable them again. b) of_clk_set_defaults() is called twice for (at least some) clock controllers registered with CLK_OF_DECLARE. It is called first in of_clk_add_provider()/of_clk_add_hw_provider() inside clk_init_cb, and again afterward in of_clk_init(). The second call in of_clk_init() may be unnecessary, but verifying that would require auditing all users of CLK_OF_DECLARE to ensure they called one of the of_clk_add{,_hw}_provider functions. Signed-off-by: Samuel Holland --- Changes RFC->v1: - Only set CLK_IS_CRITICAL, not other flags --- drivers/clk/clk-conf.c | 54 ++++++++++++++++++++++++++++++++++++++++++ drivers/clk/clk.c | 31 ++++++++++++++++++++++++ drivers/clk/clk.h | 2 ++ 3 files changed, 87 insertions(+) diff --git a/drivers/clk/clk-conf.c b/drivers/clk/clk-conf.c index 2ef819606c41..a57d28b0f397 100644 --- a/drivers/clk/clk-conf.c +++ b/drivers/clk/clk-conf.c @@ -11,6 +11,54 @@ #include #include +#include "clk.h" + +static int __set_clk_flags(struct device_node *node) +{ + struct of_phandle_args clkspec; + struct property *prop; + int i, index = 0, rc; + const __be32 *cur; + struct clk *clk; + u32 nr_cells; + + rc = of_property_read_u32(node, "#clock-cells", &nr_cells); + if (rc < 0) { + pr_err("clk: missing #clock-cells property on %pOF\n", node); + return rc; + } + + clkspec.np = node; + clkspec.args_count = nr_cells; + + of_property_for_each_u32(node, "protected-clocks", prop, cur, clkspec.args[0]) { + /* read the remainder of the clock specifier */ + for (i = 1; i < nr_cells; ++i) { + cur = of_prop_next_u32(prop, cur, &clkspec.args[i]); + if (!cur) { + pr_err("clk: invalid value of protected-clocks" + " property at %pOF\n", node); + return -EINVAL; + } + } + clk = of_clk_get_from_provider(&clkspec); + if (IS_ERR(clk)) { + if (PTR_ERR(clk) != -EPROBE_DEFER) + pr_err("clk: couldn't get protected clock" + " %u for %pOF\n", index, node); + return PTR_ERR(clk); + } + + rc = __clk_protect(clk); + if (rc < 0) + pr_warn("clk: failed to protect %s: %d\n", + __clk_get_name(clk), rc); + clk_put(clk); + index++; + } + return 0; +} + static int __set_clk_parents(struct device_node *node, bool clk_supplier) { struct of_phandle_args clkspec; @@ -135,6 +183,12 @@ int of_clk_set_defaults(struct device_node *node, bool clk_supplier) if (!node) return 0; + if (clk_supplier) { + rc = __set_clk_flags(node); + if (rc < 0) + return rc; + } + rc = __set_clk_parents(node, clk_supplier); if (rc < 0) return rc; diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c index f0f2b599fd7e..1dc1e115197b 100644 --- a/drivers/clk/clk.c +++ b/drivers/clk/clk.c @@ -4122,6 +4122,37 @@ void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw) } EXPORT_SYMBOL_GPL(devm_clk_hw_unregister); +/* + * clk-conf helpers + */ + +int __clk_protect(struct clk *clk) +{ + struct clk_core *core = clk->core; + int ret = 0; + + clk_prepare_lock(); + + /* + * If CLK_IS_CRITICAL was set in the clock's init data, then + * the clock was already prepared/enabled when it was added. + */ + if (core->flags & CLK_IS_CRITICAL) + goto out; + + core->flags |= CLK_IS_CRITICAL; + ret = clk_core_prepare(core); + if (ret) + goto out; + + ret = clk_core_enable_lock(core); + +out: + clk_prepare_unlock(); + + return ret; +} + /* * clkdev helpers */ diff --git a/drivers/clk/clk.h b/drivers/clk/clk.h index 2d801900cad5..367a0f036b13 100644 --- a/drivers/clk/clk.h +++ b/drivers/clk/clk.h @@ -24,6 +24,7 @@ struct clk_hw *clk_find_hw(const char *dev_id, const char *con_id); #ifdef CONFIG_COMMON_CLK struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw, const char *dev_id, const char *con_id); +int __clk_protect(struct clk *clk); void __clk_put(struct clk *clk); #else /* All these casts to avoid ifdefs in clkdev... */ @@ -33,6 +34,7 @@ clk_hw_create_clk(struct device *dev, struct clk_hw *hw, const char *dev_id, { return (struct clk *)hw; } +static inline int __clk_protect(struct clk *clk) { return 0; } static inline void __clk_put(struct clk *clk) { } #endif From patchwork Sun Feb 23 04:19:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Holland X-Patchwork-Id: 11398623 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id AACEC14D5 for ; Sun, 23 Feb 2020 04:19:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7C27620675 for ; Sun, 23 Feb 2020 04:19:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=sholland.org header.i=@sholland.org header.b="TsBQ7D98"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="s72qjcMR" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727024AbgBWETv (ORCPT ); Sat, 22 Feb 2020 23:19:51 -0500 Received: from out4-smtp.messagingengine.com ([66.111.4.28]:53607 "EHLO out4-smtp.messagingengine.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726884AbgBWETv (ORCPT ); Sat, 22 Feb 2020 23:19:51 -0500 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 5824321B01; Sat, 22 Feb 2020 23:19:50 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Sat, 22 Feb 2020 23:19:50 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sholland.org; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=fm2; bh=3tufXe1fL7t2q MfUV7AoAiRO7jy5bJvdqR1dzLQlDus=; b=TsBQ7D98kh1a8h/iD/7I72q/WG6yY 0pkwFIcJndxaNtJ+XQ9l+MNjsh7Z4W27s/+7CM/8BCOkx0a1JlsWAqtESIP/tPfk 9rXkDeGOsOtYtn7sDaZTaZ6koH9O62VWP7y0ybUX0FXH+mIRCp99rv5gP4cAaplo uK6GKuddfNKXy3x7Mksw7RR7M9iSbt/mMcJKTau/W7rUfEK6507yRHHPdk9F1Dal Dq0qR0DIItwLDPauI8ks4Awdkzl4WBuKH+53CMzO8oqsajoz8h7Fxa1gYRV31SLg sYYk9SOr7JkE5jCEri8swDi2Z4ENGpcvJMCIInjr/A0fx68g6ALha2XBQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=3tufXe1fL7t2qMfUV7AoAiRO7jy5bJvdqR1dzLQlDus=; b=s72qjcMR rkgX7SucHtZUUEYCFB1aG7fR7wLJ94MeJIVywjxQNFwGWAHpm9ghrmQTkhlvicI7 pxiWYaXH1tc4koeocnjm3RoEWPD2SiAm4FFl1p0DJElRb/ZSCE7rE2w0+2txjtpN bGxHbWV+o/wGpuJPWSI4yEfsAaB+iXAp3Njlo7tmt3ymiRqkpYj1zkMeU+06VcG8 DGJeIXYPbPPLASNFT/Cv9iwNI28kZLu13e9cr4FbT74XJ5z62EQBk1Uq66G/70Kn GHQftx9sgjdEcBb05sEm3BY0i9y4QEq1OOJ4Wly6A6WFBGzKEVtvYhjAG4dfv1cg bnQ9MOuaR4PYuA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrkeejgdeiiecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefhvffufffkofgjfhgggfestdekredtredttdenucfhrhhomhepufgrmhhuvghl ucfjohhllhgrnhguuceoshgrmhhuvghlsehshhholhhlrghnugdrohhrgheqnecukfhppe ejtddrudefhedrudegkedrudehudenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgr mhepmhgrihhlfhhrohhmpehsrghmuhgvlhesshhhohhllhgrnhgurdhorhhg X-ME-Proxy: Received: from titanium.stl.sholland.net (70-135-148-151.lightspeed.stlsmo.sbcglobal.net [70.135.148.151]) by mail.messagingengine.com (Postfix) with ESMTPA id 4A0C4328005A; Sat, 22 Feb 2020 23:19:49 -0500 (EST) From: Samuel Holland To: Stephen Boyd , Michael Turquette , Maxime Ripard , Chen-Yu Tsai , Andy Gross , Bjorn Andersson Cc: linux-arm-msm@vger.kernel.org, linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org, Samuel Holland Subject: [PATCH 2/2] Revert "clk: qcom: Support 'protected-clocks' property" Date: Sat, 22 Feb 2020 22:19:48 -0600 Message-Id: <20200223041948.3218-2-samuel@sholland.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200223041948.3218-1-samuel@sholland.org> References: <20200223041948.3218-1-samuel@sholland.org> MIME-Version: 1.0 Sender: linux-clk-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-clk@vger.kernel.org Now that protected-clocks is handled in the clk core, this driver-specific implementation is redundant. This reverts commit b181b3b801da8893c8eb706e448dd5111b02de60. Signed-off-by: Samuel Holland --- drivers/clk/qcom/common.c | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/drivers/clk/qcom/common.c b/drivers/clk/qcom/common.c index 60d2a78d1395..6e150fd32dbe 100644 --- a/drivers/clk/qcom/common.c +++ b/drivers/clk/qcom/common.c @@ -194,22 +194,6 @@ int qcom_cc_register_sleep_clk(struct device *dev) } EXPORT_SYMBOL_GPL(qcom_cc_register_sleep_clk); -/* Drop 'protected-clocks' from the list of clocks to register */ -static void qcom_cc_drop_protected(struct device *dev, struct qcom_cc *cc) -{ - struct device_node *np = dev->of_node; - struct property *prop; - const __be32 *p; - u32 i; - - of_property_for_each_u32(np, "protected-clocks", prop, p, i) { - if (i >= cc->num_rclks) - continue; - - cc->rclks[i] = NULL; - } -} - static struct clk_hw *qcom_cc_clk_hw_get(struct of_phandle_args *clkspec, void *data) { @@ -272,8 +256,6 @@ int qcom_cc_really_probe(struct platform_device *pdev, cc->rclks = rclks; cc->num_rclks = num_clks; - qcom_cc_drop_protected(dev, cc); - for (i = 0; i < num_clk_hws; i++) { ret = devm_clk_hw_register(dev, clk_hws[i]); if (ret)