Thomas Gleixner | 7e300da | 2019-05-28 10:10:25 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Default generic APIC driver. This handles up to 8 CPUs. |
| 4 | * |
| 5 | * Copyright 2003 Andi Kleen, SuSE Labs. |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 6 | * |
| 7 | * Generic x86 APIC driver probe layer. |
| 8 | */ |
Paul Gortmaker | 186f436 | 2016-07-13 20:18:56 -0400 | [diff] [blame] | 9 | #include <linux/export.h> |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 10 | #include <linux/errno.h> |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 11 | #include <linux/smp.h> |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 12 | |
Ingo Molnar | 13c0113 | 2020-08-06 14:34:32 +0200 | [diff] [blame] | 13 | #include <asm/io_apic.h> |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 14 | #include <asm/apic.h> |
Ingo Molnar | 6bda2c8 | 2009-01-28 19:32:55 +0100 | [diff] [blame] | 15 | #include <asm/acpi.h> |
Ingo Molnar | 6bda2c8 | 2009-01-28 19:32:55 +0100 | [diff] [blame] | 16 | |
Thomas Gleixner | c94f071 | 2019-07-22 20:47:14 +0200 | [diff] [blame] | 17 | #include "local.h" |
Ingo Molnar | 6bda2c8 | 2009-01-28 19:32:55 +0100 | [diff] [blame] | 18 | |
Tejun Heo | 3f6f679 | 2011-01-23 14:37:34 +0100 | [diff] [blame] | 19 | static int default_x86_32_early_logical_apicid(int cpu) |
| 20 | { |
| 21 | return 1 << cpu; |
| 22 | } |
| 23 | |
Suresh Siddha | 681ee44 | 2010-02-09 18:01:44 -0800 | [diff] [blame] | 24 | static void setup_apic_flat_routing(void) |
Ingo Molnar | 28aa29e | 2009-02-17 14:36:42 +0100 | [diff] [blame] | 25 | { |
| 26 | #ifdef CONFIG_X86_IO_APIC |
| 27 | printk(KERN_INFO |
| 28 | "Enabling APIC mode: Flat. Using %d I/O APICs\n", |
| 29 | nr_ioapics); |
| 30 | #endif |
| 31 | } |
| 32 | |
Thomas Gleixner | 0801bba | 2017-09-13 23:29:19 +0200 | [diff] [blame] | 33 | static int default_apic_id_registered(void) |
| 34 | { |
| 35 | return physid_isset(read_apic_id(), phys_cpu_present_map); |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | * Set up the logical destination ID. Intel recommends to set DFR, LDR and |
| 40 | * TPR before enabling an APIC. See e.g. "AP-388 82489DX User's Manual" |
| 41 | * (Intel document number 292116). |
| 42 | */ |
| 43 | static void default_init_apic_ldr(void) |
| 44 | { |
| 45 | unsigned long val; |
| 46 | |
| 47 | apic_write(APIC_DFR, APIC_DFR_VALUE); |
| 48 | val = apic_read(APIC_LDR) & ~APIC_LDR_MASK; |
| 49 | val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id()); |
| 50 | apic_write(APIC_LDR, val); |
| 51 | } |
| 52 | |
| 53 | static int default_phys_pkg_id(int cpuid_apic, int index_msb) |
| 54 | { |
| 55 | return cpuid_apic >> index_msb; |
| 56 | } |
| 57 | |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 58 | /* should be called last. */ |
| 59 | static int probe_default(void) |
| 60 | { |
| 61 | return 1; |
| 62 | } |
| 63 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 64 | static struct apic apic_default __ro_after_init = { |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 65 | |
| 66 | .name = "default", |
| 67 | .probe = probe_default, |
| 68 | .acpi_madt_oem_check = NULL, |
Daniel J Blueman | fa63030 | 2012-03-14 15:17:34 +0800 | [diff] [blame] | 69 | .apic_id_valid = default_apic_id_valid, |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 70 | .apic_id_registered = default_apic_id_registered, |
| 71 | |
Thomas Gleixner | 7216129 | 2020-10-24 22:35:05 +0100 | [diff] [blame] | 72 | .delivery_mode = APIC_DELIVERY_MODE_FIXED, |
Thomas Gleixner | 8c44963 | 2020-10-24 22:35:08 +0100 | [diff] [blame] | 73 | .dest_mode_logical = true, |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 74 | |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 75 | .disable_esr = 0, |
Thomas Gleixner | e57d04e | 2020-10-24 22:35:07 +0100 | [diff] [blame] | 76 | |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 77 | .check_apicid_used = default_check_apicid_used, |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 78 | .init_apic_ldr = default_init_apic_ldr, |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 79 | .ioapic_phys_id_map = default_ioapic_phys_id_map, |
Suresh Siddha | 681ee44 | 2010-02-09 18:01:44 -0800 | [diff] [blame] | 80 | .setup_apic_routing = setup_apic_flat_routing, |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 81 | .cpu_present_to_apicid = default_cpu_present_to_apicid, |
Cyrill Gorcunov | 7abc075 | 2009-11-10 01:06:59 +0300 | [diff] [blame] | 82 | .apicid_to_cpu_present = physid_set_mask_of_physid, |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 83 | .check_phys_apicid_present = default_check_phys_apicid_present, |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 84 | .phys_pkg_id = default_phys_pkg_id, |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 85 | |
| 86 | .get_apic_id = default_get_apic_id, |
| 87 | .set_apic_id = NULL, |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 88 | |
Thomas Gleixner | 9f9e3bb | 2017-09-13 23:29:37 +0200 | [diff] [blame] | 89 | .calc_dest_apicid = apic_flat_calc_apicid, |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 90 | |
Thomas Gleixner | 6153058 | 2015-11-04 22:57:08 +0000 | [diff] [blame] | 91 | .send_IPI = default_send_IPI_single, |
Yinghai Lu | 43f3989 | 2009-01-29 19:31:49 -0800 | [diff] [blame] | 92 | .send_IPI_mask = default_send_IPI_mask_logical, |
| 93 | .send_IPI_mask_allbutself = default_send_IPI_mask_allbutself_logical, |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 94 | .send_IPI_allbutself = default_send_IPI_allbutself, |
| 95 | .send_IPI_all = default_send_IPI_all, |
Ingo Molnar | 6b64ee0 | 2009-01-30 23:42:18 +0100 | [diff] [blame] | 96 | .send_IPI_self = default_send_IPI_self, |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 97 | |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 98 | .inquire_remote_apic = default_inquire_remote_apic, |
Yinghai Lu | c1eeb2d | 2009-02-16 23:02:14 -0800 | [diff] [blame] | 99 | |
| 100 | .read = native_apic_mem_read, |
| 101 | .write = native_apic_mem_write, |
Michael S. Tsirkin | 2a43195 | 2012-05-16 19:03:52 +0300 | [diff] [blame] | 102 | .eoi_write = native_apic_mem_write, |
Yinghai Lu | c1eeb2d | 2009-02-16 23:02:14 -0800 | [diff] [blame] | 103 | .icr_read = native_apic_icr_read, |
| 104 | .icr_write = native_apic_icr_write, |
| 105 | .wait_icr_idle = native_apic_wait_icr_idle, |
| 106 | .safe_wait_icr_idle = native_safe_apic_wait_icr_idle, |
Tejun Heo | acb8bc0 | 2011-01-23 14:37:33 +0100 | [diff] [blame] | 107 | |
Tejun Heo | 3f6f679 | 2011-01-23 14:37:34 +0100 | [diff] [blame] | 108 | .x86_32_early_logical_apicid = default_x86_32_early_logical_apicid, |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 109 | }; |
| 110 | |
Suresh Siddha | 107e0e0 | 2011-05-20 17:51:17 -0700 | [diff] [blame] | 111 | apic_driver(apic_default); |
| 112 | |
Kees Cook | 404f6aa | 2016-08-08 16:29:06 -0700 | [diff] [blame] | 113 | struct apic *apic __ro_after_init = &apic_default; |
Ingo Molnar | 7d01d32 | 2009-02-17 12:33:20 +0100 | [diff] [blame] | 114 | EXPORT_SYMBOL_GPL(apic); |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 115 | |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 116 | static int cmdline_apic __initdata; |
| 117 | static int __init parse_apic(char *arg) |
| 118 | { |
Suresh Siddha | 8b37e880 | 2011-05-20 17:51:18 -0700 | [diff] [blame] | 119 | struct apic **drv; |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 120 | |
| 121 | if (!arg) |
| 122 | return -EINVAL; |
| 123 | |
Suresh Siddha | 8b37e880 | 2011-05-20 17:51:18 -0700 | [diff] [blame] | 124 | for (drv = __apicdrivers; drv < __apicdrivers_end; drv++) { |
| 125 | if (!strcmp((*drv)->name, arg)) { |
| 126 | apic = *drv; |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 127 | cmdline_apic = 1; |
| 128 | return 0; |
| 129 | } |
| 130 | } |
| 131 | |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 132 | /* Parsed again by __setup for debug/verbose */ |
| 133 | return 0; |
| 134 | } |
| 135 | early_param("apic", parse_apic); |
| 136 | |
Suresh Siddha | 69c252f | 2011-05-20 17:51:19 -0700 | [diff] [blame] | 137 | void __init default_setup_apic_routing(void) |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 138 | { |
Denys Vlasenko | cff9ab2 | 2016-09-13 20:12:32 +0200 | [diff] [blame] | 139 | int version = boot_cpu_apic_version; |
Suresh Siddha | 69c252f | 2011-05-20 17:51:19 -0700 | [diff] [blame] | 140 | |
| 141 | if (num_possible_cpus() > 8) { |
| 142 | switch (boot_cpu_data.x86_vendor) { |
| 143 | case X86_VENDOR_INTEL: |
| 144 | if (!APIC_XAPIC(version)) { |
| 145 | def_to_bigsmp = 0; |
| 146 | break; |
| 147 | } |
Borislav Petkov | 5785675 | 2019-08-11 17:40:36 +0200 | [diff] [blame] | 148 | /* P4 and above */ |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 149 | fallthrough; |
Pu Wen | da33dfe | 2018-09-23 17:35:28 +0800 | [diff] [blame] | 150 | case X86_VENDOR_HYGON: |
Suresh Siddha | 69c252f | 2011-05-20 17:51:19 -0700 | [diff] [blame] | 151 | case X86_VENDOR_AMD: |
| 152 | def_to_bigsmp = 1; |
| 153 | } |
| 154 | } |
| 155 | |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 156 | #ifdef CONFIG_X86_BIGSMP |
| 157 | /* |
Suresh Siddha | 69c252f | 2011-05-20 17:51:19 -0700 | [diff] [blame] | 158 | * This is used to switch to bigsmp mode when |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 159 | * - There is no apic= option specified by the user |
| 160 | * - generic_apic_probe() has chosen apic_default as the sub_arch |
| 161 | * - we find more than 8 CPUs in acpi LAPIC listing with xAPIC support |
| 162 | */ |
| 163 | |
Jan Beulich | 838312b | 2011-09-28 16:44:54 +0100 | [diff] [blame] | 164 | if (!cmdline_apic && apic == &apic_default) |
| 165 | generic_bigsmp_probe(); |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 166 | #endif |
Suresh Siddha | 69c252f | 2011-05-20 17:51:19 -0700 | [diff] [blame] | 167 | |
| 168 | if (apic->setup_apic_routing) |
| 169 | apic->setup_apic_routing(); |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void __init generic_apic_probe(void) |
| 173 | { |
| 174 | if (!cmdline_apic) { |
Suresh Siddha | 8b37e880 | 2011-05-20 17:51:18 -0700 | [diff] [blame] | 175 | struct apic **drv; |
| 176 | |
| 177 | for (drv = __apicdrivers; drv < __apicdrivers_end; drv++) { |
| 178 | if ((*drv)->probe()) { |
| 179 | apic = *drv; |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 180 | break; |
| 181 | } |
| 182 | } |
| 183 | /* Not visible without early console */ |
Suresh Siddha | 8b37e880 | 2011-05-20 17:51:18 -0700 | [diff] [blame] | 184 | if (drv == __apicdrivers_end) |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 185 | panic("Didn't find an APIC driver"); |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 186 | } |
| 187 | printk(KERN_INFO "Using APIC driver %s\n", apic->name); |
| 188 | } |
| 189 | |
David Rientjes | c460b5d | 2014-07-30 23:53:34 -0700 | [diff] [blame] | 190 | /* This function can switch the APIC even after the initial ->probe() */ |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 191 | int __init default_acpi_madt_oem_check(char *oem_id, char *oem_table_id) |
| 192 | { |
Suresh Siddha | 8b37e880 | 2011-05-20 17:51:18 -0700 | [diff] [blame] | 193 | struct apic **drv; |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 194 | |
Suresh Siddha | 8b37e880 | 2011-05-20 17:51:18 -0700 | [diff] [blame] | 195 | for (drv = __apicdrivers; drv < __apicdrivers_end; drv++) { |
| 196 | if (!(*drv)->acpi_madt_oem_check) |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 197 | continue; |
Suresh Siddha | 8b37e880 | 2011-05-20 17:51:18 -0700 | [diff] [blame] | 198 | if (!(*drv)->acpi_madt_oem_check(oem_id, oem_table_id)) |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 199 | continue; |
| 200 | |
| 201 | if (!cmdline_apic) { |
Suresh Siddha | 8b37e880 | 2011-05-20 17:51:18 -0700 | [diff] [blame] | 202 | apic = *drv; |
Ingo Molnar | 7b38725 | 2009-01-28 19:11:44 +0100 | [diff] [blame] | 203 | printk(KERN_INFO "Switched to APIC driver `%s'.\n", |
| 204 | apic->name); |
| 205 | } |
| 206 | return 1; |
| 207 | } |
| 208 | return 0; |
| 209 | } |