Merge branch 'sched-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / arch / x86 / kernel / apic / apic_flat_64.c
1 /*
2  * Copyright 2004 James Cleverdon, IBM.
3  * Subject to the GNU Public License, v.2
4  *
5  * Flat APIC subarch code.
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  */
11 #include <linux/errno.h>
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/hardirq.h>
18 #include <linux/export.h>
19 #include <asm/smp.h>
20 #include <asm/apic.h>
21 #include <asm/ipi.h>
22 #include <asm/jailhouse_para.h>
23
24 #include <linux/acpi.h>
25
26 static struct apic apic_physflat;
27 static struct apic apic_flat;
28
29 struct apic *apic __ro_after_init = &apic_flat;
30 EXPORT_SYMBOL_GPL(apic);
31
32 static int flat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
33 {
34         return 1;
35 }
36
37 /*
38  * Set up the logical destination ID.
39  *
40  * Intel recommends to set DFR, LDR and TPR before enabling
41  * an APIC.  See e.g. "AP-388 82489DX User's Manual" (Intel
42  * document number 292116).  So here it goes...
43  */
44 void flat_init_apic_ldr(void)
45 {
46         unsigned long val;
47         unsigned long num, id;
48
49         num = smp_processor_id();
50         id = 1UL << num;
51         apic_write(APIC_DFR, APIC_DFR_FLAT);
52         val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
53         val |= SET_APIC_LOGICAL_ID(id);
54         apic_write(APIC_LDR, val);
55 }
56
57 static void _flat_send_IPI_mask(unsigned long mask, int vector)
58 {
59         unsigned long flags;
60
61         local_irq_save(flags);
62         __default_send_IPI_dest_field(mask, vector, apic->dest_logical);
63         local_irq_restore(flags);
64 }
65
66 static void flat_send_IPI_mask(const struct cpumask *cpumask, int vector)
67 {
68         unsigned long mask = cpumask_bits(cpumask)[0];
69
70         _flat_send_IPI_mask(mask, vector);
71 }
72
73 static void
74 flat_send_IPI_mask_allbutself(const struct cpumask *cpumask, int vector)
75 {
76         unsigned long mask = cpumask_bits(cpumask)[0];
77         int cpu = smp_processor_id();
78
79         if (cpu < BITS_PER_LONG)
80                 clear_bit(cpu, &mask);
81
82         _flat_send_IPI_mask(mask, vector);
83 }
84
85 static void flat_send_IPI_allbutself(int vector)
86 {
87         int cpu = smp_processor_id();
88 #ifdef  CONFIG_HOTPLUG_CPU
89         int hotplug = 1;
90 #else
91         int hotplug = 0;
92 #endif
93         if (hotplug || vector == NMI_VECTOR) {
94                 if (!cpumask_equal(cpu_online_mask, cpumask_of(cpu))) {
95                         unsigned long mask = cpumask_bits(cpu_online_mask)[0];
96
97                         if (cpu < BITS_PER_LONG)
98                                 clear_bit(cpu, &mask);
99
100                         _flat_send_IPI_mask(mask, vector);
101                 }
102         } else if (num_online_cpus() > 1) {
103                 __default_send_IPI_shortcut(APIC_DEST_ALLBUT,
104                                             vector, apic->dest_logical);
105         }
106 }
107
108 static void flat_send_IPI_all(int vector)
109 {
110         if (vector == NMI_VECTOR) {
111                 flat_send_IPI_mask(cpu_online_mask, vector);
112         } else {
113                 __default_send_IPI_shortcut(APIC_DEST_ALLINC,
114                                             vector, apic->dest_logical);
115         }
116 }
117
118 static unsigned int flat_get_apic_id(unsigned long x)
119 {
120         return (x >> 24) & 0xFF;
121 }
122
123 static u32 set_apic_id(unsigned int id)
124 {
125         return (id & 0xFF) << 24;
126 }
127
128 static unsigned int read_xapic_id(void)
129 {
130         return flat_get_apic_id(apic_read(APIC_ID));
131 }
132
133 static int flat_apic_id_registered(void)
134 {
135         return physid_isset(read_xapic_id(), phys_cpu_present_map);
136 }
137
138 static int flat_phys_pkg_id(int initial_apic_id, int index_msb)
139 {
140         return initial_apic_id >> index_msb;
141 }
142
143 static int flat_probe(void)
144 {
145         return 1;
146 }
147
148 static struct apic apic_flat __ro_after_init = {
149         .name                           = "flat",
150         .probe                          = flat_probe,
151         .acpi_madt_oem_check            = flat_acpi_madt_oem_check,
152         .apic_id_valid                  = default_apic_id_valid,
153         .apic_id_registered             = flat_apic_id_registered,
154
155         .irq_delivery_mode              = dest_Fixed,
156         .irq_dest_mode                  = 1, /* logical */
157
158         .disable_esr                    = 0,
159         .dest_logical                   = APIC_DEST_LOGICAL,
160         .check_apicid_used              = NULL,
161
162         .init_apic_ldr                  = flat_init_apic_ldr,
163
164         .ioapic_phys_id_map             = NULL,
165         .setup_apic_routing             = NULL,
166         .cpu_present_to_apicid          = default_cpu_present_to_apicid,
167         .apicid_to_cpu_present          = NULL,
168         .check_phys_apicid_present      = default_check_phys_apicid_present,
169         .phys_pkg_id                    = flat_phys_pkg_id,
170
171         .get_apic_id                    = flat_get_apic_id,
172         .set_apic_id                    = set_apic_id,
173
174         .calc_dest_apicid               = apic_flat_calc_apicid,
175
176         .send_IPI                       = default_send_IPI_single,
177         .send_IPI_mask                  = flat_send_IPI_mask,
178         .send_IPI_mask_allbutself       = flat_send_IPI_mask_allbutself,
179         .send_IPI_allbutself            = flat_send_IPI_allbutself,
180         .send_IPI_all                   = flat_send_IPI_all,
181         .send_IPI_self                  = apic_send_IPI_self,
182
183         .inquire_remote_apic            = default_inquire_remote_apic,
184
185         .read                           = native_apic_mem_read,
186         .write                          = native_apic_mem_write,
187         .eoi_write                      = native_apic_mem_write,
188         .icr_read                       = native_apic_icr_read,
189         .icr_write                      = native_apic_icr_write,
190         .wait_icr_idle                  = native_apic_wait_icr_idle,
191         .safe_wait_icr_idle             = native_safe_apic_wait_icr_idle,
192 };
193
194 /*
195  * Physflat mode is used when there are more than 8 CPUs on a system.
196  * We cannot use logical delivery in this case because the mask
197  * overflows, so use physical mode.
198  */
199 static int physflat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
200 {
201 #ifdef CONFIG_ACPI
202         /*
203          * Quirk: some x86_64 machines can only use physical APIC mode
204          * regardless of how many processors are present (x86_64 ES7000
205          * is an example).
206          */
207         if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
208                 (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
209                 printk(KERN_DEBUG "system APIC only can use physical flat");
210                 return 1;
211         }
212
213         if (!strncmp(oem_id, "IBM", 3) && !strncmp(oem_table_id, "EXA", 3)) {
214                 printk(KERN_DEBUG "IBM Summit detected, will use apic physical");
215                 return 1;
216         }
217 #endif
218
219         return 0;
220 }
221
222 static void physflat_init_apic_ldr(void)
223 {
224         /*
225          * LDR and DFR are not involved in physflat mode, rather:
226          * "In physical destination mode, the destination processor is
227          * specified by its local APIC ID [...]." (Intel SDM, 10.6.2.1)
228          */
229 }
230
231 static void physflat_send_IPI_allbutself(int vector)
232 {
233         default_send_IPI_mask_allbutself_phys(cpu_online_mask, vector);
234 }
235
236 static void physflat_send_IPI_all(int vector)
237 {
238         default_send_IPI_mask_sequence_phys(cpu_online_mask, vector);
239 }
240
241 static int physflat_probe(void)
242 {
243         if (apic == &apic_physflat || num_possible_cpus() > 8 ||
244             jailhouse_paravirt())
245                 return 1;
246
247         return 0;
248 }
249
250 static struct apic apic_physflat __ro_after_init = {
251
252         .name                           = "physical flat",
253         .probe                          = physflat_probe,
254         .acpi_madt_oem_check            = physflat_acpi_madt_oem_check,
255         .apic_id_valid                  = default_apic_id_valid,
256         .apic_id_registered             = flat_apic_id_registered,
257
258         .irq_delivery_mode              = dest_Fixed,
259         .irq_dest_mode                  = 0, /* physical */
260
261         .disable_esr                    = 0,
262         .dest_logical                   = 0,
263         .check_apicid_used              = NULL,
264
265         .init_apic_ldr                  = physflat_init_apic_ldr,
266
267         .ioapic_phys_id_map             = NULL,
268         .setup_apic_routing             = NULL,
269         .cpu_present_to_apicid          = default_cpu_present_to_apicid,
270         .apicid_to_cpu_present          = NULL,
271         .check_phys_apicid_present      = default_check_phys_apicid_present,
272         .phys_pkg_id                    = flat_phys_pkg_id,
273
274         .get_apic_id                    = flat_get_apic_id,
275         .set_apic_id                    = set_apic_id,
276
277         .calc_dest_apicid               = apic_default_calc_apicid,
278
279         .send_IPI                       = default_send_IPI_single_phys,
280         .send_IPI_mask                  = default_send_IPI_mask_sequence_phys,
281         .send_IPI_mask_allbutself       = default_send_IPI_mask_allbutself_phys,
282         .send_IPI_allbutself            = physflat_send_IPI_allbutself,
283         .send_IPI_all                   = physflat_send_IPI_all,
284         .send_IPI_self                  = apic_send_IPI_self,
285
286         .inquire_remote_apic            = default_inquire_remote_apic,
287
288         .read                           = native_apic_mem_read,
289         .write                          = native_apic_mem_write,
290         .eoi_write                      = native_apic_mem_write,
291         .icr_read                       = native_apic_icr_read,
292         .icr_write                      = native_apic_icr_write,
293         .wait_icr_idle                  = native_apic_wait_icr_idle,
294         .safe_wait_icr_idle             = native_safe_apic_wait_icr_idle,
295 };
296
297 /*
298  * We need to check for physflat first, so this order is important.
299  */
300 apic_drivers(apic_physflat, apic_flat);