]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/ArmDmaLib/ArmDmaLib.c
ArmPkg/ArmDmaLib: interpret GCD attributes as a bit field
[mirror_edk2.git] / ArmPkg / Library / ArmDmaLib / ArmDmaLib.c
1 /** @file
2 Generic ARM implementation of DmaLib.h
3
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
5
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include <PiDxe.h>
17 #include <Library/DebugLib.h>
18 #include <Library/DmaLib.h>
19 #include <Library/DxeServicesTableLib.h>
20 #include <Library/MemoryAllocationLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
22 #include <Library/UncachedMemoryAllocationLib.h>
23 #include <Library/IoLib.h>
24 #include <Library/BaseMemoryLib.h>
25 #include <Library/ArmLib.h>
26
27 #include <Protocol/Cpu.h>
28
29 typedef struct {
30 EFI_PHYSICAL_ADDRESS HostAddress;
31 EFI_PHYSICAL_ADDRESS DeviceAddress;
32 UINTN NumberOfBytes;
33 DMA_MAP_OPERATION Operation;
34 BOOLEAN DoubleBuffer;
35 } MAP_INFO_INSTANCE;
36
37
38
39 EFI_CPU_ARCH_PROTOCOL *gCpu;
40 UINTN gCacheAlignment = 0;
41
42 /**
43 Provides the DMA controller-specific addresses needed to access system memory.
44
45 Operation is relative to the DMA bus master.
46
47 @param Operation Indicates if the bus master is going to read or write to system memory.
48 @param HostAddress The system memory address to map to the DMA controller.
49 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
50 that were mapped.
51 @param DeviceAddress The resulting map address for the bus master controller to use to
52 access the hosts HostAddress.
53 @param Mapping A resulting value to pass to Unmap().
54
55 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
56 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
57 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
58 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
59 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
60
61 **/
62 EFI_STATUS
63 EFIAPI
64 DmaMap (
65 IN DMA_MAP_OPERATION Operation,
66 IN VOID *HostAddress,
67 IN OUT UINTN *NumberOfBytes,
68 OUT PHYSICAL_ADDRESS *DeviceAddress,
69 OUT VOID **Mapping
70 )
71 {
72 EFI_STATUS Status;
73 MAP_INFO_INSTANCE *Map;
74 VOID *Buffer;
75 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;
76
77 if (HostAddress == NULL || NumberOfBytes == NULL || DeviceAddress == NULL || Mapping == NULL ) {
78 return EFI_INVALID_PARAMETER;
79 }
80
81 if (Operation >= MapOperationMaximum) {
82 return EFI_INVALID_PARAMETER;
83 }
84
85 *DeviceAddress = ConvertToPhysicalAddress (HostAddress);
86
87 // Remember range so we can flush on the other side
88 Map = AllocatePool (sizeof (MAP_INFO_INSTANCE));
89 if (Map == NULL) {
90 return EFI_OUT_OF_RESOURCES;
91 }
92
93 *Mapping = Map;
94
95 if ((((UINTN)HostAddress & (gCacheAlignment - 1)) != 0) ||
96 ((*NumberOfBytes & (gCacheAlignment - 1)) != 0)) {
97
98 // Get the cacheability of the region
99 Status = gDS->GetMemorySpaceDescriptor (*DeviceAddress, &GcdDescriptor);
100 if (EFI_ERROR(Status)) {
101 return Status;
102 }
103
104 // If the mapped buffer is not an uncached buffer
105 if ((GcdDescriptor.Attributes & (EFI_MEMORY_WB | EFI_MEMORY_WT)) != 0) {
106 //
107 // If the buffer does not fill entire cache lines we must double buffer into
108 // uncached memory. Device (PCI) address becomes uncached page.
109 //
110 Map->DoubleBuffer = TRUE;
111 Status = DmaAllocateBuffer (EfiBootServicesData, EFI_SIZE_TO_PAGES (*NumberOfBytes), &Buffer);
112 if (EFI_ERROR (Status)) {
113 return Status;
114 }
115
116 if ((Operation == MapOperationBusMasterRead) || (Operation == MapOperationBusMasterCommonBuffer)) {
117 CopyMem (Buffer, HostAddress, *NumberOfBytes);
118 }
119
120 *DeviceAddress = (PHYSICAL_ADDRESS)(UINTN)Buffer;
121 } else {
122 Map->DoubleBuffer = FALSE;
123 }
124 } else {
125 Map->DoubleBuffer = FALSE;
126
127 // Flush the Data Cache (should not have any effect if the memory region is uncached)
128 gCpu->FlushDataCache (gCpu, *DeviceAddress, *NumberOfBytes, EfiCpuFlushTypeWriteBackInvalidate);
129
130 if ((Operation == MapOperationBusMasterRead) || (Operation == MapOperationBusMasterCommonBuffer)) {
131 // In case the buffer is used for instance to send command to a PCI controller, we must ensure the memory is uncached
132 Status = gDS->SetMemorySpaceAttributes (*DeviceAddress & ~(BASE_4KB - 1), ALIGN_VALUE (*NumberOfBytes, BASE_4KB), EFI_MEMORY_WC);
133 ASSERT_EFI_ERROR (Status);
134 }
135 }
136
137 Map->HostAddress = (UINTN)HostAddress;
138 Map->DeviceAddress = *DeviceAddress;
139 Map->NumberOfBytes = *NumberOfBytes;
140 Map->Operation = Operation;
141
142 return EFI_SUCCESS;
143 }
144
145
146 /**
147 Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()
148 operation and releases any corresponding resources.
149
150 @param Mapping The mapping value returned from DmaMap*().
151
152 @retval EFI_SUCCESS The range was unmapped.
153 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
154
155 **/
156 EFI_STATUS
157 EFIAPI
158 DmaUnmap (
159 IN VOID *Mapping
160 )
161 {
162 MAP_INFO_INSTANCE *Map;
163
164 if (Mapping == NULL) {
165 ASSERT (FALSE);
166 return EFI_INVALID_PARAMETER;
167 }
168
169 Map = (MAP_INFO_INSTANCE *)Mapping;
170
171 if (Map->DoubleBuffer) {
172 if ((Map->Operation == MapOperationBusMasterWrite) || (Map->Operation == MapOperationBusMasterCommonBuffer)) {
173 CopyMem ((VOID *)(UINTN)Map->HostAddress, (VOID *)(UINTN)Map->DeviceAddress, Map->NumberOfBytes);
174 }
175
176 DmaFreeBuffer (EFI_SIZE_TO_PAGES (Map->NumberOfBytes), (VOID *)(UINTN)Map->DeviceAddress);
177
178 } else {
179 if (Map->Operation == MapOperationBusMasterWrite) {
180 //
181 // Make sure we read buffer from uncached memory and not the cache
182 //
183 gCpu->FlushDataCache (gCpu, Map->HostAddress, Map->NumberOfBytes, EfiCpuFlushTypeInvalidate);
184 }
185 }
186
187 FreePool (Map);
188
189 return EFI_SUCCESS;
190 }
191
192 /**
193 Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.
194 mapping.
195
196 @param MemoryType The type of memory to allocate, EfiBootServicesData or
197 EfiRuntimeServicesData.
198 @param Pages The number of pages to allocate.
199 @param HostAddress A pointer to store the base system memory address of the
200 allocated range.
201
202 @retval EFI_SUCCESS The requested memory pages were allocated.
203 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
204 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
205 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
206 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
207
208 **/
209 EFI_STATUS
210 EFIAPI
211 DmaAllocateBuffer (
212 IN EFI_MEMORY_TYPE MemoryType,
213 IN UINTN Pages,
214 OUT VOID **HostAddress
215 )
216 {
217 VOID *Allocation;
218
219 if (HostAddress == NULL) {
220 return EFI_INVALID_PARAMETER;
221 }
222
223 //
224 // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData
225 //
226 // We used uncached memory to keep coherency
227 //
228 if (MemoryType == EfiBootServicesData) {
229 Allocation = UncachedAllocatePages (Pages);
230 } else if (MemoryType == EfiRuntimeServicesData) {
231 Allocation = UncachedAllocateRuntimePages (Pages);
232 } else {
233 return EFI_INVALID_PARAMETER;
234 }
235
236 if (Allocation == NULL) {
237 return EFI_OUT_OF_RESOURCES;
238 }
239
240 *HostAddress = Allocation;
241
242 return EFI_SUCCESS;
243 }
244
245
246 /**
247 Frees memory that was allocated with DmaAllocateBuffer().
248
249 @param Pages The number of pages to free.
250 @param HostAddress The base system memory address of the allocated range.
251
252 @retval EFI_SUCCESS The requested memory pages were freed.
253 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
254 was not allocated with DmaAllocateBuffer().
255
256 **/
257 EFI_STATUS
258 EFIAPI
259 DmaFreeBuffer (
260 IN UINTN Pages,
261 IN VOID *HostAddress
262 )
263 {
264 if (HostAddress == NULL) {
265 return EFI_INVALID_PARAMETER;
266 }
267
268 UncachedFreePages (HostAddress, Pages);
269 return EFI_SUCCESS;
270 }
271
272
273 EFI_STATUS
274 EFIAPI
275 ArmDmaLibConstructor (
276 IN EFI_HANDLE ImageHandle,
277 IN EFI_SYSTEM_TABLE *SystemTable
278 )
279 {
280 EFI_STATUS Status;
281
282 // Get the Cpu protocol for later use
283 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&gCpu);
284 ASSERT_EFI_ERROR(Status);
285
286 gCacheAlignment = ArmCacheWritebackGranule ();
287
288 return Status;
289 }
290