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