2 The DMA memory help functions.
4 Copyright (c) 2017, 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.
20 Provides the controller-specific addresses required to access system memory from a
23 @param IoMmu Pointer to IOMMU PPI.
24 @param Operation Indicates if the bus master is going to read or write to system memory.
25 @param HostAddress The system memory address to map to the PCI controller.
26 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
28 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
29 access the hosts HostAddress.
30 @param Mapping A resulting value to pass to Unmap().
32 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
33 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
34 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
35 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
36 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
41 IN EDKII_IOMMU_PPI
*IoMmu
,
42 IN EDKII_IOMMU_OPERATION Operation
,
44 IN OUT UINTN
*NumberOfBytes
,
45 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
61 if (EFI_ERROR (Status
)) {
62 return EFI_OUT_OF_RESOURCES
;
65 case EdkiiIoMmuOperationBusMasterRead
:
66 case EdkiiIoMmuOperationBusMasterRead64
:
67 Attribute
= EDKII_IOMMU_ACCESS_READ
;
69 case EdkiiIoMmuOperationBusMasterWrite
:
70 case EdkiiIoMmuOperationBusMasterWrite64
:
71 Attribute
= EDKII_IOMMU_ACCESS_WRITE
;
73 case EdkiiIoMmuOperationBusMasterCommonBuffer
:
74 case EdkiiIoMmuOperationBusMasterCommonBuffer64
:
75 Attribute
= EDKII_IOMMU_ACCESS_READ
| EDKII_IOMMU_ACCESS_WRITE
;
79 return EFI_INVALID_PARAMETER
;
81 Status
= IoMmu
->SetAttribute (
86 if (EFI_ERROR (Status
)) {
87 IoMmu
->Unmap (IoMmu
, Mapping
);
92 *DeviceAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
;
100 Completes the Map() operation and releases any corresponding resources.
102 @param IoMmu Pointer to IOMMU PPI.
103 @param Mapping The mapping value returned from Map().
108 IN EDKII_IOMMU_PPI
*IoMmu
,
113 IoMmu
->SetAttribute (IoMmu
, Mapping
, 0);
114 IoMmu
->Unmap (IoMmu
, Mapping
);
119 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
120 OperationBusMasterCommonBuffer64 mapping.
122 @param IoMmu Pointer to IOMMU PPI.
123 @param Pages The number of pages to allocate.
124 @param HostAddress A pointer to store the base system memory address of the
126 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
127 access the hosts HostAddress.
128 @param Mapping A resulting value to pass to Unmap().
130 @retval EFI_SUCCESS The requested memory pages were allocated.
131 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
132 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
133 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
134 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
138 IoMmuAllocateBuffer (
139 IN EDKII_IOMMU_PPI
*IoMmu
,
141 OUT VOID
**HostAddress
,
142 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
148 EFI_PHYSICAL_ADDRESS HostPhyAddress
;
155 Status
= IoMmu
->AllocateBuffer (
162 if (EFI_ERROR (Status
)) {
163 return EFI_OUT_OF_RESOURCES
;
166 NumberOfBytes
= EFI_PAGES_TO_SIZE (Pages
);
167 Status
= IoMmu
->Map (
169 EdkiiIoMmuOperationBusMasterCommonBuffer
,
175 if (EFI_ERROR (Status
)) {
176 IoMmu
->FreeBuffer (IoMmu
, Pages
, *HostAddress
);
178 return EFI_OUT_OF_RESOURCES
;
180 Status
= IoMmu
->SetAttribute (
183 EDKII_IOMMU_ACCESS_READ
| EDKII_IOMMU_ACCESS_WRITE
185 if (EFI_ERROR (Status
)) {
186 IoMmu
->Unmap (IoMmu
, *Mapping
);
187 IoMmu
->FreeBuffer (IoMmu
, Pages
, *HostAddress
);
193 Status
= PeiServicesAllocatePages (
198 if (EFI_ERROR (Status
)) {
199 return EFI_OUT_OF_RESOURCES
;
201 *HostAddress
= (VOID
*) (UINTN
) HostPhyAddress
;
202 *DeviceAddress
= HostPhyAddress
;
209 Frees memory that was allocated with AllocateBuffer().
211 @param IoMmu Pointer to IOMMU PPI.
212 @param Pages The number of pages to free.
213 @param HostAddress The base system memory address of the allocated range.
214 @param Mapping The mapping value returned from Map().
219 IN EDKII_IOMMU_PPI
*IoMmu
,
221 IN VOID
*HostAddress
,
226 IoMmu
->SetAttribute (IoMmu
, Mapping
, 0);
227 IoMmu
->Unmap (IoMmu
, Mapping
);
228 IoMmu
->FreeBuffer (IoMmu
, Pages
, HostAddress
);
235 @param IoMmu Pointer to pointer to IOMMU PPI.
240 OUT EDKII_IOMMU_PPI
**IoMmu
243 PeiServicesLocatePpi (