From patchwork Fri Oct 20 11:49:22 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoffer Dall X-Patchwork-Id: 10019985 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E7068602CB for ; Fri, 20 Oct 2017 11:57:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C9F9928EC0 for ; Fri, 20 Oct 2017 11:57:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BD14428ED0; Fri, 20 Oct 2017 11:57:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.7 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, RCVD_IN_DNSWL_MED, RCVD_IN_SORBS_SPAM autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 38FF028EC0 for ; Fri, 20 Oct 2017 11:57:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=xm/pSi/NhVXDiQLbaBzvVsE5g1jDPKsT0oDNY9QyQWY=; b=JrZ3ezsEwtRSkYfFlAydBl5r6g H0RUfk0thIJYONDkdPUqAUsCqqR2/AI7RjhrE4rw0HFJabR6gDEIyFrNh7YoacnJHwqHULjLiYJ4Y q8dIEPHimE9E4aAsyQ7jknhrwcaczIxqlXWn2HZAilZyILsY96oTlUR7v74Z7bPXoJBgmK0tn/XKO N7MZIcGDZ2OLSWSfFNH7tVRLgUfnJ0N/cxspRRRZwkAdB0SI7xAGSJj2Yj+EZs52uckZgK9D+LO1c PAiX8Qw5TUXgNuXyvtYH63nmOHjKM3VpcagW1OZk5C9wrx3c9OufziLm7Gc9zmyeluJcJFNVKey4q Nen2FCcA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1e5Vvt-00081G-7e; Fri, 20 Oct 2017 11:57:45 +0000 Received: from mail-wm0-x244.google.com ([2a00:1450:400c:c09::244]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1e5VoT-000835-TK for linux-arm-kernel@lists.infradead.org; Fri, 20 Oct 2017 11:50:08 +0000 Received: by mail-wm0-x244.google.com with SMTP id u138so21406428wmu.5 for ; Fri, 20 Oct 2017 04:49:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=8pl206cKBgrs08CursUV1drMbSHrgmdq2bIKi29MUKY=; b=JlXcpNtc5jZrdzsHI+LqBIg2VzT4AitKcUA2Y62DFFpMMGAkuki3lZCinSqVAavn6A tXILEZY0M5uCuvBZaYl32vMm7PU9x+YNg9apyvmg+gXzXqkK1mawb5/LuLwgVow+d294 2X/VYcq+Ps9IM2ayk6ORVk2yaETz32diawrG0= 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; bh=8pl206cKBgrs08CursUV1drMbSHrgmdq2bIKi29MUKY=; b=Y1Uj8IyZNCeCItVn3/tnRKycy8JmU/gkG9EZB//FXj1jLQn9AweOH2/lDfpybHEOHu JUZeJvHkY6/SUp2Sc+79USwGa9XCRnuEgOpM6Cq082U80M44MFhkLS0cinzhh/uTpTbo isyw5GtW6l4tk75OvBEYRxMfXg2MfAFOnDHaGX4yqbMoPpKnwmUQzDRaYVxhubs1O0yz dMOQWsoUPeTGCppwGc2wIsLP+WM0hErJsttOV8IHO9IN9AIpilLsBQHazoBcTP7rR1o6 G+HvjxL9c/nG66Y/1wKH8w64e9f0Ejorj9kwMWwlpalm2n4n+Wv6Gs1Rqnd6/eybSI4u cdkQ== X-Gm-Message-State: AMCzsaUMR/EHqhb/sma7rfNZgP6YV/hqSB/j/AAstDX5QnT30/doLQUh wwAktJN05csS/6bXCpaa/x8d/A== X-Google-Smtp-Source: ABhQp+Q3QhinLWohjxhs51hWtuPOeTHeSyhqQ5qivUQMZ8h/p52KojSXvbq7GTGf0/2nrEZrgf3fXQ== X-Received: by 10.80.181.27 with SMTP id y27mr6028427edd.297.1508500185828; Fri, 20 Oct 2017 04:49:45 -0700 (PDT) Received: from localhost.localdomain (xd93dd96b.cust.hiper.dk. [217.61.217.107]) by smtp.gmail.com with ESMTPSA id f53sm872234ede.63.2017.10.20.04.49.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 20 Oct 2017 04:49:44 -0700 (PDT) From: Christoffer Dall To: kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org Subject: [PATCH v4 03/20] arm64: Use physical counter for in-kernel reads when booted in EL2 Date: Fri, 20 Oct 2017 13:49:22 +0200 Message-Id: <20171020114939.12554-4-christoffer.dall@linaro.org> X-Mailer: git-send-email 2.14.2 In-Reply-To: <20171020114939.12554-1-christoffer.dall@linaro.org> References: <20171020114939.12554-1-christoffer.dall@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171020_045006_125807_548323A4 X-CRM114-Status: GOOD ( 18.99 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Christoffer Dall , kvm@vger.kernel.org, Marc Zyngier , Catalin Marinas , Will Deacon , Shih-Wei Li 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 From: Christoffer Dall Using the physical counter allows KVM to retain the offset between the virtual and physical counter as long as it is actively running a VCPU. As soon as a VCPU is released, another thread is scheduled or we start running userspace applications, we reset the offset to 0, so that userspace accessing the virtual timer can still read the virtual counter and get the same view of time as the kernel. This opens up potential improvements for KVM performance, but we have to make a few adjustments to preserve system consistency. Currently get_cycles() is hardwired to arch_counter_get_cntvct() on arm64, but as we move to using the physical timer for the in-kernel time-keeping on systems that boot in EL2, we should use the same counter for get_cycles() as for other in-kernel timekeeping operations. Similarly, implementations of arch_timer_set_next_event_phys() is modified to use the counter specific to the timer being programmed. VHE kernels or kernels continuing to use the virtual timer are unaffected. Cc: Catalin Marinas Cc: Will Deacon Cc: Mark Rutland Cc: Marc Zyngier Signed-off-by: Christoffer Dall Acked-by: Marc Zyngier --- Notes: Changes since v3: - Consolidated the change of the arch_timer_read_counter function pointer and the change to get_cycles(). A new patch was added prior to this one to support stable reads of the physical counter including errata workaround. - Added !is_hyp_mode_available() clause to ARM64 systems when selecting the in-kernel counter, so that arm64 kernels not booting in EL2 remain unmodified. - Added EXPORT_SYMBOL_GPL(arch_timer_read_counter) to support building modules that reference get_cycles() and thereby arch_timer_read_counter. arch/arm64/include/asm/timex.h | 2 +- drivers/clocksource/arm_arch_timer.c | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/arch/arm64/include/asm/timex.h b/arch/arm64/include/asm/timex.h index 81a076eb37fa..9ad60bae5c8d 100644 --- a/arch/arm64/include/asm/timex.h +++ b/arch/arm64/include/asm/timex.h @@ -22,7 +22,7 @@ * Use the current timer as a cycle counter since this is what we use for * the delay loop. */ -#define get_cycles() arch_counter_get_cntvct() +#define get_cycles() arch_timer_read_counter() #include diff --git a/drivers/clocksource/arm_arch_timer.c b/drivers/clocksource/arm_arch_timer.c index ff8f8a177156..061476e92db7 100644 --- a/drivers/clocksource/arm_arch_timer.c +++ b/drivers/clocksource/arm_arch_timer.c @@ -158,6 +158,7 @@ u32 arch_timer_reg_read(int access, enum arch_timer_reg reg, * if we don't have the cp15 accessors we won't have a problem. */ u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct; +EXPORT_SYMBOL_GPL(arch_timer_read_counter); static u64 arch_counter_read(struct clocksource *cs) { @@ -329,16 +330,19 @@ static void erratum_set_next_event_tval_generic(const int access, unsigned long struct clock_event_device *clk) { unsigned long ctrl; - u64 cval = evt + arch_counter_get_cntvct(); + u64 cval; ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk); ctrl |= ARCH_TIMER_CTRL_ENABLE; ctrl &= ~ARCH_TIMER_CTRL_IT_MASK; - if (access == ARCH_TIMER_PHYS_ACCESS) + if (access == ARCH_TIMER_PHYS_ACCESS) { + cval = evt + arch_counter_get_cntpct(); write_sysreg(cval, cntp_cval_el0); - else + } else { + cval = evt + arch_counter_get_cntvct(); write_sysreg(cval, cntv_cval_el0); + } arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk); } @@ -913,7 +917,7 @@ static void __init arch_counter_register(unsigned type) /* Register the CP15 based counter if we have one */ if (type & ARCH_TIMER_TYPE_CP15) { - if (IS_ENABLED(CONFIG_ARM64) || + if ((IS_ENABLED(CONFIG_ARM64) && !is_hyp_mode_available()) || arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) arch_timer_read_counter = arch_counter_get_cntvct; else