blob: ac0e22826357a7c3ad74a9b22ff461577f68747f [file] [log] [blame]
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001/*
2 * Xen event channels
3 *
4 * Xen models interrupts with abstract event channels. Because each
5 * domain gets 1024 event channels, but NR_IRQ is not that large, we
6 * must dynamically map irqs<->event channels. The event channels
7 * interface with the rest of the kernel by defining a xen interrupt
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008 * chip. When an event is received, it is mapped to an irq and sent
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07009 * through the normal interrupt processing path.
10 *
11 * There are four kinds of events which can be mapped to an event
12 * channel:
13 *
14 * 1. Inter-domain notifications. This includes all the virtual
15 * device events, since they're driven by front-ends in another domain
16 * (typically dom0).
17 * 2. VIRQs, typically used for timers. These are per-cpu events.
18 * 3. IPIs.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -040019 * 4. PIRQs - Hardware interrupts.
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070020 *
21 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
22 */
23
24#include <linux/linkage.h>
25#include <linux/interrupt.h>
26#include <linux/irq.h>
27#include <linux/module.h>
28#include <linux/string.h>
Christophe Saout28e08862009-01-11 11:46:23 -080029#include <linux/bootmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -040031#include <linux/irqnr.h>
Qing Hef731e3ef2010-10-11 15:30:09 +010032#include <linux/pci.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070033
Sheng Yang38e20b02010-05-14 12:40:51 +010034#include <asm/desc.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070035#include <asm/ptrace.h>
36#include <asm/irq.h>
Jeremy Fitzhardinge792dc4f2009-02-06 14:09:43 -080037#include <asm/idle.h>
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -040038#include <asm/io_apic.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070039#include <asm/sync_bitops.h>
Stefano Stabellini42a1de52010-06-24 16:42:04 +010040#include <asm/xen/pci.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070041#include <asm/xen/hypercall.h>
Adrian Bunk8d1b8752007-07-20 00:31:44 -070042#include <asm/xen/hypervisor.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070043
Sheng Yang38e20b02010-05-14 12:40:51 +010044#include <xen/xen.h>
45#include <xen/hvm.h>
Isaku Yamahatae04d0d02008-04-02 10:53:55 -070046#include <xen/xen-ops.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070047#include <xen/events.h>
48#include <xen/interface/xen.h>
49#include <xen/interface/event_channel.h>
Sheng Yang38e20b02010-05-14 12:40:51 +010050#include <xen/interface/hvm/hvm_op.h>
51#include <xen/interface/hvm/params.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070052
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070053/*
54 * This lock protects updates to the following mapping and reference-count
55 * arrays. The lock does not need to be acquired to read the mapping tables.
56 */
57static DEFINE_SPINLOCK(irq_mapping_update_lock);
58
Ian Campbell6cb65372011-03-10 16:08:11 +000059static LIST_HEAD(xen_irq_list_head);
60
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070061/* IRQ <-> VIRQ mapping. */
Tejun Heo204fba42009-06-24 15:13:45 +090062static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070063
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070064/* IRQ <-> IPI mapping */
Tejun Heo204fba42009-06-24 15:13:45 +090065static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070066
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080067/* Interrupt types. */
68enum xen_irq_type {
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -080069 IRQT_UNBOUND = 0,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070070 IRQT_PIRQ,
71 IRQT_VIRQ,
72 IRQT_IPI,
73 IRQT_EVTCHN
74};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070075
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080076/*
77 * Packed IRQ information:
78 * type - enum xen_irq_type
79 * event channel - irq->event channel mapping
80 * cpu - cpu this event channel is bound to
81 * index - type-specific information:
Stefano Stabellini42a1de52010-06-24 16:42:04 +010082 * PIRQ - vector, with MSB being "needs EIO", or physical IRQ of the HVM
83 * guest, or GSI (real passthrough IRQ) of the device.
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080084 * VIRQ - virq number
85 * IPI - IPI vector
86 * EVTCHN -
87 */
88struct irq_info
89{
Ian Campbell6cb65372011-03-10 16:08:11 +000090 struct list_head list;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080091 enum xen_irq_type type; /* type */
Ian Campbell6cb65372011-03-10 16:08:11 +000092 unsigned irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080093 unsigned short evtchn; /* event channel */
94 unsigned short cpu; /* cpu bound */
95
96 union {
97 unsigned short virq;
98 enum ipi_vector ipi;
99 struct {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100100 unsigned short pirq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800101 unsigned short gsi;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400102 unsigned char vector;
103 unsigned char flags;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400104 uint16_t domid;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800105 } pirq;
106 } u;
107};
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400108#define PIRQ_NEEDS_EOI (1 << 0)
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400109#define PIRQ_SHAREABLE (1 << 1)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800110
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400111static int *evtchn_to_irq;
Jeremy Fitzhardinge3b32f572009-08-13 12:50:37 -0700112
Ian Campbellcb60d112011-03-10 16:08:08 +0000113static DEFINE_PER_CPU(unsigned long [NR_EVENT_CHANNELS/BITS_PER_LONG],
114 cpu_evtchn_mask);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700115
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700116/* Xen will never allocate port zero for any purpose. */
117#define VALID_EVTCHN(chn) ((chn) != 0)
118
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700119static struct irq_chip xen_dynamic_chip;
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700120static struct irq_chip xen_percpu_chip;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400121static struct irq_chip xen_pirq_chip;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700122
Ian Campbell9158c352011-03-10 16:08:09 +0000123/* Get info for IRQ */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800124static struct irq_info *info_for_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700125{
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100126 return irq_get_handler_data(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700127}
128
Ian Campbell9158c352011-03-10 16:08:09 +0000129/* Constructors for packed IRQ information. */
130static void xen_irq_info_common_init(struct irq_info *info,
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000131 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000132 enum xen_irq_type type,
133 unsigned short evtchn,
134 unsigned short cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700135{
Ian Campbell9158c352011-03-10 16:08:09 +0000136
137 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
138
139 info->type = type;
Ian Campbell6cb65372011-03-10 16:08:11 +0000140 info->irq = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000141 info->evtchn = evtchn;
142 info->cpu = cpu;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000143
144 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700145}
146
Ian Campbell9158c352011-03-10 16:08:09 +0000147static void xen_irq_info_evtchn_init(unsigned irq,
148 unsigned short evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700149{
Ian Campbell9158c352011-03-10 16:08:09 +0000150 struct irq_info *info = info_for_irq(irq);
151
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000152 xen_irq_info_common_init(info, irq, IRQT_EVTCHN, evtchn, 0);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700153}
154
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000155static void xen_irq_info_ipi_init(unsigned cpu,
156 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000157 unsigned short evtchn,
158 enum ipi_vector ipi)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700159{
Ian Campbell9158c352011-03-10 16:08:09 +0000160 struct irq_info *info = info_for_irq(irq);
161
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000162 xen_irq_info_common_init(info, irq, IRQT_IPI, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000163
164 info->u.ipi = ipi;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000165
166 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700167}
168
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000169static void xen_irq_info_virq_init(unsigned cpu,
170 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000171 unsigned short evtchn,
172 unsigned short virq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700173{
Ian Campbell9158c352011-03-10 16:08:09 +0000174 struct irq_info *info = info_for_irq(irq);
175
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000176 xen_irq_info_common_init(info, irq, IRQT_VIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000177
178 info->u.virq = virq;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000179
180 per_cpu(virq_to_irq, cpu)[virq] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000181}
182
183static void xen_irq_info_pirq_init(unsigned irq,
184 unsigned short evtchn,
185 unsigned short pirq,
186 unsigned short gsi,
187 unsigned short vector,
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400188 uint16_t domid,
Ian Campbell9158c352011-03-10 16:08:09 +0000189 unsigned char flags)
190{
191 struct irq_info *info = info_for_irq(irq);
192
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000193 xen_irq_info_common_init(info, irq, IRQT_PIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000194
195 info->u.pirq.pirq = pirq;
196 info->u.pirq.gsi = gsi;
197 info->u.pirq.vector = vector;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400198 info->u.pirq.domid = domid;
Ian Campbell9158c352011-03-10 16:08:09 +0000199 info->u.pirq.flags = flags;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700200}
201
202/*
203 * Accessors for packed IRQ information.
204 */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800205static unsigned int evtchn_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700206{
Joe Jin110e7c72011-01-07 14:50:12 +0800207 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
208 return 0;
209
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800210 return info_for_irq(irq)->evtchn;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700211}
212
Ian Campbelld4c04532009-02-06 19:20:31 -0800213unsigned irq_from_evtchn(unsigned int evtchn)
214{
215 return evtchn_to_irq[evtchn];
216}
217EXPORT_SYMBOL_GPL(irq_from_evtchn);
218
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800219static enum ipi_vector ipi_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700220{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800221 struct irq_info *info = info_for_irq(irq);
222
223 BUG_ON(info == NULL);
224 BUG_ON(info->type != IRQT_IPI);
225
226 return info->u.ipi;
227}
228
229static unsigned virq_from_irq(unsigned irq)
230{
231 struct irq_info *info = info_for_irq(irq);
232
233 BUG_ON(info == NULL);
234 BUG_ON(info->type != IRQT_VIRQ);
235
236 return info->u.virq;
237}
238
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100239static unsigned pirq_from_irq(unsigned irq)
240{
241 struct irq_info *info = info_for_irq(irq);
242
243 BUG_ON(info == NULL);
244 BUG_ON(info->type != IRQT_PIRQ);
245
246 return info->u.pirq.pirq;
247}
248
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800249static enum xen_irq_type type_from_irq(unsigned irq)
250{
251 return info_for_irq(irq)->type;
252}
253
254static unsigned cpu_from_irq(unsigned irq)
255{
256 return info_for_irq(irq)->cpu;
257}
258
259static unsigned int cpu_from_evtchn(unsigned int evtchn)
260{
261 int irq = evtchn_to_irq[evtchn];
262 unsigned ret = 0;
263
264 if (irq != -1)
265 ret = cpu_from_irq(irq);
266
267 return ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700268}
269
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400270static bool pirq_needs_eoi(unsigned irq)
271{
272 struct irq_info *info = info_for_irq(irq);
273
274 BUG_ON(info->type != IRQT_PIRQ);
275
276 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
277}
278
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700279static inline unsigned long active_evtchns(unsigned int cpu,
280 struct shared_info *sh,
281 unsigned int idx)
282{
283 return (sh->evtchn_pending[idx] &
Ian Campbellcb60d112011-03-10 16:08:08 +0000284 per_cpu(cpu_evtchn_mask, cpu)[idx] &
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700285 ~sh->evtchn_mask[idx]);
286}
287
288static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
289{
290 int irq = evtchn_to_irq[chn];
291
292 BUG_ON(irq == -1);
293#ifdef CONFIG_SMP
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000294 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700295#endif
296
Ian Campbellcb60d112011-03-10 16:08:08 +0000297 clear_bit(chn, per_cpu(cpu_evtchn_mask, cpu_from_irq(irq)));
298 set_bit(chn, per_cpu(cpu_evtchn_mask, cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700299
Ian Campbellca62ce82011-03-10 16:08:12 +0000300 info_for_irq(irq)->cpu = cpu;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700301}
302
303static void init_evtchn_cpu_bindings(void)
304{
Jan Beulich1c6969e2010-11-16 14:55:33 -0800305 int i;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700306#ifdef CONFIG_SMP
Ian Campbell6cb65372011-03-10 16:08:11 +0000307 struct irq_info *info;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200308
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700309 /* By default all event channels notify CPU#0. */
Ian Campbell6cb65372011-03-10 16:08:11 +0000310 list_for_each_entry(info, &xen_irq_list_head, list) {
311 struct irq_desc *desc = irq_to_desc(info->irq);
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000312 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800313 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700314#endif
315
Jan Beulich1c6969e2010-11-16 14:55:33 -0800316 for_each_possible_cpu(i)
Ian Campbellcb60d112011-03-10 16:08:08 +0000317 memset(per_cpu(cpu_evtchn_mask, i),
318 (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700319}
320
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700321static inline void clear_evtchn(int port)
322{
323 struct shared_info *s = HYPERVISOR_shared_info;
324 sync_clear_bit(port, &s->evtchn_pending[0]);
325}
326
327static inline void set_evtchn(int port)
328{
329 struct shared_info *s = HYPERVISOR_shared_info;
330 sync_set_bit(port, &s->evtchn_pending[0]);
331}
332
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700333static inline int test_evtchn(int port)
334{
335 struct shared_info *s = HYPERVISOR_shared_info;
336 return sync_test_bit(port, &s->evtchn_pending[0]);
337}
338
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700339
340/**
341 * notify_remote_via_irq - send event to remote end of event channel via irq
342 * @irq: irq of event channel to send event to
343 *
344 * Unlike notify_remote_via_evtchn(), this is safe to use across
345 * save/restore. Notifications on a broken connection are silently
346 * dropped.
347 */
348void notify_remote_via_irq(int irq)
349{
350 int evtchn = evtchn_from_irq(irq);
351
352 if (VALID_EVTCHN(evtchn))
353 notify_remote_via_evtchn(evtchn);
354}
355EXPORT_SYMBOL_GPL(notify_remote_via_irq);
356
357static void mask_evtchn(int port)
358{
359 struct shared_info *s = HYPERVISOR_shared_info;
360 sync_set_bit(port, &s->evtchn_mask[0]);
361}
362
363static void unmask_evtchn(int port)
364{
365 struct shared_info *s = HYPERVISOR_shared_info;
366 unsigned int cpu = get_cpu();
367
368 BUG_ON(!irqs_disabled());
369
370 /* Slow path (hypercall) if this is a non-local port. */
371 if (unlikely(cpu != cpu_from_evtchn(port))) {
372 struct evtchn_unmask unmask = { .port = port };
373 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
374 } else {
Christoph Lameter780f36d2010-12-06 11:16:29 -0600375 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700376
377 sync_clear_bit(port, &s->evtchn_mask[0]);
378
379 /*
380 * The following is basically the equivalent of
381 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
382 * the interrupt edge' if the channel is masked.
383 */
384 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
385 !sync_test_and_set_bit(port / BITS_PER_LONG,
386 &vcpu_info->evtchn_pending_sel))
387 vcpu_info->evtchn_upcall_pending = 1;
388 }
389
390 put_cpu();
391}
392
Ian Campbell6cb65372011-03-10 16:08:11 +0000393static void xen_irq_init(unsigned irq)
394{
395 struct irq_info *info;
396 struct irq_desc *desc = irq_to_desc(irq);
397
Konrad Rzeszutek Wilk44626e42011-03-15 16:40:33 -0400398#ifdef CONFIG_SMP
Ian Campbell6cb65372011-03-10 16:08:11 +0000399 /* By default all event channels notify CPU#0. */
400 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Konrad Rzeszutek Wilk44626e42011-03-15 16:40:33 -0400401#endif
Ian Campbell6cb65372011-03-10 16:08:11 +0000402
Ian Campbellca62ce82011-03-10 16:08:12 +0000403 info = kzalloc(sizeof(*info), GFP_KERNEL);
404 if (info == NULL)
405 panic("Unable to allocate metadata for IRQ%d\n", irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000406
407 info->type = IRQT_UNBOUND;
408
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100409 irq_set_handler_data(irq, info);
Ian Campbellca62ce82011-03-10 16:08:12 +0000410
Ian Campbell6cb65372011-03-10 16:08:11 +0000411 list_add_tail(&info->list, &xen_irq_list_head);
412}
413
Ian Campbell7bee9762011-03-10 16:08:15 +0000414static int __must_check xen_allocate_irq_dynamic(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700415{
Ian Campbell89911502011-03-03 11:57:44 -0500416 int first = 0;
417 int irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700418
Ian Campbell89911502011-03-03 11:57:44 -0500419#ifdef CONFIG_X86_IO_APIC
420 /*
421 * For an HVM guest or domain 0 which see "real" (emulated or
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300422 * actual respectively) GSIs we allocate dynamic IRQs
Ian Campbell89911502011-03-03 11:57:44 -0500423 * e.g. those corresponding to event channels or MSIs
424 * etc. from the range above those "real" GSIs to avoid
425 * collisions.
Konrad Rzeszutek Wilkd1b758e2010-12-09 14:53:29 -0500426 */
Ian Campbell89911502011-03-03 11:57:44 -0500427 if (xen_initial_domain() || xen_hvm_domain())
428 first = get_nr_irqs_gsi();
429#endif
430
Ian Campbell89911502011-03-03 11:57:44 -0500431 irq = irq_alloc_desc_from(first, -1);
432
Ian Campbell6cb65372011-03-10 16:08:11 +0000433 xen_irq_init(irq);
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800434
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700435 return irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400436}
437
Ian Campbell7bee9762011-03-10 16:08:15 +0000438static int __must_check xen_allocate_irq_gsi(unsigned gsi)
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000439{
440 int irq;
441
Ian Campbell89911502011-03-03 11:57:44 -0500442 /*
443 * A PV guest has no concept of a GSI (since it has no ACPI
444 * nor access to/knowledge of the physical APICs). Therefore
445 * all IRQs are dynamically allocated from the entire IRQ
446 * space.
447 */
448 if (xen_pv_domain() && !xen_initial_domain())
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000449 return xen_allocate_irq_dynamic();
450
451 /* Legacy IRQ descriptors are already allocated by the arch. */
452 if (gsi < NR_IRQS_LEGACY)
Ian Campbell6cb65372011-03-10 16:08:11 +0000453 irq = gsi;
454 else
455 irq = irq_alloc_desc_at(gsi, -1);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000456
Ian Campbell6cb65372011-03-10 16:08:11 +0000457 xen_irq_init(irq);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000458
459 return irq;
460}
461
462static void xen_free_irq(unsigned irq)
463{
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100464 struct irq_info *info = irq_get_handler_data(irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000465
466 list_del(&info->list);
Ian Campbell9158c352011-03-10 16:08:09 +0000467
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100468 irq_set_handler_data(irq, NULL);
Ian Campbellca62ce82011-03-10 16:08:12 +0000469
470 kfree(info);
471
Ian Campbell72146102011-02-03 09:49:35 +0000472 /* Legacy IRQ descriptors are managed by the arch. */
473 if (irq < NR_IRQS_LEGACY)
474 return;
475
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000476 irq_free_desc(irq);
477}
478
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400479static void pirq_unmask_notify(int irq)
480{
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100481 struct physdev_eoi eoi = { .irq = pirq_from_irq(irq) };
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400482
483 if (unlikely(pirq_needs_eoi(irq))) {
484 int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
485 WARN_ON(rc);
486 }
487}
488
489static void pirq_query_unmask(int irq)
490{
491 struct physdev_irq_status_query irq_status;
492 struct irq_info *info = info_for_irq(irq);
493
494 BUG_ON(info->type != IRQT_PIRQ);
495
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100496 irq_status.irq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400497 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
498 irq_status.flags = 0;
499
500 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
501 if (irq_status.flags & XENIRQSTAT_needs_eoi)
502 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
503}
504
505static bool probing_irq(int irq)
506{
507 struct irq_desc *desc = irq_to_desc(irq);
508
509 return desc && desc->action == NULL;
510}
511
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000512static unsigned int __startup_pirq(unsigned int irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400513{
514 struct evtchn_bind_pirq bind_pirq;
515 struct irq_info *info = info_for_irq(irq);
516 int evtchn = evtchn_from_irq(irq);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400517 int rc;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400518
519 BUG_ON(info->type != IRQT_PIRQ);
520
521 if (VALID_EVTCHN(evtchn))
522 goto out;
523
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100524 bind_pirq.pirq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400525 /* NB. We are happy to share unless we are probing. */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400526 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
527 BIND_PIRQ__WILL_SHARE : 0;
528 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
529 if (rc != 0) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400530 if (!probing_irq(irq))
531 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
532 irq);
533 return 0;
534 }
535 evtchn = bind_pirq.port;
536
537 pirq_query_unmask(irq);
538
539 evtchn_to_irq[evtchn] = irq;
540 bind_evtchn_to_cpu(evtchn, 0);
541 info->evtchn = evtchn;
542
543out:
544 unmask_evtchn(evtchn);
545 pirq_unmask_notify(irq);
546
547 return 0;
548}
549
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000550static unsigned int startup_pirq(struct irq_data *data)
551{
552 return __startup_pirq(data->irq);
553}
554
555static void shutdown_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400556{
557 struct evtchn_close close;
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000558 unsigned int irq = data->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400559 struct irq_info *info = info_for_irq(irq);
560 int evtchn = evtchn_from_irq(irq);
561
562 BUG_ON(info->type != IRQT_PIRQ);
563
564 if (!VALID_EVTCHN(evtchn))
565 return;
566
567 mask_evtchn(evtchn);
568
569 close.port = evtchn;
570 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
571 BUG();
572
573 bind_evtchn_to_cpu(evtchn, 0);
574 evtchn_to_irq[evtchn] = -1;
575 info->evtchn = 0;
576}
577
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000578static void enable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400579{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000580 startup_pirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400581}
582
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000583static void disable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400584{
585}
586
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000587static void ack_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400588{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000589 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400590
Thomas Gleixnera3b975c2011-03-24 21:31:25 +0100591 irq_move_irq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400592
593 if (VALID_EVTCHN(evtchn)) {
594 mask_evtchn(evtchn);
595 clear_evtchn(evtchn);
596 }
597}
598
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400599static int find_irq_by_gsi(unsigned gsi)
600{
Ian Campbell6cb65372011-03-10 16:08:11 +0000601 struct irq_info *info;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400602
Ian Campbell6cb65372011-03-10 16:08:11 +0000603 list_for_each_entry(info, &xen_irq_list_head, list) {
604 if (info->type != IRQT_PIRQ)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400605 continue;
606
Ian Campbell6cb65372011-03-10 16:08:11 +0000607 if (info->u.pirq.gsi == gsi)
608 return info->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400609 }
610
611 return -1;
612}
613
Ian Campbellf4d06352011-03-10 16:08:07 +0000614int xen_allocate_pirq_gsi(unsigned gsi)
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100615{
Ian Campbellf4d06352011-03-10 16:08:07 +0000616 return gsi;
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100617}
618
Ian Campbell653378a2011-03-10 16:08:04 +0000619/*
620 * Do not make any assumptions regarding the relationship between the
621 * IRQ number returned here and the Xen pirq argument.
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100622 *
623 * Note: We don't assign an event channel until the irq actually started
624 * up. Return an existing irq if we've already got one for the gsi.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400625 */
Ian Campbellf4d06352011-03-10 16:08:07 +0000626int xen_bind_pirq_gsi_to_irq(unsigned gsi,
627 unsigned pirq, int shareable, char *name)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400628{
Ian Campbella0e18112011-03-10 16:08:03 +0000629 int irq = -1;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400630 struct physdev_irq irq_op;
631
632 spin_lock(&irq_mapping_update_lock);
633
634 irq = find_irq_by_gsi(gsi);
635 if (irq != -1) {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100636 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400637 irq, gsi);
638 goto out; /* XXX need refcount? */
639 }
640
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000641 irq = xen_allocate_irq_gsi(gsi);
Ian Campbell7bee9762011-03-10 16:08:15 +0000642 if (irq < 0)
643 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400644
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100645 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_level_irq,
646 name);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400647
648 irq_op.irq = irq;
Alex Nixonb5401a92010-03-18 16:31:34 -0400649 irq_op.vector = 0;
650
651 /* Only the privileged domain can do this. For non-priv, the pcifront
652 * driver provides a PCI bus that does the call to do exactly
653 * this in the priv domain. */
654 if (xen_initial_domain() &&
655 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000656 xen_free_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400657 irq = -ENOSPC;
658 goto out;
659 }
660
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400661 xen_irq_info_pirq_init(irq, 0, pirq, gsi, irq_op.vector, DOMID_SELF,
Ian Campbell9158c352011-03-10 16:08:09 +0000662 shareable ? PIRQ_SHAREABLE : 0);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400663
664out:
665 spin_unlock(&irq_mapping_update_lock);
666
667 return irq;
668}
669
Qing Hef731e3ef2010-10-11 15:30:09 +0100670#ifdef CONFIG_PCI_MSI
Ian Campbellbf480d92011-02-18 16:43:32 +0000671int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000672{
Ian Campbell5cad61a2011-02-18 16:43:31 +0000673 int rc;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000674 struct physdev_get_free_pirq op_get_free_pirq;
Ian Campbell5cad61a2011-02-18 16:43:31 +0000675
Ian Campbellbf480d92011-02-18 16:43:32 +0000676 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000677 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000678
Ian Campbell5cad61a2011-02-18 16:43:31 +0000679 WARN_ONCE(rc == -ENOSYS,
680 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
681
682 return rc ? -1 : op_get_free_pirq.pirq;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000683}
684
Ian Campbellbf480d92011-02-18 16:43:32 +0000685int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400686 int pirq, int vector, const char *name,
687 domid_t domid)
Stefano Stabellini809f9262010-07-01 17:10:39 +0100688{
Ian Campbellbf480d92011-02-18 16:43:32 +0000689 int irq, ret;
Ian Campbell4b41df72011-02-18 16:43:29 +0000690
Stefano Stabellini809f9262010-07-01 17:10:39 +0100691 spin_lock(&irq_mapping_update_lock);
692
Ian Campbell4b41df72011-02-18 16:43:29 +0000693 irq = xen_allocate_irq_dynamic();
694 if (irq == -1)
Ian Campbellbb5d0792011-02-18 16:43:28 +0000695 goto out;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100696
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100697 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_level_irq,
698 name);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100699
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400700 xen_irq_info_pirq_init(irq, 0, pirq, 0, vector, domid, 0);
Linus Torvalds5f6fb452011-03-15 19:23:40 -0700701 ret = irq_set_msi_desc(irq, msidesc);
Ian Campbellbf480d92011-02-18 16:43:32 +0000702 if (ret < 0)
703 goto error_irq;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100704out:
705 spin_unlock(&irq_mapping_update_lock);
Ian Campbell4b41df72011-02-18 16:43:29 +0000706 return irq;
Ian Campbellbf480d92011-02-18 16:43:32 +0000707error_irq:
708 spin_unlock(&irq_mapping_update_lock);
709 xen_free_irq(irq);
710 return -1;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100711}
Qing Hef731e3ef2010-10-11 15:30:09 +0100712#endif
713
Alex Nixonb5401a92010-03-18 16:31:34 -0400714int xen_destroy_irq(int irq)
715{
716 struct irq_desc *desc;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100717 struct physdev_unmap_pirq unmap_irq;
718 struct irq_info *info = info_for_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400719 int rc = -ENOENT;
720
721 spin_lock(&irq_mapping_update_lock);
722
723 desc = irq_to_desc(irq);
724 if (!desc)
725 goto out;
726
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100727 if (xen_initial_domain()) {
Konrad Rzeszutek Wilk12334712010-11-19 11:27:09 -0500728 unmap_irq.pirq = info->u.pirq.pirq;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400729 unmap_irq.domid = info->u.pirq.domid;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100730 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
731 if (rc) {
732 printk(KERN_WARNING "unmap irq failed %d\n", rc);
733 goto out;
734 }
735 }
Alex Nixonb5401a92010-03-18 16:31:34 -0400736
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000737 xen_free_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400738
739out:
740 spin_unlock(&irq_mapping_update_lock);
741 return rc;
742}
743
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000744int xen_irq_from_pirq(unsigned pirq)
745{
Ian Campbell69c358c2011-03-10 16:08:13 +0000746 int irq;
747
748 struct irq_info *info;
749
750 spin_lock(&irq_mapping_update_lock);
751
752 list_for_each_entry(info, &xen_irq_list_head, list) {
753 if (info == NULL || info->type != IRQT_PIRQ)
754 continue;
755 irq = info->irq;
756 if (info->u.pirq.pirq == pirq)
757 goto out;
758 }
759 irq = -1;
760out:
Ian Campbella7b807c2011-03-14 09:50:39 +0000761 spin_unlock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000762
763 return irq;
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000764}
765
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700766int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700767{
768 int irq;
769
770 spin_lock(&irq_mapping_update_lock);
771
772 irq = evtchn_to_irq[evtchn];
773
774 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000775 irq = xen_allocate_irq_dynamic();
Ian Campbell7bee9762011-03-10 16:08:15 +0000776 if (irq == -1)
777 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700778
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100779 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -0700780 handle_fasteoi_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700781
Ian Campbell9158c352011-03-10 16:08:09 +0000782 xen_irq_info_evtchn_init(irq, evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700783 }
784
Ian Campbell7bee9762011-03-10 16:08:15 +0000785out:
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700786 spin_unlock(&irq_mapping_update_lock);
787
788 return irq;
789}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700790EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700791
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700792static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
793{
794 struct evtchn_bind_ipi bind_ipi;
795 int evtchn, irq;
796
797 spin_lock(&irq_mapping_update_lock);
798
799 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800800
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700801 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000802 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700803 if (irq < 0)
804 goto out;
805
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100806 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700807 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700808
809 bind_ipi.vcpu = cpu;
810 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
811 &bind_ipi) != 0)
812 BUG();
813 evtchn = bind_ipi.port;
814
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000815 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700816
817 bind_evtchn_to_cpu(evtchn, cpu);
818 }
819
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700820 out:
821 spin_unlock(&irq_mapping_update_lock);
822 return irq;
823}
824
Ian Campbell2e820f52009-02-09 12:05:50 -0800825static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
826 unsigned int remote_port)
827{
828 struct evtchn_bind_interdomain bind_interdomain;
829 int err;
830
831 bind_interdomain.remote_dom = remote_domain;
832 bind_interdomain.remote_port = remote_port;
833
834 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
835 &bind_interdomain);
836
837 return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
838}
839
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700840
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100841int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700842{
843 struct evtchn_bind_virq bind_virq;
844 int evtchn, irq;
845
846 spin_lock(&irq_mapping_update_lock);
847
848 irq = per_cpu(virq_to_irq, cpu)[virq];
849
850 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000851 irq = xen_allocate_irq_dynamic();
Ian Campbell7bee9762011-03-10 16:08:15 +0000852 if (irq == -1)
853 goto out;
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700854
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100855 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700856 handle_percpu_irq, "virq");
857
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700858 bind_virq.virq = virq;
859 bind_virq.vcpu = cpu;
860 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
861 &bind_virq) != 0)
862 BUG();
863 evtchn = bind_virq.port;
864
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000865 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700866
867 bind_evtchn_to_cpu(evtchn, cpu);
868 }
869
Ian Campbell7bee9762011-03-10 16:08:15 +0000870out:
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700871 spin_unlock(&irq_mapping_update_lock);
872
873 return irq;
874}
875
876static void unbind_from_irq(unsigned int irq)
877{
878 struct evtchn_close close;
879 int evtchn = evtchn_from_irq(irq);
880
881 spin_lock(&irq_mapping_update_lock);
882
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800883 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700884 close.port = evtchn;
885 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
886 BUG();
887
888 switch (type_from_irq(irq)) {
889 case IRQT_VIRQ:
890 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800891 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700892 break;
Alex Nixond68d82a2008-08-22 11:52:15 +0100893 case IRQT_IPI:
894 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800895 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +0100896 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700897 default:
898 break;
899 }
900
901 /* Closed ports are implicitly re-bound to VCPU0. */
902 bind_evtchn_to_cpu(evtchn, 0);
903
904 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +0000905 }
906
Ian Campbellca62ce82011-03-10 16:08:12 +0000907 BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700908
Ian Campbell9158c352011-03-10 16:08:09 +0000909 xen_free_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700910
911 spin_unlock(&irq_mapping_update_lock);
912}
913
914int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -0400915 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700916 unsigned long irqflags,
917 const char *devname, void *dev_id)
918{
919 unsigned int irq;
920 int retval;
921
922 irq = bind_evtchn_to_irq(evtchn);
Ian Campbell7bee9762011-03-10 16:08:15 +0000923 if (irq < 0)
924 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700925 retval = request_irq(irq, handler, irqflags, devname, dev_id);
926 if (retval != 0) {
927 unbind_from_irq(irq);
928 return retval;
929 }
930
931 return irq;
932}
933EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
934
Ian Campbell2e820f52009-02-09 12:05:50 -0800935int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
936 unsigned int remote_port,
937 irq_handler_t handler,
938 unsigned long irqflags,
939 const char *devname,
940 void *dev_id)
941{
942 int irq, retval;
943
944 irq = bind_interdomain_evtchn_to_irq(remote_domain, remote_port);
945 if (irq < 0)
946 return irq;
947
948 retval = request_irq(irq, handler, irqflags, devname, dev_id);
949 if (retval != 0) {
950 unbind_from_irq(irq);
951 return retval;
952 }
953
954 return irq;
955}
956EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
957
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700958int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -0400959 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700960 unsigned long irqflags, const char *devname, void *dev_id)
961{
962 unsigned int irq;
963 int retval;
964
965 irq = bind_virq_to_irq(virq, cpu);
Ian Campbell7bee9762011-03-10 16:08:15 +0000966 if (irq < 0)
967 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700968 retval = request_irq(irq, handler, irqflags, devname, dev_id);
969 if (retval != 0) {
970 unbind_from_irq(irq);
971 return retval;
972 }
973
974 return irq;
975}
976EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
977
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700978int bind_ipi_to_irqhandler(enum ipi_vector ipi,
979 unsigned int cpu,
980 irq_handler_t handler,
981 unsigned long irqflags,
982 const char *devname,
983 void *dev_id)
984{
985 int irq, retval;
986
987 irq = bind_ipi_to_irq(ipi, cpu);
988 if (irq < 0)
989 return irq;
990
Thomas Gleixner676dc3c2011-02-05 20:08:59 +0000991 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700992 retval = request_irq(irq, handler, irqflags, devname, dev_id);
993 if (retval != 0) {
994 unbind_from_irq(irq);
995 return retval;
996 }
997
998 return irq;
999}
1000
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001001void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1002{
1003 free_irq(irq, dev_id);
1004 unbind_from_irq(irq);
1005}
1006EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1007
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001008void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1009{
1010 int irq = per_cpu(ipi_to_irq, cpu)[vector];
1011 BUG_ON(irq < 0);
1012 notify_remote_via_irq(irq);
1013}
1014
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001015irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
1016{
1017 struct shared_info *sh = HYPERVISOR_shared_info;
1018 int cpu = smp_processor_id();
Ian Campbellcb60d112011-03-10 16:08:08 +00001019 unsigned long *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001020 int i;
1021 unsigned long flags;
1022 static DEFINE_SPINLOCK(debug_lock);
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001023 struct vcpu_info *v;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001024
1025 spin_lock_irqsave(&debug_lock, flags);
1026
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001027 printk("\nvcpu %d\n ", cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001028
1029 for_each_online_cpu(i) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001030 int pending;
1031 v = per_cpu(xen_vcpu, i);
1032 pending = (get_irq_regs() && i == cpu)
1033 ? xen_irqs_disabled(get_irq_regs())
1034 : v->evtchn_upcall_mask;
1035 printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i,
1036 pending, v->evtchn_upcall_pending,
1037 (int)(sizeof(v->evtchn_pending_sel)*2),
1038 v->evtchn_pending_sel);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001039 }
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001040 v = per_cpu(xen_vcpu, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001041
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001042 printk("\npending:\n ");
1043 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
1044 printk("%0*lx%s", (int)sizeof(sh->evtchn_pending[0])*2,
1045 sh->evtchn_pending[i],
1046 i % 8 == 0 ? "\n " : " ");
1047 printk("\nglobal mask:\n ");
1048 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1049 printk("%0*lx%s",
1050 (int)(sizeof(sh->evtchn_mask[0])*2),
1051 sh->evtchn_mask[i],
1052 i % 8 == 0 ? "\n " : " ");
1053
1054 printk("\nglobally unmasked:\n ");
1055 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1056 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1057 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1058 i % 8 == 0 ? "\n " : " ");
1059
1060 printk("\nlocal cpu%d mask:\n ", cpu);
1061 for (i = (NR_EVENT_CHANNELS/BITS_PER_LONG)-1; i >= 0; i--)
1062 printk("%0*lx%s", (int)(sizeof(cpu_evtchn[0])*2),
1063 cpu_evtchn[i],
1064 i % 8 == 0 ? "\n " : " ");
1065
1066 printk("\nlocally unmasked:\n ");
1067 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
1068 unsigned long pending = sh->evtchn_pending[i]
1069 & ~sh->evtchn_mask[i]
1070 & cpu_evtchn[i];
1071 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1072 pending, i % 8 == 0 ? "\n " : " ");
1073 }
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001074
1075 printk("\npending list:\n");
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001076 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001077 if (sync_test_bit(i, sh->evtchn_pending)) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001078 int word_idx = i / BITS_PER_LONG;
1079 printk(" %d: event %d -> irq %d%s%s%s\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001080 cpu_from_evtchn(i), i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001081 evtchn_to_irq[i],
1082 sync_test_bit(word_idx, &v->evtchn_pending_sel)
1083 ? "" : " l2-clear",
1084 !sync_test_bit(i, sh->evtchn_mask)
1085 ? "" : " globally-masked",
1086 sync_test_bit(i, cpu_evtchn)
1087 ? "" : " locally-masked");
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001088 }
1089 }
1090
1091 spin_unlock_irqrestore(&debug_lock, flags);
1092
1093 return IRQ_HANDLED;
1094}
1095
Tejun Heo245b2e72009-06-24 15:13:48 +09001096static DEFINE_PER_CPU(unsigned, xed_nesting_count);
Keir Fraserada68142011-03-03 10:01:11 +00001097static DEFINE_PER_CPU(unsigned int, current_word_idx);
1098static DEFINE_PER_CPU(unsigned int, current_bit_idx);
Tejun Heo245b2e72009-06-24 15:13:48 +09001099
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001100/*
Scott Rixnerab7f8632011-03-03 09:30:08 +00001101 * Mask out the i least significant bits of w
1102 */
1103#define MASK_LSBS(w, i) (w & ((~0UL) << i))
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001104
1105/*
1106 * Search the CPUs pending events bitmasks. For each one found, map
1107 * the event number to an irq, and feed it into do_IRQ() for
1108 * handling.
1109 *
1110 * Xen uses a two-level bitmap to speed searching. The first level is
1111 * a bitset of words which contain pending event bits. The second
1112 * level is a bitset of pending events themselves.
1113 */
Sheng Yang38e20b02010-05-14 12:40:51 +01001114static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001115{
Keir Fraser24b51c22011-03-03 11:06:28 +00001116 int start_word_idx, start_bit_idx;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001117 int word_idx, bit_idx;
Keir Fraser24b51c22011-03-03 11:06:28 +00001118 int i;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001119 int cpu = get_cpu();
1120 struct shared_info *s = HYPERVISOR_shared_info;
Christoph Lameter780f36d2010-12-06 11:16:29 -06001121 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001122 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001123
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001124 do {
1125 unsigned long pending_words;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001126
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001127 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001128
Christoph Lameterb2e4ae62010-12-06 11:40:07 -06001129 if (__this_cpu_inc_return(xed_nesting_count) - 1)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001130 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001131
Isaku Yamahatae849c3e2008-04-02 10:53:56 -07001132#ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
1133 /* Clear master flag /before/ clearing selector flag. */
Isaku Yamahata6673cf62008-06-16 14:58:13 -07001134 wmb();
Isaku Yamahatae849c3e2008-04-02 10:53:56 -07001135#endif
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001136 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001137
Keir Fraser24b51c22011-03-03 11:06:28 +00001138 start_word_idx = __this_cpu_read(current_word_idx);
1139 start_bit_idx = __this_cpu_read(current_bit_idx);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001140
Keir Fraser24b51c22011-03-03 11:06:28 +00001141 word_idx = start_word_idx;
1142
1143 for (i = 0; pending_words != 0; i++) {
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001144 unsigned long pending_bits;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001145 unsigned long words;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001146
Scott Rixnerab7f8632011-03-03 09:30:08 +00001147 words = MASK_LSBS(pending_words, word_idx);
1148
1149 /*
Keir Fraserada68142011-03-03 10:01:11 +00001150 * If we masked out all events, wrap to beginning.
Scott Rixnerab7f8632011-03-03 09:30:08 +00001151 */
1152 if (words == 0) {
Keir Fraserada68142011-03-03 10:01:11 +00001153 word_idx = 0;
1154 bit_idx = 0;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001155 continue;
1156 }
1157 word_idx = __ffs(words);
1158
Keir Fraser24b51c22011-03-03 11:06:28 +00001159 pending_bits = active_evtchns(cpu, s, word_idx);
1160 bit_idx = 0; /* usually scan entire word from start */
1161 if (word_idx == start_word_idx) {
1162 /* We scan the starting word in two parts */
1163 if (i == 0)
1164 /* 1st time: start in the middle */
1165 bit_idx = start_bit_idx;
1166 else
1167 /* 2nd time: mask bits done already */
1168 bit_idx &= (1UL << start_bit_idx) - 1;
1169 }
1170
Scott Rixnerab7f8632011-03-03 09:30:08 +00001171 do {
1172 unsigned long bits;
1173 int port, irq;
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001174 struct irq_desc *desc;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001175
Scott Rixnerab7f8632011-03-03 09:30:08 +00001176 bits = MASK_LSBS(pending_bits, bit_idx);
1177
1178 /* If we masked out all events, move on. */
Keir Fraserada68142011-03-03 10:01:11 +00001179 if (bits == 0)
Scott Rixnerab7f8632011-03-03 09:30:08 +00001180 break;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001181
1182 bit_idx = __ffs(bits);
1183
1184 /* Process port. */
1185 port = (word_idx * BITS_PER_LONG) + bit_idx;
1186 irq = evtchn_to_irq[port];
1187
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001188 mask_evtchn(port);
1189 clear_evtchn(port);
1190
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001191 if (irq != -1) {
1192 desc = irq_to_desc(irq);
1193 if (desc)
1194 generic_handle_irq_desc(irq, desc);
1195 }
Scott Rixnerab7f8632011-03-03 09:30:08 +00001196
Keir Fraserada68142011-03-03 10:01:11 +00001197 bit_idx = (bit_idx + 1) % BITS_PER_LONG;
1198
1199 /* Next caller starts at last processed + 1 */
1200 __this_cpu_write(current_word_idx,
1201 bit_idx ? word_idx :
1202 (word_idx+1) % BITS_PER_LONG);
1203 __this_cpu_write(current_bit_idx, bit_idx);
1204 } while (bit_idx != 0);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001205
Keir Fraser24b51c22011-03-03 11:06:28 +00001206 /* Scan start_l1i twice; all others once. */
1207 if ((word_idx != start_word_idx) || (i != 0))
Scott Rixnerab7f8632011-03-03 09:30:08 +00001208 pending_words &= ~(1UL << word_idx);
Keir Fraserada68142011-03-03 10:01:11 +00001209
1210 word_idx = (word_idx + 1) % BITS_PER_LONG;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001211 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001212
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001213 BUG_ON(!irqs_disabled());
1214
Christoph Lameter780f36d2010-12-06 11:16:29 -06001215 count = __this_cpu_read(xed_nesting_count);
1216 __this_cpu_write(xed_nesting_count, 0);
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001217 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001218
1219out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -08001220
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001221 put_cpu();
1222}
1223
Sheng Yang38e20b02010-05-14 12:40:51 +01001224void xen_evtchn_do_upcall(struct pt_regs *regs)
1225{
1226 struct pt_regs *old_regs = set_irq_regs(regs);
1227
1228 exit_idle();
1229 irq_enter();
1230
1231 __xen_evtchn_do_upcall();
1232
1233 irq_exit();
1234 set_irq_regs(old_regs);
1235}
1236
1237void xen_hvm_evtchn_do_upcall(void)
1238{
1239 __xen_evtchn_do_upcall();
1240}
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001241EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +01001242
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001243/* Rebind a new event channel to an existing irq. */
1244void rebind_evtchn_irq(int evtchn, int irq)
1245{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001246 struct irq_info *info = info_for_irq(irq);
1247
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001248 /* Make sure the irq is masked, since the new event channel
1249 will also be masked. */
1250 disable_irq(irq);
1251
1252 spin_lock(&irq_mapping_update_lock);
1253
1254 /* After resume the irq<->evtchn mappings are all cleared out */
1255 BUG_ON(evtchn_to_irq[evtchn] != -1);
1256 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001257 so there should be a proper type */
1258 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001259
Ian Campbell9158c352011-03-10 16:08:09 +00001260 xen_irq_info_evtchn_init(irq, evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001261
1262 spin_unlock(&irq_mapping_update_lock);
1263
1264 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +10301265 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001266
1267 /* Unmask the event channel. */
1268 enable_irq(irq);
1269}
1270
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001271/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -07001272static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001273{
1274 struct evtchn_bind_vcpu bind_vcpu;
1275 int evtchn = evtchn_from_irq(irq);
1276
Ian Campbellbe494722011-03-10 16:08:02 +00001277 if (!VALID_EVTCHN(evtchn))
1278 return -1;
1279
1280 /*
1281 * Events delivered via platform PCI interrupts are always
1282 * routed to vcpu 0 and hence cannot be rebound.
1283 */
1284 if (xen_hvm_domain() && !xen_have_vector_callback)
Yinghai Lud5dedd42009-04-27 17:59:21 -07001285 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001286
1287 /* Send future instances of this interrupt to other vcpu. */
1288 bind_vcpu.port = evtchn;
1289 bind_vcpu.vcpu = tcpu;
1290
1291 /*
1292 * If this fails, it usually just indicates that we're dealing with a
1293 * virq or IPI channel, which don't actually need to be rebound. Ignore
1294 * it, but don't do the xenlinux-level rebind in that case.
1295 */
1296 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1297 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001298
1299 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001300}
1301
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001302static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1303 bool force)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001304{
Rusty Russell0de26522008-12-13 21:20:26 +10301305 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001306
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001307 return rebind_irq_to_cpu(data->irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001308}
1309
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001310int resend_irq_on_evtchn(unsigned int irq)
1311{
1312 int masked, evtchn = evtchn_from_irq(irq);
1313 struct shared_info *s = HYPERVISOR_shared_info;
1314
1315 if (!VALID_EVTCHN(evtchn))
1316 return 1;
1317
1318 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
1319 sync_set_bit(evtchn, s->evtchn_pending);
1320 if (!masked)
1321 unmask_evtchn(evtchn);
1322
1323 return 1;
1324}
1325
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001326static void enable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001327{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001328 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001329
1330 if (VALID_EVTCHN(evtchn))
1331 unmask_evtchn(evtchn);
1332}
1333
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001334static void disable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001335{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001336 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001337
1338 if (VALID_EVTCHN(evtchn))
1339 mask_evtchn(evtchn);
1340}
1341
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001342static void ack_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001343{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001344 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001345
Thomas Gleixnera3b975c2011-03-24 21:31:25 +01001346 irq_move_masked_irq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001347
1348 if (VALID_EVTCHN(evtchn))
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001349 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001350}
1351
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001352static int retrigger_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001353{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001354 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001355 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001356 int ret = 0;
1357
1358 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001359 int masked;
1360
1361 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1362 sync_set_bit(evtchn, sh->evtchn_pending);
1363 if (!masked)
1364 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001365 ret = 1;
1366 }
1367
1368 return ret;
1369}
1370
Ian Campbell0a852262011-03-10 16:08:06 +00001371static void restore_pirqs(void)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001372{
1373 int pirq, rc, irq, gsi;
1374 struct physdev_map_pirq map_irq;
Ian Campbell69c358c2011-03-10 16:08:13 +00001375 struct irq_info *info;
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001376
Ian Campbell69c358c2011-03-10 16:08:13 +00001377 list_for_each_entry(info, &xen_irq_list_head, list) {
1378 if (info->type != IRQT_PIRQ)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001379 continue;
1380
Ian Campbell69c358c2011-03-10 16:08:13 +00001381 pirq = info->u.pirq.pirq;
1382 gsi = info->u.pirq.gsi;
1383 irq = info->irq;
1384
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001385 /* save/restore of PT devices doesn't work, so at this point the
1386 * only devices present are GSI based emulated devices */
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001387 if (!gsi)
1388 continue;
1389
1390 map_irq.domid = DOMID_SELF;
1391 map_irq.type = MAP_PIRQ_TYPE_GSI;
1392 map_irq.index = gsi;
1393 map_irq.pirq = pirq;
1394
1395 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1396 if (rc) {
1397 printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1398 gsi, irq, pirq, rc);
Ian Campbell9158c352011-03-10 16:08:09 +00001399 xen_free_irq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001400 continue;
1401 }
1402
1403 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1404
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001405 __startup_pirq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001406 }
1407}
1408
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001409static void restore_cpu_virqs(unsigned int cpu)
1410{
1411 struct evtchn_bind_virq bind_virq;
1412 int virq, irq, evtchn;
1413
1414 for (virq = 0; virq < NR_VIRQS; virq++) {
1415 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1416 continue;
1417
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001418 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001419
1420 /* Get a new binding from Xen. */
1421 bind_virq.virq = virq;
1422 bind_virq.vcpu = cpu;
1423 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1424 &bind_virq) != 0)
1425 BUG();
1426 evtchn = bind_virq.port;
1427
1428 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001429 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001430 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001431 }
1432}
1433
1434static void restore_cpu_ipis(unsigned int cpu)
1435{
1436 struct evtchn_bind_ipi bind_ipi;
1437 int ipi, irq, evtchn;
1438
1439 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1440 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1441 continue;
1442
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001443 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001444
1445 /* Get a new binding from Xen. */
1446 bind_ipi.vcpu = cpu;
1447 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1448 &bind_ipi) != 0)
1449 BUG();
1450 evtchn = bind_ipi.port;
1451
1452 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001453 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001454 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001455 }
1456}
1457
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001458/* Clear an irq's pending state, in preparation for polling on it */
1459void xen_clear_irq_pending(int irq)
1460{
1461 int evtchn = evtchn_from_irq(irq);
1462
1463 if (VALID_EVTCHN(evtchn))
1464 clear_evtchn(evtchn);
1465}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001466EXPORT_SYMBOL(xen_clear_irq_pending);
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001467void xen_set_irq_pending(int irq)
1468{
1469 int evtchn = evtchn_from_irq(irq);
1470
1471 if (VALID_EVTCHN(evtchn))
1472 set_evtchn(evtchn);
1473}
1474
1475bool xen_test_irq_pending(int irq)
1476{
1477 int evtchn = evtchn_from_irq(irq);
1478 bool ret = false;
1479
1480 if (VALID_EVTCHN(evtchn))
1481 ret = test_evtchn(evtchn);
1482
1483 return ret;
1484}
1485
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001486/* Poll waiting for an irq to become pending with timeout. In the usual case,
1487 * the irq will be disabled so it won't deliver an interrupt. */
1488void xen_poll_irq_timeout(int irq, u64 timeout)
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001489{
1490 evtchn_port_t evtchn = evtchn_from_irq(irq);
1491
1492 if (VALID_EVTCHN(evtchn)) {
1493 struct sched_poll poll;
1494
1495 poll.nr_ports = 1;
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001496 poll.timeout = timeout;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001497 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001498
1499 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1500 BUG();
1501 }
1502}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001503EXPORT_SYMBOL(xen_poll_irq_timeout);
1504/* Poll waiting for an irq to become pending. In the usual case, the
1505 * irq will be disabled so it won't deliver an interrupt. */
1506void xen_poll_irq(int irq)
1507{
1508 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1509}
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001510
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001511void xen_irq_resume(void)
1512{
Ian Campbell6cb65372011-03-10 16:08:11 +00001513 unsigned int cpu, evtchn;
1514 struct irq_info *info;
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001515
1516 init_evtchn_cpu_bindings();
1517
1518 /* New event-channel space is not 'live' yet. */
1519 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1520 mask_evtchn(evtchn);
1521
1522 /* No IRQ <-> event-channel mappings. */
Ian Campbell6cb65372011-03-10 16:08:11 +00001523 list_for_each_entry(info, &xen_irq_list_head, list)
1524 info->evtchn = 0; /* zap event-channel binding */
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001525
1526 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1527 evtchn_to_irq[evtchn] = -1;
1528
1529 for_each_possible_cpu(cpu) {
1530 restore_cpu_virqs(cpu);
1531 restore_cpu_ipis(cpu);
1532 }
Ian Campbell69035912010-11-01 16:30:09 +00001533
Ian Campbell0a852262011-03-10 16:08:06 +00001534 restore_pirqs();
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001535}
1536
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001537static struct irq_chip xen_dynamic_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001538 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001539
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001540 .irq_disable = disable_dynirq,
1541 .irq_mask = disable_dynirq,
1542 .irq_unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001543
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001544 .irq_eoi = ack_dynirq,
1545 .irq_set_affinity = set_affinity_irq,
1546 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001547};
1548
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001549static struct irq_chip xen_pirq_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001550 .name = "xen-pirq",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001551
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001552 .irq_startup = startup_pirq,
1553 .irq_shutdown = shutdown_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001554
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001555 .irq_enable = enable_pirq,
1556 .irq_unmask = enable_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001557
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001558 .irq_disable = disable_pirq,
1559 .irq_mask = disable_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001560
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001561 .irq_ack = ack_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001562
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001563 .irq_set_affinity = set_affinity_irq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001564
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001565 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001566};
1567
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001568static struct irq_chip xen_percpu_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001569 .name = "xen-percpu",
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001570
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001571 .irq_disable = disable_dynirq,
1572 .irq_mask = disable_dynirq,
1573 .irq_unmask = enable_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001574
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001575 .irq_ack = ack_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001576};
1577
Sheng Yang38e20b02010-05-14 12:40:51 +01001578int xen_set_callback_via(uint64_t via)
1579{
1580 struct xen_hvm_param a;
1581 a.domid = DOMID_SELF;
1582 a.index = HVM_PARAM_CALLBACK_IRQ;
1583 a.value = via;
1584 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1585}
1586EXPORT_SYMBOL_GPL(xen_set_callback_via);
1587
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001588#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001589/* Vector callbacks are better than PCI interrupts to receive event
1590 * channel notifications because we can receive vector callbacks on any
1591 * vcpu and we don't need PCI support or APIC interactions. */
1592void xen_callback_vector(void)
1593{
1594 int rc;
1595 uint64_t callback_via;
1596 if (xen_have_vector_callback) {
1597 callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
1598 rc = xen_set_callback_via(callback_via);
1599 if (rc) {
1600 printk(KERN_ERR "Request for Xen HVM callback vector"
1601 " failed.\n");
1602 xen_have_vector_callback = 0;
1603 return;
1604 }
1605 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1606 "enabled\n");
1607 /* in the restore case the vector has already been allocated */
1608 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
1609 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
1610 }
1611}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001612#else
1613void xen_callback_vector(void) {}
1614#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001615
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001616void __init xen_init_IRQ(void)
1617{
Stefano Stabellinie5fc7342010-12-01 14:51:44 +00001618 int i;
Mike Travisc7a35892009-01-10 21:58:11 -08001619
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001620 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1621 GFP_KERNEL);
1622 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1623 evtchn_to_irq[i] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001624
1625 init_evtchn_cpu_bindings();
1626
1627 /* No event channels are 'live' right now. */
1628 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1629 mask_evtchn(i);
1630
Sheng Yang38e20b02010-05-14 12:40:51 +01001631 if (xen_hvm_domain()) {
1632 xen_callback_vector();
1633 native_init_IRQ();
Stefano Stabellini3942b742010-06-24 17:50:18 +01001634 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1635 * __acpi_register_gsi can point at the right function */
1636 pci_xen_hvm_init();
Sheng Yang38e20b02010-05-14 12:40:51 +01001637 } else {
1638 irq_ctx_init(smp_processor_id());
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +01001639 if (xen_initial_domain())
1640 xen_setup_pirqs();
Sheng Yang38e20b02010-05-14 12:40:51 +01001641 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001642}