3 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php.
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 #ifndef _DMAR_PROTECTION_H_
15 #define _DMAR_PROTECTION_H_
20 #include <Library/BaseLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/MemoryAllocationLib.h>
23 #include <Library/UefiBootServicesTableLib.h>
24 #include <Library/IoLib.h>
25 #include <Library/PciSegmentLib.h>
26 #include <Library/DebugLib.h>
27 #include <Library/UefiLib.h>
28 #include <Library/CacheMaintenanceLib.h>
29 #include <Library/PerformanceLib.h>
30 #include <Library/PrintLib.h>
32 #include <Guid/EventGroup.h>
33 #include <Guid/Acpi.h>
35 #include <Protocol/DxeSmmReadyToLock.h>
36 #include <Protocol/PciRootBridgeIo.h>
37 #include <Protocol/PciIo.h>
38 #include <Protocol/PciEnumerationComplete.h>
39 #include <Protocol/PlatformVtdPolicy.h>
40 #include <Protocol/IoMmu.h>
42 #include <IndustryStandard/Pci.h>
43 #include <IndustryStandard/DmaRemappingReportingTable.h>
44 #include <IndustryStandard/Vtd.h>
46 #define VTD_64BITS_ADDRESS(Lo, Hi) (LShiftU64 (Lo, 12) | LShiftU64 (Hi, 32))
48 #define ALIGN_VALUE_UP(Value, Alignment) (((Value) + (Alignment) - 1) & (~((Alignment) - 1)))
49 #define ALIGN_VALUE_LOW(Value, Alignment) ((Value) & (~((Alignment) - 1)))
52 // This is the initial max PCI DATA number.
53 // The number may be enlarged later.
55 #define MAX_VTD_PCI_DATA_NUMBER 0x100
59 VTD_SOURCE_ID PciSourceId
;
60 EDKII_PLATFORM_VTD_PCI_DEVICE_ID PciDeviceId
;
61 // for statistic analysis
66 BOOLEAN IncludeAllFlag
;
67 UINTN PciDeviceDataNumber
;
68 UINTN PciDeviceDataMaxNumber
;
69 PCI_DEVICE_DATA
*PciDeviceData
;
70 } PCI_DEVICE_INFORMATION
;
73 UINTN VtdUnitBaseAddress
;
77 VTD_ROOT_ENTRY
*RootEntryTable
;
78 VTD_EXT_ROOT_ENTRY
*ExtRootEntryTable
;
79 VTD_SECOND_LEVEL_PAGING_ENTRY
*FixedSecondLevelPagingEntry
;
80 BOOLEAN HasDirtyContext
;
81 BOOLEAN HasDirtyPages
;
82 PCI_DEVICE_INFORMATION PciDeviceInfo
;
83 } VTD_UNIT_INFORMATION
;
86 The scan bus callback function.
88 It is called in PCI bus scan for each PCI device under the bus.
90 @param[in] Context The context of the callback.
91 @param[in] Segment The segment of the source.
92 @param[in] Bus The bus of the source.
93 @param[in] Device The device of the source.
94 @param[in] Function The function of the source.
96 @retval EFI_SUCCESS The specific PCI device is processed in the callback.
100 (EFIAPI
*SCAN_BUS_FUNC_CALLBACK_FUNC
) (
108 extern EFI_ACPI_DMAR_HEADER
*mAcpiDmarTable
;
110 extern UINT64 mVtdHostAddressWidthMask
;
111 extern UINTN mVtdUnitNumber
;
112 extern VTD_UNIT_INFORMATION
*mVtdUnitInformation
;
114 extern UINT64 mBelow4GMemoryLimit
;
115 extern UINT64 mAbove4GMemoryLimit
;
117 extern EDKII_PLATFORM_VTD_POLICY_PROTOCOL
*mPlatformVTdPolicy
;
120 Prepare VTD configuration.
128 Setup VTd translation table.
130 @retval EFI_SUCCESS Setup translation table successfully.
131 @retval EFI_OUT_OF_RESOURCE Setup translation table fail.
134 SetupTranslationTable (
139 Enable DMAR translation.
141 @retval EFI_SUCCESS DMAR translation is enabled.
142 @retval EFI_DEVICE_ERROR DMAR translation is not enabled.
150 Disable DMAR translation.
152 @retval EFI_SUCCESS DMAR translation is disabled.
153 @retval EFI_DEVICE_ERROR DMAR translation is not disabled.
161 Invalid VTd global IOTLB.
163 @param[in] VtdIndex The index of VTd engine.
165 @retval EFI_SUCCESS VTd global IOTLB is invalidated.
166 @retval EFI_DEVICE_ERROR VTd global IOTLB is not invalidated.
169 InvalidateVtdIOTLBGlobal (
176 @param[in] VtdIndex The index of VTd engine.
184 Dump VTd registers for all VTd engine.
192 Dump VTd capability registers.
194 @param[in] CapReg The capability register.
198 IN VTD_CAP_REG
*CapReg
202 Dump VTd extended capability registers.
204 @param[in] ECapReg The extended capability register.
208 IN VTD_ECAP_REG
*ECapReg
212 Register PCI device to VTd engine.
214 @param[in] VtdIndex The index of VTd engine.
215 @param[in] Segment The segment of the source.
216 @param[in] SourceId The SourceId of the source.
217 @param[in] DeviceType The DMAR device scope type.
218 @param[in] CheckExist TRUE: ERROR will be returned if the PCI device is already registered.
219 FALSE: SUCCESS will be returned if the PCI device is registered.
221 @retval EFI_SUCCESS The PCI device is registered.
222 @retval EFI_OUT_OF_RESOURCES No enough resource to register a new PCI device.
223 @retval EFI_ALREADY_STARTED The device is already registered.
229 IN VTD_SOURCE_ID SourceId
,
231 IN BOOLEAN CheckExist
235 The scan bus callback function to always enable page attribute.
237 @param[in] Context The context of the callback.
238 @param[in] Segment The segment of the source.
239 @param[in] Bus The bus of the source.
240 @param[in] Device The device of the source.
241 @param[in] Function The function of the source.
243 @retval EFI_SUCCESS The VTd entry is updated to always enable all DMA access for the specific device.
247 ScanBusCallbackRegisterPciDevice (
256 Scan PCI bus and invoke callback function for each PCI devices under the bus.
258 @param[in] Context The context of the callback function.
259 @param[in] Segment The segment of the source.
260 @param[in] Bus The bus of the source.
261 @param[in] Callback The callback function in PCI scan.
263 @retval EFI_SUCCESS The PCI devices under the bus are scaned.
270 IN SCAN_BUS_FUNC_CALLBACK_FUNC Callback
274 Dump the PCI device information managed by this VTd engine.
276 @param[in] VtdIndex The index of VTd engine.
284 Find the VTd index by the Segment and SourceId.
286 @param[in] Segment The segment of the source.
287 @param[in] SourceId The SourceId of the source.
288 @param[out] ExtContextEntry The ExtContextEntry of the source.
289 @param[out] ContextEntry The ContextEntry of the source.
291 @return The index of the VTd engine.
292 @retval (UINTN)-1 The VTd engine is not found.
295 FindVtdIndexByPciDevice (
297 IN VTD_SOURCE_ID SourceId
,
298 OUT VTD_EXT_CONTEXT_ENTRY
**ExtContextEntry
,
299 OUT VTD_CONTEXT_ENTRY
**ContextEntry
303 Get the DMAR ACPI table.
305 @retval EFI_SUCCESS The DMAR ACPI table is got.
306 @retval EFI_ALREADY_STARTED The DMAR ACPI table has been got previously.
307 @retval EFI_NOT_FOUND The DMAR ACPI table is not found.
315 Parse DMAR DRHD table.
317 @return EFI_SUCCESS The DMAR DRHD table is parsed.
320 ParseDmarAcpiTableDrhd (
325 Parse DMAR RMRR table.
327 @return EFI_SUCCESS The DMAR RMRR table is parsed.
330 ParseDmarAcpiTableRmrr (
335 Dump DMAR context entry table.
337 @param[in] RootEntry DMAR root entry.
340 DumpDmarContextEntryTable (
341 IN VTD_ROOT_ENTRY
*RootEntry
345 Dump DMAR extended context entry table.
347 @param[in] ExtRootEntry DMAR extended root entry.
350 DumpDmarExtContextEntryTable (
351 IN VTD_EXT_ROOT_ENTRY
*ExtRootEntry
355 Dump DMAR second level paging entry.
357 @param[in] SecondLevelPagingEntry The second level paging entry.
360 DumpSecondLevelPagingEntry (
361 IN VOID
*SecondLevelPagingEntry
365 Set VTd attribute for a system memory.
367 @param[in] VtdIndex The index used to identify a VTd engine.
368 @param[in] DomainIdentifier The domain ID of the source.
369 @param[in] SecondLevelPagingEntry The second level paging entry in VTd table for the device.
370 @param[in] BaseAddress The base of device memory address to be used as the DMA memory.
371 @param[in] Length The length of device memory address to be used as the DMA memory.
372 @param[in] IoMmuAccess The IOMMU access.
374 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by BaseAddress and Length.
375 @retval EFI_INVALID_PARAMETER BaseAddress is not IoMmu Page size aligned.
376 @retval EFI_INVALID_PARAMETER Length is not IoMmu Page size aligned.
377 @retval EFI_INVALID_PARAMETER Length is 0.
378 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
379 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
380 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by BaseAddress and Length.
381 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
382 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
387 IN UINT16 DomainIdentifier
,
388 IN VTD_SECOND_LEVEL_PAGING_ENTRY
*SecondLevelPagingEntry
,
389 IN UINT64 BaseAddress
,
391 IN UINT64 IoMmuAccess
395 Set VTd attribute for a system memory.
397 @param[in] Segment The Segment used to identify a VTd engine.
398 @param[in] SourceId The SourceId used to identify a VTd engine and table entry.
399 @param[in] BaseAddress The base of device memory address to be used as the DMA memory.
400 @param[in] Length The length of device memory address to be used as the DMA memory.
401 @param[in] IoMmuAccess The IOMMU access.
403 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by BaseAddress and Length.
404 @retval EFI_INVALID_PARAMETER BaseAddress is not IoMmu Page size aligned.
405 @retval EFI_INVALID_PARAMETER Length is not IoMmu Page size aligned.
406 @retval EFI_INVALID_PARAMETER Length is 0.
407 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
408 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
409 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by BaseAddress and Length.
410 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
411 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
416 IN VTD_SOURCE_ID SourceId
,
417 IN UINT64 BaseAddress
,
419 IN UINT64 IoMmuAccess
423 Return the index of PCI data.
425 @param[in] VtdIndex The index used to identify a VTd engine.
426 @param[in] Segment The Segment used to identify a VTd engine.
427 @param[in] SourceId The SourceId used to identify a VTd engine and table entry.
429 @return The index of the PCI data.
430 @retval (UINTN)-1 The PCI data is not found.
436 IN VTD_SOURCE_ID SourceId
440 Dump VTd registers if there is error.
448 Initialize platform VTd policy.
451 InitializePlatformVTdPolicy (
456 Always enable the VTd page attribute for the device.
458 @param[in] Segment The Segment used to identify a VTd engine.
459 @param[in] SourceId The SourceId used to identify a VTd engine and table entry.
461 @retval EFI_SUCCESS The VTd entry is updated to always enable all DMA access for the specific device.
464 AlwaysEnablePageAttribute (
466 IN VTD_SOURCE_ID SourceId
470 Convert the DeviceHandle to SourceId and Segment.
472 @param[in] DeviceHandle The device who initiates the DMA access request.
473 @param[out] Segment The Segment used to identify a VTd engine.
474 @param[out] SourceId The SourceId used to identify a VTd engine and table entry.
476 @retval EFI_SUCCESS The Segment and SourceId are returned.
477 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
478 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
481 DeviceHandleToSourceId (
482 IN EFI_HANDLE DeviceHandle
,
484 OUT VTD_SOURCE_ID
*SourceId
488 Get device information from mapping.
490 @param[in] Mapping The mapping.
491 @param[out] DeviceAddress The device address of the mapping.
492 @param[out] NumberOfPages The number of pages of the mapping.
494 @retval EFI_SUCCESS The device information is returned.
495 @retval EFI_INVALID_PARAMETER The mapping is invalid.
498 GetDeviceInfoFromMapping (
500 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
501 OUT UINTN
*NumberOfPages
505 Initialize DMA protection.
508 InitializeDmaProtection (
515 @param[in] Pages the number of pages.
517 @return the page address.
518 @retval NULL No resource to allocate pages.
527 Flush VTD page table and context table memory.
529 This action is to make sure the IOMMU engine can get final data in memory.
531 @param[in] VtdIndex The index used to identify a VTd engine.
532 @param[in] Base The base address of memory to be flushed.
533 @param[in] Size The size of memory in bytes to be flushed.
536 FlushPageTableMemory (
543 Get PCI device information from DMAR DevScopeEntry.
545 @param[in] Segment The segment number.
546 @param[in] DmarDevScopeEntry DMAR DevScopeEntry
547 @param[out] Bus The bus number.
548 @param[out] Device The device number.
549 @param[out] Function The function number.
551 @retval EFI_SUCCESS The PCI device information is returned.
554 GetPciBusDeviceFunction (
556 IN EFI_ACPI_DMAR_DEVICE_SCOPE_STRUCTURE_HEADER
*DmarDevScopeEntry
,