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