blob: ccbf857d1d558461eb59139784faa2d0f43cabc9 [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>
Randy Dunlapb18bf092011-05-23 10:43:00 -070019#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/smp.h>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010021#include <asm/apic.h>
Yinghai Luc1eeb2d2009-02-16 23:02:14 -080022#include <asm/ipi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Lv Zheng8b484632013-12-03 08:49:16 +080024#include <linux/acpi.h>
Yinghai Lu1b9b89e2008-07-21 22:08:21 -070025
Suresh Siddha1a8880a2011-05-20 17:51:20 -070026static struct apic apic_physflat;
27static struct apic apic_flat;
28
29struct apic __read_mostly *apic = &apic_flat;
30EXPORT_SYMBOL_GPL(apic);
31
Marcin Slusarz983f91f2008-10-12 11:44:08 +020032static int flat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
Yinghai Lu1b9b89e2008-07-21 22:08:21 -070033{
34 return 1;
35}
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * Set up the logical destination ID.
39 *
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...
43 */
Daniel J Blueman9a0ebfb2011-12-05 16:20:36 +080044void flat_init_apic_ldr(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 unsigned long val;
47 unsigned long num, id;
48
49 num = smp_processor_id();
50 id = 1UL << num;
Andi Kleeneddfb4e2005-09-12 18:49:23 +020051 apic_write(APIC_DFR, APIC_DFR_FLAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
53 val |= SET_APIC_LOGICAL_ID(id);
Andi Kleeneddfb4e2005-09-12 18:49:23 +020054 apic_write(APIC_LDR, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Mike Travise7986732008-12-16 17:33:52 -080057static inline void _flat_send_IPI_mask(unsigned long mask, int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned long flags;
60
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +020061 local_irq_save(flags);
Ingo Molnardac5f412009-01-28 15:42:24 +010062 __default_send_IPI_dest_field(mask, vector, apic->dest_logical);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 local_irq_restore(flags);
64}
65
Mike Travisbcda0162008-12-16 17:33:59 -080066static void flat_send_IPI_mask(const struct cpumask *cpumask, int vector)
Mike Travise7986732008-12-16 17:33:52 -080067{
Mike Travisbcda0162008-12-16 17:33:59 -080068 unsigned long mask = cpumask_bits(cpumask)[0];
Mike Travise7986732008-12-16 17:33:52 -080069
70 _flat_send_IPI_mask(mask, vector);
71}
72
Ingo Molnardac5f412009-01-28 15:42:24 +010073static void
Alexander Gordeev49d0c7a2012-06-05 13:23:15 +020074flat_send_IPI_mask_allbutself(const struct cpumask *cpumask, int vector)
Mike Travise7986732008-12-16 17:33:52 -080075{
Mike Travisbcda0162008-12-16 17:33:59 -080076 unsigned long mask = cpumask_bits(cpumask)[0];
Mike Travise7986732008-12-16 17:33:52 -080077 int cpu = smp_processor_id();
78
79 if (cpu < BITS_PER_LONG)
80 clear_bit(cpu, &mask);
Ingo Molnardac5f412009-01-28 15:42:24 +010081
Mike Travise7986732008-12-16 17:33:52 -080082 _flat_send_IPI_mask(mask, vector);
83}
84
Ashok Raj884d9e42005-06-25 14:55:02 -070085static void flat_send_IPI_allbutself(int vector)
86{
Mike Travise7986732008-12-16 17:33:52 -080087 int cpu = smp_processor_id();
Keith Owense77deac2006-06-26 13:59:56 +020088#ifdef CONFIG_HOTPLUG_CPU
89 int hotplug = 1;
Ashok Rajfdf26d92005-09-09 13:01:52 -070090#else
Keith Owense77deac2006-06-26 13:59:56 +020091 int hotplug = 0;
Ashok Rajfdf26d92005-09-09 13:01:52 -070092#endif
Keith Owense77deac2006-06-26 13:59:56 +020093 if (hotplug || vector == NMI_VECTOR) {
Mike Travisbcda0162008-12-16 17:33:59 -080094 if (!cpumask_equal(cpu_online_mask, cpumask_of(cpu))) {
95 unsigned long mask = cpumask_bits(cpu_online_mask)[0];
Keith Owense77deac2006-06-26 13:59:56 +020096
Mike Travise7986732008-12-16 17:33:52 -080097 if (cpu < BITS_PER_LONG)
98 clear_bit(cpu, &mask);
Keith Owense77deac2006-06-26 13:59:56 +020099
Mike Travise7986732008-12-16 17:33:52 -0800100 _flat_send_IPI_mask(mask, vector);
101 }
Keith Owense77deac2006-06-26 13:59:56 +0200102 } else if (num_online_cpus() > 1) {
Ingo Molnardac5f412009-01-28 15:42:24 +0100103 __default_send_IPI_shortcut(APIC_DEST_ALLBUT,
104 vector, apic->dest_logical);
Keith Owense77deac2006-06-26 13:59:56 +0200105 }
Ashok Raj884d9e42005-06-25 14:55:02 -0700106}
107
108static void flat_send_IPI_all(int vector)
109{
Ingo Molnardac5f412009-01-28 15:42:24 +0100110 if (vector == NMI_VECTOR) {
Mike Travisbcda0162008-12-16 17:33:59 -0800111 flat_send_IPI_mask(cpu_online_mask, vector);
Ingo Molnardac5f412009-01-28 15:42:24 +0100112 } else {
113 __default_send_IPI_shortcut(APIC_DEST_ALLINC,
114 vector, apic->dest_logical);
115 }
Ashok Raj884d9e42005-06-25 14:55:02 -0700116}
117
Ingo Molnarca6c8ed2009-01-28 14:08:38 +0100118static unsigned int flat_get_apic_id(unsigned long x)
Yinghai Luf910a9d2008-07-12 01:01:20 -0700119{
120 unsigned int id;
121
122 id = (((x)>>24) & 0xFFu);
Ingo Molnarca6c8ed2009-01-28 14:08:38 +0100123
Yinghai Luf910a9d2008-07-12 01:01:20 -0700124 return id;
125}
126
127static unsigned long set_apic_id(unsigned int id)
128{
129 unsigned long x;
130
131 x = ((id & 0xFFu)<<24);
132 return x;
133}
134
Suresh Siddha0c81c742008-07-10 11:16:48 -0700135static unsigned int read_xapic_id(void)
136{
137 unsigned int id;
138
Ingo Molnarca6c8ed2009-01-28 14:08:38 +0100139 id = flat_get_apic_id(apic_read(APIC_ID));
Suresh Siddha0c81c742008-07-10 11:16:48 -0700140 return id;
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static int flat_apic_id_registered(void)
144{
Suresh Siddha0c81c742008-07-10 11:16:48 -0700145 return physid_isset(read_xapic_id(), phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Ingo Molnard4c9a9f2009-01-28 13:31:22 +0100148static int flat_phys_pkg_id(int initial_apic_id, int index_msb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Yinghai Lu2759c322009-05-15 13:05:16 -0700150 return initial_apic_id >> index_msb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Yinghai Lue8524b22011-12-21 17:45:15 -0800153static int flat_probe(void)
154{
155 return 1;
156}
157
Suresh Siddha1a8880a2011-05-20 17:51:20 -0700158static struct apic apic_flat = {
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100159 .name = "flat",
Yinghai Lue8524b22011-12-21 17:45:15 -0800160 .probe = flat_probe,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100161 .acpi_madt_oem_check = flat_acpi_madt_oem_check,
Daniel J Bluemanfa630302012-03-14 15:17:34 +0800162 .apic_id_valid = default_apic_id_valid,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100163 .apic_id_registered = flat_apic_id_registered,
164
Ingo Molnarf8987a12009-01-28 04:02:31 +0100165 .irq_delivery_mode = dest_LowestPrio,
Ingo Molnar0b06e732009-01-28 05:13:04 +0100166 .irq_dest_mode = 1, /* logical */
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100167
Alexander Gordeevbf721d32012-06-05 13:23:29 +0200168 .target_cpus = online_target_cpus,
Ingo Molnar08125d32009-01-28 05:08:44 +0100169 .disable_esr = 0,
Ingo Molnarbdb1a9b2009-01-28 05:29:25 +0100170 .dest_logical = APIC_DEST_LOGICAL,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100171 .check_apicid_used = NULL,
172 .check_apicid_present = NULL,
173
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100174 .vector_allocation_domain = flat_vector_allocation_domain,
175 .init_apic_ldr = flat_init_apic_ldr,
176
177 .ioapic_phys_id_map = NULL,
178 .setup_apic_routing = NULL,
179 .multi_timer_check = NULL,
Ingo Molnara21769a42009-01-28 06:50:47 +0100180 .cpu_present_to_apicid = default_cpu_present_to_apicid,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100181 .apicid_to_cpu_present = NULL,
182 .setup_portio_remap = NULL,
Ingo Molnara27a6212009-01-28 12:43:18 +0100183 .check_phys_apicid_present = default_check_phys_apicid_present,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100184 .enable_apic_mode = NULL,
Ingo Molnard4c9a9f2009-01-28 13:31:22 +0100185 .phys_pkg_id = flat_phys_pkg_id,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100186 .mps_oem_check = NULL,
187
Ingo Molnarca6c8ed2009-01-28 14:08:38 +0100188 .get_apic_id = flat_get_apic_id,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100189 .set_apic_id = set_apic_id,
190 .apic_id_mask = 0xFFu << 24,
191
Alexander Gordeev63982682012-06-05 13:23:44 +0200192 .cpu_mask_to_apicid_and = flat_cpu_mask_to_apicid_and,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100193
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,
199
Ingo Molnarabfa5842009-01-28 16:15:16 +0100200 .trampoline_phys_low = DEFAULT_TRAMPOLINE_PHYS_LOW,
201 .trampoline_phys_high = DEFAULT_TRAMPOLINE_PHYS_HIGH,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100202 .wait_for_init_deassert = NULL,
203 .smp_callin_clear_local_apic = NULL,
Yinghai Lu08d63b12009-04-08 08:00:01 -0700204 .inquire_remote_apic = default_inquire_remote_apic,
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800205
206 .read = native_apic_mem_read,
207 .write = native_apic_mem_write,
Michael S. Tsirkin2a431952012-05-16 19:03:52 +0300208 .eoi_write = native_apic_mem_write,
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800209 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213};
Andi Kleenf8d31192005-07-28 21:15:42 -0700214
215/*
Jasper Spaanse34b7002009-11-20 14:20:05 +0100216 * Physflat mode is used when there are more than 8 CPUs on a system.
Andi Kleenf8d31192005-07-28 21:15:42 -0700217 * We cannot use logical delivery in this case because the mask
218 * overflows, so use physical mode.
219 */
Marcin Slusarzfae172162008-10-12 11:44:09 +0200220static int physflat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700221{
222#ifdef CONFIG_ACPI
223 /*
224 * Quirk: some x86_64 machines can only use physical APIC mode
225 * regardless of how many processors are present (x86_64 ES7000
226 * is an example).
227 */
Yinghai Lu61fe91e2009-05-09 23:47:42 -0700228 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
Yinghai Lu02c1df12008-09-04 20:57:11 +0200229 (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
230 printk(KERN_DEBUG "system APIC only can use physical flat");
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700231 return 1;
Yinghai Lu02c1df12008-09-04 20:57:11 +0200232 }
Suresh Siddhadfea91d2010-01-18 12:10:48 -0800233
234 if (!strncmp(oem_id, "IBM", 3) && !strncmp(oem_table_id, "EXA", 3)) {
235 printk(KERN_DEBUG "IBM Summit detected, will use apic physical");
236 return 1;
237 }
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700238#endif
239
240 return 0;
241}
Andi Kleenf8d31192005-07-28 21:15:42 -0700242
Mike Travisbcda0162008-12-16 17:33:59 -0800243static void physflat_send_IPI_mask(const struct cpumask *cpumask, int vector)
Andi Kleenf8d31192005-07-28 21:15:42 -0700244{
Yinghai Lu43f39892009-01-29 19:31:49 -0800245 default_send_IPI_mask_sequence_phys(cpumask, vector);
Andi Kleenf8d31192005-07-28 21:15:42 -0700246}
247
Mike Travisbcda0162008-12-16 17:33:59 -0800248static void physflat_send_IPI_mask_allbutself(const struct cpumask *cpumask,
Mike Travise7986732008-12-16 17:33:52 -0800249 int vector)
250{
Yinghai Lu43f39892009-01-29 19:31:49 -0800251 default_send_IPI_mask_allbutself_phys(cpumask, vector);
Mike Travise7986732008-12-16 17:33:52 -0800252}
253
Andi Kleenf8d31192005-07-28 21:15:42 -0700254static void physflat_send_IPI_allbutself(int vector)
255{
Yinghai Lu43f39892009-01-29 19:31:49 -0800256 default_send_IPI_mask_allbutself_phys(cpu_online_mask, vector);
Andi Kleenf8d31192005-07-28 21:15:42 -0700257}
258
259static void physflat_send_IPI_all(int vector)
260{
Mike Travisbcda0162008-12-16 17:33:59 -0800261 physflat_send_IPI_mask(cpu_online_mask, vector);
Andi Kleenf8d31192005-07-28 21:15:42 -0700262}
263
Suresh Siddha9ebd6802011-05-19 16:45:46 -0700264static int physflat_probe(void)
265{
266 if (apic == &apic_physflat || num_possible_cpus() > 8)
267 return 1;
268
269 return 0;
270}
271
Suresh Siddha1a8880a2011-05-20 17:51:20 -0700272static struct apic apic_physflat = {
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100273
274 .name = "physical flat",
Suresh Siddha9ebd6802011-05-19 16:45:46 -0700275 .probe = physflat_probe,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100276 .acpi_madt_oem_check = physflat_acpi_madt_oem_check,
Daniel J Bluemanfa630302012-03-14 15:17:34 +0800277 .apic_id_valid = default_apic_id_valid,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100278 .apic_id_registered = flat_apic_id_registered,
279
Ingo Molnarf8987a12009-01-28 04:02:31 +0100280 .irq_delivery_mode = dest_Fixed,
Ingo Molnar0b06e732009-01-28 05:13:04 +0100281 .irq_dest_mode = 0, /* physical */
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100282
Alexander Gordeevbf721d32012-06-05 13:23:29 +0200283 .target_cpus = online_target_cpus,
Ingo Molnar08125d32009-01-28 05:08:44 +0100284 .disable_esr = 0,
Ingo Molnarbdb1a9b2009-01-28 05:29:25 +0100285 .dest_logical = 0,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100286 .check_apicid_used = NULL,
287 .check_apicid_present = NULL,
288
Alexander Gordeev9d8e1062012-06-07 15:14:49 +0200289 .vector_allocation_domain = default_vector_allocation_domain,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100290 /* not needed, but shouldn't hurt: */
291 .init_apic_ldr = flat_init_apic_ldr,
292
293 .ioapic_phys_id_map = NULL,
294 .setup_apic_routing = NULL,
295 .multi_timer_check = NULL,
Ingo Molnara21769a42009-01-28 06:50:47 +0100296 .cpu_present_to_apicid = default_cpu_present_to_apicid,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100297 .apicid_to_cpu_present = NULL,
298 .setup_portio_remap = NULL,
Ingo Molnara27a6212009-01-28 12:43:18 +0100299 .check_phys_apicid_present = default_check_phys_apicid_present,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100300 .enable_apic_mode = NULL,
Ingo Molnard4c9a9f2009-01-28 13:31:22 +0100301 .phys_pkg_id = flat_phys_pkg_id,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100302 .mps_oem_check = NULL,
303
Ingo Molnarca6c8ed2009-01-28 14:08:38 +0100304 .get_apic_id = flat_get_apic_id,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100305 .set_apic_id = set_apic_id,
Ingo Molnar5b812722009-01-28 14:59:17 +0100306 .apic_id_mask = 0xFFu << 24,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100307
Alexander Gordeev63982682012-06-05 13:23:44 +0200308 .cpu_mask_to_apicid_and = default_cpu_mask_to_apicid_and,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100309
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,
315
Ingo Molnarabfa5842009-01-28 16:15:16 +0100316 .trampoline_phys_low = DEFAULT_TRAMPOLINE_PHYS_LOW,
317 .trampoline_phys_high = DEFAULT_TRAMPOLINE_PHYS_HIGH,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100318 .wait_for_init_deassert = NULL,
319 .smp_callin_clear_local_apic = NULL,
Yinghai Lu08d63b12009-04-08 08:00:01 -0700320 .inquire_remote_apic = default_inquire_remote_apic,
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800321
322 .read = native_apic_mem_read,
323 .write = native_apic_mem_write,
Michael S. Tsirkin2a431952012-05-16 19:03:52 +0300324 .eoi_write = native_apic_mem_write,
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800325 .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,
Andi Kleenf8d31192005-07-28 21:15:42 -0700329};
Suresh Siddha107e0e02011-05-20 17:51:17 -0700330
331/*
332 * We need to check for physflat first, so this order is important.
333 */
334apic_drivers(apic_physflat, apic_flat);