]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg/VirtioGpuDxe: helpers for backing store (de)allocation+(un)mapping
authorLaszlo Ersek <lersek@redhat.com>
Sat, 26 Aug 2017 20:15:46 +0000 (22:15 +0200)
committerLaszlo Ersek <lersek@redhat.com>
Fri, 1 Sep 2017 12:28:20 +0000 (14:28 +0200)
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 <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>
Tested-by: Brijesh Singh <brijesh.singh@amd.com>
OvmfPkg/VirtioGpuDxe/Commands.c
OvmfPkg/VirtioGpuDxe/DriverBinding.c
OvmfPkg/VirtioGpuDxe/Gop.c
OvmfPkg/VirtioGpuDxe/VirtioGpu.h

index c1951a807e98d9c32ceae7e41f436c77f28f4f07..595a3717d926fb8c08b7e7e5102e68bcdfce686e 100644 (file)
@@ -211,6 +211,126 @@ VirtioGpuUninit (
   VirtioRingUninit (VgpuDev->VirtIo, &VgpuDev->Ring);\r
 }\r
 \r
+/**\r
+  Allocate, zero and map memory, for bus master common buffer operation, to be\r
+  attached as backing store to a host-side VirtIo GPU resource.\r
+\r
+  @param[in]  VgpuDev        The VGPU_DEV object that represents the VirtIo GPU\r
+                             device.\r
+\r
+  @param[in]  NumberOfPages  The number of whole pages to allocate and map.\r
+\r
+  @param[out] HostAddress    The system memory address of the allocated area.\r
+\r
+  @param[out] DeviceAddress  The bus master device address of the allocated\r
+                             area. The VirtIo GPU device may be programmed to\r
+                             access the allocated area through DeviceAddress;\r
+                             DeviceAddress is to be passed to the\r
+                             VirtioGpuResourceAttachBacking() function, as the\r
+                             BackingStoreDeviceAddress parameter.\r
+\r
+  @param[out] Mapping        A resulting token to pass to\r
+                             VirtioGpuUnmapAndFreeBackingStore().\r
+\r
+  @retval EFI_SUCCESS  The requested number of pages has been allocated, zeroed\r
+                       and mapped.\r
+\r
+  @return              Status codes propagated from\r
+                       VgpuDev->VirtIo->AllocateSharedPages() and\r
+                       VirtioMapAllBytesInSharedBuffer().\r
+**/\r
+EFI_STATUS\r
+VirtioGpuAllocateZeroAndMapBackingStore (\r
+  IN  VGPU_DEV             *VgpuDev,\r
+  IN  UINTN                NumberOfPages,\r
+  OUT VOID                 **HostAddress,\r
+  OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r
+  OUT VOID                 **Mapping\r
+  )\r
+{\r
+  EFI_STATUS Status;\r
+  VOID       *NewHostAddress;\r
+\r
+  Status = VgpuDev->VirtIo->AllocateSharedPages (\r
+                              VgpuDev->VirtIo,\r
+                              NumberOfPages,\r
+                              &NewHostAddress\r
+                              );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Avoid exposing stale data to the device even temporarily: zero the area\r
+  // before mapping it.\r
+  //\r
+  ZeroMem (NewHostAddress, EFI_PAGES_TO_SIZE (NumberOfPages));\r
+\r
+  Status = VirtioMapAllBytesInSharedBuffer (\r
+             VgpuDev->VirtIo,                      // VirtIo\r
+             VirtioOperationBusMasterCommonBuffer, // Operation\r
+             NewHostAddress,                       // HostAddress\r
+             EFI_PAGES_TO_SIZE (NumberOfPages),    // NumberOfBytes\r
+             DeviceAddress,                        // DeviceAddress\r
+             Mapping                               // Mapping\r
+             );\r
+  if (EFI_ERROR (Status)) {\r
+    goto FreeSharedPages;\r
+  }\r
+\r
+  *HostAddress = NewHostAddress;\r
+  return EFI_SUCCESS;\r
+\r
+FreeSharedPages:\r
+  VgpuDev->VirtIo->FreeSharedPages (\r
+                     VgpuDev->VirtIo,\r
+                     NumberOfPages,\r
+                     NewHostAddress\r
+                     );\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Unmap and free memory originally allocated and mapped with\r
+  VirtioGpuAllocateZeroAndMapBackingStore().\r
+\r
+  If the memory allocated and mapped with\r
+  VirtioGpuAllocateZeroAndMapBackingStore() was attached to a host-side VirtIo\r
+  GPU resource with VirtioGpuResourceAttachBacking(), then the caller is\r
+  responsible for detaching the backing store from the same resource, with\r
+  VirtioGpuResourceDetachBacking(), before calling this function.\r
+\r
+  @param[in] VgpuDev        The VGPU_DEV object that represents the VirtIo GPU\r
+                            device.\r
+\r
+  @param[in] NumberOfPages  The NumberOfPages parameter originally passed to\r
+                            VirtioGpuAllocateZeroAndMapBackingStore().\r
+\r
+  @param[in] HostAddress    The HostAddress value originally output by\r
+                            VirtioGpuAllocateZeroAndMapBackingStore().\r
+\r
+  @param[in] Mapping        The token that was originally output by\r
+                            VirtioGpuAllocateZeroAndMapBackingStore().\r
+**/\r
+VOID\r
+VirtioGpuUnmapAndFreeBackingStore (\r
+  IN VGPU_DEV *VgpuDev,\r
+  IN UINTN    NumberOfPages,\r
+  IN VOID     *HostAddress,\r
+  IN VOID     *Mapping\r
+  )\r
+{\r
+  VgpuDev->VirtIo->UnmapSharedBuffer (\r
+                     VgpuDev->VirtIo,\r
+                     Mapping\r
+                     );\r
+  VgpuDev->VirtIo->FreeSharedPages (\r
+                     VgpuDev->VirtIo,\r
+                     NumberOfPages,\r
+                     HostAddress\r
+                     );\r
+}\r
+\r
 /**\r
   EFI_EVENT_NOTIFY function for the VGPU_DEV.ExitBoot event. It resets the\r
   VirtIo device, causing it to release its resources and to forget its\r
index 33c1ad3b31100dd445da502b48facc579980ac93..a44d52cc810be39a245d83f07e5d6f7207541858 100644 (file)
@@ -15,7 +15,6 @@
 \r
 **/\r
 \r
-#include <Library/BaseMemoryLib.h>\r
 #include <Library/DevicePathLib.h>\r
 #include <Library/MemoryAllocationLib.h>\r
 #include <Library/PrintLib.h>\r
index b3c5dae74d0e403dc34cdb6a45bef3029f97a330..507e1a770d10afe8188ec70b38df36c2dcdffdf3 100644 (file)
@@ -14,7 +14,6 @@
 \r
 **/\r
 \r
-#include <Library/BaseMemoryLib.h>\r
 #include <Library/MemoryAllocationLib.h>\r
 \r
 #include "VirtioGpu.h"\r
index cf2a63accd7221eff1c07ff5aad8c3dc83d8510b..65b1bd6088b8890da05f067b27cc8f27a5f3de63 100644 (file)
@@ -18,6 +18,7 @@
 #define _VIRTIO_GPU_DXE_H_\r
 \r
 #include <IndustryStandard/VirtioGpu.h>\r
+#include <Library/BaseMemoryLib.h>\r
 #include <Library/DebugLib.h>\r
 #include <Library/UefiLib.h>\r
 #include <Protocol/GraphicsOutput.h>\r
@@ -189,6 +190,73 @@ VirtioGpuUninit (
   IN OUT VGPU_DEV *VgpuDev\r
   );\r
 \r
+/**\r
+  Allocate, zero and map memory, for bus master common buffer operation, to be\r
+  attached as backing store to a host-side VirtIo GPU resource.\r
+\r
+  @param[in]  VgpuDev        The VGPU_DEV object that represents the VirtIo GPU\r
+                             device.\r
+\r
+  @param[in]  NumberOfPages  The number of whole pages to allocate and map.\r
+\r
+  @param[out] HostAddress    The system memory address of the allocated area.\r
+\r
+  @param[out] DeviceAddress  The bus master device address of the allocated\r
+                             area. The VirtIo GPU device may be programmed to\r
+                             access the allocated area through DeviceAddress;\r
+                             DeviceAddress is to be passed to the\r
+                             VirtioGpuResourceAttachBacking() function, as the\r
+                             BackingStoreDeviceAddress parameter.\r
+\r
+  @param[out] Mapping        A resulting token to pass to\r
+                             VirtioGpuUnmapAndFreeBackingStore().\r
+\r
+  @retval EFI_SUCCESS  The requested number of pages has been allocated, zeroed\r
+                       and mapped.\r
+\r
+  @return              Status codes propagated from\r
+                       VgpuDev->VirtIo->AllocateSharedPages() and\r
+                       VirtioMapAllBytesInSharedBuffer().\r
+**/\r
+EFI_STATUS\r
+VirtioGpuAllocateZeroAndMapBackingStore (\r
+  IN  VGPU_DEV             *VgpuDev,\r
+  IN  UINTN                NumberOfPages,\r
+  OUT VOID                 **HostAddress,\r
+  OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r
+  OUT VOID                 **Mapping\r
+  );\r
+\r
+/**\r
+  Unmap and free memory originally allocated and mapped with\r
+  VirtioGpuAllocateZeroAndMapBackingStore().\r
+\r
+  If the memory allocated and mapped with\r
+  VirtioGpuAllocateZeroAndMapBackingStore() was attached to a host-side VirtIo\r
+  GPU resource with VirtioGpuResourceAttachBacking(), then the caller is\r
+  responsible for detaching the backing store from the same resource, with\r
+  VirtioGpuResourceDetachBacking(), before calling this function.\r
+\r
+  @param[in] VgpuDev        The VGPU_DEV object that represents the VirtIo GPU\r
+                            device.\r
+\r
+  @param[in] NumberOfPages  The NumberOfPages parameter originally passed to\r
+                            VirtioGpuAllocateZeroAndMapBackingStore().\r
+\r
+  @param[in] HostAddress    The HostAddress value originally output by\r
+                            VirtioGpuAllocateZeroAndMapBackingStore().\r
+\r
+  @param[in] Mapping        The token that was originally output by\r
+                            VirtioGpuAllocateZeroAndMapBackingStore().\r
+**/\r
+VOID\r
+VirtioGpuUnmapAndFreeBackingStore (\r
+  IN VGPU_DEV *VgpuDev,\r
+  IN UINTN    NumberOfPages,\r
+  IN VOID     *HostAddress,\r
+  IN VOID     *Mapping\r
+  );\r
+\r
 /**\r
   EFI_EVENT_NOTIFY function for the VGPU_DEV.ExitBoot event. It resets the\r
   VirtIo device, causing it to release its resources and to forget its\r