]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLib.c
OvmfPkg/QemuFwCfgLib: Add SEV support
[mirror_edk2.git] / OvmfPkg / Library / QemuFwCfgLib / QemuFwCfgLib.c
index 5c96d2af2532aafd2cb9b73f5c9ce1a0a28a1d9c..dbebd36b18530dbebac6f7fc1e7a1ea4fd4c2966 100644 (file)
@@ -2,6 +2,7 @@
 \r
   Copyright (c) 2011 - 2013, Intel Corporation. All rights reserved.<BR>\r
   Copyright (C) 2013, Red Hat, Inc.\r
 \r
   Copyright (c) 2011 - 2013, Intel Corporation. All rights reserved.<BR>\r
   Copyright (C) 2013, Red Hat, Inc.\r
+  Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
 \r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
 \r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
 #include <Library/MemoryAllocationLib.h>\r
 #include <Library/UefiBootServicesTableLib.h>\r
 \r
 #include <Library/MemoryAllocationLib.h>\r
 #include <Library/UefiBootServicesTableLib.h>\r
 \r
-\r
-/**\r
-  Reads an 8-bit I/O port fifo into a block of memory.\r
-\r
-  Reads the 8-bit I/O fifo port specified by Port.\r
-\r
-  The port is read Count times, and the read data is\r
-  stored in the provided Buffer.\r
-\r
-  This function must guarantee that all I/O read and write operations are\r
-  serialized.\r
-\r
-  If 8-bit I/O port operations are not supported, then ASSERT().\r
-\r
-  @param  Port    The I/O port to read.\r
-  @param  Count   The number of times to read I/O port.\r
-  @param  Buffer  The buffer to store the read data into.\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-IoReadFifo8 (\r
-  IN      UINTN                     Port,\r
-  IN      UINTN                     Count,\r
-  OUT     VOID                      *Buffer\r
-  );\r
-\r
-/**\r
-  Writes an 8-bit I/O port fifo from a block of memory.\r
-\r
-  Writes the 8-bit I/O fifo port specified by Port.\r
-\r
-  The port is written Count times, and the data are obtained\r
-  from the provided Buffer.\r
-\r
-  This function must guarantee that all I/O read and write operations are\r
-  serialized.\r
-\r
-  If 8-bit I/O port operations are not supported, then ASSERT().\r
-\r
-  @param  Port    The I/O port to read.\r
-  @param  Count   The number of times to read I/O port.\r
-  @param  Buffer  The buffer to store the read data into.\r
-\r
-**/\r
-VOID\r
-EFIAPI\r
-IoWriteFifo8 (\r
-  IN      UINTN                     Port,\r
-  IN      UINTN                     Count,\r
-  OUT     VOID                      *Buffer\r
-  );\r
+#include "QemuFwCfgLibInternal.h"\r
 \r
 \r
 /**\r
 \r
 \r
 /**\r
@@ -92,7 +42,131 @@ QemuFwCfgSelectItem (
   )\r
 {\r
   DEBUG ((EFI_D_INFO, "Select Item: 0x%x\n", (UINT16)(UINTN) QemuFwCfgItem));\r
   )\r
 {\r
   DEBUG ((EFI_D_INFO, "Select Item: 0x%x\n", (UINT16)(UINTN) QemuFwCfgItem));\r
-  IoWrite16 (0x510, (UINT16)(UINTN) QemuFwCfgItem);\r
+  IoWrite16 (FW_CFG_IO_SELECTOR, (UINT16)(UINTN) QemuFwCfgItem);\r
+}\r
+\r
+\r
+/**\r
+  Transfer an array of bytes, or skip a number of bytes, using the DMA\r
+  interface.\r
+\r
+  @param[in]     Size     Size in bytes to transfer or skip.\r
+\r
+  @param[in,out] Buffer   Buffer to read data into or write data from. Ignored,\r
+                          and may be NULL, if Size is zero, or Control is\r
+                          FW_CFG_DMA_CTL_SKIP.\r
+\r
+  @param[in]     Control  One of the following:\r
+                          FW_CFG_DMA_CTL_WRITE - write to fw_cfg from Buffer.\r
+                          FW_CFG_DMA_CTL_READ  - read from fw_cfg into Buffer.\r
+                          FW_CFG_DMA_CTL_SKIP  - skip bytes in fw_cfg.\r
+**/\r
+VOID\r
+InternalQemuFwCfgDmaBytes (\r
+  IN     UINT32   Size,\r
+  IN OUT VOID     *Buffer OPTIONAL,\r
+  IN     UINT32   Control\r
+  )\r
+{\r
+  volatile FW_CFG_DMA_ACCESS LocalAccess;\r
+  volatile FW_CFG_DMA_ACCESS *Access;\r
+  UINT32                     AccessHigh, AccessLow;\r
+  UINT32                     Status;\r
+  UINT32                     NumPages;\r
+  VOID                       *DmaBuffer, *BounceBuffer;\r
+\r
+  ASSERT (Control == FW_CFG_DMA_CTL_WRITE || Control == FW_CFG_DMA_CTL_READ ||\r
+    Control == FW_CFG_DMA_CTL_SKIP);\r
+\r
+  if (Size == 0) {\r
+    return;\r
+  }\r
+\r
+  //\r
+  // When SEV is enabled then allocate DMA bounce buffer\r
+  //\r
+  if (InternalQemuFwCfgSevIsEnabled ()) {\r
+    UINTN  TotalSize;\r
+\r
+    TotalSize = sizeof (*Access);\r
+    //\r
+    // Skip operation does not need buffer\r
+    //\r
+    if (Control != FW_CFG_DMA_CTL_SKIP) {\r
+      TotalSize += Size;\r
+    }\r
+\r
+    //\r
+    // Allocate SEV DMA buffer\r
+    //\r
+    NumPages = (UINT32)EFI_SIZE_TO_PAGES (TotalSize);\r
+    InternalQemuFwCfgSevDmaAllocateBuffer (&BounceBuffer, NumPages);\r
+\r
+    Access = BounceBuffer;\r
+    DmaBuffer = (UINT8*)BounceBuffer + sizeof (*Access);\r
+\r
+    //\r
+    //  Decrypt data from encrypted guest buffer into DMA buffer\r
+    //\r
+    if (Control == FW_CFG_DMA_CTL_WRITE) {\r
+      CopyMem (DmaBuffer, Buffer, Size);\r
+    }\r
+  } else {\r
+    Access = &LocalAccess;\r
+    DmaBuffer = Buffer;\r
+    BounceBuffer = NULL;\r
+  }\r
+\r
+  Access->Control = SwapBytes32 (Control);\r
+  Access->Length  = SwapBytes32 (Size);\r
+  Access->Address = SwapBytes64 ((UINTN)DmaBuffer);\r
+\r
+  //\r
+  // Delimit the transfer from (a) modifications to Access, (b) in case of a\r
+  // write, from writes to Buffer by the caller.\r
+  //\r
+  MemoryFence ();\r
+\r
+  //\r
+  // Start the transfer.\r
+  //\r
+  AccessHigh = (UINT32)RShiftU64 ((UINTN)Access, 32);\r
+  AccessLow  = (UINT32)(UINTN)Access;\r
+  IoWrite32 (FW_CFG_IO_DMA_ADDRESS,     SwapBytes32 (AccessHigh));\r
+  IoWrite32 (FW_CFG_IO_DMA_ADDRESS + 4, SwapBytes32 (AccessLow));\r
+\r
+  //\r
+  // Don't look at Access.Control before starting the transfer.\r
+  //\r
+  MemoryFence ();\r
+\r
+  //\r
+  // Wait for the transfer to complete.\r
+  //\r
+  do {\r
+    Status = SwapBytes32 (Access->Control);\r
+    ASSERT ((Status & FW_CFG_DMA_CTL_ERROR) == 0);\r
+  } while (Status != 0);\r
+\r
+  //\r
+  // After a read, the caller will want to use Buffer.\r
+  //\r
+  MemoryFence ();\r
+\r
+  //\r
+  // If Bounce buffer was allocated then copy the data into guest buffer and\r
+  // free the bounce buffer\r
+  //\r
+  if (BounceBuffer != NULL) {\r
+    //\r
+    //  Encrypt the data from DMA buffer into guest buffer\r
+    //\r
+    if (Control == FW_CFG_DMA_CTL_READ) {\r
+      CopyMem (Buffer, DmaBuffer, Size);\r
+    }\r
+\r
+    InternalQemuFwCfgSevDmaFreeBuffer (BounceBuffer, NumPages);\r
+  }\r
 }\r
 \r
 \r
 }\r
 \r
 \r
@@ -110,7 +184,11 @@ InternalQemuFwCfgReadBytes (
   IN VOID                   *Buffer  OPTIONAL\r
   )\r
 {\r
   IN VOID                   *Buffer  OPTIONAL\r
   )\r
 {\r
-  IoReadFifo8 (0x511, Size, Buffer);\r
+  if (InternalQemuFwCfgDmaIsAvailable () && Size <= MAX_UINT32) {\r
+    InternalQemuFwCfgDmaBytes ((UINT32)Size, Buffer, FW_CFG_DMA_CTL_READ);\r
+    return;\r
+  }\r
+  IoReadFifo8 (FW_CFG_IO_DATA, Size, Buffer);\r
 }\r
 \r
 \r
 }\r
 \r
 \r
@@ -158,7 +236,55 @@ QemuFwCfgWriteBytes (
   )\r
 {\r
   if (InternalQemuFwCfgIsAvailable ()) {\r
   )\r
 {\r
   if (InternalQemuFwCfgIsAvailable ()) {\r
-    IoWriteFifo8 (0x511, Size, Buffer);\r
+    if (InternalQemuFwCfgDmaIsAvailable () && Size <= MAX_UINT32) {\r
+      InternalQemuFwCfgDmaBytes ((UINT32)Size, Buffer, FW_CFG_DMA_CTL_WRITE);\r
+      return;\r
+    }\r
+    IoWriteFifo8 (FW_CFG_IO_DATA, Size, Buffer);\r
+  }\r
+}\r
+\r
+\r
+/**\r
+  Skip bytes in the firmware configuration item.\r
+\r
+  Increase the offset of the firmware configuration item without transferring\r
+  bytes between the item and a caller-provided buffer. Subsequent read, write\r
+  or skip operations will commence at the increased offset.\r
+\r
+  @param[in] Size  Number of bytes to skip.\r
+**/\r
+VOID\r
+EFIAPI\r
+QemuFwCfgSkipBytes (\r
+  IN UINTN                  Size\r
+  )\r
+{\r
+  UINTN ChunkSize;\r
+  UINT8 SkipBuffer[256];\r
+\r
+  if (!InternalQemuFwCfgIsAvailable ()) {\r
+    return;\r
+  }\r
+\r
+  if (InternalQemuFwCfgDmaIsAvailable () && Size <= MAX_UINT32) {\r
+    InternalQemuFwCfgDmaBytes ((UINT32)Size, NULL, FW_CFG_DMA_CTL_SKIP);\r
+    return;\r
+  }\r
+\r
+  //\r
+  // Emulate the skip by reading data in chunks, and throwing it away. The\r
+  // implementation below is suitable even for phases where RAM or dynamic\r
+  // allocation is not available or appropriate. It also doesn't affect the\r
+  // static data footprint for client modules. Large skips are not expected,\r
+  // therefore this fallback is not performance critical. The size of\r
+  // SkipBuffer is thought not to exert a large pressure on the stack in any\r
+  // phase.\r
+  //\r
+  while (Size > 0) {\r
+    ChunkSize = MIN (Size, sizeof SkipBuffer);\r
+    IoReadFifo8 (FW_CFG_IO_DATA, ChunkSize, SkipBuffer);\r
+    Size -= ChunkSize;\r
   }\r
 }\r
 \r
   }\r
 }\r
 \r
@@ -295,31 +421,3 @@ QemuFwCfgFindFile (
 \r
   return RETURN_NOT_FOUND;\r
 }\r
 \r
   return RETURN_NOT_FOUND;\r
 }\r
-\r
-\r
-/**\r
-  Determine if S3 support is explicitly enabled.\r
-\r
-  @retval  TRUE   if S3 support is explicitly enabled.\r
-           FALSE  otherwise. This includes unavailability of the firmware\r
-                  configuration interface.\r
-**/\r
-BOOLEAN\r
-EFIAPI\r
-QemuFwCfgS3Enabled (\r
-  VOID\r
-  )\r
-{\r
-  RETURN_STATUS        Status;\r
-  FIRMWARE_CONFIG_ITEM FwCfgItem;\r
-  UINTN                FwCfgSize;\r
-  UINT8                SystemStates[6];\r
-\r
-  Status = QemuFwCfgFindFile ("etc/system-states", &FwCfgItem, &FwCfgSize);\r
-  if (Status != RETURN_SUCCESS || FwCfgSize != sizeof SystemStates) {\r
-    return FALSE;\r
-  }\r
-  QemuFwCfgSelectItem (FwCfgItem);\r
-  QemuFwCfgReadBytes (sizeof SystemStates, SystemStates);\r
-  return (BOOLEAN) (SystemStates[3] & BIT7);\r
-}\r