From patchwork Fri May 13 04:37:39 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Crystal Wood X-Patchwork-Id: 9087901 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 6AEA29F1C3 for ; Fri, 13 May 2016 04:39:34 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 24B59201FE for ; Fri, 13 May 2016 04:39:33 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id F066C200F2 for ; Fri, 13 May 2016 04:39:31 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1b14rk-00036X-1r; Fri, 13 May 2016 04:38:20 +0000 Received: from host.buserror.net ([209.198.135.123]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1b14rX-0002sr-8M for linux-arm-kernel@lists.infradead.org; Fri, 13 May 2016 04:38:08 +0000 Received: from c-75-72-173-242.hsd1.mn.comcast.net ([75.72.173.242] helo=snotra.am.freescale.net) by host.buserror.net with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_CBC_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1b14r8-00056Q-Pp; Thu, 12 May 2016 23:37:44 -0500 From: Scott Wood To: Catalin Marinas , Will Deacon , Marc Zyngier Date: Thu, 12 May 2016 23:37:39 -0500 Message-Id: <1463114260-8724-1-git-send-email-oss@buserror.net> X-Mailer: git-send-email 2.5.0 X-SA-Exim-Connect-IP: 75.72.173.242 X-SA-Exim-Mail-From: oss@buserror.net X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Spam-Level: X-Spam-Status: No, score=-5.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 Subject: [PATCH v2 1/2] ARM64: arch_timer: Work around QorIQ Erratum A-008585 X-SA-Exim-Version: 4.2.1 (built Mon, 26 Dec 2011 16:57:07 +0000) X-SA-Exim-Scanned: Yes (on host.buserror.net) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160512_213807_395661_3742E2F5 X-CRM114-Status: GOOD ( 19.62 ) X-Spam-Score: -5.6 (-----) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Scott Wood , devicetree@vger.kernel.org, stuart.yoder@nxp.com, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Erratum A-008585 says that the ARM generic timer counter "has the potential to contain an erroneous value for a small number of core clock cycles every time the timer value changes". Accesses to TVAL (both read and write) are also affected due to the implicit counter read. Accesses to CVAL are not affected. The workaround is to reread TVAL and count registers until successive reads return the same value, and when writing TVAL to retry until counter reads before and after the write return the same value. This erratum can be found on LS1043A and LS2080A. Signed-off-by: Scott Wood Acked-by: Rob Herring --- v2: Significant rework based on feedback, including using static_key, disabling VDSO counter access rather than adding the workaround to the VDSO, and uninlining the loops. Dropped the separate property for indicating that writes to TVAL are affected, as I believe that's just a side effect of the implicit counter read being corrupted, and thus a chip that is affected by one will always be affected by the other. Dropped the arm32 portion as it seems there was confusion about whether LS1021A is affected. Currently I am being told that it is not affected. I considered writing to CVAL rather than looping on TVAL writes, but that would still have required separate set_next_event() code for the erratum, and adding CVAL to the enum would have required a bunch of extra handlers in switch statements (even where unused, due to compiler warnings about unhandled enum values) including in an arm32 header. It seemed better to avoid the arm32 interaction and new untested accessors. --- .../devicetree/bindings/arm/arch_timer.txt | 6 ++ arch/arm64/include/asm/arch_timer.h | 37 +++++-- drivers/clocksource/arm_arch_timer.c | 110 +++++++++++++++++++++ 3 files changed, 144 insertions(+), 9 deletions(-) diff --git a/Documentation/devicetree/bindings/arm/arch_timer.txt b/Documentation/devicetree/bindings/arm/arch_timer.txt index e774128..ef5fbe9 100644 --- a/Documentation/devicetree/bindings/arm/arch_timer.txt +++ b/Documentation/devicetree/bindings/arm/arch_timer.txt @@ -25,6 +25,12 @@ to deliver its interrupts via SPIs. - always-on : a boolean property. If present, the timer is powered through an always-on power domain, therefore it never loses context. +- fsl,erratum-a008585 : A boolean property. Indicates the presence of + QorIQ erratum A-008585, which says that reading the counter is + unreliable unless the same value is returned by back-to-back reads. + This also affects writes to the tval register, due to the implicit + counter read. + ** Optional properties: - arm,cpu-registers-not-fw-configured : Firmware does not initialize diff --git a/arch/arm64/include/asm/arch_timer.h b/arch/arm64/include/asm/arch_timer.h index fbe0ca3..9715e85 100644 --- a/arch/arm64/include/asm/arch_timer.h +++ b/arch/arm64/include/asm/arch_timer.h @@ -23,10 +23,33 @@ #include #include +#include #include #include +extern struct static_key_false arch_timer_read_ool_enabled; + +#define ARCH_TIMER_REG_READ(reg, func) \ +extern u64 func##_ool(void); \ +static inline u64 __##func(void) \ +{ \ + u64 val; \ + asm volatile("mrs %0, " reg : "=r" (val)); \ + return val; \ +} \ +static inline u64 _##func(void) \ +{ \ + if (static_branch_unlikely(&arch_timer_read_ool_enabled)) \ + return func##_ool(); \ + else \ + return __##func(); \ +} + +ARCH_TIMER_REG_READ("cntp_tval_el0", arch_timer_get_ptval) +ARCH_TIMER_REG_READ("cntv_tval_el0", arch_timer_get_vtval) +ARCH_TIMER_REG_READ("cntvct_el0", arch_counter_get_cntvct) + /* * These register accessors are marked inline so the compiler can * nicely work out which register we want, and chuck away the rest of @@ -66,19 +89,19 @@ u32 arch_timer_reg_read_cp15(int access, enum arch_timer_reg reg) if (access == ARCH_TIMER_PHYS_ACCESS) { switch (reg) { case ARCH_TIMER_REG_CTRL: - asm volatile("mrs %0, cntp_ctl_el0" : "=r" (val)); + asm volatile("mrs %0, cntp_ctl_el0" : "=r" (val)); break; case ARCH_TIMER_REG_TVAL: - asm volatile("mrs %0, cntp_tval_el0" : "=r" (val)); + val = _arch_timer_get_ptval(); break; } } else if (access == ARCH_TIMER_VIRT_ACCESS) { switch (reg) { case ARCH_TIMER_REG_CTRL: - asm volatile("mrs %0, cntv_ctl_el0" : "=r" (val)); + asm volatile("mrs %0, cntv_ctl_el0" : "=r" (val)); break; case ARCH_TIMER_REG_TVAL: - asm volatile("mrs %0, cntv_tval_el0" : "=r" (val)); + val = _arch_timer_get_vtval(); break; } } @@ -116,12 +139,8 @@ static inline u64 arch_counter_get_cntpct(void) static inline u64 arch_counter_get_cntvct(void) { - u64 cval; - isb(); - asm volatile("mrs %0, cntvct_el0" : "=r" (cval)); - - return cval; + return _arch_counter_get_cntvct(); } static inline int arch_timer_arch_init(void) diff --git a/drivers/clocksource/arm_arch_timer.c b/drivers/clocksource/arm_arch_timer.c index 5152b38..6f78831 100644 --- a/drivers/clocksource/arm_arch_timer.c +++ b/drivers/clocksource/arm_arch_timer.c @@ -79,10 +79,52 @@ static enum ppi_nr arch_timer_uses_ppi = VIRT_PPI; static bool arch_timer_c3stop; static bool arch_timer_mem_use_virtual; +DEFINE_STATIC_KEY_FALSE(arch_timer_read_ool_enabled); +EXPORT_SYMBOL_GPL(arch_timer_read_ool_enabled); + /* * Architected system timer support. */ +#ifdef CONFIG_ARM64 +/* + * __always_inline is used to ensure that func() is not an actual function + * pointer, which would result in the register accesses potentially being too + * far apart for the loop to work. + */ +static __always_inline u64 arch_timer_reread(u64 (*func)(void)) +{ + u64 cval_old, cval_new; + int timeout = 200; + + do { + isb(); + cval_old = func(); + cval_new = func(); + timeout--; + } while (cval_old != cval_new && timeout); + + WARN_ON_ONCE(!timeout); + return cval_new; +} + +u64 arch_counter_get_cntvct_ool(void) +{ + return arch_timer_reread(__arch_counter_get_cntvct); +} + +u64 arch_timer_get_vtval_ool(void) +{ + return arch_timer_reread(__arch_timer_get_vtval); +} + +u64 arch_timer_get_ptval_ool(void) +{ + return arch_timer_reread(__arch_timer_get_ptval); +} + +#endif /* ARM64 */ + static __always_inline void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val, struct clock_event_device *clk) @@ -232,6 +274,50 @@ static __always_inline void set_next_event(const int access, unsigned long evt, arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk); } +#ifdef CONFIG_ARM64 +static __always_inline void rewrite_tval(const int access, + unsigned long evt, struct clock_event_device *clk) +{ + u64 cval_old, cval_new; + int timeout = 200; + + do { + cval_old = __arch_counter_get_cntvct(); + arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk); + cval_new = __arch_counter_get_cntvct(); + timeout--; + } while (cval_old != cval_new && timeout); + + WARN_ON_ONCE(!timeout); +} + +static __always_inline void set_next_event_errata(const int access, + unsigned long evt, struct clock_event_device *clk) +{ + unsigned long ctrl; + + ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk); + ctrl |= ARCH_TIMER_CTRL_ENABLE; + ctrl &= ~ARCH_TIMER_CTRL_IT_MASK; + rewrite_tval(access, evt, clk); + arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk); +} + +static int arch_timer_set_next_event_virt_errata(unsigned long evt, + struct clock_event_device *clk) +{ + set_next_event_errata(ARCH_TIMER_VIRT_ACCESS, evt, clk); + return 0; +} + +static int arch_timer_set_next_event_phys_errata(unsigned long evt, + struct clock_event_device *clk) +{ + set_next_event_errata(ARCH_TIMER_PHYS_ACCESS, evt, clk); + return 0; +} +#endif /* ARM64 */ + static int arch_timer_set_next_event_virt(unsigned long evt, struct clock_event_device *clk) { @@ -277,6 +363,13 @@ static void __arch_timer_setup(unsigned type, clk->set_state_shutdown = arch_timer_shutdown_virt; clk->set_state_oneshot_stopped = arch_timer_shutdown_virt; clk->set_next_event = arch_timer_set_next_event_virt; + +#ifdef CONFIG_ARM64 + if (static_branch_unlikely(&arch_timer_read_ool_enabled)) + clk->set_next_event = + arch_timer_set_next_event_virt_errata; +#endif + break; case PHYS_SECURE_PPI: case PHYS_NONSECURE_PPI: @@ -284,6 +377,13 @@ static void __arch_timer_setup(unsigned type, clk->set_state_shutdown = arch_timer_shutdown_phys; clk->set_state_oneshot_stopped = arch_timer_shutdown_phys; clk->set_next_event = arch_timer_set_next_event_phys; + +#ifdef CONFIG_ARM64 + if (static_branch_unlikely(&arch_timer_read_ool_enabled)) + clk->set_next_event = + arch_timer_set_next_event_phys_errata; +#endif + break; default: BUG(); @@ -485,6 +585,13 @@ static void __init arch_counter_register(unsigned type) arch_timer_read_counter = arch_counter_get_cntvct; else arch_timer_read_counter = arch_counter_get_cntpct; + + /* + * Don't use the vdso fastpath if errata require using + * the out-of-line counter accessor. + */ + if (static_branch_unlikely(&arch_timer_read_ool_enabled)) + clocksource_counter.name = "arch_sys_counter_ool"; } else { arch_timer_read_counter = arch_counter_get_cntvct_mem; @@ -763,6 +870,9 @@ static void __init arch_timer_of_init(struct device_node *np) arch_timer_c3stop = !of_property_read_bool(np, "always-on"); + if (of_property_read_bool(np, "fsl,erratum-a008585")) + static_branch_enable(&arch_timer_read_ool_enabled); + /* * If we cannot rely on firmware initializing the timer registers then * we should use the physical timers instead.