+++ /dev/null
-/** @file\r
-\r
- Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>\r
- This program and the accompanying materials\r
- are licensed and made available under the terms and conditions of the BSD License\r
- which accompanies this distribution. The full text of the license may be found at\r
- http://opensource.org/licenses/bsd-license.php.\r
-\r
- THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#include "DmaProtection.h"\r
-\r
-/**\r
- Return the index of PCI data.\r
-\r
- @param[in] VtdIndex The index used to identify a VTd engine.\r
- @param[in] Segment The Segment used to identify a VTd engine.\r
- @param[in] SourceId The SourceId used to identify a VTd engine and table entry.\r
-\r
- @return The index of the PCI data.\r
- @retval (UINTN)-1 The PCI data is not found.\r
-**/\r
-UINTN\r
-GetPciDataIndex (\r
- IN UINTN VtdIndex,\r
- IN UINT16 Segment,\r
- IN VTD_SOURCE_ID SourceId\r
- )\r
-{\r
- UINTN Index;\r
- VTD_SOURCE_ID *PciSourceId;\r
-\r
- if (Segment != mVtdUnitInformation[VtdIndex].Segment) {\r
- return (UINTN)-1;\r
- }\r
-\r
- for (Index = 0; Index < mVtdUnitInformation[VtdIndex].PciDeviceInfo.PciDeviceDataNumber; Index++) {\r
- PciSourceId = &mVtdUnitInformation[VtdIndex].PciDeviceInfo.PciDeviceData[Index].PciSourceId;\r
- if ((PciSourceId->Bits.Bus == SourceId.Bits.Bus) &&\r
- (PciSourceId->Bits.Device == SourceId.Bits.Device) &&\r
- (PciSourceId->Bits.Function == SourceId.Bits.Function) ) {\r
- return Index;\r
- }\r
- }\r
-\r
- return (UINTN)-1;\r
-}\r
-\r
-/**\r
- Register PCI device to VTd engine.\r
-\r
- @param[in] VtdIndex The index of VTd engine.\r
- @param[in] Segment The segment of the source.\r
- @param[in] SourceId The SourceId of the source.\r
- @param[in] DeviceType The DMAR device scope type.\r
- @param[in] CheckExist TRUE: ERROR will be returned if the PCI device is already registered.\r
- FALSE: SUCCESS will be returned if the PCI device is registered.\r
-\r
- @retval EFI_SUCCESS The PCI device is registered.\r
- @retval EFI_OUT_OF_RESOURCES No enough resource to register a new PCI device.\r
- @retval EFI_ALREADY_STARTED The device is already registered.\r
-**/\r
-EFI_STATUS\r
-RegisterPciDevice (\r
- IN UINTN VtdIndex,\r
- IN UINT16 Segment,\r
- IN VTD_SOURCE_ID SourceId,\r
- IN UINT8 DeviceType,\r
- IN BOOLEAN CheckExist\r
- )\r
-{\r
- PCI_DEVICE_INFORMATION *PciDeviceInfo;\r
- VTD_SOURCE_ID *PciSourceId;\r
- UINTN PciDataIndex;\r
- UINTN Index;\r
- PCI_DEVICE_DATA *NewPciDeviceData;\r
- EDKII_PLATFORM_VTD_PCI_DEVICE_ID *PciDeviceId;\r
-\r
- PciDeviceInfo = &mVtdUnitInformation[VtdIndex].PciDeviceInfo;\r
-\r
- if (PciDeviceInfo->IncludeAllFlag) {\r
- //\r
- // Do not register device in other VTD Unit\r
- //\r
- for (Index = 0; Index < VtdIndex; Index++) {\r
- PciDataIndex = GetPciDataIndex (Index, Segment, SourceId);\r
- if (PciDataIndex != (UINTN)-1) {\r
- DEBUG ((DEBUG_INFO, " RegisterPciDevice: PCI S%04x B%02x D%02x F%02x already registered by Other Vtd(%d)\n", Segment, SourceId.Bits.Bus, SourceId.Bits.Device, SourceId.Bits.Function, Index));\r
- return EFI_SUCCESS;\r
- }\r
- }\r
- }\r
-\r
- PciDataIndex = GetPciDataIndex (VtdIndex, Segment, SourceId);\r
- if (PciDataIndex == (UINTN)-1) {\r
- //\r
- // Register new\r
- //\r
-\r
- if (PciDeviceInfo->PciDeviceDataNumber >= PciDeviceInfo->PciDeviceDataMaxNumber) {\r
- //\r
- // Reallocate\r
- //\r
- NewPciDeviceData = AllocateZeroPool (sizeof(*NewPciDeviceData) * (PciDeviceInfo->PciDeviceDataMaxNumber + MAX_VTD_PCI_DATA_NUMBER));\r
- if (NewPciDeviceData == NULL) {\r
- return EFI_OUT_OF_RESOURCES;\r
- }\r
- PciDeviceInfo->PciDeviceDataMaxNumber += MAX_VTD_PCI_DATA_NUMBER;\r
- if (PciDeviceInfo->PciDeviceData != NULL) {\r
- CopyMem (NewPciDeviceData, PciDeviceInfo->PciDeviceData, sizeof(*NewPciDeviceData) * PciDeviceInfo->PciDeviceDataNumber);\r
- FreePool (PciDeviceInfo->PciDeviceData);\r
- }\r
- PciDeviceInfo->PciDeviceData = NewPciDeviceData;\r
- }\r
-\r
- ASSERT (PciDeviceInfo->PciDeviceDataNumber < PciDeviceInfo->PciDeviceDataMaxNumber);\r
-\r
- PciSourceId = &PciDeviceInfo->PciDeviceData[PciDeviceInfo->PciDeviceDataNumber].PciSourceId;\r
- PciSourceId->Bits.Bus = SourceId.Bits.Bus;\r
- PciSourceId->Bits.Device = SourceId.Bits.Device;\r
- PciSourceId->Bits.Function = SourceId.Bits.Function;\r
-\r
- DEBUG ((DEBUG_INFO, " RegisterPciDevice: PCI S%04x B%02x D%02x F%02x", Segment, SourceId.Bits.Bus, SourceId.Bits.Device, SourceId.Bits.Function));\r
-\r
- PciDeviceId = &PciDeviceInfo->PciDeviceData[PciDeviceInfo->PciDeviceDataNumber].PciDeviceId;\r
- if ((DeviceType == EFI_ACPI_DEVICE_SCOPE_ENTRY_TYPE_PCI_ENDPOINT) ||\r
- (DeviceType == EFI_ACPI_DEVICE_SCOPE_ENTRY_TYPE_PCI_BRIDGE)) {\r
- PciDeviceId->VendorId = PciSegmentRead16 (PCI_SEGMENT_LIB_ADDRESS(Segment, SourceId.Bits.Bus, SourceId.Bits.Device, SourceId.Bits.Function, PCI_VENDOR_ID_OFFSET));\r
- PciDeviceId->DeviceId = PciSegmentRead16 (PCI_SEGMENT_LIB_ADDRESS(Segment, SourceId.Bits.Bus, SourceId.Bits.Device, SourceId.Bits.Function, PCI_DEVICE_ID_OFFSET));\r
- PciDeviceId->RevisionId = PciSegmentRead8 (PCI_SEGMENT_LIB_ADDRESS(Segment, SourceId.Bits.Bus, SourceId.Bits.Device, SourceId.Bits.Function, PCI_REVISION_ID_OFFSET));\r
-\r
- DEBUG ((DEBUG_INFO, " (%04x:%04x:%02x", PciDeviceId->VendorId, PciDeviceId->DeviceId, PciDeviceId->RevisionId));\r
-\r
- if (DeviceType == EFI_ACPI_DEVICE_SCOPE_ENTRY_TYPE_PCI_ENDPOINT) {\r
- PciDeviceId->SubsystemVendorId = PciSegmentRead16 (PCI_SEGMENT_LIB_ADDRESS(Segment, SourceId.Bits.Bus, SourceId.Bits.Device, SourceId.Bits.Function, PCI_SUBSYSTEM_VENDOR_ID_OFFSET));\r
- PciDeviceId->SubsystemDeviceId = PciSegmentRead16 (PCI_SEGMENT_LIB_ADDRESS(Segment, SourceId.Bits.Bus, SourceId.Bits.Device, SourceId.Bits.Function, PCI_SUBSYSTEM_ID_OFFSET));\r
- DEBUG ((DEBUG_INFO, ":%04x:%04x", PciDeviceId->SubsystemVendorId, PciDeviceId->SubsystemDeviceId));\r
- }\r
- DEBUG ((DEBUG_INFO, ")"));\r
- }\r
-\r
- PciDeviceInfo->PciDeviceData[PciDeviceInfo->PciDeviceDataNumber].DeviceType = DeviceType;\r
-\r
- if ((DeviceType != EFI_ACPI_DEVICE_SCOPE_ENTRY_TYPE_PCI_ENDPOINT) &&\r
- (DeviceType != EFI_ACPI_DEVICE_SCOPE_ENTRY_TYPE_PCI_BRIDGE)) {\r
- DEBUG ((DEBUG_INFO, " (*)"));\r
- }\r
- DEBUG ((DEBUG_INFO, "\n"));\r
-\r
- PciDeviceInfo->PciDeviceDataNumber++;\r
- } else {\r
- if (CheckExist) {\r
- DEBUG ((DEBUG_INFO, " RegisterPciDevice: PCI S%04x B%02x D%02x F%02x already registered\n", Segment, SourceId.Bits.Bus, SourceId.Bits.Device, SourceId.Bits.Function));\r
- return EFI_ALREADY_STARTED;\r
- }\r
- }\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
- The scan bus callback function to register PCI device.\r
-\r
- @param[in] Context The context of the callback.\r
- @param[in] Segment The segment of the source.\r
- @param[in] Bus The bus of the source.\r
- @param[in] Device The device of the source.\r
- @param[in] Function The function of the source.\r
-\r
- @retval EFI_SUCCESS The PCI device is registered.\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-ScanBusCallbackRegisterPciDevice (\r
- IN VOID *Context,\r
- IN UINT16 Segment,\r
- IN UINT8 Bus,\r
- IN UINT8 Device,\r
- IN UINT8 Function\r
- )\r
-{\r
- VTD_SOURCE_ID SourceId;\r
- UINTN VtdIndex;\r
- UINT8 BaseClass;\r
- UINT8 SubClass;\r
- UINT8 DeviceType;\r
- EFI_STATUS Status;\r
-\r
- VtdIndex = (UINTN)Context;\r
- SourceId.Bits.Bus = Bus;\r
- SourceId.Bits.Device = Device;\r
- SourceId.Bits.Function = Function;\r
-\r
- DeviceType = EFI_ACPI_DEVICE_SCOPE_ENTRY_TYPE_PCI_ENDPOINT;\r
- BaseClass = PciSegmentRead8 (PCI_SEGMENT_LIB_ADDRESS(Segment, Bus, Device, Function, PCI_CLASSCODE_OFFSET + 2));\r
- if (BaseClass == PCI_CLASS_BRIDGE) {\r
- SubClass = PciSegmentRead8 (PCI_SEGMENT_LIB_ADDRESS(Segment, Bus, Device, Function, PCI_CLASSCODE_OFFSET + 1));\r
- if (SubClass == PCI_CLASS_BRIDGE_P2P) {\r
- DeviceType = EFI_ACPI_DEVICE_SCOPE_ENTRY_TYPE_PCI_BRIDGE;\r
- }\r
- }\r
-\r
- Status = RegisterPciDevice (VtdIndex, Segment, SourceId, DeviceType, FALSE);\r
- return Status;\r
-}\r
-\r
-/**\r
- Scan PCI bus and invoke callback function for each PCI devices under the bus.\r
-\r
- @param[in] Context The context of the callback function.\r
- @param[in] Segment The segment of the source.\r
- @param[in] Bus The bus of the source.\r
- @param[in] Callback The callback function in PCI scan.\r
-\r
- @retval EFI_SUCCESS The PCI devices under the bus are scaned.\r
-**/\r
-EFI_STATUS\r
-ScanPciBus (\r
- IN VOID *Context,\r
- IN UINT16 Segment,\r
- IN UINT8 Bus,\r
- IN SCAN_BUS_FUNC_CALLBACK_FUNC Callback\r
- )\r
-{\r
- UINT8 Device;\r
- UINT8 Function;\r
- UINT8 SecondaryBusNumber;\r
- UINT8 HeaderType;\r
- UINT8 BaseClass;\r
- UINT8 SubClass;\r
- UINT32 MaxFunction;\r
- UINT16 VendorID;\r
- UINT16 DeviceID;\r
- EFI_STATUS Status;\r
-\r
- // Scan the PCI bus for devices\r
- for (Device = 0; Device < PCI_MAX_DEVICE + 1; Device++) {\r
- HeaderType = PciSegmentRead8 (PCI_SEGMENT_LIB_ADDRESS(Segment, Bus, Device, 0, PCI_HEADER_TYPE_OFFSET));\r
- MaxFunction = PCI_MAX_FUNC + 1;\r
- if ((HeaderType & HEADER_TYPE_MULTI_FUNCTION) == 0x00) {\r
- MaxFunction = 1;\r
- }\r
- for (Function = 0; Function < MaxFunction; Function++) {\r
- VendorID = PciSegmentRead16 (PCI_SEGMENT_LIB_ADDRESS(Segment, Bus, Device, Function, PCI_VENDOR_ID_OFFSET));\r
- DeviceID = PciSegmentRead16 (PCI_SEGMENT_LIB_ADDRESS(Segment, Bus, Device, Function, PCI_DEVICE_ID_OFFSET));\r
- if (VendorID == 0xFFFF && DeviceID == 0xFFFF) {\r
- continue;\r
- }\r
-\r
- Status = Callback (Context, Segment, Bus, Device, Function);\r
- if (EFI_ERROR (Status)) {\r
- return Status;\r
- }\r
-\r
- BaseClass = PciSegmentRead8 (PCI_SEGMENT_LIB_ADDRESS(Segment, Bus, Device, Function, PCI_CLASSCODE_OFFSET + 2));\r
- if (BaseClass == PCI_CLASS_BRIDGE) {\r
- SubClass = PciSegmentRead8 (PCI_SEGMENT_LIB_ADDRESS(Segment, Bus, Device, Function, PCI_CLASSCODE_OFFSET + 1));\r
- if (SubClass == PCI_CLASS_BRIDGE_P2P) {\r
- SecondaryBusNumber = PciSegmentRead8 (PCI_SEGMENT_LIB_ADDRESS(Segment, Bus, Device, Function, PCI_BRIDGE_SECONDARY_BUS_REGISTER_OFFSET));\r
- DEBUG ((DEBUG_INFO," ScanPciBus: PCI bridge S%04x B%02x D%02x F%02x (SecondBus:%02x)\n", Segment, Bus, Device, Function, SecondaryBusNumber));\r
- if (SecondaryBusNumber != 0) {\r
- Status = ScanPciBus (Context, Segment, SecondaryBusNumber, Callback);\r
- if (EFI_ERROR (Status)) {\r
- return Status;\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
-\r
- return EFI_SUCCESS;\r
-}\r
-\r
-/**\r
- Dump the PCI device information managed by this VTd engine.\r
-\r
- @param[in] VtdIndex The index of VTd engine.\r
-**/\r
-VOID\r
-DumpPciDeviceInfo (\r
- IN UINTN VtdIndex\r
- )\r
-{\r
- UINTN Index;\r
-\r
- DEBUG ((DEBUG_INFO,"PCI Device Information (Number 0x%x, IncludeAll - %d):\n",\r
- mVtdUnitInformation[VtdIndex].PciDeviceInfo.PciDeviceDataNumber,\r
- mVtdUnitInformation[VtdIndex].PciDeviceInfo.IncludeAllFlag\r
- ));\r
- for (Index = 0; Index < mVtdUnitInformation[VtdIndex].PciDeviceInfo.PciDeviceDataNumber; Index++) {\r
- DEBUG ((DEBUG_INFO," S%04x B%02x D%02x F%02x\n",\r
- mVtdUnitInformation[VtdIndex].Segment,\r
- mVtdUnitInformation[VtdIndex].PciDeviceInfo.PciDeviceData[Index].PciSourceId.Bits.Bus,\r
- mVtdUnitInformation[VtdIndex].PciDeviceInfo.PciDeviceData[Index].PciSourceId.Bits.Device,\r
- mVtdUnitInformation[VtdIndex].PciDeviceInfo.PciDeviceData[Index].PciSourceId.Bits.Function\r
- ));\r
- }\r
-}\r
-\r
-/**\r
- Find the VTd index by the Segment and SourceId.\r
-\r
- @param[in] Segment The segment of the source.\r
- @param[in] SourceId The SourceId of the source.\r
- @param[out] ExtContextEntry The ExtContextEntry of the source.\r
- @param[out] ContextEntry The ContextEntry of the source.\r
-\r
- @return The index of the VTd engine.\r
- @retval (UINTN)-1 The VTd engine is not found.\r
-**/\r
-UINTN\r
-FindVtdIndexByPciDevice (\r
- IN UINT16 Segment,\r
- IN VTD_SOURCE_ID SourceId,\r
- OUT VTD_EXT_CONTEXT_ENTRY **ExtContextEntry,\r
- OUT VTD_CONTEXT_ENTRY **ContextEntry\r
- )\r
-{\r
- UINTN VtdIndex;\r
- VTD_ROOT_ENTRY *RootEntry;\r
- VTD_CONTEXT_ENTRY *ContextEntryTable;\r
- VTD_CONTEXT_ENTRY *ThisContextEntry;\r
- VTD_EXT_ROOT_ENTRY *ExtRootEntry;\r
- VTD_EXT_CONTEXT_ENTRY *ExtContextEntryTable;\r
- VTD_EXT_CONTEXT_ENTRY *ThisExtContextEntry;\r
- UINTN PciDataIndex;\r
-\r
- for (VtdIndex = 0; VtdIndex < mVtdUnitNumber; VtdIndex++) {\r
- if (Segment != mVtdUnitInformation[VtdIndex].Segment) {\r
- continue;\r
- }\r
-\r
- PciDataIndex = GetPciDataIndex (VtdIndex, Segment, SourceId);\r
- if (PciDataIndex == (UINTN)-1) {\r
- continue;\r
- }\r
-\r
-// DEBUG ((DEBUG_INFO,"FindVtdIndex(0x%x) for S%04x B%02x D%02x F%02x\n", VtdIndex, Segment, SourceId.Bits.Bus, SourceId.Bits.Device, SourceId.Bits.Function));\r
-\r
- if (mVtdUnitInformation[VtdIndex].ExtRootEntryTable != 0) {\r
- ExtRootEntry = &mVtdUnitInformation[VtdIndex].ExtRootEntryTable[SourceId.Index.RootIndex];\r
- ExtContextEntryTable = (VTD_EXT_CONTEXT_ENTRY *)(UINTN)VTD_64BITS_ADDRESS(ExtRootEntry->Bits.LowerContextTablePointerLo, ExtRootEntry->Bits.LowerContextTablePointerHi) ;\r
- ThisExtContextEntry = &ExtContextEntryTable[SourceId.Index.ContextIndex];\r
- if (ThisExtContextEntry->Bits.AddressWidth == 0) {\r
- continue;\r
- }\r
- *ExtContextEntry = ThisExtContextEntry;\r
- *ContextEntry = NULL;\r
- } else {\r
- RootEntry = &mVtdUnitInformation[VtdIndex].RootEntryTable[SourceId.Index.RootIndex];\r
- ContextEntryTable = (VTD_CONTEXT_ENTRY *)(UINTN)VTD_64BITS_ADDRESS(RootEntry->Bits.ContextTablePointerLo, RootEntry->Bits.ContextTablePointerHi) ;\r
- ThisContextEntry = &ContextEntryTable[SourceId.Index.ContextIndex];\r
- if (ThisContextEntry->Bits.AddressWidth == 0) {\r
- continue;\r
- }\r
- *ExtContextEntry = NULL;\r
- *ContextEntry = ThisContextEntry;\r
- }\r
-\r
- return VtdIndex;\r
- }\r
-\r
- return (UINTN)-1;\r
-}\r
-\r