]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg: introduce IOMMU-like member functions to VIRTIO_DEVICE_PROTOCOL
authorBrijesh Singh <brijesh.singh@amd.com>
Wed, 23 Aug 2017 10:57:15 +0000 (06:57 -0400)
committerLaszlo Ersek <lersek@redhat.com>
Fri, 25 Aug 2017 08:42:18 +0000 (10:42 +0200)
The patch extends VIRTIO_DEVICE_PROTOCOL to provide the following new
member functions:

- AllocateSharedPages : allocate a memory region suitable for sharing
   between guest and hypervisor (e.g ring buffer).

- FreeSharedPages: free the memory allocated using AllocateSharedPages ().

- MapSharedBuffer: map a host address to device address suitable to share
   with device for bus master operations.

- UnmapSharedBuffer: unmap the device address obtained through the
   MapSharedBuffer().

We're free to extend the protocol structure without changing the protocol
GUID, or bumping any protocol version fields (of which we currently have
none), because VIRTIO_DEVICE_PROTOCOL is internal to edk2 by design --
see the disclaimers in "VirtioDevice.h".

The patch implements Laszlo's recommendation [1].

[1] http://mid.mail-archive.com/841bec5f-6f6e-8b1f-25ba-0fd37a915b72@redhat.com

Suggested-by: Laszlo Ersek <lersek@redhat.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Brijesh Singh <brijesh.singh@amd.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Regression-tested-by: Laszlo Ersek <lersek@redhat.com>
OvmfPkg/Include/Protocol/VirtioDevice.h

index fc166bd1a2b49713125fec2fdd731eb87fee7cf3..9a01932958a2901bc0e45e068dcc558ef463b3d1 100644 (file)
@@ -5,6 +5,7 @@
   and should not be used outside of the EDK II tree.\r
 \r
   Copyright (c) 2013, ARM Ltd. All rights reserved.<BR>\r
+  Copyright (c) 2017, AMD Inc, All rights reserved.<BR>\r
 \r
   This program and the accompanying materials are licensed and made available\r
   under the terms and conditions of the BSD License which accompanies this\r
 \r
 typedef struct _VIRTIO_DEVICE_PROTOCOL  VIRTIO_DEVICE_PROTOCOL;\r
 \r
+//\r
+// VIRTIO Operation for VIRTIO_MAP_SHARED\r
+//\r
+typedef enum {\r
+  //\r
+  // A read operation from system memory by a bus master\r
+  //\r
+  VirtioOperationBusMasterRead,\r
+  //\r
+  // A write operation to system memory by a bus master\r
+  //\r
+  VirtioOperationBusMasterWrite,\r
+  //\r
+  // Provides both read and write access to system memory by both the\r
+  // processor and a bus master\r
+  //\r
+  VirtioOperationBusMasterCommonBuffer,\r
+} VIRTIO_MAP_OPERATION;\r
+\r
 /**\r
 \r
   Read a word from the device-specific I/O region of the Virtio Header.\r
@@ -321,6 +341,121 @@ EFI_STATUS
   IN UINT8                   DeviceStatus\r
   );\r
 \r
+/**\r
+\r
+  Allocates pages that are suitable for an VirtioOperationBusMasterCommonBuffer\r
+  mapping. This means that the buffer allocated by this function supports\r
+  simultaneous access by both the processor and the bus master. The device\r
+  address that the bus master uses to access the buffer must be retrieved with\r
+  a call to VIRTIO_MAP_SHARED.\r
+\r
+  @param[in]      This              The protocol instance pointer.\r
+\r
+  @param[in]      Pages             The number of pages to allocate.\r
+\r
+  @param[in,out]  HostAddress       A pointer to store the system memory base\r
+                                    address of the allocated range.\r
+\r
+  @retval EFI_SUCCESS               The requested memory pages were allocated.\r
+  @retval EFI_OUT_OF_RESOURCES      The memory pages could not be allocated.\r
+\r
+**/\r
+typedef\r
+EFI_STATUS\r
+(EFIAPI *VIRTIO_ALLOCATE_SHARED)(\r
+  IN     VIRTIO_DEVICE_PROTOCOL                   *This,\r
+  IN     UINTN                                    Pages,\r
+  IN OUT VOID                                     **HostAddress\r
+  );\r
+\r
+/**\r
+  Frees memory that was allocated with VIRTIO_ALLOCATE_SHARED.\r
+\r
+  @param[in]  This           The protocol instance pointer.\r
+\r
+  @param[in]  Pages          The number of pages to free.\r
+\r
+  @param[in]  HostAddress    The system memory base address of the allocated\r
+                             range.\r
+\r
+**/\r
+typedef\r
+VOID\r
+(EFIAPI *VIRTIO_FREE_SHARED)(\r
+  IN  VIRTIO_DEVICE_PROTOCOL                   *This,\r
+  IN  UINTN                                    Pages,\r
+  IN  VOID                                     *HostAddress\r
+  );\r
+\r
+/**\r
+  Provides the virtio device address required to access system memory from a\r
+  DMA bus master.\r
+\r
+  The interface follows the same usage pattern as defined in UEFI spec 2.6\r
+  (Section 13.2 PCI Root Bridge I/O Protocol)\r
+\r
+  @param[in]     This             The protocol instance pointer.\r
+\r
+  @param[in]     Operation        Indicates if the bus master is going to\r
+                                  read or write to system memory.\r
+\r
+  @param[in]     HostAddress      The system memory address to map to shared\r
+                                  buffer address.\r
+\r
+  @param[in,out] NumberOfBytes    On input the number of bytes to map.\r
+                                  On output the number of bytes that were\r
+                                  mapped.\r
+\r
+  @param[out]    DeviceAddress    The resulting shared map address for the\r
+                                  bus master to access the hosts HostAddress.\r
+\r
+  @param[out]    Mapping          A resulting token to pass to\r
+                                  VIRTIO_UNMAP_SHARED.\r
+\r
+  @retval EFI_SUCCESS             The range was mapped for the returned\r
+                                  NumberOfBytes.\r
+  @retval EFI_UNSUPPORTED         The HostAddress cannot be mapped as a\r
+                                  common buffer.\r
+  @retval EFI_INVALID_PARAMETER   One or more parameters are invalid.\r
+  @retval EFI_OUT_OF_RESOURCES    The request could not be completed due to\r
+                                  a lack of resources.\r
+  @retval EFI_DEVICE_ERROR        The system hardware could not map the\r
+                                  requested address.\r
+**/\r
+\r
+typedef\r
+EFI_STATUS\r
+(EFIAPI *VIRTIO_MAP_SHARED) (\r
+  IN     VIRTIO_DEVICE_PROTOCOL       *This,\r
+  IN     VIRTIO_MAP_OPERATION         Operation,\r
+  IN     VOID                         *HostAddress,\r
+  IN OUT UINTN                        *NumberOfBytes,\r
+  OUT    EFI_PHYSICAL_ADDRESS         *DeviceAddress,\r
+  OUT    VOID                         **Mapping\r
+  );\r
+\r
+/**\r
+  Completes the VIRTIO_MAP_SHARED operation and releases any corresponding\r
+  resources.\r
+\r
+  @param[in]  This               The protocol instance pointer.\r
+\r
+  @param[in]  Mapping            The mapping token returned from\r
+                                 VIRTIO_MAP_SHARED.\r
+\r
+  @retval EFI_SUCCESS            The range was unmapped.\r
+  @retval EFI_INVALID_PARAMETER  Mapping is not a value that was returned by\r
+                                 VIRTIO_MAP_SHARED. Passing an invalid Mapping\r
+                                 token can cause undefined behavior.\r
+  @retval EFI_DEVICE_ERROR       The data was not committed to the target\r
+                                 system memory.\r
+**/\r
+typedef\r
+EFI_STATUS\r
+(EFIAPI *VIRTIO_UNMAP_SHARED)(\r
+  IN  VIRTIO_DEVICE_PROTOCOL    *This,\r
+  IN  VOID                      *Mapping\r
+  );\r
 \r
 ///\r
 ///  This protocol provides an abstraction over the VirtIo transport layer\r
@@ -361,6 +496,14 @@ struct _VIRTIO_DEVICE_PROTOCOL {
   //\r
   VIRTIO_DEVICE_WRITE         WriteDevice;\r
   VIRTIO_DEVICE_READ          ReadDevice;\r
+\r
+  //\r
+  // Functions to allocate, free, map and unmap shared buffer\r
+  //\r
+  VIRTIO_ALLOCATE_SHARED      AllocateSharedPages;\r
+  VIRTIO_FREE_SHARED          FreeSharedPages;\r
+  VIRTIO_MAP_SHARED           MapSharedBuffer;\r
+  VIRTIO_UNMAP_SHARED         UnmapSharedBuffer;\r
 };\r
 \r
 extern EFI_GUID gVirtioDeviceProtocolGuid;\r