blob: 62895cf315ffb1041e57364cd35d385a62826b25 [file] [log] [blame]
Suresh Siddha2d9579a2008-07-10 11:16:59 -07001#include <linux/threads.h>
2#include <linux/cpumask.h>
3#include <linux/string.h>
4#include <linux/kernel.h>
5#include <linux/ctype.h>
6#include <linux/init.h>
Yinghai Lu1b9b89e2008-07-21 22:08:21 -07007#include <linux/dmar.h>
8
Suresh Siddha2d9579a2008-07-10 11:16:59 -07009#include <asm/smp.h>
10#include <asm/ipi.h>
11#include <asm/genapic.h>
12
Yinghai Lu1b9b89e2008-07-21 22:08:21 -070013static int x2apic_phys;
14
15static int set_x2apic_phys_mode(char *arg)
16{
17 x2apic_phys = 1;
18 return 0;
19}
20early_param("x2apic_phys", set_x2apic_phys_mode);
21
Marcin Slusarz3cfba082008-10-12 11:46:23 +020022static int x2apic_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
Yinghai Lu1b9b89e2008-07-21 22:08:21 -070023{
Yinghai Lud25ae382008-07-25 19:39:03 -070024 if (cpu_has_x2apic && x2apic_phys)
Yinghai Lu1b9b89e2008-07-21 22:08:21 -070025 return 1;
26
27 return 0;
28}
Suresh Siddha2d9579a2008-07-10 11:16:59 -070029
30/* Start with all IRQs pointing to boot CPU. IRQ balancing will shift them. */
31
Mike Travisbcda0162008-12-16 17:33:59 -080032static const struct cpumask *x2apic_target_cpus(void)
Suresh Siddha2d9579a2008-07-10 11:16:59 -070033{
Mike Travisbcda0162008-12-16 17:33:59 -080034 return cpumask_of(0);
Suresh Siddha2d9579a2008-07-10 11:16:59 -070035}
36
Mike Travisbcda0162008-12-16 17:33:59 -080037static void x2apic_vector_allocation_domain(int cpu, struct cpumask *retmask)
Suresh Siddha2d9579a2008-07-10 11:16:59 -070038{
Mike Travisbcda0162008-12-16 17:33:59 -080039 cpumask_clear(retmask);
40 cpumask_set_cpu(cpu, retmask);
Suresh Siddha2d9579a2008-07-10 11:16:59 -070041}
42
43static void __x2apic_send_IPI_dest(unsigned int apicid, int vector,
44 unsigned int dest)
45{
46 unsigned long cfg;
47
48 cfg = __prepare_ICR(0, vector, dest);
49
50 /*
51 * send the IPI.
52 */
53 x2apic_icr_write(cfg, apicid);
54}
55
Mike Travisbcda0162008-12-16 17:33:59 -080056static void x2apic_send_IPI_mask(const struct cpumask *mask, int vector)
Suresh Siddha2d9579a2008-07-10 11:16:59 -070057{
58 unsigned long flags;
59 unsigned long query_cpu;
60
61 local_irq_save(flags);
Mike Travisbcda0162008-12-16 17:33:59 -080062 for_each_cpu(query_cpu, mask) {
Suresh Siddha2d9579a2008-07-10 11:16:59 -070063 __x2apic_send_IPI_dest(per_cpu(x86_cpu_to_apicid, query_cpu),
64 vector, APIC_DEST_PHYSICAL);
65 }
66 local_irq_restore(flags);
67}
68
Mike Travisbcda0162008-12-16 17:33:59 -080069static void x2apic_send_IPI_mask_allbutself(const struct cpumask *mask,
70 int vector)
Mike Travise7986732008-12-16 17:33:52 -080071{
72 unsigned long flags;
73 unsigned long query_cpu;
74 unsigned long this_cpu = smp_processor_id();
75
76 local_irq_save(flags);
Mike Travisbcda0162008-12-16 17:33:59 -080077 for_each_cpu(query_cpu, mask) {
Mike Travise7986732008-12-16 17:33:52 -080078 if (query_cpu != this_cpu)
79 __x2apic_send_IPI_dest(
80 per_cpu(x86_cpu_to_apicid, query_cpu),
81 vector, APIC_DEST_PHYSICAL);
82 }
83 local_irq_restore(flags);
84}
85
Suresh Siddha2d9579a2008-07-10 11:16:59 -070086static void x2apic_send_IPI_allbutself(int vector)
87{
Mike Travise7986732008-12-16 17:33:52 -080088 unsigned long flags;
89 unsigned long query_cpu;
90 unsigned long this_cpu = smp_processor_id();
Suresh Siddha2d9579a2008-07-10 11:16:59 -070091
Mike Travise7986732008-12-16 17:33:52 -080092 local_irq_save(flags);
93 for_each_online_cpu(query_cpu)
94 if (query_cpu != this_cpu)
95 __x2apic_send_IPI_dest(
96 per_cpu(x86_cpu_to_apicid, query_cpu),
97 vector, APIC_DEST_PHYSICAL);
98 local_irq_restore(flags);
Suresh Siddha2d9579a2008-07-10 11:16:59 -070099}
100
101static void x2apic_send_IPI_all(int vector)
102{
Mike Travisbcda0162008-12-16 17:33:59 -0800103 x2apic_send_IPI_mask(cpu_online_mask, vector);
Suresh Siddha2d9579a2008-07-10 11:16:59 -0700104}
105
106static int x2apic_apic_id_registered(void)
107{
108 return 1;
109}
110
Mike Travisbcda0162008-12-16 17:33:59 -0800111static unsigned int x2apic_cpu_mask_to_apicid(const struct cpumask *cpumask)
Suresh Siddha2d9579a2008-07-10 11:16:59 -0700112{
113 int cpu;
114
115 /*
116 * We're using fixed IRQ delivery, can only return one phys APIC ID.
117 * May as well be the first.
118 */
Mike Travisbcda0162008-12-16 17:33:59 -0800119 cpu = cpumask_first(cpumask);
Mike Travise7986732008-12-16 17:33:52 -0800120 if ((unsigned)cpu < nr_cpu_ids)
Suresh Siddha2d9579a2008-07-10 11:16:59 -0700121 return per_cpu(x86_cpu_to_apicid, cpu);
122 else
123 return BAD_APICID;
124}
125
Mike Travis6eeb7c52008-12-16 17:33:55 -0800126static unsigned int x2apic_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
127 const struct cpumask *andmask)
Mike Travis95d313c2008-12-16 17:33:54 -0800128{
129 int cpu;
130
131 /*
132 * We're using fixed IRQ delivery, can only return one phys APIC ID.
133 * May as well be the first.
134 */
Mike Travisa775a382008-12-17 15:21:39 -0800135 for_each_cpu_and(cpu, cpumask, andmask)
136 if (cpumask_test_cpu(cpu, cpu_online_mask))
137 break;
Mike Travis6eeb7c52008-12-16 17:33:55 -0800138 if (cpu < nr_cpu_ids)
139 return per_cpu(x86_cpu_to_apicid, cpu);
Mike Travis95d313c2008-12-16 17:33:54 -0800140 return BAD_APICID;
141}
142
Yinghai Luf910a9d2008-07-12 01:01:20 -0700143static unsigned int get_apic_id(unsigned long x)
144{
145 unsigned int id;
146
147 id = x;
148 return id;
149}
150
151static unsigned long set_apic_id(unsigned int id)
152{
153 unsigned long x;
154
155 x = id;
156 return x;
157}
158
Suresh Siddha2d9579a2008-07-10 11:16:59 -0700159static unsigned int phys_pkg_id(int index_msb)
160{
Suresh Siddhae17941b2008-08-23 17:47:11 +0200161 return current_cpu_data.initial_apicid >> index_msb;
Suresh Siddha2d9579a2008-07-10 11:16:59 -0700162}
163
164void x2apic_send_IPI_self(int vector)
165{
166 apic_write(APIC_SELF_IPI, vector);
167}
168
169void init_x2apic_ldr(void)
170{
171 return;
172}
173
174struct genapic apic_x2apic_phys = {
175 .name = "physical x2apic",
Yinghai Lu1b9b89e2008-07-21 22:08:21 -0700176 .acpi_madt_oem_check = x2apic_acpi_madt_oem_check,
Suresh Siddha2d9579a2008-07-10 11:16:59 -0700177 .int_delivery_mode = dest_Fixed,
178 .int_dest_mode = (APIC_DEST_PHYSICAL != 0),
179 .target_cpus = x2apic_target_cpus,
180 .vector_allocation_domain = x2apic_vector_allocation_domain,
181 .apic_id_registered = x2apic_apic_id_registered,
182 .init_apic_ldr = init_x2apic_ldr,
183 .send_IPI_all = x2apic_send_IPI_all,
184 .send_IPI_allbutself = x2apic_send_IPI_allbutself,
185 .send_IPI_mask = x2apic_send_IPI_mask,
Mike Travise7986732008-12-16 17:33:52 -0800186 .send_IPI_mask_allbutself = x2apic_send_IPI_mask_allbutself,
Suresh Siddha2d9579a2008-07-10 11:16:59 -0700187 .send_IPI_self = x2apic_send_IPI_self,
188 .cpu_mask_to_apicid = x2apic_cpu_mask_to_apicid,
Mike Travis95d313c2008-12-16 17:33:54 -0800189 .cpu_mask_to_apicid_and = x2apic_cpu_mask_to_apicid_and,
Suresh Siddha2d9579a2008-07-10 11:16:59 -0700190 .phys_pkg_id = phys_pkg_id,
Yinghai Luf910a9d2008-07-12 01:01:20 -0700191 .get_apic_id = get_apic_id,
192 .set_apic_id = set_apic_id,
193 .apic_id_mask = (0xFFFFFFFFu),
Suresh Siddha2d9579a2008-07-10 11:16:59 -0700194};