]> git.proxmox.com Git - mirror_edk2.git/blame - IntelSiliconPkg/Feature/VTd/IntelVTdDxe/IntelVTdDxe.c
SecurityPkg: also clear HashInterfaceHob.SupportedHashMask
[mirror_edk2.git] / IntelSiliconPkg / Feature / VTd / IntelVTdDxe / IntelVTdDxe.c
CommitLineData
c049fc99
JY
1/** @file\r
2 Intel VTd driver.\r
3\r
0bc94c74 4 Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.<BR>\r
c049fc99
JY
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
c049fc99
JY
15#include "DmaProtection.h"\r
16\r
17/**\r
18 Provides the controller-specific addresses required to access system memory from a\r
19 DMA bus master.\r
20\r
21 @param This The protocol instance pointer.\r
22 @param Operation Indicates if the bus master is going to read or write to system memory.\r
23 @param HostAddress The system memory address to map to the PCI controller.\r
24 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes\r
25 that were mapped.\r
26 @param DeviceAddress The resulting map address for the bus master PCI controller to use to\r
27 access the hosts HostAddress.\r
28 @param Mapping A resulting value to pass to Unmap().\r
29\r
30 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.\r
31 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.\r
32 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
33 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
34 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.\r
35\r
36**/\r
37EFI_STATUS\r
38EFIAPI\r
39IoMmuMap (\r
40 IN EDKII_IOMMU_PROTOCOL *This,\r
41 IN EDKII_IOMMU_OPERATION Operation,\r
42 IN VOID *HostAddress,\r
43 IN OUT UINTN *NumberOfBytes,\r
44 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r
45 OUT VOID **Mapping\r
46 );\r
47\r
48/**\r
49 Completes the Map() operation and releases any corresponding resources.\r
50\r
51 @param This The protocol instance pointer.\r
52 @param Mapping The mapping value returned from Map().\r
53\r
54 @retval EFI_SUCCESS The range was unmapped.\r
55 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().\r
56 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.\r
57**/\r
58EFI_STATUS\r
59EFIAPI\r
60IoMmuUnmap (\r
61 IN EDKII_IOMMU_PROTOCOL *This,\r
62 IN VOID *Mapping\r
63 );\r
64\r
65/**\r
66 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or\r
67 OperationBusMasterCommonBuffer64 mapping.\r
68\r
69 @param This The protocol instance pointer.\r
70 @param Type This parameter is not used and must be ignored.\r
71 @param MemoryType The type of memory to allocate, EfiBootServicesData or\r
72 EfiRuntimeServicesData.\r
73 @param Pages The number of pages to allocate.\r
74 @param HostAddress A pointer to store the base system memory address of the\r
75 allocated range.\r
76 @param Attributes The requested bit mask of attributes for the allocated range.\r
77\r
78 @retval EFI_SUCCESS The requested memory pages were allocated.\r
79 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are\r
34e18d17 80 MEMORY_WRITE_COMBINE, MEMORY_CACHED and DUAL_ADDRESS_CYCLE.\r
c049fc99
JY
81 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
82 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
83\r
84**/\r
85EFI_STATUS\r
86EFIAPI\r
87IoMmuAllocateBuffer (\r
88 IN EDKII_IOMMU_PROTOCOL *This,\r
89 IN EFI_ALLOCATE_TYPE Type,\r
90 IN EFI_MEMORY_TYPE MemoryType,\r
91 IN UINTN Pages,\r
92 IN OUT VOID **HostAddress,\r
93 IN UINT64 Attributes\r
94 );\r
95\r
96/**\r
97 Frees memory that was allocated with AllocateBuffer().\r
98\r
99 @param This The protocol instance pointer.\r
100 @param Pages The number of pages to free.\r
101 @param HostAddress The base system memory address of the allocated range.\r
102\r
103 @retval EFI_SUCCESS The requested memory pages were freed.\r
104 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages\r
105 was not allocated with AllocateBuffer().\r
106\r
107**/\r
108EFI_STATUS\r
109EFIAPI\r
110IoMmuFreeBuffer (\r
111 IN EDKII_IOMMU_PROTOCOL *This,\r
112 IN UINTN Pages,\r
113 IN VOID *HostAddress\r
114 );\r
115\r
116/**\r
117 Convert the DeviceHandle to SourceId and Segment.\r
118\r
119 @param[in] DeviceHandle The device who initiates the DMA access request.\r
120 @param[out] Segment The Segment used to identify a VTd engine.\r
121 @param[out] SourceId The SourceId used to identify a VTd engine and table entry.\r
122\r
123 @retval EFI_SUCCESS The Segment and SourceId are returned.\r
124 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.\r
125 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.\r
126**/\r
127EFI_STATUS\r
128DeviceHandleToSourceId (\r
129 IN EFI_HANDLE DeviceHandle,\r
130 OUT UINT16 *Segment,\r
131 OUT VTD_SOURCE_ID *SourceId\r
132 )\r
133{\r
134 EFI_PCI_IO_PROTOCOL *PciIo;\r
135 UINTN Seg;\r
136 UINTN Bus;\r
137 UINTN Dev;\r
138 UINTN Func;\r
139 EFI_STATUS Status;\r
140 EDKII_PLATFORM_VTD_DEVICE_INFO DeviceInfo;\r
141\r
142 Status = EFI_NOT_FOUND;\r
143 if (mPlatformVTdPolicy != NULL) {\r
144 Status = mPlatformVTdPolicy->GetDeviceId (mPlatformVTdPolicy, DeviceHandle, &DeviceInfo);\r
145 if (!EFI_ERROR(Status)) {\r
146 *Segment = DeviceInfo.Segment;\r
147 *SourceId = DeviceInfo.SourceId;\r
148 return EFI_SUCCESS;\r
149 }\r
150 }\r
151\r
152 Status = gBS->HandleProtocol (DeviceHandle, &gEfiPciIoProtocolGuid, (VOID **)&PciIo);\r
153 if (EFI_ERROR(Status)) {\r
154 return EFI_UNSUPPORTED;\r
155 }\r
156 Status = PciIo->GetLocation (PciIo, &Seg, &Bus, &Dev, &Func);\r
157 if (EFI_ERROR(Status)) {\r
158 return EFI_UNSUPPORTED;\r
159 }\r
160 *Segment = (UINT16)Seg;\r
161 SourceId->Bits.Bus = (UINT8)Bus;\r
162 SourceId->Bits.Device = (UINT8)Dev;\r
163 SourceId->Bits.Function = (UINT8)Func;\r
164\r
165 return EFI_SUCCESS;\r
166}\r
167\r
168/**\r
169 Set IOMMU attribute for a system memory.\r
170\r
171 If the IOMMU protocol exists, the system memory cannot be used\r
172 for DMA by default.\r
173\r
174 When a device requests a DMA access for a system memory,\r
175 the device driver need use SetAttribute() to update the IOMMU\r
176 attribute to request DMA access (read and/or write).\r
177\r
178 The DeviceHandle is used to identify which device submits the request.\r
179 The IOMMU implementation need translate the device path to an IOMMU device ID,\r
180 and set IOMMU hardware register accordingly.\r
181 1) DeviceHandle can be a standard PCI device.\r
182 The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.\r
183 The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.\r
184 The memory for BusMasterCommonBuffer need set EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.\r
185 After the memory is used, the memory need set 0 to keep it being protected.\r
186 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).\r
187 The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or EDKII_IOMMU_ACCESS_WRITE.\r
188\r
189 @param[in] This The protocol instance pointer.\r
190 @param[in] DeviceHandle The device who initiates the DMA access request.\r
191 @param[in] DeviceAddress The base of device memory address to be used as the DMA memory.\r
192 @param[in] Length The length of device memory address to be used as the DMA memory.\r
193 @param[in] IoMmuAccess The IOMMU access.\r
194\r
195 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by DeviceAddress and Length.\r
196 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.\r
197 @retval EFI_INVALID_PARAMETER DeviceAddress is not IoMmu Page size aligned.\r
198 @retval EFI_INVALID_PARAMETER Length is not IoMmu Page size aligned.\r
199 @retval EFI_INVALID_PARAMETER Length is 0.\r
200 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.\r
201 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.\r
202 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.\r
203 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by DeviceAddress and Length.\r
204 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.\r
205 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.\r
206\r
207**/\r
208EFI_STATUS\r
209VTdSetAttribute (\r
210 IN EDKII_IOMMU_PROTOCOL *This,\r
211 IN EFI_HANDLE DeviceHandle,\r
212 IN EFI_PHYSICAL_ADDRESS DeviceAddress,\r
213 IN UINT64 Length,\r
214 IN UINT64 IoMmuAccess\r
215 )\r
216{\r
217 EFI_STATUS Status;\r
218 UINT16 Segment;\r
219 VTD_SOURCE_ID SourceId;\r
4ad5f597
JY
220 CHAR8 PerfToken[sizeof("VTD(S0000.B00.D00.F00)")];\r
221 UINT32 Identifier;\r
c049fc99
JY
222\r
223 DumpVtdIfError ();\r
224\r
225 Status = DeviceHandleToSourceId (DeviceHandle, &Segment, &SourceId);\r
226 if (EFI_ERROR(Status)) {\r
227 return Status;\r
228 }\r
229\r
230 DEBUG ((DEBUG_VERBOSE, "IoMmuSetAttribute: "));\r
231 DEBUG ((DEBUG_VERBOSE, "PCI(S%x.B%x.D%x.F%x) ", Segment, SourceId.Bits.Bus, SourceId.Bits.Device, SourceId.Bits.Function));\r
232 DEBUG ((DEBUG_VERBOSE, "(0x%lx~0x%lx) - %lx\n", DeviceAddress, Length, IoMmuAccess));\r
233\r
0bc94c74
SZ
234 if (mAcpiDmarTable == NULL) {\r
235 //\r
236 // Record the entry to driver global variable.\r
237 // As such once VTd is activated, the setting can be adopted.\r
238 //\r
239 return RequestAccessAttribute (Segment, SourceId, DeviceAddress, Length, IoMmuAccess);\r
240 }\r
241\r
4ad5f597
JY
242 PERF_CODE (\r
243 AsciiSPrint (PerfToken, sizeof(PerfToken), "S%04xB%02xD%02xF%01x", Segment, SourceId.Bits.Bus, SourceId.Bits.Device, SourceId.Bits.Function);\r
244 Identifier = (Segment << 16) | SourceId.Uint16;\r
245 PERF_START_EX (gImageHandle, PerfToken, "IntelVTD", 0, Identifier);\r
246 );\r
247\r
c049fc99
JY
248 Status = SetAccessAttribute (Segment, SourceId, DeviceAddress, Length, IoMmuAccess);\r
249\r
4ad5f597
JY
250 PERF_CODE (\r
251 Identifier = (Segment << 16) | SourceId.Uint16;\r
252 PERF_END_EX (gImageHandle, PerfToken, "IntelVTD", 0, Identifier);\r
253 );\r
254\r
c049fc99
JY
255 return Status;\r
256}\r
257\r
258/**\r
259 Set IOMMU attribute for a system memory.\r
260\r
261 If the IOMMU protocol exists, the system memory cannot be used\r
262 for DMA by default.\r
263\r
264 When a device requests a DMA access for a system memory,\r
265 the device driver need use SetAttribute() to update the IOMMU\r
266 attribute to request DMA access (read and/or write).\r
267\r
268 The DeviceHandle is used to identify which device submits the request.\r
269 The IOMMU implementation need translate the device path to an IOMMU device ID,\r
270 and set IOMMU hardware register accordingly.\r
271 1) DeviceHandle can be a standard PCI device.\r
272 The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.\r
273 The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.\r
274 The memory for BusMasterCommonBuffer need set EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.\r
275 After the memory is used, the memory need set 0 to keep it being protected.\r
276 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).\r
277 The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or EDKII_IOMMU_ACCESS_WRITE.\r
278\r
279 @param[in] This The protocol instance pointer.\r
280 @param[in] DeviceHandle The device who initiates the DMA access request.\r
281 @param[in] Mapping The mapping value returned from Map().\r
282 @param[in] IoMmuAccess The IOMMU access.\r
283\r
284 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by DeviceAddress and Length.\r
285 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.\r
286 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().\r
287 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.\r
288 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.\r
289 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.\r
290 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by Mapping.\r
291 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.\r
292 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.\r
293\r
294**/\r
295EFI_STATUS\r
296EFIAPI\r
297IoMmuSetAttribute (\r
298 IN EDKII_IOMMU_PROTOCOL *This,\r
299 IN EFI_HANDLE DeviceHandle,\r
300 IN VOID *Mapping,\r
301 IN UINT64 IoMmuAccess\r
302 )\r
303{\r
304 EFI_STATUS Status;\r
305 EFI_PHYSICAL_ADDRESS DeviceAddress;\r
306 UINTN NumberOfPages;\r
3a716706
SZ
307 EFI_TPL OriginalTpl;\r
308\r
309 OriginalTpl = gBS->RaiseTPL (VTD_TPL_LEVEL);\r
c049fc99
JY
310\r
311 Status = GetDeviceInfoFromMapping (Mapping, &DeviceAddress, &NumberOfPages);\r
3a716706
SZ
312 if (!EFI_ERROR(Status)) {\r
313 Status = VTdSetAttribute (\r
314 This,\r
315 DeviceHandle,\r
316 DeviceAddress,\r
317 EFI_PAGES_TO_SIZE(NumberOfPages),\r
318 IoMmuAccess\r
319 );\r
c049fc99 320 }\r
3a716706
SZ
321\r
322 gBS->RestoreTPL (OriginalTpl);\r
c049fc99
JY
323\r
324 return Status;\r
325}\r
326\r
327EDKII_IOMMU_PROTOCOL mIntelVTd = {\r
328 EDKII_IOMMU_PROTOCOL_REVISION,\r
329 IoMmuSetAttribute,\r
330 IoMmuMap,\r
331 IoMmuUnmap,\r
332 IoMmuAllocateBuffer,\r
333 IoMmuFreeBuffer,\r
334};\r
335\r
336/**\r
337 Initialize the VTd driver.\r
338\r
339 @param[in] ImageHandle ImageHandle of the loaded driver\r
340 @param[in] SystemTable Pointer to the System Table\r
341\r
342 @retval EFI_SUCCESS The Protocol is installed.\r
343 @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.\r
344 @retval EFI_DEVICE_ERROR A device error occurred attempting to initialize the driver.\r
345\r
346**/\r
347EFI_STATUS\r
348EFIAPI\r
349IntelVTdInitialize (\r
350 IN EFI_HANDLE ImageHandle,\r
351 IN EFI_SYSTEM_TABLE *SystemTable\r
352 )\r
353{\r
354 EFI_STATUS Status;\r
355 EFI_HANDLE Handle;\r
356\r
c50596a7
JY
357 if ((PcdGet8(PcdVTdPolicyPropertyMask) & BIT0) == 0) {\r
358 return EFI_UNSUPPORTED;\r
359 }\r
360\r
c049fc99
JY
361 InitializeDmaProtection ();\r
362\r
363 Handle = NULL;\r
364 Status = gBS->InstallMultipleProtocolInterfaces (\r
365 &Handle,\r
366 &gEdkiiIoMmuProtocolGuid, &mIntelVTd,\r
367 NULL\r
368 );\r
369 ASSERT_EFI_ERROR (Status);\r
370\r
371 return Status;\r
372}\r