Skip to content

Commit

Permalink
UefiCpuPkg/CpuTimerDxeRiscV64: Add support for Sstc
Browse files Browse the repository at this point in the history
Sstc extension allows to program the timer and receive the interrupt
without using an SBI call. This reduces the latency to generate the timer
interrupt. So, detect whether Sstc extension is supported and use the
stimecmp register directly to program the timer interrupt.

Cc: Gerd Hoffmann <[email protected]>
Cc: Rahul Kumar <[email protected]>
Cc: Laszlo Ersek <[email protected]>
Cc: Ray Ni <[email protected]>
Cc: Andrei Warkentin <[email protected]>
Signed-off-by: Sunil V L <[email protected]>
Reviewed-by: Laszlo Ersek <[email protected]>
Reviewed-by: Andrei Warkentin <[email protected]>
Reviewed-by: Dhaval Sharma <[email protected]>
  • Loading branch information
vlsunil authored and mergify[bot] committed Jan 11, 2024
1 parent 8ae17a7 commit f910299
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions UefiCpuPkg/CpuTimerDxeRiscV64/CpuTimerDxeRiscV64.inf
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
Timer.c

[Pcd]
gEfiMdePkgTokenSpaceGuid.PcdRiscVFeatureOverride ## CONSUMES
gUefiCpuPkgTokenSpaceGuid.PcdCpuCoreCrystalClockFrequency ## CONSUMES

[Protocols]
Expand Down
49 changes: 46 additions & 3 deletions UefiCpuPkg/CpuTimerDxeRiscV64/Timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,45 @@ STATIC EFI_TIMER_NOTIFY mTimerNotifyFunction;
STATIC UINT64 mTimerPeriod = 0;
STATIC UINT64 mLastPeriodStart = 0;

//
// Sstc support
//
STATIC BOOLEAN mSstcEnabled = FALSE;

/**
Program the timer.
Program either using stimecmp (when Sstc extension is enabled) or using SBI
TIME call.
@param NextValue Core tick value the timer should expire.
**/
STATIC
VOID
RiscVProgramTimer (
UINT64 NextValue
)
{
if (mSstcEnabled) {
RiscVSetSupervisorTimeCompareRegister (NextValue);
} else {
SbiSetTimer (NextValue);
}
}

/**
Check whether Sstc is enabled in PCD.
**/
STATIC
BOOLEAN
RiscVIsSstcEnabled (
VOID
)
{
return ((PcdGet64 (PcdRiscVFeatureOverride) & RISCV_CPU_FEATURE_SSTC_BITMASK) != 0);
}

/**
Timer Interrupt Handler.
Expand Down Expand Up @@ -94,7 +133,7 @@ TimerInterruptHandler (
),
1000000u
); // convert to tick
SbiSetTimer (PeriodStart);
RiscVProgramTimer (PeriodStart);
RiscVEnableTimerInterrupt (); // enable SMode timer int
gBS->RestoreTPL (OriginalTPL);
}
Expand Down Expand Up @@ -197,8 +236,7 @@ TimerDriverSetTimerPeriod (
),
1000000u
); // convert to tick
SbiSetTimer (PeriodStart);

RiscVProgramTimer (PeriodStart);
mCpu->EnableInterrupt (mCpu);
RiscVEnableTimerInterrupt (); // enable SMode timer int
return EFI_SUCCESS;
Expand Down Expand Up @@ -282,6 +320,11 @@ TimerDriverInitialize (
//
mTimerNotifyFunction = NULL;

if (RiscVIsSstcEnabled ()) {
mSstcEnabled = TRUE;
DEBUG ((DEBUG_INFO, "TimerDriverInitialize: Timer interrupt is via Sstc extension\n"));
}

//
// Make sure the Timer Architectural Protocol is not already installed in the system
//
Expand Down
2 changes: 2 additions & 0 deletions UefiCpuPkg/CpuTimerDxeRiscV64/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
//
#define DEFAULT_TIMER_TICK_DURATION 100000

#define RISCV_CPU_FEATURE_SSTC_BITMASK BIT1

extern VOID
RiscvSetTimerPeriod (
UINT32 TimerPeriod
Expand Down

0 comments on commit f910299

Please sign in to comment.