blob: dedd5a41ba48c8d72b513c033bcf1fffd03deb71 [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>
Suresh Siddha0c81c742008-07-10 11:16:48 -070017#include <linux/hardirq.h>
Paul Gortmaker186f4362016-07-13 20:18:56 -040018#include <linux/export.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
Lv Zheng8b484632013-12-03 08:49:16 +080023#include <linux/acpi.h>
Yinghai Lu1b9b89e2008-07-21 22:08:21 -070024
Suresh Siddha1a8880a2011-05-20 17:51:20 -070025static struct apic apic_physflat;
26static struct apic apic_flat;
27
Kees Cook404f6aa2016-08-08 16:29:06 -070028struct apic *apic __ro_after_init = &apic_flat;
Suresh Siddha1a8880a2011-05-20 17:51:20 -070029EXPORT_SYMBOL_GPL(apic);
30
Marcin Slusarz983f91f2008-10-12 11:44:08 +020031static int flat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
Yinghai Lu1b9b89e2008-07-21 22:08:21 -070032{
33 return 1;
34}
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/*
37 * Set up the logical destination ID.
38 *
39 * Intel recommends to set DFR, LDR and TPR before enabling
40 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
41 * document number 292116). So here it goes...
42 */
Daniel J Blueman9a0ebfb2011-12-05 16:20:36 +080043void flat_init_apic_ldr(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 unsigned long val;
46 unsigned long num, id;
47
48 num = smp_processor_id();
49 id = 1UL << num;
Andi Kleeneddfb4e2005-09-12 18:49:23 +020050 apic_write(APIC_DFR, APIC_DFR_FLAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
52 val |= SET_APIC_LOGICAL_ID(id);
Andi Kleeneddfb4e2005-09-12 18:49:23 +020053 apic_write(APIC_LDR, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Denys Vlasenkofe2f9542016-03-06 19:11:16 +010056static void _flat_send_IPI_mask(unsigned long mask, int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 unsigned long flags;
59
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +020060 local_irq_save(flags);
Ingo Molnardac5f412009-01-28 15:42:24 +010061 __default_send_IPI_dest_field(mask, vector, apic->dest_logical);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 local_irq_restore(flags);
63}
64
Mike Travisbcda0162008-12-16 17:33:59 -080065static void flat_send_IPI_mask(const struct cpumask *cpumask, int vector)
Mike Travise7986732008-12-16 17:33:52 -080066{
Mike Travisbcda0162008-12-16 17:33:59 -080067 unsigned long mask = cpumask_bits(cpumask)[0];
Mike Travise7986732008-12-16 17:33:52 -080068
69 _flat_send_IPI_mask(mask, vector);
70}
71
Ingo Molnardac5f412009-01-28 15:42:24 +010072static void
Alexander Gordeev49d0c7a2012-06-05 13:23:15 +020073flat_send_IPI_mask_allbutself(const struct cpumask *cpumask, int vector)
Mike Travise7986732008-12-16 17:33:52 -080074{
Mike Travisbcda0162008-12-16 17:33:59 -080075 unsigned long mask = cpumask_bits(cpumask)[0];
Mike Travise7986732008-12-16 17:33:52 -080076 int cpu = smp_processor_id();
77
78 if (cpu < BITS_PER_LONG)
79 clear_bit(cpu, &mask);
Ingo Molnardac5f412009-01-28 15:42:24 +010080
Mike Travise7986732008-12-16 17:33:52 -080081 _flat_send_IPI_mask(mask, vector);
82}
83
Ashok Raj884d9e42005-06-25 14:55:02 -070084static void flat_send_IPI_allbutself(int vector)
85{
Mike Travise7986732008-12-16 17:33:52 -080086 int cpu = smp_processor_id();
Keith Owense77deac2006-06-26 13:59:56 +020087#ifdef CONFIG_HOTPLUG_CPU
88 int hotplug = 1;
Ashok Rajfdf26d92005-09-09 13:01:52 -070089#else
Keith Owense77deac2006-06-26 13:59:56 +020090 int hotplug = 0;
Ashok Rajfdf26d92005-09-09 13:01:52 -070091#endif
Keith Owense77deac2006-06-26 13:59:56 +020092 if (hotplug || vector == NMI_VECTOR) {
Mike Travisbcda0162008-12-16 17:33:59 -080093 if (!cpumask_equal(cpu_online_mask, cpumask_of(cpu))) {
94 unsigned long mask = cpumask_bits(cpu_online_mask)[0];
Keith Owense77deac2006-06-26 13:59:56 +020095
Mike Travise7986732008-12-16 17:33:52 -080096 if (cpu < BITS_PER_LONG)
97 clear_bit(cpu, &mask);
Keith Owense77deac2006-06-26 13:59:56 +020098
Mike Travise7986732008-12-16 17:33:52 -080099 _flat_send_IPI_mask(mask, vector);
100 }
Keith Owense77deac2006-06-26 13:59:56 +0200101 } else if (num_online_cpus() > 1) {
Ingo Molnardac5f412009-01-28 15:42:24 +0100102 __default_send_IPI_shortcut(APIC_DEST_ALLBUT,
103 vector, apic->dest_logical);
Keith Owense77deac2006-06-26 13:59:56 +0200104 }
Ashok Raj884d9e42005-06-25 14:55:02 -0700105}
106
107static void flat_send_IPI_all(int vector)
108{
Ingo Molnardac5f412009-01-28 15:42:24 +0100109 if (vector == NMI_VECTOR) {
Mike Travisbcda0162008-12-16 17:33:59 -0800110 flat_send_IPI_mask(cpu_online_mask, vector);
Ingo Molnardac5f412009-01-28 15:42:24 +0100111 } else {
112 __default_send_IPI_shortcut(APIC_DEST_ALLINC,
113 vector, apic->dest_logical);
114 }
Ashok Raj884d9e42005-06-25 14:55:02 -0700115}
116
Ingo Molnarca6c8ed2009-01-28 14:08:38 +0100117static unsigned int flat_get_apic_id(unsigned long x)
Yinghai Luf910a9d2008-07-12 01:01:20 -0700118{
Masahiro Yamadaf148b412016-09-11 14:58:21 +0900119 return (x >> 24) & 0xFF;
Yinghai Luf910a9d2008-07-12 01:01:20 -0700120}
121
122static unsigned long set_apic_id(unsigned int id)
123{
Masahiro Yamadaf148b412016-09-11 14:58:21 +0900124 return (id & 0xFF) << 24;
Yinghai Luf910a9d2008-07-12 01:01:20 -0700125}
126
Suresh Siddha0c81c742008-07-10 11:16:48 -0700127static unsigned int read_xapic_id(void)
128{
Masahiro Yamadaf148b412016-09-11 14:58:21 +0900129 return flat_get_apic_id(apic_read(APIC_ID));
Suresh Siddha0c81c742008-07-10 11:16:48 -0700130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static int flat_apic_id_registered(void)
133{
Suresh Siddha0c81c742008-07-10 11:16:48 -0700134 return physid_isset(read_xapic_id(), phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Ingo Molnard4c9a9f2009-01-28 13:31:22 +0100137static int flat_phys_pkg_id(int initial_apic_id, int index_msb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Yinghai Lu2759c322009-05-15 13:05:16 -0700139 return initial_apic_id >> index_msb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Yinghai Lue8524b22011-12-21 17:45:15 -0800142static int flat_probe(void)
143{
144 return 1;
145}
146
Kees Cook404f6aa2016-08-08 16:29:06 -0700147static struct apic apic_flat __ro_after_init = {
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100148 .name = "flat",
Yinghai Lue8524b22011-12-21 17:45:15 -0800149 .probe = flat_probe,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100150 .acpi_madt_oem_check = flat_acpi_madt_oem_check,
Daniel J Bluemanfa630302012-03-14 15:17:34 +0800151 .apic_id_valid = default_apic_id_valid,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100152 .apic_id_registered = flat_apic_id_registered,
153
Ingo Molnarf8987a12009-01-28 04:02:31 +0100154 .irq_delivery_mode = dest_LowestPrio,
Ingo Molnar0b06e732009-01-28 05:13:04 +0100155 .irq_dest_mode = 1, /* logical */
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100156
Alexander Gordeevbf721d32012-06-05 13:23:29 +0200157 .target_cpus = online_target_cpus,
Ingo Molnar08125d32009-01-28 05:08:44 +0100158 .disable_esr = 0,
Ingo Molnarbdb1a9b2009-01-28 05:29:25 +0100159 .dest_logical = APIC_DEST_LOGICAL,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100160 .check_apicid_used = NULL,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100161
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100162 .vector_allocation_domain = flat_vector_allocation_domain,
163 .init_apic_ldr = flat_init_apic_ldr,
164
165 .ioapic_phys_id_map = NULL,
166 .setup_apic_routing = NULL,
Ingo Molnara21769a42009-01-28 06:50:47 +0100167 .cpu_present_to_apicid = default_cpu_present_to_apicid,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100168 .apicid_to_cpu_present = NULL,
Ingo Molnara27a6212009-01-28 12:43:18 +0100169 .check_phys_apicid_present = default_check_phys_apicid_present,
Ingo Molnard4c9a9f2009-01-28 13:31:22 +0100170 .phys_pkg_id = flat_phys_pkg_id,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100171
Ingo Molnarca6c8ed2009-01-28 14:08:38 +0100172 .get_apic_id = flat_get_apic_id,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100173 .set_apic_id = set_apic_id,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100174
Thomas Gleixner91cd9cb2017-06-20 01:37:43 +0200175 .cpu_mask_to_apicid = flat_cpu_mask_to_apicid,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100176
Thomas Gleixner61530582015-11-04 22:57:08 +0000177 .send_IPI = default_send_IPI_single,
Ingo Molnarf2f05ee2009-01-28 02:37:01 +0100178 .send_IPI_mask = flat_send_IPI_mask,
179 .send_IPI_mask_allbutself = flat_send_IPI_mask_allbutself,
180 .send_IPI_allbutself = flat_send_IPI_allbutself,
181 .send_IPI_all = flat_send_IPI_all,
182 .send_IPI_self = apic_send_IPI_self,
183
Yinghai Lu08d63b12009-04-08 08:00:01 -0700184 .inquire_remote_apic = default_inquire_remote_apic,
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800185
186 .read = native_apic_mem_read,
187 .write = native_apic_mem_write,
Michael S. Tsirkin2a431952012-05-16 19:03:52 +0300188 .eoi_write = native_apic_mem_write,
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800189 .icr_read = native_apic_icr_read,
190 .icr_write = native_apic_icr_write,
191 .wait_icr_idle = native_apic_wait_icr_idle,
192 .safe_wait_icr_idle = native_safe_apic_wait_icr_idle,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193};
Andi Kleenf8d31192005-07-28 21:15:42 -0700194
195/*
Jasper Spaanse34b7002009-11-20 14:20:05 +0100196 * Physflat mode is used when there are more than 8 CPUs on a system.
Andi Kleenf8d31192005-07-28 21:15:42 -0700197 * We cannot use logical delivery in this case because the mask
198 * overflows, so use physical mode.
199 */
Marcin Slusarzfae172162008-10-12 11:44:09 +0200200static int physflat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700201{
202#ifdef CONFIG_ACPI
203 /*
204 * Quirk: some x86_64 machines can only use physical APIC mode
205 * regardless of how many processors are present (x86_64 ES7000
206 * is an example).
207 */
Yinghai Lu61fe91e2009-05-09 23:47:42 -0700208 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
Yinghai Lu02c1df12008-09-04 20:57:11 +0200209 (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
210 printk(KERN_DEBUG "system APIC only can use physical flat");
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700211 return 1;
Yinghai Lu02c1df12008-09-04 20:57:11 +0200212 }
Suresh Siddhadfea91d2010-01-18 12:10:48 -0800213
214 if (!strncmp(oem_id, "IBM", 3) && !strncmp(oem_table_id, "EXA", 3)) {
215 printk(KERN_DEBUG "IBM Summit detected, will use apic physical");
216 return 1;
217 }
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700218#endif
219
220 return 0;
221}
Andi Kleenf8d31192005-07-28 21:15:42 -0700222
Andi Kleenf8d31192005-07-28 21:15:42 -0700223static void physflat_send_IPI_allbutself(int vector)
224{
Yinghai Lu43f39892009-01-29 19:31:49 -0800225 default_send_IPI_mask_allbutself_phys(cpu_online_mask, vector);
Andi Kleenf8d31192005-07-28 21:15:42 -0700226}
227
228static void physflat_send_IPI_all(int vector)
229{
Thomas Gleixner449112f2015-11-04 22:57:02 +0000230 default_send_IPI_mask_sequence_phys(cpu_online_mask, vector);
Andi Kleenf8d31192005-07-28 21:15:42 -0700231}
232
Suresh Siddha9ebd6802011-05-19 16:45:46 -0700233static int physflat_probe(void)
234{
235 if (apic == &apic_physflat || num_possible_cpus() > 8)
236 return 1;
237
238 return 0;
239}
240
Kees Cook404f6aa2016-08-08 16:29:06 -0700241static struct apic apic_physflat __ro_after_init = {
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100242
243 .name = "physical flat",
Suresh Siddha9ebd6802011-05-19 16:45:46 -0700244 .probe = physflat_probe,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100245 .acpi_madt_oem_check = physflat_acpi_madt_oem_check,
Daniel J Bluemanfa630302012-03-14 15:17:34 +0800246 .apic_id_valid = default_apic_id_valid,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100247 .apic_id_registered = flat_apic_id_registered,
248
Ingo Molnarf8987a12009-01-28 04:02:31 +0100249 .irq_delivery_mode = dest_Fixed,
Ingo Molnar0b06e732009-01-28 05:13:04 +0100250 .irq_dest_mode = 0, /* physical */
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100251
Alexander Gordeevbf721d32012-06-05 13:23:29 +0200252 .target_cpus = online_target_cpus,
Ingo Molnar08125d32009-01-28 05:08:44 +0100253 .disable_esr = 0,
Ingo Molnarbdb1a9b2009-01-28 05:29:25 +0100254 .dest_logical = 0,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100255 .check_apicid_used = NULL,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100256
Alexander Gordeev9d8e1062012-06-07 15:14:49 +0200257 .vector_allocation_domain = default_vector_allocation_domain,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100258 /* not needed, but shouldn't hurt: */
259 .init_apic_ldr = flat_init_apic_ldr,
260
261 .ioapic_phys_id_map = NULL,
262 .setup_apic_routing = NULL,
Ingo Molnara21769a42009-01-28 06:50:47 +0100263 .cpu_present_to_apicid = default_cpu_present_to_apicid,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100264 .apicid_to_cpu_present = NULL,
Ingo Molnara27a6212009-01-28 12:43:18 +0100265 .check_phys_apicid_present = default_check_phys_apicid_present,
Ingo Molnard4c9a9f2009-01-28 13:31:22 +0100266 .phys_pkg_id = flat_phys_pkg_id,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100267
Ingo Molnarca6c8ed2009-01-28 14:08:38 +0100268 .get_apic_id = flat_get_apic_id,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100269 .set_apic_id = set_apic_id,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100270
Thomas Gleixner91cd9cb2017-06-20 01:37:43 +0200271 .cpu_mask_to_apicid = default_cpu_mask_to_apicid,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100272
Thomas Gleixner68cd88f2015-11-04 22:57:02 +0000273 .send_IPI = default_send_IPI_single_phys,
Thomas Gleixner449112f2015-11-04 22:57:02 +0000274 .send_IPI_mask = default_send_IPI_mask_sequence_phys,
275 .send_IPI_mask_allbutself = default_send_IPI_mask_allbutself_phys,
Ingo Molnar4c3e51e2009-01-28 02:37:01 +0100276 .send_IPI_allbutself = physflat_send_IPI_allbutself,
277 .send_IPI_all = physflat_send_IPI_all,
278 .send_IPI_self = apic_send_IPI_self,
279
Yinghai Lu08d63b12009-04-08 08:00:01 -0700280 .inquire_remote_apic = default_inquire_remote_apic,
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800281
282 .read = native_apic_mem_read,
283 .write = native_apic_mem_write,
Michael S. Tsirkin2a431952012-05-16 19:03:52 +0300284 .eoi_write = native_apic_mem_write,
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800285 .icr_read = native_apic_icr_read,
286 .icr_write = native_apic_icr_write,
287 .wait_icr_idle = native_apic_wait_icr_idle,
288 .safe_wait_icr_idle = native_safe_apic_wait_icr_idle,
Andi Kleenf8d31192005-07-28 21:15:42 -0700289};
Suresh Siddha107e0e02011-05-20 17:51:17 -0700290
291/*
292 * We need to check for physflat first, so this order is important.
293 */
294apic_drivers(apic_physflat, apic_flat);