]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/Library/CoherentDmaLib/CoherentDmaLib.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / EmbeddedPkg / Library / CoherentDmaLib / CoherentDmaLib.c
1 /** @file
2 Generic ARM implementation of DmaLib.h
3
4 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <Uefi.h>
11 #include <Library/DebugLib.h>
12 #include <Library/DmaLib.h>
13 #include <Library/MemoryAllocationLib.h>
14
15 STATIC
16 PHYSICAL_ADDRESS
17 HostToDeviceAddress (
18 IN VOID *Address
19 )
20 {
21 return (PHYSICAL_ADDRESS)(UINTN)Address + PcdGet64 (PcdDmaDeviceOffset);
22 }
23
24 /**
25 Provides the DMA controller-specific addresses needed to access system memory.
26
27 Operation is relative to the DMA bus master.
28
29 @param Operation Indicates if the bus master is going to read or write to system memory.
30 @param HostAddress The system memory address to map to the DMA controller.
31 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
32 that were mapped.
33 @param DeviceAddress The resulting map address for the bus master controller to use to
34 access the hosts HostAddress.
35 @param Mapping A resulting value to pass to Unmap().
36
37 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
38 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
39 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
40 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
41 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
42
43 **/
44 EFI_STATUS
45 EFIAPI
46 DmaMap (
47 IN DMA_MAP_OPERATION Operation,
48 IN VOID *HostAddress,
49 IN OUT UINTN *NumberOfBytes,
50 OUT PHYSICAL_ADDRESS *DeviceAddress,
51 OUT VOID **Mapping
52 )
53 {
54 if ((HostAddress == NULL) ||
55 (NumberOfBytes == NULL) ||
56 (DeviceAddress == NULL) ||
57 (Mapping == NULL))
58 {
59 return EFI_INVALID_PARAMETER;
60 }
61
62 *DeviceAddress = HostToDeviceAddress (HostAddress);
63 *Mapping = NULL;
64 return EFI_SUCCESS;
65 }
66
67 /**
68 Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()
69 operation and releases any corresponding resources.
70
71 @param Mapping The mapping value returned from DmaMap*().
72
73 @retval EFI_SUCCESS The range was unmapped.
74 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
75
76 **/
77 EFI_STATUS
78 EFIAPI
79 DmaUnmap (
80 IN VOID *Mapping
81 )
82 {
83 return EFI_SUCCESS;
84 }
85
86 /**
87 Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.
88 mapping.
89
90 @param MemoryType The type of memory to allocate, EfiBootServicesData or
91 EfiRuntimeServicesData.
92 @param Pages The number of pages to allocate.
93 @param HostAddress A pointer to store the base system memory address of the
94 allocated range.
95
96 @retval EFI_SUCCESS The requested memory pages were allocated.
97 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
98 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
99 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
100 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
101
102 **/
103 EFI_STATUS
104 EFIAPI
105 DmaAllocateBuffer (
106 IN EFI_MEMORY_TYPE MemoryType,
107 IN UINTN Pages,
108 OUT VOID **HostAddress
109 )
110 {
111 return DmaAllocateAlignedBuffer (MemoryType, Pages, 0, HostAddress);
112 }
113
114 /**
115 Allocates pages that are suitable for an DmaMap() of type
116 MapOperationBusMasterCommonBuffer mapping, at the requested alignment.
117
118 @param MemoryType The type of memory to allocate, EfiBootServicesData or
119 EfiRuntimeServicesData.
120 @param Pages The number of pages to allocate.
121 @param Alignment Alignment in bytes of the base of the returned
122 buffer (must be a power of 2)
123 @param HostAddress A pointer to store the base system memory address of the
124 allocated range.
125
126 @retval EFI_SUCCESS The requested memory pages were allocated.
127 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
128 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
129 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
130 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
131
132 **/
133 EFI_STATUS
134 EFIAPI
135 DmaAllocateAlignedBuffer (
136 IN EFI_MEMORY_TYPE MemoryType,
137 IN UINTN Pages,
138 IN UINTN Alignment,
139 OUT VOID **HostAddress
140 )
141 {
142 if (Alignment == 0) {
143 Alignment = EFI_PAGE_SIZE;
144 }
145
146 if ((HostAddress == NULL) ||
147 ((Alignment & (Alignment - 1)) != 0))
148 {
149 return EFI_INVALID_PARAMETER;
150 }
151
152 //
153 // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData
154 //
155 if (MemoryType == EfiBootServicesData) {
156 *HostAddress = AllocateAlignedPages (Pages, Alignment);
157 } else if (MemoryType == EfiRuntimeServicesData) {
158 *HostAddress = AllocateAlignedRuntimePages (Pages, Alignment);
159 } else {
160 return EFI_INVALID_PARAMETER;
161 }
162
163 if (*HostAddress == NULL) {
164 return EFI_OUT_OF_RESOURCES;
165 }
166
167 return EFI_SUCCESS;
168 }
169
170 /**
171 Frees memory that was allocated with DmaAllocateBuffer().
172
173 @param Pages The number of pages to free.
174 @param HostAddress The base system memory address of the allocated range.
175
176 @retval EFI_SUCCESS The requested memory pages were freed.
177 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
178 was not allocated with DmaAllocateBuffer().
179
180 **/
181 EFI_STATUS
182 EFIAPI
183 DmaFreeBuffer (
184 IN UINTN Pages,
185 IN VOID *HostAddress
186 )
187 {
188 if (HostAddress == NULL) {
189 return EFI_INVALID_PARAMETER;
190 }
191
192 FreePages (HostAddress, Pages);
193 return EFI_SUCCESS;
194 }