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>
24 #include <linux/acpi.h>
26 static struct apic apic_physflat;
27 static struct apic apic_flat;
29 struct apic __read_mostly *apic = &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 inline 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();
88 #ifdef CONFIG_HOTPLUG_CPU
93 if (hotplug || vector == NMI_VECTOR) {
94 if (!cpumask_equal(cpu_online_mask, cpumask_of(cpu))) {
95 unsigned long mask = cpumask_bits(cpu_online_mask)[0];
97 if (cpu < BITS_PER_LONG)
98 clear_bit(cpu, &mask);
100 _flat_send_IPI_mask(mask, vector);
102 } else if (num_online_cpus() > 1) {
103 __default_send_IPI_shortcut(APIC_DEST_ALLBUT,
104 vector, apic->dest_logical);
108 static void flat_send_IPI_all(int vector)
110 if (vector == NMI_VECTOR) {
111 flat_send_IPI_mask(cpu_online_mask, vector);
113 __default_send_IPI_shortcut(APIC_DEST_ALLINC,
114 vector, apic->dest_logical);
118 static unsigned int flat_get_apic_id(unsigned long x)
122 id = (((x)>>24) & 0xFFu);
127 static unsigned long set_apic_id(unsigned int id)
131 x = ((id & 0xFFu)<<24);
135 static unsigned int read_xapic_id(void)
139 id = flat_get_apic_id(apic_read(APIC_ID));
143 static int flat_apic_id_registered(void)
145 return physid_isset(read_xapic_id(), phys_cpu_present_map);
148 static int flat_phys_pkg_id(int initial_apic_id, int index_msb)
150 return initial_apic_id >> index_msb;
153 static int flat_probe(void)
158 static struct apic apic_flat = {
161 .acpi_madt_oem_check = flat_acpi_madt_oem_check,
162 .apic_id_valid = default_apic_id_valid,
163 .apic_id_registered = flat_apic_id_registered,
165 .irq_delivery_mode = dest_LowestPrio,
166 .irq_dest_mode = 1, /* logical */
168 .target_cpus = online_target_cpus,
170 .dest_logical = APIC_DEST_LOGICAL,
171 .check_apicid_used = NULL,
172 .check_apicid_present = NULL,
174 .vector_allocation_domain = flat_vector_allocation_domain,
175 .init_apic_ldr = flat_init_apic_ldr,
177 .ioapic_phys_id_map = NULL,
178 .setup_apic_routing = NULL,
179 .multi_timer_check = NULL,
180 .cpu_present_to_apicid = default_cpu_present_to_apicid,
181 .apicid_to_cpu_present = NULL,
182 .setup_portio_remap = NULL,
183 .check_phys_apicid_present = default_check_phys_apicid_present,
184 .enable_apic_mode = NULL,
185 .phys_pkg_id = flat_phys_pkg_id,
186 .mps_oem_check = NULL,
188 .get_apic_id = flat_get_apic_id,
189 .set_apic_id = set_apic_id,
190 .apic_id_mask = 0xFFu << 24,
192 .cpu_mask_to_apicid_and = flat_cpu_mask_to_apicid_and,
194 .send_IPI_mask = flat_send_IPI_mask,
195 .send_IPI_mask_allbutself = flat_send_IPI_mask_allbutself,
196 .send_IPI_allbutself = flat_send_IPI_allbutself,
197 .send_IPI_all = flat_send_IPI_all,
198 .send_IPI_self = apic_send_IPI_self,
200 .trampoline_phys_low = DEFAULT_TRAMPOLINE_PHYS_LOW,
201 .trampoline_phys_high = DEFAULT_TRAMPOLINE_PHYS_HIGH,
202 .wait_for_init_deassert = NULL,
203 .smp_callin_clear_local_apic = NULL,
204 .inquire_remote_apic = default_inquire_remote_apic,
206 .read = native_apic_mem_read,
207 .write = native_apic_mem_write,
208 .eoi_write = native_apic_mem_write,
209 .icr_read = native_apic_icr_read,
210 .icr_write = native_apic_icr_write,
211 .wait_icr_idle = native_apic_wait_icr_idle,
212 .safe_wait_icr_idle = native_safe_apic_wait_icr_idle,
216 * Physflat mode is used when there are more than 8 CPUs on a system.
217 * We cannot use logical delivery in this case because the mask
218 * overflows, so use physical mode.
220 static int physflat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
224 * Quirk: some x86_64 machines can only use physical APIC mode
225 * regardless of how many processors are present (x86_64 ES7000
228 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
229 (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
230 printk(KERN_DEBUG "system APIC only can use physical flat");
234 if (!strncmp(oem_id, "IBM", 3) && !strncmp(oem_table_id, "EXA", 3)) {
235 printk(KERN_DEBUG "IBM Summit detected, will use apic physical");
243 static void physflat_send_IPI_mask(const struct cpumask *cpumask, int vector)
245 default_send_IPI_mask_sequence_phys(cpumask, vector);
248 static void physflat_send_IPI_mask_allbutself(const struct cpumask *cpumask,
251 default_send_IPI_mask_allbutself_phys(cpumask, vector);
254 static void physflat_send_IPI_allbutself(int vector)
256 default_send_IPI_mask_allbutself_phys(cpu_online_mask, vector);
259 static void physflat_send_IPI_all(int vector)
261 physflat_send_IPI_mask(cpu_online_mask, vector);
264 static int physflat_probe(void)
266 if (apic == &apic_physflat || num_possible_cpus() > 8)
272 static struct apic apic_physflat = {
274 .name = "physical flat",
275 .probe = physflat_probe,
276 .acpi_madt_oem_check = physflat_acpi_madt_oem_check,
277 .apic_id_valid = default_apic_id_valid,
278 .apic_id_registered = flat_apic_id_registered,
280 .irq_delivery_mode = dest_Fixed,
281 .irq_dest_mode = 0, /* physical */
283 .target_cpus = online_target_cpus,
286 .check_apicid_used = NULL,
287 .check_apicid_present = NULL,
289 .vector_allocation_domain = default_vector_allocation_domain,
290 /* not needed, but shouldn't hurt: */
291 .init_apic_ldr = flat_init_apic_ldr,
293 .ioapic_phys_id_map = NULL,
294 .setup_apic_routing = NULL,
295 .multi_timer_check = NULL,
296 .cpu_present_to_apicid = default_cpu_present_to_apicid,
297 .apicid_to_cpu_present = NULL,
298 .setup_portio_remap = NULL,
299 .check_phys_apicid_present = default_check_phys_apicid_present,
300 .enable_apic_mode = NULL,
301 .phys_pkg_id = flat_phys_pkg_id,
302 .mps_oem_check = NULL,
304 .get_apic_id = flat_get_apic_id,
305 .set_apic_id = set_apic_id,
306 .apic_id_mask = 0xFFu << 24,
308 .cpu_mask_to_apicid_and = default_cpu_mask_to_apicid_and,
310 .send_IPI_mask = physflat_send_IPI_mask,
311 .send_IPI_mask_allbutself = physflat_send_IPI_mask_allbutself,
312 .send_IPI_allbutself = physflat_send_IPI_allbutself,
313 .send_IPI_all = physflat_send_IPI_all,
314 .send_IPI_self = apic_send_IPI_self,
316 .trampoline_phys_low = DEFAULT_TRAMPOLINE_PHYS_LOW,
317 .trampoline_phys_high = DEFAULT_TRAMPOLINE_PHYS_HIGH,
318 .wait_for_init_deassert = NULL,
319 .smp_callin_clear_local_apic = NULL,
320 .inquire_remote_apic = default_inquire_remote_apic,
322 .read = native_apic_mem_read,
323 .write = native_apic_mem_write,
324 .eoi_write = native_apic_mem_write,
325 .icr_read = native_apic_icr_read,
326 .icr_write = native_apic_icr_write,
327 .wait_icr_idle = native_apic_wait_icr_idle,
328 .safe_wait_icr_idle = native_safe_apic_wait_icr_idle,
332 * We need to check for physflat first, so this order is important.
334 apic_drivers(apic_physflat, apic_flat);