2 The DMA memory help function.
4 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions
8 of the BSD License which accompanies this distribution. The
9 full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 @return Pointer to IOMMU PPI.
31 EDKII_IOMMU_PPI
*IoMmu
;
34 Status
= PeiServicesLocatePpi (
40 if (!EFI_ERROR (Status
) && (IoMmu
!= NULL
)) {
48 Provides the controller-specific addresses required to access system memory from a
51 @param Operation Indicates if the bus master is going to read or write to system memory.
52 @param HostAddress The system memory address to map to the PCI controller.
53 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
55 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
56 access the hosts HostAddress.
57 @param Mapping A resulting value to pass to Unmap().
59 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
60 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
61 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
62 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
63 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
68 IN EDKII_IOMMU_OPERATION Operation
,
70 IN OUT UINTN
*NumberOfBytes
,
71 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
77 EDKII_IOMMU_PPI
*IoMmu
;
90 if (EFI_ERROR (Status
)) {
91 return EFI_OUT_OF_RESOURCES
;
94 case EdkiiIoMmuOperationBusMasterRead
:
95 case EdkiiIoMmuOperationBusMasterRead64
:
96 Attribute
= EDKII_IOMMU_ACCESS_READ
;
98 case EdkiiIoMmuOperationBusMasterWrite
:
99 case EdkiiIoMmuOperationBusMasterWrite64
:
100 Attribute
= EDKII_IOMMU_ACCESS_WRITE
;
102 case EdkiiIoMmuOperationBusMasterCommonBuffer
:
103 case EdkiiIoMmuOperationBusMasterCommonBuffer64
:
104 Attribute
= EDKII_IOMMU_ACCESS_READ
| EDKII_IOMMU_ACCESS_WRITE
;
108 return EFI_INVALID_PARAMETER
;
110 Status
= IoMmu
->SetAttribute (
115 if (EFI_ERROR (Status
)) {
119 *DeviceAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)HostAddress
;
121 Status
= EFI_SUCCESS
;
127 Completes the Map() operation and releases any corresponding resources.
129 @param Mapping The mapping value returned from Map().
131 @retval EFI_SUCCESS The range was unmapped.
132 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
133 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
141 EDKII_IOMMU_PPI
*IoMmu
;
146 Status
= IoMmu
->SetAttribute (IoMmu
, Mapping
, 0);
147 Status
= IoMmu
->Unmap (IoMmu
, Mapping
);
149 Status
= EFI_SUCCESS
;
155 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
156 OperationBusMasterCommonBuffer64 mapping.
158 @param Pages The number of pages to allocate.
159 @param HostAddress A pointer to store the base system memory address of the
161 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
162 access the hosts HostAddress.
163 @param Mapping A resulting value to pass to Unmap().
165 @retval EFI_SUCCESS The requested memory pages were allocated.
166 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
167 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
168 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
169 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
173 IoMmuAllocateBuffer (
175 OUT VOID
**HostAddress
,
176 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
182 EFI_PHYSICAL_ADDRESS HostPhyAddress
;
183 EDKII_IOMMU_PPI
*IoMmu
;
191 Status
= IoMmu
->AllocateBuffer (
198 if (EFI_ERROR (Status
)) {
199 return EFI_OUT_OF_RESOURCES
;
202 NumberOfBytes
= EFI_PAGES_TO_SIZE(Pages
);
203 Status
= IoMmu
->Map (
205 EdkiiIoMmuOperationBusMasterCommonBuffer
,
211 if (EFI_ERROR (Status
)) {
212 return EFI_OUT_OF_RESOURCES
;
214 Status
= IoMmu
->SetAttribute (
217 EDKII_IOMMU_ACCESS_READ
| EDKII_IOMMU_ACCESS_WRITE
219 if (EFI_ERROR (Status
)) {
223 Status
= PeiServicesAllocatePages (
228 if (EFI_ERROR (Status
)) {
229 return EFI_OUT_OF_RESOURCES
;
231 *HostAddress
= (VOID
*)(UINTN
)HostPhyAddress
;
232 *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
;