blob: 306e5e88fb6f4abc9a53a3ce8638ac904a67067e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright 2004 James Cleverdon, IBM.
3 * Subject to the GNU Public License, v.2
4 *
Andi Kleenf8d31192005-07-28 21:15:42 -07005 * Flat APIC subarch code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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 */
Andi Kleenf19cccf2007-05-02 19:27:21 +020011#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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>
Suresh Siddha0c81c742008-07-10 11:16:48 -070018#include <linux/hardirq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/smp.h>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010020#include <asm/apic.h>
Yinghai Luc1eeb2d2009-02-16 23:02:14 -080021#include <asm/ipi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Yinghai Lu1b9b89e2008-07-21 22:08:21 -070023#ifdef CONFIG_ACPI
24#include <acpi/acpi_bus.h>
25#endif
26
Marcin Slusarz983f91f2008-10-12 11:44:08 +020027static int flat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
Yinghai Lu1b9b89e2008-07-21 22:08:21 -070028{
29 return 1;
30}
31
Mike Travisbcda0162008-12-16 17:33:59 -080032static const struct cpumask *flat_target_cpus(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Mike Travisbcda0162008-12-16 17:33:59 -080034 return cpu_online_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
Mike Travisbcda0162008-12-16 17:33:59 -080037static void flat_vector_allocation_domain(int cpu, struct cpumask *retmask)
Eric W. Biedermanc7111c132006-10-08 07:47:55 -060038{
39 /* Careful. Some cpus do not strictly honor the set of cpus
40 * specified in the interrupt destination when using lowest
41 * priority interrupt delivery mode.
42 *
43 * In particular there was a hyperthreading cpu observed to
44 * deliver interrupts to the wrong hyperthread when only one
45 * hyperthread was specified in the interrupt desitination.
46 */
Mike Travisbcda0162008-12-16 17:33:59 -080047 cpumask_clear(retmask);
48 cpumask_bits(retmask)[0] = APIC_ALL_CPUS;
Eric W. Biedermanc7111c132006-10-08 07:47:55 -060049}
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
52 * Set up the logical destination ID.
53 *
54 * Intel recommends to set DFR, LDR and TPR before enabling
55 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
56 * document number 292116). So here it goes...
57 */
58static void flat_init_apic_ldr(void)
59{
60 unsigned long val;
61 unsigned long num, id;
62
63 num = smp_processor_id();
64 id = 1UL << num;
Andi Kleeneddfb4e2005-09-12 18:49:23 +020065 apic_write(APIC_DFR, APIC_DFR_FLAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
67 val |= SET_APIC_LOGICAL_ID(id);
Andi Kleeneddfb4e2005-09-12 18:49:23 +020068 apic_write(APIC_LDR, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
Mike Travise7986732008-12-16 17:33:52 -080071static inline void _flat_send_IPI_mask(unsigned long mask, int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 unsigned long flags;
74
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +020075 local_irq_save(flags);
Ingo Molnardac5f412009-01-28 15:42:24 +010076 __default_send_IPI_dest_field(mask, vector, apic->dest_logical);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 local_irq_restore(flags);
78}
79
Mike Travisbcda0162008-12-16 17:33:59 -080080static void flat_send_IPI_mask(const struct cpumask *cpumask, int vector)
Mike Travise7986732008-12-16 17:33:52 -080081{
Mike Travisbcda0162008-12-16 17:33:59 -080082 unsigned long mask = cpumask_bits(cpumask)[0];
Mike Travise7986732008-12-16 17:33:52 -080083
84 _flat_send_IPI_mask(mask, vector);
85}
86
Ingo Molnardac5f412009-01-28 15:42:24 +010087static void
88 flat_send_IPI_mask_allbutself(const struct cpumask *cpumask, int vector)
Mike Travise7986732008-12-16 17:33:52 -080089{
Mike Travisbcda0162008-12-16 17:33:59 -080090 unsigned long mask = cpumask_bits(cpumask)[0];
Mike Travise7986732008-12-16 17:33:52 -080091 int cpu = smp_processor_id();
92
93 if (cpu < BITS_PER_LONG)
94 clear_bit(cpu, &mask);
Ingo Molnardac5f412009-01-28 15:42:24 +010095
Mike Travise7986732008-12-16 17:33:52 -080096 _flat_send_IPI_mask(mask, vector);
97}
98
Ashok Raj884d9e42005-06-25 14:55:02 -070099static void flat_send_IPI_allbutself(int vector)
100{
Mike Travise7986732008-12-16 17:33:52 -0800101 int cpu = smp_processor_id();
Keith Owense77deac2006-06-26 13:59:56 +0200102#ifdef CONFIG_HOTPLUG_CPU
103 int hotplug = 1;
Ashok Rajfdf26d92005-09-09 13:01:52 -0700104#else
Keith Owense77deac2006-06-26 13:59:56 +0200105 int hotplug = 0;
Ashok Rajfdf26d92005-09-09 13:01:52 -0700106#endif
Keith Owense77deac2006-06-26 13:59:56 +0200107 if (hotplug || vector == NMI_VECTOR) {
Mike Travisbcda0162008-12-16 17:33:59 -0800108 if (!cpumask_equal(cpu_online_mask, cpumask_of(cpu))) {
109 unsigned long mask = cpumask_bits(cpu_online_mask)[0];
Keith Owense77deac2006-06-26 13:59:56 +0200110
Mike Travise7986732008-12-16 17:33:52 -0800111 if (cpu < BITS_PER_LONG)
112 clear_bit(cpu, &mask);
Keith Owense77deac2006-06-26 13:59:56 +0200113
Mike Travise7986732008-12-16 17:33:52 -0800114 _flat_send_IPI_mask(mask, vector);
115 }
Keith Owense77deac2006-06-26 13:59:56 +0200116 } else if (num_online_cpus() > 1) {
Ingo Molnardac5f412009-01-28 15:42:24 +0100117 __default_send_IPI_shortcut(APIC_DEST_ALLBUT,
118 vector, apic->dest_logical);
Keith Owense77deac2006-06-26 13:59:56 +0200119 }
Ashok Raj884d9e42005-06-25 14:55:02 -0700120}
121
122static void flat_send_IPI_all(int vector)
123{
Ingo Molnardac5f412009-01-28 15:42:24 +0100124 if (vector == NMI_VECTOR) {
Mike Travisbcda0162008-12-16 17:33:59 -0800125 flat_send_IPI_mask(cpu_online_mask, vector);
Ingo Molnardac5f412009-01-28 15:42:24 +0100126 } else {
127 __default_send_IPI_shortcut(APIC_DEST_ALLINC,
128 vector, apic->dest_logical);
129 }
Ashok Raj884d9e42005-06-25 14:55:02 -0700130}
131
Ingo Molnarca6c8ed2009-01-28 14:08:38 +0100132static unsigned int flat_get_apic_id(unsigned long x)
Yinghai Luf910a9d2008-07-12 01:01:20 -0700133{
134 unsigned int id;
135
136 id = (((x)>>24) & 0xFFu);
Ingo Molnarca6c8ed2009-01-28 14:08:38 +0100137
Yinghai Luf910a9d2008-07-12 01:01:20 -0700138 return id;
139}
140
141static unsigned long set_apic_id(unsigned int id)
142{
143 unsigned long x;
144
145 x = ((id & 0xFFu)<<24);
146 return x;
147}
148
Suresh Siddha0c81c742008-07-10 11:16:48 -0700149static unsigned int read_xapic_id(void)
150{
151 unsigned int id;
152
Ingo Molnarca6c8ed2009-01-28 14:08:38 +0100153 id = flat_get_apic_id(apic_read(APIC_ID));
Suresh Siddha0c81c742008-07-10 11:16:48 -0700154 return id;
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static int flat_apic_id_registered(void)
158{
Suresh Siddha0c81c742008-07-10 11:16:48 -0700159 return physid_isset(read_xapic_id(), phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Ingo Molnard4c9a9f2009-01-28 13:31:22 +0100162static int flat_phys_pkg_id(int initial_apic_id, int index_msb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
ravikiran thirumalai0f4fdb72006-06-26 13:56:04 +0200164 return hard_smp_processor_id() >> index_msb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Ingo Molnarbe163a12009-02-17 16:28:46 +0100167struct apic apic_flat = {
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100168 .name = "flat",
169 .probe = NULL,
170 .acpi_madt_oem_check = flat_acpi_madt_oem_check,
171 .apic_id_registered = flat_apic_id_registered,
172
Ingo Molnarf8987a12009-01-28 04:02:31 +0100173 .irq_delivery_mode = dest_LowestPrio,
Ingo Molnar0b06e732009-01-28 05:13:04 +0100174 .irq_dest_mode = 1, /* logical */
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100175
176 .target_cpus = flat_target_cpus,
Ingo Molnar08125d32009-01-28 05:08:44 +0100177 .disable_esr = 0,
Ingo Molnarbdb1a9b2009-01-28 05:29:25 +0100178 .dest_logical = APIC_DEST_LOGICAL,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100179 .check_apicid_used = NULL,
180 .check_apicid_present = NULL,
181
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100182 .vector_allocation_domain = flat_vector_allocation_domain,
183 .init_apic_ldr = flat_init_apic_ldr,
184
185 .ioapic_phys_id_map = NULL,
186 .setup_apic_routing = NULL,
187 .multi_timer_check = NULL,
188 .apicid_to_node = NULL,
189 .cpu_to_logical_apicid = NULL,
Ingo Molnara21769a42009-01-28 06:50:47 +0100190 .cpu_present_to_apicid = default_cpu_present_to_apicid,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100191 .apicid_to_cpu_present = NULL,
192 .setup_portio_remap = NULL,
Ingo Molnara27a6212009-01-28 12:43:18 +0100193 .check_phys_apicid_present = default_check_phys_apicid_present,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100194 .enable_apic_mode = NULL,
Ingo Molnard4c9a9f2009-01-28 13:31:22 +0100195 .phys_pkg_id = flat_phys_pkg_id,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100196 .mps_oem_check = NULL,
197
Ingo Molnarca6c8ed2009-01-28 14:08:38 +0100198 .get_apic_id = flat_get_apic_id,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100199 .set_apic_id = set_apic_id,
200 .apic_id_mask = 0xFFu << 24,
201
Yinghai Luf56e5032009-03-24 14:16:30 -0700202 .cpu_mask_to_apicid = default_cpu_mask_to_apicid,
203 .cpu_mask_to_apicid_and = default_cpu_mask_to_apicid_and,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100204
205 .send_IPI_mask = flat_send_IPI_mask,
206 .send_IPI_mask_allbutself = flat_send_IPI_mask_allbutself,
207 .send_IPI_allbutself = flat_send_IPI_allbutself,
208 .send_IPI_all = flat_send_IPI_all,
209 .send_IPI_self = apic_send_IPI_self,
210
Ingo Molnarabfa5842009-01-28 16:15:16 +0100211 .trampoline_phys_low = DEFAULT_TRAMPOLINE_PHYS_LOW,
212 .trampoline_phys_high = DEFAULT_TRAMPOLINE_PHYS_HIGH,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100213 .wait_for_init_deassert = NULL,
214 .smp_callin_clear_local_apic = NULL,
Yinghai Lu08d63b12009-04-08 08:00:01 -0700215 .inquire_remote_apic = default_inquire_remote_apic,
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800216
217 .read = native_apic_mem_read,
218 .write = native_apic_mem_write,
219 .icr_read = native_apic_icr_read,
220 .icr_write = native_apic_icr_write,
221 .wait_icr_idle = native_apic_wait_icr_idle,
222 .safe_wait_icr_idle = native_safe_apic_wait_icr_idle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223};
Andi Kleenf8d31192005-07-28 21:15:42 -0700224
225/*
226 * Physflat mode is used when there are more than 8 CPUs on a AMD system.
227 * We cannot use logical delivery in this case because the mask
228 * overflows, so use physical mode.
229 */
Marcin Slusarzfae172162008-10-12 11:44:09 +0200230static int physflat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700231{
232#ifdef CONFIG_ACPI
233 /*
234 * Quirk: some x86_64 machines can only use physical APIC mode
235 * regardless of how many processors are present (x86_64 ES7000
236 * is an example).
237 */
238 if (acpi_gbl_FADT.header.revision > FADT2_REVISION_ID &&
Yinghai Lu02c1df12008-09-04 20:57:11 +0200239 (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
240 printk(KERN_DEBUG "system APIC only can use physical flat");
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700241 return 1;
Yinghai Lu02c1df12008-09-04 20:57:11 +0200242 }
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700243#endif
244
245 return 0;
246}
Andi Kleenf8d31192005-07-28 21:15:42 -0700247
Mike Travisbcda0162008-12-16 17:33:59 -0800248static const struct cpumask *physflat_target_cpus(void)
Andi Kleenf8d31192005-07-28 21:15:42 -0700249{
Mike Travisbcda0162008-12-16 17:33:59 -0800250 return cpu_online_mask;
Andi Kleenf8d31192005-07-28 21:15:42 -0700251}
252
Mike Travisbcda0162008-12-16 17:33:59 -0800253static void physflat_vector_allocation_domain(int cpu, struct cpumask *retmask)
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600254{
Mike Travisbcda0162008-12-16 17:33:59 -0800255 cpumask_clear(retmask);
256 cpumask_set_cpu(cpu, retmask);
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600257}
258
Mike Travisbcda0162008-12-16 17:33:59 -0800259static void physflat_send_IPI_mask(const struct cpumask *cpumask, int vector)
Andi Kleenf8d31192005-07-28 21:15:42 -0700260{
Yinghai Lu43f39892009-01-29 19:31:49 -0800261 default_send_IPI_mask_sequence_phys(cpumask, vector);
Andi Kleenf8d31192005-07-28 21:15:42 -0700262}
263
Mike Travisbcda0162008-12-16 17:33:59 -0800264static void physflat_send_IPI_mask_allbutself(const struct cpumask *cpumask,
Mike Travise7986732008-12-16 17:33:52 -0800265 int vector)
266{
Yinghai Lu43f39892009-01-29 19:31:49 -0800267 default_send_IPI_mask_allbutself_phys(cpumask, vector);
Mike Travise7986732008-12-16 17:33:52 -0800268}
269
Andi Kleenf8d31192005-07-28 21:15:42 -0700270static void physflat_send_IPI_allbutself(int vector)
271{
Yinghai Lu43f39892009-01-29 19:31:49 -0800272 default_send_IPI_mask_allbutself_phys(cpu_online_mask, vector);
Andi Kleenf8d31192005-07-28 21:15:42 -0700273}
274
275static void physflat_send_IPI_all(int vector)
276{
Mike Travisbcda0162008-12-16 17:33:59 -0800277 physflat_send_IPI_mask(cpu_online_mask, vector);
Andi Kleenf8d31192005-07-28 21:15:42 -0700278}
279
Mike Travisbcda0162008-12-16 17:33:59 -0800280static unsigned int physflat_cpu_mask_to_apicid(const struct cpumask *cpumask)
Andi Kleenf8d31192005-07-28 21:15:42 -0700281{
282 int cpu;
283
284 /*
285 * We're using fixed IRQ delivery, can only return one phys APIC ID.
286 * May as well be the first.
287 */
Mike Travisbcda0162008-12-16 17:33:59 -0800288 cpu = cpumask_first(cpumask);
Mike Travis1bd9d6b2008-07-18 18:11:30 -0700289 if ((unsigned)cpu < nr_cpu_ids)
Mike Travis71fff5e2007-10-19 20:35:03 +0200290 return per_cpu(x86_cpu_to_apicid, cpu);
Andi Kleenf8d31192005-07-28 21:15:42 -0700291 else
292 return BAD_APICID;
293}
294
Mike Travis6eeb7c52008-12-16 17:33:55 -0800295static unsigned int
296physflat_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
297 const struct cpumask *andmask)
Mike Travis95d313c2008-12-16 17:33:54 -0800298{
299 int cpu;
300
301 /*
302 * We're using fixed IRQ delivery, can only return one phys APIC ID.
303 * May as well be the first.
304 */
Ingo Molnardebccb32009-01-28 15:20:18 +0100305 for_each_cpu_and(cpu, cpumask, andmask) {
Mike Travisa775a382008-12-17 15:21:39 -0800306 if (cpumask_test_cpu(cpu, cpu_online_mask))
307 break;
Ingo Molnardebccb32009-01-28 15:20:18 +0100308 }
Mike Travis6eeb7c52008-12-16 17:33:55 -0800309 if (cpu < nr_cpu_ids)
310 return per_cpu(x86_cpu_to_apicid, cpu);
Ingo Molnardebccb32009-01-28 15:20:18 +0100311
Mike Travis95d313c2008-12-16 17:33:54 -0800312 return BAD_APICID;
313}
314
Ingo Molnarbe163a12009-02-17 16:28:46 +0100315struct apic apic_physflat = {
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100316
317 .name = "physical flat",
318 .probe = NULL,
319 .acpi_madt_oem_check = physflat_acpi_madt_oem_check,
320 .apic_id_registered = flat_apic_id_registered,
321
Ingo Molnarf8987a12009-01-28 04:02:31 +0100322 .irq_delivery_mode = dest_Fixed,
Ingo Molnar0b06e732009-01-28 05:13:04 +0100323 .irq_dest_mode = 0, /* physical */
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100324
325 .target_cpus = physflat_target_cpus,
Ingo Molnar08125d32009-01-28 05:08:44 +0100326 .disable_esr = 0,
Ingo Molnarbdb1a9b2009-01-28 05:29:25 +0100327 .dest_logical = 0,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100328 .check_apicid_used = NULL,
329 .check_apicid_present = NULL,
330
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100331 .vector_allocation_domain = physflat_vector_allocation_domain,
332 /* not needed, but shouldn't hurt: */
333 .init_apic_ldr = flat_init_apic_ldr,
334
335 .ioapic_phys_id_map = NULL,
336 .setup_apic_routing = NULL,
337 .multi_timer_check = NULL,
338 .apicid_to_node = NULL,
339 .cpu_to_logical_apicid = NULL,
Ingo Molnara21769a42009-01-28 06:50:47 +0100340 .cpu_present_to_apicid = default_cpu_present_to_apicid,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100341 .apicid_to_cpu_present = NULL,
342 .setup_portio_remap = NULL,
Ingo Molnara27a6212009-01-28 12:43:18 +0100343 .check_phys_apicid_present = default_check_phys_apicid_present,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100344 .enable_apic_mode = NULL,
Ingo Molnard4c9a9f2009-01-28 13:31:22 +0100345 .phys_pkg_id = flat_phys_pkg_id,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100346 .mps_oem_check = NULL,
347
Ingo Molnarca6c8ed2009-01-28 14:08:38 +0100348 .get_apic_id = flat_get_apic_id,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100349 .set_apic_id = set_apic_id,
Ingo Molnar5b812722009-01-28 14:59:17 +0100350 .apic_id_mask = 0xFFu << 24,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100351
352 .cpu_mask_to_apicid = physflat_cpu_mask_to_apicid,
353 .cpu_mask_to_apicid_and = physflat_cpu_mask_to_apicid_and,
354
355 .send_IPI_mask = physflat_send_IPI_mask,
356 .send_IPI_mask_allbutself = physflat_send_IPI_mask_allbutself,
357 .send_IPI_allbutself = physflat_send_IPI_allbutself,
358 .send_IPI_all = physflat_send_IPI_all,
359 .send_IPI_self = apic_send_IPI_self,
360
Ingo Molnarabfa5842009-01-28 16:15:16 +0100361 .trampoline_phys_low = DEFAULT_TRAMPOLINE_PHYS_LOW,
362 .trampoline_phys_high = DEFAULT_TRAMPOLINE_PHYS_HIGH,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100363 .wait_for_init_deassert = NULL,
364 .smp_callin_clear_local_apic = NULL,
Yinghai Lu08d63b12009-04-08 08:00:01 -0700365 .inquire_remote_apic = default_inquire_remote_apic,
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800366
367 .read = native_apic_mem_read,
368 .write = native_apic_mem_write,
369 .icr_read = native_apic_icr_read,
370 .icr_write = native_apic_icr_write,
371 .wait_icr_idle = native_apic_wait_icr_idle,
372 .safe_wait_icr_idle = native_safe_apic_wait_icr_idle,
Andi Kleenf8d31192005-07-28 21:15:42 -0700373};