4 Copyright (c) 2017, 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.
17 #include <Protocol/IoMmu.h>
18 #include <Protocol/PciIo.h>
20 #include <Library/IoLib.h>
21 #include <Library/BaseLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
25 #include "DmaProtection.h"
28 Provides the controller-specific addresses required to access system memory from a
31 @param This The protocol instance pointer.
32 @param Operation Indicates if the bus master is going to read or write to system memory.
33 @param HostAddress The system memory address to map to the PCI controller.
34 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
36 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
37 access the hosts HostAddress.
38 @param Mapping A resulting value to pass to Unmap().
40 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
41 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
42 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
43 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
44 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
50 IN EDKII_IOMMU_PROTOCOL
*This
,
51 IN EDKII_IOMMU_OPERATION Operation
,
53 IN OUT UINTN
*NumberOfBytes
,
54 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
59 Completes the Map() operation and releases any corresponding resources.
61 @param This The protocol instance pointer.
62 @param Mapping The mapping value returned from Map().
64 @retval EFI_SUCCESS The range was unmapped.
65 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
66 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
71 IN EDKII_IOMMU_PROTOCOL
*This
,
76 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
77 OperationBusMasterCommonBuffer64 mapping.
79 @param This The protocol instance pointer.
80 @param Type This parameter is not used and must be ignored.
81 @param MemoryType The type of memory to allocate, EfiBootServicesData or
82 EfiRuntimeServicesData.
83 @param Pages The number of pages to allocate.
84 @param HostAddress A pointer to store the base system memory address of the
86 @param Attributes The requested bit mask of attributes for the allocated range.
88 @retval EFI_SUCCESS The requested memory pages were allocated.
89 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
90 MEMORY_WRITE_COMBINE, MEMORY_CACHED and DUAL_ADDRESS_CYCLE.
91 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
92 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
98 IN EDKII_IOMMU_PROTOCOL
*This
,
99 IN EFI_ALLOCATE_TYPE Type
,
100 IN EFI_MEMORY_TYPE MemoryType
,
102 IN OUT VOID
**HostAddress
,
107 Frees memory that was allocated with AllocateBuffer().
109 @param This The protocol instance pointer.
110 @param Pages The number of pages to free.
111 @param HostAddress The base system memory address of the allocated range.
113 @retval EFI_SUCCESS The requested memory pages were freed.
114 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
115 was not allocated with AllocateBuffer().
121 IN EDKII_IOMMU_PROTOCOL
*This
,
127 Convert the DeviceHandle to SourceId and Segment.
129 @param[in] DeviceHandle The device who initiates the DMA access request.
130 @param[out] Segment The Segment used to identify a VTd engine.
131 @param[out] SourceId The SourceId used to identify a VTd engine and table entry.
133 @retval EFI_SUCCESS The Segment and SourceId are returned.
134 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
135 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
138 DeviceHandleToSourceId (
139 IN EFI_HANDLE DeviceHandle
,
141 OUT VTD_SOURCE_ID
*SourceId
144 EFI_PCI_IO_PROTOCOL
*PciIo
;
150 EDKII_PLATFORM_VTD_DEVICE_INFO DeviceInfo
;
152 Status
= EFI_NOT_FOUND
;
153 if (mPlatformVTdPolicy
!= NULL
) {
154 Status
= mPlatformVTdPolicy
->GetDeviceId (mPlatformVTdPolicy
, DeviceHandle
, &DeviceInfo
);
155 if (!EFI_ERROR(Status
)) {
156 *Segment
= DeviceInfo
.Segment
;
157 *SourceId
= DeviceInfo
.SourceId
;
162 Status
= gBS
->HandleProtocol (DeviceHandle
, &gEfiPciIoProtocolGuid
, (VOID
**)&PciIo
);
163 if (EFI_ERROR(Status
)) {
164 return EFI_UNSUPPORTED
;
166 Status
= PciIo
->GetLocation (PciIo
, &Seg
, &Bus
, &Dev
, &Func
);
167 if (EFI_ERROR(Status
)) {
168 return EFI_UNSUPPORTED
;
170 *Segment
= (UINT16
)Seg
;
171 SourceId
->Bits
.Bus
= (UINT8
)Bus
;
172 SourceId
->Bits
.Device
= (UINT8
)Dev
;
173 SourceId
->Bits
.Function
= (UINT8
)Func
;
179 Set IOMMU attribute for a system memory.
181 If the IOMMU protocol exists, the system memory cannot be used
184 When a device requests a DMA access for a system memory,
185 the device driver need use SetAttribute() to update the IOMMU
186 attribute to request DMA access (read and/or write).
188 The DeviceHandle is used to identify which device submits the request.
189 The IOMMU implementation need translate the device path to an IOMMU device ID,
190 and set IOMMU hardware register accordingly.
191 1) DeviceHandle can be a standard PCI device.
192 The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.
193 The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.
194 The memory for BusMasterCommonBuffer need set EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.
195 After the memory is used, the memory need set 0 to keep it being protected.
196 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).
197 The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or EDKII_IOMMU_ACCESS_WRITE.
199 @param[in] This The protocol instance pointer.
200 @param[in] DeviceHandle The device who initiates the DMA access request.
201 @param[in] DeviceAddress The base of device memory address to be used as the DMA memory.
202 @param[in] Length The length of device memory address to be used as the DMA memory.
203 @param[in] IoMmuAccess The IOMMU access.
205 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by DeviceAddress and Length.
206 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
207 @retval EFI_INVALID_PARAMETER DeviceAddress is not IoMmu Page size aligned.
208 @retval EFI_INVALID_PARAMETER Length is not IoMmu Page size aligned.
209 @retval EFI_INVALID_PARAMETER Length is 0.
210 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
211 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
212 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
213 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by DeviceAddress and Length.
214 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
215 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
220 IN EDKII_IOMMU_PROTOCOL
*This
,
221 IN EFI_HANDLE DeviceHandle
,
222 IN EFI_PHYSICAL_ADDRESS DeviceAddress
,
224 IN UINT64 IoMmuAccess
229 VTD_SOURCE_ID SourceId
;
230 CHAR8 PerfToken
[sizeof("VTD(S0000.B00.D00.F00)")];
235 Status
= DeviceHandleToSourceId (DeviceHandle
, &Segment
, &SourceId
);
236 if (EFI_ERROR(Status
)) {
240 DEBUG ((DEBUG_VERBOSE
, "IoMmuSetAttribute: "));
241 DEBUG ((DEBUG_VERBOSE
, "PCI(S%x.B%x.D%x.F%x) ", Segment
, SourceId
.Bits
.Bus
, SourceId
.Bits
.Device
, SourceId
.Bits
.Function
));
242 DEBUG ((DEBUG_VERBOSE
, "(0x%lx~0x%lx) - %lx\n", DeviceAddress
, Length
, IoMmuAccess
));
245 AsciiSPrint (PerfToken
, sizeof(PerfToken
), "S%04xB%02xD%02xF%01x", Segment
, SourceId
.Bits
.Bus
, SourceId
.Bits
.Device
, SourceId
.Bits
.Function
);
246 Identifier
= (Segment
<< 16) | SourceId
.Uint16
;
247 PERF_START_EX (gImageHandle
, PerfToken
, "IntelVTD", 0, Identifier
);
250 Status
= SetAccessAttribute (Segment
, SourceId
, DeviceAddress
, Length
, IoMmuAccess
);
253 Identifier
= (Segment
<< 16) | SourceId
.Uint16
;
254 PERF_END_EX (gImageHandle
, PerfToken
, "IntelVTD", 0, Identifier
);
261 Set IOMMU attribute for a system memory.
263 If the IOMMU protocol exists, the system memory cannot be used
266 When a device requests a DMA access for a system memory,
267 the device driver need use SetAttribute() to update the IOMMU
268 attribute to request DMA access (read and/or write).
270 The DeviceHandle is used to identify which device submits the request.
271 The IOMMU implementation need translate the device path to an IOMMU device ID,
272 and set IOMMU hardware register accordingly.
273 1) DeviceHandle can be a standard PCI device.
274 The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.
275 The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.
276 The memory for BusMasterCommonBuffer need set EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.
277 After the memory is used, the memory need set 0 to keep it being protected.
278 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).
279 The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or EDKII_IOMMU_ACCESS_WRITE.
281 @param[in] This The protocol instance pointer.
282 @param[in] DeviceHandle The device who initiates the DMA access request.
283 @param[in] Mapping The mapping value returned from Map().
284 @param[in] IoMmuAccess The IOMMU access.
286 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by DeviceAddress and Length.
287 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
288 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
289 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
290 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
291 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
292 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by Mapping.
293 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
294 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
300 IN EDKII_IOMMU_PROTOCOL
*This
,
301 IN EFI_HANDLE DeviceHandle
,
303 IN UINT64 IoMmuAccess
307 EFI_PHYSICAL_ADDRESS DeviceAddress
;
310 Status
= GetDeviceInfoFromMapping (Mapping
, &DeviceAddress
, &NumberOfPages
);
311 if (EFI_ERROR(Status
)) {
314 Status
= VTdSetAttribute (
318 EFI_PAGES_TO_SIZE(NumberOfPages
),
325 EDKII_IOMMU_PROTOCOL mIntelVTd
= {
326 EDKII_IOMMU_PROTOCOL_REVISION
,
335 Initialize the VTd driver.
337 @param[in] ImageHandle ImageHandle of the loaded driver
338 @param[in] SystemTable Pointer to the System Table
340 @retval EFI_SUCCESS The Protocol is installed.
341 @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.
342 @retval EFI_DEVICE_ERROR A device error occurred attempting to initialize the driver.
348 IN EFI_HANDLE ImageHandle
,
349 IN EFI_SYSTEM_TABLE
*SystemTable
355 if ((PcdGet8(PcdVTdPolicyPropertyMask
) & BIT0
) == 0) {
356 return EFI_UNSUPPORTED
;
359 InitializeDmaProtection ();
362 Status
= gBS
->InstallMultipleProtocolInterfaces (
364 &gEdkiiIoMmuProtocolGuid
, &mIntelVTd
,
367 ASSERT_EFI_ERROR (Status
);