]> git.proxmox.com Git - mirror_edk2.git/blob - IntelSiliconPkg/Feature/VTd/IntelVTdDxe/IntelVTdDxe.c
09948ce50e940b4f408603465de9f8c33e6af20e
[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 if ((PcdGet8 (PcdVTdPolicyPropertyMask) & BIT2) != 0) {
258 //
259 // Force no IOMMU access attribute request recording before DMAR table is installed.
260 //
261 ASSERT_EFI_ERROR (EFI_NOT_READY);
262 return EFI_NOT_READY;
263 }
264 Status = RequestAccessAttribute (Segment, SourceId, DeviceAddress, Length, IoMmuAccess);
265 } else {
266 PERF_CODE (
267 AsciiSPrint (PerfToken, sizeof(PerfToken), "S%04xB%02xD%02xF%01x", Segment, SourceId.Bits.Bus, SourceId.Bits.Device, SourceId.Bits.Function);
268 Identifier = (Segment << 16) | SourceId.Uint16;
269 PERF_START_EX (gImageHandle, PerfToken, "IntelVTD", 0, Identifier);
270 );
271
272 Status = SetAccessAttribute (Segment, SourceId, DeviceAddress, Length, IoMmuAccess);
273
274 PERF_CODE (
275 Identifier = (Segment << 16) | SourceId.Uint16;
276 PERF_END_EX (gImageHandle, PerfToken, "IntelVTD", 0, Identifier);
277 );
278 }
279
280 if (!EFI_ERROR(Status)) {
281 SyncDeviceHandleToMapInfo (
282 DeviceHandle,
283 DeviceAddress,
284 Length,
285 IoMmuAccess
286 );
287 }
288
289 return Status;
290 }
291
292 /**
293 Set IOMMU attribute for a system memory.
294
295 If the IOMMU protocol exists, the system memory cannot be used
296 for DMA by default.
297
298 When a device requests a DMA access for a system memory,
299 the device driver need use SetAttribute() to update the IOMMU
300 attribute to request DMA access (read and/or write).
301
302 The DeviceHandle is used to identify which device submits the request.
303 The IOMMU implementation need translate the device path to an IOMMU device ID,
304 and set IOMMU hardware register accordingly.
305 1) DeviceHandle can be a standard PCI device.
306 The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.
307 The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.
308 The memory for BusMasterCommonBuffer need set EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.
309 After the memory is used, the memory need set 0 to keep it being protected.
310 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).
311 The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or EDKII_IOMMU_ACCESS_WRITE.
312
313 @param[in] This The protocol instance pointer.
314 @param[in] DeviceHandle The device who initiates the DMA access request.
315 @param[in] Mapping The mapping value returned from Map().
316 @param[in] IoMmuAccess The IOMMU access.
317
318 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by DeviceAddress and Length.
319 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
320 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
321 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
322 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
323 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
324 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by Mapping.
325 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
326 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
327
328 **/
329 EFI_STATUS
330 EFIAPI
331 IoMmuSetAttribute (
332 IN EDKII_IOMMU_PROTOCOL *This,
333 IN EFI_HANDLE DeviceHandle,
334 IN VOID *Mapping,
335 IN UINT64 IoMmuAccess
336 )
337 {
338 EFI_STATUS Status;
339 EFI_PHYSICAL_ADDRESS DeviceAddress;
340 UINTN NumberOfPages;
341 EFI_TPL OriginalTpl;
342
343 OriginalTpl = gBS->RaiseTPL (VTD_TPL_LEVEL);
344
345 Status = GetDeviceInfoFromMapping (Mapping, &DeviceAddress, &NumberOfPages);
346 if (!EFI_ERROR(Status)) {
347 Status = VTdSetAttribute (
348 This,
349 DeviceHandle,
350 DeviceAddress,
351 EFI_PAGES_TO_SIZE(NumberOfPages),
352 IoMmuAccess
353 );
354 }
355
356 gBS->RestoreTPL (OriginalTpl);
357
358 return Status;
359 }
360
361 EDKII_IOMMU_PROTOCOL mIntelVTd = {
362 EDKII_IOMMU_PROTOCOL_REVISION,
363 IoMmuSetAttribute,
364 IoMmuMap,
365 IoMmuUnmap,
366 IoMmuAllocateBuffer,
367 IoMmuFreeBuffer,
368 };
369
370 /**
371 Initialize the VTd driver.
372
373 @param[in] ImageHandle ImageHandle of the loaded driver
374 @param[in] SystemTable Pointer to the System Table
375
376 @retval EFI_SUCCESS The Protocol is installed.
377 @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.
378 @retval EFI_DEVICE_ERROR A device error occurred attempting to initialize the driver.
379
380 **/
381 EFI_STATUS
382 EFIAPI
383 IntelVTdInitialize (
384 IN EFI_HANDLE ImageHandle,
385 IN EFI_SYSTEM_TABLE *SystemTable
386 )
387 {
388 EFI_STATUS Status;
389 EFI_HANDLE Handle;
390
391 if ((PcdGet8(PcdVTdPolicyPropertyMask) & BIT0) == 0) {
392 return EFI_UNSUPPORTED;
393 }
394
395 InitializeDmaProtection ();
396
397 Handle = NULL;
398 Status = gBS->InstallMultipleProtocolInterfaces (
399 &Handle,
400 &gEdkiiIoMmuProtocolGuid, &mIntelVTd,
401 NULL
402 );
403 ASSERT_EFI_ERROR (Status);
404
405 return Status;
406 }