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 
20 static struct apic apic_physflat;
21 static struct apic apic_flat;
22 
23 struct apic *apic __ro_after_init = &apic_flat;
24 EXPORT_SYMBOL_GPL(apic);
25 
flat_acpi_madt_oem_check(char * oem_id,char * oem_table_id)26 static int flat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
27 {
28 	return 1;
29 }
30 
_flat_send_IPI_mask(unsigned long mask,int vector)31 static void _flat_send_IPI_mask(unsigned long mask, int vector)
32 {
33 	unsigned long flags;
34 
35 	local_irq_save(flags);
36 	__default_send_IPI_dest_field(mask, vector, APIC_DEST_LOGICAL);
37 	local_irq_restore(flags);
38 }
39 
flat_send_IPI_mask(const struct cpumask * cpumask,int vector)40 static void flat_send_IPI_mask(const struct cpumask *cpumask, int vector)
41 {
42 	unsigned long mask = cpumask_bits(cpumask)[0];
43 
44 	_flat_send_IPI_mask(mask, vector);
45 }
46 
47 static void
flat_send_IPI_mask_allbutself(const struct cpumask * cpumask,int vector)48 flat_send_IPI_mask_allbutself(const struct cpumask *cpumask, int vector)
49 {
50 	unsigned long mask = cpumask_bits(cpumask)[0];
51 	int cpu = smp_processor_id();
52 
53 	if (cpu < BITS_PER_LONG)
54 		__clear_bit(cpu, &mask);
55 
56 	_flat_send_IPI_mask(mask, vector);
57 }
58 
flat_get_apic_id(unsigned long x)59 static unsigned int flat_get_apic_id(unsigned long x)
60 {
61 	return (x >> 24) & 0xFF;
62 }
63 
set_apic_id(unsigned int id)64 static u32 set_apic_id(unsigned int id)
65 {
66 	return (id & 0xFF) << 24;
67 }
68 
flat_phys_pkg_id(int initial_apic_id,int index_msb)69 static int flat_phys_pkg_id(int initial_apic_id, int index_msb)
70 {
71 	return initial_apic_id >> index_msb;
72 }
73 
flat_probe(void)74 static int flat_probe(void)
75 {
76 	return 1;
77 }
78 
79 static struct apic apic_flat __ro_after_init = {
80 	.name				= "flat",
81 	.probe				= flat_probe,
82 	.acpi_madt_oem_check		= flat_acpi_madt_oem_check,
83 	.apic_id_registered		= default_apic_id_registered,
84 
85 	.delivery_mode			= APIC_DELIVERY_MODE_FIXED,
86 	.dest_mode_logical		= true,
87 
88 	.disable_esr			= 0,
89 
90 	.init_apic_ldr			= default_init_apic_ldr,
91 	.cpu_present_to_apicid		= default_cpu_present_to_apicid,
92 	.phys_pkg_id			= flat_phys_pkg_id,
93 
94 	.max_apic_id			= 0xFE,
95 	.get_apic_id			= flat_get_apic_id,
96 	.set_apic_id			= set_apic_id,
97 
98 	.calc_dest_apicid		= apic_flat_calc_apicid,
99 
100 	.send_IPI			= default_send_IPI_single,
101 	.send_IPI_mask			= flat_send_IPI_mask,
102 	.send_IPI_mask_allbutself	= flat_send_IPI_mask_allbutself,
103 	.send_IPI_allbutself		= default_send_IPI_allbutself,
104 	.send_IPI_all			= default_send_IPI_all,
105 	.send_IPI_self			= default_send_IPI_self,
106 
107 	.read				= native_apic_mem_read,
108 	.write				= native_apic_mem_write,
109 	.eoi				= native_apic_mem_eoi,
110 	.icr_read			= native_apic_icr_read,
111 	.icr_write			= native_apic_icr_write,
112 	.wait_icr_idle			= apic_mem_wait_icr_idle,
113 	.safe_wait_icr_idle		= apic_mem_wait_icr_idle_timeout,
114 };
115 
116 /*
117  * Physflat mode is used when there are more than 8 CPUs on a system.
118  * We cannot use logical delivery in this case because the mask
119  * overflows, so use physical mode.
120  */
physflat_acpi_madt_oem_check(char * oem_id,char * oem_table_id)121 static int physflat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
122 {
123 #ifdef CONFIG_ACPI
124 	/*
125 	 * Quirk: some x86_64 machines can only use physical APIC mode
126 	 * regardless of how many processors are present (x86_64 ES7000
127 	 * is an example).
128 	 */
129 	if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
130 		(acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
131 		printk(KERN_DEBUG "system APIC only can use physical flat");
132 		return 1;
133 	}
134 
135 	if (!strncmp(oem_id, "IBM", 3) && !strncmp(oem_table_id, "EXA", 3)) {
136 		printk(KERN_DEBUG "IBM Summit detected, will use apic physical");
137 		return 1;
138 	}
139 #endif
140 
141 	return 0;
142 }
143 
physflat_probe(void)144 static int physflat_probe(void)
145 {
146 	return apic == &apic_physflat || num_possible_cpus() > 8 || jailhouse_paravirt();
147 }
148 
149 static struct apic apic_physflat __ro_after_init = {
150 
151 	.name				= "physical flat",
152 	.probe				= physflat_probe,
153 	.acpi_madt_oem_check		= physflat_acpi_madt_oem_check,
154 	.apic_id_registered		= default_apic_id_registered,
155 
156 	.delivery_mode			= APIC_DELIVERY_MODE_FIXED,
157 	.dest_mode_logical		= false,
158 
159 	.disable_esr			= 0,
160 
161 	.check_apicid_used		= NULL,
162 	.ioapic_phys_id_map		= NULL,
163 	.cpu_present_to_apicid		= default_cpu_present_to_apicid,
164 	.phys_pkg_id			= flat_phys_pkg_id,
165 
166 	.max_apic_id			= 0xFE,
167 	.get_apic_id			= flat_get_apic_id,
168 	.set_apic_id			= set_apic_id,
169 
170 	.calc_dest_apicid		= apic_default_calc_apicid,
171 
172 	.send_IPI			= default_send_IPI_single_phys,
173 	.send_IPI_mask			= default_send_IPI_mask_sequence_phys,
174 	.send_IPI_mask_allbutself	= default_send_IPI_mask_allbutself_phys,
175 	.send_IPI_allbutself		= default_send_IPI_allbutself,
176 	.send_IPI_all			= default_send_IPI_all,
177 	.send_IPI_self			= default_send_IPI_self,
178 
179 	.read				= native_apic_mem_read,
180 	.write				= native_apic_mem_write,
181 	.eoi				= native_apic_mem_eoi,
182 	.icr_read			= native_apic_icr_read,
183 	.icr_write			= native_apic_icr_write,
184 	.wait_icr_idle			= apic_mem_wait_icr_idle,
185 	.safe_wait_icr_idle		= apic_mem_wait_icr_idle_timeout,
186 };
187 
188 /*
189  * We need to check for physflat first, so this order is important.
190  */
191 apic_drivers(apic_physflat, apic_flat);
192