blob: c772bb10b1736a8f0fc6ff7586a8d08127c24634 [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>
20#include <asm/ipi.h>
Jan Beulich00f1ea62007-05-02 19:27:04 +020021#include <asm/genapic.h>
Suresh Siddha0c81c742008-07-10 11:16:48 -070022#include <mach_apicdef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Yinghai Lu1b9b89e2008-07-21 22:08:21 -070024#ifdef CONFIG_ACPI
25#include <acpi/acpi_bus.h>
26#endif
27
Marcin Slusarz983f91f2008-10-12 11:44:08 +020028static int flat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
Yinghai Lu1b9b89e2008-07-21 22:08:21 -070029{
30 return 1;
31}
32
Mike Travise7986732008-12-16 17:33:52 -080033static const cpumask_t *flat_target_cpus(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Mike Travise7986732008-12-16 17:33:52 -080035 return &cpu_online_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
Mike Travise7986732008-12-16 17:33:52 -080038static void flat_vector_allocation_domain(int cpu, cpumask_t *retmask)
Eric W. Biedermanc7111c132006-10-08 07:47:55 -060039{
40 /* Careful. Some cpus do not strictly honor the set of cpus
41 * specified in the interrupt destination when using lowest
42 * priority interrupt delivery mode.
43 *
44 * In particular there was a hyperthreading cpu observed to
45 * deliver interrupts to the wrong hyperthread when only one
46 * hyperthread was specified in the interrupt desitination.
47 */
Mike Travise7986732008-12-16 17:33:52 -080048 *retmask = (cpumask_t) { {[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);
Fernando Luis [** ISO-8859-1 charset **] VázquezCao9062d882007-05-02 19:27:18 +020076 __send_IPI_dest_field(mask, vector, APIC_DEST_LOGICAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 local_irq_restore(flags);
78}
79
Mike Travise7986732008-12-16 17:33:52 -080080static void flat_send_IPI_mask(const cpumask_t *cpumask, int vector)
81{
82 unsigned long mask = cpus_addr(*cpumask)[0];
83
84 _flat_send_IPI_mask(mask, vector);
85}
86
87static void flat_send_IPI_mask_allbutself(const cpumask_t *cpumask, int vector)
88{
89 unsigned long mask = cpus_addr(*cpumask)[0];
90 int cpu = smp_processor_id();
91
92 if (cpu < BITS_PER_LONG)
93 clear_bit(cpu, &mask);
94 _flat_send_IPI_mask(mask, vector);
95}
96
Ashok Raj884d9e42005-06-25 14:55:02 -070097static void flat_send_IPI_allbutself(int vector)
98{
Mike Travise7986732008-12-16 17:33:52 -080099 int cpu = smp_processor_id();
Keith Owense77deac2006-06-26 13:59:56 +0200100#ifdef CONFIG_HOTPLUG_CPU
101 int hotplug = 1;
Ashok Rajfdf26d92005-09-09 13:01:52 -0700102#else
Keith Owense77deac2006-06-26 13:59:56 +0200103 int hotplug = 0;
Ashok Rajfdf26d92005-09-09 13:01:52 -0700104#endif
Keith Owense77deac2006-06-26 13:59:56 +0200105 if (hotplug || vector == NMI_VECTOR) {
Mike Travise7986732008-12-16 17:33:52 -0800106 if (!cpus_equal(cpu_online_map, cpumask_of_cpu(cpu))) {
107 unsigned long mask = cpus_addr(cpu_online_map)[0];
Keith Owense77deac2006-06-26 13:59:56 +0200108
Mike Travise7986732008-12-16 17:33:52 -0800109 if (cpu < BITS_PER_LONG)
110 clear_bit(cpu, &mask);
Keith Owense77deac2006-06-26 13:59:56 +0200111
Mike Travise7986732008-12-16 17:33:52 -0800112 _flat_send_IPI_mask(mask, vector);
113 }
Keith Owense77deac2006-06-26 13:59:56 +0200114 } else if (num_online_cpus() > 1) {
115 __send_IPI_shortcut(APIC_DEST_ALLBUT, vector,APIC_DEST_LOGICAL);
116 }
Ashok Raj884d9e42005-06-25 14:55:02 -0700117}
118
119static void flat_send_IPI_all(int vector)
120{
Keith Owense77deac2006-06-26 13:59:56 +0200121 if (vector == NMI_VECTOR)
Mike Travise7986732008-12-16 17:33:52 -0800122 flat_send_IPI_mask(&cpu_online_map, vector);
Keith Owense77deac2006-06-26 13:59:56 +0200123 else
124 __send_IPI_shortcut(APIC_DEST_ALLINC, vector, APIC_DEST_LOGICAL);
Ashok Raj884d9e42005-06-25 14:55:02 -0700125}
126
Yinghai Luf910a9d2008-07-12 01:01:20 -0700127static unsigned int get_apic_id(unsigned long x)
128{
129 unsigned int id;
130
131 id = (((x)>>24) & 0xFFu);
132 return id;
133}
134
135static unsigned long set_apic_id(unsigned int id)
136{
137 unsigned long x;
138
139 x = ((id & 0xFFu)<<24);
140 return x;
141}
142
Suresh Siddha0c81c742008-07-10 11:16:48 -0700143static unsigned int read_xapic_id(void)
144{
145 unsigned int id;
146
Yinghai Luf910a9d2008-07-12 01:01:20 -0700147 id = get_apic_id(apic_read(APIC_ID));
Suresh Siddha0c81c742008-07-10 11:16:48 -0700148 return id;
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static int flat_apic_id_registered(void)
152{
Suresh Siddha0c81c742008-07-10 11:16:48 -0700153 return physid_isset(read_xapic_id(), phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Mike Travise7986732008-12-16 17:33:52 -0800156static unsigned int flat_cpu_mask_to_apicid(const cpumask_t *cpumask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Mike Travise7986732008-12-16 17:33:52 -0800158 return cpus_addr(*cpumask)[0] & APIC_ALL_CPUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Mike Travis6eeb7c52008-12-16 17:33:55 -0800161static unsigned int flat_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
162 const struct cpumask *andmask)
Mike Travis95d313c2008-12-16 17:33:54 -0800163{
Mike Travis6eeb7c52008-12-16 17:33:55 -0800164 unsigned long mask1 = cpumask_bits(cpumask)[0] & APIC_ALL_CPUS;
165 unsigned long mask2 = cpumask_bits(andmask)[0] & APIC_ALL_CPUS;
Mike Travis95d313c2008-12-16 17:33:54 -0800166
Mike Travis6eeb7c52008-12-16 17:33:55 -0800167 return mask1 & mask2;
Mike Travis95d313c2008-12-16 17:33:54 -0800168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static unsigned int phys_pkg_id(int index_msb)
171{
ravikiran thirumalai0f4fdb72006-06-26 13:56:04 +0200172 return hard_smp_processor_id() >> index_msb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175struct genapic apic_flat = {
176 .name = "flat",
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700177 .acpi_madt_oem_check = flat_acpi_madt_oem_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 .int_delivery_mode = dest_LowestPrio,
179 .int_dest_mode = (APIC_DEST_LOGICAL != 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 .target_cpus = flat_target_cpus,
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600181 .vector_allocation_domain = flat_vector_allocation_domain,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 .apic_id_registered = flat_apic_id_registered,
183 .init_apic_ldr = flat_init_apic_ldr,
184 .send_IPI_all = flat_send_IPI_all,
185 .send_IPI_allbutself = flat_send_IPI_allbutself,
186 .send_IPI_mask = flat_send_IPI_mask,
Mike Travise7986732008-12-16 17:33:52 -0800187 .send_IPI_mask_allbutself = flat_send_IPI_mask_allbutself,
Suresh Siddhacff73a62008-07-10 11:16:53 -0700188 .send_IPI_self = apic_send_IPI_self,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 .cpu_mask_to_apicid = flat_cpu_mask_to_apicid,
Mike Travis95d313c2008-12-16 17:33:54 -0800190 .cpu_mask_to_apicid_and = flat_cpu_mask_to_apicid_and,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 .phys_pkg_id = phys_pkg_id,
Yinghai Luf910a9d2008-07-12 01:01:20 -0700192 .get_apic_id = get_apic_id,
193 .set_apic_id = set_apic_id,
194 .apic_id_mask = (0xFFu<<24),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195};
Andi Kleenf8d31192005-07-28 21:15:42 -0700196
197/*
198 * Physflat mode is used when there are more than 8 CPUs on a AMD system.
199 * We cannot use logical delivery in this case because the mask
200 * overflows, so use physical mode.
201 */
Marcin Slusarzfae172162008-10-12 11:44:09 +0200202static int physflat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700203{
204#ifdef CONFIG_ACPI
205 /*
206 * Quirk: some x86_64 machines can only use physical APIC mode
207 * regardless of how many processors are present (x86_64 ES7000
208 * is an example).
209 */
210 if (acpi_gbl_FADT.header.revision > FADT2_REVISION_ID &&
Yinghai Lu02c1df12008-09-04 20:57:11 +0200211 (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
212 printk(KERN_DEBUG "system APIC only can use physical flat");
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700213 return 1;
Yinghai Lu02c1df12008-09-04 20:57:11 +0200214 }
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700215#endif
216
217 return 0;
218}
Andi Kleenf8d31192005-07-28 21:15:42 -0700219
Mike Travise7986732008-12-16 17:33:52 -0800220static const cpumask_t *physflat_target_cpus(void)
Andi Kleenf8d31192005-07-28 21:15:42 -0700221{
Mike Travise7986732008-12-16 17:33:52 -0800222 return &cpu_online_map;
Andi Kleenf8d31192005-07-28 21:15:42 -0700223}
224
Mike Travise7986732008-12-16 17:33:52 -0800225static void physflat_vector_allocation_domain(int cpu, cpumask_t *retmask)
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600226{
Mike Travise7986732008-12-16 17:33:52 -0800227 cpus_clear(*retmask);
228 cpu_set(cpu, *retmask);
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600229}
230
Mike Travise7986732008-12-16 17:33:52 -0800231static void physflat_send_IPI_mask(const cpumask_t *cpumask, int vector)
Andi Kleenf8d31192005-07-28 21:15:42 -0700232{
233 send_IPI_mask_sequence(cpumask, vector);
234}
235
Mike Travise7986732008-12-16 17:33:52 -0800236static void physflat_send_IPI_mask_allbutself(const cpumask_t *cpumask,
237 int vector)
238{
239 send_IPI_mask_allbutself(cpumask, vector);
240}
241
Andi Kleenf8d31192005-07-28 21:15:42 -0700242static void physflat_send_IPI_allbutself(int vector)
243{
Mike Travise7986732008-12-16 17:33:52 -0800244 send_IPI_mask_allbutself(&cpu_online_map, vector);
Andi Kleenf8d31192005-07-28 21:15:42 -0700245}
246
247static void physflat_send_IPI_all(int vector)
248{
Mike Travise7986732008-12-16 17:33:52 -0800249 physflat_send_IPI_mask(&cpu_online_map, vector);
Andi Kleenf8d31192005-07-28 21:15:42 -0700250}
251
Mike Travise7986732008-12-16 17:33:52 -0800252static unsigned int physflat_cpu_mask_to_apicid(const cpumask_t *cpumask)
Andi Kleenf8d31192005-07-28 21:15:42 -0700253{
254 int cpu;
255
256 /*
257 * We're using fixed IRQ delivery, can only return one phys APIC ID.
258 * May as well be the first.
259 */
Mike Travise7986732008-12-16 17:33:52 -0800260 cpu = first_cpu(*cpumask);
Mike Travis1bd9d6b2008-07-18 18:11:30 -0700261 if ((unsigned)cpu < nr_cpu_ids)
Mike Travis71fff5e2007-10-19 20:35:03 +0200262 return per_cpu(x86_cpu_to_apicid, cpu);
Andi Kleenf8d31192005-07-28 21:15:42 -0700263 else
264 return BAD_APICID;
265}
266
Mike Travis6eeb7c52008-12-16 17:33:55 -0800267static unsigned int
268physflat_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
269 const struct cpumask *andmask)
Mike Travis95d313c2008-12-16 17:33:54 -0800270{
271 int cpu;
272
273 /*
274 * We're using fixed IRQ delivery, can only return one phys APIC ID.
275 * May as well be the first.
276 */
Mike Travis6eeb7c52008-12-16 17:33:55 -0800277 cpu = cpumask_any_and(cpumask, andmask);
278 if (cpu < nr_cpu_ids)
279 return per_cpu(x86_cpu_to_apicid, cpu);
Mike Travis95d313c2008-12-16 17:33:54 -0800280 return BAD_APICID;
281}
282
Andi Kleenf8d31192005-07-28 21:15:42 -0700283struct genapic apic_physflat = {
284 .name = "physical flat",
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700285 .acpi_madt_oem_check = physflat_acpi_madt_oem_check,
Ashok Rajc1a71a12005-09-12 18:49:24 +0200286 .int_delivery_mode = dest_Fixed,
Andi Kleenf8d31192005-07-28 21:15:42 -0700287 .int_dest_mode = (APIC_DEST_PHYSICAL != 0),
Andi Kleenf8d31192005-07-28 21:15:42 -0700288 .target_cpus = physflat_target_cpus,
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600289 .vector_allocation_domain = physflat_vector_allocation_domain,
Andi Kleenf8d31192005-07-28 21:15:42 -0700290 .apic_id_registered = flat_apic_id_registered,
291 .init_apic_ldr = flat_init_apic_ldr,/*not needed, but shouldn't hurt*/
292 .send_IPI_all = physflat_send_IPI_all,
293 .send_IPI_allbutself = physflat_send_IPI_allbutself,
294 .send_IPI_mask = physflat_send_IPI_mask,
Mike Travise7986732008-12-16 17:33:52 -0800295 .send_IPI_mask_allbutself = physflat_send_IPI_mask_allbutself,
Suresh Siddhacff73a62008-07-10 11:16:53 -0700296 .send_IPI_self = apic_send_IPI_self,
Andi Kleenf8d31192005-07-28 21:15:42 -0700297 .cpu_mask_to_apicid = physflat_cpu_mask_to_apicid,
Mike Travis95d313c2008-12-16 17:33:54 -0800298 .cpu_mask_to_apicid_and = physflat_cpu_mask_to_apicid_and,
Andi Kleenf8d31192005-07-28 21:15:42 -0700299 .phys_pkg_id = phys_pkg_id,
Yinghai Luf910a9d2008-07-12 01:01:20 -0700300 .get_apic_id = get_apic_id,
301 .set_apic_id = set_apic_id,
302 .apic_id_mask = (0xFFu<<24),
Andi Kleenf8d31192005-07-28 21:15:42 -0700303};