]> git.proxmox.com Git - mirror_edk2.git/blob - IntelSiliconPkg/Feature/VTd/IntelVTdDxe/TranslationTableEx.c
IntelSiliconPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / IntelSiliconPkg / Feature / VTd / IntelVTdDxe / TranslationTableEx.c
1 /** @file
2
3 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
5
6 **/
7
8 #include "DmaProtection.h"
9
10 /**
11 Create extended context entry.
12
13 @param[in] VtdIndex The index of the VTd engine.
14
15 @retval EFI_SUCCESS The extended context entry is created.
16 @retval EFI_OUT_OF_RESOURCE No enough resource to create extended context entry.
17 **/
18 EFI_STATUS
19 CreateExtContextEntry (
20 IN UINTN VtdIndex
21 )
22 {
23 UINTN Index;
24 VOID *Buffer;
25 UINTN RootPages;
26 UINTN ContextPages;
27 VTD_EXT_ROOT_ENTRY *ExtRootEntry;
28 VTD_EXT_CONTEXT_ENTRY *ExtContextEntryTable;
29 VTD_EXT_CONTEXT_ENTRY *ExtContextEntry;
30 VTD_SOURCE_ID *PciSourceId;
31 VTD_SOURCE_ID SourceId;
32 UINTN MaxBusNumber;
33 UINTN EntryTablePages;
34
35 MaxBusNumber = 0;
36 for (Index = 0; Index < mVtdUnitInformation[VtdIndex].PciDeviceInfo.PciDeviceDataNumber; Index++) {
37 PciSourceId = &mVtdUnitInformation[VtdIndex].PciDeviceInfo.PciDeviceData[Index].PciSourceId;
38 if (PciSourceId->Bits.Bus > MaxBusNumber) {
39 MaxBusNumber = PciSourceId->Bits.Bus;
40 }
41 }
42 DEBUG ((DEBUG_INFO," MaxBusNumber - 0x%x\n", MaxBusNumber));
43
44 RootPages = EFI_SIZE_TO_PAGES (sizeof (VTD_EXT_ROOT_ENTRY) * VTD_ROOT_ENTRY_NUMBER);
45 ContextPages = EFI_SIZE_TO_PAGES (sizeof (VTD_EXT_CONTEXT_ENTRY) * VTD_CONTEXT_ENTRY_NUMBER);
46 EntryTablePages = RootPages + ContextPages * (MaxBusNumber + 1);
47 Buffer = AllocateZeroPages (EntryTablePages);
48 if (Buffer == NULL) {
49 DEBUG ((DEBUG_INFO,"Could not Alloc Root Entry Table.. \n"));
50 return EFI_OUT_OF_RESOURCES;
51 }
52 mVtdUnitInformation[VtdIndex].ExtRootEntryTable = (VTD_EXT_ROOT_ENTRY *)Buffer;
53 Buffer = (UINT8 *)Buffer + EFI_PAGES_TO_SIZE (RootPages);
54
55 for (Index = 0; Index < mVtdUnitInformation[VtdIndex].PciDeviceInfo.PciDeviceDataNumber; Index++) {
56 PciSourceId = &mVtdUnitInformation[VtdIndex].PciDeviceInfo.PciDeviceData[Index].PciSourceId;
57
58 SourceId.Bits.Bus = PciSourceId->Bits.Bus;
59 SourceId.Bits.Device = PciSourceId->Bits.Device;
60 SourceId.Bits.Function = PciSourceId->Bits.Function;
61
62 ExtRootEntry = &mVtdUnitInformation[VtdIndex].ExtRootEntryTable[SourceId.Index.RootIndex];
63 if (ExtRootEntry->Bits.LowerPresent == 0) {
64 ExtRootEntry->Bits.LowerContextTablePointerLo = (UINT32) RShiftU64 ((UINT64)(UINTN)Buffer, 12);
65 ExtRootEntry->Bits.LowerContextTablePointerHi = (UINT32) RShiftU64 ((UINT64)(UINTN)Buffer, 32);
66 ExtRootEntry->Bits.LowerPresent = 1;
67 ExtRootEntry->Bits.UpperContextTablePointerLo = (UINT32) RShiftU64 ((UINT64)(UINTN)Buffer, 12) + 1;
68 ExtRootEntry->Bits.UpperContextTablePointerHi = (UINT32) RShiftU64 (RShiftU64 ((UINT64)(UINTN)Buffer, 12) + 1, 20);
69 ExtRootEntry->Bits.UpperPresent = 1;
70 Buffer = (UINT8 *)Buffer + EFI_PAGES_TO_SIZE (ContextPages);
71 }
72
73 ExtContextEntryTable = (VTD_EXT_CONTEXT_ENTRY *)(UINTN)VTD_64BITS_ADDRESS(ExtRootEntry->Bits.LowerContextTablePointerLo, ExtRootEntry->Bits.LowerContextTablePointerHi) ;
74 ExtContextEntry = &ExtContextEntryTable[SourceId.Index.ContextIndex];
75 ExtContextEntry->Bits.TranslationType = 0;
76 ExtContextEntry->Bits.FaultProcessingDisable = 0;
77 ExtContextEntry->Bits.Present = 0;
78
79 DEBUG ((DEBUG_INFO,"DOMAIN: S%04x, B%02x D%02x F%02x\n", mVtdUnitInformation[VtdIndex].Segment, SourceId.Bits.Bus, SourceId.Bits.Device, SourceId.Bits.Function));
80
81 switch (mVtdUnitInformation[VtdIndex].CapReg.Bits.SAGAW) {
82 case BIT1:
83 ExtContextEntry->Bits.AddressWidth = 0x1;
84 break;
85 case BIT2:
86 ExtContextEntry->Bits.AddressWidth = 0x2;
87 break;
88 }
89 }
90
91 FlushPageTableMemory (VtdIndex, (UINTN)mVtdUnitInformation[VtdIndex].ExtRootEntryTable, EFI_PAGES_TO_SIZE(EntryTablePages));
92
93 return EFI_SUCCESS;
94 }
95
96 /**
97 Dump DMAR extended context entry table.
98
99 @param[in] ExtRootEntry DMAR extended root entry.
100 **/
101 VOID
102 DumpDmarExtContextEntryTable (
103 IN VTD_EXT_ROOT_ENTRY *ExtRootEntry
104 )
105 {
106 UINTN Index;
107 UINTN Index2;
108 VTD_EXT_CONTEXT_ENTRY *ExtContextEntry;
109
110 DEBUG ((DEBUG_INFO,"=========================\n"));
111 DEBUG ((DEBUG_INFO,"DMAR ExtContext Entry Table:\n"));
112
113 DEBUG ((DEBUG_INFO,"ExtRootEntry Address - 0x%x\n", ExtRootEntry));
114
115 for (Index = 0; Index < VTD_ROOT_ENTRY_NUMBER; Index++) {
116 if ((ExtRootEntry[Index].Uint128.Uint64Lo != 0) || (ExtRootEntry[Index].Uint128.Uint64Hi != 0)) {
117 DEBUG ((DEBUG_INFO," ExtRootEntry(0x%02x) B%02x - 0x%016lx %016lx\n",
118 Index, Index, ExtRootEntry[Index].Uint128.Uint64Hi, ExtRootEntry[Index].Uint128.Uint64Lo));
119 }
120 if (ExtRootEntry[Index].Bits.LowerPresent == 0) {
121 continue;
122 }
123 ExtContextEntry = (VTD_EXT_CONTEXT_ENTRY *)(UINTN)VTD_64BITS_ADDRESS(ExtRootEntry[Index].Bits.LowerContextTablePointerLo, ExtRootEntry[Index].Bits.LowerContextTablePointerHi);
124 for (Index2 = 0; Index2 < VTD_CONTEXT_ENTRY_NUMBER/2; Index2++) {
125 if ((ExtContextEntry[Index2].Uint256.Uint64_1 != 0) || (ExtContextEntry[Index2].Uint256.Uint64_2 != 0) ||
126 (ExtContextEntry[Index2].Uint256.Uint64_3 != 0) || (ExtContextEntry[Index2].Uint256.Uint64_4 != 0)) {
127 DEBUG ((DEBUG_INFO," ExtContextEntryLower(0x%02x) D%02xF%02x - 0x%016lx %016lx %016lx %016lx\n",
128 Index2, Index2 >> 3, Index2 & 0x7, ExtContextEntry[Index2].Uint256.Uint64_4, ExtContextEntry[Index2].Uint256.Uint64_3, ExtContextEntry[Index2].Uint256.Uint64_2, ExtContextEntry[Index2].Uint256.Uint64_1));
129 }
130 if (ExtContextEntry[Index2].Bits.Present == 0) {
131 continue;
132 }
133 DumpSecondLevelPagingEntry ((VOID *)(UINTN)VTD_64BITS_ADDRESS(ExtContextEntry[Index2].Bits.SecondLevelPageTranslationPointerLo, ExtContextEntry[Index2].Bits.SecondLevelPageTranslationPointerHi));
134 }
135
136 if (ExtRootEntry[Index].Bits.UpperPresent == 0) {
137 continue;
138 }
139 ExtContextEntry = (VTD_EXT_CONTEXT_ENTRY *)(UINTN)VTD_64BITS_ADDRESS(ExtRootEntry[Index].Bits.UpperContextTablePointerLo, ExtRootEntry[Index].Bits.UpperContextTablePointerHi);
140 for (Index2 = 0; Index2 < VTD_CONTEXT_ENTRY_NUMBER/2; Index2++) {
141 if ((ExtContextEntry[Index2].Uint256.Uint64_1 != 0) || (ExtContextEntry[Index2].Uint256.Uint64_2 != 0) ||
142 (ExtContextEntry[Index2].Uint256.Uint64_3 != 0) || (ExtContextEntry[Index2].Uint256.Uint64_4 != 0)) {
143 DEBUG ((DEBUG_INFO," ExtContextEntryUpper(0x%02x) D%02xF%02x - 0x%016lx %016lx %016lx %016lx\n",
144 Index2, (Index2 + 128) >> 3, (Index2 + 128) & 0x7, ExtContextEntry[Index2].Uint256.Uint64_4, ExtContextEntry[Index2].Uint256.Uint64_3, ExtContextEntry[Index2].Uint256.Uint64_2, ExtContextEntry[Index2].Uint256.Uint64_1));
145 }
146 if (ExtContextEntry[Index2].Bits.Present == 0) {
147 continue;
148 }
149 }
150 }
151 DEBUG ((DEBUG_INFO,"=========================\n"));
152 }