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 6EC97D8003E for ; Fri, 19 Jan 2024 14:57:34 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=T+GJDGmFlJp+C3q7vmbDafFD/S8lJUqTnJvqRpNYJE8=; 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=1705676253; v=1; b=kTLPQBi4gSXAU/csOrYctLKtG1+S//+B4X0EODR014Pv0LKlV0cN+znPxe03T+IqlIWdwJbE aasOfaqTTgq/AFfBKph7Y5RNWoaOIkQsWwUbCvn2xc46gSJ8U6Jw4iI8IVySPiIGf5yckLn0+Kn XwbTmKuk3Ykl3uA3hk9+5rAM= X-Received: by 127.0.0.2 with SMTP id Tcm8YY7687511xYNMZlikDQ4; Fri, 19 Jan 2024 06:57:33 -0800 X-Received: from NAM11-CO1-obe.outbound.protection.outlook.com (NAM11-CO1-obe.outbound.protection.outlook.com [40.107.220.52]) by mx.groups.io with SMTP id smtpd.web11.5901.1705560716600998649 for ; Wed, 17 Jan 2024 22:51:56 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UkTW1Arrk+cWgvSRKLA1JpXZQgIom2H87YY4rgOUK9AZ8aoxIMDF+/gj8L3AKnNc1LbQMpXwW8exSoKwsBoU1e3KinxQ+RY0BQ1LptK5G9XzJwDeWUVvDW1FyXzjxzEV2sJ6IiMoFjTXMNSbC27SKT4wrk01f3LAEOBQ3L6ntTLQLmKYI93jm51E8svHPwUzeYf20EPTjJDjAs7YnpZ1vc4ph+3ChgrNDTc//7UvXAs9mS59Thz9j/J4qimU5mdurWNDGneTlqHLhaLA5ddP6GFIbzzTMfQTMMN45Cf1g/GWznnPmpzN2BwWK8+vrggLMLeCzAQIs/2cDKSZeSwW3A== 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=yLubwU+gXAGJTrK7DhH83ADxiebqnhyhG+zfFTqndYU=; b=fMYaWapKz3GZlDvkuU2joM++ClRzp9NsNZ2YzbmUMndd8Ro8DzZ4WcuLshdirIWmaiPUGm15vsgcBQEMhY6yxerF9W4g+1JiDNnAQS4lfdZ9pINyoHSXMndwhqwst7oXkDN2YIa7ja5FyDfpA0NVXvELo+WUEaAZuy0gbvwh07wgJ4972zkcaay1AjjU5vmTgITBsoi8Gyg8DPJ7wexJ6BuIJx3LuBUJiMYjeDjvjB11ZgsInDbWQZ7UNtf8ZbgLTbLB/pTcbD06U4wb4IHvHQoZRYgnC0I7urmzIGQ0306NYf2HGU0XaO0pZYBYA5fiH9vwNPgLh7uKcuXJD2abMg== 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 CY5PR22CA0052.namprd22.prod.outlook.com (2603:10b6:930:1d::25) by PH7PR12MB5618.namprd12.prod.outlook.com (2603:10b6:510:134::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.23; Thu, 18 Jan 2024 06:51:51 +0000 X-Received: from CY4PEPF0000E9D8.namprd05.prod.outlook.com (2603:10b6:930:1d:cafe::41) by CY5PR22CA0052.outlook.office365.com (2603:10b6:930:1d::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7202.24 via Frontend Transport; Thu, 18 Jan 2024 06:51:51 +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 CY4PEPF0000E9D8.mail.protection.outlook.com (10.167.241.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7181.14 via Frontend Transport; Thu, 18 Jan 2024 06:51:51 +0000 X-Received: from SATLEXMB06.amd.com (10.181.40.147) 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; Thu, 18 Jan 2024 00:51:50 -0600 X-Received: from SATLEXMB03.amd.com (10.181.40.144) by SATLEXMB06.amd.com (10.181.40.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.34; Thu, 18 Jan 2024 00:51:50 -0600 X-Received: from SHA-LX-MINGXZHA.amd.com (10.180.168.240) by SATLEXMB03.amd.com (10.181.40.144) with Microsoft SMTP Server id 15.1.2507.34 via Frontend Transport; Thu, 18 Jan 2024 00:51:48 -0600 From: "duke.zhai via groups.io" To: CC: Eric Xing , Duke Zhai , Igniculus Fu , Abner Chang Subject: [edk2-devel] [PATCH 16/33] AMD/VanGoghBoard: Check in BaseTscTimerLib Date: Thu, 18 Jan 2024 14:50:29 +0800 Message-ID: <20240118065046.961-17-duke.zhai@amd.com> In-Reply-To: <20240118065046.961-1-duke.zhai@amd.com> References: <20240118065046.961-1-duke.zhai@amd.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CY4PEPF0000E9D8:EE_|PH7PR12MB5618:EE_ X-MS-Office365-Filtering-Correlation-Id: 893b4e2e-f24e-4ca9-9fd4-08dc17f1f303 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Message-Info: CMVfcV1pvAxwPPha1utA2zCKrBoGf9lmlh9FpSs/7tvlrWrSC2Uwal3mrXW+WaAFr5eeu+Zwafp1SN1Ed+Pzuvp2PXXBTYCd6A6O6n6MjyLZ2zb+mMUb9d18DHs9idtaZAajbjzPPI8h4Qzf2ivSvR4PrfXwNk5LdXfIwZBO4RCabVMwOHmUBMuH6vW/ByjISUjYv4n3dyshowgqs7VFYm9LCA/EpAtvbp6332bA3pcXpukiVyKRbPwSrqlPFePDuy6FORRoaSqoVfiIRp08Eqm/6LQPjv4t24Q19MTB48QNZKfZM8J3I8jPopSg+DXf/C/EsR5IYtJSCzWfZhMjQyxI9zKNxII6HPJ1DcyrLxXMWCIwS5+Ck5epGxPhiTkAoPAV9hmY7h8dRNDYvqcTPH1lchyT+v8+0weS4vi4QeFExifVKsY15Jo/lTtEAtUW5aBW6XEQzP3s69r4Nq1kxBmiPKlWj26edmR53IO2SRthVhl7eBJ1+S20HE5XcJ1bu8ob31FLMWN2hj8CbIHhGPqEYuIReqBxG6svvGFZrlu2RNfMEqwiMrVVS11NU7TRLYjEJZENhEf0ymGA9pC4MR+HiBXoedQ/d5heMkRvpUPwTD6Kv1qkAezMnv+wvDTmP1pVRxwoVu5WwRLUNq7hXOVFucxDJUIDdeuhk2/f+zWAqFyZm2UHWynrSAxWWmKFqgyUxeW7Z/sQQst1fxbnt+PGWkfLRCNuzRtuIqfPC6OgRsH9qxaHgBUdFAaNa9aLqezX03ZmZtf6VY3pV7EEtR1hjRfRC0rMMsxFJfPdR/+Gcg+p2g5fJ2LLmwYaTvMz X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Jan 2024 06:51:51.0332 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 893b4e2e-f24e-4ca9-9fd4-08dc17f1f303 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: CY4PEPF0000E9D8.namprd05.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5618 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: W1yUnbDN43sGHwwoJWMSJqaMx7686176AA= 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=kTLPQBi4; 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 Provides basic TSC timer calibration based on the ACPI timer hardware. The performance counter features are provided by the processors time stamp = counter. Signed-off-by: Ken Yao Cc: Eric Xing Cc: Duke Zhai Cc: Igniculus Fu Cc: Abner Chang --- .../Library/TscTimerLib/BaseTscTimerLib.c | 32 +++ .../Library/TscTimerLib/BaseTscTimerLib.inf | 55 ++++ .../Library/TscTimerLib/DxeTscTimerLib.c | 89 ++++++ .../Library/TscTimerLib/DxeTscTimerLib.inf | 62 ++++ .../Library/TscTimerLib/PeiTscTimerLib.c | 62 ++++ .../Library/TscTimerLib/PeiTscTimerLib.inf | 56 ++++ .../Library/TscTimerLib/TscTimerLibInternal.h | 62 ++++ .../Library/TscTimerLib/TscTimerLibShare.c | 264 ++++++++++++++++++ 8 files changed, 682 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..a4e37da22e --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib/BaseTs= cTimerLib.c @@ -0,0 +1,32 @@ +/** @file + Implements AMD BaseTscTimerLib.c + + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +/* This file includes code originally published under the following licens= e. */ + +/** @file + ACPI Timer implements one instance of Timer Library. + + 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..7dc29cef40 --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib/BaseTs= cTimerLib.inf @@ -0,0 +1,55 @@ +## @file +# BaseTscTimerLib +# +# Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +# This file includes code originally published under the following license= . +## @file +# Base 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 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..e0616565fc --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib/DxeTsc= TimerLib.c @@ -0,0 +1,89 @@ +/** @file + Implements AMD DxeTscTimerLib.c + + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +/* This file includes code originally published under the following licens= e. */ + +/** @file + ACPI Timer implements one instance of Timer Library. + + 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..d2a562197a --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib/PeiTsc= TimerLib.c @@ -0,0 +1,62 @@ +/** @file + Implements AMD PeiTscTimerLib.c + + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +/* This file includes code originally published under the following licens= e. */ + +/** @file + ACPI Timer implements one instance of Timer Library. + + 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..68d0949cbd --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib/TscTim= erLibInternal.h @@ -0,0 +1,62 @@ +/** @file + Implements AMD TscTimerLibInternal.h + + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +/* This file includes code originally published under the following licens= e. */ + +/** @file + Header file internal to ACPI TimerLib. + +Copyright (c) Microsoft Corporation. +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..39eff36abc --- /dev/null +++ b/Platform/AMD/VanGoghBoard/VanGoghCommonPkg/Library/TscTimerLib/TscTim= erLibShare.c @@ -0,0 +1,264 @@ +/** @file + Implements AMD TscTimerLibShare.c + + Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +/* This file includes code originally published under the following licens= e. */ + +/** @file + ACPI Timer implements one instance of Timer Library. + + 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 (#114065): https://edk2.groups.io/g/devel/message/114065 Mute This Topic: https://groups.io/mt/103831177/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-