Generic ARM implementation of DmaLib.h\r
\r
Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\r
- \r
+\r
This program and the accompanying materials\r
are licensed and made available under the terms and conditions of the BSD License\r
which accompanies this distribution. The full text of the license may be found at\r
\r
**/\r
\r
-#include <Base.h>\r
+#include <PiDxe.h>\r
#include <Library/DebugLib.h>\r
#include <Library/DmaLib.h>\r
+#include <Library/DxeServicesTableLib.h>\r
#include <Library/MemoryAllocationLib.h>\r
#include <Library/UefiBootServicesTableLib.h>\r
#include <Library/UncachedMemoryAllocationLib.h>\r
EFI_CPU_ARCH_PROTOCOL *gCpu;\r
UINTN gCacheAlignment = 0;\r
\r
-\r
-\r
-\r
-/** \r
+/**\r
Provides the DMA controller-specific addresses needed to access system memory.\r
- \r
+\r
Operation is relative to the DMA bus master.\r
- \r
+\r
@param Operation Indicates if the bus master is going to read or write to system memory.\r
@param HostAddress The system memory address to map to the DMA controller.\r
@param NumberOfBytes On input the number of bytes to map. On output the number of bytes\r
- that were mapped. \r
+ that were mapped.\r
@param DeviceAddress The resulting map address for the bus master controller to use to\r
- access the hosts HostAddress. \r
+ access the hosts HostAddress.\r
@param Mapping A resulting value to pass to Unmap().\r
- \r
+\r
@retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.\r
- @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer. \r
+ @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a 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 a lack of resources.\r
@retval EFI_DEVICE_ERROR The system hardware could not map the requested address.\r
- \r
+\r
**/\r
EFI_STATUS\r
EFIAPI\r
OUT VOID **Mapping\r
)\r
{\r
- EFI_STATUS Status;\r
- MAP_INFO_INSTANCE *Map;\r
- VOID *Buffer;\r
+ EFI_STATUS Status;\r
+ MAP_INFO_INSTANCE *Map;\r
+ VOID *Buffer;\r
+ EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
\r
- if ( HostAddress == NULL || NumberOfBytes == NULL || \r
- DeviceAddress == NULL || Mapping == NULL ) {\r
+ if (HostAddress == NULL || NumberOfBytes == NULL || DeviceAddress == NULL || Mapping == NULL ) {\r
return EFI_INVALID_PARAMETER;\r
}\r
- \r
\r
if (Operation >= MapOperationMaximum) {\r
return EFI_INVALID_PARAMETER;\r
if (Map == NULL) {\r
return EFI_OUT_OF_RESOURCES;\r
}\r
- \r
+\r
*Mapping = Map;\r
\r
if ((((UINTN)HostAddress & (gCacheAlignment - 1)) != 0) ||\r
((*NumberOfBytes % gCacheAlignment) != 0)) {\r
- //\r
- // If the buffer does not fill entire cache lines we must double buffer into \r
- // uncached memory. Device (PCI) address becomes uncached page.\r
- //\r
- Map->DoubleBuffer = TRUE;\r
- Status = DmaAllocateBuffer (EfiBootServicesData, EFI_SIZE_TO_PAGES (*NumberOfBytes), &Buffer);\r
- if (EFI_ERROR (Status)) {\r
+\r
+ // Get the cacheability of the region\r
+ Status = gDS->GetMemorySpaceDescriptor (*DeviceAddress, &GcdDescriptor);\r
+ if (EFI_ERROR(Status)) {\r
return Status;\r
}\r
- \r
- *DeviceAddress = (PHYSICAL_ADDRESS)(UINTN)Buffer;\r
- \r
+\r
+ // If the mapped buffer is not an uncached buffer\r
+ if ( (GcdDescriptor.Attributes != EFI_MEMORY_WC) &&\r
+ (GcdDescriptor.Attributes != EFI_MEMORY_UC) )\r
+ {\r
+ //\r
+ // If the buffer does not fill entire cache lines we must double buffer into\r
+ // uncached memory. Device (PCI) address becomes uncached page.\r
+ //\r
+ Map->DoubleBuffer = TRUE;\r
+ Status = DmaAllocateBuffer (EfiBootServicesData, EFI_SIZE_TO_PAGES (*NumberOfBytes), &Buffer);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ if ((Operation == MapOperationBusMasterRead) || (Operation == MapOperationBusMasterCommonBuffer)) {\r
+ CopyMem (Buffer, HostAddress, *NumberOfBytes);\r
+ }\r
+\r
+ *DeviceAddress = (PHYSICAL_ADDRESS)(UINTN)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
+\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
\r
Map->HostAddress = (UINTN)HostAddress;\r
Map->NumberOfBytes = *NumberOfBytes;\r
Map->Operation = Operation;\r
\r
- if (Map->DoubleBuffer) {\r
- if (Map->Operation == MapOperationBusMasterWrite) {\r
- CopyMem ((VOID *)(UINTN)Map->DeviceAddress, (VOID *)(UINTN)Map->HostAddress, Map->NumberOfBytes);\r
- }\r
- } else {\r
- // EfiCpuFlushTypeWriteBack, EfiCpuFlushTypeInvalidate\r
- if (Map->Operation == MapOperationBusMasterWrite || Map->Operation == MapOperationBusMasterRead) {\r
- gCpu->FlushDataCache (gCpu, (EFI_PHYSICAL_ADDRESS)(UINTN)HostAddress, Map->NumberOfBytes, EfiCpuFlushTypeWriteBackInvalidate);\r
- }\r
- }\r
- \r
return EFI_SUCCESS;\r
}\r
\r
\r
-/** \r
+/**\r
Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()\r
operation and releases any corresponding resources.\r
- \r
+\r
@param Mapping The mapping value returned from DmaMap*().\r
- \r
+\r
@retval EFI_SUCCESS The range was unmapped.\r
@retval EFI_DEVICE_ERROR The data was not committed to the target system memory.\r
- \r
+\r
**/\r
EFI_STATUS\r
EFIAPI\r
)\r
{\r
MAP_INFO_INSTANCE *Map;\r
- \r
+\r
if (Mapping == NULL) {\r
ASSERT (FALSE);\r
return EFI_INVALID_PARAMETER;\r
}\r
- \r
+\r
Map = (MAP_INFO_INSTANCE *)Mapping;\r
- \r
+\r
if (Map->DoubleBuffer) {\r
- if (Map->Operation == MapOperationBusMasterRead) {\r
+ if ((Map->Operation == MapOperationBusMasterWrite) || (Map->Operation == MapOperationBusMasterCommonBuffer)) {\r
CopyMem ((VOID *)(UINTN)Map->HostAddress, (VOID *)(UINTN)Map->DeviceAddress, Map->NumberOfBytes);\r
}\r
- \r
+\r
DmaFreeBuffer (EFI_SIZE_TO_PAGES (Map->NumberOfBytes), (VOID *)(UINTN)Map->DeviceAddress);\r
- \r
+\r
} else {\r
if (Map->Operation == MapOperationBusMasterWrite) {\r
//\r
gCpu->FlushDataCache (gCpu, Map->HostAddress, Map->NumberOfBytes, EfiCpuFlushTypeInvalidate);\r
}\r
}\r
- \r
+\r
FreePool (Map);\r
\r
return EFI_SUCCESS;\r
}\r
\r
-/** \r
+/**\r
Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.\r
- mapping. \r
- \r
+ mapping.\r
+\r
@param MemoryType The type of memory to allocate, EfiBootServicesData or\r
- EfiRuntimeServicesData. \r
- @param Pages The number of pages to allocate. \r
+ EfiRuntimeServicesData.\r
+ @param Pages The number of pages to allocate.\r
@param HostAddress A pointer to store the base system memory address of the\r
- allocated range. \r
+ allocated range.\r
\r
@retval EFI_SUCCESS The requested memory pages were allocated.\r
@retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are\r
- MEMORY_WRITE_COMBINE and MEMORY_CACHED. \r
+ MEMORY_WRITE_COMBINE and MEMORY_CACHED.\r
@retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
- @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated. \r
- \r
+ @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
+\r
**/\r
EFI_STATUS\r
EFIAPI\r
}\r
\r
\r
-/** \r
+/**\r
Frees memory that was allocated with DmaAllocateBuffer().\r
- \r
- @param Pages The number of pages to free. \r
- @param HostAddress The base system memory address of the allocated range. \r
- \r
+\r
+ @param Pages The number of pages to free.\r
+ @param HostAddress The base system memory address of the allocated range.\r
+\r
@retval EFI_SUCCESS The requested memory pages were freed.\r
@retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages\r
was not allocated with DmaAllocateBuffer().\r
- \r
+\r
**/\r
EFI_STATUS\r
EFIAPI\r
{\r
if (HostAddress == NULL) {\r
return EFI_INVALID_PARAMETER;\r
- } \r
- \r
+ }\r
+\r
UncachedFreePages (HostAddress, Pages);\r
return EFI_SUCCESS;\r
}\r
Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&gCpu);\r
ASSERT_EFI_ERROR(Status);\r
\r
- gCacheAlignment = ArmDataCacheLineLength ();\r
- \r
+ gCacheAlignment = ArmCacheWritebackGranule ();\r
+\r
return Status;\r
}\r
\r