[edk2-devel] [PATCH v4 12/37] UefiCpuPkg: Add CpuMmuLib.h to UefiCpuPkg

Chao Li posted 37 patches 2 years, 1 month ago
There is a newer version of this series
[edk2-devel] [PATCH v4 12/37] UefiCpuPkg: Add CpuMmuLib.h to UefiCpuPkg
Posted by Chao Li 2 years, 1 month ago
Add a new header file CpuMmuLib.h, whitch is referenced from
ArmPkg/Include/Library/ArmMmuLib.h. Currently, only support for
LoongArch64 is added, and more architectures can be accommodated in the
future.

BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=4584

Cc: Eric Dong <eric.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Sami Mujawar <sami.mujawar@arm.com>
Cc: Sunil V L <sunilvl@ventanamicro.com>
Cc: Andrei Warkentin <andrei.warkentin@intel.com>
Signed-off-by: Chao Li <lichao@loongson.cn>
Reviewed-by: Andrei Warkentin <andrei.warkentin@intel.com>
---
 UefiCpuPkg/Include/Library/CpuMmuLib.h | 67 ++++++++++++++++++++++++++
 UefiCpuPkg/UefiCpuPkg.dec              |  4 ++
 2 files changed, 71 insertions(+)
 create mode 100644 UefiCpuPkg/Include/Library/CpuMmuLib.h

diff --git a/UefiCpuPkg/Include/Library/CpuMmuLib.h b/UefiCpuPkg/Include/Library/CpuMmuLib.h
new file mode 100644
index 0000000000..97e0ec0586
--- /dev/null
+++ b/UefiCpuPkg/Include/Library/CpuMmuLib.h
@@ -0,0 +1,67 @@
+/** @file
+
+  Copyright (c) 2023 Loongson Technology Corporation Limited. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef CPU_MMU_LIB_H_
+#define CPU_MMU_LIB_H_
+
+#include <Uefi/UefiBaseType.h>
+
+#define EFI_MEMORY_CACHETYPE_MASK  (EFI_MEMORY_UC  | \
+                                    EFI_MEMORY_WC  | \
+                                    EFI_MEMORY_WT  | \
+                                    EFI_MEMORY_WB  | \
+                                    EFI_MEMORY_UCE   \
+                                    )
+
+typedef struct {
+  EFI_PHYSICAL_ADDRESS    PhysicalBase;
+  EFI_VIRTUAL_ADDRESS     VirtualBase;
+  UINTN                   Length;
+  UINTN                   Attributes;
+} MEMORY_REGION_DESCRIPTOR;
+
+/**
+  Finds the length and memory properties of the memory region corresponding to the specified base address.
+
+  @param[in]  BaseAddress    To find the base address of the memory region.
+  @param[in]  EndAddress     To find the end address of the memory region.
+  @param[out]  RegionLength    The length of the memory region found.
+  @param[out]  RegionAttributes    Properties of the memory region found.
+
+  @retval  EFI_SUCCESS    The corresponding memory area was successfully found
+           EFI_NOT_FOUND    No memory area found
+**/
+EFI_STATUS
+EFIAPI
+GetMemoryRegionAttributes (
+  IN     UINTN  BaseAddress,
+  IN     UINTN  EndAddress,
+  OUT    UINTN  *RegionLength,
+  OUT    UINTN  *RegionAttributes
+  );
+
+/**
+  Sets the Attributes  of the specified memory region.
+
+  @param[in]  BaseAddress    The base address of the memory region to set the Attributes.
+  @param[in]  Length         The length of the memory region to set the Attributes.
+  @param[in]  Attributes     The Attributes to be set.
+  @param[in]  AttributeMask  Mask of memory attributes to take into account.
+
+  @retval  EFI_SUCCESS    The Attributes was set successfully
+**/
+EFI_STATUS
+EFIAPI
+SetMemoryRegionAttributes (
+  IN EFI_PHYSICAL_ADDRESS  BaseAddress,
+  IN UINTN                 Length,
+  IN UINTN                 Attributes,
+  IN UINT64                AttributeMask
+  );
+
+#endif // CPU_MMU_LIB_H_
diff --git a/UefiCpuPkg/UefiCpuPkg.dec b/UefiCpuPkg/UefiCpuPkg.dec
index 154b1d06fe..150beae981 100644
--- a/UefiCpuPkg/UefiCpuPkg.dec
+++ b/UefiCpuPkg/UefiCpuPkg.dec
@@ -62,6 +62,10 @@
   ##  @libraryclass  Provides function for manipulating x86 paging structures.
   CpuPageTableLib|Include/Library/CpuPageTableLib.h
 
+[LibraryClasses.LoongArch64]
+  ##  @libraryclass  Provides macros and functions for the memory management unit.
+  CpuMmuLib|Include/Library/CpuMmuLib.h
+
   ## @libraryclass   Provides functions for manipulating smram savestate registers.
   MmSaveStateLib|Include/Library/MmSaveStateLib.h
 
-- 
2.27.0



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#112403): https://edk2.groups.io/g/devel/message/112403
Mute This Topic: https://groups.io/mt/103129095/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-
Re: [edk2-devel] [PATCH v4 12/37] UefiCpuPkg: Add CpuMmuLib.h to UefiCpuPkg
Posted by Ni, Ray 2 years, 1 month ago
Chao,
Thanks for providing such a cleaner lib API.
Only 2 comments in below:

> +
> +#define EFI_MEMORY_CACHETYPE_MASK  (EFI_MEMORY_UC  | \
> +                                    EFI_MEMORY_WC  | \
> +                                    EFI_MEMORY_WT  | \
> +                                    EFI_MEMORY_WB  | \
> +                                    EFI_MEMORY_UCE   \
> +                                    )

1. Why do you need to define the EFI_MEMORY_CACHETYPE_MASK here?
Can you just mention that the following APIs only return 5 bits: UC/WC/WT/WB/UCE
without defining the EFI_MEMORY_CACHETYPE_MASK?

> +
> +typedef struct {
> +  EFI_PHYSICAL_ADDRESS    PhysicalBase;
> +  EFI_VIRTUAL_ADDRESS     VirtualBase;
> +  UINTN                   Length;
> +  UINTN                   Attributes;
> +} MEMORY_REGION_DESCRIPTOR;
> +
> +/**
> +  Finds the length and memory properties of the memory region
> corresponding to the specified base address.
> +
> +  @param[in]  BaseAddress    To find the base address of the memory
> region.
> +  @param[in]  EndAddress     To find the end address of the memory
> region.
> +  @param[out]  RegionLength    The length of the memory region
> found.
> +  @param[out]  RegionAttributes    Properties of the memory region
> found.
> +
> +  @retval  EFI_SUCCESS    The corresponding memory area was
> successfully found
> +           EFI_NOT_FOUND    No memory area found
> +**/
> +EFI_STATUS
> +EFIAPI
> +GetMemoryRegionAttributes (
> +  IN     UINTN  BaseAddress,
> +  IN     UINTN  EndAddress,
> +  OUT    UINTN  *RegionLength,
> +  OUT    UINTN  *RegionAttributes
> +  );

2. If the actual memory ranges are as follows:
[0 - 1M, UC]
[1M - 1G, WB]

What's the result of following call:
a. GetMemoryRegionAttributes (512KB, 1MB)
b. GetMemoryRegionAttributes (512KB, 2MB)



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#112466): https://edk2.groups.io/g/devel/message/112466
Mute This Topic: https://groups.io/mt/103129095/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/leave/3901457/1787277/102458076/xyzzy [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-
Re: [edk2-devel] [PATCH v4 12/37] UefiCpuPkg: Add CpuMmuLib.h to UefiCpuPkg
Posted by Chao Li 2 years, 1 month ago
Hi Ray,

For you two comments:

BTY, can you review the other UefiCpuPkg patches in this series?

On 2023/12/13 下午1:17, Ni, Ray wrote:
> Chao,
> Thanks for providing such a cleaner lib API.
> Only 2 comments in below:
>
>> +
>> +#define EFI_MEMORY_CACHETYPE_MASK  (EFI_MEMORY_UC  | \
>> +                                    EFI_MEMORY_WC  | \
>> +                                    EFI_MEMORY_WT  | \
>> +                                    EFI_MEMORY_WB  | \
>> +                                    EFI_MEMORY_UCE   \
>> +                                    )
> 1. Why do you need to define the EFI_MEMORY_CACHETYPE_MASK here?
> Can you just mention that the following APIs only return 5 bits: UC/WC/WT/WB/UCE
> without defining the EFI_MEMORY_CACHETYPE_MASK?
Agree, I will remove this definition in V5 and if someone uses this 
macro, it will be private.
>
>> +
>> +typedef struct {
>> +  EFI_PHYSICAL_ADDRESS    PhysicalBase;
>> +  EFI_VIRTUAL_ADDRESS     VirtualBase;
>> +  UINTN                   Length;
>> +  UINTN                   Attributes;
>> +} MEMORY_REGION_DESCRIPTOR;
>> +
>> +/**
>> +  Finds the length and memory properties of the memory region
>> corresponding to the specified base address.
>> +
>> +  @param[in]  BaseAddress    To find the base address of the memory
>> region.
>> +  @param[in]  EndAddress     To find the end address of the memory
>> region.
>> +  @param[out]  RegionLength    The length of the memory region
>> found.
>> +  @param[out]  RegionAttributes    Properties of the memory region
>> found.
>> +
>> +  @retval  EFI_SUCCESS    The corresponding memory area was
>> successfully found
>> +           EFI_NOT_FOUND    No memory area found
>> +**/
>> +EFI_STATUS
>> +EFIAPI
>> +GetMemoryRegionAttributes (
>> +  IN     UINTN  BaseAddress,
>> +  IN     UINTN  EndAddress,
>> +  OUT    UINTN  *RegionLength,
>> +  OUT    UINTN  *RegionAttributes
>> +  );
> 2. If the actual memory ranges are as follows:
> [0 - 1M, UC]
> [1M - 1G, WB]
>
> What's the result of following call:
> a. GetMemoryRegionAttributes (512KB, 1MB)
> b. GetMemoryRegionAttributes (512KB, 2MB)
Yes, if the given memory region has two or more types of attributes, 
this API may return the first attribute, I should design this API again, 
thanks.
>
>
>
> 
>



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#112496): https://edk2.groups.io/g/devel/message/112496
Mute This Topic: https://groups.io/mt/103129095/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-