From nobody Mon Feb 9 08:29:10 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+73959+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+73959+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1618234235; cv=none; d=zohomail.com; s=zohoarc; b=OFahulu3op0Hgm5U/x5DZYffN0wdzQMAQOJWH0xCb/AkJ9q/+dCGIg+6Thmw5+c7Kcc77yUPVX/7ss9V2RBOpokXTS4P73SjJPHR6jHU9uSy2QuOCY09DTx7x1s6gCeX6CGbeQHf386e50W+pc5Yj8hdX/B4IwtuWdeWYvxuCW8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618234235; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=anrZzQRAS2KHEKixp88TxmhefLdaQtBHXqFIrK20QzY=; b=N+3cXeujPZ0j51ClnNm5gEov2CYhFt7/hs18bbx9lkt5yDISMzrw597Qf9QwP7JqNSJAskRVWzdayeDXG8M7eM/kdwoNTIkM2fUAjKKt6nfYSlFb0zNR6hVkq7b9gYZWAN26COX5HndNb0aJ4lE25mxxFBpdtfjQxJ1NsMwRRWo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+73959+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1618234235331509.38759203810275; Mon, 12 Apr 2021 06:30:35 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id vssjYY1788612x7XkuwntEaZ; Mon, 12 Apr 2021 06:30:33 -0700 X-Received: from esa3.hc3370-68.iphmx.com (esa3.hc3370-68.iphmx.com []) by mx.groups.io with SMTP id smtpd.web11.32337.1618234230537835132 for ; Mon, 12 Apr 2021 06:30:31 -0700 IronPort-SDR: cBmtvDv21tXItbUsYqKtim7utUFFFwb5H9VT4Z5BcR2sMNrSA16OQ/BWAWCqrDN+fDjGNqkknV O77ifgn/z5AwO8Si0Q6gAspg2Oyy5oIWXaeO3R8uprQkghj4DY7/crFNhPWqIOFPMIUMlYAx1S uIVNruyt7xmQa4QNeZOMgnQo0MoMmP41qxjN9McKkENtRYN4P7PXZsrxVcwqarBzNkEQFAVBr2 SiVYCKkg2vIYKfoJ/wPAxEUO9nxaGxL7GZcXXF9YKxXKp8leFauLqndfOG42IFJEX5mH/0CKSK YSw= X-SBRS: 4.0 X-MesageID: 41388431 X-Ironport-Server: esa3.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED IronPort-HdrOrdr: A9a23:YviZnKyL9o/2HgrWLQfPKrPwr71zdoIgy1knxilNYDZ+G/b2q+ mFmvMH2RjozAsLUHY7ltyafIWGS3XQ9Zl6iLNhW4uKdg/gpWeuMcVe/ZLvqgeQfBHW2+ZB2c 5bEpRWJ8b3CTFB4PrSwA79KNo4xcnCzabAv5a9815IbSVHL55t9B14DAHzKDwTeCBjCYAiHJ SRouprzgDBRV0tYs62BmYIUoH4zrWh/q7OWhIICwUq7wODl1qTmdvHOiOVwwsEVHd3yaoimF K16jDR3LmpsP2w13bnuVP70pI+orfc4+oGIMSNj8QPQw+c6TqVWA== X-IronPort-AV: E=Sophos;i="5.82,216,1613451600"; d="scan'208";a="41388431" From: "Anthony PERARD via groups.io" To: CC: Laszlo Ersek , Jordan Justen , Ard Biesheuvel , , Anthony PERARD , Julien Grall , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= Subject: [edk2-devel] [PATCH v3 6/7] OvmfPkg/XenPlatformPei: Calibrate APIC timer frequency Date: Mon, 12 Apr 2021 14:30:02 +0100 Message-ID: <20210412133003.146438-7-anthony.perard@citrix.com> In-Reply-To: <20210412133003.146438-1-anthony.perard@citrix.com> References: <20210412133003.146438-1-anthony.perard@citrix.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,anthony.perard@citrix.com X-Gm-Message-State: Dji4bwqDScVaFCWlIuCvrJdYx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1618234233; bh=anrZzQRAS2KHEKixp88TxmhefLdaQtBHXqFIrK20QzY=; h=CC:Content-Type:Date:From:Reply-To:Subject:To; b=w4UX1940edT716WDPG9rXF0cU/Q0sBdWdTQ4LPu9VkolR7Fv2qlkUQvNHUhu65IPa+K KIkQ7DsBloZ1ZTdAZlI8DQ6MQye0xVWMSEMQ14Yst+7///rklj0HB8a+lKXL7MeUi86UL VU1rzxJUhY0fCfh9b+N9mX/Rr/rcFY4czAU= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" 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 shared_info page is mapped at the highest physical address allowed as it doesn't need to be in the RAM, thus there's a call to update the page table. 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 Acked-by: Laszlo Ersek --- Notes: CC: Roger Pau Monn=C3=A9 =20 v3: - fix debug format strings - fix coding style =20 v2: - fix CamelCases - Use U64 multiplication and division helpers - don't read TscShift from the SharedInfo page again - change the location of the shared info page to be outside of the ram - check for overflow in XenDelay - check for overflow when calculating the calculating APIC frequency OvmfPkg/XenPlatformPei/XenPlatformPei.inf | 2 + OvmfPkg/XenPlatformPei/Platform.h | 5 + OvmfPkg/XenPlatformPei/Platform.c | 1 + OvmfPkg/XenPlatformPei/Xen.c | 177 ++++++++++++++++++++++ 4 files changed, 185 insertions(+) diff --git a/OvmfPkg/XenPlatformPei/XenPlatformPei.inf b/OvmfPkg/XenPlatfor= mPei/XenPlatformPei.inf index 8790d907d3ec..5732d2188871 100644 --- a/OvmfPkg/XenPlatformPei/XenPlatformPei.inf +++ b/OvmfPkg/XenPlatformPei/XenPlatformPei.inf @@ -52,6 +52,7 @@ [LibraryClasses] DebugLib HobLib IoLib + LocalApicLib PciLib ResourcePublicationLib PeiServicesLib @@ -59,6 +60,7 @@ [LibraryClasses] MtrrLib MemEncryptSevLib PcdLib + SafeIntLib XenHypercallLib =20 [Pcd] diff --git a/OvmfPkg/XenPlatformPei/Platform.h b/OvmfPkg/XenPlatformPei/Pla= tform.h index e70ca58078eb..77d88fc159d7 100644 --- a/OvmfPkg/XenPlatformPei/Platform.h +++ b/OvmfPkg/XenPlatformPei/Platform.h @@ -132,6 +132,11 @@ PhysicalAddressIdentityMapping ( IN EFI_PHYSICAL_ADDRESS AddressToMap ); =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 b2a7d1c21dac..8b06bebd7731 100644 --- a/OvmfPkg/XenPlatformPei/Xen.c +++ b/OvmfPkg/XenPlatformPei/Xen.c @@ -21,8 +21,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -457,3 +459,178 @@ PhysicalAddressIdentityMapping ( =20 return EFI_SUCCESS; } + +STATIC +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; +} + +STATIC +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 DivU64x32 (LShiftU64 (1000000000ULL, 32), TscToSystemMultipl= ier); + if (TscShift >=3D 0) { + CpuFreq =3D RShiftU64 (CpuFreq, TscShift); + } else { + CpuFreq =3D LShiftU64 (CpuFreq, -TscShift); + } + return CpuFreq; +} + +STATIC +VOID +XenDelay ( + IN XEN_VCPU_TIME_INFO *VcpuTimeInfo, + IN UINT64 DelayNs + ) +{ + UINT64 Tick; + UINT64 CpuFreq; + UINT64 Delay; + UINT64 DelayTick; + UINT64 NewTick; + RETURN_STATUS Status; + + Tick =3D AsmReadTsc (); + + CpuFreq =3D GetCpuFreq (VcpuTimeInfo); + Status =3D SafeUint64Mult (DelayNs, CpuFreq, &Delay); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, + "XenDelay (%lu ns): delay too big in relation to CPU freq %lu Hz\n", + DelayNs, CpuFreq)); + ASSERT_EFI_ERROR (Status); + CpuDeadLoop (); + } + + DelayTick =3D DivU64x32 (Delay, 1000000000); + + NewTick =3D Tick + DelayTick; + + // + // Check for overflow + // + if (NewTick < Tick) { + // + // Overflow, wait for TSC to also overflow + // + while (AsmReadTsc () >=3D Tick) { + CpuPause (); + } + } + + while (AsmReadTsc () <=3D NewTick) { + CpuPause (); + } +} + + +/** + Calculate the frequency of the Local Apic Timer +**/ +VOID +CalibrateLapicTimer ( + VOID + ) +{ + XEN_SHARED_INFO *SharedInfo; + XEN_VCPU_TIME_INFO *VcpuTimeInfo; + UINT32 TimerTick, TimerTick2, DiffTimer; + UINT64 TscTick, TscTick2; + UINT64 Freq; + UINT64 Dividend; + EFI_STATUS Status; + + + SharedInfo =3D (VOID*)((1ULL << mPhysMemAddressWidth) - EFI_PAGE_SIZE); + Status =3D PhysicalAddressIdentityMapping ((EFI_PHYSICAL_ADDRESS)SharedI= nfo); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, + "Failed to add page table entry for Xen shared info page: %r\n", + Status)); + ASSERT_EFI_ERROR (Status); + return; + } + + Status =3D MapSharedInfoPage (SharedInfo); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed to map Xen's shared info page: %r\n", + Status)); + ASSERT_EFI_ERROR (Status); + return; + } + + 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 (); + + + DiffTimer =3D TimerTick - TimerTick2; + Status =3D SafeUint64Mult (GetCpuFreq (VcpuTimeInfo), DiffTimer, &Divide= nd); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "overflow while calculating APIC frequency\n")); + DEBUG ((DEBUG_ERROR, "CPU freq: %lu Hz; APIC timer tick count for 1 ms= : %u\n", + GetCpuFreq (VcpuTimeInfo), DiffTimer)); + ASSERT_EFI_ERROR (Status); + CpuDeadLoop (); + } + + Freq =3D DivU64x64Remainder (Dividend, TscTick2 - TscTick, NULL); + DEBUG ((DEBUG_INFO, "APIC Freq % 8lu Hz\n", Freq)); + + UnmapXenPage (SharedInfo); +} --=20 Anthony PERARD -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#73959): https://edk2.groups.io/g/devel/message/73959 Mute This Topic: https://groups.io/mt/82037840/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-