apic_flat_64.c 7.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 * Copyright 2004 James Cleverdon, IBM.
 * Subject to the GNU Public License, v.2
 *
5
 * Flat APIC subarch code.
L
Linus Torvalds 已提交
6 7 8 9 10
 *
 * Hacked for x86-64 by James Cleverdon from i386 architecture code by
 * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and
 * James Cleverdon.
 */
11
#include <linux/errno.h>
L
Linus Torvalds 已提交
12 13 14 15 16
#include <linux/threads.h>
#include <linux/cpumask.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/ctype.h>
17
#include <linux/hardirq.h>
18
#include <linux/export.h>
L
Linus Torvalds 已提交
19
#include <asm/smp.h>
I
Ingo Molnar 已提交
20
#include <asm/apic.h>
Y
Yinghai Lu 已提交
21
#include <asm/ipi.h>
22
#include <asm/jailhouse_para.h>
L
Linus Torvalds 已提交
23

24
#include <linux/acpi.h>
25

26 27 28
static struct apic apic_physflat;
static struct apic apic_flat;

29
struct apic *apic __ro_after_init = &apic_flat;
30 31
EXPORT_SYMBOL_GPL(apic);

32
static int flat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
33 34 35 36
{
	return 1;
}

L
Linus Torvalds 已提交
37 38 39 40 41 42 43
/*
 * Set up the logical destination ID.
 *
 * Intel recommends to set DFR, LDR and TPR before enabling
 * an APIC.  See e.g. "AP-388 82489DX User's Manual" (Intel
 * document number 292116).  So here it goes...
 */
44
void flat_init_apic_ldr(void)
L
Linus Torvalds 已提交
45 46 47 48 49 50
{
	unsigned long val;
	unsigned long num, id;

	num = smp_processor_id();
	id = 1UL << num;
51
	apic_write(APIC_DFR, APIC_DFR_FLAT);
L
Linus Torvalds 已提交
52 53
	val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
	val |= SET_APIC_LOGICAL_ID(id);
54
	apic_write(APIC_LDR, val);
L
Linus Torvalds 已提交
55 56
}

57
static void _flat_send_IPI_mask(unsigned long mask, int vector)
L
Linus Torvalds 已提交
58 59 60
{
	unsigned long flags;

61
	local_irq_save(flags);
62
	__default_send_IPI_dest_field(mask, vector, apic->dest_logical);
L
Linus Torvalds 已提交
63 64 65
	local_irq_restore(flags);
}

66
static void flat_send_IPI_mask(const struct cpumask *cpumask, int vector)
67
{
68
	unsigned long mask = cpumask_bits(cpumask)[0];
69 70 71 72

	_flat_send_IPI_mask(mask, vector);
}

73
static void
74
flat_send_IPI_mask_allbutself(const struct cpumask *cpumask, int vector)
75
{
76
	unsigned long mask = cpumask_bits(cpumask)[0];
77 78 79 80
	int cpu = smp_processor_id();

	if (cpu < BITS_PER_LONG)
		clear_bit(cpu, &mask);
81

82 83 84
	_flat_send_IPI_mask(mask, vector);
}

85 86
static void flat_send_IPI_allbutself(int vector)
{
87
	int cpu = smp_processor_id();
88 89
#ifdef	CONFIG_HOTPLUG_CPU
	int hotplug = 1;
90
#else
91 92 93
	int hotplug = 0;
#endif
	if (hotplug || vector == NMI_VECTOR) {
94 95
		if (!cpumask_equal(cpu_online_mask, cpumask_of(cpu))) {
			unsigned long mask = cpumask_bits(cpu_online_mask)[0];
96

97 98
			if (cpu < BITS_PER_LONG)
				clear_bit(cpu, &mask);
99

100 101
			_flat_send_IPI_mask(mask, vector);
		}
102
	} else if (num_online_cpus() > 1) {
103 104
		__default_send_IPI_shortcut(APIC_DEST_ALLBUT,
					    vector, apic->dest_logical);
105
	}
106 107 108 109
}

static void flat_send_IPI_all(int vector)
{
110
	if (vector == NMI_VECTOR) {
111
		flat_send_IPI_mask(cpu_online_mask, vector);
112 113 114 115
	} else {
		__default_send_IPI_shortcut(APIC_DEST_ALLINC,
					    vector, apic->dest_logical);
	}
116 117
}

118
static unsigned int flat_get_apic_id(unsigned long x)
Y
Yinghai Lu 已提交
119
{
120
	return (x >> 24) & 0xFF;
Y
Yinghai Lu 已提交
121 122
}

123
static u32 set_apic_id(unsigned int id)
Y
Yinghai Lu 已提交
124
{
125
	return (id & 0xFF) << 24;
Y
Yinghai Lu 已提交
126 127
}

128 129
static unsigned int read_xapic_id(void)
{
130
	return flat_get_apic_id(apic_read(APIC_ID));
131 132
}

L
Linus Torvalds 已提交
133 134
static int flat_apic_id_registered(void)
{
135
	return physid_isset(read_xapic_id(), phys_cpu_present_map);
L
Linus Torvalds 已提交
136 137
}

I
Ingo Molnar 已提交
138
static int flat_phys_pkg_id(int initial_apic_id, int index_msb)
L
Linus Torvalds 已提交
139
{
140
	return initial_apic_id >> index_msb;
L
Linus Torvalds 已提交
141 142
}

Y
Yinghai Lu 已提交
143 144 145 146 147
static int flat_probe(void)
{
	return 1;
}

148
static struct apic apic_flat __ro_after_init = {
I
Ingo Molnar 已提交
149
	.name				= "flat",
Y
Yinghai Lu 已提交
150
	.probe				= flat_probe,
I
Ingo Molnar 已提交
151
	.acpi_madt_oem_check		= flat_acpi_madt_oem_check,
152
	.apic_id_valid			= default_apic_id_valid,
I
Ingo Molnar 已提交
153 154
	.apic_id_registered		= flat_apic_id_registered,

155
	.irq_delivery_mode		= dest_Fixed,
156
	.irq_dest_mode			= 1, /* logical */
I
Ingo Molnar 已提交
157

158
	.disable_esr			= 0,
159
	.dest_logical			= APIC_DEST_LOGICAL,
I
Ingo Molnar 已提交
160 161 162 163 164 165
	.check_apicid_used		= NULL,

	.init_apic_ldr			= flat_init_apic_ldr,

	.ioapic_phys_id_map		= NULL,
	.setup_apic_routing		= NULL,
166
	.cpu_present_to_apicid		= default_cpu_present_to_apicid,
I
Ingo Molnar 已提交
167
	.apicid_to_cpu_present		= NULL,
168
	.check_phys_apicid_present	= default_check_phys_apicid_present,
I
Ingo Molnar 已提交
169
	.phys_pkg_id			= flat_phys_pkg_id,
I
Ingo Molnar 已提交
170

171
	.get_apic_id			= flat_get_apic_id,
I
Ingo Molnar 已提交
172 173
	.set_apic_id			= set_apic_id,

174
	.calc_dest_apicid		= apic_flat_calc_apicid,
I
Ingo Molnar 已提交
175

176
	.send_IPI			= default_send_IPI_single,
I
Ingo Molnar 已提交
177 178 179 180 181 182
	.send_IPI_mask			= flat_send_IPI_mask,
	.send_IPI_mask_allbutself	= flat_send_IPI_mask_allbutself,
	.send_IPI_allbutself		= flat_send_IPI_allbutself,
	.send_IPI_all			= flat_send_IPI_all,
	.send_IPI_self			= apic_send_IPI_self,

183
	.inquire_remote_apic		= default_inquire_remote_apic,
Y
Yinghai Lu 已提交
184 185 186

	.read				= native_apic_mem_read,
	.write				= native_apic_mem_write,
187
	.eoi_write			= native_apic_mem_write,
Y
Yinghai Lu 已提交
188 189 190 191
	.icr_read			= native_apic_icr_read,
	.icr_write			= native_apic_icr_write,
	.wait_icr_idle			= native_apic_wait_icr_idle,
	.safe_wait_icr_idle		= native_safe_apic_wait_icr_idle,
L
Linus Torvalds 已提交
192
};
193 194

/*
195
 * Physflat mode is used when there are more than 8 CPUs on a system.
196 197 198
 * We cannot use logical delivery in this case because the mask
 * overflows, so use physical mode.
 */
199
static int physflat_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
200 201 202 203 204 205 206
{
#ifdef CONFIG_ACPI
	/*
	 * Quirk: some x86_64 machines can only use physical APIC mode
	 * regardless of how many processors are present (x86_64 ES7000
	 * is an example).
	 */
207
	if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
208 209
		(acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL)) {
		printk(KERN_DEBUG "system APIC only can use physical flat");
210
		return 1;
211
	}
212 213 214 215 216

	if (!strncmp(oem_id, "IBM", 3) && !strncmp(oem_table_id, "EXA", 3)) {
		printk(KERN_DEBUG "IBM Summit detected, will use apic physical");
		return 1;
	}
217 218 219 220
#endif

	return 0;
}
221

222 223 224 225 226 227 228 229 230
static void physflat_init_apic_ldr(void)
{
	/*
	 * LDR and DFR are not involved in physflat mode, rather:
	 * "In physical destination mode, the destination processor is
	 * specified by its local APIC ID [...]." (Intel SDM, 10.6.2.1)
	 */
}

231 232
static void physflat_send_IPI_allbutself(int vector)
{
233
	default_send_IPI_mask_allbutself_phys(cpu_online_mask, vector);
234 235 236 237
}

static void physflat_send_IPI_all(int vector)
{
238
	default_send_IPI_mask_sequence_phys(cpu_online_mask, vector);
239 240
}

241 242
static int physflat_probe(void)
{
243 244
	if (apic == &apic_physflat || num_possible_cpus() > 8 ||
	    jailhouse_paravirt())
245 246 247 248 249
		return 1;

	return 0;
}

250
static struct apic apic_physflat __ro_after_init = {
I
Ingo Molnar 已提交
251 252

	.name				= "physical flat",
253
	.probe				= physflat_probe,
I
Ingo Molnar 已提交
254
	.acpi_madt_oem_check		= physflat_acpi_madt_oem_check,
255
	.apic_id_valid			= default_apic_id_valid,
I
Ingo Molnar 已提交
256 257
	.apic_id_registered		= flat_apic_id_registered,

258
	.irq_delivery_mode		= dest_Fixed,
259
	.irq_dest_mode			= 0, /* physical */
I
Ingo Molnar 已提交
260

261
	.disable_esr			= 0,
262
	.dest_logical			= 0,
I
Ingo Molnar 已提交
263 264
	.check_apicid_used		= NULL,

265
	.init_apic_ldr			= physflat_init_apic_ldr,
I
Ingo Molnar 已提交
266 267 268

	.ioapic_phys_id_map		= NULL,
	.setup_apic_routing		= NULL,
269
	.cpu_present_to_apicid		= default_cpu_present_to_apicid,
I
Ingo Molnar 已提交
270
	.apicid_to_cpu_present		= NULL,
271
	.check_phys_apicid_present	= default_check_phys_apicid_present,
I
Ingo Molnar 已提交
272
	.phys_pkg_id			= flat_phys_pkg_id,
I
Ingo Molnar 已提交
273

274
	.get_apic_id			= flat_get_apic_id,
I
Ingo Molnar 已提交
275 276
	.set_apic_id			= set_apic_id,

277
	.calc_dest_apicid		= apic_default_calc_apicid,
I
Ingo Molnar 已提交
278

279
	.send_IPI			= default_send_IPI_single_phys,
280 281
	.send_IPI_mask			= default_send_IPI_mask_sequence_phys,
	.send_IPI_mask_allbutself	= default_send_IPI_mask_allbutself_phys,
I
Ingo Molnar 已提交
282 283 284 285
	.send_IPI_allbutself		= physflat_send_IPI_allbutself,
	.send_IPI_all			= physflat_send_IPI_all,
	.send_IPI_self			= apic_send_IPI_self,

286
	.inquire_remote_apic		= default_inquire_remote_apic,
Y
Yinghai Lu 已提交
287 288 289

	.read				= native_apic_mem_read,
	.write				= native_apic_mem_write,
290
	.eoi_write			= native_apic_mem_write,
Y
Yinghai Lu 已提交
291 292 293 294
	.icr_read			= native_apic_icr_read,
	.icr_write			= native_apic_icr_write,
	.wait_icr_idle			= native_apic_wait_icr_idle,
	.safe_wait_icr_idle		= native_safe_apic_wait_icr_idle,
295
};
296 297 298 299 300

/*
 * We need to check for physflat first, so this order is important.
 */
apic_drivers(apic_physflat, apic_flat);