From patchwork Fri Dec 20 19:51:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Liu X-Patchwork-Id: 11306263 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 561BB14E3 for ; Fri, 20 Dec 2019 19:53:13 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 329432072B for ; Fri, 20 Dec 2019 19:53:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="R2e3vhA2" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 329432072B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=xen.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iiOJY-0005Fv-3M; Fri, 20 Dec 2019 19:51:56 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iiOJX-0005Fb-Cv for xen-devel@lists.xenproject.org; Fri, 20 Dec 2019 19:51:55 +0000 X-Inumbo-ID: 24fbc5b2-2362-11ea-88e7-bc764e2007e4 Received: from mail-wm1-x344.google.com (unknown [2a00:1450:4864:20::344]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 24fbc5b2-2362-11ea-88e7-bc764e2007e4; Fri, 20 Dec 2019 19:51:42 +0000 (UTC) Received: by mail-wm1-x344.google.com with SMTP id 20so10285717wmj.4 for ; Fri, 20 Dec 2019 11:51:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=oKZN/wLj0zIyBHSMJP0Pr97AVDWgdWD3HmHzj4XKRDM=; b=R2e3vhA2BVf38bNkDC6p5fSZQ1YgryIIrrdeontJZhXR+oH6o45aWQD/7wVKjePDRs 7oHJ2bX8wJCc9yN1N4CySxL17vFky85X8+aJSOhsMchENgfnXrS4WbBUHOACflse+u3/ pnBRritcowdJD2qZxSe4hocth4vZdpKxAauBxpPl0+9+rpnmtmI3vHlk5rFluqlfHPnA MggJco25FflczTkgWm6Dn9kC/QCw2BHhfRLR2Z5jDAXp6ANcekfQGKZ+tIX9TDA2K4xD WzbOGwOLzbPK0BwNJwqV4fEtLwMREpev1skMhOwn4gf50umQY1IN/TNMleszyM0+qOne vsVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=oKZN/wLj0zIyBHSMJP0Pr97AVDWgdWD3HmHzj4XKRDM=; b=G2+spj4VS1qAJge7WSVnZ/zW0CwxOF0z1EKONz6Avtq5udZzMlv8bT4WCeeowvEehd jdzUgw8XUlho+soGMDItVh+V6fNtlNo500h5oFv+v4v4fTrUMfCl0CuTIVA0t1VYXhZE 3vFZsE180uVVpSN0vNBE7I0LviqyXxdYMVPCv0WdvVHMjrpUBzmRuP4FX6Zrk3iWAnh4 0X27x+tPq8DE+5w9pqSIIWksUmLRVAOrzmMkB3xeogWJkTUTClWkbO6cblIq7LsE6GLY VjcPJUr9aVGxzCCPM3ybegTavOpPkRRj0//vYxNKWLzgHK2Hmb8Rnvi9BOoO9DXXKBkN HEIg== X-Gm-Message-State: APjAAAWbm1MXvJcFJEzhCEy8A8Q+5BnN/8ewZdu3H79QRD+w896wHuz2 KJIGFcYK4+0CHc4CtKo3/mGCOOBc X-Google-Smtp-Source: APXvYqyiPoHMqqFFG4lo8fAXsnK4k2XGj80fQ+TcMt3BZ4QyheBROn3xcy/1YV8NkgvZbfYR3PLbXw== X-Received: by 2002:a05:600c:2046:: with SMTP id p6mr18442661wmg.110.1576871501709; Fri, 20 Dec 2019 11:51:41 -0800 (PST) Received: from debian.mshome.net (38.163.200.146.dyn.plus.net. [146.200.163.38]) by smtp.gmail.com with ESMTPSA id a184sm11006287wmf.29.2019.12.20.11.51.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Dec 2019 11:51:41 -0800 (PST) From: Wei Liu X-Google-Original-From: Wei Liu To: Xen Development List Date: Fri, 20 Dec 2019 19:51:34 +0000 Message-Id: <20191220195135.20130-4-liuwe@microsoft.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191220195135.20130-1-liuwe@microsoft.com> References: <20191220195135.20130-1-liuwe@microsoft.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 3/4] x86: provide and use hv_tsc_scale X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Wei Liu , Wei Liu , Paul Durrant , Andrew Cooper , Paul Durrant , Michael Kelley , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" The Hyper-V clock source and Xen's own viridian code need the same functionality. Move the function in viridian/time.c to hyperv.h and use it in both places. No functional change. Signed-off-by: Wei Liu Reviewed-by: Paul Durrant --- xen/arch/x86/hvm/viridian/time.c | 30 ++-------------------------- xen/arch/x86/time.c | 7 +------ xen/include/asm-x86/guest/hyperv.h | 32 ++++++++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/xen/arch/x86/hvm/viridian/time.c b/xen/arch/x86/hvm/viridian/time.c index 32e79bbcc4..6b2d745f3a 100644 --- a/xen/arch/x86/hvm/viridian/time.c +++ b/xen/arch/x86/hvm/viridian/time.c @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -82,33 +83,6 @@ static void update_reference_tsc(const struct domain *d, bool initialize) p->tsc_sequence = seq; } -/* - * The specification says: "The partition reference time is computed - * by the following formula: - * - * ReferenceTime = ((VirtualTsc * TscScale) >> 64) + TscOffset - * - * The multiplication is a 64 bit multiplication, which results in a - * 128 bit number which is then shifted 64 times to the right to obtain - * the high 64 bits." - */ -static uint64_t scale_tsc(uint64_t tsc, uint64_t scale, int64_t offset) -{ - uint64_t result; - - /* - * Quadword MUL takes an implicit operand in RAX, and puts the result - * in RDX:RAX. Because we only want the result of the multiplication - * after shifting right by 64 bits, we therefore only need the content - * of RDX. - */ - asm ( "mulq %[scale]" - : "+a" (tsc), "=d" (result) - : [scale] "rm" (scale) ); - - return result + offset; -} - static uint64_t trc_val(const struct domain *d, int64_t offset) { uint64_t tsc, scale; @@ -116,7 +90,7 @@ static uint64_t trc_val(const struct domain *d, int64_t offset) tsc = hvm_get_guest_tsc(pt_global_vcpu_target(d)); scale = ((10000ul << 32) / d->arch.tsc_khz) << 32; - return scale_tsc(tsc, scale, offset); + return hv_scale_tsc(tsc, scale, offset); } static void time_ref_count_freeze(const struct domain *d) diff --git a/xen/arch/x86/time.c b/xen/arch/x86/time.c index bbcc9b10b8..d21875de9e 100644 --- a/xen/arch/x86/time.c +++ b/xen/arch/x86/time.c @@ -725,12 +725,7 @@ static inline uint64_t read_hyperv_timer(void) } while ( tsc_page->tsc_sequence != seq ); - /* ret = ((tsc * scale) >> 64) + offset; */ - asm ( "mul %[scale]; add %[offset], %[ret]" - : "+a" (tsc), [ret] "=&d" (ret) - : [scale] "rm" (scale), [offset] "rm" (offset) ); - - return ret; + return hv_scale_tsc(tsc, scale, offset); } static struct platform_timesource __initdata plt_hyperv_timer = diff --git a/xen/include/asm-x86/guest/hyperv.h b/xen/include/asm-x86/guest/hyperv.h index cc21b9abfc..c7a7f32bd5 100644 --- a/xen/include/asm-x86/guest/hyperv.h +++ b/xen/include/asm-x86/guest/hyperv.h @@ -19,10 +19,38 @@ #ifndef __X86_GUEST_HYPERV_H__ #define __X86_GUEST_HYPERV_H__ -#ifdef CONFIG_HYPERV_GUEST - #include +/* + * The specification says: "The partition reference time is computed + * by the following formula: + * + * ReferenceTime = ((VirtualTsc * TscScale) >> 64) + TscOffset + * + * The multiplication is a 64 bit multiplication, which results in a + * 128 bit number which is then shifted 64 times to the right to obtain + * the high 64 bits." + */ +static inline uint64_t hv_scale_tsc(uint64_t tsc, uint64_t scale, + int64_t offset) +{ + uint64_t result; + + /* + * Quadword MUL takes an implicit operand in RAX, and puts the result + * in RDX:RAX. Because we only want the result of the multiplication + * after shifting right by 64 bits, we therefore only need the content + * of RDX. + */ + asm ( "mulq %[scale]" + : "+a" (tsc), "=d" (result) + : [scale] "rm" (scale) ); + + return result + offset; +} + +#ifdef CONFIG_HYPERV_GUEST + #include struct ms_hyperv_info {