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 ((*NumberOfBytes
% gCacheAlignment
) != 0)) {
101 // If the buffer does not fill entire cache lines we must double buffer into
102 // uncached memory. Device (PCI) address becomes uncached page.
104 Map
->DoubleBuffer
= TRUE
;
105 Status
= DmaAllocateBuffer (EfiBootServicesData
, EFI_SIZE_TO_PAGES (*NumberOfBytes
), &Buffer
);
106 if (EFI_ERROR (Status
)) {
110 *DeviceAddress
= (PHYSICAL_ADDRESS
)(UINTN
)Buffer
;
113 Map
->DoubleBuffer
= FALSE
;
116 Map
->HostAddress
= (UINTN
)HostAddress
;
117 Map
->DeviceAddress
= *DeviceAddress
;
118 Map
->NumberOfBytes
= *NumberOfBytes
;
119 Map
->Operation
= Operation
;
121 if (Map
->DoubleBuffer
) {
122 if (Map
->Operation
== MapOperationBusMasterWrite
) {
123 CopyMem ((VOID
*)(UINTN
)Map
->DeviceAddress
, (VOID
*)(UINTN
)Map
->HostAddress
, Map
->NumberOfBytes
);
126 // EfiCpuFlushTypeWriteBack, EfiCpuFlushTypeInvalidate
127 if (Map
->Operation
== MapOperationBusMasterWrite
|| Map
->Operation
== MapOperationBusMasterRead
) {
128 gCpu
->FlushDataCache (gCpu
, (EFI_PHYSICAL_ADDRESS
)(UINTN
)HostAddress
, Map
->NumberOfBytes
, EfiCpuFlushTypeWriteBackInvalidate
);
137 Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()
138 operation and releases any corresponding resources.
140 @param Mapping The mapping value returned from DmaMap*().
142 @retval EFI_SUCCESS The range was unmapped.
143 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
152 MAP_INFO_INSTANCE
*Map
;
154 if (Mapping
== NULL
) {
156 return EFI_INVALID_PARAMETER
;
159 Map
= (MAP_INFO_INSTANCE
*)Mapping
;
161 if (Map
->DoubleBuffer
) {
162 if (Map
->Operation
== MapOperationBusMasterRead
) {
163 CopyMem ((VOID
*)(UINTN
)Map
->HostAddress
, (VOID
*)(UINTN
)Map
->DeviceAddress
, Map
->NumberOfBytes
);
166 DmaFreeBuffer (EFI_SIZE_TO_PAGES (Map
->NumberOfBytes
), (VOID
*)(UINTN
)Map
->DeviceAddress
);
169 if (Map
->Operation
== MapOperationBusMasterWrite
) {
171 // Make sure we read buffer from uncached memory and not the cache
173 gCpu
->FlushDataCache (gCpu
, Map
->HostAddress
, Map
->NumberOfBytes
, EfiCpuFlushTypeInvalidate
);
183 Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.
186 @param MemoryType The type of memory to allocate, EfiBootServicesData or
187 EfiRuntimeServicesData.
188 @param Pages The number of pages to allocate.
189 @param HostAddress A pointer to store the base system memory address of the
192 @retval EFI_SUCCESS The requested memory pages were allocated.
193 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
194 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
195 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
196 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
202 IN EFI_MEMORY_TYPE MemoryType
,
204 OUT VOID
**HostAddress
207 if (HostAddress
== NULL
) {
208 return EFI_INVALID_PARAMETER
;
212 // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData
214 // We used uncached memory to keep coherency
216 if (MemoryType
== EfiBootServicesData
) {
217 *HostAddress
= UncachedAllocatePages (Pages
);
218 } else if (MemoryType
!= EfiRuntimeServicesData
) {
219 *HostAddress
= UncachedAllocateRuntimePages (Pages
);
221 return EFI_INVALID_PARAMETER
;
229 Frees memory that was allocated with DmaAllocateBuffer().
231 @param Pages The number of pages to free.
232 @param HostAddress The base system memory address of the allocated range.
234 @retval EFI_SUCCESS The requested memory pages were freed.
235 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
236 was not allocated with DmaAllocateBuffer().
246 if (HostAddress
== NULL
) {
247 return EFI_INVALID_PARAMETER
;
250 UncachedFreePages (HostAddress
, Pages
);
257 ArmDmaLibConstructor (
258 IN EFI_HANDLE ImageHandle
,
259 IN EFI_SYSTEM_TABLE
*SystemTable
264 // Get the Cpu protocol for later use
265 Status
= gBS
->LocateProtocol (&gEfiCpuArchProtocolGuid
, NULL
, (VOID
**)&gCpu
);
266 ASSERT_EFI_ERROR(Status
);
268 gCacheAlignment
= ArmDataCacheLineLength ();