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