2 The DMA memory help functions.
4 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
13 Provides the controller-specific addresses required to access system memory from a
16 @param IoMmu Pointer to IOMMU PPI.
17 @param Operation Indicates if the bus master is going to read or write to system memory.
18 @param HostAddress The system memory address to map to the PCI controller.
19 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
21 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
22 access the hosts HostAddress.
23 @param Mapping A resulting value to pass to Unmap().
25 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
26 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
27 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
28 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
29 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
34 IN EDKII_IOMMU_PPI
*IoMmu
,
35 IN EDKII_IOMMU_OPERATION Operation
,
37 IN OUT UINTN
*NumberOfBytes
,
38 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
54 if (EFI_ERROR (Status
)) {
55 return EFI_OUT_OF_RESOURCES
;
58 case EdkiiIoMmuOperationBusMasterRead
:
59 case EdkiiIoMmuOperationBusMasterRead64
:
60 Attribute
= EDKII_IOMMU_ACCESS_READ
;
62 case EdkiiIoMmuOperationBusMasterWrite
:
63 case EdkiiIoMmuOperationBusMasterWrite64
:
64 Attribute
= EDKII_IOMMU_ACCESS_WRITE
;
66 case EdkiiIoMmuOperationBusMasterCommonBuffer
:
67 case EdkiiIoMmuOperationBusMasterCommonBuffer64
:
68 Attribute
= EDKII_IOMMU_ACCESS_READ
| EDKII_IOMMU_ACCESS_WRITE
;
72 return EFI_INVALID_PARAMETER
;
74 Status
= IoMmu
->SetAttribute (
79 if (EFI_ERROR (Status
)) {
80 IoMmu
->Unmap (IoMmu
, Mapping
);
85 *DeviceAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
;
93 Completes the Map() operation and releases any corresponding resources.
95 @param IoMmu Pointer to IOMMU PPI.
96 @param Mapping The mapping value returned from Map().
101 IN EDKII_IOMMU_PPI
*IoMmu
,
106 IoMmu
->SetAttribute (IoMmu
, Mapping
, 0);
107 IoMmu
->Unmap (IoMmu
, Mapping
);
112 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
113 OperationBusMasterCommonBuffer64 mapping.
115 @param IoMmu Pointer to IOMMU PPI.
116 @param Pages The number of pages to allocate.
117 @param HostAddress A pointer to store the base system memory address of the
119 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
120 access the hosts HostAddress.
121 @param Mapping A resulting value to pass to Unmap().
123 @retval EFI_SUCCESS The requested memory pages were allocated.
124 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
125 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
126 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
127 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
131 IoMmuAllocateBuffer (
132 IN EDKII_IOMMU_PPI
*IoMmu
,
134 OUT VOID
**HostAddress
,
135 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
141 EFI_PHYSICAL_ADDRESS HostPhyAddress
;
148 Status
= IoMmu
->AllocateBuffer (
155 if (EFI_ERROR (Status
)) {
156 return EFI_OUT_OF_RESOURCES
;
159 NumberOfBytes
= EFI_PAGES_TO_SIZE (Pages
);
160 Status
= IoMmu
->Map (
162 EdkiiIoMmuOperationBusMasterCommonBuffer
,
168 if (EFI_ERROR (Status
)) {
169 IoMmu
->FreeBuffer (IoMmu
, Pages
, *HostAddress
);
171 return EFI_OUT_OF_RESOURCES
;
173 Status
= IoMmu
->SetAttribute (
176 EDKII_IOMMU_ACCESS_READ
| EDKII_IOMMU_ACCESS_WRITE
178 if (EFI_ERROR (Status
)) {
179 IoMmu
->Unmap (IoMmu
, *Mapping
);
180 IoMmu
->FreeBuffer (IoMmu
, Pages
, *HostAddress
);
186 Status
= PeiServicesAllocatePages (
191 if (EFI_ERROR (Status
)) {
192 return EFI_OUT_OF_RESOURCES
;
194 *HostAddress
= (VOID
*) (UINTN
) HostPhyAddress
;
195 *DeviceAddress
= HostPhyAddress
;
202 Frees memory that was allocated with AllocateBuffer().
204 @param IoMmu Pointer to IOMMU PPI.
205 @param Pages The number of pages to free.
206 @param HostAddress The base system memory address of the allocated range.
207 @param Mapping The mapping value returned from Map().
212 IN EDKII_IOMMU_PPI
*IoMmu
,
214 IN VOID
*HostAddress
,
219 IoMmu
->SetAttribute (IoMmu
, Mapping
, 0);
220 IoMmu
->Unmap (IoMmu
, Mapping
);
221 IoMmu
->FreeBuffer (IoMmu
, Pages
, HostAddress
);
228 @param IoMmu Pointer to pointer to IOMMU PPI.
233 OUT EDKII_IOMMU_PPI
**IoMmu
236 PeiServicesLocatePpi (