extern int x2apic_mode;
 extern int x2apic_phys;
+extern void __init x2apic_set_max_apicid(u32 apicid);
 extern void __init check_x2apic(void);
 extern void x2apic_setup(void);
 static inline int x2apic_enabled(void)
 
                return;
 
        if (remap_mode != IRQ_REMAP_X2APIC_MODE) {
-               /* IR is required if there is APIC ID > 255 even when running
-                * under KVM
+               /*
+                * Using X2APIC without IR is not architecturally supported
+                * on bare metal but may be supported in guests.
                 */
-               if (max_physical_apicid > 255 ||
-                   !x86_init.hyper.x2apic_available()) {
+               if (!x86_init.hyper.x2apic_available()) {
                        pr_info("x2apic: IRQ remapping doesn't support X2APIC mode\n");
                        x2apic_disable();
                        return;
                }
 
                /*
-                * without IR all CPUs can be addressed by IOAPIC/MSI
-                * only in physical mode
+                * Without IR, all CPUs can be addressed by IOAPIC/MSI only
+                * in physical mode, and CPUs with an APIC ID that cannnot
+                * be addressed must not be brought online.
                 */
+               x2apic_set_max_apicid(255);
                x2apic_phys = 1;
        }
        x2apic_enable();
 
 int x2apic_phys;
 
 static struct apic apic_x2apic_phys;
+static u32 x2apic_max_apicid __ro_after_init;
+
+void __init x2apic_set_max_apicid(u32 apicid)
+{
+       x2apic_max_apicid = apicid;
+}
 
 static int __init set_x2apic_phys_mode(char *arg)
 {
 /* Common x2apic functions, also used by x2apic_cluster */
 int x2apic_apic_id_valid(u32 apicid)
 {
+       if (x2apic_max_apicid && apicid > x2apic_max_apicid)
+               return 0;
+
        return 1;
 }