blob: 2ec2de8d8c46525a13d91a731366d5cfbce4b37b [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
28static int __init flat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
29{
30 return 1;
31}
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static cpumask_t flat_target_cpus(void)
34{
35 return cpu_online_map;
36}
37
Eric W. Biedermanc7111c132006-10-08 07:47:55 -060038static cpumask_t flat_vector_allocation_domain(int cpu)
39{
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 */
48 cpumask_t domain = { { [0] = APIC_ALL_CPUS, } };
49 return domain;
50}
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
53 * Set up the logical destination ID.
54 *
55 * Intel recommends to set DFR, LDR and TPR before enabling
56 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
57 * document number 292116). So here it goes...
58 */
59static void flat_init_apic_ldr(void)
60{
61 unsigned long val;
62 unsigned long num, id;
63
64 num = smp_processor_id();
65 id = 1UL << num;
Andi Kleeneddfb4e2005-09-12 18:49:23 +020066 apic_write(APIC_DFR, APIC_DFR_FLAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
68 val |= SET_APIC_LOGICAL_ID(id);
Andi Kleeneddfb4e2005-09-12 18:49:23 +020069 apic_write(APIC_LDR, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static void flat_send_IPI_mask(cpumask_t cpumask, int vector)
73{
74 unsigned long mask = cpus_addr(cpumask)[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 unsigned long flags;
76
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +020077 local_irq_save(flags);
Fernando Luis [** ISO-8859-1 charset **] VázquezCao9062d882007-05-02 19:27:18 +020078 __send_IPI_dest_field(mask, vector, APIC_DEST_LOGICAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 local_irq_restore(flags);
80}
81
Ashok Raj884d9e42005-06-25 14:55:02 -070082static void flat_send_IPI_allbutself(int vector)
83{
Keith Owense77deac2006-06-26 13:59:56 +020084#ifdef CONFIG_HOTPLUG_CPU
85 int hotplug = 1;
Ashok Rajfdf26d92005-09-09 13:01:52 -070086#else
Keith Owense77deac2006-06-26 13:59:56 +020087 int hotplug = 0;
Ashok Rajfdf26d92005-09-09 13:01:52 -070088#endif
Keith Owense77deac2006-06-26 13:59:56 +020089 if (hotplug || vector == NMI_VECTOR) {
90 cpumask_t allbutme = cpu_online_map;
91
92 cpu_clear(smp_processor_id(), allbutme);
93
94 if (!cpus_empty(allbutme))
95 flat_send_IPI_mask(allbutme, vector);
96 } else if (num_online_cpus() > 1) {
97 __send_IPI_shortcut(APIC_DEST_ALLBUT, vector,APIC_DEST_LOGICAL);
98 }
Ashok Raj884d9e42005-06-25 14:55:02 -070099}
100
101static void flat_send_IPI_all(int vector)
102{
Keith Owense77deac2006-06-26 13:59:56 +0200103 if (vector == NMI_VECTOR)
104 flat_send_IPI_mask(cpu_online_map, vector);
105 else
106 __send_IPI_shortcut(APIC_DEST_ALLINC, vector, APIC_DEST_LOGICAL);
Ashok Raj884d9e42005-06-25 14:55:02 -0700107}
108
Yinghai Luf910a9d2008-07-12 01:01:20 -0700109static unsigned int get_apic_id(unsigned long x)
110{
111 unsigned int id;
112
113 id = (((x)>>24) & 0xFFu);
114 return id;
115}
116
117static unsigned long set_apic_id(unsigned int id)
118{
119 unsigned long x;
120
121 x = ((id & 0xFFu)<<24);
122 return x;
123}
124
Suresh Siddha0c81c742008-07-10 11:16:48 -0700125static unsigned int read_xapic_id(void)
126{
127 unsigned int id;
128
Yinghai Luf910a9d2008-07-12 01:01:20 -0700129 id = get_apic_id(apic_read(APIC_ID));
Suresh Siddha0c81c742008-07-10 11:16:48 -0700130 return id;
131}
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static int flat_apic_id_registered(void)
134{
Suresh Siddha0c81c742008-07-10 11:16:48 -0700135 return physid_isset(read_xapic_id(), phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
138static unsigned int flat_cpu_mask_to_apicid(cpumask_t cpumask)
139{
140 return cpus_addr(cpumask)[0] & APIC_ALL_CPUS;
141}
142
143static unsigned int phys_pkg_id(int index_msb)
144{
ravikiran thirumalai0f4fdb72006-06-26 13:56:04 +0200145 return hard_smp_processor_id() >> index_msb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148struct genapic apic_flat = {
149 .name = "flat",
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700150 .acpi_madt_oem_check = flat_acpi_madt_oem_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 .int_delivery_mode = dest_LowestPrio,
152 .int_dest_mode = (APIC_DEST_LOGICAL != 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 .target_cpus = flat_target_cpus,
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600154 .vector_allocation_domain = flat_vector_allocation_domain,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 .apic_id_registered = flat_apic_id_registered,
156 .init_apic_ldr = flat_init_apic_ldr,
157 .send_IPI_all = flat_send_IPI_all,
158 .send_IPI_allbutself = flat_send_IPI_allbutself,
159 .send_IPI_mask = flat_send_IPI_mask,
Suresh Siddhacff73a62008-07-10 11:16:53 -0700160 .send_IPI_self = apic_send_IPI_self,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 .cpu_mask_to_apicid = flat_cpu_mask_to_apicid,
162 .phys_pkg_id = phys_pkg_id,
Yinghai Luf910a9d2008-07-12 01:01:20 -0700163 .get_apic_id = get_apic_id,
164 .set_apic_id = set_apic_id,
165 .apic_id_mask = (0xFFu<<24),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166};
Andi Kleenf8d31192005-07-28 21:15:42 -0700167
168/*
169 * Physflat mode is used when there are more than 8 CPUs on a AMD system.
170 * We cannot use logical delivery in this case because the mask
171 * overflows, so use physical mode.
172 */
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700173static int __init physflat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
174{
175#ifdef CONFIG_ACPI
176 /*
177 * Quirk: some x86_64 machines can only use physical APIC mode
178 * regardless of how many processors are present (x86_64 ES7000
179 * is an example).
180 */
181 if (acpi_gbl_FADT.header.revision > FADT2_REVISION_ID &&
Yinghai Lu02c1df12008-09-04 20:57:11 +0200182 (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
183 printk(KERN_DEBUG "system APIC only can use physical flat");
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700184 return 1;
Yinghai Lu02c1df12008-09-04 20:57:11 +0200185 }
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700186#endif
187
188 return 0;
189}
Andi Kleenf8d31192005-07-28 21:15:42 -0700190
191static cpumask_t physflat_target_cpus(void)
192{
Eric W. Biederman84f404f2006-10-21 18:37:02 +0200193 return cpu_online_map;
Andi Kleenf8d31192005-07-28 21:15:42 -0700194}
195
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600196static cpumask_t physflat_vector_allocation_domain(int cpu)
197{
Akinobu Mita7281c962008-04-05 22:39:08 +0900198 return cpumask_of_cpu(cpu);
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600199}
200
Andi Kleenf8d31192005-07-28 21:15:42 -0700201static void physflat_send_IPI_mask(cpumask_t cpumask, int vector)
202{
203 send_IPI_mask_sequence(cpumask, vector);
204}
205
206static void physflat_send_IPI_allbutself(int vector)
207{
208 cpumask_t allbutme = cpu_online_map;
Zwane Mwaikambo329d4002006-01-11 22:43:09 +0100209
210 cpu_clear(smp_processor_id(), allbutme);
Andi Kleenf8d31192005-07-28 21:15:42 -0700211 physflat_send_IPI_mask(allbutme, vector);
Andi Kleenf8d31192005-07-28 21:15:42 -0700212}
213
214static void physflat_send_IPI_all(int vector)
215{
216 physflat_send_IPI_mask(cpu_online_map, vector);
217}
218
219static unsigned int physflat_cpu_mask_to_apicid(cpumask_t cpumask)
220{
221 int cpu;
222
223 /*
224 * We're using fixed IRQ delivery, can only return one phys APIC ID.
225 * May as well be the first.
226 */
227 cpu = first_cpu(cpumask);
Mike Travis1bd9d6b2008-07-18 18:11:30 -0700228 if ((unsigned)cpu < nr_cpu_ids)
Mike Travis71fff5e2007-10-19 20:35:03 +0200229 return per_cpu(x86_cpu_to_apicid, cpu);
Andi Kleenf8d31192005-07-28 21:15:42 -0700230 else
231 return BAD_APICID;
232}
233
234struct genapic apic_physflat = {
235 .name = "physical flat",
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700236 .acpi_madt_oem_check = physflat_acpi_madt_oem_check,
Ashok Rajc1a71a12005-09-12 18:49:24 +0200237 .int_delivery_mode = dest_Fixed,
Andi Kleenf8d31192005-07-28 21:15:42 -0700238 .int_dest_mode = (APIC_DEST_PHYSICAL != 0),
Andi Kleenf8d31192005-07-28 21:15:42 -0700239 .target_cpus = physflat_target_cpus,
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600240 .vector_allocation_domain = physflat_vector_allocation_domain,
Andi Kleenf8d31192005-07-28 21:15:42 -0700241 .apic_id_registered = flat_apic_id_registered,
242 .init_apic_ldr = flat_init_apic_ldr,/*not needed, but shouldn't hurt*/
243 .send_IPI_all = physflat_send_IPI_all,
244 .send_IPI_allbutself = physflat_send_IPI_allbutself,
245 .send_IPI_mask = physflat_send_IPI_mask,
Suresh Siddhacff73a62008-07-10 11:16:53 -0700246 .send_IPI_self = apic_send_IPI_self,
Andi Kleenf8d31192005-07-28 21:15:42 -0700247 .cpu_mask_to_apicid = physflat_cpu_mask_to_apicid,
248 .phys_pkg_id = phys_pkg_id,
Yinghai Luf910a9d2008-07-12 01:01:20 -0700249 .get_apic_id = get_apic_id,
250 .set_apic_id = set_apic_id,
251 .apic_id_mask = (0xFFu<<24),
Andi Kleenf8d31192005-07-28 21:15:42 -0700252};