1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2f62bae50SIngo Molnar 3521b82feSThomas Gleixner #include <linux/cpumask.h> 4521b82feSThomas Gleixner #include <linux/acpi.h> 5521b82feSThomas Gleixner 6c94f0718SThomas Gleixner #include "local.h" 7f62bae50SIngo Molnar 8ef1f87aaSSuresh Siddha int x2apic_phys; 9f62bae50SIngo Molnar 101a8880a1SSuresh Siddha static struct apic apic_x2apic_phys; 1126573a97SDavid Woodhouse static u32 x2apic_max_apicid __ro_after_init; 1226573a97SDavid Woodhouse 1326573a97SDavid Woodhouse void __init x2apic_set_max_apicid(u32 apicid) 1426573a97SDavid Woodhouse { 1526573a97SDavid Woodhouse x2apic_max_apicid = apicid; 1626573a97SDavid Woodhouse } 171a8880a1SSuresh Siddha 18afed7d17SDou Liyang static int __init set_x2apic_phys_mode(char *arg) 19f62bae50SIngo Molnar { 20f62bae50SIngo Molnar x2apic_phys = 1; 21f62bae50SIngo Molnar return 0; 22f62bae50SIngo Molnar } 23f62bae50SIngo Molnar early_param("x2apic_phys", set_x2apic_phys_mode); 24f62bae50SIngo Molnar 25cb214edeSStoney Wang static bool x2apic_fadt_phys(void) 26cb214edeSStoney Wang { 27781674fcSJan Kiszka #ifdef CONFIG_ACPI 28cb214edeSStoney Wang if ((acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID) && 29cb214edeSStoney Wang (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) { 30cb214edeSStoney Wang printk(KERN_DEBUG "System requires x2apic physical mode\n"); 31cb214edeSStoney Wang return true; 32cb214edeSStoney Wang } 33781674fcSJan Kiszka #endif 34cb214edeSStoney Wang return false; 35cb214edeSStoney Wang } 36cb214edeSStoney Wang 37f62bae50SIngo Molnar static int x2apic_acpi_madt_oem_check(char *oem_id, char *oem_table_id) 38f62bae50SIngo Molnar { 39cb214edeSStoney Wang return x2apic_enabled() && (x2apic_phys || x2apic_fadt_phys()); 40f62bae50SIngo Molnar } 41f62bae50SIngo Molnar 42f2bffe8aSThomas Gleixner static void x2apic_send_IPI(int cpu, int vector) 43f2bffe8aSThomas Gleixner { 44f2bffe8aSThomas Gleixner u32 dest = per_cpu(x86_cpu_to_apicid, cpu); 45f2bffe8aSThomas Gleixner 4625a068b8SDave Hansen /* x2apic MSRs are special and need a special fence: */ 4725a068b8SDave Hansen weak_wrmsr_fence(); 48f2bffe8aSThomas Gleixner __x2apic_send_IPI_dest(dest, vector, APIC_DEST_PHYSICAL); 49f2bffe8aSThomas Gleixner } 50f2bffe8aSThomas Gleixner 51f62bae50SIngo Molnar static void 52a27d0b5eSSuresh Siddha __x2apic_send_IPI_mask(const struct cpumask *mask, int vector, int apic_dest) 53f62bae50SIngo Molnar { 54f62bae50SIngo Molnar unsigned long query_cpu; 55a27d0b5eSSuresh Siddha unsigned long this_cpu; 56f62bae50SIngo Molnar unsigned long flags; 57f62bae50SIngo Molnar 5825a068b8SDave Hansen /* x2apic MSRs are special and need a special fence: */ 5925a068b8SDave Hansen weak_wrmsr_fence(); 60ce4e240cSSuresh Siddha 61f62bae50SIngo Molnar local_irq_save(flags); 62a27d0b5eSSuresh Siddha 63a27d0b5eSSuresh Siddha this_cpu = smp_processor_id(); 64f62bae50SIngo Molnar for_each_cpu(query_cpu, mask) { 65a27d0b5eSSuresh Siddha if (apic_dest == APIC_DEST_ALLBUT && this_cpu == query_cpu) 66f62bae50SIngo Molnar continue; 67f62bae50SIngo Molnar __x2apic_send_IPI_dest(per_cpu(x86_cpu_to_apicid, query_cpu), 68f62bae50SIngo Molnar vector, APIC_DEST_PHYSICAL); 69f62bae50SIngo Molnar } 70f62bae50SIngo Molnar local_irq_restore(flags); 71f62bae50SIngo Molnar } 72f62bae50SIngo Molnar 73a27d0b5eSSuresh Siddha static void x2apic_send_IPI_mask(const struct cpumask *mask, int vector) 74a27d0b5eSSuresh Siddha { 75a27d0b5eSSuresh Siddha __x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLINC); 76a27d0b5eSSuresh Siddha } 77a27d0b5eSSuresh Siddha 78a27d0b5eSSuresh Siddha static void 79a27d0b5eSSuresh Siddha x2apic_send_IPI_mask_allbutself(const struct cpumask *mask, int vector) 80a27d0b5eSSuresh Siddha { 81a27d0b5eSSuresh Siddha __x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLBUT); 82a27d0b5eSSuresh Siddha } 83a27d0b5eSSuresh Siddha 84*96ae35c7SThomas Gleixner static void __x2apic_send_IPI_shorthand(int vector, u32 which) 85*96ae35c7SThomas Gleixner { 86*96ae35c7SThomas Gleixner unsigned long cfg = __prepare_ICR(which, vector, 0); 87*96ae35c7SThomas Gleixner 88*96ae35c7SThomas Gleixner /* x2apic MSRs are special and need a special fence: */ 89*96ae35c7SThomas Gleixner weak_wrmsr_fence(); 90*96ae35c7SThomas Gleixner native_x2apic_icr_write(cfg, 0); 91*96ae35c7SThomas Gleixner } 92*96ae35c7SThomas Gleixner 93*96ae35c7SThomas Gleixner void x2apic_send_IPI_allbutself(int vector) 94a27d0b5eSSuresh Siddha { 9543931d35SThomas Gleixner __x2apic_send_IPI_shorthand(vector, APIC_DEST_ALLBUT); 96a27d0b5eSSuresh Siddha } 97a27d0b5eSSuresh Siddha 98*96ae35c7SThomas Gleixner void x2apic_send_IPI_all(int vector) 99f62bae50SIngo Molnar { 10043931d35SThomas Gleixner __x2apic_send_IPI_shorthand(vector, APIC_DEST_ALLINC); 101f62bae50SIngo Molnar } 102f62bae50SIngo Molnar 103*96ae35c7SThomas Gleixner void x2apic_send_IPI_self(int vector) 104*96ae35c7SThomas Gleixner { 105*96ae35c7SThomas Gleixner apic_write(APIC_SELF_IPI, vector); 106*96ae35c7SThomas Gleixner } 107*96ae35c7SThomas Gleixner 108*96ae35c7SThomas Gleixner void __x2apic_send_IPI_dest(unsigned int apicid, int vector, unsigned int dest) 109*96ae35c7SThomas Gleixner { 110*96ae35c7SThomas Gleixner unsigned long cfg = __prepare_ICR(0, vector, dest); 111*96ae35c7SThomas Gleixner native_x2apic_icr_write(cfg, apicid); 112*96ae35c7SThomas Gleixner } 113*96ae35c7SThomas Gleixner 1149ebd680bSSuresh Siddha static int x2apic_phys_probe(void) 1159ebd680bSSuresh Siddha { 11685d38d58SDheeraj Kumar Srivastava if (!x2apic_mode) 11785d38d58SDheeraj Kumar Srivastava return 0; 11885d38d58SDheeraj Kumar Srivastava 11985d38d58SDheeraj Kumar Srivastava if (x2apic_phys || x2apic_fadt_phys()) 1209ebd680bSSuresh Siddha return 1; 1219ebd680bSSuresh Siddha 1229ebd680bSSuresh Siddha return apic == &apic_x2apic_phys; 1239ebd680bSSuresh Siddha } 1249ebd680bSSuresh Siddha 125981c2eacSThomas Gleixner /* Common x2apic functions, also used by x2apic_cluster */ 126a774635dSLi RongQing int x2apic_apic_id_valid(u32 apicid) 127981c2eacSThomas Gleixner { 12826573a97SDavid Woodhouse if (x2apic_max_apicid && apicid > x2apic_max_apicid) 12926573a97SDavid Woodhouse return 0; 13026573a97SDavid Woodhouse 131981c2eacSThomas Gleixner return 1; 132981c2eacSThomas Gleixner } 133981c2eacSThomas Gleixner 134981c2eacSThomas Gleixner unsigned int x2apic_get_apic_id(unsigned long id) 135981c2eacSThomas Gleixner { 136981c2eacSThomas Gleixner return id; 137981c2eacSThomas Gleixner } 138981c2eacSThomas Gleixner 139727657e6SThomas Gleixner u32 x2apic_set_apic_id(unsigned int id) 140981c2eacSThomas Gleixner { 141981c2eacSThomas Gleixner return id; 142981c2eacSThomas Gleixner } 143981c2eacSThomas Gleixner 144981c2eacSThomas Gleixner int x2apic_phys_pkg_id(int initial_apicid, int index_msb) 145981c2eacSThomas Gleixner { 146981c2eacSThomas Gleixner return initial_apicid >> index_msb; 147981c2eacSThomas Gleixner } 148981c2eacSThomas Gleixner 149404f6aacSKees Cook static struct apic apic_x2apic_phys __ro_after_init = { 150f62bae50SIngo Molnar 151f62bae50SIngo Molnar .name = "physical x2apic", 1529ebd680bSSuresh Siddha .probe = x2apic_phys_probe, 153f62bae50SIngo Molnar .acpi_madt_oem_check = x2apic_acpi_madt_oem_check, 154b7157acfSSteffen Persvold .apic_id_valid = x2apic_apic_id_valid, 155f62bae50SIngo Molnar 15672161299SThomas Gleixner .delivery_mode = APIC_DELIVERY_MODE_FIXED, 1578c44963bSThomas Gleixner .dest_mode_logical = false, 158f62bae50SIngo Molnar 159f62bae50SIngo Molnar .disable_esr = 0, 160e57d04e5SThomas Gleixner 161f62bae50SIngo Molnar .check_apicid_used = NULL, 162f62bae50SIngo Molnar .ioapic_phys_id_map = NULL, 163f62bae50SIngo Molnar .cpu_present_to_apicid = default_cpu_present_to_apicid, 164f62bae50SIngo Molnar .phys_pkg_id = x2apic_phys_pkg_id, 165f62bae50SIngo Molnar 16679deb8e5SCyrill Gorcunov .get_apic_id = x2apic_get_apic_id, 16779deb8e5SCyrill Gorcunov .set_apic_id = x2apic_set_apic_id, 168f62bae50SIngo Molnar 1699f9e3bb1SThomas Gleixner .calc_dest_apicid = apic_default_calc_apicid, 170f62bae50SIngo Molnar 171f2bffe8aSThomas Gleixner .send_IPI = x2apic_send_IPI, 172f62bae50SIngo Molnar .send_IPI_mask = x2apic_send_IPI_mask, 173f62bae50SIngo Molnar .send_IPI_mask_allbutself = x2apic_send_IPI_mask_allbutself, 174f62bae50SIngo Molnar .send_IPI_allbutself = x2apic_send_IPI_allbutself, 175f62bae50SIngo Molnar .send_IPI_all = x2apic_send_IPI_all, 176f62bae50SIngo Molnar .send_IPI_self = x2apic_send_IPI_self, 177f62bae50SIngo Molnar 178f62bae50SIngo Molnar .read = native_apic_msr_read, 179f62bae50SIngo Molnar .write = native_apic_msr_write, 1800ab711aeSMichael S. Tsirkin .eoi_write = native_apic_msr_eoi_write, 181f62bae50SIngo Molnar .icr_read = native_x2apic_icr_read, 182f62bae50SIngo Molnar .icr_write = native_x2apic_icr_write, 183f62bae50SIngo Molnar }; 184107e0e0cSSuresh Siddha 185107e0e0cSSuresh Siddha apic_driver(apic_x2apic_phys); 186