From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) by mx.groups.io with SMTP id smtpd.web08.1930.1662484166158039579 for ; Tue, 06 Sep 2022 10:09:26 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@ventanamicro.com header.s=google header.b=YtiHAN9R; spf=pass (domain: ventanamicro.com, ip: 209.85.215.181, mailfrom: sunilvl@ventanamicro.com) Received: by mail-pg1-f181.google.com with SMTP id w63so2730790pgb.7 for ; Tue, 06 Sep 2022 10:09:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=3/JNUQJQEmKmnkK68P4c1B2DLAgFUlFUAzB7sV2am2Q=; b=YtiHAN9RWTKD4axQ4U00LEtkbtIv8YuH7wOXbMXdgEUh0nXbR1wnaN3WJ5AKs7cXMM rdEmDWlS9+hTWz8HMTAxAEgVL+EdFi52v3fQ7FjetQCX2vKHAj/MJFxpkOHUafQbZBBh v6ggj/4OxuW6kGdycI8y9rInUzjQk/bT+R4cEf3Ai8vaOB9gE8iIXwoskuBTPs8AjUME rlj02io6IRpgByfqafyVH/QciVm4msGf2/jwJItYACdd0ve3PHov4rqbrA3tHGxR2BCg EOVXk31ChcBho55qz8iQ71xydFEvw9g5xfRAnN+tjhDdzAIs+Td68BvtX6tY/s3KcjYw Wy+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=3/JNUQJQEmKmnkK68P4c1B2DLAgFUlFUAzB7sV2am2Q=; b=HM0aJLqxkSxxrr33Rre6COGWuFFwx1ru+0RTgg3i2sK/Qvvu2D1vZhmga0QPJ9aT04 TDTqnhUAqIvvdjZklemgEPlSLLcyabtu2RnR+02u5/dzUF00VzRuzUPeIFH/fhpPsYlp btNi54oBuvlf8m/nlhHHUXjvG1I85LyDjxI7V72WHLwa31LrMio4of6HxSDM6nRopjQU HJfeFtiD4MqJXl3AHbKKVnq/TG3H/bCrM/dUKG/SljL6v2OxSeTfFog2suWhME+LM1Fh rXhotyaTp8i8+tUBZc32grY5IMcSqNCYA+lgqSdEVfpdrIfrekvyIEUdWe0MvFtbwNG5 dXhQ== X-Gm-Message-State: ACgBeo2tQWURlvwYnausYEY6fEvwlxzUqjIBJ3CFAgtPBYOnAX+KjByF s9ZkjKfMaEOk2eRXFK7ffb0LLggRVjSNR6G+ X-Google-Smtp-Source: AA6agR4pDnfTIq7U93wkd6bqItjZMyC35/CPf2FVAclBpTJhBpHe7w5pVAjRJrvzTKNssWE2HCCu0w== X-Received: by 2002:a63:1a16:0:b0:430:581d:827f with SMTP id a22-20020a631a16000000b00430581d827fmr25920018pga.276.1662484165157; Tue, 06 Sep 2022 10:09:25 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([49.206.11.92]) by smtp.gmail.com with ESMTPSA id b17-20020a170903229100b00176be258f41sm3806567plh.91.2022.09.06.10.09.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Sep 2022 10:09:24 -0700 (PDT) From: "Sunil V L" To: devel@edk2.groups.io Cc: Jian J Wang , Liming Gao , Eric Dong , Ray Ni , Rahul Kumar , Debkumar De , Catharine West , Daniel Schaefer , Abner Chang , Leif Lindholm , Ard Biesheuvel , Heinrich Schuchardt , Anup Patel , Sunil V L Subject: [RFC PATCH 08/17] MdePkg: Add RiscVSbiLib Library for RISC-V Date: Tue, 6 Sep 2022 22:38:28 +0530 Message-Id: <20220906170837.491525-9-sunilvl@ventanamicro.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220906170837.491525-1-sunilvl@ventanamicro.com> References: <20220906170837.491525-1-sunilvl@ventanamicro.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable This library is required to make SBI ecalls from the S-mode EDK2. Signed-off-by: Sunil V L --- MdePkg/Include/Library/RiscVSbiLib.h | 129 ++++++++++++ MdePkg/Library/RiscVSbiLib/RiscVSbiLib.c | 228 +++++++++++++++++++++ MdePkg/Library/RiscVSbiLib/RiscVSbiLib.inf | 28 +++ 3 files changed, 385 insertions(+) create mode 100644 MdePkg/Include/Library/RiscVSbiLib.h create mode 100644 MdePkg/Library/RiscVSbiLib/RiscVSbiLib.c create mode 100644 MdePkg/Library/RiscVSbiLib/RiscVSbiLib.inf diff --git a/MdePkg/Include/Library/RiscVSbiLib.h b/MdePkg/Include/Library/= RiscVSbiLib.h new file mode 100644 index 0000000000..e94adb08fd --- /dev/null +++ b/MdePkg/Include/Library/RiscVSbiLib.h @@ -0,0 +1,129 @@ +/** @file=0D + Library to call the RISC-V SBI ecalls=0D +=0D + Copyright (c) 2021-2022, Hewlett Packard Development LP. All rights rese= rved.
=0D + Copyright (c) 2022, Ventana Micro Systems Inc. All rights reserved.
= =0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D + @par Glossary:=0D + - Hart - Hardware Thread, similar to a CPU core=0D +=0D + Currently, EDK2 needs to call SBI only to set the time and to do system = reset.=0D +=0D +**/=0D +=0D +#ifndef RISCV_SBI_LIB_H_=0D +#define RISCV_SBI_LIB_H_=0D +=0D +#include =0D +=0D +/* SBI Extension IDs */=0D +#define SBI_EXT_TIME 0x54494D45=0D +#define SBI_EXT_SRST 0x53525354=0D +=0D +/* SBI function IDs for TIME extension*/=0D +#define SBI_EXT_TIME_SET_TIMER 0x0=0D +=0D +/* SBI function IDs for SRST extension */=0D +#define SBI_EXT_SRST_RESET 0x0=0D +=0D +#define SBI_SRST_RESET_TYPE_SHUTDOWN 0x0=0D +#define SBI_SRST_RESET_TYPE_COLD_REBOOT 0x1=0D +#define SBI_SRST_RESET_TYPE_WARM_REBOOT 0x2=0D +#define SBI_SRST_RESET_TYPE_LAST SBI_SRST_RESET_TYPE_WARM_REBOOT=0D +=0D +#define SBI_SRST_RESET_REASON_NONE 0x0=0D +#define SBI_SRST_RESET_REASON_SYSFAIL 0x1=0D +=0D +/* SBI return error codes */=0D +#define SBI_SUCCESS 0=0D +#define SBI_ERR_FAILED -1=0D +#define SBI_ERR_NOT_SUPPORTED -2=0D +#define SBI_ERR_INVALID_PARAM -3=0D +#define SBI_ERR_DENIED -4=0D +#define SBI_ERR_INVALID_ADDRESS -5=0D +#define SBI_ERR_ALREADY_AVAILABLE -6=0D +#define SBI_ERR_ALREADY_STARTED -7=0D +#define SBI_ERR_ALREADY_STOPPED -8=0D +=0D +#define SBI_LAST_ERR SBI_ERR_ALREADY_STOPPED=0D +=0D +typedef struct {=0D + UINT64 BootHartId;=0D + VOID *PeiServiceTable; // PEI Service table=0D + UINT64 FlattenedDeviceTree; // Pointer to Flattened Device tree=0D +} EFI_RISCV_FIRMWARE_CONTEXT;=0D +=0D +=0D +//=0D +// EDK2 OpenSBI firmware extension return status.=0D +//=0D +typedef struct {=0D + UINTN Error; ///< SBI status code=0D + UINTN Value; ///< Value returned=0D +} SBI_RET;=0D +=0D +VOID=0D +EFIAPI=0D +SbiSetTimer (=0D + IN UINT64 Time=0D + );=0D +=0D +EFI_STATUS=0D +EFIAPI=0D +SbiSystemReset (=0D + IN UINTN ResetType,=0D + IN UINTN ResetReason=0D + );=0D +=0D +/**=0D + Get firmware context of the calling hart.=0D +=0D + @param[out] FirmwareContext The firmware context pointer.=0D +**/=0D +VOID=0D +EFIAPI=0D +GetFirmwareContext (=0D + OUT EFI_RISCV_FIRMWARE_CONTEXT **FirmwareContext=0D + );=0D +=0D +/**=0D + Set firmware context of the calling hart.=0D +=0D + @param[in] FirmwareContext The firmware context pointer.=0D +**/=0D +VOID=0D +EFIAPI=0D +SetFirmwareContext (=0D + IN EFI_RISCV_FIRMWARE_CONTEXT *FirmwareContext=0D + );=0D +=0D +/**=0D + Get pointer to OpenSBI Firmware Context=0D +=0D + Get the pointer of firmware context.=0D +=0D + @param FirmwareContextPtr Pointer to retrieve pointer to the=0D + Firmware Context.=0D +**/=0D +VOID=0D +EFIAPI=0D +GetFirmwareContextPointer (=0D + IN OUT EFI_RISCV_FIRMWARE_CONTEXT **FirmwareContextPtr=0D + );=0D +=0D +/**=0D + Set pointer to OpenSBI Firmware Context=0D +=0D + Set the pointer of firmware context.=0D +=0D + @param FirmwareContextPtr Pointer to Firmware Context.=0D +**/=0D +VOID=0D +EFIAPI=0D +SetFirmwareContextPointer (=0D + IN EFI_RISCV_FIRMWARE_CONTEXT *FirmwareContextPtr=0D + );=0D +=0D +#endif=0D diff --git a/MdePkg/Library/RiscVSbiLib/RiscVSbiLib.c b/MdePkg/Library/Risc= VSbiLib/RiscVSbiLib.c new file mode 100644 index 0000000000..39cc6628be --- /dev/null +++ b/MdePkg/Library/RiscVSbiLib/RiscVSbiLib.c @@ -0,0 +1,228 @@ +/** @file=0D + Instance of the SBI ecall library.=0D +=0D + It allows calling an SBI function via an ecall from S-Mode.=0D +=0D + Copyright (c) 2021-2022, Hewlett Packard Development LP. All rights rese= rved.
=0D + Copyright (c) 2022, Ventana Micro Systems Inc. All rights reserved.
= =0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +//=0D +// Maximum arguments for SBI ecall=0D +// It's possible to pass more but no SBI call uses more as of SBI 0.2.=0D +// The additional arguments would have to be passed on the stack instead o= f as=0D +// registers, like it's done now.=0D +//=0D +#define SBI_CALL_MAX_ARGS 6=0D +=0D +/**=0D + Call SBI call using ecall instruction.=0D +=0D + Asserts when NumArgs exceeds SBI_CALL_MAX_ARGS.=0D +=0D + @param[in] ExtId SBI extension ID.=0D + @param[in] FuncId SBI function ID.=0D + @param[in] NumArgs Number of arguments to pass to the ecall.=0D + @param[in] ... Argument list for the ecall.=0D +=0D + @retval Returns SBI_RET structure with value and error code.=0D +=0D +**/=0D +STATIC=0D +SBI_RET=0D +EFIAPI=0D +SbiCall (=0D + IN UINTN ExtId,=0D + IN UINTN FuncId,=0D + IN UINTN NumArgs,=0D + ...=0D + )=0D +{=0D + UINTN I;=0D + SBI_RET Ret;=0D + UINTN Args[SBI_CALL_MAX_ARGS];=0D + VA_LIST ArgList;=0D +=0D + VA_START (ArgList, NumArgs);=0D +=0D + ASSERT (NumArgs <=3D SBI_CALL_MAX_ARGS);=0D +=0D + for (I =3D 0; I < SBI_CALL_MAX_ARGS; I++) {=0D + if (I < NumArgs) {=0D + Args[I] =3D VA_ARG (ArgList, UINTN);=0D + } else {=0D + // Default to 0 for all arguments that are not given=0D + Args[I] =3D 0;=0D + }=0D + }=0D +=0D + VA_END (ArgList);=0D +=0D + register UINTN a0 asm ("a0") =3D Args[0];=0D + register UINTN a1 asm ("a1") =3D Args[1];=0D + register UINTN a2 asm ("a2") =3D Args[2];=0D + register UINTN a3 asm ("a3") =3D Args[3];=0D + register UINTN a4 asm ("a4") =3D Args[4];=0D + register UINTN a5 asm ("a5") =3D Args[5];=0D + register UINTN a6 asm ("a6") =3D (UINTN)(FuncId);=0D + register UINTN a7 asm ("a7") =3D (UINTN)(ExtId);=0D +=0D + asm volatile ("ecall" \=0D + : "+r" (a0), "+r" (a1) \=0D + : "r" (a2), "r" (a3), "r" (a4), "r" (a5), "r" (a6), "r" (a7) \=0D + : "memory"); \=0D + Ret.Error =3D a0;=0D + Ret.Value =3D a1;=0D + return Ret;=0D +}=0D +=0D +/**=0D + Translate SBI error code to EFI status.=0D +=0D + @param[in] SbiError SBI error code=0D + @retval EFI_STATUS=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +EFIAPI=0D +TranslateError (=0D + IN UINTN SbiError=0D + )=0D +{=0D + switch (SbiError) {=0D + case SBI_SUCCESS:=0D + return EFI_SUCCESS;=0D + case SBI_ERR_FAILED:=0D + return EFI_DEVICE_ERROR;=0D + break;=0D + case SBI_ERR_NOT_SUPPORTED:=0D + return EFI_UNSUPPORTED;=0D + break;=0D + case SBI_ERR_INVALID_PARAM:=0D + return EFI_INVALID_PARAMETER;=0D + break;=0D + case SBI_ERR_DENIED:=0D + return EFI_ACCESS_DENIED;=0D + break;=0D + case SBI_ERR_INVALID_ADDRESS:=0D + return EFI_LOAD_ERROR;=0D + break;=0D + case SBI_ERR_ALREADY_AVAILABLE:=0D + return EFI_ALREADY_STARTED;=0D + break;=0D + default:=0D + //=0D + // Reaches here only if SBI has defined a new error type=0D + //=0D + ASSERT (FALSE);=0D + return EFI_UNSUPPORTED;=0D + break;=0D + }=0D +}=0D +=0D +/**=0D + Clear pending timer interrupt bit and set timer for next event after Tim= e.=0D +=0D + To clear the timer without scheduling a timer event, set Time to a=0D + practically infinite value or mask the timer interrupt by clearing sie.S= TIE.=0D +=0D + @param[in] Time The time offset to the next scheduled t= imer interrupt.=0D +**/=0D +VOID=0D +EFIAPI=0D +SbiSetTimer (=0D + IN UINT64 Time=0D + )=0D +{=0D + SbiCall (SBI_EXT_TIME, SBI_EXT_TIME_SET_TIMER, 1, Time);=0D +}=0D +=0D +EFI_STATUS=0D +EFIAPI=0D +SbiSystemReset (=0D + IN UINTN ResetType,=0D + IN UINTN ResetReason=0D + )=0D +{=0D + SBI_RET Ret;=0D +=0D + Ret =3D SbiCall (=0D + SBI_EXT_SRST,=0D + SBI_EXT_SRST_RESET,=0D + 2,=0D + ResetType,=0D + ResetReason=0D + );=0D +=0D + return TranslateError (Ret.Error);=0D +}=0D +=0D +/**=0D + Get firmware context of the calling hart.=0D +=0D + @param[out] FirmwareContext The firmware context pointer.=0D + @retval EFI_SUCCESS The operation succeeds.=0D +**/=0D +VOID=0D +EFIAPI=0D +GetFirmwareContext (=0D + OUT EFI_RISCV_FIRMWARE_CONTEXT **FirmwareContext=0D + )=0D +{=0D + *FirmwareContext =3D (EFI_RISCV_FIRMWARE_CONTEXT *)csr_read(CSR_SSCRATCH= );=0D +}=0D +=0D +/**=0D + Set firmware context of the calling hart.=0D +=0D + @param[in] FirmwareContext The firmware context pointer.=0D +**/=0D +VOID=0D +EFIAPI=0D +SetFirmwareContext (=0D + IN EFI_RISCV_FIRMWARE_CONTEXT *FirmwareContext=0D + )=0D +{=0D + csr_write(CSR_SSCRATCH, FirmwareContext);=0D +}=0D +=0D +/**=0D + Get pointer to OpenSBI Firmware Context=0D +=0D + Get the pointer of firmware context through OpenSBI FW Extension SBI.=0D +=0D + @param FirmwareContextPtr Pointer to retrieve pointer to the=0D + Firmware Context.=0D +**/=0D +VOID=0D +EFIAPI=0D +GetFirmwareContextPointer (=0D + IN OUT EFI_RISCV_FIRMWARE_CONTEXT **FirmwareContextPtr=0D + )=0D +{=0D + GetFirmwareContext (FirmwareContextPtr);=0D +}=0D +=0D +/**=0D + Set the pointer to OpenSBI Firmware Context=0D +=0D + Set the pointer of firmware context through OpenSBI FW Extension SBI.=0D +=0D + @param FirmwareContextPtr Pointer to Firmware Context.=0D +**/=0D +VOID=0D +EFIAPI=0D +SetFirmwareContextPointer (=0D + IN EFI_RISCV_FIRMWARE_CONTEXT *FirmwareContextPtr=0D + )=0D +{=0D + SetFirmwareContext (FirmwareContextPtr);=0D +}=0D diff --git a/MdePkg/Library/RiscVSbiLib/RiscVSbiLib.inf b/MdePkg/Library/Ri= scVSbiLib/RiscVSbiLib.inf new file mode 100644 index 0000000000..b01f909300 --- /dev/null +++ b/MdePkg/Library/RiscVSbiLib/RiscVSbiLib.inf @@ -0,0 +1,28 @@ +## @file=0D +# RISC-V Library to call SBI ecalls=0D +#=0D +# Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All righ= ts reserved.
=0D +# Copyright (c) 2022, Ventana Micro Systems Inc. All rights reserved.
= =0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +##=0D +=0D +[Defines]=0D + INF_VERSION =3D 0x0001001b=0D + BASE_NAME =3D RiscVSbiLib=0D + FILE_GUID =3D D742CF3D-E600-4009-8FB5-318073008508=0D + MODULE_TYPE =3D BASE=0D + VERSION_STRING =3D 1.0=0D + LIBRARY_CLASS =3D RiscVSbiLib=0D +=0D +[Sources]=0D + RiscVSbiLib.c=0D +=0D +[Packages]=0D + MdePkg/MdePkg.dec=0D + UefiCpuPkg/UefiCpuPkg.dec=0D +=0D +[LibraryClasses]=0D + BaseLib=0D +=0D --=20 2.25.1