Skip to content

Commit 4bdec12

Browse files
ssuthiku-amdbonzini
authored andcommitted
KVM: SVM: Detect X2APIC virtualization (x2AVIC) support
Add CPUID check for the x2APIC virtualization (x2AVIC) feature. If available, the SVM driver can support both AVIC and x2AVIC modes when load the kvm_amd driver with avic=1. The operating mode will be determined at runtime depending on the guest APIC mode. Reviewed-by: Maxim Levitsky <[email protected]> Reviewed-by: Pankaj Gupta <[email protected]> Signed-off-by: Suravee Suthikulpanit <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent bf348f6 commit 4bdec12

File tree

4 files changed

+59
-13
lines changed

4 files changed

+59
-13
lines changed

arch/x86/include/asm/svm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ struct __attribute__ ((__packed__)) vmcb_control_area {
195195
#define AVIC_ENABLE_SHIFT 31
196196
#define AVIC_ENABLE_MASK (1 << AVIC_ENABLE_SHIFT)
197197

198+
#define X2APIC_MODE_SHIFT 30
199+
#define X2APIC_MODE_MASK (1 << X2APIC_MODE_SHIFT)
200+
198201
#define LBR_CTL_ENABLE_MASK BIT_ULL(0)
199202
#define VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK BIT_ULL(1)
200203

arch/x86/kvm/svm/avic.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
#define AVIC_GATAG_TO_VMID(x) ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK)
4141
#define AVIC_GATAG_TO_VCPUID(x) (x & AVIC_VCPU_ID_MASK)
4242

43+
static bool force_avic;
44+
module_param_unsafe(force_avic, bool, 0444);
45+
4346
/* Note:
4447
* This hash table is used to map VM_ID to a struct kvm_svm,
4548
* when handling AMD IOMMU GALOG notification to schedule in
@@ -50,6 +53,7 @@ static DEFINE_HASHTABLE(svm_vm_data_hash, SVM_VM_DATA_HASH_BITS);
5053
static u32 next_vm_id = 0;
5154
static bool next_vm_id_wrapped = 0;
5255
static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
56+
enum avic_modes avic_mode;
5357

5458
/*
5559
* This is a wrapper of struct amd_iommu_ir_data.
@@ -1058,3 +1062,44 @@ void avic_vcpu_unblocking(struct kvm_vcpu *vcpu)
10581062

10591063
avic_vcpu_load(vcpu, vcpu->cpu);
10601064
}
1065+
1066+
/*
1067+
* Note:
1068+
* - The module param avic enable both xAPIC and x2APIC mode.
1069+
* - Hypervisor can support both xAVIC and x2AVIC in the same guest.
1070+
* - The mode can be switched at run-time.
1071+
*/
1072+
bool avic_hardware_setup(struct kvm_x86_ops *x86_ops)
1073+
{
1074+
if (!npt_enabled)
1075+
return false;
1076+
1077+
if (boot_cpu_has(X86_FEATURE_AVIC)) {
1078+
avic_mode = AVIC_MODE_X1;
1079+
pr_info("AVIC enabled\n");
1080+
} else if (force_avic) {
1081+
/*
1082+
* Some older systems does not advertise AVIC support.
1083+
* See Revision Guide for specific AMD processor for more detail.
1084+
*/
1085+
avic_mode = AVIC_MODE_X1;
1086+
pr_warn("AVIC is not supported in CPUID but force enabled");
1087+
pr_warn("Your system might crash and burn");
1088+
}
1089+
1090+
/* AVIC is a prerequisite for x2AVIC. */
1091+
if (boot_cpu_has(X86_FEATURE_X2AVIC)) {
1092+
if (avic_mode == AVIC_MODE_X1) {
1093+
avic_mode = AVIC_MODE_X2;
1094+
pr_info("x2AVIC enabled\n");
1095+
} else {
1096+
pr_warn(FW_BUG "Cannot support x2AVIC due to AVIC is disabled");
1097+
pr_warn(FW_BUG "Try enable AVIC using force_avic option");
1098+
}
1099+
}
1100+
1101+
if (avic_mode != AVIC_MODE_NONE)
1102+
amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1103+
1104+
return !!avic_mode;
1105+
}

arch/x86/kvm/svm/svm.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,6 @@ module_param(tsc_scaling, int, 0444);
188188
static bool avic;
189189
module_param(avic, bool, 0444);
190190

191-
static bool force_avic;
192-
module_param_unsafe(force_avic, bool, 0444);
193-
194191
bool __read_mostly dump_invalid_vmcb;
195192
module_param(dump_invalid_vmcb, bool, 0644);
196193

@@ -5016,17 +5013,9 @@ static __init int svm_hardware_setup(void)
50165013
nrips = false;
50175014
}
50185015

5019-
enable_apicv = avic = avic && npt_enabled && (boot_cpu_has(X86_FEATURE_AVIC) || force_avic);
5016+
enable_apicv = avic = avic && avic_hardware_setup(&svm_x86_ops);
50205017

5021-
if (enable_apicv) {
5022-
if (!boot_cpu_has(X86_FEATURE_AVIC)) {
5023-
pr_warn("AVIC is not supported in CPUID but force enabled");
5024-
pr_warn("Your system might crash and burn");
5025-
} else
5026-
pr_info("AVIC enabled\n");
5027-
5028-
amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
5029-
} else {
5018+
if (!enable_apicv) {
50305019
svm_x86_ops.vcpu_blocking = NULL;
50315020
svm_x86_ops.vcpu_unblocking = NULL;
50325021
svm_x86_ops.vcpu_get_apicv_inhibit_reasons = NULL;

arch/x86/kvm/svm/svm.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ extern bool npt_enabled;
3636
extern int vgif;
3737
extern bool intercept_smi;
3838

39+
enum avic_modes {
40+
AVIC_MODE_NONE = 0,
41+
AVIC_MODE_X1,
42+
AVIC_MODE_X2,
43+
};
44+
45+
extern enum avic_modes avic_mode;
46+
3947
/*
4048
* Clean bits in VMCB.
4149
* VMCB_ALL_CLEAN_MASK might also need to
@@ -607,6 +615,7 @@ extern struct kvm_x86_nested_ops svm_nested_ops;
607615

608616
/* avic.c */
609617

618+
bool avic_hardware_setup(struct kvm_x86_ops *ops);
610619
int avic_ga_log_notifier(u32 ga_tag);
611620
void avic_vm_destroy(struct kvm *kvm);
612621
int avic_vm_init(struct kvm *kvm);

0 commit comments

Comments
 (0)