2 * Copyright 2004 James Cleverdon, IBM.
3 * Subject to the GNU Public License, v.2
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/errno.h>
12 #include <linux/threads.h>
13 #include <linux/cpumask.h>
14 #include <linux/string.h>
15 #include <linux/kernel.h>
16 #include <linux/ctype.h>
17 #include <linux/hardirq.h>
18 #include <linux/export.h>
22 #include <asm/jailhouse_para.h>
24 #include <linux/acpi.h>
26 static struct apic apic_physflat
;
27 static struct apic apic_flat
;
29 struct apic
*apic __ro_after_init
= &apic_flat
;
30 EXPORT_SYMBOL_GPL(apic
);
32 static int flat_acpi_madt_oem_check(char *oem_id
, char *oem_table_id
)
38 * Set up the logical destination ID.
40 * Intel recommends to set DFR, LDR and TPR before enabling
41 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
42 * document number 292116). So here it goes...
44 void flat_init_apic_ldr(void)
47 unsigned long num
, id
;
49 num
= smp_processor_id();
51 apic_write(APIC_DFR
, APIC_DFR_FLAT
);
52 val
= apic_read(APIC_LDR
) & ~APIC_LDR_MASK
;
53 val
|= SET_APIC_LOGICAL_ID(id
);
54 apic_write(APIC_LDR
, val
);
57 static void _flat_send_IPI_mask(unsigned long mask
, int vector
)
61 local_irq_save(flags
);
62 __default_send_IPI_dest_field(mask
, vector
, apic
->dest_logical
);
63 local_irq_restore(flags
);
66 static void flat_send_IPI_mask(const struct cpumask
*cpumask
, int vector
)
68 unsigned long mask
= cpumask_bits(cpumask
)[0];
70 _flat_send_IPI_mask(mask
, vector
);
74 flat_send_IPI_mask_allbutself(const struct cpumask
*cpumask
, int vector
)
76 unsigned long mask
= cpumask_bits(cpumask
)[0];
77 int cpu
= smp_processor_id();
79 if (cpu
< BITS_PER_LONG
)
80 clear_bit(cpu
, &mask
);
82 _flat_send_IPI_mask(mask
, vector
);
85 static void flat_send_IPI_allbutself(int vector
)
87 int cpu
= smp_processor_id();
89 if (IS_ENABLED(CONFIG_HOTPLUG_CPU
) || vector
== NMI_VECTOR
) {
90 if (!cpumask_equal(cpu_online_mask
, cpumask_of(cpu
))) {
91 unsigned long mask
= cpumask_bits(cpu_online_mask
)[0];
93 if (cpu
< BITS_PER_LONG
)
94 clear_bit(cpu
, &mask
);
96 _flat_send_IPI_mask(mask
, vector
);
98 } else if (num_online_cpus() > 1) {
99 __default_send_IPI_shortcut(APIC_DEST_ALLBUT
,
100 vector
, apic
->dest_logical
);
104 static void flat_send_IPI_all(int vector
)
106 if (vector
== NMI_VECTOR
) {
107 flat_send_IPI_mask(cpu_online_mask
, vector
);
109 __default_send_IPI_shortcut(APIC_DEST_ALLINC
,
110 vector
, apic
->dest_logical
);
114 static unsigned int flat_get_apic_id(unsigned long x
)
116 return (x
>> 24) & 0xFF;
119 static u32
set_apic_id(unsigned int id
)
121 return (id
& 0xFF) << 24;
124 static unsigned int read_xapic_id(void)
126 return flat_get_apic_id(apic_read(APIC_ID
));
129 static int flat_apic_id_registered(void)
131 return physid_isset(read_xapic_id(), phys_cpu_present_map
);
134 static int flat_phys_pkg_id(int initial_apic_id
, int index_msb
)
136 return initial_apic_id
>> index_msb
;
139 static int flat_probe(void)
144 static struct apic apic_flat __ro_after_init
= {
147 .acpi_madt_oem_check
= flat_acpi_madt_oem_check
,
148 .apic_id_valid
= default_apic_id_valid
,
149 .apic_id_registered
= flat_apic_id_registered
,
151 .irq_delivery_mode
= dest_Fixed
,
152 .irq_dest_mode
= 1, /* logical */
155 .dest_logical
= APIC_DEST_LOGICAL
,
156 .check_apicid_used
= NULL
,
158 .init_apic_ldr
= flat_init_apic_ldr
,
160 .ioapic_phys_id_map
= NULL
,
161 .setup_apic_routing
= NULL
,
162 .cpu_present_to_apicid
= default_cpu_present_to_apicid
,
163 .apicid_to_cpu_present
= NULL
,
164 .check_phys_apicid_present
= default_check_phys_apicid_present
,
165 .phys_pkg_id
= flat_phys_pkg_id
,
167 .get_apic_id
= flat_get_apic_id
,
168 .set_apic_id
= set_apic_id
,
170 .calc_dest_apicid
= apic_flat_calc_apicid
,
172 .send_IPI
= default_send_IPI_single
,
173 .send_IPI_mask
= flat_send_IPI_mask
,
174 .send_IPI_mask_allbutself
= flat_send_IPI_mask_allbutself
,
175 .send_IPI_allbutself
= flat_send_IPI_allbutself
,
176 .send_IPI_all
= flat_send_IPI_all
,
177 .send_IPI_self
= apic_send_IPI_self
,
179 .inquire_remote_apic
= default_inquire_remote_apic
,
181 .read
= native_apic_mem_read
,
182 .write
= native_apic_mem_write
,
183 .eoi_write
= native_apic_mem_write
,
184 .icr_read
= native_apic_icr_read
,
185 .icr_write
= native_apic_icr_write
,
186 .wait_icr_idle
= native_apic_wait_icr_idle
,
187 .safe_wait_icr_idle
= native_safe_apic_wait_icr_idle
,
191 * Physflat mode is used when there are more than 8 CPUs on a system.
192 * We cannot use logical delivery in this case because the mask
193 * overflows, so use physical mode.
195 static int physflat_acpi_madt_oem_check(char *oem_id
, char *oem_table_id
)
199 * Quirk: some x86_64 machines can only use physical APIC mode
200 * regardless of how many processors are present (x86_64 ES7000
203 if (acpi_gbl_FADT
.header
.revision
>= FADT2_REVISION_ID
&&
204 (acpi_gbl_FADT
.flags
& ACPI_FADT_APIC_PHYSICAL
)) {
205 printk(KERN_DEBUG
"system APIC only can use physical flat");
209 if (!strncmp(oem_id
, "IBM", 3) && !strncmp(oem_table_id
, "EXA", 3)) {
210 printk(KERN_DEBUG
"IBM Summit detected, will use apic physical");
218 static void physflat_init_apic_ldr(void)
221 * LDR and DFR are not involved in physflat mode, rather:
222 * "In physical destination mode, the destination processor is
223 * specified by its local APIC ID [...]." (Intel SDM, 10.6.2.1)
227 static void physflat_send_IPI_allbutself(int vector
)
229 default_send_IPI_mask_allbutself_phys(cpu_online_mask
, vector
);
232 static void physflat_send_IPI_all(int vector
)
234 default_send_IPI_mask_sequence_phys(cpu_online_mask
, vector
);
237 static int physflat_probe(void)
239 if (apic
== &apic_physflat
|| num_possible_cpus() > 8 ||
240 jailhouse_paravirt())
246 static struct apic apic_physflat __ro_after_init
= {
248 .name
= "physical flat",
249 .probe
= physflat_probe
,
250 .acpi_madt_oem_check
= physflat_acpi_madt_oem_check
,
251 .apic_id_valid
= default_apic_id_valid
,
252 .apic_id_registered
= flat_apic_id_registered
,
254 .irq_delivery_mode
= dest_Fixed
,
255 .irq_dest_mode
= 0, /* physical */
259 .check_apicid_used
= NULL
,
261 .init_apic_ldr
= physflat_init_apic_ldr
,
263 .ioapic_phys_id_map
= NULL
,
264 .setup_apic_routing
= NULL
,
265 .cpu_present_to_apicid
= default_cpu_present_to_apicid
,
266 .apicid_to_cpu_present
= NULL
,
267 .check_phys_apicid_present
= default_check_phys_apicid_present
,
268 .phys_pkg_id
= flat_phys_pkg_id
,
270 .get_apic_id
= flat_get_apic_id
,
271 .set_apic_id
= set_apic_id
,
273 .calc_dest_apicid
= apic_default_calc_apicid
,
275 .send_IPI
= default_send_IPI_single_phys
,
276 .send_IPI_mask
= default_send_IPI_mask_sequence_phys
,
277 .send_IPI_mask_allbutself
= default_send_IPI_mask_allbutself_phys
,
278 .send_IPI_allbutself
= physflat_send_IPI_allbutself
,
279 .send_IPI_all
= physflat_send_IPI_all
,
280 .send_IPI_self
= apic_send_IPI_self
,
282 .inquire_remote_apic
= default_inquire_remote_apic
,
284 .read
= native_apic_mem_read
,
285 .write
= native_apic_mem_write
,
286 .eoi_write
= native_apic_mem_write
,
287 .icr_read
= native_apic_icr_read
,
288 .icr_write
= native_apic_icr_write
,
289 .wait_icr_idle
= native_apic_wait_icr_idle
,
290 .safe_wait_icr_idle
= native_safe_apic_wait_icr_idle
,
294 * We need to check for physflat first, so this order is important.
296 apic_drivers(apic_physflat
, apic_flat
);