From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 6A10DD80DF6 for ; Tue, 17 Oct 2023 12:18:06 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=fGptTcugpTzXnA6wbkL8CgMW7pZC8sS93P/zP8HeIdU=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1697545085; v=1; b=cgq9Ew96WKM0fytGPhLeg9hL9hoWFaNoAv+jFyFaAs7T0Ym3RZhsUZZx6HW61i4WPsIbDEIb 9A+57JjCa+dA4kp0CUPSpylJNkn0DizaQ/Iu4Wx65xuTOJ6leMbghJ98g1EWlUqkf64LVq16q0y TuJf2IsIUCUrXeEe/u96FDhY= X-Received: by 127.0.0.2 with SMTP id bgFVYY7687511x28Y3OnRrUR; Tue, 17 Oct 2023 05:18:05 -0700 X-Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) by mx.groups.io with SMTP id smtpd.web10.211977.1697545084126046218 for ; Tue, 17 Oct 2023 05:18:04 -0700 X-Received: by mail-pg1-f173.google.com with SMTP id 41be03b00d2f7-578b4997decso4149302a12.0 for ; Tue, 17 Oct 2023 05:18:03 -0700 (PDT) X-Gm-Message-State: YERjrsvYBsMl4MVimZ5gg4hgx7686176AA= X-Google-Smtp-Source: AGHT+IFGu/0ySPhRnlXBP/4Vq47+lLOKcRbImyaUJ6yOvfrBDdB1pnceJfwuakEhhWBD2cF8acG5nA== X-Received: by 2002:a05:6a21:18d:b0:159:beec:79ba with SMTP id le13-20020a056a21018d00b00159beec79bamr2128514pzb.2.1697545083167; Tue, 17 Oct 2023 05:18:03 -0700 (PDT) X-Received: from dhaval.. ([2401:4900:1c80:4a26:d125:5df7:226b:329]) by smtp.gmail.com with ESMTPSA id f7-20020a17090274c700b001c62b9a51a4sm1379511plt.239.2023.10.17.05.18.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 05:18:02 -0700 (PDT) From: "Dhaval Sharma" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Jiewen Yao , Jordan Justen , Gerd Hoffmann , Sunil V L , Andrei Warkentin Subject: [edk2-devel] [PATCH v5 1/2] MdePkg:Implement RISCV CMO Date: Tue, 17 Oct 2023 17:47:54 +0530 Message-Id: <20231017121755.190285-2-dhaval@rivosinc.com> In-Reply-To: <20231017121755.190285-1-dhaval@rivosinc.com> References: <20231017121755.190285-1-dhaval@rivosinc.com> MIME-Version: 1.0 Precedence: Bulk 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 List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=cgq9Ew96; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io; dmarc=none Implementing 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. In that case we fallback on fence.i instructions. 2. Rely on the fact that platform init has initialized CMO and this implementation just checks if it is enabled. 3. In order to avoid compiler dependency injecting byte code. 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: Ard Biesheuvel Cc: Jiewen Yao Cc: Jordan Justen Cc: Gerd Hoffmann Cc: Sunil V L Cc: Andrei Warkentin Signed-off-by: Dhaval Sharma --- Notes: v5: - Addressed comments from v4 - Use #defines instead of numbers in cache instruction encoding - Addressed function naming issues from previous patch - Added new PCD to override RV CPU features - Removed code that relied on ENVCFG registers - Fixing typos in comments MdePkg/MdePkg.dec | 7 + MdePkg/Library/BaseCacheMaintenanceLib/BaseCacheMaintenanceLib.inf | 3 +- MdePkg/Library/BaseLib/BaseLib.inf | 2 +- MdePkg/Include/Register/RiscV64/RiscVEncoding.h | 6 + MdePkg/Library/BaseCacheMaintenanceLib/RiscVCache.c | 203 += ++++++++++++++++--- MdePkg/Library/BaseLib/RiscV64/FlushCache.S | 21 -- MdePkg/Library/BaseLib/RiscV64/RiscVCacheMgmt.S | 38 += +++ 7 files changed, 234 insertions(+), 46 deletions(-) diff --git a/MdePkg/MdePkg.dec b/MdePkg/MdePkg.dec index ac54338089e8..2d06cf46b1ca 100644 --- a/MdePkg/MdePkg.dec +++ b/MdePkg/MdePkg.dec @@ -2399,6 +2399,13 @@ [PcdsFixedAtBuild.AARCH64, PcdsPatchableInModule.AAR= CH64] # @Prompt CPU Rng algorithm's GUID.=0D gEfiMdePkgTokenSpaceGuid.PcdCpuRngSupportedAlgorithm|{0x00,0x00,0x00,0x0= 0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}|VOID*|0x0000= 0037=0D =0D +[PcdsFixedAtBuild.RISCV64, PcdsPatchableInModule.RISCV64]=0D + #=0D + # Configurability to override RV CPU Features=0D + # BIT 0 =3D CMO=0D + #=0D + gEfiMdePkgTokenSpaceGuid.PcdRVFeatureOverride|0x1|UINT64|0x69=0D +=0D [PcdsFixedAtBuild, PcdsPatchableInModule, PcdsDynamic, PcdsDynamicEx]=0D ## This value is used to set the base address of PCI express hierarchy.= =0D # @Prompt PCI Express Base Address.=0D diff --git a/MdePkg/Library/BaseCacheMaintenanceLib/BaseCacheMaintenanceLib= .inf b/MdePkg/Library/BaseCacheMaintenanceLib/BaseCacheMaintenanceLib.inf index 6fd9cbe5f6c9..037a0b49800a 100644 --- a/MdePkg/Library/BaseCacheMaintenanceLib/BaseCacheMaintenanceLib.inf +++ b/MdePkg/Library/BaseCacheMaintenanceLib/BaseCacheMaintenanceLib.inf @@ -55,4 +55,5 @@ [Packages] [LibraryClasses]=0D BaseLib=0D DebugLib=0D -=0D +[Pcd]=0D + gEfiMdePkgTokenSpaceGuid.PcdRVFeatureOverride=0D diff --git a/MdePkg/Library/BaseLib/BaseLib.inf b/MdePkg/Library/BaseLib/Ba= seLib.inf index 03c7b02e828b..53389389448c 100644 --- a/MdePkg/Library/BaseLib/BaseLib.inf +++ b/MdePkg/Library/BaseLib/BaseLib.inf @@ -400,7 +400,7 @@ [Sources.RISCV64] RiscV64/RiscVCpuBreakpoint.S | GCC=0D RiscV64/RiscVCpuPause.S | GCC=0D RiscV64/RiscVInterrupt.S | GCC=0D - RiscV64/FlushCache.S | GCC=0D + RiscV64/RiscVCacheMgmt.S | GCC=0D RiscV64/CpuScratch.S | GCC=0D RiscV64/ReadTimer.S | GCC=0D RiscV64/RiscVMmu.S | GCC=0D diff --git a/MdePkg/Include/Register/RiscV64/RiscVEncoding.h b/MdePkg/Inclu= de/Register/RiscV64/RiscVEncoding.h index 2bde8db478ff..5d6dcab12f74 100644 --- a/MdePkg/Include/Register/RiscV64/RiscVEncoding.h +++ b/MdePkg/Include/Register/RiscV64/RiscVEncoding.h @@ -117,4 +117,10 @@ #define CAUSE_VIRTUAL_INST_FAULT 0x16=0D #define CAUSE_STORE_GUEST_PAGE_FAULT 0x17=0D =0D +#define CPU_FLUSH_CMO_ASM 0x0025200f=0D +=0D +#define CPU_CLEAN_CMO_ASM 0x0015200f=0D +=0D +#define CPU_INVLD_CMO_ASM 0x0005200f=0D +=0D #endif=0D diff --git a/MdePkg/Library/BaseCacheMaintenanceLib/RiscVCache.c b/MdePkg/L= ibrary/BaseCacheMaintenanceLib/RiscVCache.c index d08fb9f193ca..bd8794e1d818 100644 --- a/MdePkg/Library/BaseCacheMaintenanceLib/RiscVCache.c +++ b/MdePkg/Library/BaseCacheMaintenanceLib/RiscVCache.c @@ -1,7 +1,8 @@ /** @file=0D - RISC-V specific functionality for cache.=0D + Implement Risc-V Cache Management Operations=0D =0D Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All right= s reserved.
=0D + Copyright (c) 2023, Rivos Inc. All rights reserved.
=0D =0D SPDX-License-Identifier: BSD-2-Clause-Patent=0D **/=0D @@ -9,6 +10,17 @@ #include =0D #include =0D #include =0D +#include =0D +=0D +// TODO: This will be removed once RISC-V CPU HOB is available=0D +#define RV64_CACHE_BLOCK_SIZE 64=0D +#define RV_CPU_FEATURE_CMO_BITMASK 0x1=0D +=0D +typedef enum {=0D + Clean,=0D + Flush,=0D + Invld,=0D +} CACHE_OP;=0D =0D /**=0D RISC-V invalidate instruction cache.=0D @@ -16,7 +28,7 @@ **/=0D VOID=0D EFIAPI=0D -RiscVInvalidateInstCacheAsm (=0D +RiscVInvalidateInstCacheAsmFence (=0D VOID=0D );=0D =0D @@ -26,13 +38,134 @@ RiscVInvalidateInstCacheAsm ( **/=0D VOID=0D EFIAPI=0D -RiscVInvalidateDataCacheAsm (=0D +RiscVInvalidateDataCacheAsmFence (=0D VOID=0D );=0D =0D +/**=0D + RISC-V flush cache block. Atomically perform a clean operation=0D + followed by an invalidate operation=0D +=0D +**/=0D +VOID=0D +EFIAPI=0D +RiscVCpuCacheFlushAsmCbo (=0D + UINTN=0D + );=0D +=0D +/**=0D +Perform a write transfer to another cache or to memory if the=0D +data in the copy of the cache block have been modified by a store=0D +operation=0D +=0D +**/=0D +VOID=0D +EFIAPI=0D +RiscVCpuCacheCleanAsmCbo (=0D + UINTN=0D + );=0D +=0D +/**=0D +Deallocate the copy of the cache block=0D +=0D +**/=0D +VOID=0D +EFIAPI=0D +RiscVCpuCacheInvalAsmCbo (=0D + UINTN=0D + );=0D +=0D +/**=0D +Verify CBOs are supported by this HW=0D +TODO: Use RISC-V CPU HOB once available.=0D +=0D +**/=0D +UINT64=0D +RiscvIsCMOEnabled (=0D + VOID=0D + )=0D +{=0D + // TODO: Add check for CMO from CPU HOB.=0D + // If CMO is disabled in HW, skip Override check=0D + // Otherwise this PCD can override settings=0D + return (PcdGet64 (PcdRVFeatureOverride) & RV_CPU_FEATURE_CMO_BITMASK);=0D +}=0D +=0D +/**=0D + Performs required opeartion on cache lines in the cache coherency domain= =0D + of the calling CPU. If Address is not aligned on a cache line boundary,= =0D + then entire cache line containing Address is operated. If Address + Leng= th=0D + is not aligned on a cache line boundary, then the entire cache line=0D + containing Address + Length -1 is operated.=0D + If Length is greater than (MAX_ADDRESS - Address + 1), then ASSERT().=0D + @param Address The base address of the cache lines to=0D + invalidate.=0D + @param Length The number of bytes to invalidate from the instruction=0D + cache.=0D + @param Op Type of CMO operation to be performed=0D + @return Address.=0D +=0D +**/=0D +VOID *=0D +EFIAPI=0D +CacheOpCacheRange (=0D + IN VOID *Address,=0D + IN UINTN Length,=0D + IN CACHE_OP Op=0D + )=0D +{=0D + UINTN CacheLineSize;=0D + UINTN Start;=0D + UINTN End;=0D +=0D + if (Length =3D=3D 0) {=0D + return Address;=0D + }=0D +=0D + ASSERT ((Length - 1) <=3D (MAX_ADDRESS - (UINTN)Address));=0D +=0D + CacheLineSize =3D RV64_CACHE_BLOCK_SIZE;=0D +=0D + Start =3D (UINTN)Address;=0D + //=0D + // Calculate the cache line alignment=0D + //=0D + End =3D (Start + Length + (CacheLineSize - 1)) & ~(CacheLineSize - 1)= ;=0D + Start &=3D ~((UINTN)CacheLineSize - 1);=0D +=0D + DEBUG (=0D + (DEBUG_INFO,=0D + "%a Performing Cache Management Operation %d \n", __func__, Op)=0D + );=0D +=0D + do {=0D + switch (Op) {=0D + case Invld:=0D + RiscVCpuCacheInvalAsmCbo (Start);=0D + break;=0D + case Flush:=0D + RiscVCpuCacheFlushAsmCbo (Start);=0D + break;=0D + case Clean:=0D + RiscVCpuCacheCleanAsmCbo (Start);=0D + break;=0D + default:=0D + DEBUG ((DEBUG_ERROR, "RISC-V unsupported operation\n"));=0D + break;=0D + }=0D +=0D + Start =3D Start + CacheLineSize;=0D + } while (Start !=3D End);=0D +=0D + return Address;=0D +}=0D +=0D /**=0D Invalidates the entire instruction cache in cache coherency domain of th= e=0D - calling CPU.=0D + calling CPU. Risc-V does not have currently an CBO implementation which = can=0D + invalidate entire I-cache. Hence using Fence instruction for now. P.S. F= ence=0D + instruction may or may not implement full I-cache invd functionality on = all=0D + implementations.=0D =0D **/=0D VOID=0D @@ -41,7 +174,7 @@ InvalidateInstructionCache ( VOID=0D )=0D {=0D - RiscVInvalidateInstCacheAsm ();=0D + RiscVInvalidateInstCacheAsmFence ();=0D }=0D =0D /**=0D @@ -76,12 +209,17 @@ InvalidateInstructionCacheRange ( IN UINTN Length=0D )=0D {=0D - DEBUG (=0D - (DEBUG_WARN,=0D - "%a:RISC-V unsupported function.\n"=0D - "Invalidating the whole instruction cache instead.\n", __func__)=0D - );=0D - InvalidateInstructionCache ();=0D + if (RiscvIsCMOEnabled () !=3D 0) {=0D + CacheOpCacheRange (Address, Length, Invld);=0D + } else {=0D + DEBUG (=0D + (DEBUG_WARN,=0D + "%a:RISC-V unsupported function.\n"=0D + "Invalidating the whole instruction cache instead.\n", __func__)=0D + );=0D + InvalidateInstructionCache ();=0D + }=0D +=0D return Address;=0D }=0D =0D @@ -137,7 +275,12 @@ WriteBackInvalidateDataCacheRange ( IN UINTN Length=0D )=0D {=0D - DEBUG ((DEBUG_ERROR, "%a:RISC-V unsupported function.\n", __func__));=0D + if (RiscvIsCMOEnabled () !=3D 0) {=0D + CacheOpCacheRange (Address, Length, Flush);=0D + } else {=0D + DEBUG ((DEBUG_ERROR, "%a:RISC-V unsupported function.\n", __func__));= =0D + }=0D +=0D return Address;=0D }=0D =0D @@ -176,10 +319,7 @@ WriteBackDataCache ( =0D If Length is greater than (MAX_ADDRESS - Address + 1), then ASSERT().=0D =0D - @param Address The base address of the data cache lines to write back. = If=0D - the CPU is in a physical addressing mode, then Address i= s a=0D - physical address. If the CPU is in a virtual addressing= =0D - mode, then Address is a virtual address.=0D + @param Address The base address of the data cache lines to write back.= =0D @param Length The number of bytes to write back from the data cache.=0D =0D @return Address of cache written in main memory.=0D @@ -192,7 +332,12 @@ WriteBackDataCacheRange ( IN UINTN Length=0D )=0D {=0D - DEBUG ((DEBUG_ERROR, "%a:RISC-V unsupported function.\n", __func__));=0D + if (RiscvIsCMOEnabled () !=3D 0) {=0D + CacheOpCacheRange (Address, Length, Clean);=0D + } else {=0D + DEBUG ((DEBUG_ERROR, "%a:RISC-V unsupported function.\n", __func__));= =0D + }=0D +=0D return Address;=0D }=0D =0D @@ -213,7 +358,12 @@ InvalidateDataCache ( VOID=0D )=0D {=0D - RiscVInvalidateDataCacheAsm ();=0D + DEBUG (=0D + (DEBUG_WARN,=0D + "%a:RISC-V unsupported function.\n"=0D + "Invalidating the whole Data cache instead.\n", __func__)=0D + );=0D + RiscVInvalidateDataCacheAsmFence ();=0D }=0D =0D /**=0D @@ -234,10 +384,7 @@ InvalidateDataCache ( =0D If Length is greater than (MAX_ADDRESS - Address + 1), then ASSERT().=0D =0D - @param Address The base address of the data cache lines to invalidate. = If=0D - the CPU is in a physical addressing mode, then Address i= s a=0D - physical address. If the CPU is in a virtual addressing = mode,=0D - then Address is a virtual address.=0D + @param Address The base address of the data cache lines to invalidate.= =0D @param Length The number of bytes to invalidate from the data cache.=0D =0D @return Address.=0D @@ -250,6 +397,16 @@ InvalidateDataCacheRange ( IN UINTN Length=0D )=0D {=0D - DEBUG ((DEBUG_ERROR, "%a:RISC-V unsupported function.\n", __func__));=0D + if (RiscvIsCMOEnabled () !=3D 0) {=0D + CacheOpCacheRange (Address, Length, Invld);=0D + } else {=0D + DEBUG (=0D + (DEBUG_WARN,=0D + "%a:RISC-V unsupported function.\n"=0D + "Invalidating the whole Data cache instead.\n", __func__)=0D + );=0D + InvalidateDataCache ();=0D + }=0D +=0D return Address;=0D }=0D diff --git a/MdePkg/Library/BaseLib/RiscV64/FlushCache.S b/MdePkg/Library/B= aseLib/RiscV64/FlushCache.S deleted file mode 100644 index 7c10fdd268af..000000000000 --- a/MdePkg/Library/BaseLib/RiscV64/FlushCache.S +++ /dev/null @@ -1,21 +0,0 @@ -//------------------------------------------------------------------------= ------=0D -//=0D -// RISC-V cache operation.=0D -//=0D -// Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All righ= ts reserved.
=0D -//=0D -// SPDX-License-Identifier: BSD-2-Clause-Patent=0D -//=0D -//------------------------------------------------------------------------= ------=0D -=0D -.align 3=0D -ASM_GLOBAL ASM_PFX(RiscVInvalidateInstCacheAsm)=0D -ASM_GLOBAL ASM_PFX(RiscVInvalidateDataCacheAsm)=0D -=0D -ASM_PFX(RiscVInvalidateInstCacheAsm):=0D - fence.i=0D - ret=0D -=0D -ASM_PFX(RiscVInvalidateDataCacheAsm):=0D - fence=0D - ret=0D diff --git a/MdePkg/Library/BaseLib/RiscV64/RiscVCacheMgmt.S b/MdePkg/Libra= ry/BaseLib/RiscV64/RiscVCacheMgmt.S new file mode 100644 index 000000000000..f9b79446b56a --- /dev/null +++ b/MdePkg/Library/BaseLib/RiscV64/RiscVCacheMgmt.S @@ -0,0 +1,38 @@ +//------------------------------------------------------------------------= ------=0D +//=0D +// RISC-V cache operation.=0D +//=0D +// Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All righ= ts reserved.
=0D +// Copyright (c) 2022, Rivos Inc. All rights reserved.
=0D +//=0D +// SPDX-License-Identifier: BSD-2-Clause-Patent=0D +//=0D +//------------------------------------------------------------------------= ------=0D +#include =0D +=0D +.align 3=0D +ASM_GLOBAL ASM_PFX(RiscVInvalidateInstCacheAsmFence)=0D +ASM_GLOBAL ASM_PFX(RiscVInvalidateDataCacheAsmFence)=0D +=0D +ASM_PFX(RiscVInvalidateInstCacheAsmFence):=0D + fence.i=0D + ret=0D +=0D +ASM_PFX(RiscVInvalidateDataCacheAsmFence):=0D + fence=0D + ret=0D +=0D +ASM_GLOBAL ASM_PFX (RiscVCpuCacheFlushAsmCbo)=0D +ASM_PFX (RiscVCpuCacheFlushAsmCbo):=0D + .long CPU_FLUSH_CMO_ASM=0D + ret=0D +=0D +ASM_GLOBAL ASM_PFX (RiscVCpuCacheCleanAsmCbo)=0D +ASM_PFX (RiscVCpuCacheCleanAsmCbo):=0D + .long CPU_CLEAN_CMO_ASM=0D + ret=0D +=0D +ASM_GLOBAL ASM_PFX (RiscVCpuCacheInvalAsmCbo)=0D +ASM_PFX (RiscVCpuCacheInvalAsmCbo):=0D + .long CPU_INVLD_CMO_ASM=0D + ret=0D --=20 2.39.2 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#109678): https://edk2.groups.io/g/devel/message/109678 Mute This Topic: https://groups.io/mt/102016148/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-