From nobody Sat Jul 12 10:03:05 2025 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1503926694672726.078420123041; Mon, 28 Aug 2017 06:24:54 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 9AAD12095DCB3; Mon, 28 Aug 2017 06:22:10 -0700 (PDT) Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 8D7472095DCAE for ; Mon, 28 Aug 2017 06:22:09 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7B23A883D6; Mon, 28 Aug 2017 13:24:48 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-116-67.phx2.redhat.com [10.3.116.67]) by smtp.corp.redhat.com (Postfix) with ESMTP id E95F1614C1; Mon, 28 Aug 2017 13:24:46 +0000 (UTC) X-Original-To: edk2-devel@lists.01.org DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 7B23A883D6 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=lersek@redhat.com From: Laszlo Ersek To: edk2-devel-01 Date: Mon, 28 Aug 2017 15:24:34 +0200 Message-Id: <20170828132436.15933-5-lersek@redhat.com> In-Reply-To: <20170828132436.15933-1-lersek@redhat.com> References: <20170828132436.15933-1-lersek@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Mon, 28 Aug 2017 13:24:48 +0000 (UTC) Subject: [edk2] [PATCH 4/6] OvmfPkg/VirtioGpuDxe: helpers for backing store (de)allocation+(un)mapping X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jordan Justen , Tom Lendacky , Ard Biesheuvel MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Introduce the VirtioGpuAllocateZeroAndMapBackingStore() and VirtioGpuUnmapAndFreeBackingStore() helper functions. These functions tie together the allocation, zeroing and mapping, and unmapping and deallocation, respectively, of memory that the virtio GPU will permanently reference after receiving the RESOURCE_ATTACH_BACKING command. With these functions we can keep the next patch simpler -- the GOP implementation in "Gop.c" retains its error handling structure, and remains oblivious to VIRTIO_DEVICE_PROTOCOL and VirtioLib. Cc: Ard Biesheuvel Cc: Brijesh Singh Cc: Jordan Justen Cc: Tom Lendacky Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Laszlo Ersek --- OvmfPkg/VirtioGpuDxe/VirtioGpu.h | 68 +++++++++++ OvmfPkg/VirtioGpuDxe/Commands.c | 120 ++++++++++++++++++++ OvmfPkg/VirtioGpuDxe/DriverBinding.c | 1 - OvmfPkg/VirtioGpuDxe/Gop.c | 1 - 4 files changed, 188 insertions(+), 2 deletions(-) diff --git a/OvmfPkg/VirtioGpuDxe/VirtioGpu.h b/OvmfPkg/VirtioGpuDxe/Virtio= Gpu.h index cf2a63accd72..65b1bd6088b8 100644 --- a/OvmfPkg/VirtioGpuDxe/VirtioGpu.h +++ b/OvmfPkg/VirtioGpuDxe/VirtioGpu.h @@ -1,28 +1,29 @@ /** @file =20 Internal type and macro definitions for the Virtio GPU hybrid driver. =20 Copyright (C) 2016, Red Hat, Inc. =20 This program and the accompanying materials are licensed and made availa= ble under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php =20 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WI= THOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. =20 **/ =20 #ifndef _VIRTIO_GPU_DXE_H_ #define _VIRTIO_GPU_DXE_H_ =20 #include +#include #include #include #include #include =20 // // Forward declaration of VGPU_GOP. // @@ -188,17 +189,84 @@ VOID VirtioGpuUninit ( IN OUT VGPU_DEV *VgpuDev ); =20 +/** + Allocate, zero and map memory, for bus master common buffer operation, t= o be + attached as backing store to a host-side VirtIo GPU resource. + + @param[in] VgpuDev The VGPU_DEV object that represents the VirtI= o GPU + device. + + @param[in] NumberOfPages The number of whole pages to allocate and map. + + @param[out] HostAddress The system memory address of the allocated ar= ea. + + @param[out] DeviceAddress The bus master device address of the allocated + area. The VirtIo GPU device may be programmed= to + access the allocated area through DeviceAddre= ss; + DeviceAddress is to be passed to the + VirtioGpuResourceAttachBacking() function, as= the + BackingStoreDeviceAddress parameter. + + @param[out] Mapping A resulting token to pass to + VirtioGpuUnmapAndFreeBackingStore(). + + @retval EFI_SUCCESS The requested number of pages has been allocated, z= eroed + and mapped. + + @return Status codes propagated from + VgpuDev->VirtIo->AllocateSharedPages() and + VirtioMapAllBytesInSharedBuffer(). +**/ +EFI_STATUS +VirtioGpuAllocateZeroAndMapBackingStore ( + IN VGPU_DEV *VgpuDev, + IN UINTN NumberOfPages, + OUT VOID **HostAddress, + OUT EFI_PHYSICAL_ADDRESS *DeviceAddress, + OUT VOID **Mapping + ); + +/** + Unmap and free memory originally allocated and mapped with + VirtioGpuAllocateZeroAndMapBackingStore(). + + If the memory allocated and mapped with + VirtioGpuAllocateZeroAndMapBackingStore() was attached to a host-side Vi= rtIo + GPU resource with VirtioGpuResourceAttachBacking(), then the caller is + responsible for detaching the backing store from the same resource, with + VirtioGpuResourceDetachBacking(), before calling this function. + + @param[in] VgpuDev The VGPU_DEV object that represents the VirtIo= GPU + device. + + @param[in] NumberOfPages The NumberOfPages parameter originally passed = to + VirtioGpuAllocateZeroAndMapBackingStore(). + + @param[in] HostAddress The HostAddress value originally output by + VirtioGpuAllocateZeroAndMapBackingStore(). + + @param[in] Mapping The token that was originally output by + VirtioGpuAllocateZeroAndMapBackingStore(). +**/ +VOID +VirtioGpuUnmapAndFreeBackingStore ( + IN VGPU_DEV *VgpuDev, + IN UINTN NumberOfPages, + IN VOID *HostAddress, + IN VOID *Mapping + ); + /** EFI_EVENT_NOTIFY function for the VGPU_DEV.ExitBoot event. It resets the VirtIo device, causing it to release its resources and to forget its configuration. =20 This function may only be called (that is, VGPU_DEV.ExitBoot may only be signaled) after VirtioGpuInit() returns and before VirtioGpuUninit() is called. =20 @param[in] Event Event whose notification function is being invoked. =20 @param[in] Context Pointer to the associated VGPU_DEV object. **/ diff --git a/OvmfPkg/VirtioGpuDxe/Commands.c b/OvmfPkg/VirtioGpuDxe/Command= s.c index c1951a807e98..595a3717d926 100644 --- a/OvmfPkg/VirtioGpuDxe/Commands.c +++ b/OvmfPkg/VirtioGpuDxe/Commands.c @@ -201,26 +201,146 @@ VOID VirtioGpuUninit ( IN OUT VGPU_DEV *VgpuDev ) { // // Resetting the VirtIo device makes it release its resources and forget= its // configuration. // VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, 0); VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, VgpuDev->RingMap); VirtioRingUninit (VgpuDev->VirtIo, &VgpuDev->Ring); } =20 +/** + Allocate, zero and map memory, for bus master common buffer operation, t= o be + attached as backing store to a host-side VirtIo GPU resource. + + @param[in] VgpuDev The VGPU_DEV object that represents the VirtI= o GPU + device. + + @param[in] NumberOfPages The number of whole pages to allocate and map. + + @param[out] HostAddress The system memory address of the allocated ar= ea. + + @param[out] DeviceAddress The bus master device address of the allocated + area. The VirtIo GPU device may be programmed= to + access the allocated area through DeviceAddre= ss; + DeviceAddress is to be passed to the + VirtioGpuResourceAttachBacking() function, as= the + BackingStoreDeviceAddress parameter. + + @param[out] Mapping A resulting token to pass to + VirtioGpuUnmapAndFreeBackingStore(). + + @retval EFI_SUCCESS The requested number of pages has been allocated, z= eroed + and mapped. + + @return Status codes propagated from + VgpuDev->VirtIo->AllocateSharedPages() and + VirtioMapAllBytesInSharedBuffer(). +**/ +EFI_STATUS +VirtioGpuAllocateZeroAndMapBackingStore ( + IN VGPU_DEV *VgpuDev, + IN UINTN NumberOfPages, + OUT VOID **HostAddress, + OUT EFI_PHYSICAL_ADDRESS *DeviceAddress, + OUT VOID **Mapping + ) +{ + EFI_STATUS Status; + VOID *NewHostAddress; + + Status =3D VgpuDev->VirtIo->AllocateSharedPages ( + VgpuDev->VirtIo, + NumberOfPages, + &NewHostAddress + ); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Avoid exposing stale data to the device even temporarily: zero the ar= ea + // before mapping it. + // + ZeroMem (NewHostAddress, EFI_PAGES_TO_SIZE (NumberOfPages)); + + Status =3D VirtioMapAllBytesInSharedBuffer ( + VgpuDev->VirtIo, // VirtIo + VirtioOperationBusMasterCommonBuffer, // Operation + NewHostAddress, // HostAddress + EFI_PAGES_TO_SIZE (NumberOfPages), // NumberOfBytes + DeviceAddress, // DeviceAddress + Mapping // Mapping + ); + if (EFI_ERROR (Status)) { + goto FreeSharedPages; + } + + *HostAddress =3D NewHostAddress; + return EFI_SUCCESS; + +FreeSharedPages: + VgpuDev->VirtIo->FreeSharedPages ( + VgpuDev->VirtIo, + NumberOfPages, + NewHostAddress + ); + return Status; +} + +/** + Unmap and free memory originally allocated and mapped with + VirtioGpuAllocateZeroAndMapBackingStore(). + + If the memory allocated and mapped with + VirtioGpuAllocateZeroAndMapBackingStore() was attached to a host-side Vi= rtIo + GPU resource with VirtioGpuResourceAttachBacking(), then the caller is + responsible for detaching the backing store from the same resource, with + VirtioGpuResourceDetachBacking(), before calling this function. + + @param[in] VgpuDev The VGPU_DEV object that represents the VirtIo= GPU + device. + + @param[in] NumberOfPages The NumberOfPages parameter originally passed = to + VirtioGpuAllocateZeroAndMapBackingStore(). + + @param[in] HostAddress The HostAddress value originally output by + VirtioGpuAllocateZeroAndMapBackingStore(). + + @param[in] Mapping The token that was originally output by + VirtioGpuAllocateZeroAndMapBackingStore(). +**/ +VOID +VirtioGpuUnmapAndFreeBackingStore ( + IN VGPU_DEV *VgpuDev, + IN UINTN NumberOfPages, + IN VOID *HostAddress, + IN VOID *Mapping + ) +{ + VgpuDev->VirtIo->UnmapSharedBuffer ( + VgpuDev->VirtIo, + Mapping + ); + VgpuDev->VirtIo->FreeSharedPages ( + VgpuDev->VirtIo, + NumberOfPages, + HostAddress + ); +} + /** EFI_EVENT_NOTIFY function for the VGPU_DEV.ExitBoot event. It resets the VirtIo device, causing it to release its resources and to forget its configuration. =20 This function may only be called (that is, VGPU_DEV.ExitBoot may only be signaled) after VirtioGpuInit() returns and before VirtioGpuUninit() is called. =20 @param[in] Event Event whose notification function is being invoked. =20 @param[in] Context Pointer to the associated VGPU_DEV object. **/ diff --git a/OvmfPkg/VirtioGpuDxe/DriverBinding.c b/OvmfPkg/VirtioGpuDxe/Dr= iverBinding.c index 33c1ad3b3110..a44d52cc810b 100644 --- a/OvmfPkg/VirtioGpuDxe/DriverBinding.c +++ b/OvmfPkg/VirtioGpuDxe/DriverBinding.c @@ -1,38 +1,37 @@ /** @file =20 Implement the Driver Binding Protocol and the Component Name 2 Protocol = for the Virtio GPU hybrid driver. =20 Copyright (C) 2016, Red Hat, Inc. =20 This program and the accompanying materials are licensed and made availa= ble under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php =20 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WI= THOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. =20 **/ =20 -#include #include #include #include #include #include #include #include #include #include =20 #include "VirtioGpu.h" =20 // // The device path node that describes the Video Output Device Attributes = for // the single head (UEFI child handle) that we support. // // The ACPI_DISPLAY_ADR() macro corresponds to Table B-2, section "B.4.2 _= DOD" // in the ACPI 3.0b spec, or more recently, to Table B-379, section "B.3.2 // _DOD" in the ACPI 6.0 spec. // diff --git a/OvmfPkg/VirtioGpuDxe/Gop.c b/OvmfPkg/VirtioGpuDxe/Gop.c index b3c5dae74d0e..507e1a770d10 100644 --- a/OvmfPkg/VirtioGpuDxe/Gop.c +++ b/OvmfPkg/VirtioGpuDxe/Gop.c @@ -1,44 +1,43 @@ /** @file =20 EFI_GRAPHICS_OUTPUT_PROTOCOL member functions for the VirtIo GPU driver. =20 Copyright (C) 2016, Red Hat, Inc. =20 This program and the accompanying materials are licensed and made availa= ble under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php =20 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WI= THOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. =20 **/ =20 -#include #include =20 #include "VirtioGpu.h" =20 /** Release guest-side and host-side resources that are related to an initia= lized VGPU_GOP.Gop. =20 param[in,out] VgpuGop The VGPU_GOP object to release resources for. =20 On input, the caller is responsible for having ca= lled VgpuGop->Gop.SetMode() at least once successfully. (This is equivalent to the requirement that VgpuGop->BackingStore be non-NULL. It is also equivalent to the requirement that VgpuGop->Resou= rceId be nonzero.) =20 On output, resources will be released, and VgpuGop->BackingStore and VgpuGop->ResourceId wil= l be nulled. =20 param[in] DisableHead Whether this head (scanout) currently references = the resource identified by VgpuGop->ResourceId. Only = pass FALSE when VgpuGop->Gop.SetMode() calls this func= tion while switching between modes, and set it to TRUE every other time. **/ --=20 2.14.1.3.gb7cf6e02401b _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel