x2apic_cluster.c 7.0 KB
Newer Older
1 2 3 4 5
#include <linux/threads.h>
#include <linux/cpumask.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/ctype.h>
6
#include <linux/dmar.h>
7
#include <linux/irq.h>
8
#include <linux/cpu.h>
9

10
#include <asm/smp.h>
11
#include "x2apic.h"
12

13 14 15 16 17 18
struct cluster_mask {
	unsigned int	clusterid;
	int		node;
	struct cpumask	mask;
};

19
static DEFINE_PER_CPU(u32, x86_cpu_to_logical_apicid);
20
static DEFINE_PER_CPU(cpumask_var_t, ipi_mask);
21 22
static DEFINE_PER_CPU(struct cluster_mask *, cluster_masks);
static struct cluster_mask *cluster_hotplug_mask;
23

24
static int x2apic_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
25
{
26
	return x2apic_enabled();
27 28
}

29 30 31 32 33 34 35 36
static void x2apic_send_IPI(int cpu, int vector)
{
	u32 dest = per_cpu(x86_cpu_to_logical_apicid, cpu);

	x2apic_wrmsr_fence();
	__x2apic_send_IPI_dest(dest, vector, APIC_DEST_LOGICAL);
}

37 38
static void
__x2apic_send_IPI_mask(const struct cpumask *mask, int vector, int apic_dest)
39
{
40 41
	unsigned int cpu, clustercpu;
	struct cpumask *tmpmsk;
42
	unsigned long flags;
43
	u32 dest;
44

45
	x2apic_wrmsr_fence();
46
	local_irq_save(flags);
47

48 49 50 51 52
	tmpmsk = this_cpu_cpumask_var_ptr(ipi_mask);
	cpumask_copy(tmpmsk, mask);
	/* If IPI should not be sent to self, clear current CPU */
	if (apic_dest != APIC_DEST_ALLINC)
		cpumask_clear_cpu(smp_processor_id(), tmpmsk);
53

54 55 56
	/* Collapse cpus in a cluster so a single IPI per cluster is sent */
	for_each_cpu(cpu, tmpmsk) {
		struct cluster_mask *cmsk = per_cpu(cluster_masks, cpu);
57 58

		dest = 0;
59 60
		for_each_cpu_and(clustercpu, tmpmsk, &cmsk->mask)
			dest |= per_cpu(x86_cpu_to_logical_apicid, clustercpu);
61 62

		if (!dest)
63
			continue;
64 65

		__x2apic_send_IPI_dest(dest, vector, apic->dest_logical);
66 67
		/* Remove cluster CPUs from tmpmask */
		cpumask_andnot(tmpmsk, tmpmsk, &cmsk->mask);
68
	}
69

70 71 72
	local_irq_restore(flags);
}

73 74 75 76 77
static void x2apic_send_IPI_mask(const struct cpumask *mask, int vector)
{
	__x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLINC);
}

78
static void
79
x2apic_send_IPI_mask_allbutself(const struct cpumask *mask, int vector)
80
{
81
	__x2apic_send_IPI_mask(mask, vector, APIC_DEST_ALLBUT);
82
}
83

84 85
static void x2apic_send_IPI_allbutself(int vector)
{
86
	__x2apic_send_IPI_mask(cpu_online_mask, vector, APIC_DEST_ALLBUT);
87 88 89 90
}

static void x2apic_send_IPI_all(int vector)
{
91
	__x2apic_send_IPI_mask(cpu_online_mask, vector, APIC_DEST_ALLINC);
92 93
}

94
static int
95 96
x2apic_cpu_mask_to_apicid(const struct cpumask *mask, struct irq_data *irqdata,
			  unsigned int *apicid)
M
Mike Travis 已提交
97
{
98
	struct cpumask *effmsk = irq_data_get_effective_affinity_mask(irqdata);
99
	struct cluster_mask *cmsk;
100
	unsigned int cpu;
101
	u32 dest = 0;
102

103
	cpu = cpumask_first(mask);
104
	if (cpu >= nr_cpu_ids)
105
		return -EINVAL;
106

107
	cmsk = per_cpu(cluster_masks, cpu);
108
	cpumask_clear(effmsk);
109
	for_each_cpu_and(cpu, &cmsk->mask, mask) {
110
		dest |= per_cpu(x86_cpu_to_logical_apicid, cpu);
111
		cpumask_set_cpu(cpu, effmsk);
112
	}
113 114
	*apicid = dest;
	return 0;
M
Mike Travis 已提交
115 116
}

117
static void init_x2apic_ldr(void)
118
{
119 120
	struct cluster_mask *cmsk = this_cpu_read(cluster_masks);
	u32 cluster, apicid = apic_read(APIC_LDR);
121 122
	unsigned int cpu;

123
	this_cpu_write(x86_cpu_to_logical_apicid, apicid);
124

125 126 127 128
	if (cmsk)
		goto update;

	cluster = apicid >> 16;
129
	for_each_online_cpu(cpu) {
130 131 132 133
		cmsk = per_cpu(cluster_masks, cpu);
		/* Matching cluster found. Link and update it. */
		if (cmsk && cmsk->clusterid == cluster)
			goto update;
134
	}
135 136 137 138 139
	cmsk = cluster_hotplug_mask;
	cluster_hotplug_mask = NULL;
update:
	this_cpu_write(cluster_masks, cmsk);
	cpumask_set_cpu(smp_processor_id(), &cmsk->mask);
140 141
}

142
static int alloc_clustermask(unsigned int cpu, int node)
143
{
144 145 146 147 148 149 150 151 152 153 154
	if (per_cpu(cluster_masks, cpu))
		return 0;
	/*
	 * If a hotplug spare mask exists, check whether it's on the right
	 * node. If not, free it and allocate a new one.
	 */
	if (cluster_hotplug_mask) {
		if (cluster_hotplug_mask->node == node)
			return 0;
		kfree(cluster_hotplug_mask);
	}
155

156 157 158
	cluster_hotplug_mask = kzalloc_node(sizeof(*cluster_hotplug_mask),
					    GFP_KERNEL, node);
	if (!cluster_hotplug_mask)
159
		return -ENOMEM;
160 161 162
	cluster_hotplug_mask->node = node;
	return 0;
}
163

164 165 166 167 168 169
static int x2apic_prepare_cpu(unsigned int cpu)
{
	if (alloc_clustermask(cpu, cpu_to_node(cpu)) < 0)
		return -ENOMEM;
	if (!zalloc_cpumask_var(&per_cpu(ipi_mask, cpu), GFP_KERNEL))
		return -ENOMEM;
170
	return 0;
171 172
}

173
static int x2apic_dead_cpu(unsigned int dead_cpu)
174
{
175
	struct cluster_mask *cmsk = per_cpu(cluster_masks, dead_cpu);
176

177 178
	cpumask_clear_cpu(smp_processor_id(), &cmsk->mask);
	free_cpumask_var(per_cpu(ipi_mask, dead_cpu));
179
	return 0;
180 181
}

182 183
static int x2apic_cluster_probe(void)
{
184
	if (!x2apic_mode)
185
		return 0;
186

187 188
	if (cpuhp_setup_state(CPUHP_X2APIC_PREPARE, "x86/x2apic:prepare",
			      x2apic_prepare_cpu, x2apic_dead_cpu) < 0) {
189 190 191
		pr_err("Failed to register X2APIC_PREPARE\n");
		return 0;
	}
192
	init_x2apic_ldr();
193
	return 1;
194 195
}

196 197 198 199 200
static const struct cpumask *x2apic_cluster_target_cpus(void)
{
	return cpu_all_mask;
}

201 202 203
/*
 * Each x2apic cluster is an allocation domain.
 */
204 205
static void cluster_vector_allocation_domain(int cpu, struct cpumask *retmask,
					     const struct cpumask *mask)
206
{
207 208
	struct cluster_mask *cmsk = per_cpu(cluster_masks, cpu);

209 210 211 212 213 214 215 216 217 218 219 220
	/*
	 * To minimize vector pressure, default case of boot, device bringup
	 * etc will use a single cpu for the interrupt destination.
	 *
	 * On explicit migration requests coming from irqbalance etc,
	 * interrupts will be routed to the x2apic cluster (cluster-id
	 * derived from the first cpu in the mask) members specified
	 * in the mask.
	 */
	if (mask == x2apic_cluster_target_cpus())
		cpumask_copy(retmask, cpumask_of(cpu));
	else
221
		cpumask_and(retmask, mask, &cmsk->mask);
222 223
}

224
static struct apic apic_x2apic_cluster __ro_after_init = {
I
Ingo Molnar 已提交
225 226

	.name				= "cluster x2apic",
227
	.probe				= x2apic_cluster_probe,
I
Ingo Molnar 已提交
228
	.acpi_madt_oem_check		= x2apic_acpi_madt_oem_check,
229
	.apic_id_valid			= x2apic_apic_id_valid,
I
Ingo Molnar 已提交
230 231
	.apic_id_registered		= x2apic_apic_id_registered,

232
	.irq_delivery_mode		= dest_LowestPrio,
233
	.irq_dest_mode			= 1, /* logical */
I
Ingo Molnar 已提交
234

235
	.target_cpus			= x2apic_cluster_target_cpus,
236
	.disable_esr			= 0,
237
	.dest_logical			= APIC_DEST_LOGICAL,
I
Ingo Molnar 已提交
238 239
	.check_apicid_used		= NULL,

240
	.vector_allocation_domain	= cluster_vector_allocation_domain,
I
Ingo Molnar 已提交
241 242 243 244
	.init_apic_ldr			= init_x2apic_ldr,

	.ioapic_phys_id_map		= NULL,
	.setup_apic_routing		= NULL,
245
	.cpu_present_to_apicid		= default_cpu_present_to_apicid,
I
Ingo Molnar 已提交
246
	.apicid_to_cpu_present		= NULL,
247
	.check_phys_apicid_present	= default_check_phys_apicid_present,
248
	.phys_pkg_id			= x2apic_phys_pkg_id,
I
Ingo Molnar 已提交
249

250 251
	.get_apic_id			= x2apic_get_apic_id,
	.set_apic_id			= x2apic_set_apic_id,
I
Ingo Molnar 已提交
252

253
	.cpu_mask_to_apicid		= x2apic_cpu_mask_to_apicid,
I
Ingo Molnar 已提交
254

255
	.send_IPI			= x2apic_send_IPI,
I
Ingo Molnar 已提交
256 257 258 259 260 261 262
	.send_IPI_mask			= x2apic_send_IPI_mask,
	.send_IPI_mask_allbutself	= x2apic_send_IPI_mask_allbutself,
	.send_IPI_allbutself		= x2apic_send_IPI_allbutself,
	.send_IPI_all			= x2apic_send_IPI_all,
	.send_IPI_self			= x2apic_send_IPI_self,

	.inquire_remote_apic		= NULL,
Y
Yinghai Lu 已提交
263 264 265

	.read				= native_apic_msr_read,
	.write				= native_apic_msr_write,
266
	.eoi_write			= native_apic_msr_eoi_write,
Y
Yinghai Lu 已提交
267 268 269 270
	.icr_read			= native_x2apic_icr_read,
	.icr_write			= native_x2apic_icr_write,
	.wait_icr_idle			= native_x2apic_wait_icr_idle,
	.safe_wait_icr_idle		= native_safe_x2apic_wait_icr_idle,
271
};
272 273

apic_driver(apic_x2apic_cluster);