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/init.h>
18 #include <linux/hardirq.h>
19 #include <linux/module.h>
25 #include <acpi/acpi_bus.h>
28 static struct apic apic_physflat
;
29 static struct apic apic_flat
;
31 struct apic __read_mostly
*apic
= &apic_flat
;
32 EXPORT_SYMBOL_GPL(apic
);
34 static int flat_acpi_madt_oem_check(char *oem_id
, char *oem_table_id
)
39 static const struct cpumask
*flat_target_cpus(void)
41 return cpu_online_mask
;
44 static void flat_vector_allocation_domain(int cpu
, struct cpumask
*retmask
)
46 /* Careful. Some cpus do not strictly honor the set of cpus
47 * specified in the interrupt destination when using lowest
48 * priority interrupt delivery mode.
50 * In particular there was a hyperthreading cpu observed to
51 * deliver interrupts to the wrong hyperthread when only one
52 * hyperthread was specified in the interrupt desitination.
54 cpumask_clear(retmask
);
55 cpumask_bits(retmask
)[0] = APIC_ALL_CPUS
;
59 * Set up the logical destination ID.
61 * Intel recommends to set DFR, LDR and TPR before enabling
62 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
63 * document number 292116). So here it goes...
65 void flat_init_apic_ldr(void)
68 unsigned long num
, id
;
70 num
= smp_processor_id();
72 apic_write(APIC_DFR
, APIC_DFR_FLAT
);
73 val
= apic_read(APIC_LDR
) & ~APIC_LDR_MASK
;
74 val
|= SET_APIC_LOGICAL_ID(id
);
75 apic_write(APIC_LDR
, val
);
78 static inline void _flat_send_IPI_mask(unsigned long mask
, int vector
)
82 local_irq_save(flags
);
83 __default_send_IPI_dest_field(mask
, vector
, apic
->dest_logical
);
84 local_irq_restore(flags
);
87 static void flat_send_IPI_mask(const struct cpumask
*cpumask
, int vector
)
89 unsigned long mask
= cpumask_bits(cpumask
)[0];
91 _flat_send_IPI_mask(mask
, vector
);
95 flat_send_IPI_mask_allbutself(const struct cpumask
*cpumask
, int vector
)
97 unsigned long mask
= cpumask_bits(cpumask
)[0];
98 int cpu
= smp_processor_id();
100 if (cpu
< BITS_PER_LONG
)
101 clear_bit(cpu
, &mask
);
103 _flat_send_IPI_mask(mask
, vector
);
106 static void flat_send_IPI_allbutself(int vector
)
108 int cpu
= smp_processor_id();
109 #ifdef CONFIG_HOTPLUG_CPU
114 if (hotplug
|| vector
== NMI_VECTOR
) {
115 if (!cpumask_equal(cpu_online_mask
, cpumask_of(cpu
))) {
116 unsigned long mask
= cpumask_bits(cpu_online_mask
)[0];
118 if (cpu
< BITS_PER_LONG
)
119 clear_bit(cpu
, &mask
);
121 _flat_send_IPI_mask(mask
, vector
);
123 } else if (num_online_cpus() > 1) {
124 __default_send_IPI_shortcut(APIC_DEST_ALLBUT
,
125 vector
, apic
->dest_logical
);
129 static void flat_send_IPI_all(int vector
)
131 if (vector
== NMI_VECTOR
) {
132 flat_send_IPI_mask(cpu_online_mask
, vector
);
134 __default_send_IPI_shortcut(APIC_DEST_ALLINC
,
135 vector
, apic
->dest_logical
);
139 static unsigned int flat_get_apic_id(unsigned long x
)
143 id
= (((x
)>>24) & 0xFFu
);
148 static unsigned long set_apic_id(unsigned int id
)
152 x
= ((id
& 0xFFu
)<<24);
156 static unsigned int read_xapic_id(void)
160 id
= flat_get_apic_id(apic_read(APIC_ID
));
164 static int flat_apic_id_registered(void)
166 return physid_isset(read_xapic_id(), phys_cpu_present_map
);
169 static int flat_phys_pkg_id(int initial_apic_id
, int index_msb
)
171 return initial_apic_id
>> index_msb
;
174 static int flat_probe(void)
179 static struct apic apic_flat
= {
182 .acpi_madt_oem_check
= flat_acpi_madt_oem_check
,
183 .apic_id_registered
= flat_apic_id_registered
,
185 .irq_delivery_mode
= dest_LowestPrio
,
186 .irq_dest_mode
= 1, /* logical */
188 .target_cpus
= flat_target_cpus
,
190 .dest_logical
= APIC_DEST_LOGICAL
,
191 .check_apicid_used
= NULL
,
192 .check_apicid_present
= NULL
,
194 .vector_allocation_domain
= flat_vector_allocation_domain
,
195 .init_apic_ldr
= flat_init_apic_ldr
,
197 .ioapic_phys_id_map
= NULL
,
198 .setup_apic_routing
= NULL
,
199 .multi_timer_check
= NULL
,
200 .cpu_present_to_apicid
= default_cpu_present_to_apicid
,
201 .apicid_to_cpu_present
= NULL
,
202 .setup_portio_remap
= NULL
,
203 .check_phys_apicid_present
= default_check_phys_apicid_present
,
204 .enable_apic_mode
= NULL
,
205 .phys_pkg_id
= flat_phys_pkg_id
,
206 .mps_oem_check
= NULL
,
208 .get_apic_id
= flat_get_apic_id
,
209 .set_apic_id
= set_apic_id
,
210 .apic_id_mask
= 0xFFu
<< 24,
212 .cpu_mask_to_apicid
= default_cpu_mask_to_apicid
,
213 .cpu_mask_to_apicid_and
= default_cpu_mask_to_apicid_and
,
215 .send_IPI_mask
= flat_send_IPI_mask
,
216 .send_IPI_mask_allbutself
= flat_send_IPI_mask_allbutself
,
217 .send_IPI_allbutself
= flat_send_IPI_allbutself
,
218 .send_IPI_all
= flat_send_IPI_all
,
219 .send_IPI_self
= apic_send_IPI_self
,
221 .trampoline_phys_low
= DEFAULT_TRAMPOLINE_PHYS_LOW
,
222 .trampoline_phys_high
= DEFAULT_TRAMPOLINE_PHYS_HIGH
,
223 .wait_for_init_deassert
= NULL
,
224 .smp_callin_clear_local_apic
= NULL
,
225 .inquire_remote_apic
= default_inquire_remote_apic
,
227 .read
= native_apic_mem_read
,
228 .write
= native_apic_mem_write
,
229 .icr_read
= native_apic_icr_read
,
230 .icr_write
= native_apic_icr_write
,
231 .wait_icr_idle
= native_apic_wait_icr_idle
,
232 .safe_wait_icr_idle
= native_safe_apic_wait_icr_idle
,
236 * Physflat mode is used when there are more than 8 CPUs on a system.
237 * We cannot use logical delivery in this case because the mask
238 * overflows, so use physical mode.
240 static int physflat_acpi_madt_oem_check(char *oem_id
, char *oem_table_id
)
244 * Quirk: some x86_64 machines can only use physical APIC mode
245 * regardless of how many processors are present (x86_64 ES7000
248 if (acpi_gbl_FADT
.header
.revision
>= FADT2_REVISION_ID
&&
249 (acpi_gbl_FADT
.flags
& ACPI_FADT_APIC_PHYSICAL
)) {
250 printk(KERN_DEBUG
"system APIC only can use physical flat");
254 if (!strncmp(oem_id
, "IBM", 3) && !strncmp(oem_table_id
, "EXA", 3)) {
255 printk(KERN_DEBUG
"IBM Summit detected, will use apic physical");
263 static const struct cpumask
*physflat_target_cpus(void)
265 return cpu_online_mask
;
268 static void physflat_vector_allocation_domain(int cpu
, struct cpumask
*retmask
)
270 cpumask_clear(retmask
);
271 cpumask_set_cpu(cpu
, retmask
);
274 static void physflat_send_IPI_mask(const struct cpumask
*cpumask
, int vector
)
276 default_send_IPI_mask_sequence_phys(cpumask
, vector
);
279 static void physflat_send_IPI_mask_allbutself(const struct cpumask
*cpumask
,
282 default_send_IPI_mask_allbutself_phys(cpumask
, vector
);
285 static void physflat_send_IPI_allbutself(int vector
)
287 default_send_IPI_mask_allbutself_phys(cpu_online_mask
, vector
);
290 static void physflat_send_IPI_all(int vector
)
292 physflat_send_IPI_mask(cpu_online_mask
, vector
);
295 static unsigned int physflat_cpu_mask_to_apicid(const struct cpumask
*cpumask
)
300 * We're using fixed IRQ delivery, can only return one phys APIC ID.
301 * May as well be the first.
303 cpu
= cpumask_first(cpumask
);
304 if ((unsigned)cpu
< nr_cpu_ids
)
305 return per_cpu(x86_cpu_to_apicid
, cpu
);
311 physflat_cpu_mask_to_apicid_and(const struct cpumask
*cpumask
,
312 const struct cpumask
*andmask
)
317 * We're using fixed IRQ delivery, can only return one phys APIC ID.
318 * May as well be the first.
320 for_each_cpu_and(cpu
, cpumask
, andmask
) {
321 if (cpumask_test_cpu(cpu
, cpu_online_mask
))
324 return per_cpu(x86_cpu_to_apicid
, cpu
);
327 static int physflat_probe(void)
329 if (apic
== &apic_physflat
|| num_possible_cpus() > 8)
335 static struct apic apic_physflat
= {
337 .name
= "physical flat",
338 .probe
= physflat_probe
,
339 .acpi_madt_oem_check
= physflat_acpi_madt_oem_check
,
340 .apic_id_registered
= flat_apic_id_registered
,
342 .irq_delivery_mode
= dest_Fixed
,
343 .irq_dest_mode
= 0, /* physical */
345 .target_cpus
= physflat_target_cpus
,
348 .check_apicid_used
= NULL
,
349 .check_apicid_present
= NULL
,
351 .vector_allocation_domain
= physflat_vector_allocation_domain
,
352 /* not needed, but shouldn't hurt: */
353 .init_apic_ldr
= flat_init_apic_ldr
,
355 .ioapic_phys_id_map
= NULL
,
356 .setup_apic_routing
= NULL
,
357 .multi_timer_check
= NULL
,
358 .cpu_present_to_apicid
= default_cpu_present_to_apicid
,
359 .apicid_to_cpu_present
= NULL
,
360 .setup_portio_remap
= NULL
,
361 .check_phys_apicid_present
= default_check_phys_apicid_present
,
362 .enable_apic_mode
= NULL
,
363 .phys_pkg_id
= flat_phys_pkg_id
,
364 .mps_oem_check
= NULL
,
366 .get_apic_id
= flat_get_apic_id
,
367 .set_apic_id
= set_apic_id
,
368 .apic_id_mask
= 0xFFu
<< 24,
370 .cpu_mask_to_apicid
= physflat_cpu_mask_to_apicid
,
371 .cpu_mask_to_apicid_and
= physflat_cpu_mask_to_apicid_and
,
373 .send_IPI_mask
= physflat_send_IPI_mask
,
374 .send_IPI_mask_allbutself
= physflat_send_IPI_mask_allbutself
,
375 .send_IPI_allbutself
= physflat_send_IPI_allbutself
,
376 .send_IPI_all
= physflat_send_IPI_all
,
377 .send_IPI_self
= apic_send_IPI_self
,
379 .trampoline_phys_low
= DEFAULT_TRAMPOLINE_PHYS_LOW
,
380 .trampoline_phys_high
= DEFAULT_TRAMPOLINE_PHYS_HIGH
,
381 .wait_for_init_deassert
= NULL
,
382 .smp_callin_clear_local_apic
= NULL
,
383 .inquire_remote_apic
= default_inquire_remote_apic
,
385 .read
= native_apic_mem_read
,
386 .write
= native_apic_mem_write
,
387 .icr_read
= native_apic_icr_read
,
388 .icr_write
= native_apic_icr_write
,
389 .wait_icr_idle
= native_apic_wait_icr_idle
,
390 .safe_wait_icr_idle
= native_safe_apic_wait_icr_idle
,
394 * We need to check for physflat first, so this order is important.
396 apic_drivers(apic_physflat
, apic_flat
);