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
;
87 case EdkiiIoMmuOperationBusMasterRead
:
88 case EdkiiIoMmuOperationBusMasterRead64
:
89 Attribute
= EDKII_IOMMU_ACCESS_READ
;
91 case EdkiiIoMmuOperationBusMasterWrite
:
92 case EdkiiIoMmuOperationBusMasterWrite64
:
93 Attribute
= EDKII_IOMMU_ACCESS_WRITE
;
95 case EdkiiIoMmuOperationBusMasterCommonBuffer
:
96 case EdkiiIoMmuOperationBusMasterCommonBuffer64
:
97 Attribute
= EDKII_IOMMU_ACCESS_READ
| EDKII_IOMMU_ACCESS_WRITE
;
101 return EFI_INVALID_PARAMETER
;
103 Status
= IoMmu
->SetAttribute (
108 if (EFI_ERROR (Status
)) {
112 *DeviceAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)HostAddress
;
114 Status
= EFI_SUCCESS
;
120 Completes the Map() operation and releases any corresponding resources.
122 @param Mapping The mapping value returned from Map().
124 @retval EFI_SUCCESS The range was unmapped.
125 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
126 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
134 EDKII_IOMMU_PPI
*IoMmu
;
139 Status
= IoMmu
->SetAttribute (IoMmu
, Mapping
, 0);
140 Status
= IoMmu
->Unmap (IoMmu
, Mapping
);
142 Status
= EFI_SUCCESS
;
148 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
149 OperationBusMasterCommonBuffer64 mapping.
151 @param Pages The number of pages to allocate.
152 @param HostAddress A pointer to store the base system memory address of the
154 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
155 access the hosts HostAddress.
156 @param Mapping A resulting value to pass to Unmap().
158 @retval EFI_SUCCESS The requested memory pages were allocated.
159 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
160 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
161 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
162 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
166 IoMmuAllocateBuffer (
168 OUT VOID
**HostAddress
,
169 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
175 EFI_PHYSICAL_ADDRESS HostPhyAddress
;
176 EDKII_IOMMU_PPI
*IoMmu
;
184 Status
= IoMmu
->AllocateBuffer (
191 if (EFI_ERROR (Status
)) {
192 return EFI_OUT_OF_RESOURCES
;
195 NumberOfBytes
= EFI_PAGES_TO_SIZE(Pages
);
196 Status
= IoMmu
->Map (
198 EdkiiIoMmuOperationBusMasterCommonBuffer
,
204 if (EFI_ERROR (Status
)) {
205 return EFI_OUT_OF_RESOURCES
;
207 Status
= IoMmu
->SetAttribute (
210 EDKII_IOMMU_ACCESS_READ
| EDKII_IOMMU_ACCESS_WRITE
212 if (EFI_ERROR (Status
)) {
216 Status
= PeiServicesAllocatePages (
221 if (EFI_ERROR (Status
)) {
222 return EFI_OUT_OF_RESOURCES
;
224 *HostAddress
= (VOID
*)(UINTN
)HostPhyAddress
;
225 *DeviceAddress
= HostPhyAddress
;
232 Frees memory that was allocated with AllocateBuffer().
234 @param Pages The number of pages to free.
235 @param HostAddress The base system memory address of the allocated range.
236 @param Mapping The mapping value returned from Map().
238 @retval EFI_SUCCESS The requested memory pages were freed.
239 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
240 was not allocated with AllocateBuffer().
246 IN VOID
*HostAddress
,
251 EDKII_IOMMU_PPI
*IoMmu
;
256 Status
= IoMmu
->SetAttribute (IoMmu
, Mapping
, 0);
257 Status
= IoMmu
->Unmap (IoMmu
, Mapping
);
258 Status
= IoMmu
->FreeBuffer (IoMmu
, Pages
, HostAddress
);
260 Status
= EFI_SUCCESS
;