]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/ArmDmaLib/ArmDmaLib.c
EmbeddedPkg: MmcDxe - Recieve response was missing after CMD12
[mirror_edk2.git] / ArmPkg / Library / ArmDmaLib / ArmDmaLib.c
CommitLineData
938f46b4 1/** @file\r
2 Generic ARM implementation of DmaLib.h\r
3\r
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\r
3a424c5f 5 Copyright (c) 2015 - 2017, Linaro, Ltd. All rights reserved.<BR>\r
3402aac7 6\r
938f46b4 7 This program and the accompanying materials\r
8 are licensed and made available under the terms and conditions of the BSD License\r
9 which accompanies this distribution. The full text of the license may be found at\r
10 http://opensource.org/licenses/bsd-license.php\r
11\r
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
14\r
15**/\r
16\r
e6d572ba 17#include <PiDxe.h>\r
6650d785 18#include <Library/BaseLib.h>\r
938f46b4 19#include <Library/DebugLib.h>\r
20#include <Library/DmaLib.h>\r
e6d572ba 21#include <Library/DxeServicesTableLib.h>\r
938f46b4 22#include <Library/MemoryAllocationLib.h>\r
23#include <Library/UefiBootServicesTableLib.h>\r
938f46b4 24#include <Library/IoLib.h>\r
25#include <Library/BaseMemoryLib.h>\r
938f46b4 26\r
27#include <Protocol/Cpu.h>\r
28\r
29typedef struct {\r
30 EFI_PHYSICAL_ADDRESS HostAddress;\r
df8c2668 31 VOID *BufferAddress;\r
938f46b4 32 UINTN NumberOfBytes;\r
33 DMA_MAP_OPERATION Operation;\r
34 BOOLEAN DoubleBuffer;\r
35} MAP_INFO_INSTANCE;\r
36\r
37\r
6650d785
AB
38typedef struct {\r
39 LIST_ENTRY Link;\r
40 VOID *HostAddress;\r
41 UINTN NumPages;\r
42 UINT64 Attributes;\r
43} UNCACHED_ALLOCATION;\r
938f46b4 44\r
de2ec785 45STATIC EFI_CPU_ARCH_PROTOCOL *mCpu;\r
6650d785 46STATIC LIST_ENTRY UncachedAllocationList;\r
938f46b4 47\r
bfe34275
AB
48STATIC\r
49PHYSICAL_ADDRESS\r
50HostToDeviceAddress (\r
6650d785 51 IN VOID *Address\r
bfe34275
AB
52 )\r
53{\r
6650d785 54 return (PHYSICAL_ADDRESS)(UINTN)Address + PcdGet64 (PcdArmDmaDeviceOffset);\r
bfe34275
AB
55}\r
56\r
3402aac7 57/**\r
938f46b4 58 Provides the DMA controller-specific addresses needed to access system memory.\r
3402aac7 59\r
938f46b4 60 Operation is relative to the DMA bus master.\r
3402aac7 61\r
938f46b4 62 @param Operation Indicates if the bus master is going to read or write to system memory.\r
63 @param HostAddress The system memory address to map to the DMA controller.\r
64 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes\r
3402aac7 65 that were mapped.\r
938f46b4 66 @param DeviceAddress The resulting map address for the bus master controller to use to\r
3402aac7 67 access the hosts HostAddress.\r
938f46b4 68 @param Mapping A resulting value to pass to Unmap().\r
3402aac7 69\r
938f46b4 70 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.\r
3402aac7 71 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.\r
938f46b4 72 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
73 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
74 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.\r
3402aac7 75\r
938f46b4 76**/\r
77EFI_STATUS\r
78EFIAPI\r
79DmaMap (\r
80 IN DMA_MAP_OPERATION Operation,\r
81 IN VOID *HostAddress,\r
82 IN OUT UINTN *NumberOfBytes,\r
83 OUT PHYSICAL_ADDRESS *DeviceAddress,\r
84 OUT VOID **Mapping\r
85 )\r
86{\r
e6d572ba 87 EFI_STATUS Status;\r
88 MAP_INFO_INSTANCE *Map;\r
89 VOID *Buffer;\r
90 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
3a424c5f 91 UINTN AllocSize;\r
938f46b4 92\r
e6d572ba 93 if (HostAddress == NULL || NumberOfBytes == NULL || DeviceAddress == NULL || Mapping == NULL ) {\r
938f46b4 94 return EFI_INVALID_PARAMETER;\r
95 }\r
938f46b4 96\r
97 if (Operation >= MapOperationMaximum) {\r
98 return EFI_INVALID_PARAMETER;\r
99 }\r
100\r
6650d785 101 *DeviceAddress = HostToDeviceAddress (HostAddress);\r
938f46b4 102\r
103 // Remember range so we can flush on the other side\r
104 Map = AllocatePool (sizeof (MAP_INFO_INSTANCE));\r
105 if (Map == NULL) {\r
106 return EFI_OUT_OF_RESOURCES;\r
107 }\r
3402aac7 108\r
3a424c5f
AB
109 if (Operation != MapOperationBusMasterRead &&\r
110 ((((UINTN)HostAddress & (mCpu->DmaBufferAlignment - 1)) != 0) ||\r
111 ((*NumberOfBytes & (mCpu->DmaBufferAlignment - 1)) != 0))) {\r
e6d572ba 112\r
113 // Get the cacheability of the region\r
df8c2668 114 Status = gDS->GetMemorySpaceDescriptor ((UINTN)HostAddress, &GcdDescriptor);\r
e6d572ba 115 if (EFI_ERROR(Status)) {\r
84083b12 116 goto FreeMapInfo;\r
938f46b4 117 }\r
e6d572ba 118\r
119 // If the mapped buffer is not an uncached buffer\r
885d57ef 120 if ((GcdDescriptor.Attributes & (EFI_MEMORY_WB | EFI_MEMORY_WT)) != 0) {\r
a24f7d66
AB
121 //\r
122 // Operations of type MapOperationBusMasterCommonBuffer are only allowed\r
123 // on uncached buffers.\r
124 //\r
125 if (Operation == MapOperationBusMasterCommonBuffer) {\r
126 DEBUG ((EFI_D_ERROR,\r
127 "%a: Operation type 'MapOperationBusMasterCommonBuffer' is only supported\n"\r
128 "on memory regions that were allocated using DmaAllocateBuffer ()\n",\r
129 __FUNCTION__));\r
84083b12
DE
130 Status = EFI_UNSUPPORTED;\r
131 goto FreeMapInfo;\r
a24f7d66
AB
132 }\r
133\r
e6d572ba 134 //\r
3a424c5f
AB
135 // If the buffer does not fill entire cache lines we must double buffer\r
136 // into a suitably aligned allocation that allows us to invalidate the\r
137 // cache without running the risk of corrupting adjacent unrelated data.\r
138 // Note that pool allocations are guaranteed to be 8 byte aligned, so\r
139 // we only have to add (alignment - 8) worth of padding.\r
e6d572ba 140 //\r
3a424c5f
AB
141 Map->DoubleBuffer = TRUE;\r
142 AllocSize = ALIGN_VALUE (*NumberOfBytes, mCpu->DmaBufferAlignment) +\r
143 (mCpu->DmaBufferAlignment - 8);\r
144 Map->BufferAddress = AllocatePool (AllocSize);\r
145 if (Map->BufferAddress == NULL) {\r
146 Status = EFI_OUT_OF_RESOURCES;\r
84083b12 147 goto FreeMapInfo;\r
e6d572ba 148 }\r
149\r
3a424c5f 150 Buffer = ALIGN_POINTER (Map->BufferAddress, mCpu->DmaBufferAlignment);\r
6650d785 151 *DeviceAddress = HostToDeviceAddress (Buffer);\r
3a424c5f
AB
152\r
153 //\r
154 // Get rid of any dirty cachelines covering the double buffer. This\r
155 // prevents them from being written back unexpectedly, potentially\r
156 // overwriting the data we receive from the device.\r
157 //\r
158 mCpu->FlushDataCache (mCpu, (UINTN)Buffer, *NumberOfBytes,\r
159 EfiCpuFlushTypeWriteBack);\r
e6d572ba 160 } else {\r
161 Map->DoubleBuffer = FALSE;\r
162 }\r
938f46b4 163 } else {\r
164 Map->DoubleBuffer = FALSE;\r
e6d572ba 165\r
b64e44cc
AB
166 DEBUG_CODE_BEGIN ();\r
167\r
168 //\r
169 // The operation type check above only executes if the buffer happens to be\r
170 // misaligned with respect to CWG, but even if it is aligned, we should not\r
171 // allow arbitrary buffers to be used for creating consistent mappings.\r
172 // So duplicate the check here when running in DEBUG mode, just to assert\r
173 // that we are not trying to create a consistent mapping for cached memory.\r
174 //\r
df8c2668 175 Status = gDS->GetMemorySpaceDescriptor ((UINTN)HostAddress, &GcdDescriptor);\r
b64e44cc
AB
176 ASSERT_EFI_ERROR(Status);\r
177\r
178 ASSERT (Operation != MapOperationBusMasterCommonBuffer ||\r
179 (GcdDescriptor.Attributes & (EFI_MEMORY_WB | EFI_MEMORY_WT)) == 0);\r
180\r
181 DEBUG_CODE_END ();\r
182\r
e6d572ba 183 // Flush the Data Cache (should not have any effect if the memory region is uncached)\r
df8c2668 184 mCpu->FlushDataCache (mCpu, (UINTN)HostAddress, *NumberOfBytes,\r
de2ec785 185 EfiCpuFlushTypeWriteBackInvalidate);\r
938f46b4 186 }\r
187\r
938f46b4 188 Map->HostAddress = (UINTN)HostAddress;\r
938f46b4 189 Map->NumberOfBytes = *NumberOfBytes;\r
190 Map->Operation = Operation;\r
191\r
84083b12
DE
192 *Mapping = Map;\r
193\r
938f46b4 194 return EFI_SUCCESS;\r
84083b12
DE
195\r
196FreeMapInfo:\r
197 FreePool (Map);\r
198\r
199 return Status;\r
938f46b4 200}\r
201\r
202\r
3402aac7 203/**\r
938f46b4 204 Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()\r
205 operation and releases any corresponding resources.\r
3402aac7 206\r
938f46b4 207 @param Mapping The mapping value returned from DmaMap*().\r
3402aac7 208\r
938f46b4 209 @retval EFI_SUCCESS The range was unmapped.\r
210 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.\r
a24f7d66
AB
211 @retval EFI_INVALID_PARAMETER An inconsistency was detected between the mapping type\r
212 and the DoubleBuffer field\r
3402aac7 213\r
938f46b4 214**/\r
215EFI_STATUS\r
216EFIAPI\r
217DmaUnmap (\r
218 IN VOID *Mapping\r
219 )\r
220{\r
221 MAP_INFO_INSTANCE *Map;\r
a24f7d66 222 EFI_STATUS Status;\r
3a424c5f 223 VOID *Buffer;\r
3402aac7 224\r
938f46b4 225 if (Mapping == NULL) {\r
226 ASSERT (FALSE);\r
227 return EFI_INVALID_PARAMETER;\r
228 }\r
3402aac7 229\r
938f46b4 230 Map = (MAP_INFO_INSTANCE *)Mapping;\r
3402aac7 231\r
a24f7d66 232 Status = EFI_SUCCESS;\r
938f46b4 233 if (Map->DoubleBuffer) {\r
3a424c5f 234 ASSERT (Map->Operation == MapOperationBusMasterWrite);\r
a24f7d66 235\r
3a424c5f 236 if (Map->Operation != MapOperationBusMasterWrite) {\r
a24f7d66 237 Status = EFI_INVALID_PARAMETER;\r
3a424c5f
AB
238 } else {\r
239 Buffer = ALIGN_POINTER (Map->BufferAddress, mCpu->DmaBufferAlignment);\r
3402aac7 240\r
3a424c5f
AB
241 mCpu->FlushDataCache (mCpu, (UINTN)Buffer, Map->NumberOfBytes,\r
242 EfiCpuFlushTypeInvalidate);\r
3402aac7 243\r
3a424c5f
AB
244 CopyMem ((VOID *)(UINTN)Map->HostAddress, Buffer, Map->NumberOfBytes);\r
245\r
246 FreePool (Map->BufferAddress);\r
247 }\r
938f46b4 248 } else {\r
249 if (Map->Operation == MapOperationBusMasterWrite) {\r
250 //\r
251 // Make sure we read buffer from uncached memory and not the cache\r
252 //\r
de2ec785
AB
253 mCpu->FlushDataCache (mCpu, Map->HostAddress, Map->NumberOfBytes,\r
254 EfiCpuFlushTypeInvalidate);\r
938f46b4 255 }\r
256 }\r
3402aac7 257\r
938f46b4 258 FreePool (Map);\r
259\r
a24f7d66 260 return Status;\r
938f46b4 261}\r
262\r
3402aac7 263/**\r
938f46b4 264 Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.\r
3402aac7
RC
265 mapping.\r
266\r
938f46b4 267 @param MemoryType The type of memory to allocate, EfiBootServicesData or\r
3402aac7
RC
268 EfiRuntimeServicesData.\r
269 @param Pages The number of pages to allocate.\r
938f46b4 270 @param HostAddress A pointer to store the base system memory address of the\r
3402aac7 271 allocated range.\r
938f46b4 272\r
6650d785 273 @retval EFI_SUCCESS The requested memory pages were allocated.\r
938f46b4 274 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are\r
3402aac7 275 MEMORY_WRITE_COMBINE and MEMORY_CACHED.\r
938f46b4 276 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
3402aac7
RC
277 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
278\r
938f46b4 279**/\r
280EFI_STATUS\r
281EFIAPI\r
282DmaAllocateBuffer (\r
283 IN EFI_MEMORY_TYPE MemoryType,\r
284 IN UINTN Pages,\r
285 OUT VOID **HostAddress\r
286 )\r
4b4104d8
AB
287{\r
288 return DmaAllocateAlignedBuffer (MemoryType, Pages, 0, HostAddress);\r
289}\r
290\r
291/**\r
292 Allocates pages that are suitable for an DmaMap() of type\r
293 MapOperationBusMasterCommonBuffer mapping, at the requested alignment.\r
294\r
295 @param MemoryType The type of memory to allocate, EfiBootServicesData or\r
296 EfiRuntimeServicesData.\r
297 @param Pages The number of pages to allocate.\r
298 @param Alignment Alignment in bytes of the base of the returned\r
299 buffer (must be a power of 2)\r
300 @param HostAddress A pointer to store the base system memory address of the\r
301 allocated range.\r
302\r
303 @retval EFI_SUCCESS The requested memory pages were allocated.\r
304 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are\r
305 MEMORY_WRITE_COMBINE and MEMORY_CACHED.\r
306 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
307 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
308\r
309**/\r
310EFI_STATUS\r
311EFIAPI\r
312DmaAllocateAlignedBuffer (\r
313 IN EFI_MEMORY_TYPE MemoryType,\r
314 IN UINTN Pages,\r
315 IN UINTN Alignment,\r
316 OUT VOID **HostAddress\r
317 )\r
938f46b4 318{\r
6650d785
AB
319 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
320 VOID *Allocation;\r
321 UINT64 MemType;\r
322 UNCACHED_ALLOCATION *Alloc;\r
323 EFI_STATUS Status;\r
e55f8c73 324\r
4b4104d8
AB
325 if (Alignment == 0) {\r
326 Alignment = EFI_PAGE_SIZE;\r
327 }\r
328\r
329 if (HostAddress == NULL ||\r
330 (Alignment & (Alignment - 1)) != 0) {\r
938f46b4 331 return EFI_INVALID_PARAMETER;\r
332 }\r
333\r
938f46b4 334 if (MemoryType == EfiBootServicesData) {\r
4b4104d8 335 Allocation = AllocateAlignedPages (Pages, Alignment);\r
1bfda055 336 } else if (MemoryType == EfiRuntimeServicesData) {\r
4b4104d8 337 Allocation = AllocateAlignedRuntimePages (Pages, Alignment);\r
938f46b4 338 } else {\r
339 return EFI_INVALID_PARAMETER;\r
340 }\r
341\r
e55f8c73
AB
342 if (Allocation == NULL) {\r
343 return EFI_OUT_OF_RESOURCES;\r
344 }\r
345\r
6650d785
AB
346 // Get the cacheability of the region\r
347 Status = gDS->GetMemorySpaceDescriptor ((UINTN)Allocation, &GcdDescriptor);\r
348 if (EFI_ERROR(Status)) {\r
349 goto FreeBuffer;\r
350 }\r
351\r
352 // Choose a suitable uncached memory type that is supported by the region\r
353 if (GcdDescriptor.Capabilities & EFI_MEMORY_WC) {\r
354 MemType = EFI_MEMORY_WC;\r
355 } else if (GcdDescriptor.Capabilities & EFI_MEMORY_UC) {\r
356 MemType = EFI_MEMORY_UC;\r
357 } else {\r
358 Status = EFI_UNSUPPORTED;\r
359 goto FreeBuffer;\r
360 }\r
361\r
362 Alloc = AllocatePool (sizeof *Alloc);\r
363 if (Alloc == NULL) {\r
364 goto FreeBuffer;\r
365 }\r
366\r
367 Alloc->HostAddress = Allocation;\r
368 Alloc->NumPages = Pages;\r
369 Alloc->Attributes = GcdDescriptor.Attributes;\r
370\r
371 InsertHeadList (&UncachedAllocationList, &Alloc->Link);\r
372\r
373 // Remap the region with the new attributes\r
374 Status = gDS->SetMemorySpaceAttributes ((PHYSICAL_ADDRESS)(UINTN)Allocation,\r
375 EFI_PAGES_TO_SIZE (Pages),\r
376 MemType);\r
377 if (EFI_ERROR (Status)) {\r
378 goto FreeAlloc;\r
379 }\r
380\r
381 Status = mCpu->FlushDataCache (mCpu,\r
382 (PHYSICAL_ADDRESS)(UINTN)Allocation,\r
383 EFI_PAGES_TO_SIZE (Pages),\r
384 EfiCpuFlushTypeInvalidate);\r
385 if (EFI_ERROR (Status)) {\r
386 goto FreeAlloc;\r
387 }\r
388\r
e55f8c73
AB
389 *HostAddress = Allocation;\r
390\r
938f46b4 391 return EFI_SUCCESS;\r
6650d785
AB
392\r
393FreeAlloc:\r
394 RemoveEntryList (&Alloc->Link);\r
395 FreePool (Alloc);\r
396\r
397FreeBuffer:\r
398 FreePages (Allocation, Pages);\r
399 return Status;\r
938f46b4 400}\r
401\r
402\r
3402aac7 403/**\r
938f46b4 404 Frees memory that was allocated with DmaAllocateBuffer().\r
3402aac7
RC
405\r
406 @param Pages The number of pages to free.\r
407 @param HostAddress The base system memory address of the allocated range.\r
408\r
938f46b4 409 @retval EFI_SUCCESS The requested memory pages were freed.\r
410 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages\r
411 was not allocated with DmaAllocateBuffer().\r
3402aac7 412\r
938f46b4 413**/\r
414EFI_STATUS\r
415EFIAPI\r
416DmaFreeBuffer (\r
417 IN UINTN Pages,\r
418 IN VOID *HostAddress\r
419 )\r
420{\r
6650d785
AB
421 LIST_ENTRY *Link;\r
422 UNCACHED_ALLOCATION *Alloc;\r
423 BOOLEAN Found;\r
424 EFI_STATUS Status;\r
425\r
938f46b4 426 if (HostAddress == NULL) {\r
427 return EFI_INVALID_PARAMETER;\r
3402aac7
RC
428 }\r
429\r
6650d785
AB
430 for (Link = GetFirstNode (&UncachedAllocationList), Found = FALSE;\r
431 !IsNull (&UncachedAllocationList, Link);\r
432 Link = GetNextNode (&UncachedAllocationList, Link)) {\r
433\r
434 Alloc = BASE_CR (Link, UNCACHED_ALLOCATION, Link);\r
435 if (Alloc->HostAddress == HostAddress && Alloc->NumPages == Pages) {\r
436 Found = TRUE;\r
437 break;\r
438 }\r
439 }\r
440\r
441 if (!Found) {\r
442 ASSERT (FALSE);\r
443 return EFI_INVALID_PARAMETER;\r
444 }\r
445\r
446 RemoveEntryList (&Alloc->Link);\r
447\r
448 Status = gDS->SetMemorySpaceAttributes ((PHYSICAL_ADDRESS)(UINTN)HostAddress,\r
449 EFI_PAGES_TO_SIZE (Pages),\r
450 Alloc->Attributes);\r
451 if (EFI_ERROR (Status)) {\r
452 goto FreeAlloc;\r
453 }\r
454\r
455 //\r
456 // If we fail to restore the original attributes, it is better to leak the\r
457 // memory than to return it to the heap\r
458 //\r
459 FreePages (HostAddress, Pages);\r
460\r
461FreeAlloc:\r
462 FreePool (Alloc);\r
463 return Status;\r
938f46b4 464}\r
465\r
466\r
467EFI_STATUS\r
468EFIAPI\r
469ArmDmaLibConstructor (\r
470 IN EFI_HANDLE ImageHandle,\r
471 IN EFI_SYSTEM_TABLE *SystemTable\r
472 )\r
473{\r
6650d785 474 InitializeListHead (&UncachedAllocationList);\r
938f46b4 475\r
476 // Get the Cpu protocol for later use\r
6650d785 477 return gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&mCpu);\r
938f46b4 478}\r