]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/ArmDmaLib/ArmDmaLib.c
ArmPkg/ArmDmaLib: interpret GCD attributes as a bit field
[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
e6d572ba 106 //\r
107 // If the buffer does not fill entire cache lines we must double buffer into\r
108 // uncached memory. Device (PCI) address becomes uncached page.\r
109 //\r
110 Map->DoubleBuffer = TRUE;\r
111 Status = DmaAllocateBuffer (EfiBootServicesData, EFI_SIZE_TO_PAGES (*NumberOfBytes), &Buffer);\r
112 if (EFI_ERROR (Status)) {\r
113 return Status;\r
114 }\r
115\r
116 if ((Operation == MapOperationBusMasterRead) || (Operation == MapOperationBusMasterCommonBuffer)) {\r
117 CopyMem (Buffer, HostAddress, *NumberOfBytes);\r
118 }\r
119\r
120 *DeviceAddress = (PHYSICAL_ADDRESS)(UINTN)Buffer;\r
121 } else {\r
122 Map->DoubleBuffer = FALSE;\r
123 }\r
938f46b4 124 } else {\r
125 Map->DoubleBuffer = FALSE;\r
e6d572ba 126\r
127 // Flush the Data Cache (should not have any effect if the memory region is uncached)\r
128 gCpu->FlushDataCache (gCpu, *DeviceAddress, *NumberOfBytes, EfiCpuFlushTypeWriteBackInvalidate);\r
129\r
130 if ((Operation == MapOperationBusMasterRead) || (Operation == MapOperationBusMasterCommonBuffer)) {\r
131 // In case the buffer is used for instance to send command to a PCI controller, we must ensure the memory is uncached\r
db06c2d7 132 Status = gDS->SetMemorySpaceAttributes (*DeviceAddress & ~(BASE_4KB - 1), ALIGN_VALUE (*NumberOfBytes, BASE_4KB), EFI_MEMORY_WC);\r
e6d572ba 133 ASSERT_EFI_ERROR (Status);\r
134 }\r
938f46b4 135 }\r
136\r
938f46b4 137 Map->HostAddress = (UINTN)HostAddress;\r
138 Map->DeviceAddress = *DeviceAddress;\r
139 Map->NumberOfBytes = *NumberOfBytes;\r
140 Map->Operation = Operation;\r
141\r
938f46b4 142 return EFI_SUCCESS;\r
143}\r
144\r
145\r
3402aac7 146/**\r
938f46b4 147 Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()\r
148 operation and releases any corresponding resources.\r
3402aac7 149\r
938f46b4 150 @param Mapping The mapping value returned from DmaMap*().\r
3402aac7 151\r
938f46b4 152 @retval EFI_SUCCESS The range was unmapped.\r
153 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.\r
3402aac7 154\r
938f46b4 155**/\r
156EFI_STATUS\r
157EFIAPI\r
158DmaUnmap (\r
159 IN VOID *Mapping\r
160 )\r
161{\r
162 MAP_INFO_INSTANCE *Map;\r
3402aac7 163\r
938f46b4 164 if (Mapping == NULL) {\r
165 ASSERT (FALSE);\r
166 return EFI_INVALID_PARAMETER;\r
167 }\r
3402aac7 168\r
938f46b4 169 Map = (MAP_INFO_INSTANCE *)Mapping;\r
3402aac7 170\r
938f46b4 171 if (Map->DoubleBuffer) {\r
e6d572ba 172 if ((Map->Operation == MapOperationBusMasterWrite) || (Map->Operation == MapOperationBusMasterCommonBuffer)) {\r
938f46b4 173 CopyMem ((VOID *)(UINTN)Map->HostAddress, (VOID *)(UINTN)Map->DeviceAddress, Map->NumberOfBytes);\r
174 }\r
3402aac7 175\r
938f46b4 176 DmaFreeBuffer (EFI_SIZE_TO_PAGES (Map->NumberOfBytes), (VOID *)(UINTN)Map->DeviceAddress);\r
3402aac7 177\r
938f46b4 178 } else {\r
179 if (Map->Operation == MapOperationBusMasterWrite) {\r
180 //\r
181 // Make sure we read buffer from uncached memory and not the cache\r
182 //\r
183 gCpu->FlushDataCache (gCpu, Map->HostAddress, Map->NumberOfBytes, EfiCpuFlushTypeInvalidate);\r
184 }\r
185 }\r
3402aac7 186\r
938f46b4 187 FreePool (Map);\r
188\r
189 return EFI_SUCCESS;\r
190}\r
191\r
3402aac7 192/**\r
938f46b4 193 Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.\r
3402aac7
RC
194 mapping.\r
195\r
938f46b4 196 @param MemoryType The type of memory to allocate, EfiBootServicesData or\r
3402aac7
RC
197 EfiRuntimeServicesData.\r
198 @param Pages The number of pages to allocate.\r
938f46b4 199 @param HostAddress A pointer to store the base system memory address of the\r
3402aac7 200 allocated range.\r
938f46b4 201\r
202 @retval EFI_SUCCESS The requested memory pages were allocated.\r
203 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are\r
3402aac7 204 MEMORY_WRITE_COMBINE and MEMORY_CACHED.\r
938f46b4 205 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
3402aac7
RC
206 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
207\r
938f46b4 208**/\r
209EFI_STATUS\r
210EFIAPI\r
211DmaAllocateBuffer (\r
212 IN EFI_MEMORY_TYPE MemoryType,\r
213 IN UINTN Pages,\r
214 OUT VOID **HostAddress\r
215 )\r
216{\r
e55f8c73
AB
217 VOID *Allocation;\r
218\r
938f46b4 219 if (HostAddress == NULL) {\r
220 return EFI_INVALID_PARAMETER;\r
221 }\r
222\r
223 //\r
224 // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData\r
225 //\r
226 // We used uncached memory to keep coherency\r
227 //\r
228 if (MemoryType == EfiBootServicesData) {\r
e55f8c73 229 Allocation = UncachedAllocatePages (Pages);\r
1bfda055 230 } else if (MemoryType == EfiRuntimeServicesData) {\r
e55f8c73 231 Allocation = UncachedAllocateRuntimePages (Pages);\r
938f46b4 232 } else {\r
233 return EFI_INVALID_PARAMETER;\r
234 }\r
235\r
e55f8c73
AB
236 if (Allocation == NULL) {\r
237 return EFI_OUT_OF_RESOURCES;\r
238 }\r
239\r
240 *HostAddress = Allocation;\r
241\r
938f46b4 242 return EFI_SUCCESS;\r
243}\r
244\r
245\r
3402aac7 246/**\r
938f46b4 247 Frees memory that was allocated with DmaAllocateBuffer().\r
3402aac7
RC
248\r
249 @param Pages The number of pages to free.\r
250 @param HostAddress The base system memory address of the allocated range.\r
251\r
938f46b4 252 @retval EFI_SUCCESS The requested memory pages were freed.\r
253 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages\r
254 was not allocated with DmaAllocateBuffer().\r
3402aac7 255\r
938f46b4 256**/\r
257EFI_STATUS\r
258EFIAPI\r
259DmaFreeBuffer (\r
260 IN UINTN Pages,\r
261 IN VOID *HostAddress\r
262 )\r
263{\r
264 if (HostAddress == NULL) {\r
265 return EFI_INVALID_PARAMETER;\r
3402aac7
RC
266 }\r
267\r
938f46b4 268 UncachedFreePages (HostAddress, Pages);\r
269 return EFI_SUCCESS;\r
270}\r
271\r
272\r
273EFI_STATUS\r
274EFIAPI\r
275ArmDmaLibConstructor (\r
276 IN EFI_HANDLE ImageHandle,\r
277 IN EFI_SYSTEM_TABLE *SystemTable\r
278 )\r
279{\r
280 EFI_STATUS Status;\r
281\r
282 // Get the Cpu protocol for later use\r
283 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&gCpu);\r
284 ASSERT_EFI_ERROR(Status);\r
285\r
07c70785 286 gCacheAlignment = ArmCacheWritebackGranule ();\r
3402aac7 287\r
938f46b4 288 return Status;\r
289}\r
290\r