IntelSiliconPkg IntelVTdDxe: Use TPL to protect list/engine operation
[mirror_edk2.git] / IntelSiliconPkg / Feature / VTd / 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 "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 PERF_CODE (
235 AsciiSPrint (PerfToken, sizeof(PerfToken), "S%04xB%02xD%02xF%01x", Segment, SourceId.Bits.Bus, SourceId.Bits.Device, SourceId.Bits.Function);
236 Identifier = (Segment << 16) | SourceId.Uint16;
237 PERF_START_EX (gImageHandle, PerfToken, "IntelVTD", 0, Identifier);
238 );
239
240 Status = SetAccessAttribute (Segment, SourceId, DeviceAddress, Length, IoMmuAccess);
241
242 PERF_CODE (
243 Identifier = (Segment << 16) | SourceId.Uint16;
244 PERF_END_EX (gImageHandle, PerfToken, "IntelVTD", 0, Identifier);
245 );
246
247 return Status;
248 }
249
250 /**
251 Set IOMMU attribute for a system memory.
252
253 If the IOMMU protocol exists, the system memory cannot be used
254 for DMA by default.
255
256 When a device requests a DMA access for a system memory,
257 the device driver need use SetAttribute() to update the IOMMU
258 attribute to request DMA access (read and/or write).
259
260 The DeviceHandle is used to identify which device submits the request.
261 The IOMMU implementation need translate the device path to an IOMMU device ID,
262 and set IOMMU hardware register accordingly.
263 1) DeviceHandle can be a standard PCI device.
264 The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.
265 The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.
266 The memory for BusMasterCommonBuffer need set EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.
267 After the memory is used, the memory need set 0 to keep it being protected.
268 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).
269 The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or EDKII_IOMMU_ACCESS_WRITE.
270
271 @param[in] This The protocol instance pointer.
272 @param[in] DeviceHandle The device who initiates the DMA access request.
273 @param[in] Mapping The mapping value returned from Map().
274 @param[in] IoMmuAccess The IOMMU access.
275
276 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range specified by DeviceAddress and Length.
277 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
278 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by Map().
279 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination of access.
280 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
281 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported by the IOMMU.
282 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range specified by Mapping.
283 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to modify the IOMMU access.
284 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while attempting the operation.
285
286 **/
287 EFI_STATUS
288 EFIAPI
289 IoMmuSetAttribute (
290 IN EDKII_IOMMU_PROTOCOL *This,
291 IN EFI_HANDLE DeviceHandle,
292 IN VOID *Mapping,
293 IN UINT64 IoMmuAccess
294 )
295 {
296 EFI_STATUS Status;
297 EFI_PHYSICAL_ADDRESS DeviceAddress;
298 UINTN NumberOfPages;
299 EFI_TPL OriginalTpl;
300
301 OriginalTpl = gBS->RaiseTPL (VTD_TPL_LEVEL);
302
303 Status = GetDeviceInfoFromMapping (Mapping, &DeviceAddress, &NumberOfPages);
304 if (!EFI_ERROR(Status)) {
305 Status = VTdSetAttribute (
306 This,
307 DeviceHandle,
308 DeviceAddress,
309 EFI_PAGES_TO_SIZE(NumberOfPages),
310 IoMmuAccess
311 );
312 }
313
314 gBS->RestoreTPL (OriginalTpl);
315
316 return Status;
317 }
318
319 EDKII_IOMMU_PROTOCOL mIntelVTd = {
320 EDKII_IOMMU_PROTOCOL_REVISION,
321 IoMmuSetAttribute,
322 IoMmuMap,
323 IoMmuUnmap,
324 IoMmuAllocateBuffer,
325 IoMmuFreeBuffer,
326 };
327
328 /**
329 Initialize the VTd driver.
330
331 @param[in] ImageHandle ImageHandle of the loaded driver
332 @param[in] SystemTable Pointer to the System Table
333
334 @retval EFI_SUCCESS The Protocol is installed.
335 @retval EFI_OUT_OF_RESOURCES Not enough resources available to initialize driver.
336 @retval EFI_DEVICE_ERROR A device error occurred attempting to initialize the driver.
337
338 **/
339 EFI_STATUS
340 EFIAPI
341 IntelVTdInitialize (
342 IN EFI_HANDLE ImageHandle,
343 IN EFI_SYSTEM_TABLE *SystemTable
344 )
345 {
346 EFI_STATUS Status;
347 EFI_HANDLE Handle;
348
349 if ((PcdGet8(PcdVTdPolicyPropertyMask) & BIT0) == 0) {
350 return EFI_UNSUPPORTED;
351 }
352
353 InitializeDmaProtection ();
354
355 Handle = NULL;
356 Status = gBS->InstallMultipleProtocolInterfaces (
357 &Handle,
358 &gEdkiiIoMmuProtocolGuid, &mIntelVTd,
359 NULL
360 );
361 ASSERT_EFI_ERROR (Status);
362
363 return Status;
364 }