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