xref: /openbmc/linux/arch/x86/coco/core.c (revision 0a94608f)
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 
13 #include <asm/coco.h>
14 #include <asm/processor.h>
15 
16 static enum cc_vendor vendor __ro_after_init;
17 static u64 cc_mask __ro_after_init;
18 
19 static bool intel_cc_platform_has(enum cc_attr attr)
20 {
21 	return false;
22 }
23 
24 /*
25  * SME and SEV are very similar but they are not the same, so there are
26  * times that the kernel will need to distinguish between SME and SEV. The
27  * cc_platform_has() function is used for this.  When a distinction isn't
28  * needed, the CC_ATTR_MEM_ENCRYPT attribute can be used.
29  *
30  * The trampoline code is a good example for this requirement.  Before
31  * paging is activated, SME will access all memory as decrypted, but SEV
32  * will access all memory as encrypted.  So, when APs are being brought
33  * up under SME the trampoline area cannot be encrypted, whereas under SEV
34  * the trampoline area must be encrypted.
35  */
36 static bool amd_cc_platform_has(enum cc_attr attr)
37 {
38 #ifdef CONFIG_AMD_MEM_ENCRYPT
39 	switch (attr) {
40 	case CC_ATTR_MEM_ENCRYPT:
41 		return sme_me_mask;
42 
43 	case CC_ATTR_HOST_MEM_ENCRYPT:
44 		return sme_me_mask && !(sev_status & MSR_AMD64_SEV_ENABLED);
45 
46 	case CC_ATTR_GUEST_MEM_ENCRYPT:
47 		return sev_status & MSR_AMD64_SEV_ENABLED;
48 
49 	case CC_ATTR_GUEST_STATE_ENCRYPT:
50 		return sev_status & MSR_AMD64_SEV_ES_ENABLED;
51 
52 	/*
53 	 * With SEV, the rep string I/O instructions need to be unrolled
54 	 * but SEV-ES supports them through the #VC handler.
55 	 */
56 	case CC_ATTR_GUEST_UNROLL_STRING_IO:
57 		return (sev_status & MSR_AMD64_SEV_ENABLED) &&
58 			!(sev_status & MSR_AMD64_SEV_ES_ENABLED);
59 
60 	default:
61 		return false;
62 	}
63 #else
64 	return false;
65 #endif
66 }
67 
68 static bool hyperv_cc_platform_has(enum cc_attr attr)
69 {
70 	return attr == CC_ATTR_GUEST_MEM_ENCRYPT;
71 }
72 
73 bool cc_platform_has(enum cc_attr attr)
74 {
75 	switch (vendor) {
76 	case CC_VENDOR_AMD:
77 		return amd_cc_platform_has(attr);
78 	case CC_VENDOR_INTEL:
79 		return intel_cc_platform_has(attr);
80 	case CC_VENDOR_HYPERV:
81 		return hyperv_cc_platform_has(attr);
82 	default:
83 		return false;
84 	}
85 }
86 EXPORT_SYMBOL_GPL(cc_platform_has);
87 
88 u64 cc_mkenc(u64 val)
89 {
90 	switch (vendor) {
91 	case CC_VENDOR_AMD:
92 		return val | cc_mask;
93 	default:
94 		return val;
95 	}
96 }
97 
98 u64 cc_mkdec(u64 val)
99 {
100 	switch (vendor) {
101 	case CC_VENDOR_AMD:
102 		return val & ~cc_mask;
103 	default:
104 		return val;
105 	}
106 }
107 EXPORT_SYMBOL_GPL(cc_mkdec);
108 
109 __init void cc_set_vendor(enum cc_vendor v)
110 {
111 	vendor = v;
112 }
113 
114 __init void cc_set_mask(u64 mask)
115 {
116 	cc_mask = mask;
117 }
118