]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/IoMmuDxe/AmdSevIoMmu.c
OvmfPkg/IoMmuDxe: propagate errors from AmdSevInstallIoMmuProtocol()
[mirror_edk2.git] / OvmfPkg / IoMmuDxe / AmdSevIoMmu.c
1 /** @file
2
3 The protocol provides support to allocate, free, map and umap a DMA buffer
4 for bus master (e.g PciHostBridge). When SEV is enabled, the DMA operations
5 must be performed on unencrypted buffer hence we use a bounce buffer to map
6 the guest buffer into an unencrypted DMA buffer.
7
8 Copyright (c) 2017, AMD Inc. All rights reserved.<BR>
9 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
10
11 This program and the accompanying materials are licensed and made available
12 under the terms and conditions of the BSD License which accompanies this
13 distribution. The full text of the license may be found at
14 http://opensource.org/licenses/bsd-license.php
15
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18
19 **/
20
21 #include "AmdSevIoMmu.h"
22
23 typedef struct {
24 EDKII_IOMMU_OPERATION Operation;
25 UINTN NumberOfBytes;
26 UINTN NumberOfPages;
27 EFI_PHYSICAL_ADDRESS CryptedAddress;
28 EFI_PHYSICAL_ADDRESS PlainTextAddress;
29 } MAP_INFO;
30
31 #define NO_MAPPING (VOID *) (UINTN) -1
32
33 /**
34 Provides the controller-specific addresses required to access system memory
35 from a DMA bus master. On SEV guest, the DMA operations must be performed on
36 shared buffer hence we allocate a bounce buffer to map the HostAddress to a
37 DeviceAddress. The Encryption attribute is removed from the DeviceAddress
38 buffer.
39
40 @param This The protocol instance pointer.
41 @param Operation Indicates if the bus master is going to read or
42 write to system memory.
43 @param HostAddress The system memory address to map to the PCI
44 controller.
45 @param NumberOfBytes On input the number of bytes to map. On output
46 the number of bytes that were mapped.
47 @param DeviceAddress The resulting map address for the bus master
48 PCI controller to use to access the hosts
49 HostAddress.
50 @param Mapping A resulting value to pass to Unmap().
51
52 @retval EFI_SUCCESS The range was mapped for the returned
53 NumberOfBytes.
54 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common
55 buffer.
56 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
57 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
58 lack of resources.
59 @retval EFI_DEVICE_ERROR The system hardware could not map the requested
60 address.
61
62 **/
63 EFI_STATUS
64 EFIAPI
65 IoMmuMap (
66 IN EDKII_IOMMU_PROTOCOL *This,
67 IN EDKII_IOMMU_OPERATION Operation,
68 IN VOID *HostAddress,
69 IN OUT UINTN *NumberOfBytes,
70 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
71 OUT VOID **Mapping
72 )
73 {
74 EFI_STATUS Status;
75 EFI_PHYSICAL_ADDRESS PhysicalAddress;
76 MAP_INFO *MapInfo;
77 EFI_PHYSICAL_ADDRESS DmaMemoryTop;
78 EFI_ALLOCATE_TYPE AllocateType;
79
80 if (HostAddress == NULL || NumberOfBytes == NULL || DeviceAddress == NULL ||
81 Mapping == NULL) {
82 return EFI_INVALID_PARAMETER;
83 }
84
85 //
86 // Make sure that Operation is valid
87 //
88 if ((UINT32) Operation >= EdkiiIoMmuOperationMaximum) {
89 return EFI_INVALID_PARAMETER;
90 }
91 PhysicalAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress;
92
93 DmaMemoryTop = (UINTN)-1;
94 AllocateType = AllocateAnyPages;
95
96 if (((Operation != EdkiiIoMmuOperationBusMasterRead64 &&
97 Operation != EdkiiIoMmuOperationBusMasterWrite64 &&
98 Operation != EdkiiIoMmuOperationBusMasterCommonBuffer64)) &&
99 ((PhysicalAddress + *NumberOfBytes) > SIZE_4GB)) {
100 //
101 // If the root bridge or the device cannot handle performing DMA above
102 // 4GB but any part of the DMA transfer being mapped is above 4GB, then
103 // map the DMA transfer to a buffer below 4GB.
104 //
105 DmaMemoryTop = SIZE_4GB - 1;
106 AllocateType = AllocateMaxAddress;
107
108 if (Operation == EdkiiIoMmuOperationBusMasterCommonBuffer ||
109 Operation == EdkiiIoMmuOperationBusMasterCommonBuffer64) {
110 //
111 // Common Buffer operations can not be remapped. If the common buffer
112 // if above 4GB, then it is not possible to generate a mapping, so
113 // return an error.
114 //
115 return EFI_UNSUPPORTED;
116 }
117 }
118
119 //
120 // CommandBuffer was allocated by us (AllocateBuffer) and is already in
121 // unencryted buffer so no need to create bounce buffer
122 //
123 if (Operation == EdkiiIoMmuOperationBusMasterCommonBuffer ||
124 Operation == EdkiiIoMmuOperationBusMasterCommonBuffer64) {
125 *Mapping = NO_MAPPING;
126 *DeviceAddress = PhysicalAddress;
127
128 return EFI_SUCCESS;
129 }
130
131 //
132 // Allocate a MAP_INFO structure to remember the mapping when Unmap() is
133 // called later.
134 //
135 MapInfo = AllocatePool (sizeof (MAP_INFO));
136 if (MapInfo == NULL) {
137 *NumberOfBytes = 0;
138 return EFI_OUT_OF_RESOURCES;
139 }
140
141 //
142 // Initialize the MAP_INFO structure
143 //
144 MapInfo->Operation = Operation;
145 MapInfo->NumberOfBytes = *NumberOfBytes;
146 MapInfo->NumberOfPages = EFI_SIZE_TO_PAGES (MapInfo->NumberOfBytes);
147 MapInfo->CryptedAddress = PhysicalAddress;
148 MapInfo->PlainTextAddress = DmaMemoryTop;
149
150 //
151 // Allocate a buffer to map the transfer to.
152 //
153 Status = gBS->AllocatePages (
154 AllocateType,
155 EfiBootServicesData,
156 MapInfo->NumberOfPages,
157 &MapInfo->PlainTextAddress
158 );
159 if (EFI_ERROR (Status)) {
160 FreePool (MapInfo);
161 *NumberOfBytes = 0;
162 return Status;
163 }
164
165 //
166 // Clear the memory encryption mask from the device buffer
167 //
168 Status = MemEncryptSevClearPageEncMask (
169 0,
170 MapInfo->PlainTextAddress,
171 MapInfo->NumberOfPages,
172 TRUE
173 );
174 ASSERT_EFI_ERROR(Status);
175
176 //
177 // If this is a read operation from the Bus Master's point of view,
178 // then copy the contents of the real buffer into the mapped buffer
179 // so the Bus Master can read the contents of the real buffer.
180 //
181 if (Operation == EdkiiIoMmuOperationBusMasterRead ||
182 Operation == EdkiiIoMmuOperationBusMasterRead64) {
183 CopyMem (
184 (VOID *) (UINTN) MapInfo->PlainTextAddress,
185 (VOID *) (UINTN) MapInfo->CryptedAddress,
186 MapInfo->NumberOfBytes
187 );
188 }
189
190 //
191 // The DeviceAddress is the address of the maped buffer below 4GB
192 //
193 *DeviceAddress = MapInfo->PlainTextAddress;
194
195 //
196 // Return a pointer to the MAP_INFO structure in Mapping
197 //
198 *Mapping = MapInfo;
199
200 DEBUG ((
201 DEBUG_VERBOSE,
202 "%a PlainText 0x%Lx Crypted 0x%Lx Pages 0x%Lx Bytes 0x%Lx\n",
203 __FUNCTION__,
204 MapInfo->PlainTextAddress,
205 MapInfo->CryptedAddress,
206 (UINT64)MapInfo->NumberOfPages,
207 (UINT64)MapInfo->NumberOfBytes
208 ));
209
210 return EFI_SUCCESS;
211 }
212
213 /**
214 Completes the Map() operation and releases any corresponding resources.
215
216 @param This The protocol instance pointer.
217 @param Mapping The mapping value returned from Map().
218
219 @retval EFI_SUCCESS The range was unmapped.
220 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by
221 Map().
222 @retval EFI_DEVICE_ERROR The data was not committed to the target system
223 memory.
224 **/
225 EFI_STATUS
226 EFIAPI
227 IoMmuUnmap (
228 IN EDKII_IOMMU_PROTOCOL *This,
229 IN VOID *Mapping
230 )
231 {
232 MAP_INFO *MapInfo;
233 EFI_STATUS Status;
234
235 if (Mapping == NULL) {
236 return EFI_INVALID_PARAMETER;
237 }
238
239 //
240 // See if the Map() operation associated with this Unmap() required a mapping
241 // buffer. If a mapping buffer was not required, then this function simply
242 // buffer. If a mapping buffer was not required, then this function simply
243 //
244 if (Mapping == NO_MAPPING) {
245 return EFI_SUCCESS;
246 }
247
248 MapInfo = (MAP_INFO *)Mapping;
249
250 //
251 // If this is a write operation from the Bus Master's point of view,
252 // then copy the contents of the mapped buffer into the real buffer
253 // so the processor can read the contents of the real buffer.
254 //
255 if (MapInfo->Operation == EdkiiIoMmuOperationBusMasterWrite ||
256 MapInfo->Operation == EdkiiIoMmuOperationBusMasterWrite64) {
257 CopyMem (
258 (VOID *) (UINTN) MapInfo->CryptedAddress,
259 (VOID *) (UINTN) MapInfo->PlainTextAddress,
260 MapInfo->NumberOfBytes
261 );
262 }
263
264 DEBUG ((
265 DEBUG_VERBOSE,
266 "%a PlainText 0x%Lx Crypted 0x%Lx Pages 0x%Lx Bytes 0x%Lx\n",
267 __FUNCTION__,
268 MapInfo->PlainTextAddress,
269 MapInfo->CryptedAddress,
270 (UINT64)MapInfo->NumberOfPages,
271 (UINT64)MapInfo->NumberOfBytes
272 ));
273 //
274 // Restore the memory encryption mask
275 //
276 Status = MemEncryptSevSetPageEncMask (
277 0,
278 MapInfo->PlainTextAddress,
279 MapInfo->NumberOfPages,
280 TRUE
281 );
282 ASSERT_EFI_ERROR(Status);
283
284 //
285 // Free the mapped buffer and the MAP_INFO structure.
286 //
287 gBS->FreePages (MapInfo->PlainTextAddress, MapInfo->NumberOfPages);
288 FreePool (Mapping);
289 return EFI_SUCCESS;
290 }
291
292 /**
293 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
294 OperationBusMasterCommonBuffer64 mapping.
295
296 @param This The protocol instance pointer.
297 @param Type This parameter is not used and must be ignored.
298 @param MemoryType The type of memory to allocate,
299 EfiBootServicesData or EfiRuntimeServicesData.
300 @param Pages The number of pages to allocate.
301 @param HostAddress A pointer to store the base system memory
302 address of the allocated range.
303 @param Attributes The requested bit mask of attributes for the
304 allocated range.
305
306 @retval EFI_SUCCESS The requested memory pages were allocated.
307 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal
308 attribute bits are MEMORY_WRITE_COMBINE and
309 MEMORY_CACHED.
310 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
311 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
312
313 **/
314 EFI_STATUS
315 EFIAPI
316 IoMmuAllocateBuffer (
317 IN EDKII_IOMMU_PROTOCOL *This,
318 IN EFI_ALLOCATE_TYPE Type,
319 IN EFI_MEMORY_TYPE MemoryType,
320 IN UINTN Pages,
321 IN OUT VOID **HostAddress,
322 IN UINT64 Attributes
323 )
324 {
325 EFI_STATUS Status;
326 EFI_PHYSICAL_ADDRESS PhysicalAddress;
327
328 //
329 // Validate Attributes
330 //
331 if ((Attributes & EDKII_IOMMU_ATTRIBUTE_INVALID_FOR_ALLOCATE_BUFFER) != 0) {
332 return EFI_UNSUPPORTED;
333 }
334
335 //
336 // Check for invalid inputs
337 //
338 if (HostAddress == NULL) {
339 return EFI_INVALID_PARAMETER;
340 }
341
342 //
343 // The only valid memory types are EfiBootServicesData and
344 // EfiRuntimeServicesData
345 //
346 if (MemoryType != EfiBootServicesData &&
347 MemoryType != EfiRuntimeServicesData) {
348 return EFI_INVALID_PARAMETER;
349 }
350
351 PhysicalAddress = (UINTN)-1;
352 if ((Attributes & EDKII_IOMMU_ATTRIBUTE_DUAL_ADDRESS_CYCLE) == 0) {
353 //
354 // Limit allocations to memory below 4GB
355 //
356 PhysicalAddress = SIZE_4GB - 1;
357 }
358 Status = gBS->AllocatePages (
359 AllocateMaxAddress,
360 MemoryType,
361 Pages,
362 &PhysicalAddress
363 );
364 if (!EFI_ERROR (Status)) {
365 *HostAddress = (VOID *) (UINTN) PhysicalAddress;
366
367 //
368 // Clear memory encryption mask
369 //
370 Status = MemEncryptSevClearPageEncMask (0, PhysicalAddress, Pages, TRUE);
371 ASSERT_EFI_ERROR(Status);
372 }
373
374 DEBUG ((
375 DEBUG_VERBOSE,
376 "%a Address 0x%Lx Pages 0x%Lx\n",
377 __FUNCTION__,
378 PhysicalAddress,
379 (UINT64)Pages
380 ));
381 return Status;
382 }
383
384 /**
385 Frees memory that was allocated with AllocateBuffer().
386
387 @param This The protocol instance pointer.
388 @param Pages The number of pages to free.
389 @param HostAddress The base system memory address of the allocated
390 range.
391
392 @retval EFI_SUCCESS The requested memory pages were freed.
393 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and
394 Pages was not allocated with AllocateBuffer().
395
396 **/
397 EFI_STATUS
398 EFIAPI
399 IoMmuFreeBuffer (
400 IN EDKII_IOMMU_PROTOCOL *This,
401 IN UINTN Pages,
402 IN VOID *HostAddress
403 )
404 {
405 EFI_STATUS Status;
406
407 //
408 // Set memory encryption mask
409 //
410 Status = MemEncryptSevSetPageEncMask (
411 0,
412 (EFI_PHYSICAL_ADDRESS)(UINTN)HostAddress,
413 Pages,
414 TRUE
415 );
416 ASSERT_EFI_ERROR(Status);
417
418 DEBUG ((
419 DEBUG_VERBOSE,
420 "%a Address 0x%Lx Pages 0x%Lx\n",
421 __FUNCTION__,
422 (UINT64)(UINTN)HostAddress,
423 (UINT64)Pages
424 ));
425 return gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress, Pages);
426 }
427
428
429 /**
430 Set IOMMU attribute for a system memory.
431
432 If the IOMMU protocol exists, the system memory cannot be used
433 for DMA by default.
434
435 When a device requests a DMA access for a system memory,
436 the device driver need use SetAttribute() to update the IOMMU
437 attribute to request DMA access (read and/or write).
438
439 The DeviceHandle is used to identify which device submits the request.
440 The IOMMU implementation need translate the device path to an IOMMU device
441 ID, and set IOMMU hardware register accordingly.
442 1) DeviceHandle can be a standard PCI device.
443 The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.
444 The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.
445 The memory for BusMasterCommonBuffer need set
446 EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.
447 After the memory is used, the memory need set 0 to keep it being
448 protected.
449 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).
450 The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or
451 EDKII_IOMMU_ACCESS_WRITE.
452
453 @param[in] This The protocol instance pointer.
454 @param[in] DeviceHandle The device who initiates the DMA access
455 request.
456 @param[in] Mapping The mapping value returned from Map().
457 @param[in] IoMmuAccess The IOMMU access.
458
459 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range
460 specified by DeviceAddress and Length.
461 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
462 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by
463 Map().
464 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination
465 of access.
466 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
467 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported
468 by the IOMMU.
469 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range
470 specified by Mapping.
471 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
472 modify the IOMMU access.
473 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while
474 attempting the operation.
475
476 **/
477 EFI_STATUS
478 EFIAPI
479 IoMmuSetAttribute (
480 IN EDKII_IOMMU_PROTOCOL *This,
481 IN EFI_HANDLE DeviceHandle,
482 IN VOID *Mapping,
483 IN UINT64 IoMmuAccess
484 )
485 {
486 return EFI_UNSUPPORTED;
487 }
488
489 EDKII_IOMMU_PROTOCOL mAmdSev = {
490 EDKII_IOMMU_PROTOCOL_REVISION,
491 IoMmuSetAttribute,
492 IoMmuMap,
493 IoMmuUnmap,
494 IoMmuAllocateBuffer,
495 IoMmuFreeBuffer,
496 };
497
498 /**
499 Initialize Iommu Protocol.
500
501 **/
502 EFI_STATUS
503 EFIAPI
504 AmdSevInstallIoMmuProtocol (
505 VOID
506 )
507 {
508 EFI_STATUS Status;
509 EFI_HANDLE Handle;
510
511 Handle = NULL;
512 Status = gBS->InstallMultipleProtocolInterfaces (
513 &Handle,
514 &gEdkiiIoMmuProtocolGuid, &mAmdSev,
515 NULL
516 );
517 return Status;
518 }