From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by mx.groups.io with SMTP id smtpd.web12.6600.1662550631377451143 for ; Wed, 07 Sep 2022 04:37:11 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@ventanamicro.com header.s=google header.b=NI3I2mj4; spf=pass (domain: ventanamicro.com, ip: 209.85.210.181, mailfrom: sunilvl@ventanamicro.com) Received: by mail-pf1-f181.google.com with SMTP id 145so14351356pfw.4 for ; Wed, 07 Sep 2022 04:37:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=VsauADjaqzxmmQCST8QUfhf7CW+NNS8kXNLAN3Vp8BU=; b=NI3I2mj4qkL5sJdcBOZsEMjgKSJ+eTTjg1Tlz6k73vWaBZHEtXcSHLo1Qm6IRB+SZu WcJGUfX3RT+Cx9MJ76vSbozMSPo71yCFI6WR66fpR9v3XoCbiv+396XwiEPiWDDT4YfO YDmes62907uuO2O8pAOYDFwFHxKXM+XGkpR+97fmDVlmlh2cuQF6aywyOaluIycLqTK8 QUSVvbzcNtdYdLW3haEJlbBYw3cCEDCc+LV6r7c8qhkhd56aQLfvaPnLdxvdra27teXJ DZx4eECLTeKDnhzykMXh5Kp0fzSFjj1mX+9rHBZipxRhEabL5oIAc4CsLtIrj7wTOsEx altA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=VsauADjaqzxmmQCST8QUfhf7CW+NNS8kXNLAN3Vp8BU=; b=4kWf36GrN5VPmq6WLCx2O8xNiLG2amOKld1ITNQXb1ImRQSzZo+/Xo3oGQzr+keTEh p4XbY0O/arNAW3NR97jUita7o6f5M4WDC4NIx2YbdV1lzVDts8lBoWQVShD8AGxq44qB KBL4sWDI47SNIUcGP4Ysp0WjgQd0gTBsIVPZzxXFcw4OdGgHh2zbhePf5zyU8zt3lszF MHkO3dzXouvkWaV7dHn30tLgWzlCC5C8xkOWiw9l27UWEMWW8FURAFwQgIemEJIiMjv9 0IJi1a7NF6ZRtSQ5zwamm22plDmrKqcfQLmAOW/UwzlZ9OxFHY9l0vSUHQEc97SvZiHi NLJA== X-Gm-Message-State: ACgBeo1cLbHPq/Y95pZ2G9r4VovdgzcJG6Jd5l3zrmAXooOiSJvziATL UGeFYiIjyMAj5zhqwtA2jHrZKlsvZ1uyPqra X-Google-Smtp-Source: AA6agR6OPkU8hNo+quEyHECdIPFUzoBVzZETb/am8O3RZrWVBxoZe7Is/FbEUdimit3+HH1ZwA39/A== X-Received: by 2002:a63:fc45:0:b0:434:1ff6:e546 with SMTP id r5-20020a63fc45000000b004341ff6e546mr3045987pgk.327.1662550630473; Wed, 07 Sep 2022 04:37:10 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([49.206.11.92]) by smtp.gmail.com with ESMTPSA id 8-20020a17090a000800b001fd77933fb3sm10797230pja.17.2022.09.07.04.37.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Sep 2022 04:37:10 -0700 (PDT) From: "Sunil V L" To: devel@edk2.groups.io Cc: Jian J Wang , Liming Gao , Eric Dong , Ray Ni , Rahul Kumar , Debkumar De , Catharine West , Daniel Schaefer , Abner Chang , Leif Lindholm , Andrew Fish , Ard Biesheuvel , Heinrich Schuchardt , Anup Patel , Sunil V L Subject: [RFC PATCH V2 07/19] MdePkg: Add ArchTimerLib library Date: Wed, 7 Sep 2022 17:06:14 +0530 Message-Id: <20220907113626.540065-8-sunilvl@ventanamicro.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220907113626.540065-1-sunilvl@ventanamicro.com> References: <20220907113626.540065-1-sunilvl@ventanamicro.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This library implements the TimerLib.h functionality. This library is similar to CpuTimerLib but needs the library constructor. Signed-off-by: Sunil V L --- MdePkg/Library/ArchTimerLib/ArchTimerLib.inf | 40 +++ MdePkg/Library/ArchTimerLib/RiscV64/CpuTimerLib.c | 299 ++++++++++++++++++++ MdePkg/Library/ArchTimerLib/ArchTimerLib.uni | 14 + 3 files changed, 353 insertions(+) diff --git a/MdePkg/Library/ArchTimerLib/ArchTimerLib.inf b/MdePkg/Library/ArchTimerLib/ArchTimerLib.inf new file mode 100644 index 000000000000..b61ae58d0142 --- /dev/null +++ b/MdePkg/Library/ArchTimerLib/ArchTimerLib.inf @@ -0,0 +1,40 @@ +## @file +# Timer Library Instance which needs a constructor for the architecture. +# +# Copyright (c) 2016 - 2019, Hewlett Packard Enterprise Development LP. All rights reserved.
+# Copyright (c) 2022, Ventana Micro System Inc. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION = 0x0001001b + BASE_NAME = ArchTimerLib + FILE_GUID = D3CF51A9-1CEA-4776-A8AB-CCFD14D7DAAF + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = TimerLib + MODULE_UNI_FILE = ArchTimerLib.uni + CONSTRUCTOR = ArchTimerLibConstructor + +[Sources.RISCV64] + RiscV64/CpuTimerLib.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + UefiCpuPkg/UefiCpuPkg.dec + EmbeddedPkg/EmbeddedPkg.dec + +[LibraryClasses] + BaseLib + DebugLib + FdtLib + HobLib + +[Pcd] + gUefiCpuPkgTokenSpaceGuid.PcdRiscVTimerFrequencyInHz ## CONSUMES + +[Guids] + gFdtHobGuid diff --git a/MdePkg/Library/ArchTimerLib/RiscV64/CpuTimerLib.c b/MdePkg/Library/ArchTimerLib/RiscV64/CpuTimerLib.c new file mode 100644 index 000000000000..a81ac8c37cad --- /dev/null +++ b/MdePkg/Library/ArchTimerLib/RiscV64/CpuTimerLib.c @@ -0,0 +1,299 @@ +/** @file + RISC-V instance of Timer Library. + + Copyright (c) 2016 - 2022, Hewlett Packard Enterprise Development LP. All rights reserved.
+ Copyright (c) 2022, Ventana Micro Systems Inc. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +STATIC UINT32 mTimeBaseFrequency; +STATIC BOOLEAN mTimeBaseFreqInitialized; + +UINT32 +InternalGetTimerFrequency( + VOID + ) +{ + return mTimeBaseFrequency; +} + + +/** + 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 Delay A period of time to delay in ticks. + +**/ +VOID +InternalRiscVTimerDelay ( + IN UINT32 Delay + ) +{ + UINT32 Ticks; + UINT32 Times; + + Times = Delay >> (RISCV_TIMER_COMPARE_BITS - 2); + Delay &= ((1 << (RISCV_TIMER_COMPARE_BITS - 2)) - 1); + do { + // + // The target timer count is calculated here + // + Ticks = csr_read(CSR_TIME) + Delay; + Delay = 1 << (RISCV_TIMER_COMPARE_BITS - 2); + while (((Ticks - csr_read(CSR_TIME)) & (1 << (RISCV_TIMER_COMPARE_BITS - 1))) == 0) { + CpuPause (); + } + } while (Times-- > 0); +} + +/** + Stalls the CPU for at least the given number of microseconds. + + Stalls the CPU for the number of microseconds specified by MicroSeconds. + + @param MicroSeconds The minimum number of microseconds to delay. + + @return MicroSeconds + +**/ +UINTN +EFIAPI +MicroSecondDelay ( + IN UINTN MicroSeconds + ) +{ + InternalRiscVTimerDelay ( + (UINT32)DivU64x32 ( + MultU64x32 ( + MicroSeconds, + InternalGetTimerFrequency() + ), + 1000000u + ) + ); + return MicroSeconds; +} + +/** + Stalls the CPU for at least the given number of nanoseconds. + + Stalls the CPU for the number of nanoseconds specified by NanoSeconds. + + @param NanoSeconds The minimum number of nanoseconds to delay. + + @return NanoSeconds + +**/ +UINTN +EFIAPI +NanoSecondDelay ( + IN UINTN NanoSeconds + ) +{ + InternalRiscVTimerDelay ( + (UINT32)DivU64x32 ( + MultU64x32 ( + NanoSeconds, + InternalGetTimerFrequency() + ), + 1000000000u + ) + ); + return NanoSeconds; +} + +/** + Retrieves the current value of a 64-bit free running performance counter. + + Retrieves the current value of a 64-bit free running performance counter. The + counter can either count up by 1 or count down by 1. If the physical + performance counter counts by a larger increment, then the counter values + must be translated. The properties of the counter can be retrieved from + GetPerformanceCounterProperties(). + + @return The current value of the free running performance counter. + +**/ +UINT64 +EFIAPI +GetPerformanceCounter ( + VOID + ) +{ + return (UINT64)csr_read (CSR_TIME); +} + +/**return + Retrieves the 64-bit frequency in Hz and the range of performance counter + values. + + If StartValue is not NULL, then the value that the performance counter starts + with immediately after is it rolls over is returned in StartValue. If + EndValue is not NULL, then the value that the performance counter end with + immediately before it rolls over is returned in EndValue. The 64-bit + frequency of the performance counter in Hz is always returned. If StartValue + is less than EndValue, then the performance counter counts up. If StartValue + is greater than EndValue, then the performance counter counts down. For + example, a 64-bit free running counter that counts up would have a StartValue + of 0 and an EndValue of 0xFFFFFFFFFFFFFFFF. A 24-bit free running counter + that counts down would have a StartValue of 0xFFFFFF and an EndValue of 0. + + @param StartValue The value the performance counter starts with when it + rolls over. + @param EndValue The value that the performance counter ends with before + it rolls over. + + @return The frequency in Hz. + +**/ +UINT64 +EFIAPI +GetPerformanceCounterProperties ( + OUT UINT64 *StartValue, OPTIONAL + OUT UINT64 *EndValue OPTIONAL + ) +{ + if (StartValue != NULL) { + *StartValue = 0; + } + + if (EndValue != NULL) { + *EndValue = 32 - 1; + } + + return InternalGetTimerFrequency(); +} + +/** + 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 counter. + + @return The elapsed time in nanoseconds. + +**/ +UINT64 +EFIAPI +GetTimeInNanoSecond ( + IN UINT64 Ticks + ) +{ + UINT64 NanoSeconds; + UINT32 Remainder; + + // + // Ticks + // Time = --------- x 1,000,000,000 + // Frequency + // + NanoSeconds = MultU64x32 (DivU64x32Remainder (Ticks, InternalGetTimerFrequency(), &Remainder), 1000000000u); + + // + // Frequency < 0x100000000, so Remainder < 0x100000000, then (Remainder * 1,000,000,000) + // will not overflow 64-bit. + // + NanoSeconds += DivU64x32 (MultU64x32 ((UINT64)Remainder, 1000000000u), InternalGetTimerFrequency()); + + return NanoSeconds; +} + +STATIC +RETURN_STATUS +EFIAPI +FdtInitializeTimerFrequency ( + VOID + ) +{ + VOID *Hob; + VOID *Fdt; + INT32 CpusNode, Len; + const fdt32_t *Prop; + + Hob = GetFirstGuidHob (&gFdtHobGuid); + if ((Hob == NULL) || (GET_GUID_HOB_DATA_SIZE (Hob) != sizeof (UINT64))) { + DEBUG ((DEBUG_ERROR, "%a: No FDT Hob found\n", __FUNCTION__)); + return EFI_NOT_FOUND; + } + + Fdt = (VOID *)(UINTN)*(UINT64 *)GET_GUID_HOB_DATA (Hob); + + if (fdt_check_header (Fdt) != 0) { + DEBUG (( + DEBUG_ERROR, + "%a: No DTB found @ 0x%p\n", + __FUNCTION__, + Fdt + )); + return EFI_NOT_FOUND; + } + + // The "cpus" node resides at the the root of the DT. Fetch it. + CpusNode = fdt_path_offset (Fdt, "/cpus"); + if (CpusNode < 0) { + DEBUG ((DEBUG_ERROR, "%a: Invalid /cpus node\n", __FUNCTION__)); + return EFI_NOT_FOUND; + } + + Prop = fdt_getprop((void *)Fdt, CpusNode, "timebase-frequency", &Len); + if (!Prop) { + DEBUG ((DEBUG_ERROR, "%a: timebase-frequency propertynot found\n", __FUNCTION__)); + return EFI_NOT_FOUND; + } + + mTimeBaseFrequency = fdt32_to_cpu(*Prop); + DEBUG((DEBUG_INFO, "%a: Timer Frequency (DT) is set to 0x%x\n", __FUNCTION__, mTimeBaseFrequency)); + + return EFI_SUCCESS; +} +/** + Initializes the Timer Frequency by reading it from the DTB + +**/ +RETURN_STATUS +EFIAPI +ArchTimerLibConstructor ( + VOID + ) +{ + EFI_STATUS Status; + + /* + * Initialize only once + */ + if (mTimeBaseFreqInitialized) { + return EFI_SUCCESS; + } + + mTimeBaseFreqInitialized = 1; + + Status = FdtInitializeTimerFrequency(); + + if (EFI_ERROR(Status)) { + mTimeBaseFrequency = PcdGet32 (PcdRiscVTimerFrequencyInHz); + DEBUG((DEBUG_INFO, "%a: Timer Frequency (PCD) is set to 0x%x\n", __FUNCTION__, mTimeBaseFrequency)); + } + + return EFI_SUCCESS; +} diff --git a/MdePkg/Library/ArchTimerLib/ArchTimerLib.uni b/MdePkg/Library/ArchTimerLib/ArchTimerLib.uni new file mode 100644 index 000000000000..1c900bea42bf --- /dev/null +++ b/MdePkg/Library/ArchTimerLib/ArchTimerLib.uni @@ -0,0 +1,14 @@ +// /** @file +// Base CPU Timer Library +// +// Provides basic timer support using architecture specific methods. +// +// Copyright (c) 2022, Ventana Micro Systems Inc. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_MODULE_ABSTRACT #language en-US "CPU Timer Library" + +#string STR_MODULE_DESCRIPTION #language en-US "Provides basic timer support using architecture specific methods" -- 2.25.1