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