[edk2] [PATCH 2/6] OvmfPkg/VirtioGpuDxe: map virtio GPU command objects to device addresses

Laszlo Ersek posted 6 patches 7 years, 3 months ago
[edk2] [PATCH 2/6] OvmfPkg/VirtioGpuDxe: map virtio GPU command objects to device addresses
Posted by Laszlo Ersek 7 years, 3 months ago
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 <ard.biesheuvel@linaro.org>
Cc: Brijesh Singh <brijesh.singh@amd.com>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
---
 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


_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel