From nobody Sun Feb 8 14:53:04 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+100765+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+100765+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1678137991; cv=none; d=zohomail.com; s=zohoarc; b=Aa1uEGd3Ft7oaGK4FDGsb+nq+csAnxeSAEJClQoHEGNvCwrm+yhH7NxKjBM6m3ATtIuOVzU1QLKQm/truOclbnrEnllg+bAvvlVDWifDAY0n79e2535qmzEZoPex+l+uyaQhiXtZC01DMtEfCsjBx8S31ECqF47eAohnWDRS0dk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678137991; h=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=4lrZmj7EOAZXUU3MpGJwlZjA6WTxbeD35MyajY5dXwY=; b=MChISjU8TlN0T+J6bEkVPCaU+raLEoUwTYFQlgsUvpnEgpUH7sjJvxUoAL4NIqgWYsfzvZJ2u1F6JJ+5P99S3RQqx8S31p17SqPKNUWs/zcXaMqrW9pIAxyaLesivZtze+w5TXaVepQXjmFg+B7aSXQQmAGnVFtsNJGhOQzzT9c= 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+100765+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 167813799191085.12307412359053; Mon, 6 Mar 2023 13:26:31 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id UsAqYY1788612xq2iBMMaNJN; Mon, 06 Mar 2023 13:26:31 -0800 X-Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by mx.groups.io with SMTP id smtpd.web10.1898.1678137984252750213 for ; Mon, 06 Mar 2023 13:26:29 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10641"; a="315335591" X-IronPort-AV: E=Sophos;i="5.98,238,1673942400"; d="scan'208";a="315335591" X-Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Mar 2023 13:26:29 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10641"; a="819444273" X-IronPort-AV: E=Sophos;i="5.98,238,1673942400"; d="scan'208";a="819444273" X-Received: from awarkent-mobl1.amr.corp.intel.com ([10.212.90.17]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Mar 2023 13:26:29 -0800 From: "Andrei Warkentin" To: devel@edk2.groups.io Cc: Andrei Warkentin , Daniel Schaefer , Sunil V L Subject: [edk2-devel] [edk2 6/7] [PATCH v2] UefiCpuPkg: CpuTimerDxeRiscV64: fix tick duration accounting Date: Mon, 6 Mar 2023 15:26:14 -0600 Message-Id: <20230306212615.7400-7-andrei.warkentin@intel.com> In-Reply-To: <20230306212615.7400-1-andrei.warkentin@intel.com> References: <20230306212615.7400-1-andrei.warkentin@intel.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,andrei.warkentin@intel.com X-Gm-Message-State: d1YUgT0Xx8LZu1SxjJbeMy4xx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1678137991; bh=9h+FSTN9gVuoM1HjtFvdR1fzUQoW222QLhSAQIKoJaA=; h=Cc:Date:From:Reply-To:Subject:To; b=ixQAtYTVZT8UsUgtE0La6nYFFicDad4aTVlpECLHfA47UcPREyMHCtNpRIldu0R0hN0 Di1ElTF4UVHGjHrSjZts3dbolcMKvsP//QhqS+PCjjyVDa7rhFGmziPEKJjwBF8vcVBEK 4t9/jX9KuKin3xDsKSvw3pCV4OjEW4zCH0o= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1678137993557100024 Content-Type: text/plain; charset="utf-8" The TimerDxe implementation doesn't account for the physical time passed due to timer handler execution or (perhaps even more importantly) time spent with interrupts masked. Other implementations (e.g. like the Arm one) do. If the timer tick is always incremented at a fixed rate, then you can slow down UEFI's perception of time by running long sections of code in a critical section. Cc: Daniel Schaefer Reviewed-by: Sunil V L Signed-off-by: Andrei Warkentin --- UefiCpuPkg/CpuTimerDxeRiscV64/Timer.c | 33 +++++++++++--------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/UefiCpuPkg/CpuTimerDxeRiscV64/Timer.c b/UefiCpuPkg/CpuTimerDxe= RiscV64/Timer.c index db153f715e60..50f57a9780f0 100644 --- a/UefiCpuPkg/CpuTimerDxeRiscV64/Timer.c +++ b/UefiCpuPkg/CpuTimerDxeRiscV64/Timer.c @@ -40,7 +40,8 @@ STATIC EFI_TIMER_NOTIFY mTimerNotifyFunction; // // The current period of the timer interrupt // -STATIC UINT64 mTimerPeriod =3D 0; +STATIC UINT64 mTimerPeriod =3D 0; +STATIC UINT64 mLastPeriodStart =3D 0; =20 /** Timer Interrupt Handler. @@ -56,25 +57,31 @@ TimerInterruptHandler ( ) { EFI_TPL OriginalTPL; - UINT64 RiscvTimer; + UINT64 PeriodStart =3D RiscVReadTimer (); =20 OriginalTPL =3D gBS->RaiseTPL (TPL_HIGH_LEVEL); if (mTimerNotifyFunction !=3D NULL) { - mTimerNotifyFunction (mTimerPeriod); + // + // For any number of reasons, the ticks could be coming + // in slower than mTimerPeriod. For example, some code + // is doing a *lot* of stuff inside an EFI_TPL_HIGH + // critical section, and this should not cause the EFI + // time to increment slower. So when we take an interrupt, + // account for the actual time passed. + // + mTimerNotifyFunction (PeriodStart - mLastPeriodStart); } =20 - RiscVDisableTimerInterrupt (); // Disable SMode timer int - RiscVClearPendingTimerInterrupt (); if (mTimerPeriod =3D=3D 0) { + RiscVDisableTimerInterrupt (); gBS->RestoreTPL (OriginalTPL); - RiscVDisableTimerInterrupt (); // Disable SMode timer int return; } =20 - RiscvTimer =3D RiscVReadTimer (); - SbiSetTimer (RiscvTimer +=3D mTimerPeriod); + mLastPeriodStart =3D PeriodStart; + SbiSetTimer (PeriodStart +=3D mTimerPeriod); + RiscVEnableTimerInterrupt (); // enable SMode timer int gBS->RestoreTPL (OriginalTPL); - RiscVEnableTimerInterrupt (); // enable SMode timer int } =20 /** @@ -154,8 +161,6 @@ TimerDriverSetTimerPeriod ( IN UINT64 TimerPeriod ) { - UINT64 RiscvTimer; - DEBUG ((DEBUG_INFO, "TimerDriverSetTimerPeriod(0x%lx)\n", TimerPeriod)); =20 if (TimerPeriod =3D=3D 0) { @@ -164,9 +169,9 @@ TimerDriverSetTimerPeriod ( return EFI_SUCCESS; } =20 - mTimerPeriod =3D TimerPeriod / 10; // convert unit from 100ns to 1us - RiscvTimer =3D RiscVReadTimer (); - SbiSetTimer (RiscvTimer + mTimerPeriod); + mTimerPeriod =3D TimerPeriod / 10; // convert unit from 100ns to 1us + mLastPeriodStart =3D RiscVReadTimer (); + SbiSetTimer (mLastPeriodStart + mTimerPeriod); =20 mCpu->EnableInterrupt (mCpu); RiscVEnableTimerInterrupt (); // enable SMode timer int --=20 2.25.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 (#100765): https://edk2.groups.io/g/devel/message/100765 Mute This Topic: https://groups.io/mt/97436053/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-