1 /*
2  * Copyright 2004 James Cleverdon, IBM.
3  * Subject to the GNU Public License, v.2
4  *
5  * Flat APIC subarch code.
6  *
7  * Hacked for x86-64 by James Cleverdon from i386 architecture code by
8  * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and
9  * James Cleverdon.
10  */
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>
20 #include <asm/smp.h>
21 #include <asm/apic.h>
22 #include <asm/ipi.h>
23 
24 #ifdef CONFIG_ACPI
25 #include <acpi/acpi_bus.h>
26 #endif
27 
28 static struct apic apic_physflat;
29 static struct apic apic_flat;
30 
31 struct apic __read_mostly *apic = &apic_flat;
32 EXPORT_SYMBOL_GPL(apic);
33 
34 static int flat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
35 {
36 	return 1;
37 }
38 
39 /*
40  * Set up the logical destination ID.
41  *
42  * Intel recommends to set DFR, LDR and TPR before enabling
43  * an APIC.  See e.g. "AP-388 82489DX User's Manual" (Intel
44  * document number 292116).  So here it goes...
45  */
46 void flat_init_apic_ldr(void)
47 {
48 	unsigned long val;
49 	unsigned long num, id;
50 
51 	num = smp_processor_id();
52 	id = 1UL << num;
53 	apic_write(APIC_DFR, APIC_DFR_FLAT);
54 	val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
55 	val |= SET_APIC_LOGICAL_ID(id);
56 	apic_write(APIC_LDR, val);
57 }
58 
59 static inline void _flat_send_IPI_mask(unsigned long mask, int vector)
60 {
61 	unsigned long flags;
62 
63 	local_irq_save(flags);
64 	__default_send_IPI_dest_field(mask, vector, apic->dest_logical);
65 	local_irq_restore(flags);
66 }
67 
68 static void flat_send_IPI_mask(const struct cpumask *cpumask, int vector)
69 {
70 	unsigned long mask = cpumask_bits(cpumask)[0];
71 
72 	_flat_send_IPI_mask(mask, vector);
73 }
74 
75 static void
76 flat_send_IPI_mask_allbutself(const struct cpumask *cpumask, int vector)
77 {
78 	unsigned long mask = cpumask_bits(cpumask)[0];
79 	int cpu = smp_processor_id();
80 
81 	if (cpu < BITS_PER_LONG)
82 		clear_bit(cpu, &mask);
83 
84 	_flat_send_IPI_mask(mask, vector);
85 }
86 
87 static void flat_send_IPI_allbutself(int vector)
88 {
89 	int cpu = smp_processor_id();
90 #ifdef	CONFIG_HOTPLUG_CPU
91 	int hotplug = 1;
92 #else
93 	int hotplug = 0;
94 #endif
95 	if (hotplug || vector == NMI_VECTOR) {
96 		if (!cpumask_equal(cpu_online_mask, cpumask_of(cpu))) {
97 			unsigned long mask = cpumask_bits(cpu_online_mask)[0];
98 
99 			if (cpu < BITS_PER_LONG)
100 				clear_bit(cpu, &mask);
101 
102 			_flat_send_IPI_mask(mask, vector);
103 		}
104 	} else if (num_online_cpus() > 1) {
105 		__default_send_IPI_shortcut(APIC_DEST_ALLBUT,
106 					    vector, apic->dest_logical);
107 	}
108 }
109 
110 static void flat_send_IPI_all(int vector)
111 {
112 	if (vector == NMI_VECTOR) {
113 		flat_send_IPI_mask(cpu_online_mask, vector);
114 	} else {
115 		__default_send_IPI_shortcut(APIC_DEST_ALLINC,
116 					    vector, apic->dest_logical);
117 	}
118 }
119 
120 static unsigned int flat_get_apic_id(unsigned long x)
121 {
122 	unsigned int id;
123 
124 	id = (((x)>>24) & 0xFFu);
125 
126 	return id;
127 }
128 
129 static unsigned long set_apic_id(unsigned int id)
130 {
131 	unsigned long x;
132 
133 	x = ((id & 0xFFu)<<24);
134 	return x;
135 }
136 
137 static unsigned int read_xapic_id(void)
138 {
139 	unsigned int id;
140 
141 	id = flat_get_apic_id(apic_read(APIC_ID));
142 	return id;
143 }
144 
145 static int flat_apic_id_registered(void)
146 {
147 	return physid_isset(read_xapic_id(), phys_cpu_present_map);
148 }
149 
150 static int flat_phys_pkg_id(int initial_apic_id, int index_msb)
151 {
152 	return initial_apic_id >> index_msb;
153 }
154 
155 static int flat_probe(void)
156 {
157 	return 1;
158 }
159 
160 static struct apic apic_flat =  {
161 	.name				= "flat",
162 	.probe				= flat_probe,
163 	.acpi_madt_oem_check		= flat_acpi_madt_oem_check,
164 	.apic_id_valid			= default_apic_id_valid,
165 	.apic_id_registered		= flat_apic_id_registered,
166 
167 	.irq_delivery_mode		= dest_LowestPrio,
168 	.irq_dest_mode			= 1, /* logical */
169 
170 	.target_cpus			= online_target_cpus,
171 	.disable_esr			= 0,
172 	.dest_logical			= APIC_DEST_LOGICAL,
173 	.check_apicid_used		= NULL,
174 	.check_apicid_present		= NULL,
175 
176 	.vector_allocation_domain	= flat_vector_allocation_domain,
177 	.init_apic_ldr			= flat_init_apic_ldr,
178 
179 	.ioapic_phys_id_map		= NULL,
180 	.setup_apic_routing		= NULL,
181 	.multi_timer_check		= NULL,
182 	.cpu_present_to_apicid		= default_cpu_present_to_apicid,
183 	.apicid_to_cpu_present		= NULL,
184 	.setup_portio_remap		= NULL,
185 	.check_phys_apicid_present	= default_check_phys_apicid_present,
186 	.enable_apic_mode		= NULL,
187 	.phys_pkg_id			= flat_phys_pkg_id,
188 	.mps_oem_check			= NULL,
189 
190 	.get_apic_id			= flat_get_apic_id,
191 	.set_apic_id			= set_apic_id,
192 	.apic_id_mask			= 0xFFu << 24,
193 
194 	.cpu_mask_to_apicid_and		= flat_cpu_mask_to_apicid_and,
195 
196 	.send_IPI_mask			= flat_send_IPI_mask,
197 	.send_IPI_mask_allbutself	= flat_send_IPI_mask_allbutself,
198 	.send_IPI_allbutself		= flat_send_IPI_allbutself,
199 	.send_IPI_all			= flat_send_IPI_all,
200 	.send_IPI_self			= apic_send_IPI_self,
201 
202 	.trampoline_phys_low		= DEFAULT_TRAMPOLINE_PHYS_LOW,
203 	.trampoline_phys_high		= DEFAULT_TRAMPOLINE_PHYS_HIGH,
204 	.wait_for_init_deassert		= NULL,
205 	.smp_callin_clear_local_apic	= NULL,
206 	.inquire_remote_apic		= default_inquire_remote_apic,
207 
208 	.read				= native_apic_mem_read,
209 	.write				= native_apic_mem_write,
210 	.eoi_write			= native_apic_mem_write,
211 	.icr_read			= native_apic_icr_read,
212 	.icr_write			= native_apic_icr_write,
213 	.wait_icr_idle			= native_apic_wait_icr_idle,
214 	.safe_wait_icr_idle		= native_safe_apic_wait_icr_idle,
215 };
216 
217 /*
218  * Physflat mode is used when there are more than 8 CPUs on a system.
219  * We cannot use logical delivery in this case because the mask
220  * overflows, so use physical mode.
221  */
222 static int physflat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
223 {
224 #ifdef CONFIG_ACPI
225 	/*
226 	 * Quirk: some x86_64 machines can only use physical APIC mode
227 	 * regardless of how many processors are present (x86_64 ES7000
228 	 * is an example).
229 	 */
230 	if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
231 		(acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
232 		printk(KERN_DEBUG "system APIC only can use physical flat");
233 		return 1;
234 	}
235 
236 	if (!strncmp(oem_id, "IBM", 3) && !strncmp(oem_table_id, "EXA", 3)) {
237 		printk(KERN_DEBUG "IBM Summit detected, will use apic physical");
238 		return 1;
239 	}
240 #endif
241 
242 	return 0;
243 }
244 
245 static void physflat_send_IPI_mask(const struct cpumask *cpumask, int vector)
246 {
247 	default_send_IPI_mask_sequence_phys(cpumask, vector);
248 }
249 
250 static void physflat_send_IPI_mask_allbutself(const struct cpumask *cpumask,
251 					      int vector)
252 {
253 	default_send_IPI_mask_allbutself_phys(cpumask, vector);
254 }
255 
256 static void physflat_send_IPI_allbutself(int vector)
257 {
258 	default_send_IPI_mask_allbutself_phys(cpu_online_mask, vector);
259 }
260 
261 static void physflat_send_IPI_all(int vector)
262 {
263 	physflat_send_IPI_mask(cpu_online_mask, vector);
264 }
265 
266 static int physflat_probe(void)
267 {
268 	if (apic == &apic_physflat || num_possible_cpus() > 8)
269 		return 1;
270 
271 	return 0;
272 }
273 
274 static struct apic apic_physflat =  {
275 
276 	.name				= "physical flat",
277 	.probe				= physflat_probe,
278 	.acpi_madt_oem_check		= physflat_acpi_madt_oem_check,
279 	.apic_id_valid			= default_apic_id_valid,
280 	.apic_id_registered		= flat_apic_id_registered,
281 
282 	.irq_delivery_mode		= dest_Fixed,
283 	.irq_dest_mode			= 0, /* physical */
284 
285 	.target_cpus			= online_target_cpus,
286 	.disable_esr			= 0,
287 	.dest_logical			= 0,
288 	.check_apicid_used		= NULL,
289 	.check_apicid_present		= NULL,
290 
291 	.vector_allocation_domain	= default_vector_allocation_domain,
292 	/* not needed, but shouldn't hurt: */
293 	.init_apic_ldr			= flat_init_apic_ldr,
294 
295 	.ioapic_phys_id_map		= NULL,
296 	.setup_apic_routing		= NULL,
297 	.multi_timer_check		= NULL,
298 	.cpu_present_to_apicid		= default_cpu_present_to_apicid,
299 	.apicid_to_cpu_present		= NULL,
300 	.setup_portio_remap		= NULL,
301 	.check_phys_apicid_present	= default_check_phys_apicid_present,
302 	.enable_apic_mode		= NULL,
303 	.phys_pkg_id			= flat_phys_pkg_id,
304 	.mps_oem_check			= NULL,
305 
306 	.get_apic_id			= flat_get_apic_id,
307 	.set_apic_id			= set_apic_id,
308 	.apic_id_mask			= 0xFFu << 24,
309 
310 	.cpu_mask_to_apicid_and		= default_cpu_mask_to_apicid_and,
311 
312 	.send_IPI_mask			= physflat_send_IPI_mask,
313 	.send_IPI_mask_allbutself	= physflat_send_IPI_mask_allbutself,
314 	.send_IPI_allbutself		= physflat_send_IPI_allbutself,
315 	.send_IPI_all			= physflat_send_IPI_all,
316 	.send_IPI_self			= apic_send_IPI_self,
317 
318 	.trampoline_phys_low		= DEFAULT_TRAMPOLINE_PHYS_LOW,
319 	.trampoline_phys_high		= DEFAULT_TRAMPOLINE_PHYS_HIGH,
320 	.wait_for_init_deassert		= NULL,
321 	.smp_callin_clear_local_apic	= NULL,
322 	.inquire_remote_apic		= default_inquire_remote_apic,
323 
324 	.read				= native_apic_mem_read,
325 	.write				= native_apic_mem_write,
326 	.eoi_write			= native_apic_mem_write,
327 	.icr_read			= native_apic_icr_read,
328 	.icr_write			= native_apic_icr_write,
329 	.wait_icr_idle			= native_apic_wait_icr_idle,
330 	.safe_wait_icr_idle		= native_safe_apic_wait_icr_idle,
331 };
332 
333 /*
334  * We need to check for physflat first, so this order is important.
335  */
336 apic_drivers(apic_physflat, apic_flat);
337