#define AVIC_GATAG_TO_VMID(x)          ((x >> AVIC_VCPU_ID_BITS) & AVIC_VM_ID_MASK)
 #define AVIC_GATAG_TO_VCPUID(x)                (x & AVIC_VCPU_ID_MASK)
 
+static bool force_avic;
+module_param_unsafe(force_avic, bool, 0444);
+
 /* Note:
  * This hash table is used to map VM_ID to a struct kvm_svm,
  * when handling AMD IOMMU GALOG notification to schedule in
 static u32 next_vm_id = 0;
 static bool next_vm_id_wrapped = 0;
 static DEFINE_SPINLOCK(svm_vm_data_hash_lock);
+enum avic_modes avic_mode;
 
 /*
  * This is a wrapper of struct amd_iommu_ir_data.
 
        avic_vcpu_load(vcpu, vcpu->cpu);
 }
+
+/*
+ * Note:
+ * - The module param avic enable both xAPIC and x2APIC mode.
+ * - Hypervisor can support both xAVIC and x2AVIC in the same guest.
+ * - The mode can be switched at run-time.
+ */
+bool avic_hardware_setup(struct kvm_x86_ops *x86_ops)
+{
+       if (!npt_enabled)
+               return false;
+
+       if (boot_cpu_has(X86_FEATURE_AVIC)) {
+               avic_mode = AVIC_MODE_X1;
+               pr_info("AVIC enabled\n");
+       } else if (force_avic) {
+               /*
+                * Some older systems does not advertise AVIC support.
+                * See Revision Guide for specific AMD processor for more detail.
+                */
+               avic_mode = AVIC_MODE_X1;
+               pr_warn("AVIC is not supported in CPUID but force enabled");
+               pr_warn("Your system might crash and burn");
+       }
+
+       /* AVIC is a prerequisite for x2AVIC. */
+       if (boot_cpu_has(X86_FEATURE_X2AVIC)) {
+               if (avic_mode == AVIC_MODE_X1) {
+                       avic_mode = AVIC_MODE_X2;
+                       pr_info("x2AVIC enabled\n");
+               } else {
+                       pr_warn(FW_BUG "Cannot support x2AVIC due to AVIC is disabled");
+                       pr_warn(FW_BUG "Try enable AVIC using force_avic option");
+               }
+       }
+
+       if (avic_mode != AVIC_MODE_NONE)
+               amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
+
+       return !!avic_mode;
+}
 
 static bool avic;
 module_param(avic, bool, 0444);
 
-static bool force_avic;
-module_param_unsafe(force_avic, bool, 0444);
-
 bool __read_mostly dump_invalid_vmcb;
 module_param(dump_invalid_vmcb, bool, 0644);
 
                        nrips = false;
        }
 
-       enable_apicv = avic = avic && npt_enabled && (boot_cpu_has(X86_FEATURE_AVIC) || force_avic);
+       enable_apicv = avic = avic && avic_hardware_setup(&svm_x86_ops);
 
-       if (enable_apicv) {
-               if (!boot_cpu_has(X86_FEATURE_AVIC)) {
-                       pr_warn("AVIC is not supported in CPUID but force enabled");
-                       pr_warn("Your system might crash and burn");
-               } else
-                       pr_info("AVIC enabled\n");
-
-               amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
-       } else {
+       if (!enable_apicv) {
                svm_x86_ops.vcpu_blocking = NULL;
                svm_x86_ops.vcpu_unblocking = NULL;
                svm_x86_ops.vcpu_get_apicv_inhibit_reasons = NULL;
 
 extern int vgif;
 extern bool intercept_smi;
 
+enum avic_modes {
+       AVIC_MODE_NONE = 0,
+       AVIC_MODE_X1,
+       AVIC_MODE_X2,
+};
+
+extern enum avic_modes avic_mode;
+
 /*
  * Clean bits in VMCB.
  * VMCB_ALL_CLEAN_MASK might also need to
 
 /* avic.c */
 
+bool avic_hardware_setup(struct kvm_x86_ops *ops);
 int avic_ga_log_notifier(u32 ga_tag);
 void avic_vm_destroy(struct kvm *kvm);
 int avic_vm_init(struct kvm *kvm);