From nobody Mon Feb 9 09:16:02 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zohomail.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=citrix.com Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1580300003210299.7432306635758; Wed, 29 Jan 2020 04:13:23 -0800 (PST) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iwmDM-0007vV-IF; Wed, 29 Jan 2020 12:13:00 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iwmDL-0007uz-PK for xen-devel@lists.xenproject.org; Wed, 29 Jan 2020 12:12:59 +0000 Received: from esa3.hc3370-68.iphmx.com (unknown [216.71.145.155]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id a5df111e-4290-11ea-ad98-bc764e2007e4; Wed, 29 Jan 2020 12:12:41 +0000 (UTC) X-Inumbo-ID: a5df111e-4290-11ea-ad98-bc764e2007e4 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1580299963; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ko7mPFESyttG6Cuo2jpDQQtOn5TR6/Qfkew50O7TNrQ=; b=Iy4dGhtyoUZfhph+0O+x5qsbupPBaPn/SeETd85s2MaTPXKg3GV5tPRM CD9wxrM9lNH1TRPqpxQl+wVZWsLFlmh9nq7mtVz9ly8pxTeD6Dbrf3ASE MqKV9wGJMjAas0VOvU6kQG4gTGroPkf8REUUymS8EXyrumzyi6tpSVr+X U=; Authentication-Results: esa3.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=anthony.perard@citrix.com; spf=Pass smtp.mailfrom=anthony.perard@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: none (zohomail.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Received-SPF: None (esa3.hc3370-68.iphmx.com: no sender authenticity information available from domain of anthony.perard@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa3.hc3370-68.iphmx.com; envelope-from="anthony.perard@citrix.com"; x-sender="anthony.perard@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa3.hc3370-68.iphmx.com: domain of anthony.perard@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa3.hc3370-68.iphmx.com; envelope-from="anthony.perard@citrix.com"; x-sender="anthony.perard@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ip4:168.245.78.127 ~all" Received-SPF: None (esa3.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa3.hc3370-68.iphmx.com; envelope-from="anthony.perard@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: DZAls7KjEEPgtD69CE24aJpVJ4rWeEnQNyNqRqMNlSKdfNm1Tx4T9anLwckAfhYAdFemmYexW3 PGdASXZsOpxGgSRlHUBoKKqDzXxqTvTdVnPxP1dAMH/zNVryQmgJEDVCPl35XmRFpTKeBG4CWJ h2U/lVBqB2zZm+g1S5XxY1d/LoH4K42d//MZhEGhEQXV8q+OYQs9BYBAsi5eRy1oTRDjdlGikT Klo2TDvp6fUeU/EYbnptMX43WG3cTWSBjw1vLBbF3HWllP4b0/OPQnmJPSvT5Qg0v/bi9WnSlN WoU= X-SBRS: 2.7 X-MesageID: 11606521 X-Ironport-Server: esa3.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.70,377,1574139600"; d="scan'208";a="11606521" From: Anthony PERARD To: Date: Wed, 29 Jan 2020 12:12:34 +0000 Message-ID: <20200129121235.1814563-5-anthony.perard@citrix.com> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200129121235.1814563-1-anthony.perard@citrix.com> References: <20200129121235.1814563-1-anthony.perard@citrix.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 4/5] OvmfPkg/XenPlatformPei: Calibrate APIC timer frequency 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: Julien Grall , Ard Biesheuvel , Jordan Justen , Liming Gao , Anthony Perard , Michael D Kinney , xen-devel@lists.xenproject.org, Laszlo Ersek , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Calculate the frequency of the APIC timer that Xen provides. Even though the frequency is currently hard-coded, it isn't part of the public ABI that Xen provides and thus may change at any time. OVMF needs to determine the frequency by an other mean. Fortunately, Xen provides a way to determines the frequency of the TSC, so we can use TSC to calibrate the frequency of the APIC timer. That information is found in the shared_info page which we map and unmap once done (XenBusDxe is going to map the page somewhere else). The calculated frequency is only logged in this patch, it will be used in a following patch. Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2490 Signed-off-by: Anthony PERARD --- CC: Roger Pau Monn=C3=A9 --- OvmfPkg/XenPlatformPei/XenPlatformPei.inf | 1 + OvmfPkg/XenPlatformPei/Platform.h | 5 + OvmfPkg/XenPlatformPei/Platform.c | 1 + OvmfPkg/XenPlatformPei/Xen.c | 123 ++++++++++++++++++++++ 4 files changed, 130 insertions(+) diff --git a/OvmfPkg/XenPlatformPei/XenPlatformPei.inf b/OvmfPkg/XenPlatfor= mPei/XenPlatformPei.inf index 0ef77db92c03..335a442538c2 100644 --- a/OvmfPkg/XenPlatformPei/XenPlatformPei.inf +++ b/OvmfPkg/XenPlatformPei/XenPlatformPei.inf @@ -52,6 +52,7 @@ [LibraryClasses] DebugLib HobLib IoLib + LocalApicLib PciLib ResourcePublicationLib PeiServicesLib diff --git a/OvmfPkg/XenPlatformPei/Platform.h b/OvmfPkg/XenPlatformPei/Pla= tform.h index 7661f4a8de0a..97e482a065f0 100644 --- a/OvmfPkg/XenPlatformPei/Platform.h +++ b/OvmfPkg/XenPlatformPei/Platform.h @@ -127,6 +127,11 @@ XenGetE820Map ( UINT32 *Count ); =20 +VOID +CalibrateLapicTimer ( + VOID + ); + extern EFI_BOOT_MODE mBootMode; =20 extern UINT8 mPhysMemAddressWidth; diff --git a/OvmfPkg/XenPlatformPei/Platform.c b/OvmfPkg/XenPlatformPei/Pla= tform.c index 717fd0ab1a45..e9511eb40c62 100644 --- a/OvmfPkg/XenPlatformPei/Platform.c +++ b/OvmfPkg/XenPlatformPei/Platform.c @@ -448,6 +448,7 @@ InitializeXenPlatform ( InitializeRamRegions (); =20 InitializeXen (); + CalibrateLapicTimer (); =20 if (mBootMode !=3D BOOT_ON_S3_RESUME) { ReserveEmuVariableNvStore (); diff --git a/OvmfPkg/XenPlatformPei/Xen.c b/OvmfPkg/XenPlatformPei/Xen.c index c41fecdc486e..d6cdc9a8e31c 100644 --- a/OvmfPkg/XenPlatformPei/Xen.c +++ b/OvmfPkg/XenPlatformPei/Xen.c @@ -19,6 +19,7 @@ // #include #include +#include #include #include #include @@ -386,3 +387,125 @@ InitializeXen ( =20 return EFI_SUCCESS; } + + +EFI_STATUS +MapSharedInfoPage ( + IN VOID *PagePtr + ) +{ + xen_add_to_physmap_t Parameters; + INTN ReturnCode; + + Parameters.domid =3D DOMID_SELF; + Parameters.space =3D XENMAPSPACE_shared_info; + Parameters.idx =3D 0; + Parameters.gpfn =3D (UINTN) PagePtr >> EFI_PAGE_SHIFT; + ReturnCode =3D XenHypercallMemoryOp (XENMEM_add_to_physmap, &Parameters); + if (ReturnCode !=3D 0) { + return EFI_NO_MAPPING; + } + return EFI_SUCCESS; +} + +VOID +UnmapXenPage ( + IN VOID *PagePtr + ) +{ + xen_remove_from_physmap_t Parameters; + INTN ReturnCode; + + Parameters.domid =3D DOMID_SELF; + Parameters.gpfn =3D (UINTN) PagePtr >> EFI_PAGE_SHIFT; + ReturnCode =3D XenHypercallMemoryOp (XENMEM_remove_from_physmap, &Parame= ters); + ASSERT (ReturnCode =3D=3D 0); +} + + +STATIC +UINT64 +GetCPUFreq ( + IN XEN_VCPU_TIME_INFO *VcpuTime + ) +{ + UINT32 Version; + UINT32 TSCToSystemMultiplier; + INT8 TSCShift; + UINT64 CPUFreq; + + do { + Version =3D VcpuTime->Version; + MemoryFence (); + TSCToSystemMultiplier =3D VcpuTime->TSCToSystemMultiplier; + TSCShift =3D VcpuTime->TSCShift; + MemoryFence (); + } while (((Version & 1) !=3D 0) && (Version !=3D VcpuTime->Version)); + + CPUFreq =3D (1000000000ULL << 32) / TSCToSystemMultiplier; + if (TSCShift >=3D 0) { + CPUFreq >>=3D VcpuTime->TSCShift; + } else { + CPUFreq <<=3D -VcpuTime->TSCShift; + } + return CPUFreq; +} + +VOID +XenDelay ( + IN XEN_VCPU_TIME_INFO *VcpuTimeInfo, + IN UINT64 DelayNs + ) +{ + UINT64 Tick; + + Tick =3D AsmReadTsc (); + Tick +=3D (DelayNs * GetCPUFreq (VcpuTimeInfo)) / 1000000000ULL; + while (AsmReadTsc() <=3D Tick) { + CpuPause(); + } +} + + +/** + Calculate the frequency of the Local Apic Timer +**/ +VOID +CalibrateLapicTimer ( + VOID + ) +{ + XEN_SHARED_INFO *SharedInfo; + XEN_VCPU_TIME_INFO *VcpuTimeInfo; + UINT32 TimerTick, TimerTick2; + UINT64 TscTick, TscTick2; + UINT64 Freq; + EFI_STATUS Status; + + SharedInfo =3D AllocatePages (1); + Status =3D MapSharedInfoPage (SharedInfo); + ASSERT_EFI_ERROR (Status); + if (EFI_ERROR (Status)) { + goto Exit; + } + + VcpuTimeInfo =3D &SharedInfo->VcpuInfo[0].Time; + + InitializeApicTimer (1, MAX_UINT32, TRUE, 0); + DisableApicTimerInterrupt (); + + TimerTick =3D GetApicTimerCurrentCount (); + TscTick =3D AsmReadTsc (); + XenDelay (VcpuTimeInfo, 1000000ULL); + TimerTick2 =3D GetApicTimerCurrentCount (); + TscTick2 =3D AsmReadTsc (); + + Freq =3D (GetCPUFreq (VcpuTimeInfo) * (TimerTick - TimerTick2)) + / (TscTick2 - TscTick); + DEBUG ((DEBUG_INFO, "APIC Freq % 8lu Hz\n", Freq)); + + UnmapXenPage (SharedInfo); + +Exit: + FreePages (SharedInfo, 1); +} --=20 Anthony PERARD _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel