From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by mx.groups.io with SMTP id smtpd.web11.56886.1675698494218885639 for ; Mon, 06 Feb 2023 07:48:14 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=SLXV9bFe; spf=pass (domain: intel.com, ip: 192.55.52.88, mailfrom: andrei.warkentin@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675698494; x=1707234494; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=S4NZVh9yD+3kjkkkKiMwgBRfSWxcD7sYH6jdwbh3F6k=; b=SLXV9bFe+rGEZ/mLT0OZhnqDWcNBbtmjzK4FPrUJtOTgZHB6iMd1/FZ1 TdYLUmh7fSH8DcJs0rVFK/7fz7p5QXZekyVnkCGiDcBUYzJf3gawzChQX 1W1huRxcybqiLi+vHw2Z21H0pvQ4zF5gLkl/1f0rKxEsRMhBcSgbgB6EB izRi89FuQy9K5UzSfnUBN8tWGSQNB0qZLzdPKRRXpFaJu29JVzOg7oCem /c/cCs9uFdXLbMaiGKoZ+FTQBusQZeu5CoLPTqH1c6PJmeQF/9Yb3DQYU XdXY4ualRDCwMiUY0eRcxWiBUVYnN0j0XLrDpPttV4l5TNj9YQQO/UG0O Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10613"; a="356593865" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="356593865" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2023 07:47:32 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10613"; a="911960998" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="911960998" Received: from fmsmsx601.amr.corp.intel.com ([10.18.126.81]) by fmsmga006.fm.intel.com with ESMTP; 06 Feb 2023 07:47:32 -0800 Received: from fmsmsx610.amr.corp.intel.com (10.18.126.90) by fmsmsx601.amr.corp.intel.com (10.18.126.81) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16; Mon, 6 Feb 2023 07:47:32 -0800 Received: from fmsedg601.ED.cps.intel.com (10.1.192.135) by fmsmsx610.amr.corp.intel.com (10.18.126.90) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16 via Frontend Transport; Mon, 6 Feb 2023 07:47:32 -0800 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (104.47.59.175) by edgegateway.intel.com (192.55.55.70) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.16; Mon, 6 Feb 2023 07:47:31 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Qvp5SSnXISfgMZV7yAEDtAx8xAsRS8clUv/BorKRb5+eyJTH4WEfutrSSuyc0CniN0VxbNXG0WUleqJojcdhD4WAZFxtS6Q+So8TRDnKyORJJZZAN99NK1teR6WfvSAzIJe2pG8qLwmWdfYfRA4tOAyHKBKHgA+owy5fwn+/XhrFz3uCOmtXQoGVBXdx5yoS8APpMijuJWCLy79kptpMd6GJKM2EpBHQOT/X2oJGWJ4HOZtu9F/+ZZNu6niHhvT8dxFzD2ytUZqoTuxbla3ZIUEgj4laUW8saXDeX6dYISl1RxdW282eXkiBy6zSDWfbQqWWnc3c5ZyVAY7Vt2we6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=jXpPT7RkvfRH3wNM0AUv+TAu0fjag+8QqDSY0yD5DyA=; b=k+uz3+U7S0Vf2DKmTthcQ57ZCFHpFnRxS419JmgKcg7hZ+fS6YFn1vXfxM02eutC9YX9nOXp3Zvg3y3SdG/c7dGNDpp6tpW88T9GBT+OkLzUQspaK0PJ6H7QLLgJNIOGajKYQ8Jq5Z5p5A7xv3t7R8goJsKnBzsmJGhfAYcHc3IRvPJ/FG2zYrAqNBxIkhzNAZQf46EiahDkADfgVddNpo0yzAB/deGwisI9DfhjIBPqj3lfNrAVTMPtO3DS+MBpOOshFgi+SsrU/5VRWjcAgy3hjf0t9cdEJiDMsKzRRpsVOuj0nvlYSajdOkMAjGgcXpslN04Vg2F3D7hMsGyckQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Received: from PH8PR11MB6856.namprd11.prod.outlook.com (2603:10b6:510:22b::7) by BY1PR11MB8032.namprd11.prod.outlook.com (2603:10b6:a03:524::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6064.34; Mon, 6 Feb 2023 15:47:29 +0000 Received: from PH8PR11MB6856.namprd11.prod.outlook.com ([fe80::9dba:633a:320c:87b]) by PH8PR11MB6856.namprd11.prod.outlook.com ([fe80::9dba:633a:320c:87b%5]) with mapi id 15.20.6064.032; Mon, 6 Feb 2023 15:47:29 +0000 From: "Andrei Warkentin" To: "devel@edk2.groups.io" , "sunilvl@ventanamicro.com" CC: "Kinney, Michael D" , "Gao, Liming" , "Liu, Zhiguang" , "Abner Chang" Subject: Re: [edk2-devel] [edk2-staging/RiscV64QemuVirt PATCH V7 03/20] MdePkg: Add BaseRiscVSbiLib Library for RISC-V Thread-Topic: [edk2-devel] [edk2-staging/RiscV64QemuVirt PATCH V7 03/20] MdePkg: Add BaseRiscVSbiLib Library for RISC-V Thread-Index: AQHZM01Y/AGtQ8YD8U6l7j8MW7Ublq7CHgCQ Date: Mon, 6 Feb 2023 15:47:29 +0000 Message-ID: References: <20230128191807.2080547-1-sunilvl@ventanamicro.com> <20230128191807.2080547-4-sunilvl@ventanamicro.com> In-Reply-To: <20230128191807.2080547-4-sunilvl@ventanamicro.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: PH8PR11MB6856:EE_|BY1PR11MB8032:EE_ x-ms-office365-filtering-correlation-id: aab0c664-8c1c-49bc-3228-08db085973fa x-ld-processed: 46c98d88-e344-4ed4-8496-4ed7712e255d,ExtAddr x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 1lzOHusN0MqiGbUdGZohyML/r9JnYHwIB9B6AlpYhFLCoIyqX5ELPP6N0pU/APwNGgAfxF+tEIZKuUh+5+CMNeanL+aAd4QJx/jgUOsxk+5zdejH53fqhl3T2Ld+imdPKSJLsAS//keWjOk/WQnbA6TCFl5GySsT3KoIseFpMDv30wPNaZmy0CgA6k49whxzWr/YYoT8YSpZq4NdV6rjuSVQxeupr0gkJgWsZn4QrhgU0xM7p3A46InVyEkSng5YUSHFFn1DCwn5xn5d7yt/6KV9ws9Gpqz6f7R6v4hMtDlayHtlSkQ6tlbxP8uIsi+D/mttfYRfXcD0Q+wli/yHTRHEytm8QtSFfg8fMN6vdgTOSv+H9MHvBeQOim9B7rWXDJgnCtE5qr7wJqlkYeQbNlgMbypJCP4ifKWHVN0GdioinUP6WazRw9RyVMqJN8832yfli6JaXEBHFj6hVpuJWVfnGSn2PumLPE4iFSXiDfsnZzkXFuqA7dE3rRkvzjL3qO7pTWGu1+gk2zSLhn85ZALcLE3hCEP8YIu8eJaeUx+Zy05z/qLxopXW+IKP+X5eoSXMwT572seKnH5097H9A6ysYBPiNtMd/kIw4qkKztF5Hy5VAzNZceMMjSJc8kcbPwY68+sebg2sE8O8DcJgtg/Ju+Y1kXR8A5hiyYXPc17YG13AtSRqXOspJvsHc73fw4T5JcIPybu578+M5t6xiWxEwXLHOveJtKG4KBHrh2GTPNNp9PpCxiNbNcR6a278jJVvVTGaFxIwUFbOSfxcqA== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PH8PR11MB6856.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230025)(376002)(39860400002)(136003)(396003)(346002)(366004)(451199018)(82960400001)(86362001)(38070700005)(38100700002)(122000001)(33656002)(26005)(41300700001)(66476007)(66446008)(64756008)(8676002)(4326008)(8936002)(5660300002)(52536014)(66946007)(66556008)(54906003)(110136005)(76116006)(316002)(55016003)(71200400001)(2906002)(30864003)(83380400001)(478600001)(966005)(7696005)(9686003)(186003)(53546011)(6506007)(66899018);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?d/9hl5ar6jKA9XK8XBIWQotDC6MXXjBx3aFAFIc7n/CcUyMLRCe3ygDlvREx?= =?us-ascii?Q?x/RUhdCKy1dBhES7if1meEK1HfG6/Bo7M7/Ay+mX9JSPksQJhmlHRSOxLFUg?= =?us-ascii?Q?7EpHSeX75acMCvRdyDUUv7c6jhFJRmAvPIY7GfksYtxTTF804m9jbqSEuMLP?= =?us-ascii?Q?YQAj+rhV0EAq+/b71ct6yKTxoZkfEtK75OkO9k0ezoeFsm3HOCWm5/qwcdnc?= =?us-ascii?Q?M1t0LKKkcLF3LubaNl0npTj3IxfbS/n98LFtnt4V8eRXgPVj49JJNhTLy7RJ?= =?us-ascii?Q?2Sf0h0T0hRLBJV2xsdKh/KDjv7Uqv9iRM4FqaQafrj8KL7Hgn6NM4bFn0eaV?= =?us-ascii?Q?3Lh1j1wE5wDIgZEA4l3CyEcJ8/wCI9ibKi+dCi24KiYAveSOQiuf5YmeHZGj?= =?us-ascii?Q?e2CFeDa3HcAIA+oIuLsg06ipGDId7/pHgH/FOmZhRNH/c7CoJykQD2ZpTNmh?= =?us-ascii?Q?y5CB57UQp8BaiLkn8FQEuAW8RDp4W2lNzm2FVleitAX7NVa+BfgwuMf2qHQ3?= =?us-ascii?Q?WqlrbLaf+OuA47SENyIesqsR6RKMZqrRl3hZkzVc0goG+fTPZ7q9nGa638ME?= =?us-ascii?Q?MLTLp+xdz/ooibzR7gdQ98Z3zx7sX6kVizOOz/yq3hSIrBLzA/HNU+IdGcMg?= =?us-ascii?Q?thPMtCleaSXk8Cq3JVrOFbp+KDVSm6q1Pa6rqPrru1xX4ME90y1xzmZGAu+G?= =?us-ascii?Q?4SW645KyIzu2VNv9S95qu6P6WB6Ubq+etikYeF/CCSz8qo+RSImWps1vPZii?= =?us-ascii?Q?82irk48BztfDGyXPFB4CnO+KW0RPStOL3kJn6tkMOy1vRh6PMRdKoeKJPkMw?= =?us-ascii?Q?xjpDlwZ8iZG+PQUlAyZ5/rse1kbKmlG7ggo5ZH8ZxVXHjeuX2chKJOnxMQMA?= =?us-ascii?Q?FQvkEJ8qJb/V7/9qLZiMC9aczP1Rjn9M5b/utiDKB//raGeuc9AgFRsEGoPM?= =?us-ascii?Q?V03jI7CHXkGwnCX7Uzm+phTICjswFyVv81JCFCLEEgRIdsKzfe+KJZ8ifu7n?= =?us-ascii?Q?Se2KoLX1+FSKTIj81ZnWisipYgoLDL3HKN7iD2Ri6tpDh8qBUxANckCClitr?= =?us-ascii?Q?tSsG1/wELS/7ZIt1bQ7EPs0HkJKpLSBgunzE1Pr0cagh2/g3TUCXNkr3sapt?= =?us-ascii?Q?zJwf9ki60FE7qjRoY6zCr2+3SQl3Ytq6zTnKPYEpt/VyS+bK8tlm89DtNm0K?= =?us-ascii?Q?8GQr8abmNG6cZaJR5c331YCWVXfsB/qM9IoSqmBN8jCNrAakb8imgSLttB1x?= =?us-ascii?Q?naP9PVcmnyodahOEBKwsuOeeK9GSRU28BtDnIk9+ROoXJGGXShyPfu4L5UIK?= =?us-ascii?Q?7YPcBQFTNlMndLojmZVweU6joIDqrpF495CtwubrwaOnEsgy1DYOlUNUDMul?= =?us-ascii?Q?TK+wvGxuFXDgPrsZv+IzYvq1mRvOm2aRYr4jRmwCm7GPOyLrjSoooCZheXiu?= =?us-ascii?Q?GKC/2T3KVlTt+jm3YkqEov6grKHw793fWsQAS0wM8lEsEJFe+aAahaC5iFF3?= =?us-ascii?Q?cv7/u2YTzBEq1/QJHOvb9ey8S9dPOkhNn4jJ0veh8k049Bwf04Yo5yeCoXeb?= =?us-ascii?Q?NUwkw3fsQbneMjSAsZujhBt2bszlDzR/PpdaRhG5gvyM5hAQ1IpQ85c4wS98?= =?us-ascii?Q?dg=3D=3D?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: PH8PR11MB6856.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: aab0c664-8c1c-49bc-3228-08db085973fa X-MS-Exchange-CrossTenant-originalarrivaltime: 06 Feb 2023 15:47:29.3164 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: s+A3xDFpiaNAodpvx1PWLyQhEwaKnsXkL2Ghz7s3+Y7mXn/9JKrlddzcY8+l/6DLiazgq5WNnmVFhgKKMuKWypahn4AwNVVrOsPMHKlzj4g= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY1PR11MB8032 Return-Path: andrei.warkentin@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Andrei Warkentin -----Original Message----- From: devel@edk2.groups.io On Behalf Of Sunil V L Sent: Saturday, January 28, 2023 1:18 PM To: devel@edk2.groups.io Cc: Kinney, Michael D ; Gao, Liming ; Liu, Zhiguang ; Abner Chang Subject: [edk2-devel] [edk2-staging/RiscV64QemuVirt PATCH V7 03/20] MdePkg:= Add BaseRiscVSbiLib Library for RISC-V REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4076 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 Cc: Liming Gao Cc: Zhiguang Liu Signed-off-by: Sunil V L Acked-by: Abner Chang --- 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..ca2e= 4dcf815c 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 =20 +[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 =20 +[Components.RISCV64] + MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf + [BuildOptions] diff --git a/MdePkg/Library/BaseRiscVSbiLib/BaseRiscVSbiLib.inf b/MdePkg/Li= brary/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=20 +rights reserved.
# # SPDX-License-Identifier: BSD-2-Clause-Patent=20 +# ## + +[Defines] + INF_VERSION =3D 0x0001001b + BASE_NAME =3D BaseRiscVSbiLib + FILE_GUID =3D D742CF3D-E600-4009-8FB5-318073008508 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D RiscVSbiLib + +[Sources] + BaseRiscVSbiLib.c + +[Packages] + MdePkg/MdePkg.dec + +[LibraryClasses] + BaseLib diff --git a/MdePkg/Include/Library/BaseRiscVSbiLib.h b/MdePkg/Include/Libr= ary/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=20 + reserved.
+ + 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 + +/* 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 = =20 +0x0 + +/* SBI function IDs for SRST extension */ #define SBI_EXT_SRST_RESET =20 +0x0 + +#define SBI_SRST_RESET_TYPE_SHUTDOWN 0x0 +#define SBI_SRST_RESET_TYPE_COLD_REBOOT 0x1 #define=20 +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/Libr= ary/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=20 + reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include + +// +// 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 =3D SBI_ERR_INVALID_PARAM; + Ret.Value =3D -1; + return Ret; + } + + for (I =3D 0; I < SBI_CALL_MAX_ARGS; I++) { + if (I < NumArgs) { + Args[I] =3D VA_ARG (ArgList, UINTN); + } else { + // Default to 0 for all arguments that are not given + Args[I] =3D 0; + } + } + + VA_END (ArgList); + + register UINTN a0 asm ("a0") =3D Args[0]; register UINTN a1 asm=20 + ("a1") =3D Args[1]; register UINTN a2 asm ("a2") =3D Args[2]; register= =20 + UINTN a3 asm ("a3") =3D Args[3]; register UINTN a4 asm ("a4") =3D=20 + Args[4]; register UINTN a5 asm ("a5") =3D Args[5]; register UINTN a6= =20 + asm ("a6") =3D (UINTN)(FuncId); register UINTN a7 asm ("a7") =3D=20 + (UINTN)(ExtId); + + asm volatile ("ecall" \ + : "+r" (a0), "+r" (a1) \ + : "r" (a2), "r" (a3), "r" (a4), "r" (a5), "r" (a6), "r" (a7) \ + : "memory"); \ + Ret.Error =3D a0; + Ret.Value =3D a1; + return Ret; +} + +/** + Translate SBI error code to EFI status. + + @param[in] SbiError SBI error code + @retval EFI_STATUS +**/ +STATIC +EFI_STATUS +EFIAPI +TranslateError ( + IN UINTN SbiError + ) +{ + switch (SbiError) { + case SBI_SUCCESS: + return EFI_SUCCESS; + case SBI_ERR_FAILED: + return EFI_DEVICE_ERROR; + break; + case SBI_ERR_NOT_SUPPORTED: + return EFI_UNSUPPORTED; + break; + case SBI_ERR_INVALID_PARAM: + return EFI_INVALID_PARAMETER; + break; + case SBI_ERR_DENIED: + return EFI_ACCESS_DENIED; + break; + case SBI_ERR_INVALID_ADDRESS: + return EFI_LOAD_ERROR; + break; + case SBI_ERR_ALREADY_AVAILABLE: + return EFI_ALREADY_STARTED; + break; + default: + // + // Reaches here only if SBI has defined a new error type + // + ASSERT (FALSE); + return EFI_UNSUPPORTED; + break; + } +} + +/** + Clear pending timer interrupt bit and set timer for next event after Tim= e. + + To clear the timer without scheduling a timer event, set Time to a =20 + practically infinite value or mask the timer interrupt by clearing sie.ST= IE. + + @param[in] Time The time offset to the next scheduled t= imer interrupt. +**/ +VOID +EFIAPI +SbiSetTimer ( + IN UINT64 Time + ) +{ + SbiCall (SBI_EXT_TIME, SBI_EXT_TIME_SET_TIMER, 1, Time); } + +EFI_STATUS +EFIAPI +SbiSystemReset ( + IN UINTN ResetType, + IN UINTN ResetReason + ) +{ + SBI_RET Ret; + + Ret =3D SbiCall ( + SBI_EXT_SRST, + SBI_EXT_SRST_RESET, + 2, + ResetType, + ResetReason + ); + + return TranslateError (Ret.Error); +} + +/** + Get firmware context of the calling hart. + + @param[out] FirmwareContext The firmware context pointer. + @retval EFI_SUCCESS The operation succeeds. +**/ +VOID +EFIAPI +GetFirmwareContext ( + OUT EFI_RISCV_FIRMWARE_CONTEXT **FirmwareContext + ) +{ + *FirmwareContext =3D (EFI_RISCV_FIRMWARE_CONTEXT=20 +*)RiscVGetSupervisorScratch (); } + +/** + Set firmware context of the calling hart. + + @param[in] FirmwareContext The firmware context pointer. +**/ +VOID +EFIAPI +SetFirmwareContext ( + IN EFI_RISCV_FIRMWARE_CONTEXT *FirmwareContext + ) +{ + RiscVSetSupervisorScratch ((UINT64)FirmwareContext); } + +/** + Get pointer to OpenSBI Firmware Context + + Get the pointer of firmware context through OpenSBI FW Extension SBI. + + @param FirmwareContextPtr Pointer to retrieve pointer to the + Firmware Context. +**/ +VOID +EFIAPI +GetFirmwareContextPointer ( + IN OUT EFI_RISCV_FIRMWARE_CONTEXT **FirmwareContextPtr + ) +{ + GetFirmwareContext (FirmwareContextPtr); } + +/** + Set the pointer to OpenSBI Firmware Context + + Set the pointer of firmware context through OpenSBI FW Extension SBI. + + @param FirmwareContextPtr Pointer to Firmware Context. +**/ +VOID +EFIAPI +SetFirmwareContextPointer ( + IN EFI_RISCV_FIRMWARE_CONTEXT *FirmwareContextPtr + ) +{ + SetFirmwareContext (FirmwareContextPtr); } -- 2.38.0