public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Sunil V L" <sunilvl@ventanamicro.com>
To: devel@edk2.groups.io, michael.d.kinney@intel.com
Cc: "Gao, Liming" <gaoliming@byosoft.com.cn>,
	"Liu, Zhiguang" <zhiguang.liu@intel.com>,
	Abner Chang <abner.chang@amd.com>
Subject: Re: [edk2-devel] [edk2-staging/RiscV64QemuVirt PATCH V7 03/20] MdePkg: Add BaseRiscVSbiLib Library for RISC-V
Date: Thu, 9 Feb 2023 12:48:50 +0530	[thread overview]
Message-ID: <Y+SeWlg8sVRX3MjA@sunil-laptop> (raw)
In-Reply-To: <CO1PR11MB492967020422D9DEC7D90FF1D2D99@CO1PR11MB4929.namprd11.prod.outlook.com>

Hi Mike,

On Thu, Feb 09, 2023 at 01:45:48AM +0000, Michael D Kinney wrote:
> One comment below.
> 
> Mike
> 
> > -----Original Message-----
> > From: Sunil V L <sunilvl@ventanamicro.com>
> > Sent: Saturday, January 28, 2023 11:18 AM
> > 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>;
> > Abner Chang <abner.chang@amd.com>
> > Subject: [edk2-staging/RiscV64QemuVirt PATCH V7 03/20] MdePkg: Add BaseRiscVSbiLib Library for RISC-V
> > 
> > REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4076
> > 
> > This library is required to make SBI ecalls from the S-mode EDK2.
> > This is mostly copied from
> > edk2-platforms/Silicon/RISC-V/ProcessorPkg/Library/RiscVEdk2SbiLib
> > 
> > Cc: Michael D Kinney <michael.d.kinney@intel.com>
> > Cc: Liming Gao <gaoliming@byosoft.com.cn>
> > Cc: Zhiguang Liu <zhiguang.liu@intel.com>
> > Signed-off-by: Sunil V L <sunilvl@ventanamicro.com>
> > Acked-by: Abner Chang <abner.chang@amd.com>
> > ---
> >  MdePkg/MdePkg.dec                                  |   4 +
> >  MdePkg/MdePkg.dsc                                  |   3 +
> >  MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf |  25 +++
> >  MdePkg/Include/Library/BaseRiscVSbiLib.h           | 127 +++++++++++
> >  MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.c   | 227 ++++++++++++++++++++
> >  5 files changed, 386 insertions(+)
> > 
> > diff --git a/MdePkg/MdePkg.dec b/MdePkg/MdePkg.dec
> > index 3d08f20d15b0..ca2e4dcf815c 100644
> > --- a/MdePkg/MdePkg.dec
> > +++ b/MdePkg/MdePkg.dec
> > @@ -316,6 +316,10 @@ [LibraryClasses.IA32, LibraryClasses.X64]
> >    ##  @libraryclass  Provides function to support TDX processing.
> >    TdxLib|Include/Library/TdxLib.h
> > 
> > +[LibraryClasses.RISCV64]
> > +  ##  @libraryclass  Provides function to make ecalls to SBI
> > +  BaseRiscVSbiLib|Include/Library/BaseRiscVSbiLib.h
> > +
> >  [Guids]
> >    #
> >    # GUID defined in UEFI2.1/UEFI2.0/EFI1.1
> > diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc
> > index 32a852dc466e..0ac7618b4623 100644
> > --- a/MdePkg/MdePkg.dsc
> > +++ b/MdePkg/MdePkg.dsc
> > @@ -190,4 +190,7 @@ [Components.ARM, Components.AARCH64]
> >    MdePkg/Library/BaseIoLibIntrinsic/BaseIoLibIntrinsicArmVirt.inf
> >    MdePkg/Library/BaseStackCheckLib/BaseStackCheckLib.inf
> > 
> > +[Components.RISCV64]
> > +  MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf
> > +
> >  [BuildOptions]
> > diff --git a/MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf b/MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf
> > new file mode 100644
> > index 000000000000..d03132bf01c1
> > --- /dev/null
> > +++ b/MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf
> > @@ -0,0 +1,25 @@
> > +## @file
> > +# RISC-V Library to call SBI ecalls
> > +#
> > +#  Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
> > +#
> > +#  SPDX-License-Identifier: BSD-2-Clause-Patent
> > +#
> > +##
> > +
> > +[Defines]
> > +  INF_VERSION         = 0x0001001b
> > +  BASE_NAME           = BaseRiscVSbiLib
> > +  FILE_GUID           = D742CF3D-E600-4009-8FB5-318073008508
> > +  MODULE_TYPE         = BASE
> > +  VERSION_STRING      = 1.0
> > +  LIBRARY_CLASS       = RiscVSbiLib
> > +
> > +[Sources]
> > +  BaseRiscVSbiLib.c
> > +
> > +[Packages]
> > +  MdePkg/MdePkg.dec
> > +
> > +[LibraryClasses]
> > +  BaseLib
> > diff --git a/MdePkg/Include/Library/BaseRiscVSbiLib.h b/MdePkg/Include/Library/BaseRiscVSbiLib.h
> > new file mode 100644
> > index 000000000000..3a3cbfb879f1
> > --- /dev/null
> > +++ b/MdePkg/Include/Library/BaseRiscVSbiLib.h
> > @@ -0,0 +1,127 @@
> > +/** @file
> > +  Library to call the RISC-V SBI ecalls
> > +
> > +  Copyright (c) 2021-2022, Hewlett Packard Development LP. All rights reserved.<BR>
> > +
> > +  SPDX-License-Identifier: BSD-2-Clause-Patent
> > +
> > +  @par Glossary:
> > +    - Hart - Hardware Thread, similar to a CPU core
> > +
> > +  Currently, EDK2 needs to call SBI only to set the time and to do system reset.
> > +
> > +**/
> > +
> > +#ifndef RISCV_SBI_LIB_H_
> > +#define RISCV_SBI_LIB_H_
> > +
> > +#include <Uefi.h>
> > +
> > +/* SBI Extension IDs */
> > +#define SBI_EXT_TIME  0x54494D45
> > +#define SBI_EXT_SRST  0x53525354
> > +
> > +/* SBI function IDs for TIME extension*/
> > +#define SBI_EXT_TIME_SET_TIMER  0x0
> > +
> > +/* SBI function IDs for SRST extension */
> > +#define SBI_EXT_SRST_RESET  0x0
> > +
> > +#define SBI_SRST_RESET_TYPE_SHUTDOWN     0x0
> > +#define SBI_SRST_RESET_TYPE_COLD_REBOOT  0x1
> > +#define SBI_SRST_RESET_TYPE_WARM_REBOOT  0x2
> > +
> > +#define SBI_SRST_RESET_REASON_NONE     0x0
> > +#define SBI_SRST_RESET_REASON_SYSFAIL  0x1
> > +
> > +/* SBI return error codes */
> > +#define SBI_SUCCESS                0
> > +#define SBI_ERR_FAILED             -1
> > +#define SBI_ERR_NOT_SUPPORTED      -2
> > +#define SBI_ERR_INVALID_PARAM      -3
> > +#define SBI_ERR_DENIED             -4
> > +#define SBI_ERR_INVALID_ADDRESS    -5
> > +#define SBI_ERR_ALREADY_AVAILABLE  -6
> > +#define SBI_ERR_ALREADY_STARTED    -7
> > +#define SBI_ERR_ALREADY_STOPPED    -8
> > +
> > +#define SBI_LAST_ERR  SBI_ERR_ALREADY_STOPPED
> > +
> > +typedef struct {
> > +  UINT64    BootHartId;
> > +  VOID      *PeiServiceTable;    // PEI Service table
> > +  VOID      *PrePiHobList;       // Pre PI Hob List
> > +  UINT64    FlattenedDeviceTree; // Pointer to Flattened Device tree
> > +} EFI_RISCV_FIRMWARE_CONTEXT;
> > +
> > +//
> > +// EDK2 OpenSBI firmware extension return status.
> > +//
> > +typedef struct {
> > +  UINTN    Error; ///< SBI status code
> > +  UINTN    Value; ///< Value returned
> > +} SBI_RET;
> > +
> > +VOID
> > +EFIAPI
> > +SbiSetTimer (
> > +  IN  UINT64  Time
> > +  );
> > +
> > +EFI_STATUS
> > +EFIAPI
> > +SbiSystemReset (
> > +  IN  UINTN  ResetType,
> > +  IN  UINTN  ResetReason
> > +  );
> > +
> > +/**
> > +  Get firmware context of the calling hart.
> > +
> > +  @param[out] FirmwareContext      The firmware context pointer.
> > +**/
> > +VOID
> > +EFIAPI
> > +GetFirmwareContext (
> > +  OUT EFI_RISCV_FIRMWARE_CONTEXT  **FirmwareContext
> > +  );
> > +
> > +/**
> > +  Set firmware context of the calling hart.
> > +
> > +  @param[in] FirmwareContext       The firmware context pointer.
> > +**/
> > +VOID
> > +EFIAPI
> > +SetFirmwareContext (
> > +  IN EFI_RISCV_FIRMWARE_CONTEXT  *FirmwareContext
> > +  );
> > +
> > +/**
> > +  Get pointer to OpenSBI Firmware Context
> > +
> > +  Get the pointer of firmware context.
> > +
> > +  @param    FirmwareContextPtr   Pointer to retrieve pointer to the
> > +                                 Firmware Context.
> > +**/
> > +VOID
> > +EFIAPI
> > +GetFirmwareContextPointer (
> > +  IN OUT EFI_RISCV_FIRMWARE_CONTEXT  **FirmwareContextPtr
> > +  );
> > +
> > +/**
> > +  Set pointer to OpenSBI Firmware Context
> > +
> > +  Set the pointer of firmware context.
> > +
> > +  @param    FirmwareContextPtr   Pointer to Firmware Context.
> > +**/
> > +VOID
> > +EFIAPI
> > +SetFirmwareContextPointer (
> > +  IN EFI_RISCV_FIRMWARE_CONTEXT  *FirmwareContextPtr
> > +  );
> > +
> > +#endif
> > diff --git a/MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.c b/MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.c
> > new file mode 100644
> > index 000000000000..15222a528753
> > --- /dev/null
> > +++ b/MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.c
> > @@ -0,0 +1,227 @@
> > +/** @file
> > +  Instance of the SBI ecall library.
> > +
> > +  It allows calling an SBI function via an ecall from S-Mode.
> > +
> > +  Copyright (c) 2021-2022, Hewlett Packard Development LP. All rights reserved.<BR>
> > +
> > +  SPDX-License-Identifier: BSD-2-Clause-Patent
> > +
> > +**/
> > +
> > +#include <Library/BaseLib.h>
> > +#include <Library/BaseMemoryLib.h>
> > +#include <Library/DebugLib.h>
> > +#include <Library/BaseRiscVSbiLib.h>
> > +
> > +//
> > +// Maximum arguments for SBI ecall
> > +#define SBI_CALL_MAX_ARGS  6
> > +
> > +/**
> > +  Call SBI call using ecall instruction.
> > +
> > +  Asserts when NumArgs exceeds SBI_CALL_MAX_ARGS.
> > +
> > +  @param[in] ExtId    SBI extension ID.
> > +  @param[in] FuncId   SBI function ID.
> > +  @param[in] NumArgs  Number of arguments to pass to the ecall.
> > +  @param[in] ...      Argument list for the ecall.
> > +
> > +  @retval  Returns SBI_RET structure with value and error code.
> > +
> > +**/
> > +STATIC
> > +SBI_RET
> > +EFIAPI
> > +SbiCall (
> > +  IN  UINTN  ExtId,
> > +  IN  UINTN  FuncId,
> > +  IN  UINTN  NumArgs,
> > +  ...
> > +  )
> > +{
> > +  UINTN    I;
> > +  SBI_RET  Ret;
> > +  UINTN    Args[SBI_CALL_MAX_ARGS];
> > +  VA_LIST  ArgList;
> > +
> > +  VA_START (ArgList, NumArgs);
> > +
> > +  if (NumArgs > SBI_CALL_MAX_ARGS) {
> > +    Ret.Error = SBI_ERR_INVALID_PARAM;
> > +    Ret.Value = -1;
> > +    return Ret;
> > +  }
> > +
> > +  for (I = 0; I < SBI_CALL_MAX_ARGS; I++) {
> > +    if (I < NumArgs) {
> > +      Args[I] = VA_ARG (ArgList, UINTN);
> > +    } else {
> > +      // Default to 0 for all arguments that are not given
> > +      Args[I] = 0;
> > +    }
> > +  }
> > +
> > +  VA_END (ArgList);
> > +
> > +  register UINTN  a0 asm ("a0") = Args[0];
> > +  register UINTN  a1 asm ("a1") = Args[1];
> > +  register UINTN  a2 asm ("a2") = Args[2];
> > +  register UINTN  a3 asm ("a3") = Args[3];
> > +  register UINTN  a4 asm ("a4") = Args[4];
> > +  register UINTN  a5 asm ("a5") = Args[5];
> > +  register UINTN  a6 asm ("a6") = (UINTN)(FuncId);
> > +  register UINTN  a7 asm ("a7") = (UINTN)(ExtId);
> > +
> > +  asm volatile ("ecall" \
> > +       : "+r" (a0), "+r" (a1) \
> > +       : "r" (a2), "r" (a3), "r" (a4), "r" (a5), "r" (a6), "r" (a7) \
> > +       : "memory"); \
> > +  Ret.Error = a0;
> > +  Ret.Value = a1;
> 
> Why is inline assembly used here?  I see use of .S files in BaseLib
> Can this be converted to .S file?
> 
It was ported as is from edk2-platforms. I can convert into .S file and
I prefer to add it in this library itself than BaseLib.

Thanks!,
Sunil

  reply	other threads:[~2023-02-09  7:19 UTC|newest]

Thread overview: 80+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-28 19:17 [edk2-staging/RiscV64QemuVirt PATCH V7 00/20] Add support for RISC-V virt machine Sunil V L
2023-01-28 19:17 ` [edk2-staging/RiscV64QemuVirt PATCH V7 01/20] MdePkg/Register: Add register definition header files for RISC-V Sunil V L
2023-02-06 15:44   ` [edk2-devel] " Andrei Warkentin
2023-02-09  1:44   ` Michael D Kinney
2023-01-28 19:17 ` [edk2-staging/RiscV64QemuVirt PATCH V7 02/20] MdePkg/BaseLib: RISC-V: Add few more helper functions Sunil V L
2023-02-06 15:46   ` [edk2-devel] " Andrei Warkentin
2023-02-09  1:43   ` Michael D Kinney
2023-02-09  7:21     ` Sunil V L
2023-01-28 19:17 ` [edk2-staging/RiscV64QemuVirt PATCH V7 03/20] MdePkg: Add BaseRiscVSbiLib Library for RISC-V Sunil V L
2023-02-06 15:47   ` [edk2-devel] " Andrei Warkentin
2023-02-09  1:45   ` Michael D Kinney
2023-02-09  7:18     ` Sunil V L [this message]
2023-02-09 15:47       ` [edk2-devel] " Michael D Kinney
2023-01-28 19:17 ` [edk2-staging/RiscV64QemuVirt PATCH V7 04/20] UefiCpuPkg: Add RISCV_EFI_BOOT_PROTOCOL related definitions Sunil V L
2023-02-06 15:47   ` [edk2-devel] " Andrei Warkentin
2023-02-09  5:16   ` Ni, Ray
2023-01-28 19:17 ` [edk2-staging/RiscV64QemuVirt PATCH V7 05/20] UefiCpuPkg: Add CpuTimerDxe module Sunil V L
2023-02-08 18:02   ` [edk2-devel] " Michael D Kinney
2023-02-08 18:12     ` Sunil V L
2023-02-09  5:21   ` Ni, Ray
2023-02-09 10:17   ` Michael Brown
2023-02-09 10:28     ` Sunil V L
2023-02-09 10:30       ` Michael Brown
2023-02-09 10:37         ` Sunil V L
2023-01-28 19:17 ` [edk2-staging/RiscV64QemuVirt PATCH V7 06/20] UefiCpuPkg/CpuExceptionHandlerLib: Add RISC-V instance Sunil V L
2023-02-09  5:24   ` [edk2-devel] " Ni, Ray
2023-01-28 19:17 ` [edk2-staging/RiscV64QemuVirt PATCH V7 07/20] UefiCpuPkg/CpuDxe: " Sunil V L
2023-02-06 15:58   ` [edk2-devel] " Andrei Warkentin
2023-02-08  5:05     ` Sunil V L
2023-02-09  5:43       ` Ni, Ray
2023-02-09  5:49         ` Sunil V L
2023-01-28 19:17 ` [edk2-staging/RiscV64QemuVirt PATCH V7 08/20] UefiCpuPkg/CpuTimerLib: " Sunil V L
2023-01-30 11:07   ` [edk2-devel] " dhaval
2023-01-30 13:08     ` Sunil V L
2023-02-06 16:00   ` Andrei Warkentin
2023-02-09  5:37   ` Ni, Ray
2023-01-28 19:17 ` [edk2-staging/RiscV64QemuVirt PATCH V7 09/20] UefiCpuPkg/UefiCpuPkg.ci.yaml: Ignore RISC-V file Sunil V L
2023-02-06 16:00   ` [edk2-devel] " Andrei Warkentin
2023-02-09  1:50   ` Michael D Kinney
2023-02-09  5:38   ` Ni, Ray
2023-01-28 19:17 ` [edk2-staging/RiscV64QemuVirt PATCH V7 10/20] EmbeddedPkg: Enable PcdPrePiCpuIoSize for RISC-V Sunil V L
2023-02-06 16:00   ` [edk2-devel] " Andrei Warkentin
2023-02-09  1:51   ` Michael D Kinney
2023-01-28 19:17 ` [edk2-staging/RiscV64QemuVirt PATCH V7 11/20] ArmVirtPkg/PlatformHasAcpiDtDxe: Move to OvmfPkg Sunil V L
2023-02-06 16:01   ` [edk2-devel] " Andrei Warkentin
2023-01-28 19:17 ` [edk2-staging/RiscV64QemuVirt PATCH V7 12/20] ArmVirtPkg: Fix up the location of PlatformHasAcpiDtDxe Sunil V L
2023-02-06 16:01   ` [edk2-devel] " Andrei Warkentin
2023-01-28 19:18 ` [edk2-staging/RiscV64QemuVirt PATCH V7 13/20] OvmfPkg/RiscVVirt: Add PlatformBootManagerLib library Sunil V L
2023-02-06 16:01   ` [edk2-devel] " Andrei Warkentin
2023-01-28 19:18 ` [edk2-staging/RiscV64QemuVirt PATCH V7 14/20] OvmfPkg/RiscVVirt: Add PrePiHobListPointerLib library Sunil V L
2023-02-06 16:01   ` [edk2-devel] " Andrei Warkentin
2023-01-28 19:18 ` [edk2-staging/RiscV64QemuVirt PATCH V7 15/20] OvmfPkg/RiscVVirt: Add ResetSystemLib library Sunil V L
2023-02-06 16:01   ` [edk2-devel] " Andrei Warkentin
2023-01-28 19:18 ` [edk2-staging/RiscV64QemuVirt PATCH V7 16/20] OvmfPkg/RiscVVirt: Add VirtNorFlashPlatformLib library Sunil V L
2023-02-06 16:02   ` [edk2-devel] " Andrei Warkentin
2023-01-28 19:18 ` [edk2-staging/RiscV64QemuVirt PATCH V7 17/20] OvmfPkg/RiscVVirt: Add PciCpuIo2Dxe module Sunil V L
2023-01-30 10:12   ` [edk2-devel] " dhaval
2023-01-30 13:05     ` Sunil V L
2023-01-30 14:33       ` dhaval
2023-02-06 16:02   ` Andrei Warkentin
2023-01-28 19:18 ` [edk2-staging/RiscV64QemuVirt PATCH V7 18/20] OvmfPkg/RiscVVirt: Add SEC module Sunil V L
2023-01-30  5:17   ` [edk2-devel] " dhaval
2023-01-30  6:00     ` Sunil V L
2023-02-06 16:03   ` Andrei Warkentin
2023-01-28 19:18 ` [edk2-staging/RiscV64QemuVirt PATCH V7 19/20] OvmfPkg/RiscVVirt: Add build files for Qemu Virt platform Sunil V L
2023-02-06 16:03   ` [edk2-devel] " Andrei Warkentin
2023-01-28 19:18 ` [edk2-staging/RiscV64QemuVirt PATCH V7 20/20] Maintainers.txt: Add entry for OvmfPkg/RiscVVirt Sunil V L
2023-02-06 16:04   ` [edk2-devel] " Andrei Warkentin
2023-02-09  1:51   ` Michael D Kinney
2023-02-09  3:32     ` [edk2-devel] " Yao, Jiewen
2023-02-09  4:34       ` Sunil V L
2023-02-09  5:07         ` Yao, Jiewen
2023-02-09  5:15           ` Chang, Abner
2023-02-09 14:05           ` Yao, Jiewen
2023-02-09 15:19             ` Sunil V L
2023-02-09 15:21               ` Yao, Jiewen
     [not found] ` <173E8F29CD0D02D8.27165@groups.io>
2023-01-30 13:43   ` [edk2-devel] [edk2-staging/RiscV64QemuVirt PATCH V7 11/20] ArmVirtPkg/PlatformHasAcpiDtDxe: Move to OvmfPkg Sunil V L
2023-02-03 12:29     ` Ard Biesheuvel
     [not found] ` <173E8F254E9BED62.27165@groups.io>
2023-02-02 14:35   ` [edk2-devel] [edk2-staging/RiscV64QemuVirt PATCH V7 03/20] MdePkg: Add BaseRiscVSbiLib Library for RISC-V Sunil V L
     [not found] ` <173E8F293E682CB4.27165@groups.io>
2023-02-03  9:46   ` [edk2-devel] [edk2-staging/RiscV64QemuVirt PATCH V7 10/20] EmbeddedPkg: Enable PcdPrePiCpuIoSize " Sunil V L

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y+SeWlg8sVRX3MjA@sunil-laptop \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox