Skip to content

Commit

Permalink
UefiCpuPkg: RiscV64: initialize FPU
Browse files Browse the repository at this point in the history
The OpenSSL library uses floating point registers.
The is no guarantee that a prior firmware stage has enabled the FPU.

* Enable the FPU and set it to state 'dirty'.
* Clear the fcsr CSR.

Signed-off-by: Heinrich Schuchardt <[email protected]>
  • Loading branch information
xypron committed Sep 17, 2024
1 parent 6c4f771 commit 03ee98e
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions OvmfPkg/RiscVVirt/RiscVVirt.dsc.inc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
# RISC-V Architectural Libraries
CpuExceptionHandlerLib|UefiCpuPkg/Library/BaseRiscV64CpuExceptionHandlerLib/BaseRiscV64CpuExceptionHandlerLib.inf
RiscVSbiLib|MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf
RiscVFpuLib|UefiCpuPkg/Library/BaseRiscVFpuLib/BaseRiscVFpuLib.inf
RiscVMmuLib|UefiCpuPkg/Library/BaseRiscVMmuLib/BaseRiscVMmuLib.inf
PlatformBootManagerLib|OvmfPkg/RiscVVirt/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf
ResetSystemLib|OvmfPkg/RiscVVirt/Library/ResetSystemLib/BaseResetSystemLib.inf
Expand Down
6 changes: 6 additions & 0 deletions UefiCpuPkg/CpuDxeRiscV64/CpuDxe.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,12 @@ InitializeCpu (
Status = RiscVConfigureMmu ();
ASSERT_EFI_ERROR (Status);

//
// Initialize FPU
//
Status = RiscVInitializeFpu ();
ASSERT_EFI_ERROR (Status);

//
// Install Boot protocol
//
Expand Down
1 change: 1 addition & 0 deletions UefiCpuPkg/CpuDxeRiscV64/CpuDxe.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <Protocol/Cpu.h>
#include <Protocol/RiscVBootProtocol.h>
#include <Library/BaseRiscVFpuLib.h>
#include <Library/BaseRiscVSbiLib.h>
#include <Library/BaseRiscVMmuLib.h>
#include <Library/BaseLib.h>
Expand Down
1 change: 1 addition & 0 deletions UefiCpuPkg/CpuDxeRiscV64/CpuDxeRiscV64.inf
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
PeCoffGetEntryPointLib
RiscVSbiLib
RiscVMmuLib
RiscVFpuLib
CacheMaintenanceLib

[Sources]
Expand Down
21 changes: 21 additions & 0 deletions UefiCpuPkg/Include/Library/BaseRiscVFpuLib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/** @file
Copyright (c) 2024, Canonical Services Ltd<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#ifndef BASE_RISCV_FPU_LIB_H_
#define BASE_RISCV_FPU_LIB_H_

/**
Initialize floating point unit
**/
EFI_STATUS
EFIAPI
RiscVInitializeFpu (
VOID
);

#endif /* BASE_RISCV_FPU_LIB_H_ */
26 changes: 26 additions & 0 deletions UefiCpuPkg/Library/BaseRiscVFpuLib/BaseRiscVFpuLib.inf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## @file
# RISC-V FPU library.
#
# Copyright (c) 2024, Canonical Services Ltd
#
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
##

[Defines]
INF_VERSION = 0x0001001b
BASE_NAME = BaseRiscVFpuLib
FILE_GUID = e600fe4d-8595-40f3-90a0-5f043ce155c2
MODULE_TYPE = BASE
VERSION_STRING = 1.0
LIBRARY_CLASS = RiscVFpuLib

[Sources]
RiscVFpuCore.S

[Packages]
MdePkg/MdePkg.dec
UefiCpuPkg/UefiCpuPkg.dec

[LibraryClasses]
BaseLib
22 changes: 22 additions & 0 deletions UefiCpuPkg/Library/BaseRiscVFpuLib/RiscVFpuCore.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/** @file
*
* Copyright (c) 2024, Canonical Services Ltd
*
* SPDX-License-Identifier: BSD-2-Clause-Patent
*
**/

#include <Library/BaseRiscVFpuLib.h>
#include <Register/RiscV64/RiscVImpl.h>

//
// Initialize floating point unit
//
ASM_FUNC (RiscVInitializeFpu)
csrr a0, CSR_SSTATUS
li a1, MSTATUS_FS
or a0, a0, a1
csrw CSR_SSTATUS, a0
csrw CSR_FCSR, x0
li a0, 0
ret
2 changes: 2 additions & 0 deletions UefiCpuPkg/UefiCpuPkg.dec
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
SmmRelocationLib|Include/Library/SmmRelocationLib.h

[LibraryClasses.RISCV64]
## @libraryclass Provides function to initialize the FPU.
RiscVFpuLib|Include/Library/BaseRiscVFpuLib.h
## @libraryclass Provides functions to manage MMU features on RISCV64 CPUs.
##
RiscVMmuLib|Include/Library/BaseRiscVMmuLib.h
Expand Down
1 change: 1 addition & 0 deletions UefiCpuPkg/UefiCpuPkg.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
[Components.RISCV64]
UefiCpuPkg/Library/BaseRiscV64CpuExceptionHandlerLib/BaseRiscV64CpuExceptionHandlerLib.inf
UefiCpuPkg/Library/BaseRiscV64CpuTimerLib/BaseRiscV64CpuTimerLib.inf
UefiCpuPkg/Library/BaseRiscVFpuLib/BaseRiscVFpuLib.inf
UefiCpuPkg/Library/BaseRiscVMmuLib/BaseRiscVMmuLib.inf
UefiCpuPkg/CpuTimerDxeRiscV64/CpuTimerDxeRiscV64.inf
UefiCpuPkg/CpuDxeRiscV64/CpuDxeRiscV64.inf
Expand Down

0 comments on commit 03ee98e

Please sign in to comment.