From nobody Sun May 5 08:12:58 2024 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 1503926686365455.26386649736196; Mon, 28 Aug 2017 06:24:46 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id D99C62095DC9F; Mon, 28 Aug 2017 06:22:04 -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 8DA6721DF809B for ; Mon, 28 Aug 2017 06:22:03 -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 8BE7E91FC3; Mon, 28 Aug 2017 13:24:42 +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 2FB166061E; Mon, 28 Aug 2017 13:24:41 +0000 (UTC) X-Original-To: edk2-devel@lists.01.org DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 8BE7E91FC3 Authentication-Results: ext-mx05.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx05.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:31 +0200 Message-Id: <20170828132436.15933-2-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.29]); Mon, 28 Aug 2017 13:24:42 +0000 (UTC) Subject: [edk2] [PATCH 1/6] OvmfPkg/VirtioGpuDxe: map VRING for bus master common buffer operation 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" VirtioGpuDxe uses one virtio ring, for VIRTIO_GPU_CONTROL_QUEUE. Map it for bus master common buffer operation with VirtioRingMap(), so that it can be accessed equally by both guest and hypervisor even if an IOMMU is used. (VirtioRingInit() already allocates the ring suitably for this, see commit b0338c53297c, "OvmfPkg/VirtioLib: alloc VRING buffer with AllocateSharedPages()", 2017-08-23). Pass the resultant translation offset ("RingBaseShift"), from system memory address to bus master device address, to VIRTIO_SET_QUEUE_ADDRESS. Unmap the ring in all contexts where the ring becomes unused (these contexts are mutually exclusive): - in VirtioGpuInit(): the ring has been mapped, but we cannot complete the virtio initialization for another reason, - in VirtioGpuUninit(): the virtio initialization has succeeded, but VirtioGpuDriverBindingStart() fails for another reason, or VirtioGpuDriverBindingStop() unbinds the device after use, - in VirtioGpuExitBoot(): ExitBootServices() is called after VirtioGpuDriverBindingStart() has successfully bound the device. (Unmapping the ring does not change the UEFI memory map.) Cc: Ard Biesheuvel Cc: Brijesh Singh Cc: Jordan Justen Cc: Tom Lendacky Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Laszlo Ersek Tested-By: Brijesh Singh --- OvmfPkg/VirtioGpuDxe/VirtioGpu.h | 6 +++++ OvmfPkg/VirtioGpuDxe/Commands.c | 27 +++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/OvmfPkg/VirtioGpuDxe/VirtioGpu.h b/OvmfPkg/VirtioGpuDxe/Virtio= Gpu.h index 078b7d44d83e..193e932e1430 100644 --- a/OvmfPkg/VirtioGpuDxe/VirtioGpu.h +++ b/OvmfPkg/VirtioGpuDxe/VirtioGpu.h @@ -40,46 +40,52 @@ typedef struct VGPU_GOP_STRUCT VGPU_GOP; typedef struct { // // VirtIo represents access to the Virtio GPU device. Never NULL. // VIRTIO_DEVICE_PROTOCOL *VirtIo; =20 // // BusName carries a customized name for // EFI_COMPONENT_NAME2_PROTOCOL.GetControllerName(). It is expressed in = table // form because it can theoretically support several languages. Never NU= LL. // EFI_UNICODE_STRING_TABLE *BusName; =20 // // VirtIo ring used for VirtIo communication. // VRING Ring; =20 + // + // Token associated with Ring's mapping for bus master common buffer + // operation, from VirtioRingMap(). + // + VOID *RingMap; + // // Event to be signaled at ExitBootServices(). // EFI_EVENT ExitBoot; =20 // // Common running counter for all VirtIo GPU requests that ask for fenci= ng. // UINT64 FenceId; =20 // // The Child field references the GOP wrapper structure. If this pointer= is // NULL, then the hybrid driver has bound (i.e., started) the // VIRTIO_DEVICE_PROTOCOL controller without producing the child GOP // controller (that is, after Start() was called with RemainingDevicePath // pointing to and End of Device Path node). Child can be created and // destroyed, even repeatedly, independently of VGPU_DEV. // // In practice, this field represents the single head (scanout) that we // support. // VGPU_GOP *Child; } VGPU_DEV; =20 // // The Graphics Output Protocol wrapper structure. // #define VGPU_GOP_SIG SIGNATURE_64 ('V', 'G', 'P', 'U', '_', 'G', 'O', 'P') diff --git a/OvmfPkg/VirtioGpuDxe/Commands.c b/OvmfPkg/VirtioGpuDxe/Command= s.c index 5cb003161207..4e19bac606ee 100644 --- a/OvmfPkg/VirtioGpuDxe/Commands.c +++ b/OvmfPkg/VirtioGpuDxe/Commands.c @@ -39,119 +39,138 @@ EFI_STATUS VirtioGpuInit ( IN OUT VGPU_DEV *VgpuDev ) { UINT8 NextDevStat; EFI_STATUS Status; UINT64 Features; UINT16 QueueSize; + UINT64 RingBaseShift; =20 // // Execute virtio-v1.0-cs04, 3.1.1 Driver Requirements: Device // Initialization. // // 1. Reset the device. // NextDevStat =3D 0; Status =3D VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevSta= t); if (EFI_ERROR (Status)) { goto Failed; } =20 // // 2. Set the ACKNOWLEDGE status bit [...] // NextDevStat |=3D VSTAT_ACK; Status =3D VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevSta= t); if (EFI_ERROR (Status)) { goto Failed; } =20 // // 3. Set the DRIVER status bit [...] // NextDevStat |=3D VSTAT_DRIVER; Status =3D VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevSta= t); if (EFI_ERROR (Status)) { goto Failed; } =20 // // 4. Read device feature bits... // Status =3D VgpuDev->VirtIo->GetDeviceFeatures (VgpuDev->VirtIo, &Feature= s); if (EFI_ERROR (Status)) { goto Failed; } if ((Features & VIRTIO_F_VERSION_1) =3D=3D 0) { Status =3D EFI_UNSUPPORTED; goto Failed; } // // We only want the most basic 2D features. // Features &=3D VIRTIO_F_VERSION_1; =20 // // ... and write the subset of feature bits understood by the [...] driv= er to // the device. [...] // 5. Set the FEATURES_OK status bit. // 6. Re-read device status to ensure the FEATURES_OK bit is still set [= ...] // Status =3D Virtio10WriteFeatures (VgpuDev->VirtIo, Features, &NextDevSta= t); if (EFI_ERROR (Status)) { goto Failed; } =20 // // 7. Perform device-specific setup, including discovery of virtqueues f= or // the device [...] // Status =3D VgpuDev->VirtIo->SetQueueSel (VgpuDev->VirtIo, VIRTIO_GPU_CONTROL_QUEUE); if (EFI_ERROR (Status)) { goto Failed; } Status =3D VgpuDev->VirtIo->GetQueueNumMax (VgpuDev->VirtIo, &QueueSize); if (EFI_ERROR (Status)) { goto Failed; } =20 // // We implement each VirtIo GPU command that we use with two descriptors: // request, response. // if (QueueSize < 2) { Status =3D EFI_UNSUPPORTED; goto Failed; } =20 // // [...] population of virtqueues [...] // Status =3D VirtioRingInit (VgpuDev->VirtIo, QueueSize, &VgpuDev->Ring); if (EFI_ERROR (Status)) { goto Failed; } + // + // If anything fails from here on, we have to release the ring. + // + Status =3D VirtioRingMap ( + VgpuDev->VirtIo, + &VgpuDev->Ring, + &RingBaseShift, + &VgpuDev->RingMap + ); + if (EFI_ERROR (Status)) { + goto ReleaseQueue; + } + // + // If anything fails from here on, we have to unmap the ring. + // Status =3D VgpuDev->VirtIo->SetQueueAddress ( VgpuDev->VirtIo, &VgpuDev->Ring, - 0 + RingBaseShift ); if (EFI_ERROR (Status)) { - goto ReleaseQueue; + goto UnmapQueue; } =20 // // 8. Set the DRIVER_OK status bit. // NextDevStat |=3D VSTAT_DRIVER_OK; Status =3D VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevSta= t); if (EFI_ERROR (Status)) { - goto ReleaseQueue; + goto UnmapQueue; } =20 return EFI_SUCCESS; =20 +UnmapQueue: + VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, VgpuDev->RingMap); + ReleaseQueue: VirtioRingUninit (VgpuDev->VirtIo, &VgpuDev->Ring); =20 @@ -182,25 +201,26 @@ 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 /** 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. **/ @@ -209,53 +229,54 @@ EFIAPI VirtioGpuExitBoot ( IN EFI_EVENT Event, IN VOID *Context ) { VGPU_DEV *VgpuDev; =20 VgpuDev =3D Context; VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, 0); + VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, VgpuDev->RingMap); } =20 /** Internal utility function that sends a request to the VirtIo GPU device model, awaits the answer from the host, and returns a status. =20 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo G= PU device. The caller is responsible to have successfully invoked VirtioGpuInit() on VgpuDev previously, while VirtioGpuUninit() must not have been called on VgpuDev. =20 @param[in] RequestType The type of the request. The caller is responsib= le for providing a VirtioGpuCmd* RequestType which,= on success, elicits a VirtioGpuRespOkNodata response from the host. =20 @param[in] Fence Whether to enable fencing for this request. Fenc= ing forces the host to complete the command before producing a response. If Fence is TRUE, then VgpuDev->FenceId is consumed, and incremented. =20 @param[in,out] Header Pointer to the caller-allocated request object. = The request must start with VIRTIO_GPU_CONTROL_HEADE= R. This function overwrites all fields of Header be= fore submitting the request to the host: =20 - it sets Type from RequestType, =20 - it sets Flags and FenceId based on Fence, =20 - it zeroes CtxId and Padding. =20 @param[in] RequestSize Size of the entire caller-allocated request obje= ct, including the leading VIRTIO_GPU_CONTROL_HEADER. =20 @retval EFI_SUCCESS Operation successful. =20 @retval EFI_DEVICE_ERROR The host rejected the request. The host e= rror code has been logged on the EFI_D_ERROR l= evel. =20 @return Codes for unexpected errors in VirtIo messaging. **/ --=20 2.14.1.3.gb7cf6e02401b _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Sun May 5 08:12:58 2024 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 1503926688382889.1541349266197; Mon, 28 Aug 2017 06:24:48 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 215D32095DCA7; Mon, 28 Aug 2017 06:22:07 -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 D5A5021DF809B for ; Mon, 28 Aug 2017 06:22:05 -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 D42FE4ACBA; Mon, 28 Aug 2017 13:24:44 +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 7CA0860605; Mon, 28 Aug 2017 13:24:43 +0000 (UTC) X-Original-To: edk2-devel@lists.01.org DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com D42FE4ACBA Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.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:32 +0200 Message-Id: <20170828132436.15933-3-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.38]); Mon, 28 Aug 2017 13:24:45 +0000 (UTC) Subject: [edk2] [PATCH 2/6] OvmfPkg/VirtioGpuDxe: map virtio GPU command objects to device addresses 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" Every virtio GPU command used by VirtioGpuDxe is synchronous and formatted as a two-descriptor chain: request, response. The internal workhorse function that all the command-specific functions call for such messaging is VirtioGpuSendCommand(). In VirtioGpuSendCommand(), map the request from system memory to bus master device address for BusMasterRead operation, and map the response from system memory to bus master device address for BusMasterWrite operation. Pass the bus master device addresses to VirtioAppendDesc(). (See also commit 4b725858de68, "OvmfPkg/VirtioLib: change the parameter of VirtioAppendDesc() to UINT64", 2017-08-23.) Cc: Ard Biesheuvel Cc: Brijesh Singh Cc: Jordan Justen Cc: Tom Lendacky Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Laszlo Ersek Tested-By: Brijesh Singh --- OvmfPkg/VirtioGpuDxe/Commands.c | 83 ++++++++++++++++++-- 1 file changed, 75 insertions(+), 8 deletions(-) diff --git a/OvmfPkg/VirtioGpuDxe/Commands.c b/OvmfPkg/VirtioGpuDxe/Command= s.c index 4e19bac606ee..bdedea1df6a7 100644 --- a/OvmfPkg/VirtioGpuDxe/Commands.c +++ b/OvmfPkg/VirtioGpuDxe/Commands.c @@ -229,148 +229,215 @@ EFIAPI VirtioGpuExitBoot ( IN EFI_EVENT Event, IN VOID *Context ) { VGPU_DEV *VgpuDev; =20 VgpuDev =3D Context; VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, 0); VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, VgpuDev->RingMap); } =20 /** Internal utility function that sends a request to the VirtIo GPU device model, awaits the answer from the host, and returns a status. =20 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo G= PU device. The caller is responsible to have successfully invoked VirtioGpuInit() on VgpuDev previously, while VirtioGpuUninit() must not have been called on VgpuDev. =20 @param[in] RequestType The type of the request. The caller is responsib= le for providing a VirtioGpuCmd* RequestType which,= on success, elicits a VirtioGpuRespOkNodata response from the host. =20 @param[in] Fence Whether to enable fencing for this request. Fenc= ing forces the host to complete the command before producing a response. If Fence is TRUE, then VgpuDev->FenceId is consumed, and incremented. =20 @param[in,out] Header Pointer to the caller-allocated request object. = The request must start with VIRTIO_GPU_CONTROL_HEADE= R. This function overwrites all fields of Header be= fore submitting the request to the host: =20 - it sets Type from RequestType, =20 - it sets Flags and FenceId based on Fence, =20 - it zeroes CtxId and Padding. =20 @param[in] RequestSize Size of the entire caller-allocated request obje= ct, including the leading VIRTIO_GPU_CONTROL_HEADER. =20 @retval EFI_SUCCESS Operation successful. =20 @retval EFI_DEVICE_ERROR The host rejected the request. The host e= rror code has been logged on the EFI_D_ERROR l= evel. =20 @return Codes for unexpected errors in VirtIo - messaging. + messaging, or request/response + mapping/unmapping. **/ STATIC EFI_STATUS VirtioGpuSendCommand ( IN OUT VGPU_DEV *VgpuDev, IN VIRTIO_GPU_CONTROL_TYPE RequestType, IN BOOLEAN Fence, IN OUT volatile VIRTIO_GPU_CONTROL_HEADER *Header, IN UINTN RequestSize ) { DESC_INDICES Indices; volatile VIRTIO_GPU_CONTROL_HEADER Response; EFI_STATUS Status; UINT32 ResponseSize; + EFI_PHYSICAL_ADDRESS RequestDeviceAddress; + VOID *RequestMap; + EFI_PHYSICAL_ADDRESS ResponseDeviceAddress; + VOID *ResponseMap; =20 // // Initialize Header. // Header->Type =3D RequestType; if (Fence) { Header->Flags =3D VIRTIO_GPU_FLAG_FENCE; Header->FenceId =3D VgpuDev->FenceId++; } else { Header->Flags =3D 0; Header->FenceId =3D 0; } Header->CtxId =3D 0; Header->Padding =3D 0; =20 ASSERT (RequestSize >=3D sizeof *Header); ASSERT (RequestSize <=3D MAX_UINT32); =20 + // + // Map request and response to bus master device addresses. + // + Status =3D VirtioMapAllBytesInSharedBuffer ( + VgpuDev->VirtIo, + VirtioOperationBusMasterRead, + (VOID *)Header, + RequestSize, + &RequestDeviceAddress, + &RequestMap + ); + if (EFI_ERROR (Status)) { + return Status; + } + Status =3D VirtioMapAllBytesInSharedBuffer ( + VgpuDev->VirtIo, + VirtioOperationBusMasterWrite, + (VOID *)&Response, + sizeof Response, + &ResponseDeviceAddress, + &ResponseMap + ); + if (EFI_ERROR (Status)) { + goto UnmapRequest; + } + // // Compose the descriptor chain. // VirtioPrepare (&VgpuDev->Ring, &Indices); - VirtioAppendDesc (&VgpuDev->Ring, (UINTN)Header, (UINT32)RequestSize, - VRING_DESC_F_NEXT, &Indices); - VirtioAppendDesc (&VgpuDev->Ring, (UINTN)&Response, sizeof Response, - VRING_DESC_F_WRITE, &Indices); + VirtioAppendDesc ( + &VgpuDev->Ring, + RequestDeviceAddress, + (UINT32)RequestSize, + VRING_DESC_F_NEXT, + &Indices + ); + VirtioAppendDesc ( + &VgpuDev->Ring, + ResponseDeviceAddress, + (UINT32)sizeof Response, + VRING_DESC_F_WRITE, + &Indices + ); =20 // // Send the command. // Status =3D VirtioFlush (VgpuDev->VirtIo, VIRTIO_GPU_CONTROL_QUEUE, &VgpuDev->Ring, &Indices, &ResponseSize); if (EFI_ERROR (Status)) { - return Status; + goto UnmapResponse; } =20 // - // Parse the response. + // Verify response size. // if (ResponseSize !=3D sizeof Response) { DEBUG ((EFI_D_ERROR, "%a: malformed response to Request=3D0x%x\n", __FUNCTION__, (UINT32)RequestType)); - return EFI_PROTOCOL_ERROR; + Status =3D EFI_PROTOCOL_ERROR; + goto UnmapResponse; } =20 + // + // Unmap response and request, in reverse order of mapping. On error, the + // respective mapping is invalidated anyway, only the data may not have = been + // committed to system memory (in case of VirtioOperationBusMasterWrite). + // + Status =3D VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, Response= Map); + if (EFI_ERROR (Status)) { + goto UnmapRequest; + } + Status =3D VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, RequestM= ap); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Parse the response. + // if (Response.Type =3D=3D VirtioGpuRespOkNodata) { return EFI_SUCCESS; } =20 DEBUG ((EFI_D_ERROR, "%a: Request=3D0x%x Response=3D0x%x\n", __FUNCTION_= _, (UINT32)RequestType, Response.Type)); return EFI_DEVICE_ERROR; + +UnmapResponse: + VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, ResponseMap); + +UnmapRequest: + VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, RequestMap); + + return Status; } =20 /** The following functions send requests to the VirtIo GPU device model, aw= ait the answer from the host, and return a status. They share the following interface details: =20 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo G= PU device. The caller is responsible to have successfully invoked VirtioGpuInit() on VgpuDev previously, while VirtioGpuUninit() must not have been called on VgpuDev. =20 @retval EFI_INVALID_PARAMETER Invalid command-specific parameters were detected by this driver. =20 @retval EFI_SUCCESS Operation successful. =20 @retval EFI_DEVICE_ERROR The host rejected the request. The host e= rror code has been logged on the EFI_D_ERROR l= evel. =20 @return Codes for unexpected errors in VirtIo messaging. =20 For the command-specific parameters, please consult the GPU Device secti= on of the VirtIo 1.0 specification (see references in "OvmfPkg/Include/IndustryStandard/VirtioGpu.h"). **/ --=20 2.14.1.3.gb7cf6e02401b _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Sun May 5 08:12:58 2024 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 1503926691374847.8384166587117; Mon, 28 Aug 2017 06:24:51 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 5DFA62095DCAB; Mon, 28 Aug 2017 06:22:09 -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 9B2E421DF809B for ; Mon, 28 Aug 2017 06:22:07 -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 93B14883B0; Mon, 28 Aug 2017 13:24:46 +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 37EFD60A9B; Mon, 28 Aug 2017 13:24:45 +0000 (UTC) X-Original-To: edk2-devel@lists.01.org DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 93B14883B0 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:33 +0200 Message-Id: <20170828132436.15933-4-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:46 +0000 (UTC) Subject: [edk2] [PATCH 3/6] OvmfPkg/VirtioGpuDxe: take EFI_PHYSICAL_ADDRESS in ResourceAttachBacking() 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" The RESOURCE_ATTACH_BACKING virtio GPU command assigns guest-side backing pages to a host-side resource that was created earlier with the RESOURCE_CREATE_2D command. We compose the RESOURCE_ATTACH_BACKING command in the VirtioGpuResourceAttachBacking() function. Currently this function takes the parameter IN VOID *FirstBackingPage This is only appropriate as long as we pass a (guest-phys) system memory address to the device. In preparation for a mapped bus master device address, change the above parameter to IN EFI_PHYSICAL_ADDRESS BackingStoreDeviceAddress In order to keep the current call site functional, move the (VOID*) to (UINTN) conversion out of the function, to the call site. The "Request.Entry.Addr" field already has type UINT64. This patch is similar to commit 4b725858de68 ("OvmfPkg/VirtioLib: change the parameter of VirtioAppendDesc() to UINT64", 2017-08-23). Cc: Ard Biesheuvel Cc: Brijesh Singh Cc: Jordan Justen Cc: Tom Lendacky Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Laszlo Ersek Tested-By: Brijesh Singh --- OvmfPkg/VirtioGpuDxe/VirtioGpu.h | 8 ++++---- OvmfPkg/VirtioGpuDxe/Commands.c | 10 +++++----- OvmfPkg/VirtioGpuDxe/Gop.c | 8 ++++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/OvmfPkg/VirtioGpuDxe/VirtioGpu.h b/OvmfPkg/VirtioGpuDxe/Virtio= Gpu.h index 193e932e1430..cf2a63accd72 100644 --- a/OvmfPkg/VirtioGpuDxe/VirtioGpu.h +++ b/OvmfPkg/VirtioGpuDxe/VirtioGpu.h @@ -252,10 +252,10 @@ VirtioGpuResourceUnref ( =20 EFI_STATUS VirtioGpuResourceAttachBacking ( - IN OUT VGPU_DEV *VgpuDev, - IN UINT32 ResourceId, - IN VOID *FirstBackingPage, - IN UINTN NumberOfPages + IN OUT VGPU_DEV *VgpuDev, + IN UINT32 ResourceId, + IN EFI_PHYSICAL_ADDRESS BackingStoreDeviceAddress, + IN UINTN NumberOfPages ); =20 EFI_STATUS diff --git a/OvmfPkg/VirtioGpuDxe/Commands.c b/OvmfPkg/VirtioGpuDxe/Command= s.c index bdedea1df6a7..c1951a807e98 100644 --- a/OvmfPkg/VirtioGpuDxe/Commands.c +++ b/OvmfPkg/VirtioGpuDxe/Commands.c @@ -496,29 +496,29 @@ VirtioGpuResourceUnref ( =20 EFI_STATUS VirtioGpuResourceAttachBacking ( - IN OUT VGPU_DEV *VgpuDev, - IN UINT32 ResourceId, - IN VOID *FirstBackingPage, - IN UINTN NumberOfPages + IN OUT VGPU_DEV *VgpuDev, + IN UINT32 ResourceId, + IN EFI_PHYSICAL_ADDRESS BackingStoreDeviceAddress, + IN UINTN NumberOfPages ) { volatile VIRTIO_GPU_RESOURCE_ATTACH_BACKING Request; =20 if (ResourceId =3D=3D 0) { return EFI_INVALID_PARAMETER; } =20 Request.ResourceId =3D ResourceId; Request.NrEntries =3D 1; - Request.Entry.Addr =3D (UINTN)FirstBackingPage; + Request.Entry.Addr =3D BackingStoreDeviceAddress; Request.Entry.Length =3D (UINT32)EFI_PAGES_TO_SIZE (NumberOfPages); Request.Entry.Padding =3D 0; =20 return VirtioGpuSendCommand ( VgpuDev, VirtioGpuCmdResourceAttachBacking, FALSE, // Fence &Request.Header, sizeof Request ); } diff --git a/OvmfPkg/VirtioGpuDxe/Gop.c b/OvmfPkg/VirtioGpuDxe/Gop.c index 3438bd03224e..b3c5dae74d0e 100644 --- a/OvmfPkg/VirtioGpuDxe/Gop.c +++ b/OvmfPkg/VirtioGpuDxe/Gop.c @@ -229,176 +229,176 @@ EFIAPI GopSetMode ( IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This, IN UINT32 ModeNumber ) { VGPU_GOP *VgpuGop; UINT32 NewResourceId; UINTN NewNumberOfBytes; UINTN NewNumberOfPages; VOID *NewBackingStore; EFI_STATUS Status; EFI_STATUS Status2; =20 if (ModeNumber >=3D ARRAY_SIZE (mGopResolutions)) { return EFI_UNSUPPORTED; } =20 VgpuGop =3D VGPU_GOP_FROM_GOP (This); =20 // // Distinguish the first (internal) call from the other (protocol consum= er) // calls. // if (VgpuGop->ResourceId =3D=3D 0) { // // Set up the Gop -> GopMode -> GopModeInfo pointer chain, and the oth= er // (nonzero) constant fields. // // No direct framebuffer access is supported, only Blt() is. // VgpuGop->Gop.Mode =3D &VgpuGop->GopMode; =20 VgpuGop->GopMode.MaxMode =3D (UINT32)(ARRAY_SIZE (mGopResoluti= ons)); VgpuGop->GopMode.Info =3D &VgpuGop->GopModeInfo; VgpuGop->GopMode.SizeOfInfo =3D sizeof VgpuGop->GopModeInfo; =20 VgpuGop->GopModeInfo.PixelFormat =3D PixelBltOnly; =20 // // This is the first time we create a host side resource. // NewResourceId =3D 1; } else { // // We already have an active host side resource. Create the new one wi= thout // interfering with the current one, so that we can cleanly bail out on // error, without disturbing the current graphics mode. // // The formula below will alternate between IDs 1 and 2. // NewResourceId =3D 3 - VgpuGop->ResourceId; } =20 // // Create the 2D host resource. // Status =3D VirtioGpuResourceCreate2d ( VgpuGop->ParentBus, // VgpuDev NewResourceId, // ResourceId VirtioGpuFormatB8G8R8X8Unorm, // Format mGopResolutions[ModeNumber].Width, // Width mGopResolutions[ModeNumber].Height // Height ); if (EFI_ERROR (Status)) { return Status; } =20 // // Allocate guest backing store. // NewNumberOfBytes =3D mGopResolutions[ModeNumber].Width * mGopResolutions[ModeNumber].Height * sizeof (UINT32); NewNumberOfPages =3D EFI_SIZE_TO_PAGES (NewNumberOfBytes); NewBackingStore =3D AllocatePages (NewNumberOfPages); if (NewBackingStore =3D=3D NULL) { Status =3D EFI_OUT_OF_RESOURCES; goto DestroyHostResource; } // // Fill visible part of backing store with black. // ZeroMem (NewBackingStore, NewNumberOfBytes); =20 // // Attach backing store to the host resource. // Status =3D VirtioGpuResourceAttachBacking ( - VgpuGop->ParentBus, // VgpuDev - NewResourceId, // ResourceId - NewBackingStore, // FirstBackingPage - NewNumberOfPages // NumberOfPages + VgpuGop->ParentBus, // VgpuDev + NewResourceId, // ResourceId + (UINTN)NewBackingStore, // BackingStoreDeviceAddress + NewNumberOfPages // NumberOfPages ); if (EFI_ERROR (Status)) { goto FreeBackingStore; } =20 // // Point head (scanout) #0 to the host resource. // Status =3D VirtioGpuSetScanout ( VgpuGop->ParentBus, // VgpuDev 0, // X 0, // Y mGopResolutions[ModeNumber].Width, // Width mGopResolutions[ModeNumber].Height, // Height 0, // ScanoutId NewResourceId // ResourceId ); if (EFI_ERROR (Status)) { goto DetachBackingStore; } =20 // // If this is not the first (i.e., internal) call, then we have to (a) f= lush // the new resource to head (scanout) #0, after having flipped the latte= r to // the former above, plus (b) release the old resources. // if (VgpuGop->ResourceId !=3D 0) { Status =3D VirtioGpuResourceFlush ( VgpuGop->ParentBus, // VgpuDev 0, // X 0, // Y mGopResolutions[ModeNumber].Width, // Width mGopResolutions[ModeNumber].Height, // Height NewResourceId // ResourceId ); if (EFI_ERROR (Status)) { // // Flip head (scanout) #0 back to the current resource. If this fail= s, we // cannot continue, as this error occurs on the error path and is // therefore non-recoverable. // Status2 =3D VirtioGpuSetScanout ( VgpuGop->ParentBus, // VgpuDev 0, // X 0, // Y mGopResolutions[This->Mode->Mode].Width, // Width mGopResolutions[This->Mode->Mode].Height, // Height 0, // ScanoutId VgpuGop->ResourceId // ResourceId ); ASSERT_EFI_ERROR (Status2); if (EFI_ERROR (Status2)) { CpuDeadLoop (); } goto DetachBackingStore; } =20 // // Flush successful; release the old resources (without disabling head // (scanout) #0). // ReleaseGopResources (VgpuGop, FALSE /* DisableHead */); } =20 // // This is either the first (internal) call when we have no old resources // yet, or we've changed the mode successfully and released the old // resources. // ASSERT (VgpuGop->ResourceId =3D=3D 0); ASSERT (VgpuGop->BackingStore =3D=3D NULL); =20 VgpuGop->ResourceId =3D NewResourceId; VgpuGop->BackingStore =3D NewBackingStore; VgpuGop->NumberOfPages =3D NewNumberOfPages; =20 // // Populate Mode and ModeInfo (mutable fields only). // VgpuGop->GopMode.Mode =3D ModeNumber; VgpuGop->GopModeInfo.HorizontalResolution =3D mGopResolutions[ModeNumber].W= idth; VgpuGop->GopModeInfo.VerticalResolution =3D mGopResolutions[ModeNumber].= Height; VgpuGop->GopModeInfo.PixelsPerScanLine =3D mGopResolutions[ModeNumber].W= idth; return EFI_SUCCESS; --=20 2.14.1.3.gb7cf6e02401b _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Sun May 5 08:12:58 2024 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 Tested-By: Brijesh Singh --- 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 From nobody Sun May 5 08:12:58 2024 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 1503926697522289.2119182718577; Mon, 28 Aug 2017 06:24:57 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id E46142095AE47; Mon, 28 Aug 2017 06:22:12 -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 3D9AB2095DCB8 for ; Mon, 28 Aug 2017 06:22:11 -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 3A101C047B73; Mon, 28 Aug 2017 13:24:50 +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 D409560A9B; Mon, 28 Aug 2017 13:24:48 +0000 (UTC) X-Original-To: edk2-devel@lists.01.org DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3A101C047B73 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.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:35 +0200 Message-Id: <20170828132436.15933-6-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.31]); Mon, 28 Aug 2017 13:24:50 +0000 (UTC) Subject: [edk2] [PATCH 5/6] OvmfPkg/VirtioGpuDxe: map backing store to bus master device address 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" VirtioGpuDxe is a UEFI Bus driver (not a Device driver). This is because a UEFI graphics driver is expected to produce its GraphicsOutput protocol instance(s) on new child handle(s) of the video controller handle, one child handle (plus GOP) per video output (or, one child handle plus GOP per combination of multiple video outputs). In VirtioGpuDxe, we support a single VirtIo GPU head (scanout), namely head#0. This means that, with regard to a specific VirtIo GPU device, the driver may be in one of three states, at any time: [1] VirtioGpuDxe has not bound the device at all, [2] VirtioGpuDxe has bound the device, but not produced the sole child handle for head#0, [3] VirtioGpuDxe has bound the device, and produced the sole child handle for head#0, with a GOP instance on the child handle. (Which state the driver is in wrt. a given VirtIo GPU device depends on the VirtioGpuDriverBindingStart() / VirtioGpuDriverBindingStop() invocations issued by the ConnectController() / DisconnectController() boot services. In turn those come from BDS or e.g. the UEFI shell.) The concept of "current video mode" is technically tied to the GOP (i.e., the child handle, state [3] only), not the VirtIo GPU controller handle. This is why we manage the storage that backs the current video mode in our EFI_GRAPHICS_OUTPUT_PROTOCOL.SetMode() member implementation. GopSetMode() is first called *internally*, when we enter state [3] (that is, when we produce the child handle + GOP on it): VirtioGpuDriverBindingStart() [DriverBinding.c] InitVgpuGop() [DriverBinding.c] VgpuGop->Gop.SetMode() [Gop.c] When this happens, we allocate the backing store *without* having a preexistent backing store (due to no preexistent video mode and GOP). Skipping VirtIo GPU details not relevant for this patch, we just note that the backing store is exposed *permanently* to the VirtIo GPU device, with the RESOURCE_ATTACH_BACKING command. When external clients call the EFI_GRAPHICS_OUTPUT_PROTOCOL.Blt() member function -- called GopBlt() in this driver --, in state [3], the function operates on the backing store, and sends only small messages to the VirtIo GPU device. When external clients call GopSetMode() for switching between video modes -- in state [3] --, then - a new backing store is allocated and exposed to the device (attached to a new host-side VirtIo GPU resource), - head#0 is flipped to the new backing store, - on success, the ReleaseGopResources() function both detaches the previous backing store from the VirtIo GPU device, an releases it. The new backing store address and size are saved in our GOP object. (In other words, we "commit" to the new video mode.) When the DisconnectController() boot service asks us to leave state [3] -- we can leave it directly only for state [2] --, then the ReleaseGopResources() function is called on a different path: VirtioGpuDriverBindingStop() [DriverBinding.c] UninitVgpuGop() [DriverBinding.c] ReleaseGopResources() [Gop.c] In this case, the backing store being released is still in use (we're not leaving it for a new mode -- head#0 has not been flipped "away" from it), so in ReleaseGopResources() we disable head#0 first. (The ReleaseGopResources() function is called the same way on the error path in InitVgpuGop(), if the first -- internal -- VgpuGop->Gop.SetMode() call succeeds, but the rest of InitVgpuGop() fails.) Based on the above, for IOMMU-compatibility, - in GopSetMode(), don't just allocate, but also map the backing store of the nascent video mode to a device address, for bus master common buffer operation, - (the VirtioGpuAllocateZeroAndMapBackingStore() helper function introduced in the last patch takes care of zeroing internally,) - pass the device address to the VirtIo GPU device in the RESOURCE_ATTACH_BACKING command, - if GopSetMode() succeeds, save the mapping token, - if GopSetMode() fails, don't just free but also unmap the still-born backing store, - in ReleaseGopResources(), don't just free but also unmap the backing store -- which is the previous backing store if we're mode-switching, and the current backing store if we're leaving state [3]. Finally, ExitBootServices() may be called when the driver is in either state [1], [2] or [3], wrt. a given VirtIo GPU device. (Of course we are only notified in states [2] and [3].) If we get the notification in state [3], then the current video mode's backing store has to be unmapped, but not released. (We must not change the UEFI memory map.) Cc: Ard Biesheuvel Cc: Brijesh Singh Cc: Jordan Justen Cc: Tom Lendacky Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Laszlo Ersek Tested-By: Brijesh Singh --- OvmfPkg/VirtioGpuDxe/VirtioGpu.h | 7 +++ OvmfPkg/VirtioGpuDxe/Commands.c | 21 ++++++++ OvmfPkg/VirtioGpuDxe/Gop.c | 55 +++++++++++++------- 3 files changed, 64 insertions(+), 19 deletions(-) diff --git a/OvmfPkg/VirtioGpuDxe/VirtioGpu.h b/OvmfPkg/VirtioGpuDxe/Virtio= Gpu.h index 65b1bd6088b8..73893ccb56eb 100644 --- a/OvmfPkg/VirtioGpuDxe/VirtioGpu.h +++ b/OvmfPkg/VirtioGpuDxe/VirtioGpu.h @@ -94,80 +94,87 @@ typedef struct { struct VGPU_GOP_STRUCT { UINT64 Signature; =20 // // ParentBus points to the parent VGPU_DEV object. Never NULL. // VGPU_DEV *ParentBus; =20 // // GopName carries a customized name for // EFI_COMPONENT_NAME2_PROTOCOL.GetControllerName(). It is expressed in = table // form because it can theoretically support several languages. Never NU= LL. // EFI_UNICODE_STRING_TABLE *GopName; =20 // // GopHandle is the UEFI child handle that carries the device path ending // with the ACPI ADR node, and the Graphics Output Protocol. Never NULL. // EFI_HANDLE GopHandle; =20 // // The GopDevicePath field is the device path installed on GopHandle, // ending with an ACPI ADR node. Never NULL. // EFI_DEVICE_PATH_PROTOCOL *GopDevicePath; =20 // // The Gop field is installed on the child handle as Graphics Output Pro= tocol // interface. // EFI_GRAPHICS_OUTPUT_PROTOCOL Gop; =20 // // Referenced by Gop.Mode, GopMode provides a summary about the supported // graphics modes, and the current mode. // EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE GopMode; =20 // // Referenced by GopMode.Info, GopModeInfo provides detailed information // about the current mode. // EFI_GRAPHICS_OUTPUT_MODE_INFORMATION GopModeInfo; =20 // // Identifier of the 2D host resource that is in use by this head (scano= ut) // of the VirtIo GPU device. Zero until the first successful -- internal= -- // Gop.SetMode() call, never zero afterwards. // UINT32 ResourceId; =20 // // A number of whole pages providing the backing store for the 2D host // resource identified by ResourceId above. NULL until the first success= ful // -- internal -- Gop.SetMode() call, never NULL afterwards. // UINT32 *BackingStore; UINTN NumberOfPages; + + // + // Token associated with BackingStore's mapping for bus master common + // buffer operation. BackingStoreMap is valid if, and only if, + // BackingStore is non-NULL. + // + VOID *BackingStoreMap; }; =20 // // VirtIo GPU initialization, and commands (primitives) for the GPU device. // /** Configure the VirtIo GPU device that underlies VgpuDev. =20 @param[in,out] VgpuDev The VGPU_DEV object to set up VirtIo messaging f= or. On input, the caller is responsible for having initialized VgpuDev->VirtIo. On output, VgpuDev-= >Ring has been initialized, and synchronous VirtIo GPU commands (primitives) can be submitted to the de= vice. =20 @retval EFI_SUCCESS VirtIo GPU configuration successful. =20 @retval EFI_UNSUPPORTED The host-side configuration of the VirtIo GPU i= s not supported by this driver. =20 @retval Error codes from underlying functions. **/ diff --git a/OvmfPkg/VirtioGpuDxe/Commands.c b/OvmfPkg/VirtioGpuDxe/Command= s.c index 595a3717d926..db5bdbca4bee 100644 --- a/OvmfPkg/VirtioGpuDxe/Commands.c +++ b/OvmfPkg/VirtioGpuDxe/Commands.c @@ -349,55 +349,76 @@ EFIAPI VirtioGpuExitBoot ( IN EFI_EVENT Event, IN VOID *Context ) { VGPU_DEV *VgpuDev; =20 VgpuDev =3D Context; VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, 0); + + // + // If VirtioGpuDriverBindingStart() and VirtioGpuDriverBindingStop() have + // been called thus far in such a sequence that right now our (sole) chi= ld + // handle exists -- with the GOP on it standing for head (scanout) #0 --, + // then we have to unmap the current video mode's backing store. + // + if (VgpuDev->Child !=3D NULL) { + // + // The current video mode is guaranteed to have a valid and mapped bac= king + // store, due to the first Gop.SetMode() call, made internally in + // InitVgpuGop(). + // + ASSERT (VgpuDev->Child->BackingStore !=3D NULL); + + VgpuDev->VirtIo->UnmapSharedBuffer ( + VgpuDev->VirtIo, + VgpuDev->Child->BackingStoreMap + ); + } + VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, VgpuDev->RingMap); } =20 /** Internal utility function that sends a request to the VirtIo GPU device model, awaits the answer from the host, and returns a status. =20 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo G= PU device. The caller is responsible to have successfully invoked VirtioGpuInit() on VgpuDev previously, while VirtioGpuUninit() must not have been called on VgpuDev. =20 @param[in] RequestType The type of the request. The caller is responsib= le for providing a VirtioGpuCmd* RequestType which,= on success, elicits a VirtioGpuRespOkNodata response from the host. =20 @param[in] Fence Whether to enable fencing for this request. Fenc= ing forces the host to complete the command before producing a response. If Fence is TRUE, then VgpuDev->FenceId is consumed, and incremented. =20 @param[in,out] Header Pointer to the caller-allocated request object. = The request must start with VIRTIO_GPU_CONTROL_HEADE= R. This function overwrites all fields of Header be= fore submitting the request to the host: =20 - it sets Type from RequestType, =20 - it sets Flags and FenceId based on Fence, =20 - it zeroes CtxId and Padding. =20 @param[in] RequestSize Size of the entire caller-allocated request obje= ct, including the leading VIRTIO_GPU_CONTROL_HEADER. =20 @retval EFI_SUCCESS Operation successful. =20 @retval EFI_DEVICE_ERROR The host rejected the request. The host e= rror code has been logged on the EFI_D_ERROR l= evel. =20 @return Codes for unexpected errors in VirtIo messaging, or request/response mapping/unmapping. **/ diff --git a/OvmfPkg/VirtioGpuDxe/Gop.c b/OvmfPkg/VirtioGpuDxe/Gop.c index 507e1a770d10..936e181e8a87 100644 --- a/OvmfPkg/VirtioGpuDxe/Gop.c +++ b/OvmfPkg/VirtioGpuDxe/Gop.c @@ -45,95 +45,101 @@ VOID ReleaseGopResources ( IN OUT VGPU_GOP *VgpuGop, IN BOOLEAN DisableHead ) { EFI_STATUS Status; =20 ASSERT (VgpuGop->ResourceId !=3D 0); ASSERT (VgpuGop->BackingStore !=3D NULL); =20 // // If any of the following host-side destruction steps fail, we can't ge= t out // of an inconsistent state, so we'll hang. In general errors in object // destruction can hardly be recovered from. // if (DisableHead) { // // Dissociate head (scanout) #0 from the currently used 2D host resour= ce, // by setting ResourceId=3D0 for it. // Status =3D VirtioGpuSetScanout ( VgpuGop->ParentBus, // VgpuDev 0, 0, 0, 0, // X, Y, Width, Height 0, // ScanoutId 0 // ResourceId ); // // HACK BEGINS HERE // // According to the GPU Device section of the VirtIo specification, the // above operation is valid: // // "The driver can use resource_id =3D 0 to disable a scanout." // // However, in practice QEMU does not allow us to disable head (scanou= t) #0 // -- it rejects the command with response code 0x1202 // (VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID). Looking at the QEMU source // code, function virtio_gpu_set_scanout() in "hw/display/virtio-gpu.c= ", // this appears fully intentional, despite not being documented in the // spec. // // Surprisingly, ignoring the error here, and proceeding to release // host-side resources that presumably underlie head (scanout) #0, work // without any problems -- the driver survives repeated "disconnect" / // "connect -r" commands in the UEFI shell. // // So, for now, let's just suppress the error. // Status =3D EFI_SUCCESS; // // HACK ENDS HERE // =20 ASSERT_EFI_ERROR (Status); if (EFI_ERROR (Status)) { CpuDeadLoop (); } } =20 // // Detach backing pages from the currently used 2D host resource. // Status =3D VirtioGpuResourceDetachBacking ( VgpuGop->ParentBus, // VgpuDev VgpuGop->ResourceId // ResourceId ); ASSERT_EFI_ERROR (Status); if (EFI_ERROR (Status)) { CpuDeadLoop (); } =20 // - // Release backing pages. + // Unmap and release backing pages. // - FreePages (VgpuGop->BackingStore, VgpuGop->NumberOfPages); + VirtioGpuUnmapAndFreeBackingStore ( + VgpuGop->ParentBus, // VgpuDev + VgpuGop->NumberOfPages, // NumberOfPages + VgpuGop->BackingStore, // HostAddress + VgpuGop->BackingStoreMap // Mapping + ); VgpuGop->BackingStore =3D NULL; VgpuGop->NumberOfPages =3D 0; + VgpuGop->BackingStoreMap =3D NULL; =20 // // Destroy the currently used 2D host resource. // Status =3D VirtioGpuResourceUnref ( VgpuGop->ParentBus, // VgpuDev VgpuGop->ResourceId // ResourceId ); ASSERT_EFI_ERROR (Status); if (EFI_ERROR (Status)) { CpuDeadLoop (); } VgpuGop->ResourceId =3D 0; } =20 // // The resolutions supported by this driver. // @@ -228,176 +234,182 @@ EFIAPI GopSetMode ( IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This, IN UINT32 ModeNumber ) { - VGPU_GOP *VgpuGop; - UINT32 NewResourceId; - UINTN NewNumberOfBytes; - UINTN NewNumberOfPages; - VOID *NewBackingStore; + VGPU_GOP *VgpuGop; + UINT32 NewResourceId; + UINTN NewNumberOfBytes; + UINTN NewNumberOfPages; + VOID *NewBackingStore; + EFI_PHYSICAL_ADDRESS NewBackingStoreDeviceAddress; + VOID *NewBackingStoreMap; + EFI_STATUS Status; EFI_STATUS Status2; =20 if (ModeNumber >=3D ARRAY_SIZE (mGopResolutions)) { return EFI_UNSUPPORTED; } =20 VgpuGop =3D VGPU_GOP_FROM_GOP (This); =20 // // Distinguish the first (internal) call from the other (protocol consum= er) // calls. // if (VgpuGop->ResourceId =3D=3D 0) { // // Set up the Gop -> GopMode -> GopModeInfo pointer chain, and the oth= er // (nonzero) constant fields. // // No direct framebuffer access is supported, only Blt() is. // VgpuGop->Gop.Mode =3D &VgpuGop->GopMode; =20 VgpuGop->GopMode.MaxMode =3D (UINT32)(ARRAY_SIZE (mGopResoluti= ons)); VgpuGop->GopMode.Info =3D &VgpuGop->GopModeInfo; VgpuGop->GopMode.SizeOfInfo =3D sizeof VgpuGop->GopModeInfo; =20 VgpuGop->GopModeInfo.PixelFormat =3D PixelBltOnly; =20 // // This is the first time we create a host side resource. // NewResourceId =3D 1; } else { // // We already have an active host side resource. Create the new one wi= thout // interfering with the current one, so that we can cleanly bail out on // error, without disturbing the current graphics mode. // // The formula below will alternate between IDs 1 and 2. // NewResourceId =3D 3 - VgpuGop->ResourceId; } =20 // // Create the 2D host resource. // Status =3D VirtioGpuResourceCreate2d ( VgpuGop->ParentBus, // VgpuDev NewResourceId, // ResourceId VirtioGpuFormatB8G8R8X8Unorm, // Format mGopResolutions[ModeNumber].Width, // Width mGopResolutions[ModeNumber].Height // Height ); if (EFI_ERROR (Status)) { return Status; } =20 // - // Allocate guest backing store. + // Allocate, zero and map guest backing store, for bus master common buf= fer + // operation. // NewNumberOfBytes =3D mGopResolutions[ModeNumber].Width * mGopResolutions[ModeNumber].Height * sizeof (UINT32); NewNumberOfPages =3D EFI_SIZE_TO_PAGES (NewNumberOfBytes); - NewBackingStore =3D AllocatePages (NewNumberOfPages); - if (NewBackingStore =3D=3D NULL) { - Status =3D EFI_OUT_OF_RESOURCES; + Status =3D VirtioGpuAllocateZeroAndMapBackingStore ( + VgpuGop->ParentBus, // VgpuDev + NewNumberOfPages, // NumberOfPages + &NewBackingStore, // HostAddress + &NewBackingStoreDeviceAddress, // DeviceAddress + &NewBackingStoreMap // Mapping + ); + if (EFI_ERROR (Status)) { goto DestroyHostResource; } - // - // Fill visible part of backing store with black. - // - ZeroMem (NewBackingStore, NewNumberOfBytes); =20 // // Attach backing store to the host resource. // Status =3D VirtioGpuResourceAttachBacking ( VgpuGop->ParentBus, // VgpuDev NewResourceId, // ResourceId - (UINTN)NewBackingStore, // BackingStoreDeviceAddress + NewBackingStoreDeviceAddress, // BackingStoreDeviceAddress NewNumberOfPages // NumberOfPages ); if (EFI_ERROR (Status)) { - goto FreeBackingStore; + goto UnmapAndFreeBackingStore; } =20 // // Point head (scanout) #0 to the host resource. // Status =3D VirtioGpuSetScanout ( VgpuGop->ParentBus, // VgpuDev 0, // X 0, // Y mGopResolutions[ModeNumber].Width, // Width mGopResolutions[ModeNumber].Height, // Height 0, // ScanoutId NewResourceId // ResourceId ); if (EFI_ERROR (Status)) { goto DetachBackingStore; } =20 // // If this is not the first (i.e., internal) call, then we have to (a) f= lush // the new resource to head (scanout) #0, after having flipped the latte= r to // the former above, plus (b) release the old resources. // if (VgpuGop->ResourceId !=3D 0) { Status =3D VirtioGpuResourceFlush ( VgpuGop->ParentBus, // VgpuDev 0, // X 0, // Y mGopResolutions[ModeNumber].Width, // Width mGopResolutions[ModeNumber].Height, // Height NewResourceId // ResourceId ); if (EFI_ERROR (Status)) { // // Flip head (scanout) #0 back to the current resource. If this fail= s, we // cannot continue, as this error occurs on the error path and is // therefore non-recoverable. // Status2 =3D VirtioGpuSetScanout ( VgpuGop->ParentBus, // VgpuDev 0, // X 0, // Y mGopResolutions[This->Mode->Mode].Width, // Width mGopResolutions[This->Mode->Mode].Height, // Height 0, // ScanoutId VgpuGop->ResourceId // ResourceId ); ASSERT_EFI_ERROR (Status2); if (EFI_ERROR (Status2)) { CpuDeadLoop (); } goto DetachBackingStore; } =20 // // Flush successful; release the old resources (without disabling head // (scanout) #0). // ReleaseGopResources (VgpuGop, FALSE /* DisableHead */); } =20 // // This is either the first (internal) call when we have no old resources // yet, or we've changed the mode successfully and released the old // resources. // ASSERT (VgpuGop->ResourceId =3D=3D 0); ASSERT (VgpuGop->BackingStore =3D=3D NULL); =20 VgpuGop->ResourceId =3D NewResourceId; VgpuGop->BackingStore =3D NewBackingStore; VgpuGop->NumberOfPages =3D NewNumberOfPages; + VgpuGop->BackingStoreMap =3D NewBackingStoreMap; =20 // // Populate Mode and ModeInfo (mutable fields only). // VgpuGop->GopMode.Mode =3D ModeNumber; VgpuGop->GopModeInfo.HorizontalResolution =3D mGopResolutions[ModeNumber].W= idth; VgpuGop->GopModeInfo.VerticalResolution =3D mGopResolutions[ModeNumber].= Height; VgpuGop->GopModeInfo.PixelsPerScanLine =3D mGopResolutions[ModeNumber].W= idth; return EFI_SUCCESS; @@ -409,8 +421,13 @@ DetachBackingStore: CpuDeadLoop (); } =20 -FreeBackingStore: - FreePages (NewBackingStore, NewNumberOfPages); +UnmapAndFreeBackingStore: + VirtioGpuUnmapAndFreeBackingStore ( + VgpuGop->ParentBus, // VgpuDev + NewNumberOfPages, // NumberOfPages + NewBackingStore, // HostAddress + NewBackingStoreMap // Mapping + ); =20 DestroyHostResource: Status2 =3D VirtioGpuResourceUnref (VgpuGop->ParentBus, NewResourceId); --=20 2.14.1.3.gb7cf6e02401b _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel From nobody Sun May 5 08:12:58 2024 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 1503926700726600.0015471532563; Mon, 28 Aug 2017 06:25:00 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 2A8AA21D2E624; Mon, 28 Aug 2017 06:22:16 -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 EA7992095DCBD for ; Mon, 28 Aug 2017 06:22:12 -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 E613C7F405; Mon, 28 Aug 2017 13:24:51 +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 8EC4660A9B; Mon, 28 Aug 2017 13:24:50 +0000 (UTC) X-Original-To: edk2-devel@lists.01.org DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com E613C7F405 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.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:36 +0200 Message-Id: <20170828132436.15933-7-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.25]); Mon, 28 Aug 2017 13:24:52 +0000 (UTC) Subject: [edk2] [PATCH 6/6] OvmfPkg/VirtioGpuDxe: negotiate VIRTIO_F_IOMMU_PLATFORM 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" VirtioGpuDxe is now IOMMU-clean; it translates system memory addresses to bus master device addresses. Negotiate VIRTIO_F_IOMMU_PLATFORM in parallel with VIRTIO_F_VERSION_1. (Note: the VirtIo GPU device, and this driver, are virtio-1.0 only (a.k.a. "modern-only").) Cc: Ard Biesheuvel Cc: Brijesh Singh Cc: Jordan Justen Cc: Tom Lendacky Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Laszlo Ersek Tested-By: Brijesh Singh --- OvmfPkg/VirtioGpuDxe/Commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OvmfPkg/VirtioGpuDxe/Commands.c b/OvmfPkg/VirtioGpuDxe/Command= s.c index db5bdbca4bee..6e70b1c33f65 100644 --- a/OvmfPkg/VirtioGpuDxe/Commands.c +++ b/OvmfPkg/VirtioGpuDxe/Commands.c @@ -39,131 +39,131 @@ EFI_STATUS VirtioGpuInit ( IN OUT VGPU_DEV *VgpuDev ) { UINT8 NextDevStat; EFI_STATUS Status; UINT64 Features; UINT16 QueueSize; UINT64 RingBaseShift; =20 // // Execute virtio-v1.0-cs04, 3.1.1 Driver Requirements: Device // Initialization. // // 1. Reset the device. // NextDevStat =3D 0; Status =3D VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevSta= t); if (EFI_ERROR (Status)) { goto Failed; } =20 // // 2. Set the ACKNOWLEDGE status bit [...] // NextDevStat |=3D VSTAT_ACK; Status =3D VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevSta= t); if (EFI_ERROR (Status)) { goto Failed; } =20 // // 3. Set the DRIVER status bit [...] // NextDevStat |=3D VSTAT_DRIVER; Status =3D VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevSta= t); if (EFI_ERROR (Status)) { goto Failed; } =20 // // 4. Read device feature bits... // Status =3D VgpuDev->VirtIo->GetDeviceFeatures (VgpuDev->VirtIo, &Feature= s); if (EFI_ERROR (Status)) { goto Failed; } if ((Features & VIRTIO_F_VERSION_1) =3D=3D 0) { Status =3D EFI_UNSUPPORTED; goto Failed; } // // We only want the most basic 2D features. // - Features &=3D VIRTIO_F_VERSION_1; + Features &=3D VIRTIO_F_VERSION_1 | VIRTIO_F_IOMMU_PLATFORM; =20 // // ... and write the subset of feature bits understood by the [...] driv= er to // the device. [...] // 5. Set the FEATURES_OK status bit. // 6. Re-read device status to ensure the FEATURES_OK bit is still set [= ...] // Status =3D Virtio10WriteFeatures (VgpuDev->VirtIo, Features, &NextDevSta= t); if (EFI_ERROR (Status)) { goto Failed; } =20 // // 7. Perform device-specific setup, including discovery of virtqueues f= or // the device [...] // Status =3D VgpuDev->VirtIo->SetQueueSel (VgpuDev->VirtIo, VIRTIO_GPU_CONTROL_QUEUE); if (EFI_ERROR (Status)) { goto Failed; } Status =3D VgpuDev->VirtIo->GetQueueNumMax (VgpuDev->VirtIo, &QueueSize); if (EFI_ERROR (Status)) { goto Failed; } =20 // // We implement each VirtIo GPU command that we use with two descriptors: // request, response. // if (QueueSize < 2) { Status =3D EFI_UNSUPPORTED; goto Failed; } =20 // // [...] population of virtqueues [...] // Status =3D VirtioRingInit (VgpuDev->VirtIo, QueueSize, &VgpuDev->Ring); if (EFI_ERROR (Status)) { goto Failed; } // // If anything fails from here on, we have to release the ring. // Status =3D VirtioRingMap ( VgpuDev->VirtIo, &VgpuDev->Ring, &RingBaseShift, &VgpuDev->RingMap ); if (EFI_ERROR (Status)) { goto ReleaseQueue; } // // If anything fails from here on, we have to unmap the ring. // Status =3D VgpuDev->VirtIo->SetQueueAddress ( VgpuDev->VirtIo, &VgpuDev->Ring, RingBaseShift ); if (EFI_ERROR (Status)) { goto UnmapQueue; } =20 // // 8. Set the DRIVER_OK status bit. // NextDevStat |=3D VSTAT_DRIVER_OK; Status =3D VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevSta= t); if (EFI_ERROR (Status)) { goto UnmapQueue; } =20 return EFI_SUCCESS; --=20 2.14.1.3.gb7cf6e02401b _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel