TianoCore EDK2 master
Loading...
Searching...
No Matches
DmaMem.c
Go to the documentation of this file.
1
10#include "UhcPeim.h"
11
34 IN EDKII_IOMMU_PPI *IoMmu,
35 IN EDKII_IOMMU_OPERATION Operation,
36 IN VOID *HostAddress,
37 IN OUT UINTN *NumberOfBytes,
38 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
39 OUT VOID **Mapping
40 )
41{
42 EFI_STATUS Status;
43 UINT64 Attribute;
44
45 if (IoMmu != NULL) {
46 Status = IoMmu->Map (
47 IoMmu,
48 Operation,
49 HostAddress,
50 NumberOfBytes,
51 DeviceAddress,
52 Mapping
53 );
54 if (EFI_ERROR (Status)) {
55 return EFI_OUT_OF_RESOURCES;
56 }
57
58 switch (Operation) {
61 Attribute = EDKII_IOMMU_ACCESS_READ;
62 break;
65 Attribute = EDKII_IOMMU_ACCESS_WRITE;
66 break;
69 Attribute = EDKII_IOMMU_ACCESS_READ | EDKII_IOMMU_ACCESS_WRITE;
70 break;
71 default:
72 ASSERT (FALSE);
73 return EFI_INVALID_PARAMETER;
74 }
75
76 Status = IoMmu->SetAttribute (
77 IoMmu,
78 *Mapping,
79 Attribute
80 );
81 if (EFI_ERROR (Status)) {
82 IoMmu->Unmap (IoMmu, Mapping);
83 *Mapping = NULL;
84 return Status;
85 }
86 } else {
87 *DeviceAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)HostAddress;
88 *Mapping = NULL;
89 Status = EFI_SUCCESS;
90 }
91
92 return Status;
93}
94
102VOID
104 IN EDKII_IOMMU_PPI *IoMmu,
105 IN VOID *Mapping
106 )
107{
108 if (IoMmu != NULL) {
109 IoMmu->SetAttribute (IoMmu, Mapping, 0);
110 IoMmu->Unmap (IoMmu, Mapping);
111 }
112}
113
135 IN EDKII_IOMMU_PPI *IoMmu,
136 IN UINTN Pages,
137 OUT VOID **HostAddress,
138 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
139 OUT VOID **Mapping
140 )
141{
142 EFI_STATUS Status;
143 UINTN NumberOfBytes;
144 EFI_PHYSICAL_ADDRESS HostPhyAddress;
145
146 *HostAddress = NULL;
147 *DeviceAddress = 0;
148 *Mapping = NULL;
149
150 if (IoMmu != NULL) {
151 Status = IoMmu->AllocateBuffer (
152 IoMmu,
154 Pages,
155 HostAddress,
156 0
157 );
158 if (EFI_ERROR (Status)) {
159 return EFI_OUT_OF_RESOURCES;
160 }
161
162 NumberOfBytes = EFI_PAGES_TO_SIZE (Pages);
163 Status = IoMmu->Map (
164 IoMmu,
166 *HostAddress,
167 &NumberOfBytes,
168 DeviceAddress,
169 Mapping
170 );
171 if (EFI_ERROR (Status)) {
172 IoMmu->FreeBuffer (IoMmu, Pages, *HostAddress);
173 *HostAddress = NULL;
174 return EFI_OUT_OF_RESOURCES;
175 }
176
177 Status = IoMmu->SetAttribute (
178 IoMmu,
179 *Mapping,
180 EDKII_IOMMU_ACCESS_READ | EDKII_IOMMU_ACCESS_WRITE
181 );
182 if (EFI_ERROR (Status)) {
183 IoMmu->Unmap (IoMmu, *Mapping);
184 IoMmu->FreeBuffer (IoMmu, Pages, *HostAddress);
185 *Mapping = NULL;
186 *HostAddress = NULL;
187 return Status;
188 }
189 } else {
190 Status = PeiServicesAllocatePages (
192 Pages,
193 &HostPhyAddress
194 );
195 if (EFI_ERROR (Status)) {
196 return EFI_OUT_OF_RESOURCES;
197 }
198
199 *HostAddress = (VOID *)(UINTN)HostPhyAddress;
200 *DeviceAddress = HostPhyAddress;
201 *Mapping = NULL;
202 }
203
204 return Status;
205}
206
213VOID
215 OUT EDKII_IOMMU_PPI **IoMmu
216 )
217{
218 *IoMmu = NULL;
221 0,
222 NULL,
223 (VOID **)IoMmu
224 );
225}
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
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
VOID IoMmuInit(OUT EDKII_IOMMU_PPI **IoMmu)
Definition: DmaMem.c:238
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