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