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