]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLib.c
OvmfPkg: Modify QemuFwCfgLib to use new IoLib class library
[mirror_edk2.git] / OvmfPkg / Library / QemuFwCfgLib / QemuFwCfgLib.c
index 985b383c26b80249a9542f821c89fabafc9db53b..a228c029dfb909edadb3247017264c182ea6f0e7 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
-STATIC BOOLEAN mQemuFwCfgSupported = FALSE;\r
+#include "QemuFwCfgLibInternal.h"\r
 \r
 \r
 /**\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
+  Selects a firmware configuration item for reading.\r
+  \r
+  Following this call, any data read from this item will start from\r
+  the beginning of the configuration item's data.\r
 \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
+  @param[in] QemuFwCfgItem - Firmware Configuration item to read\r
 \r
 **/\r
 VOID\r
 EFIAPI\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
+QemuFwCfgSelectItem (\r
+  IN FIRMWARE_CONFIG_ITEM   QemuFwCfgItem\r
+  )\r
+{\r
+  DEBUG ((EFI_D_INFO, "Select Item: 0x%x\n", (UINT16)(UINTN) QemuFwCfgItem));\r
+  IoWrite16 (0x510, (UINT16)(UINTN) QemuFwCfgItem);\r
+}\r
 \r
 \r
-  If 8-bit I/O port operations are not supported, then ASSERT().\r
 \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
+  Transfer an array of bytes using the DMA interface.\r
 \r
 \r
+  @param[in]     Size    Size in bytes to transfer.\r
+  @param[in,out] Buffer  Buffer to read data into or write data from. May be\r
+                         NULL if Size is zero.\r
+  @param[in]     Write   TRUE if writing to fw_cfg from Buffer, FALSE if\r
+                         reading from fw_cfg into Buffer.\r
 **/\r
 VOID\r
 **/\r
 VOID\r
-EFIAPI\r
-IoWriteFifo8 (\r
-  IN      UINTN                     Port,\r
-  IN      UINTN                     Count,\r
-  OUT     VOID                      *Buffer\r
-  );\r
-\r
-\r
-/**\r
-  Returns a boolean indicating if the firmware configuration interface\r
-  is available or not.\r
+InternalQemuFwCfgDmaBytes (\r
+  IN     UINT32   Size,\r
+  IN OUT VOID     *Buffer OPTIONAL,\r
+  IN     BOOLEAN  Write\r
+  )\r
+{\r
+  volatile FW_CFG_DMA_ACCESS Access;\r
+  UINT32                     AccessHigh, AccessLow;\r
+  UINT32                     Status;\r
 \r
 \r
-  This function may change fw_cfg state.\r
+  if (Size == 0) {\r
+    return;\r
+  }\r
 \r
 \r
-  @retval    TRUE   The interface is available\r
-  @retval    FALSE  The interface is not available\r
+  Access.Control = SwapBytes32 (\r
+                    Write ? FW_CFG_DMA_CTL_WRITE : FW_CFG_DMA_CTL_READ\r
+                    );\r
+  Access.Length  = SwapBytes32 (Size);\r
+  Access.Address = SwapBytes64 ((UINTN)Buffer);\r
 \r
 \r
-**/\r
-BOOLEAN\r
-EFIAPI\r
-QemuFwCfgIsAvailable (\r
-  VOID\r
-  )\r
-{\r
-  return InternalQemuFwCfgIsAvailable ();\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
+  //\r
+  // Start the transfer.\r
+  //\r
+  AccessHigh = (UINT32)RShiftU64 ((UINTN)&Access, 32);\r
+  AccessLow  = (UINT32)(UINTN)&Access;\r
+  IoWrite32 (0x514, SwapBytes32 (AccessHigh));\r
+  IoWrite32 (0x518, SwapBytes32 (AccessLow));\r
 \r
 \r
-/**\r
-  Selects a firmware configuration item for reading.\r
-  \r
-  Following this call, any data read from this item will start from\r
-  the beginning of the configuration item's data.\r
+  //\r
+  // Don't look at Access.Control before starting the transfer.\r
+  //\r
+  MemoryFence ();\r
 \r
 \r
-  @param[in] QemuFwCfgItem - Firmware Configuration item to read\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
-**/\r
-VOID\r
-EFIAPI\r
-QemuFwCfgSelectItem (\r
-  IN FIRMWARE_CONFIG_ITEM   QemuFwCfgItem\r
-  )\r
-{\r
-  DEBUG ((EFI_D_INFO, "Select Item: 0x%x\n", (UINT16)(UINTN) QemuFwCfgItem));\r
-  IoWrite16 (0x510, (UINT16)(UINTN) QemuFwCfgItem);\r
+  //\r
+  // After a read, the caller will want to use Buffer.\r
+  //\r
+  MemoryFence ();\r
 }\r
 \r
 \r
 }\r
 \r
 \r
@@ -132,6 +124,10 @@ InternalQemuFwCfgReadBytes (
   IN VOID                   *Buffer  OPTIONAL\r
   )\r
 {\r
   IN VOID                   *Buffer  OPTIONAL\r
   )\r
 {\r
+  if (InternalQemuFwCfgDmaIsAvailable () && Size <= MAX_UINT32) {\r
+    InternalQemuFwCfgDmaBytes ((UINT32)Size, Buffer, FALSE);\r
+    return;\r
+  }\r
   IoReadFifo8 (0x511, Size, Buffer);\r
 }\r
 \r
   IoReadFifo8 (0x511, Size, Buffer);\r
 }\r
 \r
@@ -180,6 +176,10 @@ QemuFwCfgWriteBytes (
   )\r
 {\r
   if (InternalQemuFwCfgIsAvailable ()) {\r
   )\r
 {\r
   if (InternalQemuFwCfgIsAvailable ()) {\r
+    if (InternalQemuFwCfgDmaIsAvailable () && Size <= MAX_UINT32) {\r
+      InternalQemuFwCfgDmaBytes ((UINT32)Size, Buffer, TRUE);\r
+      return;\r
+    }\r
     IoWriteFifo8 (0x511, Size, Buffer);\r
   }\r
 }\r
     IoWriteFifo8 (0x511, Size, Buffer);\r
   }\r
 }\r
@@ -265,39 +265,6 @@ QemuFwCfgRead64 (
 }\r
 \r
 \r
 }\r
 \r
 \r
-RETURN_STATUS\r
-EFIAPI\r
-QemuFwCfgInitialize (\r
-  VOID\r
-  )\r
-{\r
-  UINT32 Signature;\r
-  UINT32 Revision;\r
-\r
-  //\r
-  // Enable the access routines while probing to see if it is supported.\r
-  //\r
-  mQemuFwCfgSupported = TRUE;\r
-\r
-  QemuFwCfgSelectItem (QemuFwCfgItemSignature);\r
-  Signature = QemuFwCfgRead32 ();\r
-  DEBUG ((EFI_D_INFO, "FW CFG Signature: 0x%x\n", Signature));\r
-  QemuFwCfgSelectItem (QemuFwCfgItemInterfaceVersion);\r
-  Revision = QemuFwCfgRead32 ();\r
-  DEBUG ((EFI_D_INFO, "FW CFG Revision: 0x%x\n", Revision));\r
-  if ((Signature != SIGNATURE_32 ('Q', 'E', 'M', 'U')) ||\r
-      (Revision < 1)\r
-     ) {\r
-    DEBUG ((EFI_D_INFO, "QemuFwCfg interface not supported.\n"));\r
-    mQemuFwCfgSupported = FALSE;\r
-    return RETURN_SUCCESS;\r
-  }\r
-\r
-  DEBUG ((EFI_D_INFO, "QemuFwCfg interface is supported.\n"));\r
-  return RETURN_SUCCESS;\r
-}\r
-\r
-\r
 /**\r
   Find the configuration item corresponding to the firmware configuration file.\r
 \r
 /**\r
   Find the configuration item corresponding to the firmware configuration file.\r
 \r
@@ -333,11 +300,12 @@ QemuFwCfgFindFile (
     UINT32 FileSize;\r
     UINT16 FileSelect;\r
     UINT16 FileReserved;\r
     UINT32 FileSize;\r
     UINT16 FileSelect;\r
     UINT16 FileReserved;\r
-    CHAR8  FName[56];\r
+    CHAR8  FName[QEMU_FW_CFG_FNAME_SIZE];\r
 \r
     FileSize     = QemuFwCfgRead32 ();\r
     FileSelect   = QemuFwCfgRead16 ();\r
     FileReserved = QemuFwCfgRead16 ();\r
 \r
     FileSize     = QemuFwCfgRead32 ();\r
     FileSelect   = QemuFwCfgRead16 ();\r
     FileReserved = QemuFwCfgRead16 ();\r
+    (VOID) FileReserved; /* Force a do-nothing reference. */\r
     InternalQemuFwCfgReadBytes (sizeof (FName), FName);\r
 \r
     if (AsciiStrCmp (Name, FName) == 0) {\r
     InternalQemuFwCfgReadBytes (sizeof (FName), FName);\r
 \r
     if (AsciiStrCmp (Name, FName) == 0) {\r
@@ -352,19 +320,28 @@ QemuFwCfgFindFile (
 \r
 \r
 /**\r
 \r
 \r
 /**\r
-  Returns a boolean indicating if the firmware configuration interface is\r
-  available for library-internal purposes.\r
+  Determine if S3 support is explicitly enabled.\r
 \r
 \r
-  This function never changes fw_cfg state.\r
-\r
-  @retval    TRUE   The interface is available internally.\r
-  @retval    FALSE  The interface is not available internally.\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
 **/\r
 BOOLEAN\r
 EFIAPI\r
-InternalQemuFwCfgIsAvailable (\r
+QemuFwCfgS3Enabled (\r
   VOID\r
   )\r
 {\r
   VOID\r
   )\r
 {\r
-  return mQemuFwCfgSupported;\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
 }\r