From: Ashish Kalra The new RMPOPT instruction sets bits in a per-CPU RMPOPT table, which indicates whether specific 1GB physical memory regions contain SEV-SNP guest memory. Per-CPU RMPOPT tables support at most 2 TB of addressable memory for RMP optimizations. Initialize the per-CPU RMPOPT table base to the starting physical address. This enables RMP optimization for up to 2 TB of system RAM on all CPUs. Suggested-by: Thomas Lendacky Signed-off-by: Ashish Kalra --- arch/x86/include/asm/msr-index.h | 3 +++ arch/x86/virt/svm/sev.c | 37 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index da5275d8eda6..8e7da03abd5b 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -753,6 +753,9 @@ #define MSR_AMD64_SEG_RMP_ENABLED_BIT 0 #define MSR_AMD64_SEG_RMP_ENABLED BIT_ULL(MSR_AMD64_SEG_RMP_ENABLED_BIT) #define MSR_AMD64_RMP_SEGMENT_SHIFT(x) (((x) & GENMASK_ULL(13, 8)) >> 8) +#define MSR_AMD64_RMPOPT_BASE 0xc0010139 +#define MSR_AMD64_RMPOPT_ENABLE_BIT 0 +#define MSR_AMD64_RMPOPT_ENABLE BIT_ULL(MSR_AMD64_RMPOPT_ENABLE_BIT) #define MSR_SVSM_CAA 0xc001f000 diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c index a4f3a364fb65..405199c2f563 100644 --- a/arch/x86/virt/svm/sev.c +++ b/arch/x86/virt/svm/sev.c @@ -500,6 +500,41 @@ static bool __init setup_rmptable(void) } } +static void __configure_rmpopt(void *val) +{ + u64 rmpopt_base = ((u64)val & PUD_MASK) | MSR_AMD64_RMPOPT_ENABLE; + + wrmsrq(MSR_AMD64_RMPOPT_BASE, rmpopt_base); +} + +static __init void configure_and_enable_rmpopt(void) +{ + phys_addr_t pa_start = ALIGN_DOWN(PFN_PHYS(min_low_pfn), PUD_SIZE); + + if (!cpu_feature_enabled(X86_FEATURE_RMPOPT)) { + pr_debug("RMPOPT not supported on this platform\n"); + return; + } + + if (!cc_platform_has(CC_ATTR_HOST_SEV_SNP)) { + pr_debug("RMPOPT optimizations not enabled as SNP support is not enabled\n"); + return; + } + + if (!(rmp_cfg & MSR_AMD64_SEG_RMP_ENABLED)) { + pr_info("RMPOPT optimizations not enabled, segmented RMP required\n"); + return; + } + + /* + * Per-CPU RMPOPT tables support at most 2 TB of addressable memory for RMP optimizations. + * + * Set per-core RMPOPT base to min_low_pfn to enable RMP optimization for + * up to 2TB of system RAM on all CPUs. + */ + on_each_cpu_mask(cpu_online_mask, __configure_rmpopt, (void *)pa_start, true); +} + /* * Do the necessary preparations which are verified by the firmware as * described in the SNP_INIT_EX firmware command description in the SNP @@ -555,6 +590,8 @@ int __init snp_rmptable_init(void) skip_enable: cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/rmptable_init:online", __snp_enable, NULL); + configure_and_enable_rmpopt(); + /* * Setting crash_kexec_post_notifiers to 'true' to ensure that SNP panic * notifier is invoked to do SNP IOMMU shutdown before kdump. -- 2.43.0