Re: [edk2-devel] [PATCH v8 14/37] UefiCpuPkg: Add CpuMmuLib to UefiCpuPkg

2024-03-05 Thread Chao Li

OK, I'll give it a try.


Thanks,
Chao
On 2024/3/5 20:09, Laszlo Ersek wrote:

On 3/5/24 12:50, Chao Li wrote:

Hi Laszlo,

OK, I see, let's me try. And I have another question:

Where should the low-level library be placed? Under the
UefiCpuPkg/Library and as the same folder as CpuMmuLib?

In my opinion:

Under UefiCpuPkg/Library -- yes.

In the same folder as CpuMmuLib -- no. Instances of different library
classes are never placed in the same folder.

Laszlo



Thanks,
Chao
On 2024/3/5 17:26, Laszlo Ersek wrote:

Hello Chao,

On 3/4/24 04:39, Chao Li wrote:

Hi Laszlo,

OK.

When I discussed the CpuMmuLib API as a public API with Ray in the early
days, the API recommended by Ray should be the patch 13 in this series,
which only contains set/get memory region attribute, but in the first
version in this series, it contains more API, one is ConfigureMmu API,
which is included in ARM and RISCV versions. This API provides a
interface called in PEI or DXE stage, it will configures the MMU and
enables it, such as creating the page tables, filling the static page
tables, cofniguring the MMU registers, enbale MMU, etc.

The paths for ARM and RISCV version APIs:

ARM: ArmPkg/Include/Library/ArmMmuLib.h

RISCV: UefiCpuPkg/Include/Library/BaseRiscVMmuLib.h

I have now re-read this subthread in its entirey, and I think I
understand what's up.

What confused me recently was your expression "so should we open
configure API?"

I think what you meant by "opening" was "publicly declaring".

Anyway, here's my recommendation (consistently with what I said earlier
in this thread):

- I think you need a new (lower-level) library class and instance for
exposing ConfigureMemoryManagementUnit().

Please see:  (msgid
<2a91f2f0-df4c-e106-65cd-79be16722...@redhat.com>).

- I do not recommend trying to unify the new LoongArch library classes
(low level and high level) with "ArmMmuLib.h" or "BaseRiscVMmuLib.h".

Each of "ArmMmuLib.h" and "BaseRiscVMmuLib.h" seems to declare both
low-level and high-level interfaces. For LoongArch, we apparently don't
want that; hence the idea to introduce two library classes for LoongArch
-- that may be considered an improved design.

At the same time, I don't believe that this requires us to unify
"ArmMmuLib.h" and "BaseRiscVMmuLib.h" with the new, LoongArch-motivated
library class design.

Laszlo








-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#116384): https://edk2.groups.io/g/devel/message/116384
Mute This Topic: https://groups.io/mt/103971653/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [PATCH v8 14/37] UefiCpuPkg: Add CpuMmuLib to UefiCpuPkg

2024-03-05 Thread Laszlo Ersek
On 3/5/24 12:50, Chao Li wrote:
> Hi Laszlo,
> 
> OK, I see, let's me try. And I have another question:
> 
> Where should the low-level library be placed? Under the
> UefiCpuPkg/Library and as the same folder as CpuMmuLib?

In my opinion:

Under UefiCpuPkg/Library -- yes.

In the same folder as CpuMmuLib -- no. Instances of different library
classes are never placed in the same folder.

Laszlo

> 
> 
> Thanks,
> Chao
> On 2024/3/5 17:26, Laszlo Ersek wrote:
>> Hello Chao,
>>
>> On 3/4/24 04:39, Chao Li wrote:
>>> Hi Laszlo,
>>>
>>> OK.
>>>
>>> When I discussed the CpuMmuLib API as a public API with Ray in the early
>>> days, the API recommended by Ray should be the patch 13 in this series,
>>> which only contains set/get memory region attribute, but in the first
>>> version in this series, it contains more API, one is ConfigureMmu API,
>>> which is included in ARM and RISCV versions. This API provides a
>>> interface called in PEI or DXE stage, it will configures the MMU and
>>> enables it, such as creating the page tables, filling the static page
>>> tables, cofniguring the MMU registers, enbale MMU, etc.
>>>
>>> The paths for ARM and RISCV version APIs:
>>>
>>> ARM: ArmPkg/Include/Library/ArmMmuLib.h
>>>
>>> RISCV: UefiCpuPkg/Include/Library/BaseRiscVMmuLib.h
>> I have now re-read this subthread in its entirey, and I think I
>> understand what's up.
>>
>> What confused me recently was your expression "so should we open
>> configure API?"
>>
>> I think what you meant by "opening" was "publicly declaring".
>>
>> Anyway, here's my recommendation (consistently with what I said earlier
>> in this thread):
>>
>> - I think you need a new (lower-level) library class and instance for
>> exposing ConfigureMemoryManagementUnit().
>>
>> Please see:  (msgid
>> <2a91f2f0-df4c-e106-65cd-79be16722...@redhat.com>).
>>
>> - I do not recommend trying to unify the new LoongArch library classes
>> (low level and high level) with "ArmMmuLib.h" or "BaseRiscVMmuLib.h".
>>
>> Each of "ArmMmuLib.h" and "BaseRiscVMmuLib.h" seems to declare both
>> low-level and high-level interfaces. For LoongArch, we apparently don't
>> want that; hence the idea to introduce two library classes for LoongArch
>> -- that may be considered an improved design.
>>
>> At the same time, I don't believe that this requires us to unify
>> "ArmMmuLib.h" and "BaseRiscVMmuLib.h" with the new, LoongArch-motivated
>> library class design.
>>
>> Laszlo



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#116383): https://edk2.groups.io/g/devel/message/116383
Mute This Topic: https://groups.io/mt/103971653/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [PATCH v8 14/37] UefiCpuPkg: Add CpuMmuLib to UefiCpuPkg

2024-03-05 Thread Chao Li

Hi Laszlo,

OK, I see, let's me try. And I have another question:

Where should the low-level library be placed? Under the 
UefiCpuPkg/Library and as the same folder as CpuMmuLib?



Thanks,
Chao
On 2024/3/5 17:26, Laszlo Ersek wrote:

Hello Chao,

On 3/4/24 04:39, Chao Li wrote:

Hi Laszlo,

OK.

When I discussed the CpuMmuLib API as a public API with Ray in the early
days, the API recommended by Ray should be the patch 13 in this series,
which only contains set/get memory region attribute, but in the first
version in this series, it contains more API, one is ConfigureMmu API,
which is included in ARM and RISCV versions. This API provides a
interface called in PEI or DXE stage, it will configures the MMU and
enables it, such as creating the page tables, filling the static page
tables, cofniguring the MMU registers, enbale MMU, etc.

The paths for ARM and RISCV version APIs:

ARM: ArmPkg/Include/Library/ArmMmuLib.h

RISCV: UefiCpuPkg/Include/Library/BaseRiscVMmuLib.h

I have now re-read this subthread in its entirey, and I think I
understand what's up.

What confused me recently was your expression "so should we open
configure API?"

I think what you meant by "opening" was "publicly declaring".

Anyway, here's my recommendation (consistently with what I said earlier
in this thread):

- I think you need a new (lower-level) library class and instance for
exposing ConfigureMemoryManagementUnit().

Please see:  (msgid
<2a91f2f0-df4c-e106-65cd-79be16722...@redhat.com>).

- I do not recommend trying to unify the new LoongArch library classes
(low level and high level) with "ArmMmuLib.h" or "BaseRiscVMmuLib.h".

Each of "ArmMmuLib.h" and "BaseRiscVMmuLib.h" seems to declare both
low-level and high-level interfaces. For LoongArch, we apparently don't
want that; hence the idea to introduce two library classes for LoongArch
-- that may be considered an improved design.

At the same time, I don't believe that this requires us to unify
"ArmMmuLib.h" and "BaseRiscVMmuLib.h" with the new, LoongArch-motivated
library class design.

Laszlo



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#116377): https://edk2.groups.io/g/devel/message/116377
Mute This Topic: https://groups.io/mt/103971653/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [PATCH v8 14/37] UefiCpuPkg: Add CpuMmuLib to UefiCpuPkg

2024-03-05 Thread Laszlo Ersek
Hello Chao,

On 3/4/24 04:39, Chao Li wrote:
> Hi Laszlo,
> 
> OK.
> 
> When I discussed the CpuMmuLib API as a public API with Ray in the early
> days, the API recommended by Ray should be the patch 13 in this series,
> which only contains set/get memory region attribute, but in the first
> version in this series, it contains more API, one is ConfigureMmu API,
> which is included in ARM and RISCV versions. This API provides a
> interface called in PEI or DXE stage, it will configures the MMU and
> enables it, such as creating the page tables, filling the static page
> tables, cofniguring the MMU registers, enbale MMU, etc.
> 
> The paths for ARM and RISCV version APIs:
> 
> ARM: ArmPkg/Include/Library/ArmMmuLib.h
> 
> RISCV: UefiCpuPkg/Include/Library/BaseRiscVMmuLib.h

I have now re-read this subthread in its entirey, and I think I
understand what's up.

What confused me recently was your expression "so should we open
configure API?"

I think what you meant by "opening" was "publicly declaring".

Anyway, here's my recommendation (consistently with what I said earlier
in this thread):

- I think you need a new (lower-level) library class and instance for
exposing ConfigureMemoryManagementUnit().

Please see:  (msgid
<2a91f2f0-df4c-e106-65cd-79be16722...@redhat.com>).

- I do not recommend trying to unify the new LoongArch library classes
(low level and high level) with "ArmMmuLib.h" or "BaseRiscVMmuLib.h".

Each of "ArmMmuLib.h" and "BaseRiscVMmuLib.h" seems to declare both
low-level and high-level interfaces. For LoongArch, we apparently don't
want that; hence the idea to introduce two library classes for LoongArch
-- that may be considered an improved design.

At the same time, I don't believe that this requires us to unify
"ArmMmuLib.h" and "BaseRiscVMmuLib.h" with the new, LoongArch-motivated
library class design.

Laszlo



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#116357): https://edk2.groups.io/g/devel/message/116357
Mute This Topic: https://groups.io/mt/103971653/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [PATCH v8 14/37] UefiCpuPkg: Add CpuMmuLib to UefiCpuPkg

2024-03-03 Thread Chao Li

Hi Laszlo,

OK.

When I discussed the CpuMmuLib API as a public API with Ray in the early 
days, the API recommended by Ray should be the patch 13 in this series, 
which only contains set/get memory region attribute, but in the first 
version in this series, it contains more API, one is ConfigureMmu API, 
which is included in ARM and RISCV versions. This API provides a 
interface called in PEI or DXE stage, it will configures the MMU and 
enables it, such as creating the page tables, filling the static page 
tables, cofniguring the MMU registers, enbale MMU, etc.


The paths for ARM and RISCV version APIs:

ARM: ArmPkg/Include/Library/ArmMmuLib.h

RISCV: UefiCpuPkg/Include/Library/BaseRiscVMmuLib.h


Thanks,
Chao
On 2024/3/1 19:27, Laszlo Ersek wrote:

On 3/1/24 02:26, Chao Li wrote:

Hi Ray, Lazslo,

This library is almost complete to refactored, it refer to ARM and
RISC-V version, the API include set/get memory region attribute.

I have one last question, in ARM and RISC-V version, even LoongArch old
and current version, they all request a configure interface, which may
be called in PEI or DXE stage, so should we open configure API? If so,
it is possible for ARM RISC-V and LongArch's MMU libraries to be meged
into the same instance if they wished.

Sorry, I've lost all context by now. I don't understand what you mean by
"they all request a configure interface".

Please provide pathnames and function names for the existent code that
you are referring to as examples (and then please restate / rephrase
your question as well).

Laszlo



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#116303): https://edk2.groups.io/g/devel/message/116303
Mute This Topic: https://groups.io/mt/103971653/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [PATCH v8 14/37] UefiCpuPkg: Add CpuMmuLib to UefiCpuPkg

2024-03-01 Thread Laszlo Ersek
On 3/1/24 02:26, Chao Li wrote:
> Hi Ray, Lazslo,
> 
> This library is almost complete to refactored, it refer to ARM and
> RISC-V version, the API include set/get memory region attribute.
> 
> I have one last question, in ARM and RISC-V version, even LoongArch old
> and current version, they all request a configure interface, which may
> be called in PEI or DXE stage, so should we open configure API? If so,
> it is possible for ARM RISC-V and LongArch's MMU libraries to be meged
> into the same instance if they wished.

Sorry, I've lost all context by now. I don't understand what you mean by
"they all request a configure interface".

Please provide pathnames and function names for the existent code that
you are referring to as examples (and then please restate / rephrase
your question as well).

Laszlo



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#116233): https://edk2.groups.io/g/devel/message/116233
Mute This Topic: https://groups.io/mt/103971653/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH v8 14/37] UefiCpuPkg: Add CpuMmuLib to UefiCpuPkg

2024-01-25 Thread Chao Li
Add a new library named CpuMmuLib and add a LoongArch64 instance with in
the library.
It provides two-stage MMU libraryinstances, PEI and DXE.

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

Cc: Eric Dong 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Signed-off-by: Chao Li 
Co-authored-by: Baoqi Zhang 
Co-authored-by: Dongyan Qian 
Co-authored-by: Xianglai Li 
Co-authored-by: Bibo Mao 
---
 UefiCpuPkg/Library/CpuMmuLib/DxeCpuMmuLib.inf |  36 +
 UefiCpuPkg/Library/CpuMmuLib/DxeCpuMmuLib.uni |  14 +
 .../CpuMmuLib/LoongArch64/CommonMmuLib.c  | 988 ++
 .../CpuMmuLib/LoongArch64/CommonMmuLib.h  |  43 +
 .../Library/CpuMmuLib/LoongArch64/Page.h  | 279 +
 .../CpuMmuLib/LoongArch64/PeiCpuMmuLib.c  | 178 
 .../Library/CpuMmuLib/LoongArch64/Tlb.h   |  48 +
 .../CpuMmuLib/LoongArch64/TlbOperation.S  |  44 +
 UefiCpuPkg/Library/CpuMmuLib/PeiCpuMmuLib.inf |  44 +
 UefiCpuPkg/Library/CpuMmuLib/PeiCpuMmuLib.uni |  14 +
 UefiCpuPkg/UefiCpuPkg.dsc |   4 +
 11 files changed, 1692 insertions(+)
 create mode 100644 UefiCpuPkg/Library/CpuMmuLib/DxeCpuMmuLib.inf
 create mode 100644 UefiCpuPkg/Library/CpuMmuLib/DxeCpuMmuLib.uni
 create mode 100644 UefiCpuPkg/Library/CpuMmuLib/LoongArch64/CommonMmuLib.c
 create mode 100644 UefiCpuPkg/Library/CpuMmuLib/LoongArch64/CommonMmuLib.h
 create mode 100644 UefiCpuPkg/Library/CpuMmuLib/LoongArch64/Page.h
 create mode 100644 UefiCpuPkg/Library/CpuMmuLib/LoongArch64/PeiCpuMmuLib.c
 create mode 100644 UefiCpuPkg/Library/CpuMmuLib/LoongArch64/Tlb.h
 create mode 100644 UefiCpuPkg/Library/CpuMmuLib/LoongArch64/TlbOperation.S
 create mode 100644 UefiCpuPkg/Library/CpuMmuLib/PeiCpuMmuLib.inf
 create mode 100644 UefiCpuPkg/Library/CpuMmuLib/PeiCpuMmuLib.uni

diff --git a/UefiCpuPkg/Library/CpuMmuLib/DxeCpuMmuLib.inf 
b/UefiCpuPkg/Library/CpuMmuLib/DxeCpuMmuLib.inf
new file mode 100644
index 00..bfce3ce96d
--- /dev/null
+++ b/UefiCpuPkg/Library/CpuMmuLib/DxeCpuMmuLib.inf
@@ -0,0 +1,36 @@
+## @file
+#  CPU Memory Map Unit DXE phase driver.
+#
+#  Copyright (c) 2024 Loongson Technology Corporation Limited. All rights 
reserved.
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION= 1.29
+  BASE_NAME  = DxeCpuMmuLib
+  MODULE_UNI_FILE= DxeCpuMmuLib.uni
+  FILE_GUID  = DA8F0232-FB14-42F0-922C-63104D2C70BE
+  MODULE_TYPE= DXE_DRIVER
+  VERSION_STRING = 1.0
+  LIBRARY_CLASS  = CpuMmuLib | DXE_DRIVER
+  CONSTRUCTOR= MmuInitialize
+
+#
+#  VALID_ARCHITECTURES   = LOONGARCH64
+#
+
+[Sources.LoongArch64]
+  LoongArch64/TlbOperation.S   | GCC
+  LoongArch64/CommonMmuLib.c
+  LoongArch64/Page.h
+  LoongArch64/Tlb.h
+
+[Packages]
+  MdePkg/MdePkg.dec
+  UefiCpuPkg/UefiCpuPkg.dec
+
+[LibraryClasses]
+  DebugLib
+  MemoryAllocationLib
diff --git a/UefiCpuPkg/Library/CpuMmuLib/DxeCpuMmuLib.uni 
b/UefiCpuPkg/Library/CpuMmuLib/DxeCpuMmuLib.uni
new file mode 100644
index 00..7342249516
--- /dev/null
+++ b/UefiCpuPkg/Library/CpuMmuLib/DxeCpuMmuLib.uni
@@ -0,0 +1,14 @@
+// /** @file
+// CPU Memory Manager Unit library instance for DXE modules.
+//
+// CPU Memory Manager Unit library instance for DXE modules.
+//
+// Copyright (c) 2024, Loongson Technology Corporation Limited. All rights 
reserved.
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+// **/
+
+#string STR_MODULE_ABSTRACT #language en-US "CPU Memory Manager 
Unit library instance for DXE modules."
+
+#string STR_MODULE_DESCRIPTION  #language en-US "CPU Memory Manager 
Unit library instance for DXE modules."
diff --git a/UefiCpuPkg/Library/CpuMmuLib/LoongArch64/CommonMmuLib.c 
b/UefiCpuPkg/Library/CpuMmuLib/LoongArch64/CommonMmuLib.c
new file mode 100644
index 00..2e852c3371
--- /dev/null
+++ b/UefiCpuPkg/Library/CpuMmuLib/LoongArch64/CommonMmuLib.c
@@ -0,0 +1,988 @@
+/** @file
+
+  CPU Memory Map Unit Handler Library common functions.
+
+  Copyright (c) 2024 Loongson Technology Corporation Limited. All rights 
reserved.
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+  @par Glossary:
+- Pgd or Pgd or PGD- Page Global Directory
+- Pud or Pud or PUD- Page Upper Directory
+- Pmd or Pmd or PMD- Page Middle Directory
+- Pte or pte or PTE- Page Table Entry
+- Val or VAL or val- Value
+- Dir- Directory
+**/
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "Tlb.h"
+#include "Page.h"
+
+#define SWAP_PAGE_DIR  CsrRead(LOONGARCH_CSR_PGDL)
+#define EFI_MEMORY_CACHETYPE_MASK  (EFI_MEMORY_UC  | \
+EFI_MEMORY_WC  | \
+EFI_MEMORY_WT  | \
+EFI_MEMORY_WB  | \
+EFI_MEMORY_UCE   \
+