Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp5932025rdb; Sun, 17 Sep 2023 20:31:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGc5H6l9ozM51bwDqhvZQmu/Q3TKPHnMHpqfzoIkV/9PDXap/M73rPGFv/987j06hhMKZZh X-Received: by 2002:a05:6a00:cd1:b0:690:23b3:e030 with SMTP id b17-20020a056a000cd100b0069023b3e030mr8251391pfv.33.1695007915924; Sun, 17 Sep 2023 20:31:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695007915; cv=none; d=google.com; s=arc-20160816; b=g4zpYu9lz1wXLHSMMInoAD546ogZ6PJePGF9ZIzRbCsWqj8r//URnJyjGinFofAktd EWdEN2PtsPAKpAwffzw6r+GA8tdVgafXiFQRZ9sTGYcZhl72YPcNfh78CkpRiHxhl4Ke FCG/R0q217a9BFzUXpC0P4qs7nK9/TcgtmWycpKTqxerunHVHA+Io13U4bWPclsPSc+/ MwcybKB93GOQS9Ys0nVCkwG2CaqEThjywHEDtJJZWtphdy3UcCr1A+2IyQMHm31z7rpe 3xCauWmbvk5bFuPp7ZWEzIjdnKAwgZv5PDtrMUWh6bTz4+RWKkmoamEmJI3Y5z5JhR48 fbXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=d58DRMcmmqVdQpNWAbMiLz0ISFX3idJISFoDmPX5pV4=; fh=GcDD8kqSv7mGJsBEaENS9NE07tpyWHOAMcs7aovMq10=; b=qF1o22fsb+ULnCI0MK3CNA3VOGLzYTN5DrrEonyXNWBRCGsGx7zICu+8S0HHIZwIt1 UQSMjoxoJr6bZGqX1lgyDX/e+0vGBpPXOSgRLPX+d8742NEf9J/ln4ojqADD4qqOGte/ TbT76+i2dXHpJ3fWSFSpSMGB56G9fWrkS9kRoHN5JGzylOCagWUB0XarlQZyzxlOKz9C 0oxWZGr71cNIOcWqO/qPKCBeTq0hGlRqPO1mD7tbrU339P6+fYmvzlGGk1DUAgSVQVvT 2ghLMAhjY2VzgUWuav6LUWYnLAAFn0TIhqUUgyGcBYXr355Y8uZ8aP6x3fvrUMvHD0ZD 6Esg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Fs1yJekj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id b13-20020a6567cd000000b005778dc6a82csi7123909pgs.739.2023.09.17.20.31.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 17 Sep 2023 20:31:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Fs1yJekj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id F38BE8322737; Sun, 17 Sep 2023 15:47:41 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239626AbjIQWlH (ORCPT + 99 others); Sun, 17 Sep 2023 18:41:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236690AbjIQWko (ORCPT ); Sun, 17 Sep 2023 18:40:44 -0400 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B75412C; Sun, 17 Sep 2023 15:40:39 -0700 (PDT) Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-404773f2501so33760315e9.0; Sun, 17 Sep 2023 15:40:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1694990437; x=1695595237; darn=vger.kernel.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=Fs1yJekj5VZUsnEwk9NjkR7+d7C7lyzCpZip1ahwWs1mGgft6TG5RqPx+wrvTtqsWZ /QCbhKrRJDXxHldMLq3iHY0DvSJ37cGAsd7rO7u7G/UnOnohw8sUhKsgv1hvGTDNDAww OX1qTn04J6/iBfzcbRpJdPelsM1+/TTMGCiYgsKVnOiS154bMd/NNIt9qBLDJeXCilSZ KH4tAyjbUS8GMCB2HoFbFGcUmmeuP6Cl+f74TnNYNpq4AkxAzW55/wRZtPH2ugWiikcC 1OO0wAH6oq7hdQYEhqqvGXuFwBi9j1ffgisbh9sYlvjgvGgiOaL7QWlnO2eQWUtCdhWw iAxA== 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=VXyNeom/Fyb4dEl2F1Y5JaxUVfwz3zHMy5p7T1SbScVrYrFJhvSsihGAOehk9q9k9P Ea55zYD7NtynzM5/E6wt2bPGX5xTQbdfThUzr5GtPezM1yo7UUnz64zaeQCG7sm6xOrA ETZXEfxQGNdKH+yL3U14tiBoxg/DD/eeyH5ukz6c7Zb7lHHpfhPSgSctb2ZSPSQtpkhU 5fhd1pwaw+PrxOtxRRABr1G1jHj/wPtmEW9ZTnTAZb7VXebpbjpIY+6oDvgW8P8II5GB +1SRmnAKEw/+4aJlee8aK9gP5j5hDKPsoIplFBwKmY8UWxmvyO+cat/NaloJzV9EUwwZ 7tfg== X-Gm-Message-State: AOJu0YyIe0uu+Qhs+6T5rOShuYIqN5wHgEn7t7u+ESt94JseKIqY0jy1 mewS16w93B3q9CTezvjGCyA= 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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit 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-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Sun, 17 Sep 2023 15:47:42 -0700 (PDT) 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); -- 2.34.1