]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg/VirtNorFlashDxe: use EFI_MEMORY_WC and drop AlignedCopyMem()
authorArd Biesheuvel <ardb@kernel.org>
Mon, 24 Oct 2022 16:16:18 +0000 (18:16 +0200)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Thu, 27 Oct 2022 16:52:01 +0000 (16:52 +0000)
NOR flash emulation under KVM involves switching between two modes,
where array mode is backed by a read-only memslot, and programming mode
is fully emulated, i.e., the memory region is not backed by anything,
and the faulting accesses are forwarded to the VMM by the hypervisor,
which translates them into NOR flash programming commands.

Normally, we are limited to the use of device attributes when mapping
such regions, given that the programming mode has MMIO semantics.
However, when running under KVM, the chosen memory attributes only take
effect when in array mode, since no memory mapping exists otherwise.

This means we can tune the memory mapping so it behaves a bit more like
a ROM, by switching to EFI_MEMORY_WC attributes. This means we no longer
need a special CopyMem() implementation that avoids unaligned accesses
at all cost.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Reviewed-by: Sunil V L <sunilvl@ventanamicro.com>
OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c
OvmfPkg/VirtNorFlashDxe/VirtNorFlashDxe.c

index 0343131a5448acd5d8d78a360c77a17ea62dc94b..1afd60ce66eb1b82216ea3386a51cf0bdc337570 100644 (file)
@@ -401,67 +401,6 @@ NorFlashWriteBlocks (
   return Status;\r
 }\r
 \r
-#define BOTH_ALIGNED(a, b, align)  ((((UINTN)(a) | (UINTN)(b)) & ((align) - 1)) == 0)\r
-\r
-/**\r
-  Copy Length bytes from Source to Destination, using aligned accesses only.\r
-  Note that this implementation uses memcpy() semantics rather then memmove()\r
-  semantics, i.e., SourceBuffer and DestinationBuffer should not overlap.\r
-\r
-  @param  DestinationBuffer The target of the copy request.\r
-  @param  SourceBuffer      The place to copy from.\r
-  @param  Length            The number of bytes to copy.\r
-\r
-  @return Destination\r
-\r
-**/\r
-STATIC\r
-VOID *\r
-AlignedCopyMem (\r
-  OUT     VOID        *DestinationBuffer,\r
-  IN      CONST VOID  *SourceBuffer,\r
-  IN      UINTN       Length\r
-  )\r
-{\r
-  UINT8         *Destination8;\r
-  CONST UINT8   *Source8;\r
-  UINT32        *Destination32;\r
-  CONST UINT32  *Source32;\r
-  UINT64        *Destination64;\r
-  CONST UINT64  *Source64;\r
-\r
-  if (BOTH_ALIGNED (DestinationBuffer, SourceBuffer, 8) && (Length >= 8)) {\r
-    Destination64 = DestinationBuffer;\r
-    Source64      = SourceBuffer;\r
-    while (Length >= 8) {\r
-      *Destination64++ = *Source64++;\r
-      Length          -= 8;\r
-    }\r
-\r
-    Destination8 = (UINT8 *)Destination64;\r
-    Source8      = (CONST UINT8 *)Source64;\r
-  } else if (BOTH_ALIGNED (DestinationBuffer, SourceBuffer, 4) && (Length >= 4)) {\r
-    Destination32 = DestinationBuffer;\r
-    Source32      = SourceBuffer;\r
-    while (Length >= 4) {\r
-      *Destination32++ = *Source32++;\r
-      Length          -= 4;\r
-    }\r
-\r
-    Destination8 = (UINT8 *)Destination32;\r
-    Source8      = (CONST UINT8 *)Source32;\r
-  } else {\r
-    Destination8 = DestinationBuffer;\r
-    Source8      = SourceBuffer;\r
-  }\r
-\r
-  while (Length-- != 0) {\r
-    *Destination8++ = *Source8++;\r
-  }\r
-\r
-  return DestinationBuffer;\r
-}\r
-\r
 EFI_STATUS\r
 NorFlashReadBlocks (\r
   IN NOR_FLASH_INSTANCE  *Instance,\r
@@ -516,7 +455,7 @@ NorFlashReadBlocks (
   SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);\r
 \r
   // Readout the data\r
-  AlignedCopyMem (Buffer, (VOID *)StartAddress, BufferSizeInBytes);\r
+  CopyMem (Buffer, (VOID *)StartAddress, BufferSizeInBytes);\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -558,7 +497,7 @@ NorFlashRead (
   SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);\r
 \r
   // Readout the data\r
-  AlignedCopyMem (Buffer, (VOID *)(StartAddress + Offset), BufferSizeInBytes);\r
+  CopyMem (Buffer, (VOID *)(StartAddress + Offset), BufferSizeInBytes);\r
 \r
   return EFI_SUCCESS;\r
 }\r
index f9a41f6aab0f9189a625fc02501e068e732945cb..ff3121af2a4003b6f84ea4cfd202c9bb0157b3f7 100644 (file)
@@ -394,14 +394,14 @@ NorFlashFvbInitialize (
                   EfiGcdMemoryTypeMemoryMappedIo,\r
                   Instance->DeviceBaseAddress,\r
                   RuntimeMmioRegionSize,\r
-                  EFI_MEMORY_UC | EFI_MEMORY_RUNTIME\r
+                  EFI_MEMORY_WC | EFI_MEMORY_RUNTIME\r
                   );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
   Status = gDS->SetMemorySpaceAttributes (\r
                   Instance->DeviceBaseAddress,\r
                   RuntimeMmioRegionSize,\r
-                  EFI_MEMORY_UC | EFI_MEMORY_RUNTIME\r
+                  EFI_MEMORY_WC | EFI_MEMORY_RUNTIME\r
                   );\r
   ASSERT_EFI_ERROR (Status);\r
 \r