+ 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