#include <Library/UefiBootServicesTableLib.h>\r
#include <Library/UncachedMemoryAllocationLib.h>\r
#include <Library/IoLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/ArmLib.h>\r
#include <Omap3530/Omap3530.h>\r
\r
#include <Protocol/Cpu.h>\r
EFI_PHYSICAL_ADDRESS DeviceAddress;\r
UINTN NumberOfBytes;\r
DMA_MAP_OPERATION Operation;\r
+ BOOLEAN DoubleBuffer;\r
} MAP_INFO_INSTANCE;\r
\r
\r
\r
-EFI_CPU_ARCH_PROTOCOL *gCpu;
+EFI_CPU_ARCH_PROTOCOL *gCpu;\r
+UINTN gCacheAlignment = 0;\r
\r
/** \r
Configure OMAP DMA Channel\r
@retval EFI_INVALID_PARAMETER Channel is not valid\r
@retval EFI_DEVICE_ERROR The system hardware could not map the requested information.\r
\r
-**/
-EFI_STATUS
-EFIAPI
-EnableDmaChannel (
- IN UINTN Channel,
- IN OMAP_DMA4 *DMA4
- )
-{
- UINT32 RegVal;
-
-
- if (Channel > DMA4_MAX_CHANNEL) {
- return EFI_INVALID_PARAMETER;
- }
-
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EnableDmaChannel (\r
+ IN UINTN Channel,\r
+ IN OMAP_DMA4 *DMA4\r
+ )\r
+{\r
+ UINT32 RegVal;\r
+\r
+\r
+ if (Channel > DMA4_MAX_CHANNEL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
/* 1) Configure the transfer parameters in the logical DMA registers */\r
/*-------------------------------------------------------------------*/\r
\r
/* 2) Start the DMA transfer by Setting the enable bit CCR[7]=1 */\r
/*--------------------------------------------------------------*/\r
//write enable bit\r
- MmioOr32 (DMA4_CCR(Channel), DMA4_CCR_ENABLE); //Launch transfer
-
- return EFI_SUCCESS;
-}
-
+ MmioOr32 (DMA4_CCR(Channel), DMA4_CCR_ENABLE); //Launch transfer\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
/** \r
Turn of DMA channel configured by EnableDma().\r
\r
@retval EFI_INVALID_PARAMETER Channel is not valid\r
@retval EFI_DEVICE_ERROR The system hardware could not map the requested information.\r
\r
-**/
-EFI_STATUS
-EFIAPI
-DisableDmaChannel (
- IN UINTN Channel,
- IN UINT32 SuccessMask,
- IN UINT32 ErrorMask
- )
-{
- EFI_STATUS Status = EFI_SUCCESS;
- UINT32 Reg;
-
-
- if (Channel > DMA4_MAX_CHANNEL) {
- return EFI_INVALID_PARAMETER;
- }
-
- do {
- Reg = MmioRead32 (DMA4_CSR(Channel));
- if ((Reg & ErrorMask) != 0) {
- Status = EFI_DEVICE_ERROR;
- DEBUG ((EFI_D_ERROR, "DMA Error (%d) %x\n", Channel, Reg));
- break;
- }
- } while ((Reg & SuccessMask) != SuccessMask);
-
-
- // Disable all status bits and clear them
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+DisableDmaChannel (\r
+ IN UINTN Channel,\r
+ IN UINT32 SuccessMask,\r
+ IN UINT32 ErrorMask\r
+ )\r
+{\r
+ EFI_STATUS Status = EFI_SUCCESS;\r
+ UINT32 Reg;\r
+\r
+\r
+ if (Channel > DMA4_MAX_CHANNEL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ do {\r
+ Reg = MmioRead32 (DMA4_CSR(Channel));\r
+ if ((Reg & ErrorMask) != 0) {\r
+ Status = EFI_DEVICE_ERROR;\r
+ DEBUG ((EFI_D_ERROR, "DMA Error (%d) %x\n", Channel, Reg));\r
+ break;\r
+ }\r
+ } while ((Reg & SuccessMask) != SuccessMask);\r
+\r
+\r
+ // Disable all status bits and clear them\r
MmioWrite32 (DMA4_CICR (Channel), 0);\r
- MmioWrite32 (DMA4_CSR (Channel), DMA4_CSR_RESET);
-
- MmioAnd32 (DMA4_CCR(0), ~(DMA4_CCR_ENABLE | DMA4_CCR_RD_ACTIVE | DMA4_CCR_WR_ACTIVE));
- return Status;
-}
-
-
-
+ MmioWrite32 (DMA4_CSR (Channel), DMA4_CSR_RESET);\r
+\r
+ MmioAnd32 (DMA4_CCR(0), ~(DMA4_CCR_ENABLE | DMA4_CCR_RD_ACTIVE | DMA4_CCR_WR_ACTIVE)); \r
+ return Status;\r
+}\r
+\r
+\r
+\r
/** \r
Provides the DMA controller-specific addresses needed to access system memory.\r
\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
-**/
-EFI_STATUS
-EFIAPI
-DmaMap (
- IN DMA_MAP_OPERATION Operation,
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+DmaMap (\r
+ IN DMA_MAP_OPERATION Operation,\r
IN VOID *HostAddress,\r
IN OUT UINTN *NumberOfBytes,\r
OUT PHYSICAL_ADDRESS *DeviceAddress,\r
OUT VOID **Mapping\r
- )
-{
- MAP_INFO_INSTANCE *Map;
-
- if ( HostAddress == NULL || NumberOfBytes == NULL ||
- DeviceAddress == NULL || Mapping == NULL ) {
- return EFI_INVALID_PARAMETER;
- }
-
-
- if (Operation >= MapOperationMaximum) {
- return EFI_INVALID_PARAMETER;
- }
-
- *DeviceAddress = ConvertToPhysicalAddress (HostAddress);
-
- // Remember range so we can flush on the other side
- Map = AllocatePool (sizeof (MAP_INFO_INSTANCE));
- if (Map == NULL) {
- return EFI_OUT_OF_RESOURCES;
- }
-
- *Mapping = Map;
-
- Map->HostAddress = (UINTN)HostAddress;
- Map->DeviceAddress = *DeviceAddress;
- Map->NumberOfBytes = *NumberOfBytes;
- Map->Operation = Operation;
-
- // EfiCpuFlushTypeWriteBack, EfiCpuFlushTypeInvalidate
- gCpu->FlushDataCache (gCpu, (EFI_PHYSICAL_ADDRESS)(UINTN)HostAddress, *NumberOfBytes, EfiCpuFlushTypeWriteBackInvalidate);
-
- return EFI_SUCCESS;
-}
-
-
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ MAP_INFO_INSTANCE *Map;\r
+ VOID *Buffer;\r
+\r
+ if ( HostAddress == NULL || NumberOfBytes == NULL || \r
+ DeviceAddress == NULL || Mapping == NULL ) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ \r
+\r
+ if (Operation >= MapOperationMaximum) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ *DeviceAddress = ConvertToPhysicalAddress (HostAddress);\r
+\r
+ // Remember range so we can flush on the other side\r
+ Map = AllocatePool (sizeof (MAP_INFO_INSTANCE));\r
+ if (Map == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+ \r
+ *Mapping = Map;\r
+\r
+ if (((UINTN)HostAddress & (gCacheAlignment - 1)) != 0) {\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
+ *DeviceAddress = (PHYSICAL_ADDRESS)(UINTN)Buffer;\r
+ \r
+ } else {\r
+ Map->DoubleBuffer = FALSE;\r
+ }\r
+\r
+ *NumberOfBytes &= *NumberOfBytes & ~(gCacheAlignment - 1); // Only do it on full cache lines\r
+ \r
+ Map->HostAddress = (UINTN)HostAddress;\r
+ Map->DeviceAddress = *DeviceAddress;\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
Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()\r
operation and releases any corresponding resources.\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
-**/
-EFI_STATUS
-EFIAPI
-DmaUnmap (
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+DmaUnmap (\r
IN VOID *Mapping\r
- )
-{
- MAP_INFO_INSTANCE *Map;
-
- if (Mapping == NULL) {
- ASSERT (FALSE);
- return EFI_INVALID_PARAMETER;
- }
-
- Map = (MAP_INFO_INSTANCE *)Mapping;
- if (Map->Operation == MapOperationBusMasterWrite) {
- //
- // Make sure we read buffer from uncached memory and not the cache
- //
- gCpu->FlushDataCache (gCpu, Map->HostAddress, Map->NumberOfBytes, EfiCpuFlushTypeInvalidate);
- }
-
- FreePool (Map);
-
- return EFI_SUCCESS;
-}
-
+ )\r
+{\r
+ MAP_INFO_INSTANCE *Map;\r
+ \r
+ if (Mapping == NULL) {\r
+ ASSERT (FALSE);\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ \r
+ Map = (MAP_INFO_INSTANCE *)Mapping;\r
+ \r
+ if (Map->DoubleBuffer) {\r
+ if (Map->Operation == MapOperationBusMasterRead) {\r
+ CopyMem ((VOID *)(UINTN)Map->HostAddress, (VOID *)(UINTN)Map->DeviceAddress, Map->NumberOfBytes);\r
+ }\r
+ \r
+ DmaFreeBuffer (EFI_SIZE_TO_PAGES (Map->NumberOfBytes), (VOID *)(UINTN)Map->DeviceAddress);\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
+ }\r
+ }\r
+ \r
+ FreePool (Map);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
/** \r
Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.\r
mapping. \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
-**/EFI_STATUS
-EFIAPI
-DmaAllocateBuffer (
- IN EFI_MEMORY_TYPE MemoryType,
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+DmaAllocateBuffer (\r
+ IN EFI_MEMORY_TYPE MemoryType,\r
IN UINTN Pages,\r
OUT VOID **HostAddress\r
)\r
-{
- if (HostAddress == NULL) {
- return EFI_INVALID_PARAMETER;
- }
-
- //
- // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData
- //
- // We used uncached memory to keep coherency
- //
- if (MemoryType == EfiBootServicesData) {
- *HostAddress = UncachedAllocatePages (Pages);
- } else if (MemoryType != EfiRuntimeServicesData) {
- *HostAddress = UncachedAllocateRuntimePages (Pages);
- } else {
- return EFI_INVALID_PARAMETER;
- }
-
- return EFI_SUCCESS;
-}
-
-
+{\r
+ if (HostAddress == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData\r
+ //\r
+ // We used uncached memory to keep coherency\r
+ //\r
+ if (MemoryType == EfiBootServicesData) {\r
+ *HostAddress = UncachedAllocatePages (Pages);\r
+ } else if (MemoryType != EfiRuntimeServicesData) {\r
+ *HostAddress = UncachedAllocateRuntimePages (Pages);\r
+ } else {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
/** \r
Frees memory that was allocated with DmaAllocateBuffer().\r
\r
was not allocated with DmaAllocateBuffer().\r
\r
**/\r
-EFI_STATUS
-EFIAPI
-DmaFreeBuffer (
+EFI_STATUS\r
+EFIAPI\r
+DmaFreeBuffer (\r
IN UINTN Pages,\r
IN VOID *HostAddress\r
)\r
-{
- if (HostAddress == NULL) {
- return EFI_INVALID_PARAMETER;
- }
-
- UncachedFreePages (HostAddress, Pages);
- return EFI_SUCCESS;
-}
-
-
-EFI_STATUS
-EFIAPI
-OmapDmaLibConstructor (
- IN EFI_HANDLE ImageHandle,
- IN EFI_SYSTEM_TABLE *SystemTable
- )
-{
- EFI_STATUS Status;
-
- // Get the Cpu protocol for later use
- Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&gCpu);
- ASSERT_EFI_ERROR(Status);
-
- return EFI_SUCCESS;
-}
-
+{\r
+ if (HostAddress == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ } \r
+ \r
+ UncachedFreePages (HostAddress, Pages);\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+OmapDmaLibConstructor (\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_SYSTEM_TABLE *SystemTable\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ // Get the Cpu protocol for later use\r
+ Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&gCpu);\r
+ ASSERT_EFI_ERROR(Status);\r
+\r
+ gCacheAlignment = ArmDataCacheLineLength ();\r
+ \r
+ return Status;\r
+}\r
+\r