Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 James Cleverdon, IBM. |
| 3 | * Subject to the GNU Public License, v.2 |
| 4 | * |
Andi Kleen | f8d3119 | 2005-07-28 21:15:42 -0700 | [diff] [blame] | 5 | * Flat APIC subarch code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 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 Kleen | f19cccf | 2007-05-02 19:27:21 +0200 | [diff] [blame] | 11 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #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> |
| 18 | #include <asm/smp.h> |
| 19 | #include <asm/ipi.h> |
Jan Beulich | 00f1ea6 | 2007-05-02 19:27:04 +0200 | [diff] [blame] | 20 | #include <asm/genapic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | static cpumask_t flat_target_cpus(void) |
| 23 | { |
| 24 | return cpu_online_map; |
| 25 | } |
| 26 | |
Eric W. Biederman | c7111c13 | 2006-10-08 07:47:55 -0600 | [diff] [blame] | 27 | static cpumask_t flat_vector_allocation_domain(int cpu) |
| 28 | { |
| 29 | /* Careful. Some cpus do not strictly honor the set of cpus |
| 30 | * specified in the interrupt destination when using lowest |
| 31 | * priority interrupt delivery mode. |
| 32 | * |
| 33 | * In particular there was a hyperthreading cpu observed to |
| 34 | * deliver interrupts to the wrong hyperthread when only one |
| 35 | * hyperthread was specified in the interrupt desitination. |
| 36 | */ |
| 37 | cpumask_t domain = { { [0] = APIC_ALL_CPUS, } }; |
| 38 | return domain; |
| 39 | } |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* |
| 42 | * Set up the logical destination ID. |
| 43 | * |
| 44 | * Intel recommends to set DFR, LDR and TPR before enabling |
| 45 | * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel |
| 46 | * document number 292116). So here it goes... |
| 47 | */ |
| 48 | static void flat_init_apic_ldr(void) |
| 49 | { |
| 50 | unsigned long val; |
| 51 | unsigned long num, id; |
| 52 | |
| 53 | num = smp_processor_id(); |
| 54 | id = 1UL << num; |
Andi Kleen | eddfb4e | 2005-09-12 18:49:23 +0200 | [diff] [blame] | 55 | apic_write(APIC_DFR, APIC_DFR_FLAT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | val = apic_read(APIC_LDR) & ~APIC_LDR_MASK; |
| 57 | val |= SET_APIC_LOGICAL_ID(id); |
Andi Kleen | eddfb4e | 2005-09-12 18:49:23 +0200 | [diff] [blame] | 58 | apic_write(APIC_LDR, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | static void flat_send_IPI_mask(cpumask_t cpumask, int vector) |
| 62 | { |
| 63 | unsigned long mask = cpus_addr(cpumask)[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | unsigned long flags; |
| 65 | |
Fernando Luis Vázquez Cao | 2b94ab2 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 66 | local_irq_save(flags); |
Fernando Luis [** ISO-8859-1 charset **] VázquezCao | 9062d88 | 2007-05-02 19:27:18 +0200 | [diff] [blame] | 67 | __send_IPI_dest_field(mask, vector, APIC_DEST_LOGICAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | local_irq_restore(flags); |
| 69 | } |
| 70 | |
Ashok Raj | 884d9e4 | 2005-06-25 14:55:02 -0700 | [diff] [blame] | 71 | static void flat_send_IPI_allbutself(int vector) |
| 72 | { |
Keith Owens | e77deac | 2006-06-26 13:59:56 +0200 | [diff] [blame] | 73 | #ifdef CONFIG_HOTPLUG_CPU |
| 74 | int hotplug = 1; |
Ashok Raj | fdf26d9 | 2005-09-09 13:01:52 -0700 | [diff] [blame] | 75 | #else |
Keith Owens | e77deac | 2006-06-26 13:59:56 +0200 | [diff] [blame] | 76 | int hotplug = 0; |
Ashok Raj | fdf26d9 | 2005-09-09 13:01:52 -0700 | [diff] [blame] | 77 | #endif |
Keith Owens | e77deac | 2006-06-26 13:59:56 +0200 | [diff] [blame] | 78 | if (hotplug || vector == NMI_VECTOR) { |
| 79 | cpumask_t allbutme = cpu_online_map; |
| 80 | |
| 81 | cpu_clear(smp_processor_id(), allbutme); |
| 82 | |
| 83 | if (!cpus_empty(allbutme)) |
| 84 | flat_send_IPI_mask(allbutme, vector); |
| 85 | } else if (num_online_cpus() > 1) { |
| 86 | __send_IPI_shortcut(APIC_DEST_ALLBUT, vector,APIC_DEST_LOGICAL); |
| 87 | } |
Ashok Raj | 884d9e4 | 2005-06-25 14:55:02 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static void flat_send_IPI_all(int vector) |
| 91 | { |
Keith Owens | e77deac | 2006-06-26 13:59:56 +0200 | [diff] [blame] | 92 | if (vector == NMI_VECTOR) |
| 93 | flat_send_IPI_mask(cpu_online_map, vector); |
| 94 | else |
| 95 | __send_IPI_shortcut(APIC_DEST_ALLINC, vector, APIC_DEST_LOGICAL); |
Ashok Raj | 884d9e4 | 2005-06-25 14:55:02 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | static int flat_apic_id_registered(void) |
| 99 | { |
| 100 | return physid_isset(GET_APIC_ID(apic_read(APIC_ID)), phys_cpu_present_map); |
| 101 | } |
| 102 | |
| 103 | static unsigned int flat_cpu_mask_to_apicid(cpumask_t cpumask) |
| 104 | { |
| 105 | return cpus_addr(cpumask)[0] & APIC_ALL_CPUS; |
| 106 | } |
| 107 | |
| 108 | static unsigned int phys_pkg_id(int index_msb) |
| 109 | { |
ravikiran thirumalai | 0f4fdb7 | 2006-06-26 13:56:04 +0200 | [diff] [blame] | 110 | return hard_smp_processor_id() >> index_msb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | struct genapic apic_flat = { |
| 114 | .name = "flat", |
| 115 | .int_delivery_mode = dest_LowestPrio, |
| 116 | .int_dest_mode = (APIC_DEST_LOGICAL != 0), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | .target_cpus = flat_target_cpus, |
Eric W. Biederman | c7111c13 | 2006-10-08 07:47:55 -0600 | [diff] [blame] | 118 | .vector_allocation_domain = flat_vector_allocation_domain, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | .apic_id_registered = flat_apic_id_registered, |
| 120 | .init_apic_ldr = flat_init_apic_ldr, |
| 121 | .send_IPI_all = flat_send_IPI_all, |
| 122 | .send_IPI_allbutself = flat_send_IPI_allbutself, |
| 123 | .send_IPI_mask = flat_send_IPI_mask, |
| 124 | .cpu_mask_to_apicid = flat_cpu_mask_to_apicid, |
| 125 | .phys_pkg_id = phys_pkg_id, |
| 126 | }; |
Andi Kleen | f8d3119 | 2005-07-28 21:15:42 -0700 | [diff] [blame] | 127 | |
| 128 | /* |
| 129 | * Physflat mode is used when there are more than 8 CPUs on a AMD system. |
| 130 | * We cannot use logical delivery in this case because the mask |
| 131 | * overflows, so use physical mode. |
| 132 | */ |
| 133 | |
| 134 | static cpumask_t physflat_target_cpus(void) |
| 135 | { |
Eric W. Biederman | 84f404f | 2006-10-21 18:37:02 +0200 | [diff] [blame] | 136 | return cpu_online_map; |
Andi Kleen | f8d3119 | 2005-07-28 21:15:42 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Eric W. Biederman | c7111c13 | 2006-10-08 07:47:55 -0600 | [diff] [blame] | 139 | static cpumask_t physflat_vector_allocation_domain(int cpu) |
| 140 | { |
| 141 | cpumask_t domain = CPU_MASK_NONE; |
| 142 | cpu_set(cpu, domain); |
| 143 | return domain; |
| 144 | } |
| 145 | |
| 146 | |
Andi Kleen | f8d3119 | 2005-07-28 21:15:42 -0700 | [diff] [blame] | 147 | static void physflat_send_IPI_mask(cpumask_t cpumask, int vector) |
| 148 | { |
| 149 | send_IPI_mask_sequence(cpumask, vector); |
| 150 | } |
| 151 | |
| 152 | static void physflat_send_IPI_allbutself(int vector) |
| 153 | { |
| 154 | cpumask_t allbutme = cpu_online_map; |
Zwane Mwaikambo | 329d400 | 2006-01-11 22:43:09 +0100 | [diff] [blame] | 155 | |
| 156 | cpu_clear(smp_processor_id(), allbutme); |
Andi Kleen | f8d3119 | 2005-07-28 21:15:42 -0700 | [diff] [blame] | 157 | physflat_send_IPI_mask(allbutme, vector); |
Andi Kleen | f8d3119 | 2005-07-28 21:15:42 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void physflat_send_IPI_all(int vector) |
| 161 | { |
| 162 | physflat_send_IPI_mask(cpu_online_map, vector); |
| 163 | } |
| 164 | |
| 165 | static unsigned int physflat_cpu_mask_to_apicid(cpumask_t cpumask) |
| 166 | { |
| 167 | int cpu; |
| 168 | |
| 169 | /* |
| 170 | * We're using fixed IRQ delivery, can only return one phys APIC ID. |
| 171 | * May as well be the first. |
| 172 | */ |
| 173 | cpu = first_cpu(cpumask); |
| 174 | if ((unsigned)cpu < NR_CPUS) |
Mike Travis | 71fff5e | 2007-10-19 20:35:03 +0200 | [diff] [blame^] | 175 | return per_cpu(x86_cpu_to_apicid, cpu); |
Andi Kleen | f8d3119 | 2005-07-28 21:15:42 -0700 | [diff] [blame] | 176 | else |
| 177 | return BAD_APICID; |
| 178 | } |
| 179 | |
| 180 | struct genapic apic_physflat = { |
| 181 | .name = "physical flat", |
Ashok Raj | c1a71a1 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 182 | .int_delivery_mode = dest_Fixed, |
Andi Kleen | f8d3119 | 2005-07-28 21:15:42 -0700 | [diff] [blame] | 183 | .int_dest_mode = (APIC_DEST_PHYSICAL != 0), |
Andi Kleen | f8d3119 | 2005-07-28 21:15:42 -0700 | [diff] [blame] | 184 | .target_cpus = physflat_target_cpus, |
Eric W. Biederman | c7111c13 | 2006-10-08 07:47:55 -0600 | [diff] [blame] | 185 | .vector_allocation_domain = physflat_vector_allocation_domain, |
Andi Kleen | f8d3119 | 2005-07-28 21:15:42 -0700 | [diff] [blame] | 186 | .apic_id_registered = flat_apic_id_registered, |
| 187 | .init_apic_ldr = flat_init_apic_ldr,/*not needed, but shouldn't hurt*/ |
| 188 | .send_IPI_all = physflat_send_IPI_all, |
| 189 | .send_IPI_allbutself = physflat_send_IPI_allbutself, |
| 190 | .send_IPI_mask = physflat_send_IPI_mask, |
| 191 | .cpu_mask_to_apicid = physflat_cpu_mask_to_apicid, |
| 192 | .phys_pkg_id = phys_pkg_id, |
| 193 | }; |