1f62bae50SIngo Molnar #include <linux/threads.h> 2f62bae50SIngo Molnar #include <linux/cpumask.h> 3f62bae50SIngo Molnar #include <linux/string.h> 4f62bae50SIngo Molnar #include <linux/kernel.h> 5f62bae50SIngo Molnar #include <linux/ctype.h> 6f62bae50SIngo Molnar #include <linux/dmar.h> 7f62bae50SIngo Molnar 8f62bae50SIngo Molnar #include <asm/smp.h> 9981c2eacSThomas Gleixner #include <asm/ipi.h> 10981c2eacSThomas Gleixner #include "x2apic.h" 11f62bae50SIngo Molnar 12ef1f87aaSSuresh Siddha int x2apic_phys; 13f62bae50SIngo Molnar 141a8880a1SSuresh Siddha static struct apic apic_x2apic_phys; 151a8880a1SSuresh Siddha 16f62bae50SIngo Molnar static int set_x2apic_phys_mode(char *arg) 17f62bae50SIngo Molnar { 18f62bae50SIngo Molnar x2apic_phys = 1; 19f62bae50SIngo Molnar return 0; 20f62bae50SIngo Molnar } 21f62bae50SIngo Molnar early_param("x2apic_phys", set_x2apic_phys_mode); 22f62bae50SIngo Molnar 23cb214edeSStoney Wang static bool x2apic_fadt_phys(void) 24cb214edeSStoney Wang { 25781674fcSJan Kiszka #ifdef CONFIG_ACPI 26cb214edeSStoney Wang if ((acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID) && 27cb214edeSStoney Wang (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) { 28cb214edeSStoney Wang printk(KERN_DEBUG "System requires x2apic physical mode\n"); 29cb214edeSStoney Wang return true; 30cb214edeSStoney Wang } 31781674fcSJan Kiszka #endif 32cb214edeSStoney Wang return false; 33cb214edeSStoney Wang } 34cb214edeSStoney Wang 35f62bae50SIngo Molnar static int x2apic_acpi_madt_oem_check(char *oem_id, char *oem_table_id) 36f62bae50SIngo Molnar { 37cb214edeSStoney Wang return x2apic_enabled() && (x2apic_phys || x2apic_fadt_phys()); 38f62bae50SIngo Molnar } 39f62bae50SIngo Molnar 40f2bffe8aSThomas Gleixner static void x2apic_send_IPI(int cpu, int vector) 41f2bffe8aSThomas Gleixner { 42f2bffe8aSThomas Gleixner u32 dest = per_cpu(x86_cpu_to_apicid, cpu); 43f2bffe8aSThomas Gleixner 44f2bffe8aSThomas Gleixner x2apic_wrmsr_fence(); 45f2bffe8aSThomas Gleixner __x2apic_send_IPI_dest(dest, vector, APIC_DEST_PHYSICAL); 46f2bffe8aSThomas Gleixner } 47f2bffe8aSThomas Gleixner 48f62bae50SIngo Molnar static void 49a27d0b5eSSuresh Siddha __x2apic_send_IPI_mask(const struct cpumask *mask, int vector, int apic_dest) 50f62bae50SIngo Molnar { 51f62bae50SIngo Molnar unsigned long query_cpu; 52a27d0b5eSSuresh Siddha unsigned long this_cpu; 53f62bae50SIngo Molnar unsigned long flags; 54f62bae50SIngo Molnar 55ce4e240cSSuresh Siddha x2apic_wrmsr_fence(); 56ce4e240cSSuresh Siddha 57f62bae50SIngo Molnar local_irq_save(flags); 58a27d0b5eSSuresh Siddha 59a27d0b5eSSuresh Siddha this_cpu = smp_processor_id(); 60f62bae50SIngo Molnar for_each_cpu(query_cpu, mask) { 61a27d0b5eSSuresh Siddha if (apic_dest == APIC_DEST_ALLBUT && this_cpu == query_cpu) 62f62bae50SIngo Molnar continue; 63f62bae50SIngo Molnar __x2apic_send_IPI_dest(per_cpu(x86_cpu_to_apicid, query_cpu), 64f62bae50SIngo Molnar vector, APIC_DEST_PHYSICAL); 65f62bae50SIngo Molnar } 66f62bae50SIngo Molnar local_irq_restore(flags); 67f62bae50SIngo Molnar } 68f62bae50SIngo Molnar 69a27d0b5eSSuresh Siddha static void x2apic_send_IPI_mask(const struct cpumask *mask, int vector) 70a27d0b5eSSuresh Siddha { 71a27d0b5eSSuresh Siddha __x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLINC); 72a27d0b5eSSuresh Siddha } 73a27d0b5eSSuresh Siddha 74a27d0b5eSSuresh Siddha static void 75a27d0b5eSSuresh Siddha x2apic_send_IPI_mask_allbutself(const struct cpumask *mask, int vector) 76a27d0b5eSSuresh Siddha { 77a27d0b5eSSuresh Siddha __x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLBUT); 78a27d0b5eSSuresh Siddha } 79a27d0b5eSSuresh Siddha 80a27d0b5eSSuresh Siddha static void x2apic_send_IPI_allbutself(int vector) 81a27d0b5eSSuresh Siddha { 82a27d0b5eSSuresh Siddha __x2apic_send_IPI_mask(cpu_online_mask, vector, APIC_DEST_ALLBUT); 83a27d0b5eSSuresh Siddha } 84a27d0b5eSSuresh Siddha 85f62bae50SIngo Molnar static void x2apic_send_IPI_all(int vector) 86f62bae50SIngo Molnar { 87a27d0b5eSSuresh Siddha __x2apic_send_IPI_mask(cpu_online_mask, vector, APIC_DEST_ALLINC); 88f62bae50SIngo Molnar } 89f62bae50SIngo Molnar 90f62bae50SIngo Molnar static void init_x2apic_ldr(void) 91f62bae50SIngo Molnar { 92f62bae50SIngo Molnar } 93f62bae50SIngo Molnar 949ebd680bSSuresh Siddha static int x2apic_phys_probe(void) 959ebd680bSSuresh Siddha { 96cb214edeSStoney Wang if (x2apic_mode && (x2apic_phys || x2apic_fadt_phys())) 979ebd680bSSuresh Siddha return 1; 989ebd680bSSuresh Siddha 999ebd680bSSuresh Siddha return apic == &apic_x2apic_phys; 1009ebd680bSSuresh Siddha } 1019ebd680bSSuresh Siddha 102981c2eacSThomas Gleixner /* Common x2apic functions, also used by x2apic_cluster */ 103981c2eacSThomas Gleixner int x2apic_apic_id_valid(int apicid) 104981c2eacSThomas Gleixner { 105981c2eacSThomas Gleixner return 1; 106981c2eacSThomas Gleixner } 107981c2eacSThomas Gleixner 108981c2eacSThomas Gleixner int x2apic_apic_id_registered(void) 109981c2eacSThomas Gleixner { 110981c2eacSThomas Gleixner return 1; 111981c2eacSThomas Gleixner } 112981c2eacSThomas Gleixner 113981c2eacSThomas Gleixner void __x2apic_send_IPI_dest(unsigned int apicid, int vector, unsigned int dest) 114981c2eacSThomas Gleixner { 115981c2eacSThomas Gleixner unsigned long cfg = __prepare_ICR(0, vector, dest); 116981c2eacSThomas Gleixner native_x2apic_icr_write(cfg, apicid); 117981c2eacSThomas Gleixner } 118981c2eacSThomas Gleixner 119981c2eacSThomas Gleixner unsigned int x2apic_get_apic_id(unsigned long id) 120981c2eacSThomas Gleixner { 121981c2eacSThomas Gleixner return id; 122981c2eacSThomas Gleixner } 123981c2eacSThomas Gleixner 124727657e6SThomas Gleixner u32 x2apic_set_apic_id(unsigned int id) 125981c2eacSThomas Gleixner { 126981c2eacSThomas Gleixner return id; 127981c2eacSThomas Gleixner } 128981c2eacSThomas Gleixner 129981c2eacSThomas Gleixner int x2apic_phys_pkg_id(int initial_apicid, int index_msb) 130981c2eacSThomas Gleixner { 131981c2eacSThomas Gleixner return initial_apicid >> index_msb; 132981c2eacSThomas Gleixner } 133981c2eacSThomas Gleixner 134981c2eacSThomas Gleixner void x2apic_send_IPI_self(int vector) 135981c2eacSThomas Gleixner { 136981c2eacSThomas Gleixner apic_write(APIC_SELF_IPI, vector); 137981c2eacSThomas Gleixner } 138981c2eacSThomas Gleixner 139404f6aacSKees Cook static struct apic apic_x2apic_phys __ro_after_init = { 140f62bae50SIngo Molnar 141f62bae50SIngo Molnar .name = "physical x2apic", 1429ebd680bSSuresh Siddha .probe = x2apic_phys_probe, 143f62bae50SIngo Molnar .acpi_madt_oem_check = x2apic_acpi_madt_oem_check, 144b7157acfSSteffen Persvold .apic_id_valid = x2apic_apic_id_valid, 145f62bae50SIngo Molnar .apic_id_registered = x2apic_apic_id_registered, 146f62bae50SIngo Molnar 147f62bae50SIngo Molnar .irq_delivery_mode = dest_Fixed, 148f62bae50SIngo Molnar .irq_dest_mode = 0, /* physical */ 149f62bae50SIngo Molnar 150f62bae50SIngo Molnar .disable_esr = 0, 151f62bae50SIngo Molnar .dest_logical = 0, 152f62bae50SIngo Molnar .check_apicid_used = NULL, 153f62bae50SIngo Molnar 1549d8e1066SAlexander Gordeev .vector_allocation_domain = default_vector_allocation_domain, 155f62bae50SIngo Molnar .init_apic_ldr = init_x2apic_ldr, 156f62bae50SIngo Molnar 157f62bae50SIngo Molnar .ioapic_phys_id_map = NULL, 158f62bae50SIngo Molnar .setup_apic_routing = NULL, 159f62bae50SIngo Molnar .cpu_present_to_apicid = default_cpu_present_to_apicid, 160f62bae50SIngo Molnar .apicid_to_cpu_present = NULL, 161f62bae50SIngo Molnar .check_phys_apicid_present = default_check_phys_apicid_present, 162f62bae50SIngo Molnar .phys_pkg_id = x2apic_phys_pkg_id, 163f62bae50SIngo Molnar 16479deb8e5SCyrill Gorcunov .get_apic_id = x2apic_get_apic_id, 16579deb8e5SCyrill Gorcunov .set_apic_id = x2apic_set_apic_id, 166f62bae50SIngo Molnar 16791cd9cb7SThomas Gleixner .cpu_mask_to_apicid = default_cpu_mask_to_apicid, 168*9f9e3bb1SThomas Gleixner .calc_dest_apicid = apic_default_calc_apicid, 169f62bae50SIngo Molnar 170f2bffe8aSThomas Gleixner .send_IPI = x2apic_send_IPI, 171f62bae50SIngo Molnar .send_IPI_mask = x2apic_send_IPI_mask, 172f62bae50SIngo Molnar .send_IPI_mask_allbutself = x2apic_send_IPI_mask_allbutself, 173f62bae50SIngo Molnar .send_IPI_allbutself = x2apic_send_IPI_allbutself, 174f62bae50SIngo Molnar .send_IPI_all = x2apic_send_IPI_all, 175f62bae50SIngo Molnar .send_IPI_self = x2apic_send_IPI_self, 176f62bae50SIngo Molnar 177f62bae50SIngo Molnar .inquire_remote_apic = NULL, 178f62bae50SIngo Molnar 179f62bae50SIngo Molnar .read = native_apic_msr_read, 180f62bae50SIngo Molnar .write = native_apic_msr_write, 1810ab711aeSMichael S. Tsirkin .eoi_write = native_apic_msr_eoi_write, 182f62bae50SIngo Molnar .icr_read = native_x2apic_icr_read, 183f62bae50SIngo Molnar .icr_write = native_x2apic_icr_write, 184f62bae50SIngo Molnar .wait_icr_idle = native_x2apic_wait_icr_idle, 185f62bae50SIngo Molnar .safe_wait_icr_idle = native_safe_x2apic_wait_icr_idle, 186f62bae50SIngo Molnar }; 187107e0e0cSSuresh Siddha 188107e0e0cSSuresh Siddha apic_driver(apic_x2apic_phys); 189