2 The DMA memory help function.
4 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "SdBlockIoPei.h"
12 EDKII_IOMMU_PPI
*mIoMmu
;
15 Provides the controller-specific addresses required to access system memory from a
18 @param Operation Indicates if the bus master is going to read or write to system memory.
19 @param HostAddress The system memory address to map to the PCI controller.
20 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
22 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
23 access the hosts HostAddress.
24 @param Mapping A resulting value to pass to Unmap().
26 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
27 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
28 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
29 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
30 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
35 IN EDKII_IOMMU_OPERATION Operation
,
37 IN OUT UINTN
*NumberOfBytes
,
38 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
46 Status
= mIoMmu
->Map (
54 if (EFI_ERROR (Status
)) {
55 return EFI_OUT_OF_RESOURCES
;
59 case EdkiiIoMmuOperationBusMasterRead
:
60 case EdkiiIoMmuOperationBusMasterRead64
:
61 Attribute
= EDKII_IOMMU_ACCESS_READ
;
63 case EdkiiIoMmuOperationBusMasterWrite
:
64 case EdkiiIoMmuOperationBusMasterWrite64
:
65 Attribute
= EDKII_IOMMU_ACCESS_WRITE
;
67 case EdkiiIoMmuOperationBusMasterCommonBuffer
:
68 case EdkiiIoMmuOperationBusMasterCommonBuffer64
:
69 Attribute
= EDKII_IOMMU_ACCESS_READ
| EDKII_IOMMU_ACCESS_WRITE
;
73 return EFI_INVALID_PARAMETER
;
76 Status
= mIoMmu
->SetAttribute (
81 if (EFI_ERROR (Status
)) {
85 *DeviceAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)HostAddress
;
94 Completes the Map() operation and releases any corresponding resources.
96 @param Mapping The mapping value returned from Map().
98 @retval EFI_SUCCESS The range was unmapped.
99 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
100 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
109 if (mIoMmu
!= NULL
) {
110 Status
= mIoMmu
->SetAttribute (mIoMmu
, Mapping
, 0);
111 Status
= mIoMmu
->Unmap (mIoMmu
, Mapping
);
113 Status
= EFI_SUCCESS
;
120 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
121 OperationBusMasterCommonBuffer64 mapping.
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 (
140 OUT VOID
**HostAddress
,
141 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
147 EFI_PHYSICAL_ADDRESS HostPhyAddress
;
152 if (mIoMmu
!= NULL
) {
153 Status
= mIoMmu
->AllocateBuffer (
160 if (EFI_ERROR (Status
)) {
161 return EFI_OUT_OF_RESOURCES
;
164 NumberOfBytes
= EFI_PAGES_TO_SIZE (Pages
);
165 Status
= mIoMmu
->Map (
167 EdkiiIoMmuOperationBusMasterCommonBuffer
,
173 if (EFI_ERROR (Status
)) {
174 return EFI_OUT_OF_RESOURCES
;
177 Status
= mIoMmu
->SetAttribute (
180 EDKII_IOMMU_ACCESS_READ
| EDKII_IOMMU_ACCESS_WRITE
182 if (EFI_ERROR (Status
)) {
186 Status
= PeiServicesAllocatePages (
191 if (EFI_ERROR (Status
)) {
192 return EFI_OUT_OF_RESOURCES
;
195 *HostAddress
= (VOID
*)(UINTN
)HostPhyAddress
;
196 *DeviceAddress
= HostPhyAddress
;
204 Frees memory that was allocated with AllocateBuffer().
206 @param Pages The number of pages to free.
207 @param HostAddress The base system memory address of the allocated range.
208 @param Mapping The mapping value returned from Map().
210 @retval EFI_SUCCESS The requested memory pages were freed.
211 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
212 was not allocated with AllocateBuffer().
218 IN VOID
*HostAddress
,
224 if (mIoMmu
!= NULL
) {
225 Status
= mIoMmu
->SetAttribute (mIoMmu
, Mapping
, 0);
226 Status
= mIoMmu
->Unmap (mIoMmu
, Mapping
);
227 Status
= mIoMmu
->FreeBuffer (mIoMmu
, Pages
, HostAddress
);
229 Status
= EFI_SUCCESS
;
243 PeiServicesLocatePpi (