]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/ArmDmaLib/ArmDmaLib.c
566f77d03f293109e1e0d99fe5ca72b0301b34a0
[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 Copyright (c) 2015 - 2017, Linaro, Ltd. All rights reserved.<BR>
6
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include <PiDxe.h>
18 #include <Library/DebugLib.h>
19 #include <Library/DmaLib.h>
20 #include <Library/DxeServicesTableLib.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/UefiBootServicesTableLib.h>
23 #include <Library/UncachedMemoryAllocationLib.h>
24 #include <Library/IoLib.h>
25 #include <Library/BaseMemoryLib.h>
26
27 #include <Protocol/Cpu.h>
28
29 typedef struct {
30 EFI_PHYSICAL_ADDRESS HostAddress;
31 VOID *BufferAddress;
32 UINTN NumberOfBytes;
33 DMA_MAP_OPERATION Operation;
34 BOOLEAN DoubleBuffer;
35 } MAP_INFO_INSTANCE;
36
37
38
39 STATIC EFI_CPU_ARCH_PROTOCOL *mCpu;
40
41 STATIC
42 PHYSICAL_ADDRESS
43 HostToDeviceAddress (
44 IN PHYSICAL_ADDRESS HostAddress
45 )
46 {
47 return HostAddress + PcdGet64 (PcdArmDmaDeviceOffset);
48 }
49
50 /**
51 Provides the DMA controller-specific addresses needed to access system memory.
52
53 Operation is relative to the DMA bus master.
54
55 @param Operation Indicates if the bus master is going to read or write to system memory.
56 @param HostAddress The system memory address to map to the DMA controller.
57 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
58 that were mapped.
59 @param DeviceAddress The resulting map address for the bus master controller to use to
60 access the hosts HostAddress.
61 @param Mapping A resulting value to pass to Unmap().
62
63 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
64 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
65 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
66 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
67 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
68
69 **/
70 EFI_STATUS
71 EFIAPI
72 DmaMap (
73 IN DMA_MAP_OPERATION Operation,
74 IN VOID *HostAddress,
75 IN OUT UINTN *NumberOfBytes,
76 OUT PHYSICAL_ADDRESS *DeviceAddress,
77 OUT VOID **Mapping
78 )
79 {
80 EFI_STATUS Status;
81 MAP_INFO_INSTANCE *Map;
82 VOID *Buffer;
83 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;
84 UINTN AllocSize;
85
86 if (HostAddress == NULL || NumberOfBytes == NULL || DeviceAddress == NULL || Mapping == NULL ) {
87 return EFI_INVALID_PARAMETER;
88 }
89
90 if (Operation >= MapOperationMaximum) {
91 return EFI_INVALID_PARAMETER;
92 }
93
94 //
95 // The debug implementation of UncachedMemoryAllocationLib in ArmPkg returns
96 // a virtual uncached alias, and unmaps the cached ID mapping of the buffer,
97 // in order to catch inadvertent references to the cached mapping.
98 // Since HostToDeviceAddress () expects ID mapped input addresses, convert
99 // the host address to an ID mapped address first.
100 //
101 *DeviceAddress = HostToDeviceAddress (ConvertToPhysicalAddress (HostAddress));
102
103 // Remember range so we can flush on the other side
104 Map = AllocatePool (sizeof (MAP_INFO_INSTANCE));
105 if (Map == NULL) {
106 return EFI_OUT_OF_RESOURCES;
107 }
108
109 if (Operation != MapOperationBusMasterRead &&
110 ((((UINTN)HostAddress & (mCpu->DmaBufferAlignment - 1)) != 0) ||
111 ((*NumberOfBytes & (mCpu->DmaBufferAlignment - 1)) != 0))) {
112
113 // Get the cacheability of the region
114 Status = gDS->GetMemorySpaceDescriptor ((UINTN)HostAddress, &GcdDescriptor);
115 if (EFI_ERROR(Status)) {
116 goto FreeMapInfo;
117 }
118
119 // If the mapped buffer is not an uncached buffer
120 if ((GcdDescriptor.Attributes & (EFI_MEMORY_WB | EFI_MEMORY_WT)) != 0) {
121 //
122 // Operations of type MapOperationBusMasterCommonBuffer are only allowed
123 // on uncached buffers.
124 //
125 if (Operation == MapOperationBusMasterCommonBuffer) {
126 DEBUG ((EFI_D_ERROR,
127 "%a: Operation type 'MapOperationBusMasterCommonBuffer' is only supported\n"
128 "on memory regions that were allocated using DmaAllocateBuffer ()\n",
129 __FUNCTION__));
130 Status = EFI_UNSUPPORTED;
131 goto FreeMapInfo;
132 }
133
134 //
135 // If the buffer does not fill entire cache lines we must double buffer
136 // into a suitably aligned allocation that allows us to invalidate the
137 // cache without running the risk of corrupting adjacent unrelated data.
138 // Note that pool allocations are guaranteed to be 8 byte aligned, so
139 // we only have to add (alignment - 8) worth of padding.
140 //
141 Map->DoubleBuffer = TRUE;
142 AllocSize = ALIGN_VALUE (*NumberOfBytes, mCpu->DmaBufferAlignment) +
143 (mCpu->DmaBufferAlignment - 8);
144 Map->BufferAddress = AllocatePool (AllocSize);
145 if (Map->BufferAddress == NULL) {
146 Status = EFI_OUT_OF_RESOURCES;
147 goto FreeMapInfo;
148 }
149
150 Buffer = ALIGN_POINTER (Map->BufferAddress, mCpu->DmaBufferAlignment);
151 *DeviceAddress = HostToDeviceAddress (ConvertToPhysicalAddress (Buffer));
152
153 //
154 // Get rid of any dirty cachelines covering the double buffer. This
155 // prevents them from being written back unexpectedly, potentially
156 // overwriting the data we receive from the device.
157 //
158 mCpu->FlushDataCache (mCpu, (UINTN)Buffer, *NumberOfBytes,
159 EfiCpuFlushTypeWriteBack);
160 } else {
161 Map->DoubleBuffer = FALSE;
162 }
163 } else {
164 Map->DoubleBuffer = FALSE;
165
166 DEBUG_CODE_BEGIN ();
167
168 //
169 // The operation type check above only executes if the buffer happens to be
170 // misaligned with respect to CWG, but even if it is aligned, we should not
171 // allow arbitrary buffers to be used for creating consistent mappings.
172 // So duplicate the check here when running in DEBUG mode, just to assert
173 // that we are not trying to create a consistent mapping for cached memory.
174 //
175 Status = gDS->GetMemorySpaceDescriptor ((UINTN)HostAddress, &GcdDescriptor);
176 ASSERT_EFI_ERROR(Status);
177
178 ASSERT (Operation != MapOperationBusMasterCommonBuffer ||
179 (GcdDescriptor.Attributes & (EFI_MEMORY_WB | EFI_MEMORY_WT)) == 0);
180
181 DEBUG_CODE_END ();
182
183 // Flush the Data Cache (should not have any effect if the memory region is uncached)
184 mCpu->FlushDataCache (mCpu, (UINTN)HostAddress, *NumberOfBytes,
185 EfiCpuFlushTypeWriteBackInvalidate);
186 }
187
188 Map->HostAddress = (UINTN)HostAddress;
189 Map->NumberOfBytes = *NumberOfBytes;
190 Map->Operation = Operation;
191
192 *Mapping = Map;
193
194 return EFI_SUCCESS;
195
196 FreeMapInfo:
197 FreePool (Map);
198
199 return Status;
200 }
201
202
203 /**
204 Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()
205 operation and releases any corresponding resources.
206
207 @param Mapping The mapping value returned from DmaMap*().
208
209 @retval EFI_SUCCESS The range was unmapped.
210 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
211 @retval EFI_INVALID_PARAMETER An inconsistency was detected between the mapping type
212 and the DoubleBuffer field
213
214 **/
215 EFI_STATUS
216 EFIAPI
217 DmaUnmap (
218 IN VOID *Mapping
219 )
220 {
221 MAP_INFO_INSTANCE *Map;
222 EFI_STATUS Status;
223 VOID *Buffer;
224
225 if (Mapping == NULL) {
226 ASSERT (FALSE);
227 return EFI_INVALID_PARAMETER;
228 }
229
230 Map = (MAP_INFO_INSTANCE *)Mapping;
231
232 Status = EFI_SUCCESS;
233 if (Map->DoubleBuffer) {
234 ASSERT (Map->Operation == MapOperationBusMasterWrite);
235
236 if (Map->Operation != MapOperationBusMasterWrite) {
237 Status = EFI_INVALID_PARAMETER;
238 } else {
239 Buffer = ALIGN_POINTER (Map->BufferAddress, mCpu->DmaBufferAlignment);
240
241 mCpu->FlushDataCache (mCpu, (UINTN)Buffer, Map->NumberOfBytes,
242 EfiCpuFlushTypeInvalidate);
243
244 CopyMem ((VOID *)(UINTN)Map->HostAddress, Buffer, Map->NumberOfBytes);
245
246 FreePool (Map->BufferAddress);
247 }
248 } else {
249 if (Map->Operation == MapOperationBusMasterWrite) {
250 //
251 // Make sure we read buffer from uncached memory and not the cache
252 //
253 mCpu->FlushDataCache (mCpu, Map->HostAddress, Map->NumberOfBytes,
254 EfiCpuFlushTypeInvalidate);
255 }
256 }
257
258 FreePool (Map);
259
260 return Status;
261 }
262
263 /**
264 Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.
265 mapping.
266
267 @param MemoryType The type of memory to allocate, EfiBootServicesData or
268 EfiRuntimeServicesData.
269 @param Pages The number of pages to allocate.
270 @param HostAddress A pointer to store the base system memory address of the
271 allocated range.
272
273 @retval EFI_SUCCESS The requested memory pages were allocated.
274 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
275 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
276 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
277 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
278
279 **/
280 EFI_STATUS
281 EFIAPI
282 DmaAllocateBuffer (
283 IN EFI_MEMORY_TYPE MemoryType,
284 IN UINTN Pages,
285 OUT VOID **HostAddress
286 )
287 {
288 VOID *Allocation;
289
290 if (HostAddress == NULL) {
291 return EFI_INVALID_PARAMETER;
292 }
293
294 //
295 // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData
296 //
297 // We used uncached memory to keep coherency
298 //
299 if (MemoryType == EfiBootServicesData) {
300 Allocation = UncachedAllocatePages (Pages);
301 } else if (MemoryType == EfiRuntimeServicesData) {
302 Allocation = UncachedAllocateRuntimePages (Pages);
303 } else {
304 return EFI_INVALID_PARAMETER;
305 }
306
307 if (Allocation == NULL) {
308 return EFI_OUT_OF_RESOURCES;
309 }
310
311 *HostAddress = Allocation;
312
313 return EFI_SUCCESS;
314 }
315
316
317 /**
318 Frees memory that was allocated with DmaAllocateBuffer().
319
320 @param Pages The number of pages to free.
321 @param HostAddress The base system memory address of the allocated range.
322
323 @retval EFI_SUCCESS The requested memory pages were freed.
324 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
325 was not allocated with DmaAllocateBuffer().
326
327 **/
328 EFI_STATUS
329 EFIAPI
330 DmaFreeBuffer (
331 IN UINTN Pages,
332 IN VOID *HostAddress
333 )
334 {
335 if (HostAddress == NULL) {
336 return EFI_INVALID_PARAMETER;
337 }
338
339 UncachedFreePages (HostAddress, Pages);
340 return EFI_SUCCESS;
341 }
342
343
344 EFI_STATUS
345 EFIAPI
346 ArmDmaLibConstructor (
347 IN EFI_HANDLE ImageHandle,
348 IN EFI_SYSTEM_TABLE *SystemTable
349 )
350 {
351 EFI_STATUS Status;
352
353 // Get the Cpu protocol for later use
354 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&mCpu);
355 ASSERT_EFI_ERROR(Status);
356
357 return Status;
358 }
359