4 Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "DmaProtection.h"
12 Provides the controller-specific addresses required to access system memory from a
15 @param This The protocol instance pointer.
16 @param Operation Indicates if the bus master is going to read or write to system memory.
17 @param HostAddress The system memory address to map to the PCI controller.
18 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
20 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
21 access the hosts HostAddress.
22 @param Mapping A resulting value to pass to Unmap().
24 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
25 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
26 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
27 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
28 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
34 IN EDKII_IOMMU_PROTOCOL
*This
,
35 IN EDKII_IOMMU_OPERATION Operation
,
37 IN OUT UINTN
*NumberOfBytes
,
38 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
43 Completes the Map() operation and releases any corresponding resources.
45 @param This The protocol instance pointer.
46 @param Mapping The mapping value returned from Map().
48 @retval EFI_SUCCESS The range was unmapped.
49 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
50 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
55 IN EDKII_IOMMU_PROTOCOL
*This
,
60 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
61 OperationBusMasterCommonBuffer64 mapping.
63 @param This The protocol instance pointer.
64 @param Type This parameter is not used and must be ignored.
65 @param MemoryType The type of memory to allocate, EfiBootServicesData or
66 EfiRuntimeServicesData.
67 @param Pages The number of pages to allocate.
68 @param HostAddress A pointer to store the base system memory address of the
70 @param Attributes The requested bit mask of attributes for the allocated range.
72 @retval EFI_SUCCESS The requested memory pages were allocated.
73 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
74 MEMORY_WRITE_COMBINE, MEMORY_CACHED and DUAL_ADDRESS_CYCLE.
75 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
76 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
82 IN EDKII_IOMMU_PROTOCOL
*This
,
83 IN EFI_ALLOCATE_TYPE Type
,
84 IN EFI_MEMORY_TYPE MemoryType
,
86 IN OUT VOID
**HostAddress
,
91 Frees memory that was allocated with AllocateBuffer().
93 @param This The protocol instance pointer.
94 @param Pages The number of pages to free.
95 @param HostAddress The base system memory address of the allocated range.
97 @retval EFI_SUCCESS The requested memory pages were freed.
98 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
99 was not allocated with AllocateBuffer().
105 IN EDKII_IOMMU_PROTOCOL
*This
,
111 This function fills DeviceHandle/IoMmuAccess to the MAP_HANDLE_INFO,
112 based upon the DeviceAddress.
114 @param[in] DeviceHandle The device who initiates the DMA access request.
115 @param[in] DeviceAddress The base of device memory address to be used as the DMA memory.
116 @param[in] Length The length of device memory address to be used as the DMA memory.
117 @param[in] IoMmuAccess The IOMMU access.
121 SyncDeviceHandleToMapInfo (
122 IN EFI_HANDLE DeviceHandle
,
123 IN EFI_PHYSICAL_ADDRESS DeviceAddress
,
125 IN UINT64 IoMmuAccess
129 Convert the DeviceHandle to SourceId and Segment.
131 @param[in] DeviceHandle The device who initiates the DMA access request.
132 @param[out] Segment The Segment used to identify a VTd engine.
133 @param[out] SourceId The SourceId used to identify a VTd engine and table entry.
135 @retval EFI_SUCCESS The Segment and SourceId are returned.
136 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
137 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
140 DeviceHandleToSourceId (
141 IN EFI_HANDLE DeviceHandle
,
143 OUT VTD_SOURCE_ID
*SourceId
146 EFI_PCI_IO_PROTOCOL
*PciIo
;
152 EDKII_PLATFORM_VTD_DEVICE_INFO DeviceInfo
;
154 Status
= EFI_NOT_FOUND
;
155 if (mPlatformVTdPolicy
!= NULL
) {
156 Status
= mPlatformVTdPolicy
->GetDeviceId (mPlatformVTdPolicy
, DeviceHandle
, &DeviceInfo
);
157 if (!EFI_ERROR(Status
)) {
158 *Segment
= DeviceInfo
.Segment
;
159 *SourceId
= DeviceInfo
.SourceId
;
164 Status
= gBS
->HandleProtocol (DeviceHandle
, &gEfiPciIoProtocolGuid
, (VOID
**)&PciIo
);
165 if (EFI_ERROR(Status
)) {
166 return EFI_UNSUPPORTED
;
168 Status
= PciIo
->GetLocation (PciIo
, &Seg
, &Bus
, &Dev
, &Func
);
169 if (EFI_ERROR(Status
)) {
170 return EFI_UNSUPPORTED
;
172 *Segment
= (UINT16
)Seg
;
173 SourceId
->Bits
.Bus
= (UINT8
)Bus
;
174 SourceId
->Bits
.Device
= (UINT8
)Dev
;
175 SourceId
->Bits
.Function
= (UINT8
)Func
;
181 Set IOMMU attribute for a system memory.
183 If the IOMMU protocol exists, the system memory cannot be used
186 When a device requests a DMA access for a system memory,
187 the device driver need use SetAttribute() to update the IOMMU
188 attribute to request DMA access (read and/or write).
190 The DeviceHandle is used to identify which device submits the request.
191 The IOMMU implementation need translate the device path to an IOMMU device ID,
192 and set IOMMU hardware register accordingly.
193 1) DeviceHandle can be a standard PCI device.
194 The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.
195 The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.
196 The memory for BusMasterCommonBuffer need set EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.
197 After the memory is used, the memory need set 0 to keep it being protected.
198 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).
199 The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or EDKII_IOMMU_ACCESS_WRITE.
201 @param[in] This The protocol instance pointer.
202 @param[in] DeviceHandle The device who initiates the DMA access request.
203 @param[in] DeviceAddress The base of device memory address to be used as the DMA memory.
204 @param[in] Length The length of device memory address to be used as the DMA memory.
205 @param[in] IoMmuAccess The IOMMU access.
207 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by DeviceAddress and Length.
208 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
209 @retval EFI_INVALID_PARAMETER DeviceAddress is not IoMmu Page size aligned.
210 @retval EFI_INVALID_PARAMETER Length is not IoMmu Page size aligned.
211 @retval EFI_INVALID_PARAMETER Length is 0.
212 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
213 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
214 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
215 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by DeviceAddress and Length.
216 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
217 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
222 IN EDKII_IOMMU_PROTOCOL
*This
,
223 IN EFI_HANDLE DeviceHandle
,
224 IN EFI_PHYSICAL_ADDRESS DeviceAddress
,
226 IN UINT64 IoMmuAccess
231 VTD_SOURCE_ID SourceId
;
232 CHAR8 PerfToken
[sizeof("VTD(S0000.B00.D00.F00)")];
237 Status
= DeviceHandleToSourceId (DeviceHandle
, &Segment
, &SourceId
);
238 if (EFI_ERROR(Status
)) {
242 DEBUG ((DEBUG_VERBOSE
, "IoMmuSetAttribute: "));
243 DEBUG ((DEBUG_VERBOSE
, "PCI(S%x.B%x.D%x.F%x) ", Segment
, SourceId
.Bits
.Bus
, SourceId
.Bits
.Device
, SourceId
.Bits
.Function
));
244 DEBUG ((DEBUG_VERBOSE
, "(0x%lx~0x%lx) - %lx\n", DeviceAddress
, Length
, IoMmuAccess
));
246 if (mAcpiDmarTable
== NULL
) {
248 // Record the entry to driver global variable.
249 // As such once VTd is activated, the setting can be adopted.
251 if ((PcdGet8 (PcdVTdPolicyPropertyMask
) & BIT2
) != 0) {
253 // Force no IOMMU access attribute request recording before DMAR table is installed.
255 ASSERT_EFI_ERROR (EFI_NOT_READY
);
256 return EFI_NOT_READY
;
258 Status
= RequestAccessAttribute (Segment
, SourceId
, DeviceAddress
, Length
, IoMmuAccess
);
261 AsciiSPrint (PerfToken
, sizeof(PerfToken
), "S%04xB%02xD%02xF%01x", Segment
, SourceId
.Bits
.Bus
, SourceId
.Bits
.Device
, SourceId
.Bits
.Function
);
262 Identifier
= (Segment
<< 16) | SourceId
.Uint16
;
263 PERF_START_EX (gImageHandle
, PerfToken
, "IntelVTD", 0, Identifier
);
266 Status
= SetAccessAttribute (Segment
, SourceId
, DeviceAddress
, Length
, IoMmuAccess
);
269 Identifier
= (Segment
<< 16) | SourceId
.Uint16
;
270 PERF_END_EX (gImageHandle
, PerfToken
, "IntelVTD", 0, Identifier
);
274 if (!EFI_ERROR(Status
)) {
275 SyncDeviceHandleToMapInfo (
287 Set IOMMU attribute for a system memory.
289 If the IOMMU protocol exists, the system memory cannot be used
292 When a device requests a DMA access for a system memory,
293 the device driver need use SetAttribute() to update the IOMMU
294 attribute to request DMA access (read and/or write).
296 The DeviceHandle is used to identify which device submits the request.
297 The IOMMU implementation need translate the device path to an IOMMU device ID,
298 and set IOMMU hardware register accordingly.
299 1) DeviceHandle can be a standard PCI device.
300 The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.
301 The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.
302 The memory for BusMasterCommonBuffer need set EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.
303 After the memory is used, the memory need set 0 to keep it being protected.
304 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).
305 The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or EDKII_IOMMU_ACCESS_WRITE.
307 @param[in] This The protocol instance pointer.
308 @param[in] DeviceHandle The device who initiates the DMA access request.
309 @param[in] Mapping The mapping value returned from Map().
310 @param[in] IoMmuAccess The IOMMU access.
312 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by DeviceAddress and Length.
313 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
314 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
315 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
316 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
317 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
318 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by Mapping.
319 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
320 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
326 IN EDKII_IOMMU_PROTOCOL
*This
,
327 IN EFI_HANDLE DeviceHandle
,
329 IN UINT64 IoMmuAccess
333 EFI_PHYSICAL_ADDRESS DeviceAddress
;
337 OriginalTpl
= gBS
->RaiseTPL (VTD_TPL_LEVEL
);
339 Status
= GetDeviceInfoFromMapping (Mapping
, &DeviceAddress
, &NumberOfPages
);
340 if (!EFI_ERROR(Status
)) {
341 Status
= VTdSetAttribute (
345 EFI_PAGES_TO_SIZE(NumberOfPages
),
350 gBS
->RestoreTPL (OriginalTpl
);
355 EDKII_IOMMU_PROTOCOL mIntelVTd
= {
356 EDKII_IOMMU_PROTOCOL_REVISION
,
365 Initialize the VTd driver.
367 @param[in] ImageHandle ImageHandle of the loaded driver
368 @param[in] SystemTable Pointer to the System Table
370 @retval EFI_SUCCESS The Protocol is installed.
371 @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.
372 @retval EFI_DEVICE_ERROR A device error occurred attempting to initialize the driver.
378 IN EFI_HANDLE ImageHandle
,
379 IN EFI_SYSTEM_TABLE
*SystemTable
385 if ((PcdGet8(PcdVTdPolicyPropertyMask
) & BIT0
) == 0) {
386 return EFI_UNSUPPORTED
;
389 InitializeDmaProtection ();
392 Status
= gBS
->InstallMultipleProtocolInterfaces (
394 &gEdkiiIoMmuProtocolGuid
, &mIntelVTd
,
397 ASSERT_EFI_ERROR (Status
);