Skip to content

Commit b80fd5a

Browse files
ssuthiku-amdjfvogel
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]> (cherry picked from commit 4bdec12) Orabug: 34817119 Signed-off-by: Alejandro Jimenez <[email protected]> Reviewed-by: Boris Ostrovsky <[email protected]> Conflicts: arch/x86/kvm/svm/svm.c Conflict due to UEK missing: 54744e1 KVM: SVM: Move svm_hardware_setup() and its helpers below svm_x86_ops a3c19d5 KVM: SVM: Nullify vcpu_(un)blocking() hooks if AVIC is disabled (cherry picked from commit 51fa7e6fe054edcb7f505e7b2c7e428063e988b6) Signed-off-by: Jack Vogel <[email protected]>
1 parent a306b97 commit b80fd5a

File tree

4 files changed

+58
-14
lines changed

4 files changed

+58
-14
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.
@@ -1104,3 +1108,44 @@ void avic_vcpu_unblocking(struct kvm_vcpu *vcpu)
11041108

11051109
avic_vcpu_load(vcpu);
11061110
}
1111+
1112+
/*
1113+
* Note:
1114+
* - The module param avic enable both xAPIC and x2APIC mode.
1115+
* - Hypervisor can support both xAVIC and x2AVIC in the same guest.
1116+
* - The mode can be switched at run-time.
1117+
*/
1118+
bool avic_hardware_setup(struct kvm_x86_ops *x86_ops)
1119+
{
1120+
if (!npt_enabled)
1121+
return false;
1122+
1123+
if (boot_cpu_has(X86_FEATURE_AVIC)) {
1124+
avic_mode = AVIC_MODE_X1;
1125+
pr_info("AVIC enabled\n");
1126+
} else if (force_avic) {
1127+
/*
1128+
* Some older systems does not advertise AVIC support.
1129+
* See Revision Guide for specific AMD processor for more detail.
1130+
*/
1131+
avic_mode = AVIC_MODE_X1;
1132+
pr_warn("AVIC is not supported in CPUID but force enabled");
1133+
pr_warn("Your system might crash and burn");
1134+
}
1135+
1136+
/* AVIC is a prerequisite for x2AVIC. */
1137+
if (boot_cpu_has(X86_FEATURE_X2AVIC)) {
1138+
if (avic_mode == AVIC_MODE_X1) {
1139+
avic_mode = AVIC_MODE_X2;
1140+
pr_info("x2AVIC enabled\n");
1141+
} else {
1142+
pr_warn(FW_BUG "Cannot support x2AVIC due to AVIC is disabled");
1143+
pr_warn(FW_BUG "Try enable AVIC using force_avic option");
1144+
}
1145+
}
1146+
1147+
if (avic_mode != AVIC_MODE_NONE)
1148+
amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1149+
1150+
return !!avic_mode;
1151+
}

arch/x86/kvm/svm/svm.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,6 @@ module_param(vgif, int, 0444);
193193
static bool avic;
194194
module_param(avic, bool, 0444);
195195

196-
static bool force_avic;
197-
module_param_unsafe(force_avic, bool, 0444);
198-
199196
bool __read_mostly dump_invalid_vmcb;
200197
module_param(dump_invalid_vmcb, bool, 0644);
201198

@@ -1085,17 +1082,7 @@ static __init int svm_hardware_setup(void)
10851082
nrips = false;
10861083
}
10871084

1088-
enable_apicv = avic = avic && npt_enabled && (boot_cpu_has(X86_FEATURE_AVIC) || force_avic);
1089-
1090-
if (enable_apicv) {
1091-
if (!boot_cpu_has(X86_FEATURE_AVIC)) {
1092-
pr_warn("AVIC is not supported in CPUID but force enabled");
1093-
pr_warn("Your system might crash and burn");
1094-
} else
1095-
pr_info("AVIC enabled\n");
1096-
1097-
amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1098-
}
1085+
enable_apicv = avic = avic && avic_hardware_setup(&svm_x86_ops);
10991086

11001087
if (vls) {
11011088
if (!npt_enabled ||

arch/x86/kvm/svm/svm.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ extern u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
3535
extern bool npt_enabled;
3636
extern bool intercept_smi;
3737

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

500508
/* avic.c */
501509

510+
bool avic_hardware_setup(struct kvm_x86_ops *ops);
502511
int avic_ga_log_notifier(u32 ga_tag);
503512
void avic_vm_destroy(struct kvm *kvm);
504513
int avic_vm_init(struct kvm *kvm);

0 commit comments

Comments
 (0)