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
);
285 // Free the mapped buffer and the MAP_INFO structure.
287 gBS
->FreePages (MapInfo
->PlainTextAddress
, MapInfo
->NumberOfPages
);
293 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
294 OperationBusMasterCommonBuffer64 mapping.
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
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
310 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
311 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
316 IoMmuAllocateBuffer (
317 IN EDKII_IOMMU_PROTOCOL
*This
,
318 IN EFI_ALLOCATE_TYPE Type
,
319 IN EFI_MEMORY_TYPE MemoryType
,
321 IN OUT VOID
**HostAddress
,
326 EFI_PHYSICAL_ADDRESS PhysicalAddress
;
329 // Validate Attributes
331 if ((Attributes
& EDKII_IOMMU_ATTRIBUTE_INVALID_FOR_ALLOCATE_BUFFER
) != 0) {
332 return EFI_UNSUPPORTED
;
336 // Check for invalid inputs
338 if (HostAddress
== NULL
) {
339 return EFI_INVALID_PARAMETER
;
343 // The only valid memory types are EfiBootServicesData and
344 // EfiRuntimeServicesData
346 if (MemoryType
!= EfiBootServicesData
&&
347 MemoryType
!= EfiRuntimeServicesData
) {
348 return EFI_INVALID_PARAMETER
;
351 PhysicalAddress
= (UINTN
)-1;
352 if ((Attributes
& EDKII_IOMMU_ATTRIBUTE_DUAL_ADDRESS_CYCLE
) == 0) {
354 // Limit allocations to memory below 4GB
356 PhysicalAddress
= SIZE_4GB
- 1;
358 Status
= gBS
->AllocatePages (
364 if (!EFI_ERROR (Status
)) {
365 *HostAddress
= (VOID
*) (UINTN
) PhysicalAddress
;
368 // Clear memory encryption mask
370 Status
= MemEncryptSevClearPageEncMask (0, PhysicalAddress
, Pages
, TRUE
);
371 ASSERT_EFI_ERROR(Status
);
376 "%a Address 0x%Lx Pages 0x%Lx\n",
385 Frees memory that was allocated with AllocateBuffer().
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
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().
400 IN EDKII_IOMMU_PROTOCOL
*This
,
408 // Set memory encryption mask
410 Status
= MemEncryptSevSetPageEncMask (
412 (EFI_PHYSICAL_ADDRESS
)(UINTN
)HostAddress
,
416 ASSERT_EFI_ERROR(Status
);
420 "%a Address 0x%Lx Pages 0x%Lx\n",
422 (UINT64
)(UINTN
)HostAddress
,
425 return gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
, Pages
);
430 Set IOMMU attribute for a system memory.
432 If the IOMMU protocol exists, the system memory cannot be used
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).
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
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.
453 @param[in] This The protocol instance pointer.
454 @param[in] DeviceHandle The device who initiates the DMA access
456 @param[in] Mapping The mapping value returned from Map().
457 @param[in] IoMmuAccess The IOMMU access.
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
464 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination
466 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
467 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported
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.
480 IN EDKII_IOMMU_PROTOCOL
*This
,
481 IN EFI_HANDLE DeviceHandle
,
483 IN UINT64 IoMmuAccess
486 return EFI_UNSUPPORTED
;
489 EDKII_IOMMU_PROTOCOL mAmdSev
= {
490 EDKII_IOMMU_PROTOCOL_REVISION
,
499 Initialize Iommu Protocol.
504 AmdSevInstallIoMmuProtocol (
512 Status
= gBS
->InstallMultipleProtocolInterfaces (
514 &gEdkiiIoMmuProtocolGuid
, &mAmdSev
,