blob: 6ef49e338a731c24e8cac9f3ba46271226762087 [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>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010010#include <asm/apic.h>
Yinghai Luc1eeb2d2009-02-16 23:02:14 -080011#include <asm/ipi.h>
Suresh Siddha2d9579a2008-07-10 11:16:59 -070012
Suresh Siddhaef1f87a2009-02-21 14:23:21 -080013int x2apic_phys;
Yinghai Lu1b9b89e2008-07-21 22:08:21 -070014
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{
Suresh Siddhaef1f87a2009-02-21 14:23:21 -080024 if (x2apic_phys)
25 return x2apic_enabled();
26 else
27 return 0;
Yinghai Lu1b9b89e2008-07-21 22:08:21 -070028}
Suresh Siddha2d9579a2008-07-10 11:16:59 -070029
Yinghai Lu087d7e52009-08-04 08:59:59 -070030/*
31 * need to use more than cpu 0, because we need more vectors when
32 * MSI-X are used.
33 */
Mike Travisbcda0162008-12-16 17:33:59 -080034static const struct cpumask *x2apic_target_cpus(void)
Suresh Siddha2d9579a2008-07-10 11:16:59 -070035{
Yinghai Lu087d7e52009-08-04 08:59:59 -070036 return cpu_online_mask;
Suresh Siddha2d9579a2008-07-10 11:16:59 -070037}
38
Mike Travisbcda0162008-12-16 17:33:59 -080039static void x2apic_vector_allocation_domain(int cpu, struct cpumask *retmask)
Suresh Siddha2d9579a2008-07-10 11:16:59 -070040{
Mike Travisbcda0162008-12-16 17:33:59 -080041 cpumask_clear(retmask);
42 cpumask_set_cpu(cpu, retmask);
Suresh Siddha2d9579a2008-07-10 11:16:59 -070043}
44
45static void __x2apic_send_IPI_dest(unsigned int apicid, int vector,
46 unsigned int dest)
47{
48 unsigned long cfg;
49
50 cfg = __prepare_ICR(0, vector, dest);
51
52 /*
53 * send the IPI.
54 */
Yinghai Luc1eeb2d2009-02-16 23:02:14 -080055 native_x2apic_icr_write(cfg, apicid);
Suresh Siddha2d9579a2008-07-10 11:16:59 -070056}
57
Ingo Molnardac5f412009-01-28 15:42:24 +010058static void
Suresh Siddhaa27d0b52011-05-19 16:45:47 -070059__x2apic_send_IPI_mask(const struct cpumask *mask, int vector, int apic_dest)
Mike Travise7986732008-12-16 17:33:52 -080060{
Ingo Molnardac5f412009-01-28 15:42:24 +010061 unsigned long query_cpu;
Suresh Siddhaa27d0b52011-05-19 16:45:47 -070062 unsigned long this_cpu;
Ingo Molnardac5f412009-01-28 15:42:24 +010063 unsigned long flags;
Mike Travise7986732008-12-16 17:33:52 -080064
Suresh Siddhace4e2402009-03-17 10:16:54 -080065 x2apic_wrmsr_fence();
66
Mike Travise7986732008-12-16 17:33:52 -080067 local_irq_save(flags);
Suresh Siddhaa27d0b52011-05-19 16:45:47 -070068
69 this_cpu = smp_processor_id();
Mike Travisbcda0162008-12-16 17:33:59 -080070 for_each_cpu(query_cpu, mask) {
Suresh Siddhaa27d0b52011-05-19 16:45:47 -070071 if (apic_dest == APIC_DEST_ALLBUT && this_cpu == query_cpu)
Ingo Molnardac5f412009-01-28 15:42:24 +010072 continue;
73 __x2apic_send_IPI_dest(per_cpu(x86_cpu_to_apicid, query_cpu),
74 vector, APIC_DEST_PHYSICAL);
75 }
Mike Travise7986732008-12-16 17:33:52 -080076 local_irq_restore(flags);
Suresh Siddha2d9579a2008-07-10 11:16:59 -070077}
78
Suresh Siddhaa27d0b52011-05-19 16:45:47 -070079static void x2apic_send_IPI_mask(const struct cpumask *mask, int vector)
80{
81 __x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLINC);
82}
83
84static void
85 x2apic_send_IPI_mask_allbutself(const struct cpumask *mask, int vector)
86{
87 __x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLBUT);
88}
89
90static void x2apic_send_IPI_allbutself(int vector)
91{
92 __x2apic_send_IPI_mask(cpu_online_mask, vector, APIC_DEST_ALLBUT);
93}
94
Suresh Siddha2d9579a2008-07-10 11:16:59 -070095static void x2apic_send_IPI_all(int vector)
96{
Suresh Siddhaa27d0b52011-05-19 16:45:47 -070097 __x2apic_send_IPI_mask(cpu_online_mask, vector, APIC_DEST_ALLINC);
Suresh Siddha2d9579a2008-07-10 11:16:59 -070098}
99
100static int x2apic_apic_id_registered(void)
101{
102 return 1;
103}
104
Mike Travisbcda0162008-12-16 17:33:59 -0800105static unsigned int x2apic_cpu_mask_to_apicid(const struct cpumask *cpumask)
Suresh Siddha2d9579a2008-07-10 11:16:59 -0700106{
Suresh Siddha2d9579a2008-07-10 11:16:59 -0700107 /*
108 * We're using fixed IRQ delivery, can only return one phys APIC ID.
109 * May as well be the first.
110 */
Ingo Molnardebccb32009-01-28 15:20:18 +0100111 int cpu = cpumask_first(cpumask);
112
Mike Travise7986732008-12-16 17:33:52 -0800113 if ((unsigned)cpu < nr_cpu_ids)
Suresh Siddha2d9579a2008-07-10 11:16:59 -0700114 return per_cpu(x86_cpu_to_apicid, cpu);
115 else
116 return BAD_APICID;
117}
118
Ingo Molnardebccb32009-01-28 15:20:18 +0100119static unsigned int
120x2apic_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
121 const struct cpumask *andmask)
Mike Travis95d313c2008-12-16 17:33:54 -0800122{
123 int cpu;
124
125 /*
126 * We're using fixed IRQ delivery, can only return one phys APIC ID.
127 * May as well be the first.
128 */
Ingo Molnardebccb32009-01-28 15:20:18 +0100129 for_each_cpu_and(cpu, cpumask, andmask) {
Mike Travisa775a382008-12-17 15:21:39 -0800130 if (cpumask_test_cpu(cpu, cpu_online_mask))
131 break;
Ingo Molnardebccb32009-01-28 15:20:18 +0100132 }
133
Suresh Siddha18374d82009-12-17 18:29:46 -0800134 return per_cpu(x86_cpu_to_apicid, cpu);
Mike Travis95d313c2008-12-16 17:33:54 -0800135}
136
Ingo Molnarca6c8ed2009-01-28 14:08:38 +0100137static unsigned int x2apic_phys_get_apic_id(unsigned long x)
Yinghai Luf910a9d2008-07-12 01:01:20 -0700138{
Ingo Molnardac5f412009-01-28 15:42:24 +0100139 return x;
Yinghai Luf910a9d2008-07-12 01:01:20 -0700140}
141
142static unsigned long set_apic_id(unsigned int id)
143{
Ingo Molnardac5f412009-01-28 15:42:24 +0100144 return id;
Yinghai Luf910a9d2008-07-12 01:01:20 -0700145}
146
Ingo Molnard4c9a9f2009-01-28 13:31:22 +0100147static int x2apic_phys_pkg_id(int initial_apicid, int index_msb)
Suresh Siddha2d9579a2008-07-10 11:16:59 -0700148{
Yinghai Lud8c7eb32009-07-25 03:23:09 -0700149 return initial_apicid >> index_msb;
Suresh Siddha2d9579a2008-07-10 11:16:59 -0700150}
151
Jaswinder Singh Rajput4d08d972008-12-29 22:11:40 +0530152static void x2apic_send_IPI_self(int vector)
Suresh Siddha2d9579a2008-07-10 11:16:59 -0700153{
154 apic_write(APIC_SELF_IPI, vector);
155}
156
Jaswinder Singh Rajput4d08d972008-12-29 22:11:40 +0530157static void init_x2apic_ldr(void)
Suresh Siddha2d9579a2008-07-10 11:16:59 -0700158{
Suresh Siddha2d9579a2008-07-10 11:16:59 -0700159}
160
Suresh Siddha9ebd6802011-05-19 16:45:46 -0700161static int x2apic_phys_probe(void)
162{
163 if (x2apic_mode && x2apic_phys)
164 return 1;
165
166 return apic == &apic_x2apic_phys;
167}
168
Ingo Molnarbe163a12009-02-17 16:28:46 +0100169struct apic apic_x2apic_phys = {
Ingo Molnar05c155c2009-01-28 02:37:01 +0100170
171 .name = "physical x2apic",
Suresh Siddha9ebd6802011-05-19 16:45:46 -0700172 .probe = x2apic_phys_probe,
Ingo Molnar05c155c2009-01-28 02:37:01 +0100173 .acpi_madt_oem_check = x2apic_acpi_madt_oem_check,
174 .apic_id_registered = x2apic_apic_id_registered,
175
Ingo Molnarf8987a12009-01-28 04:02:31 +0100176 .irq_delivery_mode = dest_Fixed,
Ingo Molnar0b06e732009-01-28 05:13:04 +0100177 .irq_dest_mode = 0, /* physical */
Ingo Molnar05c155c2009-01-28 02:37:01 +0100178
179 .target_cpus = x2apic_target_cpus,
Ingo Molnar08125d32009-01-28 05:08:44 +0100180 .disable_esr = 0,
Ingo Molnarbdb1a9b2009-01-28 05:29:25 +0100181 .dest_logical = 0,
Ingo Molnar05c155c2009-01-28 02:37:01 +0100182 .check_apicid_used = NULL,
183 .check_apicid_present = NULL,
184
Ingo Molnar05c155c2009-01-28 02:37:01 +0100185 .vector_allocation_domain = x2apic_vector_allocation_domain,
186 .init_apic_ldr = init_x2apic_ldr,
187
188 .ioapic_phys_id_map = NULL,
189 .setup_apic_routing = NULL,
190 .multi_timer_check = NULL,
Ingo Molnara21769a42009-01-28 06:50:47 +0100191 .cpu_present_to_apicid = default_cpu_present_to_apicid,
Ingo Molnar05c155c2009-01-28 02:37:01 +0100192 .apicid_to_cpu_present = NULL,
193 .setup_portio_remap = NULL,
Ingo Molnara27a6212009-01-28 12:43:18 +0100194 .check_phys_apicid_present = default_check_phys_apicid_present,
Ingo Molnar05c155c2009-01-28 02:37:01 +0100195 .enable_apic_mode = NULL,
Ingo Molnard4c9a9f2009-01-28 13:31:22 +0100196 .phys_pkg_id = x2apic_phys_pkg_id,
Ingo Molnar05c155c2009-01-28 02:37:01 +0100197 .mps_oem_check = NULL,
198
Ingo Molnarca6c8ed2009-01-28 14:08:38 +0100199 .get_apic_id = x2apic_phys_get_apic_id,
Ingo Molnar05c155c2009-01-28 02:37:01 +0100200 .set_apic_id = set_apic_id,
201 .apic_id_mask = 0xFFFFFFFFu,
202
203 .cpu_mask_to_apicid = x2apic_cpu_mask_to_apicid,
204 .cpu_mask_to_apicid_and = x2apic_cpu_mask_to_apicid_and,
205
206 .send_IPI_mask = x2apic_send_IPI_mask,
207 .send_IPI_mask_allbutself = x2apic_send_IPI_mask_allbutself,
208 .send_IPI_allbutself = x2apic_send_IPI_allbutself,
209 .send_IPI_all = x2apic_send_IPI_all,
210 .send_IPI_self = x2apic_send_IPI_self,
211
Ingo Molnarabfa5842009-01-28 16:15:16 +0100212 .trampoline_phys_low = DEFAULT_TRAMPOLINE_PHYS_LOW,
213 .trampoline_phys_high = DEFAULT_TRAMPOLINE_PHYS_HIGH,
Ingo Molnar05c155c2009-01-28 02:37:01 +0100214 .wait_for_init_deassert = NULL,
215 .smp_callin_clear_local_apic = NULL,
Ingo Molnar05c155c2009-01-28 02:37:01 +0100216 .inquire_remote_apic = NULL,
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800217
218 .read = native_apic_msr_read,
219 .write = native_apic_msr_write,
220 .icr_read = native_x2apic_icr_read,
221 .icr_write = native_x2apic_icr_write,
222 .wait_icr_idle = native_x2apic_wait_icr_idle,
223 .safe_wait_icr_idle = native_safe_x2apic_wait_icr_idle,
Suresh Siddha2d9579a2008-07-10 11:16:59 -0700224};