public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH v5 0/3] RISC-V SBI-backed SerialLib
@ 2023-03-10 17:04 Andrei Warkentin
  2023-03-10 17:04 ` [PATCH v5 1/3] MdePkg: BaseRiscVSbiLib: make more useful to consumers Andrei Warkentin
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Andrei Warkentin @ 2023-03-10 17:04 UTC (permalink / raw)
  To: devel; +Cc: Andrei Warkentin

Hello,

Here are three patches that provide a SerialLib backed by SBI console. 
Both legacy and DBCN mechanisms are supported in various execution environments and have been tested with UART and HTIF consoles.

MdePkg reviewers: please review MdePkg.dsc changes.

This is also available at
https://github.com/andreiw/edk2-rv-wip/tree/patchset-2

Compared to v4:

(not sent out). CC MdePkg maintainers, fix copyright date in SecMain.c.

Comapred to v3

EccCheck fixes. Add MdePkg infs to DSC.

Compared to v2:
- Probes legacy extension as well.
- Encode supported module types in the INF file. This is done using LIBRARY_CLASS,
  as MODULE_TYPE cannot encode multiple types, so MODULE_TYPE is retained as BASE.
- Update INF version and generate brand new GUIDs instead of editing them.
- Checked that all patches retain ^M endings.


Andrei Warkentin (3):
  MdePkg: BaseRiscVSbiLib: make more useful to consumers
  MdePkg: add SBI-based SerialPortLib for RISC-V
  OvmfPkg: RiscVVirt: Add missing SerialPortInitialize to Sec

 MdePkg/MdePkg.dsc                                                              |   2 +
 MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf |  36 +++
 MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf     |  39 +++
 OvmfPkg/RiscVVirt/Sec/SecMain.inf                                              |   1 +
 MdePkg/Include/Library/BaseRiscVSbiLib.h                                       |  40 ++-
 OvmfPkg/RiscVVirt/Sec/SecMain.h                                                |   1 +
 MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.c                               |   3 +-
 MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c   | 288 ++++++++++++++++++++
 MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c       | 233 ++++++++++++++++
 OvmfPkg/RiscVVirt/Sec/SecMain.c                                                |   4 +-
 MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni |  16 ++
 MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni     |  16 ++
 12 files changed, 673 insertions(+), 6 deletions(-)
 create mode 100644 MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf
 create mode 100644 MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf
 create mode 100644 MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c
 create mode 100644 MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c
 create mode 100644 MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni
 create mode 100644 MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni

-- 
2.25.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

* [PATCH v5 1/3] MdePkg: BaseRiscVSbiLib: make more useful to consumers
  2023-03-10 17:04 [PATCH v5 0/3] RISC-V SBI-backed SerialLib Andrei Warkentin
@ 2023-03-10 17:04 ` Andrei Warkentin
  2023-03-10 17:04 ` [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V Andrei Warkentin
  2023-03-10 17:04 ` [PATCH v5 3/3] OvmfPkg: RiscVVirt: Add missing SerialPortInitialize to Sec Andrei Warkentin
  2 siblings, 0 replies; 14+ messages in thread
From: Andrei Warkentin @ 2023-03-10 17:04 UTC (permalink / raw)
  To: devel; +Cc: Andrei Warkentin, Daniel Schaefer, Gerd Hoffmann, Sunil V L

Add a few more definitions and make SbiCall and TranslateError
usable (not static) by library users.

Cc: Daniel Schaefer <git@danielschaefer.me>
Acked-by: Gerd Hoffmann <kraxel@redhat.com>
Reviewed-by: Sunil V L <sunilvl@ventanamicro.com>
Signed-off-by: Andrei Warkentin <andrei.warkentin@intel.com>
---
 MdePkg/Include/Library/BaseRiscVSbiLib.h         | 40 ++++++++++++++++++--
 MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.c |  3 +-
 2 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/MdePkg/Include/Library/BaseRiscVSbiLib.h b/MdePkg/Include/Library/BaseRiscVSbiLib.h
index e75520b4b888..2244165a6b6a 100644
--- a/MdePkg/Include/Library/BaseRiscVSbiLib.h
+++ b/MdePkg/Include/Library/BaseRiscVSbiLib.h
@@ -2,6 +2,7 @@
   Library to call the RISC-V SBI ecalls
 
   Copyright (c) 2021-2022, Hewlett Packard Development LP. All rights reserved.<BR>
+  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
 
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
@@ -18,10 +19,28 @@
 #include <Uefi.h>
 
 /* SBI Extension IDs */
-#define SBI_EXT_TIME  0x54494D45
-#define SBI_EXT_SRST  0x53525354
+#define SBI_EXT_0_1_CONSOLE_PUTCHAR  0x1
+#define SBI_EXT_0_1_CONSOLE_GETCHAR  0x2
+#define SBI_EXT_BASE                 0x10
+#define SBI_EXT_DBCN                 0x4442434E
+#define SBI_EXT_TIME                 0x54494D45
+#define SBI_EXT_SRST                 0x53525354
 
-/* SBI function IDs for TIME extension*/
+/* SBI function IDs for base extension */
+#define SBI_EXT_BASE_SPEC_VERSION   0x0
+#define SBI_EXT_BASE_IMPL_ID        0x1
+#define SBI_EXT_BASE_IMPL_VERSION   0x2
+#define SBI_EXT_BASE_PROBE_EXT      0x3
+#define SBI_EXT_BASE_GET_MVENDORID  0x4
+#define SBI_EXT_BASE_GET_MARCHID    0x5
+#define SBI_EXT_BASE_GET_MIMPID     0x6
+
+/* SBI function IDs for DBCN extension */
+#define SBI_EXT_DBCN_WRITE       0x0
+#define SBI_EXT_DBCN_READ        0x1
+#define SBI_EXT_DBCN_WRITE_BYTE  0x2
+
+/* SBI function IDs for TIME extension */
 #define SBI_EXT_TIME_SET_TIMER  0x0
 
 /* SBI function IDs for SRST extension */
@@ -62,6 +81,21 @@ typedef struct {
   UINTN    Value; ///< Value returned
 } SBI_RET;
 
+SBI_RET
+EFIAPI
+SbiCall (
+  IN  UINTN  ExtId,
+  IN  UINTN  FuncId,
+  IN  UINTN  NumArgs,
+  ...
+  );
+
+EFI_STATUS
+EFIAPI
+TranslateError (
+  IN  UINTN  SbiError
+  );
+
 VOID
 EFIAPI
 SbiSetTimer (
diff --git a/MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.c b/MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.c
index 2ba8f5ed366a..1141abfab9d0 100644
--- a/MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.c
+++ b/MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.c
@@ -4,6 +4,7 @@
   It allows calling an SBI function via an ecall from S-Mode.
 
   Copyright (c) 2021-2022, Hewlett Packard Development LP. All rights reserved.<BR>
+  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
 
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
@@ -31,7 +32,6 @@
   @retval  Returns SBI_RET structure with value and error code.
 
 **/
-STATIC
 SBI_RET
 EFIAPI
 SbiCall (
@@ -88,7 +88,6 @@ SbiCall (
   @param[in] SbiError   SBI error code
   @retval EFI_STATUS
 **/
-STATIC
 EFI_STATUS
 EFIAPI
 TranslateError (
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
  2023-03-10 17:04 [PATCH v5 0/3] RISC-V SBI-backed SerialLib Andrei Warkentin
  2023-03-10 17:04 ` [PATCH v5 1/3] MdePkg: BaseRiscVSbiLib: make more useful to consumers Andrei Warkentin
@ 2023-03-10 17:04 ` Andrei Warkentin
  2023-03-17  4:53   ` Andrei Warkentin
  2023-03-10 17:04 ` [PATCH v5 3/3] OvmfPkg: RiscVVirt: Add missing SerialPortInitialize to Sec Andrei Warkentin
  2 siblings, 1 reply; 14+ messages in thread
From: Andrei Warkentin @ 2023-03-10 17:04 UTC (permalink / raw)
  To: devel
  Cc: Andrei Warkentin, Daniel Schaefer, Sunil V L, Michael D Kinney,
	Liming Gao, Zhiguang Liu, Gerd Hoffmann

These are implementations of SerialPortLib using SBI console services.
- SecPeiRiscVSerialPortLibSbi is appropriate for SEC/PEI (XIP) environments
- PrePiDxeRiscVSerialPortLibSbi is appropriate for PrePI/DXE environments

Tested with:
- Qemu RiscVVirt (non-DBCN case, backed by UART)
- TinyEMU + RiscVVirt (non-DBCN case, HTIF)
- TinyEMU + RiscVVirt (DBCN case, HTIF)

Cc: Daniel Schaefer <git@danielschaefer.me>
Cc: Sunil V L <sunilvl@ventanamicro.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Zhiguang Liu <zhiguang.liu@intel.com>
Acked-by: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Andrei Warkentin <andrei.warkentin@intel.com>
---
 MdePkg/MdePkg.dsc                                                              |   2 +
 MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf |  36 +++
 MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf     |  39 +++
 MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c   | 288 ++++++++++++++++++++
 MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c       | 233 ++++++++++++++++
 MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni |  16 ++
 MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni     |  16 ++
 7 files changed, 630 insertions(+)

diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc
index 0ac7618b4623..fb31349042fb 100644
--- a/MdePkg/MdePkg.dsc
+++ b/MdePkg/MdePkg.dsc
@@ -192,5 +192,7 @@ [Components.ARM, Components.AARCH64]
 
 [Components.RISCV64]
   MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf
+  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf
+  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf
 
 [BuildOptions]
diff --git a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf
new file mode 100644
index 000000000000..1d8544756bb9
--- /dev/null
+++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf
@@ -0,0 +1,36 @@
+## @file
+#  Serial Port Library backed by SBI console.
+#
+#  Meant for PrePi and DXE environments (where globals are allowed). See
+#  SecPeiSerialPortLibRiscVSbi for a reduced variant appropriate for SEC
+#  and PEI (XIP) environments.
+#
+#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x0001001B
+  BASE_NAME                      = PrePiDxeRiscVSerialPortLibSbi
+  MODULE_UNI_FILE                = PrePiDxeRiscVSerialPortLibSbi.uni
+  FILE_GUID                      = 872af743-ab56-45b4-a065-602567f4820c
+  MODULE_TYPE                    = BASE
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = SerialPortLib | SEC DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER UEFI_DRIVER UEFI_APPLICATION
+
+
+#
+#  VALID_ARCHITECTURES           = RISCV64
+#
+
+[Sources]
+  PrePiDxeRiscVSerialPortLibSbi.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+
+[LibraryClasses]
+  RiscVSbiLib
diff --git a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf
new file mode 100644
index 000000000000..7bc7cd47c11c
--- /dev/null
+++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf
@@ -0,0 +1,39 @@
+## @file
+#  Serial Port Library backed by SBI console.
+#
+#  Meant for SEC and PEI (XIP) environments.
+#
+#  Due to limitations of SBI console interface and XIP environments
+#  (on use of globals), this library instance does not implement reading
+#  and polling the serial port. See PrePiDxeSerialPortLibRiscVSbi for
+#  the full-featured variant meant for PrePi and DXE environments.
+#
+#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x0001001B
+  BASE_NAME                      = SecPeiRiscVSerialPortLibSbi
+  MODULE_UNI_FILE                = SecPeiRiscVSerialPortLibSbi.uni
+  FILE_GUID                      = 639fad38-4bfd-4eb9-9f09-e97c7947d480
+  MODULE_TYPE                    = BASE
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = SerialPortLib | SEC PEI_CORE PEIM
+
+
+#
+#  VALID_ARCHITECTURES           = RISCV64
+#
+
+[Sources]
+  SecPeiRiscVSerialPortLibSbi.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+
+[LibraryClasses]
+  RiscVSbiLib
diff --git a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c
new file mode 100644
index 000000000000..058a6573ef2d
--- /dev/null
+++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c
@@ -0,0 +1,288 @@
+/** @file
+  Serial Port Library backed by SBI console.
+
+  Meant for PrePi and DXE environments (where globals are allowed). See
+  SecPeiSerialPortLibRiscVSbi for a reduced variant appropriate for SEC
+  and PEI (XIP) environments.
+
+  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Base.h>
+#include <Library/SerialPortLib.h>
+#include <Library/BaseRiscVSbiLib.h>
+
+STATIC BOOLEAN  mHaveDbcn          = FALSE;
+STATIC BOOLEAN  mHaveLegacyPutchar = FALSE;
+STATIC BOOLEAN  mHaveLegacyGetchar = FALSE;
+STATIC INT64    mLastGetChar       = -1;
+
+/**
+  Initialize the serial device hardware.
+
+  If no initialization is required, then return RETURN_SUCCESS.
+  If the serial device was successfully initialized, then return RETURN_SUCCESS.
+  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
+
+  @retval RETURN_SUCCESS        The serial device was initialized.
+  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
+
+**/
+RETURN_STATUS
+EFIAPI
+SerialPortInitialize (
+  VOID
+  )
+{
+  SBI_RET  Ret;
+
+  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_DBCN);
+  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
+      (Ret.Value != 0))
+  {
+    mHaveDbcn = TRUE;
+  } else {
+    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_PUTCHAR);
+    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
+        (Ret.Value != 0))
+    {
+      mHaveLegacyPutchar = TRUE;
+    }
+
+    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_GETCHAR);
+    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
+        (Ret.Value != 0))
+    {
+      mHaveLegacyGetchar = TRUE;
+    }
+  }
+
+  return RETURN_SUCCESS;
+}
+
+/**
+  Write data from buffer to serial device.
+
+  Writes NumberOfBytes data bytes from Buffer to the serial device.
+  The number of bytes actually written to the serial device is returned.
+  If the return value is less than NumberOfBytes, then the write operation failed.
+  If Buffer is NULL, then ASSERT().
+  If NumberOfBytes is zero, then return 0.
+
+  @param  Buffer           The pointer to the data buffer to be written.
+  @param  NumberOfBytes    The number of bytes to written to the serial device.
+
+  @retval 0                NumberOfBytes is 0.
+  @retval >0               The number of bytes written to the serial device.
+                           If this value is less than NumberOfBytes, then the write operation failed.
+
+**/
+UINTN
+EFIAPI
+SerialPortWrite (
+  IN UINT8  *Buffer,
+  IN UINTN  NumberOfBytes
+  )
+{
+  UINTN  Index;
+
+  if (mHaveDbcn) {
+    SBI_RET  Ret;
+    Ret = SbiCall (
+            SBI_EXT_DBCN,
+            SBI_EXT_DBCN_WRITE,
+            3,
+            NumberOfBytes,
+            ((UINTN)Buffer),
+            0
+            );
+    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
+      return 0;
+    }
+
+    return Ret.Value;
+  } else if (mHaveLegacyPutchar) {
+    for (Index = 0; Index < NumberOfBytes; Index++) {
+      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
+    }
+
+    return Index;
+  }
+
+  /*
+   * Neither DBCN or legacy extension were present.
+   */
+  return NumberOfBytes;
+}
+
+/**
+  Read data from serial device and save the datas in buffer.
+
+  Reads NumberOfBytes data bytes from a serial device into the buffer
+  specified by Buffer. The number of bytes actually read is returned.
+  If the return value is less than NumberOfBytes, then the rest operation failed.
+  If Buffer is NULL, then ASSERT().
+  If NumberOfBytes is zero, then return 0.
+
+  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
+  @param  NumberOfBytes    The number of bytes which will be read.
+
+  @retval 0                Read data failed; No data is to be read.
+  @retval >0               The actual number of bytes read from serial device.
+
+**/
+UINTN
+EFIAPI
+SerialPortRead (
+  OUT UINT8  *Buffer,
+  IN  UINTN  NumberOfBytes
+  )
+{
+  UINTN  Index;
+
+  Index = 0;
+  while ((Index < NumberOfBytes) && SerialPortPoll ()) {
+    Buffer[Index++] = (UINT8)mLastGetChar;
+    mLastGetChar    = -1;
+  }
+
+  return Index;
+}
+
+/**
+  Polls a serial device to see if there is any data waiting to be read.
+
+  Polls a serial device to see if there is any data waiting to be read.
+  If there is data waiting to be read from the serial device, then TRUE is returned.
+  If there is no data waiting to be read from the serial device, then FALSE is returned.
+
+  @retval TRUE             Data is waiting to be read from the serial device.
+  @retval FALSE            There is no data waiting to be read from the serial device.
+
+**/
+BOOLEAN
+EFIAPI
+SerialPortPoll (
+  VOID
+  )
+{
+  /*
+   * Careful. OpenSBI with HTIF console will return -1 followed by -2
+   * if there is no character received. So just check for values >= 0.
+   */
+
+  if (mLastGetChar >= 0) {
+    return TRUE;
+  }
+
+  if (mHaveDbcn) {
+    UINT8    Buffer;
+    SBI_RET  Ret;
+
+    Ret = SbiCall (
+            SBI_EXT_DBCN,
+            SBI_EXT_DBCN_READ,
+            3,
+            1,
+            ((UINTN)&Buffer),
+            0
+            );
+    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
+        (Ret.Value == 1))
+    {
+      mLastGetChar = Buffer;
+    }
+  } else if (mHaveLegacyGetchar) {
+    mLastGetChar = (INT64)SbiCall (SBI_EXT_0_1_CONSOLE_GETCHAR, 0, 0).Error;
+  }
+
+  return mLastGetChar >= 0;
+}
+
+/**
+  Sets the control bits on a serial device.
+
+  @param Control                Sets the bits of Control that are settable.
+
+  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
+  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
+  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
+
+**/
+RETURN_STATUS
+EFIAPI
+SerialPortSetControl (
+  IN UINT32  Control
+  )
+{
+  return RETURN_SUCCESS;
+}
+
+/**
+  Retrieve the status of the control bits on a serial device.
+
+  @param Control                A pointer to return the current control signals from the serial device.
+
+  @retval RETURN_SUCCESS        The control bits were read from the serial device.
+  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
+  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
+
+**/
+RETURN_STATUS
+EFIAPI
+SerialPortGetControl (
+  OUT UINT32  *Control
+  )
+{
+  *Control = 0;
+  return RETURN_SUCCESS;
+}
+
+/**
+  Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
+  data bits, and stop bits on a serial device.
+
+  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
+                            device's default interface speed.
+                            On output, the value actually set.
+  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
+                            serial interface. A ReceiveFifoDepth value of 0 will use
+                            the device's default FIFO depth.
+                            On output, the value actually set.
+  @param Timeout            The requested time out for a single character in microseconds.
+                            This timeout applies to both the transmit and receive side of the
+                            interface. A Timeout value of 0 will use the device's default time
+                            out value.
+                            On output, the value actually set.
+  @param Parity             The type of parity to use on this serial device. A Parity value of
+                            DefaultParity will use the device's default parity value.
+                            On output, the value actually set.
+  @param DataBits           The number of data bits to use on the serial device. A DataBits
+                            vaule of 0 will use the device's default data bit setting.
+                            On output, the value actually set.
+  @param StopBits           The number of stop bits to use on this serial device. A StopBits
+                            value of DefaultStopBits will use the device's default number of
+                            stop bits.
+                            On output, the value actually set.
+
+  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
+  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
+  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
+  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
+
+**/
+RETURN_STATUS
+EFIAPI
+SerialPortSetAttributes (
+  IN OUT UINT64              *BaudRate,
+  IN OUT UINT32              *ReceiveFifoDepth,
+  IN OUT UINT32              *Timeout,
+  IN OUT EFI_PARITY_TYPE     *Parity,
+  IN OUT UINT8               *DataBits,
+  IN OUT EFI_STOP_BITS_TYPE  *StopBits
+  )
+{
+  return RETURN_SUCCESS;
+}
diff --git a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c
new file mode 100644
index 000000000000..0ad5931be3ae
--- /dev/null
+++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c
@@ -0,0 +1,233 @@
+/** @file
+  Serial Port Library backed by SBI console.
+
+  Meant for SEC and PEI (XIP) environments.
+
+  Due to limitations of SBI console interface and XIP environments
+  (on use of globals), this library instance does not implement reading
+  and polling the serial port. See PrePiDxeSerialPortLibRiscVSbi for
+  the full-featured variant meant for PrePi and DXE environments.
+
+  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Base.h>
+#include <Library/SerialPortLib.h>
+#include <Library/BaseRiscVSbiLib.h>
+
+/**
+  Initialize the serial device hardware.
+
+  If no initialization is required, then return RETURN_SUCCESS.
+  If the serial device was successfully initialized, then return RETURN_SUCCESS.
+  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
+
+  @retval RETURN_SUCCESS        The serial device was initialized.
+  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
+
+**/
+RETURN_STATUS
+EFIAPI
+SerialPortInitialize (
+  VOID
+  )
+{
+  return RETURN_SUCCESS;
+}
+
+/**
+  Write data from buffer to serial device.
+
+  Writes NumberOfBytes data bytes from Buffer to the serial device.
+  The number of bytes actually written to the serial device is returned.
+  If the return value is less than NumberOfBytes, then the write operation failed.
+  If Buffer is NULL, then ASSERT().
+  If NumberOfBytes is zero, then return 0.
+
+  @param  Buffer           The pointer to the data buffer to be written.
+  @param  NumberOfBytes    The number of bytes to written to the serial device.
+
+  @retval 0                NumberOfBytes is 0.
+  @retval >0               The number of bytes written to the serial device.
+                           If this value is less than NumberOfBytes, then the write operation failed.
+
+**/
+UINTN
+EFIAPI
+SerialPortWrite (
+  IN UINT8  *Buffer,
+  IN UINTN  NumberOfBytes
+  )
+{
+  SBI_RET  Ret;
+  UINTN    Index;
+
+  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_DBCN);
+  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
+      (Ret.Value != 0))
+  {
+    Ret = SbiCall (
+            SBI_EXT_DBCN,
+            SBI_EXT_DBCN_WRITE,
+            3,
+            NumberOfBytes,
+            ((UINTN)Buffer),
+            0
+            );
+    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
+      return 0;
+    }
+
+    return Ret.Value;
+  }
+
+  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_PUTCHAR);
+  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
+      (Ret.Value != 0))
+  {
+    for (Index = 0; Index < NumberOfBytes; Index++) {
+      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
+    }
+
+    return Index;
+  }
+
+  /*
+   * Neither DBCN or legacy extension were present.
+   */
+  return NumberOfBytes;
+}
+
+/**
+  Read data from serial device and save the datas in buffer.
+
+  Reads NumberOfBytes data bytes from a serial device into the buffer
+  specified by Buffer. The number of bytes actually read is returned.
+  If the return value is less than NumberOfBytes, then the rest operation failed.
+  If Buffer is NULL, then ASSERT().
+  If NumberOfBytes is zero, then return 0.
+
+  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
+  @param  NumberOfBytes    The number of bytes which will be read.
+
+  @retval 0                Read data failed; No data is to be read.
+  @retval >0               The actual number of bytes read from serial device.
+
+**/
+UINTN
+EFIAPI
+SerialPortRead (
+  OUT UINT8  *Buffer,
+  IN  UINTN  NumberOfBytes
+  )
+{
+  return 0;
+}
+
+/**
+  Polls a serial device to see if there is any data waiting to be read.
+
+  Polls a serial device to see if there is any data waiting to be read.
+  If there is data waiting to be read from the serial device, then TRUE is returned.
+  If there is no data waiting to be read from the serial device, then FALSE is returned.
+
+  @retval TRUE             Data is waiting to be read from the serial device.
+  @retval FALSE            There is no data waiting to be read from the serial device.
+
+**/
+BOOLEAN
+EFIAPI
+SerialPortPoll (
+  VOID
+  )
+{
+  return FALSE;
+}
+
+/**
+  Sets the control bits on a serial device.
+
+  @param Control                Sets the bits of Control that are settable.
+
+  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
+  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
+  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
+
+**/
+RETURN_STATUS
+EFIAPI
+SerialPortSetControl (
+  IN UINT32  Control
+  )
+{
+  return RETURN_SUCCESS;
+}
+
+/**
+  Retrieve the status of the control bits on a serial device.
+
+  @param Control                A pointer to return the current control signals from the serial device.
+
+  @retval RETURN_SUCCESS        The control bits were read from the serial device.
+  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
+  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
+
+**/
+RETURN_STATUS
+EFIAPI
+SerialPortGetControl (
+  OUT UINT32  *Control
+  )
+{
+  *Control = 0;
+  return RETURN_SUCCESS;
+}
+
+/**
+  Sets the baud rate, receive FIFO depth, transmit/receice time out, parity,
+  data bits, and stop bits on a serial device.
+
+  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
+                            device's default interface speed.
+                            On output, the value actually set.
+  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
+                            serial interface. A ReceiveFifoDepth value of 0 will use
+                            the device's default FIFO depth.
+                            On output, the value actually set.
+  @param Timeout            The requested time out for a single character in microseconds.
+                            This timeout applies to both the transmit and receive side of the
+                            interface. A Timeout value of 0 will use the device's default time
+                            out value.
+                            On output, the value actually set.
+  @param Parity             The type of parity to use on this serial device. A Parity value of
+                            DefaultParity will use the device's default parity value.
+                            On output, the value actually set.
+  @param DataBits           The number of data bits to use on the serial device. A DataBits
+                            vaule of 0 will use the device's default data bit setting.
+                            On output, the value actually set.
+  @param StopBits           The number of stop bits to use on this serial device. A StopBits
+                            value of DefaultStopBits will use the device's default number of
+                            stop bits.
+                            On output, the value actually set.
+
+  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
+  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
+  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
+  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
+
+**/
+RETURN_STATUS
+EFIAPI
+SerialPortSetAttributes (
+  IN OUT UINT64              *BaudRate,
+  IN OUT UINT32              *ReceiveFifoDepth,
+  IN OUT UINT32              *Timeout,
+  IN OUT EFI_PARITY_TYPE     *Parity,
+  IN OUT UINT8               *DataBits,
+  IN OUT EFI_STOP_BITS_TYPE  *StopBits
+  )
+{
+  return RETURN_SUCCESS;
+}
diff --git a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni
new file mode 100644
index 000000000000..7b22caa5a090
--- /dev/null
+++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni
@@ -0,0 +1,16 @@
+// /** @file
+// Serial Port Library backed by SBI console.
+//
+// Serial Port Library backed by SBI console.
+//
+// Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+// **/
+
+
+#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
+
+#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
+
diff --git a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni
new file mode 100644
index 000000000000..7b22caa5a090
--- /dev/null
+++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni
@@ -0,0 +1,16 @@
+// /** @file
+// Serial Port Library backed by SBI console.
+//
+// Serial Port Library backed by SBI console.
+//
+// Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+// **/
+
+
+#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
+
+#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
+
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [PATCH v5 3/3] OvmfPkg: RiscVVirt: Add missing SerialPortInitialize to Sec
  2023-03-10 17:04 [PATCH v5 0/3] RISC-V SBI-backed SerialLib Andrei Warkentin
  2023-03-10 17:04 ` [PATCH v5 1/3] MdePkg: BaseRiscVSbiLib: make more useful to consumers Andrei Warkentin
  2023-03-10 17:04 ` [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V Andrei Warkentin
@ 2023-03-10 17:04 ` Andrei Warkentin
  2 siblings, 0 replies; 14+ messages in thread
From: Andrei Warkentin @ 2023-03-10 17:04 UTC (permalink / raw)
  To: devel; +Cc: Andrei Warkentin, Daniel Schaefer, Sunil V L

If the SerialPortLib had any initialization needed, this
would be skipped in the RiscVVirt Sec. Follow the example
seen elsewhere (ArmVirtPkg PrePi).

Seen with BaseSerialPortLibRiscVSbi not using DBCN in Sec, yet
using DBCN elsewhere.

Cc: Daniel Schaefer <git@danielschaefer.me>
Reviewed-by: Sunil V L <sunilvl@ventanamicro.com>
Signed-off-by: Andrei Warkentin <andrei.warkentin@intel.com>
---
 OvmfPkg/RiscVVirt/Sec/SecMain.inf | 1 +
 OvmfPkg/RiscVVirt/Sec/SecMain.h   | 1 +
 OvmfPkg/RiscVVirt/Sec/SecMain.c   | 4 +++-
 3 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/OvmfPkg/RiscVVirt/Sec/SecMain.inf b/OvmfPkg/RiscVVirt/Sec/SecMain.inf
index aed35d3af596..0e2a5785e8a4 100644
--- a/OvmfPkg/RiscVVirt/Sec/SecMain.inf
+++ b/OvmfPkg/RiscVVirt/Sec/SecMain.inf
@@ -48,6 +48,7 @@ [LibraryClasses]
   FdtLib
   MemoryAllocationLib
   HobLib
+  SerialPortLib
 
 [Ppis]
   gEfiTemporaryRamSupportPpiGuid                # PPI ALWAYS_PRODUCED
diff --git a/OvmfPkg/RiscVVirt/Sec/SecMain.h b/OvmfPkg/RiscVVirt/Sec/SecMain.h
index 83a8058efe40..7c7650f0d298 100644
--- a/OvmfPkg/RiscVVirt/Sec/SecMain.h
+++ b/OvmfPkg/RiscVVirt/Sec/SecMain.h
@@ -29,6 +29,7 @@
 #include <Library/PrePiLib.h>
 #include <Library/PlatformInitLib.h>
 #include <Library/PrePiHobListPointerLib.h>
+#include <Library/SerialPortLib.h>
 #include <Register/RiscV64/RiscVImpl.h>
 
 /**
diff --git a/OvmfPkg/RiscVVirt/Sec/SecMain.c b/OvmfPkg/RiscVVirt/Sec/SecMain.c
index adf73f2eb66c..b35c77774510 100644
--- a/OvmfPkg/RiscVVirt/Sec/SecMain.c
+++ b/OvmfPkg/RiscVVirt/Sec/SecMain.c
@@ -1,7 +1,7 @@
 /** @file
   RISC-V SEC phase module for Qemu Virt.
 
-  Copyright (c) 2008 - 2015, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2008 - 2023, Intel Corporation. All rights reserved.<BR>
   Copyright (c) 2022, Ventana Micro Systems Inc. All rights reserved.<BR>
 
   SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -57,6 +57,8 @@ SecStartup (
   UINT64                      StackBase;
   UINT32                      StackSize;
 
+  SerialPortInitialize ();
+
   //
   // Report Status Code to indicate entering SEC core
   //
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
  2023-03-10 17:04 ` [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V Andrei Warkentin
@ 2023-03-17  4:53   ` Andrei Warkentin
  2023-03-17  5:00     ` Michael D Kinney
  0 siblings, 1 reply; 14+ messages in thread
From: Andrei Warkentin @ 2023-03-17  4:53 UTC (permalink / raw)
  To: devel@edk2.groups.io; +Cc: Kinney, Michael D, Gao, Liming, Liu, Zhiguang

Hi folks,

Can I get a review for the changes to MdePkg.dsc to include the new libs on RISC-V? That's the minor thing preventing this patch set from being merged 😊.

A

-----Original Message-----
From: Warkentin, Andrei <andrei.warkentin@intel.com> 
Sent: Friday, March 10, 2023 11:05 AM
To: devel@edk2.groups.io
Cc: Warkentin, Andrei <andrei.warkentin@intel.com>; Daniel Schaefer <git@danielschaefer.me>; Sunil V L <sunilvl@ventanamicro.com>; Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>; Gerd Hoffmann <kraxel@redhat.com>
Subject: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V

These are implementations of SerialPortLib using SBI console services.
- SecPeiRiscVSerialPortLibSbi is appropriate for SEC/PEI (XIP) environments
- PrePiDxeRiscVSerialPortLibSbi is appropriate for PrePI/DXE environments

Tested with:
- Qemu RiscVVirt (non-DBCN case, backed by UART)
- TinyEMU + RiscVVirt (non-DBCN case, HTIF)
- TinyEMU + RiscVVirt (DBCN case, HTIF)

Cc: Daniel Schaefer <git@danielschaefer.me>
Cc: Sunil V L <sunilvl@ventanamicro.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Zhiguang Liu <zhiguang.liu@intel.com>
Acked-by: Gerd Hoffmann <kraxel@redhat.com>
Signed-off-by: Andrei Warkentin <andrei.warkentin@intel.com>
---
 MdePkg/MdePkg.dsc                                                              |   2 +
 MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf |  36 +++
 MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf     |  39 +++
 MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c   | 288 ++++++++++++++++++++
 MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c       | 233 ++++++++++++++++
 MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni |  16 ++
 MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni     |  16 ++
 7 files changed, 630 insertions(+)

diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index 0ac7618b4623..fb31349042fb 100644
--- a/MdePkg/MdePkg.dsc
+++ b/MdePkg/MdePkg.dsc
@@ -192,5 +192,7 @@ [Components.ARM, Components.AARCH64]
 
 [Components.RISCV64]
   MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf
+  
+ MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi
+ .inf  
+ MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLi
+ bSbi.inf
 
 [BuildOptions]
diff --git a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf
new file mode 100644
index 000000000000..1d8544756bb9
--- /dev/null
+++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
+++ rtLibSbi.inf
@@ -0,0 +1,36 @@
+## @file
+#  Serial Port Library backed by SBI console.
+#
+#  Meant for PrePi and DXE environments (where globals are allowed). 
+See #  SecPeiSerialPortLibRiscVSbi for a reduced variant appropriate 
+for SEC #  and PEI (XIP) environments.
+#
+#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> # #  
+SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
+
+[Defines]
+  INF_VERSION                    = 0x0001001B
+  BASE_NAME                      = PrePiDxeRiscVSerialPortLibSbi
+  MODULE_UNI_FILE                = PrePiDxeRiscVSerialPortLibSbi.uni
+  FILE_GUID                      = 872af743-ab56-45b4-a065-602567f4820c
+  MODULE_TYPE                    = BASE
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = SerialPortLib | SEC DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER UEFI_DRIVER UEFI_APPLICATION
+
+
+#
+#  VALID_ARCHITECTURES           = RISCV64
+#
+
+[Sources]
+  PrePiDxeRiscVSerialPortLibSbi.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+
+[LibraryClasses]
+  RiscVSbiLib
diff --git a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf
new file mode 100644
index 000000000000..7bc7cd47c11c
--- /dev/null
+++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLi
+++ bSbi.inf
@@ -0,0 +1,39 @@
+## @file
+#  Serial Port Library backed by SBI console.
+#
+#  Meant for SEC and PEI (XIP) environments.
+#
+#  Due to limitations of SBI console interface and XIP environments #  
+(on use of globals), this library instance does not implement reading #  
+and polling the serial port. See PrePiDxeSerialPortLibRiscVSbi for #  
+the full-featured variant meant for PrePi and DXE environments.
+#
+#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> # #  
+SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
+
+[Defines]
+  INF_VERSION                    = 0x0001001B
+  BASE_NAME                      = SecPeiRiscVSerialPortLibSbi
+  MODULE_UNI_FILE                = SecPeiRiscVSerialPortLibSbi.uni
+  FILE_GUID                      = 639fad38-4bfd-4eb9-9f09-e97c7947d480
+  MODULE_TYPE                    = BASE
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = SerialPortLib | SEC PEI_CORE PEIM
+
+
+#
+#  VALID_ARCHITECTURES           = RISCV64
+#
+
+[Sources]
+  SecPeiRiscVSerialPortLibSbi.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+
+[LibraryClasses]
+  RiscVSbiLib
diff --git a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c
new file mode 100644
index 000000000000..058a6573ef2d
--- /dev/null
+++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
+++ rtLibSbi.c
@@ -0,0 +1,288 @@
+/** @file
+  Serial Port Library backed by SBI console.
+
+  Meant for PrePi and DXE environments (where globals are allowed). See  
+ SecPeiSerialPortLibRiscVSbi for a reduced variant appropriate for SEC  
+ and PEI (XIP) environments.
+
+  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Base.h>
+#include <Library/SerialPortLib.h>
+#include <Library/BaseRiscVSbiLib.h>
+
+STATIC BOOLEAN  mHaveDbcn          = FALSE;
+STATIC BOOLEAN  mHaveLegacyPutchar = FALSE; STATIC BOOLEAN  
+mHaveLegacyGetchar = FALSE;
+STATIC INT64    mLastGetChar       = -1;
+
+/**
+  Initialize the serial device hardware.
+
+  If no initialization is required, then return RETURN_SUCCESS.
+  If the serial device was successfully initialized, then return RETURN_SUCCESS.
+  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
+
+  @retval RETURN_SUCCESS        The serial device was initialized.
+  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
+
+**/
+RETURN_STATUS
+EFIAPI
+SerialPortInitialize (
+  VOID
+  )
+{
+  SBI_RET  Ret;
+
+  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, 
+ SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
+      (Ret.Value != 0))
+  {
+    mHaveDbcn = TRUE;
+  } else {
+    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_PUTCHAR);
+    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
+        (Ret.Value != 0))
+    {
+      mHaveLegacyPutchar = TRUE;
+    }
+
+    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_GETCHAR);
+    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
+        (Ret.Value != 0))
+    {
+      mHaveLegacyGetchar = TRUE;
+    }
+  }
+
+  return RETURN_SUCCESS;
+}
+
+/**
+  Write data from buffer to serial device.
+
+  Writes NumberOfBytes data bytes from Buffer to the serial device.
+  The number of bytes actually written to the serial device is returned.
+  If the return value is less than NumberOfBytes, then the write operation failed.
+  If Buffer is NULL, then ASSERT().
+  If NumberOfBytes is zero, then return 0.
+
+  @param  Buffer           The pointer to the data buffer to be written.
+  @param  NumberOfBytes    The number of bytes to written to the serial device.
+
+  @retval 0                NumberOfBytes is 0.
+  @retval >0               The number of bytes written to the serial device.
+                           If this value is less than NumberOfBytes, then the write operation failed.
+
+**/
+UINTN
+EFIAPI
+SerialPortWrite (
+  IN UINT8  *Buffer,
+  IN UINTN  NumberOfBytes
+  )
+{
+  UINTN  Index;
+
+  if (mHaveDbcn) {
+    SBI_RET  Ret;
+    Ret = SbiCall (
+            SBI_EXT_DBCN,
+            SBI_EXT_DBCN_WRITE,
+            3,
+            NumberOfBytes,
+            ((UINTN)Buffer),
+            0
+            );
+    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
+      return 0;
+    }
+
+    return Ret.Value;
+  } else if (mHaveLegacyPutchar) {
+    for (Index = 0; Index < NumberOfBytes; Index++) {
+      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
+    }
+
+    return Index;
+  }
+
+  /*
+   * Neither DBCN or legacy extension were present.
+   */
+  return NumberOfBytes;
+}
+
+/**
+  Read data from serial device and save the datas in buffer.
+
+  Reads NumberOfBytes data bytes from a serial device into the buffer  
+ specified by Buffer. The number of bytes actually read is returned.
+  If the return value is less than NumberOfBytes, then the rest operation failed.
+  If Buffer is NULL, then ASSERT().
+  If NumberOfBytes is zero, then return 0.
+
+  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
+  @param  NumberOfBytes    The number of bytes which will be read.
+
+  @retval 0                Read data failed; No data is to be read.
+  @retval >0               The actual number of bytes read from serial device.
+
+**/
+UINTN
+EFIAPI
+SerialPortRead (
+  OUT UINT8  *Buffer,
+  IN  UINTN  NumberOfBytes
+  )
+{
+  UINTN  Index;
+
+  Index = 0;
+  while ((Index < NumberOfBytes) && SerialPortPoll ()) {
+    Buffer[Index++] = (UINT8)mLastGetChar;
+    mLastGetChar    = -1;
+  }
+
+  return Index;
+}
+
+/**
+  Polls a serial device to see if there is any data waiting to be read.
+
+  Polls a serial device to see if there is any data waiting to be read.
+  If there is data waiting to be read from the serial device, then TRUE is returned.
+  If there is no data waiting to be read from the serial device, then FALSE is returned.
+
+  @retval TRUE             Data is waiting to be read from the serial device.
+  @retval FALSE            There is no data waiting to be read from the serial device.
+
+**/
+BOOLEAN
+EFIAPI
+SerialPortPoll (
+  VOID
+  )
+{
+  /*
+   * Careful. OpenSBI with HTIF console will return -1 followed by -2
+   * if there is no character received. So just check for values >= 0.
+   */
+
+  if (mLastGetChar >= 0) {
+    return TRUE;
+  }
+
+  if (mHaveDbcn) {
+    UINT8    Buffer;
+    SBI_RET  Ret;
+
+    Ret = SbiCall (
+            SBI_EXT_DBCN,
+            SBI_EXT_DBCN_READ,
+            3,
+            1,
+            ((UINTN)&Buffer),
+            0
+            );
+    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
+        (Ret.Value == 1))
+    {
+      mLastGetChar = Buffer;
+    }
+  } else if (mHaveLegacyGetchar) {
+    mLastGetChar = (INT64)SbiCall (SBI_EXT_0_1_CONSOLE_GETCHAR, 0, 
+ 0).Error;  }
+
+  return mLastGetChar >= 0;
+}
+
+/**
+  Sets the control bits on a serial device.
+
+  @param Control                Sets the bits of Control that are settable.
+
+  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
+  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
+  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
+
+**/
+RETURN_STATUS
+EFIAPI
+SerialPortSetControl (
+  IN UINT32  Control
+  )
+{
+  return RETURN_SUCCESS;
+}
+
+/**
+  Retrieve the status of the control bits on a serial device.
+
+  @param Control                A pointer to return the current control signals from the serial device.
+
+  @retval RETURN_SUCCESS        The control bits were read from the serial device.
+  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
+  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
+
+**/
+RETURN_STATUS
+EFIAPI
+SerialPortGetControl (
+  OUT UINT32  *Control
+  )
+{
+  *Control = 0;
+  return RETURN_SUCCESS;
+}
+
+/**
+  Sets the baud rate, receive FIFO depth, transmit/receice time out, 
+parity,
+  data bits, and stop bits on a serial device.
+
+  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
+                            device's default interface speed.
+                            On output, the value actually set.
+  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
+                            serial interface. A ReceiveFifoDepth value of 0 will use
+                            the device's default FIFO depth.
+                            On output, the value actually set.
+  @param Timeout            The requested time out for a single character in microseconds.
+                            This timeout applies to both the transmit and receive side of the
+                            interface. A Timeout value of 0 will use the device's default time
+                            out value.
+                            On output, the value actually set.
+  @param Parity             The type of parity to use on this serial device. A Parity value of
+                            DefaultParity will use the device's default parity value.
+                            On output, the value actually set.
+  @param DataBits           The number of data bits to use on the serial device. A DataBits
+                            vaule of 0 will use the device's default data bit setting.
+                            On output, the value actually set.
+  @param StopBits           The number of stop bits to use on this serial device. A StopBits
+                            value of DefaultStopBits will use the device's default number of
+                            stop bits.
+                            On output, the value actually set.
+
+  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
+  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
+  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
+  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
+
+**/
+RETURN_STATUS
+EFIAPI
+SerialPortSetAttributes (
+  IN OUT UINT64              *BaudRate,
+  IN OUT UINT32              *ReceiveFifoDepth,
+  IN OUT UINT32              *Timeout,
+  IN OUT EFI_PARITY_TYPE     *Parity,
+  IN OUT UINT8               *DataBits,
+  IN OUT EFI_STOP_BITS_TYPE  *StopBits
+  )
+{
+  return RETURN_SUCCESS;
+}
diff --git a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c
new file mode 100644
index 000000000000..0ad5931be3ae
--- /dev/null
+++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLi
+++ bSbi.c
@@ -0,0 +1,233 @@
+/** @file
+  Serial Port Library backed by SBI console.
+
+  Meant for SEC and PEI (XIP) environments.
+
+  Due to limitations of SBI console interface and XIP environments  (on 
+ use of globals), this library instance does not implement reading  and 
+ polling the serial port. See PrePiDxeSerialPortLibRiscVSbi for  the 
+ full-featured variant meant for PrePi and DXE environments.
+
+  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Base.h>
+#include <Library/SerialPortLib.h>
+#include <Library/BaseRiscVSbiLib.h>
+
+/**
+  Initialize the serial device hardware.
+
+  If no initialization is required, then return RETURN_SUCCESS.
+  If the serial device was successfully initialized, then return RETURN_SUCCESS.
+  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
+
+  @retval RETURN_SUCCESS        The serial device was initialized.
+  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
+
+**/
+RETURN_STATUS
+EFIAPI
+SerialPortInitialize (
+  VOID
+  )
+{
+  return RETURN_SUCCESS;
+}
+
+/**
+  Write data from buffer to serial device.
+
+  Writes NumberOfBytes data bytes from Buffer to the serial device.
+  The number of bytes actually written to the serial device is returned.
+  If the return value is less than NumberOfBytes, then the write operation failed.
+  If Buffer is NULL, then ASSERT().
+  If NumberOfBytes is zero, then return 0.
+
+  @param  Buffer           The pointer to the data buffer to be written.
+  @param  NumberOfBytes    The number of bytes to written to the serial device.
+
+  @retval 0                NumberOfBytes is 0.
+  @retval >0               The number of bytes written to the serial device.
+                           If this value is less than NumberOfBytes, then the write operation failed.
+
+**/
+UINTN
+EFIAPI
+SerialPortWrite (
+  IN UINT8  *Buffer,
+  IN UINTN  NumberOfBytes
+  )
+{
+  SBI_RET  Ret;
+  UINTN    Index;
+
+  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, 
+ SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
+      (Ret.Value != 0))
+  {
+    Ret = SbiCall (
+            SBI_EXT_DBCN,
+            SBI_EXT_DBCN_WRITE,
+            3,
+            NumberOfBytes,
+            ((UINTN)Buffer),
+            0
+            );
+    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
+      return 0;
+    }
+
+    return Ret.Value;
+  }
+
+  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, 
+ SBI_EXT_0_1_CONSOLE_PUTCHAR);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
+      (Ret.Value != 0))
+  {
+    for (Index = 0; Index < NumberOfBytes; Index++) {
+      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
+    }
+
+    return Index;
+  }
+
+  /*
+   * Neither DBCN or legacy extension were present.
+   */
+  return NumberOfBytes;
+}
+
+/**
+  Read data from serial device and save the datas in buffer.
+
+  Reads NumberOfBytes data bytes from a serial device into the buffer  
+ specified by Buffer. The number of bytes actually read is returned.
+  If the return value is less than NumberOfBytes, then the rest operation failed.
+  If Buffer is NULL, then ASSERT().
+  If NumberOfBytes is zero, then return 0.
+
+  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
+  @param  NumberOfBytes    The number of bytes which will be read.
+
+  @retval 0                Read data failed; No data is to be read.
+  @retval >0               The actual number of bytes read from serial device.
+
+**/
+UINTN
+EFIAPI
+SerialPortRead (
+  OUT UINT8  *Buffer,
+  IN  UINTN  NumberOfBytes
+  )
+{
+  return 0;
+}
+
+/**
+  Polls a serial device to see if there is any data waiting to be read.
+
+  Polls a serial device to see if there is any data waiting to be read.
+  If there is data waiting to be read from the serial device, then TRUE is returned.
+  If there is no data waiting to be read from the serial device, then FALSE is returned.
+
+  @retval TRUE             Data is waiting to be read from the serial device.
+  @retval FALSE            There is no data waiting to be read from the serial device.
+
+**/
+BOOLEAN
+EFIAPI
+SerialPortPoll (
+  VOID
+  )
+{
+  return FALSE;
+}
+
+/**
+  Sets the control bits on a serial device.
+
+  @param Control                Sets the bits of Control that are settable.
+
+  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
+  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
+  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
+
+**/
+RETURN_STATUS
+EFIAPI
+SerialPortSetControl (
+  IN UINT32  Control
+  )
+{
+  return RETURN_SUCCESS;
+}
+
+/**
+  Retrieve the status of the control bits on a serial device.
+
+  @param Control                A pointer to return the current control signals from the serial device.
+
+  @retval RETURN_SUCCESS        The control bits were read from the serial device.
+  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
+  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
+
+**/
+RETURN_STATUS
+EFIAPI
+SerialPortGetControl (
+  OUT UINT32  *Control
+  )
+{
+  *Control = 0;
+  return RETURN_SUCCESS;
+}
+
+/**
+  Sets the baud rate, receive FIFO depth, transmit/receice time out, 
+parity,
+  data bits, and stop bits on a serial device.
+
+  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
+                            device's default interface speed.
+                            On output, the value actually set.
+  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
+                            serial interface. A ReceiveFifoDepth value of 0 will use
+                            the device's default FIFO depth.
+                            On output, the value actually set.
+  @param Timeout            The requested time out for a single character in microseconds.
+                            This timeout applies to both the transmit and receive side of the
+                            interface. A Timeout value of 0 will use the device's default time
+                            out value.
+                            On output, the value actually set.
+  @param Parity             The type of parity to use on this serial device. A Parity value of
+                            DefaultParity will use the device's default parity value.
+                            On output, the value actually set.
+  @param DataBits           The number of data bits to use on the serial device. A DataBits
+                            vaule of 0 will use the device's default data bit setting.
+                            On output, the value actually set.
+  @param StopBits           The number of stop bits to use on this serial device. A StopBits
+                            value of DefaultStopBits will use the device's default number of
+                            stop bits.
+                            On output, the value actually set.
+
+  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
+  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
+  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
+  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
+
+**/
+RETURN_STATUS
+EFIAPI
+SerialPortSetAttributes (
+  IN OUT UINT64              *BaudRate,
+  IN OUT UINT32              *ReceiveFifoDepth,
+  IN OUT UINT32              *Timeout,
+  IN OUT EFI_PARITY_TYPE     *Parity,
+  IN OUT UINT8               *DataBits,
+  IN OUT EFI_STOP_BITS_TYPE  *StopBits
+  )
+{
+  return RETURN_SUCCESS;
+}
diff --git a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni
new file mode 100644
index 000000000000..7b22caa5a090
--- /dev/null
+++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
+++ rtLibSbi.uni
@@ -0,0 +1,16 @@
+// /** @file
+// Serial Port Library backed by SBI console.
+//
+// Serial Port Library backed by SBI console.
+//
+// Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> // 
+// SPDX-License-Identifier: BSD-2-Clause-Patent // // **/
+
+
+#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
+
+#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
+
diff --git a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni
new file mode 100644
index 000000000000..7b22caa5a090
--- /dev/null
+++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni
@@ -0,0 +1,16 @@
+// /** @file
+// Serial Port Library backed by SBI console.
+//
+// Serial Port Library backed by SBI console.
+//
+// Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+// **/
+
+
+#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
+
+#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
+
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
  2023-03-17  4:53   ` Andrei Warkentin
@ 2023-03-17  5:00     ` Michael D Kinney
  2023-03-17  5:44       ` Andrei Warkentin
       [not found]       ` <174D1E9AD1BD3A96.10271@groups.io>
  0 siblings, 2 replies; 14+ messages in thread
From: Michael D Kinney @ 2023-03-17  5:00 UTC (permalink / raw)
  To: Warkentin, Andrei, devel@edk2.groups.io
  Cc: Gao, Liming, Liu, Zhiguang, Kinney, Michael D

What is the definition of "PrePI"?  That is not a prefix I have seen used before.

Mike

> -----Original Message-----
> From: Warkentin, Andrei <andrei.warkentin@intel.com>
> Sent: Thursday, March 16, 2023 9:53 PM
> To: devel@edk2.groups.io
> Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>
> Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
> 
> Hi folks,
> 
> Can I get a review for the changes to MdePkg.dsc to include the new libs on RISC-V? That's the minor thing preventing this patch
> set from being merged 😊.
> 
> A
> 
> -----Original Message-----
> From: Warkentin, Andrei <andrei.warkentin@intel.com>
> Sent: Friday, March 10, 2023 11:05 AM
> To: devel@edk2.groups.io
> Cc: Warkentin, Andrei <andrei.warkentin@intel.com>; Daniel Schaefer <git@danielschaefer.me>; Sunil V L <sunilvl@ventanamicro.com>;
> Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>;
> Gerd Hoffmann <kraxel@redhat.com>
> Subject: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
> 
> These are implementations of SerialPortLib using SBI console services.
> - SecPeiRiscVSerialPortLibSbi is appropriate for SEC/PEI (XIP) environments
> - PrePiDxeRiscVSerialPortLibSbi is appropriate for PrePI/DXE environments
> 
> Tested with:
> - Qemu RiscVVirt (non-DBCN case, backed by UART)
> - TinyEMU + RiscVVirt (non-DBCN case, HTIF)
> - TinyEMU + RiscVVirt (DBCN case, HTIF)
> 
> Cc: Daniel Schaefer <git@danielschaefer.me>
> Cc: Sunil V L <sunilvl@ventanamicro.com>
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> Cc: Liming Gao <gaoliming@byosoft.com.cn>
> Cc: Zhiguang Liu <zhiguang.liu@intel.com>
> Acked-by: Gerd Hoffmann <kraxel@redhat.com>
> Signed-off-by: Andrei Warkentin <andrei.warkentin@intel.com>
> ---
>  MdePkg/MdePkg.dsc                                                              |   2 +
>  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf |  36 +++
>  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf     |  39 +++
>  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c   | 288 ++++++++++++++++++++
>  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c       | 233 ++++++++++++++++
>  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni |  16 ++
>  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni     |  16 ++
>  7 files changed, 630 insertions(+)
> 
> diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index 0ac7618b4623..fb31349042fb 100644
> --- a/MdePkg/MdePkg.dsc
> +++ b/MdePkg/MdePkg.dsc
> @@ -192,5 +192,7 @@ [Components.ARM, Components.AARCH64]
> 
>  [Components.RISCV64]
>    MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf
> +
> + MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi
> + .inf
> + MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLi
> + bSbi.inf
> 
>  [BuildOptions]
> diff --git a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf
> b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf
> new file mode 100644
> index 000000000000..1d8544756bb9
> --- /dev/null
> +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
> +++ rtLibSbi.inf
> @@ -0,0 +1,36 @@
> +## @file
> +#  Serial Port Library backed by SBI console.
> +#
> +#  Meant for PrePi and DXE environments (where globals are allowed).
> +See #  SecPeiSerialPortLibRiscVSbi for a reduced variant appropriate
> +for SEC #  and PEI (XIP) environments.
> +#
> +#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> # #
> +SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001B
> +  BASE_NAME                      = PrePiDxeRiscVSerialPortLibSbi
> +  MODULE_UNI_FILE                = PrePiDxeRiscVSerialPortLibSbi.uni
> +  FILE_GUID                      = 872af743-ab56-45b4-a065-602567f4820c
> +  MODULE_TYPE                    = BASE
> +  VERSION_STRING                 = 1.0
> +  LIBRARY_CLASS                  = SerialPortLib | SEC DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER UEFI_DRIVER UEFI_APPLICATION
> +
> +
> +#
> +#  VALID_ARCHITECTURES           = RISCV64
> +#
> +
> +[Sources]
> +  PrePiDxeRiscVSerialPortLibSbi.c
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +
> +[LibraryClasses]
> +  RiscVSbiLib
> diff --git a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf
> b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf
> new file mode 100644
> index 000000000000..7bc7cd47c11c
> --- /dev/null
> +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLi
> +++ bSbi.inf
> @@ -0,0 +1,39 @@
> +## @file
> +#  Serial Port Library backed by SBI console.
> +#
> +#  Meant for SEC and PEI (XIP) environments.
> +#
> +#  Due to limitations of SBI console interface and XIP environments #
> +(on use of globals), this library instance does not implement reading #
> +and polling the serial port. See PrePiDxeSerialPortLibRiscVSbi for #
> +the full-featured variant meant for PrePi and DXE environments.
> +#
> +#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> # #
> +SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001B
> +  BASE_NAME                      = SecPeiRiscVSerialPortLibSbi
> +  MODULE_UNI_FILE                = SecPeiRiscVSerialPortLibSbi.uni
> +  FILE_GUID                      = 639fad38-4bfd-4eb9-9f09-e97c7947d480
> +  MODULE_TYPE                    = BASE
> +  VERSION_STRING                 = 1.0
> +  LIBRARY_CLASS                  = SerialPortLib | SEC PEI_CORE PEIM
> +
> +
> +#
> +#  VALID_ARCHITECTURES           = RISCV64
> +#
> +
> +[Sources]
> +  SecPeiRiscVSerialPortLibSbi.c
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +
> +[LibraryClasses]
> +  RiscVSbiLib
> diff --git a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c
> b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c
> new file mode 100644
> index 000000000000..058a6573ef2d
> --- /dev/null
> +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
> +++ rtLibSbi.c
> @@ -0,0 +1,288 @@
> +/** @file
> +  Serial Port Library backed by SBI console.
> +
> +  Meant for PrePi and DXE environments (where globals are allowed). See
> + SecPeiSerialPortLibRiscVSbi for a reduced variant appropriate for SEC
> + and PEI (XIP) environments.
> +
> +  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include <Base.h>
> +#include <Library/SerialPortLib.h>
> +#include <Library/BaseRiscVSbiLib.h>
> +
> +STATIC BOOLEAN  mHaveDbcn          = FALSE;
> +STATIC BOOLEAN  mHaveLegacyPutchar = FALSE; STATIC BOOLEAN
> +mHaveLegacyGetchar = FALSE;
> +STATIC INT64    mLastGetChar       = -1;
> +
> +/**
> +  Initialize the serial device hardware.
> +
> +  If no initialization is required, then return RETURN_SUCCESS.
> +  If the serial device was successfully initialized, then return RETURN_SUCCESS.
> +  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
> +
> +  @retval RETURN_SUCCESS        The serial device was initialized.
> +  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortInitialize (
> +  VOID
> +  )
> +{
> +  SBI_RET  Ret;
> +
> +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1,
> + SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> +      (Ret.Value != 0))
> +  {
> +    mHaveDbcn = TRUE;
> +  } else {
> +    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_PUTCHAR);
> +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> +        (Ret.Value != 0))
> +    {
> +      mHaveLegacyPutchar = TRUE;
> +    }
> +
> +    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_GETCHAR);
> +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> +        (Ret.Value != 0))
> +    {
> +      mHaveLegacyGetchar = TRUE;
> +    }
> +  }
> +
> +  return RETURN_SUCCESS;
> +}
> +
> +/**
> +  Write data from buffer to serial device.
> +
> +  Writes NumberOfBytes data bytes from Buffer to the serial device.
> +  The number of bytes actually written to the serial device is returned.
> +  If the return value is less than NumberOfBytes, then the write operation failed.
> +  If Buffer is NULL, then ASSERT().
> +  If NumberOfBytes is zero, then return 0.
> +
> +  @param  Buffer           The pointer to the data buffer to be written.
> +  @param  NumberOfBytes    The number of bytes to written to the serial device.
> +
> +  @retval 0                NumberOfBytes is 0.
> +  @retval >0               The number of bytes written to the serial device.
> +                           If this value is less than NumberOfBytes, then the write operation failed.
> +
> +**/
> +UINTN
> +EFIAPI
> +SerialPortWrite (
> +  IN UINT8  *Buffer,
> +  IN UINTN  NumberOfBytes
> +  )
> +{
> +  UINTN  Index;
> +
> +  if (mHaveDbcn) {
> +    SBI_RET  Ret;
> +    Ret = SbiCall (
> +            SBI_EXT_DBCN,
> +            SBI_EXT_DBCN_WRITE,
> +            3,
> +            NumberOfBytes,
> +            ((UINTN)Buffer),
> +            0
> +            );
> +    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
> +      return 0;
> +    }
> +
> +    return Ret.Value;
> +  } else if (mHaveLegacyPutchar) {
> +    for (Index = 0; Index < NumberOfBytes; Index++) {
> +      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
> +    }
> +
> +    return Index;
> +  }
> +
> +  /*
> +   * Neither DBCN or legacy extension were present.
> +   */
> +  return NumberOfBytes;
> +}
> +
> +/**
> +  Read data from serial device and save the datas in buffer.
> +
> +  Reads NumberOfBytes data bytes from a serial device into the buffer
> + specified by Buffer. The number of bytes actually read is returned.
> +  If the return value is less than NumberOfBytes, then the rest operation failed.
> +  If Buffer is NULL, then ASSERT().
> +  If NumberOfBytes is zero, then return 0.
> +
> +  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
> +  @param  NumberOfBytes    The number of bytes which will be read.
> +
> +  @retval 0                Read data failed; No data is to be read.
> +  @retval >0               The actual number of bytes read from serial device.
> +
> +**/
> +UINTN
> +EFIAPI
> +SerialPortRead (
> +  OUT UINT8  *Buffer,
> +  IN  UINTN  NumberOfBytes
> +  )
> +{
> +  UINTN  Index;
> +
> +  Index = 0;
> +  while ((Index < NumberOfBytes) && SerialPortPoll ()) {
> +    Buffer[Index++] = (UINT8)mLastGetChar;
> +    mLastGetChar    = -1;
> +  }
> +
> +  return Index;
> +}
> +
> +/**
> +  Polls a serial device to see if there is any data waiting to be read.
> +
> +  Polls a serial device to see if there is any data waiting to be read.
> +  If there is data waiting to be read from the serial device, then TRUE is returned.
> +  If there is no data waiting to be read from the serial device, then FALSE is returned.
> +
> +  @retval TRUE             Data is waiting to be read from the serial device.
> +  @retval FALSE            There is no data waiting to be read from the serial device.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +SerialPortPoll (
> +  VOID
> +  )
> +{
> +  /*
> +   * Careful. OpenSBI with HTIF console will return -1 followed by -2
> +   * if there is no character received. So just check for values >= 0.
> +   */
> +
> +  if (mLastGetChar >= 0) {
> +    return TRUE;
> +  }
> +
> +  if (mHaveDbcn) {
> +    UINT8    Buffer;
> +    SBI_RET  Ret;
> +
> +    Ret = SbiCall (
> +            SBI_EXT_DBCN,
> +            SBI_EXT_DBCN_READ,
> +            3,
> +            1,
> +            ((UINTN)&Buffer),
> +            0
> +            );
> +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> +        (Ret.Value == 1))
> +    {
> +      mLastGetChar = Buffer;
> +    }
> +  } else if (mHaveLegacyGetchar) {
> +    mLastGetChar = (INT64)SbiCall (SBI_EXT_0_1_CONSOLE_GETCHAR, 0,
> + 0).Error;  }
> +
> +  return mLastGetChar >= 0;
> +}
> +
> +/**
> +  Sets the control bits on a serial device.
> +
> +  @param Control                Sets the bits of Control that are settable.
> +
> +  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
> +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortSetControl (
> +  IN UINT32  Control
> +  )
> +{
> +  return RETURN_SUCCESS;
> +}
> +
> +/**
> +  Retrieve the status of the control bits on a serial device.
> +
> +  @param Control                A pointer to return the current control signals from the serial device.
> +
> +  @retval RETURN_SUCCESS        The control bits were read from the serial device.
> +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortGetControl (
> +  OUT UINT32  *Control
> +  )
> +{
> +  *Control = 0;
> +  return RETURN_SUCCESS;
> +}
> +
> +/**
> +  Sets the baud rate, receive FIFO depth, transmit/receice time out,
> +parity,
> +  data bits, and stop bits on a serial device.
> +
> +  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
> +                            device's default interface speed.
> +                            On output, the value actually set.
> +  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
> +                            serial interface. A ReceiveFifoDepth value of 0 will use
> +                            the device's default FIFO depth.
> +                            On output, the value actually set.
> +  @param Timeout            The requested time out for a single character in microseconds.
> +                            This timeout applies to both the transmit and receive side of the
> +                            interface. A Timeout value of 0 will use the device's default time
> +                            out value.
> +                            On output, the value actually set.
> +  @param Parity             The type of parity to use on this serial device. A Parity value of
> +                            DefaultParity will use the device's default parity value.
> +                            On output, the value actually set.
> +  @param DataBits           The number of data bits to use on the serial device. A DataBits
> +                            vaule of 0 will use the device's default data bit setting.
> +                            On output, the value actually set.
> +  @param StopBits           The number of stop bits to use on this serial device. A StopBits
> +                            value of DefaultStopBits will use the device's default number of
> +                            stop bits.
> +                            On output, the value actually set.
> +
> +  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
> +  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
> +  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
> +  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortSetAttributes (
> +  IN OUT UINT64              *BaudRate,
> +  IN OUT UINT32              *ReceiveFifoDepth,
> +  IN OUT UINT32              *Timeout,
> +  IN OUT EFI_PARITY_TYPE     *Parity,
> +  IN OUT UINT8               *DataBits,
> +  IN OUT EFI_STOP_BITS_TYPE  *StopBits
> +  )
> +{
> +  return RETURN_SUCCESS;
> +}
> diff --git a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c
> b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c
> new file mode 100644
> index 000000000000..0ad5931be3ae
> --- /dev/null
> +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLi
> +++ bSbi.c
> @@ -0,0 +1,233 @@
> +/** @file
> +  Serial Port Library backed by SBI console.
> +
> +  Meant for SEC and PEI (XIP) environments.
> +
> +  Due to limitations of SBI console interface and XIP environments  (on
> + use of globals), this library instance does not implement reading  and
> + polling the serial port. See PrePiDxeSerialPortLibRiscVSbi for  the
> + full-featured variant meant for PrePi and DXE environments.
> +
> +  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include <Base.h>
> +#include <Library/SerialPortLib.h>
> +#include <Library/BaseRiscVSbiLib.h>
> +
> +/**
> +  Initialize the serial device hardware.
> +
> +  If no initialization is required, then return RETURN_SUCCESS.
> +  If the serial device was successfully initialized, then return RETURN_SUCCESS.
> +  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
> +
> +  @retval RETURN_SUCCESS        The serial device was initialized.
> +  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortInitialize (
> +  VOID
> +  )
> +{
> +  return RETURN_SUCCESS;
> +}
> +
> +/**
> +  Write data from buffer to serial device.
> +
> +  Writes NumberOfBytes data bytes from Buffer to the serial device.
> +  The number of bytes actually written to the serial device is returned.
> +  If the return value is less than NumberOfBytes, then the write operation failed.
> +  If Buffer is NULL, then ASSERT().
> +  If NumberOfBytes is zero, then return 0.
> +
> +  @param  Buffer           The pointer to the data buffer to be written.
> +  @param  NumberOfBytes    The number of bytes to written to the serial device.
> +
> +  @retval 0                NumberOfBytes is 0.
> +  @retval >0               The number of bytes written to the serial device.
> +                           If this value is less than NumberOfBytes, then the write operation failed.
> +
> +**/
> +UINTN
> +EFIAPI
> +SerialPortWrite (
> +  IN UINT8  *Buffer,
> +  IN UINTN  NumberOfBytes
> +  )
> +{
> +  SBI_RET  Ret;
> +  UINTN    Index;
> +
> +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1,
> + SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> +      (Ret.Value != 0))
> +  {
> +    Ret = SbiCall (
> +            SBI_EXT_DBCN,
> +            SBI_EXT_DBCN_WRITE,
> +            3,
> +            NumberOfBytes,
> +            ((UINTN)Buffer),
> +            0
> +            );
> +    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
> +      return 0;
> +    }
> +
> +    return Ret.Value;
> +  }
> +
> +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1,
> + SBI_EXT_0_1_CONSOLE_PUTCHAR);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> +      (Ret.Value != 0))
> +  {
> +    for (Index = 0; Index < NumberOfBytes; Index++) {
> +      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
> +    }
> +
> +    return Index;
> +  }
> +
> +  /*
> +   * Neither DBCN or legacy extension were present.
> +   */
> +  return NumberOfBytes;
> +}
> +
> +/**
> +  Read data from serial device and save the datas in buffer.
> +
> +  Reads NumberOfBytes data bytes from a serial device into the buffer
> + specified by Buffer. The number of bytes actually read is returned.
> +  If the return value is less than NumberOfBytes, then the rest operation failed.
> +  If Buffer is NULL, then ASSERT().
> +  If NumberOfBytes is zero, then return 0.
> +
> +  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
> +  @param  NumberOfBytes    The number of bytes which will be read.
> +
> +  @retval 0                Read data failed; No data is to be read.
> +  @retval >0               The actual number of bytes read from serial device.
> +
> +**/
> +UINTN
> +EFIAPI
> +SerialPortRead (
> +  OUT UINT8  *Buffer,
> +  IN  UINTN  NumberOfBytes
> +  )
> +{
> +  return 0;
> +}
> +
> +/**
> +  Polls a serial device to see if there is any data waiting to be read.
> +
> +  Polls a serial device to see if there is any data waiting to be read.
> +  If there is data waiting to be read from the serial device, then TRUE is returned.
> +  If there is no data waiting to be read from the serial device, then FALSE is returned.
> +
> +  @retval TRUE             Data is waiting to be read from the serial device.
> +  @retval FALSE            There is no data waiting to be read from the serial device.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +SerialPortPoll (
> +  VOID
> +  )
> +{
> +  return FALSE;
> +}
> +
> +/**
> +  Sets the control bits on a serial device.
> +
> +  @param Control                Sets the bits of Control that are settable.
> +
> +  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
> +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortSetControl (
> +  IN UINT32  Control
> +  )
> +{
> +  return RETURN_SUCCESS;
> +}
> +
> +/**
> +  Retrieve the status of the control bits on a serial device.
> +
> +  @param Control                A pointer to return the current control signals from the serial device.
> +
> +  @retval RETURN_SUCCESS        The control bits were read from the serial device.
> +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortGetControl (
> +  OUT UINT32  *Control
> +  )
> +{
> +  *Control = 0;
> +  return RETURN_SUCCESS;
> +}
> +
> +/**
> +  Sets the baud rate, receive FIFO depth, transmit/receice time out,
> +parity,
> +  data bits, and stop bits on a serial device.
> +
> +  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
> +                            device's default interface speed.
> +                            On output, the value actually set.
> +  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
> +                            serial interface. A ReceiveFifoDepth value of 0 will use
> +                            the device's default FIFO depth.
> +                            On output, the value actually set.
> +  @param Timeout            The requested time out for a single character in microseconds.
> +                            This timeout applies to both the transmit and receive side of the
> +                            interface. A Timeout value of 0 will use the device's default time
> +                            out value.
> +                            On output, the value actually set.
> +  @param Parity             The type of parity to use on this serial device. A Parity value of
> +                            DefaultParity will use the device's default parity value.
> +                            On output, the value actually set.
> +  @param DataBits           The number of data bits to use on the serial device. A DataBits
> +                            vaule of 0 will use the device's default data bit setting.
> +                            On output, the value actually set.
> +  @param StopBits           The number of stop bits to use on this serial device. A StopBits
> +                            value of DefaultStopBits will use the device's default number of
> +                            stop bits.
> +                            On output, the value actually set.
> +
> +  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
> +  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
> +  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
> +  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortSetAttributes (
> +  IN OUT UINT64              *BaudRate,
> +  IN OUT UINT32              *ReceiveFifoDepth,
> +  IN OUT UINT32              *Timeout,
> +  IN OUT EFI_PARITY_TYPE     *Parity,
> +  IN OUT UINT8               *DataBits,
> +  IN OUT EFI_STOP_BITS_TYPE  *StopBits
> +  )
> +{
> +  return RETURN_SUCCESS;
> +}
> diff --git a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni
> b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni
> new file mode 100644
> index 000000000000..7b22caa5a090
> --- /dev/null
> +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
> +++ rtLibSbi.uni
> @@ -0,0 +1,16 @@
> +// /** @file
> +// Serial Port Library backed by SBI console.
> +//
> +// Serial Port Library backed by SBI console.
> +//
> +// Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> //
> +// SPDX-License-Identifier: BSD-2-Clause-Patent // // **/
> +
> +
> +#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
> +
> +#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
> +
> diff --git a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni
> b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni
> new file mode 100644
> index 000000000000..7b22caa5a090
> --- /dev/null
> +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni
> @@ -0,0 +1,16 @@
> +// /** @file
> +// Serial Port Library backed by SBI console.
> +//
> +// Serial Port Library backed by SBI console.
> +//
> +// Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> +//
> +// SPDX-License-Identifier: BSD-2-Clause-Patent
> +//
> +// **/
> +
> +
> +#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
> +
> +#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
> +
> --
> 2.25.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
  2023-03-17  5:00     ` Michael D Kinney
@ 2023-03-17  5:44       ` Andrei Warkentin
  2023-03-27 16:32         ` Michael D Kinney
       [not found]       ` <174D1E9AD1BD3A96.10271@groups.io>
  1 sibling, 1 reply; 14+ messages in thread
From: Andrei Warkentin @ 2023-03-17  5:44 UTC (permalink / raw)
  To: Kinney, Michael D, devel@edk2.groups.io; +Cc: Gao, Liming, Liu, Zhiguang

PrePi refers to a SEC-like stage in a Tiano implementation that skips the PEI stage. Reasons for skipping PEI usually center around memory initialization being done at an earlier stage of firmware initialization - for example, prior or part of TF-A firmware on AArch64 or prior or part of SBI implementation on RISC-V.

E.g. ArmVirtPkg/PrePi and OvmfPkg/RiscVVirt/Sec. The later is not a "SEC" in the send of handing off to PeiCore, but instead hands off to DxeCore.

There is no base type for PrePi. The type is still SEC. But it a) assumes RAM is available b) builds HOBs and hands-off to DXE.

The name has been around for at least half a decade now and making it a part of the SerialPortLib component name is important as it signifies the scenario where the code can apply.

A

-----Original Message-----
From: Kinney, Michael D <michael.d.kinney@intel.com> 
Sent: Friday, March 17, 2023 12:01 AM
To: Warkentin, Andrei <andrei.warkentin@intel.com>; devel@edk2.groups.io
Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>
Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V

What is the definition of "PrePI"?  That is not a prefix I have seen used before.

Mike

> -----Original Message-----
> From: Warkentin, Andrei <andrei.warkentin@intel.com>
> Sent: Thursday, March 16, 2023 9:53 PM
> To: devel@edk2.groups.io
> Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming 
> <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>
> Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for 
> RISC-V
> 
> Hi folks,
> 
> Can I get a review for the changes to MdePkg.dsc to include the new 
> libs on RISC-V? That's the minor thing preventing this patch set from being merged 😊.
> 
> A
> 
> -----Original Message-----
> From: Warkentin, Andrei <andrei.warkentin@intel.com>
> Sent: Friday, March 10, 2023 11:05 AM
> To: devel@edk2.groups.io
> Cc: Warkentin, Andrei <andrei.warkentin@intel.com>; Daniel Schaefer 
> <git@danielschaefer.me>; Sunil V L <sunilvl@ventanamicro.com>; Kinney, 
> Michael D <michael.d.kinney@intel.com>; Gao, Liming 
> <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>; 
> Gerd Hoffmann <kraxel@redhat.com>
> Subject: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
> 
> These are implementations of SerialPortLib using SBI console services.
> - SecPeiRiscVSerialPortLibSbi is appropriate for SEC/PEI (XIP) 
> environments
> - PrePiDxeRiscVSerialPortLibSbi is appropriate for PrePI/DXE 
> environments
> 
> Tested with:
> - Qemu RiscVVirt (non-DBCN case, backed by UART)
> - TinyEMU + RiscVVirt (non-DBCN case, HTIF)
> - TinyEMU + RiscVVirt (DBCN case, HTIF)
> 
> Cc: Daniel Schaefer <git@danielschaefer.me>
> Cc: Sunil V L <sunilvl@ventanamicro.com>
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> Cc: Liming Gao <gaoliming@byosoft.com.cn>
> Cc: Zhiguang Liu <zhiguang.liu@intel.com>
> Acked-by: Gerd Hoffmann <kraxel@redhat.com>
> Signed-off-by: Andrei Warkentin <andrei.warkentin@intel.com>
> ---
>  MdePkg/MdePkg.dsc                                                              |   2 +
>  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf |  36 +++
>  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf     |  39 +++
>  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c   | 288 ++++++++++++++++++++
>  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c       | 233 ++++++++++++++++
>  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni |  16 ++
>  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni     |  16 ++
>  7 files changed, 630 insertions(+)
> 
> diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index 
> 0ac7618b4623..fb31349042fb 100644
> --- a/MdePkg/MdePkg.dsc
> +++ b/MdePkg/MdePkg.dsc
> @@ -192,5 +192,7 @@ [Components.ARM, Components.AARCH64]
> 
>  [Components.RISCV64]
>    MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf
> +
> + MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> + bi
> + .inf
> + MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> + Li
> + bSbi.inf
> 
>  [BuildOptions]
> diff --git 
> a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> LibSbi.inf 
> b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> LibSbi.inf
> new file mode 100644
> index 000000000000..1d8544756bb9
> --- /dev/null
> +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> +++ Po
> +++ rtLibSbi.inf
> @@ -0,0 +1,36 @@
> +## @file
> +#  Serial Port Library backed by SBI console.
> +#
> +#  Meant for PrePi and DXE environments (where globals are allowed).
> +See #  SecPeiSerialPortLibRiscVSbi for a reduced variant appropriate 
> +for SEC #  and PEI (XIP) environments.
> +#
> +#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> # 
> +#
> +SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001B
> +  BASE_NAME                      = PrePiDxeRiscVSerialPortLibSbi
> +  MODULE_UNI_FILE                = PrePiDxeRiscVSerialPortLibSbi.uni
> +  FILE_GUID                      = 872af743-ab56-45b4-a065-602567f4820c
> +  MODULE_TYPE                    = BASE
> +  VERSION_STRING                 = 1.0
> +  LIBRARY_CLASS                  = SerialPortLib | SEC DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER UEFI_DRIVER UEFI_APPLICATION
> +
> +
> +#
> +#  VALID_ARCHITECTURES           = RISCV64
> +#
> +
> +[Sources]
> +  PrePiDxeRiscVSerialPortLibSbi.c
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +
> +[LibraryClasses]
> +  RiscVSbiLib
> diff --git 
> a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> bi.inf 
> b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> bi.inf
> new file mode 100644
> index 000000000000..7bc7cd47c11c
> --- /dev/null
> +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> +++ Li
> +++ bSbi.inf
> @@ -0,0 +1,39 @@
> +## @file
> +#  Serial Port Library backed by SBI console.
> +#
> +#  Meant for SEC and PEI (XIP) environments.
> +#
> +#  Due to limitations of SBI console interface and XIP environments # 
> +(on use of globals), this library instance does not implement reading 
> +# and polling the serial port. See PrePiDxeSerialPortLibRiscVSbi for 
> +# the full-featured variant meant for PrePi and DXE environments.
> +#
> +#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> # 
> +#
> +SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001B
> +  BASE_NAME                      = SecPeiRiscVSerialPortLibSbi
> +  MODULE_UNI_FILE                = SecPeiRiscVSerialPortLibSbi.uni
> +  FILE_GUID                      = 639fad38-4bfd-4eb9-9f09-e97c7947d480
> +  MODULE_TYPE                    = BASE
> +  VERSION_STRING                 = 1.0
> +  LIBRARY_CLASS                  = SerialPortLib | SEC PEI_CORE PEIM
> +
> +
> +#
> +#  VALID_ARCHITECTURES           = RISCV64
> +#
> +
> +[Sources]
> +  SecPeiRiscVSerialPortLibSbi.c
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +
> +[LibraryClasses]
> +  RiscVSbiLib
> diff --git 
> a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> LibSbi.c 
> b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> LibSbi.c
> new file mode 100644
> index 000000000000..058a6573ef2d
> --- /dev/null
> +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> +++ Po
> +++ rtLibSbi.c
> @@ -0,0 +1,288 @@
> +/** @file
> +  Serial Port Library backed by SBI console.
> +
> +  Meant for PrePi and DXE environments (where globals are allowed). 
> + See SecPeiSerialPortLibRiscVSbi for a reduced variant appropriate 
> + for SEC and PEI (XIP) environments.
> +
> +  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include <Base.h>
> +#include <Library/SerialPortLib.h>
> +#include <Library/BaseRiscVSbiLib.h>
> +
> +STATIC BOOLEAN  mHaveDbcn          = FALSE;
> +STATIC BOOLEAN  mHaveLegacyPutchar = FALSE; STATIC BOOLEAN 
> +mHaveLegacyGetchar = FALSE;
> +STATIC INT64    mLastGetChar       = -1;
> +
> +/**
> +  Initialize the serial device hardware.
> +
> +  If no initialization is required, then return RETURN_SUCCESS.
> +  If the serial device was successfully initialized, then return RETURN_SUCCESS.
> +  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
> +
> +  @retval RETURN_SUCCESS        The serial device was initialized.
> +  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortInitialize (
> +  VOID
> +  )
> +{
> +  SBI_RET  Ret;
> +
> +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, 
> + SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> +      (Ret.Value != 0))
> +  {
> +    mHaveDbcn = TRUE;
> +  } else {
> +    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_PUTCHAR);
> +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> +        (Ret.Value != 0))
> +    {
> +      mHaveLegacyPutchar = TRUE;
> +    }
> +
> +    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_GETCHAR);
> +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> +        (Ret.Value != 0))
> +    {
> +      mHaveLegacyGetchar = TRUE;
> +    }
> +  }
> +
> +  return RETURN_SUCCESS;
> +}
> +
> +/**
> +  Write data from buffer to serial device.
> +
> +  Writes NumberOfBytes data bytes from Buffer to the serial device.
> +  The number of bytes actually written to the serial device is returned.
> +  If the return value is less than NumberOfBytes, then the write operation failed.
> +  If Buffer is NULL, then ASSERT().
> +  If NumberOfBytes is zero, then return 0.
> +
> +  @param  Buffer           The pointer to the data buffer to be written.
> +  @param  NumberOfBytes    The number of bytes to written to the serial device.
> +
> +  @retval 0                NumberOfBytes is 0.
> +  @retval >0               The number of bytes written to the serial device.
> +                           If this value is less than NumberOfBytes, then the write operation failed.
> +
> +**/
> +UINTN
> +EFIAPI
> +SerialPortWrite (
> +  IN UINT8  *Buffer,
> +  IN UINTN  NumberOfBytes
> +  )
> +{
> +  UINTN  Index;
> +
> +  if (mHaveDbcn) {
> +    SBI_RET  Ret;
> +    Ret = SbiCall (
> +            SBI_EXT_DBCN,
> +            SBI_EXT_DBCN_WRITE,
> +            3,
> +            NumberOfBytes,
> +            ((UINTN)Buffer),
> +            0
> +            );
> +    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
> +      return 0;
> +    }
> +
> +    return Ret.Value;
> +  } else if (mHaveLegacyPutchar) {
> +    for (Index = 0; Index < NumberOfBytes; Index++) {
> +      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
> +    }
> +
> +    return Index;
> +  }
> +
> +  /*
> +   * Neither DBCN or legacy extension were present.
> +   */
> +  return NumberOfBytes;
> +}
> +
> +/**
> +  Read data from serial device and save the datas in buffer.
> +
> +  Reads NumberOfBytes data bytes from a serial device into the buffer 
> + specified by Buffer. The number of bytes actually read is returned.
> +  If the return value is less than NumberOfBytes, then the rest operation failed.
> +  If Buffer is NULL, then ASSERT().
> +  If NumberOfBytes is zero, then return 0.
> +
> +  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
> +  @param  NumberOfBytes    The number of bytes which will be read.
> +
> +  @retval 0                Read data failed; No data is to be read.
> +  @retval >0               The actual number of bytes read from serial device.
> +
> +**/
> +UINTN
> +EFIAPI
> +SerialPortRead (
> +  OUT UINT8  *Buffer,
> +  IN  UINTN  NumberOfBytes
> +  )
> +{
> +  UINTN  Index;
> +
> +  Index = 0;
> +  while ((Index < NumberOfBytes) && SerialPortPoll ()) {
> +    Buffer[Index++] = (UINT8)mLastGetChar;
> +    mLastGetChar    = -1;
> +  }
> +
> +  return Index;
> +}
> +
> +/**
> +  Polls a serial device to see if there is any data waiting to be read.
> +
> +  Polls a serial device to see if there is any data waiting to be read.
> +  If there is data waiting to be read from the serial device, then TRUE is returned.
> +  If there is no data waiting to be read from the serial device, then FALSE is returned.
> +
> +  @retval TRUE             Data is waiting to be read from the serial device.
> +  @retval FALSE            There is no data waiting to be read from the serial device.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +SerialPortPoll (
> +  VOID
> +  )
> +{
> +  /*
> +   * Careful. OpenSBI with HTIF console will return -1 followed by -2
> +   * if there is no character received. So just check for values >= 0.
> +   */
> +
> +  if (mLastGetChar >= 0) {
> +    return TRUE;
> +  }
> +
> +  if (mHaveDbcn) {
> +    UINT8    Buffer;
> +    SBI_RET  Ret;
> +
> +    Ret = SbiCall (
> +            SBI_EXT_DBCN,
> +            SBI_EXT_DBCN_READ,
> +            3,
> +            1,
> +            ((UINTN)&Buffer),
> +            0
> +            );
> +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> +        (Ret.Value == 1))
> +    {
> +      mLastGetChar = Buffer;
> +    }
> +  } else if (mHaveLegacyGetchar) {
> +    mLastGetChar = (INT64)SbiCall (SBI_EXT_0_1_CONSOLE_GETCHAR, 0, 
> + 0).Error;  }
> +
> +  return mLastGetChar >= 0;
> +}
> +
> +/**
> +  Sets the control bits on a serial device.
> +
> +  @param Control                Sets the bits of Control that are settable.
> +
> +  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
> +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortSetControl (
> +  IN UINT32  Control
> +  )
> +{
> +  return RETURN_SUCCESS;
> +}
> +
> +/**
> +  Retrieve the status of the control bits on a serial device.
> +
> +  @param Control                A pointer to return the current control signals from the serial device.
> +
> +  @retval RETURN_SUCCESS        The control bits were read from the serial device.
> +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortGetControl (
> +  OUT UINT32  *Control
> +  )
> +{
> +  *Control = 0;
> +  return RETURN_SUCCESS;
> +}
> +
> +/**
> +  Sets the baud rate, receive FIFO depth, transmit/receice time out, 
> +parity,
> +  data bits, and stop bits on a serial device.
> +
> +  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
> +                            device's default interface speed.
> +                            On output, the value actually set.
> +  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
> +                            serial interface. A ReceiveFifoDepth value of 0 will use
> +                            the device's default FIFO depth.
> +                            On output, the value actually set.
> +  @param Timeout            The requested time out for a single character in microseconds.
> +                            This timeout applies to both the transmit and receive side of the
> +                            interface. A Timeout value of 0 will use the device's default time
> +                            out value.
> +                            On output, the value actually set.
> +  @param Parity             The type of parity to use on this serial device. A Parity value of
> +                            DefaultParity will use the device's default parity value.
> +                            On output, the value actually set.
> +  @param DataBits           The number of data bits to use on the serial device. A DataBits
> +                            vaule of 0 will use the device's default data bit setting.
> +                            On output, the value actually set.
> +  @param StopBits           The number of stop bits to use on this serial device. A StopBits
> +                            value of DefaultStopBits will use the device's default number of
> +                            stop bits.
> +                            On output, the value actually set.
> +
> +  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
> +  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
> +  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
> +  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortSetAttributes (
> +  IN OUT UINT64              *BaudRate,
> +  IN OUT UINT32              *ReceiveFifoDepth,
> +  IN OUT UINT32              *Timeout,
> +  IN OUT EFI_PARITY_TYPE     *Parity,
> +  IN OUT UINT8               *DataBits,
> +  IN OUT EFI_STOP_BITS_TYPE  *StopBits
> +  )
> +{
> +  return RETURN_SUCCESS;
> +}
> diff --git 
> a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> bi.c 
> b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> bi.c
> new file mode 100644
> index 000000000000..0ad5931be3ae
> --- /dev/null
> +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> +++ Li
> +++ bSbi.c
> @@ -0,0 +1,233 @@
> +/** @file
> +  Serial Port Library backed by SBI console.
> +
> +  Meant for SEC and PEI (XIP) environments.
> +
> +  Due to limitations of SBI console interface and XIP environments  
> + (on use of globals), this library instance does not implement 
> + reading  and polling the serial port. See 
> + PrePiDxeSerialPortLibRiscVSbi for  the full-featured variant meant for PrePi and DXE environments.
> +
> +  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include <Base.h>
> +#include <Library/SerialPortLib.h>
> +#include <Library/BaseRiscVSbiLib.h>
> +
> +/**
> +  Initialize the serial device hardware.
> +
> +  If no initialization is required, then return RETURN_SUCCESS.
> +  If the serial device was successfully initialized, then return RETURN_SUCCESS.
> +  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
> +
> +  @retval RETURN_SUCCESS        The serial device was initialized.
> +  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortInitialize (
> +  VOID
> +  )
> +{
> +  return RETURN_SUCCESS;
> +}
> +
> +/**
> +  Write data from buffer to serial device.
> +
> +  Writes NumberOfBytes data bytes from Buffer to the serial device.
> +  The number of bytes actually written to the serial device is returned.
> +  If the return value is less than NumberOfBytes, then the write operation failed.
> +  If Buffer is NULL, then ASSERT().
> +  If NumberOfBytes is zero, then return 0.
> +
> +  @param  Buffer           The pointer to the data buffer to be written.
> +  @param  NumberOfBytes    The number of bytes to written to the serial device.
> +
> +  @retval 0                NumberOfBytes is 0.
> +  @retval >0               The number of bytes written to the serial device.
> +                           If this value is less than NumberOfBytes, then the write operation failed.
> +
> +**/
> +UINTN
> +EFIAPI
> +SerialPortWrite (
> +  IN UINT8  *Buffer,
> +  IN UINTN  NumberOfBytes
> +  )
> +{
> +  SBI_RET  Ret;
> +  UINTN    Index;
> +
> +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, 
> + SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> +      (Ret.Value != 0))
> +  {
> +    Ret = SbiCall (
> +            SBI_EXT_DBCN,
> +            SBI_EXT_DBCN_WRITE,
> +            3,
> +            NumberOfBytes,
> +            ((UINTN)Buffer),
> +            0
> +            );
> +    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
> +      return 0;
> +    }
> +
> +    return Ret.Value;
> +  }
> +
> +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, 
> + SBI_EXT_0_1_CONSOLE_PUTCHAR);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> +      (Ret.Value != 0))
> +  {
> +    for (Index = 0; Index < NumberOfBytes; Index++) {
> +      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
> +    }
> +
> +    return Index;
> +  }
> +
> +  /*
> +   * Neither DBCN or legacy extension were present.
> +   */
> +  return NumberOfBytes;
> +}
> +
> +/**
> +  Read data from serial device and save the datas in buffer.
> +
> +  Reads NumberOfBytes data bytes from a serial device into the buffer 
> + specified by Buffer. The number of bytes actually read is returned.
> +  If the return value is less than NumberOfBytes, then the rest operation failed.
> +  If Buffer is NULL, then ASSERT().
> +  If NumberOfBytes is zero, then return 0.
> +
> +  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
> +  @param  NumberOfBytes    The number of bytes which will be read.
> +
> +  @retval 0                Read data failed; No data is to be read.
> +  @retval >0               The actual number of bytes read from serial device.
> +
> +**/
> +UINTN
> +EFIAPI
> +SerialPortRead (
> +  OUT UINT8  *Buffer,
> +  IN  UINTN  NumberOfBytes
> +  )
> +{
> +  return 0;
> +}
> +
> +/**
> +  Polls a serial device to see if there is any data waiting to be read.
> +
> +  Polls a serial device to see if there is any data waiting to be read.
> +  If there is data waiting to be read from the serial device, then TRUE is returned.
> +  If there is no data waiting to be read from the serial device, then FALSE is returned.
> +
> +  @retval TRUE             Data is waiting to be read from the serial device.
> +  @retval FALSE            There is no data waiting to be read from the serial device.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +SerialPortPoll (
> +  VOID
> +  )
> +{
> +  return FALSE;
> +}
> +
> +/**
> +  Sets the control bits on a serial device.
> +
> +  @param Control                Sets the bits of Control that are settable.
> +
> +  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
> +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortSetControl (
> +  IN UINT32  Control
> +  )
> +{
> +  return RETURN_SUCCESS;
> +}
> +
> +/**
> +  Retrieve the status of the control bits on a serial device.
> +
> +  @param Control                A pointer to return the current control signals from the serial device.
> +
> +  @retval RETURN_SUCCESS        The control bits were read from the serial device.
> +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortGetControl (
> +  OUT UINT32  *Control
> +  )
> +{
> +  *Control = 0;
> +  return RETURN_SUCCESS;
> +}
> +
> +/**
> +  Sets the baud rate, receive FIFO depth, transmit/receice time out, 
> +parity,
> +  data bits, and stop bits on a serial device.
> +
> +  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
> +                            device's default interface speed.
> +                            On output, the value actually set.
> +  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
> +                            serial interface. A ReceiveFifoDepth value of 0 will use
> +                            the device's default FIFO depth.
> +                            On output, the value actually set.
> +  @param Timeout            The requested time out for a single character in microseconds.
> +                            This timeout applies to both the transmit and receive side of the
> +                            interface. A Timeout value of 0 will use the device's default time
> +                            out value.
> +                            On output, the value actually set.
> +  @param Parity             The type of parity to use on this serial device. A Parity value of
> +                            DefaultParity will use the device's default parity value.
> +                            On output, the value actually set.
> +  @param DataBits           The number of data bits to use on the serial device. A DataBits
> +                            vaule of 0 will use the device's default data bit setting.
> +                            On output, the value actually set.
> +  @param StopBits           The number of stop bits to use on this serial device. A StopBits
> +                            value of DefaultStopBits will use the device's default number of
> +                            stop bits.
> +                            On output, the value actually set.
> +
> +  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
> +  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
> +  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
> +  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortSetAttributes (
> +  IN OUT UINT64              *BaudRate,
> +  IN OUT UINT32              *ReceiveFifoDepth,
> +  IN OUT UINT32              *Timeout,
> +  IN OUT EFI_PARITY_TYPE     *Parity,
> +  IN OUT UINT8               *DataBits,
> +  IN OUT EFI_STOP_BITS_TYPE  *StopBits
> +  )
> +{
> +  return RETURN_SUCCESS;
> +}
> diff --git 
> a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> LibSbi.uni 
> b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> LibSbi.uni
> new file mode 100644
> index 000000000000..7b22caa5a090
> --- /dev/null
> +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> +++ Po
> +++ rtLibSbi.uni
> @@ -0,0 +1,16 @@
> +// /** @file
> +// Serial Port Library backed by SBI console.
> +//
> +// Serial Port Library backed by SBI console.
> +//
> +// Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> // 
> +// SPDX-License-Identifier: BSD-2-Clause-Patent // // **/
> +
> +
> +#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
> +
> +#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
> +
> diff --git 
> a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> bi.uni 
> b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> bi.uni
> new file mode 100644
> index 000000000000..7b22caa5a090
> --- /dev/null
> +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> +++ LibSbi.uni
> @@ -0,0 +1,16 @@
> +// /** @file
> +// Serial Port Library backed by SBI console.
> +//
> +// Serial Port Library backed by SBI console.
> +//
> +// Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> // 
> +// SPDX-License-Identifier: BSD-2-Clause-Patent // // **/
> +
> +
> +#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
> +
> +#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
> +
> --
> 2.25.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [edk2-devel] [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
       [not found]       ` <174D1E9AD1BD3A96.10271@groups.io>
@ 2023-03-27 16:10         ` Andrei Warkentin
  0 siblings, 0 replies; 14+ messages in thread
From: Andrei Warkentin @ 2023-03-27 16:10 UTC (permalink / raw)
  To: devel@edk2.groups.io, Warkentin, Andrei, Kinney, Michael D
  Cc: Gao, Liming, Liu, Zhiguang, Sunil V L

Hi folks,

If there aren't any remaining concerns, can I get a reviewed-by on the MdePkg changes?

A

-----Original Message-----
From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Andrei Warkentin
Sent: Friday, March 17, 2023 12:45 AM
To: Kinney, Michael D <michael.d.kinney@intel.com>; devel@edk2.groups.io
Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>
Subject: Re: [edk2-devel] [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V

PrePi refers to a SEC-like stage in a Tiano implementation that skips the PEI stage. Reasons for skipping PEI usually center around memory initialization being done at an earlier stage of firmware initialization - for example, prior or part of TF-A firmware on AArch64 or prior or part of SBI implementation on RISC-V.

E.g. ArmVirtPkg/PrePi and OvmfPkg/RiscVVirt/Sec. The later is not a "SEC" in the send of handing off to PeiCore, but instead hands off to DxeCore.

There is no base type for PrePi. The type is still SEC. But it a) assumes RAM is available b) builds HOBs and hands-off to DXE.

The name has been around for at least half a decade now and making it a part of the SerialPortLib component name is important as it signifies the scenario where the code can apply.

A

-----Original Message-----
From: Kinney, Michael D <michael.d.kinney@intel.com>
Sent: Friday, March 17, 2023 12:01 AM
To: Warkentin, Andrei <andrei.warkentin@intel.com>; devel@edk2.groups.io
Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>
Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V

What is the definition of "PrePI"?  That is not a prefix I have seen used before.

Mike

> -----Original Message-----
> From: Warkentin, Andrei <andrei.warkentin@intel.com>
> Sent: Thursday, March 16, 2023 9:53 PM
> To: devel@edk2.groups.io
> Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming 
> <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>
> Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for 
> RISC-V
> 
> Hi folks,
> 
> Can I get a review for the changes to MdePkg.dsc to include the new 
> libs on RISC-V? That's the minor thing preventing this patch set from being merged 😊.
> 
> A
> 
> -----Original Message-----
> From: Warkentin, Andrei <andrei.warkentin@intel.com>
> Sent: Friday, March 10, 2023 11:05 AM
> To: devel@edk2.groups.io
> Cc: Warkentin, Andrei <andrei.warkentin@intel.com>; Daniel Schaefer 
> <git@danielschaefer.me>; Sunil V L <sunilvl@ventanamicro.com>; Kinney, 
> Michael D <michael.d.kinney@intel.com>; Gao, Liming 
> <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>; 
> Gerd Hoffmann <kraxel@redhat.com>
> Subject: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
> 
> These are implementations of SerialPortLib using SBI console services.
> - SecPeiRiscVSerialPortLibSbi is appropriate for SEC/PEI (XIP) 
> environments
> - PrePiDxeRiscVSerialPortLibSbi is appropriate for PrePI/DXE 
> environments
> 
> Tested with:
> - Qemu RiscVVirt (non-DBCN case, backed by UART)
> - TinyEMU + RiscVVirt (non-DBCN case, HTIF)
> - TinyEMU + RiscVVirt (DBCN case, HTIF)
> 
> Cc: Daniel Schaefer <git@danielschaefer.me>
> Cc: Sunil V L <sunilvl@ventanamicro.com>
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> Cc: Liming Gao <gaoliming@byosoft.com.cn>
> Cc: Zhiguang Liu <zhiguang.liu@intel.com>
> Acked-by: Gerd Hoffmann <kraxel@redhat.com>
> Signed-off-by: Andrei Warkentin <andrei.warkentin@intel.com>
> ---
>  MdePkg/MdePkg.dsc                                                              |   2 +
>  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf |  36 +++
>  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf     |  39 +++
>  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c   | 288 ++++++++++++++++++++
>  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c       | 233 ++++++++++++++++
>  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni |  16 ++
>  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni     |  16 ++
>  7 files changed, 630 insertions(+)
> 
> diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index 
> 0ac7618b4623..fb31349042fb 100644
> --- a/MdePkg/MdePkg.dsc
> +++ b/MdePkg/MdePkg.dsc
> @@ -192,5 +192,7 @@ [Components.ARM, Components.AARCH64]
> 
>  [Components.RISCV64]
>    MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf
> +
> + MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> + bi
> + .inf
> + MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> + Li
> + bSbi.inf
> 
>  [BuildOptions]
> diff --git
> a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> LibSbi.inf
> b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> LibSbi.inf
> new file mode 100644
> index 000000000000..1d8544756bb9
> --- /dev/null
> +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> +++ Po
> +++ rtLibSbi.inf
> @@ -0,0 +1,36 @@
> +## @file
> +#  Serial Port Library backed by SBI console.
> +#
> +#  Meant for PrePi and DXE environments (where globals are allowed).
> +See #  SecPeiSerialPortLibRiscVSbi for a reduced variant appropriate 
> +for SEC #  and PEI (XIP) environments.
> +#
> +#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> # 
> +#
> +SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001B
> +  BASE_NAME                      = PrePiDxeRiscVSerialPortLibSbi
> +  MODULE_UNI_FILE                = PrePiDxeRiscVSerialPortLibSbi.uni
> +  FILE_GUID                      = 872af743-ab56-45b4-a065-602567f4820c
> +  MODULE_TYPE                    = BASE
> +  VERSION_STRING                 = 1.0
> +  LIBRARY_CLASS                  = SerialPortLib | SEC DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER UEFI_DRIVER UEFI_APPLICATION
> +
> +
> +#
> +#  VALID_ARCHITECTURES           = RISCV64
> +#
> +
> +[Sources]
> +  PrePiDxeRiscVSerialPortLibSbi.c
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +
> +[LibraryClasses]
> +  RiscVSbiLib
> diff --git
> a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> bi.inf
> b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> bi.inf
> new file mode 100644
> index 000000000000..7bc7cd47c11c
> --- /dev/null
> +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> +++ Li
> +++ bSbi.inf
> @@ -0,0 +1,39 @@
> +## @file
> +#  Serial Port Library backed by SBI console.
> +#
> +#  Meant for SEC and PEI (XIP) environments.
> +#
> +#  Due to limitations of SBI console interface and XIP environments # 
> +(on use of globals), this library instance does not implement reading 
> +# and polling the serial port. See PrePiDxeSerialPortLibRiscVSbi for 
> +# the full-featured variant meant for PrePi and DXE environments.
> +#
> +#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> # 
> +#
> +SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> +
> +[Defines]
> +  INF_VERSION                    = 0x0001001B
> +  BASE_NAME                      = SecPeiRiscVSerialPortLibSbi
> +  MODULE_UNI_FILE                = SecPeiRiscVSerialPortLibSbi.uni
> +  FILE_GUID                      = 639fad38-4bfd-4eb9-9f09-e97c7947d480
> +  MODULE_TYPE                    = BASE
> +  VERSION_STRING                 = 1.0
> +  LIBRARY_CLASS                  = SerialPortLib | SEC PEI_CORE PEIM
> +
> +
> +#
> +#  VALID_ARCHITECTURES           = RISCV64
> +#
> +
> +[Sources]
> +  SecPeiRiscVSerialPortLibSbi.c
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +
> +[LibraryClasses]
> +  RiscVSbiLib
> diff --git
> a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> LibSbi.c
> b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> LibSbi.c
> new file mode 100644
> index 000000000000..058a6573ef2d
> --- /dev/null
> +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> +++ Po
> +++ rtLibSbi.c
> @@ -0,0 +1,288 @@
> +/** @file
> +  Serial Port Library backed by SBI console.
> +
> +  Meant for PrePi and DXE environments (where globals are allowed). 
> + See SecPeiSerialPortLibRiscVSbi for a reduced variant appropriate 
> + for SEC and PEI (XIP) environments.
> +
> +  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include <Base.h>
> +#include <Library/SerialPortLib.h>
> +#include <Library/BaseRiscVSbiLib.h>
> +
> +STATIC BOOLEAN  mHaveDbcn          = FALSE;
> +STATIC BOOLEAN  mHaveLegacyPutchar = FALSE; STATIC BOOLEAN 
> +mHaveLegacyGetchar = FALSE;
> +STATIC INT64    mLastGetChar       = -1;
> +
> +/**
> +  Initialize the serial device hardware.
> +
> +  If no initialization is required, then return RETURN_SUCCESS.
> +  If the serial device was successfully initialized, then return RETURN_SUCCESS.
> +  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
> +
> +  @retval RETURN_SUCCESS        The serial device was initialized.
> +  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortInitialize (
> +  VOID
> +  )
> +{
> +  SBI_RET  Ret;
> +
> +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, 
> + SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> +      (Ret.Value != 0))
> +  {
> +    mHaveDbcn = TRUE;
> +  } else {
> +    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_PUTCHAR);
> +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> +        (Ret.Value != 0))
> +    {
> +      mHaveLegacyPutchar = TRUE;
> +    }
> +
> +    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_GETCHAR);
> +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> +        (Ret.Value != 0))
> +    {
> +      mHaveLegacyGetchar = TRUE;
> +    }
> +  }
> +
> +  return RETURN_SUCCESS;
> +}
> +
> +/**
> +  Write data from buffer to serial device.
> +
> +  Writes NumberOfBytes data bytes from Buffer to the serial device.
> +  The number of bytes actually written to the serial device is returned.
> +  If the return value is less than NumberOfBytes, then the write operation failed.
> +  If Buffer is NULL, then ASSERT().
> +  If NumberOfBytes is zero, then return 0.
> +
> +  @param  Buffer           The pointer to the data buffer to be written.
> +  @param  NumberOfBytes    The number of bytes to written to the serial device.
> +
> +  @retval 0                NumberOfBytes is 0.
> +  @retval >0               The number of bytes written to the serial device.
> +                           If this value is less than NumberOfBytes, then the write operation failed.
> +
> +**/
> +UINTN
> +EFIAPI
> +SerialPortWrite (
> +  IN UINT8  *Buffer,
> +  IN UINTN  NumberOfBytes
> +  )
> +{
> +  UINTN  Index;
> +
> +  if (mHaveDbcn) {
> +    SBI_RET  Ret;
> +    Ret = SbiCall (
> +            SBI_EXT_DBCN,
> +            SBI_EXT_DBCN_WRITE,
> +            3,
> +            NumberOfBytes,
> +            ((UINTN)Buffer),
> +            0
> +            );
> +    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
> +      return 0;
> +    }
> +
> +    return Ret.Value;
> +  } else if (mHaveLegacyPutchar) {
> +    for (Index = 0; Index < NumberOfBytes; Index++) {
> +      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
> +    }
> +
> +    return Index;
> +  }
> +
> +  /*
> +   * Neither DBCN or legacy extension were present.
> +   */
> +  return NumberOfBytes;
> +}
> +
> +/**
> +  Read data from serial device and save the datas in buffer.
> +
> +  Reads NumberOfBytes data bytes from a serial device into the buffer 
> + specified by Buffer. The number of bytes actually read is returned.
> +  If the return value is less than NumberOfBytes, then the rest operation failed.
> +  If Buffer is NULL, then ASSERT().
> +  If NumberOfBytes is zero, then return 0.
> +
> +  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
> +  @param  NumberOfBytes    The number of bytes which will be read.
> +
> +  @retval 0                Read data failed; No data is to be read.
> +  @retval >0               The actual number of bytes read from serial device.
> +
> +**/
> +UINTN
> +EFIAPI
> +SerialPortRead (
> +  OUT UINT8  *Buffer,
> +  IN  UINTN  NumberOfBytes
> +  )
> +{
> +  UINTN  Index;
> +
> +  Index = 0;
> +  while ((Index < NumberOfBytes) && SerialPortPoll ()) {
> +    Buffer[Index++] = (UINT8)mLastGetChar;
> +    mLastGetChar    = -1;
> +  }
> +
> +  return Index;
> +}
> +
> +/**
> +  Polls a serial device to see if there is any data waiting to be read.
> +
> +  Polls a serial device to see if there is any data waiting to be read.
> +  If there is data waiting to be read from the serial device, then TRUE is returned.
> +  If there is no data waiting to be read from the serial device, then FALSE is returned.
> +
> +  @retval TRUE             Data is waiting to be read from the serial device.
> +  @retval FALSE            There is no data waiting to be read from the serial device.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +SerialPortPoll (
> +  VOID
> +  )
> +{
> +  /*
> +   * Careful. OpenSBI with HTIF console will return -1 followed by -2
> +   * if there is no character received. So just check for values >= 0.
> +   */
> +
> +  if (mLastGetChar >= 0) {
> +    return TRUE;
> +  }
> +
> +  if (mHaveDbcn) {
> +    UINT8    Buffer;
> +    SBI_RET  Ret;
> +
> +    Ret = SbiCall (
> +            SBI_EXT_DBCN,
> +            SBI_EXT_DBCN_READ,
> +            3,
> +            1,
> +            ((UINTN)&Buffer),
> +            0
> +            );
> +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> +        (Ret.Value == 1))
> +    {
> +      mLastGetChar = Buffer;
> +    }
> +  } else if (mHaveLegacyGetchar) {
> +    mLastGetChar = (INT64)SbiCall (SBI_EXT_0_1_CONSOLE_GETCHAR, 0, 
> + 0).Error;  }
> +
> +  return mLastGetChar >= 0;
> +}
> +
> +/**
> +  Sets the control bits on a serial device.
> +
> +  @param Control                Sets the bits of Control that are settable.
> +
> +  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
> +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortSetControl (
> +  IN UINT32  Control
> +  )
> +{
> +  return RETURN_SUCCESS;
> +}
> +
> +/**
> +  Retrieve the status of the control bits on a serial device.
> +
> +  @param Control                A pointer to return the current control signals from the serial device.
> +
> +  @retval RETURN_SUCCESS        The control bits were read from the serial device.
> +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortGetControl (
> +  OUT UINT32  *Control
> +  )
> +{
> +  *Control = 0;
> +  return RETURN_SUCCESS;
> +}
> +
> +/**
> +  Sets the baud rate, receive FIFO depth, transmit/receice time out, 
> +parity,
> +  data bits, and stop bits on a serial device.
> +
> +  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
> +                            device's default interface speed.
> +                            On output, the value actually set.
> +  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
> +                            serial interface. A ReceiveFifoDepth value of 0 will use
> +                            the device's default FIFO depth.
> +                            On output, the value actually set.
> +  @param Timeout            The requested time out for a single character in microseconds.
> +                            This timeout applies to both the transmit and receive side of the
> +                            interface. A Timeout value of 0 will use the device's default time
> +                            out value.
> +                            On output, the value actually set.
> +  @param Parity             The type of parity to use on this serial device. A Parity value of
> +                            DefaultParity will use the device's default parity value.
> +                            On output, the value actually set.
> +  @param DataBits           The number of data bits to use on the serial device. A DataBits
> +                            vaule of 0 will use the device's default data bit setting.
> +                            On output, the value actually set.
> +  @param StopBits           The number of stop bits to use on this serial device. A StopBits
> +                            value of DefaultStopBits will use the device's default number of
> +                            stop bits.
> +                            On output, the value actually set.
> +
> +  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
> +  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
> +  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
> +  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortSetAttributes (
> +  IN OUT UINT64              *BaudRate,
> +  IN OUT UINT32              *ReceiveFifoDepth,
> +  IN OUT UINT32              *Timeout,
> +  IN OUT EFI_PARITY_TYPE     *Parity,
> +  IN OUT UINT8               *DataBits,
> +  IN OUT EFI_STOP_BITS_TYPE  *StopBits
> +  )
> +{
> +  return RETURN_SUCCESS;
> +}
> diff --git
> a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> bi.c
> b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> bi.c
> new file mode 100644
> index 000000000000..0ad5931be3ae
> --- /dev/null
> +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> +++ Li
> +++ bSbi.c
> @@ -0,0 +1,233 @@
> +/** @file
> +  Serial Port Library backed by SBI console.
> +
> +  Meant for SEC and PEI (XIP) environments.
> +
> +  Due to limitations of SBI console interface and XIP environments 
> + (on use of globals), this library instance does not implement 
> + reading  and polling the serial port. See 
> + PrePiDxeSerialPortLibRiscVSbi for  the full-featured variant meant for PrePi and DXE environments.
> +
> +  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include <Base.h>
> +#include <Library/SerialPortLib.h>
> +#include <Library/BaseRiscVSbiLib.h>
> +
> +/**
> +  Initialize the serial device hardware.
> +
> +  If no initialization is required, then return RETURN_SUCCESS.
> +  If the serial device was successfully initialized, then return RETURN_SUCCESS.
> +  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
> +
> +  @retval RETURN_SUCCESS        The serial device was initialized.
> +  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortInitialize (
> +  VOID
> +  )
> +{
> +  return RETURN_SUCCESS;
> +}
> +
> +/**
> +  Write data from buffer to serial device.
> +
> +  Writes NumberOfBytes data bytes from Buffer to the serial device.
> +  The number of bytes actually written to the serial device is returned.
> +  If the return value is less than NumberOfBytes, then the write operation failed.
> +  If Buffer is NULL, then ASSERT().
> +  If NumberOfBytes is zero, then return 0.
> +
> +  @param  Buffer           The pointer to the data buffer to be written.
> +  @param  NumberOfBytes    The number of bytes to written to the serial device.
> +
> +  @retval 0                NumberOfBytes is 0.
> +  @retval >0               The number of bytes written to the serial device.
> +                           If this value is less than NumberOfBytes, then the write operation failed.
> +
> +**/
> +UINTN
> +EFIAPI
> +SerialPortWrite (
> +  IN UINT8  *Buffer,
> +  IN UINTN  NumberOfBytes
> +  )
> +{
> +  SBI_RET  Ret;
> +  UINTN    Index;
> +
> +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, 
> + SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> +      (Ret.Value != 0))
> +  {
> +    Ret = SbiCall (
> +            SBI_EXT_DBCN,
> +            SBI_EXT_DBCN_WRITE,
> +            3,
> +            NumberOfBytes,
> +            ((UINTN)Buffer),
> +            0
> +            );
> +    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
> +      return 0;
> +    }
> +
> +    return Ret.Value;
> +  }
> +
> +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, 
> + SBI_EXT_0_1_CONSOLE_PUTCHAR);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> +      (Ret.Value != 0))
> +  {
> +    for (Index = 0; Index < NumberOfBytes; Index++) {
> +      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
> +    }
> +
> +    return Index;
> +  }
> +
> +  /*
> +   * Neither DBCN or legacy extension were present.
> +   */
> +  return NumberOfBytes;
> +}
> +
> +/**
> +  Read data from serial device and save the datas in buffer.
> +
> +  Reads NumberOfBytes data bytes from a serial device into the buffer 
> + specified by Buffer. The number of bytes actually read is returned.
> +  If the return value is less than NumberOfBytes, then the rest operation failed.
> +  If Buffer is NULL, then ASSERT().
> +  If NumberOfBytes is zero, then return 0.
> +
> +  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
> +  @param  NumberOfBytes    The number of bytes which will be read.
> +
> +  @retval 0                Read data failed; No data is to be read.
> +  @retval >0               The actual number of bytes read from serial device.
> +
> +**/
> +UINTN
> +EFIAPI
> +SerialPortRead (
> +  OUT UINT8  *Buffer,
> +  IN  UINTN  NumberOfBytes
> +  )
> +{
> +  return 0;
> +}
> +
> +/**
> +  Polls a serial device to see if there is any data waiting to be read.
> +
> +  Polls a serial device to see if there is any data waiting to be read.
> +  If there is data waiting to be read from the serial device, then TRUE is returned.
> +  If there is no data waiting to be read from the serial device, then FALSE is returned.
> +
> +  @retval TRUE             Data is waiting to be read from the serial device.
> +  @retval FALSE            There is no data waiting to be read from the serial device.
> +
> +**/
> +BOOLEAN
> +EFIAPI
> +SerialPortPoll (
> +  VOID
> +  )
> +{
> +  return FALSE;
> +}
> +
> +/**
> +  Sets the control bits on a serial device.
> +
> +  @param Control                Sets the bits of Control that are settable.
> +
> +  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
> +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortSetControl (
> +  IN UINT32  Control
> +  )
> +{
> +  return RETURN_SUCCESS;
> +}
> +
> +/**
> +  Retrieve the status of the control bits on a serial device.
> +
> +  @param Control                A pointer to return the current control signals from the serial device.
> +
> +  @retval RETURN_SUCCESS        The control bits were read from the serial device.
> +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortGetControl (
> +  OUT UINT32  *Control
> +  )
> +{
> +  *Control = 0;
> +  return RETURN_SUCCESS;
> +}
> +
> +/**
> +  Sets the baud rate, receive FIFO depth, transmit/receice time out, 
> +parity,
> +  data bits, and stop bits on a serial device.
> +
> +  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
> +                            device's default interface speed.
> +                            On output, the value actually set.
> +  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
> +                            serial interface. A ReceiveFifoDepth value of 0 will use
> +                            the device's default FIFO depth.
> +                            On output, the value actually set.
> +  @param Timeout            The requested time out for a single character in microseconds.
> +                            This timeout applies to both the transmit and receive side of the
> +                            interface. A Timeout value of 0 will use the device's default time
> +                            out value.
> +                            On output, the value actually set.
> +  @param Parity             The type of parity to use on this serial device. A Parity value of
> +                            DefaultParity will use the device's default parity value.
> +                            On output, the value actually set.
> +  @param DataBits           The number of data bits to use on the serial device. A DataBits
> +                            vaule of 0 will use the device's default data bit setting.
> +                            On output, the value actually set.
> +  @param StopBits           The number of stop bits to use on this serial device. A StopBits
> +                            value of DefaultStopBits will use the device's default number of
> +                            stop bits.
> +                            On output, the value actually set.
> +
> +  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
> +  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
> +  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
> +  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
> +
> +**/
> +RETURN_STATUS
> +EFIAPI
> +SerialPortSetAttributes (
> +  IN OUT UINT64              *BaudRate,
> +  IN OUT UINT32              *ReceiveFifoDepth,
> +  IN OUT UINT32              *Timeout,
> +  IN OUT EFI_PARITY_TYPE     *Parity,
> +  IN OUT UINT8               *DataBits,
> +  IN OUT EFI_STOP_BITS_TYPE  *StopBits
> +  )
> +{
> +  return RETURN_SUCCESS;
> +}
> diff --git
> a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> LibSbi.uni
> b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> LibSbi.uni
> new file mode 100644
> index 000000000000..7b22caa5a090
> --- /dev/null
> +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> +++ Po
> +++ rtLibSbi.uni
> @@ -0,0 +1,16 @@
> +// /** @file
> +// Serial Port Library backed by SBI console.
> +//
> +// Serial Port Library backed by SBI console.
> +//
> +// Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> // 
> +// SPDX-License-Identifier: BSD-2-Clause-Patent // // **/
> +
> +
> +#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
> +
> +#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
> +
> diff --git
> a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> bi.uni
> b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> bi.uni
> new file mode 100644
> index 000000000000..7b22caa5a090
> --- /dev/null
> +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> +++ LibSbi.uni
> @@ -0,0 +1,16 @@
> +// /** @file
> +// Serial Port Library backed by SBI console.
> +//
> +// Serial Port Library backed by SBI console.
> +//
> +// Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> // 
> +// SPDX-License-Identifier: BSD-2-Clause-Patent // // **/
> +
> +
> +#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
> +
> +#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
> +
> --
> 2.25.1







^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
  2023-03-17  5:44       ` Andrei Warkentin
@ 2023-03-27 16:32         ` Michael D Kinney
  2023-03-28 20:00           ` Andrei Warkentin
  0 siblings, 1 reply; 14+ messages in thread
From: Michael D Kinney @ 2023-03-27 16:32 UTC (permalink / raw)
  To: Warkentin, Andrei, devel@edk2.groups.io
  Cc: Gao, Liming, Liu, Zhiguang, Kinney, Michael D

I think that would still be called Sec.  Are there any issues with using Sec prefix?

Mike

> -----Original Message-----
> From: Warkentin, Andrei <andrei.warkentin@intel.com>
> Sent: Thursday, March 16, 2023 10:45 PM
> To: Kinney, Michael D <michael.d.kinney@intel.com>; devel@edk2.groups.io
> Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>
> Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
> 
> PrePi refers to a SEC-like stage in a Tiano implementation that skips the PEI stage. Reasons for skipping PEI usually center around
> memory initialization being done at an earlier stage of firmware initialization - for example, prior or part of TF-A firmware on
> AArch64 or prior or part of SBI implementation on RISC-V.
> 
> E.g. ArmVirtPkg/PrePi and OvmfPkg/RiscVVirt/Sec. The later is not a "SEC" in the send of handing off to PeiCore, but instead hands
> off to DxeCore.
> 
> There is no base type for PrePi. The type is still SEC. But it a) assumes RAM is available b) builds HOBs and hands-off to DXE.
> 
> The name has been around for at least half a decade now and making it a part of the SerialPortLib component name is important as it
> signifies the scenario where the code can apply.
> 
> A
> 
> -----Original Message-----
> From: Kinney, Michael D <michael.d.kinney@intel.com>
> Sent: Friday, March 17, 2023 12:01 AM
> To: Warkentin, Andrei <andrei.warkentin@intel.com>; devel@edk2.groups.io
> Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>
> Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
> 
> What is the definition of "PrePI"?  That is not a prefix I have seen used before.
> 
> Mike
> 
> > -----Original Message-----
> > From: Warkentin, Andrei <andrei.warkentin@intel.com>
> > Sent: Thursday, March 16, 2023 9:53 PM
> > To: devel@edk2.groups.io
> > Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming
> > <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>
> > Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for
> > RISC-V
> >
> > Hi folks,
> >
> > Can I get a review for the changes to MdePkg.dsc to include the new
> > libs on RISC-V? That's the minor thing preventing this patch set from being merged 😊.
> >
> > A
> >
> > -----Original Message-----
> > From: Warkentin, Andrei <andrei.warkentin@intel.com>
> > Sent: Friday, March 10, 2023 11:05 AM
> > To: devel@edk2.groups.io
> > Cc: Warkentin, Andrei <andrei.warkentin@intel.com>; Daniel Schaefer
> > <git@danielschaefer.me>; Sunil V L <sunilvl@ventanamicro.com>; Kinney,
> > Michael D <michael.d.kinney@intel.com>; Gao, Liming
> > <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>;
> > Gerd Hoffmann <kraxel@redhat.com>
> > Subject: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
> >
> > These are implementations of SerialPortLib using SBI console services.
> > - SecPeiRiscVSerialPortLibSbi is appropriate for SEC/PEI (XIP)
> > environments
> > - PrePiDxeRiscVSerialPortLibSbi is appropriate for PrePI/DXE
> > environments
> >
> > Tested with:
> > - Qemu RiscVVirt (non-DBCN case, backed by UART)
> > - TinyEMU + RiscVVirt (non-DBCN case, HTIF)
> > - TinyEMU + RiscVVirt (DBCN case, HTIF)
> >
> > Cc: Daniel Schaefer <git@danielschaefer.me>
> > Cc: Sunil V L <sunilvl@ventanamicro.com>
> > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > Cc: Liming Gao <gaoliming@byosoft.com.cn>
> > Cc: Zhiguang Liu <zhiguang.liu@intel.com>
> > Acked-by: Gerd Hoffmann <kraxel@redhat.com>
> > Signed-off-by: Andrei Warkentin <andrei.warkentin@intel.com>
> > ---
> >  MdePkg/MdePkg.dsc                                                              |   2 +
> >  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf |  36 +++
> >  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf     |  39 +++
> >  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c   | 288 ++++++++++++++++++++
> >  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c       | 233 ++++++++++++++++
> >  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni |  16 ++
> >  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni     |  16 ++
> >  7 files changed, 630 insertions(+)
> >
> > diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index
> > 0ac7618b4623..fb31349042fb 100644
> > --- a/MdePkg/MdePkg.dsc
> > +++ b/MdePkg/MdePkg.dsc
> > @@ -192,5 +192,7 @@ [Components.ARM, Components.AARCH64]
> >
> >  [Components.RISCV64]
> >    MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf
> > +
> > + MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> > + bi
> > + .inf
> > + MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> > + Li
> > + bSbi.inf
> >
> >  [BuildOptions]
> > diff --git
> > a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> > LibSbi.inf
> > b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> > LibSbi.inf
> > new file mode 100644
> > index 000000000000..1d8544756bb9
> > --- /dev/null
> > +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> > +++ Po
> > +++ rtLibSbi.inf
> > @@ -0,0 +1,36 @@
> > +## @file
> > +#  Serial Port Library backed by SBI console.
> > +#
> > +#  Meant for PrePi and DXE environments (where globals are allowed).
> > +See #  SecPeiSerialPortLibRiscVSbi for a reduced variant appropriate
> > +for SEC #  and PEI (XIP) environments.
> > +#
> > +#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> #
> > +#
> > +SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> > +
> > +[Defines]
> > +  INF_VERSION                    = 0x0001001B
> > +  BASE_NAME                      = PrePiDxeRiscVSerialPortLibSbi
> > +  MODULE_UNI_FILE                = PrePiDxeRiscVSerialPortLibSbi.uni
> > +  FILE_GUID                      = 872af743-ab56-45b4-a065-602567f4820c
> > +  MODULE_TYPE                    = BASE
> > +  VERSION_STRING                 = 1.0
> > +  LIBRARY_CLASS                  = SerialPortLib | SEC DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER UEFI_DRIVER UEFI_APPLICATION
> > +
> > +
> > +#
> > +#  VALID_ARCHITECTURES           = RISCV64
> > +#
> > +
> > +[Sources]
> > +  PrePiDxeRiscVSerialPortLibSbi.c
> > +
> > +[Packages]
> > +  MdePkg/MdePkg.dec
> > +
> > +[LibraryClasses]
> > +  RiscVSbiLib
> > diff --git
> > a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> > bi.inf
> > b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> > bi.inf
> > new file mode 100644
> > index 000000000000..7bc7cd47c11c
> > --- /dev/null
> > +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> > +++ Li
> > +++ bSbi.inf
> > @@ -0,0 +1,39 @@
> > +## @file
> > +#  Serial Port Library backed by SBI console.
> > +#
> > +#  Meant for SEC and PEI (XIP) environments.
> > +#
> > +#  Due to limitations of SBI console interface and XIP environments #
> > +(on use of globals), this library instance does not implement reading
> > +# and polling the serial port. See PrePiDxeSerialPortLibRiscVSbi for
> > +# the full-featured variant meant for PrePi and DXE environments.
> > +#
> > +#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> #
> > +#
> > +SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> > +
> > +[Defines]
> > +  INF_VERSION                    = 0x0001001B
> > +  BASE_NAME                      = SecPeiRiscVSerialPortLibSbi
> > +  MODULE_UNI_FILE                = SecPeiRiscVSerialPortLibSbi.uni
> > +  FILE_GUID                      = 639fad38-4bfd-4eb9-9f09-e97c7947d480
> > +  MODULE_TYPE                    = BASE
> > +  VERSION_STRING                 = 1.0
> > +  LIBRARY_CLASS                  = SerialPortLib | SEC PEI_CORE PEIM
> > +
> > +
> > +#
> > +#  VALID_ARCHITECTURES           = RISCV64
> > +#
> > +
> > +[Sources]
> > +  SecPeiRiscVSerialPortLibSbi.c
> > +
> > +[Packages]
> > +  MdePkg/MdePkg.dec
> > +
> > +[LibraryClasses]
> > +  RiscVSbiLib
> > diff --git
> > a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> > LibSbi.c
> > b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> > LibSbi.c
> > new file mode 100644
> > index 000000000000..058a6573ef2d
> > --- /dev/null
> > +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> > +++ Po
> > +++ rtLibSbi.c
> > @@ -0,0 +1,288 @@
> > +/** @file
> > +  Serial Port Library backed by SBI console.
> > +
> > +  Meant for PrePi and DXE environments (where globals are allowed).
> > + See SecPeiSerialPortLibRiscVSbi for a reduced variant appropriate
> > + for SEC and PEI (XIP) environments.
> > +
> > +  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> > +  SPDX-License-Identifier: BSD-2-Clause-Patent
> > +
> > +**/
> > +
> > +#include <Base.h>
> > +#include <Library/SerialPortLib.h>
> > +#include <Library/BaseRiscVSbiLib.h>
> > +
> > +STATIC BOOLEAN  mHaveDbcn          = FALSE;
> > +STATIC BOOLEAN  mHaveLegacyPutchar = FALSE; STATIC BOOLEAN
> > +mHaveLegacyGetchar = FALSE;
> > +STATIC INT64    mLastGetChar       = -1;
> > +
> > +/**
> > +  Initialize the serial device hardware.
> > +
> > +  If no initialization is required, then return RETURN_SUCCESS.
> > +  If the serial device was successfully initialized, then return RETURN_SUCCESS.
> > +  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
> > +
> > +  @retval RETURN_SUCCESS        The serial device was initialized.
> > +  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
> > +
> > +**/
> > +RETURN_STATUS
> > +EFIAPI
> > +SerialPortInitialize (
> > +  VOID
> > +  )
> > +{
> > +  SBI_RET  Ret;
> > +
> > +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1,
> > + SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > +      (Ret.Value != 0))
> > +  {
> > +    mHaveDbcn = TRUE;
> > +  } else {
> > +    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_PUTCHAR);
> > +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > +        (Ret.Value != 0))
> > +    {
> > +      mHaveLegacyPutchar = TRUE;
> > +    }
> > +
> > +    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_GETCHAR);
> > +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > +        (Ret.Value != 0))
> > +    {
> > +      mHaveLegacyGetchar = TRUE;
> > +    }
> > +  }
> > +
> > +  return RETURN_SUCCESS;
> > +}
> > +
> > +/**
> > +  Write data from buffer to serial device.
> > +
> > +  Writes NumberOfBytes data bytes from Buffer to the serial device.
> > +  The number of bytes actually written to the serial device is returned.
> > +  If the return value is less than NumberOfBytes, then the write operation failed.
> > +  If Buffer is NULL, then ASSERT().
> > +  If NumberOfBytes is zero, then return 0.
> > +
> > +  @param  Buffer           The pointer to the data buffer to be written.
> > +  @param  NumberOfBytes    The number of bytes to written to the serial device.
> > +
> > +  @retval 0                NumberOfBytes is 0.
> > +  @retval >0               The number of bytes written to the serial device.
> > +                           If this value is less than NumberOfBytes, then the write operation failed.
> > +
> > +**/
> > +UINTN
> > +EFIAPI
> > +SerialPortWrite (
> > +  IN UINT8  *Buffer,
> > +  IN UINTN  NumberOfBytes
> > +  )
> > +{
> > +  UINTN  Index;
> > +
> > +  if (mHaveDbcn) {
> > +    SBI_RET  Ret;
> > +    Ret = SbiCall (
> > +            SBI_EXT_DBCN,
> > +            SBI_EXT_DBCN_WRITE,
> > +            3,
> > +            NumberOfBytes,
> > +            ((UINTN)Buffer),
> > +            0
> > +            );
> > +    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
> > +      return 0;
> > +    }
> > +
> > +    return Ret.Value;
> > +  } else if (mHaveLegacyPutchar) {
> > +    for (Index = 0; Index < NumberOfBytes; Index++) {
> > +      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
> > +    }
> > +
> > +    return Index;
> > +  }
> > +
> > +  /*
> > +   * Neither DBCN or legacy extension were present.
> > +   */
> > +  return NumberOfBytes;
> > +}
> > +
> > +/**
> > +  Read data from serial device and save the datas in buffer.
> > +
> > +  Reads NumberOfBytes data bytes from a serial device into the buffer
> > + specified by Buffer. The number of bytes actually read is returned.
> > +  If the return value is less than NumberOfBytes, then the rest operation failed.
> > +  If Buffer is NULL, then ASSERT().
> > +  If NumberOfBytes is zero, then return 0.
> > +
> > +  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
> > +  @param  NumberOfBytes    The number of bytes which will be read.
> > +
> > +  @retval 0                Read data failed; No data is to be read.
> > +  @retval >0               The actual number of bytes read from serial device.
> > +
> > +**/
> > +UINTN
> > +EFIAPI
> > +SerialPortRead (
> > +  OUT UINT8  *Buffer,
> > +  IN  UINTN  NumberOfBytes
> > +  )
> > +{
> > +  UINTN  Index;
> > +
> > +  Index = 0;
> > +  while ((Index < NumberOfBytes) && SerialPortPoll ()) {
> > +    Buffer[Index++] = (UINT8)mLastGetChar;
> > +    mLastGetChar    = -1;
> > +  }
> > +
> > +  return Index;
> > +}
> > +
> > +/**
> > +  Polls a serial device to see if there is any data waiting to be read.
> > +
> > +  Polls a serial device to see if there is any data waiting to be read.
> > +  If there is data waiting to be read from the serial device, then TRUE is returned.
> > +  If there is no data waiting to be read from the serial device, then FALSE is returned.
> > +
> > +  @retval TRUE             Data is waiting to be read from the serial device.
> > +  @retval FALSE            There is no data waiting to be read from the serial device.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +SerialPortPoll (
> > +  VOID
> > +  )
> > +{
> > +  /*
> > +   * Careful. OpenSBI with HTIF console will return -1 followed by -2
> > +   * if there is no character received. So just check for values >= 0.
> > +   */
> > +
> > +  if (mLastGetChar >= 0) {
> > +    return TRUE;
> > +  }
> > +
> > +  if (mHaveDbcn) {
> > +    UINT8    Buffer;
> > +    SBI_RET  Ret;
> > +
> > +    Ret = SbiCall (
> > +            SBI_EXT_DBCN,
> > +            SBI_EXT_DBCN_READ,
> > +            3,
> > +            1,
> > +            ((UINTN)&Buffer),
> > +            0
> > +            );
> > +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > +        (Ret.Value == 1))
> > +    {
> > +      mLastGetChar = Buffer;
> > +    }
> > +  } else if (mHaveLegacyGetchar) {
> > +    mLastGetChar = (INT64)SbiCall (SBI_EXT_0_1_CONSOLE_GETCHAR, 0,
> > + 0).Error;  }
> > +
> > +  return mLastGetChar >= 0;
> > +}
> > +
> > +/**
> > +  Sets the control bits on a serial device.
> > +
> > +  @param Control                Sets the bits of Control that are settable.
> > +
> > +  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
> > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > +
> > +**/
> > +RETURN_STATUS
> > +EFIAPI
> > +SerialPortSetControl (
> > +  IN UINT32  Control
> > +  )
> > +{
> > +  return RETURN_SUCCESS;
> > +}
> > +
> > +/**
> > +  Retrieve the status of the control bits on a serial device.
> > +
> > +  @param Control                A pointer to return the current control signals from the serial device.
> > +
> > +  @retval RETURN_SUCCESS        The control bits were read from the serial device.
> > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > +
> > +**/
> > +RETURN_STATUS
> > +EFIAPI
> > +SerialPortGetControl (
> > +  OUT UINT32  *Control
> > +  )
> > +{
> > +  *Control = 0;
> > +  return RETURN_SUCCESS;
> > +}
> > +
> > +/**
> > +  Sets the baud rate, receive FIFO depth, transmit/receice time out,
> > +parity,
> > +  data bits, and stop bits on a serial device.
> > +
> > +  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
> > +                            device's default interface speed.
> > +                            On output, the value actually set.
> > +  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
> > +                            serial interface. A ReceiveFifoDepth value of 0 will use
> > +                            the device's default FIFO depth.
> > +                            On output, the value actually set.
> > +  @param Timeout            The requested time out for a single character in microseconds.
> > +                            This timeout applies to both the transmit and receive side of the
> > +                            interface. A Timeout value of 0 will use the device's default time
> > +                            out value.
> > +                            On output, the value actually set.
> > +  @param Parity             The type of parity to use on this serial device. A Parity value of
> > +                            DefaultParity will use the device's default parity value.
> > +                            On output, the value actually set.
> > +  @param DataBits           The number of data bits to use on the serial device. A DataBits
> > +                            vaule of 0 will use the device's default data bit setting.
> > +                            On output, the value actually set.
> > +  @param StopBits           The number of stop bits to use on this serial device. A StopBits
> > +                            value of DefaultStopBits will use the device's default number of
> > +                            stop bits.
> > +                            On output, the value actually set.
> > +
> > +  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
> > +  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
> > +  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
> > +  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
> > +
> > +**/
> > +RETURN_STATUS
> > +EFIAPI
> > +SerialPortSetAttributes (
> > +  IN OUT UINT64              *BaudRate,
> > +  IN OUT UINT32              *ReceiveFifoDepth,
> > +  IN OUT UINT32              *Timeout,
> > +  IN OUT EFI_PARITY_TYPE     *Parity,
> > +  IN OUT UINT8               *DataBits,
> > +  IN OUT EFI_STOP_BITS_TYPE  *StopBits
> > +  )
> > +{
> > +  return RETURN_SUCCESS;
> > +}
> > diff --git
> > a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> > bi.c
> > b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> > bi.c
> > new file mode 100644
> > index 000000000000..0ad5931be3ae
> > --- /dev/null
> > +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> > +++ Li
> > +++ bSbi.c
> > @@ -0,0 +1,233 @@
> > +/** @file
> > +  Serial Port Library backed by SBI console.
> > +
> > +  Meant for SEC and PEI (XIP) environments.
> > +
> > +  Due to limitations of SBI console interface and XIP environments
> > + (on use of globals), this library instance does not implement
> > + reading  and polling the serial port. See
> > + PrePiDxeSerialPortLibRiscVSbi for  the full-featured variant meant for PrePi and DXE environments.
> > +
> > +  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> > +  SPDX-License-Identifier: BSD-2-Clause-Patent
> > +
> > +**/
> > +
> > +#include <Base.h>
> > +#include <Library/SerialPortLib.h>
> > +#include <Library/BaseRiscVSbiLib.h>
> > +
> > +/**
> > +  Initialize the serial device hardware.
> > +
> > +  If no initialization is required, then return RETURN_SUCCESS.
> > +  If the serial device was successfully initialized, then return RETURN_SUCCESS.
> > +  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
> > +
> > +  @retval RETURN_SUCCESS        The serial device was initialized.
> > +  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
> > +
> > +**/
> > +RETURN_STATUS
> > +EFIAPI
> > +SerialPortInitialize (
> > +  VOID
> > +  )
> > +{
> > +  return RETURN_SUCCESS;
> > +}
> > +
> > +/**
> > +  Write data from buffer to serial device.
> > +
> > +  Writes NumberOfBytes data bytes from Buffer to the serial device.
> > +  The number of bytes actually written to the serial device is returned.
> > +  If the return value is less than NumberOfBytes, then the write operation failed.
> > +  If Buffer is NULL, then ASSERT().
> > +  If NumberOfBytes is zero, then return 0.
> > +
> > +  @param  Buffer           The pointer to the data buffer to be written.
> > +  @param  NumberOfBytes    The number of bytes to written to the serial device.
> > +
> > +  @retval 0                NumberOfBytes is 0.
> > +  @retval >0               The number of bytes written to the serial device.
> > +                           If this value is less than NumberOfBytes, then the write operation failed.
> > +
> > +**/
> > +UINTN
> > +EFIAPI
> > +SerialPortWrite (
> > +  IN UINT8  *Buffer,
> > +  IN UINTN  NumberOfBytes
> > +  )
> > +{
> > +  SBI_RET  Ret;
> > +  UINTN    Index;
> > +
> > +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1,
> > + SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > +      (Ret.Value != 0))
> > +  {
> > +    Ret = SbiCall (
> > +            SBI_EXT_DBCN,
> > +            SBI_EXT_DBCN_WRITE,
> > +            3,
> > +            NumberOfBytes,
> > +            ((UINTN)Buffer),
> > +            0
> > +            );
> > +    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
> > +      return 0;
> > +    }
> > +
> > +    return Ret.Value;
> > +  }
> > +
> > +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1,
> > + SBI_EXT_0_1_CONSOLE_PUTCHAR);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > +      (Ret.Value != 0))
> > +  {
> > +    for (Index = 0; Index < NumberOfBytes; Index++) {
> > +      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
> > +    }
> > +
> > +    return Index;
> > +  }
> > +
> > +  /*
> > +   * Neither DBCN or legacy extension were present.
> > +   */
> > +  return NumberOfBytes;
> > +}
> > +
> > +/**
> > +  Read data from serial device and save the datas in buffer.
> > +
> > +  Reads NumberOfBytes data bytes from a serial device into the buffer
> > + specified by Buffer. The number of bytes actually read is returned.
> > +  If the return value is less than NumberOfBytes, then the rest operation failed.
> > +  If Buffer is NULL, then ASSERT().
> > +  If NumberOfBytes is zero, then return 0.
> > +
> > +  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
> > +  @param  NumberOfBytes    The number of bytes which will be read.
> > +
> > +  @retval 0                Read data failed; No data is to be read.
> > +  @retval >0               The actual number of bytes read from serial device.
> > +
> > +**/
> > +UINTN
> > +EFIAPI
> > +SerialPortRead (
> > +  OUT UINT8  *Buffer,
> > +  IN  UINTN  NumberOfBytes
> > +  )
> > +{
> > +  return 0;
> > +}
> > +
> > +/**
> > +  Polls a serial device to see if there is any data waiting to be read.
> > +
> > +  Polls a serial device to see if there is any data waiting to be read.
> > +  If there is data waiting to be read from the serial device, then TRUE is returned.
> > +  If there is no data waiting to be read from the serial device, then FALSE is returned.
> > +
> > +  @retval TRUE             Data is waiting to be read from the serial device.
> > +  @retval FALSE            There is no data waiting to be read from the serial device.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +SerialPortPoll (
> > +  VOID
> > +  )
> > +{
> > +  return FALSE;
> > +}
> > +
> > +/**
> > +  Sets the control bits on a serial device.
> > +
> > +  @param Control                Sets the bits of Control that are settable.
> > +
> > +  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
> > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > +
> > +**/
> > +RETURN_STATUS
> > +EFIAPI
> > +SerialPortSetControl (
> > +  IN UINT32  Control
> > +  )
> > +{
> > +  return RETURN_SUCCESS;
> > +}
> > +
> > +/**
> > +  Retrieve the status of the control bits on a serial device.
> > +
> > +  @param Control                A pointer to return the current control signals from the serial device.
> > +
> > +  @retval RETURN_SUCCESS        The control bits were read from the serial device.
> > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > +
> > +**/
> > +RETURN_STATUS
> > +EFIAPI
> > +SerialPortGetControl (
> > +  OUT UINT32  *Control
> > +  )
> > +{
> > +  *Control = 0;
> > +  return RETURN_SUCCESS;
> > +}
> > +
> > +/**
> > +  Sets the baud rate, receive FIFO depth, transmit/receice time out,
> > +parity,
> > +  data bits, and stop bits on a serial device.
> > +
> > +  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
> > +                            device's default interface speed.
> > +                            On output, the value actually set.
> > +  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
> > +                            serial interface. A ReceiveFifoDepth value of 0 will use
> > +                            the device's default FIFO depth.
> > +                            On output, the value actually set.
> > +  @param Timeout            The requested time out for a single character in microseconds.
> > +                            This timeout applies to both the transmit and receive side of the
> > +                            interface. A Timeout value of 0 will use the device's default time
> > +                            out value.
> > +                            On output, the value actually set.
> > +  @param Parity             The type of parity to use on this serial device. A Parity value of
> > +                            DefaultParity will use the device's default parity value.
> > +                            On output, the value actually set.
> > +  @param DataBits           The number of data bits to use on the serial device. A DataBits
> > +                            vaule of 0 will use the device's default data bit setting.
> > +                            On output, the value actually set.
> > +  @param StopBits           The number of stop bits to use on this serial device. A StopBits
> > +                            value of DefaultStopBits will use the device's default number of
> > +                            stop bits.
> > +                            On output, the value actually set.
> > +
> > +  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
> > +  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
> > +  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
> > +  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
> > +
> > +**/
> > +RETURN_STATUS
> > +EFIAPI
> > +SerialPortSetAttributes (
> > +  IN OUT UINT64              *BaudRate,
> > +  IN OUT UINT32              *ReceiveFifoDepth,
> > +  IN OUT UINT32              *Timeout,
> > +  IN OUT EFI_PARITY_TYPE     *Parity,
> > +  IN OUT UINT8               *DataBits,
> > +  IN OUT EFI_STOP_BITS_TYPE  *StopBits
> > +  )
> > +{
> > +  return RETURN_SUCCESS;
> > +}
> > diff --git
> > a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> > LibSbi.uni
> > b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPort
> > LibSbi.uni
> > new file mode 100644
> > index 000000000000..7b22caa5a090
> > --- /dev/null
> > +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> > +++ Po
> > +++ rtLibSbi.uni
> > @@ -0,0 +1,16 @@
> > +// /** @file
> > +// Serial Port Library backed by SBI console.
> > +//
> > +// Serial Port Library backed by SBI console.
> > +//
> > +// Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> //
> > +// SPDX-License-Identifier: BSD-2-Clause-Patent // // **/
> > +
> > +
> > +#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
> > +
> > +#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
> > +
> > diff --git
> > a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> > bi.uni
> > b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibS
> > bi.uni
> > new file mode 100644
> > index 000000000000..7b22caa5a090
> > --- /dev/null
> > +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> > +++ LibSbi.uni
> > @@ -0,0 +1,16 @@
> > +// /** @file
> > +// Serial Port Library backed by SBI console.
> > +//
> > +// Serial Port Library backed by SBI console.
> > +//
> > +// Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> //
> > +// SPDX-License-Identifier: BSD-2-Clause-Patent // // **/
> > +
> > +
> > +#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
> > +
> > +#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
> > +
> > --
> > 2.25.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
  2023-03-27 16:32         ` Michael D Kinney
@ 2023-03-28 20:00           ` Andrei Warkentin
  2023-03-28 20:09             ` Michael D Kinney
  0 siblings, 1 reply; 14+ messages in thread
From: Andrei Warkentin @ 2023-03-28 20:00 UTC (permalink / raw)
  To: Kinney, Michael D, devel@edk2.groups.io; +Cc: Gao, Liming, Liu, Zhiguang

Sec isn't a good fit, because it has implications on the environment, which is why the contribution also includes a separate SecPei variant as well.

In naming, I followed other edk2 examples such as ./EmbeddedPkg/Library/PrePiExtractGuidedSectionLib and ./EmbeddedPkg/Library/PrePiMemoryAllocationLi. I would prefer not to move one of these libraries into EmbeddedPkg - that wouldn't make much sense to me (esp. since there's nothing "embedded" about the approach per se, it can be used on any implementation).

A
-----Original Message-----
From: Kinney, Michael D <michael.d.kinney@intel.com> 
Sent: Monday, March 27, 2023 11:32 AM
To: Warkentin, Andrei <andrei.warkentin@intel.com>; devel@edk2.groups.io
Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>
Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V

I think that would still be called Sec.  Are there any issues with using Sec prefix?

Mike

> -----Original Message-----
> From: Warkentin, Andrei <andrei.warkentin@intel.com>
> Sent: Thursday, March 16, 2023 10:45 PM
> To: Kinney, Michael D <michael.d.kinney@intel.com>; 
> devel@edk2.groups.io
> Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang 
> <zhiguang.liu@intel.com>
> Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for 
> RISC-V
> 
> PrePi refers to a SEC-like stage in a Tiano implementation that skips 
> the PEI stage. Reasons for skipping PEI usually center around memory 
> initialization being done at an earlier stage of firmware 
> initialization - for example, prior or part of TF-A firmware on
> AArch64 or prior or part of SBI implementation on RISC-V.
> 
> E.g. ArmVirtPkg/PrePi and OvmfPkg/RiscVVirt/Sec. The later is not a 
> "SEC" in the send of handing off to PeiCore, but instead hands off to DxeCore.
> 
> There is no base type for PrePi. The type is still SEC. But it a) assumes RAM is available b) builds HOBs and hands-off to DXE.
> 
> The name has been around for at least half a decade now and making it 
> a part of the SerialPortLib component name is important as it signifies the scenario where the code can apply.
> 
> A
> 
> -----Original Message-----
> From: Kinney, Michael D <michael.d.kinney@intel.com>
> Sent: Friday, March 17, 2023 12:01 AM
> To: Warkentin, Andrei <andrei.warkentin@intel.com>; 
> devel@edk2.groups.io
> Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang 
> <zhiguang.liu@intel.com>; Kinney, Michael D 
> <michael.d.kinney@intel.com>
> Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for 
> RISC-V
> 
> What is the definition of "PrePI"?  That is not a prefix I have seen used before.
> 
> Mike
> 
> > -----Original Message-----
> > From: Warkentin, Andrei <andrei.warkentin@intel.com>
> > Sent: Thursday, March 16, 2023 9:53 PM
> > To: devel@edk2.groups.io
> > Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming 
> > <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>
> > Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for 
> > RISC-V
> >
> > Hi folks,
> >
> > Can I get a review for the changes to MdePkg.dsc to include the new 
> > libs on RISC-V? That's the minor thing preventing this patch set from being merged 😊.
> >
> > A
> >
> > -----Original Message-----
> > From: Warkentin, Andrei <andrei.warkentin@intel.com>
> > Sent: Friday, March 10, 2023 11:05 AM
> > To: devel@edk2.groups.io
> > Cc: Warkentin, Andrei <andrei.warkentin@intel.com>; Daniel Schaefer 
> > <git@danielschaefer.me>; Sunil V L <sunilvl@ventanamicro.com>; 
> > Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming 
> > <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>; 
> > Gerd Hoffmann <kraxel@redhat.com>
> > Subject: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for 
> > RISC-V
> >
> > These are implementations of SerialPortLib using SBI console services.
> > - SecPeiRiscVSerialPortLibSbi is appropriate for SEC/PEI (XIP) 
> > environments
> > - PrePiDxeRiscVSerialPortLibSbi is appropriate for PrePI/DXE 
> > environments
> >
> > Tested with:
> > - Qemu RiscVVirt (non-DBCN case, backed by UART)
> > - TinyEMU + RiscVVirt (non-DBCN case, HTIF)
> > - TinyEMU + RiscVVirt (DBCN case, HTIF)
> >
> > Cc: Daniel Schaefer <git@danielschaefer.me>
> > Cc: Sunil V L <sunilvl@ventanamicro.com>
> > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > Cc: Liming Gao <gaoliming@byosoft.com.cn>
> > Cc: Zhiguang Liu <zhiguang.liu@intel.com>
> > Acked-by: Gerd Hoffmann <kraxel@redhat.com>
> > Signed-off-by: Andrei Warkentin <andrei.warkentin@intel.com>
> > ---
> >  MdePkg/MdePkg.dsc                                                              |   2 +
> >  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf |  36 +++
> >  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf     |  39 +++
> >  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c   | 288 ++++++++++++++++++++
> >  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c       | 233 ++++++++++++++++
> >  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni |  16 ++
> >  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni     |  16 ++
> >  7 files changed, 630 insertions(+)
> >
> > diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index 
> > 0ac7618b4623..fb31349042fb 100644
> > --- a/MdePkg/MdePkg.dsc
> > +++ b/MdePkg/MdePkg.dsc
> > @@ -192,5 +192,7 @@ [Components.ARM, Components.AARCH64]
> >
> >  [Components.RISCV64]
> >    MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf
> > +
> > + MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLi
> > + bS
> > + bi
> > + .inf
> > + MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
> > + rt
> > + Li
> > + bSbi.inf
> >
> >  [BuildOptions]
> > diff --git
> > a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
> > rt
> > LibSbi.inf
> > b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
> > rt
> > LibSbi.inf
> > new file mode 100644
> > index 000000000000..1d8544756bb9
> > --- /dev/null
> > +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSeri
> > +++ al
> > +++ Po
> > +++ rtLibSbi.inf
> > @@ -0,0 +1,36 @@
> > +## @file
> > +#  Serial Port Library backed by SBI console.
> > +#
> > +#  Meant for PrePi and DXE environments (where globals are allowed).
> > +See #  SecPeiSerialPortLibRiscVSbi for a reduced variant 
> > +appropriate for SEC #  and PEI (XIP) environments.
> > +#
> > +#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> 
> > +# #
> > +SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> > +
> > +[Defines]
> > +  INF_VERSION                    = 0x0001001B
> > +  BASE_NAME                      = PrePiDxeRiscVSerialPortLibSbi
> > +  MODULE_UNI_FILE                = PrePiDxeRiscVSerialPortLibSbi.uni
> > +  FILE_GUID                      = 872af743-ab56-45b4-a065-602567f4820c
> > +  MODULE_TYPE                    = BASE
> > +  VERSION_STRING                 = 1.0
> > +  LIBRARY_CLASS                  = SerialPortLib | SEC DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER UEFI_DRIVER UEFI_APPLICATION
> > +
> > +
> > +#
> > +#  VALID_ARCHITECTURES           = RISCV64
> > +#
> > +
> > +[Sources]
> > +  PrePiDxeRiscVSerialPortLibSbi.c
> > +
> > +[Packages]
> > +  MdePkg/MdePkg.dec
> > +
> > +[LibraryClasses]
> > +  RiscVSbiLib
> > diff --git
> > a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLi
> > bS
> > bi.inf
> > b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLi
> > bS
> > bi.inf
> > new file mode 100644
> > index 000000000000..7bc7cd47c11c
> > --- /dev/null
> > +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPo
> > +++ rt
> > +++ Li
> > +++ bSbi.inf
> > @@ -0,0 +1,39 @@
> > +## @file
> > +#  Serial Port Library backed by SBI console.
> > +#
> > +#  Meant for SEC and PEI (XIP) environments.
> > +#
> > +#  Due to limitations of SBI console interface and XIP environments 
> > +# (on use of globals), this library instance does not implement 
> > +reading # and polling the serial port. See 
> > +PrePiDxeSerialPortLibRiscVSbi for # the full-featured variant meant for PrePi and DXE environments.
> > +#
> > +#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> 
> > +# #
> > +SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> > +
> > +[Defines]
> > +  INF_VERSION                    = 0x0001001B
> > +  BASE_NAME                      = SecPeiRiscVSerialPortLibSbi
> > +  MODULE_UNI_FILE                = SecPeiRiscVSerialPortLibSbi.uni
> > +  FILE_GUID                      = 639fad38-4bfd-4eb9-9f09-e97c7947d480
> > +  MODULE_TYPE                    = BASE
> > +  VERSION_STRING                 = 1.0
> > +  LIBRARY_CLASS                  = SerialPortLib | SEC PEI_CORE PEIM
> > +
> > +
> > +#
> > +#  VALID_ARCHITECTURES           = RISCV64
> > +#
> > +
> > +[Sources]
> > +  SecPeiRiscVSerialPortLibSbi.c
> > +
> > +[Packages]
> > +  MdePkg/MdePkg.dec
> > +
> > +[LibraryClasses]
> > +  RiscVSbiLib
> > diff --git
> > a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
> > rt
> > LibSbi.c
> > b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
> > rt
> > LibSbi.c
> > new file mode 100644
> > index 000000000000..058a6573ef2d
> > --- /dev/null
> > +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSeri
> > +++ al
> > +++ Po
> > +++ rtLibSbi.c
> > @@ -0,0 +1,288 @@
> > +/** @file
> > +  Serial Port Library backed by SBI console.
> > +
> > +  Meant for PrePi and DXE environments (where globals are allowed).
> > + See SecPeiSerialPortLibRiscVSbi for a reduced variant appropriate 
> > + for SEC and PEI (XIP) environments.
> > +
> > +  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> > +  SPDX-License-Identifier: BSD-2-Clause-Patent
> > +
> > +**/
> > +
> > +#include <Base.h>
> > +#include <Library/SerialPortLib.h>
> > +#include <Library/BaseRiscVSbiLib.h>
> > +
> > +STATIC BOOLEAN  mHaveDbcn          = FALSE;
> > +STATIC BOOLEAN  mHaveLegacyPutchar = FALSE; STATIC BOOLEAN 
> > +mHaveLegacyGetchar = FALSE;
> > +STATIC INT64    mLastGetChar       = -1;
> > +
> > +/**
> > +  Initialize the serial device hardware.
> > +
> > +  If no initialization is required, then return RETURN_SUCCESS.
> > +  If the serial device was successfully initialized, then return RETURN_SUCCESS.
> > +  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
> > +
> > +  @retval RETURN_SUCCESS        The serial device was initialized.
> > +  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
> > +
> > +**/
> > +RETURN_STATUS
> > +EFIAPI
> > +SerialPortInitialize (
> > +  VOID
> > +  )
> > +{
> > +  SBI_RET  Ret;
> > +
> > +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, 
> > + SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > +      (Ret.Value != 0))
> > +  {
> > +    mHaveDbcn = TRUE;
> > +  } else {
> > +    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_PUTCHAR);
> > +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > +        (Ret.Value != 0))
> > +    {
> > +      mHaveLegacyPutchar = TRUE;
> > +    }
> > +
> > +    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_GETCHAR);
> > +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > +        (Ret.Value != 0))
> > +    {
> > +      mHaveLegacyGetchar = TRUE;
> > +    }
> > +  }
> > +
> > +  return RETURN_SUCCESS;
> > +}
> > +
> > +/**
> > +  Write data from buffer to serial device.
> > +
> > +  Writes NumberOfBytes data bytes from Buffer to the serial device.
> > +  The number of bytes actually written to the serial device is returned.
> > +  If the return value is less than NumberOfBytes, then the write operation failed.
> > +  If Buffer is NULL, then ASSERT().
> > +  If NumberOfBytes is zero, then return 0.
> > +
> > +  @param  Buffer           The pointer to the data buffer to be written.
> > +  @param  NumberOfBytes    The number of bytes to written to the serial device.
> > +
> > +  @retval 0                NumberOfBytes is 0.
> > +  @retval >0               The number of bytes written to the serial device.
> > +                           If this value is less than NumberOfBytes, then the write operation failed.
> > +
> > +**/
> > +UINTN
> > +EFIAPI
> > +SerialPortWrite (
> > +  IN UINT8  *Buffer,
> > +  IN UINTN  NumberOfBytes
> > +  )
> > +{
> > +  UINTN  Index;
> > +
> > +  if (mHaveDbcn) {
> > +    SBI_RET  Ret;
> > +    Ret = SbiCall (
> > +            SBI_EXT_DBCN,
> > +            SBI_EXT_DBCN_WRITE,
> > +            3,
> > +            NumberOfBytes,
> > +            ((UINTN)Buffer),
> > +            0
> > +            );
> > +    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
> > +      return 0;
> > +    }
> > +
> > +    return Ret.Value;
> > +  } else if (mHaveLegacyPutchar) {
> > +    for (Index = 0; Index < NumberOfBytes; Index++) {
> > +      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
> > +    }
> > +
> > +    return Index;
> > +  }
> > +
> > +  /*
> > +   * Neither DBCN or legacy extension were present.
> > +   */
> > +  return NumberOfBytes;
> > +}
> > +
> > +/**
> > +  Read data from serial device and save the datas in buffer.
> > +
> > +  Reads NumberOfBytes data bytes from a serial device into the 
> > + buffer specified by Buffer. The number of bytes actually read is returned.
> > +  If the return value is less than NumberOfBytes, then the rest operation failed.
> > +  If Buffer is NULL, then ASSERT().
> > +  If NumberOfBytes is zero, then return 0.
> > +
> > +  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
> > +  @param  NumberOfBytes    The number of bytes which will be read.
> > +
> > +  @retval 0                Read data failed; No data is to be read.
> > +  @retval >0               The actual number of bytes read from serial device.
> > +
> > +**/
> > +UINTN
> > +EFIAPI
> > +SerialPortRead (
> > +  OUT UINT8  *Buffer,
> > +  IN  UINTN  NumberOfBytes
> > +  )
> > +{
> > +  UINTN  Index;
> > +
> > +  Index = 0;
> > +  while ((Index < NumberOfBytes) && SerialPortPoll ()) {
> > +    Buffer[Index++] = (UINT8)mLastGetChar;
> > +    mLastGetChar    = -1;
> > +  }
> > +
> > +  return Index;
> > +}
> > +
> > +/**
> > +  Polls a serial device to see if there is any data waiting to be read.
> > +
> > +  Polls a serial device to see if there is any data waiting to be read.
> > +  If there is data waiting to be read from the serial device, then TRUE is returned.
> > +  If there is no data waiting to be read from the serial device, then FALSE is returned.
> > +
> > +  @retval TRUE             Data is waiting to be read from the serial device.
> > +  @retval FALSE            There is no data waiting to be read from the serial device.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +SerialPortPoll (
> > +  VOID
> > +  )
> > +{
> > +  /*
> > +   * Careful. OpenSBI with HTIF console will return -1 followed by -2
> > +   * if there is no character received. So just check for values >= 0.
> > +   */
> > +
> > +  if (mLastGetChar >= 0) {
> > +    return TRUE;
> > +  }
> > +
> > +  if (mHaveDbcn) {
> > +    UINT8    Buffer;
> > +    SBI_RET  Ret;
> > +
> > +    Ret = SbiCall (
> > +            SBI_EXT_DBCN,
> > +            SBI_EXT_DBCN_READ,
> > +            3,
> > +            1,
> > +            ((UINTN)&Buffer),
> > +            0
> > +            );
> > +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > +        (Ret.Value == 1))
> > +    {
> > +      mLastGetChar = Buffer;
> > +    }
> > +  } else if (mHaveLegacyGetchar) {
> > +    mLastGetChar = (INT64)SbiCall (SBI_EXT_0_1_CONSOLE_GETCHAR, 0, 
> > + 0).Error;  }
> > +
> > +  return mLastGetChar >= 0;
> > +}
> > +
> > +/**
> > +  Sets the control bits on a serial device.
> > +
> > +  @param Control                Sets the bits of Control that are settable.
> > +
> > +  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
> > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > +
> > +**/
> > +RETURN_STATUS
> > +EFIAPI
> > +SerialPortSetControl (
> > +  IN UINT32  Control
> > +  )
> > +{
> > +  return RETURN_SUCCESS;
> > +}
> > +
> > +/**
> > +  Retrieve the status of the control bits on a serial device.
> > +
> > +  @param Control                A pointer to return the current control signals from the serial device.
> > +
> > +  @retval RETURN_SUCCESS        The control bits were read from the serial device.
> > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > +
> > +**/
> > +RETURN_STATUS
> > +EFIAPI
> > +SerialPortGetControl (
> > +  OUT UINT32  *Control
> > +  )
> > +{
> > +  *Control = 0;
> > +  return RETURN_SUCCESS;
> > +}
> > +
> > +/**
> > +  Sets the baud rate, receive FIFO depth, transmit/receice time 
> > +out, parity,
> > +  data bits, and stop bits on a serial device.
> > +
> > +  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
> > +                            device's default interface speed.
> > +                            On output, the value actually set.
> > +  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
> > +                            serial interface. A ReceiveFifoDepth value of 0 will use
> > +                            the device's default FIFO depth.
> > +                            On output, the value actually set.
> > +  @param Timeout            The requested time out for a single character in microseconds.
> > +                            This timeout applies to both the transmit and receive side of the
> > +                            interface. A Timeout value of 0 will use the device's default time
> > +                            out value.
> > +                            On output, the value actually set.
> > +  @param Parity             The type of parity to use on this serial device. A Parity value of
> > +                            DefaultParity will use the device's default parity value.
> > +                            On output, the value actually set.
> > +  @param DataBits           The number of data bits to use on the serial device. A DataBits
> > +                            vaule of 0 will use the device's default data bit setting.
> > +                            On output, the value actually set.
> > +  @param StopBits           The number of stop bits to use on this serial device. A StopBits
> > +                            value of DefaultStopBits will use the device's default number of
> > +                            stop bits.
> > +                            On output, the value actually set.
> > +
> > +  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
> > +  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
> > +  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
> > +  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
> > +
> > +**/
> > +RETURN_STATUS
> > +EFIAPI
> > +SerialPortSetAttributes (
> > +  IN OUT UINT64              *BaudRate,
> > +  IN OUT UINT32              *ReceiveFifoDepth,
> > +  IN OUT UINT32              *Timeout,
> > +  IN OUT EFI_PARITY_TYPE     *Parity,
> > +  IN OUT UINT8               *DataBits,
> > +  IN OUT EFI_STOP_BITS_TYPE  *StopBits
> > +  )
> > +{
> > +  return RETURN_SUCCESS;
> > +}
> > diff --git
> > a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLi
> > bS
> > bi.c
> > b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLi
> > bS
> > bi.c
> > new file mode 100644
> > index 000000000000..0ad5931be3ae
> > --- /dev/null
> > +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPo
> > +++ rt
> > +++ Li
> > +++ bSbi.c
> > @@ -0,0 +1,233 @@
> > +/** @file
> > +  Serial Port Library backed by SBI console.
> > +
> > +  Meant for SEC and PEI (XIP) environments.
> > +
> > +  Due to limitations of SBI console interface and XIP environments 
> > + (on use of globals), this library instance does not implement 
> > + reading  and polling the serial port. See 
> > + PrePiDxeSerialPortLibRiscVSbi for  the full-featured variant meant for PrePi and DXE environments.
> > +
> > +  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> > +  SPDX-License-Identifier: BSD-2-Clause-Patent
> > +
> > +**/
> > +
> > +#include <Base.h>
> > +#include <Library/SerialPortLib.h>
> > +#include <Library/BaseRiscVSbiLib.h>
> > +
> > +/**
> > +  Initialize the serial device hardware.
> > +
> > +  If no initialization is required, then return RETURN_SUCCESS.
> > +  If the serial device was successfully initialized, then return RETURN_SUCCESS.
> > +  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
> > +
> > +  @retval RETURN_SUCCESS        The serial device was initialized.
> > +  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
> > +
> > +**/
> > +RETURN_STATUS
> > +EFIAPI
> > +SerialPortInitialize (
> > +  VOID
> > +  )
> > +{
> > +  return RETURN_SUCCESS;
> > +}
> > +
> > +/**
> > +  Write data from buffer to serial device.
> > +
> > +  Writes NumberOfBytes data bytes from Buffer to the serial device.
> > +  The number of bytes actually written to the serial device is returned.
> > +  If the return value is less than NumberOfBytes, then the write operation failed.
> > +  If Buffer is NULL, then ASSERT().
> > +  If NumberOfBytes is zero, then return 0.
> > +
> > +  @param  Buffer           The pointer to the data buffer to be written.
> > +  @param  NumberOfBytes    The number of bytes to written to the serial device.
> > +
> > +  @retval 0                NumberOfBytes is 0.
> > +  @retval >0               The number of bytes written to the serial device.
> > +                           If this value is less than NumberOfBytes, then the write operation failed.
> > +
> > +**/
> > +UINTN
> > +EFIAPI
> > +SerialPortWrite (
> > +  IN UINT8  *Buffer,
> > +  IN UINTN  NumberOfBytes
> > +  )
> > +{
> > +  SBI_RET  Ret;
> > +  UINTN    Index;
> > +
> > +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, 
> > + SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > +      (Ret.Value != 0))
> > +  {
> > +    Ret = SbiCall (
> > +            SBI_EXT_DBCN,
> > +            SBI_EXT_DBCN_WRITE,
> > +            3,
> > +            NumberOfBytes,
> > +            ((UINTN)Buffer),
> > +            0
> > +            );
> > +    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
> > +      return 0;
> > +    }
> > +
> > +    return Ret.Value;
> > +  }
> > +
> > +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, 
> > + SBI_EXT_0_1_CONSOLE_PUTCHAR);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > +      (Ret.Value != 0))
> > +  {
> > +    for (Index = 0; Index < NumberOfBytes; Index++) {
> > +      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
> > +    }
> > +
> > +    return Index;
> > +  }
> > +
> > +  /*
> > +   * Neither DBCN or legacy extension were present.
> > +   */
> > +  return NumberOfBytes;
> > +}
> > +
> > +/**
> > +  Read data from serial device and save the datas in buffer.
> > +
> > +  Reads NumberOfBytes data bytes from a serial device into the 
> > + buffer specified by Buffer. The number of bytes actually read is returned.
> > +  If the return value is less than NumberOfBytes, then the rest operation failed.
> > +  If Buffer is NULL, then ASSERT().
> > +  If NumberOfBytes is zero, then return 0.
> > +
> > +  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
> > +  @param  NumberOfBytes    The number of bytes which will be read.
> > +
> > +  @retval 0                Read data failed; No data is to be read.
> > +  @retval >0               The actual number of bytes read from serial device.
> > +
> > +**/
> > +UINTN
> > +EFIAPI
> > +SerialPortRead (
> > +  OUT UINT8  *Buffer,
> > +  IN  UINTN  NumberOfBytes
> > +  )
> > +{
> > +  return 0;
> > +}
> > +
> > +/**
> > +  Polls a serial device to see if there is any data waiting to be read.
> > +
> > +  Polls a serial device to see if there is any data waiting to be read.
> > +  If there is data waiting to be read from the serial device, then TRUE is returned.
> > +  If there is no data waiting to be read from the serial device, then FALSE is returned.
> > +
> > +  @retval TRUE             Data is waiting to be read from the serial device.
> > +  @retval FALSE            There is no data waiting to be read from the serial device.
> > +
> > +**/
> > +BOOLEAN
> > +EFIAPI
> > +SerialPortPoll (
> > +  VOID
> > +  )
> > +{
> > +  return FALSE;
> > +}
> > +
> > +/**
> > +  Sets the control bits on a serial device.
> > +
> > +  @param Control                Sets the bits of Control that are settable.
> > +
> > +  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
> > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > +
> > +**/
> > +RETURN_STATUS
> > +EFIAPI
> > +SerialPortSetControl (
> > +  IN UINT32  Control
> > +  )
> > +{
> > +  return RETURN_SUCCESS;
> > +}
> > +
> > +/**
> > +  Retrieve the status of the control bits on a serial device.
> > +
> > +  @param Control                A pointer to return the current control signals from the serial device.
> > +
> > +  @retval RETURN_SUCCESS        The control bits were read from the serial device.
> > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > +
> > +**/
> > +RETURN_STATUS
> > +EFIAPI
> > +SerialPortGetControl (
> > +  OUT UINT32  *Control
> > +  )
> > +{
> > +  *Control = 0;
> > +  return RETURN_SUCCESS;
> > +}
> > +
> > +/**
> > +  Sets the baud rate, receive FIFO depth, transmit/receice time 
> > +out, parity,
> > +  data bits, and stop bits on a serial device.
> > +
> > +  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
> > +                            device's default interface speed.
> > +                            On output, the value actually set.
> > +  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
> > +                            serial interface. A ReceiveFifoDepth value of 0 will use
> > +                            the device's default FIFO depth.
> > +                            On output, the value actually set.
> > +  @param Timeout            The requested time out for a single character in microseconds.
> > +                            This timeout applies to both the transmit and receive side of the
> > +                            interface. A Timeout value of 0 will use the device's default time
> > +                            out value.
> > +                            On output, the value actually set.
> > +  @param Parity             The type of parity to use on this serial device. A Parity value of
> > +                            DefaultParity will use the device's default parity value.
> > +                            On output, the value actually set.
> > +  @param DataBits           The number of data bits to use on the serial device. A DataBits
> > +                            vaule of 0 will use the device's default data bit setting.
> > +                            On output, the value actually set.
> > +  @param StopBits           The number of stop bits to use on this serial device. A StopBits
> > +                            value of DefaultStopBits will use the device's default number of
> > +                            stop bits.
> > +                            On output, the value actually set.
> > +
> > +  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
> > +  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
> > +  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
> > +  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
> > +
> > +**/
> > +RETURN_STATUS
> > +EFIAPI
> > +SerialPortSetAttributes (
> > +  IN OUT UINT64              *BaudRate,
> > +  IN OUT UINT32              *ReceiveFifoDepth,
> > +  IN OUT UINT32              *Timeout,
> > +  IN OUT EFI_PARITY_TYPE     *Parity,
> > +  IN OUT UINT8               *DataBits,
> > +  IN OUT EFI_STOP_BITS_TYPE  *StopBits
> > +  )
> > +{
> > +  return RETURN_SUCCESS;
> > +}
> > diff --git
> > a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
> > rt
> > LibSbi.uni
> > b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
> > rt
> > LibSbi.uni
> > new file mode 100644
> > index 000000000000..7b22caa5a090
> > --- /dev/null
> > +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSeri
> > +++ al
> > +++ Po
> > +++ rtLibSbi.uni
> > @@ -0,0 +1,16 @@
> > +// /** @file
> > +// Serial Port Library backed by SBI console.
> > +//
> > +// Serial Port Library backed by SBI console.
> > +//
> > +// Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> 
> > +// // SPDX-License-Identifier: BSD-2-Clause-Patent // // **/
> > +
> > +
> > +#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
> > +
> > +#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
> > +
> > diff --git
> > a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLi
> > bS
> > bi.uni
> > b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLi
> > bS
> > bi.uni
> > new file mode 100644
> > index 000000000000..7b22caa5a090
> > --- /dev/null
> > +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPo
> > +++ rt
> > +++ LibSbi.uni
> > @@ -0,0 +1,16 @@
> > +// /** @file
> > +// Serial Port Library backed by SBI console.
> > +//
> > +// Serial Port Library backed by SBI console.
> > +//
> > +// Copyright (c) 2023, Intel Corporation. All rights reserved.<BR> 
> > +// // SPDX-License-Identifier: BSD-2-Clause-Patent // // **/
> > +
> > +
> > +#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
> > +
> > +#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
> > +
> > --
> > 2.25.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
  2023-03-28 20:00           ` Andrei Warkentin
@ 2023-03-28 20:09             ` Michael D Kinney
  2023-03-28 20:20               ` Andrei Warkentin
  0 siblings, 1 reply; 14+ messages in thread
From: Michael D Kinney @ 2023-03-28 20:09 UTC (permalink / raw)
  To: Warkentin, Andrei, devel@edk2.groups.io
  Cc: Gao, Liming, Liu, Zhiguang, Kinney, Michael D

I agree that moving into EmbeddedPkg does not make sense.

EmbeddedPkg is very old without many updates.  That package is not the best
to use for examples.

I think you are suggesting that there is a difference in a library that runs
in an SEC env with intent to hand off to PEI and a library that runs in an
SEC env with intent to handoff to DXE.  They are both SEC module types and the
execution environment is the same.  The only difference is how they prepare
for the handoff.

My interest here is to make sure the prefixes are used consistently and if
we truly have a new prefix type, then to clearly define it so it can be
used consistently going forward.

Mike 

> -----Original Message-----
> From: Warkentin, Andrei <andrei.warkentin@intel.com>
> Sent: Tuesday, March 28, 2023 1:00 PM
> To: Kinney, Michael D <michael.d.kinney@intel.com>; devel@edk2.groups.io
> Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>
> Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
> 
> Sec isn't a good fit, because it has implications on the environment, which is why the contribution also includes a separate SecPei
> variant as well.
> 
> In naming, I followed other edk2 examples such as ./EmbeddedPkg/Library/PrePiExtractGuidedSectionLib and
> ./EmbeddedPkg/Library/PrePiMemoryAllocationLi. I would prefer not to move one of these libraries into EmbeddedPkg - that wouldn't
> make much sense to me (esp. since there's nothing "embedded" about the approach per se, it can be used on any implementation).
> 
> A
> -----Original Message-----
> From: Kinney, Michael D <michael.d.kinney@intel.com>
> Sent: Monday, March 27, 2023 11:32 AM
> To: Warkentin, Andrei <andrei.warkentin@intel.com>; devel@edk2.groups.io
> Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>
> Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
> 
> I think that would still be called Sec.  Are there any issues with using Sec prefix?
> 
> Mike
> 
> > -----Original Message-----
> > From: Warkentin, Andrei <andrei.warkentin@intel.com>
> > Sent: Thursday, March 16, 2023 10:45 PM
> > To: Kinney, Michael D <michael.d.kinney@intel.com>;
> > devel@edk2.groups.io
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang
> > <zhiguang.liu@intel.com>
> > Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for
> > RISC-V
> >
> > PrePi refers to a SEC-like stage in a Tiano implementation that skips
> > the PEI stage. Reasons for skipping PEI usually center around memory
> > initialization being done at an earlier stage of firmware
> > initialization - for example, prior or part of TF-A firmware on
> > AArch64 or prior or part of SBI implementation on RISC-V.
> >
> > E.g. ArmVirtPkg/PrePi and OvmfPkg/RiscVVirt/Sec. The later is not a
> > "SEC" in the send of handing off to PeiCore, but instead hands off to DxeCore.
> >
> > There is no base type for PrePi. The type is still SEC. But it a) assumes RAM is available b) builds HOBs and hands-off to DXE.
> >
> > The name has been around for at least half a decade now and making it
> > a part of the SerialPortLib component name is important as it signifies the scenario where the code can apply.
> >
> > A
> >
> > -----Original Message-----
> > From: Kinney, Michael D <michael.d.kinney@intel.com>
> > Sent: Friday, March 17, 2023 12:01 AM
> > To: Warkentin, Andrei <andrei.warkentin@intel.com>;
> > devel@edk2.groups.io
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang
> > <zhiguang.liu@intel.com>; Kinney, Michael D
> > <michael.d.kinney@intel.com>
> > Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for
> > RISC-V
> >
> > What is the definition of "PrePI"?  That is not a prefix I have seen used before.
> >
> > Mike
> >
> > > -----Original Message-----
> > > From: Warkentin, Andrei <andrei.warkentin@intel.com>
> > > Sent: Thursday, March 16, 2023 9:53 PM
> > > To: devel@edk2.groups.io
> > > Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming
> > > <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>
> > > Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for
> > > RISC-V
> > >
> > > Hi folks,
> > >
> > > Can I get a review for the changes to MdePkg.dsc to include the new
> > > libs on RISC-V? That's the minor thing preventing this patch set from being merged 😊.
> > >
> > > A
> > >
> > > -----Original Message-----
> > > From: Warkentin, Andrei <andrei.warkentin@intel.com>
> > > Sent: Friday, March 10, 2023 11:05 AM
> > > To: devel@edk2.groups.io
> > > Cc: Warkentin, Andrei <andrei.warkentin@intel.com>; Daniel Schaefer
> > > <git@danielschaefer.me>; Sunil V L <sunilvl@ventanamicro.com>;
> > > Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming
> > > <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>;
> > > Gerd Hoffmann <kraxel@redhat.com>
> > > Subject: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for
> > > RISC-V
> > >
> > > These are implementations of SerialPortLib using SBI console services.
> > > - SecPeiRiscVSerialPortLibSbi is appropriate for SEC/PEI (XIP)
> > > environments
> > > - PrePiDxeRiscVSerialPortLibSbi is appropriate for PrePI/DXE
> > > environments
> > >
> > > Tested with:
> > > - Qemu RiscVVirt (non-DBCN case, backed by UART)
> > > - TinyEMU + RiscVVirt (non-DBCN case, HTIF)
> > > - TinyEMU + RiscVVirt (DBCN case, HTIF)
> > >
> > > Cc: Daniel Schaefer <git@danielschaefer.me>
> > > Cc: Sunil V L <sunilvl@ventanamicro.com>
> > > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > > Cc: Liming Gao <gaoliming@byosoft.com.cn>
> > > Cc: Zhiguang Liu <zhiguang.liu@intel.com>
> > > Acked-by: Gerd Hoffmann <kraxel@redhat.com>
> > > Signed-off-by: Andrei Warkentin <andrei.warkentin@intel.com>
> > > ---
> > >  MdePkg/MdePkg.dsc                                                              |   2 +
> > >  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf |  36 +++
> > >  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf     |  39 +++
> > >  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c   | 288 ++++++++++++++++++++
> > >  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c       | 233 ++++++++++++++++
> > >  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni |  16 ++
> > >  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni     |  16 ++
> > >  7 files changed, 630 insertions(+)
> > >
> > > diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index
> > > 0ac7618b4623..fb31349042fb 100644
> > > --- a/MdePkg/MdePkg.dsc
> > > +++ b/MdePkg/MdePkg.dsc
> > > @@ -192,5 +192,7 @@ [Components.ARM, Components.AARCH64]
> > >
> > >  [Components.RISCV64]
> > >    MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf
> > > +
> > > + MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLi
> > > + bS
> > > + bi
> > > + .inf
> > > + MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
> > > + rt
> > > + Li
> > > + bSbi.inf
> > >
> > >  [BuildOptions]
> > > diff --git
> > > a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
> > > rt
> > > LibSbi.inf
> > > b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
> > > rt
> > > LibSbi.inf
> > > new file mode 100644
> > > index 000000000000..1d8544756bb9
> > > --- /dev/null
> > > +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSeri
> > > +++ al
> > > +++ Po
> > > +++ rtLibSbi.inf
> > > @@ -0,0 +1,36 @@
> > > +## @file
> > > +#  Serial Port Library backed by SBI console.
> > > +#
> > > +#  Meant for PrePi and DXE environments (where globals are allowed).
> > > +See #  SecPeiSerialPortLibRiscVSbi for a reduced variant
> > > +appropriate for SEC #  and PEI (XIP) environments.
> > > +#
> > > +#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> > > +# #
> > > +SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> > > +
> > > +[Defines]
> > > +  INF_VERSION                    = 0x0001001B
> > > +  BASE_NAME                      = PrePiDxeRiscVSerialPortLibSbi
> > > +  MODULE_UNI_FILE                = PrePiDxeRiscVSerialPortLibSbi.uni
> > > +  FILE_GUID                      = 872af743-ab56-45b4-a065-602567f4820c
> > > +  MODULE_TYPE                    = BASE
> > > +  VERSION_STRING                 = 1.0
> > > +  LIBRARY_CLASS                  = SerialPortLib | SEC DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER UEFI_DRIVER UEFI_APPLICATION
> > > +
> > > +
> > > +#
> > > +#  VALID_ARCHITECTURES           = RISCV64
> > > +#
> > > +
> > > +[Sources]
> > > +  PrePiDxeRiscVSerialPortLibSbi.c
> > > +
> > > +[Packages]
> > > +  MdePkg/MdePkg.dec
> > > +
> > > +[LibraryClasses]
> > > +  RiscVSbiLib
> > > diff --git
> > > a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLi
> > > bS
> > > bi.inf
> > > b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLi
> > > bS
> > > bi.inf
> > > new file mode 100644
> > > index 000000000000..7bc7cd47c11c
> > > --- /dev/null
> > > +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPo
> > > +++ rt
> > > +++ Li
> > > +++ bSbi.inf
> > > @@ -0,0 +1,39 @@
> > > +## @file
> > > +#  Serial Port Library backed by SBI console.
> > > +#
> > > +#  Meant for SEC and PEI (XIP) environments.
> > > +#
> > > +#  Due to limitations of SBI console interface and XIP environments
> > > +# (on use of globals), this library instance does not implement
> > > +reading # and polling the serial port. See
> > > +PrePiDxeSerialPortLibRiscVSbi for # the full-featured variant meant for PrePi and DXE environments.
> > > +#
> > > +#  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> > > +# #
> > > +SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> > > +
> > > +[Defines]
> > > +  INF_VERSION                    = 0x0001001B
> > > +  BASE_NAME                      = SecPeiRiscVSerialPortLibSbi
> > > +  MODULE_UNI_FILE                = SecPeiRiscVSerialPortLibSbi.uni
> > > +  FILE_GUID                      = 639fad38-4bfd-4eb9-9f09-e97c7947d480
> > > +  MODULE_TYPE                    = BASE
> > > +  VERSION_STRING                 = 1.0
> > > +  LIBRARY_CLASS                  = SerialPortLib | SEC PEI_CORE PEIM
> > > +
> > > +
> > > +#
> > > +#  VALID_ARCHITECTURES           = RISCV64
> > > +#
> > > +
> > > +[Sources]
> > > +  SecPeiRiscVSerialPortLibSbi.c
> > > +
> > > +[Packages]
> > > +  MdePkg/MdePkg.dec
> > > +
> > > +[LibraryClasses]
> > > +  RiscVSbiLib
> > > diff --git
> > > a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
> > > rt
> > > LibSbi.c
> > > b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
> > > rt
> > > LibSbi.c
> > > new file mode 100644
> > > index 000000000000..058a6573ef2d
> > > --- /dev/null
> > > +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSeri
> > > +++ al
> > > +++ Po
> > > +++ rtLibSbi.c
> > > @@ -0,0 +1,288 @@
> > > +/** @file
> > > +  Serial Port Library backed by SBI console.
> > > +
> > > +  Meant for PrePi and DXE environments (where globals are allowed).
> > > + See SecPeiSerialPortLibRiscVSbi for a reduced variant appropriate
> > > + for SEC and PEI (XIP) environments.
> > > +
> > > +  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> > > +  SPDX-License-Identifier: BSD-2-Clause-Patent
> > > +
> > > +**/
> > > +
> > > +#include <Base.h>
> > > +#include <Library/SerialPortLib.h>
> > > +#include <Library/BaseRiscVSbiLib.h>
> > > +
> > > +STATIC BOOLEAN  mHaveDbcn          = FALSE;
> > > +STATIC BOOLEAN  mHaveLegacyPutchar = FALSE; STATIC BOOLEAN
> > > +mHaveLegacyGetchar = FALSE;
> > > +STATIC INT64    mLastGetChar       = -1;
> > > +
> > > +/**
> > > +  Initialize the serial device hardware.
> > > +
> > > +  If no initialization is required, then return RETURN_SUCCESS.
> > > +  If the serial device was successfully initialized, then return RETURN_SUCCESS.
> > > +  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
> > > +
> > > +  @retval RETURN_SUCCESS        The serial device was initialized.
> > > +  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
> > > +
> > > +**/
> > > +RETURN_STATUS
> > > +EFIAPI
> > > +SerialPortInitialize (
> > > +  VOID
> > > +  )
> > > +{
> > > +  SBI_RET  Ret;
> > > +
> > > +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1,
> > > + SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > +      (Ret.Value != 0))
> > > +  {
> > > +    mHaveDbcn = TRUE;
> > > +  } else {
> > > +    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_PUTCHAR);
> > > +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > +        (Ret.Value != 0))
> > > +    {
> > > +      mHaveLegacyPutchar = TRUE;
> > > +    }
> > > +
> > > +    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_GETCHAR);
> > > +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > +        (Ret.Value != 0))
> > > +    {
> > > +      mHaveLegacyGetchar = TRUE;
> > > +    }
> > > +  }
> > > +
> > > +  return RETURN_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  Write data from buffer to serial device.
> > > +
> > > +  Writes NumberOfBytes data bytes from Buffer to the serial device.
> > > +  The number of bytes actually written to the serial device is returned.
> > > +  If the return value is less than NumberOfBytes, then the write operation failed.
> > > +  If Buffer is NULL, then ASSERT().
> > > +  If NumberOfBytes is zero, then return 0.
> > > +
> > > +  @param  Buffer           The pointer to the data buffer to be written.
> > > +  @param  NumberOfBytes    The number of bytes to written to the serial device.
> > > +
> > > +  @retval 0                NumberOfBytes is 0.
> > > +  @retval >0               The number of bytes written to the serial device.
> > > +                           If this value is less than NumberOfBytes, then the write operation failed.
> > > +
> > > +**/
> > > +UINTN
> > > +EFIAPI
> > > +SerialPortWrite (
> > > +  IN UINT8  *Buffer,
> > > +  IN UINTN  NumberOfBytes
> > > +  )
> > > +{
> > > +  UINTN  Index;
> > > +
> > > +  if (mHaveDbcn) {
> > > +    SBI_RET  Ret;
> > > +    Ret = SbiCall (
> > > +            SBI_EXT_DBCN,
> > > +            SBI_EXT_DBCN_WRITE,
> > > +            3,
> > > +            NumberOfBytes,
> > > +            ((UINTN)Buffer),
> > > +            0
> > > +            );
> > > +    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
> > > +      return 0;
> > > +    }
> > > +
> > > +    return Ret.Value;
> > > +  } else if (mHaveLegacyPutchar) {
> > > +    for (Index = 0; Index < NumberOfBytes; Index++) {
> > > +      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
> > > +    }
> > > +
> > > +    return Index;
> > > +  }
> > > +
> > > +  /*
> > > +   * Neither DBCN or legacy extension were present.
> > > +   */
> > > +  return NumberOfBytes;
> > > +}
> > > +
> > > +/**
> > > +  Read data from serial device and save the datas in buffer.
> > > +
> > > +  Reads NumberOfBytes data bytes from a serial device into the
> > > + buffer specified by Buffer. The number of bytes actually read is returned.
> > > +  If the return value is less than NumberOfBytes, then the rest operation failed.
> > > +  If Buffer is NULL, then ASSERT().
> > > +  If NumberOfBytes is zero, then return 0.
> > > +
> > > +  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
> > > +  @param  NumberOfBytes    The number of bytes which will be read.
> > > +
> > > +  @retval 0                Read data failed; No data is to be read.
> > > +  @retval >0               The actual number of bytes read from serial device.
> > > +
> > > +**/
> > > +UINTN
> > > +EFIAPI
> > > +SerialPortRead (
> > > +  OUT UINT8  *Buffer,
> > > +  IN  UINTN  NumberOfBytes
> > > +  )
> > > +{
> > > +  UINTN  Index;
> > > +
> > > +  Index = 0;
> > > +  while ((Index < NumberOfBytes) && SerialPortPoll ()) {
> > > +    Buffer[Index++] = (UINT8)mLastGetChar;
> > > +    mLastGetChar    = -1;
> > > +  }
> > > +
> > > +  return Index;
> > > +}
> > > +
> > > +/**
> > > +  Polls a serial device to see if there is any data waiting to be read.
> > > +
> > > +  Polls a serial device to see if there is any data waiting to be read.
> > > +  If there is data waiting to be read from the serial device, then TRUE is returned.
> > > +  If there is no data waiting to be read from the serial device, then FALSE is returned.
> > > +
> > > +  @retval TRUE             Data is waiting to be read from the serial device.
> > > +  @retval FALSE            There is no data waiting to be read from the serial device.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +SerialPortPoll (
> > > +  VOID
> > > +  )
> > > +{
> > > +  /*
> > > +   * Careful. OpenSBI with HTIF console will return -1 followed by -2
> > > +   * if there is no character received. So just check for values >= 0.
> > > +   */
> > > +
> > > +  if (mLastGetChar >= 0) {
> > > +    return TRUE;
> > > +  }
> > > +
> > > +  if (mHaveDbcn) {
> > > +    UINT8    Buffer;
> > > +    SBI_RET  Ret;
> > > +
> > > +    Ret = SbiCall (
> > > +            SBI_EXT_DBCN,
> > > +            SBI_EXT_DBCN_READ,
> > > +            3,
> > > +            1,
> > > +            ((UINTN)&Buffer),
> > > +            0
> > > +            );
> > > +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > +        (Ret.Value == 1))
> > > +    {
> > > +      mLastGetChar = Buffer;
> > > +    }
> > > +  } else if (mHaveLegacyGetchar) {
> > > +    mLastGetChar = (INT64)SbiCall (SBI_EXT_0_1_CONSOLE_GETCHAR, 0,
> > > + 0).Error;  }
> > > +
> > > +  return mLastGetChar >= 0;
> > > +}
> > > +
> > > +/**
> > > +  Sets the control bits on a serial device.
> > > +
> > > +  @param Control                Sets the bits of Control that are settable.
> > > +
> > > +  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
> > > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > > +
> > > +**/
> > > +RETURN_STATUS
> > > +EFIAPI
> > > +SerialPortSetControl (
> > > +  IN UINT32  Control
> > > +  )
> > > +{
> > > +  return RETURN_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  Retrieve the status of the control bits on a serial device.
> > > +
> > > +  @param Control                A pointer to return the current control signals from the serial device.
> > > +
> > > +  @retval RETURN_SUCCESS        The control bits were read from the serial device.
> > > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > > +
> > > +**/
> > > +RETURN_STATUS
> > > +EFIAPI
> > > +SerialPortGetControl (
> > > +  OUT UINT32  *Control
> > > +  )
> > > +{
> > > +  *Control = 0;
> > > +  return RETURN_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  Sets the baud rate, receive FIFO depth, transmit/receice time
> > > +out, parity,
> > > +  data bits, and stop bits on a serial device.
> > > +
> > > +  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
> > > +                            device's default interface speed.
> > > +                            On output, the value actually set.
> > > +  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
> > > +                            serial interface. A ReceiveFifoDepth value of 0 will use
> > > +                            the device's default FIFO depth.
> > > +                            On output, the value actually set.
> > > +  @param Timeout            The requested time out for a single character in microseconds.
> > > +                            This timeout applies to both the transmit and receive side of the
> > > +                            interface. A Timeout value of 0 will use the device's default time
> > > +                            out value.
> > > +                            On output, the value actually set.
> > > +  @param Parity             The type of parity to use on this serial device. A Parity value of
> > > +                            DefaultParity will use the device's default parity value.
> > > +                            On output, the value actually set.
> > > +  @param DataBits           The number of data bits to use on the serial device. A DataBits
> > > +                            vaule of 0 will use the device's default data bit setting.
> > > +                            On output, the value actually set.
> > > +  @param StopBits           The number of stop bits to use on this serial device. A StopBits
> > > +                            value of DefaultStopBits will use the device's default number of
> > > +                            stop bits.
> > > +                            On output, the value actually set.
> > > +
> > > +  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
> > > +  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
> > > +  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
> > > +  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
> > > +
> > > +**/
> > > +RETURN_STATUS
> > > +EFIAPI
> > > +SerialPortSetAttributes (
> > > +  IN OUT UINT64              *BaudRate,
> > > +  IN OUT UINT32              *ReceiveFifoDepth,
> > > +  IN OUT UINT32              *Timeout,
> > > +  IN OUT EFI_PARITY_TYPE     *Parity,
> > > +  IN OUT UINT8               *DataBits,
> > > +  IN OUT EFI_STOP_BITS_TYPE  *StopBits
> > > +  )
> > > +{
> > > +  return RETURN_SUCCESS;
> > > +}
> > > diff --git
> > > a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLi
> > > bS
> > > bi.c
> > > b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLi
> > > bS
> > > bi.c
> > > new file mode 100644
> > > index 000000000000..0ad5931be3ae
> > > --- /dev/null
> > > +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPo
> > > +++ rt
> > > +++ Li
> > > +++ bSbi.c
> > > @@ -0,0 +1,233 @@
> > > +/** @file
> > > +  Serial Port Library backed by SBI console.
> > > +
> > > +  Meant for SEC and PEI (XIP) environments.
> > > +
> > > +  Due to limitations of SBI console interface and XIP environments
> > > + (on use of globals), this library instance does not implement
> > > + reading  and polling the serial port. See
> > > + PrePiDxeSerialPortLibRiscVSbi for  the full-featured variant meant for PrePi and DXE environments.
> > > +
> > > +  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> > > +  SPDX-License-Identifier: BSD-2-Clause-Patent
> > > +
> > > +**/
> > > +
> > > +#include <Base.h>
> > > +#include <Library/SerialPortLib.h>
> > > +#include <Library/BaseRiscVSbiLib.h>
> > > +
> > > +/**
> > > +  Initialize the serial device hardware.
> > > +
> > > +  If no initialization is required, then return RETURN_SUCCESS.
> > > +  If the serial device was successfully initialized, then return RETURN_SUCCESS.
> > > +  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
> > > +
> > > +  @retval RETURN_SUCCESS        The serial device was initialized.
> > > +  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
> > > +
> > > +**/
> > > +RETURN_STATUS
> > > +EFIAPI
> > > +SerialPortInitialize (
> > > +  VOID
> > > +  )
> > > +{
> > > +  return RETURN_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  Write data from buffer to serial device.
> > > +
> > > +  Writes NumberOfBytes data bytes from Buffer to the serial device.
> > > +  The number of bytes actually written to the serial device is returned.
> > > +  If the return value is less than NumberOfBytes, then the write operation failed.
> > > +  If Buffer is NULL, then ASSERT().
> > > +  If NumberOfBytes is zero, then return 0.
> > > +
> > > +  @param  Buffer           The pointer to the data buffer to be written.
> > > +  @param  NumberOfBytes    The number of bytes to written to the serial device.
> > > +
> > > +  @retval 0                NumberOfBytes is 0.
> > > +  @retval >0               The number of bytes written to the serial device.
> > > +                           If this value is less than NumberOfBytes, then the write operation failed.
> > > +
> > > +**/
> > > +UINTN
> > > +EFIAPI
> > > +SerialPortWrite (
> > > +  IN UINT8  *Buffer,
> > > +  IN UINTN  NumberOfBytes
> > > +  )
> > > +{
> > > +  SBI_RET  Ret;
> > > +  UINTN    Index;
> > > +
> > > +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1,
> > > + SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > +      (Ret.Value != 0))
> > > +  {
> > > +    Ret = SbiCall (
> > > +            SBI_EXT_DBCN,
> > > +            SBI_EXT_DBCN_WRITE,
> > > +            3,
> > > +            NumberOfBytes,
> > > +            ((UINTN)Buffer),
> > > +            0
> > > +            );
> > > +    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
> > > +      return 0;
> > > +    }
> > > +
> > > +    return Ret.Value;
> > > +  }
> > > +
> > > +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1,
> > > + SBI_EXT_0_1_CONSOLE_PUTCHAR);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > +      (Ret.Value != 0))
> > > +  {
> > > +    for (Index = 0; Index < NumberOfBytes; Index++) {
> > > +      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
> > > +    }
> > > +
> > > +    return Index;
> > > +  }
> > > +
> > > +  /*
> > > +   * Neither DBCN or legacy extension were present.
> > > +   */
> > > +  return NumberOfBytes;
> > > +}
> > > +
> > > +/**
> > > +  Read data from serial device and save the datas in buffer.
> > > +
> > > +  Reads NumberOfBytes data bytes from a serial device into the
> > > + buffer specified by Buffer. The number of bytes actually read is returned.
> > > +  If the return value is less than NumberOfBytes, then the rest operation failed.
> > > +  If Buffer is NULL, then ASSERT().
> > > +  If NumberOfBytes is zero, then return 0.
> > > +
> > > +  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
> > > +  @param  NumberOfBytes    The number of bytes which will be read.
> > > +
> > > +  @retval 0                Read data failed; No data is to be read.
> > > +  @retval >0               The actual number of bytes read from serial device.
> > > +
> > > +**/
> > > +UINTN
> > > +EFIAPI
> > > +SerialPortRead (
> > > +  OUT UINT8  *Buffer,
> > > +  IN  UINTN  NumberOfBytes
> > > +  )
> > > +{
> > > +  return 0;
> > > +}
> > > +
> > > +/**
> > > +  Polls a serial device to see if there is any data waiting to be read.
> > > +
> > > +  Polls a serial device to see if there is any data waiting to be read.
> > > +  If there is data waiting to be read from the serial device, then TRUE is returned.
> > > +  If there is no data waiting to be read from the serial device, then FALSE is returned.
> > > +
> > > +  @retval TRUE             Data is waiting to be read from the serial device.
> > > +  @retval FALSE            There is no data waiting to be read from the serial device.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +SerialPortPoll (
> > > +  VOID
> > > +  )
> > > +{
> > > +  return FALSE;
> > > +}
> > > +
> > > +/**
> > > +  Sets the control bits on a serial device.
> > > +
> > > +  @param Control                Sets the bits of Control that are settable.
> > > +
> > > +  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
> > > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > > +
> > > +**/
> > > +RETURN_STATUS
> > > +EFIAPI
> > > +SerialPortSetControl (
> > > +  IN UINT32  Control
> > > +  )
> > > +{
> > > +  return RETURN_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  Retrieve the status of the control bits on a serial device.
> > > +
> > > +  @param Control                A pointer to return the current control signals from the serial device.
> > > +
> > > +  @retval RETURN_SUCCESS        The control bits were read from the serial device.
> > > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > > +
> > > +**/
> > > +RETURN_STATUS
> > > +EFIAPI
> > > +SerialPortGetControl (
> > > +  OUT UINT32  *Control
> > > +  )
> > > +{
> > > +  *Control = 0;
> > > +  return RETURN_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  Sets the baud rate, receive FIFO depth, transmit/receice time
> > > +out, parity,
> > > +  data bits, and stop bits on a serial device.
> > > +
> > > +  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
> > > +                            device's default interface speed.
> > > +                            On output, the value actually set.
> > > +  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
> > > +                            serial interface. A ReceiveFifoDepth value of 0 will use
> > > +                            the device's default FIFO depth.
> > > +                            On output, the value actually set.
> > > +  @param Timeout            The requested time out for a single character in microseconds.
> > > +                            This timeout applies to both the transmit and receive side of the
> > > +                            interface. A Timeout value of 0 will use the device's default time
> > > +                            out value.
> > > +                            On output, the value actually set.
> > > +  @param Parity             The type of parity to use on this serial device. A Parity value of
> > > +                            DefaultParity will use the device's default parity value.
> > > +                            On output, the value actually set.
> > > +  @param DataBits           The number of data bits to use on the serial device. A DataBits
> > > +                            vaule of 0 will use the device's default data bit setting.
> > > +                            On output, the value actually set.
> > > +  @param StopBits           The number of stop bits to use on this serial device. A StopBits
> > > +                            value of DefaultStopBits will use the device's default number of
> > > +                            stop bits.
> > > +                            On output, the value actually set.
> > > +
> > > +  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
> > > +  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
> > > +  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
> > > +  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
> > > +
> > > +**/
> > > +RETURN_STATUS
> > > +EFIAPI
> > > +SerialPortSetAttributes (
> > > +  IN OUT UINT64              *BaudRate,
> > > +  IN OUT UINT32              *ReceiveFifoDepth,
> > > +  IN OUT UINT32              *Timeout,
> > > +  IN OUT EFI_PARITY_TYPE     *Parity,
> > > +  IN OUT UINT8               *DataBits,
> > > +  IN OUT EFI_STOP_BITS_TYPE  *StopBits
> > > +  )
> > > +{
> > > +  return RETURN_SUCCESS;
> > > +}
> > > diff --git
> > > a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
> > > rt
> > > LibSbi.uni
> > > b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPo
> > > rt
> > > LibSbi.uni
> > > new file mode 100644
> > > index 000000000000..7b22caa5a090
> > > --- /dev/null
> > > +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSeri
> > > +++ al
> > > +++ Po
> > > +++ rtLibSbi.uni
> > > @@ -0,0 +1,16 @@
> > > +// /** @file
> > > +// Serial Port Library backed by SBI console.
> > > +//
> > > +// Serial Port Library backed by SBI console.
> > > +//
> > > +// Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> > > +// // SPDX-License-Identifier: BSD-2-Clause-Patent // // **/
> > > +
> > > +
> > > +#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
> > > +
> > > +#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
> > > +
> > > diff --git
> > > a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLi
> > > bS
> > > bi.uni
> > > b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLi
> > > bS
> > > bi.uni
> > > new file mode 100644
> > > index 000000000000..7b22caa5a090
> > > --- /dev/null
> > > +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPo
> > > +++ rt
> > > +++ LibSbi.uni
> > > @@ -0,0 +1,16 @@
> > > +// /** @file
> > > +// Serial Port Library backed by SBI console.
> > > +//
> > > +// Serial Port Library backed by SBI console.
> > > +//
> > > +// Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> > > +// // SPDX-License-Identifier: BSD-2-Clause-Patent // // **/
> > > +
> > > +
> > > +#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
> > > +
> > > +#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
> > > +
> > > --
> > > 2.25.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
  2023-03-28 20:09             ` Michael D Kinney
@ 2023-03-28 20:20               ` Andrei Warkentin
  2023-03-28 21:25                 ` Michael D Kinney
  0 siblings, 1 reply; 14+ messages in thread
From: Andrei Warkentin @ 2023-03-28 20:20 UTC (permalink / raw)
  To: Kinney, Michael D, devel@edk2.groups.io; +Cc: Gao, Liming, Liu, Zhiguang

So I'd be in support of formally defining a PrePi prefix. If we go with Sec, then we'd have to come up with some other way of disambiguating "hand off to PEI" vs "hand off to DXE", and it seems like PrePi is already that.

What do you entail the process to be followed here? Is there value in defining a dedicated PrePi module type (like SEC)? I'd be happy to propose the edk2 changes to support that. Can we decouple this work from these specific SerialPortLib contributions?

A

-----Original Message-----
From: Kinney, Michael D <michael.d.kinney@intel.com> 
Sent: Tuesday, March 28, 2023 3:10 PM
To: Warkentin, Andrei <andrei.warkentin@intel.com>; devel@edk2.groups.io
Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>
Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V

I agree that moving into EmbeddedPkg does not make sense.

EmbeddedPkg is very old without many updates.  That package is not the best to use for examples.

I think you are suggesting that there is a difference in a library that runs in an SEC env with intent to hand off to PEI and a library that runs in an SEC env with intent to handoff to DXE.  They are both SEC module types and the execution environment is the same.  The only difference is how they prepare for the handoff.

My interest here is to make sure the prefixes are used consistently and if we truly have a new prefix type, then to clearly define it so it can be used consistently going forward.

Mike 

> -----Original Message-----
> From: Warkentin, Andrei <andrei.warkentin@intel.com>
> Sent: Tuesday, March 28, 2023 1:00 PM
> To: Kinney, Michael D <michael.d.kinney@intel.com>; 
> devel@edk2.groups.io
> Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang 
> <zhiguang.liu@intel.com>
> Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for 
> RISC-V
> 
> Sec isn't a good fit, because it has implications on the environment, 
> which is why the contribution also includes a separate SecPei variant as well.
> 
> In naming, I followed other edk2 examples such as 
> ./EmbeddedPkg/Library/PrePiExtractGuidedSectionLib and 
> ./EmbeddedPkg/Library/PrePiMemoryAllocationLi. I would prefer not to move one of these libraries into EmbeddedPkg - that wouldn't make much sense to me (esp. since there's nothing "embedded" about the approach per se, it can be used on any implementation).
> 
> A
> -----Original Message-----
> From: Kinney, Michael D <michael.d.kinney@intel.com>
> Sent: Monday, March 27, 2023 11:32 AM
> To: Warkentin, Andrei <andrei.warkentin@intel.com>; 
> devel@edk2.groups.io
> Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang 
> <zhiguang.liu@intel.com>; Kinney, Michael D 
> <michael.d.kinney@intel.com>
> Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for 
> RISC-V
> 
> I think that would still be called Sec.  Are there any issues with using Sec prefix?
> 
> Mike
> 
> > -----Original Message-----
> > From: Warkentin, Andrei <andrei.warkentin@intel.com>
> > Sent: Thursday, March 16, 2023 10:45 PM
> > To: Kinney, Michael D <michael.d.kinney@intel.com>; 
> > devel@edk2.groups.io
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang 
> > <zhiguang.liu@intel.com>
> > Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for 
> > RISC-V
> >
> > PrePi refers to a SEC-like stage in a Tiano implementation that 
> > skips the PEI stage. Reasons for skipping PEI usually center around 
> > memory initialization being done at an earlier stage of firmware 
> > initialization - for example, prior or part of TF-A firmware on
> > AArch64 or prior or part of SBI implementation on RISC-V.
> >
> > E.g. ArmVirtPkg/PrePi and OvmfPkg/RiscVVirt/Sec. The later is not a 
> > "SEC" in the send of handing off to PeiCore, but instead hands off to DxeCore.
> >
> > There is no base type for PrePi. The type is still SEC. But it a) assumes RAM is available b) builds HOBs and hands-off to DXE.
> >
> > The name has been around for at least half a decade now and making 
> > it a part of the SerialPortLib component name is important as it signifies the scenario where the code can apply.
> >
> > A
> >
> > -----Original Message-----
> > From: Kinney, Michael D <michael.d.kinney@intel.com>
> > Sent: Friday, March 17, 2023 12:01 AM
> > To: Warkentin, Andrei <andrei.warkentin@intel.com>; 
> > devel@edk2.groups.io
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang 
> > <zhiguang.liu@intel.com>; Kinney, Michael D 
> > <michael.d.kinney@intel.com>
> > Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for 
> > RISC-V
> >
> > What is the definition of "PrePI"?  That is not a prefix I have seen used before.
> >
> > Mike
> >
> > > -----Original Message-----
> > > From: Warkentin, Andrei <andrei.warkentin@intel.com>
> > > Sent: Thursday, March 16, 2023 9:53 PM
> > > To: devel@edk2.groups.io
> > > Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming 
> > > <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>
> > > Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib 
> > > for RISC-V
> > >
> > > Hi folks,
> > >
> > > Can I get a review for the changes to MdePkg.dsc to include the 
> > > new libs on RISC-V? That's the minor thing preventing this patch set from being merged 😊.
> > >
> > > A
> > >
> > > -----Original Message-----
> > > From: Warkentin, Andrei <andrei.warkentin@intel.com>
> > > Sent: Friday, March 10, 2023 11:05 AM
> > > To: devel@edk2.groups.io
> > > Cc: Warkentin, Andrei <andrei.warkentin@intel.com>; Daniel 
> > > Schaefer <git@danielschaefer.me>; Sunil V L 
> > > <sunilvl@ventanamicro.com>; Kinney, Michael D 
> > > <michael.d.kinney@intel.com>; Gao, Liming 
> > > <gaoliming@byosoft.com.cn>; Liu, Zhiguang 
> > > <zhiguang.liu@intel.com>; Gerd Hoffmann <kraxel@redhat.com>
> > > Subject: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for 
> > > RISC-V
> > >
> > > These are implementations of SerialPortLib using SBI console services.
> > > - SecPeiRiscVSerialPortLibSbi is appropriate for SEC/PEI (XIP) 
> > > environments
> > > - PrePiDxeRiscVSerialPortLibSbi is appropriate for PrePI/DXE 
> > > environments
> > >
> > > Tested with:
> > > - Qemu RiscVVirt (non-DBCN case, backed by UART)
> > > - TinyEMU + RiscVVirt (non-DBCN case, HTIF)
> > > - TinyEMU + RiscVVirt (DBCN case, HTIF)
> > >
> > > Cc: Daniel Schaefer <git@danielschaefer.me>
> > > Cc: Sunil V L <sunilvl@ventanamicro.com>
> > > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > > Cc: Liming Gao <gaoliming@byosoft.com.cn>
> > > Cc: Zhiguang Liu <zhiguang.liu@intel.com>
> > > Acked-by: Gerd Hoffmann <kraxel@redhat.com>
> > > Signed-off-by: Andrei Warkentin <andrei.warkentin@intel.com>
> > > ---
> > >  MdePkg/MdePkg.dsc                                                              |   2 +
> > >  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf |  36 +++
> > >  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf     |  39 +++
> > >  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c   | 288 ++++++++++++++++++++
> > >  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c       | 233 ++++++++++++++++
> > >  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni |  16 ++
> > >  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni     |  16 ++
> > >  7 files changed, 630 insertions(+)
> > >
> > > diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index 
> > > 0ac7618b4623..fb31349042fb 100644
> > > --- a/MdePkg/MdePkg.dsc
> > > +++ b/MdePkg/MdePkg.dsc
> > > @@ -192,5 +192,7 @@ [Components.ARM, Components.AARCH64]
> > >
> > >  [Components.RISCV64]
> > >    MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf
> > > +
> > > + MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> > > + Li
> > > + bS
> > > + bi
> > > + .inf
> > > + MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> > > + Po
> > > + rt
> > > + Li
> > > + bSbi.inf
> > >
> > >  [BuildOptions]
> > > diff --git
> > > a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> > > Po
> > > rt
> > > LibSbi.inf
> > > b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> > > Po
> > > rt
> > > LibSbi.inf
> > > new file mode 100644
> > > index 000000000000..1d8544756bb9
> > > --- /dev/null
> > > +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSe
> > > +++ ri
> > > +++ al
> > > +++ Po
> > > +++ rtLibSbi.inf
> > > @@ -0,0 +1,36 @@
> > > +## @file
> > > +#  Serial Port Library backed by SBI console.
> > > +#
> > > +#  Meant for PrePi and DXE environments (where globals are allowed).
> > > +See #  SecPeiSerialPortLibRiscVSbi for a reduced variant 
> > > +appropriate for SEC #  and PEI (XIP) environments.
> > > +#
> > > +#  Copyright (c) 2023, Intel Corporation. All rights 
> > > +reserved.<BR> # #
> > > +SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> > > +
> > > +[Defines]
> > > +  INF_VERSION                    = 0x0001001B
> > > +  BASE_NAME                      = PrePiDxeRiscVSerialPortLibSbi
> > > +  MODULE_UNI_FILE                = PrePiDxeRiscVSerialPortLibSbi.uni
> > > +  FILE_GUID                      = 872af743-ab56-45b4-a065-602567f4820c
> > > +  MODULE_TYPE                    = BASE
> > > +  VERSION_STRING                 = 1.0
> > > +  LIBRARY_CLASS                  = SerialPortLib | SEC DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER UEFI_DRIVER UEFI_APPLICATION
> > > +
> > > +
> > > +#
> > > +#  VALID_ARCHITECTURES           = RISCV64
> > > +#
> > > +
> > > +[Sources]
> > > +  PrePiDxeRiscVSerialPortLibSbi.c
> > > +
> > > +[Packages]
> > > +  MdePkg/MdePkg.dec
> > > +
> > > +[LibraryClasses]
> > > +  RiscVSbiLib
> > > diff --git
> > > a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> > > Li
> > > bS
> > > bi.inf
> > > b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> > > Li
> > > bS
> > > bi.inf
> > > new file mode 100644
> > > index 000000000000..7bc7cd47c11c
> > > --- /dev/null
> > > +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerial
> > > +++ Po
> > > +++ rt
> > > +++ Li
> > > +++ bSbi.inf
> > > @@ -0,0 +1,39 @@
> > > +## @file
> > > +#  Serial Port Library backed by SBI console.
> > > +#
> > > +#  Meant for SEC and PEI (XIP) environments.
> > > +#
> > > +#  Due to limitations of SBI console interface and XIP 
> > > +environments # (on use of globals), this library instance does 
> > > +not implement reading # and polling the serial port. See 
> > > +PrePiDxeSerialPortLibRiscVSbi for # the full-featured variant meant for PrePi and DXE environments.
> > > +#
> > > +#  Copyright (c) 2023, Intel Corporation. All rights 
> > > +reserved.<BR> # #
> > > +SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> > > +
> > > +[Defines]
> > > +  INF_VERSION                    = 0x0001001B
> > > +  BASE_NAME                      = SecPeiRiscVSerialPortLibSbi
> > > +  MODULE_UNI_FILE                = SecPeiRiscVSerialPortLibSbi.uni
> > > +  FILE_GUID                      = 639fad38-4bfd-4eb9-9f09-e97c7947d480
> > > +  MODULE_TYPE                    = BASE
> > > +  VERSION_STRING                 = 1.0
> > > +  LIBRARY_CLASS                  = SerialPortLib | SEC PEI_CORE PEIM
> > > +
> > > +
> > > +#
> > > +#  VALID_ARCHITECTURES           = RISCV64
> > > +#
> > > +
> > > +[Sources]
> > > +  SecPeiRiscVSerialPortLibSbi.c
> > > +
> > > +[Packages]
> > > +  MdePkg/MdePkg.dec
> > > +
> > > +[LibraryClasses]
> > > +  RiscVSbiLib
> > > diff --git
> > > a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> > > Po
> > > rt
> > > LibSbi.c
> > > b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> > > Po
> > > rt
> > > LibSbi.c
> > > new file mode 100644
> > > index 000000000000..058a6573ef2d
> > > --- /dev/null
> > > +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSe
> > > +++ ri
> > > +++ al
> > > +++ Po
> > > +++ rtLibSbi.c
> > > @@ -0,0 +1,288 @@
> > > +/** @file
> > > +  Serial Port Library backed by SBI console.
> > > +
> > > +  Meant for PrePi and DXE environments (where globals are allowed).
> > > + See SecPeiSerialPortLibRiscVSbi for a reduced variant 
> > > + appropriate for SEC and PEI (XIP) environments.
> > > +
> > > +  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> > > +  SPDX-License-Identifier: BSD-2-Clause-Patent
> > > +
> > > +**/
> > > +
> > > +#include <Base.h>
> > > +#include <Library/SerialPortLib.h> #include 
> > > +<Library/BaseRiscVSbiLib.h>
> > > +
> > > +STATIC BOOLEAN  mHaveDbcn          = FALSE;
> > > +STATIC BOOLEAN  mHaveLegacyPutchar = FALSE; STATIC BOOLEAN 
> > > +mHaveLegacyGetchar = FALSE;
> > > +STATIC INT64    mLastGetChar       = -1;
> > > +
> > > +/**
> > > +  Initialize the serial device hardware.
> > > +
> > > +  If no initialization is required, then return RETURN_SUCCESS.
> > > +  If the serial device was successfully initialized, then return RETURN_SUCCESS.
> > > +  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
> > > +
> > > +  @retval RETURN_SUCCESS        The serial device was initialized.
> > > +  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
> > > +
> > > +**/
> > > +RETURN_STATUS
> > > +EFIAPI
> > > +SerialPortInitialize (
> > > +  VOID
> > > +  )
> > > +{
> > > +  SBI_RET  Ret;
> > > +
> > > +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, 
> > > + SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > +      (Ret.Value != 0))
> > > +  {
> > > +    mHaveDbcn = TRUE;
> > > +  } else {
> > > +    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_PUTCHAR);
> > > +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > +        (Ret.Value != 0))
> > > +    {
> > > +      mHaveLegacyPutchar = TRUE;
> > > +    }
> > > +
> > > +    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_GETCHAR);
> > > +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > +        (Ret.Value != 0))
> > > +    {
> > > +      mHaveLegacyGetchar = TRUE;
> > > +    }
> > > +  }
> > > +
> > > +  return RETURN_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  Write data from buffer to serial device.
> > > +
> > > +  Writes NumberOfBytes data bytes from Buffer to the serial device.
> > > +  The number of bytes actually written to the serial device is returned.
> > > +  If the return value is less than NumberOfBytes, then the write operation failed.
> > > +  If Buffer is NULL, then ASSERT().
> > > +  If NumberOfBytes is zero, then return 0.
> > > +
> > > +  @param  Buffer           The pointer to the data buffer to be written.
> > > +  @param  NumberOfBytes    The number of bytes to written to the serial device.
> > > +
> > > +  @retval 0                NumberOfBytes is 0.
> > > +  @retval >0               The number of bytes written to the serial device.
> > > +                           If this value is less than NumberOfBytes, then the write operation failed.
> > > +
> > > +**/
> > > +UINTN
> > > +EFIAPI
> > > +SerialPortWrite (
> > > +  IN UINT8  *Buffer,
> > > +  IN UINTN  NumberOfBytes
> > > +  )
> > > +{
> > > +  UINTN  Index;
> > > +
> > > +  if (mHaveDbcn) {
> > > +    SBI_RET  Ret;
> > > +    Ret = SbiCall (
> > > +            SBI_EXT_DBCN,
> > > +            SBI_EXT_DBCN_WRITE,
> > > +            3,
> > > +            NumberOfBytes,
> > > +            ((UINTN)Buffer),
> > > +            0
> > > +            );
> > > +    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
> > > +      return 0;
> > > +    }
> > > +
> > > +    return Ret.Value;
> > > +  } else if (mHaveLegacyPutchar) {
> > > +    for (Index = 0; Index < NumberOfBytes; Index++) {
> > > +      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
> > > +    }
> > > +
> > > +    return Index;
> > > +  }
> > > +
> > > +  /*
> > > +   * Neither DBCN or legacy extension were present.
> > > +   */
> > > +  return NumberOfBytes;
> > > +}
> > > +
> > > +/**
> > > +  Read data from serial device and save the datas in buffer.
> > > +
> > > +  Reads NumberOfBytes data bytes from a serial device into the 
> > > + buffer specified by Buffer. The number of bytes actually read is returned.
> > > +  If the return value is less than NumberOfBytes, then the rest operation failed.
> > > +  If Buffer is NULL, then ASSERT().
> > > +  If NumberOfBytes is zero, then return 0.
> > > +
> > > +  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
> > > +  @param  NumberOfBytes    The number of bytes which will be read.
> > > +
> > > +  @retval 0                Read data failed; No data is to be read.
> > > +  @retval >0               The actual number of bytes read from serial device.
> > > +
> > > +**/
> > > +UINTN
> > > +EFIAPI
> > > +SerialPortRead (
> > > +  OUT UINT8  *Buffer,
> > > +  IN  UINTN  NumberOfBytes
> > > +  )
> > > +{
> > > +  UINTN  Index;
> > > +
> > > +  Index = 0;
> > > +  while ((Index < NumberOfBytes) && SerialPortPoll ()) {
> > > +    Buffer[Index++] = (UINT8)mLastGetChar;
> > > +    mLastGetChar    = -1;
> > > +  }
> > > +
> > > +  return Index;
> > > +}
> > > +
> > > +/**
> > > +  Polls a serial device to see if there is any data waiting to be read.
> > > +
> > > +  Polls a serial device to see if there is any data waiting to be read.
> > > +  If there is data waiting to be read from the serial device, then TRUE is returned.
> > > +  If there is no data waiting to be read from the serial device, then FALSE is returned.
> > > +
> > > +  @retval TRUE             Data is waiting to be read from the serial device.
> > > +  @retval FALSE            There is no data waiting to be read from the serial device.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +SerialPortPoll (
> > > +  VOID
> > > +  )
> > > +{
> > > +  /*
> > > +   * Careful. OpenSBI with HTIF console will return -1 followed by -2
> > > +   * if there is no character received. So just check for values >= 0.
> > > +   */
> > > +
> > > +  if (mLastGetChar >= 0) {
> > > +    return TRUE;
> > > +  }
> > > +
> > > +  if (mHaveDbcn) {
> > > +    UINT8    Buffer;
> > > +    SBI_RET  Ret;
> > > +
> > > +    Ret = SbiCall (
> > > +            SBI_EXT_DBCN,
> > > +            SBI_EXT_DBCN_READ,
> > > +            3,
> > > +            1,
> > > +            ((UINTN)&Buffer),
> > > +            0
> > > +            );
> > > +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > +        (Ret.Value == 1))
> > > +    {
> > > +      mLastGetChar = Buffer;
> > > +    }
> > > +  } else if (mHaveLegacyGetchar) {
> > > +    mLastGetChar = (INT64)SbiCall (SBI_EXT_0_1_CONSOLE_GETCHAR, 
> > > + 0, 0).Error;  }
> > > +
> > > +  return mLastGetChar >= 0;
> > > +}
> > > +
> > > +/**
> > > +  Sets the control bits on a serial device.
> > > +
> > > +  @param Control                Sets the bits of Control that are settable.
> > > +
> > > +  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
> > > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > > +
> > > +**/
> > > +RETURN_STATUS
> > > +EFIAPI
> > > +SerialPortSetControl (
> > > +  IN UINT32  Control
> > > +  )
> > > +{
> > > +  return RETURN_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  Retrieve the status of the control bits on a serial device.
> > > +
> > > +  @param Control                A pointer to return the current control signals from the serial device.
> > > +
> > > +  @retval RETURN_SUCCESS        The control bits were read from the serial device.
> > > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > > +
> > > +**/
> > > +RETURN_STATUS
> > > +EFIAPI
> > > +SerialPortGetControl (
> > > +  OUT UINT32  *Control
> > > +  )
> > > +{
> > > +  *Control = 0;
> > > +  return RETURN_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  Sets the baud rate, receive FIFO depth, transmit/receice time 
> > > +out, parity,
> > > +  data bits, and stop bits on a serial device.
> > > +
> > > +  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
> > > +                            device's default interface speed.
> > > +                            On output, the value actually set.
> > > +  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
> > > +                            serial interface. A ReceiveFifoDepth value of 0 will use
> > > +                            the device's default FIFO depth.
> > > +                            On output, the value actually set.
> > > +  @param Timeout            The requested time out for a single character in microseconds.
> > > +                            This timeout applies to both the transmit and receive side of the
> > > +                            interface. A Timeout value of 0 will use the device's default time
> > > +                            out value.
> > > +                            On output, the value actually set.
> > > +  @param Parity             The type of parity to use on this serial device. A Parity value of
> > > +                            DefaultParity will use the device's default parity value.
> > > +                            On output, the value actually set.
> > > +  @param DataBits           The number of data bits to use on the serial device. A DataBits
> > > +                            vaule of 0 will use the device's default data bit setting.
> > > +                            On output, the value actually set.
> > > +  @param StopBits           The number of stop bits to use on this serial device. A StopBits
> > > +                            value of DefaultStopBits will use the device's default number of
> > > +                            stop bits.
> > > +                            On output, the value actually set.
> > > +
> > > +  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
> > > +  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
> > > +  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
> > > +  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
> > > +
> > > +**/
> > > +RETURN_STATUS
> > > +EFIAPI
> > > +SerialPortSetAttributes (
> > > +  IN OUT UINT64              *BaudRate,
> > > +  IN OUT UINT32              *ReceiveFifoDepth,
> > > +  IN OUT UINT32              *Timeout,
> > > +  IN OUT EFI_PARITY_TYPE     *Parity,
> > > +  IN OUT UINT8               *DataBits,
> > > +  IN OUT EFI_STOP_BITS_TYPE  *StopBits
> > > +  )
> > > +{
> > > +  return RETURN_SUCCESS;
> > > +}
> > > diff --git
> > > a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> > > Li
> > > bS
> > > bi.c
> > > b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> > > Li
> > > bS
> > > bi.c
> > > new file mode 100644
> > > index 000000000000..0ad5931be3ae
> > > --- /dev/null
> > > +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerial
> > > +++ Po
> > > +++ rt
> > > +++ Li
> > > +++ bSbi.c
> > > @@ -0,0 +1,233 @@
> > > +/** @file
> > > +  Serial Port Library backed by SBI console.
> > > +
> > > +  Meant for SEC and PEI (XIP) environments.
> > > +
> > > +  Due to limitations of SBI console interface and XIP 
> > > + environments (on use of globals), this library instance does not 
> > > + implement reading  and polling the serial port. See 
> > > + PrePiDxeSerialPortLibRiscVSbi for  the full-featured variant meant for PrePi and DXE environments.
> > > +
> > > +  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> > > +  SPDX-License-Identifier: BSD-2-Clause-Patent
> > > +
> > > +**/
> > > +
> > > +#include <Base.h>
> > > +#include <Library/SerialPortLib.h> #include 
> > > +<Library/BaseRiscVSbiLib.h>
> > > +
> > > +/**
> > > +  Initialize the serial device hardware.
> > > +
> > > +  If no initialization is required, then return RETURN_SUCCESS.
> > > +  If the serial device was successfully initialized, then return RETURN_SUCCESS.
> > > +  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
> > > +
> > > +  @retval RETURN_SUCCESS        The serial device was initialized.
> > > +  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
> > > +
> > > +**/
> > > +RETURN_STATUS
> > > +EFIAPI
> > > +SerialPortInitialize (
> > > +  VOID
> > > +  )
> > > +{
> > > +  return RETURN_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  Write data from buffer to serial device.
> > > +
> > > +  Writes NumberOfBytes data bytes from Buffer to the serial device.
> > > +  The number of bytes actually written to the serial device is returned.
> > > +  If the return value is less than NumberOfBytes, then the write operation failed.
> > > +  If Buffer is NULL, then ASSERT().
> > > +  If NumberOfBytes is zero, then return 0.
> > > +
> > > +  @param  Buffer           The pointer to the data buffer to be written.
> > > +  @param  NumberOfBytes    The number of bytes to written to the serial device.
> > > +
> > > +  @retval 0                NumberOfBytes is 0.
> > > +  @retval >0               The number of bytes written to the serial device.
> > > +                           If this value is less than NumberOfBytes, then the write operation failed.
> > > +
> > > +**/
> > > +UINTN
> > > +EFIAPI
> > > +SerialPortWrite (
> > > +  IN UINT8  *Buffer,
> > > +  IN UINTN  NumberOfBytes
> > > +  )
> > > +{
> > > +  SBI_RET  Ret;
> > > +  UINTN    Index;
> > > +
> > > +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, 
> > > + SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > +      (Ret.Value != 0))
> > > +  {
> > > +    Ret = SbiCall (
> > > +            SBI_EXT_DBCN,
> > > +            SBI_EXT_DBCN_WRITE,
> > > +            3,
> > > +            NumberOfBytes,
> > > +            ((UINTN)Buffer),
> > > +            0
> > > +            );
> > > +    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
> > > +      return 0;
> > > +    }
> > > +
> > > +    return Ret.Value;
> > > +  }
> > > +
> > > +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, 
> > > + SBI_EXT_0_1_CONSOLE_PUTCHAR);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > +      (Ret.Value != 0))
> > > +  {
> > > +    for (Index = 0; Index < NumberOfBytes; Index++) {
> > > +      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
> > > +    }
> > > +
> > > +    return Index;
> > > +  }
> > > +
> > > +  /*
> > > +   * Neither DBCN or legacy extension were present.
> > > +   */
> > > +  return NumberOfBytes;
> > > +}
> > > +
> > > +/**
> > > +  Read data from serial device and save the datas in buffer.
> > > +
> > > +  Reads NumberOfBytes data bytes from a serial device into the 
> > > + buffer specified by Buffer. The number of bytes actually read is returned.
> > > +  If the return value is less than NumberOfBytes, then the rest operation failed.
> > > +  If Buffer is NULL, then ASSERT().
> > > +  If NumberOfBytes is zero, then return 0.
> > > +
> > > +  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
> > > +  @param  NumberOfBytes    The number of bytes which will be read.
> > > +
> > > +  @retval 0                Read data failed; No data is to be read.
> > > +  @retval >0               The actual number of bytes read from serial device.
> > > +
> > > +**/
> > > +UINTN
> > > +EFIAPI
> > > +SerialPortRead (
> > > +  OUT UINT8  *Buffer,
> > > +  IN  UINTN  NumberOfBytes
> > > +  )
> > > +{
> > > +  return 0;
> > > +}
> > > +
> > > +/**
> > > +  Polls a serial device to see if there is any data waiting to be read.
> > > +
> > > +  Polls a serial device to see if there is any data waiting to be read.
> > > +  If there is data waiting to be read from the serial device, then TRUE is returned.
> > > +  If there is no data waiting to be read from the serial device, then FALSE is returned.
> > > +
> > > +  @retval TRUE             Data is waiting to be read from the serial device.
> > > +  @retval FALSE            There is no data waiting to be read from the serial device.
> > > +
> > > +**/
> > > +BOOLEAN
> > > +EFIAPI
> > > +SerialPortPoll (
> > > +  VOID
> > > +  )
> > > +{
> > > +  return FALSE;
> > > +}
> > > +
> > > +/**
> > > +  Sets the control bits on a serial device.
> > > +
> > > +  @param Control                Sets the bits of Control that are settable.
> > > +
> > > +  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
> > > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > > +
> > > +**/
> > > +RETURN_STATUS
> > > +EFIAPI
> > > +SerialPortSetControl (
> > > +  IN UINT32  Control
> > > +  )
> > > +{
> > > +  return RETURN_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  Retrieve the status of the control bits on a serial device.
> > > +
> > > +  @param Control                A pointer to return the current control signals from the serial device.
> > > +
> > > +  @retval RETURN_SUCCESS        The control bits were read from the serial device.
> > > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > > +
> > > +**/
> > > +RETURN_STATUS
> > > +EFIAPI
> > > +SerialPortGetControl (
> > > +  OUT UINT32  *Control
> > > +  )
> > > +{
> > > +  *Control = 0;
> > > +  return RETURN_SUCCESS;
> > > +}
> > > +
> > > +/**
> > > +  Sets the baud rate, receive FIFO depth, transmit/receice time 
> > > +out, parity,
> > > +  data bits, and stop bits on a serial device.
> > > +
> > > +  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
> > > +                            device's default interface speed.
> > > +                            On output, the value actually set.
> > > +  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
> > > +                            serial interface. A ReceiveFifoDepth value of 0 will use
> > > +                            the device's default FIFO depth.
> > > +                            On output, the value actually set.
> > > +  @param Timeout            The requested time out for a single character in microseconds.
> > > +                            This timeout applies to both the transmit and receive side of the
> > > +                            interface. A Timeout value of 0 will use the device's default time
> > > +                            out value.
> > > +                            On output, the value actually set.
> > > +  @param Parity             The type of parity to use on this serial device. A Parity value of
> > > +                            DefaultParity will use the device's default parity value.
> > > +                            On output, the value actually set.
> > > +  @param DataBits           The number of data bits to use on the serial device. A DataBits
> > > +                            vaule of 0 will use the device's default data bit setting.
> > > +                            On output, the value actually set.
> > > +  @param StopBits           The number of stop bits to use on this serial device. A StopBits
> > > +                            value of DefaultStopBits will use the device's default number of
> > > +                            stop bits.
> > > +                            On output, the value actually set.
> > > +
> > > +  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
> > > +  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
> > > +  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
> > > +  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
> > > +
> > > +**/
> > > +RETURN_STATUS
> > > +EFIAPI
> > > +SerialPortSetAttributes (
> > > +  IN OUT UINT64              *BaudRate,
> > > +  IN OUT UINT32              *ReceiveFifoDepth,
> > > +  IN OUT UINT32              *Timeout,
> > > +  IN OUT EFI_PARITY_TYPE     *Parity,
> > > +  IN OUT UINT8               *DataBits,
> > > +  IN OUT EFI_STOP_BITS_TYPE  *StopBits
> > > +  )
> > > +{
> > > +  return RETURN_SUCCESS;
> > > +}
> > > diff --git
> > > a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> > > Po
> > > rt
> > > LibSbi.uni
> > > b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> > > Po
> > > rt
> > > LibSbi.uni
> > > new file mode 100644
> > > index 000000000000..7b22caa5a090
> > > --- /dev/null
> > > +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSe
> > > +++ ri
> > > +++ al
> > > +++ Po
> > > +++ rtLibSbi.uni
> > > @@ -0,0 +1,16 @@
> > > +// /** @file
> > > +// Serial Port Library backed by SBI console.
> > > +//
> > > +// Serial Port Library backed by SBI console.
> > > +//
> > > +// Copyright (c) 2023, Intel Corporation. All rights 
> > > +reserved.<BR> // // SPDX-License-Identifier: BSD-2-Clause-Patent 
> > > +// // **/
> > > +
> > > +
> > > +#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
> > > +
> > > +#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
> > > +
> > > diff --git
> > > a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> > > Li
> > > bS
> > > bi.uni
> > > b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> > > Li
> > > bS
> > > bi.uni
> > > new file mode 100644
> > > index 000000000000..7b22caa5a090
> > > --- /dev/null
> > > +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerial
> > > +++ Po
> > > +++ rt
> > > +++ LibSbi.uni
> > > @@ -0,0 +1,16 @@
> > > +// /** @file
> > > +// Serial Port Library backed by SBI console.
> > > +//
> > > +// Serial Port Library backed by SBI console.
> > > +//
> > > +// Copyright (c) 2023, Intel Corporation. All rights 
> > > +reserved.<BR> // // SPDX-License-Identifier: BSD-2-Clause-Patent 
> > > +// // **/
> > > +
> > > +
> > > +#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
> > > +
> > > +#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
> > > +
> > > --
> > > 2.25.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
  2023-03-28 20:20               ` Andrei Warkentin
@ 2023-03-28 21:25                 ` Michael D Kinney
  2023-03-28 22:00                   ` Andrei Warkentin
  0 siblings, 1 reply; 14+ messages in thread
From: Michael D Kinney @ 2023-03-28 21:25 UTC (permalink / raw)
  To: Warkentin, Andrei, devel@edk2.groups.io
  Cc: Gao, Liming, Liu, Zhiguang, Kinney, Michael D

From looking at the details of your libs, it is not related to DXE.

It is SEC with no writable globals, or SEC with writable globals.

Both libs could be used in an SEC implementation that hands off to PEI or DXE.

You current proposed names are:
* SecPeiRiscVSerialPortLibSbi
* PrePiDxeRiscVSerialPortLibSbi

The current naming convention is one or more module types followed by the
Library Class Name followed by an optional string that describes
the services that the Library Class implementation depends.  You have
used Sbi as this optional postfix.  However, you have two different SBI
environments.  One where the code/data is running from RAM where global
variables can be used and one where code/data is running from ROM where
global variable can not be used.

How about the following names?
* SecPeiRiscVSerialPortLibSbi
* SecPeiDxeRiscVSerialPortLibSbiRam

In fact, both libs are of type BASE which means they are really compatible
with all module types as long as the Sbi vs SbiRam difference is understood
by the platform developer that has to set the lib mappings in the DSC file.

So a further refinement would be:
* BaseRiscVSerialPortLibSbi
* BaseRiscVSerialPortLibSbiRam

We usually do not put a CPU arch in the Library Instance name, but SBI is 
Unique to RiscV and the library services being used are from the RiscVSbiLib
class.  Adding the full lib class name to the end adds the RiscV keyword to 
the library instance name.

* BaseSerialPortLibRiscVSbiLib
* BaseSerialPortLibRiscVSbiLibRam


Mike

> -----Original Message-----
> From: Warkentin, Andrei <andrei.warkentin@intel.com>
> Sent: Tuesday, March 28, 2023 1:20 PM
> To: Kinney, Michael D <michael.d.kinney@intel.com>; devel@edk2.groups.io
> Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>
> Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
> 
> So I'd be in support of formally defining a PrePi prefix. If we go with Sec, then we'd have to come up with some other way of
> disambiguating "hand off to PEI" vs "hand off to DXE", and it seems like PrePi is already that.
> 
> What do you entail the process to be followed here? Is there value in defining a dedicated PrePi module type (like SEC)? I'd be
> happy to propose the edk2 changes to support that. Can we decouple this work from these specific SerialPortLib contributions?
> 
> A
> 
> -----Original Message-----
> From: Kinney, Michael D <michael.d.kinney@intel.com>
> Sent: Tuesday, March 28, 2023 3:10 PM
> To: Warkentin, Andrei <andrei.warkentin@intel.com>; devel@edk2.groups.io
> Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>
> Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
> 
> I agree that moving into EmbeddedPkg does not make sense.
> 
> EmbeddedPkg is very old without many updates.  That package is not the best to use for examples.
> 
> I think you are suggesting that there is a difference in a library that runs in an SEC env with intent to hand off to PEI and a
> library that runs in an SEC env with intent to handoff to DXE.  They are both SEC module types and the execution environment is the
> same.  The only difference is how they prepare for the handoff.
> 
> My interest here is to make sure the prefixes are used consistently and if we truly have a new prefix type, then to clearly define
> it so it can be used consistently going forward.
> 
> Mike
> 
> > -----Original Message-----
> > From: Warkentin, Andrei <andrei.warkentin@intel.com>
> > Sent: Tuesday, March 28, 2023 1:00 PM
> > To: Kinney, Michael D <michael.d.kinney@intel.com>;
> > devel@edk2.groups.io
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang
> > <zhiguang.liu@intel.com>
> > Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for
> > RISC-V
> >
> > Sec isn't a good fit, because it has implications on the environment,
> > which is why the contribution also includes a separate SecPei variant as well.
> >
> > In naming, I followed other edk2 examples such as
> > ./EmbeddedPkg/Library/PrePiExtractGuidedSectionLib and
> > ./EmbeddedPkg/Library/PrePiMemoryAllocationLi. I would prefer not to move one of these libraries into EmbeddedPkg - that wouldn't
> make much sense to me (esp. since there's nothing "embedded" about the approach per se, it can be used on any implementation).
> >
> > A
> > -----Original Message-----
> > From: Kinney, Michael D <michael.d.kinney@intel.com>
> > Sent: Monday, March 27, 2023 11:32 AM
> > To: Warkentin, Andrei <andrei.warkentin@intel.com>;
> > devel@edk2.groups.io
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang
> > <zhiguang.liu@intel.com>; Kinney, Michael D
> > <michael.d.kinney@intel.com>
> > Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for
> > RISC-V
> >
> > I think that would still be called Sec.  Are there any issues with using Sec prefix?
> >
> > Mike
> >
> > > -----Original Message-----
> > > From: Warkentin, Andrei <andrei.warkentin@intel.com>
> > > Sent: Thursday, March 16, 2023 10:45 PM
> > > To: Kinney, Michael D <michael.d.kinney@intel.com>;
> > > devel@edk2.groups.io
> > > Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang
> > > <zhiguang.liu@intel.com>
> > > Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for
> > > RISC-V
> > >
> > > PrePi refers to a SEC-like stage in a Tiano implementation that
> > > skips the PEI stage. Reasons for skipping PEI usually center around
> > > memory initialization being done at an earlier stage of firmware
> > > initialization - for example, prior or part of TF-A firmware on
> > > AArch64 or prior or part of SBI implementation on RISC-V.
> > >
> > > E.g. ArmVirtPkg/PrePi and OvmfPkg/RiscVVirt/Sec. The later is not a
> > > "SEC" in the send of handing off to PeiCore, but instead hands off to DxeCore.
> > >
> > > There is no base type for PrePi. The type is still SEC. But it a) assumes RAM is available b) builds HOBs and hands-off to DXE.
> > >
> > > The name has been around for at least half a decade now and making
> > > it a part of the SerialPortLib component name is important as it signifies the scenario where the code can apply.
> > >
> > > A
> > >
> > > -----Original Message-----
> > > From: Kinney, Michael D <michael.d.kinney@intel.com>
> > > Sent: Friday, March 17, 2023 12:01 AM
> > > To: Warkentin, Andrei <andrei.warkentin@intel.com>;
> > > devel@edk2.groups.io
> > > Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang
> > > <zhiguang.liu@intel.com>; Kinney, Michael D
> > > <michael.d.kinney@intel.com>
> > > Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for
> > > RISC-V
> > >
> > > What is the definition of "PrePI"?  That is not a prefix I have seen used before.
> > >
> > > Mike
> > >
> > > > -----Original Message-----
> > > > From: Warkentin, Andrei <andrei.warkentin@intel.com>
> > > > Sent: Thursday, March 16, 2023 9:53 PM
> > > > To: devel@edk2.groups.io
> > > > Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming
> > > > <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>
> > > > Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib
> > > > for RISC-V
> > > >
> > > > Hi folks,
> > > >
> > > > Can I get a review for the changes to MdePkg.dsc to include the
> > > > new libs on RISC-V? That's the minor thing preventing this patch set from being merged 😊.
> > > >
> > > > A
> > > >
> > > > -----Original Message-----
> > > > From: Warkentin, Andrei <andrei.warkentin@intel.com>
> > > > Sent: Friday, March 10, 2023 11:05 AM
> > > > To: devel@edk2.groups.io
> > > > Cc: Warkentin, Andrei <andrei.warkentin@intel.com>; Daniel
> > > > Schaefer <git@danielschaefer.me>; Sunil V L
> > > > <sunilvl@ventanamicro.com>; Kinney, Michael D
> > > > <michael.d.kinney@intel.com>; Gao, Liming
> > > > <gaoliming@byosoft.com.cn>; Liu, Zhiguang
> > > > <zhiguang.liu@intel.com>; Gerd Hoffmann <kraxel@redhat.com>
> > > > Subject: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for
> > > > RISC-V
> > > >
> > > > These are implementations of SerialPortLib using SBI console services.
> > > > - SecPeiRiscVSerialPortLibSbi is appropriate for SEC/PEI (XIP)
> > > > environments
> > > > - PrePiDxeRiscVSerialPortLibSbi is appropriate for PrePI/DXE
> > > > environments
> > > >
> > > > Tested with:
> > > > - Qemu RiscVVirt (non-DBCN case, backed by UART)
> > > > - TinyEMU + RiscVVirt (non-DBCN case, HTIF)
> > > > - TinyEMU + RiscVVirt (DBCN case, HTIF)
> > > >
> > > > Cc: Daniel Schaefer <git@danielschaefer.me>
> > > > Cc: Sunil V L <sunilvl@ventanamicro.com>
> > > > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > > > Cc: Liming Gao <gaoliming@byosoft.com.cn>
> > > > Cc: Zhiguang Liu <zhiguang.liu@intel.com>
> > > > Acked-by: Gerd Hoffmann <kraxel@redhat.com>
> > > > Signed-off-by: Andrei Warkentin <andrei.warkentin@intel.com>
> > > > ---
> > > >  MdePkg/MdePkg.dsc                                                              |   2 +
> > > >  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf |  36 +++
> > > >  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf     |  39 +++
> > > >  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c   | 288 ++++++++++++++++++++
> > > >  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c       | 233 ++++++++++++++++
> > > >  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni |  16 ++
> > > >  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni     |  16 ++
> > > >  7 files changed, 630 insertions(+)
> > > >
> > > > diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index
> > > > 0ac7618b4623..fb31349042fb 100644
> > > > --- a/MdePkg/MdePkg.dsc
> > > > +++ b/MdePkg/MdePkg.dsc
> > > > @@ -192,5 +192,7 @@ [Components.ARM, Components.AARCH64]
> > > >
> > > >  [Components.RISCV64]
> > > >    MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf
> > > > +
> > > > + MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> > > > + Li
> > > > + bS
> > > > + bi
> > > > + .inf
> > > > + MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> > > > + Po
> > > > + rt
> > > > + Li
> > > > + bSbi.inf
> > > >
> > > >  [BuildOptions]
> > > > diff --git
> > > > a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> > > > Po
> > > > rt
> > > > LibSbi.inf
> > > > b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> > > > Po
> > > > rt
> > > > LibSbi.inf
> > > > new file mode 100644
> > > > index 000000000000..1d8544756bb9
> > > > --- /dev/null
> > > > +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSe
> > > > +++ ri
> > > > +++ al
> > > > +++ Po
> > > > +++ rtLibSbi.inf
> > > > @@ -0,0 +1,36 @@
> > > > +## @file
> > > > +#  Serial Port Library backed by SBI console.
> > > > +#
> > > > +#  Meant for PrePi and DXE environments (where globals are allowed).
> > > > +See #  SecPeiSerialPortLibRiscVSbi for a reduced variant
> > > > +appropriate for SEC #  and PEI (XIP) environments.
> > > > +#
> > > > +#  Copyright (c) 2023, Intel Corporation. All rights
> > > > +reserved.<BR> # #
> > > > +SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> > > > +
> > > > +[Defines]
> > > > +  INF_VERSION                    = 0x0001001B
> > > > +  BASE_NAME                      = PrePiDxeRiscVSerialPortLibSbi
> > > > +  MODULE_UNI_FILE                = PrePiDxeRiscVSerialPortLibSbi.uni
> > > > +  FILE_GUID                      = 872af743-ab56-45b4-a065-602567f4820c
> > > > +  MODULE_TYPE                    = BASE
> > > > +  VERSION_STRING                 = 1.0
> > > > +  LIBRARY_CLASS                  = SerialPortLib | SEC DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER UEFI_DRIVER UEFI_APPLICATION
> > > > +
> > > > +
> > > > +#
> > > > +#  VALID_ARCHITECTURES           = RISCV64
> > > > +#
> > > > +
> > > > +[Sources]
> > > > +  PrePiDxeRiscVSerialPortLibSbi.c
> > > > +
> > > > +[Packages]
> > > > +  MdePkg/MdePkg.dec
> > > > +
> > > > +[LibraryClasses]
> > > > +  RiscVSbiLib
> > > > diff --git
> > > > a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> > > > Li
> > > > bS
> > > > bi.inf
> > > > b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> > > > Li
> > > > bS
> > > > bi.inf
> > > > new file mode 100644
> > > > index 000000000000..7bc7cd47c11c
> > > > --- /dev/null
> > > > +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerial
> > > > +++ Po
> > > > +++ rt
> > > > +++ Li
> > > > +++ bSbi.inf
> > > > @@ -0,0 +1,39 @@
> > > > +## @file
> > > > +#  Serial Port Library backed by SBI console.
> > > > +#
> > > > +#  Meant for SEC and PEI (XIP) environments.
> > > > +#
> > > > +#  Due to limitations of SBI console interface and XIP
> > > > +environments # (on use of globals), this library instance does
> > > > +not implement reading # and polling the serial port. See
> > > > +PrePiDxeSerialPortLibRiscVSbi for # the full-featured variant meant for PrePi and DXE environments.
> > > > +#
> > > > +#  Copyright (c) 2023, Intel Corporation. All rights
> > > > +reserved.<BR> # #
> > > > +SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> > > > +
> > > > +[Defines]
> > > > +  INF_VERSION                    = 0x0001001B
> > > > +  BASE_NAME                      = SecPeiRiscVSerialPortLibSbi
> > > > +  MODULE_UNI_FILE                = SecPeiRiscVSerialPortLibSbi.uni
> > > > +  FILE_GUID                      = 639fad38-4bfd-4eb9-9f09-e97c7947d480
> > > > +  MODULE_TYPE                    = BASE
> > > > +  VERSION_STRING                 = 1.0
> > > > +  LIBRARY_CLASS                  = SerialPortLib | SEC PEI_CORE PEIM
> > > > +
> > > > +
> > > > +#
> > > > +#  VALID_ARCHITECTURES           = RISCV64
> > > > +#
> > > > +
> > > > +[Sources]
> > > > +  SecPeiRiscVSerialPortLibSbi.c
> > > > +
> > > > +[Packages]
> > > > +  MdePkg/MdePkg.dec
> > > > +
> > > > +[LibraryClasses]
> > > > +  RiscVSbiLib
> > > > diff --git
> > > > a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> > > > Po
> > > > rt
> > > > LibSbi.c
> > > > b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> > > > Po
> > > > rt
> > > > LibSbi.c
> > > > new file mode 100644
> > > > index 000000000000..058a6573ef2d
> > > > --- /dev/null
> > > > +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSe
> > > > +++ ri
> > > > +++ al
> > > > +++ Po
> > > > +++ rtLibSbi.c
> > > > @@ -0,0 +1,288 @@
> > > > +/** @file
> > > > +  Serial Port Library backed by SBI console.
> > > > +
> > > > +  Meant for PrePi and DXE environments (where globals are allowed).
> > > > + See SecPeiSerialPortLibRiscVSbi for a reduced variant
> > > > + appropriate for SEC and PEI (XIP) environments.
> > > > +
> > > > +  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> > > > +  SPDX-License-Identifier: BSD-2-Clause-Patent
> > > > +
> > > > +**/
> > > > +
> > > > +#include <Base.h>
> > > > +#include <Library/SerialPortLib.h> #include
> > > > +<Library/BaseRiscVSbiLib.h>
> > > > +
> > > > +STATIC BOOLEAN  mHaveDbcn          = FALSE;
> > > > +STATIC BOOLEAN  mHaveLegacyPutchar = FALSE; STATIC BOOLEAN
> > > > +mHaveLegacyGetchar = FALSE;
> > > > +STATIC INT64    mLastGetChar       = -1;
> > > > +
> > > > +/**
> > > > +  Initialize the serial device hardware.
> > > > +
> > > > +  If no initialization is required, then return RETURN_SUCCESS.
> > > > +  If the serial device was successfully initialized, then return RETURN_SUCCESS.
> > > > +  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
> > > > +
> > > > +  @retval RETURN_SUCCESS        The serial device was initialized.
> > > > +  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
> > > > +
> > > > +**/
> > > > +RETURN_STATUS
> > > > +EFIAPI
> > > > +SerialPortInitialize (
> > > > +  VOID
> > > > +  )
> > > > +{
> > > > +  SBI_RET  Ret;
> > > > +
> > > > +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1,
> > > > + SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > > +      (Ret.Value != 0))
> > > > +  {
> > > > +    mHaveDbcn = TRUE;
> > > > +  } else {
> > > > +    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_PUTCHAR);
> > > > +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > > +        (Ret.Value != 0))
> > > > +    {
> > > > +      mHaveLegacyPutchar = TRUE;
> > > > +    }
> > > > +
> > > > +    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_GETCHAR);
> > > > +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > > +        (Ret.Value != 0))
> > > > +    {
> > > > +      mHaveLegacyGetchar = TRUE;
> > > > +    }
> > > > +  }
> > > > +
> > > > +  return RETURN_SUCCESS;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Write data from buffer to serial device.
> > > > +
> > > > +  Writes NumberOfBytes data bytes from Buffer to the serial device.
> > > > +  The number of bytes actually written to the serial device is returned.
> > > > +  If the return value is less than NumberOfBytes, then the write operation failed.
> > > > +  If Buffer is NULL, then ASSERT().
> > > > +  If NumberOfBytes is zero, then return 0.
> > > > +
> > > > +  @param  Buffer           The pointer to the data buffer to be written.
> > > > +  @param  NumberOfBytes    The number of bytes to written to the serial device.
> > > > +
> > > > +  @retval 0                NumberOfBytes is 0.
> > > > +  @retval >0               The number of bytes written to the serial device.
> > > > +                           If this value is less than NumberOfBytes, then the write operation failed.
> > > > +
> > > > +**/
> > > > +UINTN
> > > > +EFIAPI
> > > > +SerialPortWrite (
> > > > +  IN UINT8  *Buffer,
> > > > +  IN UINTN  NumberOfBytes
> > > > +  )
> > > > +{
> > > > +  UINTN  Index;
> > > > +
> > > > +  if (mHaveDbcn) {
> > > > +    SBI_RET  Ret;
> > > > +    Ret = SbiCall (
> > > > +            SBI_EXT_DBCN,
> > > > +            SBI_EXT_DBCN_WRITE,
> > > > +            3,
> > > > +            NumberOfBytes,
> > > > +            ((UINTN)Buffer),
> > > > +            0
> > > > +            );
> > > > +    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
> > > > +      return 0;
> > > > +    }
> > > > +
> > > > +    return Ret.Value;
> > > > +  } else if (mHaveLegacyPutchar) {
> > > > +    for (Index = 0; Index < NumberOfBytes; Index++) {
> > > > +      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
> > > > +    }
> > > > +
> > > > +    return Index;
> > > > +  }
> > > > +
> > > > +  /*
> > > > +   * Neither DBCN or legacy extension were present.
> > > > +   */
> > > > +  return NumberOfBytes;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Read data from serial device and save the datas in buffer.
> > > > +
> > > > +  Reads NumberOfBytes data bytes from a serial device into the
> > > > + buffer specified by Buffer. The number of bytes actually read is returned.
> > > > +  If the return value is less than NumberOfBytes, then the rest operation failed.
> > > > +  If Buffer is NULL, then ASSERT().
> > > > +  If NumberOfBytes is zero, then return 0.
> > > > +
> > > > +  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
> > > > +  @param  NumberOfBytes    The number of bytes which will be read.
> > > > +
> > > > +  @retval 0                Read data failed; No data is to be read.
> > > > +  @retval >0               The actual number of bytes read from serial device.
> > > > +
> > > > +**/
> > > > +UINTN
> > > > +EFIAPI
> > > > +SerialPortRead (
> > > > +  OUT UINT8  *Buffer,
> > > > +  IN  UINTN  NumberOfBytes
> > > > +  )
> > > > +{
> > > > +  UINTN  Index;
> > > > +
> > > > +  Index = 0;
> > > > +  while ((Index < NumberOfBytes) && SerialPortPoll ()) {
> > > > +    Buffer[Index++] = (UINT8)mLastGetChar;
> > > > +    mLastGetChar    = -1;
> > > > +  }
> > > > +
> > > > +  return Index;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Polls a serial device to see if there is any data waiting to be read.
> > > > +
> > > > +  Polls a serial device to see if there is any data waiting to be read.
> > > > +  If there is data waiting to be read from the serial device, then TRUE is returned.
> > > > +  If there is no data waiting to be read from the serial device, then FALSE is returned.
> > > > +
> > > > +  @retval TRUE             Data is waiting to be read from the serial device.
> > > > +  @retval FALSE            There is no data waiting to be read from the serial device.
> > > > +
> > > > +**/
> > > > +BOOLEAN
> > > > +EFIAPI
> > > > +SerialPortPoll (
> > > > +  VOID
> > > > +  )
> > > > +{
> > > > +  /*
> > > > +   * Careful. OpenSBI with HTIF console will return -1 followed by -2
> > > > +   * if there is no character received. So just check for values >= 0.
> > > > +   */
> > > > +
> > > > +  if (mLastGetChar >= 0) {
> > > > +    return TRUE;
> > > > +  }
> > > > +
> > > > +  if (mHaveDbcn) {
> > > > +    UINT8    Buffer;
> > > > +    SBI_RET  Ret;
> > > > +
> > > > +    Ret = SbiCall (
> > > > +            SBI_EXT_DBCN,
> > > > +            SBI_EXT_DBCN_READ,
> > > > +            3,
> > > > +            1,
> > > > +            ((UINTN)&Buffer),
> > > > +            0
> > > > +            );
> > > > +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > > +        (Ret.Value == 1))
> > > > +    {
> > > > +      mLastGetChar = Buffer;
> > > > +    }
> > > > +  } else if (mHaveLegacyGetchar) {
> > > > +    mLastGetChar = (INT64)SbiCall (SBI_EXT_0_1_CONSOLE_GETCHAR,
> > > > + 0, 0).Error;  }
> > > > +
> > > > +  return mLastGetChar >= 0;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Sets the control bits on a serial device.
> > > > +
> > > > +  @param Control                Sets the bits of Control that are settable.
> > > > +
> > > > +  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
> > > > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > > > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > > > +
> > > > +**/
> > > > +RETURN_STATUS
> > > > +EFIAPI
> > > > +SerialPortSetControl (
> > > > +  IN UINT32  Control
> > > > +  )
> > > > +{
> > > > +  return RETURN_SUCCESS;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Retrieve the status of the control bits on a serial device.
> > > > +
> > > > +  @param Control                A pointer to return the current control signals from the serial device.
> > > > +
> > > > +  @retval RETURN_SUCCESS        The control bits were read from the serial device.
> > > > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > > > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > > > +
> > > > +**/
> > > > +RETURN_STATUS
> > > > +EFIAPI
> > > > +SerialPortGetControl (
> > > > +  OUT UINT32  *Control
> > > > +  )
> > > > +{
> > > > +  *Control = 0;
> > > > +  return RETURN_SUCCESS;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Sets the baud rate, receive FIFO depth, transmit/receice time
> > > > +out, parity,
> > > > +  data bits, and stop bits on a serial device.
> > > > +
> > > > +  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
> > > > +                            device's default interface speed.
> > > > +                            On output, the value actually set.
> > > > +  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
> > > > +                            serial interface. A ReceiveFifoDepth value of 0 will use
> > > > +                            the device's default FIFO depth.
> > > > +                            On output, the value actually set.
> > > > +  @param Timeout            The requested time out for a single character in microseconds.
> > > > +                            This timeout applies to both the transmit and receive side of the
> > > > +                            interface. A Timeout value of 0 will use the device's default time
> > > > +                            out value.
> > > > +                            On output, the value actually set.
> > > > +  @param Parity             The type of parity to use on this serial device. A Parity value of
> > > > +                            DefaultParity will use the device's default parity value.
> > > > +                            On output, the value actually set.
> > > > +  @param DataBits           The number of data bits to use on the serial device. A DataBits
> > > > +                            vaule of 0 will use the device's default data bit setting.
> > > > +                            On output, the value actually set.
> > > > +  @param StopBits           The number of stop bits to use on this serial device. A StopBits
> > > > +                            value of DefaultStopBits will use the device's default number of
> > > > +                            stop bits.
> > > > +                            On output, the value actually set.
> > > > +
> > > > +  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
> > > > +  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
> > > > +  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
> > > > +  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
> > > > +
> > > > +**/
> > > > +RETURN_STATUS
> > > > +EFIAPI
> > > > +SerialPortSetAttributes (
> > > > +  IN OUT UINT64              *BaudRate,
> > > > +  IN OUT UINT32              *ReceiveFifoDepth,
> > > > +  IN OUT UINT32              *Timeout,
> > > > +  IN OUT EFI_PARITY_TYPE     *Parity,
> > > > +  IN OUT UINT8               *DataBits,
> > > > +  IN OUT EFI_STOP_BITS_TYPE  *StopBits
> > > > +  )
> > > > +{
> > > > +  return RETURN_SUCCESS;
> > > > +}
> > > > diff --git
> > > > a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> > > > Li
> > > > bS
> > > > bi.c
> > > > b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> > > > Li
> > > > bS
> > > > bi.c
> > > > new file mode 100644
> > > > index 000000000000..0ad5931be3ae
> > > > --- /dev/null
> > > > +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerial
> > > > +++ Po
> > > > +++ rt
> > > > +++ Li
> > > > +++ bSbi.c
> > > > @@ -0,0 +1,233 @@
> > > > +/** @file
> > > > +  Serial Port Library backed by SBI console.
> > > > +
> > > > +  Meant for SEC and PEI (XIP) environments.
> > > > +
> > > > +  Due to limitations of SBI console interface and XIP
> > > > + environments (on use of globals), this library instance does not
> > > > + implement reading  and polling the serial port. See
> > > > + PrePiDxeSerialPortLibRiscVSbi for  the full-featured variant meant for PrePi and DXE environments.
> > > > +
> > > > +  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
> > > > +  SPDX-License-Identifier: BSD-2-Clause-Patent
> > > > +
> > > > +**/
> > > > +
> > > > +#include <Base.h>
> > > > +#include <Library/SerialPortLib.h> #include
> > > > +<Library/BaseRiscVSbiLib.h>
> > > > +
> > > > +/**
> > > > +  Initialize the serial device hardware.
> > > > +
> > > > +  If no initialization is required, then return RETURN_SUCCESS.
> > > > +  If the serial device was successfully initialized, then return RETURN_SUCCESS.
> > > > +  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
> > > > +
> > > > +  @retval RETURN_SUCCESS        The serial device was initialized.
> > > > +  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
> > > > +
> > > > +**/
> > > > +RETURN_STATUS
> > > > +EFIAPI
> > > > +SerialPortInitialize (
> > > > +  VOID
> > > > +  )
> > > > +{
> > > > +  return RETURN_SUCCESS;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Write data from buffer to serial device.
> > > > +
> > > > +  Writes NumberOfBytes data bytes from Buffer to the serial device.
> > > > +  The number of bytes actually written to the serial device is returned.
> > > > +  If the return value is less than NumberOfBytes, then the write operation failed.
> > > > +  If Buffer is NULL, then ASSERT().
> > > > +  If NumberOfBytes is zero, then return 0.
> > > > +
> > > > +  @param  Buffer           The pointer to the data buffer to be written.
> > > > +  @param  NumberOfBytes    The number of bytes to written to the serial device.
> > > > +
> > > > +  @retval 0                NumberOfBytes is 0.
> > > > +  @retval >0               The number of bytes written to the serial device.
> > > > +                           If this value is less than NumberOfBytes, then the write operation failed.
> > > > +
> > > > +**/
> > > > +UINTN
> > > > +EFIAPI
> > > > +SerialPortWrite (
> > > > +  IN UINT8  *Buffer,
> > > > +  IN UINTN  NumberOfBytes
> > > > +  )
> > > > +{
> > > > +  SBI_RET  Ret;
> > > > +  UINTN    Index;
> > > > +
> > > > +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1,
> > > > + SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > > +      (Ret.Value != 0))
> > > > +  {
> > > > +    Ret = SbiCall (
> > > > +            SBI_EXT_DBCN,
> > > > +            SBI_EXT_DBCN_WRITE,
> > > > +            3,
> > > > +            NumberOfBytes,
> > > > +            ((UINTN)Buffer),
> > > > +            0
> > > > +            );
> > > > +    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
> > > > +      return 0;
> > > > +    }
> > > > +
> > > > +    return Ret.Value;
> > > > +  }
> > > > +
> > > > +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1,
> > > > + SBI_EXT_0_1_CONSOLE_PUTCHAR);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > > +      (Ret.Value != 0))
> > > > +  {
> > > > +    for (Index = 0; Index < NumberOfBytes; Index++) {
> > > > +      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
> > > > +    }
> > > > +
> > > > +    return Index;
> > > > +  }
> > > > +
> > > > +  /*
> > > > +   * Neither DBCN or legacy extension were present.
> > > > +   */
> > > > +  return NumberOfBytes;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Read data from serial device and save the datas in buffer.
> > > > +
> > > > +  Reads NumberOfBytes data bytes from a serial device into the
> > > > + buffer specified by Buffer. The number of bytes actually read is returned.
> > > > +  If the return value is less than NumberOfBytes, then the rest operation failed.
> > > > +  If Buffer is NULL, then ASSERT().
> > > > +  If NumberOfBytes is zero, then return 0.
> > > > +
> > > > +  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
> > > > +  @param  NumberOfBytes    The number of bytes which will be read.
> > > > +
> > > > +  @retval 0                Read data failed; No data is to be read.
> > > > +  @retval >0               The actual number of bytes read from serial device.
> > > > +
> > > > +**/
> > > > +UINTN
> > > > +EFIAPI
> > > > +SerialPortRead (
> > > > +  OUT UINT8  *Buffer,
> > > > +  IN  UINTN  NumberOfBytes
> > > > +  )
> > > > +{
> > > > +  return 0;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Polls a serial device to see if there is any data waiting to be read.
> > > > +
> > > > +  Polls a serial device to see if there is any data waiting to be read.
> > > > +  If there is data waiting to be read from the serial device, then TRUE is returned.
> > > > +  If there is no data waiting to be read from the serial device, then FALSE is returned.
> > > > +
> > > > +  @retval TRUE             Data is waiting to be read from the serial device.
> > > > +  @retval FALSE            There is no data waiting to be read from the serial device.
> > > > +
> > > > +**/
> > > > +BOOLEAN
> > > > +EFIAPI
> > > > +SerialPortPoll (
> > > > +  VOID
> > > > +  )
> > > > +{
> > > > +  return FALSE;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Sets the control bits on a serial device.
> > > > +
> > > > +  @param Control                Sets the bits of Control that are settable.
> > > > +
> > > > +  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
> > > > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > > > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > > > +
> > > > +**/
> > > > +RETURN_STATUS
> > > > +EFIAPI
> > > > +SerialPortSetControl (
> > > > +  IN UINT32  Control
> > > > +  )
> > > > +{
> > > > +  return RETURN_SUCCESS;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Retrieve the status of the control bits on a serial device.
> > > > +
> > > > +  @param Control                A pointer to return the current control signals from the serial device.
> > > > +
> > > > +  @retval RETURN_SUCCESS        The control bits were read from the serial device.
> > > > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > > > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > > > +
> > > > +**/
> > > > +RETURN_STATUS
> > > > +EFIAPI
> > > > +SerialPortGetControl (
> > > > +  OUT UINT32  *Control
> > > > +  )
> > > > +{
> > > > +  *Control = 0;
> > > > +  return RETURN_SUCCESS;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Sets the baud rate, receive FIFO depth, transmit/receice time
> > > > +out, parity,
> > > > +  data bits, and stop bits on a serial device.
> > > > +
> > > > +  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
> > > > +                            device's default interface speed.
> > > > +                            On output, the value actually set.
> > > > +  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
> > > > +                            serial interface. A ReceiveFifoDepth value of 0 will use
> > > > +                            the device's default FIFO depth.
> > > > +                            On output, the value actually set.
> > > > +  @param Timeout            The requested time out for a single character in microseconds.
> > > > +                            This timeout applies to both the transmit and receive side of the
> > > > +                            interface. A Timeout value of 0 will use the device's default time
> > > > +                            out value.
> > > > +                            On output, the value actually set.
> > > > +  @param Parity             The type of parity to use on this serial device. A Parity value of
> > > > +                            DefaultParity will use the device's default parity value.
> > > > +                            On output, the value actually set.
> > > > +  @param DataBits           The number of data bits to use on the serial device. A DataBits
> > > > +                            vaule of 0 will use the device's default data bit setting.
> > > > +                            On output, the value actually set.
> > > > +  @param StopBits           The number of stop bits to use on this serial device. A StopBits
> > > > +                            value of DefaultStopBits will use the device's default number of
> > > > +                            stop bits.
> > > > +                            On output, the value actually set.
> > > > +
> > > > +  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
> > > > +  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
> > > > +  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
> > > > +  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
> > > > +
> > > > +**/
> > > > +RETURN_STATUS
> > > > +EFIAPI
> > > > +SerialPortSetAttributes (
> > > > +  IN OUT UINT64              *BaudRate,
> > > > +  IN OUT UINT32              *ReceiveFifoDepth,
> > > > +  IN OUT UINT32              *Timeout,
> > > > +  IN OUT EFI_PARITY_TYPE     *Parity,
> > > > +  IN OUT UINT8               *DataBits,
> > > > +  IN OUT EFI_STOP_BITS_TYPE  *StopBits
> > > > +  )
> > > > +{
> > > > +  return RETURN_SUCCESS;
> > > > +}
> > > > diff --git
> > > > a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> > > > Po
> > > > rt
> > > > LibSbi.uni
> > > > b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerial
> > > > Po
> > > > rt
> > > > LibSbi.uni
> > > > new file mode 100644
> > > > index 000000000000..7b22caa5a090
> > > > --- /dev/null
> > > > +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSe
> > > > +++ ri
> > > > +++ al
> > > > +++ Po
> > > > +++ rtLibSbi.uni
> > > > @@ -0,0 +1,16 @@
> > > > +// /** @file
> > > > +// Serial Port Library backed by SBI console.
> > > > +//
> > > > +// Serial Port Library backed by SBI console.
> > > > +//
> > > > +// Copyright (c) 2023, Intel Corporation. All rights
> > > > +reserved.<BR> // // SPDX-License-Identifier: BSD-2-Clause-Patent
> > > > +// // **/
> > > > +
> > > > +
> > > > +#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
> > > > +
> > > > +#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
> > > > +
> > > > diff --git
> > > > a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> > > > Li
> > > > bS
> > > > bi.uni
> > > > b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPort
> > > > Li
> > > > bS
> > > > bi.uni
> > > > new file mode 100644
> > > > index 000000000000..7b22caa5a090
> > > > --- /dev/null
> > > > +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerial
> > > > +++ Po
> > > > +++ rt
> > > > +++ LibSbi.uni
> > > > @@ -0,0 +1,16 @@
> > > > +// /** @file
> > > > +// Serial Port Library backed by SBI console.
> > > > +//
> > > > +// Serial Port Library backed by SBI console.
> > > > +//
> > > > +// Copyright (c) 2023, Intel Corporation. All rights
> > > > +reserved.<BR> // // SPDX-License-Identifier: BSD-2-Clause-Patent
> > > > +// // **/
> > > > +
> > > > +
> > > > +#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
> > > > +
> > > > +#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
> > > > +
> > > > --
> > > > 2.25.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V
  2023-03-28 21:25                 ` Michael D Kinney
@ 2023-03-28 22:00                   ` Andrei Warkentin
  0 siblings, 0 replies; 14+ messages in thread
From: Andrei Warkentin @ 2023-03-28 22:00 UTC (permalink / raw)
  To: Kinney, Michael D, devel@edk2.groups.io; +Cc: Gao, Liming, Liu, Zhiguang

Thanks for the in-depth proposal, this is undoubtedly going to be very helpful for future reference.

Will rework and resend.

A

-----Original Message-----
From: Kinney, Michael D <michael.d.kinney@intel.com> 
Sent: Tuesday, March 28, 2023 4:26 PM
To: Warkentin, Andrei <andrei.warkentin@intel.com>; devel@edk2.groups.io
Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang <zhiguang.liu@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>
Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V

From looking at the details of your libs, it is not related to DXE.

It is SEC with no writable globals, or SEC with writable globals.

Both libs could be used in an SEC implementation that hands off to PEI or DXE.

You current proposed names are:
* SecPeiRiscVSerialPortLibSbi
* PrePiDxeRiscVSerialPortLibSbi

The current naming convention is one or more module types followed by the Library Class Name followed by an optional string that describes the services that the Library Class implementation depends.  You have used Sbi as this optional postfix.  However, you have two different SBI environments.  One where the code/data is running from RAM where global variables can be used and one where code/data is running from ROM where global variable can not be used.

How about the following names?
* SecPeiRiscVSerialPortLibSbi
* SecPeiDxeRiscVSerialPortLibSbiRam

In fact, both libs are of type BASE which means they are really compatible with all module types as long as the Sbi vs SbiRam difference is understood by the platform developer that has to set the lib mappings in the DSC file.

So a further refinement would be:
* BaseRiscVSerialPortLibSbi
* BaseRiscVSerialPortLibSbiRam

We usually do not put a CPU arch in the Library Instance name, but SBI is Unique to RiscV and the library services being used are from the RiscVSbiLib class.  Adding the full lib class name to the end adds the RiscV keyword to the library instance name.

* BaseSerialPortLibRiscVSbiLib
* BaseSerialPortLibRiscVSbiLibRam


Mike

> -----Original Message-----
> From: Warkentin, Andrei <andrei.warkentin@intel.com>
> Sent: Tuesday, March 28, 2023 1:20 PM
> To: Kinney, Michael D <michael.d.kinney@intel.com>; 
> devel@edk2.groups.io
> Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang 
> <zhiguang.liu@intel.com>
> Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for 
> RISC-V
> 
> So I'd be in support of formally defining a PrePi prefix. If we go 
> with Sec, then we'd have to come up with some other way of disambiguating "hand off to PEI" vs "hand off to DXE", and it seems like PrePi is already that.
> 
> What do you entail the process to be followed here? Is there value in 
> defining a dedicated PrePi module type (like SEC)? I'd be happy to propose the edk2 changes to support that. Can we decouple this work from these specific SerialPortLib contributions?
> 
> A
> 
> -----Original Message-----
> From: Kinney, Michael D <michael.d.kinney@intel.com>
> Sent: Tuesday, March 28, 2023 3:10 PM
> To: Warkentin, Andrei <andrei.warkentin@intel.com>; 
> devel@edk2.groups.io
> Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang 
> <zhiguang.liu@intel.com>; Kinney, Michael D 
> <michael.d.kinney@intel.com>
> Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for 
> RISC-V
> 
> I agree that moving into EmbeddedPkg does not make sense.
> 
> EmbeddedPkg is very old without many updates.  That package is not the best to use for examples.
> 
> I think you are suggesting that there is a difference in a library 
> that runs in an SEC env with intent to hand off to PEI and a library 
> that runs in an SEC env with intent to handoff to DXE.  They are both SEC module types and the execution environment is the same.  The only difference is how they prepare for the handoff.
> 
> My interest here is to make sure the prefixes are used consistently 
> and if we truly have a new prefix type, then to clearly define it so it can be used consistently going forward.
> 
> Mike
> 
> > -----Original Message-----
> > From: Warkentin, Andrei <andrei.warkentin@intel.com>
> > Sent: Tuesday, March 28, 2023 1:00 PM
> > To: Kinney, Michael D <michael.d.kinney@intel.com>; 
> > devel@edk2.groups.io
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang 
> > <zhiguang.liu@intel.com>
> > Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for 
> > RISC-V
> >
> > Sec isn't a good fit, because it has implications on the 
> > environment, which is why the contribution also includes a separate SecPei variant as well.
> >
> > In naming, I followed other edk2 examples such as 
> > ./EmbeddedPkg/Library/PrePiExtractGuidedSectionLib and 
> > ./EmbeddedPkg/Library/PrePiMemoryAllocationLi. I would prefer not to 
> > move one of these libraries into EmbeddedPkg - that wouldn't
> make much sense to me (esp. since there's nothing "embedded" about the approach per se, it can be used on any implementation).
> >
> > A
> > -----Original Message-----
> > From: Kinney, Michael D <michael.d.kinney@intel.com>
> > Sent: Monday, March 27, 2023 11:32 AM
> > To: Warkentin, Andrei <andrei.warkentin@intel.com>; 
> > devel@edk2.groups.io
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang 
> > <zhiguang.liu@intel.com>; Kinney, Michael D 
> > <michael.d.kinney@intel.com>
> > Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for 
> > RISC-V
> >
> > I think that would still be called Sec.  Are there any issues with using Sec prefix?
> >
> > Mike
> >
> > > -----Original Message-----
> > > From: Warkentin, Andrei <andrei.warkentin@intel.com>
> > > Sent: Thursday, March 16, 2023 10:45 PM
> > > To: Kinney, Michael D <michael.d.kinney@intel.com>; 
> > > devel@edk2.groups.io
> > > Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang 
> > > <zhiguang.liu@intel.com>
> > > Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib 
> > > for RISC-V
> > >
> > > PrePi refers to a SEC-like stage in a Tiano implementation that 
> > > skips the PEI stage. Reasons for skipping PEI usually center 
> > > around memory initialization being done at an earlier stage of 
> > > firmware initialization - for example, prior or part of TF-A 
> > > firmware on
> > > AArch64 or prior or part of SBI implementation on RISC-V.
> > >
> > > E.g. ArmVirtPkg/PrePi and OvmfPkg/RiscVVirt/Sec. The later is not 
> > > a "SEC" in the send of handing off to PeiCore, but instead hands off to DxeCore.
> > >
> > > There is no base type for PrePi. The type is still SEC. But it a) assumes RAM is available b) builds HOBs and hands-off to DXE.
> > >
> > > The name has been around for at least half a decade now and making 
> > > it a part of the SerialPortLib component name is important as it signifies the scenario where the code can apply.
> > >
> > > A
> > >
> > > -----Original Message-----
> > > From: Kinney, Michael D <michael.d.kinney@intel.com>
> > > Sent: Friday, March 17, 2023 12:01 AM
> > > To: Warkentin, Andrei <andrei.warkentin@intel.com>; 
> > > devel@edk2.groups.io
> > > Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Liu, Zhiguang 
> > > <zhiguang.liu@intel.com>; Kinney, Michael D 
> > > <michael.d.kinney@intel.com>
> > > Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib 
> > > for RISC-V
> > >
> > > What is the definition of "PrePI"?  That is not a prefix I have seen used before.
> > >
> > > Mike
> > >
> > > > -----Original Message-----
> > > > From: Warkentin, Andrei <andrei.warkentin@intel.com>
> > > > Sent: Thursday, March 16, 2023 9:53 PM
> > > > To: devel@edk2.groups.io
> > > > Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming 
> > > > <gaoliming@byosoft.com.cn>; Liu, Zhiguang 
> > > > <zhiguang.liu@intel.com>
> > > > Subject: RE: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib 
> > > > for RISC-V
> > > >
> > > > Hi folks,
> > > >
> > > > Can I get a review for the changes to MdePkg.dsc to include the 
> > > > new libs on RISC-V? That's the minor thing preventing this patch set from being merged 😊.
> > > >
> > > > A
> > > >
> > > > -----Original Message-----
> > > > From: Warkentin, Andrei <andrei.warkentin@intel.com>
> > > > Sent: Friday, March 10, 2023 11:05 AM
> > > > To: devel@edk2.groups.io
> > > > Cc: Warkentin, Andrei <andrei.warkentin@intel.com>; Daniel 
> > > > Schaefer <git@danielschaefer.me>; Sunil V L 
> > > > <sunilvl@ventanamicro.com>; Kinney, Michael D 
> > > > <michael.d.kinney@intel.com>; Gao, Liming 
> > > > <gaoliming@byosoft.com.cn>; Liu, Zhiguang 
> > > > <zhiguang.liu@intel.com>; Gerd Hoffmann <kraxel@redhat.com>
> > > > Subject: [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for 
> > > > RISC-V
> > > >
> > > > These are implementations of SerialPortLib using SBI console services.
> > > > - SecPeiRiscVSerialPortLibSbi is appropriate for SEC/PEI (XIP) 
> > > > environments
> > > > - PrePiDxeRiscVSerialPortLibSbi is appropriate for PrePI/DXE 
> > > > environments
> > > >
> > > > Tested with:
> > > > - Qemu RiscVVirt (non-DBCN case, backed by UART)
> > > > - TinyEMU + RiscVVirt (non-DBCN case, HTIF)
> > > > - TinyEMU + RiscVVirt (DBCN case, HTIF)
> > > >
> > > > Cc: Daniel Schaefer <git@danielschaefer.me>
> > > > Cc: Sunil V L <sunilvl@ventanamicro.com>
> > > > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > > > Cc: Liming Gao <gaoliming@byosoft.com.cn>
> > > > Cc: Zhiguang Liu <zhiguang.liu@intel.com>
> > > > Acked-by: Gerd Hoffmann <kraxel@redhat.com>
> > > > Signed-off-by: Andrei Warkentin <andrei.warkentin@intel.com>
> > > > ---
> > > >  MdePkg/MdePkg.dsc                                                              |   2 +
> > > >  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.inf |  36 +++
> > > >  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.inf     |  39 +++
> > > >  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.c   | 288 ++++++++++++++++++++
> > > >  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.c       | 233 ++++++++++++++++
> > > >  MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSerialPortLibSbi.uni |  16 ++
> > > >  MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPortLibSbi.uni     |  16 ++
> > > >  7 files changed, 630 insertions(+)
> > > >
> > > > diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index 
> > > > 0ac7618b4623..fb31349042fb 100644
> > > > --- a/MdePkg/MdePkg.dsc
> > > > +++ b/MdePkg/MdePkg.dsc
> > > > @@ -192,5 +192,7 @@ [Components.ARM, Components.AARCH64]
> > > >
> > > >  [Components.RISCV64]
> > > >    MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf
> > > > +
> > > > + MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPo
> > > > + rt
> > > > + Li
> > > > + bS
> > > > + bi
> > > > + .inf
> > > > + MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSeri
> > > > + al
> > > > + Po
> > > > + rt
> > > > + Li
> > > > + bSbi.inf
> > > >
> > > >  [BuildOptions]
> > > > diff --git
> > > > a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSeri
> > > > al
> > > > Po
> > > > rt
> > > > LibSbi.inf
> > > > b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSeri
> > > > al
> > > > Po
> > > > rt
> > > > LibSbi.inf
> > > > new file mode 100644
> > > > index 000000000000..1d8544756bb9
> > > > --- /dev/null
> > > > +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscV
> > > > +++ Se
> > > > +++ ri
> > > > +++ al
> > > > +++ Po
> > > > +++ rtLibSbi.inf
> > > > @@ -0,0 +1,36 @@
> > > > +## @file
> > > > +#  Serial Port Library backed by SBI console.
> > > > +#
> > > > +#  Meant for PrePi and DXE environments (where globals are allowed).
> > > > +See #  SecPeiSerialPortLibRiscVSbi for a reduced variant 
> > > > +appropriate for SEC #  and PEI (XIP) environments.
> > > > +#
> > > > +#  Copyright (c) 2023, Intel Corporation. All rights 
> > > > +reserved.<BR> # #
> > > > +SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> > > > +
> > > > +[Defines]
> > > > +  INF_VERSION                    = 0x0001001B
> > > > +  BASE_NAME                      = PrePiDxeRiscVSerialPortLibSbi
> > > > +  MODULE_UNI_FILE                = PrePiDxeRiscVSerialPortLibSbi.uni
> > > > +  FILE_GUID                      = 872af743-ab56-45b4-a065-602567f4820c
> > > > +  MODULE_TYPE                    = BASE
> > > > +  VERSION_STRING                 = 1.0
> > > > +  LIBRARY_CLASS                  = SerialPortLib | SEC DXE_CORE DXE_DRIVER DXE_RUNTIME_DRIVER UEFI_DRIVER UEFI_APPLICATION
> > > > +
> > > > +
> > > > +#
> > > > +#  VALID_ARCHITECTURES           = RISCV64
> > > > +#
> > > > +
> > > > +[Sources]
> > > > +  PrePiDxeRiscVSerialPortLibSbi.c
> > > > +
> > > > +[Packages]
> > > > +  MdePkg/MdePkg.dec
> > > > +
> > > > +[LibraryClasses]
> > > > +  RiscVSbiLib
> > > > diff --git
> > > > a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPo
> > > > rt
> > > > Li
> > > > bS
> > > > bi.inf
> > > > b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPo
> > > > rt
> > > > Li
> > > > bS
> > > > bi.inf
> > > > new file mode 100644
> > > > index 000000000000..7bc7cd47c11c
> > > > --- /dev/null
> > > > +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSeri
> > > > +++ al
> > > > +++ Po
> > > > +++ rt
> > > > +++ Li
> > > > +++ bSbi.inf
> > > > @@ -0,0 +1,39 @@
> > > > +## @file
> > > > +#  Serial Port Library backed by SBI console.
> > > > +#
> > > > +#  Meant for SEC and PEI (XIP) environments.
> > > > +#
> > > > +#  Due to limitations of SBI console interface and XIP 
> > > > +environments # (on use of globals), this library instance does 
> > > > +not implement reading # and polling the serial port. See 
> > > > +PrePiDxeSerialPortLibRiscVSbi for # the full-featured variant meant for PrePi and DXE environments.
> > > > +#
> > > > +#  Copyright (c) 2023, Intel Corporation. All rights 
> > > > +reserved.<BR> # #
> > > > +SPDX-License-Identifier: BSD-2-Clause-Patent # # ##
> > > > +
> > > > +[Defines]
> > > > +  INF_VERSION                    = 0x0001001B
> > > > +  BASE_NAME                      = SecPeiRiscVSerialPortLibSbi
> > > > +  MODULE_UNI_FILE                = SecPeiRiscVSerialPortLibSbi.uni
> > > > +  FILE_GUID                      = 639fad38-4bfd-4eb9-9f09-e97c7947d480
> > > > +  MODULE_TYPE                    = BASE
> > > > +  VERSION_STRING                 = 1.0
> > > > +  LIBRARY_CLASS                  = SerialPortLib | SEC PEI_CORE PEIM
> > > > +
> > > > +
> > > > +#
> > > > +#  VALID_ARCHITECTURES           = RISCV64
> > > > +#
> > > > +
> > > > +[Sources]
> > > > +  SecPeiRiscVSerialPortLibSbi.c
> > > > +
> > > > +[Packages]
> > > > +  MdePkg/MdePkg.dec
> > > > +
> > > > +[LibraryClasses]
> > > > +  RiscVSbiLib
> > > > diff --git
> > > > a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSeri
> > > > al
> > > > Po
> > > > rt
> > > > LibSbi.c
> > > > b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSeri
> > > > al
> > > > Po
> > > > rt
> > > > LibSbi.c
> > > > new file mode 100644
> > > > index 000000000000..058a6573ef2d
> > > > --- /dev/null
> > > > +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscV
> > > > +++ Se
> > > > +++ ri
> > > > +++ al
> > > > +++ Po
> > > > +++ rtLibSbi.c
> > > > @@ -0,0 +1,288 @@
> > > > +/** @file
> > > > +  Serial Port Library backed by SBI console.
> > > > +
> > > > +  Meant for PrePi and DXE environments (where globals are allowed).
> > > > + See SecPeiSerialPortLibRiscVSbi for a reduced variant 
> > > > + appropriate for SEC and PEI (XIP) environments.
> > > > +
> > > > +  Copyright (c) 2023, Intel Corporation. All rights 
> > > > + reserved.<BR>
> > > > +  SPDX-License-Identifier: BSD-2-Clause-Patent
> > > > +
> > > > +**/
> > > > +
> > > > +#include <Base.h>
> > > > +#include <Library/SerialPortLib.h> #include 
> > > > +<Library/BaseRiscVSbiLib.h>
> > > > +
> > > > +STATIC BOOLEAN  mHaveDbcn          = FALSE;
> > > > +STATIC BOOLEAN  mHaveLegacyPutchar = FALSE; STATIC BOOLEAN 
> > > > +mHaveLegacyGetchar = FALSE;
> > > > +STATIC INT64    mLastGetChar       = -1;
> > > > +
> > > > +/**
> > > > +  Initialize the serial device hardware.
> > > > +
> > > > +  If no initialization is required, then return RETURN_SUCCESS.
> > > > +  If the serial device was successfully initialized, then return RETURN_SUCCESS.
> > > > +  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
> > > > +
> > > > +  @retval RETURN_SUCCESS        The serial device was initialized.
> > > > +  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
> > > > +
> > > > +**/
> > > > +RETURN_STATUS
> > > > +EFIAPI
> > > > +SerialPortInitialize (
> > > > +  VOID
> > > > +  )
> > > > +{
> > > > +  SBI_RET  Ret;
> > > > +
> > > > +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, 
> > > > + SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > > +      (Ret.Value != 0))
> > > > +  {
> > > > +    mHaveDbcn = TRUE;
> > > > +  } else {
> > > > +    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_PUTCHAR);
> > > > +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > > +        (Ret.Value != 0))
> > > > +    {
> > > > +      mHaveLegacyPutchar = TRUE;
> > > > +    }
> > > > +
> > > > +    Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, SBI_EXT_0_1_CONSOLE_GETCHAR);
> > > > +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > > +        (Ret.Value != 0))
> > > > +    {
> > > > +      mHaveLegacyGetchar = TRUE;
> > > > +    }
> > > > +  }
> > > > +
> > > > +  return RETURN_SUCCESS;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Write data from buffer to serial device.
> > > > +
> > > > +  Writes NumberOfBytes data bytes from Buffer to the serial device.
> > > > +  The number of bytes actually written to the serial device is returned.
> > > > +  If the return value is less than NumberOfBytes, then the write operation failed.
> > > > +  If Buffer is NULL, then ASSERT().
> > > > +  If NumberOfBytes is zero, then return 0.
> > > > +
> > > > +  @param  Buffer           The pointer to the data buffer to be written.
> > > > +  @param  NumberOfBytes    The number of bytes to written to the serial device.
> > > > +
> > > > +  @retval 0                NumberOfBytes is 0.
> > > > +  @retval >0               The number of bytes written to the serial device.
> > > > +                           If this value is less than NumberOfBytes, then the write operation failed.
> > > > +
> > > > +**/
> > > > +UINTN
> > > > +EFIAPI
> > > > +SerialPortWrite (
> > > > +  IN UINT8  *Buffer,
> > > > +  IN UINTN  NumberOfBytes
> > > > +  )
> > > > +{
> > > > +  UINTN  Index;
> > > > +
> > > > +  if (mHaveDbcn) {
> > > > +    SBI_RET  Ret;
> > > > +    Ret = SbiCall (
> > > > +            SBI_EXT_DBCN,
> > > > +            SBI_EXT_DBCN_WRITE,
> > > > +            3,
> > > > +            NumberOfBytes,
> > > > +            ((UINTN)Buffer),
> > > > +            0
> > > > +            );
> > > > +    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
> > > > +      return 0;
> > > > +    }
> > > > +
> > > > +    return Ret.Value;
> > > > +  } else if (mHaveLegacyPutchar) {
> > > > +    for (Index = 0; Index < NumberOfBytes; Index++) {
> > > > +      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
> > > > +    }
> > > > +
> > > > +    return Index;
> > > > +  }
> > > > +
> > > > +  /*
> > > > +   * Neither DBCN or legacy extension were present.
> > > > +   */
> > > > +  return NumberOfBytes;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Read data from serial device and save the datas in buffer.
> > > > +
> > > > +  Reads NumberOfBytes data bytes from a serial device into the 
> > > > + buffer specified by Buffer. The number of bytes actually read is returned.
> > > > +  If the return value is less than NumberOfBytes, then the rest operation failed.
> > > > +  If Buffer is NULL, then ASSERT().
> > > > +  If NumberOfBytes is zero, then return 0.
> > > > +
> > > > +  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
> > > > +  @param  NumberOfBytes    The number of bytes which will be read.
> > > > +
> > > > +  @retval 0                Read data failed; No data is to be read.
> > > > +  @retval >0               The actual number of bytes read from serial device.
> > > > +
> > > > +**/
> > > > +UINTN
> > > > +EFIAPI
> > > > +SerialPortRead (
> > > > +  OUT UINT8  *Buffer,
> > > > +  IN  UINTN  NumberOfBytes
> > > > +  )
> > > > +{
> > > > +  UINTN  Index;
> > > > +
> > > > +  Index = 0;
> > > > +  while ((Index < NumberOfBytes) && SerialPortPoll ()) {
> > > > +    Buffer[Index++] = (UINT8)mLastGetChar;
> > > > +    mLastGetChar    = -1;
> > > > +  }
> > > > +
> > > > +  return Index;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Polls a serial device to see if there is any data waiting to be read.
> > > > +
> > > > +  Polls a serial device to see if there is any data waiting to be read.
> > > > +  If there is data waiting to be read from the serial device, then TRUE is returned.
> > > > +  If there is no data waiting to be read from the serial device, then FALSE is returned.
> > > > +
> > > > +  @retval TRUE             Data is waiting to be read from the serial device.
> > > > +  @retval FALSE            There is no data waiting to be read from the serial device.
> > > > +
> > > > +**/
> > > > +BOOLEAN
> > > > +EFIAPI
> > > > +SerialPortPoll (
> > > > +  VOID
> > > > +  )
> > > > +{
> > > > +  /*
> > > > +   * Careful. OpenSBI with HTIF console will return -1 followed by -2
> > > > +   * if there is no character received. So just check for values >= 0.
> > > > +   */
> > > > +
> > > > +  if (mLastGetChar >= 0) {
> > > > +    return TRUE;
> > > > +  }
> > > > +
> > > > +  if (mHaveDbcn) {
> > > > +    UINT8    Buffer;
> > > > +    SBI_RET  Ret;
> > > > +
> > > > +    Ret = SbiCall (
> > > > +            SBI_EXT_DBCN,
> > > > +            SBI_EXT_DBCN_READ,
> > > > +            3,
> > > > +            1,
> > > > +            ((UINTN)&Buffer),
> > > > +            0
> > > > +            );
> > > > +    if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > > +        (Ret.Value == 1))
> > > > +    {
> > > > +      mLastGetChar = Buffer;
> > > > +    }
> > > > +  } else if (mHaveLegacyGetchar) {
> > > > +    mLastGetChar = (INT64)SbiCall (SBI_EXT_0_1_CONSOLE_GETCHAR, 
> > > > + 0, 0).Error;  }
> > > > +
> > > > +  return mLastGetChar >= 0;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Sets the control bits on a serial device.
> > > > +
> > > > +  @param Control                Sets the bits of Control that are settable.
> > > > +
> > > > +  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
> > > > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > > > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > > > +
> > > > +**/
> > > > +RETURN_STATUS
> > > > +EFIAPI
> > > > +SerialPortSetControl (
> > > > +  IN UINT32  Control
> > > > +  )
> > > > +{
> > > > +  return RETURN_SUCCESS;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Retrieve the status of the control bits on a serial device.
> > > > +
> > > > +  @param Control                A pointer to return the current control signals from the serial device.
> > > > +
> > > > +  @retval RETURN_SUCCESS        The control bits were read from the serial device.
> > > > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > > > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > > > +
> > > > +**/
> > > > +RETURN_STATUS
> > > > +EFIAPI
> > > > +SerialPortGetControl (
> > > > +  OUT UINT32  *Control
> > > > +  )
> > > > +{
> > > > +  *Control = 0;
> > > > +  return RETURN_SUCCESS;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Sets the baud rate, receive FIFO depth, transmit/receice time 
> > > > +out, parity,
> > > > +  data bits, and stop bits on a serial device.
> > > > +
> > > > +  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
> > > > +                            device's default interface speed.
> > > > +                            On output, the value actually set.
> > > > +  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
> > > > +                            serial interface. A ReceiveFifoDepth value of 0 will use
> > > > +                            the device's default FIFO depth.
> > > > +                            On output, the value actually set.
> > > > +  @param Timeout            The requested time out for a single character in microseconds.
> > > > +                            This timeout applies to both the transmit and receive side of the
> > > > +                            interface. A Timeout value of 0 will use the device's default time
> > > > +                            out value.
> > > > +                            On output, the value actually set.
> > > > +  @param Parity             The type of parity to use on this serial device. A Parity value of
> > > > +                            DefaultParity will use the device's default parity value.
> > > > +                            On output, the value actually set.
> > > > +  @param DataBits           The number of data bits to use on the serial device. A DataBits
> > > > +                            vaule of 0 will use the device's default data bit setting.
> > > > +                            On output, the value actually set.
> > > > +  @param StopBits           The number of stop bits to use on this serial device. A StopBits
> > > > +                            value of DefaultStopBits will use the device's default number of
> > > > +                            stop bits.
> > > > +                            On output, the value actually set.
> > > > +
> > > > +  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
> > > > +  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
> > > > +  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
> > > > +  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
> > > > +
> > > > +**/
> > > > +RETURN_STATUS
> > > > +EFIAPI
> > > > +SerialPortSetAttributes (
> > > > +  IN OUT UINT64              *BaudRate,
> > > > +  IN OUT UINT32              *ReceiveFifoDepth,
> > > > +  IN OUT UINT32              *Timeout,
> > > > +  IN OUT EFI_PARITY_TYPE     *Parity,
> > > > +  IN OUT UINT8               *DataBits,
> > > > +  IN OUT EFI_STOP_BITS_TYPE  *StopBits
> > > > +  )
> > > > +{
> > > > +  return RETURN_SUCCESS;
> > > > +}
> > > > diff --git
> > > > a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPo
> > > > rt
> > > > Li
> > > > bS
> > > > bi.c
> > > > b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPo
> > > > rt
> > > > Li
> > > > bS
> > > > bi.c
> > > > new file mode 100644
> > > > index 000000000000..0ad5931be3ae
> > > > --- /dev/null
> > > > +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSeri
> > > > +++ al
> > > > +++ Po
> > > > +++ rt
> > > > +++ Li
> > > > +++ bSbi.c
> > > > @@ -0,0 +1,233 @@
> > > > +/** @file
> > > > +  Serial Port Library backed by SBI console.
> > > > +
> > > > +  Meant for SEC and PEI (XIP) environments.
> > > > +
> > > > +  Due to limitations of SBI console interface and XIP 
> > > > + environments (on use of globals), this library instance does 
> > > > + not implement reading  and polling the serial port. See 
> > > > + PrePiDxeSerialPortLibRiscVSbi for  the full-featured variant meant for PrePi and DXE environments.
> > > > +
> > > > +  Copyright (c) 2023, Intel Corporation. All rights 
> > > > + reserved.<BR>
> > > > +  SPDX-License-Identifier: BSD-2-Clause-Patent
> > > > +
> > > > +**/
> > > > +
> > > > +#include <Base.h>
> > > > +#include <Library/SerialPortLib.h> #include 
> > > > +<Library/BaseRiscVSbiLib.h>
> > > > +
> > > > +/**
> > > > +  Initialize the serial device hardware.
> > > > +
> > > > +  If no initialization is required, then return RETURN_SUCCESS.
> > > > +  If the serial device was successfully initialized, then return RETURN_SUCCESS.
> > > > +  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
> > > > +
> > > > +  @retval RETURN_SUCCESS        The serial device was initialized.
> > > > +  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
> > > > +
> > > > +**/
> > > > +RETURN_STATUS
> > > > +EFIAPI
> > > > +SerialPortInitialize (
> > > > +  VOID
> > > > +  )
> > > > +{
> > > > +  return RETURN_SUCCESS;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Write data from buffer to serial device.
> > > > +
> > > > +  Writes NumberOfBytes data bytes from Buffer to the serial device.
> > > > +  The number of bytes actually written to the serial device is returned.
> > > > +  If the return value is less than NumberOfBytes, then the write operation failed.
> > > > +  If Buffer is NULL, then ASSERT().
> > > > +  If NumberOfBytes is zero, then return 0.
> > > > +
> > > > +  @param  Buffer           The pointer to the data buffer to be written.
> > > > +  @param  NumberOfBytes    The number of bytes to written to the serial device.
> > > > +
> > > > +  @retval 0                NumberOfBytes is 0.
> > > > +  @retval >0               The number of bytes written to the serial device.
> > > > +                           If this value is less than NumberOfBytes, then the write operation failed.
> > > > +
> > > > +**/
> > > > +UINTN
> > > > +EFIAPI
> > > > +SerialPortWrite (
> > > > +  IN UINT8  *Buffer,
> > > > +  IN UINTN  NumberOfBytes
> > > > +  )
> > > > +{
> > > > +  SBI_RET  Ret;
> > > > +  UINTN    Index;
> > > > +
> > > > +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, 
> > > > + SBI_EXT_DBCN);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > > +      (Ret.Value != 0))
> > > > +  {
> > > > +    Ret = SbiCall (
> > > > +            SBI_EXT_DBCN,
> > > > +            SBI_EXT_DBCN_WRITE,
> > > > +            3,
> > > > +            NumberOfBytes,
> > > > +            ((UINTN)Buffer),
> > > > +            0
> > > > +            );
> > > > +    if (TranslateError (Ret.Error) != EFI_SUCCESS) {
> > > > +      return 0;
> > > > +    }
> > > > +
> > > > +    return Ret.Value;
> > > > +  }
> > > > +
> > > > +  Ret = SbiCall (SBI_EXT_BASE, SBI_EXT_BASE_PROBE_EXT, 1, 
> > > > + SBI_EXT_0_1_CONSOLE_PUTCHAR);  if ((TranslateError (Ret.Error) == EFI_SUCCESS) &&
> > > > +      (Ret.Value != 0))
> > > > +  {
> > > > +    for (Index = 0; Index < NumberOfBytes; Index++) {
> > > > +      SbiCall (SBI_EXT_0_1_CONSOLE_PUTCHAR, 0, 1, Buffer[Index]);
> > > > +    }
> > > > +
> > > > +    return Index;
> > > > +  }
> > > > +
> > > > +  /*
> > > > +   * Neither DBCN or legacy extension were present.
> > > > +   */
> > > > +  return NumberOfBytes;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Read data from serial device and save the datas in buffer.
> > > > +
> > > > +  Reads NumberOfBytes data bytes from a serial device into the 
> > > > + buffer specified by Buffer. The number of bytes actually read is returned.
> > > > +  If the return value is less than NumberOfBytes, then the rest operation failed.
> > > > +  If Buffer is NULL, then ASSERT().
> > > > +  If NumberOfBytes is zero, then return 0.
> > > > +
> > > > +  @param  Buffer           The pointer to the data buffer to store the data read from the serial device.
> > > > +  @param  NumberOfBytes    The number of bytes which will be read.
> > > > +
> > > > +  @retval 0                Read data failed; No data is to be read.
> > > > +  @retval >0               The actual number of bytes read from serial device.
> > > > +
> > > > +**/
> > > > +UINTN
> > > > +EFIAPI
> > > > +SerialPortRead (
> > > > +  OUT UINT8  *Buffer,
> > > > +  IN  UINTN  NumberOfBytes
> > > > +  )
> > > > +{
> > > > +  return 0;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Polls a serial device to see if there is any data waiting to be read.
> > > > +
> > > > +  Polls a serial device to see if there is any data waiting to be read.
> > > > +  If there is data waiting to be read from the serial device, then TRUE is returned.
> > > > +  If there is no data waiting to be read from the serial device, then FALSE is returned.
> > > > +
> > > > +  @retval TRUE             Data is waiting to be read from the serial device.
> > > > +  @retval FALSE            There is no data waiting to be read from the serial device.
> > > > +
> > > > +**/
> > > > +BOOLEAN
> > > > +EFIAPI
> > > > +SerialPortPoll (
> > > > +  VOID
> > > > +  )
> > > > +{
> > > > +  return FALSE;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Sets the control bits on a serial device.
> > > > +
> > > > +  @param Control                Sets the bits of Control that are settable.
> > > > +
> > > > +  @retval RETURN_SUCCESS        The new control bits were set on the serial device.
> > > > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > > > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > > > +
> > > > +**/
> > > > +RETURN_STATUS
> > > > +EFIAPI
> > > > +SerialPortSetControl (
> > > > +  IN UINT32  Control
> > > > +  )
> > > > +{
> > > > +  return RETURN_SUCCESS;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Retrieve the status of the control bits on a serial device.
> > > > +
> > > > +  @param Control                A pointer to return the current control signals from the serial device.
> > > > +
> > > > +  @retval RETURN_SUCCESS        The control bits were read from the serial device.
> > > > +  @retval RETURN_UNSUPPORTED    The serial device does not support this operation.
> > > > +  @retval RETURN_DEVICE_ERROR   The serial device is not functioning correctly.
> > > > +
> > > > +**/
> > > > +RETURN_STATUS
> > > > +EFIAPI
> > > > +SerialPortGetControl (
> > > > +  OUT UINT32  *Control
> > > > +  )
> > > > +{
> > > > +  *Control = 0;
> > > > +  return RETURN_SUCCESS;
> > > > +}
> > > > +
> > > > +/**
> > > > +  Sets the baud rate, receive FIFO depth, transmit/receice time 
> > > > +out, parity,
> > > > +  data bits, and stop bits on a serial device.
> > > > +
> > > > +  @param BaudRate           The requested baud rate. A BaudRate value of 0 will use the
> > > > +                            device's default interface speed.
> > > > +                            On output, the value actually set.
> > > > +  @param ReveiveFifoDepth   The requested depth of the FIFO on the receive side of the
> > > > +                            serial interface. A ReceiveFifoDepth value of 0 will use
> > > > +                            the device's default FIFO depth.
> > > > +                            On output, the value actually set.
> > > > +  @param Timeout            The requested time out for a single character in microseconds.
> > > > +                            This timeout applies to both the transmit and receive side of the
> > > > +                            interface. A Timeout value of 0 will use the device's default time
> > > > +                            out value.
> > > > +                            On output, the value actually set.
> > > > +  @param Parity             The type of parity to use on this serial device. A Parity value of
> > > > +                            DefaultParity will use the device's default parity value.
> > > > +                            On output, the value actually set.
> > > > +  @param DataBits           The number of data bits to use on the serial device. A DataBits
> > > > +                            vaule of 0 will use the device's default data bit setting.
> > > > +                            On output, the value actually set.
> > > > +  @param StopBits           The number of stop bits to use on this serial device. A StopBits
> > > > +                            value of DefaultStopBits will use the device's default number of
> > > > +                            stop bits.
> > > > +                            On output, the value actually set.
> > > > +
> > > > +  @retval RETURN_SUCCESS            The new attributes were set on the serial device.
> > > > +  @retval RETURN_UNSUPPORTED        The serial device does not support this operation.
> > > > +  @retval RETURN_INVALID_PARAMETER  One or more of the attributes has an unsupported value.
> > > > +  @retval RETURN_DEVICE_ERROR       The serial device is not functioning correctly.
> > > > +
> > > > +**/
> > > > +RETURN_STATUS
> > > > +EFIAPI
> > > > +SerialPortSetAttributes (
> > > > +  IN OUT UINT64              *BaudRate,
> > > > +  IN OUT UINT32              *ReceiveFifoDepth,
> > > > +  IN OUT UINT32              *Timeout,
> > > > +  IN OUT EFI_PARITY_TYPE     *Parity,
> > > > +  IN OUT UINT8               *DataBits,
> > > > +  IN OUT EFI_STOP_BITS_TYPE  *StopBits
> > > > +  )
> > > > +{
> > > > +  return RETURN_SUCCESS;
> > > > +}
> > > > diff --git
> > > > a/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSeri
> > > > al
> > > > Po
> > > > rt
> > > > LibSbi.uni
> > > > b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscVSeri
> > > > al
> > > > Po
> > > > rt
> > > > LibSbi.uni
> > > > new file mode 100644
> > > > index 000000000000..7b22caa5a090
> > > > --- /dev/null
> > > > +++ b/MdePkg/Library/PrePiDxeRiscVSerialPortLibSbi/PrePiDxeRiscV
> > > > +++ Se
> > > > +++ ri
> > > > +++ al
> > > > +++ Po
> > > > +++ rtLibSbi.uni
> > > > @@ -0,0 +1,16 @@
> > > > +// /** @file
> > > > +// Serial Port Library backed by SBI console.
> > > > +//
> > > > +// Serial Port Library backed by SBI console.
> > > > +//
> > > > +// Copyright (c) 2023, Intel Corporation. All rights 
> > > > +reserved.<BR> // // SPDX-License-Identifier: 
> > > > +BSD-2-Clause-Patent // // **/
> > > > +
> > > > +
> > > > +#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
> > > > +
> > > > +#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
> > > > +
> > > > diff --git
> > > > a/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPo
> > > > rt
> > > > Li
> > > > bS
> > > > bi.uni
> > > > b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSerialPo
> > > > rt
> > > > Li
> > > > bS
> > > > bi.uni
> > > > new file mode 100644
> > > > index 000000000000..7b22caa5a090
> > > > --- /dev/null
> > > > +++ b/MdePkg/Library/SecPeiRiscVSerialPortLibSbi/SecPeiRiscVSeri
> > > > +++ al
> > > > +++ Po
> > > > +++ rt
> > > > +++ LibSbi.uni
> > > > @@ -0,0 +1,16 @@
> > > > +// /** @file
> > > > +// Serial Port Library backed by SBI console.
> > > > +//
> > > > +// Serial Port Library backed by SBI console.
> > > > +//
> > > > +// Copyright (c) 2023, Intel Corporation. All rights 
> > > > +reserved.<BR> // // SPDX-License-Identifier: 
> > > > +BSD-2-Clause-Patent // // **/
> > > > +
> > > > +
> > > > +#string STR_MODULE_ABSTRACT             #language en-US "Serial Port Library backed by SBI console"
> > > > +
> > > > +#string STR_MODULE_DESCRIPTION          #language en-US "Serial Port Library backed by SBI console."
> > > > +
> > > > --
> > > > 2.25.1


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2023-03-28 22:00 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-03-10 17:04 [PATCH v5 0/3] RISC-V SBI-backed SerialLib Andrei Warkentin
2023-03-10 17:04 ` [PATCH v5 1/3] MdePkg: BaseRiscVSbiLib: make more useful to consumers Andrei Warkentin
2023-03-10 17:04 ` [PATCH v5 2/3] MdePkg: add SBI-based SerialPortLib for RISC-V Andrei Warkentin
2023-03-17  4:53   ` Andrei Warkentin
2023-03-17  5:00     ` Michael D Kinney
2023-03-17  5:44       ` Andrei Warkentin
2023-03-27 16:32         ` Michael D Kinney
2023-03-28 20:00           ` Andrei Warkentin
2023-03-28 20:09             ` Michael D Kinney
2023-03-28 20:20               ` Andrei Warkentin
2023-03-28 21:25                 ` Michael D Kinney
2023-03-28 22:00                   ` Andrei Warkentin
     [not found]       ` <174D1E9AD1BD3A96.10271@groups.io>
2023-03-27 16:10         ` [edk2-devel] " Andrei Warkentin
2023-03-10 17:04 ` [PATCH v5 3/3] OvmfPkg: RiscVVirt: Add missing SerialPortInitialize to Sec Andrei Warkentin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox