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