]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/ArmDmaLib/ArmDmaLib.c
4476e8b461e39b1e7d051e3b89489c9c0fb25c9b
[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 <PiDxe.h>
17 #include <Library/DebugLib.h>
18 #include <Library/DmaLib.h>
19 #include <Library/DxeServicesTableLib.h>
20 #include <Library/MemoryAllocationLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
22 #include <Library/UncachedMemoryAllocationLib.h>
23 #include <Library/IoLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/ArmLib.h>
26
27 #include <Protocol/Cpu.h>
28
29 typedef struct {
30 EFI_PHYSICAL_ADDRESS HostAddress;
31 EFI_PHYSICAL_ADDRESS DeviceAddress;
32 UINTN NumberOfBytes;
33 DMA_MAP_OPERATION Operation;
34 BOOLEAN DoubleBuffer;
35 } MAP_INFO_INSTANCE;
36
37
38
39 EFI_CPU_ARCH_PROTOCOL *gCpu;
40 UINTN gCacheAlignment = 0;
41
42 /**
43 Provides the DMA controller-specific addresses needed to access system memory.
44
45 Operation is relative to the DMA bus master.
46
47 @param Operation Indicates if the bus master is going to read or write to system memory.
48 @param HostAddress The system memory address to map to the DMA controller.
49 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
50 that were mapped.
51 @param DeviceAddress The resulting map address for the bus master controller to use to
52 access the hosts HostAddress.
53 @param Mapping A resulting value to pass to Unmap().
54
55 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
56 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
57 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
58 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
59 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
60
61 **/
62 EFI_STATUS
63 EFIAPI
64 DmaMap (
65 IN DMA_MAP_OPERATION Operation,
66 IN VOID *HostAddress,
67 IN OUT UINTN *NumberOfBytes,
68 OUT PHYSICAL_ADDRESS *DeviceAddress,
69 OUT VOID **Mapping
70 )
71 {
72 EFI_STATUS Status;
73 MAP_INFO_INSTANCE *Map;
74 VOID *Buffer;
75 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;
76
77 if (HostAddress == NULL || NumberOfBytes == NULL || DeviceAddress == NULL || Mapping == NULL ) {
78 return EFI_INVALID_PARAMETER;
79 }
80
81 if (Operation >= MapOperationMaximum) {
82 return EFI_INVALID_PARAMETER;
83 }
84
85 *DeviceAddress = ConvertToPhysicalAddress (HostAddress);
86
87 // Remember range so we can flush on the other side
88 Map = AllocatePool (sizeof (MAP_INFO_INSTANCE));
89 if (Map == NULL) {
90 return EFI_OUT_OF_RESOURCES;
91 }
92
93 if ((((UINTN)HostAddress & (gCacheAlignment - 1)) != 0) ||
94 ((*NumberOfBytes & (gCacheAlignment - 1)) != 0)) {
95
96 // Get the cacheability of the region
97 Status = gDS->GetMemorySpaceDescriptor (*DeviceAddress, &GcdDescriptor);
98 if (EFI_ERROR(Status)) {
99 goto FreeMapInfo;
100 }
101
102 // If the mapped buffer is not an uncached buffer
103 if ((GcdDescriptor.Attributes & (EFI_MEMORY_WB | EFI_MEMORY_WT)) != 0) {
104 //
105 // Operations of type MapOperationBusMasterCommonBuffer are only allowed
106 // on uncached buffers.
107 //
108 if (Operation == MapOperationBusMasterCommonBuffer) {
109 DEBUG ((EFI_D_ERROR,
110 "%a: Operation type 'MapOperationBusMasterCommonBuffer' is only supported\n"
111 "on memory regions that were allocated using DmaAllocateBuffer ()\n",
112 __FUNCTION__));
113 Status = EFI_UNSUPPORTED;
114 goto FreeMapInfo;
115 }
116
117 //
118 // If the buffer does not fill entire cache lines we must double buffer into
119 // uncached memory. Device (PCI) address becomes uncached page.
120 //
121 Map->DoubleBuffer = TRUE;
122 Status = DmaAllocateBuffer (EfiBootServicesData, EFI_SIZE_TO_PAGES (*NumberOfBytes), &Buffer);
123 if (EFI_ERROR (Status)) {
124 goto FreeMapInfo;
125 }
126
127 if (Operation == MapOperationBusMasterRead) {
128 CopyMem (Buffer, HostAddress, *NumberOfBytes);
129 }
130
131 *DeviceAddress = (PHYSICAL_ADDRESS)(UINTN)Buffer;
132 } else {
133 Map->DoubleBuffer = FALSE;
134 }
135 } else {
136 Map->DoubleBuffer = FALSE;
137
138 DEBUG_CODE_BEGIN ();
139
140 //
141 // The operation type check above only executes if the buffer happens to be
142 // misaligned with respect to CWG, but even if it is aligned, we should not
143 // allow arbitrary buffers to be used for creating consistent mappings.
144 // So duplicate the check here when running in DEBUG mode, just to assert
145 // that we are not trying to create a consistent mapping for cached memory.
146 //
147 Status = gDS->GetMemorySpaceDescriptor (*DeviceAddress, &GcdDescriptor);
148 ASSERT_EFI_ERROR(Status);
149
150 ASSERT (Operation != MapOperationBusMasterCommonBuffer ||
151 (GcdDescriptor.Attributes & (EFI_MEMORY_WB | EFI_MEMORY_WT)) == 0);
152
153 DEBUG_CODE_END ();
154
155 // Flush the Data Cache (should not have any effect if the memory region is uncached)
156 gCpu->FlushDataCache (gCpu, *DeviceAddress, *NumberOfBytes, EfiCpuFlushTypeWriteBackInvalidate);
157 }
158
159 Map->HostAddress = (UINTN)HostAddress;
160 Map->DeviceAddress = *DeviceAddress;
161 Map->NumberOfBytes = *NumberOfBytes;
162 Map->Operation = Operation;
163
164 *Mapping = Map;
165
166 return EFI_SUCCESS;
167
168 FreeMapInfo:
169 FreePool (Map);
170
171 return Status;
172 }
173
174
175 /**
176 Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()
177 operation and releases any corresponding resources.
178
179 @param Mapping The mapping value returned from DmaMap*().
180
181 @retval EFI_SUCCESS The range was unmapped.
182 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
183 @retval EFI_INVALID_PARAMETER An inconsistency was detected between the mapping type
184 and the DoubleBuffer field
185
186 **/
187 EFI_STATUS
188 EFIAPI
189 DmaUnmap (
190 IN VOID *Mapping
191 )
192 {
193 MAP_INFO_INSTANCE *Map;
194 EFI_STATUS Status;
195
196 if (Mapping == NULL) {
197 ASSERT (FALSE);
198 return EFI_INVALID_PARAMETER;
199 }
200
201 Map = (MAP_INFO_INSTANCE *)Mapping;
202
203 Status = EFI_SUCCESS;
204 if (Map->DoubleBuffer) {
205 ASSERT (Map->Operation != MapOperationBusMasterCommonBuffer);
206
207 if (Map->Operation == MapOperationBusMasterCommonBuffer) {
208 Status = EFI_INVALID_PARAMETER;
209 } else if (Map->Operation == MapOperationBusMasterWrite) {
210 CopyMem ((VOID *)(UINTN)Map->HostAddress, (VOID *)(UINTN)Map->DeviceAddress, Map->NumberOfBytes);
211 }
212
213 DmaFreeBuffer (EFI_SIZE_TO_PAGES (Map->NumberOfBytes), (VOID *)(UINTN)Map->DeviceAddress);
214
215 } else {
216 if (Map->Operation == MapOperationBusMasterWrite) {
217 //
218 // Make sure we read buffer from uncached memory and not the cache
219 //
220 gCpu->FlushDataCache (gCpu, Map->HostAddress, Map->NumberOfBytes, EfiCpuFlushTypeInvalidate);
221 }
222 }
223
224 FreePool (Map);
225
226 return Status;
227 }
228
229 /**
230 Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.
231 mapping.
232
233 @param MemoryType The type of memory to allocate, EfiBootServicesData or
234 EfiRuntimeServicesData.
235 @param Pages The number of pages to allocate.
236 @param HostAddress A pointer to store the base system memory address of the
237 allocated range.
238
239 @retval EFI_SUCCESS The requested memory pages were allocated.
240 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
241 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
242 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
243 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
244
245 **/
246 EFI_STATUS
247 EFIAPI
248 DmaAllocateBuffer (
249 IN EFI_MEMORY_TYPE MemoryType,
250 IN UINTN Pages,
251 OUT VOID **HostAddress
252 )
253 {
254 VOID *Allocation;
255
256 if (HostAddress == NULL) {
257 return EFI_INVALID_PARAMETER;
258 }
259
260 //
261 // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData
262 //
263 // We used uncached memory to keep coherency
264 //
265 if (MemoryType == EfiBootServicesData) {
266 Allocation = UncachedAllocatePages (Pages);
267 } else if (MemoryType == EfiRuntimeServicesData) {
268 Allocation = UncachedAllocateRuntimePages (Pages);
269 } else {
270 return EFI_INVALID_PARAMETER;
271 }
272
273 if (Allocation == NULL) {
274 return EFI_OUT_OF_RESOURCES;
275 }
276
277 *HostAddress = Allocation;
278
279 return EFI_SUCCESS;
280 }
281
282
283 /**
284 Frees memory that was allocated with DmaAllocateBuffer().
285
286 @param Pages The number of pages to free.
287 @param HostAddress The base system memory address of the allocated range.
288
289 @retval EFI_SUCCESS The requested memory pages were freed.
290 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
291 was not allocated with DmaAllocateBuffer().
292
293 **/
294 EFI_STATUS
295 EFIAPI
296 DmaFreeBuffer (
297 IN UINTN Pages,
298 IN VOID *HostAddress
299 )
300 {
301 if (HostAddress == NULL) {
302 return EFI_INVALID_PARAMETER;
303 }
304
305 UncachedFreePages (HostAddress, Pages);
306 return EFI_SUCCESS;
307 }
308
309
310 EFI_STATUS
311 EFIAPI
312 ArmDmaLibConstructor (
313 IN EFI_HANDLE ImageHandle,
314 IN EFI_SYSTEM_TABLE *SystemTable
315 )
316 {
317 EFI_STATUS Status;
318
319 // Get the Cpu protocol for later use
320 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&gCpu);
321 ASSERT_EFI_ERROR(Status);
322
323 gCacheAlignment = ArmCacheWritebackGranule ();
324
325 return Status;
326 }
327