OvmfPkg/AmdSevDxe: refresh #includes and LibraryClasses
[mirror_edk2.git] / OvmfPkg / AmdSevDxe / AmdSevDxe.c
1 /** @file
2
3 AMD Sev Dxe driver. This driver is dispatched early in DXE, due to being list
4 in APRIORI. It clears C-bit from MMIO and NonExistent Memory space when SEV
5 is enabled.
6
7 Copyright (c) 2017, AMD Inc. All rights reserved.<BR>
8
9 This program and the accompanying materials are licensed and made available
10 under the terms and conditions of the BSD License which accompanies this
11 distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
13
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
15 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16
17 **/
18
19 #include <Library/DebugLib.h>
20 #include <Library/DxeServicesTableLib.h>
21 #include <Library/MemEncryptSevLib.h>
22 #include <Library/MemoryAllocationLib.h>
23
24 EFI_STATUS
25 EFIAPI
26 AmdSevDxeEntryPoint (
27 IN EFI_HANDLE ImageHandle,
28 IN EFI_SYSTEM_TABLE *SystemTable
29 )
30 {
31 EFI_STATUS Status;
32 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *AllDescMap;
33 UINTN NumEntries;
34 UINTN Index;
35
36 //
37 // Do nothing when SEV is not enabled
38 //
39 if (!MemEncryptSevIsEnabled ()) {
40 return EFI_UNSUPPORTED;
41 }
42
43 //
44 // Iterate through the GCD map and clear the C-bit from MMIO and NonExistent
45 // memory space. The NonExistent memory space will be used for mapping the
46 // MMIO space added later (eg PciRootBridge). By clearing both known MMIO and
47 // NonExistent memory space can gurantee that current and furture MMIO adds
48 // will have C-bit cleared.
49 //
50 Status = gDS->GetMemorySpaceMap (&NumEntries, &AllDescMap);
51 if (!EFI_ERROR (Status)) {
52 for (Index = 0; Index < NumEntries; Index++) {
53 CONST EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Desc;
54
55 Desc = &AllDescMap[Index];
56 if (Desc->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo ||
57 Desc->GcdMemoryType == EfiGcdMemoryTypeNonExistent) {
58 Status = MemEncryptSevClearPageEncMask (
59 0,
60 Desc->BaseAddress,
61 EFI_SIZE_TO_PAGES (Desc->Length),
62 FALSE
63 );
64 ASSERT_EFI_ERROR (Status);
65 }
66 }
67
68 FreePool (AllDescMap);
69 }
70
71 return EFI_SUCCESS;
72 }