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