]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPkg/Library/ArmDmaLib/ArmDmaLib.c
7fef208c78c1867122275960bb912c4c7d011503
[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 <Base.h>
17 #include <Library/DebugLib.h>
18 #include <Library/DmaLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
21 #include <Library/UncachedMemoryAllocationLib.h>
22 #include <Library/IoLib.h>
23 #include <Library/BaseMemoryLib.h>
24 #include <Library/ArmLib.h>
25
26 #include <Protocol/Cpu.h>
27
28 typedef struct {
29 EFI_PHYSICAL_ADDRESS HostAddress;
30 EFI_PHYSICAL_ADDRESS DeviceAddress;
31 UINTN NumberOfBytes;
32 DMA_MAP_OPERATION Operation;
33 BOOLEAN DoubleBuffer;
34 } MAP_INFO_INSTANCE;
35
36
37
38 EFI_CPU_ARCH_PROTOCOL *gCpu;
39 UINTN gCacheAlignment = 0;
40
41
42
43
44 /**
45 Provides the DMA controller-specific addresses needed to access system memory.
46
47 Operation is relative to the DMA bus master.
48
49 @param Operation Indicates if the bus master is going to read or write to system memory.
50 @param HostAddress The system memory address to map to the DMA controller.
51 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
52 that were mapped.
53 @param DeviceAddress The resulting map address for the bus master controller to use to
54 access the hosts HostAddress.
55 @param Mapping A resulting value to pass to Unmap().
56
57 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
58 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
59 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
60 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
61 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
62
63 **/
64 EFI_STATUS
65 EFIAPI
66 DmaMap (
67 IN DMA_MAP_OPERATION Operation,
68 IN VOID *HostAddress,
69 IN OUT UINTN *NumberOfBytes,
70 OUT PHYSICAL_ADDRESS *DeviceAddress,
71 OUT VOID **Mapping
72 )
73 {
74 EFI_STATUS Status;
75 MAP_INFO_INSTANCE *Map;
76 VOID *Buffer;
77
78 if ( HostAddress == NULL || NumberOfBytes == NULL ||
79 DeviceAddress == NULL || Mapping == NULL ) {
80 return EFI_INVALID_PARAMETER;
81 }
82
83
84 if (Operation >= MapOperationMaximum) {
85 return EFI_INVALID_PARAMETER;
86 }
87
88 *DeviceAddress = ConvertToPhysicalAddress (HostAddress);
89
90 // Remember range so we can flush on the other side
91 Map = AllocatePool (sizeof (MAP_INFO_INSTANCE));
92 if (Map == NULL) {
93 return EFI_OUT_OF_RESOURCES;
94 }
95
96 *Mapping = Map;
97
98 if ((((UINTN)HostAddress & (gCacheAlignment - 1)) != 0) ||
99 ((*NumberOfBytes % gCacheAlignment) != 0)) {
100 //
101 // If the buffer does not fill entire cache lines we must double buffer into
102 // uncached memory. Device (PCI) address becomes uncached page.
103 //
104 Map->DoubleBuffer = TRUE;
105 Status = DmaAllocateBuffer (EfiBootServicesData, EFI_SIZE_TO_PAGES (*NumberOfBytes), &Buffer);
106 if (EFI_ERROR (Status)) {
107 return Status;
108 }
109
110 *DeviceAddress = (PHYSICAL_ADDRESS)(UINTN)Buffer;
111
112 } else {
113 Map->DoubleBuffer = FALSE;
114 }
115
116 Map->HostAddress = (UINTN)HostAddress;
117 Map->DeviceAddress = *DeviceAddress;
118 Map->NumberOfBytes = *NumberOfBytes;
119 Map->Operation = Operation;
120
121 if (Map->DoubleBuffer) {
122 if (Map->Operation == MapOperationBusMasterWrite) {
123 CopyMem ((VOID *)(UINTN)Map->DeviceAddress, (VOID *)(UINTN)Map->HostAddress, Map->NumberOfBytes);
124 }
125 } else {
126 // EfiCpuFlushTypeWriteBack, EfiCpuFlushTypeInvalidate
127 if (Map->Operation == MapOperationBusMasterWrite || Map->Operation == MapOperationBusMasterRead) {
128 gCpu->FlushDataCache (gCpu, (EFI_PHYSICAL_ADDRESS)(UINTN)HostAddress, Map->NumberOfBytes, EfiCpuFlushTypeWriteBackInvalidate);
129 }
130 }
131
132 return EFI_SUCCESS;
133 }
134
135
136 /**
137 Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()
138 operation and releases any corresponding resources.
139
140 @param Mapping The mapping value returned from DmaMap*().
141
142 @retval EFI_SUCCESS The range was unmapped.
143 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
144
145 **/
146 EFI_STATUS
147 EFIAPI
148 DmaUnmap (
149 IN VOID *Mapping
150 )
151 {
152 MAP_INFO_INSTANCE *Map;
153
154 if (Mapping == NULL) {
155 ASSERT (FALSE);
156 return EFI_INVALID_PARAMETER;
157 }
158
159 Map = (MAP_INFO_INSTANCE *)Mapping;
160
161 if (Map->DoubleBuffer) {
162 if (Map->Operation == MapOperationBusMasterRead) {
163 CopyMem ((VOID *)(UINTN)Map->HostAddress, (VOID *)(UINTN)Map->DeviceAddress, Map->NumberOfBytes);
164 }
165
166 DmaFreeBuffer (EFI_SIZE_TO_PAGES (Map->NumberOfBytes), (VOID *)(UINTN)Map->DeviceAddress);
167
168 } else {
169 if (Map->Operation == MapOperationBusMasterWrite) {
170 //
171 // Make sure we read buffer from uncached memory and not the cache
172 //
173 gCpu->FlushDataCache (gCpu, Map->HostAddress, Map->NumberOfBytes, EfiCpuFlushTypeInvalidate);
174 }
175 }
176
177 FreePool (Map);
178
179 return EFI_SUCCESS;
180 }
181
182 /**
183 Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.
184 mapping.
185
186 @param MemoryType The type of memory to allocate, EfiBootServicesData or
187 EfiRuntimeServicesData.
188 @param Pages The number of pages to allocate.
189 @param HostAddress A pointer to store the base system memory address of the
190 allocated range.
191
192 @retval EFI_SUCCESS The requested memory pages were allocated.
193 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
194 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
195 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
196 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
197
198 **/
199 EFI_STATUS
200 EFIAPI
201 DmaAllocateBuffer (
202 IN EFI_MEMORY_TYPE MemoryType,
203 IN UINTN Pages,
204 OUT VOID **HostAddress
205 )
206 {
207 if (HostAddress == NULL) {
208 return EFI_INVALID_PARAMETER;
209 }
210
211 //
212 // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData
213 //
214 // We used uncached memory to keep coherency
215 //
216 if (MemoryType == EfiBootServicesData) {
217 *HostAddress = UncachedAllocatePages (Pages);
218 } else if (MemoryType != EfiRuntimeServicesData) {
219 *HostAddress = UncachedAllocateRuntimePages (Pages);
220 } else {
221 return EFI_INVALID_PARAMETER;
222 }
223
224 return EFI_SUCCESS;
225 }
226
227
228 /**
229 Frees memory that was allocated with DmaAllocateBuffer().
230
231 @param Pages The number of pages to free.
232 @param HostAddress The base system memory address of the allocated range.
233
234 @retval EFI_SUCCESS The requested memory pages were freed.
235 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
236 was not allocated with DmaAllocateBuffer().
237
238 **/
239 EFI_STATUS
240 EFIAPI
241 DmaFreeBuffer (
242 IN UINTN Pages,
243 IN VOID *HostAddress
244 )
245 {
246 if (HostAddress == NULL) {
247 return EFI_INVALID_PARAMETER;
248 }
249
250 UncachedFreePages (HostAddress, Pages);
251 return EFI_SUCCESS;
252 }
253
254
255 EFI_STATUS
256 EFIAPI
257 ArmDmaLibConstructor (
258 IN EFI_HANDLE ImageHandle,
259 IN EFI_SYSTEM_TABLE *SystemTable
260 )
261 {
262 EFI_STATUS Status;
263
264 // Get the Cpu protocol for later use
265 Status = gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&gCpu);
266 ASSERT_EFI_ERROR(Status);
267
268 gCacheAlignment = ArmDataCacheLineLength ();
269
270 return Status;
271 }
272