public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [edk2-platforms][PATCH V2 0/7] loongarch add flash device.
@ 2023-01-10  1:16 xianglai
  2023-01-10  1:16 ` [edk2-platforms][PATCH V2 1/7] Platform/Loongson: add bootmode support xianglai
                   ` (6 more replies)
  0 siblings, 7 replies; 10+ messages in thread
From: xianglai @ 2023-01-10  1:16 UTC (permalink / raw)
  To: devel
  Cc: Ard Biesheuvel, Bibo Mao, Chao Li, Leif Lindholm, Liming Gao,
	Michael D Kinney

This is a patch series that contains:
1.loongarch add flash device driver.
2.loongarch add nvme device driver.
3.Optimize the mmu lib of loongarch.
4.Enable zero address protection.

v2 changes:
- Split the MMU lib-related patches further.
- Remove the for loop of flash information lookup
  in VirtNorFlashPlatformLib Because the Loongarch
  virtual machine only emulates one flash .
- Add the necessary comments in VirtNorFlashPlatformLib.
- Remove the hard coding associated with the Flash base address.
- Optimize the process of converting huge pages to
   page table entries in MMU lib.

Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Bibo Mao <maobibo@loongson.cn>
Cc: Chao Li <lichao@loongson.cn>
Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Michael D Kinney <michael.d.kinney@intel.com>

Bibo Mao (2):
  Platform/Loongson: add bootmode support.
  Platform/Loongson:add nvme device driver for loongarch.

xianglai li (5):
  Platform/Loongson: Support pflash for loongarch.
  Platform/Loongson: Modify the page table entry access priority.
  Platform/Loongson: Optimize page table entry null determination.
  Platform/Loongson: Optimize the huge page and page entry conversion.
  Platform/Loongson: Enable zero address protection.

 .../Library/MmuLib/MmuBaseLibPei.inf          |   2 +
 .../Library/MmuLib/MmuLibCore.c               | 119 +++++++++++----
 .../Library/MmuLib/MmuLibCorePei.c            |   5 +
 .../LoongArchQemuPkg/Library/MmuLib/page.h    |   5 +-
 .../Library/NorFlashQemuLib/NorFlashQemuLib.c | 141 ++++++++++++++++++
 .../NorFlashQemuLib/NorFlashQemuLib.inf       |  43 ++++++
 .../Loongson/LoongArchQemuPkg/Loongson.dsc    |  22 ++-
 .../Loongson/LoongArchQemuPkg/Loongson.fdf    |   8 +-
 .../LoongArchQemuPkg/PlatformPei/Platform.c   |   5 +
 .../LoongArchQemuPkg/VarStore.fdf.inc         |  67 +++++++++
 10 files changed, 375 insertions(+), 42 deletions(-)
 create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c
 create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf
 create mode 100644 Platform/Loongson/LoongArchQemuPkg/VarStore.fdf.inc

-- 
2.31.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [edk2-platforms][PATCH V2 1/7] Platform/Loongson: add bootmode support.
  2023-01-10  1:16 [edk2-platforms][PATCH V2 0/7] loongarch add flash device xianglai
@ 2023-01-10  1:16 ` xianglai
  2023-01-10  1:16 ` [edk2-platforms][PATCH V2 2/7] Platform/Loongson:add nvme device driver for loongarch xianglai
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: xianglai @ 2023-01-10  1:16 UTC (permalink / raw)
  To: devel
  Cc: Bibo Mao, Ard Biesheuvel, Chao Li, Leif Lindholm, Liming Gao,
	Michael D Kinney

From: Bibo Mao <maobibo@loongson.cn>

add bootmode support in PEI phase.

Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Bibo Mao <maobibo@loongson.cn>
Cc: Chao Li <lichao@loongson.cn>
Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Signed-off-by: Bibo Mao <maobibo@loongson.cn>
Signed-off-by: xianglai li <lixianglai@loongson.cn>
---
 Platform/Loongson/LoongArchQemuPkg/PlatformPei/Platform.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/Platform/Loongson/LoongArchQemuPkg/PlatformPei/Platform.c b/Platform/Loongson/LoongArchQemuPkg/PlatformPei/Platform.c
index 32b6518f8f..84bb8e8a6d 100644
--- a/Platform/Loongson/LoongArchQemuPkg/PlatformPei/Platform.c
+++ b/Platform/Loongson/LoongArchQemuPkg/PlatformPei/Platform.c
@@ -53,6 +53,8 @@ CONST EFI_PEI_PPI_DESCRIPTOR  mPpiListBootMode = {
   NULL
 };
 
+STATIC EFI_BOOT_MODE  mBootMode = BOOT_WITH_FULL_CONFIGURATION;
+
 /**
   Create Reserved type memory range hand off block.
 
@@ -417,6 +419,9 @@ InitializePlatform (
 
   DEBUG ((DEBUG_INFO, "Platform PEIM Loaded\n"));
 
+  Status = PeiServicesSetBootMode (mBootMode);
+  ASSERT_EFI_ERROR (Status);
+
   Status = PeiServicesInstallPpi (&mPpiListBootMode);
   ASSERT_EFI_ERROR (Status);
 
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [edk2-platforms][PATCH V2 2/7] Platform/Loongson:add nvme device driver for loongarch.
  2023-01-10  1:16 [edk2-platforms][PATCH V2 0/7] loongarch add flash device xianglai
  2023-01-10  1:16 ` [edk2-platforms][PATCH V2 1/7] Platform/Loongson: add bootmode support xianglai
@ 2023-01-10  1:16 ` xianglai
  2023-01-10  1:16 ` [edk2-platforms][PATCH V2 3/7] Platform/Loongson: Support pflash " xianglai
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: xianglai @ 2023-01-10  1:16 UTC (permalink / raw)
  To: devel
  Cc: Bibo Mao, Ard Biesheuvel, Chao Li, Leif Lindholm, Liming Gao,
	Michael D Kinney

From: Bibo Mao <maobibo@loongson.cn>

add nvme device driver support loongarch qemu virt machine.

Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Bibo Mao <maobibo@loongson.cn>
Cc: Chao Li <lichao@loongson.cn>
Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Signed-off-by: Bibo Mao <maobibo@loongson.cn>
Signed-off-by: xianglai li <lixianglai@loongson.cn>
---
 Platform/Loongson/LoongArchQemuPkg/Loongson.dsc | 5 +++++
 Platform/Loongson/LoongArchQemuPkg/Loongson.fdf | 5 +++++
 2 files changed, 10 insertions(+)

diff --git a/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc b/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
index 2d63f7d168..a9b5c8c514 100644
--- a/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
+++ b/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
@@ -550,6 +550,11 @@
   MdeModulePkg/Bus/Scsi/ScsiBusDxe/ScsiBusDxe.inf
   MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDiskDxe.inf
 
+  #
+  # NVME Driver
+  #
+  MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressDxe.inf
+
   #
   # SMBIOS Support
   #
diff --git a/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf b/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf
index 784f255910..ee89097344 100644
--- a/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf
+++ b/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf
@@ -180,6 +180,11 @@ INF  OvmfPkg/SataControllerDxe/SataControllerDxe.inf
 INF  MdeModulePkg/Bus/Scsi/ScsiBusDxe/ScsiBusDxe.inf
 INF  MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDiskDxe.inf
 
+#
+# NVME
+#
+INF MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressDxe.inf
+
 #
 # Usb Support
 #
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [edk2-platforms][PATCH V2 3/7] Platform/Loongson: Support pflash for loongarch.
  2023-01-10  1:16 [edk2-platforms][PATCH V2 0/7] loongarch add flash device xianglai
  2023-01-10  1:16 ` [edk2-platforms][PATCH V2 1/7] Platform/Loongson: add bootmode support xianglai
  2023-01-10  1:16 ` [edk2-platforms][PATCH V2 2/7] Platform/Loongson:add nvme device driver for loongarch xianglai
@ 2023-01-10  1:16 ` xianglai
  2023-01-10  7:12   ` [edk2-devel] " Chao Li
  2023-01-10  1:16 ` [edk2-platforms][PATCH V2 4/7] Platform/Loongson: Modify the page table entry access priority xianglai
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 10+ messages in thread
From: xianglai @ 2023-01-10  1:16 UTC (permalink / raw)
  To: devel
  Cc: Ard Biesheuvel, Bibo Mao, Chao Li, Leif Lindholm, Liming Gao,
	Michael D Kinney

Add pflash driver for loongarch.

Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Bibo Mao <maobibo@loongson.cn>
Cc: Chao Li <lichao@loongson.cn>
Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Signed-off-by: xianglai li <lixianglai@loongson.cn>
---
 .../Library/NorFlashQemuLib/NorFlashQemuLib.c | 141 ++++++++++++++++++
 .../NorFlashQemuLib/NorFlashQemuLib.inf       |  43 ++++++
 .../Loongson/LoongArchQemuPkg/Loongson.dsc    |  15 +-
 .../Loongson/LoongArchQemuPkg/Loongson.fdf    |   3 +-
 .../LoongArchQemuPkg/VarStore.fdf.inc         |  67 +++++++++
 5 files changed, 260 insertions(+), 9 deletions(-)
 create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c
 create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf
 create mode 100644 Platform/Loongson/LoongArchQemuPkg/VarStore.fdf.inc

diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c b/Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c
new file mode 100644
index 0000000000..2e0bf3cef0
--- /dev/null
+++ b/Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c
@@ -0,0 +1,141 @@
+/** @file
+
+  Copyright (c) 2023 Loongson Technology Corporation Limited. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+
+#include <Library/BaseLib.h>
+#include <Library/DebugLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/VirtNorFlashPlatformLib.h>
+
+#include <Protocol/FdtClient.h>
+
+#define QEMU_NOR_BLOCK_SIZE  SIZE_128KB
+
+EFI_STATUS
+VirtNorFlashPlatformInitialization (
+  VOID
+  )
+{
+  return EFI_SUCCESS;
+}
+
+STATIC VIRT_NOR_FLASH_DESCRIPTION  mNorFlashDevices;
+
+EFI_STATUS
+VirtNorFlashPlatformGetDevices (
+  OUT VIRT_NOR_FLASH_DESCRIPTION  **NorFlashDescriptions,
+  OUT UINT32                      *Count
+  )
+{
+  FDT_CLIENT_PROTOCOL  *FdtClient;
+  INT32                Node;
+  EFI_STATUS           Status;
+  EFI_STATUS           FindNodeStatus;
+  CONST UINT32         *Reg;
+  UINT32               PropSize;
+  UINT64               Base;
+  UINT64               Size;
+
+  Status = gBS->LocateProtocol (
+                  &gFdtClientProtocolGuid,
+                  NULL,
+                  (VOID **)&FdtClient
+                  );
+  ASSERT_EFI_ERROR (Status);
+
+  FindNodeStatus = FdtClient->FindCompatibleNode (
+                                     FdtClient,
+                                     "cfi-flash",
+                                     &Node
+                                     );
+  ASSERT_EFI_ERROR (FindNodeStatus);
+
+  Status = FdtClient->GetNodeProperty (
+                        FdtClient,
+                        Node,
+                        "reg",
+                        (CONST VOID **)&Reg,
+                        &PropSize
+                        );
+  if (EFI_ERROR (Status)) {
+    DEBUG ((
+      DEBUG_ERROR,
+      "%a: GetNodeProperty () failed (Status == %r)\n",
+      __FUNCTION__,
+      Status
+      ));
+    return Status;
+  }
+
+  ASSERT ((PropSize % (4 * sizeof (UINT32))) == 0);
+
+  if (PropSize < (4 * sizeof (UINT32))) {
+    DEBUG ((
+      DEBUG_ERROR,
+      "%a: reg node size(%d) is too small \n",
+      __FUNCTION__,
+      PropSize
+      ));
+    return EFI_NOT_FOUND;
+  }
+
+  Base = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[0]));
+  Size = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[2]));
+
+  mNorFlashDevices.DeviceBaseAddress = (UINTN)Base;
+  mNorFlashDevices.RegionBaseAddress = (UINTN)Base;
+  mNorFlashDevices.Size              = (UINTN)Size;
+  mNorFlashDevices.BlockSize         = QEMU_NOR_BLOCK_SIZE;
+
+  Status = PcdSet32S (PcdFlashNvStorageVariableBase, Base);
+  ASSERT_EFI_ERROR (Status);
+
+  /*
+   * Base is the value of PcdFlashNvStorageVariableBase,
+   * PcdFlashNvStorageFtwWorkingBase can be got by
+   *   PcdFlashNvStorageVariableBase + PcdFlashNvStorageVariableSize
+   */
+  Base += PcdGet32 (PcdFlashNvStorageVariableSize);
+  Status = PcdSet32S (PcdFlashNvStorageFtwWorkingBase, Base);
+  ASSERT_EFI_ERROR (Status);
+
+  /*
+   * Now,Base is the value of PcdFlashNvStorageFtwWorkingBase,
+   * PcdFlashNvStorageFtwSpareBase can be got by
+   *   PcdFlashNvStorageFtwWorkingBase + PcdFlashNvStorageFtwWorkingSize.
+   */
+  Base += PcdGet32 (PcdFlashNvStorageFtwWorkingSize);
+  Status = PcdSet32S (PcdFlashNvStorageFtwSpareBase, Base);
+  ASSERT_EFI_ERROR (Status);
+
+  //
+  // UEFI takes ownership of the NOR flash, and exposes its functionality
+  // through the UEFI Runtime Services GetVariable, SetVariable, etc. This
+  // means we need to disable it in the device tree to prevent the OS from
+  // attaching its device driver as well.
+  // Note that this also hides other flash banks, but the only other flash
+  // bank we expect to encounter is the one that carries the UEFI executable
+  // code, which is not intended to be guest updatable, and is usually backed
+  // in a readonly manner by QEMU anyway.
+  //
+  Status = FdtClient->SetNodeProperty (
+                        FdtClient,
+                        Node,
+                        "status",
+                        "disabled",
+                        sizeof ("disabled")
+                        );
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_WARN, "Failed to set NOR flash status to 'disabled'\n"));
+  }
+
+  *NorFlashDescriptions = &mNorFlashDevices;
+  *Count                = 1;
+
+  return EFI_SUCCESS;
+}
diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf b/Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf
new file mode 100644
index 0000000000..da05ca0898
--- /dev/null
+++ b/Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf
@@ -0,0 +1,43 @@
+## @file
+#
+#  Copyright (c) 2023 Loongson Technology Corporation Limited. All rights reserved.<BR>
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = NorFlashQemuLib
+  FILE_GUID                      = 339B7829-4C5F-4EFC-B2DD-5050E530DECE
+  MODULE_TYPE                    = DXE_DRIVER
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = VirtNorFlashPlatformLib
+
+[Sources.common]
+  NorFlashQemuLib.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  EmbeddedPkg/EmbeddedPkg.dec
+  OvmfPkg/OvmfPkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+
+[LibraryClasses]
+  BaseLib
+  DebugLib
+  UefiBootServicesTableLib
+
+[Protocols]
+  gFdtClientProtocolGuid          ## CONSUMES
+
+[Depex]
+  gFdtClientProtocolGuid
+
+[Pcd]
+gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize
+gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize
+gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize
+gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase
+gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase
+gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase
diff --git a/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc b/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
index a9b5c8c514..05913db144 100644
--- a/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
+++ b/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
@@ -175,6 +175,7 @@
   DebugLib                         | MdePkg/Library/BaseDebugLibSerialPort/BaseDebugLibSerialPort.inf
   PeiServicesLib                   | MdePkg/Library/PeiServicesLib/PeiServicesLib.inf
   VariableFlashInfoLib             | MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.inf
+  VirtNorFlashPlatformLib          | Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf
 
 [LibraryClasses.common.SEC]
   PcdLib                           | MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf
@@ -360,9 +361,9 @@
   #
 !include NetworkPkg/NetworkPcds.dsc.inc
 
-  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize         | 0x10000
-  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize         | 0x20000
-  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize       | 0x10000
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize         | 0x40000
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize         | 0x40000
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize       | 0x40000
 
 ################################################################################
 #
@@ -373,6 +374,7 @@
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase         | 0
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64       | 0
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64       | 0
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase         | 0
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase       | 0
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64     | 0
   gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvStoreReserved         | 0
@@ -471,15 +473,12 @@
   #
   # Variable
   #
-
-  OvmfPkg/EmuVariableFvbRuntimeDxe/Fvb.inf {
-    <LibraryClasses>
-      PlatformFvbLib|OvmfPkg/Library/EmuVariableFvbLib/EmuVariableFvbLib.inf
-  }
+  OvmfPkg/VirtNorFlashDxe/VirtNorFlashDxe.inf
   MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf
   MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf {
     <LibraryClasses>
       NULL|MdeModulePkg/Library/VarCheckUefiLib/VarCheckUefiLib.inf
+      NULL|EmbeddedPkg/Library/NvVarStoreFormattedLib/NvVarStoreFormattedLib.inf
       BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
   }
 
diff --git a/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf b/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf
index ee89097344..675d86094c 100644
--- a/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf
+++ b/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf
@@ -30,6 +30,7 @@ $(DXEFV_OFFSET)|$(DXEFV_SIZE)
 gLoongArchQemuPkgTokenSpaceGuid.PcdFlashDxeFvBase|gLoongArchQemuPkgTokenSpaceGuid.PcdFlashDxeFvSize
 FV = FVMAIN_COMPACT
 
+!include VarStore.fdf.inc
 #####################################################################################################
 [FV.SECFV]
 FvNameGuid         = 587d4265-5e71-41da-9c35-4258551f1e22
@@ -135,7 +136,7 @@ INF  MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleRuntimeDxe.inf
 #
 # Variable
 #
-INF  OvmfPkg/EmuVariableFvbRuntimeDxe/Fvb.inf
+INF  OvmfPkg/VirtNorFlashDxe/VirtNorFlashDxe.inf
 INF  MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf
 INF  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
 #
diff --git a/Platform/Loongson/LoongArchQemuPkg/VarStore.fdf.inc b/Platform/Loongson/LoongArchQemuPkg/VarStore.fdf.inc
new file mode 100644
index 0000000000..83ce3d8008
--- /dev/null
+++ b/Platform/Loongson/LoongArchQemuPkg/VarStore.fdf.inc
@@ -0,0 +1,67 @@
+## @file
+#
+#  Copyright (c) 2023 Loongson Technology Corporation Limited. All rights reserved.<BR>
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[FD.QEMU_VARS]
+BaseAddress   = 0x0
+Size          = 0x1000000
+ErasePolarity = 1
+BlockSize     = 0x20000
+NumBlocks     = 128
+
+0x00000000|0x00040000
+#NV_VARIABLE_STORE
+DATA = {
+  ## This is the EFI_FIRMWARE_VOLUME_HEADER
+  # ZeroVector []
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  # FileSystemGuid: gEfiSystemNvDataFvGuid         =
+  #   { 0xFFF12B8D, 0x7696, 0x4C8B,
+  #     { 0xA9, 0x85, 0x27, 0x47, 0x07, 0x5B, 0x4F, 0x50 }}
+  0x8D, 0x2B, 0xF1, 0xFF, 0x96, 0x76, 0x8B, 0x4C,
+  0xA9, 0x85, 0x27, 0x47, 0x07, 0x5B, 0x4F, 0x50,
+  # FvLength: 0xC0000
+  0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00,
+  # Signature "_FVH"       # Attributes
+  0x5f, 0x46, 0x56, 0x48, 0xff, 0xfe, 0x04, 0x00,
+  # HeaderLength # CheckSum # ExtHeaderOffset #Reserved #Revision
+  0x48, 0x00, 0x28, 0x09, 0x00, 0x00, 0x00, 0x02,
+  # Blockmap[0]: 0x3 Blocks * 0x40000 Bytes / Block
+  0x3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
+  # Blockmap[1]: End
+  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+  ## This is the VARIABLE_STORE_HEADER
+  # It is compatible with SECURE_BOOT_ENABLE == FALSE as well.
+  # Signature: gEfiAuthenticatedVariableGuid =
+  #   { 0xaaf32c78, 0x947b, 0x439a,
+  #     { 0xa1, 0x80, 0x2e, 0x14, 0x4e, 0xc3, 0x77, 0x92 }}
+  0x78, 0x2c, 0xf3, 0xaa, 0x7b, 0x94, 0x9a, 0x43,
+  0xa1, 0x80, 0x2e, 0x14, 0x4e, 0xc3, 0x77, 0x92,
+  # Size: 0x40000 (gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize) -
+  #         0x48 (size of EFI_FIRMWARE_VOLUME_HEADER) = 0x3ffb8
+  # This can speed up the Variable Dispatch a bit.
+  0xB8, 0xFF, 0x03, 0x00,
+  # FORMATTED: 0x5A #HEALTHY: 0xFE #Reserved: UINT16 #Reserved1: UINT32
+  0x5A, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+}
+
+0x00040000|0x00040000
+#NV_FTW_WORKING
+DATA = {
+  # EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER->Signature = gEdkiiWorkingBlockSignatureGuid         =
+  #  { 0x9e58292b, 0x7c68, 0x497d, { 0xa0, 0xce, 0x65,  0x0, 0xfd, 0x9f, 0x1b, 0x95 }}
+  0x2b, 0x29, 0x58, 0x9e, 0x68, 0x7c, 0x7d, 0x49,
+  0xa0, 0xce, 0x65,  0x0, 0xfd, 0x9f, 0x1b, 0x95,
+  # Crc:UINT32            #WorkingBlockValid:1, WorkingBlockInvalid:1, Reserved
+  0x5b, 0xe7, 0xc6, 0x86, 0xFE, 0xFF, 0xFF, 0xFF,
+  # WriteQueueSize: UINT64
+  0xE0, 0xFF, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00
+}
+
+0x00080000|0x00040000
+#NV_FTW_SPARE
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [edk2-platforms][PATCH V2 4/7] Platform/Loongson: Modify the page table entry access priority.
  2023-01-10  1:16 [edk2-platforms][PATCH V2 0/7] loongarch add flash device xianglai
                   ` (2 preceding siblings ...)
  2023-01-10  1:16 ` [edk2-platforms][PATCH V2 3/7] Platform/Loongson: Support pflash " xianglai
@ 2023-01-10  1:16 ` xianglai
  2023-01-10  1:16 ` [edk2-platforms][PATCH V2 5/7] Platform/Loongson: Optimize page table entry null determination xianglai
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 10+ messages in thread
From: xianglai @ 2023-01-10  1:16 UTC (permalink / raw)
  To: devel
  Cc: Ard Biesheuvel, Bibo Mao, Chao Li, Leif Lindholm, Liming Gao,
	Michael D Kinney

Modify the page table entry access priority
 When the Page table properties are set.

Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Bibo Mao <maobibo@loongson.cn>
Cc: Chao Li <lichao@loongson.cn>
Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Signed-off-by: xianglai li <lixianglai@loongson.cn>
---
 Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c
index b932e3d568..9e2bd3344a 100644
--- a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c
+++ b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c
@@ -671,7 +671,7 @@ EfiAttributeToLoongArchAttribute (
   IN UINTN  EfiAttributes
   )
 {
-  UINTN  LoongArchAttributes = PAGE_VALID | PAGE_DIRTY | CACHE_CC | PAGE_USER | PAGE_GLOBAL;
+  UINTN  LoongArchAttributes = PAGE_VALID | PAGE_DIRTY | PLV_KERNEL | PAGE_GLOBAL;
   switch (EfiAttributes & EFI_MEMORY_CACHETYPE_MASK) {
     case EFI_MEMORY_UC:
       LoongArchAttributes |= CACHE_SUC;
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [edk2-platforms][PATCH V2 5/7] Platform/Loongson: Optimize page table entry null determination.
  2023-01-10  1:16 [edk2-platforms][PATCH V2 0/7] loongarch add flash device xianglai
                   ` (3 preceding siblings ...)
  2023-01-10  1:16 ` [edk2-platforms][PATCH V2 4/7] Platform/Loongson: Modify the page table entry access priority xianglai
@ 2023-01-10  1:16 ` xianglai
  2023-01-10  1:16 ` [edk2-platforms][PATCH V2 6/7] Platform/Loongson: Optimize the huge page and page entry conversion xianglai
  2023-01-10  1:16 ` [edk2-platforms][PATCH V2 7/7] Platform/Loongson: Enable zero address protection xianglai
  6 siblings, 0 replies; 10+ messages in thread
From: xianglai @ 2023-01-10  1:16 UTC (permalink / raw)
  To: devel
  Cc: Ard Biesheuvel, Bibo Mao, Chao Li, Leif Lindholm, Liming Gao,
	Michael D Kinney

Modify the null judgment condition of page table entries.

Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Bibo Mao <maobibo@loongson.cn>
Cc: Chao Li <lichao@loongson.cn>
Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Signed-off-by: xianglai li <lixianglai@loongson.cn>
---
 Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/page.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/page.h b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/page.h
index 6ab07e7900..84c7c13919 100644
--- a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/page.h
+++ b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/page.h
@@ -275,6 +275,6 @@ pte_none (
   IN PTE pte
   )
 {
-  return (!(PTE_VAL(pte) & (~PAGE_GLOBAL)));
+  return (!(PTE_VAL(pte) & (~PAGE_VALID)));
 }
 #endif // PAGE_H_
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [edk2-platforms][PATCH V2 6/7] Platform/Loongson: Optimize the huge page and page entry conversion.
  2023-01-10  1:16 [edk2-platforms][PATCH V2 0/7] loongarch add flash device xianglai
                   ` (4 preceding siblings ...)
  2023-01-10  1:16 ` [edk2-platforms][PATCH V2 5/7] Platform/Loongson: Optimize page table entry null determination xianglai
@ 2023-01-10  1:16 ` xianglai
  2023-01-10  1:16 ` [edk2-platforms][PATCH V2 7/7] Platform/Loongson: Enable zero address protection xianglai
  6 siblings, 0 replies; 10+ messages in thread
From: xianglai @ 2023-01-10  1:16 UTC (permalink / raw)
  To: devel
  Cc: Ard Biesheuvel, Bibo Mao, Chao Li, Leif Lindholm, Liming Gao,
	Michael D Kinney

Optimize the process of converting huge pages
 to page table entries.

Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Bibo Mao <maobibo@loongson.cn>
Cc: Chao Li <lichao@loongson.cn>
Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Signed-off-by: xianglai li <lixianglai@loongson.cn>
---
 .../Library/MmuLib/MmuLibCore.c               | 109 +++++++++++++-----
 .../LoongArchQemuPkg/Library/MmuLib/page.h    |   3 +
 2 files changed, 82 insertions(+), 30 deletions(-)

diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c
index 9e2bd3344a..dac38c63f2 100644
--- a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c
+++ b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c
@@ -449,6 +449,29 @@ GetPteAddress (
   return PteOffset (Pmd, Address);
 }
 
+/**
+  Gets the Attributes of Huge Page.
+
+  @param  Pmd  A pointer to the page middle directory.
+
+  @retval     Value of Attributes.
+**/
+UINTN
+GetHugePageAttributes (
+  IN  PMD *Pmd
+  )
+{
+  UINTN Attributes;
+  UINTN GlobalFlag;
+  UINTN HugeVal = PMD_VAL(*Pmd);
+
+  Attributes = HugeVal & (~HUGEP_PAGE_MASK);
+  GlobalFlag = ((Attributes & (1 << PAGE_HGLOBAL_SHIFT)) >> PAGE_HGLOBAL_SHIFT) << PAGE_GLOBAL_SHIFT;
+  Attributes &= ~(1 << PAGE_HGLOBAL_SHIFT);
+  Attributes |= GlobalFlag;
+  return Attributes;
+}
+
 /**
   Establishes a page table entry based on the specified memory region.
 
@@ -477,13 +500,13 @@ MemoryMapPteRange (
     return EFI_OUT_OF_RESOURCES;
   }
 
+  DEBUG ((DEBUG_VERBOSE,
+    "%a %d Address %p End %p  Attributes %llx\n",
+    __func__, __LINE__,  Address, End, Attributes));
+
   do {
     UpDate = FALSE;
     PteVal = MAKE_PTE (Address, Attributes);
-    DEBUG ((DEBUG_VERBOSE,
-      "%a %d Address %p  PGD_INDEX %p PUD_INDEX   %p PMD_INDEX  %p PTE_INDEX  %p MAKE_PTE  %p\n",
-      __func__, __LINE__,  Address, PGD_INDEX (Address), PUD_INDEX (Address), PMD_INDEX (Address),
-      PTE_INDEX (Address), PteVal));
 
     if ((!pte_none (*Pte)) &&
         (PTE_VAL(*Pte) != PTE_VAL(PteVal)))
@@ -500,6 +523,55 @@ MemoryMapPteRange (
   return EFI_SUCCESS;
 }
 
+/**
+  Convert Huge Page to Page.
+
+  @param  Pmd  A pointer to the page middle directory.
+  @param  Address  The memory space start address.
+  @param  End  The end address of the memory space.
+  @param  Attributes  Memory space Attributes.
+
+  @retval  EFI_SUCCESS   The page table entry was created successfully.
+  @retval  EFI_OUT_OF_RESOURCES  Page table entry establishment failed due to resource exhaustion.
+**/
+EFI_STATUS
+ConvertHugePageToPage (
+  IN  PMD *Pmd,
+  IN UINTN Address,
+  IN UINTN End,
+  IN UINTN Attributes
+  )
+{
+  UINTN OldAttributes;
+  UINTN HugePageEnd;
+  UINTN HugePageStart;
+  EFI_STATUS Status;
+
+  if ((pmd_none (*Pmd)) ||
+      (!IS_HUGE_PAGE (Pmd->PmdVal)))
+  {
+    Status |= MemoryMapPteRange (Pmd, Address, End, Attributes);
+  } else {
+    OldAttributes = GetHugePageAttributes(Pmd);
+    SetPmd (Pmd, (PTE *)PcdGet64 (PcdInvalidPte));
+    HugePageStart = Address & PMD_MASK;
+    HugePageEnd = HugePageStart + HUGE_PAGE_SIZE;
+    ASSERT (HugePageEnd >= End);
+
+    if (Address > HugePageStart) {
+      Status |= MemoryMapPteRange (Pmd, HugePageStart, Address, OldAttributes);
+    }
+
+    Status |= MemoryMapPteRange (Pmd, Address, End, Attributes);
+
+    if (End < HugePageEnd) {
+      Status |= MemoryMapPteRange (Pmd, End, HugePageEnd, OldAttributes);
+    }
+  }
+
+  return Status;
+}
+
 /**
   Establishes a page middle directory based on the specified memory region.
 
@@ -520,10 +592,7 @@ MemoryMapPmdRange (
   )
 {
   PMD *Pmd;
-  PTE *Pte;
   UINTN Next;
-  UINTN AddressStart_HugePage;
-  UINTN AddressEnd_HugePage;
 
   Pmd = PmdAllocGet (Pud, Address);
   if (!Pmd) {
@@ -534,7 +603,7 @@ MemoryMapPmdRange (
     Next = PMD_ADDRESS_END (Address, End);
     if (((Address & (~PMD_MASK)) == 0) &&
         ((Next &  (~PMD_MASK)) == 0) &&
-        (pmd_none (*Pmd)))
+        (pmd_none (*Pmd) || IS_HUGE_PAGE (Pmd->PmdVal)))
     {
       DEBUG ((DEBUG_VERBOSE,
         "%a %d Address %p  PGD_INDEX %p PUD_INDEX   %p PMD_INDEX  %p MAKE_HUGE_PTE  %p\n",
@@ -543,28 +612,7 @@ MemoryMapPmdRange (
 
       SetPmd (Pmd, (PTE *)MAKE_HUGE_PTE (Address, Attributes));
     } else {
-       if ((pmd_none (*Pmd)) ||
-          ((!pmd_none (*Pmd)) &&
-           (!IS_HUGE_PAGE (Pmd->PmdVal))))
-       {
-         if (MemoryMapPteRange (Pmd, Address, Next, Attributes)) {
-           return EFI_OUT_OF_RESOURCES;
-         }
-       } else {
-         SetPmd (Pmd, (PTE *)PcdGet64 (PcdInvalidPte));
-         AddressStart_HugePage = Address & PMD_MASK;
-         AddressEnd_HugePage = AddressStart_HugePage + HUGE_PAGE_SIZE;
-         if (MemoryMapPteRange (Pmd, AddressStart_HugePage, AddressEnd_HugePage, Attributes)) {
-           return EFI_OUT_OF_RESOURCES;
-         }
-         Pte = GetPteAddress (AddressStart_HugePage);
-         if (Pte == NULL) {
-           continue ;
-         }
-         if (AddressEnd_HugePage > End) {
-           Next = End;
-         }
-       }
+      ConvertHugePageToPage (Pmd, Address, Next, Attributes);
     }
   } while (Pmd++, Address = Next, Address != End);
 
@@ -788,6 +836,7 @@ LoongArchSetMemoryAttributes (
   Attributes = EfiAttributeToLoongArchAttribute (Attributes);
   DEBUG ((DEBUG_VERBOSE, "%a %d %p %p %p.\n", __func__, __LINE__, BaseAddress , Length, Attributes));
   MemoryMapPageRange (BaseAddress, BaseAddress + Length, Attributes);
+  DEBUG ((DEBUG_VERBOSE, "%a %d end.\n", __func__, __LINE__));
 
   return EFI_SUCCESS;
 }
diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/page.h b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/page.h
index 84c7c13919..927aeb018d 100644
--- a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/page.h
+++ b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/page.h
@@ -42,6 +42,9 @@
 #define PFN_MASK                            (~(((UINTN)(1) << (EFI_PAGE_SHIFT)) - 1) & \
                                              (((UINTN)(1) << (PAGE_PFN_END_SHIFT)) - 1))
 
+#define HUGEP_PAGE_MASK                     (~(((UINTN)(1) << (PMD_SHIFT)) - 1) & \
+                                             (((UINTN)(1) << (PAGE_PFN_END_SHIFT)) - 1))
+
 typedef struct { UINTN PgdVal; } PGD;
 typedef struct { UINTN PudVal; } PUD;
 typedef struct { UINTN PmdVal; } PMD;
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [edk2-platforms][PATCH V2 7/7] Platform/Loongson: Enable zero address protection.
  2023-01-10  1:16 [edk2-platforms][PATCH V2 0/7] loongarch add flash device xianglai
                   ` (5 preceding siblings ...)
  2023-01-10  1:16 ` [edk2-platforms][PATCH V2 6/7] Platform/Loongson: Optimize the huge page and page entry conversion xianglai
@ 2023-01-10  1:16 ` xianglai
  6 siblings, 0 replies; 10+ messages in thread
From: xianglai @ 2023-01-10  1:16 UTC (permalink / raw)
  To: devel
  Cc: Ard Biesheuvel, Bibo Mao, Chao Li, Leif Lindholm, Liming Gao,
	Michael D Kinney

Set the 0 page property to unreadable, non-writable, non-executable

Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Bibo Mao <maobibo@loongson.cn>
Cc: Chao Li <lichao@loongson.cn>
Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Signed-off-by: xianglai li <lixianglai@loongson.cn>
---
 .../LoongArchQemuPkg/Library/MmuLib/MmuBaseLibPei.inf     | 2 ++
 .../Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c | 8 +++++++-
 .../LoongArchQemuPkg/Library/MmuLib/MmuLibCorePei.c       | 5 +++++
 Platform/Loongson/LoongArchQemuPkg/Loongson.dsc           | 2 ++
 4 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuBaseLibPei.inf b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuBaseLibPei.inf
index 12848eecfe..d7ab37eea4 100644
--- a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuBaseLibPei.inf
+++ b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuBaseLibPei.inf
@@ -28,6 +28,7 @@
   MdePkg/MdePkg.dec
   Platform/Loongson/LoongArchQemuPkg/Loongson.dec
   OvmfPkg/OvmfPkg.dec
+  MdeModulePkg/MdeModulePkg.dec
 
 [PCD]
   gLoongArchQemuPkgTokenSpaceGuid.PcdSwapPageDir
@@ -38,6 +39,7 @@
   gLoongArchQemuPkgTokenSpaceGuid.PcdFlashSecFvSize
   gLoongArchQemuPkgTokenSpaceGuid.PcdFlashSecFvBase
   gLoongArchQemuPkgTokenSpaceGuid.PcdRamSize
+  gEfiMdeModulePkgTokenSpaceGuid.PcdNullPointerDetectionPropertyMask
 
 [LibraryClasses]
   MemoryAllocationLib
diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c
index dac38c63f2..a45bcbdb6d 100644
--- a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c
+++ b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCore.c
@@ -735,10 +735,16 @@ EfiAttributeToLoongArchAttribute (
   }
 
   // Write protection attributes
-  if ((EfiAttributes & EFI_MEMORY_RO) != 0) {
+  if (((EfiAttributes & EFI_MEMORY_RO) != 0) ||
+      ((EfiAttributes & EFI_MEMORY_WP) != 0))
+  {
     LoongArchAttributes &= ~PAGE_DIRTY;
   }
 
+  if (EfiAttributes & EFI_MEMORY_RP) {
+    LoongArchAttributes |= PAGE_NO_READ;
+  }
+
   //eXecute protection attribute
   if ((EfiAttributes & EFI_MEMORY_XP) != 0) {
     LoongArchAttributes |= PAGE_NO_EXEC;
diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCorePei.c b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCorePei.c
index 32a7fc0beb..a21233e796 100644
--- a/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCorePei.c
+++ b/Platform/Loongson/LoongArchQemuPkg/Library/MmuLib/MmuLibCorePei.c
@@ -21,6 +21,7 @@
 #include <Library/QemuFwCfgLib.h>
 #include "MmuLibCore.h"
 #include <Library/CacheMaintenanceLib.h>
+#include <Library/MmuLib.h>
 
 /**
   Return the Virtual Memory Map of your platform
@@ -170,6 +171,10 @@ ConfigureMmu (VOID)
     MemoryTable++;
   }
 
+  if (PcdGet8 (PcdNullPointerDetectionPropertyMask) & BIT0) {
+    LoongArchSetMemoryAttributes (0, EFI_PAGE_SIZE, EFI_MEMORY_RP | EFI_MEMORY_XP | EFI_MEMORY_WP);
+  }
+
   TlbReEntry = AllocatePages (1);
   if (TlbReEntry == NULL) {
     goto FreeTranslationTable;
diff --git a/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc b/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
index 05913db144..13145cf051 100644
--- a/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
+++ b/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
@@ -365,6 +365,8 @@
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize         | 0x40000
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize       | 0x40000
 
+  gEfiMdeModulePkgTokenSpaceGuid.PcdNullPointerDetectionPropertyMask   | 1
+
 ################################################################################
 #
 # Pcd Dynamic Section - list of all EDK II PCD Entries defined by this Platform
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [edk2-devel] [edk2-platforms][PATCH V2 3/7] Platform/Loongson: Support pflash for loongarch.
  2023-01-10  1:16 ` [edk2-platforms][PATCH V2 3/7] Platform/Loongson: Support pflash " xianglai
@ 2023-01-10  7:12   ` Chao Li
  2023-01-11  7:16     ` xianglai
  0 siblings, 1 reply; 10+ messages in thread
From: Chao Li @ 2023-01-10  7:12 UTC (permalink / raw)
  To: devel, lixianglai
  Cc: Ard Biesheuvel, Bibo Mao, Leif Lindholm, Liming Gao,
	Michael D Kinney

[-- Attachment #1: Type: text/plain, Size: 14490 bytes --]

I suggest to splitting this patch into two, one for adding new driver, 
and one for adjusting the code.


Thanks,
Chao
在 2023/1/10 09:16, xianglai 写道:
> Add pflash driver for loongarch.
>
> Cc: Ard Biesheuvel<ardb+tianocore@kernel.org>
> Cc: Bibo Mao<maobibo@loongson.cn>
> Cc: Chao Li<lichao@loongson.cn>
> Cc: Leif Lindholm<quic_llindhol@quicinc.com>
> Cc: Liming Gao<gaoliming@byosoft.com.cn>
> Cc: Michael D Kinney<michael.d.kinney@intel.com>
> Signed-off-by: xianglai li<lixianglai@loongson.cn>
> ---
>   .../Library/NorFlashQemuLib/NorFlashQemuLib.c | 141 ++++++++++++++++++
>   .../NorFlashQemuLib/NorFlashQemuLib.inf       |  43 ++++++
>   .../Loongson/LoongArchQemuPkg/Loongson.dsc    |  15 +-
>   .../Loongson/LoongArchQemuPkg/Loongson.fdf    |   3 +-
>   .../LoongArchQemuPkg/VarStore.fdf.inc         |  67 +++++++++
>   5 files changed, 260 insertions(+), 9 deletions(-)
>   create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c
>   create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf
>   create mode 100644 Platform/Loongson/LoongArchQemuPkg/VarStore.fdf.inc
>
> diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c b/Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c
> new file mode 100644
> index 0000000000..2e0bf3cef0
> --- /dev/null
> +++ b/Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c
> @@ -0,0 +1,141 @@
> +/** @file
> +
> +  Copyright (c) 2023 Loongson Technology Corporation Limited. All rights reserved.<BR>
> +
> +  SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +
> +#include <Library/BaseLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/UefiBootServicesTableLib.h>
> +#include <Library/VirtNorFlashPlatformLib.h>
> +
> +#include <Protocol/FdtClient.h>
> +
> +#define QEMU_NOR_BLOCK_SIZE  SIZE_128KB
> +
> +EFI_STATUS
> +VirtNorFlashPlatformInitialization (
> +  VOID
> +  )
> +{
> +  return EFI_SUCCESS;
> +}
> +
> +STATIC VIRT_NOR_FLASH_DESCRIPTION  mNorFlashDevices;
> +
> +EFI_STATUS
> +VirtNorFlashPlatformGetDevices (
> +  OUT VIRT_NOR_FLASH_DESCRIPTION  **NorFlashDescriptions,
> +  OUT UINT32                      *Count
> +  )
> +{
> +  FDT_CLIENT_PROTOCOL  *FdtClient;
> +  INT32                Node;
> +  EFI_STATUS           Status;
> +  EFI_STATUS           FindNodeStatus;
> +  CONST UINT32         *Reg;
> +  UINT32               PropSize;
> +  UINT64               Base;
> +  UINT64               Size;
> +
> +  Status = gBS->LocateProtocol (
> +                  &gFdtClientProtocolGuid,
> +                  NULL,
> +                  (VOID **)&FdtClient
> +                  );
> +  ASSERT_EFI_ERROR (Status);
> +
> +  FindNodeStatus = FdtClient->FindCompatibleNode (
> +                                     FdtClient,
> +                                     "cfi-flash",
> +                                     &Node
> +                                     );
> +  ASSERT_EFI_ERROR (FindNodeStatus);
> +
> +  Status = FdtClient->GetNodeProperty (
> +                        FdtClient,
> +                        Node,
> +                        "reg",
> +                        (CONST VOID **)&Reg,
> +                        &PropSize
> +                        );
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((
> +      DEBUG_ERROR,
> +      "%a: GetNodeProperty () failed (Status == %r)\n",
> +      __FUNCTION__,
> +      Status
> +      ));
> +    return Status;
> +  }
> +
> +  ASSERT ((PropSize % (4 * sizeof (UINT32))) == 0);
> +
> +  if (PropSize < (4 * sizeof (UINT32))) {
> +    DEBUG ((
> +      DEBUG_ERROR,
> +      "%a: reg node size(%d) is too small \n",
> +      __FUNCTION__,
> +      PropSize
> +      ));
> +    return EFI_NOT_FOUND;
> +  }
> +
> +  Base = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[0]));
> +  Size = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[2]));
> +
> +  mNorFlashDevices.DeviceBaseAddress = (UINTN)Base;
> +  mNorFlashDevices.RegionBaseAddress = (UINTN)Base;
> +  mNorFlashDevices.Size              = (UINTN)Size;
> +  mNorFlashDevices.BlockSize         = QEMU_NOR_BLOCK_SIZE;
> +
> +  Status = PcdSet32S (PcdFlashNvStorageVariableBase, Base);
> +  ASSERT_EFI_ERROR (Status);
> +
> +  /*
> +   * Base is the value of PcdFlashNvStorageVariableBase,
> +   * PcdFlashNvStorageFtwWorkingBase can be got by
> +   *   PcdFlashNvStorageVariableBase + PcdFlashNvStorageVariableSize
> +   */
> +  Base += PcdGet32 (PcdFlashNvStorageVariableSize);
> +  Status = PcdSet32S (PcdFlashNvStorageFtwWorkingBase, Base);
> +  ASSERT_EFI_ERROR (Status);
> +
> +  /*
> +   * Now,Base is the value of PcdFlashNvStorageFtwWorkingBase,
> +   * PcdFlashNvStorageFtwSpareBase can be got by
> +   *   PcdFlashNvStorageFtwWorkingBase + PcdFlashNvStorageFtwWorkingSize.
> +   */
> +  Base += PcdGet32 (PcdFlashNvStorageFtwWorkingSize);
> +  Status = PcdSet32S (PcdFlashNvStorageFtwSpareBase, Base);
> +  ASSERT_EFI_ERROR (Status);
> +
> +  //
> +  // UEFI takes ownership of the NOR flash, and exposes its functionality
> +  // through the UEFI Runtime Services GetVariable, SetVariable, etc. This
> +  // means we need to disable it in the device tree to prevent the OS from
> +  // attaching its device driver as well.
> +  // Note that this also hides other flash banks, but the only other flash
> +  // bank we expect to encounter is the one that carries the UEFI executable
> +  // code, which is not intended to be guest updatable, and is usually backed
> +  // in a readonly manner by QEMU anyway.
> +  //
> +  Status = FdtClient->SetNodeProperty (
> +                        FdtClient,
> +                        Node,
> +                        "status",
> +                        "disabled",
> +                        sizeof ("disabled")
> +                        );
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((DEBUG_WARN, "Failed to set NOR flash status to 'disabled'\n"));
> +  }
> +
> +  *NorFlashDescriptions = &mNorFlashDevices;
> +  *Count                = 1;
> +
> +  return EFI_SUCCESS;
> +}
> diff --git a/Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf b/Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf
> new file mode 100644
> index 0000000000..da05ca0898
> --- /dev/null
> +++ b/Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf
> @@ -0,0 +1,43 @@
> +## @file
> +#
> +#  Copyright (c) 2023 Loongson Technology Corporation Limited. All rights reserved.<BR>
> +#
> +#  SPDX-License-Identifier: BSD-2-Clause-Patent
> +#
> +##
> +
> +[Defines]
> +  INF_VERSION                    = 0x00010005
> +  BASE_NAME                      = NorFlashQemuLib
> +  FILE_GUID                      = 339B7829-4C5F-4EFC-B2DD-5050E530DECE
> +  MODULE_TYPE                    = DXE_DRIVER
> +  VERSION_STRING                 = 1.0
> +  LIBRARY_CLASS                  = VirtNorFlashPlatformLib
> +
> +[Sources.common]
> +  NorFlashQemuLib.c
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +  EmbeddedPkg/EmbeddedPkg.dec
> +  OvmfPkg/OvmfPkg.dec
> +  MdeModulePkg/MdeModulePkg.dec
> +
> +[LibraryClasses]
> +  BaseLib
> +  DebugLib
> +  UefiBootServicesTableLib
> +
> +[Protocols]
> +  gFdtClientProtocolGuid          ## CONSUMES
> +
> +[Depex]
> +  gFdtClientProtocolGuid
> +
> +[Pcd]
> +gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize
> +gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize
> +gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize
> +gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase
> +gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase
> +gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase
> diff --git a/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc b/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
> index a9b5c8c514..05913db144 100644
> --- a/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
> +++ b/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
> @@ -175,6 +175,7 @@
>     DebugLib                         | MdePkg/Library/BaseDebugLibSerialPort/BaseDebugLibSerialPort.inf
>     PeiServicesLib                   | MdePkg/Library/PeiServicesLib/PeiServicesLib.inf
>     VariableFlashInfoLib             | MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.inf
> +  VirtNorFlashPlatformLib          | Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf
>   
>   [LibraryClasses.common.SEC]
>     PcdLib                           | MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf
> @@ -360,9 +361,9 @@
>     #
>   !include NetworkPkg/NetworkPcds.dsc.inc
>   
> -  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize         | 0x10000
> -  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize         | 0x20000
> -  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize       | 0x10000
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize         | 0x40000
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize         | 0x40000
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize       | 0x40000
>   
>   ################################################################################
>   #
> @@ -373,6 +374,7 @@
>     gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase         | 0
>     gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64       | 0
>     gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64       | 0
> +  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase         | 0
>     gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase       | 0
>     gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64     | 0
>     gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvStoreReserved         | 0
> @@ -471,15 +473,12 @@
>     #
>     # Variable
>     #
> -
> -  OvmfPkg/EmuVariableFvbRuntimeDxe/Fvb.inf {
> -    <LibraryClasses>
> -      PlatformFvbLib|OvmfPkg/Library/EmuVariableFvbLib/EmuVariableFvbLib.inf
> -  }
> +  OvmfPkg/VirtNorFlashDxe/VirtNorFlashDxe.inf
>     MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf
>     MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf {
>       <LibraryClasses>
>         NULL|MdeModulePkg/Library/VarCheckUefiLib/VarCheckUefiLib.inf
> +      NULL|EmbeddedPkg/Library/NvVarStoreFormattedLib/NvVarStoreFormattedLib.inf
>         BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
>     }
>   
> diff --git a/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf b/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf
> index ee89097344..675d86094c 100644
> --- a/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf
> +++ b/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf
> @@ -30,6 +30,7 @@ $(DXEFV_OFFSET)|$(DXEFV_SIZE)
>   gLoongArchQemuPkgTokenSpaceGuid.PcdFlashDxeFvBase|gLoongArchQemuPkgTokenSpaceGuid.PcdFlashDxeFvSize
>   FV = FVMAIN_COMPACT
>   
> +!include VarStore.fdf.inc
>   #####################################################################################################
>   [FV.SECFV]
>   FvNameGuid         = 587d4265-5e71-41da-9c35-4258551f1e22
> @@ -135,7 +136,7 @@ INF  MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleRuntimeDxe.inf
>   #
>   # Variable
>   #
> -INF  OvmfPkg/EmuVariableFvbRuntimeDxe/Fvb.inf
> +INF  OvmfPkg/VirtNorFlashDxe/VirtNorFlashDxe.inf
>   INF  MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf
>   INF  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
>   #
> diff --git a/Platform/Loongson/LoongArchQemuPkg/VarStore.fdf.inc b/Platform/Loongson/LoongArchQemuPkg/VarStore.fdf.inc
> new file mode 100644
> index 0000000000..83ce3d8008
> --- /dev/null
> +++ b/Platform/Loongson/LoongArchQemuPkg/VarStore.fdf.inc
> @@ -0,0 +1,67 @@
> +## @file
> +#
> +#  Copyright (c) 2023 Loongson Technology Corporation Limited. All rights reserved.<BR>
> +#
> +#  SPDX-License-Identifier: BSD-2-Clause-Patent
> +#
> +##
> +
> +[FD.QEMU_VARS]
> +BaseAddress   = 0x0
> +Size          = 0x1000000
> +ErasePolarity = 1
> +BlockSize     = 0x20000
> +NumBlocks     = 128
> +
> +0x00000000|0x00040000
> +#NV_VARIABLE_STORE
> +DATA = {
> +  ## This is the EFI_FIRMWARE_VOLUME_HEADER
> +  # ZeroVector []
> +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
> +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
> +  # FileSystemGuid: gEfiSystemNvDataFvGuid         =
> +  #   { 0xFFF12B8D, 0x7696, 0x4C8B,
> +  #     { 0xA9, 0x85, 0x27, 0x47, 0x07, 0x5B, 0x4F, 0x50 }}
> +  0x8D, 0x2B, 0xF1, 0xFF, 0x96, 0x76, 0x8B, 0x4C,
> +  0xA9, 0x85, 0x27, 0x47, 0x07, 0x5B, 0x4F, 0x50,
> +  # FvLength: 0xC0000
> +  0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00,
> +  # Signature "_FVH"       # Attributes
> +  0x5f, 0x46, 0x56, 0x48, 0xff, 0xfe, 0x04, 0x00,
> +  # HeaderLength # CheckSum # ExtHeaderOffset #Reserved #Revision
> +  0x48, 0x00, 0x28, 0x09, 0x00, 0x00, 0x00, 0x02,
> +  # Blockmap[0]: 0x3 Blocks * 0x40000 Bytes / Block
> +  0x3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
> +  # Blockmap[1]: End
> +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
> +  ## This is the VARIABLE_STORE_HEADER
> +  # It is compatible with SECURE_BOOT_ENABLE == FALSE as well.
> +  # Signature: gEfiAuthenticatedVariableGuid =
> +  #   { 0xaaf32c78, 0x947b, 0x439a,
> +  #     { 0xa1, 0x80, 0x2e, 0x14, 0x4e, 0xc3, 0x77, 0x92 }}
> +  0x78, 0x2c, 0xf3, 0xaa, 0x7b, 0x94, 0x9a, 0x43,
> +  0xa1, 0x80, 0x2e, 0x14, 0x4e, 0xc3, 0x77, 0x92,
> +  # Size: 0x40000 (gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize) -
> +  #         0x48 (size of EFI_FIRMWARE_VOLUME_HEADER) = 0x3ffb8
> +  # This can speed up the Variable Dispatch a bit.
> +  0xB8, 0xFF, 0x03, 0x00,
> +  # FORMATTED: 0x5A #HEALTHY: 0xFE #Reserved: UINT16 #Reserved1: UINT32
> +  0x5A, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
> +}
> +
> +0x00040000|0x00040000
> +#NV_FTW_WORKING
> +DATA = {
> +  # EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER->Signature = gEdkiiWorkingBlockSignatureGuid         =
> +  #  { 0x9e58292b, 0x7c68, 0x497d, { 0xa0, 0xce, 0x65,  0x0, 0xfd, 0x9f, 0x1b, 0x95 }}
> +  0x2b, 0x29, 0x58, 0x9e, 0x68, 0x7c, 0x7d, 0x49,
> +  0xa0, 0xce, 0x65,  0x0, 0xfd, 0x9f, 0x1b, 0x95,
> +  # Crc:UINT32            #WorkingBlockValid:1, WorkingBlockInvalid:1, Reserved
> +  0x5b, 0xe7, 0xc6, 0x86, 0xFE, 0xFF, 0xFF, 0xFF,
> +  # WriteQueueSize: UINT64
> +  0xE0, 0xFF, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00
> +}
> +
> +0x00080000|0x00040000
> +#NV_FTW_SPARE

[-- Attachment #2: Type: text/html, Size: 15093 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [edk2-devel] [edk2-platforms][PATCH V2 3/7] Platform/Loongson: Support pflash for loongarch.
  2023-01-10  7:12   ` [edk2-devel] " Chao Li
@ 2023-01-11  7:16     ` xianglai
  0 siblings, 0 replies; 10+ messages in thread
From: xianglai @ 2023-01-11  7:16 UTC (permalink / raw)
  To: devel, lichao
  Cc: Ard Biesheuvel, Bibo Mao, Leif Lindholm, Liming Gao,
	Michael D Kinney

Ok, I'll split it in the next version.

Thanks,

xianglai


在 2023/1/10 下午3:12, Chao Li 写道:
> I suggest to splitting this patch into two, one for adding new driver, 
> and one for adjusting the code.
>
>
> Thanks,
> Chao
> 在 2023/1/10 09:16, xianglai 写道:
>> Add pflash driver for loongarch.
>>
>> Cc: Ard Biesheuvel<ardb+tianocore@kernel.org>
>> Cc: Bibo Mao<maobibo@loongson.cn>
>> Cc: Chao Li<lichao@loongson.cn>
>> Cc: Leif Lindholm<quic_llindhol@quicinc.com>
>> Cc: Liming Gao<gaoliming@byosoft.com.cn>
>> Cc: Michael D Kinney<michael.d.kinney@intel.com>
>> Signed-off-by: xianglai li<lixianglai@loongson.cn>
>> ---
>>   .../Library/NorFlashQemuLib/NorFlashQemuLib.c | 141 ++++++++++++++++++
>>   .../NorFlashQemuLib/NorFlashQemuLib.inf       |  43 ++++++
>>   .../Loongson/LoongArchQemuPkg/Loongson.dsc    |  15 +-
>>   .../Loongson/LoongArchQemuPkg/Loongson.fdf    |   3 +-
>>   .../LoongArchQemuPkg/VarStore.fdf.inc         |  67 +++++++++
>>   5 files changed, 260 insertions(+), 9 deletions(-)
>>   create mode 100644 
>> Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c
>>   create mode 100644 
>> Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf
>>   create mode 100644 Platform/Loongson/LoongArchQemuPkg/VarStore.fdf.inc
>>
>> diff --git 
>> a/Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c 
>> b/Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c 
>>
>> new file mode 100644
>> index 0000000000..2e0bf3cef0
>> --- /dev/null
>> +++ 
>> b/Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c
>> @@ -0,0 +1,141 @@
>> +/** @file
>> +
>> +  Copyright (c) 2023 Loongson Technology Corporation Limited. All 
>> rights reserved.<BR>
>> +
>> +  SPDX-License-Identifier: BSD-2-Clause-Patent
>> +
>> +**/
>> +
>> +
>> +#include <Library/BaseLib.h>
>> +#include <Library/DebugLib.h>
>> +#include <Library/UefiBootServicesTableLib.h>
>> +#include <Library/VirtNorFlashPlatformLib.h>
>> +
>> +#include <Protocol/FdtClient.h>
>> +
>> +#define QEMU_NOR_BLOCK_SIZE  SIZE_128KB
>> +
>> +EFI_STATUS
>> +VirtNorFlashPlatformInitialization (
>> +  VOID
>> +  )
>> +{
>> +  return EFI_SUCCESS;
>> +}
>> +
>> +STATIC VIRT_NOR_FLASH_DESCRIPTION  mNorFlashDevices;
>> +
>> +EFI_STATUS
>> +VirtNorFlashPlatformGetDevices (
>> +  OUT VIRT_NOR_FLASH_DESCRIPTION  **NorFlashDescriptions,
>> +  OUT UINT32                      *Count
>> +  )
>> +{
>> +  FDT_CLIENT_PROTOCOL  *FdtClient;
>> +  INT32                Node;
>> +  EFI_STATUS           Status;
>> +  EFI_STATUS           FindNodeStatus;
>> +  CONST UINT32         *Reg;
>> +  UINT32               PropSize;
>> +  UINT64               Base;
>> +  UINT64               Size;
>> +
>> +  Status = gBS->LocateProtocol (
>> +                  &gFdtClientProtocolGuid,
>> +                  NULL,
>> +                  (VOID **)&FdtClient
>> +                  );
>> +  ASSERT_EFI_ERROR (Status);
>> +
>> +  FindNodeStatus = FdtClient->FindCompatibleNode (
>> +                                     FdtClient,
>> +                                     "cfi-flash",
>> +                                     &Node
>> +                                     );
>> +  ASSERT_EFI_ERROR (FindNodeStatus);
>> +
>> +  Status = FdtClient->GetNodeProperty (
>> +                        FdtClient,
>> +                        Node,
>> +                        "reg",
>> +                        (CONST VOID **)&Reg,
>> +                        &PropSize
>> +                        );
>> +  if (EFI_ERROR (Status)) {
>> +    DEBUG ((
>> +      DEBUG_ERROR,
>> +      "%a: GetNodeProperty () failed (Status == %r)\n",
>> +      __FUNCTION__,
>> +      Status
>> +      ));
>> +    return Status;
>> +  }
>> +
>> +  ASSERT ((PropSize % (4 * sizeof (UINT32))) == 0);
>> +
>> +  if (PropSize < (4 * sizeof (UINT32))) {
>> +    DEBUG ((
>> +      DEBUG_ERROR,
>> +      "%a: reg node size(%d) is too small \n",
>> +      __FUNCTION__,
>> +      PropSize
>> +      ));
>> +    return EFI_NOT_FOUND;
>> +  }
>> +
>> +  Base = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[0]));
>> +  Size = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[2]));
>> +
>> +  mNorFlashDevices.DeviceBaseAddress = (UINTN)Base;
>> +  mNorFlashDevices.RegionBaseAddress = (UINTN)Base;
>> +  mNorFlashDevices.Size              = (UINTN)Size;
>> +  mNorFlashDevices.BlockSize         = QEMU_NOR_BLOCK_SIZE;
>> +
>> +  Status = PcdSet32S (PcdFlashNvStorageVariableBase, Base);
>> +  ASSERT_EFI_ERROR (Status);
>> +
>> +  /*
>> +   * Base is the value of PcdFlashNvStorageVariableBase,
>> +   * PcdFlashNvStorageFtwWorkingBase can be got by
>> +   *   PcdFlashNvStorageVariableBase + PcdFlashNvStorageVariableSize
>> +   */
>> +  Base += PcdGet32 (PcdFlashNvStorageVariableSize);
>> +  Status = PcdSet32S (PcdFlashNvStorageFtwWorkingBase, Base);
>> +  ASSERT_EFI_ERROR (Status);
>> +
>> +  /*
>> +   * Now,Base is the value of PcdFlashNvStorageFtwWorkingBase,
>> +   * PcdFlashNvStorageFtwSpareBase can be got by
>> +   *   PcdFlashNvStorageFtwWorkingBase + 
>> PcdFlashNvStorageFtwWorkingSize.
>> +   */
>> +  Base += PcdGet32 (PcdFlashNvStorageFtwWorkingSize);
>> +  Status = PcdSet32S (PcdFlashNvStorageFtwSpareBase, Base);
>> +  ASSERT_EFI_ERROR (Status);
>> +
>> +  //
>> +  // UEFI takes ownership of the NOR flash, and exposes its 
>> functionality
>> +  // through the UEFI Runtime Services GetVariable, SetVariable, 
>> etc. This
>> +  // means we need to disable it in the device tree to prevent the 
>> OS from
>> +  // attaching its device driver as well.
>> +  // Note that this also hides other flash banks, but the only other 
>> flash
>> +  // bank we expect to encounter is the one that carries the UEFI 
>> executable
>> +  // code, which is not intended to be guest updatable, and is 
>> usually backed
>> +  // in a readonly manner by QEMU anyway.
>> +  //
>> +  Status = FdtClient->SetNodeProperty (
>> +                        FdtClient,
>> +                        Node,
>> +                        "status",
>> +                        "disabled",
>> +                        sizeof ("disabled")
>> +                        );
>> +  if (EFI_ERROR (Status)) {
>> +    DEBUG ((DEBUG_WARN, "Failed to set NOR flash status to 
>> 'disabled'\n"));
>> +  }
>> +
>> +  *NorFlashDescriptions = &mNorFlashDevices;
>> +  *Count                = 1;
>> +
>> +  return EFI_SUCCESS;
>> +}
>> diff --git 
>> a/Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf 
>> b/Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf 
>>
>> new file mode 100644
>> index 0000000000..da05ca0898
>> --- /dev/null
>> +++ 
>> b/Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf
>> @@ -0,0 +1,43 @@
>> +## @file
>> +#
>> +#  Copyright (c) 2023 Loongson Technology Corporation Limited. All 
>> rights reserved.<BR>
>> +#
>> +#  SPDX-License-Identifier: BSD-2-Clause-Patent
>> +#
>> +##
>> +
>> +[Defines]
>> +  INF_VERSION                    = 0x00010005
>> +  BASE_NAME                      = NorFlashQemuLib
>> +  FILE_GUID                      = 339B7829-4C5F-4EFC-B2DD-5050E530DECE
>> +  MODULE_TYPE                    = DXE_DRIVER
>> +  VERSION_STRING                 = 1.0
>> +  LIBRARY_CLASS                  = VirtNorFlashPlatformLib
>> +
>> +[Sources.common]
>> +  NorFlashQemuLib.c
>> +
>> +[Packages]
>> +  MdePkg/MdePkg.dec
>> +  EmbeddedPkg/EmbeddedPkg.dec
>> +  OvmfPkg/OvmfPkg.dec
>> +  MdeModulePkg/MdeModulePkg.dec
>> +
>> +[LibraryClasses]
>> +  BaseLib
>> +  DebugLib
>> +  UefiBootServicesTableLib
>> +
>> +[Protocols]
>> +  gFdtClientProtocolGuid          ## CONSUMES
>> +
>> +[Depex]
>> +  gFdtClientProtocolGuid
>> +
>> +[Pcd]
>> +gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize
>> +gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize
>> +gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize
>> +gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase
>> +gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase
>> +gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase
>> diff --git a/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc 
>> b/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
>> index a9b5c8c514..05913db144 100644
>> --- a/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
>> +++ b/Platform/Loongson/LoongArchQemuPkg/Loongson.dsc
>> @@ -175,6 +175,7 @@
>>     DebugLib                         | 
>> MdePkg/Library/BaseDebugLibSerialPort/BaseDebugLibSerialPort.inf
>>     PeiServicesLib                   | 
>> MdePkg/Library/PeiServicesLib/PeiServicesLib.inf
>>     VariableFlashInfoLib             | 
>> MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.inf
>> +  VirtNorFlashPlatformLib          | 
>> Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf
>>     [LibraryClasses.common.SEC]
>>     PcdLib                           | 
>> MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf
>> @@ -360,9 +361,9 @@
>>     #
>>   !include NetworkPkg/NetworkPcds.dsc.inc
>>   - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize | 
>> 0x10000
>> - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize | 0x20000
>> - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize | 
>> 0x10000
>> + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize | 0x40000
>> + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize | 0x40000
>> + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize | 
>> 0x40000
>> ################################################################################
>>   #
>> @@ -373,6 +374,7 @@
>> gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase | 0
>> gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64 | 0
>> gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64 | 0
>> + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase | 0
>> gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase | 0
>> gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64 | 0
>> gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvStoreReserved | 0
>> @@ -471,15 +473,12 @@
>>     #
>>     # Variable
>>     #
>> -
>> -  OvmfPkg/EmuVariableFvbRuntimeDxe/Fvb.inf {
>> -    <LibraryClasses>
>> - PlatformFvbLib|OvmfPkg/Library/EmuVariableFvbLib/EmuVariableFvbLib.inf
>> -  }
>> +  OvmfPkg/VirtNorFlashDxe/VirtNorFlashDxe.inf
>> MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf
>> MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf {
>>       <LibraryClasses>
>> NULL|MdeModulePkg/Library/VarCheckUefiLib/VarCheckUefiLib.inf
>> + 
>> NULL|EmbeddedPkg/Library/NvVarStoreFormattedLib/NvVarStoreFormattedLib.inf
>> BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
>>     }
>>   diff --git a/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf 
>> b/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf
>> index ee89097344..675d86094c 100644
>> --- a/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf
>> +++ b/Platform/Loongson/LoongArchQemuPkg/Loongson.fdf
>> @@ -30,6 +30,7 @@ $(DXEFV_OFFSET)|$(DXEFV_SIZE)
>> gLoongArchQemuPkgTokenSpaceGuid.PcdFlashDxeFvBase|gLoongArchQemuPkgTokenSpaceGuid.PcdFlashDxeFvSize
>>   FV = FVMAIN_COMPACT
>>   +!include VarStore.fdf.inc
>> #####################################################################################################
>>   [FV.SECFV]
>>   FvNameGuid         = 587d4265-5e71-41da-9c35-4258551f1e22
>> @@ -135,7 +136,7 @@ INF 
>> MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleRuntimeDxe.inf
>>   #
>>   # Variable
>>   #
>> -INF  OvmfPkg/EmuVariableFvbRuntimeDxe/Fvb.inf
>> +INF  OvmfPkg/VirtNorFlashDxe/VirtNorFlashDxe.inf
>>   INF 
>> MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf
>>   INF MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf
>>   #
>> diff --git a/Platform/Loongson/LoongArchQemuPkg/VarStore.fdf.inc 
>> b/Platform/Loongson/LoongArchQemuPkg/VarStore.fdf.inc
>> new file mode 100644
>> index 0000000000..83ce3d8008
>> --- /dev/null
>> +++ b/Platform/Loongson/LoongArchQemuPkg/VarStore.fdf.inc
>> @@ -0,0 +1,67 @@
>> +## @file
>> +#
>> +#  Copyright (c) 2023 Loongson Technology Corporation Limited. All 
>> rights reserved.<BR>
>> +#
>> +#  SPDX-License-Identifier: BSD-2-Clause-Patent
>> +#
>> +##
>> +
>> +[FD.QEMU_VARS]
>> +BaseAddress   = 0x0
>> +Size          = 0x1000000
>> +ErasePolarity = 1
>> +BlockSize     = 0x20000
>> +NumBlocks     = 128
>> +
>> +0x00000000|0x00040000
>> +#NV_VARIABLE_STORE
>> +DATA = {
>> +  ## This is the EFI_FIRMWARE_VOLUME_HEADER
>> +  # ZeroVector []
>> +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
>> +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
>> +  # FileSystemGuid: gEfiSystemNvDataFvGuid         =
>> +  #   { 0xFFF12B8D, 0x7696, 0x4C8B,
>> +  #     { 0xA9, 0x85, 0x27, 0x47, 0x07, 0x5B, 0x4F, 0x50 }}
>> +  0x8D, 0x2B, 0xF1, 0xFF, 0x96, 0x76, 0x8B, 0x4C,
>> +  0xA9, 0x85, 0x27, 0x47, 0x07, 0x5B, 0x4F, 0x50,
>> +  # FvLength: 0xC0000
>> +  0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00,
>> +  # Signature "_FVH"       # Attributes
>> +  0x5f, 0x46, 0x56, 0x48, 0xff, 0xfe, 0x04, 0x00,
>> +  # HeaderLength # CheckSum # ExtHeaderOffset #Reserved #Revision
>> +  0x48, 0x00, 0x28, 0x09, 0x00, 0x00, 0x00, 0x02,
>> +  # Blockmap[0]: 0x3 Blocks * 0x40000 Bytes / Block
>> +  0x3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
>> +  # Blockmap[1]: End
>> +  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
>> +  ## This is the VARIABLE_STORE_HEADER
>> +  # It is compatible with SECURE_BOOT_ENABLE == FALSE as well.
>> +  # Signature: gEfiAuthenticatedVariableGuid =
>> +  #   { 0xaaf32c78, 0x947b, 0x439a,
>> +  #     { 0xa1, 0x80, 0x2e, 0x14, 0x4e, 0xc3, 0x77, 0x92 }}
>> +  0x78, 0x2c, 0xf3, 0xaa, 0x7b, 0x94, 0x9a, 0x43,
>> +  0xa1, 0x80, 0x2e, 0x14, 0x4e, 0xc3, 0x77, 0x92,
>> +  # Size: 0x40000 
>> (gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize) -
>> +  #         0x48 (size of EFI_FIRMWARE_VOLUME_HEADER) = 0x3ffb8
>> +  # This can speed up the Variable Dispatch a bit.
>> +  0xB8, 0xFF, 0x03, 0x00,
>> +  # FORMATTED: 0x5A #HEALTHY: 0xFE #Reserved: UINT16 #Reserved1: UINT32
>> +  0x5A, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
>> +}
>> +
>> +0x00040000|0x00040000
>> +#NV_FTW_WORKING
>> +DATA = {
>> +  # EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER->Signature = 
>> gEdkiiWorkingBlockSignatureGuid         =
>> +  #  { 0x9e58292b, 0x7c68, 0x497d, { 0xa0, 0xce, 0x65,  0x0, 0xfd, 
>> 0x9f, 0x1b, 0x95 }}
>> +  0x2b, 0x29, 0x58, 0x9e, 0x68, 0x7c, 0x7d, 0x49,
>> +  0xa0, 0xce, 0x65,  0x0, 0xfd, 0x9f, 0x1b, 0x95,
>> +  # Crc:UINT32            #WorkingBlockValid:1, 
>> WorkingBlockInvalid:1, Reserved
>> +  0x5b, 0xe7, 0xc6, 0x86, 0xFE, 0xFF, 0xFF, 0xFF,
>> +  # WriteQueueSize: UINT64
>> +  0xE0, 0xFF, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00
>> +}
>> +
>> +0x00080000|0x00040000
>> +#NV_FTW_SPARE
>
>
> 
>
>
>


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-01-11  7:16 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-01-10  1:16 [edk2-platforms][PATCH V2 0/7] loongarch add flash device xianglai
2023-01-10  1:16 ` [edk2-platforms][PATCH V2 1/7] Platform/Loongson: add bootmode support xianglai
2023-01-10  1:16 ` [edk2-platforms][PATCH V2 2/7] Platform/Loongson:add nvme device driver for loongarch xianglai
2023-01-10  1:16 ` [edk2-platforms][PATCH V2 3/7] Platform/Loongson: Support pflash " xianglai
2023-01-10  7:12   ` [edk2-devel] " Chao Li
2023-01-11  7:16     ` xianglai
2023-01-10  1:16 ` [edk2-platforms][PATCH V2 4/7] Platform/Loongson: Modify the page table entry access priority xianglai
2023-01-10  1:16 ` [edk2-platforms][PATCH V2 5/7] Platform/Loongson: Optimize page table entry null determination xianglai
2023-01-10  1:16 ` [edk2-platforms][PATCH V2 6/7] Platform/Loongson: Optimize the huge page and page entry conversion xianglai
2023-01-10  1:16 ` [edk2-platforms][PATCH V2 7/7] Platform/Loongson: Enable zero address protection xianglai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox