2 * Copyright 2004 James Cleverdon, IBM.
3 * Subject to the GNU Public License, v.2
5 * Flat APIC subarch code. Maximum 8 CPUs, logical delivery.
7 * Hacked for x86-64 by James Cleverdon from i386 architecture code by
8 * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and
10 * Ashok Raj <ashok.raj@intel.com>
11 * Removed IPI broadcast shortcut to support CPU hotplug
13 #include <linux/config.h>
14 #include <linux/threads.h>
15 #include <linux/cpumask.h>
16 #include <linux/string.h>
17 #include <linux/kernel.h>
18 #include <linux/ctype.h>
19 #include <linux/init.h>
24 * The following permit choosing broadcast IPI shortcut v.s sending IPI only
25 * to online cpus via the send_IPI_mask varient.
26 * The mask version is my preferred option, since it eliminates a lot of
27 * other extra code that would need to be written to cleanup intrs sent
28 * to a CPU while offline.
30 * Sending broadcast introduces lots of trouble in CPU hotplug situations.
31 * These IPI's are delivered to cpu's irrespective of their offline status
32 * and could pickup stale intr data when these CPUS are turned online.
34 * Not using broadcast is a cleaner approach IMO, but Andi Kleen disagrees with
35 * the idea of not using broadcast IPI's anymore. Hence the run time check
36 * is introduced, on his request so we can choose an alternate mechanism.
38 * Initial wacky performance tests that collect cycle counts show
39 * no increase in using mask v.s broadcast version. In fact they seem
40 * identical in terms of cycle counts.
42 * if we need to use broadcast, we need to do the following.
46 * clear any pending IPI, just ack and clear all pending intr
51 * The complicated dummy irq processing shown above is not required if
52 * we didnt sent IPI's to wrong CPU's in the first place.
54 * - Ashok Raj <ashok.raj@intel.com>
56 #ifdef CONFIG_HOTPLUG_CPU
57 #define DEFAULT_SEND_IPI (1)
59 #define DEFAULT_SEND_IPI (0)
62 static int no_broadcast=DEFAULT_SEND_IPI;
64 static cpumask_t flat_target_cpus(void)
66 return cpu_online_map;
70 * Set up the logical destination ID.
72 * Intel recommends to set DFR, LDR and TPR before enabling
73 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
74 * document number 292116). So here it goes...
76 static void flat_init_apic_ldr(void)
79 unsigned long num, id;
81 num = smp_processor_id();
83 x86_cpu_to_log_apicid[num] = id;
84 apic_write_around(APIC_DFR, APIC_DFR_FLAT);
85 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
86 val |= SET_APIC_LOGICAL_ID(id);
87 apic_write_around(APIC_LDR, val);
90 static void flat_send_IPI_mask(cpumask_t cpumask, int vector)
92 unsigned long mask = cpus_addr(cpumask)[0];
96 local_save_flags(flags);
102 apic_wait_icr_idle();
105 * prepare target chip field
107 cfg = __prepare_ICR2(mask);
108 apic_write_around(APIC_ICR2, cfg);
113 cfg = __prepare_ICR(0, vector, APIC_DEST_LOGICAL);
116 * Send the IPI. The write to APIC_ICR fires this off.
118 apic_write_around(APIC_ICR, cfg);
119 local_irq_restore(flags);
122 static inline void __local_flat_send_IPI_allbutself(int vector)
125 cpumask_t mask = cpu_online_map;
126 int this_cpu = get_cpu();
128 cpu_clear(this_cpu, mask);
129 flat_send_IPI_mask(mask, vector);
133 __send_IPI_shortcut(APIC_DEST_ALLBUT, vector, APIC_DEST_LOGICAL);
136 static inline void __local_flat_send_IPI_all(int vector)
139 flat_send_IPI_mask(cpu_online_map, vector);
141 __send_IPI_shortcut(APIC_DEST_ALLINC, vector, APIC_DEST_LOGICAL);
144 static void flat_send_IPI_allbutself(int vector)
146 if (((num_online_cpus()) - 1) >= 1)
147 __local_flat_send_IPI_allbutself(vector);
150 static void flat_send_IPI_all(int vector)
152 __local_flat_send_IPI_all(vector);
155 static int flat_apic_id_registered(void)
157 return physid_isset(GET_APIC_ID(apic_read(APIC_ID)), phys_cpu_present_map);
160 static unsigned int flat_cpu_mask_to_apicid(cpumask_t cpumask)
162 return cpus_addr(cpumask)[0] & APIC_ALL_CPUS;
165 static unsigned int phys_pkg_id(int index_msb)
170 return ((ebx >> 24) & 0xFF) >> index_msb;
173 static __init int no_ipi_broadcast(char *str)
175 get_option(&str, &no_broadcast);
176 printk ("Using %s mode\n", no_broadcast ? "No IPI Broadcast" :
181 __setup("no_ipi_broadcast", no_ipi_broadcast);
183 struct genapic apic_flat = {
185 .int_delivery_mode = dest_LowestPrio,
186 .int_dest_mode = (APIC_DEST_LOGICAL != 0),
187 .int_delivery_dest = APIC_DEST_LOGICAL | APIC_DM_LOWEST,
188 .target_cpus = flat_target_cpus,
189 .apic_id_registered = flat_apic_id_registered,
190 .init_apic_ldr = flat_init_apic_ldr,
191 .send_IPI_all = flat_send_IPI_all,
192 .send_IPI_allbutself = flat_send_IPI_allbutself,
193 .send_IPI_mask = flat_send_IPI_mask,
194 .cpu_mask_to_apicid = flat_cpu_mask_to_apicid,
195 .phys_pkg_id = phys_pkg_id,
198 static int __init print_ipi_mode(void)
200 printk ("Using IPI %s mode\n", no_broadcast ? "No-Shortcut" :
205 late_initcall(print_ipi_mode);