2 Generic ARM implementation of DmaLib.h
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Library/DebugLib.h>
18 #include <Library/DmaLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
21 #include <Library/UncachedMemoryAllocationLib.h>
22 #include <Library/IoLib.h>
23 #include <Library/BaseMemoryLib.h>
24 #include <Library/ArmLib.h>
26 #include <Protocol/Cpu.h>
29 EFI_PHYSICAL_ADDRESS HostAddress
;
30 EFI_PHYSICAL_ADDRESS DeviceAddress
;
32 DMA_MAP_OPERATION Operation
;
38 EFI_CPU_ARCH_PROTOCOL
*gCpu
;
39 UINTN gCacheAlignment
= 0;
45 Provides the DMA controller-specific addresses needed to access system memory.
47 Operation is relative to the DMA bus master.
49 @param Operation Indicates if the bus master is going to read or write to system memory.
50 @param HostAddress The system memory address to map to the DMA controller.
51 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
53 @param DeviceAddress The resulting map address for the bus master controller to use to
54 access the hosts HostAddress.
55 @param Mapping A resulting value to pass to Unmap().
57 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
58 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
59 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
60 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
61 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
67 IN DMA_MAP_OPERATION Operation
,
69 IN OUT UINTN
*NumberOfBytes
,
70 OUT PHYSICAL_ADDRESS
*DeviceAddress
,
75 MAP_INFO_INSTANCE
*Map
;
78 if ( HostAddress
== NULL
|| NumberOfBytes
== NULL
||
79 DeviceAddress
== NULL
|| Mapping
== NULL
) {
80 return EFI_INVALID_PARAMETER
;
84 if (Operation
>= MapOperationMaximum
) {
85 return EFI_INVALID_PARAMETER
;
88 *DeviceAddress
= ConvertToPhysicalAddress (HostAddress
);
90 // Remember range so we can flush on the other side
91 Map
= AllocatePool (sizeof (MAP_INFO_INSTANCE
));
93 return EFI_OUT_OF_RESOURCES
;
98 if (((UINTN
)HostAddress
& (gCacheAlignment
- 1)) != 0) {
99 Map
->DoubleBuffer
= TRUE
;
100 Status
= DmaAllocateBuffer (EfiBootServicesData
, EFI_SIZE_TO_PAGES (*NumberOfBytes
), &Buffer
);
101 if (EFI_ERROR (Status
)) {
105 *DeviceAddress
= (PHYSICAL_ADDRESS
)(UINTN
)Buffer
;
108 Map
->DoubleBuffer
= FALSE
;
111 *NumberOfBytes
&= *NumberOfBytes
& ~(gCacheAlignment
- 1); // Only do it on full cache lines
113 Map
->HostAddress
= (UINTN
)HostAddress
;
114 Map
->DeviceAddress
= *DeviceAddress
;
115 Map
->NumberOfBytes
= *NumberOfBytes
;
116 Map
->Operation
= Operation
;
118 if (Map
->DoubleBuffer
) {
119 if (Map
->Operation
== MapOperationBusMasterWrite
) {
120 CopyMem ((VOID
*)(UINTN
)Map
->DeviceAddress
, (VOID
*)(UINTN
)Map
->HostAddress
, Map
->NumberOfBytes
);
123 // EfiCpuFlushTypeWriteBack, EfiCpuFlushTypeInvalidate
124 if (Map
->Operation
== MapOperationBusMasterWrite
|| Map
->Operation
== MapOperationBusMasterRead
) {
125 gCpu
->FlushDataCache (gCpu
, (EFI_PHYSICAL_ADDRESS
)(UINTN
)HostAddress
, Map
->NumberOfBytes
, EfiCpuFlushTypeWriteBackInvalidate
);
134 Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()
135 operation and releases any corresponding resources.
137 @param Mapping The mapping value returned from DmaMap*().
139 @retval EFI_SUCCESS The range was unmapped.
140 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
149 MAP_INFO_INSTANCE
*Map
;
151 if (Mapping
== NULL
) {
153 return EFI_INVALID_PARAMETER
;
156 Map
= (MAP_INFO_INSTANCE
*)Mapping
;
158 if (Map
->DoubleBuffer
) {
159 if (Map
->Operation
== MapOperationBusMasterRead
) {
160 CopyMem ((VOID
*)(UINTN
)Map
->HostAddress
, (VOID
*)(UINTN
)Map
->DeviceAddress
, Map
->NumberOfBytes
);
163 DmaFreeBuffer (EFI_SIZE_TO_PAGES (Map
->NumberOfBytes
), (VOID
*)(UINTN
)Map
->DeviceAddress
);
166 if (Map
->Operation
== MapOperationBusMasterWrite
) {
168 // Make sure we read buffer from uncached memory and not the cache
170 gCpu
->FlushDataCache (gCpu
, Map
->HostAddress
, Map
->NumberOfBytes
, EfiCpuFlushTypeInvalidate
);
180 Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.
183 @param MemoryType The type of memory to allocate, EfiBootServicesData or
184 EfiRuntimeServicesData.
185 @param Pages The number of pages to allocate.
186 @param HostAddress A pointer to store the base system memory address of the
189 @retval EFI_SUCCESS The requested memory pages were allocated.
190 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
191 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
192 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
193 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
199 IN EFI_MEMORY_TYPE MemoryType
,
201 OUT VOID
**HostAddress
204 if (HostAddress
== NULL
) {
205 return EFI_INVALID_PARAMETER
;
209 // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData
211 // We used uncached memory to keep coherency
213 if (MemoryType
== EfiBootServicesData
) {
214 *HostAddress
= UncachedAllocatePages (Pages
);
215 } else if (MemoryType
!= EfiRuntimeServicesData
) {
216 *HostAddress
= UncachedAllocateRuntimePages (Pages
);
218 return EFI_INVALID_PARAMETER
;
226 Frees memory that was allocated with DmaAllocateBuffer().
228 @param Pages The number of pages to free.
229 @param HostAddress The base system memory address of the allocated range.
231 @retval EFI_SUCCESS The requested memory pages were freed.
232 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
233 was not allocated with DmaAllocateBuffer().
243 if (HostAddress
== NULL
) {
244 return EFI_INVALID_PARAMETER
;
247 UncachedFreePages (HostAddress
, Pages
);
254 ArmDmaLibConstructor (
255 IN EFI_HANDLE ImageHandle
,
256 IN EFI_SYSTEM_TABLE
*SystemTable
261 // Get the Cpu protocol for later use
262 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**)&gCpu
);
263 ASSERT_EFI_ERROR(Status
);
265 gCacheAlignment
= ArmDataCacheLineLength ();