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>
31 #include <Library/ReportStatusCodeLib.h>
33 #include <Guid/EventGroup.h>
34 #include <Guid/Acpi.h>
36 #include <Protocol/DxeSmmReadyToLock.h>
37 #include <Protocol/PciRootBridgeIo.h>
38 #include <Protocol/PciIo.h>
39 #include <Protocol/PciEnumerationComplete.h>
40 #include <Protocol/PlatformVtdPolicy.h>
41 #include <Protocol/IoMmu.h>
43 #include <IndustryStandard/Pci.h>
44 #include <IndustryStandard/DmaRemappingReportingTable.h>
45 #include <IndustryStandard/Vtd.h>
47 #define VTD_64BITS_ADDRESS(Lo, Hi) (LShiftU64 (Lo, 12) | LShiftU64 (Hi, 32))
49 #define ALIGN_VALUE_UP(Value, Alignment) (((Value) + (Alignment) - 1) & (~((Alignment) - 1)))
50 #define ALIGN_VALUE_LOW(Value, Alignment) ((Value) & (~((Alignment) - 1)))
52 #define VTD_TPL_LEVEL TPL_NOTIFY
55 // This is the initial max PCI DATA number.
56 // The number may be enlarged later.
58 #define MAX_VTD_PCI_DATA_NUMBER 0x100
62 VTD_SOURCE_ID PciSourceId
;
63 EDKII_PLATFORM_VTD_PCI_DEVICE_ID PciDeviceId
;
64 // for statistic analysis
69 BOOLEAN IncludeAllFlag
;
70 UINTN PciDeviceDataNumber
;
71 UINTN PciDeviceDataMaxNumber
;
72 PCI_DEVICE_DATA
*PciDeviceData
;
73 } PCI_DEVICE_INFORMATION
;
76 UINTN VtdUnitBaseAddress
;
80 VTD_ROOT_ENTRY
*RootEntryTable
;
81 VTD_EXT_ROOT_ENTRY
*ExtRootEntryTable
;
82 VTD_SECOND_LEVEL_PAGING_ENTRY
*FixedSecondLevelPagingEntry
;
83 BOOLEAN HasDirtyContext
;
84 BOOLEAN HasDirtyPages
;
85 PCI_DEVICE_INFORMATION PciDeviceInfo
;
86 } VTD_UNIT_INFORMATION
;
89 // This is the initial max ACCESS request.
90 // The number may be enlarged later.
92 #define MAX_VTD_ACCESS_REQUEST 0x100
96 VTD_SOURCE_ID SourceId
;
100 } VTD_ACCESS_REQUEST
;
104 The scan bus callback function.
106 It is called in PCI bus scan for each PCI device under the bus.
108 @param[in] Context The context of the callback.
109 @param[in] Segment The segment of the source.
110 @param[in] Bus The bus of the source.
111 @param[in] Device The device of the source.
112 @param[in] Function The function of the source.
114 @retval EFI_SUCCESS The specific PCI device is processed in the callback.
118 (EFIAPI
*SCAN_BUS_FUNC_CALLBACK_FUNC
) (
126 extern EFI_ACPI_DMAR_HEADER
*mAcpiDmarTable
;
128 extern UINTN mVtdUnitNumber
;
129 extern VTD_UNIT_INFORMATION
*mVtdUnitInformation
;
131 extern UINT64 mBelow4GMemoryLimit
;
132 extern UINT64 mAbove4GMemoryLimit
;
134 extern EDKII_PLATFORM_VTD_POLICY_PROTOCOL
*mPlatformVTdPolicy
;
137 Prepare VTD configuration.
145 Setup VTd translation table.
147 @retval EFI_SUCCESS Setup translation table successfully.
148 @retval EFI_OUT_OF_RESOURCE Setup translation table fail.
151 SetupTranslationTable (
156 Enable DMAR translation.
158 @retval EFI_SUCCESS DMAR translation is enabled.
159 @retval EFI_DEVICE_ERROR DMAR translation is not enabled.
167 Disable DMAR translation.
169 @retval EFI_SUCCESS DMAR translation is disabled.
170 @retval EFI_DEVICE_ERROR DMAR translation is not disabled.
178 Invalid VTd global IOTLB.
180 @param[in] VtdIndex The index of VTd engine.
182 @retval EFI_SUCCESS VTd global IOTLB is invalidated.
183 @retval EFI_DEVICE_ERROR VTd global IOTLB is not invalidated.
186 InvalidateVtdIOTLBGlobal (
193 @param[in] VtdIndex The index of VTd engine.
201 Dump VTd registers for all VTd engine.
209 Dump VTd capability registers.
211 @param[in] CapReg The capability register.
215 IN VTD_CAP_REG
*CapReg
219 Dump VTd extended capability registers.
221 @param[in] ECapReg The extended capability register.
225 IN VTD_ECAP_REG
*ECapReg
229 Register PCI device to VTd engine.
231 @param[in] VtdIndex The index of VTd engine.
232 @param[in] Segment The segment of the source.
233 @param[in] SourceId The SourceId of the source.
234 @param[in] DeviceType The DMAR device scope type.
235 @param[in] CheckExist TRUE: ERROR will be returned if the PCI device is already registered.
236 FALSE: SUCCESS will be returned if the PCI device is registered.
238 @retval EFI_SUCCESS The PCI device is registered.
239 @retval EFI_OUT_OF_RESOURCES No enough resource to register a new PCI device.
240 @retval EFI_ALREADY_STARTED The device is already registered.
246 IN VTD_SOURCE_ID SourceId
,
248 IN BOOLEAN CheckExist
252 The scan bus callback function to always enable page attribute.
254 @param[in] Context The context of the callback.
255 @param[in] Segment The segment of the source.
256 @param[in] Bus The bus of the source.
257 @param[in] Device The device of the source.
258 @param[in] Function The function of the source.
260 @retval EFI_SUCCESS The VTd entry is updated to always enable all DMA access for the specific device.
264 ScanBusCallbackRegisterPciDevice (
273 Scan PCI bus and invoke callback function for each PCI devices under the bus.
275 @param[in] Context The context of the callback function.
276 @param[in] Segment The segment of the source.
277 @param[in] Bus The bus of the source.
278 @param[in] Callback The callback function in PCI scan.
280 @retval EFI_SUCCESS The PCI devices under the bus are scaned.
287 IN SCAN_BUS_FUNC_CALLBACK_FUNC Callback
291 Dump the PCI device information managed by this VTd engine.
293 @param[in] VtdIndex The index of VTd engine.
301 Find the VTd index by the Segment and SourceId.
303 @param[in] Segment The segment of the source.
304 @param[in] SourceId The SourceId of the source.
305 @param[out] ExtContextEntry The ExtContextEntry of the source.
306 @param[out] ContextEntry The ContextEntry of the source.
308 @return The index of the VTd engine.
309 @retval (UINTN)-1 The VTd engine is not found.
312 FindVtdIndexByPciDevice (
314 IN VTD_SOURCE_ID SourceId
,
315 OUT VTD_EXT_CONTEXT_ENTRY
**ExtContextEntry
,
316 OUT VTD_CONTEXT_ENTRY
**ContextEntry
320 Get the DMAR ACPI table.
322 @retval EFI_SUCCESS The DMAR ACPI table is got.
323 @retval EFI_ALREADY_STARTED The DMAR ACPI table has been got previously.
324 @retval EFI_NOT_FOUND The DMAR ACPI table is not found.
332 Parse DMAR DRHD table.
334 @return EFI_SUCCESS The DMAR DRHD table is parsed.
337 ParseDmarAcpiTableDrhd (
342 Parse DMAR RMRR table.
344 @return EFI_SUCCESS The DMAR RMRR table is parsed.
347 ParseDmarAcpiTableRmrr (
352 Dump DMAR context entry table.
354 @param[in] RootEntry DMAR root entry.
357 DumpDmarContextEntryTable (
358 IN VTD_ROOT_ENTRY
*RootEntry
362 Dump DMAR extended context entry table.
364 @param[in] ExtRootEntry DMAR extended root entry.
367 DumpDmarExtContextEntryTable (
368 IN VTD_EXT_ROOT_ENTRY
*ExtRootEntry
372 Dump DMAR second level paging entry.
374 @param[in] SecondLevelPagingEntry The second level paging entry.
377 DumpSecondLevelPagingEntry (
378 IN VOID
*SecondLevelPagingEntry
382 Set VTd attribute for a system memory.
384 @param[in] VtdIndex The index used to identify a VTd engine.
385 @param[in] DomainIdentifier The domain ID of the source.
386 @param[in] SecondLevelPagingEntry The second level paging entry in VTd table for the device.
387 @param[in] BaseAddress The base of device memory address to be used as the DMA memory.
388 @param[in] Length The length of device memory address to be used as the DMA memory.
389 @param[in] IoMmuAccess The IOMMU access.
391 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by BaseAddress and Length.
392 @retval EFI_INVALID_PARAMETER BaseAddress is not IoMmu Page size aligned.
393 @retval EFI_INVALID_PARAMETER Length is not IoMmu Page size aligned.
394 @retval EFI_INVALID_PARAMETER Length is 0.
395 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
396 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
397 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by BaseAddress and Length.
398 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
399 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
404 IN UINT16 DomainIdentifier
,
405 IN VTD_SECOND_LEVEL_PAGING_ENTRY
*SecondLevelPagingEntry
,
406 IN UINT64 BaseAddress
,
408 IN UINT64 IoMmuAccess
412 Set VTd attribute for a system memory.
414 @param[in] Segment The Segment used to identify a VTd engine.
415 @param[in] SourceId The SourceId used to identify a VTd engine and table entry.
416 @param[in] BaseAddress The base of device memory address to be used as the DMA memory.
417 @param[in] Length The length of device memory address to be used as the DMA memory.
418 @param[in] IoMmuAccess The IOMMU access.
420 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by BaseAddress and Length.
421 @retval EFI_INVALID_PARAMETER BaseAddress is not IoMmu Page size aligned.
422 @retval EFI_INVALID_PARAMETER Length is not IoMmu Page size aligned.
423 @retval EFI_INVALID_PARAMETER Length is 0.
424 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
425 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
426 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by BaseAddress and Length.
427 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
428 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
433 IN VTD_SOURCE_ID SourceId
,
434 IN UINT64 BaseAddress
,
436 IN UINT64 IoMmuAccess
440 Return the index of PCI data.
442 @param[in] VtdIndex The index used to identify a VTd engine.
443 @param[in] Segment The Segment used to identify a VTd engine.
444 @param[in] SourceId The SourceId used to identify a VTd engine and table entry.
446 @return The index of the PCI data.
447 @retval (UINTN)-1 The PCI data is not found.
453 IN VTD_SOURCE_ID SourceId
457 Dump VTd registers if there is error.
465 Initialize platform VTd policy.
468 InitializePlatformVTdPolicy (
473 Always enable the VTd page attribute for the device.
475 @param[in] Segment The Segment used to identify a VTd engine.
476 @param[in] SourceId The SourceId used to identify a VTd engine and table entry.
478 @retval EFI_SUCCESS The VTd entry is updated to always enable all DMA access for the specific device.
481 AlwaysEnablePageAttribute (
483 IN VTD_SOURCE_ID SourceId
487 Convert the DeviceHandle to SourceId and Segment.
489 @param[in] DeviceHandle The device who initiates the DMA access request.
490 @param[out] Segment The Segment used to identify a VTd engine.
491 @param[out] SourceId The SourceId used to identify a VTd engine and table entry.
493 @retval EFI_SUCCESS The Segment and SourceId are returned.
494 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
495 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
498 DeviceHandleToSourceId (
499 IN EFI_HANDLE DeviceHandle
,
501 OUT VTD_SOURCE_ID
*SourceId
505 Get device information from mapping.
507 @param[in] Mapping The mapping.
508 @param[out] DeviceAddress The device address of the mapping.
509 @param[out] NumberOfPages The number of pages of the mapping.
511 @retval EFI_SUCCESS The device information is returned.
512 @retval EFI_INVALID_PARAMETER The mapping is invalid.
515 GetDeviceInfoFromMapping (
517 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
518 OUT UINTN
*NumberOfPages
522 Initialize DMA protection.
525 InitializeDmaProtection (
532 @param[in] Pages the number of pages.
534 @return the page address.
535 @retval NULL No resource to allocate pages.
544 Flush VTD page table and context table memory.
546 This action is to make sure the IOMMU engine can get final data in memory.
548 @param[in] VtdIndex The index used to identify a VTd engine.
549 @param[in] Base The base address of memory to be flushed.
550 @param[in] Size The size of memory in bytes to be flushed.
553 FlushPageTableMemory (
560 Get PCI device information from DMAR DevScopeEntry.
562 @param[in] Segment The segment number.
563 @param[in] DmarDevScopeEntry DMAR DevScopeEntry
564 @param[out] Bus The bus number.
565 @param[out] Device The device number.
566 @param[out] Function The function number.
568 @retval EFI_SUCCESS The PCI device information is returned.
571 GetPciBusDeviceFunction (
573 IN EFI_ACPI_DMAR_DEVICE_SCOPE_STRUCTURE_HEADER
*DmarDevScopeEntry
,
580 Append VTd Access Request to global.
582 @param[in] Segment The Segment used to identify a VTd engine.
583 @param[in] SourceId The SourceId used to identify a VTd engine and table entry.
584 @param[in] BaseAddress The base of device memory address to be used as the DMA memory.
585 @param[in] Length The length of device memory address to be used as the DMA memory.
586 @param[in] IoMmuAccess The IOMMU access.
588 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by BaseAddress and Length.
589 @retval EFI_INVALID_PARAMETER BaseAddress is not IoMmu Page size aligned.
590 @retval EFI_INVALID_PARAMETER Length is not IoMmu Page size aligned.
591 @retval EFI_INVALID_PARAMETER Length is 0.
592 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
593 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
594 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by BaseAddress and Length.
595 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
596 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
600 RequestAccessAttribute (
602 IN VTD_SOURCE_ID SourceId
,
603 IN UINT64 BaseAddress
,
605 IN UINT64 IoMmuAccess