1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2004 James Cleverdon, IBM.
5 * Flat APIC subarch code.
7 * Hacked for x86-64 by James Cleverdon from i386 architecture code by
8 * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and
11 #include <linux/acpi.h>
12 #include <linux/errno.h>
13 #include <linux/threads.h>
14 #include <linux/cpumask.h>
15 #include <linux/string.h>
16 #include <linux/kernel.h>
17 #include <linux/ctype.h>
18 #include <linux/hardirq.h>
19 #include <linux/export.h>
24 #include <asm/apic_flat_64.h>
25 #include <asm/jailhouse_para.h>
27 static struct apic apic_physflat;
28 static struct apic apic_flat;
30 struct apic *apic __ro_after_init = &apic_flat;
31 EXPORT_SYMBOL_GPL(apic);
33 static int flat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
39 * Set up the logical destination ID.
41 * Intel recommends to set DFR, LDR and TPR before enabling
42 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
43 * document number 292116). So here it goes...
45 void flat_init_apic_ldr(void)
48 unsigned long num, id;
50 num = smp_processor_id();
52 apic_write(APIC_DFR, APIC_DFR_FLAT);
53 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
54 val |= SET_APIC_LOGICAL_ID(id);
55 apic_write(APIC_LDR, val);
58 static void _flat_send_IPI_mask(unsigned long mask, int vector)
62 local_irq_save(flags);
63 __default_send_IPI_dest_field(mask, vector, apic->dest_logical);
64 local_irq_restore(flags);
67 static void flat_send_IPI_mask(const struct cpumask *cpumask, int vector)
69 unsigned long mask = cpumask_bits(cpumask)[0];
71 _flat_send_IPI_mask(mask, vector);
75 flat_send_IPI_mask_allbutself(const struct cpumask *cpumask, int vector)
77 unsigned long mask = cpumask_bits(cpumask)[0];
78 int cpu = smp_processor_id();
80 if (cpu < BITS_PER_LONG)
81 __clear_bit(cpu, &mask);
83 _flat_send_IPI_mask(mask, vector);
86 static void flat_send_IPI_allbutself(int vector)
88 int cpu = smp_processor_id();
90 if (IS_ENABLED(CONFIG_HOTPLUG_CPU) || vector == NMI_VECTOR) {
91 if (!cpumask_equal(cpu_online_mask, cpumask_of(cpu))) {
92 unsigned long mask = cpumask_bits(cpu_online_mask)[0];
94 if (cpu < BITS_PER_LONG)
95 __clear_bit(cpu, &mask);
97 _flat_send_IPI_mask(mask, vector);
99 } else if (num_online_cpus() > 1) {
100 __default_send_IPI_shortcut(APIC_DEST_ALLBUT,
101 vector, apic->dest_logical);
105 static void flat_send_IPI_all(int vector)
107 if (vector == NMI_VECTOR) {
108 flat_send_IPI_mask(cpu_online_mask, vector);
110 __default_send_IPI_shortcut(APIC_DEST_ALLINC,
111 vector, apic->dest_logical);
115 static unsigned int flat_get_apic_id(unsigned long x)
117 return (x >> 24) & 0xFF;
120 static u32 set_apic_id(unsigned int id)
122 return (id & 0xFF) << 24;
125 static unsigned int read_xapic_id(void)
127 return flat_get_apic_id(apic_read(APIC_ID));
130 static int flat_apic_id_registered(void)
132 return physid_isset(read_xapic_id(), phys_cpu_present_map);
135 static int flat_phys_pkg_id(int initial_apic_id, int index_msb)
137 return initial_apic_id >> index_msb;
140 static int flat_probe(void)
145 static struct apic apic_flat __ro_after_init = {
148 .acpi_madt_oem_check = flat_acpi_madt_oem_check,
149 .apic_id_valid = default_apic_id_valid,
150 .apic_id_registered = flat_apic_id_registered,
152 .irq_delivery_mode = dest_Fixed,
153 .irq_dest_mode = 1, /* logical */
156 .dest_logical = APIC_DEST_LOGICAL,
157 .check_apicid_used = NULL,
159 .init_apic_ldr = flat_init_apic_ldr,
161 .ioapic_phys_id_map = NULL,
162 .setup_apic_routing = NULL,
163 .cpu_present_to_apicid = default_cpu_present_to_apicid,
164 .apicid_to_cpu_present = NULL,
165 .check_phys_apicid_present = default_check_phys_apicid_present,
166 .phys_pkg_id = flat_phys_pkg_id,
168 .get_apic_id = flat_get_apic_id,
169 .set_apic_id = set_apic_id,
171 .calc_dest_apicid = apic_flat_calc_apicid,
173 .send_IPI = default_send_IPI_single,
174 .send_IPI_mask = flat_send_IPI_mask,
175 .send_IPI_mask_allbutself = flat_send_IPI_mask_allbutself,
176 .send_IPI_allbutself = flat_send_IPI_allbutself,
177 .send_IPI_all = flat_send_IPI_all,
178 .send_IPI_self = apic_send_IPI_self,
180 .inquire_remote_apic = default_inquire_remote_apic,
182 .read = native_apic_mem_read,
183 .write = native_apic_mem_write,
184 .eoi_write = native_apic_mem_write,
185 .icr_read = native_apic_icr_read,
186 .icr_write = native_apic_icr_write,
187 .wait_icr_idle = native_apic_wait_icr_idle,
188 .safe_wait_icr_idle = native_safe_apic_wait_icr_idle,
192 * Physflat mode is used when there are more than 8 CPUs on a system.
193 * We cannot use logical delivery in this case because the mask
194 * overflows, so use physical mode.
196 static int physflat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
200 * Quirk: some x86_64 machines can only use physical APIC mode
201 * regardless of how many processors are present (x86_64 ES7000
204 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
205 (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
206 printk(KERN_DEBUG "system APIC only can use physical flat");
210 if (!strncmp(oem_id, "IBM", 3) && !strncmp(oem_table_id, "EXA", 3)) {
211 printk(KERN_DEBUG "IBM Summit detected, will use apic physical");
219 static void physflat_init_apic_ldr(void)
222 * LDR and DFR are not involved in physflat mode, rather:
223 * "In physical destination mode, the destination processor is
224 * specified by its local APIC ID [...]." (Intel SDM, 10.6.2.1)
228 static void physflat_send_IPI_allbutself(int vector)
230 default_send_IPI_mask_allbutself_phys(cpu_online_mask, vector);
233 static void physflat_send_IPI_all(int vector)
235 default_send_IPI_mask_sequence_phys(cpu_online_mask, vector);
238 static int physflat_probe(void)
240 if (apic == &apic_physflat || num_possible_cpus() > 8 ||
241 jailhouse_paravirt())
247 static struct apic apic_physflat __ro_after_init = {
249 .name = "physical flat",
250 .probe = physflat_probe,
251 .acpi_madt_oem_check = physflat_acpi_madt_oem_check,
252 .apic_id_valid = default_apic_id_valid,
253 .apic_id_registered = flat_apic_id_registered,
255 .irq_delivery_mode = dest_Fixed,
256 .irq_dest_mode = 0, /* physical */
260 .check_apicid_used = NULL,
262 .init_apic_ldr = physflat_init_apic_ldr,
264 .ioapic_phys_id_map = NULL,
265 .setup_apic_routing = NULL,
266 .cpu_present_to_apicid = default_cpu_present_to_apicid,
267 .apicid_to_cpu_present = NULL,
268 .check_phys_apicid_present = default_check_phys_apicid_present,
269 .phys_pkg_id = flat_phys_pkg_id,
271 .get_apic_id = flat_get_apic_id,
272 .set_apic_id = set_apic_id,
274 .calc_dest_apicid = apic_default_calc_apicid,
276 .send_IPI = default_send_IPI_single_phys,
277 .send_IPI_mask = default_send_IPI_mask_sequence_phys,
278 .send_IPI_mask_allbutself = default_send_IPI_mask_allbutself_phys,
279 .send_IPI_allbutself = physflat_send_IPI_allbutself,
280 .send_IPI_all = physflat_send_IPI_all,
281 .send_IPI_self = apic_send_IPI_self,
283 .inquire_remote_apic = default_inquire_remote_apic,
285 .read = native_apic_mem_read,
286 .write = native_apic_mem_write,
287 .eoi_write = native_apic_mem_write,
288 .icr_read = native_apic_icr_read,
289 .icr_write = native_apic_icr_write,
290 .wait_icr_idle = native_apic_wait_icr_idle,
291 .safe_wait_icr_idle = native_safe_apic_wait_icr_idle,
295 * We need to check for physflat first, so this order is important.
297 apic_drivers(apic_physflat, apic_flat);