]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg/VirtioLib: add VirtioMapAllBytesInSharedBuffer() helper function
authorBrijesh Singh <brijesh.singh@amd.com>
Wed, 23 Aug 2017 10:57:16 +0000 (06:57 -0400)
committerLaszlo Ersek <lersek@redhat.com>
Fri, 25 Aug 2017 08:42:18 +0000 (10:42 +0200)
The function can be used for mapping the system physical address to virtio
device address using VIRTIO_DEVICE_PROTOCOL.MapSharedBuffer (). The
function helps with centralizing error handling, and it allows the caller
to pass in constant or other evaluated expressions for NumberOfBytes.

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>
[lersek@redhat.com: s/This/VirtIo/ in the new function's comment blocks]
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Regression-tested-by: Laszlo Ersek <lersek@redhat.com>
OvmfPkg/Include/Library/VirtioLib.h
OvmfPkg/Library/VirtioLib/VirtioLib.c

index 5badfb32917fab75f85a8eedb647adb22ee0463b..660ea2767781b940126f8ad022deb0200d13f040 100644 (file)
@@ -3,6 +3,7 @@
   Declarations of utility functions used by virtio device drivers.\r
 \r
   Copyright (C) 2012-2016, Red Hat, Inc.\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
@@ -235,4 +236,55 @@ Virtio10WriteFeatures (
   IN OUT UINT8                  *DeviceStatus\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
+  The VirtioMapAllBytesInSharedBuffer() is similar to VIRTIO_MAP_SHARED\r
+  with exception that NumberOfBytes is IN-only parameter. The function\r
+  maps all the bytes specified in NumberOfBytes param in one consecutive\r
+  range.\r
+\r
+  @param[in]     VirtIo           The virtio device for which the mapping is\r
+                                  requested.\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]     NumberOfBytes    Number of bytes to map.\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
+\r
+  @retval EFI_SUCCESS             The NumberOfBytes is succesfully mapped.\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. This includes the case\r
+                                  when NumberOfBytes bytes cannot be mapped\r
+                                  in one consecutive range.\r
+  @retval EFI_DEVICE_ERROR        The system hardware could not map the\r
+                                  requested address.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+VirtioMapAllBytesInSharedBuffer (\r
+  IN  VIRTIO_DEVICE_PROTOCOL  *VirtIo,\r
+  IN  VIRTIO_MAP_OPERATION    Operation,\r
+  IN  VOID                    *HostAddress,\r
+  IN  UINTN                   NumberOfBytes,\r
+  OUT EFI_PHYSICAL_ADDRESS    *DeviceAddress,\r
+  OUT VOID                    **Mapping\r
+  );\r
 #endif // _VIRTIO_LIB_H_\r
index 845f206369a39208bda7bf8c0d65285c6afe2dc7..4ad38f06982612a6799801f9250e04a1d8a9a37e 100644 (file)
@@ -4,6 +4,7 @@
 \r
   Copyright (C) 2012-2016, Red Hat, Inc.\r
   Portion of Copyright (C) 2013, ARM Ltd.\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
@@ -414,3 +415,87 @@ Virtio10WriteFeatures (
 \r
   return Status;\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
+  The VirtioMapAllBytesInSharedBuffer() is similar to VIRTIO_MAP_SHARED\r
+  with exception that NumberOfBytes is IN-only parameter. The function\r
+  maps all the bytes specified in NumberOfBytes param in one consecutive\r
+  range.\r
+\r
+  @param[in]     VirtIo           The virtio device for which the mapping is\r
+                                  requested.\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]     NumberOfBytes    Number of bytes to map.\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
+\r
+  @retval EFI_SUCCESS             The NumberOfBytes is succesfully mapped.\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. This includes the case\r
+                                  when NumberOfBytes bytes cannot be mapped\r
+                                  in one consecutive range.\r
+  @retval EFI_DEVICE_ERROR        The system hardware could not map the\r
+                                  requested address.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+VirtioMapAllBytesInSharedBuffer (\r
+  IN  VIRTIO_DEVICE_PROTOCOL  *VirtIo,\r
+  IN  VIRTIO_MAP_OPERATION    Operation,\r
+  IN  VOID                    *HostAddress,\r
+  IN  UINTN                   NumberOfBytes,\r
+  OUT EFI_PHYSICAL_ADDRESS    *DeviceAddress,\r
+  OUT VOID                    **Mapping\r
+  )\r
+{\r
+  EFI_STATUS            Status;\r
+  VOID                  *MapInfo;\r
+  UINTN                 Size;\r
+  EFI_PHYSICAL_ADDRESS  PhysicalAddress;\r
+\r
+  Size = NumberOfBytes;\r
+  Status = VirtIo->MapSharedBuffer (\r
+                     VirtIo,\r
+                     Operation,\r
+                     HostAddress,\r
+                     &Size,\r
+                     &PhysicalAddress,\r
+                     &MapInfo\r
+                     );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  if (Size < NumberOfBytes) {\r
+    goto Failed;\r
+  }\r
+\r
+  *Mapping = MapInfo;\r
+  *DeviceAddress = PhysicalAddress;\r
+\r
+  return EFI_SUCCESS;\r
+\r
+Failed:\r
+  VirtIo->UnmapSharedBuffer (VirtIo, MapInfo);\r
+  return EFI_OUT_OF_RESOURCES;\r
+}\r