From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 8DBFFD811B2 for ; Fri, 26 Jan 2024 06:02:37 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=ywyeWQCw47niEGhpyzoWqY3tWOcsit4009evzBwhX7M=; c=relaxed/simple; d=groups.io; h=ARC-Seal:ARC-Message-Signature:ARC-Authentication-Results:Received-SPF:From:To:CC:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding:Content-Type; s=20140610; t=1706248956; v=1; b=Fw9p0iVYrteBPmGbqXj2D8btZQ+TII1FJmQCUX+LQLIVANaznIM592rc1ymocdrs97epgafU k0K93oziMYuXfiNY93vkTNGxCMjzmJPMPxa8McbnXEmb3Yg0qtvqgCORewWHFg8iZqrnNU95ulC k1nd5aqLmPnTWm6Skt6WZZwQ= X-Received: by 127.0.0.2 with SMTP id n1KtYY7687511xvfBESUAc7y; Thu, 25 Jan 2024 22:02:36 -0800 X-Received: from NAM12-BN8-obe.outbound.protection.outlook.com (NAM12-BN8-obe.outbound.protection.outlook.com [40.107.237.41]) by mx.groups.io with SMTP id smtpd.web11.9687.1706248955527416672 for ; Thu, 25 Jan 2024 22:02:35 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EgiYXmQmn5Ysz2Q9Jalt19TOP+uZktdUfxc0L69whSIdms9LsjD2f1yo/8wq+OzvLtQOyshOQ5NmCVq2/YJkIPF+31a3QS4N2z+xyoM+ToTVxrKfxYHOIJU0CFlGRfLqtg2EX21JKX1Pk2L4ouVGISkfwqiBMLzq+jxkJrkks+GkZ7kADsf+VjJ5yAMnh8M6XU+XCsQonvZtldLdmoWhKjTF1I0kRGNUm/ieP+CwoRjpV08i+h09F8whbLkfZz/d/s3faBdroKmHXvN/7XNVsL5j/gJ1E3MSscZvkrO7xbegY5AeVe/potxa7uCxHmEb5meCo6DE2u47uPyfEqWZYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=HgZ3ET9m9Tcg8+NEswFmFibNZ3Q71kVP85ubNYCId/A=; b=VvM/pXJ1gPB44j+mR/Mil6mnbPqh9MzewA9bdnFzOE/EA4A0CDspX0C5QLcfHxJU/+qtwtfLGd4pxe99fAQSqoJY3gT1rnYUAFxNLItg1LNU0Al8LC5f04q4x3zkz/9MsioX1J/C771etbLA2L3MRMDWRDinRr4QAvbem+BL94wt6jZJbTebxsZgvENYN2AHXPvuofXmzWIh/ttV6x775B+2FVpayb+fZf+RbM1AEyeFFxEf+20UdPFza0QDI/j4rPf6l22sTc0EM34NeflJhaSqCdCZS2DuUbH7LvEG1lyLqPeHUWgCCvk+pEMpfOEqXPLdmhnYOJQxVRHmM2Pbaw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=edk2.groups.io smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) X-Received: from CH2PR12CA0002.namprd12.prod.outlook.com (2603:10b6:610:57::12) by PH7PR12MB7354.namprd12.prod.outlook.com (2603:10b6:510:20d::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.22; Fri, 26 Jan 2024 06:02:32 +0000 X-Received: from DS2PEPF0000343D.namprd02.prod.outlook.com (2603:10b6:610:57:cafe::b3) by CH2PR12CA0002.outlook.office365.com (2603:10b6:610:57::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7228.27 via Frontend Transport; Fri, 26 Jan 2024 06:02:32 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C X-Received: from SATLEXMB04.amd.com (165.204.84.17) by DS2PEPF0000343D.mail.protection.outlook.com (10.167.18.40) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7228.16 via Frontend Transport; Fri, 26 Jan 2024 06:02:32 +0000 X-Received: from SHA-LX-MINGXZHA.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Fri, 26 Jan 2024 00:02:30 -0600 From: "Zhai, MingXin (Duke) via groups.io" To: CC: Eric Xing , Duke Zhai , Igniculus Fu , Abner Chang Subject: [edk2-devel] [PATCH V2 16/32] AMD/VanGoghBoard: Check in BaseTscTimerLib Date: Fri, 26 Jan 2024 14:00:34 +0800 Message-ID: <20240126060050.1725-17-duke.zhai@amd.com> In-Reply-To: <20240126060050.1725-1-duke.zhai@amd.com> References: <20240126060050.1725-1-duke.zhai@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS2PEPF0000343D:EE_|PH7PR12MB7354:EE_ X-MS-Office365-Filtering-Correlation-Id: 8a41a27b-fc8a-4f19-b9ba-08dc1e3462db X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Message-Info: h6cmlMpzQWTpkKxNMCQbhf12uqK1YGkV0fmeP0KAe1BZbTDtc9GL8Kw+Tw9VeLkcX6ATXeb79HdzN8mslmxg5vFS9eI2sYuDIQvR8dXQHO2MrsI5cUAwRTz87E7Fa2UEuytJMp2671jZyiWWdYDiG4cNGKSKiiDp13oNMyuWYJgoCXOmG6vX3J2bNWPAM93p0/tPVEuW5czZyPC7+ZBUqc9sI1oKXJXKriKZIXlb4/Wp9ku4SKK/BXXYjdVCbh0J4pjR8IUyj9rTT+0w92RPVyhyTEWn7Y+Q1weP5CpusRfzOUfuYoRDfLNr1q+Xosdw/egmMz0GoAtzuNoqpmF8zOHYKH7hz3aO7z1iAgORh/71KQEbaITxAN4+R/6ZsgJ+j20P642vApQXugTXlfbQqmhaXJkF7vacZLDxRJKkcuBIh0770Ly1iIDlIT+8o4uiDxqyRM3SdEF+57rQ7z9/z1ihAMQGxnRTyOCaeyJQI1UxkL3iSJgq7YFnsmmbwkQu9Wc7Sej0NGBDDVXlal9lcmBxvafIcuAjChLtUygWKtXnEPCnXkcY3VhYPeKSpv2o2kU1Ben1Z9nwWXsjb4kd7sKjMHooOh+KF5j+5l80U4r4UA/kR0Q95U1R8JLnUsYsr4kJnxOBKjqt/58Mbii13WjYZcu5dSeO5n6VGqhd+WWT4SyXKpqWp7Unozb+3HY3BM2KPSNFYJx4GpiCXX84BG0KTQRzMNTh1tQ9e83YjiEPOt5wMzLlo7+cYUXgG6GPdVLAwhmYfPwG3IvVSpujWQGC2kVaJ4d2JAikNjKOgpWTa5iQnwFPMIJmEURIPErO X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jan 2024 06:02:32.4686 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8a41a27b-fc8a-4f19-b9ba-08dc1e3462db X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS2PEPF0000343D.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB7354 Precedence: Bulk 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,duke.zhai@amd.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: k7ZKhCPTqlsAhTL76q28kSFmx7686176AA= Content-Transfer-Encoding: quoted-printable Content-Type: text/plain X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=Fw9p0iVY; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io; dmarc=none; arc=reject ("signature check failed: fail, {[1] = sig:microsoft.com:reject}") From: Duke Zhai BZ #:4640 In V2: Improve coding style. 1.Remove the leading underscore and use double underscore at trailing in = C header files. 2.Remove old tianocore licenses and redundant license description. 3.Improve coding style. For example: remove space between @param. In V1: Provides basic TSC timer calibration based on the ACPI timer hardware. The performance counter features are provided by the processors time stam= p counter. Signed-off-by: Ken Yao Cc: Eric Xing Cc: Duke Zhai Cc: Igniculus Fu Cc: Abner Chang --- .../Library/TscTimerLib/BaseTscTimerLib.c | 23 ++ .../Library/TscTimerLib/BaseTscTimerLib.inf | 43 +++ .../Library/TscTimerLib/DxeTscTimerLib.c | 80 ++++++ .../Library/TscTimerLib/DxeTscTimerLib.inf | 62 +++++ .../Library/TscTimerLib/PeiTscTimerLib.c | 53 ++++ .../Library/TscTimerLib/PeiTscTimerLib.inf | 56 ++++ .../Library/TscTimerLib/TscTimerLibInternal.h | 53 ++++ .../Library/TscTimerLib/TscTimerLibShare.c | 255 ++++++++++++++++++ 8 files changed, 625 insertions(+) create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscT= imerLib/BaseTscTimerLib.c create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscT= imerLib/BaseTscTimerLib.inf create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscT= imerLib/DxeTscTimerLib.c create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscT= imerLib/DxeTscTimerLib.inf create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscT= imerLib/PeiTscTimerLib.c create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscT= imerLib/PeiTscTimerLib.inf create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscT= imerLib/TscTimerLibInternal.h create mode 100644 Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscT= imerLib/TscTimerLibShare.c diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib= /BaseTscTimerLib.c b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/Tsc= TimerLib/BaseTscTimerLib.c new file mode 100644 index 0000000000..7dfef490e9 --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib/BaseTs= cTimerLib.c @@ -0,0 +1,23 @@ +/** @file + ACPI Timer implements one instance of Timer Library. + + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TscTimerLibInternal.h" + +/** Get TSC frequency. + + @return The number of TSC counts per second. + +**/ +UINT64 +InternalGetTscFrequency ( + VOID + ) +{ + return InternalCalculateTscFrequency (); +} diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib= /BaseTscTimerLib.inf b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/T= scTimerLib/BaseTscTimerLib.inf new file mode 100644 index 0000000000..d6c4e2e1d6 --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib/BaseTs= cTimerLib.inf @@ -0,0 +1,43 @@ +## @file +# BaseTscTimerLib +# Provides basic timer support using the ACPI timer hardware. The perfor= mance +# counter features are provided by the processors time stamp counter. +# +# Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+# Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D BaseTscTimerLib + FILE_GUID =3D D29338B9-50FE-4e4f-B7D4-A150A2C1F4FB + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D TimerLib + + +# +# VALID_ARCHITECTURES =3D IA32 X64 +# + +[Sources.common] + TscTimerLibShare.c + BaseTscTimerLib.c + TscTimerLibInternal.h + + +[Packages] + MdePkg/MdePkg.dec + AgesaPublic/AgesaPublic.dec + + +[LibraryClasses] + PcdLib + PciLib + IoLib + BaseLib + +[Pcd.common] + gEfiAmdAgesaPkgTokenSpaceGuid.PcdAmdFchCfgAcpiPmTmrBlkAddr diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib= /DxeTscTimerLib.c b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscT= imerLib/DxeTscTimerLib.c new file mode 100644 index 0000000000..5a374665c3 --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib/DxeTsc= TimerLib.c @@ -0,0 +1,80 @@ +/** @file + ACPI Timer implements one instance of Timer Library. + + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+ Copyright (c) Microsoft Corporation. + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include "TscTimerLibInternal.h" + +UINT64 mTscFrequency; + +/** The constructor function determines the actual TSC frequency. + + First, Get TSC frequency from system configuration table with TSC freque= ncy GUID, + if the table is not found, install it. + This function will always return EFI_SUCCESS. + + @param ImageHandle The firmware allocated handle for the EFI imag= e. + @param SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS. + +**/ +EFI_STATUS +EFIAPI +DxeTscTimerLibConstructor ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + UINT64 *TscFrequency; + + TscFrequency =3D NULL; + // + // Get TSC frequency from system configuration table with TSC frequency = GUID. + // + Status =3D EfiGetSystemConfigurationTable (&gAmdCommonPkgTscFrequencyGui= d, (VOID **)&TscFrequency); + if (Status =3D=3D EFI_SUCCESS) { + ASSERT (TscFrequency !=3D NULL); + mTscFrequency =3D *TscFrequency; + return EFI_SUCCESS; + } + + // + // TSC frequency GUID system configuration table is not found, install i= t. + // + + Status =3D gBS->AllocatePool (EfiBootServicesData, sizeof (UINT64), (VOI= D **)&TscFrequency); + ASSERT_EFI_ERROR (Status); + + *TscFrequency =3D InternalCalculateTscFrequency (); + // + // TscFrequency now points to the number of TSC counts per second, insta= ll system configuration table for it. + // + gBS->InstallConfigurationTable (&gAmdCommonPkgTscFrequencyGuid, TscFrequ= ency); + + mTscFrequency =3D *TscFrequency; + return EFI_SUCCESS; +} + +/** Get TSC frequency. + + @return The number of TSC counts per second. + +**/ +UINT64 +InternalGetTscFrequency ( + VOID + ) +{ + return mTscFrequency; +} diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib= /DxeTscTimerLib.inf b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/Ts= cTimerLib/DxeTscTimerLib.inf new file mode 100644 index 0000000000..c1a5d39ab1 --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib/DxeTsc= TimerLib.inf @@ -0,0 +1,62 @@ +## @file +# DxeTscTimerLib +# +# Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +## @file +# DXE ACPI Timer Library +# +# Provides basic timer support using the ACPI timer hardware. The perfor= mance +# counter features are provided by the processors time stamp counter. +# +# Note: The implementation uses the lower 24-bits of the ACPI timer and +# is compatible with both 24-bit and 32-bit ACPI timers. +# +# Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D DxeTscTimerLib + FILE_GUID =3D 95ab030f-b4fd-4ee4-92a5-9e04e87634d9 + MODULE_TYPE =3D DXE_DRIVER + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D TimerLib|DXE_CORE DXE_DRIVER DXE_RUNT= IME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER SMM_CORE + + CONSTRUCTOR =3D DxeTscTimerLibConstructor + + +# +# VALID_ARCHITECTURES =3D IA32 X64 +# + +[Sources.common] + TscTimerLibShare.c + DxeTscTimerLib.c + TscTimerLibInternal.h + + +[Packages] + MdePkg/MdePkg.dec + VanGoghCommonPkg/AmdCommonPkg.dec + AgesaPublic/AgesaPublic.dec + +[LibraryClasses] + UefiBootServicesTableLib + PcdLib + PciLib + IoLib + BaseLib + UefiLib + DebugLib + +[Guids] + gAmdCommonPkgTscFrequencyGuid ## CONSUMES ## Sy= stem Configuration Table + +[Pcd.common] + gEfiAmdAgesaPkgTokenSpaceGuid.PcdAmdFchCfgAcpiPmTmrBlkAddr diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib= /PeiTscTimerLib.c b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscT= imerLib/PeiTscTimerLib.c new file mode 100644 index 0000000000..338821b5d7 --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib/PeiTsc= TimerLib.c @@ -0,0 +1,53 @@ +/** @file + ACPI Timer implements one instance of Timer Library. + + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include "TscTimerLibInternal.h" + +/** Get TSC frequency from TSC frequency GUID HOB, if the HOB is not foun= d, build it. + + @return The number of TSC counts per second. + +**/ +UINT64 +InternalGetTscFrequency ( + VOID + ) +{ + EFI_HOB_GUID_TYPE *GuidHob; + VOID *DataInHob; + UINT64 TscFrequency; + + // + // Get TSC frequency from TSC frequency GUID HOB. + // + GuidHob =3D GetFirstGuidHob (&gAmdCommonPkgTscFrequencyGuid); + if (GuidHob !=3D NULL) { + DataInHob =3D GET_GUID_HOB_DATA (GuidHob); + TscFrequency =3D *(UINT64 *)DataInHob; + return TscFrequency; + } + + // + // TSC frequency GUID HOB is not found, build it. + // + + TscFrequency =3D InternalCalculateTscFrequency (); + // + // TscFrequency is now equal to the number of TSC counts per second, bui= ld GUID HOB for it. + // + BuildGuidDataHob ( + &gAmdCommonPkgTscFrequencyGuid, + &TscFrequency, + sizeof (UINT64) + ); + + return TscFrequency; +} diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib= /PeiTscTimerLib.inf b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/Ts= cTimerLib/PeiTscTimerLib.inf new file mode 100644 index 0000000000..e1c3bb695c --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib/PeiTsc= TimerLib.inf @@ -0,0 +1,56 @@ +## @file +# PeiTscTimerLib +# +# Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +## @file +# PEI ACPI Timer Library +# +# Provides basic timer support using the ACPI timer hardware. The perfor= mance +# counter features are provided by the processors time stamp counter. +# +# Note: The implementation uses the lower 24-bits of the ACPI timer and +# is compatible with both 24-bit and 32-bit ACPI timers. +# +# Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D PeiTscTimerLib + FILE_GUID =3D 342C36C0-15DF-43b4-9EC9-FBF748BFB3D1 + MODULE_TYPE =3D PEIM + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D TimerLib|PEIM PEI_CORE + +# +# VALID_ARCHITECTURES =3D IA32 X64 +# + +[Sources.common] + TscTimerLibShare.c + PeiTscTimerLib.c + TscTimerLibInternal.h + +[Packages] + MdePkg/MdePkg.dec + AgesaPublic/AgesaPublic.dec + VanGoghCommonPkg/AmdCommonPkg.dec + +[LibraryClasses] + PcdLib + PciLib + IoLib + BaseLib + HobLib + +[Guids] + gAmdCommonPkgTscFrequencyGuid ## PRODUCES ## HO= B + +[Pcd.common] + gEfiAmdAgesaPkgTokenSpaceGuid.PcdAmdFchCfgAcpiPmTmrBlkAddr diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib= /TscTimerLibInternal.h b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library= /TscTimerLib/TscTimerLibInternal.h new file mode 100644 index 0000000000..cd4fc9db14 --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib/TscTim= erLibInternal.h @@ -0,0 +1,53 @@ +/** @file + Header file internal to ACPI TimerLib. + +Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+Copyright (c) Microsoft Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef TSC_TIMER_LIB_INTERNAL_H__ +#define TSC_TIMER_LIB_INTERNAL_H__ + +#include +#include +#include +#include +#include + +#include +#define ACPI_MMIO_BASE 0xFED80000ul +#define PMIO_BASE 0x300 // DWORD +#define FCH_PMIOA_REG64 0x64 // AcpiPmTmrBlk + +/** Get TSC frequency. + + @return The number of TSC counts per second. + +**/ +UINT64 +InternalGetTscFrequency ( + VOID + ); + +/** Calculate TSC frequency. + + The TSC counting frequency is determined by comparing how far it counts + during a 1ms period as determined by the ACPI timer. The ACPI timer is + used because it counts at a known frequency. + If ACPI I/O space not enabled, this function will enable it. Then the + TSC is sampled, followed by waiting for 3579 clocks of the ACPI timer, o= r 1ms. + The TSC is then sampled again. The difference multiplied by 1000 is the = TSC + frequency. There will be a small error because of the overhead of readin= g + the ACPI timer. An attempt is made to determine and compensate for this = error. + + @return The number of TSC counts per second. + +**/ +UINT64 +InternalCalculateTscFrequency ( + VOID + ); + +#endif diff --git a/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib= /TscTimerLibShare.c b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/Ts= cTimerLib/TscTimerLibShare.c new file mode 100644 index 0000000000..a49a2db2b3 --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib/TscTim= erLibShare.c @@ -0,0 +1,255 @@ +/** @file + ACPI Timer implements one instance of Timer Library. + + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "TscTimerLibInternal.h" + +/** Calculate TSC frequency. + + The TSC counting frequency is determined by comparing how far it counts + during a 1ms period as determined by the ACPI timer. The ACPI timer is + used because it counts at a known frequency. + If ACPI I/O space not enabled, this function will enable it. Then the + TSC is sampled, followed by waiting for 3579 clocks of the ACPI timer, o= r 1ms. + The TSC is then sampled again. The difference multiplied by 1000 is the = TSC + frequency. There will be a small error because of the overhead of readin= g + the ACPI timer. An attempt is made to determine and compensate for this = error. + + @return The number of TSC counts per second. + +**/ +UINT64 +InternalCalculateTscFrequency ( + VOID + ) +{ + UINT64 StartTSC; + UINT64 EndTSC; + UINT16 TimerAddr; + UINT32 Ticks; + UINT64 TscFrequency; + + TimerAddr =3D MmioRead16 (ACPI_MMIO_BASE + PMIO_BASE + FCH_PMIOA_REG64); + if (TimerAddr =3D=3D 0) { + TimerAddr =3D PcdGet16 (PcdAmdFchCfgAcpiPmTmrBlkAddr); + MmioWrite16 (ACPI_MMIO_BASE + PMIO_BASE + FCH_PMIOA_REG64, TimerAddr); + } + + // + // ACPI I/O space should be enabled now, locate the ACPI Timer. + // ACPI I/O base address maybe have be initialized by other driver with = different value, + // So get it from PCI space directly. + // + Ticks =3D IoRead32 (TimerAddr) + (3579); // Set Ticks to 1ms in the= future + StartTSC =3D AsmReadTsc (); // Get base value for the = TSC + // + // Wait until the ACPI timer has counted 1ms. + // Timer wrap-arounds are handled correctly by this function. + // When the current ACPI timer value is greater than 'Ticks', the while = loop will exit. + // + while (((Ticks - IoRead32 (TimerAddr)) & BIT23) =3D=3D 0) { + CpuPause (); + } + + EndTSC =3D AsmReadTsc (); // TSC value 1ms later + + TscFrequency =3D MultU64x32 ( + (EndTSC - StartTSC), // Number of TSC counts in 1= ms + 1000 // Number of ms in a second + ); + + return TscFrequency; +} + +/** Stalls the CPU for at least the given number of ticks. + + Stalls the CPU for at least the given number of ticks. It's invoked by + MicroSecondDelay() and NanoSecondDelay(). + + @param[in] Delay A period of time to delay in ticks. + +**/ +VOID +InternalX86Delay ( + IN UINT64 Delay + ) +{ + UINT64 Ticks; + + // + // The target timer count is calculated here + // + Ticks =3D AsmReadTsc () + Delay; + + // + // Wait until time out + // Timer wrap-arounds are NOT handled correctly by this function. + // Thus, this function must be called within 10 years of reset since + // Intel ensures a minimum of 10 years before the TSC wraps. + // + while (AsmReadTsc () <=3D Ticks) { + CpuPause (); + } +} + +/** Stalls the CPU for at least the specified number of MicroSeconds. + + @param[in] MicroSeconds The minimum number of microseconds to delay. + + @return The value of MicroSeconds input. + +**/ +UINTN +EFIAPI +MicroSecondDelay ( + IN UINTN MicroSeconds + ) +{ + InternalX86Delay ( + DivU64x32 ( + MultU64x64 ( + InternalGetTscFrequency (), + MicroSeconds + ), + 1000000u + ) + ); + return MicroSeconds; +} + +/** Stalls the CPU for at least the specified number of NanoSeconds. + + @param[in] NanoSeconds The minimum number of nanoseconds to delay. + + @return The value of NanoSeconds input. + +**/ +UINTN +EFIAPI +NanoSecondDelay ( + IN UINTN NanoSeconds + ) +{ + InternalX86Delay ( + DivU64x32 ( + MultU64x32 ( + InternalGetTscFrequency (), + (UINT32)NanoSeconds + ), + 1000000000u + ) + ); + return NanoSeconds; +} + +/** Retrieves the current value of the 64-bit free running Time-Stamp cou= nter. + + The time-stamp counter (as implemented in the P6 family, Pentium, Pentiu= m M, + Pentium 4, Intel Xeon, Intel Core Solo and Intel Core Duo processors and + later processors) is a 64-bit counter that is set to 0 following a RESET= of + the processor. Following a RESET, the counter increments even when the + processor is halted by the HLT instruction or the external STPCLK# pin. = Note + that the assertion of the external DPSLP# pin may cause the time-stamp + counter to stop. + + The properties of the counter can be retrieved by the + GetPerformanceCounterProperties() function. + + @return The current value of the free running performance counter. + +**/ +UINT64 +EFIAPI +GetPerformanceCounter ( + VOID + ) +{ + return AsmReadTsc (); +} + +/** Retrieves the 64-bit frequency in Hz and the range of performance cou= nter + values. + + If StartValue is not NULL, then the value that the performance counter s= tarts + with, 0x0, is returned in StartValue. If EndValue is not NULL, then the = value + that the performance counter end with, 0xFFFFFFFFFFFFFFFF, is returned i= n + EndValue. + + The 64-bit frequency of the performance counter, in Hz, is always return= ed. + To determine average processor clock frequency, Intel recommends the use= of + EMON logic to count processor core clocks over the period of time for wh= ich + the average is required. + + + @param[out] StartValue Pointer to where the performance counter's sta= rting value is saved, or NULL. + @param[out] EndValue Pointer to where the performance counter's end= ing value is saved, or NULL. + + @return The frequency in Hz. + +**/ +UINT64 +EFIAPI +GetPerformanceCounterProperties ( + OUT UINT64 *StartValue, OPTIONAL + OUT UINT64 *EndValue OPTIONAL + ) +{ + if (StartValue !=3D NULL) { + *StartValue =3D 0; + } + + if (EndValue !=3D NULL) { + *EndValue =3D 0xFFFFFFFFFFFFFFFFull; + } + + return InternalGetTscFrequency (); +} + +/** + Converts elapsed ticks of performance counter to time in nanoseconds. + + This function converts the elapsed ticks of running performance counter = to + time value in unit of nanoseconds. + + @param Ticks The number of elapsed ticks of running performance cou= nter. + + @return The elapsed time in nanoseconds. + +**/ +UINT64 +EFIAPI +GetTimeInNanoSecond ( + IN UINT64 Ticks + ) +{ + UINT64 Frequency; + UINT64 NanoSeconds; + UINT64 Remainder; + INTN Shift; + + Frequency =3D GetPerformanceCounterProperties (NULL, NULL); + + // + // Ticks + // Time =3D --------- x 1,000,000,000 + // Frequency + // + NanoSeconds =3D MultU64x32 (DivU64x64Remainder (Ticks, Frequency, &Remai= nder), 1000000000u); + + // + // Ensure (Remainder * 1,000,000,000) will not overflow 64-bit. + // Since 2^29 < 1,000,000,000 =3D 0x3B9ACA00 < 2^30, Remainder should < = 2^(64-30) =3D 2^34, + // i.e. highest bit set in Remainder should <=3D 33. + // + Shift =3D MAX (0, HighBitSet64 (Remainder) - 33); + Remainder =3D RShiftU64 (Remainder, (UINTN)Shift); + Frequency =3D RShiftU64 (Frequency, (UINTN)Shift); + NanoSeconds +=3D DivU64x64Remainder (MultU64x32 (Remainder, 1000000000u)= , Frequency, NULL); + + return NanoSeconds; +} -- 2.31.1 -=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 (#114509): https://edk2.groups.io/g/devel/message/114509 Mute This Topic: https://groups.io/mt/103971406/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-