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