From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from loongson.cn (loongson.cn [114.242.206.163]) by mx.groups.io with SMTP id smtpd.web10.158257.1673839369841906593 for ; Sun, 15 Jan 2023 19:22:50 -0800 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: loongson.cn, ip: 114.242.206.163, mailfrom: lichao@loongson.cn) Received: from loongson.cn (unknown [10.40.24.149]) by gateway (Coremail) with SMTP id _____8AxmOkIw8RjK8wBAA--.1726S3; Mon, 16 Jan 2023 11:22:48 +0800 (CST) Received: from [10.40.24.149] (unknown [10.40.24.149]) by localhost.localdomain (Coremail) with SMTP id AQAAf8BxTL4Iw8Rj5AUaAA--.49177S3; Mon, 16 Jan 2023 11:22:48 +0800 (CST) Message-ID: Date: Mon, 16 Jan 2023 11:22:48 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: Re: [edk2-devel] [edk2-platforms][PATCH V4 3/8] Platform/Loongson: Add pflash driver. To: devel@edk2.groups.io, lixianglai@loongson.cn Cc: Ard Biesheuvel , Bibo Mao , Leif Lindholm , Liming Gao , Michael D Kinney References: <1d99a25fdce36a0fe1dd51405d004f3392a3b43d.1673579388.git.lixianglai@loongson.cn> From: "Chao Li" In-Reply-To: <1d99a25fdce36a0fe1dd51405d004f3392a3b43d.1673579388.git.lixianglai@loongson.cn> X-CM-TRANSID: AQAAf8BxTL4Iw8Rj5AUaAA--.49177S3 X-CM-SenderInfo: xolfxt3r6o00pqjv00gofq/1tbiAQANCGPD7O0FjwACst X-Coremail-Antispam: 1Uk129KBjvJXoW3Gr1ftr15Zr4kWF43Zw45Awb_yoWxWF4kpa 1UXaykKwsrXFW3twsxA3WUXr43AFW3C39xJ39FqF1DC3ZrKFnIkw1jvr1rJFsrXry8Xr1j q3y09ayDW3WDXw7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj DUYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUUba8YFVCjjxCrM7AC8VAFwI0_Jr0_ Gr1l1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s1l1IIY67AEw4v_Jrv_JF1l8cAvFV AK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2 z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwA2z4x0Y4vEx4A2jsIE14v26r4UJVWxJr 1l84ACjcxK6I8E87Iv6xkF7I0E14v26r4UJVWxJr1le2I262IYc4CY6c8Ij28IcVAaY2xG 8wAqjxCEc2xF0cIa020Ex4CE44I27wAv7VC0I7IYx2IY67AKxVWUtVWrXwAv7VC2z280aV AFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcVAKI48JMx8GjcxK6IxK0xII j40E5I8CrwCYjI0SjxkI62AI1cAE67vIY487MxAIw28IcxkI7VAKI48JMxC20s026xCaFV Cjc4AY6r1j6r4UMxCIbckI1I0E14v26r1q6r43MI8I3I0E5I8CrVAFwI0_JrI_JrWlx2Iq xVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42 IY6xIIjxv20xvE14v26r4j6ryUMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY 6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aV CY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IU8IksDUUUUU== Content-Type: multipart/alternative; boundary="------------j5TlvOqerV7D9FZqtsOvGVZc" --------------j5TlvOqerV7D9FZqtsOvGVZc Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Reviewed-by: Chao Li Thanks, Chao 在 2023/1/13 11:17, xianglai 写道: > Add pflash driver for loongarch. > > Cc: Ard Biesheuvel > Cc: Bibo Mao > Cc: Chao Li > Cc: Leif Lindholm > Cc: Liming Gao > Cc: Michael D Kinney > Signed-off-by: xianglai li > --- > .../Library/NorFlashQemuLib/NorFlashQemuLib.c | 141 ++++++++++++++++++ > .../NorFlashQemuLib/NorFlashQemuLib.inf | 43 ++++++ > 2 files changed, 184 insertions(+) > create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c > create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf > > 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.
> + > + SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > + > +#include > +#include > +#include > +#include > + > +#include > + > +#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.
> +# > +# 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 --------------j5TlvOqerV7D9FZqtsOvGVZc Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

Reviewed-by: Chao Li <lichao@loongson.cn>


Thanks,
Chao
在 2023/1/13 11:17, 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 ++++++
 2 files changed, 184 insertions(+)
 create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.c
 create mode 100644 Platform/Loongson/LoongArchQemuPkg/Library/NorFlashQemuLib/NorFlashQemuLib.inf

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
--------------j5TlvOqerV7D9FZqtsOvGVZc--