IntelSiliconPkg: Fix VS2015 NOOPT IA32 build failure in IntelVTdDxe
[mirror_edk2.git] / IntelSiliconPkg / IntelVTdDxe / TranslationTableEx.c
CommitLineData
c049fc99
JY
1/** @file\r
2\r
3 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>\r
4 This program and the accompanying materials\r
5 are licensed and made available under the terms and conditions of the BSD License\r
6 which accompanies this distribution. The full text of the license may be found at\r
7 http://opensource.org/licenses/bsd-license.php.\r
8\r
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12**/\r
13\r
14#include "DmaProtection.h"\r
15\r
16/**\r
17 Create extended context entry.\r
18\r
19 @param[in] VtdIndex The index of the VTd engine.\r
20\r
21 @retval EFI_SUCCESS The extended context entry is created.\r
22 @retval EFI_OUT_OF_RESOURCE No enough resource to create extended context entry.\r
23**/\r
24EFI_STATUS\r
25CreateExtContextEntry (\r
26 IN UINTN VtdIndex\r
27 )\r
28{\r
29 UINTN Index;\r
30 VOID *Buffer;\r
31 UINTN RootPages;\r
32 UINTN ContextPages;\r
33 VTD_EXT_ROOT_ENTRY *ExtRootEntry;\r
34 VTD_EXT_CONTEXT_ENTRY *ExtContextEntryTable;\r
35 VTD_EXT_CONTEXT_ENTRY *ExtContextEntry;\r
36 VTD_SOURCE_ID *PciDescriptor;\r
37 VTD_SOURCE_ID SourceId;\r
38 UINTN MaxBusNumber;\r
39 UINTN EntryTablePages;\r
40\r
41 MaxBusNumber = 0;\r
42 for (Index = 0; Index < mVtdUnitInformation[VtdIndex].PciDeviceInfo.PciDescriptorNumber; Index++) {\r
43 PciDescriptor = &mVtdUnitInformation[VtdIndex].PciDeviceInfo.PciDescriptors[Index];\r
44 if (PciDescriptor->Bits.Bus > MaxBusNumber) {\r
45 MaxBusNumber = PciDescriptor->Bits.Bus;\r
46 }\r
47 }\r
48 DEBUG ((DEBUG_INFO," MaxBusNumber - 0x%x\n", MaxBusNumber));\r
49\r
50 RootPages = EFI_SIZE_TO_PAGES (sizeof (VTD_EXT_ROOT_ENTRY) * VTD_ROOT_ENTRY_NUMBER);\r
51 ContextPages = EFI_SIZE_TO_PAGES (sizeof (VTD_EXT_CONTEXT_ENTRY) * VTD_CONTEXT_ENTRY_NUMBER);\r
52 EntryTablePages = RootPages + ContextPages * (MaxBusNumber + 1);\r
53 Buffer = AllocateZeroPages (EntryTablePages);\r
54 if (Buffer == NULL) {\r
55 DEBUG ((DEBUG_INFO,"Could not Alloc Root Entry Table.. \n"));\r
56 return EFI_OUT_OF_RESOURCES;\r
57 }\r
58 mVtdUnitInformation[VtdIndex].ExtRootEntryTable = (VTD_EXT_ROOT_ENTRY *)Buffer;\r
59 Buffer = (UINT8 *)Buffer + EFI_PAGES_TO_SIZE (RootPages);\r
60\r
61 for (Index = 0; Index < mVtdUnitInformation[VtdIndex].PciDeviceInfo.PciDescriptorNumber; Index++) {\r
62 PciDescriptor = &mVtdUnitInformation[VtdIndex].PciDeviceInfo.PciDescriptors[Index];\r
63\r
64 SourceId.Bits.Bus = PciDescriptor->Bits.Bus;\r
65 SourceId.Bits.Device = PciDescriptor->Bits.Device;\r
66 SourceId.Bits.Function = PciDescriptor->Bits.Function;\r
67\r
68 ExtRootEntry = &mVtdUnitInformation[VtdIndex].ExtRootEntryTable[SourceId.Index.RootIndex];\r
69 if (ExtRootEntry->Bits.LowerPresent == 0) {\r
76c6f69c
SZ
70 ExtRootEntry->Bits.LowerContextTablePointerLo = (UINT32) RShiftU64 ((UINT64)(UINTN)Buffer, 12);\r
71 ExtRootEntry->Bits.LowerContextTablePointerHi = (UINT32) RShiftU64 ((UINT64)(UINTN)Buffer, 32);\r
c049fc99 72 ExtRootEntry->Bits.LowerPresent = 1;\r
76c6f69c
SZ
73 ExtRootEntry->Bits.UpperContextTablePointerLo = (UINT32) RShiftU64 ((UINT64)(UINTN)Buffer, 12) + 1;\r
74 ExtRootEntry->Bits.UpperContextTablePointerHi = (UINT32) RShiftU64 (RShiftU64 ((UINT64)(UINTN)Buffer, 12) + 1, 20);\r
c049fc99
JY
75 ExtRootEntry->Bits.UpperPresent = 1;\r
76 Buffer = (UINT8 *)Buffer + EFI_PAGES_TO_SIZE (ContextPages);\r
77 }\r
78\r
76c6f69c 79 ExtContextEntryTable = (VTD_EXT_CONTEXT_ENTRY *)(UINTN)VTD_64BITS_ADDRESS(ExtRootEntry->Bits.LowerContextTablePointerLo, ExtRootEntry->Bits.LowerContextTablePointerHi) ;\r
c049fc99
JY
80 ExtContextEntry = &ExtContextEntryTable[SourceId.Index.ContextIndex];\r
81 ExtContextEntry->Bits.TranslationType = 0;\r
82 ExtContextEntry->Bits.FaultProcessingDisable = 0;\r
83 ExtContextEntry->Bits.Present = 0;\r
84\r
85 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));\r
86\r
87 switch (mVtdUnitInformation[VtdIndex].CapReg.Bits.SAGAW) {\r
88 case BIT1:\r
89 ExtContextEntry->Bits.AddressWidth = 0x1;\r
90 break;\r
91 case BIT2:\r
92 ExtContextEntry->Bits.AddressWidth = 0x2;\r
93 break;\r
94 }\r
95 }\r
96\r
97 return EFI_SUCCESS;\r
98}\r
99\r
100/**\r
101 Dump DMAR extended context entry table.\r
102\r
103 @param[in] ExtRootEntry DMAR extended root entry.\r
104**/\r
105VOID\r
106DumpDmarExtContextEntryTable (\r
107 IN VTD_EXT_ROOT_ENTRY *ExtRootEntry\r
108 )\r
109{\r
110 UINTN Index;\r
111 UINTN Index2;\r
112 VTD_EXT_CONTEXT_ENTRY *ExtContextEntry;\r
113\r
114 DEBUG ((DEBUG_INFO,"=========================\n"));\r
115 DEBUG ((DEBUG_INFO,"DMAR ExtContext Entry Table:\n"));\r
116\r
117 DEBUG ((DEBUG_INFO,"ExtRootEntry Address - 0x%x\n", ExtRootEntry));\r
118\r
119 for (Index = 0; Index < VTD_ROOT_ENTRY_NUMBER; Index++) {\r
120 if ((ExtRootEntry[Index].Uint128.Uint64Lo != 0) || (ExtRootEntry[Index].Uint128.Uint64Hi != 0)) {\r
121 DEBUG ((DEBUG_INFO," ExtRootEntry(0x%02x) B%02x - 0x%016lx %016lx\n",\r
122 Index, Index, ExtRootEntry[Index].Uint128.Uint64Hi, ExtRootEntry[Index].Uint128.Uint64Lo));\r
123 }\r
124 if (ExtRootEntry[Index].Bits.LowerPresent == 0) {\r
125 continue;\r
126 }\r
76c6f69c 127 ExtContextEntry = (VTD_EXT_CONTEXT_ENTRY *)(UINTN)VTD_64BITS_ADDRESS(ExtRootEntry[Index].Bits.LowerContextTablePointerLo, ExtRootEntry[Index].Bits.LowerContextTablePointerHi);\r
c049fc99
JY
128 for (Index2 = 0; Index2 < VTD_CONTEXT_ENTRY_NUMBER/2; Index2++) {\r
129 if ((ExtContextEntry[Index2].Uint256.Uint64_1 != 0) || (ExtContextEntry[Index2].Uint256.Uint64_2 != 0) ||\r
130 (ExtContextEntry[Index2].Uint256.Uint64_3 != 0) || (ExtContextEntry[Index2].Uint256.Uint64_4 != 0)) {\r
131 DEBUG ((DEBUG_INFO," ExtContextEntryLower(0x%02x) D%02xF%02x - 0x%016lx %016lx %016lx %016lx\n",\r
132 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));\r
133 }\r
134 if (ExtContextEntry[Index2].Bits.Present == 0) {\r
135 continue;\r
136 }\r
137 }\r
138\r
139 if (ExtRootEntry[Index].Bits.UpperPresent == 0) {\r
140 continue;\r
141 }\r
76c6f69c 142 ExtContextEntry = (VTD_EXT_CONTEXT_ENTRY *)(UINTN)VTD_64BITS_ADDRESS(ExtRootEntry[Index].Bits.UpperContextTablePointerLo, ExtRootEntry[Index].Bits.UpperContextTablePointerHi);\r
c049fc99
JY
143 for (Index2 = 0; Index2 < VTD_CONTEXT_ENTRY_NUMBER/2; Index2++) {\r
144 if ((ExtContextEntry[Index2].Uint256.Uint64_1 != 0) || (ExtContextEntry[Index2].Uint256.Uint64_2 != 0) ||\r
145 (ExtContextEntry[Index2].Uint256.Uint64_3 != 0) || (ExtContextEntry[Index2].Uint256.Uint64_4 != 0)) {\r
146 DEBUG ((DEBUG_INFO," ExtContextEntryUpper(0x%02x) D%02xF%02x - 0x%016lx %016lx %016lx %016lx\n",\r
147 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));\r
148 }\r
149 if (ExtContextEntry[Index2].Bits.Present == 0) {\r
150 continue;\r
151 }\r
152 }\r
153 }\r
154 DEBUG ((DEBUG_INFO,"=========================\n"));\r
155}\r