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 case CC_ATTR_GUEST_SEV_SNP: 61 return sev_status & MSR_AMD64_SEV_SNP_ENABLED; 62 63 default: 64 return false; 65 } 66 #else 67 return false; 68 #endif 69 } 70 71 static bool hyperv_cc_platform_has(enum cc_attr attr) 72 { 73 return attr == CC_ATTR_GUEST_MEM_ENCRYPT; 74 } 75 76 bool cc_platform_has(enum cc_attr attr) 77 { 78 switch (vendor) { 79 case CC_VENDOR_AMD: 80 return amd_cc_platform_has(attr); 81 case CC_VENDOR_INTEL: 82 return intel_cc_platform_has(attr); 83 case CC_VENDOR_HYPERV: 84 return hyperv_cc_platform_has(attr); 85 default: 86 return false; 87 } 88 } 89 EXPORT_SYMBOL_GPL(cc_platform_has); 90 91 u64 cc_mkenc(u64 val) 92 { 93 switch (vendor) { 94 case CC_VENDOR_AMD: 95 return val | cc_mask; 96 default: 97 return val; 98 } 99 } 100 101 u64 cc_mkdec(u64 val) 102 { 103 switch (vendor) { 104 case CC_VENDOR_AMD: 105 return val & ~cc_mask; 106 default: 107 return val; 108 } 109 } 110 EXPORT_SYMBOL_GPL(cc_mkdec); 111 112 __init void cc_set_vendor(enum cc_vendor v) 113 { 114 vendor = v; 115 } 116 117 __init void cc_set_mask(u64 mask) 118 { 119 cc_mask = mask; 120 } 121