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/AcpiSystemDescriptionTable.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)))
53 // This is the initial max PCI descriptor.
54 // The number may be enlarged later.
56 #define MAX_PCI_DESCRIPTORS 0x100
59 BOOLEAN IncludeAllFlag
;
60 UINTN PciDescriptorNumber
;
61 UINTN PciDescriptorMaxNumber
;
62 BOOLEAN
*IsRealPciDevice
;
63 VTD_SOURCE_ID
*PciDescriptors
;
64 // for statistic analysis
66 } PCI_DEVICE_INFORMATION
;
69 UINTN VtdUnitBaseAddress
;
73 VTD_ROOT_ENTRY
*RootEntryTable
;
74 VTD_EXT_ROOT_ENTRY
*ExtRootEntryTable
;
75 VTD_SECOND_LEVEL_PAGING_ENTRY
*FixedSecondLevelPagingEntry
;
76 BOOLEAN HasDirtyContext
;
77 BOOLEAN HasDirtyPages
;
78 PCI_DEVICE_INFORMATION PciDeviceInfo
;
79 } VTD_UNIT_INFORMATION
;
81 extern EFI_ACPI_DMAR_HEADER
*mAcpiDmarTable
;
83 extern UINT64 mVtdHostAddressWidthMask
;
84 extern UINTN mVtdUnitNumber
;
85 extern VTD_UNIT_INFORMATION
*mVtdUnitInformation
;
87 extern UINT64 mBelow4GMemoryLimit
;
88 extern UINT64 mAbove4GMemoryLimit
;
90 extern EDKII_PLATFORM_VTD_POLICY_PROTOCOL
*mPlatformVTdPolicy
;
93 Prepare VTD configuration.
101 Setup VTd translation table.
103 @retval EFI_SUCCESS Setup translation table successfully.
104 @retval EFI_OUT_OF_RESOURCE Setup translation table fail.
107 SetupTranslationTable (
112 Enable DMAR translation.
114 @retval EFI_SUCCESS DMAR translation is enabled.
115 @retval EFI_DEVICE_ERROR DMAR translation is not enabled.
123 Disable DMAR translation.
125 @retval EFI_SUCCESS DMAR translation is disabled.
126 @retval EFI_DEVICE_ERROR DMAR translation is not disabled.
134 Invalid VTd global IOTLB.
136 @param[in] VtdIndex The index of VTd engine.
138 @retval EFI_SUCCESS VTd global IOTLB is invalidated.
139 @retval EFI_DEVICE_ERROR VTd global IOTLB is not invalidated.
142 InvalidateVtdIOTLBGlobal (
149 @param[in] VtdIndex The index of VTd engine.
157 Dump VTd registers for all VTd engine.
165 Dump VTd capability registers.
167 @param[in] CapReg The capability register.
171 IN VTD_CAP_REG
*CapReg
175 Dump VTd extended capability registers.
177 @param[in] ECapReg The extended capability register.
181 IN VTD_ECAP_REG
*ECapReg
185 Register PCI device to VTd engine as PCI descriptor.
187 @param[in] VtdIndex The index of VTd engine.
188 @param[in] Segment The segment of the source.
189 @param[in] SourceId The SourceId of the source.
190 @param[in] IsRealPciDevice TRUE: It is a real PCI device.
191 FALSE: It is not a real PCI device.
192 @param[in] CheckExist TRUE: ERROR will be returned if the PCI device is already registered.
193 FALSE: SUCCESS will be returned if the PCI device is registered.
195 @retval EFI_SUCCESS The PCI device is registered.
196 @retval EFI_OUT_OF_RESOURCES No enough resource to register a new PCI device.
197 @retval EFI_ALREADY_STARTED The device is already registered.
203 IN VTD_SOURCE_ID SourceId
,
204 IN BOOLEAN IsRealPciDevice
,
205 IN BOOLEAN CheckExist
209 Scan PCI bus and register PCI devices under the bus.
211 @param[in] VtdIndex The index of VTd engine.
212 @param[in] Segment The segment of the source.
213 @param[in] Bus The bus of the source.
215 @retval EFI_SUCCESS The PCI devices under the bus are registered.
216 @retval EFI_OUT_OF_RESOURCES No enough resource to register a new PCI device.
226 Dump the PCI device information managed by this VTd engine.
228 @param[in] VtdIndex The index of VTd engine.
236 Find the VTd index by the Segment and SourceId.
238 @param[in] Segment The segment of the source.
239 @param[in] SourceId The SourceId of the source.
240 @param[out] ExtContextEntry The ExtContextEntry of the source.
241 @param[out] ContextEntry The ContextEntry of the source.
243 @return The index of the PCI descriptor.
244 @retval (UINTN)-1 The PCI descriptor is not found.
247 FindVtdIndexByPciDevice (
249 IN VTD_SOURCE_ID SourceId
,
250 OUT VTD_EXT_CONTEXT_ENTRY
**ExtContextEntry
,
251 OUT VTD_CONTEXT_ENTRY
**ContextEntry
255 Get the DMAR ACPI table.
257 @retval EFI_SUCCESS The DMAR ACPI table is got.
258 @retval EFI_NOT_FOUND The DMAR ACPI table is not found.
266 Parse DMAR DRHD table.
268 @return EFI_SUCCESS The DMAR DRHD table is parsed.
271 ParseDmarAcpiTableDrhd (
276 Parse DMAR RMRR table.
278 @return EFI_SUCCESS The DMAR RMRR table is parsed.
281 ParseDmarAcpiTableRmrr (
286 Dump DMAR context entry table.
288 @param[in] RootEntry DMAR root entry.
291 DumpDmarContextEntryTable (
292 IN VTD_ROOT_ENTRY
*RootEntry
296 Dump DMAR extended context entry table.
298 @param[in] ExtRootEntry DMAR extended root entry.
301 DumpDmarExtContextEntryTable (
302 IN VTD_EXT_ROOT_ENTRY
*ExtRootEntry
306 Dump DMAR second level paging entry.
308 @param[in] SecondLevelPagingEntry The second level paging entry.
311 DumpSecondLevelPagingEntry (
312 IN VOID
*SecondLevelPagingEntry
316 Set VTd attribute for a system memory.
318 @param[in] VtdIndex The index used to identify a VTd engine.
319 @param[in] SecondLevelPagingEntry The second level paging entry in VTd table for the device.
320 @param[in] BaseAddress The base of device memory address to be used as the DMA memory.
321 @param[in] Length The length of device memory address to be used as the DMA memory.
322 @param[in] IoMmuAccess The IOMMU access.
324 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by BaseAddress and Length.
325 @retval EFI_INVALID_PARAMETER BaseAddress is not IoMmu Page size aligned.
326 @retval EFI_INVALID_PARAMETER Length is not IoMmu Page size aligned.
327 @retval EFI_INVALID_PARAMETER Length is 0.
328 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
329 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
330 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by BaseAddress and Length.
331 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
332 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
337 IN UINT16 DomainIdentifier
,
338 IN VTD_SECOND_LEVEL_PAGING_ENTRY
*SecondLevelPagingEntry
,
339 IN UINT64 BaseAddress
,
341 IN UINT64 IoMmuAccess
345 Set VTd attribute for a system memory.
347 @param[in] Segment The Segment used to identify a VTd engine.
348 @param[in] SourceId The SourceId used to identify a VTd engine and table entry.
349 @param[in] BaseAddress The base of device memory address to be used as the DMA memory.
350 @param[in] Length The length of device memory address to be used as the DMA memory.
351 @param[in] IoMmuAccess The IOMMU access.
353 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by BaseAddress and Length.
354 @retval EFI_INVALID_PARAMETER BaseAddress is not IoMmu Page size aligned.
355 @retval EFI_INVALID_PARAMETER Length is not IoMmu Page size aligned.
356 @retval EFI_INVALID_PARAMETER Length is 0.
357 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
358 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
359 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by BaseAddress and Length.
360 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
361 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
366 IN VTD_SOURCE_ID SourceId
,
367 IN UINT64 BaseAddress
,
369 IN UINT64 IoMmuAccess
373 Return the index of PCI descriptor.
375 @param[in] VtdIndex The index used to identify a VTd engine.
376 @param[in] Segment The Segment used to identify a VTd engine.
377 @param[in] SourceId The SourceId used to identify a VTd engine and table entry.
379 @return The index of the PCI descriptor.
380 @retval (UINTN)-1 The PCI descriptor is not found.
386 IN VTD_SOURCE_ID SourceId
390 Dump VTd registers if there is error.
398 Initialize platform VTd policy.
401 InitializePlatformVTdPolicy (
406 Always enable the VTd page attribute for the device.
408 @param[in] Segment The Segment used to identify a VTd engine.
409 @param[in] SourceId The SourceId used to identify a VTd engine and table entry.
411 @retval EFI_SUCCESS The VTd entry is updated to always enable all DMA access for the specific device.
414 AlwaysEnablePageAttribute (
416 IN VTD_SOURCE_ID SourceId
420 Convert the DeviceHandle to SourceId and Segment.
422 @param[in] DeviceHandle The device who initiates the DMA access request.
423 @param[out] Segment The Segment used to identify a VTd engine.
424 @param[out] SourceId The SourceId used to identify a VTd engine and table entry.
426 @retval EFI_SUCCESS The Segment and SourceId are returned.
427 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
428 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
431 DeviceHandleToSourceId (
432 IN EFI_HANDLE DeviceHandle
,
434 OUT VTD_SOURCE_ID
*SourceId
438 Get device information from mapping.
440 @param[in] Mapping The mapping.
441 @param[out] DeviceAddress The device address of the mapping.
442 @param[out] NumberOfPages The number of pages of the mapping.
444 @retval EFI_SUCCESS The device information is returned.
445 @retval EFI_INVALID_PARAMETER The mapping is invalid.
448 GetDeviceInfoFromMapping (
450 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
451 OUT UINTN
*NumberOfPages
455 Initialize DMA protection.
458 InitializeDmaProtection (
465 @param[in] Pages the number of pages.
467 @return the page address.
468 @retval NULL No resource to allocate pages.
477 Flush VTD page table and context table memory.
479 This action is to make sure the IOMMU engine can get final data in memory.
481 @param[in] VtdIndex The index used to identify a VTd engine.
482 @param[in] Base The base address of memory to be flushed.
483 @param[in] Size The size of memory in bytes to be flushed.
486 FlushPageTableMemory (