Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 1 | #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 Lu | 1b9b89e | 2008-07-21 22:08:21 -0700 | [diff] [blame] | 7 | #include <linux/dmar.h> |
| 8 | |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 9 | #include <asm/smp.h> |
Ingo Molnar | 7b6aa33 | 2009-02-17 13:58:15 +0100 | [diff] [blame] | 10 | #include <asm/apic.h> |
Yinghai Lu | c1eeb2d | 2009-02-16 23:02:14 -0800 | [diff] [blame] | 11 | #include <asm/ipi.h> |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 12 | |
Suresh Siddha | ef1f87a | 2009-02-21 14:23:21 -0800 | [diff] [blame] | 13 | int x2apic_phys; |
Yinghai Lu | 1b9b89e | 2008-07-21 22:08:21 -0700 | [diff] [blame] | 14 | |
| 15 | static int set_x2apic_phys_mode(char *arg) |
| 16 | { |
| 17 | x2apic_phys = 1; |
| 18 | return 0; |
| 19 | } |
| 20 | early_param("x2apic_phys", set_x2apic_phys_mode); |
| 21 | |
Marcin Slusarz | 3cfba08 | 2008-10-12 11:46:23 +0200 | [diff] [blame] | 22 | static int x2apic_acpi_madt_oem_check(char *oem_id, char *oem_table_id) |
Yinghai Lu | 1b9b89e | 2008-07-21 22:08:21 -0700 | [diff] [blame] | 23 | { |
Suresh Siddha | ef1f87a | 2009-02-21 14:23:21 -0800 | [diff] [blame] | 24 | if (x2apic_phys) |
| 25 | return x2apic_enabled(); |
| 26 | else |
| 27 | return 0; |
Yinghai Lu | 1b9b89e | 2008-07-21 22:08:21 -0700 | [diff] [blame] | 28 | } |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 29 | |
Yinghai Lu | 087d7e5 | 2009-08-04 08:59:59 -0700 | [diff] [blame] | 30 | /* |
| 31 | * need to use more than cpu 0, because we need more vectors when |
| 32 | * MSI-X are used. |
| 33 | */ |
Mike Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 34 | static const struct cpumask *x2apic_target_cpus(void) |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 35 | { |
Yinghai Lu | 087d7e5 | 2009-08-04 08:59:59 -0700 | [diff] [blame] | 36 | return cpu_online_mask; |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Mike Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 39 | static void x2apic_vector_allocation_domain(int cpu, struct cpumask *retmask) |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 40 | { |
Mike Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 41 | cpumask_clear(retmask); |
| 42 | cpumask_set_cpu(cpu, retmask); |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static 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 Lu | c1eeb2d | 2009-02-16 23:02:14 -0800 | [diff] [blame] | 55 | native_x2apic_icr_write(cfg, apicid); |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Ingo Molnar | dac5f41 | 2009-01-28 15:42:24 +0100 | [diff] [blame] | 58 | static void |
Suresh Siddha | a27d0b5 | 2011-05-19 16:45:47 -0700 | [diff] [blame^] | 59 | __x2apic_send_IPI_mask(const struct cpumask *mask, int vector, int apic_dest) |
Mike Travis | e798673 | 2008-12-16 17:33:52 -0800 | [diff] [blame] | 60 | { |
Ingo Molnar | dac5f41 | 2009-01-28 15:42:24 +0100 | [diff] [blame] | 61 | unsigned long query_cpu; |
Suresh Siddha | a27d0b5 | 2011-05-19 16:45:47 -0700 | [diff] [blame^] | 62 | unsigned long this_cpu; |
Ingo Molnar | dac5f41 | 2009-01-28 15:42:24 +0100 | [diff] [blame] | 63 | unsigned long flags; |
Mike Travis | e798673 | 2008-12-16 17:33:52 -0800 | [diff] [blame] | 64 | |
Suresh Siddha | ce4e240 | 2009-03-17 10:16:54 -0800 | [diff] [blame] | 65 | x2apic_wrmsr_fence(); |
| 66 | |
Mike Travis | e798673 | 2008-12-16 17:33:52 -0800 | [diff] [blame] | 67 | local_irq_save(flags); |
Suresh Siddha | a27d0b5 | 2011-05-19 16:45:47 -0700 | [diff] [blame^] | 68 | |
| 69 | this_cpu = smp_processor_id(); |
Mike Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 70 | for_each_cpu(query_cpu, mask) { |
Suresh Siddha | a27d0b5 | 2011-05-19 16:45:47 -0700 | [diff] [blame^] | 71 | if (apic_dest == APIC_DEST_ALLBUT && this_cpu == query_cpu) |
Ingo Molnar | dac5f41 | 2009-01-28 15:42:24 +0100 | [diff] [blame] | 72 | continue; |
| 73 | __x2apic_send_IPI_dest(per_cpu(x86_cpu_to_apicid, query_cpu), |
| 74 | vector, APIC_DEST_PHYSICAL); |
| 75 | } |
Mike Travis | e798673 | 2008-12-16 17:33:52 -0800 | [diff] [blame] | 76 | local_irq_restore(flags); |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Suresh Siddha | a27d0b5 | 2011-05-19 16:45:47 -0700 | [diff] [blame^] | 79 | static void x2apic_send_IPI_mask(const struct cpumask *mask, int vector) |
| 80 | { |
| 81 | __x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLINC); |
| 82 | } |
| 83 | |
| 84 | static 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 | |
| 90 | static void x2apic_send_IPI_allbutself(int vector) |
| 91 | { |
| 92 | __x2apic_send_IPI_mask(cpu_online_mask, vector, APIC_DEST_ALLBUT); |
| 93 | } |
| 94 | |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 95 | static void x2apic_send_IPI_all(int vector) |
| 96 | { |
Suresh Siddha | a27d0b5 | 2011-05-19 16:45:47 -0700 | [diff] [blame^] | 97 | __x2apic_send_IPI_mask(cpu_online_mask, vector, APIC_DEST_ALLINC); |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static int x2apic_apic_id_registered(void) |
| 101 | { |
| 102 | return 1; |
| 103 | } |
| 104 | |
Mike Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 105 | static unsigned int x2apic_cpu_mask_to_apicid(const struct cpumask *cpumask) |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 106 | { |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 107 | /* |
| 108 | * We're using fixed IRQ delivery, can only return one phys APIC ID. |
| 109 | * May as well be the first. |
| 110 | */ |
Ingo Molnar | debccb3 | 2009-01-28 15:20:18 +0100 | [diff] [blame] | 111 | int cpu = cpumask_first(cpumask); |
| 112 | |
Mike Travis | e798673 | 2008-12-16 17:33:52 -0800 | [diff] [blame] | 113 | if ((unsigned)cpu < nr_cpu_ids) |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 114 | return per_cpu(x86_cpu_to_apicid, cpu); |
| 115 | else |
| 116 | return BAD_APICID; |
| 117 | } |
| 118 | |
Ingo Molnar | debccb3 | 2009-01-28 15:20:18 +0100 | [diff] [blame] | 119 | static unsigned int |
| 120 | x2apic_cpu_mask_to_apicid_and(const struct cpumask *cpumask, |
| 121 | const struct cpumask *andmask) |
Mike Travis | 95d313c | 2008-12-16 17:33:54 -0800 | [diff] [blame] | 122 | { |
| 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 Molnar | debccb3 | 2009-01-28 15:20:18 +0100 | [diff] [blame] | 129 | for_each_cpu_and(cpu, cpumask, andmask) { |
Mike Travis | a775a38 | 2008-12-17 15:21:39 -0800 | [diff] [blame] | 130 | if (cpumask_test_cpu(cpu, cpu_online_mask)) |
| 131 | break; |
Ingo Molnar | debccb3 | 2009-01-28 15:20:18 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Suresh Siddha | 18374d8 | 2009-12-17 18:29:46 -0800 | [diff] [blame] | 134 | return per_cpu(x86_cpu_to_apicid, cpu); |
Mike Travis | 95d313c | 2008-12-16 17:33:54 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Ingo Molnar | ca6c8ed | 2009-01-28 14:08:38 +0100 | [diff] [blame] | 137 | static unsigned int x2apic_phys_get_apic_id(unsigned long x) |
Yinghai Lu | f910a9d | 2008-07-12 01:01:20 -0700 | [diff] [blame] | 138 | { |
Ingo Molnar | dac5f41 | 2009-01-28 15:42:24 +0100 | [diff] [blame] | 139 | return x; |
Yinghai Lu | f910a9d | 2008-07-12 01:01:20 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static unsigned long set_apic_id(unsigned int id) |
| 143 | { |
Ingo Molnar | dac5f41 | 2009-01-28 15:42:24 +0100 | [diff] [blame] | 144 | return id; |
Yinghai Lu | f910a9d | 2008-07-12 01:01:20 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Ingo Molnar | d4c9a9f | 2009-01-28 13:31:22 +0100 | [diff] [blame] | 147 | static int x2apic_phys_pkg_id(int initial_apicid, int index_msb) |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 148 | { |
Yinghai Lu | d8c7eb3 | 2009-07-25 03:23:09 -0700 | [diff] [blame] | 149 | return initial_apicid >> index_msb; |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Jaswinder Singh Rajput | 4d08d97 | 2008-12-29 22:11:40 +0530 | [diff] [blame] | 152 | static void x2apic_send_IPI_self(int vector) |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 153 | { |
| 154 | apic_write(APIC_SELF_IPI, vector); |
| 155 | } |
| 156 | |
Jaswinder Singh Rajput | 4d08d97 | 2008-12-29 22:11:40 +0530 | [diff] [blame] | 157 | static void init_x2apic_ldr(void) |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 158 | { |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Suresh Siddha | 9ebd680 | 2011-05-19 16:45:46 -0700 | [diff] [blame] | 161 | static 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 Molnar | be163a1 | 2009-02-17 16:28:46 +0100 | [diff] [blame] | 169 | struct apic apic_x2apic_phys = { |
Ingo Molnar | 05c155c | 2009-01-28 02:37:01 +0100 | [diff] [blame] | 170 | |
| 171 | .name = "physical x2apic", |
Suresh Siddha | 9ebd680 | 2011-05-19 16:45:46 -0700 | [diff] [blame] | 172 | .probe = x2apic_phys_probe, |
Ingo Molnar | 05c155c | 2009-01-28 02:37:01 +0100 | [diff] [blame] | 173 | .acpi_madt_oem_check = x2apic_acpi_madt_oem_check, |
| 174 | .apic_id_registered = x2apic_apic_id_registered, |
| 175 | |
Ingo Molnar | f8987a1 | 2009-01-28 04:02:31 +0100 | [diff] [blame] | 176 | .irq_delivery_mode = dest_Fixed, |
Ingo Molnar | 0b06e73 | 2009-01-28 05:13:04 +0100 | [diff] [blame] | 177 | .irq_dest_mode = 0, /* physical */ |
Ingo Molnar | 05c155c | 2009-01-28 02:37:01 +0100 | [diff] [blame] | 178 | |
| 179 | .target_cpus = x2apic_target_cpus, |
Ingo Molnar | 08125d3 | 2009-01-28 05:08:44 +0100 | [diff] [blame] | 180 | .disable_esr = 0, |
Ingo Molnar | bdb1a9b | 2009-01-28 05:29:25 +0100 | [diff] [blame] | 181 | .dest_logical = 0, |
Ingo Molnar | 05c155c | 2009-01-28 02:37:01 +0100 | [diff] [blame] | 182 | .check_apicid_used = NULL, |
| 183 | .check_apicid_present = NULL, |
| 184 | |
Ingo Molnar | 05c155c | 2009-01-28 02:37:01 +0100 | [diff] [blame] | 185 | .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 Molnar | a21769a4 | 2009-01-28 06:50:47 +0100 | [diff] [blame] | 191 | .cpu_present_to_apicid = default_cpu_present_to_apicid, |
Ingo Molnar | 05c155c | 2009-01-28 02:37:01 +0100 | [diff] [blame] | 192 | .apicid_to_cpu_present = NULL, |
| 193 | .setup_portio_remap = NULL, |
Ingo Molnar | a27a621 | 2009-01-28 12:43:18 +0100 | [diff] [blame] | 194 | .check_phys_apicid_present = default_check_phys_apicid_present, |
Ingo Molnar | 05c155c | 2009-01-28 02:37:01 +0100 | [diff] [blame] | 195 | .enable_apic_mode = NULL, |
Ingo Molnar | d4c9a9f | 2009-01-28 13:31:22 +0100 | [diff] [blame] | 196 | .phys_pkg_id = x2apic_phys_pkg_id, |
Ingo Molnar | 05c155c | 2009-01-28 02:37:01 +0100 | [diff] [blame] | 197 | .mps_oem_check = NULL, |
| 198 | |
Ingo Molnar | ca6c8ed | 2009-01-28 14:08:38 +0100 | [diff] [blame] | 199 | .get_apic_id = x2apic_phys_get_apic_id, |
Ingo Molnar | 05c155c | 2009-01-28 02:37:01 +0100 | [diff] [blame] | 200 | .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 Molnar | abfa584 | 2009-01-28 16:15:16 +0100 | [diff] [blame] | 212 | .trampoline_phys_low = DEFAULT_TRAMPOLINE_PHYS_LOW, |
| 213 | .trampoline_phys_high = DEFAULT_TRAMPOLINE_PHYS_HIGH, |
Ingo Molnar | 05c155c | 2009-01-28 02:37:01 +0100 | [diff] [blame] | 214 | .wait_for_init_deassert = NULL, |
| 215 | .smp_callin_clear_local_apic = NULL, |
Ingo Molnar | 05c155c | 2009-01-28 02:37:01 +0100 | [diff] [blame] | 216 | .inquire_remote_apic = NULL, |
Yinghai Lu | c1eeb2d | 2009-02-16 23:02:14 -0800 | [diff] [blame] | 217 | |
| 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 Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 224 | }; |