TianoCore EDK2 master
Loading...
Searching...
No Matches
DmaMem.c
Go to the documentation of this file.
1
10#include "AhciPei.h"
11
20 VOID
21 )
22{
23 EFI_STATUS Status;
24 EDKII_IOMMU_PPI *IoMmu;
25
26 IoMmu = NULL;
27 Status = PeiServicesLocatePpi (
29 0,
30 NULL,
31 (VOID **)&IoMmu
32 );
33 if (!EFI_ERROR (Status) && (IoMmu != NULL)) {
34 return IoMmu;
35 }
36
37 return NULL;
38}
39
61 IN EDKII_IOMMU_OPERATION Operation,
62 IN VOID *HostAddress,
63 IN OUT UINTN *NumberOfBytes,
64 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
65 OUT VOID **Mapping
66 )
67{
68 EFI_STATUS Status;
69 UINT64 Attribute;
70 EDKII_IOMMU_PPI *IoMmu;
71
72 IoMmu = GetIoMmu ();
73
74 if (IoMmu != NULL) {
75 Status = IoMmu->Map (
76 IoMmu,
77 Operation,
78 HostAddress,
79 NumberOfBytes,
80 DeviceAddress,
81 Mapping
82 );
83 if (EFI_ERROR (Status)) {
84 return EFI_OUT_OF_RESOURCES;
85 }
86
87 switch (Operation) {
90 Attribute = EDKII_IOMMU_ACCESS_READ;
91 break;
94 Attribute = EDKII_IOMMU_ACCESS_WRITE;
95 break;
98 Attribute = EDKII_IOMMU_ACCESS_READ | EDKII_IOMMU_ACCESS_WRITE;
99 break;
100 default:
101 ASSERT (FALSE);
102 return EFI_INVALID_PARAMETER;
103 }
104
105 Status = IoMmu->SetAttribute (
106 IoMmu,
107 *Mapping,
108 Attribute
109 );
110 if (EFI_ERROR (Status)) {
111 return Status;
112 }
113 } else {
114 *DeviceAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)HostAddress;
115 *Mapping = NULL;
116 Status = EFI_SUCCESS;
117 }
118
119 return Status;
120}
121
133 IN VOID *Mapping
134 )
135{
136 EFI_STATUS Status;
137 EDKII_IOMMU_PPI *IoMmu;
138
139 IoMmu = GetIoMmu ();
140
141 if (IoMmu != NULL) {
142 Status = IoMmu->SetAttribute (IoMmu, Mapping, 0);
143 Status = IoMmu->Unmap (IoMmu, Mapping);
144 } else {
145 Status = EFI_SUCCESS;
146 }
147
148 return Status;
149}
150
171 IN UINTN Pages,
172 OUT VOID **HostAddress,
173 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
174 OUT VOID **Mapping
175 )
176{
177 EFI_STATUS Status;
178 UINTN NumberOfBytes;
179 EFI_PHYSICAL_ADDRESS HostPhyAddress;
180 EDKII_IOMMU_PPI *IoMmu;
181
182 *HostAddress = NULL;
183 *DeviceAddress = 0;
184
185 IoMmu = GetIoMmu ();
186
187 if (IoMmu != NULL) {
188 Status = IoMmu->AllocateBuffer (
189 IoMmu,
191 Pages,
192 HostAddress,
193 0
194 );
195 if (EFI_ERROR (Status)) {
196 return EFI_OUT_OF_RESOURCES;
197 }
198
199 NumberOfBytes = EFI_PAGES_TO_SIZE (Pages);
200 Status = IoMmu->Map (
201 IoMmu,
203 *HostAddress,
204 &NumberOfBytes,
205 DeviceAddress,
206 Mapping
207 );
208 if (EFI_ERROR (Status)) {
209 return EFI_OUT_OF_RESOURCES;
210 }
211
212 Status = IoMmu->SetAttribute (
213 IoMmu,
214 *Mapping,
215 EDKII_IOMMU_ACCESS_READ | EDKII_IOMMU_ACCESS_WRITE
216 );
217 if (EFI_ERROR (Status)) {
218 return Status;
219 }
220 } else {
221 Status = PeiServicesAllocatePages (
223 Pages,
224 &HostPhyAddress
225 );
226 if (EFI_ERROR (Status)) {
227 return EFI_OUT_OF_RESOURCES;
228 }
229
230 *HostAddress = (VOID *)(UINTN)HostPhyAddress;
231 *DeviceAddress = HostPhyAddress;
232 *Mapping = NULL;
233 }
234
235 return Status;
236}
237
252 IN UINTN Pages,
253 IN VOID *HostAddress,
254 IN VOID *Mapping
255 )
256{
257 EFI_STATUS Status;
258 EDKII_IOMMU_PPI *IoMmu;
259
260 IoMmu = GetIoMmu ();
261
262 if (IoMmu != NULL) {
263 Status = IoMmu->SetAttribute (IoMmu, Mapping, 0);
264 Status = IoMmu->Unmap (IoMmu, Mapping);
265 Status = IoMmu->FreeBuffer (IoMmu, Pages, HostAddress);
266 } else {
267 Status = EFI_SUCCESS;
268 }
269
270 return Status;
271}
UINT64 UINTN
EFI_STATUS IoMmuUnmap(IN VOID *Mapping)
Definition: DmaMem.c:132
EFI_STATUS IoMmuAllocateBuffer(IN UINTN Pages, OUT VOID **HostAddress, OUT EFI_PHYSICAL_ADDRESS *DeviceAddress, OUT VOID **Mapping)
Definition: DmaMem.c:170
EFI_STATUS IoMmuMap(IN EDKII_IOMMU_OPERATION Operation, IN VOID *HostAddress, IN OUT UINTN *NumberOfBytes, OUT EFI_PHYSICAL_ADDRESS *DeviceAddress, OUT VOID **Mapping)
Definition: DmaMem.c:60
EDKII_IOMMU_PPI * GetIoMmu(VOID)
Definition: DmaMem.c:19
EFI_STATUS IoMmuFreeBuffer(IN UINTN Pages, IN VOID *HostAddress, IN VOID *Mapping)
Definition: DmaMem.c:251
EFI_STATUS EFIAPI PeiServicesLocatePpi(IN CONST EFI_GUID *Guid, IN UINTN Instance, IN OUT EFI_PEI_PPI_DESCRIPTOR **PpiDescriptor, IN OUT VOID **Ppi)
EFI_STATUS EFIAPI PeiServicesAllocatePages(IN EFI_MEMORY_TYPE MemoryType, IN UINTN Pages, OUT EFI_PHYSICAL_ADDRESS *Memory)
#define NULL
Definition: Base.h:319
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
EFI_GUID gEdkiiIoMmuPpiGuid
EDKII_IOMMU_OPERATION
Definition: IoMmu.h:44
@ EdkiiIoMmuOperationBusMasterWrite
Definition: IoMmu.h:54
@ EdkiiIoMmuOperationBusMasterWrite64
Definition: IoMmu.h:69
@ EdkiiIoMmuOperationBusMasterCommonBuffer
Definition: IoMmu.h:59
@ EdkiiIoMmuOperationBusMasterRead64
Definition: IoMmu.h:64
@ EdkiiIoMmuOperationBusMasterRead
Definition: IoMmu.h:49
@ EdkiiIoMmuOperationBusMasterCommonBuffer64
Definition: IoMmu.h:74
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
#define EFI_PAGES_TO_SIZE(Pages)
Definition: UefiBaseType.h:213
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
@ EfiBootServicesData