xref: /openbmc/linux/arch/x86/kernel/apic/x2apic_phys.c (revision 9ebd680bd029a9fc47399ca61c950f8b6730ac40)
1f62bae50SIngo Molnar #include <linux/threads.h>
2f62bae50SIngo Molnar #include <linux/cpumask.h>
3f62bae50SIngo Molnar #include <linux/string.h>
4f62bae50SIngo Molnar #include <linux/kernel.h>
5f62bae50SIngo Molnar #include <linux/ctype.h>
6f62bae50SIngo Molnar #include <linux/init.h>
7f62bae50SIngo Molnar #include <linux/dmar.h>
8f62bae50SIngo Molnar 
9f62bae50SIngo Molnar #include <asm/smp.h>
10f62bae50SIngo Molnar #include <asm/apic.h>
11f62bae50SIngo Molnar #include <asm/ipi.h>
12f62bae50SIngo Molnar 
13ef1f87aaSSuresh Siddha int x2apic_phys;
14f62bae50SIngo Molnar 
15f62bae50SIngo Molnar static int set_x2apic_phys_mode(char *arg)
16f62bae50SIngo Molnar {
17f62bae50SIngo Molnar 	x2apic_phys = 1;
18f62bae50SIngo Molnar 	return 0;
19f62bae50SIngo Molnar }
20f62bae50SIngo Molnar early_param("x2apic_phys", set_x2apic_phys_mode);
21f62bae50SIngo Molnar 
22f62bae50SIngo Molnar static int x2apic_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
23f62bae50SIngo Molnar {
24ef1f87aaSSuresh Siddha 	if (x2apic_phys)
25ef1f87aaSSuresh Siddha 		return x2apic_enabled();
26ef1f87aaSSuresh Siddha 	else
27f62bae50SIngo Molnar 		return 0;
28f62bae50SIngo Molnar }
29f62bae50SIngo Molnar 
30087d7e56SYinghai Lu /*
31087d7e56SYinghai Lu  * need to use more than cpu 0, because we need more vectors when
32087d7e56SYinghai Lu  * MSI-X are used.
33087d7e56SYinghai Lu  */
34f62bae50SIngo Molnar static const struct cpumask *x2apic_target_cpus(void)
35f62bae50SIngo Molnar {
36087d7e56SYinghai Lu 	return cpu_online_mask;
37f62bae50SIngo Molnar }
38f62bae50SIngo Molnar 
39f62bae50SIngo Molnar static void x2apic_vector_allocation_domain(int cpu, struct cpumask *retmask)
40f62bae50SIngo Molnar {
41f62bae50SIngo Molnar 	cpumask_clear(retmask);
42f62bae50SIngo Molnar 	cpumask_set_cpu(cpu, retmask);
43f62bae50SIngo Molnar }
44f62bae50SIngo Molnar 
45f62bae50SIngo Molnar static void __x2apic_send_IPI_dest(unsigned int apicid, int vector,
46f62bae50SIngo Molnar 				   unsigned int dest)
47f62bae50SIngo Molnar {
48f62bae50SIngo Molnar 	unsigned long cfg;
49f62bae50SIngo Molnar 
50f62bae50SIngo Molnar 	cfg = __prepare_ICR(0, vector, dest);
51f62bae50SIngo Molnar 
52f62bae50SIngo Molnar 	/*
53f62bae50SIngo Molnar 	 * send the IPI.
54f62bae50SIngo Molnar 	 */
55f62bae50SIngo Molnar 	native_x2apic_icr_write(cfg, apicid);
56f62bae50SIngo Molnar }
57f62bae50SIngo Molnar 
58f62bae50SIngo Molnar static void x2apic_send_IPI_mask(const struct cpumask *mask, int vector)
59f62bae50SIngo Molnar {
60f62bae50SIngo Molnar 	unsigned long query_cpu;
61f62bae50SIngo Molnar 	unsigned long flags;
62f62bae50SIngo Molnar 
63ce4e240cSSuresh Siddha 	x2apic_wrmsr_fence();
64ce4e240cSSuresh Siddha 
65f62bae50SIngo Molnar 	local_irq_save(flags);
66f62bae50SIngo Molnar 	for_each_cpu(query_cpu, mask) {
67f62bae50SIngo Molnar 		__x2apic_send_IPI_dest(per_cpu(x86_cpu_to_apicid, query_cpu),
68f62bae50SIngo Molnar 				       vector, APIC_DEST_PHYSICAL);
69f62bae50SIngo Molnar 	}
70f62bae50SIngo Molnar 	local_irq_restore(flags);
71f62bae50SIngo Molnar }
72f62bae50SIngo Molnar 
73f62bae50SIngo Molnar static void
74f62bae50SIngo Molnar  x2apic_send_IPI_mask_allbutself(const struct cpumask *mask, int vector)
75f62bae50SIngo Molnar {
76f62bae50SIngo Molnar 	unsigned long this_cpu = smp_processor_id();
77f62bae50SIngo Molnar 	unsigned long query_cpu;
78f62bae50SIngo Molnar 	unsigned long flags;
79f62bae50SIngo Molnar 
80ce4e240cSSuresh Siddha 	x2apic_wrmsr_fence();
81ce4e240cSSuresh Siddha 
82f62bae50SIngo Molnar 	local_irq_save(flags);
83f62bae50SIngo Molnar 	for_each_cpu(query_cpu, mask) {
84f62bae50SIngo Molnar 		if (query_cpu != this_cpu)
85f62bae50SIngo Molnar 			__x2apic_send_IPI_dest(
86f62bae50SIngo Molnar 				per_cpu(x86_cpu_to_apicid, query_cpu),
87f62bae50SIngo Molnar 				vector, APIC_DEST_PHYSICAL);
88f62bae50SIngo Molnar 	}
89f62bae50SIngo Molnar 	local_irq_restore(flags);
90f62bae50SIngo Molnar }
91f62bae50SIngo Molnar 
92f62bae50SIngo Molnar static void x2apic_send_IPI_allbutself(int vector)
93f62bae50SIngo Molnar {
94f62bae50SIngo Molnar 	unsigned long this_cpu = smp_processor_id();
95f62bae50SIngo Molnar 	unsigned long query_cpu;
96f62bae50SIngo Molnar 	unsigned long flags;
97f62bae50SIngo Molnar 
98ce4e240cSSuresh Siddha 	x2apic_wrmsr_fence();
99ce4e240cSSuresh Siddha 
100f62bae50SIngo Molnar 	local_irq_save(flags);
101f62bae50SIngo Molnar 	for_each_online_cpu(query_cpu) {
102f62bae50SIngo Molnar 		if (query_cpu == this_cpu)
103f62bae50SIngo Molnar 			continue;
104f62bae50SIngo Molnar 		__x2apic_send_IPI_dest(per_cpu(x86_cpu_to_apicid, query_cpu),
105f62bae50SIngo Molnar 				       vector, APIC_DEST_PHYSICAL);
106f62bae50SIngo Molnar 	}
107f62bae50SIngo Molnar 	local_irq_restore(flags);
108f62bae50SIngo Molnar }
109f62bae50SIngo Molnar 
110f62bae50SIngo Molnar static void x2apic_send_IPI_all(int vector)
111f62bae50SIngo Molnar {
112f62bae50SIngo Molnar 	x2apic_send_IPI_mask(cpu_online_mask, vector);
113f62bae50SIngo Molnar }
114f62bae50SIngo Molnar 
115f62bae50SIngo Molnar static int x2apic_apic_id_registered(void)
116f62bae50SIngo Molnar {
117f62bae50SIngo Molnar 	return 1;
118f62bae50SIngo Molnar }
119f62bae50SIngo Molnar 
120f62bae50SIngo Molnar static unsigned int x2apic_cpu_mask_to_apicid(const struct cpumask *cpumask)
121f62bae50SIngo Molnar {
122f62bae50SIngo Molnar 	/*
123f62bae50SIngo Molnar 	 * We're using fixed IRQ delivery, can only return one phys APIC ID.
124f62bae50SIngo Molnar 	 * May as well be the first.
125f62bae50SIngo Molnar 	 */
126f62bae50SIngo Molnar 	int cpu = cpumask_first(cpumask);
127f62bae50SIngo Molnar 
128f62bae50SIngo Molnar 	if ((unsigned)cpu < nr_cpu_ids)
129f62bae50SIngo Molnar 		return per_cpu(x86_cpu_to_apicid, cpu);
130f62bae50SIngo Molnar 	else
131f62bae50SIngo Molnar 		return BAD_APICID;
132f62bae50SIngo Molnar }
133f62bae50SIngo Molnar 
134f62bae50SIngo Molnar static unsigned int
135f62bae50SIngo Molnar x2apic_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
136f62bae50SIngo Molnar 			      const struct cpumask *andmask)
137f62bae50SIngo Molnar {
138f62bae50SIngo Molnar 	int cpu;
139f62bae50SIngo Molnar 
140f62bae50SIngo Molnar 	/*
141f62bae50SIngo Molnar 	 * We're using fixed IRQ delivery, can only return one phys APIC ID.
142f62bae50SIngo Molnar 	 * May as well be the first.
143f62bae50SIngo Molnar 	 */
144f62bae50SIngo Molnar 	for_each_cpu_and(cpu, cpumask, andmask) {
145f62bae50SIngo Molnar 		if (cpumask_test_cpu(cpu, cpu_online_mask))
146f62bae50SIngo Molnar 			break;
147f62bae50SIngo Molnar 	}
148f62bae50SIngo Molnar 
149f62bae50SIngo Molnar 	return per_cpu(x86_cpu_to_apicid, cpu);
150f62bae50SIngo Molnar }
151f62bae50SIngo Molnar 
152f62bae50SIngo Molnar static unsigned int x2apic_phys_get_apic_id(unsigned long x)
153f62bae50SIngo Molnar {
154f62bae50SIngo Molnar 	return x;
155f62bae50SIngo Molnar }
156f62bae50SIngo Molnar 
157f62bae50SIngo Molnar static unsigned long set_apic_id(unsigned int id)
158f62bae50SIngo Molnar {
159f62bae50SIngo Molnar 	return id;
160f62bae50SIngo Molnar }
161f62bae50SIngo Molnar 
162f62bae50SIngo Molnar static int x2apic_phys_pkg_id(int initial_apicid, int index_msb)
163f62bae50SIngo Molnar {
164d8c7eb34SYinghai Lu 	return initial_apicid >> index_msb;
165f62bae50SIngo Molnar }
166f62bae50SIngo Molnar 
167f62bae50SIngo Molnar static void x2apic_send_IPI_self(int vector)
168f62bae50SIngo Molnar {
169f62bae50SIngo Molnar 	apic_write(APIC_SELF_IPI, vector);
170f62bae50SIngo Molnar }
171f62bae50SIngo Molnar 
172f62bae50SIngo Molnar static void init_x2apic_ldr(void)
173f62bae50SIngo Molnar {
174f62bae50SIngo Molnar }
175f62bae50SIngo Molnar 
176*9ebd680bSSuresh Siddha static int x2apic_phys_probe(void)
177*9ebd680bSSuresh Siddha {
178*9ebd680bSSuresh Siddha 	if (x2apic_mode && x2apic_phys)
179*9ebd680bSSuresh Siddha 		return 1;
180*9ebd680bSSuresh Siddha 
181*9ebd680bSSuresh Siddha 	return apic == &apic_x2apic_phys;
182*9ebd680bSSuresh Siddha }
183*9ebd680bSSuresh Siddha 
184f62bae50SIngo Molnar struct apic apic_x2apic_phys = {
185f62bae50SIngo Molnar 
186f62bae50SIngo Molnar 	.name				= "physical x2apic",
187*9ebd680bSSuresh Siddha 	.probe				= x2apic_phys_probe,
188f62bae50SIngo Molnar 	.acpi_madt_oem_check		= x2apic_acpi_madt_oem_check,
189f62bae50SIngo Molnar 	.apic_id_registered		= x2apic_apic_id_registered,
190f62bae50SIngo Molnar 
191f62bae50SIngo Molnar 	.irq_delivery_mode		= dest_Fixed,
192f62bae50SIngo Molnar 	.irq_dest_mode			= 0, /* physical */
193f62bae50SIngo Molnar 
194f62bae50SIngo Molnar 	.target_cpus			= x2apic_target_cpus,
195f62bae50SIngo Molnar 	.disable_esr			= 0,
196f62bae50SIngo Molnar 	.dest_logical			= 0,
197f62bae50SIngo Molnar 	.check_apicid_used		= NULL,
198f62bae50SIngo Molnar 	.check_apicid_present		= NULL,
199f62bae50SIngo Molnar 
200f62bae50SIngo Molnar 	.vector_allocation_domain	= x2apic_vector_allocation_domain,
201f62bae50SIngo Molnar 	.init_apic_ldr			= init_x2apic_ldr,
202f62bae50SIngo Molnar 
203f62bae50SIngo Molnar 	.ioapic_phys_id_map		= NULL,
204f62bae50SIngo Molnar 	.setup_apic_routing		= NULL,
205f62bae50SIngo Molnar 	.multi_timer_check		= NULL,
206f62bae50SIngo Molnar 	.cpu_present_to_apicid		= default_cpu_present_to_apicid,
207f62bae50SIngo Molnar 	.apicid_to_cpu_present		= NULL,
208f62bae50SIngo Molnar 	.setup_portio_remap		= NULL,
209f62bae50SIngo Molnar 	.check_phys_apicid_present	= default_check_phys_apicid_present,
210f62bae50SIngo Molnar 	.enable_apic_mode		= NULL,
211f62bae50SIngo Molnar 	.phys_pkg_id			= x2apic_phys_pkg_id,
212f62bae50SIngo Molnar 	.mps_oem_check			= NULL,
213f62bae50SIngo Molnar 
214f62bae50SIngo Molnar 	.get_apic_id			= x2apic_phys_get_apic_id,
215f62bae50SIngo Molnar 	.set_apic_id			= set_apic_id,
216f62bae50SIngo Molnar 	.apic_id_mask			= 0xFFFFFFFFu,
217f62bae50SIngo Molnar 
218f62bae50SIngo Molnar 	.cpu_mask_to_apicid		= x2apic_cpu_mask_to_apicid,
219f62bae50SIngo Molnar 	.cpu_mask_to_apicid_and		= x2apic_cpu_mask_to_apicid_and,
220f62bae50SIngo Molnar 
221f62bae50SIngo Molnar 	.send_IPI_mask			= x2apic_send_IPI_mask,
222f62bae50SIngo Molnar 	.send_IPI_mask_allbutself	= x2apic_send_IPI_mask_allbutself,
223f62bae50SIngo Molnar 	.send_IPI_allbutself		= x2apic_send_IPI_allbutself,
224f62bae50SIngo Molnar 	.send_IPI_all			= x2apic_send_IPI_all,
225f62bae50SIngo Molnar 	.send_IPI_self			= x2apic_send_IPI_self,
226f62bae50SIngo Molnar 
227f62bae50SIngo Molnar 	.trampoline_phys_low		= DEFAULT_TRAMPOLINE_PHYS_LOW,
228f62bae50SIngo Molnar 	.trampoline_phys_high		= DEFAULT_TRAMPOLINE_PHYS_HIGH,
229f62bae50SIngo Molnar 	.wait_for_init_deassert		= NULL,
230f62bae50SIngo Molnar 	.smp_callin_clear_local_apic	= NULL,
231f62bae50SIngo Molnar 	.inquire_remote_apic		= NULL,
232f62bae50SIngo Molnar 
233f62bae50SIngo Molnar 	.read				= native_apic_msr_read,
234f62bae50SIngo Molnar 	.write				= native_apic_msr_write,
235f62bae50SIngo Molnar 	.icr_read			= native_x2apic_icr_read,
236f62bae50SIngo Molnar 	.icr_write			= native_x2apic_icr_write,
237f62bae50SIngo Molnar 	.wait_icr_idle			= native_x2apic_wait_icr_idle,
238f62bae50SIngo Molnar 	.safe_wait_icr_idle		= native_safe_x2apic_wait_icr_idle,
239f62bae50SIngo Molnar };
240