Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3009405pxj; Mon, 10 May 2021 16:18:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx/0emMBRYCTv0NYdnyWQ7RFb6C/odxh0JGuDt//NaMqQYSALIoOzbYftDiz/u/KRck9gDb X-Received: by 2002:a5d:9546:: with SMTP id a6mr19404856ios.102.1620688724620; Mon, 10 May 2021 16:18:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620688724; cv=none; d=google.com; s=arc-20160816; b=go66U1CkTjak6npZrPaB3TMmCT6Tj6/vjVr3CuatpiDdF0HaqJ01s7f6fzsqmaJotR KPf8iSV2wl96NP3wcrQ7fJg4zZYdKV/b4OXnxYOEQfTQyi/JNw451TD4BAxLT0qWbxHE bJkJR3AJtAWbGegtiJLRmhLDX5+7XrA6Cpx7Z5wakoPntZr/4rqoIYtAKrOY4VZEOGhc 4kXlYMuv05BrKvg2SpsTGifBCUtkx7akmiJtjNLVBgnp17s8m8KXNNxAHX2PNUCBuQ+0 xZxHJAtYaJhLaV/i9/cmvqGj60sZPrrULEdFM8Gpn97GWT3utiG23u8IsxPgGYkKTXXT /z9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=3LQbWgDnRWrw67JOcPZWnjtRYMIRGGi64/mZ6QT+/Lg=; b=uSHw+HxrIgEIwiSLrsr2tT+Jv3snksmI7K3PFGrlNrKzEN1+plJDLlLkaeu7ei7rrL 9sayXWiCTVe+SafdITVO5uSIbbnFNKKYStHDTDv6VTZCO1Mi2/bWDPKQkk9mAR85dJY8 eG7HTgK0eCI2NkjoKxFKhhdHqRth7ee0TZqV/BW8EyBuAQG4S1i3nWXIEKwsWBUdWO3F DKVLXkFyF04ZkZUAYhB38ltHyJdEVOsYwJc7in+78is3kW72JoY489HNyMYGxRXozKb5 1uVdYhX5wNUuOn18+zIGs3aQX7CNH/ac8RMCQOJ4qTGMIe+A9cCC0Cba8NfaUNJV87SN GOog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=XcNwzUcN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f20si14715093ion.8.2021.05.10.16.18.32; Mon, 10 May 2021 16:18:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=XcNwzUcN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230095AbhEJXSz (ORCPT + 99 others); Mon, 10 May 2021 19:18:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229961AbhEJXSv (ORCPT ); Mon, 10 May 2021 19:18:51 -0400 Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 445FDC061574; Mon, 10 May 2021 16:17:46 -0700 (PDT) Received: by mail-lf1-x129.google.com with SMTP id a2so4850676lfc.9; Mon, 10 May 2021 16:17:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=3LQbWgDnRWrw67JOcPZWnjtRYMIRGGi64/mZ6QT+/Lg=; b=XcNwzUcNbm84wVkSFYSpgPvvKY7Ljq+qjzLDmaIjMq/ehF2qRvUSjiUnXk9BG5qM75 hKaue9PkHlTn8UvBTdSFqQPH2Tjt0ogYH++ye3wBXbsGy33Lhr0a+hZJWEbHd72qGXTl rmh2MOMToq/ndlSph+4V3CiSNANTQtx08iUJMloYFcAdp5TJfeRnvhhM/jJt9gBiLDv3 jtiupVz8VgLHr4CCXw8mRNCtgbiClzgiiYacaxuKNKUTlFiV4i3D0DLNe4fE3j+nhMiS C55XPeXxMtdQkWpV11l0vxrrxo3rs6M+tJqoOvjr63TIRTCAABjsy2zGh2+RDu73vBsr NNnQ== 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=3LQbWgDnRWrw67JOcPZWnjtRYMIRGGi64/mZ6QT+/Lg=; b=ook2hfAaAxjIxY+C3DWwE2cpkyzt9AR7Izv/XtVpe8G7KjuEaB0g/I641IoS/bJIG/ zdu/6QXgnOaL5NLZ/DOhmLiodxCaDL6MPo7R7naOR2MNF69jckURGgKE6pXMfa3U5M7B 7TByQLb1jSLqqc9XtaYanxJgOeeoM+k2LAVlxeFRbfW2asXUTcMb/QWWcchFw35mMHdj E7sdkPScmzKZt2i11NW9/1219FrD5stM/8C56tJBj4+CFSyC8ew2yublIi0HhiRrQGFY 11QtvAUpmzVk7ShhJVOMLkMC134DdlRzmbur6ciQ7riCkAJMFq9CkmSqThcByHAl6ofU tdKg== X-Gm-Message-State: AOAM531SlzS8d13J957/G1wVG7EdlCz5P7nkqx7BCI+4SSS4CPqsQ18b Dlk+R8zkTiaV8R4QqAhjfkM= X-Received: by 2002:a05:6512:31c6:: with SMTP id j6mr18794865lfe.129.1620688664787; Mon, 10 May 2021 16:17:44 -0700 (PDT) Received: from localhost.localdomain (109-252-193-91.dynamic.spd-mgts.ru. [109.252.193.91]) by smtp.gmail.com with ESMTPSA id k8sm2422254lfo.123.2021.05.10.16.17.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 May 2021 16:17:44 -0700 (PDT) From: Dmitry Osipenko To: Thierry Reding , Jonathan Hunter , Peter De Schrijver , Prashant Gaikwad , Michael Turquette , Stephen Boyd , Rob Herring , =?UTF-8?q?Micha=C5=82=20Miros=C5=82aw?= Cc: linux-tegra@vger.kernel.org, linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org Subject: [PATCH v7 2/8] clk: tegra: Fix refcounting of gate clocks Date: Tue, 11 May 2021 02:17:31 +0300 Message-Id: <20210510231737.30313-3-digetx@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210510231737.30313-1-digetx@gmail.com> References: <20210510231737.30313-1-digetx@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The refcounting of the gate clocks has a bug causing the enable_refcnt to underflow when unused clocks are disabled. This happens because clk provider erroneously bumps the refcount if clock is enabled at a boot time, which it shouldn't be doing, and it does this only for the gate clocks, while peripheral clocks are using the same gate ops and the peripheral clocks are missing the initial bump. Hence the refcount of the peripheral clocks is 0 when unused clocks are disabled and then the counter is decremented further by the gate ops, causing the integer underflow. Fix this problem by removing the erroneous bump and by implementing the disable_unused() callback, which disables the unused gates properly. The visible effect of the bug is such that the unused clocks are never gated if a loaded kernel module grabs the unused clocks and starts to use them. In practice this shouldn't cause any real problems for the drivers and boards supported by the kernel today. Acked-by: Thierry Reding Signed-off-by: Dmitry Osipenko --- drivers/clk/tegra/clk-periph-gate.c | 72 +++++++++++++++++++---------- drivers/clk/tegra/clk-periph.c | 11 +++++ 2 files changed, 58 insertions(+), 25 deletions(-) diff --git a/drivers/clk/tegra/clk-periph-gate.c b/drivers/clk/tegra/clk-periph-gate.c index 4b31beefc9fc..dc3f92678407 100644 --- a/drivers/clk/tegra/clk-periph-gate.c +++ b/drivers/clk/tegra/clk-periph-gate.c @@ -48,18 +48,9 @@ static int clk_periph_is_enabled(struct clk_hw *hw) return state; } -static int clk_periph_enable(struct clk_hw *hw) +static void clk_periph_enable_locked(struct clk_hw *hw) { struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw); - unsigned long flags = 0; - - spin_lock_irqsave(&periph_ref_lock, flags); - - gate->enable_refcnt[gate->clk_num]++; - if (gate->enable_refcnt[gate->clk_num] > 1) { - spin_unlock_irqrestore(&periph_ref_lock, flags); - return 0; - } write_enb_set(periph_clk_to_bit(gate), gate); udelay(2); @@ -78,6 +69,32 @@ static int clk_periph_enable(struct clk_hw *hw) udelay(1); writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE); } +} + +static void clk_periph_disable_locked(struct clk_hw *hw) +{ + struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw); + + /* + * If peripheral is in the APB bus then read the APB bus to + * flush the write operation in apb bus. This will avoid the + * peripheral access after disabling clock + */ + if (gate->flags & TEGRA_PERIPH_ON_APB) + tegra_read_chipid(); + + write_enb_clr(periph_clk_to_bit(gate), gate); +} + +static int clk_periph_enable(struct clk_hw *hw) +{ + struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw); + unsigned long flags = 0; + + spin_lock_irqsave(&periph_ref_lock, flags); + + if (!gate->enable_refcnt[gate->clk_num]++) + clk_periph_enable_locked(hw); spin_unlock_irqrestore(&periph_ref_lock, flags); @@ -91,21 +108,28 @@ static void clk_periph_disable(struct clk_hw *hw) spin_lock_irqsave(&periph_ref_lock, flags); - gate->enable_refcnt[gate->clk_num]--; - if (gate->enable_refcnt[gate->clk_num] > 0) { - spin_unlock_irqrestore(&periph_ref_lock, flags); - return; - } + WARN_ON(!gate->enable_refcnt[gate->clk_num]); + + if (--gate->enable_refcnt[gate->clk_num] == 0) + clk_periph_disable_locked(hw); + + spin_unlock_irqrestore(&periph_ref_lock, flags); +} + +static void clk_periph_disable_unused(struct clk_hw *hw) +{ + struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw); + unsigned long flags = 0; + + spin_lock_irqsave(&periph_ref_lock, flags); /* - * If peripheral is in the APB bus then read the APB bus to - * flush the write operation in apb bus. This will avoid the - * peripheral access after disabling clock + * Some clocks are duplicated and some of them are marked as critical, + * like fuse and fuse_burn for example, thus the enable_refcnt will + * be non-zero here if the "unused" duplicate is disabled by CCF. */ - if (gate->flags & TEGRA_PERIPH_ON_APB) - tegra_read_chipid(); - - write_enb_clr(periph_clk_to_bit(gate), gate); + if (!gate->enable_refcnt[gate->clk_num]) + clk_periph_disable_locked(hw); spin_unlock_irqrestore(&periph_ref_lock, flags); } @@ -114,6 +138,7 @@ const struct clk_ops tegra_clk_periph_gate_ops = { .is_enabled = clk_periph_is_enabled, .enable = clk_periph_enable, .disable = clk_periph_disable, + .disable_unused = clk_periph_disable_unused, }; struct clk *tegra_clk_register_periph_gate(const char *name, @@ -148,9 +173,6 @@ struct clk *tegra_clk_register_periph_gate(const char *name, gate->enable_refcnt = enable_refcnt; gate->regs = pregs; - if (read_enb(gate) & periph_clk_to_bit(gate)) - enable_refcnt[clk_num]++; - /* Data in .init is copied by clk_register(), so stack variable OK */ gate->hw.init = &init; diff --git a/drivers/clk/tegra/clk-periph.c b/drivers/clk/tegra/clk-periph.c index 67620c7ecd9e..79ca3aa072b7 100644 --- a/drivers/clk/tegra/clk-periph.c +++ b/drivers/clk/tegra/clk-periph.c @@ -100,6 +100,15 @@ static void clk_periph_disable(struct clk_hw *hw) gate_ops->disable(gate_hw); } +static void clk_periph_disable_unused(struct clk_hw *hw) +{ + struct tegra_clk_periph *periph = to_clk_periph(hw); + const struct clk_ops *gate_ops = periph->gate_ops; + struct clk_hw *gate_hw = &periph->gate.hw; + + gate_ops->disable_unused(gate_hw); +} + static void clk_periph_restore_context(struct clk_hw *hw) { struct tegra_clk_periph *periph = to_clk_periph(hw); @@ -126,6 +135,7 @@ const struct clk_ops tegra_clk_periph_ops = { .is_enabled = clk_periph_is_enabled, .enable = clk_periph_enable, .disable = clk_periph_disable, + .disable_unused = clk_periph_disable_unused, .restore_context = clk_periph_restore_context, }; @@ -135,6 +145,7 @@ static const struct clk_ops tegra_clk_periph_nodiv_ops = { .is_enabled = clk_periph_is_enabled, .enable = clk_periph_enable, .disable = clk_periph_disable, + .disable_unused = clk_periph_disable_unused, .restore_context = clk_periph_restore_context, }; -- 2.30.2