1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Confidential Computing Platform Capability checks
4  *
5  * Copyright (C) 2021 Advanced Micro Devices, Inc.
6  *
7  * Author: Tom Lendacky <thomas.lendacky@amd.com>
8  */
9 
10 #include <linux/export.h>
11 #include <linux/cc_platform.h>
12 #include <linux/mem_encrypt.h>
13 
14 #include <asm/processor.h>
15 
intel_cc_platform_has(enum cc_attr attr)16 static bool __maybe_unused intel_cc_platform_has(enum cc_attr attr)
17 {
18 #ifdef CONFIG_INTEL_TDX_GUEST
19 	return false;
20 #else
21 	return false;
22 #endif
23 }
24 
25 /*
26  * SME and SEV are very similar but they are not the same, so there are
27  * times that the kernel will need to distinguish between SME and SEV. The
28  * cc_platform_has() function is used for this.  When a distinction isn't
29  * needed, the CC_ATTR_MEM_ENCRYPT attribute can be used.
30  *
31  * The trampoline code is a good example for this requirement.  Before
32  * paging is activated, SME will access all memory as decrypted, but SEV
33  * will access all memory as encrypted.  So, when APs are being brought
34  * up under SME the trampoline area cannot be encrypted, whereas under SEV
35  * the trampoline area must be encrypted.
36  */
amd_cc_platform_has(enum cc_attr attr)37 static bool amd_cc_platform_has(enum cc_attr attr)
38 {
39 #ifdef CONFIG_AMD_MEM_ENCRYPT
40 	switch (attr) {
41 	case CC_ATTR_MEM_ENCRYPT:
42 		return sme_me_mask;
43 
44 	case CC_ATTR_HOST_MEM_ENCRYPT:
45 		return sme_me_mask && !(sev_status & MSR_AMD64_SEV_ENABLED);
46 
47 	case CC_ATTR_GUEST_MEM_ENCRYPT:
48 		return sev_status & MSR_AMD64_SEV_ENABLED;
49 
50 	case CC_ATTR_GUEST_STATE_ENCRYPT:
51 		return sev_status & MSR_AMD64_SEV_ES_ENABLED;
52 
53 	default:
54 		return false;
55 	}
56 #else
57 	return false;
58 #endif
59 }
60 
61 
cc_platform_has(enum cc_attr attr)62 bool cc_platform_has(enum cc_attr attr)
63 {
64 	if (sme_me_mask)
65 		return amd_cc_platform_has(attr);
66 
67 	return false;
68 }
69 EXPORT_SYMBOL_GPL(cc_platform_has);
70