3 Copyright (c) 2019, Linaro, Ltd. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License which accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/BaseLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/DmaLib.h>
19 #include <Library/UefiBootServicesTableLib.h>
20 #include <Protocol/IoMmu.h>
23 Set IOMMU attribute for a system memory.
25 If the IOMMU protocol exists, the system memory cannot be used
28 When a device requests a DMA access for a system memory,
29 the device driver need use SetAttribute() to update the IOMMU
30 attribute to request DMA access (read and/or write).
32 The DeviceHandle is used to identify which device submits the request.
33 The IOMMU implementation need translate the device path to an IOMMU device
34 ID, and set IOMMU hardware register accordingly.
35 1) DeviceHandle can be a standard PCI device.
36 The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.
37 The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.
38 The memory for BusMasterCommonBuffer need set
39 EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.
40 After the memory is used, the memory need set 0 to keep it being
42 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).
43 The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or
44 EDKII_IOMMU_ACCESS_WRITE.
46 @param[in] This The protocol instance pointer.
47 @param[in] DeviceHandle The device who initiates the DMA access
49 @param[in] Mapping The mapping value returned from Map().
50 @param[in] IoMmuAccess The IOMMU access.
52 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range
53 specified by DeviceAddress and Length.
54 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
55 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by
57 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination
59 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
60 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported
62 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range
64 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
65 modify the IOMMU access.
66 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while
67 attempting the operation.
73 NonCoherentIoMmuSetAttribute (
74 IN EDKII_IOMMU_PROTOCOL
*This
,
75 IN EFI_HANDLE DeviceHandle
,
80 return EFI_UNSUPPORTED
;
84 Provides the controller-specific addresses required to access system memory
85 from a DMA bus master. On SEV guest, the DMA operations must be performed on
86 shared buffer hence we allocate a bounce buffer to map the HostAddress to a
87 DeviceAddress. The Encryption attribute is removed from the DeviceAddress
90 @param This The protocol instance pointer.
91 @param Operation Indicates if the bus master is going to read or
92 write to system memory.
93 @param HostAddress The system memory address to map to the PCI
95 @param NumberOfBytes On input the number of bytes to map. On output
96 the number of bytes that were mapped.
97 @param DeviceAddress The resulting map address for the bus master
98 PCI controller to use to access the hosts
100 @param Mapping A resulting value to pass to Unmap().
102 @retval EFI_SUCCESS The range was mapped for the returned
104 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common
106 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
107 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
109 @retval EFI_DEVICE_ERROR The system hardware could not map the requested
116 NonCoherentIoMmuMap (
117 IN EDKII_IOMMU_PROTOCOL
*This
,
118 IN EDKII_IOMMU_OPERATION Operation
,
119 IN VOID
*HostAddress
,
120 IN OUT UINTN
*NumberOfBytes
,
121 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
125 DMA_MAP_OPERATION DmaOperation
;
128 case EdkiiIoMmuOperationBusMasterRead
:
129 case EdkiiIoMmuOperationBusMasterRead64
:
130 DmaOperation
= MapOperationBusMasterRead
;
133 case EdkiiIoMmuOperationBusMasterWrite
:
134 case EdkiiIoMmuOperationBusMasterWrite64
:
135 DmaOperation
= MapOperationBusMasterWrite
;
138 case EdkiiIoMmuOperationBusMasterCommonBuffer
:
139 case EdkiiIoMmuOperationBusMasterCommonBuffer64
:
140 DmaOperation
= MapOperationBusMasterCommonBuffer
;
145 return EFI_INVALID_PARAMETER
;
148 return DmaMap (DmaOperation
, HostAddress
, NumberOfBytes
,
149 DeviceAddress
, Mapping
);
153 Completes the Map() operation and releases any corresponding resources.
155 @param This The protocol instance pointer.
156 @param Mapping The mapping value returned from Map().
158 @retval EFI_SUCCESS The range was unmapped.
159 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by
161 @retval EFI_DEVICE_ERROR The data was not committed to the target system
167 NonCoherentIoMmuUnmap (
168 IN EDKII_IOMMU_PROTOCOL
*This
,
172 return DmaUnmap (Mapping
);
176 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
177 OperationBusMasterCommonBuffer64 mapping.
179 @param This The protocol instance pointer.
180 @param Type This parameter is not used and must be ignored.
181 @param MemoryType The type of memory to allocate,
182 EfiBootServicesData or EfiRuntimeServicesData.
183 @param Pages The number of pages to allocate.
184 @param HostAddress A pointer to store the base system memory
185 address of the allocated range.
186 @param Attributes The requested bit mask of attributes for the
189 @retval EFI_SUCCESS The requested memory pages were allocated.
190 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal
191 attribute bits are MEMORY_WRITE_COMBINE and
193 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
194 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
200 NonCoherentIoMmuAllocateBuffer (
201 IN EDKII_IOMMU_PROTOCOL
*This
,
202 IN EFI_ALLOCATE_TYPE Type
,
203 IN EFI_MEMORY_TYPE MemoryType
,
205 IN OUT VOID
**HostAddress
,
209 return DmaAllocateBuffer (MemoryType
, Pages
, HostAddress
);
213 Frees memory that was allocated with AllocateBuffer().
215 @param This The protocol instance pointer.
216 @param Pages The number of pages to free.
217 @param HostAddress The base system memory address of the allocated
220 @retval EFI_SUCCESS The requested memory pages were freed.
221 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and
222 Pages was not allocated with AllocateBuffer().
228 NonCoherentIoMmuFreeBuffer (
229 IN EDKII_IOMMU_PROTOCOL
*This
,
234 return DmaFreeBuffer (Pages
, HostAddress
);
237 STATIC EDKII_IOMMU_PROTOCOL mNonCoherentIoMmuOps
= {
238 EDKII_IOMMU_PROTOCOL_REVISION
,
239 NonCoherentIoMmuSetAttribute
,
241 NonCoherentIoMmuUnmap
,
242 NonCoherentIoMmuAllocateBuffer
,
243 NonCoherentIoMmuFreeBuffer
,
249 NonCoherentIoMmuDxeEntryPoint (
250 IN EFI_HANDLE ImageHandle
,
251 IN EFI_SYSTEM_TABLE
*SystemTable
254 return gBS
->InstallMultipleProtocolInterfaces (&ImageHandle
,
255 &gEdkiiIoMmuProtocolGuid
, &mNonCoherentIoMmuOps
,