3 Copyright (c) 2017 - 2019, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
8 #ifndef _DMAR_PROTECTION_H_
9 #define _DMAR_PROTECTION_H_
14 #include <Library/BaseLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/MemoryAllocationLib.h>
17 #include <Library/UefiBootServicesTableLib.h>
18 #include <Library/IoLib.h>
19 #include <Library/PciSegmentLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/UefiLib.h>
22 #include <Library/CacheMaintenanceLib.h>
23 #include <Library/PerformanceLib.h>
24 #include <Library/PrintLib.h>
25 #include <Library/ReportStatusCodeLib.h>
27 #include <Guid/EventGroup.h>
28 #include <Guid/Acpi.h>
30 #include <Protocol/DxeSmmReadyToLock.h>
31 #include <Protocol/PciRootBridgeIo.h>
32 #include <Protocol/PciIo.h>
33 #include <Protocol/PciEnumerationComplete.h>
34 #include <Protocol/PlatformVtdPolicy.h>
35 #include <Protocol/IoMmu.h>
37 #include <IndustryStandard/Pci.h>
38 #include <IndustryStandard/DmaRemappingReportingTable.h>
39 #include <IndustryStandard/Vtd.h>
41 #define VTD_64BITS_ADDRESS(Lo, Hi) (LShiftU64 (Lo, 12) | LShiftU64 (Hi, 32))
43 #define ALIGN_VALUE_UP(Value, Alignment) (((Value) + (Alignment) - 1) & (~((Alignment) - 1)))
44 #define ALIGN_VALUE_LOW(Value, Alignment) ((Value) & (~((Alignment) - 1)))
46 #define VTD_TPL_LEVEL TPL_NOTIFY
49 // This is the initial max PCI DATA number.
50 // The number may be enlarged later.
52 #define MAX_VTD_PCI_DATA_NUMBER 0x100
56 VTD_SOURCE_ID PciSourceId
;
57 EDKII_PLATFORM_VTD_PCI_DEVICE_ID PciDeviceId
;
58 // for statistic analysis
63 BOOLEAN IncludeAllFlag
;
64 UINTN PciDeviceDataNumber
;
65 UINTN PciDeviceDataMaxNumber
;
66 PCI_DEVICE_DATA
*PciDeviceData
;
67 } PCI_DEVICE_INFORMATION
;
70 UINTN VtdUnitBaseAddress
;
74 VTD_ROOT_ENTRY
*RootEntryTable
;
75 VTD_EXT_ROOT_ENTRY
*ExtRootEntryTable
;
76 VTD_SECOND_LEVEL_PAGING_ENTRY
*FixedSecondLevelPagingEntry
;
77 BOOLEAN HasDirtyContext
;
78 BOOLEAN HasDirtyPages
;
79 PCI_DEVICE_INFORMATION PciDeviceInfo
;
80 } VTD_UNIT_INFORMATION
;
83 // This is the initial max ACCESS request.
84 // The number may be enlarged later.
86 #define MAX_VTD_ACCESS_REQUEST 0x100
90 VTD_SOURCE_ID SourceId
;
98 The scan bus callback function.
100 It is called in PCI bus scan for each PCI device under the bus.
102 @param[in] Context The context of the callback.
103 @param[in] Segment The segment of the source.
104 @param[in] Bus The bus of the source.
105 @param[in] Device The device of the source.
106 @param[in] Function The function of the source.
108 @retval EFI_SUCCESS The specific PCI device is processed in the callback.
112 (EFIAPI
*SCAN_BUS_FUNC_CALLBACK_FUNC
) (
120 extern EFI_ACPI_DMAR_HEADER
*mAcpiDmarTable
;
122 extern UINTN mVtdUnitNumber
;
123 extern VTD_UNIT_INFORMATION
*mVtdUnitInformation
;
125 extern UINT64 mBelow4GMemoryLimit
;
126 extern UINT64 mAbove4GMemoryLimit
;
128 extern EDKII_PLATFORM_VTD_POLICY_PROTOCOL
*mPlatformVTdPolicy
;
131 Prepare VTD configuration.
139 Setup VTd translation table.
141 @retval EFI_SUCCESS Setup translation table successfully.
142 @retval EFI_OUT_OF_RESOURCE Setup translation table fail.
145 SetupTranslationTable (
150 Enable DMAR translation.
152 @retval EFI_SUCCESS DMAR translation is enabled.
153 @retval EFI_DEVICE_ERROR DMAR translation is not enabled.
161 Disable DMAR translation.
163 @retval EFI_SUCCESS DMAR translation is disabled.
164 @retval EFI_DEVICE_ERROR DMAR translation is not disabled.
172 Flush VTd engine write buffer.
174 @param[in] VtdIndex The index used to identify a VTd engine.
182 Invalidate VTd context cache.
184 @param[in] VtdIndex The index used to identify a VTd engine.
187 InvalidateContextCache (
192 Invalidate VTd IOTLB.
194 @param[in] VtdIndex The index used to identify a VTd engine.
202 Invalid VTd global IOTLB.
204 @param[in] VtdIndex The index of VTd engine.
206 @retval EFI_SUCCESS VTd global IOTLB is invalidated.
207 @retval EFI_DEVICE_ERROR VTd global IOTLB is not invalidated.
210 InvalidateVtdIOTLBGlobal (
217 @param[in] VtdIndex The index of VTd engine.
225 Dump VTd registers for all VTd engine.
233 Dump VTd capability registers.
235 @param[in] CapReg The capability register.
239 IN VTD_CAP_REG
*CapReg
243 Dump VTd extended capability registers.
245 @param[in] ECapReg The extended capability register.
249 IN VTD_ECAP_REG
*ECapReg
253 Register PCI device to VTd engine.
255 @param[in] VtdIndex The index of VTd engine.
256 @param[in] Segment The segment of the source.
257 @param[in] SourceId The SourceId of the source.
258 @param[in] DeviceType The DMAR device scope type.
259 @param[in] CheckExist TRUE: ERROR will be returned if the PCI device is already registered.
260 FALSE: SUCCESS will be returned if the PCI device is registered.
262 @retval EFI_SUCCESS The PCI device is registered.
263 @retval EFI_OUT_OF_RESOURCES No enough resource to register a new PCI device.
264 @retval EFI_ALREADY_STARTED The device is already registered.
270 IN VTD_SOURCE_ID SourceId
,
272 IN BOOLEAN CheckExist
276 The scan bus callback function to always enable page attribute.
278 @param[in] Context The context of the callback.
279 @param[in] Segment The segment of the source.
280 @param[in] Bus The bus of the source.
281 @param[in] Device The device of the source.
282 @param[in] Function The function of the source.
284 @retval EFI_SUCCESS The VTd entry is updated to always enable all DMA access for the specific device.
288 ScanBusCallbackRegisterPciDevice (
297 Scan PCI bus and invoke callback function for each PCI devices under the bus.
299 @param[in] Context The context of the callback function.
300 @param[in] Segment The segment of the source.
301 @param[in] Bus The bus of the source.
302 @param[in] Callback The callback function in PCI scan.
304 @retval EFI_SUCCESS The PCI devices under the bus are scaned.
311 IN SCAN_BUS_FUNC_CALLBACK_FUNC Callback
315 Dump the PCI device information managed by this VTd engine.
317 @param[in] VtdIndex The index of VTd engine.
325 Find the VTd index by the Segment and SourceId.
327 @param[in] Segment The segment of the source.
328 @param[in] SourceId The SourceId of the source.
329 @param[out] ExtContextEntry The ExtContextEntry of the source.
330 @param[out] ContextEntry The ContextEntry of the source.
332 @return The index of the VTd engine.
333 @retval (UINTN)-1 The VTd engine is not found.
336 FindVtdIndexByPciDevice (
338 IN VTD_SOURCE_ID SourceId
,
339 OUT VTD_EXT_CONTEXT_ENTRY
**ExtContextEntry
,
340 OUT VTD_CONTEXT_ENTRY
**ContextEntry
344 Get the DMAR ACPI table.
346 @retval EFI_SUCCESS The DMAR ACPI table is got.
347 @retval EFI_ALREADY_STARTED The DMAR ACPI table has been got previously.
348 @retval EFI_NOT_FOUND The DMAR ACPI table is not found.
356 Parse DMAR DRHD table.
358 @return EFI_SUCCESS The DMAR DRHD table is parsed.
361 ParseDmarAcpiTableDrhd (
366 Parse DMAR RMRR table.
368 @return EFI_SUCCESS The DMAR RMRR table is parsed.
371 ParseDmarAcpiTableRmrr (
376 Dump DMAR context entry table.
378 @param[in] RootEntry DMAR root entry.
381 DumpDmarContextEntryTable (
382 IN VTD_ROOT_ENTRY
*RootEntry
386 Dump DMAR extended context entry table.
388 @param[in] ExtRootEntry DMAR extended root entry.
391 DumpDmarExtContextEntryTable (
392 IN VTD_EXT_ROOT_ENTRY
*ExtRootEntry
396 Dump DMAR second level paging entry.
398 @param[in] SecondLevelPagingEntry The second level paging entry.
401 DumpSecondLevelPagingEntry (
402 IN VOID
*SecondLevelPagingEntry
406 Set VTd attribute for a system memory.
408 @param[in] VtdIndex The index used to identify a VTd engine.
409 @param[in] DomainIdentifier The domain ID of the source.
410 @param[in] SecondLevelPagingEntry The second level paging entry in VTd table for the device.
411 @param[in] BaseAddress The base of device memory address to be used as the DMA memory.
412 @param[in] Length The length of device memory address to be used as the DMA memory.
413 @param[in] IoMmuAccess The IOMMU access.
415 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by BaseAddress and Length.
416 @retval EFI_INVALID_PARAMETER BaseAddress is not IoMmu Page size aligned.
417 @retval EFI_INVALID_PARAMETER Length is not IoMmu Page size aligned.
418 @retval EFI_INVALID_PARAMETER Length is 0.
419 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
420 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
421 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by BaseAddress and Length.
422 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
423 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
428 IN UINT16 DomainIdentifier
,
429 IN VTD_SECOND_LEVEL_PAGING_ENTRY
*SecondLevelPagingEntry
,
430 IN UINT64 BaseAddress
,
432 IN UINT64 IoMmuAccess
436 Set VTd attribute for a system memory.
438 @param[in] Segment The Segment used to identify a VTd engine.
439 @param[in] SourceId The SourceId used to identify a VTd engine and table entry.
440 @param[in] BaseAddress The base of device memory address to be used as the DMA memory.
441 @param[in] Length The length of device memory address to be used as the DMA memory.
442 @param[in] IoMmuAccess The IOMMU access.
444 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by BaseAddress and Length.
445 @retval EFI_INVALID_PARAMETER BaseAddress is not IoMmu Page size aligned.
446 @retval EFI_INVALID_PARAMETER Length is not IoMmu Page size aligned.
447 @retval EFI_INVALID_PARAMETER Length is 0.
448 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
449 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
450 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by BaseAddress and Length.
451 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
452 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
457 IN VTD_SOURCE_ID SourceId
,
458 IN UINT64 BaseAddress
,
460 IN UINT64 IoMmuAccess
464 Return the index of PCI data.
466 @param[in] VtdIndex The index used to identify a VTd engine.
467 @param[in] Segment The Segment used to identify a VTd engine.
468 @param[in] SourceId The SourceId used to identify a VTd engine and table entry.
470 @return The index of the PCI data.
471 @retval (UINTN)-1 The PCI data is not found.
477 IN VTD_SOURCE_ID SourceId
481 Dump VTd registers if there is error.
489 Initialize platform VTd policy.
492 InitializePlatformVTdPolicy (
497 Always enable the VTd page attribute for the device.
499 @param[in] Segment The Segment used to identify a VTd engine.
500 @param[in] SourceId The SourceId used to identify a VTd engine and table entry.
502 @retval EFI_SUCCESS The VTd entry is updated to always enable all DMA access for the specific device.
505 AlwaysEnablePageAttribute (
507 IN VTD_SOURCE_ID SourceId
511 Convert the DeviceHandle to SourceId and Segment.
513 @param[in] DeviceHandle The device who initiates the DMA access request.
514 @param[out] Segment The Segment used to identify a VTd engine.
515 @param[out] SourceId The SourceId used to identify a VTd engine and table entry.
517 @retval EFI_SUCCESS The Segment and SourceId are returned.
518 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
519 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
522 DeviceHandleToSourceId (
523 IN EFI_HANDLE DeviceHandle
,
525 OUT VTD_SOURCE_ID
*SourceId
529 Get device information from mapping.
531 @param[in] Mapping The mapping.
532 @param[out] DeviceAddress The device address of the mapping.
533 @param[out] NumberOfPages The number of pages of the mapping.
535 @retval EFI_SUCCESS The device information is returned.
536 @retval EFI_INVALID_PARAMETER The mapping is invalid.
539 GetDeviceInfoFromMapping (
541 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
542 OUT UINTN
*NumberOfPages
546 Initialize DMA protection.
549 InitializeDmaProtection (
556 @param[in] Pages the number of pages.
558 @return the page address.
559 @retval NULL No resource to allocate pages.
568 Flush VTD page table and context table memory.
570 This action is to make sure the IOMMU engine can get final data in memory.
572 @param[in] VtdIndex The index used to identify a VTd engine.
573 @param[in] Base The base address of memory to be flushed.
574 @param[in] Size The size of memory in bytes to be flushed.
577 FlushPageTableMemory (
584 Get PCI device information from DMAR DevScopeEntry.
586 @param[in] Segment The segment number.
587 @param[in] DmarDevScopeEntry DMAR DevScopeEntry
588 @param[out] Bus The bus number.
589 @param[out] Device The device number.
590 @param[out] Function The function number.
592 @retval EFI_SUCCESS The PCI device information is returned.
595 GetPciBusDeviceFunction (
597 IN EFI_ACPI_DMAR_DEVICE_SCOPE_STRUCTURE_HEADER
*DmarDevScopeEntry
,
604 Append VTd Access Request to global.
606 @param[in] Segment The Segment used to identify a VTd engine.
607 @param[in] SourceId The SourceId used to identify a VTd engine and table entry.
608 @param[in] BaseAddress The base of device memory address to be used as the DMA memory.
609 @param[in] Length The length of device memory address to be used as the DMA memory.
610 @param[in] IoMmuAccess The IOMMU access.
612 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by BaseAddress and Length.
613 @retval EFI_INVALID_PARAMETER BaseAddress is not IoMmu Page size aligned.
614 @retval EFI_INVALID_PARAMETER Length is not IoMmu Page size aligned.
615 @retval EFI_INVALID_PARAMETER Length is 0.
616 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
617 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
618 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by BaseAddress and Length.
619 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
620 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
624 RequestAccessAttribute (
626 IN VTD_SOURCE_ID SourceId
,
627 IN UINT64 BaseAddress
,
629 IN UINT64 IoMmuAccess