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.
8 Copyright (c) 2017, AMD Inc. All rights reserved.<BR>
9 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
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
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.
21 #include "AmdSevIoMmu.h"
23 #define MAP_INFO_SIG SIGNATURE_64 ('M', 'A', 'P', '_', 'I', 'N', 'F', 'O')
28 EDKII_IOMMU_OPERATION Operation
;
31 EFI_PHYSICAL_ADDRESS CryptedAddress
;
32 EFI_PHYSICAL_ADDRESS PlainTextAddress
;
36 // List of MAP_INFO structures recycled by Unmap().
38 // Recycled MAP_INFO structures are equally good for future recycling and
41 STATIC LIST_ENTRY mRecycledMapInfos
= INITIALIZE_LIST_HEAD_VARIABLE (
45 #define COMMON_BUFFER_SIG SIGNATURE_64 ('C', 'M', 'N', 'B', 'U', 'F', 'F', 'R')
48 // The following structure enables Map() and Unmap() to perform in-place
49 // decryption and encryption, respectively, for BusMasterCommonBuffer[64]
50 // operations, without dynamic memory allocation or release.
52 // Both COMMON_BUFFER_HEADER and COMMON_BUFFER_HEADER.StashBuffer are allocated
53 // by AllocateBuffer() and released by FreeBuffer().
60 // Always allocated from EfiBootServicesData type memory, and always
66 // Followed by the actual common buffer, starting at the next page.
68 } COMMON_BUFFER_HEADER
;
72 Provides the controller-specific addresses required to access system memory
73 from a DMA bus master. On SEV guest, the DMA operations must be performed on
74 shared buffer hence we allocate a bounce buffer to map the HostAddress to a
75 DeviceAddress. The Encryption attribute is removed from the DeviceAddress
78 @param This The protocol instance pointer.
79 @param Operation Indicates if the bus master is going to read or
80 write to system memory.
81 @param HostAddress The system memory address to map to the PCI
83 @param NumberOfBytes On input the number of bytes to map. On output
84 the number of bytes that were mapped.
85 @param DeviceAddress The resulting map address for the bus master
86 PCI controller to use to access the hosts
88 @param Mapping A resulting value to pass to Unmap().
90 @retval EFI_SUCCESS The range was mapped for the returned
92 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common
94 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
95 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
97 @retval EFI_DEVICE_ERROR The system hardware could not map the requested
104 IN EDKII_IOMMU_PROTOCOL
*This
,
105 IN EDKII_IOMMU_OPERATION Operation
,
106 IN VOID
*HostAddress
,
107 IN OUT UINTN
*NumberOfBytes
,
108 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
113 LIST_ENTRY
*RecycledMapInfo
;
115 EFI_ALLOCATE_TYPE AllocateType
;
116 COMMON_BUFFER_HEADER
*CommonBufferHeader
;
117 VOID
*DecryptionSource
;
119 if (HostAddress
== NULL
|| NumberOfBytes
== NULL
|| DeviceAddress
== NULL
||
121 return EFI_INVALID_PARAMETER
;
125 // Allocate a MAP_INFO structure to remember the mapping when Unmap() is
128 RecycledMapInfo
= GetFirstNode (&mRecycledMapInfos
);
129 if (RecycledMapInfo
== &mRecycledMapInfos
) {
131 // No recycled MAP_INFO structure, allocate a new one.
133 MapInfo
= AllocatePool (sizeof (MAP_INFO
));
134 if (MapInfo
== NULL
) {
135 Status
= EFI_OUT_OF_RESOURCES
;
139 MapInfo
= CR (RecycledMapInfo
, MAP_INFO
, Link
, MAP_INFO_SIG
);
140 RemoveEntryList (RecycledMapInfo
);
144 // Initialize the MAP_INFO structure, except the PlainTextAddress field
146 ZeroMem (&MapInfo
->Link
, sizeof MapInfo
->Link
);
147 MapInfo
->Signature
= MAP_INFO_SIG
;
148 MapInfo
->Operation
= Operation
;
149 MapInfo
->NumberOfBytes
= *NumberOfBytes
;
150 MapInfo
->NumberOfPages
= EFI_SIZE_TO_PAGES (MapInfo
->NumberOfBytes
);
151 MapInfo
->CryptedAddress
= (UINTN
)HostAddress
;
154 // In the switch statement below, we point "MapInfo->PlainTextAddress" to the
155 // plaintext buffer, according to Operation. We also set "DecryptionSource".
157 MapInfo
->PlainTextAddress
= MAX_ADDRESS
;
158 AllocateType
= AllocateAnyPages
;
159 DecryptionSource
= (VOID
*)(UINTN
)MapInfo
->CryptedAddress
;
162 // For BusMasterRead[64] and BusMasterWrite[64] operations, a bounce buffer
163 // is necessary regardless of whether the original (crypted) buffer crosses
164 // the 4GB limit or not -- we have to allocate a separate plaintext buffer.
165 // The only variable is whether the plaintext buffer should be under 4GB.
167 case EdkiiIoMmuOperationBusMasterRead
:
168 case EdkiiIoMmuOperationBusMasterWrite
:
169 MapInfo
->PlainTextAddress
= BASE_4GB
- 1;
170 AllocateType
= AllocateMaxAddress
;
174 case EdkiiIoMmuOperationBusMasterRead64
:
175 case EdkiiIoMmuOperationBusMasterWrite64
:
177 // Allocate the implicit plaintext bounce buffer.
179 Status
= gBS
->AllocatePages (
182 MapInfo
->NumberOfPages
,
183 &MapInfo
->PlainTextAddress
185 if (EFI_ERROR (Status
)) {
191 // For BusMasterCommonBuffer[64] operations, a to-be-plaintext buffer and a
192 // stash buffer (for in-place decryption) have been allocated already, with
193 // AllocateBuffer(). We only check whether the address of the to-be-plaintext
194 // buffer is low enough for the requested operation.
196 case EdkiiIoMmuOperationBusMasterCommonBuffer
:
197 if ((MapInfo
->CryptedAddress
> BASE_4GB
) ||
198 (EFI_PAGES_TO_SIZE (MapInfo
->NumberOfPages
) >
199 BASE_4GB
- MapInfo
->CryptedAddress
)) {
201 // CommonBuffer operations cannot be remapped. If the common buffer is
202 // above 4GB, then it is not possible to generate a mapping, so return an
205 Status
= EFI_UNSUPPORTED
;
211 case EdkiiIoMmuOperationBusMasterCommonBuffer64
:
213 // The buffer at MapInfo->CryptedAddress comes from AllocateBuffer().
215 MapInfo
->PlainTextAddress
= MapInfo
->CryptedAddress
;
217 // Stash the crypted data.
219 CommonBufferHeader
= (COMMON_BUFFER_HEADER
*)(
220 (UINTN
)MapInfo
->CryptedAddress
- EFI_PAGE_SIZE
222 ASSERT (CommonBufferHeader
->Signature
== COMMON_BUFFER_SIG
);
224 CommonBufferHeader
->StashBuffer
,
225 (VOID
*)(UINTN
)MapInfo
->CryptedAddress
,
226 MapInfo
->NumberOfBytes
229 // Point "DecryptionSource" to the stash buffer so that we decrypt
230 // it to the original location, after the switch statement.
232 DecryptionSource
= CommonBufferHeader
->StashBuffer
;
237 // Operation is invalid
239 Status
= EFI_INVALID_PARAMETER
;
244 // Clear the memory encryption mask on the plaintext buffer.
246 Status
= MemEncryptSevClearPageEncMask (
248 MapInfo
->PlainTextAddress
,
249 MapInfo
->NumberOfPages
,
252 ASSERT_EFI_ERROR (Status
);
253 if (EFI_ERROR (Status
)) {
258 // If this is a read operation from the Bus Master's point of view,
259 // then copy the contents of the real buffer into the mapped buffer
260 // so the Bus Master can read the contents of the real buffer.
262 // For BusMasterCommonBuffer[64] operations, the CopyMem() below will decrypt
263 // the original data (from the stash buffer) back to the original location.
265 if (Operation
== EdkiiIoMmuOperationBusMasterRead
||
266 Operation
== EdkiiIoMmuOperationBusMasterRead64
||
267 Operation
== EdkiiIoMmuOperationBusMasterCommonBuffer
||
268 Operation
== EdkiiIoMmuOperationBusMasterCommonBuffer64
) {
270 (VOID
*) (UINTN
) MapInfo
->PlainTextAddress
,
272 MapInfo
->NumberOfBytes
277 // Populate output parameters.
279 *DeviceAddress
= MapInfo
->PlainTextAddress
;
284 "%a PlainText 0x%Lx Crypted 0x%Lx Pages 0x%Lx Bytes 0x%Lx\n",
286 MapInfo
->PlainTextAddress
,
287 MapInfo
->CryptedAddress
,
288 (UINT64
)MapInfo
->NumberOfPages
,
289 (UINT64
)MapInfo
->NumberOfBytes
303 Completes the Map() operation and releases any corresponding resources.
305 @param This The protocol instance pointer.
306 @param Mapping The mapping value returned from Map().
308 @retval EFI_SUCCESS The range was unmapped.
309 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by
311 @retval EFI_DEVICE_ERROR The data was not committed to the target system
317 IN EDKII_IOMMU_PROTOCOL
*This
,
323 COMMON_BUFFER_HEADER
*CommonBufferHeader
;
324 VOID
*EncryptionTarget
;
326 if (Mapping
== NULL
) {
327 return EFI_INVALID_PARAMETER
;
330 MapInfo
= (MAP_INFO
*)Mapping
;
333 // set CommonBufferHeader to suppress incorrect compiler/analyzer warnings
335 CommonBufferHeader
= NULL
;
338 // For BusMasterWrite[64] operations and BusMasterCommonBuffer[64] operations
339 // we have to encrypt the results, ultimately to the original place (i.e.,
340 // "MapInfo->CryptedAddress").
342 // For BusMasterCommonBuffer[64] operations however, this encryption has to
343 // land in-place, so divert the encryption to the stash buffer first.
345 EncryptionTarget
= (VOID
*)(UINTN
)MapInfo
->CryptedAddress
;
347 switch (MapInfo
->Operation
) {
348 case EdkiiIoMmuOperationBusMasterCommonBuffer
:
349 case EdkiiIoMmuOperationBusMasterCommonBuffer64
:
350 ASSERT (MapInfo
->PlainTextAddress
== MapInfo
->CryptedAddress
);
352 CommonBufferHeader
= (COMMON_BUFFER_HEADER
*)(
353 (UINTN
)MapInfo
->PlainTextAddress
- EFI_PAGE_SIZE
355 ASSERT (CommonBufferHeader
->Signature
== COMMON_BUFFER_SIG
);
356 EncryptionTarget
= CommonBufferHeader
->StashBuffer
;
361 case EdkiiIoMmuOperationBusMasterWrite
:
362 case EdkiiIoMmuOperationBusMasterWrite64
:
365 (VOID
*) (UINTN
) MapInfo
->PlainTextAddress
,
366 MapInfo
->NumberOfBytes
372 // nothing to encrypt after BusMasterRead[64] operations
379 "%a PlainText 0x%Lx Crypted 0x%Lx Pages 0x%Lx Bytes 0x%Lx\n",
381 MapInfo
->PlainTextAddress
,
382 MapInfo
->CryptedAddress
,
383 (UINT64
)MapInfo
->NumberOfPages
,
384 (UINT64
)MapInfo
->NumberOfBytes
388 // Restore the memory encryption mask on the area we used to hold the
391 Status
= MemEncryptSevSetPageEncMask (
393 MapInfo
->PlainTextAddress
,
394 MapInfo
->NumberOfPages
,
397 ASSERT_EFI_ERROR (Status
);
398 if (EFI_ERROR (Status
)) {
403 // For BusMasterCommonBuffer[64] operations, copy the stashed data to the
404 // original (now encrypted) location.
406 // For all other operations, fill the late bounce buffer (which existed as
407 // plaintext at some point) with zeros, and then release it.
409 if (MapInfo
->Operation
== EdkiiIoMmuOperationBusMasterCommonBuffer
||
410 MapInfo
->Operation
== EdkiiIoMmuOperationBusMasterCommonBuffer64
) {
412 (VOID
*)(UINTN
)MapInfo
->CryptedAddress
,
413 CommonBufferHeader
->StashBuffer
,
414 MapInfo
->NumberOfBytes
418 // Recycle the MAP_INFO structure.
420 InsertTailList (&mRecycledMapInfos
, &MapInfo
->Link
);
423 (VOID
*)(UINTN
)MapInfo
->PlainTextAddress
,
424 EFI_PAGES_TO_SIZE (MapInfo
->NumberOfPages
)
426 gBS
->FreePages (MapInfo
->PlainTextAddress
, MapInfo
->NumberOfPages
);
429 // Free the MAP_INFO structure.
438 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
439 OperationBusMasterCommonBuffer64 mapping.
441 @param This The protocol instance pointer.
442 @param Type This parameter is not used and must be ignored.
443 @param MemoryType The type of memory to allocate,
444 EfiBootServicesData or EfiRuntimeServicesData.
445 @param Pages The number of pages to allocate.
446 @param HostAddress A pointer to store the base system memory
447 address of the allocated range.
448 @param Attributes The requested bit mask of attributes for the
451 @retval EFI_SUCCESS The requested memory pages were allocated.
452 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal
453 attribute bits are MEMORY_WRITE_COMBINE and
455 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
456 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
461 IoMmuAllocateBuffer (
462 IN EDKII_IOMMU_PROTOCOL
*This
,
463 IN EFI_ALLOCATE_TYPE Type
,
464 IN EFI_MEMORY_TYPE MemoryType
,
466 IN OUT VOID
**HostAddress
,
471 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
473 UINTN CommonBufferPages
;
474 COMMON_BUFFER_HEADER
*CommonBufferHeader
;
477 // Validate Attributes
479 if ((Attributes
& EDKII_IOMMU_ATTRIBUTE_INVALID_FOR_ALLOCATE_BUFFER
) != 0) {
480 return EFI_UNSUPPORTED
;
484 // Check for invalid inputs
486 if (HostAddress
== NULL
) {
487 return EFI_INVALID_PARAMETER
;
491 // The only valid memory types are EfiBootServicesData and
492 // EfiRuntimeServicesData
494 if (MemoryType
!= EfiBootServicesData
&&
495 MemoryType
!= EfiRuntimeServicesData
) {
496 return EFI_INVALID_PARAMETER
;
500 // We'll need a header page for the COMMON_BUFFER_HEADER structure.
502 if (Pages
> MAX_UINTN
- 1) {
503 return EFI_OUT_OF_RESOURCES
;
505 CommonBufferPages
= Pages
+ 1;
508 // Allocate the stash in EfiBootServicesData type memory.
510 // Map() will temporarily save encrypted data in the stash for
511 // BusMasterCommonBuffer[64] operations, so the data can be decrypted to the
512 // original location.
514 // Unmap() will temporarily save plaintext data in the stash for
515 // BusMasterCommonBuffer[64] operations, so the data can be encrypted to the
516 // original location.
518 // StashBuffer always resides in encrypted memory.
520 StashBuffer
= AllocatePages (Pages
);
521 if (StashBuffer
== NULL
) {
522 return EFI_OUT_OF_RESOURCES
;
525 PhysicalAddress
= (UINTN
)-1;
526 if ((Attributes
& EDKII_IOMMU_ATTRIBUTE_DUAL_ADDRESS_CYCLE
) == 0) {
528 // Limit allocations to memory below 4GB
530 PhysicalAddress
= SIZE_4GB
- 1;
532 Status
= gBS
->AllocatePages (
538 if (EFI_ERROR (Status
)) {
539 goto FreeStashBuffer
;
542 CommonBufferHeader
= (VOID
*)(UINTN
)PhysicalAddress
;
543 PhysicalAddress
+= EFI_PAGE_SIZE
;
545 CommonBufferHeader
->Signature
= COMMON_BUFFER_SIG
;
546 CommonBufferHeader
->StashBuffer
= StashBuffer
;
548 *HostAddress
= (VOID
*)(UINTN
)PhysicalAddress
;
552 "%a Address 0x%Lx Pages 0x%Lx\n",
560 FreePages (StashBuffer
, Pages
);
565 Frees memory that was allocated with AllocateBuffer().
567 @param This The protocol instance pointer.
568 @param Pages The number of pages to free.
569 @param HostAddress The base system memory address of the allocated
572 @retval EFI_SUCCESS The requested memory pages were freed.
573 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and
574 Pages was not allocated with AllocateBuffer().
580 IN EDKII_IOMMU_PROTOCOL
*This
,
585 UINTN CommonBufferPages
;
586 COMMON_BUFFER_HEADER
*CommonBufferHeader
;
588 CommonBufferPages
= Pages
+ 1;
589 CommonBufferHeader
= (COMMON_BUFFER_HEADER
*)(
590 (UINTN
)HostAddress
- EFI_PAGE_SIZE
594 // Check the signature.
596 ASSERT (CommonBufferHeader
->Signature
== COMMON_BUFFER_SIG
);
597 if (CommonBufferHeader
->Signature
!= COMMON_BUFFER_SIG
) {
598 return EFI_INVALID_PARAMETER
;
602 // Free the stash buffer. This buffer was always encrypted, so no need to
605 FreePages (CommonBufferHeader
->StashBuffer
, Pages
);
609 "%a Address 0x%Lx Pages 0x%Lx\n",
611 (UINT64
)(UINTN
)HostAddress
,
616 // Release the common buffer itself. Unmap() has re-encrypted it in-place, so
617 // no need to zero it.
619 return gBS
->FreePages ((UINTN
)CommonBufferHeader
, CommonBufferPages
);
624 Set IOMMU attribute for a system memory.
626 If the IOMMU protocol exists, the system memory cannot be used
629 When a device requests a DMA access for a system memory,
630 the device driver need use SetAttribute() to update the IOMMU
631 attribute to request DMA access (read and/or write).
633 The DeviceHandle is used to identify which device submits the request.
634 The IOMMU implementation need translate the device path to an IOMMU device
635 ID, and set IOMMU hardware register accordingly.
636 1) DeviceHandle can be a standard PCI device.
637 The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.
638 The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.
639 The memory for BusMasterCommonBuffer need set
640 EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.
641 After the memory is used, the memory need set 0 to keep it being
643 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).
644 The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or
645 EDKII_IOMMU_ACCESS_WRITE.
647 @param[in] This The protocol instance pointer.
648 @param[in] DeviceHandle The device who initiates the DMA access
650 @param[in] Mapping The mapping value returned from Map().
651 @param[in] IoMmuAccess The IOMMU access.
653 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range
654 specified by DeviceAddress and Length.
655 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
656 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by
658 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination
660 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
661 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported
663 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range
664 specified by Mapping.
665 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
666 modify the IOMMU access.
667 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while
668 attempting the operation.
674 IN EDKII_IOMMU_PROTOCOL
*This
,
675 IN EFI_HANDLE DeviceHandle
,
677 IN UINT64 IoMmuAccess
680 return EFI_UNSUPPORTED
;
683 EDKII_IOMMU_PROTOCOL mAmdSev
= {
684 EDKII_IOMMU_PROTOCOL_REVISION
,
693 Initialize Iommu Protocol.
698 AmdSevInstallIoMmuProtocol (
706 Status
= gBS
->InstallMultipleProtocolInterfaces (
708 &gEdkiiIoMmuProtocolGuid
, &mAmdSev
,