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