blob: 6a6ffcd978f6ed788eee1696fc506f8199e0c21b [file] [log] [blame]
Tom Lendackyaa5a4612021-09-08 17:58:34 -05001// 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
Tianyu Lanc789b902021-12-13 02:14:03 -050014#include <asm/mshyperv.h>
Tom Lendackyaa5a4612021-09-08 17:58:34 -050015#include <asm/processor.h>
16
17static bool __maybe_unused intel_cc_platform_has(enum cc_attr attr)
18{
19#ifdef CONFIG_INTEL_TDX_GUEST
20 return false;
21#else
22 return false;
23#endif
24}
25
26/*
27 * SME and SEV are very similar but they are not the same, so there are
28 * times that the kernel will need to distinguish between SME and SEV. The
29 * cc_platform_has() function is used for this. When a distinction isn't
30 * needed, the CC_ATTR_MEM_ENCRYPT attribute can be used.
31 *
32 * The trampoline code is a good example for this requirement. Before
33 * paging is activated, SME will access all memory as decrypted, but SEV
34 * will access all memory as encrypted. So, when APs are being brought
35 * up under SME the trampoline area cannot be encrypted, whereas under SEV
36 * the trampoline area must be encrypted.
37 */
38static bool amd_cc_platform_has(enum cc_attr attr)
39{
40#ifdef CONFIG_AMD_MEM_ENCRYPT
41 switch (attr) {
42 case CC_ATTR_MEM_ENCRYPT:
43 return sme_me_mask;
44
45 case CC_ATTR_HOST_MEM_ENCRYPT:
46 return sme_me_mask && !(sev_status & MSR_AMD64_SEV_ENABLED);
47
48 case CC_ATTR_GUEST_MEM_ENCRYPT:
49 return sev_status & MSR_AMD64_SEV_ENABLED;
50
51 case CC_ATTR_GUEST_STATE_ENCRYPT:
52 return sev_status & MSR_AMD64_SEV_ES_ENABLED;
53
Kuppuswamy Sathyanarayanan8260b982021-12-06 16:55:03 +030054 /*
55 * With SEV, the rep string I/O instructions need to be unrolled
56 * but SEV-ES supports them through the #VC handler.
57 */
58 case CC_ATTR_GUEST_UNROLL_STRING_IO:
59 return (sev_status & MSR_AMD64_SEV_ENABLED) &&
60 !(sev_status & MSR_AMD64_SEV_ES_ENABLED);
61
Tom Lendackyaa5a4612021-09-08 17:58:34 -050062 default:
63 return false;
64 }
65#else
66 return false;
67#endif
68}
69
Tianyu Lanc789b902021-12-13 02:14:03 -050070static bool hyperv_cc_platform_has(enum cc_attr attr)
71{
72 return attr == CC_ATTR_GUEST_MEM_ENCRYPT;
73}
Tom Lendackyaa5a4612021-09-08 17:58:34 -050074
75bool cc_platform_has(enum cc_attr attr)
76{
77 if (sme_me_mask)
78 return amd_cc_platform_has(attr);
79
Tianyu Lanc789b902021-12-13 02:14:03 -050080 if (hv_is_isolation_supported())
81 return hyperv_cc_platform_has(attr);
82
Tom Lendackyaa5a4612021-09-08 17:58:34 -050083 return false;
84}
85EXPORT_SYMBOL_GPL(cc_platform_has);