aboutsummaryrefslogtreecommitdiffstats
path: root/linux-2.6-xen-sparse/arch/x86_64/kernel/genapic_xen.c
blob: 1171aad778876123e39b9332822e635c317d6b05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Copyright 2004 James Cleverdon, IBM.
 * Subject to the GNU Public License, v.2
 *
 * Xen APIC subarch code.  Maximum 8 CPUs, logical delivery.
 *
 * Hacked for x86-64 by James Cleverdon from i386 architecture code by
 * Martin Bligh, Andi Kleen, James Bottomley, John Stultz, and
 * James Cleverdon.
 *
 * Hacked to pieces for Xen by Chris Wright.
 */
#include <linux/threads.h>
#include <linux/cpumask.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/ctype.h>
#include <linux/init.h>
#ifdef CONFIG_XEN_PRIVILEGED_GUEST
#include <asm/smp.h>
#include <asm/ipi.h>
#else
#include <asm/apic.h>
#include <asm/apicdef.h>
#include <asm/genapic.h>
#endif
#include <xen/evtchn.h>

DECLARE_PER_CPU(int, ipi_to_irq[NR_IPIS]);

static inline void __send_IPI_one(unsigned int cpu, int vector)
{
	int irq = per_cpu(ipi_to_irq, cpu)[vector];
	BUG_ON(irq < 0);
	notify_remote_via_irq(irq);
}

void xen_send_IPI_shortcut(unsigned int shortcut, int vector, unsigned int dest)
{
	int cpu;

	switch (shortcut) {
	case APIC_DEST_SELF:
		__send_IPI_one(smp_processor_id(), vector);
		break;
	case APIC_DEST_ALLBUT:
		for (cpu = 0; cpu < NR_CPUS; ++cpu) {
			if (cpu == smp_processor_id())
				continue;
			if (cpu_isset(cpu, cpu_online_map)) {
				__send_IPI_one(cpu, vector);
			}
		}
		break;
	case APIC_DEST_ALLINC:
		for (cpu = 0; cpu < NR_CPUS; ++cpu) {
			if (cpu_isset(cpu, cpu_online_map)) {
				__send_IPI_one(cpu, vector);
			}
		}
		break;
	default:
		printk("XXXXXX __send_IPI_shortcut %08x vector %d\n", shortcut,
		       vector);
		break;
	}
}

static cpumask_t xen_target_cpus(void)
{
	return cpu_online_map;
}

/*
 * Set up the logical destination ID.
 * Do nothing, not called now.
 */
static void xen_init_apic_ldr(void)
{
	Dprintk("%s\n", __FUNCTION__);
	return;
}

static void xen_send_IPI_allbutself(int vector)
{
	/*
	 * if there are no other CPUs in the system then
	 * we get an APIC send error if we try to broadcast.
	 * thus we have to avoid sending IPIs in this case.
	 */
	Dprintk("%s\n", __FUNCTION__);
	if (num_online_cpus() > 1)
		xen_send_IPI_shortcut(APIC_DEST_ALLBUT, vector, APIC_DEST_LOGICAL);
}

static void xen_send_IPI_all(int vector)
{
	Dprintk("%s\n", __FUNCTION__);
	xen_send_IPI_shortcut(APIC_DEST_ALLINC, vector, APIC_DEST_LOGICAL);
}

static void xen_send_IPI_mask(cpumask_t cpumask, int vector)
{
	unsigned long mask = cpus_addr(cpumask)[0];
	unsigned int cpu;
	unsigned long flags;

	Dprintk("%s\n", __FUNCTION__);
	local_irq_save(flags);
	WARN_ON(mask & ~cpus_addr(cpu_online_map)[0]);

	for (cpu = 0; cpu < NR_CPUS; ++cpu) {
		if (cpu_isset(cpu, cpumask)) {
			__send_IPI_one(cpu, vector);
		}
	}
	local_irq_restore(flags);
}

#ifdef CONFIG_XEN_PRIVILEGED_GUEST
static int xen_apic_id_registered(void)
{
	/* better be set */
	Dprintk("%s\n", __FUNCTION__);
	return physid_isset(smp_processor_id(), phys_cpu_present_map);
}
#endif

static unsigned int xen_cpu_mask_to_apicid(cpumask_t cpumask)
{
	Dprintk("%s\n", __FUNCTION__);
	return cpus_addr(cpumask)[0] & APIC_ALL_CPUS;
}

static unsigned int phys_pkg_id(int index_msb)
{
	u32 ebx;

	Dprintk("%s\n", __FUNCTION__);
	ebx = cpuid_ebx(1);
	return ((ebx >> 24) & 0xFF) >> index_msb;
}

struct genapic apic_xen =  {
	.name = "xen",
#ifdef CONFIG_XEN_PRIVILEGED_GUEST
	.int_delivery_mode = dest_LowestPrio,
#endif
	.int_dest_mode = (APIC_DEST_LOGICAL != 0),
	.int_delivery_dest = APIC_DEST_LOGICAL | APIC_DM_LOWEST,
	.target_cpus = xen_target_cpus,
#ifdef CONFIG_XEN_PRIVILEGED_GUEST
	.apic_id_registered = xen_apic_id_registered,
#endif
	.init_apic_ldr = xen_init_apic_ldr,
	.send_IPI_all = xen_send_IPI_all,
	.send_IPI_allbutself = xen_send_IPI_allbutself,
	.send_IPI_mask = xen_send_IPI_mask,
	.cpu_mask_to_apicid = xen_cpu_mask_to_apicid,
	.phys_pkg_id = phys_pkg_id,
};