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