From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web08.693.1666630909256629378 for ; Mon, 24 Oct 2022 10:01:49 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ne5OglpW; spf=pass (domain: kernel.org, ip: 139.178.84.217, mailfrom: ardb@kernel.org) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A4A5C611DD; Mon, 24 Oct 2022 17:01:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BCDF6C43470; Mon, 24 Oct 2022 17:01:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666630908; bh=x7yNc9oyHHia5ONKYu6OutfCsYEdxdrG5F1IY0GIcfQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ne5OglpWr+1ERf+g/7826r1G1VdDft/AmyqvGaW3WJ4tx5buT1HYqWCJoSH5jac79 L/fpnCyyUyzlXnAbQA7QZ6JSt2nJkpziLX/p36QG6oHqZdpIkik88+bzA6Zt6x8eOT Uoj0yTFDrAwdm8bXpcnymn9E90WpTc7vNg62MAXTbjtyAfdL64PBu9ubaIdKcRVUl0 +43bJemgR1VESfatRrLtbiG3/ZJRhatmhQAT4U3e19Urw1h4VvPDm0jUZlbFlxelmu SroQeWVpLdkbFy9c63zLpkebJy2EWSaFHCEHP4G5nic4adePKeDMMgmF3sr4eovvvr 1k+2xAZJ3otNg== From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Gerd Hoffmann , Sunil V L , Sami Mujawar , Leif Lindholm Subject: [PATCH 08/11] OvmfPkg/VirtNorFlashDxe: use EFI_MEMORY_WC and drop AlignedCopyMem() Date: Mon, 24 Oct 2022 19:01:19 +0200 Message-Id: <20221024170122.594577-9-ardb@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221024170122.594577-1-ardb@kernel.org> References: <20221024170122.594577-1-ardb@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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 --- OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c | 65 +------------------- OvmfPkg/VirtNorFlashDxe/VirtNorFlashDxe.c | 4 +- 2 files changed, 4 insertions(+), 65 deletions(-) diff --git a/OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c b/OvmfPkg/VirtNorFlashD= xe/VirtNorFlash.c index d87cdd49d6a6..392e17988472 100644 --- a/OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c +++ b/OvmfPkg/VirtNorFlashDxe/VirtNorFlash.c @@ -401,67 +401,6 @@ NorFlashWriteBlocks ( return Status;=0D }=0D =0D -#define BOTH_ALIGNED(a, b, align) ((((UINTN)(a) | (UINTN)(b)) & ((align) = - 1)) =3D=3D 0)=0D -=0D -/**=0D - Copy Length bytes from Source to Destination, using aligned accesses onl= y.=0D - Note that this implementation uses memcpy() semantics rather then memmov= e()=0D - semantics, i.e., SourceBuffer and DestinationBuffer should not overlap.= =0D -=0D - @param DestinationBuffer The target of the copy request.=0D - @param SourceBuffer The place to copy from.=0D - @param Length The number of bytes to copy.=0D -=0D - @return Destination=0D -=0D -**/=0D -STATIC=0D -VOID *=0D -AlignedCopyMem (=0D - OUT VOID *DestinationBuffer,=0D - IN CONST VOID *SourceBuffer,=0D - IN UINTN Length=0D - )=0D -{=0D - UINT8 *Destination8;=0D - CONST UINT8 *Source8;=0D - UINT32 *Destination32;=0D - CONST UINT32 *Source32;=0D - UINT64 *Destination64;=0D - CONST UINT64 *Source64;=0D -=0D - if (BOTH_ALIGNED (DestinationBuffer, SourceBuffer, 8) && (Length >=3D 8)= ) {=0D - Destination64 =3D DestinationBuffer;=0D - Source64 =3D SourceBuffer;=0D - while (Length >=3D 8) {=0D - *Destination64++ =3D *Source64++;=0D - Length -=3D 8;=0D - }=0D -=0D - Destination8 =3D (UINT8 *)Destination64;=0D - Source8 =3D (CONST UINT8 *)Source64;=0D - } else if (BOTH_ALIGNED (DestinationBuffer, SourceBuffer, 4) && (Length = >=3D 4)) {=0D - Destination32 =3D DestinationBuffer;=0D - Source32 =3D SourceBuffer;=0D - while (Length >=3D 4) {=0D - *Destination32++ =3D *Source32++;=0D - Length -=3D 4;=0D - }=0D -=0D - Destination8 =3D (UINT8 *)Destination32;=0D - Source8 =3D (CONST UINT8 *)Source32;=0D - } else {=0D - Destination8 =3D DestinationBuffer;=0D - Source8 =3D SourceBuffer;=0D - }=0D -=0D - while (Length-- !=3D 0) {=0D - *Destination8++ =3D *Source8++;=0D - }=0D -=0D - return DestinationBuffer;=0D -}=0D -=0D EFI_STATUS=0D NorFlashReadBlocks (=0D IN NOR_FLASH_INSTANCE *Instance,=0D @@ -516,7 +455,7 @@ NorFlashReadBlocks ( SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);=0D =0D // Readout the data=0D - AlignedCopyMem (Buffer, (VOID *)StartAddress, BufferSizeInBytes);=0D + CopyMem (Buffer, (VOID *)StartAddress, BufferSizeInBytes);=0D =0D return EFI_SUCCESS;=0D }=0D @@ -558,7 +497,7 @@ NorFlashRead ( SEND_NOR_COMMAND (Instance->DeviceBaseAddress, 0, P30_CMD_READ_ARRAY);=0D =0D // Readout the data=0D - AlignedCopyMem (Buffer, (VOID *)(StartAddress + Offset), BufferSizeInByt= es);=0D + CopyMem (Buffer, (VOID *)(StartAddress + Offset), BufferSizeInBytes);=0D =0D return EFI_SUCCESS;=0D }=0D diff --git a/OvmfPkg/VirtNorFlashDxe/VirtNorFlashDxe.c b/OvmfPkg/VirtNorFla= shDxe/VirtNorFlashDxe.c index f9a41f6aab0f..ff3121af2a40 100644 --- a/OvmfPkg/VirtNorFlashDxe/VirtNorFlashDxe.c +++ b/OvmfPkg/VirtNorFlashDxe/VirtNorFlashDxe.c @@ -394,14 +394,14 @@ NorFlashFvbInitialize ( EfiGcdMemoryTypeMemoryMappedIo,=0D Instance->DeviceBaseAddress,=0D RuntimeMmioRegionSize,=0D - EFI_MEMORY_UC | EFI_MEMORY_RUNTIME=0D + EFI_MEMORY_WC | EFI_MEMORY_RUNTIME=0D );=0D ASSERT_EFI_ERROR (Status);=0D =0D Status =3D gDS->SetMemorySpaceAttributes (=0D Instance->DeviceBaseAddress,=0D RuntimeMmioRegionSize,=0D - EFI_MEMORY_UC | EFI_MEMORY_RUNTIME=0D + EFI_MEMORY_WC | EFI_MEMORY_RUNTIME=0D );=0D ASSERT_EFI_ERROR (Status);=0D =0D --=20 2.35.1