From nobody Fri Apr 19 08:40:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+101796+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+101796+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1679672630; cv=none; d=zohomail.com; s=zohoarc; b=nO/YXiE3cmx/1GJsfdhpjNLhUMCb4t+gvvJUJByH/hlfseILO1RqvhrMSxo5Ju/jXBaXB/ecVHTNIOqD8OS2z4sNpJ/ixdwYKwimk8a+xY5/WzrG8ZUvx44nhTllxSacs/E4A1R3X9oenmiDGyFwYWF3r2yHgZcu2BCv20ZUdzg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1679672630; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=AQmsGPeGbNeKtEs+B2t7NyaW/K9kiwuP+j3fwhDejIc=; b=Xh5QWGqdROa1qkx9rXeU07jjgY7nmTTKzrTRdQlp0u9UDe88Fh78/9S3xgBqaYJFGnlF8zSd2Tg8VJl5kOPA/3U4z20lDWZBypl6IqqHnd9O1YEjhRkxWDdQtnu7R9y3faBiS5rXR+fvubWrbKa2zNa6wZ1U20npsGl4SYpxzno= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+101796+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1679672630824575.4993153711622; Fri, 24 Mar 2023 08:43:50 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id iSsfYY1788612xtviiugcZpi; Fri, 24 Mar 2023 08:43:50 -0700 X-Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by mx.groups.io with SMTP id smtpd.web10.105577.1679672629359329183 for ; Fri, 24 Mar 2023 08:43:49 -0700 X-Received: by mail-pl1-f174.google.com with SMTP id o2so2163806plg.4 for ; Fri, 24 Mar 2023 08:43:49 -0700 (PDT) X-Gm-Message-State: lNthobJ63MoeBtwnWpThzHUpx1787277AA= X-Google-Smtp-Source: AKy350ayoIMTGDKKXt9QnnL2qqomyYLsGn7cYdPk6VO6Sr0uOZr/6roJbTflj1i7VzUCplsbYoLr3w== X-Received: by 2002:a17:903:27ce:b0:19c:da7f:a234 with SMTP id km14-20020a17090327ce00b0019cda7fa234mr2602068plb.67.1679672628564; Fri, 24 Mar 2023 08:43:48 -0700 (PDT) X-Received: from dhaval.ba.rivosinc.com ([171.76.83.64]) by smtp.gmail.com with ESMTPSA id je19-20020a170903265300b0019f3e339fb4sm14365369plb.187.2023.03.24.08.43.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Mar 2023 08:43:48 -0700 (PDT) From: "Dhaval Sharma" To: devel@edk2.groups.io Cc: Sunil V L , Andrei Warkentin , Daniel Schaefer Subject: [edk2-devel] [PATCH v1 1/2] MdePkg/BaseCacheMaintenanceLib: Enable RISCV CMO Date: Fri, 24 Mar 2023 21:13:41 +0530 Message-Id: <20230324154342.180062-2-dhaval@rivosinc.com> In-Reply-To: <20230324154342.180062-1-dhaval@rivosinc.com> References: <20230324154342.180062-1-dhaval@rivosinc.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,dhaval@rivosinc.com Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1679672630; bh=pnNmeRrHd1ebYvQlsfD7CC7EeQb27gyl4u5b7ppmiTw=; h=Cc:Date:From:Reply-To:Subject:To; b=C3aUFuYpd91qVVg+D2bCgOV7chBQD9iKkLUFh368G7cEf9h5JdtJ+wr/GS+km6Lx8Ny B9MTNBuJABBecVCgTByoV07+KMNHOcX6zinTgNGUh/q0I5V9fWZRSO3n0qdNfkPvy1K+a q5aCigaDDiEcigK8MzoRIGdzEImtJnmogYI= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1679672631973100001 Content-Type: text/plain; charset="utf-8" Adding code to support Cache Management Operations (CMO) defined by RV spec https://github.com/riscv/riscv-CMOs Notes: 1. CMO only supports block based Operations. Meaning complete cache flush/invd/clean Operations are not available 2. Current implementation uses ifence instructions but it maybe platform specific. Many platforms may not support cache Operations based on ifence. 3. For now adding CMO on top of ifence as it is not considered harmful. 4. This requires support for GCC12.2 onwards. Test: 1. Ensured correct instructions are refelecting in asm 2. Able to boot platform with RiscVVirtQemu config 3. Not able to verify actual instruction in HW as Qemu ignores any actual cache operations. Cc: Sunil V L Cc: Andrei Warkentin Cc: Daniel Schaefer Signed-off-by: Dhaval Sharma --- MdePkg/Library/BaseLib/BaseLib.inf | 1 + MdePkg/Library/BaseCacheMaintenanceLib/RiscVCache.c | 126 ++++++++++++++++= ++-- MdePkg/Library/BaseLib/RiscV64/RiscVCpuCache.S | 23 ++++ 3 files changed, 143 insertions(+), 7 deletions(-) diff --git a/MdePkg/Library/BaseLib/BaseLib.inf b/MdePkg/Library/BaseLib/Ba= seLib.inf index 3a48492b1a01..0d6d6b7414c8 100644 --- a/MdePkg/Library/BaseLib/BaseLib.inf +++ b/MdePkg/Library/BaseLib/BaseLib.inf @@ -398,6 +398,7 @@ [Sources.RISCV64] RiscV64/MemoryFence.S | GCC RiscV64/RiscVSetJumpLongJump.S | GCC RiscV64/RiscVCpuBreakpoint.S | GCC + RiscV64/RiscVCpuCache.S | GCC RiscV64/RiscVCpuPause.S | GCC RiscV64/RiscVInterrupt.S | GCC RiscV64/FlushCache.S | GCC diff --git a/MdePkg/Library/BaseCacheMaintenanceLib/RiscVCache.c b/MdePkg/L= ibrary/BaseCacheMaintenanceLib/RiscVCache.c index d08fb9f193ca..8e88b8391a74 100644 --- a/MdePkg/Library/BaseCacheMaintenanceLib/RiscVCache.c +++ b/MdePkg/Library/BaseCacheMaintenanceLib/RiscVCache.c @@ -10,9 +10,111 @@ #include #include =20 +/** + Use runtime discovery mechanism in future when avalable + through https://lists.riscv.org/g/tech-privileged/topic/83853282 +**/ +#define RV64_CACHE_BLOCK_SIZE 64 + +typedef enum{ + cln, + flsh, + invd, +}CACHE_OP; + +/* Ideally we should do this through BaseLib.h by adding + Asm*CacheLine functions. This can be done after Initial + RV refactoring is complete. For now call functions directly +*/ +VOID +EFIAPI RiscVCpuCacheFlush ( + UINTN + ); + +VOID +EFIAPI RiscVCpuCacheClean ( + UINTN + ); + +VOID +EFIAPI RiscVCpuCacheInval ( + UINTN + ); + +/** + Performs required opeartion on cache lines in the cache coherency domain + of the calling CPU. If Address is not aligned on a cache line boundary, + then entire cache line containing Address is operated. If Address + Leng= th + is not aligned on a cache line boundary, then the entire cache line + containing Address + Length -1 is operated. + + If Length is greater than (MAX_ADDRESS - Address + 1), then ASSERT(). + + @param Address The base address of the cache lines to + invalidate. If the CPU is in a physical addressing mode,= then + Address is a physical address. If the CPU is in a virtual + addressing mode, then Address is a virtual address. + + @param Length The number of bytes to invalidate from the instruction c= ache. + + @return Address. + +**/ + +VOID * +EFIAPI +CacheOpCacheRange ( + IN VOID *Address, + IN UINTN Length, + IN CACHE_OP op + ) +{ + UINTN CacheLineSize; + UINTN Start; + UINTN End; + + if (Length =3D=3D 0) { + return Address; + } + + ASSERT ((Length - 1) <=3D (MAX_ADDRESS - (UINTN)Address)); + + // + // Cache line size is 8 * Bits 15-08 of EBX returned from CPUID 01H + // + CacheLineSize =3D RV64_CACHE_BLOCK_SIZE; + + Start =3D (UINTN)Address; + // + // Calculate the cache line alignment + // + End =3D (Start + Length + (CacheLineSize - 1)) & ~(CacheLineSize - 1); + Start &=3D ~((UINTN)CacheLineSize - 1); + + do { + switch (op) { + case invd: + RiscVCpuCacheInval(Start); + break; + case flsh: + RiscVCpuCacheFlush(Start); + break; + case cln: + RiscVCpuCacheClean(Start); + break; + default: + DEBUG ((DEBUG_ERROR, "%a:RISC-V unsupported operation\n")); + break; + } + + Start =3D Start + CacheLineSize; + } while (Start !=3D End); + + return Address; +} + /** RISC-V invalidate instruction cache. - **/ VOID EFIAPI @@ -22,7 +124,6 @@ RiscVInvalidateInstCacheAsm ( =20 /** RISC-V invalidate data cache. - **/ VOID EFIAPI @@ -32,7 +133,9 @@ RiscVInvalidateDataCacheAsm ( =20 /** Invalidates the entire instruction cache in cache coherency domain of the - calling CPU. + calling CPU. This may not clear $IC on all RV implementations. + RV CMO only offers block operations as per spec. Entire cache invd will = be + platform dependent implementation. =20 **/ VOID @@ -77,11 +180,13 @@ InvalidateInstructionCacheRange ( ) { DEBUG ( - (DEBUG_WARN, + (DEBUG_ERROR, "%a:RISC-V unsupported function.\n" "Invalidating the whole instruction cache instead.\n", __func__) ); InvalidateInstructionCache (); + //RV does not support $I specific operation. + CacheOpCacheRange(Address, Length, invd); return Address; } =20 @@ -93,6 +198,8 @@ InvalidateInstructionCacheRange ( of the calling CPU. This function guarantees that all dirty cache lines = are written back to system memory, and also invalidates all the data cache l= ines in the cache coherency domain of the calling CPU. + RV CMO only offers block operations as per spec. Entire cache invd will = be + platform dependent implementation. =20 **/ VOID @@ -137,7 +244,7 @@ WriteBackInvalidateDataCacheRange ( IN UINTN Length ) { - DEBUG ((DEBUG_ERROR, "%a:RISC-V unsupported function.\n", __func__)); + CacheOpCacheRange(Address, Length, flsh); return Address; } =20 @@ -149,6 +256,8 @@ WriteBackInvalidateDataCacheRange ( CPU. This function guarantees that all dirty cache lines are written bac= k to system memory. This function may also invalidate all the data cache line= s in the cache coherency domain of the calling CPU. + RV CMO only offers block operations as per spec. Entire cache invd will = be + platform dependent implementation. =20 **/ VOID @@ -192,7 +301,7 @@ WriteBackDataCacheRange ( IN UINTN Length ) { - DEBUG ((DEBUG_ERROR, "%a:RISC-V unsupported function.\n", __func__)); + CacheOpCacheRange(Address, Length, cln); return Address; } =20 @@ -205,6 +314,8 @@ WriteBackDataCacheRange ( written back to system memory. It is typically used for cache diagnostic= s. If the CPU does not support invalidation of the entire data cache, then a w= rite back and invalidate operation should be performed on the entire data cac= he. + RV CMO only offers block operations as per spec. Entire cache invd will = be + platform dependent implementation. =20 **/ VOID @@ -250,6 +361,7 @@ InvalidateDataCacheRange ( IN UINTN Length ) { - DEBUG ((DEBUG_ERROR, "%a:RISC-V unsupported function.\n", __func__)); + //RV does not support $D specific operation. + CacheOpCacheRange(Address, Length, invd); return Address; } diff --git a/MdePkg/Library/BaseLib/RiscV64/RiscVCpuCache.S b/MdePkg/Librar= y/BaseLib/RiscV64/RiscVCpuCache.S new file mode 100644 index 000000000000..0913ed3e9221 --- /dev/null +++ b/MdePkg/Library/BaseLib/RiscV64/RiscVCpuCache.S @@ -0,0 +1,23 @@ +//------------------------------------------------------------------------= ------ +// +// CpuPause for RISC-V +// +// Copyright (c) 2022, Rivos Inc. All rights reserved.
+// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +//------------------------------------------------------------------------= ------ +ASM_GLOBAL ASM_PFX(RiscVCpuCacheFlush) +ASM_PFX(RiscVCpuCacheFlush): + cbo.flush (a0) + ret + +ASM_GLOBAL ASM_PFX(RiscVCpuCacheClean) +ASM_PFX(RiscVCpuCacheClean): + cbo.clean (a0) + ret + +ASM_GLOBAL ASM_PFX(RiscVCpuCacheInval) +ASM_PFX(RiscVCpuCacheInval): + cbo.inval (a0) + ret --=20 2.40.0.rc0.57.g454dfcbddf -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#101796): https://edk2.groups.io/g/devel/message/101796 Mute This Topic: https://groups.io/mt/97826396/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Fri Apr 19 08:40:22 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+101797+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+101797+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1679672632; cv=none; d=zohomail.com; s=zohoarc; b=GZ32tnQAmIBB+k8nXwCrVQ8Z7W36QtLaME3q3ZkWCQGAq/l4x6g5hJDPjC5sgq0mKSDb2orbpnnna9UxycY4nWCcxOtFzaGzmYCEOXgMIg5Z1rCCzWJ7UkHB5OYIfu6621g1h2tjzgFnEPARqFSkh+6UvD1O4VbkPeDeWXfRV/M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1679672632; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=np7sX1dt8m9OI+LhwjWEu/tcjSEQvR23d0RRrSjKMdc=; b=YIxpzud1Ba6GAFMSXwZpHB1+BXI0ffh1Pd7nmiKoMY874XpvypUrAZBKcQJwksxzUPjp6/u97p5SPhYKTe4lGLHG+Utj1nnZwdTyGd1IOPmVy8I8nyPG+5kAEGXr/QA25sawZlxFZT9WyRAm1xKC83cM03pESlxKVbA0btD3DJ8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+101797+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 167967263206935.357279538738226; Fri, 24 Mar 2023 08:43:52 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id sCJUYY1788612xUOFDJS9L5K; Fri, 24 Mar 2023 08:43:51 -0700 X-Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by mx.groups.io with SMTP id smtpd.web10.105576.1679672627427833635 for ; Fri, 24 Mar 2023 08:43:51 -0700 X-Received: by mail-pl1-f174.google.com with SMTP id bc12so2201186plb.0 for ; Fri, 24 Mar 2023 08:43:51 -0700 (PDT) X-Gm-Message-State: ODCKQNLCO7eO0DxLaPQ8JFoqx1787277AA= X-Google-Smtp-Source: AKy350aw9O0GIBLFLtE46juai7J38nt9gLTMu7ojMCoMtfsObkUY7p/GkUKXoWBVFQccWZJGH95rLw== X-Received: by 2002:a17:903:8cf:b0:19e:500b:517a with SMTP id lk15-20020a17090308cf00b0019e500b517amr2224817plb.69.1679672630568; Fri, 24 Mar 2023 08:43:50 -0700 (PDT) X-Received: from dhaval.ba.rivosinc.com ([171.76.83.64]) by smtp.gmail.com with ESMTPSA id je19-20020a170903265300b0019f3e339fb4sm14365369plb.187.2023.03.24.08.43.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Mar 2023 08:43:50 -0700 (PDT) From: "Dhaval Sharma" To: devel@edk2.groups.io Cc: Sunil V L , Andrei Warkentin , Daniel Schaefer Subject: [edk2-devel] [PATCH v1 2/2] OvmfPkg/RiscVVirt: Enable CMO support Date: Fri, 24 Mar 2023 21:13:42 +0530 Message-Id: <20230324154342.180062-3-dhaval@rivosinc.com> In-Reply-To: <20230324154342.180062-1-dhaval@rivosinc.com> References: <20230324154342.180062-1-dhaval@rivosinc.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,dhaval@rivosinc.com Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1679672631; bh=xOwemSjwn/6o6RKYyEH7mr+sIQog3w/XDnDymjb83ng=; h=Cc:Date:From:Reply-To:Subject:To; b=BtRwa8N+ECKcUnoBtXGtRthSdHieMEUVDEdVhgyAHQHpwUQWWLYViGVH7PaqbQ/fsNM yi22isst/Z51wbgzRdnFd+j8DFwrM75AgdmHTw/ovnWL8/wfT+pOystKIm7hU2xwx0Yln n7Fe7JcuFkTbw9rRH34MWtTBzErix7rwYYU= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1679672633895100005 Content-Type: text/plain; charset="utf-8" Cc: Sunil V L Cc: Andrei Warkentin Cc: Daniel Schaefer Signed-off-by: Dhaval Sharma Add support for Cache Management Operations --- OvmfPkg/RiscVVirt/RiscVVirtQemu.dsc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/OvmfPkg/RiscVVirt/RiscVVirtQemu.dsc b/OvmfPkg/RiscVVirt/RiscVV= irtQemu.dsc index 28d9af4d79b9..16c591d94228 100644 --- a/OvmfPkg/RiscVVirt/RiscVVirtQemu.dsc +++ b/OvmfPkg/RiscVVirt/RiscVVirtQemu.dsc @@ -46,6 +46,12 @@ [Defines] DEFINE NETWORK_ALLOW_HTTP_CONNECTIONS =3D TRUE DEFINE NETWORK_ISCSI_ENABLE =3D FALSE =20 +# +# CMO support for RV. It depends on 2 factors. First support in compiler +# GCC:Binutils 2.39 (GCC12.2+) is required. +# + DEFINE RV_CMO_FEATURE_AVAILABLE =3D FALSE + !if $(NETWORK_SNP_ENABLE) =3D=3D TRUE !error "NETWORK_SNP_ENABLE is IA32/X64/EBC only" !endif @@ -112,6 +118,9 @@ [LibraryClasses.common] TpmPlatformHierarchyLib|SecurityPkg/Library/PeiDxeTpmPlatformHierarchyLi= bNull/PeiDxeTpmPlatformHierarchyLib.inf !endif =20 +!if $(RV_CMO_FEATURE_AVAILABLE) =3D=3D TRUE + CacheMaintenanceLib|MdePkg/Library/BaseCacheMaintenanceLib/BaseCacheMai= ntenanceLib.inf +!endif [LibraryClasses.common.DXE_DRIVER] ReportStatusCodeLib|MdeModulePkg/Library/DxeReportStatusCodeLib/DxeRepor= tStatusCodeLib.inf PciExpressLib|OvmfPkg/Library/BaseCachingPciExpressLib/BaseCachingPciExp= ressLib.inf --=20 2.40.0.rc0.57.g454dfcbddf -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#101797): https://edk2.groups.io/g/devel/message/101797 Mute This Topic: https://groups.io/mt/97826399/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-