]> git.proxmox.com Git - mirror_edk2.git/blob - IntelSiliconPkg/Feature/VTd/IntelVTdDxe/IntelVTdDxe.c
841a5a9264aa9b6f040502fcd772be9923d57daa
[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 Convert the DeviceHandle to SourceId and Segment.
118
119 @param[in] DeviceHandle The device who initiates the DMA access request.
120 @param[out] Segment The Segment used to identify a VTd engine.
121 @param[out] SourceId The SourceId used to identify a VTd engine and table entry.
122
123 @retval EFI_SUCCESS The Segment and SourceId are returned.
124 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
125 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
126 **/
127 EFI_STATUS
128 DeviceHandleToSourceId (
129 IN EFI_HANDLE DeviceHandle,
130 OUT UINT16 *Segment,
131 OUT VTD_SOURCE_ID *SourceId
132 )
133 {
134 EFI_PCI_IO_PROTOCOL *PciIo;
135 UINTN Seg;
136 UINTN Bus;
137 UINTN Dev;
138 UINTN Func;
139 EFI_STATUS Status;
140 EDKII_PLATFORM_VTD_DEVICE_INFO DeviceInfo;
141
142 Status = EFI_NOT_FOUND;
143 if (mPlatformVTdPolicy != NULL) {
144 Status = mPlatformVTdPolicy->GetDeviceId (mPlatformVTdPolicy, DeviceHandle, &DeviceInfo);
145 if (!EFI_ERROR(Status)) {
146 *Segment = DeviceInfo.Segment;
147 *SourceId = DeviceInfo.SourceId;
148 return EFI_SUCCESS;
149 }
150 }
151
152 Status = gBS->HandleProtocol (DeviceHandle, &gEfiPciIoProtocolGuid, (VOID **)&PciIo);
153 if (EFI_ERROR(Status)) {
154 return EFI_UNSUPPORTED;
155 }
156 Status = PciIo->GetLocation (PciIo, &Seg, &Bus, &Dev, &Func);
157 if (EFI_ERROR(Status)) {
158 return EFI_UNSUPPORTED;
159 }
160 *Segment = (UINT16)Seg;
161 SourceId->Bits.Bus = (UINT8)Bus;
162 SourceId->Bits.Device = (UINT8)Dev;
163 SourceId->Bits.Function = (UINT8)Func;
164
165 return EFI_SUCCESS;
166 }
167
168 /**
169 Set IOMMU attribute for a system memory.
170
171 If the IOMMU protocol exists, the system memory cannot be used
172 for DMA by default.
173
174 When a device requests a DMA access for a system memory,
175 the device driver need use SetAttribute() to update the IOMMU
176 attribute to request DMA access (read and/or write).
177
178 The DeviceHandle is used to identify which device submits the request.
179 The IOMMU implementation need translate the device path to an IOMMU device ID,
180 and set IOMMU hardware register accordingly.
181 1) DeviceHandle can be a standard PCI device.
182 The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.
183 The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.
184 The memory for BusMasterCommonBuffer need set EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.
185 After the memory is used, the memory need set 0 to keep it being protected.
186 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).
187 The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or EDKII_IOMMU_ACCESS_WRITE.
188
189 @param[in] This The protocol instance pointer.
190 @param[in] DeviceHandle The device who initiates the DMA access request.
191 @param[in] DeviceAddress The base of device memory address to be used as the DMA memory.
192 @param[in] Length The length of device memory address to be used as the DMA memory.
193 @param[in] IoMmuAccess The IOMMU access.
194
195 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by DeviceAddress and Length.
196 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
197 @retval EFI_INVALID_PARAMETER DeviceAddress is not IoMmu Page size aligned.
198 @retval EFI_INVALID_PARAMETER Length is not IoMmu Page size aligned.
199 @retval EFI_INVALID_PARAMETER Length is 0.
200 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
201 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
202 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
203 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by DeviceAddress and Length.
204 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
205 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
206
207 **/
208 EFI_STATUS
209 VTdSetAttribute (
210 IN EDKII_IOMMU_PROTOCOL *This,
211 IN EFI_HANDLE DeviceHandle,
212 IN EFI_PHYSICAL_ADDRESS DeviceAddress,
213 IN UINT64 Length,
214 IN UINT64 IoMmuAccess
215 )
216 {
217 EFI_STATUS Status;
218 UINT16 Segment;
219 VTD_SOURCE_ID SourceId;
220 CHAR8 PerfToken[sizeof("VTD(S0000.B00.D00.F00)")];
221 UINT32 Identifier;
222
223 DumpVtdIfError ();
224
225 Status = DeviceHandleToSourceId (DeviceHandle, &Segment, &SourceId);
226 if (EFI_ERROR(Status)) {
227 return Status;
228 }
229
230 DEBUG ((DEBUG_VERBOSE, "IoMmuSetAttribute: "));
231 DEBUG ((DEBUG_VERBOSE, "PCI(S%x.B%x.D%x.F%x) ", Segment, SourceId.Bits.Bus, SourceId.Bits.Device, SourceId.Bits.Function));
232 DEBUG ((DEBUG_VERBOSE, "(0x%lx~0x%lx) - %lx\n", DeviceAddress, Length, IoMmuAccess));
233
234 if (mAcpiDmarTable == NULL) {
235 //
236 // Record the entry to driver global variable.
237 // As such once VTd is activated, the setting can be adopted.
238 //
239 return RequestAccessAttribute (Segment, SourceId, DeviceAddress, Length, IoMmuAccess);
240 }
241
242 PERF_CODE (
243 AsciiSPrint (PerfToken, sizeof(PerfToken), "S%04xB%02xD%02xF%01x", Segment, SourceId.Bits.Bus, SourceId.Bits.Device, SourceId.Bits.Function);
244 Identifier = (Segment << 16) | SourceId.Uint16;
245 PERF_START_EX (gImageHandle, PerfToken, "IntelVTD", 0, Identifier);
246 );
247
248 Status = SetAccessAttribute (Segment, SourceId, DeviceAddress, Length, IoMmuAccess);
249
250 PERF_CODE (
251 Identifier = (Segment << 16) | SourceId.Uint16;
252 PERF_END_EX (gImageHandle, PerfToken, "IntelVTD", 0, Identifier);
253 );
254
255 return Status;
256 }
257
258 /**
259 Set IOMMU attribute for a system memory.
260
261 If the IOMMU protocol exists, the system memory cannot be used
262 for DMA by default.
263
264 When a device requests a DMA access for a system memory,
265 the device driver need use SetAttribute() to update the IOMMU
266 attribute to request DMA access (read and/or write).
267
268 The DeviceHandle is used to identify which device submits the request.
269 The IOMMU implementation need translate the device path to an IOMMU device ID,
270 and set IOMMU hardware register accordingly.
271 1) DeviceHandle can be a standard PCI device.
272 The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.
273 The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.
274 The memory for BusMasterCommonBuffer need set EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.
275 After the memory is used, the memory need set 0 to keep it being protected.
276 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).
277 The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or EDKII_IOMMU_ACCESS_WRITE.
278
279 @param[in] This The protocol instance pointer.
280 @param[in] DeviceHandle The device who initiates the DMA access request.
281 @param[in] Mapping The mapping value returned from Map().
282 @param[in] IoMmuAccess The IOMMU access.
283
284 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by DeviceAddress and Length.
285 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
286 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
287 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
288 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
289 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
290 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by Mapping.
291 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
292 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
293
294 **/
295 EFI_STATUS
296 EFIAPI
297 IoMmuSetAttribute (
298 IN EDKII_IOMMU_PROTOCOL *This,
299 IN EFI_HANDLE DeviceHandle,
300 IN VOID *Mapping,
301 IN UINT64 IoMmuAccess
302 )
303 {
304 EFI_STATUS Status;
305 EFI_PHYSICAL_ADDRESS DeviceAddress;
306 UINTN NumberOfPages;
307 EFI_TPL OriginalTpl;
308
309 OriginalTpl = gBS->RaiseTPL (VTD_TPL_LEVEL);
310
311 Status = GetDeviceInfoFromMapping (Mapping, &DeviceAddress, &NumberOfPages);
312 if (!EFI_ERROR(Status)) {
313 Status = VTdSetAttribute (
314 This,
315 DeviceHandle,
316 DeviceAddress,
317 EFI_PAGES_TO_SIZE(NumberOfPages),
318 IoMmuAccess
319 );
320 }
321
322 gBS->RestoreTPL (OriginalTpl);
323
324 return Status;
325 }
326
327 EDKII_IOMMU_PROTOCOL mIntelVTd = {
328 EDKII_IOMMU_PROTOCOL_REVISION,
329 IoMmuSetAttribute,
330 IoMmuMap,
331 IoMmuUnmap,
332 IoMmuAllocateBuffer,
333 IoMmuFreeBuffer,
334 };
335
336 /**
337 Initialize the VTd driver.
338
339 @param[in] ImageHandle ImageHandle of the loaded driver
340 @param[in] SystemTable Pointer to the System Table
341
342 @retval EFI_SUCCESS The Protocol is installed.
343 @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.
344 @retval EFI_DEVICE_ERROR A device error occurred attempting to initialize the driver.
345
346 **/
347 EFI_STATUS
348 EFIAPI
349 IntelVTdInitialize (
350 IN EFI_HANDLE ImageHandle,
351 IN EFI_SYSTEM_TABLE *SystemTable
352 )
353 {
354 EFI_STATUS Status;
355 EFI_HANDLE Handle;
356
357 if ((PcdGet8(PcdVTdPolicyPropertyMask) & BIT0) == 0) {
358 return EFI_UNSUPPORTED;
359 }
360
361 InitializeDmaProtection ();
362
363 Handle = NULL;
364 Status = gBS->InstallMultipleProtocolInterfaces (
365 &Handle,
366 &gEdkiiIoMmuProtocolGuid, &mIntelVTd,
367 NULL
368 );
369 ASSERT_EFI_ERROR (Status);
370
371 return Status;
372 }