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