hc
2024-08-12 233ab1bd4c5697f5cdec94e60206e8c6ac609b4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/** @file
  The definition for DMA access Library.
 
  Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent
 
**/
 
#ifndef __DMA_ACCESS_LIB_H__
#define __DMA_ACCESS_LIB_H__
 
#define MAX_VTD_PCI_DATA_NUMBER             0x100
 
#define VTD_64BITS_ADDRESS(Lo, Hi) (LShiftU64 (Lo, 12) | LShiftU64 (Hi, 32))
 
typedef struct {
  UINT8                            DeviceType;
  VTD_SOURCE_ID                    PciSourceId;
} PEI_PCI_DEVICE_DATA;
 
typedef struct {
  BOOLEAN                          IncludeAllFlag;
  UINT32                           PciDeviceDataNumber;
  UINT32                           PciDeviceDataMaxNumber;
  UINT32                           PciDeviceDataPageSize;
  UINT32                           PciDeviceData;
} PEI_PCI_DEVICE_INFORMATION;
 
typedef struct {
  UINT32                           VtdUnitBaseAddress;
  UINT16                           Segment;
  VTD_VER_REG                      VerReg;
  VTD_CAP_REG                      CapReg;
  VTD_ECAP_REG                     ECapReg;
  BOOLEAN                          Is5LevelPaging;
  UINT32                           FixedSecondLevelPagingEntry;
  UINT32                           RmrrSecondLevelPagingEntry;
  UINT32                           RootEntryTable;
  UINT32                           ExtRootEntryTable;
  UINT16                           RootEntryTablePageSize;
  UINT16                           ExtRootEntryTablePageSize;
  PEI_PCI_DEVICE_INFORMATION       PciDeviceInfo;
  UINT8                            EnableQueuedInvalidation;
  UINT16                           QiDescLength;
  QI_DESC                          *QiDesc;
  UINT16                           QiFreeHead;
} VTD_UNIT_INFO;
 
typedef struct {
  UINT32                           AcpiDmarTable;
  UINT8                            HostAddressWidth;
  UINT32                           VTdEngineCount;
  VTD_UNIT_INFO                    VtdUnitInfo[1];
} VTD_INFO;
 
typedef struct {
  UINT64                            DmaBufferBase;
  UINT64                            DmaBufferSize;
  UINT64                            DmaBufferLimit;
  UINT64                            DmaBufferCurrentTop;
  UINT64                            DmaBufferCurrentBottom;
} DMA_BUFFER_INFO;
 
/**
  Enable VTd translation table protection.
 
  @param[in]  VTdInfo           The VTd engine context information.
  @param[in]  EngineMask        The mask of the VTd engine to be accessed.
**/
VOID
EnableVTdTranslationProtectionAll (
  IN VTD_INFO                   *VTdInfo,
  IN UINT64                     EngineMask
  );
 
/**
  Enable VTd translation table protection.
 
  @param[in]  VTdInfo           The VTd engine context information.
 
  @retval EFI_SUCCESS           DMAR translation is enabled.
  @retval EFI_DEVICE_ERROR      DMAR translation is not enabled.
**/
EFI_STATUS
EnableVTdTranslationProtection (
  IN VTD_INFO                   *VTdInfo
  );
 
/**
  Disable VTd translation table protection.
 
  @param[in]  VTdInfo           The VTd engine context information.
  @param[in]  EngineMask        The mask of the VTd engine to be accessed.
**/
VOID
DisableVTdTranslationProtection (
  IN VTD_INFO                   *VTdInfo,
  IN UINT64                     EngineMask
  );
 
/**
  Parse DMAR DRHD table.
 
  @param[in]  AcpiDmarTable     DMAR ACPI table
 
  @return EFI_SUCCESS           The DMAR DRHD table is parsed.
**/
EFI_STATUS
ParseDmarAcpiTableDrhd (
  IN EFI_ACPI_DMAR_HEADER       *AcpiDmarTable
  );
 
/**
  Parse DMAR DRHD table.
 
  @param[in]  VTdInfo           The VTd engine context information.
**/
VOID
ParseDmarAcpiTableRmrr (
  IN VTD_INFO                   *VTdInfo
  );
 
/**
  Dump DMAR ACPI table.
 
  @param[in]  Dmar              DMAR ACPI table
**/
VOID
DumpAcpiDMAR (
  IN EFI_ACPI_DMAR_HEADER       *Dmar
  );
 
/**
  Prepare VTD cache invalidation configuration.
 
  @param[in]  VTdInfo           The VTd engine context information.
 
  @retval EFI_SUCCESS           Prepare Vtd config success
**/
EFI_STATUS
PrepareVtdCacheInvalidationConfig (
  IN VTD_INFO                   *VTdInfo
  );
 
/**
  Prepare VTD configuration.
 
  @param[in]  VTdInfo           The VTd engine context information.
 
  @retval EFI_SUCCESS           Prepare Vtd config success
**/
EFI_STATUS
PrepareVtdConfig (
  IN VTD_INFO                   *VTdInfo
  );
 
/**
  Setup VTd translation table.
 
  @param[in]  VTdInfo           The VTd engine context information.
 
  @retval EFI_SUCCESS           Setup translation table successfully.
  @retval EFI_OUT_OF_RESOURCE   Setup translation table fail.
**/
EFI_STATUS
SetupTranslationTable (
  IN VTD_INFO                   *VTdInfo
  );
 
/**
  Flush VTD page table and context table memory.
 
  This action is to make sure the IOMMU engine can get final data in memory.
 
  @param[in]  VTdUnitInfo       The VTd engine unit information.
  @param[in]  Base              The base address of memory to be flushed.
  @param[in]  Size              The size of memory in bytes to be flushed.
**/
VOID
FlushPageTableMemory (
  IN VTD_UNIT_INFO              *VTdUnitInfo,
  IN UINTN                      Base,
  IN UINTN                      Size
  );
 
/**
  Allocate zero pages.
 
  @param[in]  Pages             the number of pages.
 
  @return the page address.
  @retval NULL No resource to allocate pages.
**/
VOID *
EFIAPI
AllocateZeroPages (
  IN UINTN                      Pages
  );
 
/**
  Return the index of PCI data.
 
  @param[in]  VTdUnitInfo       The VTd engine unit information.
  @param[in]  Segment           The Segment used to identify a VTd engine.
  @param[in]  SourceId          The SourceId used to identify a VTd engine and table entry.
 
  @return The index of the PCI data.
  @retval (UINTN)-1  The PCI data is not found.
**/
UINTN
GetPciDataIndex (
  IN VTD_UNIT_INFO              *VTdUnitInfo,
  IN UINT16                     Segment,
  IN VTD_SOURCE_ID              SourceId
  );
 
/**
  Always enable the VTd page attribute for the device.
 
  @param[in]  VTdInfo           The VTd engine context information.
  @param[in]  Segment           The Segment used to identify a VTd engine.
  @param[in]  SourceId          The SourceId used to identify a VTd engine and table entry.
  @param[in]  MemoryBase        The base of the memory.
  @param[in]  MemoryLimit       The limit of the memory.
  @param[in]  IoMmuAccess       The IOMMU access.
 
  @retval EFI_SUCCESS           The VTd entry is updated to always enable all DMA access for the specific device.
**/
EFI_STATUS
EnableRmrrPageAttribute (
  IN VTD_INFO                   *VTdInfo,
  IN UINT16                     Segment,
  IN VTD_SOURCE_ID              SourceId,
  IN UINT64                     MemoryBase,
  IN UINT64                     MemoryLimit,
  IN UINT64                     IoMmuAccess
  );
 
extern EFI_GUID mVTdInfoGuid;
extern EFI_GUID mDmaBufferInfoGuid;
 
#endif