From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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) 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 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) From: Laszlo Ersek To: edk2-devel-01 Cc: Ard Biesheuvel , Brijesh Singh , Jordan Justen , Tom Lendacky 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: [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: , X-List-Received-Date: Mon, 28 Aug 2017 13:22:06 -0000 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 --- OvmfPkg/VirtioGpuDxe/Commands.c | 83 ++++++++++++++++++-- 1 file changed, 75 insertions(+), 8 deletions(-) diff --git a/OvmfPkg/VirtioGpuDxe/Commands.c b/OvmfPkg/VirtioGpuDxe/Commands.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; VgpuDev = Context; VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, 0); VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, VgpuDev->RingMap); } /** Internal utility function that sends a request to the VirtIo GPU device model, awaits the answer from the host, and returns a status. @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU device. The caller is responsible to have successfully invoked VirtioGpuInit() on VgpuDev previously, while VirtioGpuUninit() must not have been called on VgpuDev. @param[in] RequestType The type of the request. The caller is responsible for providing a VirtioGpuCmd* RequestType which, on success, elicits a VirtioGpuRespOkNodata response from the host. @param[in] Fence Whether to enable fencing for this request. Fencing forces the host to complete the command before producing a response. If Fence is TRUE, then VgpuDev->FenceId is consumed, and incremented. @param[in,out] Header Pointer to the caller-allocated request object. The request must start with VIRTIO_GPU_CONTROL_HEADER. This function overwrites all fields of Header before submitting the request to the host: - it sets Type from RequestType, - it sets Flags and FenceId based on Fence, - it zeroes CtxId and Padding. @param[in] RequestSize Size of the entire caller-allocated request object, including the leading VIRTIO_GPU_CONTROL_HEADER. @retval EFI_SUCCESS Operation successful. @retval EFI_DEVICE_ERROR The host rejected the request. The host error code has been logged on the EFI_D_ERROR level. @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; // // Initialize Header. // Header->Type = RequestType; if (Fence) { Header->Flags = VIRTIO_GPU_FLAG_FENCE; Header->FenceId = VgpuDev->FenceId++; } else { Header->Flags = 0; Header->FenceId = 0; } Header->CtxId = 0; Header->Padding = 0; ASSERT (RequestSize >= sizeof *Header); ASSERT (RequestSize <= MAX_UINT32); + // + // Map request and response to bus master device addresses. + // + Status = VirtioMapAllBytesInSharedBuffer ( + VgpuDev->VirtIo, + VirtioOperationBusMasterRead, + (VOID *)Header, + RequestSize, + &RequestDeviceAddress, + &RequestMap + ); + if (EFI_ERROR (Status)) { + return Status; + } + Status = 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 + ); // // Send the command. // Status = VirtioFlush (VgpuDev->VirtIo, VIRTIO_GPU_CONTROL_QUEUE, &VgpuDev->Ring, &Indices, &ResponseSize); if (EFI_ERROR (Status)) { - return Status; + goto UnmapResponse; } // - // Parse the response. + // Verify response size. // if (ResponseSize != sizeof Response) { DEBUG ((EFI_D_ERROR, "%a: malformed response to Request=0x%x\n", __FUNCTION__, (UINT32)RequestType)); - return EFI_PROTOCOL_ERROR; + Status = EFI_PROTOCOL_ERROR; + goto UnmapResponse; } + // + // 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 = VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, ResponseMap); + if (EFI_ERROR (Status)) { + goto UnmapRequest; + } + Status = VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, RequestMap); + if (EFI_ERROR (Status)) { + return Status; + } + + // + // Parse the response. + // if (Response.Type == VirtioGpuRespOkNodata) { return EFI_SUCCESS; } DEBUG ((EFI_D_ERROR, "%a: Request=0x%x Response=0x%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; } /** The following functions send requests to the VirtIo GPU device model, await the answer from the host, and return a status. They share the following interface details: @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU device. The caller is responsible to have successfully invoked VirtioGpuInit() on VgpuDev previously, while VirtioGpuUninit() must not have been called on VgpuDev. @retval EFI_INVALID_PARAMETER Invalid command-specific parameters were detected by this driver. @retval EFI_SUCCESS Operation successful. @retval EFI_DEVICE_ERROR The host rejected the request. The host error code has been logged on the EFI_D_ERROR level. @return Codes for unexpected errors in VirtIo messaging. For the command-specific parameters, please consult the GPU Device section of the VirtIo 1.0 specification (see references in "OvmfPkg/Include/IndustryStandard/VirtioGpu.h"). **/ -- 2.14.1.3.gb7cf6e02401b