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
CreateExtContextEntry(IN UINTN VtdIndex)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 mVtdUnitInformation[VtdIndex].Is5LevelPaging = FALSE;
82 if ((mVtdUnitInformation[VtdIndex].CapReg.Bits.SAGAW & BIT3) != 0) {
83 mVtdUnitInformation[VtdIndex].Is5LevelPaging = TRUE;
84 if ((mAcpiDmarTable->HostAddressWidth <= 48) &&
85 ((mVtdUnitInformation[VtdIndex].CapReg.Bits.SAGAW & BIT2) != 0)) {
86 mVtdUnitInformation[VtdIndex].Is5LevelPaging = FALSE;
87 }
88 } else if ((mVtdUnitInformation[VtdIndex].CapReg.Bits.SAGAW & BIT2) == 0) {
89 DEBUG((DEBUG_ERROR, "!!!! Page-table type is not supported on VTD %d !!!!\n", VtdIndex));
90 return EFI_UNSUPPORTED;
91 }
92
93 if (mVtdUnitInformation[VtdIndex].Is5LevelPaging) {
94 ExtContextEntry->Bits.AddressWidth = 0x3;
95 DEBUG((DEBUG_INFO, "Using 5-level page-table on VTD %d\n", VtdIndex));
96 } else {
97 ExtContextEntry->Bits.AddressWidth = 0x2;
98 DEBUG((DEBUG_INFO, "Using 4-level page-table on VTD %d\n", VtdIndex));
99 }
100
101
102 }
103
104 FlushPageTableMemory (VtdIndex, (UINTN)mVtdUnitInformation[VtdIndex].ExtRootEntryTable, EFI_PAGES_TO_SIZE(EntryTablePages));
105
106 return EFI_SUCCESS;
107 }
108
109 /**
110 Dump DMAR extended context entry table.
111
112 @param[in] ExtRootEntry DMAR extended root entry.
113 @param[in] Is5LevelPaging If it is the 5 level paging.
114 **/
115 VOID
DumpDmarExtContextEntryTable(IN VTD_EXT_ROOT_ENTRY * ExtRootEntry,IN BOOLEAN Is5LevelPaging)116 DumpDmarExtContextEntryTable (
117 IN VTD_EXT_ROOT_ENTRY *ExtRootEntry,
118 IN BOOLEAN Is5LevelPaging
119 )
120 {
121 UINTN Index;
122 UINTN Index2;
123 VTD_EXT_CONTEXT_ENTRY *ExtContextEntry;
124
125 DEBUG ((DEBUG_INFO,"=========================\n"));
126 DEBUG ((DEBUG_INFO,"DMAR ExtContext Entry Table:\n"));
127
128 DEBUG ((DEBUG_INFO,"ExtRootEntry Address - 0x%x\n", ExtRootEntry));
129
130 for (Index = 0; Index < VTD_ROOT_ENTRY_NUMBER; Index++) {
131 if ((ExtRootEntry[Index].Uint128.Uint64Lo != 0) || (ExtRootEntry[Index].Uint128.Uint64Hi != 0)) {
132 DEBUG ((DEBUG_INFO," ExtRootEntry(0x%02x) B%02x - 0x%016lx %016lx\n",
133 Index, Index, ExtRootEntry[Index].Uint128.Uint64Hi, ExtRootEntry[Index].Uint128.Uint64Lo));
134 }
135 if (ExtRootEntry[Index].Bits.LowerPresent == 0) {
136 continue;
137 }
138 ExtContextEntry = (VTD_EXT_CONTEXT_ENTRY *)(UINTN)VTD_64BITS_ADDRESS(ExtRootEntry[Index].Bits.LowerContextTablePointerLo, ExtRootEntry[Index].Bits.LowerContextTablePointerHi);
139 for (Index2 = 0; Index2 < VTD_CONTEXT_ENTRY_NUMBER/2; Index2++) {
140 if ((ExtContextEntry[Index2].Uint256.Uint64_1 != 0) || (ExtContextEntry[Index2].Uint256.Uint64_2 != 0) ||
141 (ExtContextEntry[Index2].Uint256.Uint64_3 != 0) || (ExtContextEntry[Index2].Uint256.Uint64_4 != 0)) {
142 DEBUG ((DEBUG_INFO," ExtContextEntryLower(0x%02x) D%02xF%02x - 0x%016lx %016lx %016lx %016lx\n",
143 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));
144 }
145 if (ExtContextEntry[Index2].Bits.Present == 0) {
146 continue;
147 }
148 DumpSecondLevelPagingEntry ((VOID *)(UINTN)VTD_64BITS_ADDRESS(ExtContextEntry[Index2].Bits.SecondLevelPageTranslationPointerLo, ExtContextEntry[Index2].Bits.SecondLevelPageTranslationPointerHi), Is5LevelPaging);
149 }
150
151 if (ExtRootEntry[Index].Bits.UpperPresent == 0) {
152 continue;
153 }
154 ExtContextEntry = (VTD_EXT_CONTEXT_ENTRY *)(UINTN)VTD_64BITS_ADDRESS(ExtRootEntry[Index].Bits.UpperContextTablePointerLo, ExtRootEntry[Index].Bits.UpperContextTablePointerHi);
155 for (Index2 = 0; Index2 < VTD_CONTEXT_ENTRY_NUMBER/2; Index2++) {
156 if ((ExtContextEntry[Index2].Uint256.Uint64_1 != 0) || (ExtContextEntry[Index2].Uint256.Uint64_2 != 0) ||
157 (ExtContextEntry[Index2].Uint256.Uint64_3 != 0) || (ExtContextEntry[Index2].Uint256.Uint64_4 != 0)) {
158 DEBUG ((DEBUG_INFO," ExtContextEntryUpper(0x%02x) D%02xF%02x - 0x%016lx %016lx %016lx %016lx\n",
159 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));
160 }
161 if (ExtContextEntry[Index2].Bits.Present == 0) {
162 continue;
163 }
164 }
165 }
166 DEBUG ((DEBUG_INFO,"=========================\n"));
167 }
168