]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ArmPkg/Library/ArmDmaLib/ArmDmaLib.c
ArmPkg/ArmDmaLib: add support for fixed host-to-device DMA offset
[mirror_edk2.git] / ArmPkg / Library / ArmDmaLib / ArmDmaLib.c
index 0e6fdce8ab52e3a05baba5e63d3a4b2ab387fc88..acc106bcf4a8b609943660ac1293cf2571607c8e 100644 (file)
 #include <Library/UncachedMemoryAllocationLib.h>\r
 #include <Library/IoLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
-#include <Library/ArmLib.h>\r
 \r
 #include <Protocol/Cpu.h>\r
 \r
 typedef struct {\r
   EFI_PHYSICAL_ADDRESS      HostAddress;\r
-  EFI_PHYSICAL_ADDRESS      DeviceAddress;\r
+  VOID                      *BufferAddress;\r
   UINTN                     NumberOfBytes;\r
   DMA_MAP_OPERATION         Operation;\r
   BOOLEAN                   DoubleBuffer;\r
@@ -36,8 +35,16 @@ typedef struct {
 \r
 \r
 \r
-EFI_CPU_ARCH_PROTOCOL      *gCpu;\r
-UINTN                      gCacheAlignment = 0;\r
+STATIC EFI_CPU_ARCH_PROTOCOL      *mCpu;\r
+\r
+STATIC\r
+PHYSICAL_ADDRESS\r
+HostToDeviceAddress (\r
+  IN  PHYSICAL_ADDRESS  HostAddress\r
+  )\r
+{\r
+  return HostAddress + PcdGet64 (PcdArmDmaDeviceOffset);\r
+}\r
 \r
 /**\r
   Provides the DMA controller-specific addresses needed to access system memory.\r
@@ -82,7 +89,14 @@ DmaMap (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  *DeviceAddress = ConvertToPhysicalAddress (HostAddress);\r
+  //\r
+  // The debug implementation of UncachedMemoryAllocationLib in ArmPkg returns\r
+  // a virtual uncached alias, and unmaps the cached ID mapping of the buffer,\r
+  // in order to catch inadvertent references to the cached mapping.\r
+  // Since HostToDeviceAddress () expects ID mapped input addresses, convert\r
+  // the host address to an ID mapped address first.\r
+  //\r
+  *DeviceAddress = HostToDeviceAddress (ConvertToPhysicalAddress (HostAddress));\r
 \r
   // Remember range so we can flush on the other side\r
   Map = AllocatePool (sizeof (MAP_INFO_INSTANCE));\r
@@ -90,15 +104,13 @@ DmaMap (
     return  EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
-  *Mapping = Map;\r
-\r
-  if ((((UINTN)HostAddress & (gCacheAlignment - 1)) != 0) ||\r
-      ((*NumberOfBytes & (gCacheAlignment - 1)) != 0)) {\r
+  if ((((UINTN)HostAddress & (mCpu->DmaBufferAlignment - 1)) != 0) ||\r
+      ((*NumberOfBytes & (mCpu->DmaBufferAlignment - 1)) != 0)) {\r
 \r
     // Get the cacheability of the region\r
-    Status = gDS->GetMemorySpaceDescriptor (*DeviceAddress, &GcdDescriptor);\r
+    Status = gDS->GetMemorySpaceDescriptor ((UINTN)HostAddress, &GcdDescriptor);\r
     if (EFI_ERROR(Status)) {\r
-      return Status;\r
+      goto FreeMapInfo;\r
     }\r
 \r
     // If the mapped buffer is not an uncached buffer\r
@@ -112,7 +124,8 @@ DmaMap (
           "%a: Operation type 'MapOperationBusMasterCommonBuffer' is only supported\n"\r
           "on memory regions that were allocated using DmaAllocateBuffer ()\n",\r
           __FUNCTION__));\r
-        return EFI_UNSUPPORTED;\r
+        Status = EFI_UNSUPPORTED;\r
+        goto FreeMapInfo;\r
       }\r
 \r
       //\r
@@ -122,36 +135,55 @@ DmaMap (
       Map->DoubleBuffer  = TRUE;\r
       Status = DmaAllocateBuffer (EfiBootServicesData, EFI_SIZE_TO_PAGES (*NumberOfBytes), &Buffer);\r
       if (EFI_ERROR (Status)) {\r
-        return Status;\r
+        goto FreeMapInfo;\r
       }\r
 \r
       if (Operation == MapOperationBusMasterRead) {\r
         CopyMem (Buffer, HostAddress, *NumberOfBytes);\r
       }\r
 \r
-      *DeviceAddress = (PHYSICAL_ADDRESS)(UINTN)Buffer;\r
+      *DeviceAddress = HostToDeviceAddress (ConvertToPhysicalAddress ((UINTN)Buffer));\r
+      Map->BufferAddress = Buffer;\r
     } else {\r
       Map->DoubleBuffer  = FALSE;\r
     }\r
   } else {\r
     Map->DoubleBuffer  = FALSE;\r
 \r
-    // Flush the Data Cache (should not have any effect if the memory region is uncached)\r
-    gCpu->FlushDataCache (gCpu, *DeviceAddress, *NumberOfBytes, EfiCpuFlushTypeWriteBackInvalidate);\r
+    DEBUG_CODE_BEGIN ();\r
 \r
-    if ((Operation == MapOperationBusMasterRead) || (Operation == MapOperationBusMasterCommonBuffer)) {\r
-      // In case the buffer is used for instance to send command to a PCI controller, we must ensure the memory is uncached\r
-      Status = gDS->SetMemorySpaceAttributes (*DeviceAddress & ~(BASE_4KB - 1), ALIGN_VALUE (*NumberOfBytes, BASE_4KB), EFI_MEMORY_WC);\r
-      ASSERT_EFI_ERROR (Status);\r
-    }\r
+    //\r
+    // The operation type check above only executes if the buffer happens to be\r
+    // misaligned with respect to CWG, but even if it is aligned, we should not\r
+    // allow arbitrary buffers to be used for creating consistent mappings.\r
+    // So duplicate the check here when running in DEBUG mode, just to assert\r
+    // that we are not trying to create a consistent mapping for cached memory.\r
+    //\r
+    Status = gDS->GetMemorySpaceDescriptor ((UINTN)HostAddress, &GcdDescriptor);\r
+    ASSERT_EFI_ERROR(Status);\r
+\r
+    ASSERT (Operation != MapOperationBusMasterCommonBuffer ||\r
+            (GcdDescriptor.Attributes & (EFI_MEMORY_WB | EFI_MEMORY_WT)) == 0);\r
+\r
+    DEBUG_CODE_END ();\r
+\r
+    // Flush the Data Cache (should not have any effect if the memory region is uncached)\r
+    mCpu->FlushDataCache (mCpu, (UINTN)HostAddress, *NumberOfBytes,\r
+            EfiCpuFlushTypeWriteBackInvalidate);\r
   }\r
 \r
   Map->HostAddress   = (UINTN)HostAddress;\r
-  Map->DeviceAddress = *DeviceAddress;\r
   Map->NumberOfBytes = *NumberOfBytes;\r
   Map->Operation     = Operation;\r
 \r
+  *Mapping = Map;\r
+\r
   return EFI_SUCCESS;\r
+\r
+FreeMapInfo:\r
+  FreePool (Map);\r
+\r
+  return Status;\r
 }\r
 \r
 \r
@@ -190,17 +222,19 @@ DmaUnmap (
     if (Map->Operation == MapOperationBusMasterCommonBuffer) {\r
       Status = EFI_INVALID_PARAMETER;\r
     } else if (Map->Operation == MapOperationBusMasterWrite) {\r
-      CopyMem ((VOID *)(UINTN)Map->HostAddress, (VOID *)(UINTN)Map->DeviceAddress, Map->NumberOfBytes);\r
+      CopyMem ((VOID *)(UINTN)Map->HostAddress, Map->BufferAddress,\r
+        Map->NumberOfBytes);\r
     }\r
 \r
-    DmaFreeBuffer (EFI_SIZE_TO_PAGES (Map->NumberOfBytes), (VOID *)(UINTN)Map->DeviceAddress);\r
+    DmaFreeBuffer (EFI_SIZE_TO_PAGES (Map->NumberOfBytes), Map->BufferAddress);\r
 \r
   } else {\r
     if (Map->Operation == MapOperationBusMasterWrite) {\r
       //\r
       // Make sure we read buffer from uncached memory and not the cache\r
       //\r
-      gCpu->FlushDataCache (gCpu, Map->HostAddress, Map->NumberOfBytes, EfiCpuFlushTypeInvalidate);\r
+      mCpu->FlushDataCache (mCpu, Map->HostAddress, Map->NumberOfBytes,\r
+              EfiCpuFlushTypeInvalidate);\r
     }\r
   }\r
 \r
@@ -300,11 +334,9 @@ ArmDmaLibConstructor (
   EFI_STATUS              Status;\r
 \r
   // Get the Cpu protocol for later use\r
-  Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&gCpu);\r
+  Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&mCpu);\r
   ASSERT_EFI_ERROR(Status);\r
 \r
-  gCacheAlignment = ArmCacheWritebackGranule ();\r
-\r
   return Status;\r
 }\r
 \r