3 Copyright (c) 2017 - 2018, 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)))
51 #define VTD_TPL_LEVEL TPL_NOTIFY
54 // This is the initial max PCI DATA number.
55 // The number may be enlarged later.
57 #define MAX_VTD_PCI_DATA_NUMBER 0x100
61 VTD_SOURCE_ID PciSourceId
;
62 EDKII_PLATFORM_VTD_PCI_DEVICE_ID PciDeviceId
;
63 // for statistic analysis
68 BOOLEAN IncludeAllFlag
;
69 UINTN PciDeviceDataNumber
;
70 UINTN PciDeviceDataMaxNumber
;
71 PCI_DEVICE_DATA
*PciDeviceData
;
72 } PCI_DEVICE_INFORMATION
;
75 UINTN VtdUnitBaseAddress
;
79 VTD_ROOT_ENTRY
*RootEntryTable
;
80 VTD_EXT_ROOT_ENTRY
*ExtRootEntryTable
;
81 VTD_SECOND_LEVEL_PAGING_ENTRY
*FixedSecondLevelPagingEntry
;
82 BOOLEAN HasDirtyContext
;
83 BOOLEAN HasDirtyPages
;
84 PCI_DEVICE_INFORMATION PciDeviceInfo
;
85 } VTD_UNIT_INFORMATION
;
88 // This is the initial max ACCESS request.
89 // The number may be enlarged later.
91 #define MAX_VTD_ACCESS_REQUEST 0x100
95 VTD_SOURCE_ID SourceId
;
103 The scan bus callback function.
105 It is called in PCI bus scan for each PCI device under the bus.
107 @param[in] Context The context of the callback.
108 @param[in] Segment The segment of the source.
109 @param[in] Bus The bus of the source.
110 @param[in] Device The device of the source.
111 @param[in] Function The function of the source.
113 @retval EFI_SUCCESS The specific PCI device is processed in the callback.
117 (EFIAPI
*SCAN_BUS_FUNC_CALLBACK_FUNC
) (
125 extern EFI_ACPI_DMAR_HEADER
*mAcpiDmarTable
;
127 extern UINTN mVtdUnitNumber
;
128 extern VTD_UNIT_INFORMATION
*mVtdUnitInformation
;
130 extern UINT64 mBelow4GMemoryLimit
;
131 extern UINT64 mAbove4GMemoryLimit
;
133 extern EDKII_PLATFORM_VTD_POLICY_PROTOCOL
*mPlatformVTdPolicy
;
136 Prepare VTD configuration.
144 Setup VTd translation table.
146 @retval EFI_SUCCESS Setup translation table successfully.
147 @retval EFI_OUT_OF_RESOURCE Setup translation table fail.
150 SetupTranslationTable (
155 Enable DMAR translation.
157 @retval EFI_SUCCESS DMAR translation is enabled.
158 @retval EFI_DEVICE_ERROR DMAR translation is not enabled.
166 Disable DMAR translation.
168 @retval EFI_SUCCESS DMAR translation is disabled.
169 @retval EFI_DEVICE_ERROR DMAR translation is not disabled.
177 Invalid VTd global IOTLB.
179 @param[in] VtdIndex The index of VTd engine.
181 @retval EFI_SUCCESS VTd global IOTLB is invalidated.
182 @retval EFI_DEVICE_ERROR VTd global IOTLB is not invalidated.
185 InvalidateVtdIOTLBGlobal (
192 @param[in] VtdIndex The index of VTd engine.
200 Dump VTd registers for all VTd engine.
208 Dump VTd capability registers.
210 @param[in] CapReg The capability register.
214 IN VTD_CAP_REG
*CapReg
218 Dump VTd extended capability registers.
220 @param[in] ECapReg The extended capability register.
224 IN VTD_ECAP_REG
*ECapReg
228 Register PCI device to VTd engine.
230 @param[in] VtdIndex The index of VTd engine.
231 @param[in] Segment The segment of the source.
232 @param[in] SourceId The SourceId of the source.
233 @param[in] DeviceType The DMAR device scope type.
234 @param[in] CheckExist TRUE: ERROR will be returned if the PCI device is already registered.
235 FALSE: SUCCESS will be returned if the PCI device is registered.
237 @retval EFI_SUCCESS The PCI device is registered.
238 @retval EFI_OUT_OF_RESOURCES No enough resource to register a new PCI device.
239 @retval EFI_ALREADY_STARTED The device is already registered.
245 IN VTD_SOURCE_ID SourceId
,
247 IN BOOLEAN CheckExist
251 The scan bus callback function to always enable page attribute.
253 @param[in] Context The context of the callback.
254 @param[in] Segment The segment of the source.
255 @param[in] Bus The bus of the source.
256 @param[in] Device The device of the source.
257 @param[in] Function The function of the source.
259 @retval EFI_SUCCESS The VTd entry is updated to always enable all DMA access for the specific device.
263 ScanBusCallbackRegisterPciDevice (
272 Scan PCI bus and invoke callback function for each PCI devices under the bus.
274 @param[in] Context The context of the callback function.
275 @param[in] Segment The segment of the source.
276 @param[in] Bus The bus of the source.
277 @param[in] Callback The callback function in PCI scan.
279 @retval EFI_SUCCESS The PCI devices under the bus are scaned.
286 IN SCAN_BUS_FUNC_CALLBACK_FUNC Callback
290 Dump the PCI device information managed by this VTd engine.
292 @param[in] VtdIndex The index of VTd engine.
300 Find the VTd index by the Segment and SourceId.
302 @param[in] Segment The segment of the source.
303 @param[in] SourceId The SourceId of the source.
304 @param[out] ExtContextEntry The ExtContextEntry of the source.
305 @param[out] ContextEntry The ContextEntry of the source.
307 @return The index of the VTd engine.
308 @retval (UINTN)-1 The VTd engine is not found.
311 FindVtdIndexByPciDevice (
313 IN VTD_SOURCE_ID SourceId
,
314 OUT VTD_EXT_CONTEXT_ENTRY
**ExtContextEntry
,
315 OUT VTD_CONTEXT_ENTRY
**ContextEntry
319 Get the DMAR ACPI table.
321 @retval EFI_SUCCESS The DMAR ACPI table is got.
322 @retval EFI_ALREADY_STARTED The DMAR ACPI table has been got previously.
323 @retval EFI_NOT_FOUND The DMAR ACPI table is not found.
331 Parse DMAR DRHD table.
333 @return EFI_SUCCESS The DMAR DRHD table is parsed.
336 ParseDmarAcpiTableDrhd (
341 Parse DMAR RMRR table.
343 @return EFI_SUCCESS The DMAR RMRR table is parsed.
346 ParseDmarAcpiTableRmrr (
351 Dump DMAR context entry table.
353 @param[in] RootEntry DMAR root entry.
356 DumpDmarContextEntryTable (
357 IN VTD_ROOT_ENTRY
*RootEntry
361 Dump DMAR extended context entry table.
363 @param[in] ExtRootEntry DMAR extended root entry.
366 DumpDmarExtContextEntryTable (
367 IN VTD_EXT_ROOT_ENTRY
*ExtRootEntry
371 Dump DMAR second level paging entry.
373 @param[in] SecondLevelPagingEntry The second level paging entry.
376 DumpSecondLevelPagingEntry (
377 IN VOID
*SecondLevelPagingEntry
381 Set VTd attribute for a system memory.
383 @param[in] VtdIndex The index used to identify a VTd engine.
384 @param[in] DomainIdentifier The domain ID of the source.
385 @param[in] SecondLevelPagingEntry The second level paging entry in VTd table for the device.
386 @param[in] BaseAddress The base of device memory address to be used as the DMA memory.
387 @param[in] Length The length of device memory address to be used as the DMA memory.
388 @param[in] IoMmuAccess The IOMMU access.
390 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by BaseAddress and Length.
391 @retval EFI_INVALID_PARAMETER BaseAddress is not IoMmu Page size aligned.
392 @retval EFI_INVALID_PARAMETER Length is not IoMmu Page size aligned.
393 @retval EFI_INVALID_PARAMETER Length is 0.
394 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
395 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
396 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by BaseAddress and Length.
397 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
398 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
403 IN UINT16 DomainIdentifier
,
404 IN VTD_SECOND_LEVEL_PAGING_ENTRY
*SecondLevelPagingEntry
,
405 IN UINT64 BaseAddress
,
407 IN UINT64 IoMmuAccess
411 Set VTd attribute for a system memory.
413 @param[in] Segment The Segment used to identify a VTd engine.
414 @param[in] SourceId The SourceId used to identify a VTd engine and table entry.
415 @param[in] BaseAddress The base of device memory address to be used as the DMA memory.
416 @param[in] Length The length of device memory address to be used as the DMA memory.
417 @param[in] IoMmuAccess The IOMMU access.
419 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by BaseAddress and Length.
420 @retval EFI_INVALID_PARAMETER BaseAddress is not IoMmu Page size aligned.
421 @retval EFI_INVALID_PARAMETER Length is not IoMmu Page size aligned.
422 @retval EFI_INVALID_PARAMETER Length is 0.
423 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
424 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
425 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by BaseAddress and Length.
426 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
427 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
432 IN VTD_SOURCE_ID SourceId
,
433 IN UINT64 BaseAddress
,
435 IN UINT64 IoMmuAccess
439 Return the index of PCI data.
441 @param[in] VtdIndex The index used to identify a VTd engine.
442 @param[in] Segment The Segment used to identify a VTd engine.
443 @param[in] SourceId The SourceId used to identify a VTd engine and table entry.
445 @return The index of the PCI data.
446 @retval (UINTN)-1 The PCI data is not found.
452 IN VTD_SOURCE_ID SourceId
456 Dump VTd registers if there is error.
464 Initialize platform VTd policy.
467 InitializePlatformVTdPolicy (
472 Always enable the VTd page attribute for the device.
474 @param[in] Segment The Segment used to identify a VTd engine.
475 @param[in] SourceId The SourceId used to identify a VTd engine and table entry.
477 @retval EFI_SUCCESS The VTd entry is updated to always enable all DMA access for the specific device.
480 AlwaysEnablePageAttribute (
482 IN VTD_SOURCE_ID SourceId
486 Convert the DeviceHandle to SourceId and Segment.
488 @param[in] DeviceHandle The device who initiates the DMA access request.
489 @param[out] Segment The Segment used to identify a VTd engine.
490 @param[out] SourceId The SourceId used to identify a VTd engine and table entry.
492 @retval EFI_SUCCESS The Segment and SourceId are returned.
493 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
494 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
497 DeviceHandleToSourceId (
498 IN EFI_HANDLE DeviceHandle
,
500 OUT VTD_SOURCE_ID
*SourceId
504 Get device information from mapping.
506 @param[in] Mapping The mapping.
507 @param[out] DeviceAddress The device address of the mapping.
508 @param[out] NumberOfPages The number of pages of the mapping.
510 @retval EFI_SUCCESS The device information is returned.
511 @retval EFI_INVALID_PARAMETER The mapping is invalid.
514 GetDeviceInfoFromMapping (
516 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
517 OUT UINTN
*NumberOfPages
521 Initialize DMA protection.
524 InitializeDmaProtection (
531 @param[in] Pages the number of pages.
533 @return the page address.
534 @retval NULL No resource to allocate pages.
543 Flush VTD page table and context table memory.
545 This action is to make sure the IOMMU engine can get final data in memory.
547 @param[in] VtdIndex The index used to identify a VTd engine.
548 @param[in] Base The base address of memory to be flushed.
549 @param[in] Size The size of memory in bytes to be flushed.
552 FlushPageTableMemory (
559 Get PCI device information from DMAR DevScopeEntry.
561 @param[in] Segment The segment number.
562 @param[in] DmarDevScopeEntry DMAR DevScopeEntry
563 @param[out] Bus The bus number.
564 @param[out] Device The device number.
565 @param[out] Function The function number.
567 @retval EFI_SUCCESS The PCI device information is returned.
570 GetPciBusDeviceFunction (
572 IN EFI_ACPI_DMAR_DEVICE_SCOPE_STRUCTURE_HEADER
*DmarDevScopeEntry
,
579 Append VTd Access Request to global.
581 @param[in] Segment The Segment used to identify a VTd engine.
582 @param[in] SourceId The SourceId used to identify a VTd engine and table entry.
583 @param[in] BaseAddress The base of device memory address to be used as the DMA memory.
584 @param[in] Length The length of device memory address to be used as the DMA memory.
585 @param[in] IoMmuAccess The IOMMU access.
587 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by BaseAddress and Length.
588 @retval EFI_INVALID_PARAMETER BaseAddress is not IoMmu Page size aligned.
589 @retval EFI_INVALID_PARAMETER Length is not IoMmu Page size aligned.
590 @retval EFI_INVALID_PARAMETER Length is 0.
591 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
592 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
593 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by BaseAddress and Length.
594 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
595 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
599 RequestAccessAttribute (
601 IN VTD_SOURCE_ID SourceId
,
602 IN UINT64 BaseAddress
,
604 IN UINT64 IoMmuAccess