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