(This is migrated from edk2-platforms)
Cc: Daniel Schaefer <daniel.schae...@hpe.com>
Cc: Abner Chang <abner.ch...@hpe.com>
Cc: Sunil V L <suni...@ventanamicro.com>
Reviewed-by: Abner Chang <abner.ch...@hpe.com>

Signed-off-by: Daniel Schaefer <daniel.schae...@hpe.com>
---
 .../Library/ResetSystemLib/ResetSystemLib.inf |  33 +++++
 .../Include/Library/RiscVEdk2SbiLib.h         |  38 +++++-
 .../Library/ResetSystemLib/ResetSystemLib.c   | 128 ++++++++++++++++++
 .../Library/RiscVEdk2SbiLib/RiscVEdk2SbiLib.c |  44 +++++-
 4 files changed, 241 insertions(+), 2 deletions(-)
 create mode 100644 
Platform/RISC-V/PlatformPkg/Library/ResetSystemLib/ResetSystemLib.inf
 create mode 100644 
Platform/RISC-V/PlatformPkg/Library/ResetSystemLib/ResetSystemLib.c

diff --git 
a/Platform/RISC-V/PlatformPkg/Library/ResetSystemLib/ResetSystemLib.inf 
b/Platform/RISC-V/PlatformPkg/Library/ResetSystemLib/ResetSystemLib.inf
new file mode 100644
index 0000000000..f876ae2056
--- /dev/null
+++ b/Platform/RISC-V/PlatformPkg/Library/ResetSystemLib/ResetSystemLib.inf
@@ -0,0 +1,33 @@
+## @file
+#  Library instance for ResetSystem library class for RISC-V using SBI ecalls
+#
+#  Copyright (c) 2021, Hewlett Packard Enterprise Development LP. All rights 
reserved.<BR>
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = ResetSystemLib
+  FILE_GUID                      = 3eff6057-1116-4dcb-837e-c0ef1a120ab1
+  MODULE_TYPE                    = BASE
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = ResetSystemLib
+
+#
+# The following information is for reference only and not required by the 
build tools.
+#
+#  VALID_ARCHITECTURES           = RISCV64
+#
+
+[Sources]
+  ResetSystemLib.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  Silicon/RISC-V/ProcessorPkg/RiscVProcessorPkg.dec
+
+[LibraryClasses]
+  DebugLib
+  RiscVEdk2SbiLib
diff --git a/Silicon/RISC-V/ProcessorPkg/Include/Library/RiscVEdk2SbiLib.h 
b/Silicon/RISC-V/ProcessorPkg/Include/Library/RiscVEdk2SbiLib.h
index 66a87cb8c3..88d957f002 100644
--- a/Silicon/RISC-V/ProcessorPkg/Include/Library/RiscVEdk2SbiLib.h
+++ b/Silicon/RISC-V/ProcessorPkg/Include/Library/RiscVEdk2SbiLib.h
@@ -1,7 +1,7 @@
 /** @file
   Library to call the RISC-V SBI ecalls
 
-  Copyright (c) 2020, Hewlett Packard Development LP. All rights reserved.<BR>
+  Copyright (c) 2021, Hewlett Packard Development LP. All rights reserved.<BR>
 
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
@@ -474,6 +474,42 @@ SbiRemoteHfenceVvma (
   IN  UINTN                          Size
   );
 
+///
+/// Firmware System Reset (SRST) Extension
+///
+
+/**
+  Reset the system
+
+  The System Reset Extension provides a function that allow the supervisor
+  software to request system-level reboot or shutdown. The term "system" refers
+  to the world-view of supervisor software and the underlying SBI
+  implementation could be machine mode firmware or hypervisor.
+
+  Valid parameters for ResetType and ResetReason are defined in 
sbi_ecall_interface.h
+
+  #define SBI_SRST_RESET_TYPE_SHUTDOWN    0x0
+  #define SBI_SRST_RESET_TYPE_COLD_REBOOT 0x1
+  #define SBI_SRST_RESET_TYPE_WARM_REBOOT 0x2
+
+  #define SBI_SRST_RESET_REASON_NONE      0x0
+  #define SBI_SRST_RESET_REASON_SYSFAIL   0x1
+
+  When the call is successful, it will not return.
+
+  @param[in]  ResetType            Typ of reset: Shutdown, cold-, or 
warm-reset.
+  @param[in]  ResetReason          Why the system resets. No reason or system 
failure.
+  @retval EFI_INVALID_PARAMETER    Either ResetType or ResetReason is invalid.
+  @retval EFI_UNSUPPORTED          ResetType is valid but not implemented on 
the platform.
+  @retval EFI_DEVICE_ERROR         Unknown error.
+**/
+EFI_STATUS
+EFIAPI
+SbiSystemReset (
+  IN  UINTN                          ResetType,
+  IN  UINTN                          ResetReason
+  );
+
 ///
 /// Vendor Specific extension space: Extension Ids 0x09000000 through 
0x09FFFFFF
 ///
diff --git 
a/Platform/RISC-V/PlatformPkg/Library/ResetSystemLib/ResetSystemLib.c 
b/Platform/RISC-V/PlatformPkg/Library/ResetSystemLib/ResetSystemLib.c
new file mode 100644
index 0000000000..67e40151d1
--- /dev/null
+++ b/Platform/RISC-V/PlatformPkg/Library/ResetSystemLib/ResetSystemLib.c
@@ -0,0 +1,128 @@
+/** @file
+  Reset System Library functions for RISC-V
+
+  Copyright (c) 2021, Hewlett Packard Development LP. All rights reserved.<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Library/DebugLib.h>
+#include <Library/ResetSystemLib.h>
+#include <Library/RiscVEdk2SbiLib.h>
+
+/**
+  This function causes a system-wide reset (cold reset), in which
+  all circuitry within the system returns to its initial state. This type of 
reset
+  is asynchronous to system operation and operates without regard to
+  cycle boundaries.
+
+  If this function returns, it means that the system does not support cold 
reset.
+**/
+VOID
+EFIAPI
+ResetCold (
+  VOID
+  )
+{
+  // Warm Reset via SBI ecall
+  SbiSystemReset (SBI_SRST_RESET_TYPE_COLD_REBOOT, SBI_SRST_RESET_REASON_NONE);
+}
+
+/**
+  This function causes a system-wide initialization (warm reset), in which all 
processors
+  are set to their initial state. Pending cycles are not corrupted.
+
+  If this function returns, it means that the system does not support warm 
reset.
+**/
+VOID
+EFIAPI
+ResetWarm (
+  VOID
+  )
+{
+  // Warm Reset via SBI ecall
+  SbiSystemReset (SBI_SRST_RESET_TYPE_WARM_REBOOT, SBI_SRST_RESET_REASON_NONE);
+}
+
+/**
+  This function causes the system to enter a power state equivalent
+  to the ACPI G2/S5 or G3 states.
+
+  If this function returns, it means that the system does not support shutdown 
reset.
+**/
+VOID
+EFIAPI
+ResetShutdown (
+  VOID
+  )
+{
+  // Shut down via SBI ecall
+  SbiSystemReset (SBI_SRST_RESET_TYPE_SHUTDOWN, SBI_SRST_RESET_REASON_NONE);
+}
+
+/**
+  This function causes a systemwide reset. The exact type of the reset is
+  defined by the EFI_GUID that follows the Null-terminated Unicode string 
passed
+  into ResetData. If the platform does not recognize the EFI_GUID in ResetData
+  the platform must pick a supported reset type to perform. The platform may
+  optionally log the parameters from any non-normal reset that occurs.
+
+  @param[in]  DataSize   The size, in bytes, of ResetData.
+  @param[in]  ResetData  The data buffer starts with a Null-terminated string,
+                         followed by the EFI_GUID.
+**/
+VOID
+EFIAPI
+ResetPlatformSpecific (
+  IN UINTN   DataSize,
+  IN VOID    *ResetData
+  )
+{
+  //
+  // Can map to OpenSBI vendor or platform specific reset type.
+  //
+  return;
+}
+
+/**
+  The ResetSystem function resets the entire platform.
+
+  @param[in] ResetType      The type of reset to perform.
+  @param[in] ResetStatus    The status code for the reset.
+  @param[in] DataSize       The size, in bytes, of ResetData.
+  @param[in] ResetData      For a ResetType of EfiResetCold, EfiResetWarm, or 
EfiResetShutdown
+                            the data buffer starts with a Null-terminated 
string, optionally
+                            followed by additional binary data. The string is 
a description
+                            that the caller may use to further indicate the 
reason for the
+                            system reset.
+**/
+VOID
+EFIAPI
+ResetSystem (
+  IN EFI_RESET_TYPE               ResetType,
+  IN EFI_STATUS                   ResetStatus,
+  IN UINTN                        DataSize,
+  IN VOID                         *ResetData OPTIONAL
+  )
+{
+  switch (ResetType) {
+  case EfiResetWarm:
+    ResetWarm ();
+    break;
+
+  case EfiResetCold:
+    ResetCold ();
+    break;
+
+  case EfiResetShutdown:
+    ResetShutdown ();
+    return;
+
+  case EfiResetPlatformSpecific:
+    ResetPlatformSpecific (DataSize, ResetData);
+    return;
+
+  default:
+    return;
+  }
+}
diff --git 
a/Silicon/RISC-V/ProcessorPkg/Library/RiscVEdk2SbiLib/RiscVEdk2SbiLib.c 
b/Silicon/RISC-V/ProcessorPkg/Library/RiscVEdk2SbiLib/RiscVEdk2SbiLib.c
index 9bbeaaec3f..319526ed8f 100644
--- a/Silicon/RISC-V/ProcessorPkg/Library/RiscVEdk2SbiLib/RiscVEdk2SbiLib.c
+++ b/Silicon/RISC-V/ProcessorPkg/Library/RiscVEdk2SbiLib/RiscVEdk2SbiLib.c
@@ -15,7 +15,7 @@
   - SbiLegacyRemoteSfenceVmaAsid -> Use SbiRemoteSfenceVmaAsid
   - SbiLegacyShutdown            -> Wait for new System Reset extension
 
-  Copyright (c) 2020, Hewlett Packard Development LP. All rights reserved.<BR>
+  Copyright (c) 2021, Hewlett Packard Development LP. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
   @par Revision Reference:
@@ -715,6 +715,48 @@ SbiRemoteHFenceVvma (
   return TranslateError (Ret.Error);
 }
 
+/**
+  Reset the system
+
+  The System Reset Extension provides a function that allow the supervisor
+  software to request system-level reboot or shutdown. The term "system" refers
+  to the world-view of supervisor software and the underlying SBI
+  implementation could be machine mode firmware or hypervisor.
+
+  Valid parameters for ResetType and ResetReason are defined in 
sbi_ecall_interface.h
+
+  #define SBI_SRST_RESET_TYPE_SHUTDOWN    0x0
+  #define SBI_SRST_RESET_TYPE_COLD_REBOOT 0x1
+  #define SBI_SRST_RESET_TYPE_WARM_REBOOT 0x2
+
+  #define SBI_SRST_RESET_REASON_NONE      0x0
+  #define SBI_SRST_RESET_REASON_SYSFAIL   0x1
+
+  When the call is successful, it will not return.
+
+  @param[in]  ResetType            Typ of reset: Shutdown, cold-, or 
warm-reset.
+  @param[in]  ResetReason          Why the system resets. No reason or system 
failure.
+  @retval EFI_INVALID_PARAMETER    Either ResetType or ResetReason is invalid.
+  @retval EFI_UNSUPPORTED          ResetType is valid but not implemented on 
the platform.
+  @retval EFI_DEVICE_ERROR         Unknown error.
+**/
+EFI_STATUS
+EFIAPI
+SbiSystemReset (
+  IN  UINTN                          ResetType,
+  IN  UINTN                          ResetReason
+  )
+{
+  SbiRet Ret = SbiCall (
+                 SBI_EXT_SRST,
+                 SBI_EXT_SRST_RESET,
+                 2,
+                 ResetType,
+                 ResetReason
+                 );
+  return TranslateError (Ret.Error);
+}
+
 //
 // SBI interface function for the vendor extension
 //
-- 
2.31.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#85378): https://edk2.groups.io/g/devel/message/85378
Mute This Topic: https://groups.io/mt/88278553/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-


Reply via email to