From patchwork Sun Sep 17 22:40:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Bara X-Patchwork-Id: 13388725 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9A7FCCD37B4 for ; Sun, 17 Sep 2023 22:41:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=lhCIluGzlnPUDgSVazmRKVvIBvxfUxYOQ71I+oqwmlg=; b=MSodjFlBXRDgzi A8D7XhLlxk7kQgAmqlcN1lh9IUDflTrEKS2pqSEKMOTr9YUR+5g2oL3LFffnhXMSA2DDopi7NFnCZ PGCICFK3l9NKyt27Y2lti6ccR+7YbdYtHWT00ra8kHSadXzihSUIwPVCF8s0Z++cViHrkOySie/XO E8Ay9PqWOAkpwupyKbC8xywfOM+fqGUEXZgvs4VH8cFxBO/vNmfuhJoiqMYiBkQtW3Zfoyu4PpMM9 +GDNXYwGKqKjFwu7KYqI5Ey4XXeJk29H1jcLrB+iycFuP+xyU+LkMbw2GtMm/kM0mtm6J+tP6yMTR bnR6jUAJXf2kHOrVgaGQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qi0Ra-00E54C-3B; Sun, 17 Sep 2023 22:40:46 +0000 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qi0RT-00E4z4-23 for linux-arm-kernel@lists.infradead.org; Sun, 17 Sep 2023 22:40:42 +0000 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-40473f1fe9fso38843345e9.2 for ; Sun, 17 Sep 2023 15:40:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1694990437; x=1695595237; darn=lists.infradead.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=d58DRMcmmqVdQpNWAbMiLz0ISFX3idJISFoDmPX5pV4=; b=LxRx/s7rWNW+VKj4/A27hNuBH0p4xxjzBbRZcBOaoG7TjN+ONXkJSvHPzKGZiKy04z 8R5zsSsp9J51LMh97T8lP0hCH9pzdifL+N1tRiSYCZt72XBRl4tn+awWMB1pitXjffOr 9optaT9gp3N1/y91vztYoF4uuqjYM99dwZ/4HxdSQBcR1UMJ71+vWBzS64VN6G0We3Al MLJq6jskd6vL5gY1fJRbBQ8Q3iToN9NSMxjDeImVqcO5wJKMnfxzobkJEciyQLKKVb39 czHwTLpJA2C9Im6d3OmppV1hmGDyFAn+VCIakVLEzMvo2cWzpOwohdhPw9pqJakp8G22 LPtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694990437; x=1695595237; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=d58DRMcmmqVdQpNWAbMiLz0ISFX3idJISFoDmPX5pV4=; b=IDzgqsy1y3mSrRYjkvUSZGNXUdlM/iQnhbRC+seDkzdud1SCoKd66bFohaUtZr8pLJ p47PSILJjs5prx3d0MEZAfJJMWDhxCIcHIKqEnSU6meZUX1esLDdQ01EFAsS/ccupE7Q 5gj7o0DrSZVy6fwAUxcnQlQtf9M8CnEIXD34D60FjF7BI23VO7/xjt0xiO9hSPOcZ/Kq WmGcsMxogSS7/NGuij+s0KKPs/HsuSnxqFEKTN8DWPa8E7kDQs1IH6T60loK0XDds9Rn bBFpmn35kwfSyYm8LEOoQg+YrFJSwmzUGmHj5E7KhgC8JMVdGl/4mBEmypBF7Wn2qAoQ yZkw== X-Gm-Message-State: AOJu0Yx06tBQHFJK930qzbFxz12RHoWwwjz9tVHcr0ZgA5Vttcne1fW4 Ot7SaMlaKmPoC0gvmWOYPto= X-Google-Smtp-Source: AGHT+IEYhBep8mg6WS7aKBOBPPKW2JCJgL7HelrqDWQ8VqlnSyxcZRRMfPYvNeB+WumXVoC+/YvMXQ== X-Received: by 2002:a05:600c:2288:b0:404:7865:bf4 with SMTP id 8-20020a05600c228800b0040478650bf4mr5869641wmf.39.1694990437396; Sun, 17 Sep 2023 15:40:37 -0700 (PDT) Received: from [127.0.1.1] ([91.230.2.244]) by smtp.gmail.com with ESMTPSA id x14-20020a1c7c0e000000b003fe2b081661sm13642931wmc.30.2023.09.17.15.40.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Sep 2023 15:40:37 -0700 (PDT) From: Benjamin Bara Date: Mon, 18 Sep 2023 00:40:03 +0200 Subject: [PATCH 07/13] clk: detect unintended rate changes MIME-Version: 1.0 Message-Id: <20230918-imx8mp-dtsi-v1-7-1d008b3237c0@skidata.com> References: <20230918-imx8mp-dtsi-v1-0-1d008b3237c0@skidata.com> In-Reply-To: <20230918-imx8mp-dtsi-v1-0-1d008b3237c0@skidata.com> To: Rob Herring , Krzysztof Kozlowski , Conor Dooley , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , NXP Linux Team , Michael Turquette , Stephen Boyd , Russell King , Abel Vesa , Peng Fan Cc: Frank Oltmanns , Maxime Ripard , devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, Benjamin Bara X-Mailer: b4 0.12.3 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230917_154039_679476_936175D4 X-CRM114-Status: GOOD ( 24.37 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Benjamin Bara As we now keep track of the clocks which are allowed to change - namely the ones which are along the ancestor line between the rate trigger and the top-most changed clock, we can run through the subtree of changes and look for unexpected ones. Shared parents must set their rate in a way, that all consumer-configured rates are respected. As this is sometimes not possible and clocks sometime doesn't require the *exact* rate, we might have to find a way to find out if it is *exact enough*. Then we could fix it in the core. Signed-off-by: Benjamin Bara --- drivers/clk/clk.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c index 82c65ed432c5..faececc44c28 100644 --- a/drivers/clk/clk.c +++ b/drivers/clk/clk.c @@ -2280,6 +2280,74 @@ static struct clk_core *clk_calc_new_rates(struct clk_core *core, return top; } +/* + * If the changed clock is consumer-configured, but not an ancestor of the + * trigger, it is most likely an unintended change. As a workaround, we try to + * set the rate back to the old without changing the parent. If this is not + * possible, the change should not have been suggested in the first place. + */ +static struct clk_core *clk_detect_unintended_rate_changes(struct clk_core *core, + bool fix) +{ + struct clk_core *child, *tmp_clk; + + if (core->rate == core->new_rate) + return NULL; + + if (core->set_rate && core != rate_trigger_clk->core && + !clk_core_is_ancestor(rate_trigger_clk->core, core)) { + struct clk_core *parent = core->new_parent ? : core->parent; + struct clk_rate_request req; + + pr_debug("%s: unintended change by %s (%lu -> %lu)\n", core->name, + rate_trigger_clk->core->name, core->rate, core->new_rate); + + if (fix) { + clk_hw_init_rate_request(core->hw, &req, core->rate); + req.best_parent_rate = parent->new_rate; + req.best_parent_hw = parent->hw; + + if (clk_core_round_rate_nolock(core, &req)) + return core; + + /* TODO: how close is close enough? */ + if (req.rate != core->rate) { + pr_debug("%s: %s fix failed, req=%lu, sugg=%lu\n", + __func__, core->name, core->rate, req.rate); + return core; + } + if (req.best_parent_rate != parent->new_rate || + req.best_parent_hw != parent->hw) { + pr_debug("%s: %s fix failed, req=%s@%lu, sugg=%s@%lu\n", + __func__, core->name, parent->name, + parent->new_rate, + req.best_parent_hw->core->name, + req.best_parent_rate); + return core; + } + + core->new_rate = core->rate; + } + return NULL; + } + + hlist_for_each_entry(child, &core->children, child_node) { + if (child->new_parent && child->new_parent != core) + continue; + tmp_clk = clk_detect_unintended_rate_changes(child, fix); + if (tmp_clk) + return tmp_clk; + } + + if (core->new_child) { + tmp_clk = clk_detect_unintended_rate_changes(core->new_child, fix); + if (tmp_clk) + return tmp_clk; + } + + return NULL; +} + /* * Notify about rate changes in a subtree. Always walk down the whole tree * so that in case of an error we can walk down the whole tree again and @@ -2484,6 +2552,21 @@ static int clk_core_set_rate_nolock(struct clk_core *core, goto err; } + /* + * The notifying process offers the possibility to fix the rates of + * unrelated clocks along the tree. After that, run a detection to find + * clocks which are potentially wrongly configured now. These might be + * fixed by the core in the future. + */ + fail_clk = clk_detect_unintended_rate_changes(top, false); + if (fail_clk) { + pr_err("%s: unintended rate change cannot be fixed\n", + fail_clk->name); + ret = -EINVAL; + goto err; + } + + /* change the rates */ clk_change_rate(top);