]>
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 | |
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 | |
28 | typedef struct {\r | |
29 | EFI_PHYSICAL_ADDRESS HostAddress;\r | |
30 | EFI_PHYSICAL_ADDRESS DeviceAddress;\r | |
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 | 38 | STATIC 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 |
60 | EFI_STATUS\r | |
61 | EFIAPI\r | |
62 | DmaMap (\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 | |
151acec6 | 95 | Status = gDS->GetMemorySpaceDescriptor (*DeviceAddress, &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 | |
129 | *DeviceAddress = (PHYSICAL_ADDRESS)(UINTN)Buffer;\r | |
130 | } else {\r | |
131 | Map->DoubleBuffer = FALSE;\r | |
132 | }\r | |
938f46b4 | 133 | } else {\r |
134 | Map->DoubleBuffer = FALSE;\r | |
e6d572ba | 135 | \r |
b64e44cc AB |
136 | DEBUG_CODE_BEGIN ();\r |
137 | \r | |
138 | //\r | |
139 | // The operation type check above only executes if the buffer happens to be\r | |
140 | // misaligned with respect to CWG, but even if it is aligned, we should not\r | |
141 | // allow arbitrary buffers to be used for creating consistent mappings.\r | |
142 | // So duplicate the check here when running in DEBUG mode, just to assert\r | |
143 | // that we are not trying to create a consistent mapping for cached memory.\r | |
144 | //\r | |
145 | Status = gDS->GetMemorySpaceDescriptor (*DeviceAddress, &GcdDescriptor);\r | |
146 | ASSERT_EFI_ERROR(Status);\r | |
147 | \r | |
148 | ASSERT (Operation != MapOperationBusMasterCommonBuffer ||\r | |
149 | (GcdDescriptor.Attributes & (EFI_MEMORY_WB | EFI_MEMORY_WT)) == 0);\r | |
150 | \r | |
151 | DEBUG_CODE_END ();\r | |
152 | \r | |
e6d572ba | 153 | // Flush the Data Cache (should not have any effect if the memory region is uncached)\r |
de2ec785 AB |
154 | mCpu->FlushDataCache (mCpu, *DeviceAddress, *NumberOfBytes,\r |
155 | EfiCpuFlushTypeWriteBackInvalidate);\r | |
938f46b4 | 156 | }\r |
157 | \r | |
938f46b4 | 158 | Map->HostAddress = (UINTN)HostAddress;\r |
159 | Map->DeviceAddress = *DeviceAddress;\r | |
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 |
167 | FreeMapInfo:\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 |
186 | EFI_STATUS\r | |
187 | EFIAPI\r | |
188 | DmaUnmap (\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 | |
938f46b4 | 209 | CopyMem ((VOID *)(UINTN)Map->HostAddress, (VOID *)(UINTN)Map->DeviceAddress, Map->NumberOfBytes);\r |
210 | }\r | |
3402aac7 | 211 | \r |
938f46b4 | 212 | DmaFreeBuffer (EFI_SIZE_TO_PAGES (Map->NumberOfBytes), (VOID *)(UINTN)Map->DeviceAddress);\r |
3402aac7 | 213 | \r |
938f46b4 | 214 | } else {\r |
215 | if (Map->Operation == MapOperationBusMasterWrite) {\r | |
216 | //\r | |
217 | // Make sure we read buffer from uncached memory and not the cache\r | |
218 | //\r | |
de2ec785 AB |
219 | mCpu->FlushDataCache (mCpu, Map->HostAddress, Map->NumberOfBytes,\r |
220 | EfiCpuFlushTypeInvalidate);\r | |
938f46b4 | 221 | }\r |
222 | }\r | |
3402aac7 | 223 | \r |
938f46b4 | 224 | FreePool (Map);\r |
225 | \r | |
a24f7d66 | 226 | return Status;\r |
938f46b4 | 227 | }\r |
228 | \r | |
3402aac7 | 229 | /**\r |
938f46b4 | 230 | Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.\r |
3402aac7 RC |
231 | mapping.\r |
232 | \r | |
938f46b4 | 233 | @param MemoryType The type of memory to allocate, EfiBootServicesData or\r |
3402aac7 RC |
234 | EfiRuntimeServicesData.\r |
235 | @param Pages The number of pages to allocate.\r | |
938f46b4 | 236 | @param HostAddress A pointer to store the base system memory address of the\r |
3402aac7 | 237 | allocated range.\r |
938f46b4 | 238 | \r |
239 | @retval EFI_SUCCESS The requested memory pages were allocated.\r | |
240 | @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are\r | |
3402aac7 | 241 | MEMORY_WRITE_COMBINE and MEMORY_CACHED.\r |
938f46b4 | 242 | @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r |
3402aac7 RC |
243 | @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r |
244 | \r | |
938f46b4 | 245 | **/\r |
246 | EFI_STATUS\r | |
247 | EFIAPI\r | |
248 | DmaAllocateBuffer (\r | |
249 | IN EFI_MEMORY_TYPE MemoryType,\r | |
250 | IN UINTN Pages,\r | |
251 | OUT VOID **HostAddress\r | |
252 | )\r | |
253 | {\r | |
e55f8c73 AB |
254 | VOID *Allocation;\r |
255 | \r | |
938f46b4 | 256 | if (HostAddress == NULL) {\r |
257 | return EFI_INVALID_PARAMETER;\r | |
258 | }\r | |
259 | \r | |
260 | //\r | |
261 | // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData\r | |
262 | //\r | |
263 | // We used uncached memory to keep coherency\r | |
264 | //\r | |
265 | if (MemoryType == EfiBootServicesData) {\r | |
e55f8c73 | 266 | Allocation = UncachedAllocatePages (Pages);\r |
1bfda055 | 267 | } else if (MemoryType == EfiRuntimeServicesData) {\r |
e55f8c73 | 268 | Allocation = UncachedAllocateRuntimePages (Pages);\r |
938f46b4 | 269 | } else {\r |
270 | return EFI_INVALID_PARAMETER;\r | |
271 | }\r | |
272 | \r | |
e55f8c73 AB |
273 | if (Allocation == NULL) {\r |
274 | return EFI_OUT_OF_RESOURCES;\r | |
275 | }\r | |
276 | \r | |
277 | *HostAddress = Allocation;\r | |
278 | \r | |
938f46b4 | 279 | return EFI_SUCCESS;\r |
280 | }\r | |
281 | \r | |
282 | \r | |
3402aac7 | 283 | /**\r |
938f46b4 | 284 | Frees memory that was allocated with DmaAllocateBuffer().\r |
3402aac7 RC |
285 | \r |
286 | @param Pages The number of pages to free.\r | |
287 | @param HostAddress The base system memory address of the allocated range.\r | |
288 | \r | |
938f46b4 | 289 | @retval EFI_SUCCESS The requested memory pages were freed.\r |
290 | @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages\r | |
291 | was not allocated with DmaAllocateBuffer().\r | |
3402aac7 | 292 | \r |
938f46b4 | 293 | **/\r |
294 | EFI_STATUS\r | |
295 | EFIAPI\r | |
296 | DmaFreeBuffer (\r | |
297 | IN UINTN Pages,\r | |
298 | IN VOID *HostAddress\r | |
299 | )\r | |
300 | {\r | |
301 | if (HostAddress == NULL) {\r | |
302 | return EFI_INVALID_PARAMETER;\r | |
3402aac7 RC |
303 | }\r |
304 | \r | |
938f46b4 | 305 | UncachedFreePages (HostAddress, Pages);\r |
306 | return EFI_SUCCESS;\r | |
307 | }\r | |
308 | \r | |
309 | \r | |
310 | EFI_STATUS\r | |
311 | EFIAPI\r | |
312 | ArmDmaLibConstructor (\r | |
313 | IN EFI_HANDLE ImageHandle,\r | |
314 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
315 | )\r | |
316 | {\r | |
317 | EFI_STATUS Status;\r | |
318 | \r | |
319 | // Get the Cpu protocol for later use\r | |
de2ec785 | 320 | Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&mCpu);\r |
938f46b4 | 321 | ASSERT_EFI_ERROR(Status);\r |
322 | \r | |
938f46b4 | 323 | return Status;\r |
324 | }\r | |
325 | \r |