Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp233652pxb; Thu, 14 Jan 2021 04:44:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJyMwfcG0qDH+E3CEFLNsk4wC4UopzM8IhX4DlQf2dUru5BFgwt6h/o48FYXkNr0KUA/bc5n X-Received: by 2002:a50:f392:: with SMTP id g18mr5627590edm.306.1610628292189; Thu, 14 Jan 2021 04:44:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610628292; cv=none; d=google.com; s=arc-20160816; b=a0ooBiyAY+l7iR4wXUt+gwU5j5fJMXraH6rG+W48taSDgObGQRIDjAqYd2ZivYFgs+ +OxTPCtI5L4BF9LEzkkeLtlXF4Z3OMFlE2qG1ToimybQsm0E8Hj1WhZl5oTSVVQrM3Qz JHjNmTWw6vZkOQDH56DHJQGyhuW6CtCUdDS+8PbcyrA++ZCTR+MInrwYQHeSYUprrSWD m+lUgshzXpHuFvKsGlC4RC+1cKDSB+Zs+G1a4CG0eT13p7riXEbMsHBkTW0OJj24BTv4 yPjh1leE6+a07AArOXfsi2UGt2YHwTI695zJ1CGTEt0HInSRJ1LIESFF8nKhQWPber61 He9g== 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=CfjYSudYSWUCREat+I9vqS97DeZs0Rx3BsPzJnhAA4w=; b=wKbrEC4XKyPYa5W59YPj4uzOjg7ho4SMcUnR1Rjc/VlbTYdPqHmNxHRCr/JUg+U0FV cbFTEPqRyK95orPSPDHJksV1yN/PA8HBxbW32F3towgFuD1lx82etAWUuhYVVgpKJqEo FOU+xjihjgs2bOlKLguYGdRCxBcawgGTS1L9J2VXSIr1OyWF37rGQZTCM0F7GEu9PGWM kADn7yAyCC//n0P6T/wTKJfYiFgNgSt9QMCHCvZaCl51/qrWV9O0R5rr2wFSxymGT83X egVYFbuWtD0LKLK1G7IL7SbgoGGsnsIB4sqmAWOiLPyCs1CasTJcWfylR2RXJjr7VwEp q9rA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=lGUG7aVV; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w5si2401158ejc.599.2021.01.14.04.44.28; Thu, 14 Jan 2021 04:44:52 -0800 (PST) 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=@kernel.org header.s=k20201202 header.b=lGUG7aVV; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728875AbhANMmP (ORCPT + 99 others); Thu, 14 Jan 2021 07:42:15 -0500 Received: from mail.kernel.org ([198.145.29.99]:59364 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726329AbhANMmO (ORCPT ); Thu, 14 Jan 2021 07:42:14 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id C5DD523A61; Thu, 14 Jan 2021 12:40:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1610628056; bh=BM2JrmrK5VRVYXEAX31FmsqxA+U0Nplk6UV2WlHNTT8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lGUG7aVVKLpy0ULIGMXUEMD7z3ejJd1HmKIM0lJ8DsPhmL5gQs8JX3k78U556NlS7 ia/F0SoOL7h9crLT+zM0RYBO24dV7JHvX9Ju8OHlMRQc9XXK8uZNC3IPCVQhHWWviV FBh9DExD1rw2ZCijn4Gdgd8r79UFZnQDsImn+dEkI1azCBgj/6gwhPlyU375PW0TeW bPrLKAv1igtFr+i0b1LVWgZ0rFYaIQPLuZI1wsMy+ZIkc3D0fXxOALF4FlY1loW/oN iRg6jrcBlIn63S4nrhdQyMhk0PN7D1+imM/HbJX5VZQx8/GZMvvx9QeR6UviO1eRA0 RajRerSTd0YUQ== Received: by pali.im (Postfix) id 8E51B843; Thu, 14 Jan 2021 13:40:54 +0100 (CET) From: =?UTF-8?q?Pali=20Roh=C3=A1r?= To: Gregory Clement , Andrew Lunn , Michael Turquette , Stephen Boyd , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org Cc: =?UTF-8?q?Marek=20Beh=C3=BAn?= , "Miquel Raynal" , "Tomasz Maciej Nowak" , "Luka Perkov" , "Andre Heider" , "Vladimir Vid" , "Russell King" , =?UTF-8?q?G=C3=A9rald=20Kerma?= , "Konstantin Porotchkin" Subject: [PATCH mvebu v2 06/10] clk: mvebu: armada-37xx-periph: Fix workaround for switching from L1 to L0 Date: Thu, 14 Jan 2021 13:40:28 +0100 Message-Id: <20210114124032.12765-7-pali@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210114124032.12765-1-pali@kernel.org> References: <20210114124032.12765-1-pali@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When CPU frequency is at 250 MHz and set_rate() is called with 500 MHz (L1) quickly followed by a call with 1 GHz (L0), the CPU does not necessarily stay in L1 for at least 20ms as is required by Marvell errata. This situation happens frequently with the ondemand cpufreq governor and can be also reproduced with userspace governor. In most cases it causes CPU to crash. This change fixes the above issue and ensures that the CPU always stays in L1 for at least 20ms when switching from any state to L0. Signed-off-by: Marek Behún Signed-off-by: Pali Rohár Fixes: 61c40f35f5cd ("clk: mvebu: armada-37xx-periph: Fix switching CPU rate from 300Mhz to 1.2GHz") Cc: stable@vger.kernel.org --- drivers/clk/mvebu/armada-37xx-periph.c | 45 ++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/drivers/clk/mvebu/armada-37xx-periph.c b/drivers/clk/mvebu/armada-37xx-periph.c index b15e177bea7e..32ac6b6b7530 100644 --- a/drivers/clk/mvebu/armada-37xx-periph.c +++ b/drivers/clk/mvebu/armada-37xx-periph.c @@ -84,6 +84,7 @@ struct clk_pm_cpu { void __iomem *reg_div; u8 shift_div; struct regmap *nb_pm_base; + unsigned long l1_expiration; }; #define to_clk_double_div(_hw) container_of(_hw, struct clk_double_div, hw) @@ -504,22 +505,52 @@ static long clk_pm_cpu_round_rate(struct clk_hw *hw, unsigned long rate, * 2. Sleep 20ms for stabling VDD voltage * 3. Then switch from L1 (500/600 MHz) to L0 (1000/1200 MHz). */ -static void clk_pm_cpu_set_rate_wa(unsigned long rate, struct regmap *base) +static void clk_pm_cpu_set_rate_wa(struct clk_pm_cpu *pm_cpu, + unsigned int new_level, unsigned long rate, + struct regmap *base) { unsigned int cur_level; - if (rate < 1000 * 1000 * 1000) - return; - regmap_read(base, ARMADA_37XX_NB_CPU_LOAD, &cur_level); cur_level &= ARMADA_37XX_NB_CPU_LOAD_MASK; - if (cur_level <= ARMADA_37XX_DVFS_LOAD_1) + + if (cur_level == new_level) + return; + + /* + * System wants to go to L1 on its own. If we are going from L2/L3, + * remember when 20ms will expire. If from L0, set the value so that + * next switch to L0 won't have to wait. + */ + if (new_level == ARMADA_37XX_DVFS_LOAD_1) { + if (cur_level == ARMADA_37XX_DVFS_LOAD_0) + pm_cpu->l1_expiration = jiffies; + else + pm_cpu->l1_expiration = jiffies + msecs_to_jiffies(20); return; + } + + /* + * If we are setting to L2/L3, just invalidate L1 expiration time, + * sleeping is not needed. + */ + if (rate < 1000*1000*1000) + goto invalidate_l1_exp; + + /* + * We are going to L0 with rate >= 1GHz. Check whether we have been at + * L1 for long enough time. If not, go to L1 for 20ms. + */ + if (pm_cpu->l1_expiration && jiffies >= pm_cpu->l1_expiration) + goto invalidate_l1_exp; regmap_update_bits(base, ARMADA_37XX_NB_CPU_LOAD, ARMADA_37XX_NB_CPU_LOAD_MASK, ARMADA_37XX_DVFS_LOAD_1); msleep(20); + +invalidate_l1_exp: + pm_cpu->l1_expiration = 0; } static int clk_pm_cpu_set_rate(struct clk_hw *hw, unsigned long rate, @@ -553,7 +584,9 @@ static int clk_pm_cpu_set_rate(struct clk_hw *hw, unsigned long rate, reg = ARMADA_37XX_NB_CPU_LOAD; mask = ARMADA_37XX_NB_CPU_LOAD_MASK; - clk_pm_cpu_set_rate_wa(rate, base); + /* Apply workaround when base CPU frequency is 1000 or 1200 MHz */ + if (parent_rate >= 1000*1000*1000) + clk_pm_cpu_set_rate_wa(pm_cpu, load_level, rate, base); regmap_update_bits(base, reg, mask, load_level); -- 2.20.1