]>
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 | |
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 | |
29 | typedef 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 | |
39 | EFI_CPU_ARCH_PROTOCOL *gCpu;\r | |
40 | UINTN 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 | |
62 | EFI_STATUS\r | |
63 | EFIAPI\r | |
64 | DmaMap (\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 | |
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 | |
3f5aa193 | 134 | Status = gDS->SetMemorySpaceAttributes (ALIGN_VALUE(*DeviceAddress - BASE_4KB - 1,BASE_4KB), 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 | |
148 | /** \r | |
149 | Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()\r | |
150 | operation and releases any corresponding resources.\r | |
151 | \r | |
152 | @param Mapping The mapping value returned from DmaMap*().\r | |
153 | \r | |
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 | |
156 | \r | |
157 | **/\r | |
158 | EFI_STATUS\r | |
159 | EFIAPI\r | |
160 | DmaUnmap (\r | |
161 | IN VOID *Mapping\r | |
162 | )\r | |
163 | {\r | |
164 | MAP_INFO_INSTANCE *Map;\r | |
165 | \r | |
166 | if (Mapping == NULL) {\r | |
167 | ASSERT (FALSE);\r | |
168 | return EFI_INVALID_PARAMETER;\r | |
169 | }\r | |
170 | \r | |
171 | Map = (MAP_INFO_INSTANCE *)Mapping;\r | |
172 | \r | |
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 | |
177 | \r | |
178 | DmaFreeBuffer (EFI_SIZE_TO_PAGES (Map->NumberOfBytes), (VOID *)(UINTN)Map->DeviceAddress);\r | |
179 | \r | |
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 | |
188 | \r | |
189 | FreePool (Map);\r | |
190 | \r | |
191 | return EFI_SUCCESS;\r | |
192 | }\r | |
193 | \r | |
194 | /** \r | |
195 | Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.\r | |
196 | mapping. \r | |
197 | \r | |
198 | @param MemoryType The type of memory to allocate, EfiBootServicesData or\r | |
199 | EfiRuntimeServicesData. \r | |
200 | @param Pages The number of pages to allocate. \r | |
201 | @param HostAddress A pointer to store the base system memory address of the\r | |
202 | allocated range. \r | |
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 | |
206 | MEMORY_WRITE_COMBINE and MEMORY_CACHED. \r | |
207 | @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r | |
208 | @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated. \r | |
209 | \r | |
210 | **/\r | |
211 | EFI_STATUS\r | |
212 | EFIAPI\r | |
213 | DmaAllocateBuffer (\r | |
214 | IN EFI_MEMORY_TYPE MemoryType,\r | |
215 | IN UINTN Pages,\r | |
216 | OUT VOID **HostAddress\r | |
217 | )\r | |
218 | {\r | |
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 | |
229 | *HostAddress = UncachedAllocatePages (Pages);\r | |
1bfda055 | 230 | } else if (MemoryType == EfiRuntimeServicesData) {\r |
938f46b4 | 231 | *HostAddress = UncachedAllocateRuntimePages (Pages);\r |
232 | } else {\r | |
233 | return EFI_INVALID_PARAMETER;\r | |
234 | }\r | |
235 | \r | |
236 | return EFI_SUCCESS;\r | |
237 | }\r | |
238 | \r | |
239 | \r | |
240 | /** \r | |
241 | Frees memory that was allocated with DmaAllocateBuffer().\r | |
242 | \r | |
243 | @param Pages The number of pages to free. \r | |
244 | @param HostAddress The base system memory address of the allocated range. \r | |
245 | \r | |
246 | @retval EFI_SUCCESS The requested memory pages were freed.\r | |
247 | @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages\r | |
248 | was not allocated with DmaAllocateBuffer().\r | |
249 | \r | |
250 | **/\r | |
251 | EFI_STATUS\r | |
252 | EFIAPI\r | |
253 | DmaFreeBuffer (\r | |
254 | IN UINTN Pages,\r | |
255 | IN VOID *HostAddress\r | |
256 | )\r | |
257 | {\r | |
258 | if (HostAddress == NULL) {\r | |
259 | return EFI_INVALID_PARAMETER;\r | |
260 | } \r | |
261 | \r | |
262 | UncachedFreePages (HostAddress, Pages);\r | |
263 | return EFI_SUCCESS;\r | |
264 | }\r | |
265 | \r | |
266 | \r | |
267 | EFI_STATUS\r | |
268 | EFIAPI\r | |
269 | ArmDmaLibConstructor (\r | |
270 | IN EFI_HANDLE ImageHandle,\r | |
271 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
272 | )\r | |
273 | {\r | |
274 | EFI_STATUS Status;\r | |
275 | \r | |
276 | // Get the Cpu protocol for later use\r | |
277 | Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&gCpu);\r | |
278 | ASSERT_EFI_ERROR(Status);\r | |
279 | \r | |
280 | gCacheAlignment = ArmDataCacheLineLength ();\r | |
281 | \r | |
282 | return Status;\r | |
283 | }\r | |
284 | \r |