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"
24 EDKII_IOMMU_OPERATION Operation
;
27 EFI_PHYSICAL_ADDRESS CryptedAddress
;
28 EFI_PHYSICAL_ADDRESS PlainTextAddress
;
31 #define NO_MAPPING (VOID *) (UINTN) -1
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
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
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
50 @param Mapping A resulting value to pass to Unmap().
52 @retval EFI_SUCCESS The range was mapped for the returned
54 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common
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
59 @retval EFI_DEVICE_ERROR The system hardware could not map the requested
66 IN EDKII_IOMMU_PROTOCOL
*This
,
67 IN EDKII_IOMMU_OPERATION Operation
,
69 IN OUT UINTN
*NumberOfBytes
,
70 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
75 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
77 EFI_PHYSICAL_ADDRESS DmaMemoryTop
;
78 EFI_ALLOCATE_TYPE AllocateType
;
80 if (HostAddress
== NULL
|| NumberOfBytes
== NULL
|| DeviceAddress
== NULL
||
82 return EFI_INVALID_PARAMETER
;
86 // Make sure that Operation is valid
88 if ((UINT32
) Operation
>= EdkiiIoMmuOperationMaximum
) {
89 return EFI_INVALID_PARAMETER
;
91 PhysicalAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
;
93 DmaMemoryTop
= (UINTN
)-1;
94 AllocateType
= AllocateAnyPages
;
96 if (((Operation
!= EdkiiIoMmuOperationBusMasterRead64
&&
97 Operation
!= EdkiiIoMmuOperationBusMasterWrite64
&&
98 Operation
!= EdkiiIoMmuOperationBusMasterCommonBuffer64
)) &&
99 ((PhysicalAddress
+ *NumberOfBytes
) > SIZE_4GB
)) {
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.
105 DmaMemoryTop
= SIZE_4GB
- 1;
106 AllocateType
= AllocateMaxAddress
;
108 if (Operation
== EdkiiIoMmuOperationBusMasterCommonBuffer
||
109 Operation
== EdkiiIoMmuOperationBusMasterCommonBuffer64
) {
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
115 return EFI_UNSUPPORTED
;
120 // CommandBuffer was allocated by us (AllocateBuffer) and is already in
121 // unencryted buffer so no need to create bounce buffer
123 if (Operation
== EdkiiIoMmuOperationBusMasterCommonBuffer
||
124 Operation
== EdkiiIoMmuOperationBusMasterCommonBuffer64
) {
125 *Mapping
= NO_MAPPING
;
126 *DeviceAddress
= PhysicalAddress
;
132 // Allocate a MAP_INFO structure to remember the mapping when Unmap() is
135 MapInfo
= AllocatePool (sizeof (MAP_INFO
));
136 if (MapInfo
== NULL
) {
138 return EFI_OUT_OF_RESOURCES
;
142 // Initialize the MAP_INFO structure
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
;
151 // Allocate a buffer to map the transfer to.
153 Status
= gBS
->AllocatePages (
156 MapInfo
->NumberOfPages
,
157 &MapInfo
->PlainTextAddress
159 if (EFI_ERROR (Status
)) {
166 // Clear the memory encryption mask from the device buffer
168 Status
= MemEncryptSevClearPageEncMask (
170 MapInfo
->PlainTextAddress
,
171 MapInfo
->NumberOfPages
,
174 ASSERT_EFI_ERROR(Status
);
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.
181 if (Operation
== EdkiiIoMmuOperationBusMasterRead
||
182 Operation
== EdkiiIoMmuOperationBusMasterRead64
) {
184 (VOID
*) (UINTN
) MapInfo
->PlainTextAddress
,
185 (VOID
*) (UINTN
) MapInfo
->CryptedAddress
,
186 MapInfo
->NumberOfBytes
191 // The DeviceAddress is the address of the maped buffer below 4GB
193 *DeviceAddress
= MapInfo
->PlainTextAddress
;
196 // Return a pointer to the MAP_INFO structure in Mapping
202 "%a PlainText 0x%Lx Crypted 0x%Lx Pages 0x%Lx Bytes 0x%Lx\n",
204 MapInfo
->PlainTextAddress
,
205 MapInfo
->CryptedAddress
,
206 (UINT64
)MapInfo
->NumberOfPages
,
207 (UINT64
)MapInfo
->NumberOfBytes
214 Completes the Map() operation and releases any corresponding resources.
216 @param This The protocol instance pointer.
217 @param Mapping The mapping value returned from Map().
219 @retval EFI_SUCCESS The range was unmapped.
220 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by
222 @retval EFI_DEVICE_ERROR The data was not committed to the target system
228 IN EDKII_IOMMU_PROTOCOL
*This
,
235 if (Mapping
== NULL
) {
236 return EFI_INVALID_PARAMETER
;
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
244 if (Mapping
== NO_MAPPING
) {
248 MapInfo
= (MAP_INFO
*)Mapping
;
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.
255 if (MapInfo
->Operation
== EdkiiIoMmuOperationBusMasterWrite
||
256 MapInfo
->Operation
== EdkiiIoMmuOperationBusMasterWrite64
) {
258 (VOID
*) (UINTN
) MapInfo
->CryptedAddress
,
259 (VOID
*) (UINTN
) MapInfo
->PlainTextAddress
,
260 MapInfo
->NumberOfBytes
266 "%a PlainText 0x%Lx Crypted 0x%Lx Pages 0x%Lx Bytes 0x%Lx\n",
268 MapInfo
->PlainTextAddress
,
269 MapInfo
->CryptedAddress
,
270 (UINT64
)MapInfo
->NumberOfPages
,
271 (UINT64
)MapInfo
->NumberOfBytes
274 // Restore the memory encryption mask
276 Status
= MemEncryptSevSetPageEncMask (
278 MapInfo
->PlainTextAddress
,
279 MapInfo
->NumberOfPages
,
282 ASSERT_EFI_ERROR(Status
);
284 (VOID
*)(UINTN
)MapInfo
->PlainTextAddress
,
285 EFI_PAGES_TO_SIZE (MapInfo
->NumberOfPages
)
289 // Free the mapped buffer and the MAP_INFO structure.
291 gBS
->FreePages (MapInfo
->PlainTextAddress
, MapInfo
->NumberOfPages
);
297 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
298 OperationBusMasterCommonBuffer64 mapping.
300 @param This The protocol instance pointer.
301 @param Type This parameter is not used and must be ignored.
302 @param MemoryType The type of memory to allocate,
303 EfiBootServicesData or EfiRuntimeServicesData.
304 @param Pages The number of pages to allocate.
305 @param HostAddress A pointer to store the base system memory
306 address of the allocated range.
307 @param Attributes The requested bit mask of attributes for the
310 @retval EFI_SUCCESS The requested memory pages were allocated.
311 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal
312 attribute bits are MEMORY_WRITE_COMBINE and
314 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
315 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
320 IoMmuAllocateBuffer (
321 IN EDKII_IOMMU_PROTOCOL
*This
,
322 IN EFI_ALLOCATE_TYPE Type
,
323 IN EFI_MEMORY_TYPE MemoryType
,
325 IN OUT VOID
**HostAddress
,
330 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
333 // Validate Attributes
335 if ((Attributes
& EDKII_IOMMU_ATTRIBUTE_INVALID_FOR_ALLOCATE_BUFFER
) != 0) {
336 return EFI_UNSUPPORTED
;
340 // Check for invalid inputs
342 if (HostAddress
== NULL
) {
343 return EFI_INVALID_PARAMETER
;
347 // The only valid memory types are EfiBootServicesData and
348 // EfiRuntimeServicesData
350 if (MemoryType
!= EfiBootServicesData
&&
351 MemoryType
!= EfiRuntimeServicesData
) {
352 return EFI_INVALID_PARAMETER
;
355 PhysicalAddress
= (UINTN
)-1;
356 if ((Attributes
& EDKII_IOMMU_ATTRIBUTE_DUAL_ADDRESS_CYCLE
) == 0) {
358 // Limit allocations to memory below 4GB
360 PhysicalAddress
= SIZE_4GB
- 1;
362 Status
= gBS
->AllocatePages (
368 if (!EFI_ERROR (Status
)) {
369 *HostAddress
= (VOID
*) (UINTN
) PhysicalAddress
;
372 // Clear memory encryption mask
374 Status
= MemEncryptSevClearPageEncMask (0, PhysicalAddress
, Pages
, TRUE
);
375 ASSERT_EFI_ERROR(Status
);
380 "%a Address 0x%Lx Pages 0x%Lx\n",
389 Frees memory that was allocated with AllocateBuffer().
391 @param This The protocol instance pointer.
392 @param Pages The number of pages to free.
393 @param HostAddress The base system memory address of the allocated
396 @retval EFI_SUCCESS The requested memory pages were freed.
397 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and
398 Pages was not allocated with AllocateBuffer().
404 IN EDKII_IOMMU_PROTOCOL
*This
,
412 // Set memory encryption mask
414 Status
= MemEncryptSevSetPageEncMask (
416 (EFI_PHYSICAL_ADDRESS
)(UINTN
)HostAddress
,
420 ASSERT_EFI_ERROR(Status
);
421 ZeroMem (HostAddress
, EFI_PAGES_TO_SIZE (Pages
));
425 "%a Address 0x%Lx Pages 0x%Lx\n",
427 (UINT64
)(UINTN
)HostAddress
,
430 return gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
, Pages
);
435 Set IOMMU attribute for a system memory.
437 If the IOMMU protocol exists, the system memory cannot be used
440 When a device requests a DMA access for a system memory,
441 the device driver need use SetAttribute() to update the IOMMU
442 attribute to request DMA access (read and/or write).
444 The DeviceHandle is used to identify which device submits the request.
445 The IOMMU implementation need translate the device path to an IOMMU device
446 ID, and set IOMMU hardware register accordingly.
447 1) DeviceHandle can be a standard PCI device.
448 The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.
449 The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.
450 The memory for BusMasterCommonBuffer need set
451 EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.
452 After the memory is used, the memory need set 0 to keep it being
454 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).
455 The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or
456 EDKII_IOMMU_ACCESS_WRITE.
458 @param[in] This The protocol instance pointer.
459 @param[in] DeviceHandle The device who initiates the DMA access
461 @param[in] Mapping The mapping value returned from Map().
462 @param[in] IoMmuAccess The IOMMU access.
464 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range
465 specified by DeviceAddress and Length.
466 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
467 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by
469 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination
471 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
472 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported
474 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range
475 specified by Mapping.
476 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
477 modify the IOMMU access.
478 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while
479 attempting the operation.
485 IN EDKII_IOMMU_PROTOCOL
*This
,
486 IN EFI_HANDLE DeviceHandle
,
488 IN UINT64 IoMmuAccess
491 return EFI_UNSUPPORTED
;
494 EDKII_IOMMU_PROTOCOL mAmdSev
= {
495 EDKII_IOMMU_PROTOCOL_REVISION
,
504 Initialize Iommu Protocol.
509 AmdSevInstallIoMmuProtocol (
517 Status
= gBS
->InstallMultipleProtocolInterfaces (
519 &gEdkiiIoMmuProtocolGuid
, &mAmdSev
,