|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* |
| 3 | + * AMD SVM-SEV Host Support. |
| 4 | + * |
| 5 | + * Copyright (C) 2023 Advanced Micro Devices, Inc. |
| 6 | + * |
| 7 | + * Author: Ashish Kalra <ashish.kalra@amd.com> |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +#include <linux/cc_platform.h> |
| 12 | +#include <linux/printk.h> |
| 13 | +#include <linux/mm_types.h> |
| 14 | +#include <linux/set_memory.h> |
| 15 | +#include <linux/memblock.h> |
| 16 | +#include <linux/kernel.h> |
| 17 | +#include <linux/mm.h> |
| 18 | +#include <linux/cpumask.h> |
| 19 | +#include <linux/iommu.h> |
| 20 | +#include <linux/amd-iommu.h> |
| 21 | + |
| 22 | +#include <asm/sev.h> |
| 23 | +#include <asm/processor.h> |
| 24 | +#include <asm/setup.h> |
| 25 | +#include <asm/svm.h> |
| 26 | +#include <asm/smp.h> |
| 27 | +#include <asm/cpu.h> |
| 28 | +#include <asm/apic.h> |
| 29 | +#include <asm/cpuid.h> |
| 30 | +#include <asm/cmdline.h> |
| 31 | +#include <asm/iommu.h> |
| 32 | + |
| 33 | +/* |
| 34 | + * The RMP entry format is not architectural. The format is defined in PPR |
| 35 | + * Family 19h Model 01h, Rev B1 processor. |
| 36 | + */ |
| 37 | +struct rmpentry { |
| 38 | + u64 assigned : 1, |
| 39 | + pagesize : 1, |
| 40 | + immutable : 1, |
| 41 | + rsvd1 : 9, |
| 42 | + gpa : 39, |
| 43 | + asid : 10, |
| 44 | + vmsa : 1, |
| 45 | + validated : 1, |
| 46 | + rsvd2 : 1; |
| 47 | + u64 rsvd3; |
| 48 | +} __packed; |
| 49 | + |
| 50 | +/* |
| 51 | + * The first 16KB from the RMP_BASE is used by the processor for the |
| 52 | + * bookkeeping, the range needs to be added during the RMP entry lookup. |
| 53 | + */ |
| 54 | +#define RMPTABLE_CPU_BOOKKEEPING_SZ 0x4000 |
| 55 | + |
| 56 | +static u64 probed_rmp_base, probed_rmp_size; |
| 57 | +static struct rmpentry *rmptable __ro_after_init; |
| 58 | +static u64 rmptable_max_pfn __ro_after_init; |
| 59 | + |
| 60 | +#undef pr_fmt |
| 61 | +#define pr_fmt(fmt) "SEV-SNP: " fmt |
| 62 | + |
| 63 | +static int __mfd_enable(unsigned int cpu) |
| 64 | +{ |
| 65 | + u64 val; |
| 66 | + |
| 67 | + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) |
| 68 | + return 0; |
| 69 | + |
| 70 | + rdmsrl(MSR_AMD64_SYSCFG, val); |
| 71 | + |
| 72 | + val |= MSR_AMD64_SYSCFG_MFDM; |
| 73 | + |
| 74 | + wrmsrl(MSR_AMD64_SYSCFG, val); |
| 75 | + |
| 76 | + return 0; |
| 77 | +} |
| 78 | + |
| 79 | +static __init void mfd_enable(void *arg) |
| 80 | +{ |
| 81 | + __mfd_enable(smp_processor_id()); |
| 82 | +} |
| 83 | + |
| 84 | +static int __snp_enable(unsigned int cpu) |
| 85 | +{ |
| 86 | + u64 val; |
| 87 | + |
| 88 | + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) |
| 89 | + return 0; |
| 90 | + |
| 91 | + rdmsrl(MSR_AMD64_SYSCFG, val); |
| 92 | + |
| 93 | + val |= MSR_AMD64_SYSCFG_SNP_EN; |
| 94 | + val |= MSR_AMD64_SYSCFG_SNP_VMPL_EN; |
| 95 | + |
| 96 | + wrmsrl(MSR_AMD64_SYSCFG, val); |
| 97 | + |
| 98 | + return 0; |
| 99 | +} |
| 100 | + |
| 101 | +static __init void snp_enable(void *arg) |
| 102 | +{ |
| 103 | + __snp_enable(smp_processor_id()); |
| 104 | +} |
| 105 | + |
| 106 | +#define RMP_ADDR_MASK GENMASK_ULL(51, 13) |
| 107 | + |
| 108 | +bool snp_probe_rmptable_info(void) |
| 109 | +{ |
| 110 | + u64 max_rmp_pfn, calc_rmp_sz, rmp_sz, rmp_base, rmp_end; |
| 111 | + |
| 112 | + rdmsrl(MSR_AMD64_RMP_BASE, rmp_base); |
| 113 | + rdmsrl(MSR_AMD64_RMP_END, rmp_end); |
| 114 | + |
| 115 | + if (!(rmp_base & RMP_ADDR_MASK) || !(rmp_end & RMP_ADDR_MASK)) { |
| 116 | + pr_err("Memory for the RMP table has not been reserved by BIOS\n"); |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + if (rmp_base > rmp_end) { |
| 121 | + pr_err("RMP configuration not valid: base=%#llx, end=%#llx\n", rmp_base, rmp_end); |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + rmp_sz = rmp_end - rmp_base + 1; |
| 126 | + |
| 127 | + /* |
| 128 | + * Calculate the amount the memory that must be reserved by the BIOS to |
| 129 | + * address the whole RAM, including the bookkeeping area. The RMP itself |
| 130 | + * must also be covered. |
| 131 | + */ |
| 132 | + max_rmp_pfn = max_pfn; |
| 133 | + if (PHYS_PFN(rmp_end) > max_pfn) |
| 134 | + max_rmp_pfn = PHYS_PFN(rmp_end); |
| 135 | + |
| 136 | + calc_rmp_sz = (max_rmp_pfn << 4) + RMPTABLE_CPU_BOOKKEEPING_SZ; |
| 137 | + |
| 138 | + if (calc_rmp_sz > rmp_sz) { |
| 139 | + pr_err("Memory reserved for the RMP table does not cover full system RAM (expected 0x%llx got 0x%llx)\n", |
| 140 | + calc_rmp_sz, rmp_sz); |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + probed_rmp_base = rmp_base; |
| 145 | + probed_rmp_size = rmp_sz; |
| 146 | + |
| 147 | + pr_info("RMP table physical range [0x%016llx - 0x%016llx]\n", |
| 148 | + probed_rmp_base, probed_rmp_base + probed_rmp_size - 1); |
| 149 | + |
| 150 | + return true; |
| 151 | +} |
| 152 | + |
| 153 | +/* |
| 154 | + * Do the necessary preparations which are verified by the firmware as |
| 155 | + * described in the SNP_INIT_EX firmware command description in the SNP |
| 156 | + * firmware ABI spec. |
| 157 | + */ |
| 158 | +static int __init snp_rmptable_init(void) |
| 159 | +{ |
| 160 | + void *rmptable_start; |
| 161 | + u64 rmptable_size; |
| 162 | + u64 val; |
| 163 | + |
| 164 | + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) |
| 165 | + return 0; |
| 166 | + |
| 167 | + if (!amd_iommu_snp_en) |
| 168 | + return 0; |
| 169 | + |
| 170 | + if (!probed_rmp_size) |
| 171 | + goto nosnp; |
| 172 | + |
| 173 | + rmptable_start = memremap(probed_rmp_base, probed_rmp_size, MEMREMAP_WB); |
| 174 | + if (!rmptable_start) { |
| 175 | + pr_err("Failed to map RMP table\n"); |
| 176 | + return 1; |
| 177 | + } |
| 178 | + |
| 179 | + /* |
| 180 | + * Check if SEV-SNP is already enabled, this can happen in case of |
| 181 | + * kexec boot. |
| 182 | + */ |
| 183 | + rdmsrl(MSR_AMD64_SYSCFG, val); |
| 184 | + if (val & MSR_AMD64_SYSCFG_SNP_EN) |
| 185 | + goto skip_enable; |
| 186 | + |
| 187 | + memset(rmptable_start, 0, probed_rmp_size); |
| 188 | + |
| 189 | + /* Flush the caches to ensure that data is written before SNP is enabled. */ |
| 190 | + wbinvd_on_all_cpus(); |
| 191 | + |
| 192 | + /* MtrrFixDramModEn must be enabled on all the CPUs prior to enabling SNP. */ |
| 193 | + on_each_cpu(mfd_enable, NULL, 1); |
| 194 | + |
| 195 | + on_each_cpu(snp_enable, NULL, 1); |
| 196 | + |
| 197 | +skip_enable: |
| 198 | + rmptable_start += RMPTABLE_CPU_BOOKKEEPING_SZ; |
| 199 | + rmptable_size = probed_rmp_size - RMPTABLE_CPU_BOOKKEEPING_SZ; |
| 200 | + |
| 201 | + rmptable = (struct rmpentry *)rmptable_start; |
| 202 | + rmptable_max_pfn = rmptable_size / sizeof(struct rmpentry) - 1; |
| 203 | + |
| 204 | + cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/rmptable_init:online", __snp_enable, NULL); |
| 205 | + |
| 206 | + return 0; |
| 207 | + |
| 208 | +nosnp: |
| 209 | + setup_clear_cpu_cap(X86_FEATURE_SEV_SNP); |
| 210 | + return -ENOSYS; |
| 211 | +} |
| 212 | + |
| 213 | +/* |
| 214 | + * This must be called after the IOMMU has been initialized. |
| 215 | + */ |
| 216 | +device_initcall(snp_rmptable_init); |
0 commit comments