From patchwork Fri Oct 25 09:16:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Liu X-Patchwork-Id: 11211819 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 A00C7139A for ; Fri, 25 Oct 2019 09:18:40 +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 78CDC2070B for ; Fri, 25 Oct 2019 09:18:40 +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="u1Mryk9C" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 78CDC2070B 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 1iNvii-0007yc-DZ; Fri, 25 Oct 2019 09:17:20 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iNvih-0007yB-OO for xen-devel@lists.xenproject.org; Fri, 25 Oct 2019 09:17:19 +0000 X-Inumbo-ID: 2d12897e-f708-11e9-a531-bc764e2007e4 Received: from mail-wr1-x442.google.com (unknown [2a00:1450:4864:20::442]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 2d12897e-f708-11e9-a531-bc764e2007e4; Fri, 25 Oct 2019 09:16:50 +0000 (UTC) Received: by mail-wr1-x442.google.com with SMTP id z11so1408488wro.11 for ; Fri, 25 Oct 2019 02:16:50 -0700 (PDT) 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=pFFSinDfDG+eojxO6ZAruZ5d2cmA+pbfzhrKVPc1pNc=; b=u1Mryk9C8zbqSKL/8xx1Kqo2EPGmIUWXfVgp3yUIyORHS2bOKn6TtB0nsdaO01IUOm Elk1yBJsEAgbhsQdOZVYMwYvqvyy7TQdCDeq0mWuy16Zz2bZijjFUZYr/DYFs+wqP6ji Vj3uXac7a1MqkXwNzuTVxIqCsYvQSelrYqrwZeo1LcXvF50KtHhYzjBd6Q6FcvZxKxH6 gfOVdkz2WxrmlhEzZd7H5++vCuIz6g62M66C4LFyf/bce5qld4t3y4rWGvB2EopGeF9g timXMRzellO/sBdJGvvC/yA0Y+eGiC/yp2rqOj/Wesm/eBKanakDmx7rS6I4N+cDji/a vl0g== 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=pFFSinDfDG+eojxO6ZAruZ5d2cmA+pbfzhrKVPc1pNc=; b=XjcTsB2jyR0XoS34F/DoeoP05Y7yp/BYSFRwLQv8d/6RkJweDOJtiFfQ0qV8oiGmyM jHZqkJqoOxWguI9afjMk9OkcvOmtufzmgzbOIMLPmlFcKj8nRFaVDUomujQLDsw/3elZ ytoUgGTVfZoNbahqIj1aSwDK4xyBA4hUA/F79lgoh7y4Zhfuq4FIR8M6h9FariuBRsAv Bvd6RccDiNyrMfXiyCurX+o0I9Wl/ovqd0Dlstcdj05Jf0pW4c9SsNYZFDH/q9Wu1sXu RbS5SG3BdT4fS0QUYLvFZciCkv1luVjO7ni3JOLsNyEpCeY16eR3Jc8aqYPrAUhISA0q /png== X-Gm-Message-State: APjAAAWXdsmPQAaIN8bwAxr1GVOT+bSmAcErMGC6iXsBek+Qj/kKbgeF nwzgm0RqjWEVlSes0waf8JxzsLNXyzs= X-Google-Smtp-Source: APXvYqzXA/psT/wMrYqnbfQ2J0JO5Is0SIQaysEdOkAobeYO2cnOUCH7oDIQ1KNuWFgl6A/9vlRUfg== X-Received: by 2002:a5d:6785:: with SMTP id v5mr1950190wru.174.1571995009404; Fri, 25 Oct 2019 02:16:49 -0700 (PDT) Received: from debian.mshome.net (54.163.200.146.dyn.plus.net. [146.200.163.54]) by smtp.gmail.com with ESMTPSA id b62sm1873283wmc.13.2019.10.25.02.16.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Oct 2019 02:16:48 -0700 (PDT) From: Wei Liu X-Google-Original-From: Wei Liu To: Xen Development List Date: Fri, 25 Oct 2019 10:16:18 +0100 Message-Id: <20191025091618.10153-8-liuwe@microsoft.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191025091618.10153-1-liuwe@microsoft.com> References: <20191025091618.10153-1-liuwe@microsoft.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH for-next 7/7] x86: implement Hyper-V clock source 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 , Michael Kelley , Jan Beulich , =?utf-8?q?Roger_Pau_Monn=C3=A9?= Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Implement a clock source using Hyper-V's reference TSC page. Signed-off-by: Wei Liu --- Relevant spec: https://github.com/MicrosoftDocs/Virtualization-Documentation/raw/live/tlfs/Hypervisor%20Top%20Level%20Functional%20Specification%20v5.0C.pdf Section 12.6. --- xen/arch/x86/time.c | 87 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/xen/arch/x86/time.c b/xen/arch/x86/time.c index d8242295ef..f7e93b8a1f 100644 --- a/xen/arch/x86/time.c +++ b/xen/arch/x86/time.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -614,6 +615,89 @@ static struct platform_timesource __initdata plt_xen_timer = }; #endif +#ifdef CONFIG_HYPERV_GUEST +/************************************************************ + * PLATFORM TIMER 6: HYPER-V REFERENCE TSC + */ + +static struct ms_hyperv_tsc_page hyperv_tsc_page __aligned(PAGE_SIZE); + +static int64_t __init init_hyperv_timer(struct platform_timesource *pts) +{ + unsigned long maddr; + uint64_t tsc_msr, freq; + + if ( !hyperv_guest || + !(ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE) ) + return 0; + + maddr = virt_to_maddr(&hyperv_tsc_page); + + /* + * Per Hyper-V TLFS: + * 1. Read existing MSR value + * 2. Preserve bits [11:1] + * 3. Set bits [63:12] to be guest physical address of tsc page + * 4. Set enabled bit (0) + * 5. Write back new MSR value + */ + rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr); + tsc_msr &= GENMASK_ULL(11, 1); + tsc_msr = tsc_msr | (uint64_t)maddr | 1 /* enabled */; + wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr); + + /* Get TSC frequency from Hyper-V */ + rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq); + pts->frequency = freq; + + return freq; +} + +static inline uint64_t read_hyperv_timer(void) +{ + uint64_t scale, offset, ret, tsc; + uint32_t seq; + struct ms_hyperv_tsc_page *tsc_page = &hyperv_tsc_page; + + do { + seq = tsc_page->tsc_sequence; + + /* Seq 0 is special. It means the TSC enlightenment is not + * available at the moment. The reference time can only be + * obtained from the Reference Counter MSR. + */ + if ( seq == 0 ) + { + rdmsrl(HV_X64_MSR_TIME_REF_COUNT, ret); + return ret; + } + + smp_rmb(); + + tsc = rdtsc_ordered(); + scale = tsc_page->tsc_scale; + offset = tsc_page->tsc_offset; + + smp_rmb(); + + } while (tsc_page->tsc_sequence != seq); + + /* x86 has ARCH_SUPPORTS_INT128 */ + ret = (uint64_t)(((__uint128_t)tsc * scale) >> 64) + offset; + + return ret; +} + +static struct platform_timesource __initdata plt_hyperv_timer = +{ + .id = "hyperv", + .name = "HYPER-V REFERENCE TSC", + .read_counter = read_hyperv_timer, + .init = init_hyperv_timer, + .counter_bits = 63, +}; +#endif + /************************************************************ * GENERIC PLATFORM TIMER INFRASTRUCTURE */ @@ -763,6 +847,9 @@ static u64 __init init_platform_timer(void) static struct platform_timesource * __initdata plt_timers[] = { #ifdef CONFIG_XEN_GUEST &plt_xen_timer, +#endif +#ifdef CONFIG_HYPERV_GUEST + &plt_hyperv_timer, #endif &plt_hpet, &plt_pmtimer, &plt_pit };