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