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> |
| 10 | #include <asm/ipi.h> |
| 11 | #include <asm/genapic.h> |
| 12 | |
Yinghai Lu | 1b9b89e | 2008-07-21 22:08:21 -0700 | [diff] [blame] | 13 | static int x2apic_phys; |
| 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 | { |
Yinghai Lu | d25ae38 | 2008-07-25 19:39:03 -0700 | [diff] [blame] | 24 | if (cpu_has_x2apic && x2apic_phys) |
Yinghai Lu | 1b9b89e | 2008-07-21 22:08:21 -0700 | [diff] [blame] | 25 | return 1; |
| 26 | |
| 27 | return 0; |
| 28 | } |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 29 | |
| 30 | /* Start with all IRQs pointing to boot CPU. IRQ balancing will shift them. */ |
| 31 | |
Mike Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 32 | static const struct cpumask *x2apic_target_cpus(void) |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 33 | { |
Mike Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 34 | return cpumask_of(0); |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Mike Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 37 | static void x2apic_vector_allocation_domain(int cpu, struct cpumask *retmask) |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 38 | { |
Mike Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 39 | cpumask_clear(retmask); |
| 40 | cpumask_set_cpu(cpu, retmask); |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static 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 Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 56 | static void x2apic_send_IPI_mask(const struct cpumask *mask, int vector) |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 57 | { |
| 58 | unsigned long flags; |
| 59 | unsigned long query_cpu; |
| 60 | |
| 61 | local_irq_save(flags); |
Mike Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 62 | for_each_cpu(query_cpu, mask) { |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 63 | __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 Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 69 | static void x2apic_send_IPI_mask_allbutself(const struct cpumask *mask, |
| 70 | int vector) |
Mike Travis | e798673 | 2008-12-16 17:33:52 -0800 | [diff] [blame] | 71 | { |
| 72 | unsigned long flags; |
| 73 | unsigned long query_cpu; |
| 74 | unsigned long this_cpu = smp_processor_id(); |
| 75 | |
| 76 | local_irq_save(flags); |
Mike Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 77 | for_each_cpu(query_cpu, mask) { |
Mike Travis | e798673 | 2008-12-16 17:33:52 -0800 | [diff] [blame] | 78 | 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 Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 86 | static void x2apic_send_IPI_allbutself(int vector) |
| 87 | { |
Mike Travis | e798673 | 2008-12-16 17:33:52 -0800 | [diff] [blame] | 88 | unsigned long flags; |
| 89 | unsigned long query_cpu; |
| 90 | unsigned long this_cpu = smp_processor_id(); |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 91 | |
Mike Travis | e798673 | 2008-12-16 17:33:52 -0800 | [diff] [blame] | 92 | 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 Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static void x2apic_send_IPI_all(int vector) |
| 102 | { |
Mike Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 103 | x2apic_send_IPI_mask(cpu_online_mask, vector); |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static int x2apic_apic_id_registered(void) |
| 107 | { |
| 108 | return 1; |
| 109 | } |
| 110 | |
Mike Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 111 | static unsigned int x2apic_cpu_mask_to_apicid(const struct cpumask *cpumask) |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 112 | { |
| 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 Travis | bcda016 | 2008-12-16 17:33:59 -0800 | [diff] [blame] | 119 | cpu = cpumask_first(cpumask); |
Mike Travis | e798673 | 2008-12-16 17:33:52 -0800 | [diff] [blame] | 120 | if ((unsigned)cpu < nr_cpu_ids) |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 121 | return per_cpu(x86_cpu_to_apicid, cpu); |
| 122 | else |
| 123 | return BAD_APICID; |
| 124 | } |
| 125 | |
Mike Travis | 6eeb7c5 | 2008-12-16 17:33:55 -0800 | [diff] [blame] | 126 | static unsigned int x2apic_cpu_mask_to_apicid_and(const struct cpumask *cpumask, |
| 127 | const struct cpumask *andmask) |
Mike Travis | 95d313c | 2008-12-16 17:33:54 -0800 | [diff] [blame] | 128 | { |
| 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 Travis | a775a38 | 2008-12-17 15:21:39 -0800 | [diff] [blame^] | 135 | for_each_cpu_and(cpu, cpumask, andmask) |
| 136 | if (cpumask_test_cpu(cpu, cpu_online_mask)) |
| 137 | break; |
Mike Travis | 6eeb7c5 | 2008-12-16 17:33:55 -0800 | [diff] [blame] | 138 | if (cpu < nr_cpu_ids) |
| 139 | return per_cpu(x86_cpu_to_apicid, cpu); |
Mike Travis | 95d313c | 2008-12-16 17:33:54 -0800 | [diff] [blame] | 140 | return BAD_APICID; |
| 141 | } |
| 142 | |
Yinghai Lu | f910a9d | 2008-07-12 01:01:20 -0700 | [diff] [blame] | 143 | static unsigned int get_apic_id(unsigned long x) |
| 144 | { |
| 145 | unsigned int id; |
| 146 | |
| 147 | id = x; |
| 148 | return id; |
| 149 | } |
| 150 | |
| 151 | static unsigned long set_apic_id(unsigned int id) |
| 152 | { |
| 153 | unsigned long x; |
| 154 | |
| 155 | x = id; |
| 156 | return x; |
| 157 | } |
| 158 | |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 159 | static unsigned int phys_pkg_id(int index_msb) |
| 160 | { |
Suresh Siddha | e17941b | 2008-08-23 17:47:11 +0200 | [diff] [blame] | 161 | return current_cpu_data.initial_apicid >> index_msb; |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void x2apic_send_IPI_self(int vector) |
| 165 | { |
| 166 | apic_write(APIC_SELF_IPI, vector); |
| 167 | } |
| 168 | |
| 169 | void init_x2apic_ldr(void) |
| 170 | { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | struct genapic apic_x2apic_phys = { |
| 175 | .name = "physical x2apic", |
Yinghai Lu | 1b9b89e | 2008-07-21 22:08:21 -0700 | [diff] [blame] | 176 | .acpi_madt_oem_check = x2apic_acpi_madt_oem_check, |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 177 | .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 Travis | e798673 | 2008-12-16 17:33:52 -0800 | [diff] [blame] | 186 | .send_IPI_mask_allbutself = x2apic_send_IPI_mask_allbutself, |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 187 | .send_IPI_self = x2apic_send_IPI_self, |
| 188 | .cpu_mask_to_apicid = x2apic_cpu_mask_to_apicid, |
Mike Travis | 95d313c | 2008-12-16 17:33:54 -0800 | [diff] [blame] | 189 | .cpu_mask_to_apicid_and = x2apic_cpu_mask_to_apicid_and, |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 190 | .phys_pkg_id = phys_pkg_id, |
Yinghai Lu | f910a9d | 2008-07-12 01:01:20 -0700 | [diff] [blame] | 191 | .get_apic_id = get_apic_id, |
| 192 | .set_apic_id = set_apic_id, |
| 193 | .apic_id_mask = (0xFFFFFFFFu), |
Suresh Siddha | 2d9579a | 2008-07-10 11:16:59 -0700 | [diff] [blame] | 194 | }; |