]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/ArmDmaLib/ArmDmaLib.c
Add DMA Lib for generic ARM cache coherency model.
[mirror_edk2.git] / ArmPkg / Library / ArmDmaLib / ArmDmaLib.c
1 /** @file
2 Generic ARM implementation of DmaLib.h
3
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
5
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
10
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.
13
14 **/
15
16 #include <Base.h>
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>
25
26 #include <Protocol/Cpu.h>
27
28 typedef struct {
29 EFI_PHYSICAL_ADDRESS HostAddress;
30 EFI_PHYSICAL_ADDRESS DeviceAddress;
31 UINTN NumberOfBytes;
32 DMA_MAP_OPERATION Operation;
33 BOOLEAN DoubleBuffer;
34 } MAP_INFO_INSTANCE;
35
36
37
38 EFI_CPU_ARCH_PROTOCOL *gCpu;
39 UINTN gCacheAlignment = 0;
40
41
42
43
44 /**
45 Provides the DMA controller-specific addresses needed to access system memory.
46
47 Operation is relative to the DMA bus master.
48
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
52 that were mapped.
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().
56
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.
62
63 **/
64 EFI_STATUS
65 EFIAPI
66 DmaMap (
67 IN DMA_MAP_OPERATION Operation,
68 IN VOID *HostAddress,
69 IN OUT UINTN *NumberOfBytes,
70 OUT PHYSICAL_ADDRESS *DeviceAddress,
71 OUT VOID **Mapping
72 )
73 {
74 EFI_STATUS Status;
75 MAP_INFO_INSTANCE *Map;
76 VOID *Buffer;
77
78 if ( HostAddress == NULL || NumberOfBytes == NULL ||
79 DeviceAddress == NULL || Mapping == NULL ) {
80 return EFI_INVALID_PARAMETER;
81 }
82
83
84 if (Operation >= MapOperationMaximum) {
85 return EFI_INVALID_PARAMETER;
86 }
87
88 *DeviceAddress = ConvertToPhysicalAddress (HostAddress);
89
90 // Remember range so we can flush on the other side
91 Map = AllocatePool (sizeof (MAP_INFO_INSTANCE));
92 if (Map == NULL) {
93 return EFI_OUT_OF_RESOURCES;
94 }
95
96 *Mapping = Map;
97
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)) {
102 return Status;
103 }
104
105 *DeviceAddress = (PHYSICAL_ADDRESS)(UINTN)Buffer;
106
107 } else {
108 Map->DoubleBuffer = FALSE;
109 }
110
111 *NumberOfBytes &= *NumberOfBytes & ~(gCacheAlignment - 1); // Only do it on full cache lines
112
113 Map->HostAddress = (UINTN)HostAddress;
114 Map->DeviceAddress = *DeviceAddress;
115 Map->NumberOfBytes = *NumberOfBytes;
116 Map->Operation = Operation;
117
118 if (Map->DoubleBuffer) {
119 if (Map->Operation == MapOperationBusMasterWrite) {
120 CopyMem ((VOID *)(UINTN)Map->DeviceAddress, (VOID *)(UINTN)Map->HostAddress, Map->NumberOfBytes);
121 }
122 } else {
123 // EfiCpuFlushTypeWriteBack, EfiCpuFlushTypeInvalidate
124 if (Map->Operation == MapOperationBusMasterWrite || Map->Operation == MapOperationBusMasterRead) {
125 gCpu->FlushDataCache (gCpu, (EFI_PHYSICAL_ADDRESS)(UINTN)HostAddress, Map->NumberOfBytes, EfiCpuFlushTypeWriteBackInvalidate);
126 }
127 }
128
129 return EFI_SUCCESS;
130 }
131
132
133 /**
134 Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()
135 operation and releases any corresponding resources.
136
137 @param Mapping The mapping value returned from DmaMap*().
138
139 @retval EFI_SUCCESS The range was unmapped.
140 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
141
142 **/
143 EFI_STATUS
144 EFIAPI
145 DmaUnmap (
146 IN VOID *Mapping
147 )
148 {
149 MAP_INFO_INSTANCE *Map;
150
151 if (Mapping == NULL) {
152 ASSERT (FALSE);
153 return EFI_INVALID_PARAMETER;
154 }
155
156 Map = (MAP_INFO_INSTANCE *)Mapping;
157
158 if (Map->DoubleBuffer) {
159 if (Map->Operation == MapOperationBusMasterRead) {
160 CopyMem ((VOID *)(UINTN)Map->HostAddress, (VOID *)(UINTN)Map->DeviceAddress, Map->NumberOfBytes);
161 }
162
163 DmaFreeBuffer (EFI_SIZE_TO_PAGES (Map->NumberOfBytes), (VOID *)(UINTN)Map->DeviceAddress);
164
165 } else {
166 if (Map->Operation == MapOperationBusMasterWrite) {
167 //
168 // Make sure we read buffer from uncached memory and not the cache
169 //
170 gCpu->FlushDataCache (gCpu, Map->HostAddress, Map->NumberOfBytes, EfiCpuFlushTypeInvalidate);
171 }
172 }
173
174 FreePool (Map);
175
176 return EFI_SUCCESS;
177 }
178
179 /**
180 Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.
181 mapping.
182
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
187 allocated range.
188
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.
194
195 **/
196 EFI_STATUS
197 EFIAPI
198 DmaAllocateBuffer (
199 IN EFI_MEMORY_TYPE MemoryType,
200 IN UINTN Pages,
201 OUT VOID **HostAddress
202 )
203 {
204 if (HostAddress == NULL) {
205 return EFI_INVALID_PARAMETER;
206 }
207
208 //
209 // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData
210 //
211 // We used uncached memory to keep coherency
212 //
213 if (MemoryType == EfiBootServicesData) {
214 *HostAddress = UncachedAllocatePages (Pages);
215 } else if (MemoryType != EfiRuntimeServicesData) {
216 *HostAddress = UncachedAllocateRuntimePages (Pages);
217 } else {
218 return EFI_INVALID_PARAMETER;
219 }
220
221 return EFI_SUCCESS;
222 }
223
224
225 /**
226 Frees memory that was allocated with DmaAllocateBuffer().
227
228 @param Pages The number of pages to free.
229 @param HostAddress The base system memory address of the allocated range.
230
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().
234
235 **/
236 EFI_STATUS
237 EFIAPI
238 DmaFreeBuffer (
239 IN UINTN Pages,
240 IN VOID *HostAddress
241 )
242 {
243 if (HostAddress == NULL) {
244 return EFI_INVALID_PARAMETER;
245 }
246
247 UncachedFreePages (HostAddress, Pages);
248 return EFI_SUCCESS;
249 }
250
251
252 EFI_STATUS
253 EFIAPI
254 ArmDmaLibConstructor (
255 IN EFI_HANDLE ImageHandle,
256 IN EFI_SYSTEM_TABLE *SystemTable
257 )
258 {
259 EFI_STATUS Status;
260
261 // Get the Cpu protocol for later use
262 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&gCpu);
263 ASSERT_EFI_ERROR(Status);
264
265 gCacheAlignment = ArmDataCacheLineLength ();
266
267 return Status;
268 }
269