2 The DMA memory help function.
4 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
15 @return Pointer to IOMMU PPI.
24 EDKII_IOMMU_PPI
*IoMmu
;
27 Status
= PeiServicesLocatePpi (
33 if (!EFI_ERROR (Status
) && (IoMmu
!= NULL
)) {
41 Provides the controller-specific addresses required to access system memory from a
44 @param Operation Indicates if the bus master is going to read or write to system memory.
45 @param HostAddress The system memory address to map to the PCI controller.
46 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
48 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
49 access the hosts HostAddress.
50 @param Mapping A resulting value to pass to Unmap().
52 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
53 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
54 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
55 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
56 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
61 IN EDKII_IOMMU_OPERATION Operation
,
63 IN OUT UINTN
*NumberOfBytes
,
64 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
70 EDKII_IOMMU_PPI
*IoMmu
;
83 if (EFI_ERROR (Status
)) {
84 return EFI_OUT_OF_RESOURCES
;
88 case EdkiiIoMmuOperationBusMasterRead
:
89 case EdkiiIoMmuOperationBusMasterRead64
:
90 Attribute
= EDKII_IOMMU_ACCESS_READ
;
92 case EdkiiIoMmuOperationBusMasterWrite
:
93 case EdkiiIoMmuOperationBusMasterWrite64
:
94 Attribute
= EDKII_IOMMU_ACCESS_WRITE
;
96 case EdkiiIoMmuOperationBusMasterCommonBuffer
:
97 case EdkiiIoMmuOperationBusMasterCommonBuffer64
:
98 Attribute
= EDKII_IOMMU_ACCESS_READ
| EDKII_IOMMU_ACCESS_WRITE
;
102 return EFI_INVALID_PARAMETER
;
105 Status
= IoMmu
->SetAttribute (
110 if (EFI_ERROR (Status
)) {
114 *DeviceAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)HostAddress
;
116 Status
= EFI_SUCCESS
;
123 Completes the Map() operation and releases any corresponding resources.
125 @param Mapping The mapping value returned from Map().
127 @retval EFI_SUCCESS The range was unmapped.
128 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
129 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
137 EDKII_IOMMU_PPI
*IoMmu
;
142 Status
= IoMmu
->SetAttribute (IoMmu
, Mapping
, 0);
143 Status
= IoMmu
->Unmap (IoMmu
, Mapping
);
145 Status
= EFI_SUCCESS
;
152 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
153 OperationBusMasterCommonBuffer64 mapping.
155 @param Pages The number of pages to allocate.
156 @param HostAddress A pointer to store the base system memory address of the
158 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
159 access the hosts HostAddress.
160 @param Mapping A resulting value to pass to Unmap().
162 @retval EFI_SUCCESS The requested memory pages were allocated.
163 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
164 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
165 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
166 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
170 IoMmuAllocateBuffer (
172 OUT VOID
**HostAddress
,
173 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
179 EFI_PHYSICAL_ADDRESS HostPhyAddress
;
180 EDKII_IOMMU_PPI
*IoMmu
;
188 Status
= IoMmu
->AllocateBuffer (
195 if (EFI_ERROR (Status
)) {
196 return EFI_OUT_OF_RESOURCES
;
199 NumberOfBytes
= EFI_PAGES_TO_SIZE (Pages
);
200 Status
= IoMmu
->Map (
202 EdkiiIoMmuOperationBusMasterCommonBuffer
,
208 if (EFI_ERROR (Status
)) {
209 return EFI_OUT_OF_RESOURCES
;
212 Status
= IoMmu
->SetAttribute (
215 EDKII_IOMMU_ACCESS_READ
| EDKII_IOMMU_ACCESS_WRITE
217 if (EFI_ERROR (Status
)) {
221 Status
= PeiServicesAllocatePages (
226 if (EFI_ERROR (Status
)) {
227 return EFI_OUT_OF_RESOURCES
;
230 *HostAddress
= (VOID
*)(UINTN
)HostPhyAddress
;
231 *DeviceAddress
= HostPhyAddress
;
239 Frees memory that was allocated with AllocateBuffer().
241 @param Pages The number of pages to free.
242 @param HostAddress The base system memory address of the allocated range.
243 @param Mapping The mapping value returned from Map().
245 @retval EFI_SUCCESS The requested memory pages were freed.
246 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
247 was not allocated with AllocateBuffer().
253 IN VOID
*HostAddress
,
258 EDKII_IOMMU_PPI
*IoMmu
;
263 Status
= IoMmu
->SetAttribute (IoMmu
, Mapping
, 0);
264 Status
= IoMmu
->Unmap (IoMmu
, Mapping
);
265 Status
= IoMmu
->FreeBuffer (IoMmu
, Pages
, HostAddress
);
267 Status
= EFI_SUCCESS
;