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.
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.
14 #include "DmaProtection.h"
17 Create extended context entry.
19 @param[in] VtdIndex The index of the VTd engine.
21 @retval EFI_SUCCESS The extended context entry is created.
22 @retval EFI_OUT_OF_RESOURCE No enough resource to create extended context entry.
25 CreateExtContextEntry (
33 VTD_EXT_ROOT_ENTRY
*ExtRootEntry
;
34 VTD_EXT_CONTEXT_ENTRY
*ExtContextEntryTable
;
35 VTD_EXT_CONTEXT_ENTRY
*ExtContextEntry
;
36 VTD_SOURCE_ID
*PciSourceId
;
37 VTD_SOURCE_ID SourceId
;
39 UINTN EntryTablePages
;
42 for (Index
= 0; Index
< mVtdUnitInformation
[VtdIndex
].PciDeviceInfo
.PciDeviceDataNumber
; Index
++) {
43 PciSourceId
= &mVtdUnitInformation
[VtdIndex
].PciDeviceInfo
.PciDeviceData
[Index
].PciSourceId
;
44 if (PciSourceId
->Bits
.Bus
> MaxBusNumber
) {
45 MaxBusNumber
= PciSourceId
->Bits
.Bus
;
48 DEBUG ((DEBUG_INFO
," MaxBusNumber - 0x%x\n", MaxBusNumber
));
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
);
55 DEBUG ((DEBUG_INFO
,"Could not Alloc Root Entry Table.. \n"));
56 return EFI_OUT_OF_RESOURCES
;
58 mVtdUnitInformation
[VtdIndex
].ExtRootEntryTable
= (VTD_EXT_ROOT_ENTRY
*)Buffer
;
59 Buffer
= (UINT8
*)Buffer
+ EFI_PAGES_TO_SIZE (RootPages
);
61 for (Index
= 0; Index
< mVtdUnitInformation
[VtdIndex
].PciDeviceInfo
.PciDeviceDataNumber
; Index
++) {
62 PciSourceId
= &mVtdUnitInformation
[VtdIndex
].PciDeviceInfo
.PciDeviceData
[Index
].PciSourceId
;
64 SourceId
.Bits
.Bus
= PciSourceId
->Bits
.Bus
;
65 SourceId
.Bits
.Device
= PciSourceId
->Bits
.Device
;
66 SourceId
.Bits
.Function
= PciSourceId
->Bits
.Function
;
68 ExtRootEntry
= &mVtdUnitInformation
[VtdIndex
].ExtRootEntryTable
[SourceId
.Index
.RootIndex
];
69 if (ExtRootEntry
->Bits
.LowerPresent
== 0) {
70 ExtRootEntry
->Bits
.LowerContextTablePointerLo
= (UINT32
) RShiftU64 ((UINT64
)(UINTN
)Buffer
, 12);
71 ExtRootEntry
->Bits
.LowerContextTablePointerHi
= (UINT32
) RShiftU64 ((UINT64
)(UINTN
)Buffer
, 32);
72 ExtRootEntry
->Bits
.LowerPresent
= 1;
73 ExtRootEntry
->Bits
.UpperContextTablePointerLo
= (UINT32
) RShiftU64 ((UINT64
)(UINTN
)Buffer
, 12) + 1;
74 ExtRootEntry
->Bits
.UpperContextTablePointerHi
= (UINT32
) RShiftU64 (RShiftU64 ((UINT64
)(UINTN
)Buffer
, 12) + 1, 20);
75 ExtRootEntry
->Bits
.UpperPresent
= 1;
76 Buffer
= (UINT8
*)Buffer
+ EFI_PAGES_TO_SIZE (ContextPages
);
79 ExtContextEntryTable
= (VTD_EXT_CONTEXT_ENTRY
*)(UINTN
)VTD_64BITS_ADDRESS(ExtRootEntry
->Bits
.LowerContextTablePointerLo
, ExtRootEntry
->Bits
.LowerContextTablePointerHi
) ;
80 ExtContextEntry
= &ExtContextEntryTable
[SourceId
.Index
.ContextIndex
];
81 ExtContextEntry
->Bits
.TranslationType
= 0;
82 ExtContextEntry
->Bits
.FaultProcessingDisable
= 0;
83 ExtContextEntry
->Bits
.Present
= 0;
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
));
87 switch (mVtdUnitInformation
[VtdIndex
].CapReg
.Bits
.SAGAW
) {
89 ExtContextEntry
->Bits
.AddressWidth
= 0x1;
92 ExtContextEntry
->Bits
.AddressWidth
= 0x2;
97 FlushPageTableMemory (VtdIndex
, (UINTN
)mVtdUnitInformation
[VtdIndex
].ExtRootEntryTable
, EFI_PAGES_TO_SIZE(EntryTablePages
));
103 Dump DMAR extended context entry table.
105 @param[in] ExtRootEntry DMAR extended root entry.
108 DumpDmarExtContextEntryTable (
109 IN VTD_EXT_ROOT_ENTRY
*ExtRootEntry
114 VTD_EXT_CONTEXT_ENTRY
*ExtContextEntry
;
116 DEBUG ((DEBUG_INFO
,"=========================\n"));
117 DEBUG ((DEBUG_INFO
,"DMAR ExtContext Entry Table:\n"));
119 DEBUG ((DEBUG_INFO
,"ExtRootEntry Address - 0x%x\n", ExtRootEntry
));
121 for (Index
= 0; Index
< VTD_ROOT_ENTRY_NUMBER
; Index
++) {
122 if ((ExtRootEntry
[Index
].Uint128
.Uint64Lo
!= 0) || (ExtRootEntry
[Index
].Uint128
.Uint64Hi
!= 0)) {
123 DEBUG ((DEBUG_INFO
," ExtRootEntry(0x%02x) B%02x - 0x%016lx %016lx\n",
124 Index
, Index
, ExtRootEntry
[Index
].Uint128
.Uint64Hi
, ExtRootEntry
[Index
].Uint128
.Uint64Lo
));
126 if (ExtRootEntry
[Index
].Bits
.LowerPresent
== 0) {
129 ExtContextEntry
= (VTD_EXT_CONTEXT_ENTRY
*)(UINTN
)VTD_64BITS_ADDRESS(ExtRootEntry
[Index
].Bits
.LowerContextTablePointerLo
, ExtRootEntry
[Index
].Bits
.LowerContextTablePointerHi
);
130 for (Index2
= 0; Index2
< VTD_CONTEXT_ENTRY_NUMBER
/2; Index2
++) {
131 if ((ExtContextEntry
[Index2
].Uint256
.Uint64_1
!= 0) || (ExtContextEntry
[Index2
].Uint256
.Uint64_2
!= 0) ||
132 (ExtContextEntry
[Index2
].Uint256
.Uint64_3
!= 0) || (ExtContextEntry
[Index2
].Uint256
.Uint64_4
!= 0)) {
133 DEBUG ((DEBUG_INFO
," ExtContextEntryLower(0x%02x) D%02xF%02x - 0x%016lx %016lx %016lx %016lx\n",
134 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
));
136 if (ExtContextEntry
[Index2
].Bits
.Present
== 0) {
141 if (ExtRootEntry
[Index
].Bits
.UpperPresent
== 0) {
144 ExtContextEntry
= (VTD_EXT_CONTEXT_ENTRY
*)(UINTN
)VTD_64BITS_ADDRESS(ExtRootEntry
[Index
].Bits
.UpperContextTablePointerLo
, ExtRootEntry
[Index
].Bits
.UpperContextTablePointerHi
);
145 for (Index2
= 0; Index2
< VTD_CONTEXT_ENTRY_NUMBER
/2; Index2
++) {
146 if ((ExtContextEntry
[Index2
].Uint256
.Uint64_1
!= 0) || (ExtContextEntry
[Index2
].Uint256
.Uint64_2
!= 0) ||
147 (ExtContextEntry
[Index2
].Uint256
.Uint64_3
!= 0) || (ExtContextEntry
[Index2
].Uint256
.Uint64_4
!= 0)) {
148 DEBUG ((DEBUG_INFO
," ExtContextEntryUpper(0x%02x) D%02xF%02x - 0x%016lx %016lx %016lx %016lx\n",
149 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
));
151 if (ExtContextEntry
[Index2
].Bits
.Present
== 0) {
156 DEBUG ((DEBUG_INFO
,"=========================\n"));