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