]> git.proxmox.com Git - mirror_edk2.git/blame - ArmPkg/Library/ArmDmaLib/ArmDmaLib.c
ArmPkg: Fix ARMGCC build
[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
5 \r
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
938f46b4 42/** \r
43 Provides the DMA controller-specific addresses needed to access system memory.\r
44 \r
45 Operation is relative to the DMA bus master.\r
46 \r
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
50 that were mapped. \r
51 @param DeviceAddress The resulting map address for the bus master controller to use to\r
52 access the hosts HostAddress. \r
53 @param Mapping A resulting value to pass to Unmap().\r
54 \r
55 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.\r
56 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer. \r
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
60 \r
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
92 \r
93 *Mapping = Map;\r
94\r
3ea3ff88 95 if ((((UINTN)HostAddress & (gCacheAlignment - 1)) != 0) ||\r
96 ((*NumberOfBytes % gCacheAlignment) != 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
105 if (GcdDescriptor.Attributes != EFI_MEMORY_UC) {\r
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
132 Status = gDS->SetMemorySpaceAttributes (ALIGN_VALUE(*DeviceAddress - BASE_4KB - 1,BASE_4KB), ALIGN_VALUE(*NumberOfBytes,BASE_4KB), EFI_MEMORY_UC);\r
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
146/** \r
147 Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()\r
148 operation and releases any corresponding resources.\r
149 \r
150 @param Mapping The mapping value returned from DmaMap*().\r
151 \r
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
154 \r
155**/\r
156EFI_STATUS\r
157EFIAPI\r
158DmaUnmap (\r
159 IN VOID *Mapping\r
160 )\r
161{\r
162 MAP_INFO_INSTANCE *Map;\r
163 \r
164 if (Mapping == NULL) {\r
165 ASSERT (FALSE);\r
166 return EFI_INVALID_PARAMETER;\r
167 }\r
168 \r
169 Map = (MAP_INFO_INSTANCE *)Mapping;\r
170 \r
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
175 \r
176 DmaFreeBuffer (EFI_SIZE_TO_PAGES (Map->NumberOfBytes), (VOID *)(UINTN)Map->DeviceAddress);\r
177 \r
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
186 \r
187 FreePool (Map);\r
188\r
189 return EFI_SUCCESS;\r
190}\r
191\r
192/** \r
193 Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.\r
194 mapping. \r
195 \r
196 @param MemoryType The type of memory to allocate, EfiBootServicesData or\r
197 EfiRuntimeServicesData. \r
198 @param Pages The number of pages to allocate. \r
199 @param HostAddress A pointer to store the base system memory address of the\r
200 allocated range. \r
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
204 MEMORY_WRITE_COMBINE and MEMORY_CACHED. \r
205 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
206 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated. \r
207 \r
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
217 if (HostAddress == NULL) {\r
218 return EFI_INVALID_PARAMETER;\r
219 }\r
220\r
221 //\r
222 // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData\r
223 //\r
224 // We used uncached memory to keep coherency\r
225 //\r
226 if (MemoryType == EfiBootServicesData) {\r
227 *HostAddress = UncachedAllocatePages (Pages);\r
1bfda055 228 } else if (MemoryType == EfiRuntimeServicesData) {\r
938f46b4 229 *HostAddress = UncachedAllocateRuntimePages (Pages);\r
230 } else {\r
231 return EFI_INVALID_PARAMETER;\r
232 }\r
233\r
234 return EFI_SUCCESS;\r
235}\r
236\r
237\r
238/** \r
239 Frees memory that was allocated with DmaAllocateBuffer().\r
240 \r
241 @param Pages The number of pages to free. \r
242 @param HostAddress The base system memory address of the allocated range. \r
243 \r
244 @retval EFI_SUCCESS The requested memory pages were freed.\r
245 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages\r
246 was not allocated with DmaAllocateBuffer().\r
247 \r
248**/\r
249EFI_STATUS\r
250EFIAPI\r
251DmaFreeBuffer (\r
252 IN UINTN Pages,\r
253 IN VOID *HostAddress\r
254 )\r
255{\r
256 if (HostAddress == NULL) {\r
257 return EFI_INVALID_PARAMETER;\r
258 } \r
259 \r
260 UncachedFreePages (HostAddress, Pages);\r
261 return EFI_SUCCESS;\r
262}\r
263\r
264\r
265EFI_STATUS\r
266EFIAPI\r
267ArmDmaLibConstructor (\r
268 IN EFI_HANDLE ImageHandle,\r
269 IN EFI_SYSTEM_TABLE *SystemTable\r
270 )\r
271{\r
272 EFI_STATUS Status;\r
273\r
274 // Get the Cpu protocol for later use\r
275 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&gCpu);\r
276 ASSERT_EFI_ERROR(Status);\r
277\r
278 gCacheAlignment = ArmDataCacheLineLength ();\r
279 \r
280 return Status;\r
281}\r
282\r