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