]>
Commit | Line | Data |
---|---|---|
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 | |
3a424c5f | 5 | Copyright (c) 2015 - 2017, Linaro, Ltd. All rights reserved.<BR>\r |
3402aac7 | 6 | \r |
938f46b4 | 7 | This program and the accompanying materials\r |
8 | are licensed and made available under the terms and conditions of the BSD License\r | |
9 | which accompanies this distribution. The full text of the license may be found at\r | |
10 | http://opensource.org/licenses/bsd-license.php\r | |
11 | \r | |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
13 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
14 | \r | |
15 | **/\r | |
16 | \r | |
e6d572ba | 17 | #include <PiDxe.h>\r |
938f46b4 | 18 | #include <Library/DebugLib.h>\r |
19 | #include <Library/DmaLib.h>\r | |
e6d572ba | 20 | #include <Library/DxeServicesTableLib.h>\r |
938f46b4 | 21 | #include <Library/MemoryAllocationLib.h>\r |
22 | #include <Library/UefiBootServicesTableLib.h>\r | |
23 | #include <Library/UncachedMemoryAllocationLib.h>\r | |
24 | #include <Library/IoLib.h>\r | |
25 | #include <Library/BaseMemoryLib.h>\r | |
938f46b4 | 26 | \r |
27 | #include <Protocol/Cpu.h>\r | |
28 | \r | |
29 | typedef struct {\r | |
30 | EFI_PHYSICAL_ADDRESS HostAddress;\r | |
df8c2668 | 31 | VOID *BufferAddress;\r |
938f46b4 | 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 | |
de2ec785 | 39 | STATIC EFI_CPU_ARCH_PROTOCOL *mCpu;\r |
938f46b4 | 40 | \r |
bfe34275 AB |
41 | STATIC\r |
42 | PHYSICAL_ADDRESS\r | |
43 | HostToDeviceAddress (\r | |
44 | IN PHYSICAL_ADDRESS HostAddress\r | |
45 | )\r | |
46 | {\r | |
47 | return HostAddress + PcdGet64 (PcdArmDmaDeviceOffset);\r | |
48 | }\r | |
49 | \r | |
3402aac7 | 50 | /**\r |
938f46b4 | 51 | Provides the DMA controller-specific addresses needed to access system memory.\r |
3402aac7 | 52 | \r |
938f46b4 | 53 | Operation is relative to the DMA bus master.\r |
3402aac7 | 54 | \r |
938f46b4 | 55 | @param Operation Indicates if the bus master is going to read or write to system memory.\r |
56 | @param HostAddress The system memory address to map to the DMA controller.\r | |
57 | @param NumberOfBytes On input the number of bytes to map. On output the number of bytes\r | |
3402aac7 | 58 | that were mapped.\r |
938f46b4 | 59 | @param DeviceAddress The resulting map address for the bus master controller to use to\r |
3402aac7 | 60 | access the hosts HostAddress.\r |
938f46b4 | 61 | @param Mapping A resulting value to pass to Unmap().\r |
3402aac7 | 62 | \r |
938f46b4 | 63 | @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.\r |
3402aac7 | 64 | @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.\r |
938f46b4 | 65 | @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r |
66 | @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r | |
67 | @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.\r | |
3402aac7 | 68 | \r |
938f46b4 | 69 | **/\r |
70 | EFI_STATUS\r | |
71 | EFIAPI\r | |
72 | DmaMap (\r | |
73 | IN DMA_MAP_OPERATION Operation,\r | |
74 | IN VOID *HostAddress,\r | |
75 | IN OUT UINTN *NumberOfBytes,\r | |
76 | OUT PHYSICAL_ADDRESS *DeviceAddress,\r | |
77 | OUT VOID **Mapping\r | |
78 | )\r | |
79 | {\r | |
e6d572ba | 80 | EFI_STATUS Status;\r |
81 | MAP_INFO_INSTANCE *Map;\r | |
82 | VOID *Buffer;\r | |
83 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r | |
3a424c5f | 84 | UINTN AllocSize;\r |
938f46b4 | 85 | \r |
e6d572ba | 86 | if (HostAddress == NULL || NumberOfBytes == NULL || DeviceAddress == NULL || Mapping == NULL ) {\r |
938f46b4 | 87 | return EFI_INVALID_PARAMETER;\r |
88 | }\r | |
938f46b4 | 89 | \r |
90 | if (Operation >= MapOperationMaximum) {\r | |
91 | return EFI_INVALID_PARAMETER;\r | |
92 | }\r | |
93 | \r | |
bfe34275 AB |
94 | //\r |
95 | // The debug implementation of UncachedMemoryAllocationLib in ArmPkg returns\r | |
96 | // a virtual uncached alias, and unmaps the cached ID mapping of the buffer,\r | |
97 | // in order to catch inadvertent references to the cached mapping.\r | |
98 | // Since HostToDeviceAddress () expects ID mapped input addresses, convert\r | |
99 | // the host address to an ID mapped address first.\r | |
100 | //\r | |
101 | *DeviceAddress = HostToDeviceAddress (ConvertToPhysicalAddress (HostAddress));\r | |
938f46b4 | 102 | \r |
103 | // Remember range so we can flush on the other side\r | |
104 | Map = AllocatePool (sizeof (MAP_INFO_INSTANCE));\r | |
105 | if (Map == NULL) {\r | |
106 | return EFI_OUT_OF_RESOURCES;\r | |
107 | }\r | |
3402aac7 | 108 | \r |
3a424c5f AB |
109 | if (Operation != MapOperationBusMasterRead &&\r |
110 | ((((UINTN)HostAddress & (mCpu->DmaBufferAlignment - 1)) != 0) ||\r | |
111 | ((*NumberOfBytes & (mCpu->DmaBufferAlignment - 1)) != 0))) {\r | |
e6d572ba | 112 | \r |
113 | // Get the cacheability of the region\r | |
df8c2668 | 114 | Status = gDS->GetMemorySpaceDescriptor ((UINTN)HostAddress, &GcdDescriptor);\r |
e6d572ba | 115 | if (EFI_ERROR(Status)) {\r |
84083b12 | 116 | goto FreeMapInfo;\r |
938f46b4 | 117 | }\r |
e6d572ba | 118 | \r |
119 | // If the mapped buffer is not an uncached buffer\r | |
885d57ef | 120 | if ((GcdDescriptor.Attributes & (EFI_MEMORY_WB | EFI_MEMORY_WT)) != 0) {\r |
a24f7d66 AB |
121 | //\r |
122 | // Operations of type MapOperationBusMasterCommonBuffer are only allowed\r | |
123 | // on uncached buffers.\r | |
124 | //\r | |
125 | if (Operation == MapOperationBusMasterCommonBuffer) {\r | |
126 | DEBUG ((EFI_D_ERROR,\r | |
127 | "%a: Operation type 'MapOperationBusMasterCommonBuffer' is only supported\n"\r | |
128 | "on memory regions that were allocated using DmaAllocateBuffer ()\n",\r | |
129 | __FUNCTION__));\r | |
84083b12 DE |
130 | Status = EFI_UNSUPPORTED;\r |
131 | goto FreeMapInfo;\r | |
a24f7d66 AB |
132 | }\r |
133 | \r | |
e6d572ba | 134 | //\r |
3a424c5f AB |
135 | // If the buffer does not fill entire cache lines we must double buffer\r |
136 | // into a suitably aligned allocation that allows us to invalidate the\r | |
137 | // cache without running the risk of corrupting adjacent unrelated data.\r | |
138 | // Note that pool allocations are guaranteed to be 8 byte aligned, so\r | |
139 | // we only have to add (alignment - 8) worth of padding.\r | |
e6d572ba | 140 | //\r |
3a424c5f AB |
141 | Map->DoubleBuffer = TRUE;\r |
142 | AllocSize = ALIGN_VALUE (*NumberOfBytes, mCpu->DmaBufferAlignment) +\r | |
143 | (mCpu->DmaBufferAlignment - 8);\r | |
144 | Map->BufferAddress = AllocatePool (AllocSize);\r | |
145 | if (Map->BufferAddress == NULL) {\r | |
146 | Status = EFI_OUT_OF_RESOURCES;\r | |
84083b12 | 147 | goto FreeMapInfo;\r |
e6d572ba | 148 | }\r |
149 | \r | |
3a424c5f | 150 | Buffer = ALIGN_POINTER (Map->BufferAddress, mCpu->DmaBufferAlignment);\r |
018c3c0b | 151 | *DeviceAddress = HostToDeviceAddress (ConvertToPhysicalAddress (Buffer));\r |
3a424c5f AB |
152 | \r |
153 | //\r | |
154 | // Get rid of any dirty cachelines covering the double buffer. This\r | |
155 | // prevents them from being written back unexpectedly, potentially\r | |
156 | // overwriting the data we receive from the device.\r | |
157 | //\r | |
158 | mCpu->FlushDataCache (mCpu, (UINTN)Buffer, *NumberOfBytes,\r | |
159 | EfiCpuFlushTypeWriteBack);\r | |
e6d572ba | 160 | } else {\r |
161 | Map->DoubleBuffer = FALSE;\r | |
162 | }\r | |
938f46b4 | 163 | } else {\r |
164 | Map->DoubleBuffer = FALSE;\r | |
e6d572ba | 165 | \r |
b64e44cc AB |
166 | DEBUG_CODE_BEGIN ();\r |
167 | \r | |
168 | //\r | |
169 | // The operation type check above only executes if the buffer happens to be\r | |
170 | // misaligned with respect to CWG, but even if it is aligned, we should not\r | |
171 | // allow arbitrary buffers to be used for creating consistent mappings.\r | |
172 | // So duplicate the check here when running in DEBUG mode, just to assert\r | |
173 | // that we are not trying to create a consistent mapping for cached memory.\r | |
174 | //\r | |
df8c2668 | 175 | Status = gDS->GetMemorySpaceDescriptor ((UINTN)HostAddress, &GcdDescriptor);\r |
b64e44cc AB |
176 | ASSERT_EFI_ERROR(Status);\r |
177 | \r | |
178 | ASSERT (Operation != MapOperationBusMasterCommonBuffer ||\r | |
179 | (GcdDescriptor.Attributes & (EFI_MEMORY_WB | EFI_MEMORY_WT)) == 0);\r | |
180 | \r | |
181 | DEBUG_CODE_END ();\r | |
182 | \r | |
e6d572ba | 183 | // Flush the Data Cache (should not have any effect if the memory region is uncached)\r |
df8c2668 | 184 | mCpu->FlushDataCache (mCpu, (UINTN)HostAddress, *NumberOfBytes,\r |
de2ec785 | 185 | EfiCpuFlushTypeWriteBackInvalidate);\r |
938f46b4 | 186 | }\r |
187 | \r | |
938f46b4 | 188 | Map->HostAddress = (UINTN)HostAddress;\r |
938f46b4 | 189 | Map->NumberOfBytes = *NumberOfBytes;\r |
190 | Map->Operation = Operation;\r | |
191 | \r | |
84083b12 DE |
192 | *Mapping = Map;\r |
193 | \r | |
938f46b4 | 194 | return EFI_SUCCESS;\r |
84083b12 DE |
195 | \r |
196 | FreeMapInfo:\r | |
197 | FreePool (Map);\r | |
198 | \r | |
199 | return Status;\r | |
938f46b4 | 200 | }\r |
201 | \r | |
202 | \r | |
3402aac7 | 203 | /**\r |
938f46b4 | 204 | Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()\r |
205 | operation and releases any corresponding resources.\r | |
3402aac7 | 206 | \r |
938f46b4 | 207 | @param Mapping The mapping value returned from DmaMap*().\r |
3402aac7 | 208 | \r |
938f46b4 | 209 | @retval EFI_SUCCESS The range was unmapped.\r |
210 | @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.\r | |
a24f7d66 AB |
211 | @retval EFI_INVALID_PARAMETER An inconsistency was detected between the mapping type\r |
212 | and the DoubleBuffer field\r | |
3402aac7 | 213 | \r |
938f46b4 | 214 | **/\r |
215 | EFI_STATUS\r | |
216 | EFIAPI\r | |
217 | DmaUnmap (\r | |
218 | IN VOID *Mapping\r | |
219 | )\r | |
220 | {\r | |
221 | MAP_INFO_INSTANCE *Map;\r | |
a24f7d66 | 222 | EFI_STATUS Status;\r |
3a424c5f | 223 | VOID *Buffer;\r |
3402aac7 | 224 | \r |
938f46b4 | 225 | if (Mapping == NULL) {\r |
226 | ASSERT (FALSE);\r | |
227 | return EFI_INVALID_PARAMETER;\r | |
228 | }\r | |
3402aac7 | 229 | \r |
938f46b4 | 230 | Map = (MAP_INFO_INSTANCE *)Mapping;\r |
3402aac7 | 231 | \r |
a24f7d66 | 232 | Status = EFI_SUCCESS;\r |
938f46b4 | 233 | if (Map->DoubleBuffer) {\r |
3a424c5f | 234 | ASSERT (Map->Operation == MapOperationBusMasterWrite);\r |
a24f7d66 | 235 | \r |
3a424c5f | 236 | if (Map->Operation != MapOperationBusMasterWrite) {\r |
a24f7d66 | 237 | Status = EFI_INVALID_PARAMETER;\r |
3a424c5f AB |
238 | } else {\r |
239 | Buffer = ALIGN_POINTER (Map->BufferAddress, mCpu->DmaBufferAlignment);\r | |
3402aac7 | 240 | \r |
3a424c5f AB |
241 | mCpu->FlushDataCache (mCpu, (UINTN)Buffer, Map->NumberOfBytes,\r |
242 | EfiCpuFlushTypeInvalidate);\r | |
3402aac7 | 243 | \r |
3a424c5f AB |
244 | CopyMem ((VOID *)(UINTN)Map->HostAddress, Buffer, Map->NumberOfBytes);\r |
245 | \r | |
246 | FreePool (Map->BufferAddress);\r | |
247 | }\r | |
938f46b4 | 248 | } else {\r |
249 | if (Map->Operation == MapOperationBusMasterWrite) {\r | |
250 | //\r | |
251 | // Make sure we read buffer from uncached memory and not the cache\r | |
252 | //\r | |
de2ec785 AB |
253 | mCpu->FlushDataCache (mCpu, Map->HostAddress, Map->NumberOfBytes,\r |
254 | EfiCpuFlushTypeInvalidate);\r | |
938f46b4 | 255 | }\r |
256 | }\r | |
3402aac7 | 257 | \r |
938f46b4 | 258 | FreePool (Map);\r |
259 | \r | |
a24f7d66 | 260 | return Status;\r |
938f46b4 | 261 | }\r |
262 | \r | |
3402aac7 | 263 | /**\r |
938f46b4 | 264 | Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.\r |
3402aac7 RC |
265 | mapping.\r |
266 | \r | |
938f46b4 | 267 | @param MemoryType The type of memory to allocate, EfiBootServicesData or\r |
3402aac7 RC |
268 | EfiRuntimeServicesData.\r |
269 | @param Pages The number of pages to allocate.\r | |
938f46b4 | 270 | @param HostAddress A pointer to store the base system memory address of the\r |
3402aac7 | 271 | allocated range.\r |
938f46b4 | 272 | \r |
273 | @retval EFI_SUCCESS The requested memory pages were allocated.\r | |
274 | @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are\r | |
3402aac7 | 275 | MEMORY_WRITE_COMBINE and MEMORY_CACHED.\r |
938f46b4 | 276 | @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r |
3402aac7 RC |
277 | @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r |
278 | \r | |
938f46b4 | 279 | **/\r |
280 | EFI_STATUS\r | |
281 | EFIAPI\r | |
282 | DmaAllocateBuffer (\r | |
283 | IN EFI_MEMORY_TYPE MemoryType,\r | |
284 | IN UINTN Pages,\r | |
285 | OUT VOID **HostAddress\r | |
286 | )\r | |
287 | {\r | |
e55f8c73 AB |
288 | VOID *Allocation;\r |
289 | \r | |
938f46b4 | 290 | if (HostAddress == NULL) {\r |
291 | return EFI_INVALID_PARAMETER;\r | |
292 | }\r | |
293 | \r | |
294 | //\r | |
295 | // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData\r | |
296 | //\r | |
297 | // We used uncached memory to keep coherency\r | |
298 | //\r | |
299 | if (MemoryType == EfiBootServicesData) {\r | |
e55f8c73 | 300 | Allocation = UncachedAllocatePages (Pages);\r |
1bfda055 | 301 | } else if (MemoryType == EfiRuntimeServicesData) {\r |
e55f8c73 | 302 | Allocation = UncachedAllocateRuntimePages (Pages);\r |
938f46b4 | 303 | } else {\r |
304 | return EFI_INVALID_PARAMETER;\r | |
305 | }\r | |
306 | \r | |
e55f8c73 AB |
307 | if (Allocation == NULL) {\r |
308 | return EFI_OUT_OF_RESOURCES;\r | |
309 | }\r | |
310 | \r | |
311 | *HostAddress = Allocation;\r | |
312 | \r | |
938f46b4 | 313 | return EFI_SUCCESS;\r |
314 | }\r | |
315 | \r | |
316 | \r | |
3402aac7 | 317 | /**\r |
938f46b4 | 318 | Frees memory that was allocated with DmaAllocateBuffer().\r |
3402aac7 RC |
319 | \r |
320 | @param Pages The number of pages to free.\r | |
321 | @param HostAddress The base system memory address of the allocated range.\r | |
322 | \r | |
938f46b4 | 323 | @retval EFI_SUCCESS The requested memory pages were freed.\r |
324 | @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages\r | |
325 | was not allocated with DmaAllocateBuffer().\r | |
3402aac7 | 326 | \r |
938f46b4 | 327 | **/\r |
328 | EFI_STATUS\r | |
329 | EFIAPI\r | |
330 | DmaFreeBuffer (\r | |
331 | IN UINTN Pages,\r | |
332 | IN VOID *HostAddress\r | |
333 | )\r | |
334 | {\r | |
335 | if (HostAddress == NULL) {\r | |
336 | return EFI_INVALID_PARAMETER;\r | |
3402aac7 RC |
337 | }\r |
338 | \r | |
938f46b4 | 339 | UncachedFreePages (HostAddress, Pages);\r |
340 | return EFI_SUCCESS;\r | |
341 | }\r | |
342 | \r | |
343 | \r | |
344 | EFI_STATUS\r | |
345 | EFIAPI\r | |
346 | ArmDmaLibConstructor (\r | |
347 | IN EFI_HANDLE ImageHandle,\r | |
348 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
349 | )\r | |
350 | {\r | |
351 | EFI_STATUS Status;\r | |
352 | \r | |
353 | // Get the Cpu protocol for later use\r | |
de2ec785 | 354 | Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&mCpu);\r |
938f46b4 | 355 | ASSERT_EFI_ERROR(Status);\r |
356 | \r | |
938f46b4 | 357 | return Status;\r |
358 | }\r | |
359 | \r |