Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp108927iob; Tue, 17 May 2022 20:29:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyijNQPXiggMhEhd3EkJHwp5eKgZ/ZdGMcezgS6uqVRnz6OF/0pXdGi+0Sfq0DLxRjXbOgd X-Received: by 2002:a17:903:228a:b0:161:ba1f:4c28 with SMTP id b10-20020a170903228a00b00161ba1f4c28mr3135382plh.121.1652844546614; Tue, 17 May 2022 20:29:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652844546; cv=none; d=google.com; s=arc-20160816; b=EElASj6JGCyftk0bpez4p0drlM61Awx/LXqXmgVo3dIB6Yz4LlL+FXXkfTVQmjW268 lmdppva9n0R6jB8mssIjnLUKzC31XeWx+ysQ8ITHDVmoFExF1fXm9InNWSZsPEyXZ+M+ M2BVP/kbYLCatzC+xHg8/a3Su3urv75L64g8Vhmp4Tn02WstyZRZNfPh1hg1w/Bow2Qq Ryw22Ibtrq3HkP2q8RwX8sNvZxkcXPyGfmyDngYKY6jeCaPBP7RyocIytlNmJNV+5KRP BV1EgE4uGfJnflf8ikOVfaq0MzSYmUQP17Ps5XQsPRoRwBSHaL8BFru4tpTtfccqcxQD xsUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=sLYhfBC2Wn0WI956K4cX5zfF6kFRcWXkiUrSI8cWUU4=; b=Kh4NWoC7PmGX6LlNxA+bc0xs5+DvcpH/DGkZjRuaQ3c4pcbEKVie9WpukDqrqD6Djl oYLnux3CvHRt+007fzBUw4FC/GYLdpZIw7H7PlfP6/QKBYgEcS6U5MhJq1uvu1vnZSnz 4ldoTqQGvKd6zieHT9DMCIRq8M/NaU54QCpknY/o0AmPOwD0ddQ0D2lbvcdRHug3Xf2y XwlQIMykN9DYybFJ1PIwycrt+pATUtTP9ItLn0Zdq+A37t4x15CwVpNt9cb0/bYmfrL4 Syl99kvdZlrsIKakAaYwAXS8AqSgf3rKpen5+ZpULLurAf1wQiuJ8KXvuHN5k+gKm12b zk2Q== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id z15-20020a17090a8b8f00b001c664d0720bsi4661482pjn.69.2022.05.17.20.29.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 May 2022 20:29:06 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0FFD26C55D; Tue, 17 May 2022 20:23:53 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234618AbiEQUrM (ORCPT + 99 others); Tue, 17 May 2022 16:47:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231235AbiEQUrI (ORCPT ); Tue, 17 May 2022 16:47:08 -0400 Received: from mail-oi1-f175.google.com (mail-oi1-f175.google.com [209.85.167.175]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C12C537A1A; Tue, 17 May 2022 13:47:07 -0700 (PDT) Received: by mail-oi1-f175.google.com with SMTP id i66so259923oia.11; Tue, 17 May 2022 13:47:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=sLYhfBC2Wn0WI956K4cX5zfF6kFRcWXkiUrSI8cWUU4=; b=YH7cGEpJzSdOTGuMPGILJiTNw1FSI9Ljeg0aNGrmGJr66MJ6zfk46BrxE3t0LwQCnJ gBFDScwaIU+YMB0A63cHeAFgVuhbpL0rZNt3WxsBFpqz6SjcntVCb29CzH4lbfSg1MyT 24s8s7MG4yHf82UcwLgwmrrFkZIdqEyJpU6BvmJYj9eM3kKTIjokkcgKHV0VnO50tKsB WyAYbNf5rIrtfDcrcw1MfTNgl7mjIIks025TnVbDKosjHfMCDc96tGIoVmwHHXHXy5AV ANq7XQJjj0VzGScjUxvjUQEopo62RPAT5idfsT5BD2ltDzUUe2q6RXElXl4IdXZLFFVb Dhvw== X-Gm-Message-State: AOAM5320wVKZ+qCyjFd6fKw432DGsVcQkfCLx37Q1LwvQn5+ZXVqanpH bjJ2L1TfWBloqpF5KLvv3g== X-Received: by 2002:aca:bc57:0:b0:326:b2b6:2535 with SMTP id m84-20020acabc57000000b00326b2b62535mr16708742oif.107.1652820427027; Tue, 17 May 2022 13:47:07 -0700 (PDT) Received: from robh.at.kernel.org (66-90-144-107.dyn.grandenetworks.net. [66.90.144.107]) by smtp.gmail.com with ESMTPSA id g14-20020a056830160e00b006060322125csm150201otr.44.2022.05.17.13.47.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 May 2022 13:47:06 -0700 (PDT) Received: (nullmailer pid 1610923 invoked by uid 1000); Tue, 17 May 2022 20:47:05 -0000 Date: Tue, 17 May 2022 15:47:05 -0500 From: Rob Herring To: AngeloGioacchino Del Regno Cc: daniel.lezcano@linaro.org, tglx@linutronix.de, krzysztof.kozlowski+dt@linaro.org, matthias.bgg@gmail.com, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, konrad.dybcio@somainline.org, marijn.suijten@somainline.org, martin.botka@somainline.org, ~postmarketos/upstreaming@lists.sr.ht, phone-devel@vger.kernel.org, paul.bouchara@somainline.org Subject: Re: [PATCH v2 2/2] clocksource/drivers/timer-mediatek: Implement CPUXGPT timers Message-ID: <20220517204705.GA1597201-robh@kernel.org> References: <20220509210741.12020-1-angelogioacchino.delregno@collabora.com> <20220509210741.12020-3-angelogioacchino.delregno@collabora.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220509210741.12020-3-angelogioacchino.delregno@collabora.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 On Mon, May 09, 2022 at 11:07:40PM +0200, AngeloGioacchino Del Regno wrote: > Some MediaTek platforms with a buggy TrustZone ATF firmware will not > initialize the AArch64 System Timer correctly: in these cases, the > System Timer address is correctly programmed, as well as the CNTFRQ_EL0 > register (reading 13MHz, as it should be), but the assigned hardware > timers are never started before (or after) booting Linux. I believe the upstream position in regards to arch timer work-arounds is fix the firmware. Rob > > In this condition, any call to function get_cycles() will be returning > zero, as CNTVCT_EL0 will always read zero. > > One common critical symptom of that is trying to use the udelay() > function (calling __delay()), which executes the following loop: > > start = get_cycles(); > while ((get_cycles() - start) < cycles) > cpu_relax(); > > which, when CNTVCT_EL0 always reads zero, translates to: > > while((0 - 0) < 0) ==> while(0 < 0) > > ... generating an infinite loop, even though zero is never less > than zero, but always equal to it (this has to be researched, > but it's out of the scope of this commit). > > To fix this issue on the affected MediaTek platforms, the solution > is to simply start the timers that are designed to be System Timer(s). > These timers, downstream, are called "CPUXGPT" and there is one > timer per CPU core; luckily, it is not necessary to set a start bit > on each CPUX General Purpose Timer, but it's conveniently enough to: > - Set the clock divider (input = 26MHz, divider = 2, output = 13MHz); > - Set the ENABLE bit on a global register (starts all CPUX timers). > > The only small hurdle with this setup is that it's all done through > the MCUSYS wrapper, where it is needed, for each read or write, to > select a register address (by writing it to an index register) and > then to perform any R/W on a "CON" register. > > For example, writing "0x1" to the CPUXGPT register offset 0x4: > - Write 0x4 to mcusys INDEX register > - Write 0x1 to mcusys CON register > > Reading from CPUXGPT register offset 0x4: > - Write 0x4 to mcusys INDEX register > - Read mcusys CON register. > > Finally, starting this timer makes platforms affected by this issue > to work correctly. > > Signed-off-by: AngeloGioacchino Del Regno > --- > drivers/clocksource/timer-mediatek.c | 119 +++++++++++++++++++++++++++ > 1 file changed, 119 insertions(+) > > diff --git a/drivers/clocksource/timer-mediatek.c b/drivers/clocksource/timer-mediatek.c > index 7bcb4a3f26fb..a3e90047f9ac 100644 > --- a/drivers/clocksource/timer-mediatek.c > +++ b/drivers/clocksource/timer-mediatek.c > @@ -22,6 +22,19 @@ > > #define TIMER_SYNC_TICKS (3) > > +/* cpux mcusys wrapper */ > +#define CPUX_CON_REG 0x0 > +#define CPUX_IDX_REG 0x4 > + > +/* cpux */ > +#define CPUX_IDX_GLOBAL_CTRL 0x0 > + #define CPUX_ENABLE BIT(0) > + #define CPUX_CLK_DIV_MASK GENMASK(10, 8) > + #define CPUX_CLK_DIV1 BIT(8) > + #define CPUX_CLK_DIV2 BIT(9) > + #define CPUX_CLK_DIV4 BIT(10) > +#define CPUX_IDX_GLOBAL_IRQ 0x30 > + > /* gpt */ > #define GPT_IRQ_EN_REG 0x00 > #define GPT_IRQ_ENABLE(val) BIT((val) - 1) > @@ -72,6 +85,57 @@ > > static void __iomem *gpt_sched_reg __read_mostly; > > +static u32 mtk_cpux_readl(u32 reg_idx, struct timer_of *to) > +{ > + writel(reg_idx, timer_of_base(to) + CPUX_IDX_REG); > + return readl(timer_of_base(to) + CPUX_CON_REG); > +} > + > +static void mtk_cpux_writel(u32 val, u32 reg_idx, struct timer_of *to) > +{ > + writel(reg_idx, timer_of_base(to) + CPUX_IDX_REG); > + writel(val, timer_of_base(to) + CPUX_CON_REG); > +} > + > +static void mtk_cpux_disable_irq(struct timer_of *to) > +{ > + const unsigned long *irq_mask = cpumask_bits(cpu_possible_mask); > + u32 val; > + > + val = mtk_cpux_readl(CPUX_IDX_GLOBAL_IRQ, to); > + val &= ~(*irq_mask); > + mtk_cpux_writel(val, CPUX_IDX_GLOBAL_IRQ, to); > +} > + > +static void mtk_cpux_enable_irq(struct timer_of *to) > +{ > + const unsigned long *irq_mask = cpumask_bits(cpu_possible_mask); > + u32 val; > + > + val = mtk_cpux_readl(CPUX_IDX_GLOBAL_IRQ, to); > + val |= *irq_mask; > + mtk_cpux_writel(val, CPUX_IDX_GLOBAL_IRQ, to); > +} > + > +static int mtk_cpux_clkevt_shutdown(struct clock_event_device *clkevt) > +{ > + /* Clear any irq */ > + mtk_cpux_disable_irq(to_timer_of(clkevt)); > + > + /* > + * Disabling CPUXGPT timer will crash the platform, especially > + * if Trusted Firmware is using it (usually, for sleep states), > + * so we only mask the IRQ and call it a day. > + */ > + return 0; > +} > + > +static int mtk_cpux_clkevt_resume(struct clock_event_device *clkevt) > +{ > + mtk_cpux_enable_irq(to_timer_of(clkevt)); > + return 0; > +} > + > static void mtk_syst_ack_irq(struct timer_of *to) > { > /* Clear and disable interrupt */ > @@ -281,6 +345,60 @@ static struct timer_of to = { > }, > }; > > +static int __init mtk_cpux_init(struct device_node *node) > +{ > + static struct timer_of to_cpux; > + u32 freq, val; > + int ret; > + > + /* > + * There are per-cpu interrupts for the CPUX General Purpose Timer > + * but since this timer feeds the AArch64 System Timer we can rely > + * on the CPU timer PPIs as well, so we don't declare TIMER_OF_IRQ. > + */ > + to_cpux.flags = TIMER_OF_BASE | TIMER_OF_CLOCK; > + to_cpux.clkevt.name = "mtk-cpuxgpt"; > + to_cpux.clkevt.rating = 10; > + to_cpux.clkevt.cpumask = cpu_possible_mask; > + to_cpux.clkevt.set_state_shutdown = mtk_cpux_clkevt_shutdown; > + to_cpux.clkevt.tick_resume = mtk_cpux_clkevt_resume; > + > + /* If this fails, bad things are about to happen... */ > + ret = timer_of_init(node, &to_cpux); > + if (ret) { > + WARN(1, "Cannot start CPUX timers.\n"); > + return ret; > + } > + > + /* > + * Check if we're given a clock with the right frequency for this > + * timer, otherwise warn but keep going with the setup anyway, as > + * that makes it possible to still boot the kernel, even though > + * it may not work correctly (random lockups, etc). > + * The reason behind this is that having an early UART may not be > + * possible for everyone and this gives a chance to retrieve kmsg > + * for eventual debugging even on consumer devices. > + */ > + freq = timer_of_rate(&to_cpux); > + if (freq > 13000000) > + WARN(1, "Requested unsupported timer frequency %u\n", freq); > + > + /* Clock input is 26MHz, set DIV2 to achieve 13MHz clock */ > + val = mtk_cpux_readl(CPUX_IDX_GLOBAL_CTRL, &to_cpux); > + val &= ~CPUX_CLK_DIV_MASK; > + val |= CPUX_CLK_DIV2; > + mtk_cpux_writel(val, CPUX_IDX_GLOBAL_CTRL, &to_cpux); > + > + /* Enable all CPUXGPT timers */ > + val = mtk_cpux_readl(CPUX_IDX_GLOBAL_CTRL, &to_cpux); > + mtk_cpux_writel(val | CPUX_ENABLE, CPUX_IDX_GLOBAL_CTRL, &to_cpux); > + > + clockevents_config_and_register(&to_cpux.clkevt, timer_of_rate(&to_cpux), > + TIMER_SYNC_TICKS, 0xffffffff); > + > + return 0; > +} > + > static int __init mtk_syst_init(struct device_node *node) > { > int ret; > @@ -339,3 +457,4 @@ static int __init mtk_gpt_init(struct device_node *node) > } > TIMER_OF_DECLARE(mtk_mt6577, "mediatek,mt6577-timer", mtk_gpt_init); > TIMER_OF_DECLARE(mtk_mt6765, "mediatek,mt6765-timer", mtk_syst_init); > +TIMER_OF_DECLARE(mtk_mt6795, "mediatek,mt6795-systimer", mtk_cpux_init); > -- > 2.35.1 > >