xref: /openbmc/linux/arch/x86/kernel/apic/x2apic_phys.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2f62bae50SIngo Molnar 
3521b82feSThomas Gleixner #include <linux/cpumask.h>
4521b82feSThomas Gleixner #include <linux/acpi.h>
5521b82feSThomas Gleixner 
6c94f0718SThomas Gleixner #include "local.h"
7f62bae50SIngo Molnar 
8ef1f87aaSSuresh Siddha int x2apic_phys;
9f62bae50SIngo Molnar 
101a8880a1SSuresh Siddha static struct apic apic_x2apic_phys;
11b5a5ce58SThomas Gleixner u32 x2apic_max_apicid __ro_after_init = UINT_MAX;
1226573a97SDavid Woodhouse 
x2apic_set_max_apicid(u32 apicid)1326573a97SDavid Woodhouse void __init x2apic_set_max_apicid(u32 apicid)
1426573a97SDavid Woodhouse {
1526573a97SDavid Woodhouse 	x2apic_max_apicid = apicid;
16b5a5ce58SThomas Gleixner 	if (apic->x2apic_set_max_apicid)
17b5a5ce58SThomas Gleixner 		apic->max_apic_id = apicid;
1826573a97SDavid Woodhouse }
191a8880a1SSuresh Siddha 
set_x2apic_phys_mode(char * arg)20afed7d17SDou Liyang static int __init set_x2apic_phys_mode(char *arg)
21f62bae50SIngo Molnar {
22f62bae50SIngo Molnar 	x2apic_phys = 1;
23f62bae50SIngo Molnar 	return 0;
24f62bae50SIngo Molnar }
25f62bae50SIngo Molnar early_param("x2apic_phys", set_x2apic_phys_mode);
26f62bae50SIngo Molnar 
x2apic_fadt_phys(void)27cb214edeSStoney Wang static bool x2apic_fadt_phys(void)
28cb214edeSStoney Wang {
29781674fcSJan Kiszka #ifdef CONFIG_ACPI
30cb214edeSStoney Wang 	if ((acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID) &&
31cb214edeSStoney Wang 		(acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
32cb214edeSStoney Wang 		printk(KERN_DEBUG "System requires x2apic physical mode\n");
33cb214edeSStoney Wang 		return true;
34cb214edeSStoney Wang 	}
35781674fcSJan Kiszka #endif
36cb214edeSStoney Wang 	return false;
37cb214edeSStoney Wang }
38cb214edeSStoney Wang 
x2apic_acpi_madt_oem_check(char * oem_id,char * oem_table_id)39f62bae50SIngo Molnar static int x2apic_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
40f62bae50SIngo Molnar {
41cb214edeSStoney Wang 	return x2apic_enabled() && (x2apic_phys || x2apic_fadt_phys());
42f62bae50SIngo Molnar }
43f62bae50SIngo Molnar 
x2apic_send_IPI(int cpu,int vector)44f2bffe8aSThomas Gleixner static void x2apic_send_IPI(int cpu, int vector)
45f2bffe8aSThomas Gleixner {
46f2bffe8aSThomas Gleixner 	u32 dest = per_cpu(x86_cpu_to_apicid, cpu);
47f2bffe8aSThomas Gleixner 
4825a068b8SDave Hansen 	/* x2apic MSRs are special and need a special fence: */
4925a068b8SDave Hansen 	weak_wrmsr_fence();
50f2bffe8aSThomas Gleixner 	__x2apic_send_IPI_dest(dest, vector, APIC_DEST_PHYSICAL);
51f2bffe8aSThomas Gleixner }
52f2bffe8aSThomas Gleixner 
53f62bae50SIngo Molnar static void
__x2apic_send_IPI_mask(const struct cpumask * mask,int vector,int apic_dest)54a27d0b5eSSuresh Siddha __x2apic_send_IPI_mask(const struct cpumask *mask, int vector, int apic_dest)
55f62bae50SIngo Molnar {
56f62bae50SIngo Molnar 	unsigned long query_cpu;
57a27d0b5eSSuresh Siddha 	unsigned long this_cpu;
58f62bae50SIngo Molnar 	unsigned long flags;
59f62bae50SIngo Molnar 
6025a068b8SDave Hansen 	/* x2apic MSRs are special and need a special fence: */
6125a068b8SDave Hansen 	weak_wrmsr_fence();
62ce4e240cSSuresh Siddha 
63f62bae50SIngo Molnar 	local_irq_save(flags);
64a27d0b5eSSuresh Siddha 
65a27d0b5eSSuresh Siddha 	this_cpu = smp_processor_id();
66f62bae50SIngo Molnar 	for_each_cpu(query_cpu, mask) {
67a27d0b5eSSuresh Siddha 		if (apic_dest == APIC_DEST_ALLBUT && this_cpu == query_cpu)
68f62bae50SIngo Molnar 			continue;
69f62bae50SIngo Molnar 		__x2apic_send_IPI_dest(per_cpu(x86_cpu_to_apicid, query_cpu),
70f62bae50SIngo Molnar 				       vector, APIC_DEST_PHYSICAL);
71f62bae50SIngo Molnar 	}
72f62bae50SIngo Molnar 	local_irq_restore(flags);
73f62bae50SIngo Molnar }
74f62bae50SIngo Molnar 
x2apic_send_IPI_mask(const struct cpumask * mask,int vector)75a27d0b5eSSuresh Siddha static void x2apic_send_IPI_mask(const struct cpumask *mask, int vector)
76a27d0b5eSSuresh Siddha {
77a27d0b5eSSuresh Siddha 	__x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLINC);
78a27d0b5eSSuresh Siddha }
79a27d0b5eSSuresh Siddha 
80a27d0b5eSSuresh Siddha static void
x2apic_send_IPI_mask_allbutself(const struct cpumask * mask,int vector)81a27d0b5eSSuresh Siddha  x2apic_send_IPI_mask_allbutself(const struct cpumask *mask, int vector)
82a27d0b5eSSuresh Siddha {
83a27d0b5eSSuresh Siddha 	__x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLBUT);
84a27d0b5eSSuresh Siddha }
85a27d0b5eSSuresh Siddha 
__x2apic_send_IPI_shorthand(int vector,u32 which)8696ae35c7SThomas Gleixner static void __x2apic_send_IPI_shorthand(int vector, u32 which)
8796ae35c7SThomas Gleixner {
8896ae35c7SThomas Gleixner 	unsigned long cfg = __prepare_ICR(which, vector, 0);
8996ae35c7SThomas Gleixner 
9096ae35c7SThomas Gleixner 	/* x2apic MSRs are special and need a special fence: */
9196ae35c7SThomas Gleixner 	weak_wrmsr_fence();
9296ae35c7SThomas Gleixner 	native_x2apic_icr_write(cfg, 0);
9396ae35c7SThomas Gleixner }
9496ae35c7SThomas Gleixner 
x2apic_send_IPI_allbutself(int vector)9596ae35c7SThomas Gleixner void x2apic_send_IPI_allbutself(int vector)
96a27d0b5eSSuresh Siddha {
9743931d35SThomas Gleixner 	__x2apic_send_IPI_shorthand(vector, APIC_DEST_ALLBUT);
98a27d0b5eSSuresh Siddha }
99a27d0b5eSSuresh Siddha 
x2apic_send_IPI_all(int vector)10096ae35c7SThomas Gleixner void x2apic_send_IPI_all(int vector)
101f62bae50SIngo Molnar {
10243931d35SThomas Gleixner 	__x2apic_send_IPI_shorthand(vector, APIC_DEST_ALLINC);
103f62bae50SIngo Molnar }
104f62bae50SIngo Molnar 
x2apic_send_IPI_self(int vector)10596ae35c7SThomas Gleixner void x2apic_send_IPI_self(int vector)
10696ae35c7SThomas Gleixner {
10796ae35c7SThomas Gleixner 	apic_write(APIC_SELF_IPI, vector);
10896ae35c7SThomas Gleixner }
10996ae35c7SThomas Gleixner 
__x2apic_send_IPI_dest(unsigned int apicid,int vector,unsigned int dest)11096ae35c7SThomas Gleixner void __x2apic_send_IPI_dest(unsigned int apicid, int vector, unsigned int dest)
11196ae35c7SThomas Gleixner {
11296ae35c7SThomas Gleixner 	unsigned long cfg = __prepare_ICR(0, vector, dest);
11396ae35c7SThomas Gleixner 	native_x2apic_icr_write(cfg, apicid);
11496ae35c7SThomas Gleixner }
11596ae35c7SThomas Gleixner 
x2apic_phys_probe(void)1169ebd680bSSuresh Siddha static int x2apic_phys_probe(void)
1179ebd680bSSuresh Siddha {
11885d38d58SDheeraj Kumar Srivastava 	if (!x2apic_mode)
11985d38d58SDheeraj Kumar Srivastava 		return 0;
12085d38d58SDheeraj Kumar Srivastava 
12185d38d58SDheeraj Kumar Srivastava 	if (x2apic_phys || x2apic_fadt_phys())
1229ebd680bSSuresh Siddha 		return 1;
1239ebd680bSSuresh Siddha 
1249ebd680bSSuresh Siddha 	return apic == &apic_x2apic_phys;
1259ebd680bSSuresh Siddha }
1269ebd680bSSuresh Siddha 
x2apic_get_apic_id(unsigned long id)127981c2eacSThomas Gleixner unsigned int x2apic_get_apic_id(unsigned long id)
128981c2eacSThomas Gleixner {
129981c2eacSThomas Gleixner 	return id;
130981c2eacSThomas Gleixner }
131981c2eacSThomas Gleixner 
x2apic_set_apic_id(unsigned int id)132727657e6SThomas Gleixner u32 x2apic_set_apic_id(unsigned int id)
133981c2eacSThomas Gleixner {
134981c2eacSThomas Gleixner 	return id;
135981c2eacSThomas Gleixner }
136981c2eacSThomas Gleixner 
x2apic_phys_pkg_id(int initial_apicid,int index_msb)137981c2eacSThomas Gleixner int x2apic_phys_pkg_id(int initial_apicid, int index_msb)
138981c2eacSThomas Gleixner {
139981c2eacSThomas Gleixner 	return initial_apicid >> index_msb;
140981c2eacSThomas Gleixner }
141981c2eacSThomas Gleixner 
142404f6aacSKees Cook static struct apic apic_x2apic_phys __ro_after_init = {
143f62bae50SIngo Molnar 
144f62bae50SIngo Molnar 	.name				= "physical x2apic",
1459ebd680bSSuresh Siddha 	.probe				= x2apic_phys_probe,
146f62bae50SIngo Molnar 	.acpi_madt_oem_check		= x2apic_acpi_madt_oem_check,
147f62bae50SIngo Molnar 
14872161299SThomas Gleixner 	.delivery_mode			= APIC_DELIVERY_MODE_FIXED,
1498c44963bSThomas Gleixner 	.dest_mode_logical		= false,
150f62bae50SIngo Molnar 
151f62bae50SIngo Molnar 	.disable_esr			= 0,
152e57d04e5SThomas Gleixner 
153f62bae50SIngo Molnar 	.cpu_present_to_apicid		= default_cpu_present_to_apicid,
154f62bae50SIngo Molnar 	.phys_pkg_id			= x2apic_phys_pkg_id,
155f62bae50SIngo Molnar 
156d92e5e7cSThomas Gleixner 	.max_apic_id			= UINT_MAX,
157b5a5ce58SThomas Gleixner 	.x2apic_set_max_apicid		= true,
15879deb8e5SCyrill Gorcunov 	.get_apic_id			= x2apic_get_apic_id,
15979deb8e5SCyrill Gorcunov 	.set_apic_id			= x2apic_set_apic_id,
160f62bae50SIngo Molnar 
1619f9e3bb1SThomas Gleixner 	.calc_dest_apicid		= apic_default_calc_apicid,
162f62bae50SIngo Molnar 
163f2bffe8aSThomas Gleixner 	.send_IPI			= x2apic_send_IPI,
164f62bae50SIngo Molnar 	.send_IPI_mask			= x2apic_send_IPI_mask,
165f62bae50SIngo Molnar 	.send_IPI_mask_allbutself	= x2apic_send_IPI_mask_allbutself,
166f62bae50SIngo Molnar 	.send_IPI_allbutself		= x2apic_send_IPI_allbutself,
167f62bae50SIngo Molnar 	.send_IPI_all			= x2apic_send_IPI_all,
168f62bae50SIngo Molnar 	.send_IPI_self			= x2apic_send_IPI_self,
169f62bae50SIngo Molnar 
170f62bae50SIngo Molnar 	.read				= native_apic_msr_read,
171f62bae50SIngo Molnar 	.write				= native_apic_msr_write,
172*185c8f33SThomas Gleixner 	.eoi				= native_apic_msr_eoi,
173f62bae50SIngo Molnar 	.icr_read			= native_x2apic_icr_read,
174f62bae50SIngo Molnar 	.icr_write			= native_x2apic_icr_write,
175f62bae50SIngo Molnar };
176107e0e0cSSuresh Siddha 
177107e0e0cSSuresh Siddha apic_driver(apic_x2apic_phys);
178