4 Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this 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.
15 #include "DmaProtection.h"
18 Provides the controller-specific addresses required to access system memory from a
21 @param This The protocol instance pointer.
22 @param Operation Indicates if the bus master is going to read or write to system memory.
23 @param HostAddress The system memory address to map to the PCI controller.
24 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
26 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
27 access the hosts HostAddress.
28 @param Mapping A resulting value to pass to Unmap().
30 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
31 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
32 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
33 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
34 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
40 IN EDKII_IOMMU_PROTOCOL
*This
,
41 IN EDKII_IOMMU_OPERATION Operation
,
43 IN OUT UINTN
*NumberOfBytes
,
44 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
49 Completes the Map() operation and releases any corresponding resources.
51 @param This The protocol instance pointer.
52 @param Mapping The mapping value returned from Map().
54 @retval EFI_SUCCESS The range was unmapped.
55 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
56 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
61 IN EDKII_IOMMU_PROTOCOL
*This
,
66 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
67 OperationBusMasterCommonBuffer64 mapping.
69 @param This The protocol instance pointer.
70 @param Type This parameter is not used and must be ignored.
71 @param MemoryType The type of memory to allocate, EfiBootServicesData or
72 EfiRuntimeServicesData.
73 @param Pages The number of pages to allocate.
74 @param HostAddress A pointer to store the base system memory address of the
76 @param Attributes The requested bit mask of attributes for the allocated range.
78 @retval EFI_SUCCESS The requested memory pages were allocated.
79 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
80 MEMORY_WRITE_COMBINE, MEMORY_CACHED and DUAL_ADDRESS_CYCLE.
81 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
82 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
88 IN EDKII_IOMMU_PROTOCOL
*This
,
89 IN EFI_ALLOCATE_TYPE Type
,
90 IN EFI_MEMORY_TYPE MemoryType
,
92 IN OUT VOID
**HostAddress
,
97 Frees memory that was allocated with AllocateBuffer().
99 @param This The protocol instance pointer.
100 @param Pages The number of pages to free.
101 @param HostAddress The base system memory address of the allocated range.
103 @retval EFI_SUCCESS The requested memory pages were freed.
104 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
105 was not allocated with AllocateBuffer().
111 IN EDKII_IOMMU_PROTOCOL
*This
,
117 This function fills DeviceHandle/IoMmuAccess to the MAP_HANDLE_INFO,
118 based upon the DeviceAddress.
120 @param[in] DeviceHandle The device who initiates the DMA access request.
121 @param[in] DeviceAddress The base of device memory address to be used as the DMA memory.
122 @param[in] Length The length of device memory address to be used as the DMA memory.
123 @param[in] IoMmuAccess The IOMMU access.
127 SyncDeviceHandleToMapInfo (
128 IN EFI_HANDLE DeviceHandle
,
129 IN EFI_PHYSICAL_ADDRESS DeviceAddress
,
131 IN UINT64 IoMmuAccess
135 Convert the DeviceHandle to SourceId and Segment.
137 @param[in] DeviceHandle The device who initiates the DMA access request.
138 @param[out] Segment The Segment used to identify a VTd engine.
139 @param[out] SourceId The SourceId used to identify a VTd engine and table entry.
141 @retval EFI_SUCCESS The Segment and SourceId are returned.
142 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
143 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
146 DeviceHandleToSourceId (
147 IN EFI_HANDLE DeviceHandle
,
149 OUT VTD_SOURCE_ID
*SourceId
152 EFI_PCI_IO_PROTOCOL
*PciIo
;
158 EDKII_PLATFORM_VTD_DEVICE_INFO DeviceInfo
;
160 Status
= EFI_NOT_FOUND
;
161 if (mPlatformVTdPolicy
!= NULL
) {
162 Status
= mPlatformVTdPolicy
->GetDeviceId (mPlatformVTdPolicy
, DeviceHandle
, &DeviceInfo
);
163 if (!EFI_ERROR(Status
)) {
164 *Segment
= DeviceInfo
.Segment
;
165 *SourceId
= DeviceInfo
.SourceId
;
170 Status
= gBS
->HandleProtocol (DeviceHandle
, &gEfiPciIoProtocolGuid
, (VOID
**)&PciIo
);
171 if (EFI_ERROR(Status
)) {
172 return EFI_UNSUPPORTED
;
174 Status
= PciIo
->GetLocation (PciIo
, &Seg
, &Bus
, &Dev
, &Func
);
175 if (EFI_ERROR(Status
)) {
176 return EFI_UNSUPPORTED
;
178 *Segment
= (UINT16
)Seg
;
179 SourceId
->Bits
.Bus
= (UINT8
)Bus
;
180 SourceId
->Bits
.Device
= (UINT8
)Dev
;
181 SourceId
->Bits
.Function
= (UINT8
)Func
;
187 Set IOMMU attribute for a system memory.
189 If the IOMMU protocol exists, the system memory cannot be used
192 When a device requests a DMA access for a system memory,
193 the device driver need use SetAttribute() to update the IOMMU
194 attribute to request DMA access (read and/or write).
196 The DeviceHandle is used to identify which device submits the request.
197 The IOMMU implementation need translate the device path to an IOMMU device ID,
198 and set IOMMU hardware register accordingly.
199 1) DeviceHandle can be a standard PCI device.
200 The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.
201 The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.
202 The memory for BusMasterCommonBuffer need set EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.
203 After the memory is used, the memory need set 0 to keep it being protected.
204 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).
205 The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or EDKII_IOMMU_ACCESS_WRITE.
207 @param[in] This The protocol instance pointer.
208 @param[in] DeviceHandle The device who initiates the DMA access request.
209 @param[in] DeviceAddress The base of device memory address to be used as the DMA memory.
210 @param[in] Length The length of device memory address to be used as the DMA memory.
211 @param[in] IoMmuAccess The IOMMU access.
213 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by DeviceAddress and Length.
214 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
215 @retval EFI_INVALID_PARAMETER DeviceAddress is not IoMmu Page size aligned.
216 @retval EFI_INVALID_PARAMETER Length is not IoMmu Page size aligned.
217 @retval EFI_INVALID_PARAMETER Length is 0.
218 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
219 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
220 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
221 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by DeviceAddress and Length.
222 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
223 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
228 IN EDKII_IOMMU_PROTOCOL
*This
,
229 IN EFI_HANDLE DeviceHandle
,
230 IN EFI_PHYSICAL_ADDRESS DeviceAddress
,
232 IN UINT64 IoMmuAccess
237 VTD_SOURCE_ID SourceId
;
238 CHAR8 PerfToken
[sizeof("VTD(S0000.B00.D00.F00)")];
243 Status
= DeviceHandleToSourceId (DeviceHandle
, &Segment
, &SourceId
);
244 if (EFI_ERROR(Status
)) {
248 DEBUG ((DEBUG_VERBOSE
, "IoMmuSetAttribute: "));
249 DEBUG ((DEBUG_VERBOSE
, "PCI(S%x.B%x.D%x.F%x) ", Segment
, SourceId
.Bits
.Bus
, SourceId
.Bits
.Device
, SourceId
.Bits
.Function
));
250 DEBUG ((DEBUG_VERBOSE
, "(0x%lx~0x%lx) - %lx\n", DeviceAddress
, Length
, IoMmuAccess
));
252 if (mAcpiDmarTable
== NULL
) {
254 // Record the entry to driver global variable.
255 // As such once VTd is activated, the setting can be adopted.
257 Status
= RequestAccessAttribute (Segment
, SourceId
, DeviceAddress
, Length
, IoMmuAccess
);
260 AsciiSPrint (PerfToken
, sizeof(PerfToken
), "S%04xB%02xD%02xF%01x", Segment
, SourceId
.Bits
.Bus
, SourceId
.Bits
.Device
, SourceId
.Bits
.Function
);
261 Identifier
= (Segment
<< 16) | SourceId
.Uint16
;
262 PERF_START_EX (gImageHandle
, PerfToken
, "IntelVTD", 0, Identifier
);
265 Status
= SetAccessAttribute (Segment
, SourceId
, DeviceAddress
, Length
, IoMmuAccess
);
268 Identifier
= (Segment
<< 16) | SourceId
.Uint16
;
269 PERF_END_EX (gImageHandle
, PerfToken
, "IntelVTD", 0, Identifier
);
273 if (!EFI_ERROR(Status
)) {
274 SyncDeviceHandleToMapInfo (
286 Set IOMMU attribute for a system memory.
288 If the IOMMU protocol exists, the system memory cannot be used
291 When a device requests a DMA access for a system memory,
292 the device driver need use SetAttribute() to update the IOMMU
293 attribute to request DMA access (read and/or write).
295 The DeviceHandle is used to identify which device submits the request.
296 The IOMMU implementation need translate the device path to an IOMMU device ID,
297 and set IOMMU hardware register accordingly.
298 1) DeviceHandle can be a standard PCI device.
299 The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.
300 The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.
301 The memory for BusMasterCommonBuffer need set EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.
302 After the memory is used, the memory need set 0 to keep it being protected.
303 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).
304 The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or EDKII_IOMMU_ACCESS_WRITE.
306 @param[in] This The protocol instance pointer.
307 @param[in] DeviceHandle The device who initiates the DMA access request.
308 @param[in] Mapping The mapping value returned from Map().
309 @param[in] IoMmuAccess The IOMMU access.
311 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by DeviceAddress and Length.
312 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
313 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
314 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
315 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
316 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
317 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by Mapping.
318 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
319 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
325 IN EDKII_IOMMU_PROTOCOL
*This
,
326 IN EFI_HANDLE DeviceHandle
,
328 IN UINT64 IoMmuAccess
332 EFI_PHYSICAL_ADDRESS DeviceAddress
;
336 OriginalTpl
= gBS
->RaiseTPL (VTD_TPL_LEVEL
);
338 Status
= GetDeviceInfoFromMapping (Mapping
, &DeviceAddress
, &NumberOfPages
);
339 if (!EFI_ERROR(Status
)) {
340 Status
= VTdSetAttribute (
344 EFI_PAGES_TO_SIZE(NumberOfPages
),
349 gBS
->RestoreTPL (OriginalTpl
);
354 EDKII_IOMMU_PROTOCOL mIntelVTd
= {
355 EDKII_IOMMU_PROTOCOL_REVISION
,
364 Initialize the VTd driver.
366 @param[in] ImageHandle ImageHandle of the loaded driver
367 @param[in] SystemTable Pointer to the System Table
369 @retval EFI_SUCCESS The Protocol is installed.
370 @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.
371 @retval EFI_DEVICE_ERROR A device error occurred attempting to initialize the driver.
377 IN EFI_HANDLE ImageHandle
,
378 IN EFI_SYSTEM_TABLE
*SystemTable
384 if ((PcdGet8(PcdVTdPolicyPropertyMask
) & BIT0
) == 0) {
385 return EFI_UNSUPPORTED
;
388 InitializeDmaProtection ();
391 Status
= gBS
->InstallMultipleProtocolInterfaces (
393 &gEdkiiIoMmuProtocolGuid
, &mIntelVTd
,
396 ASSERT_EFI_ERROR (Status
);