blob: 2dffa43696d85107c6a1b146262e999e1506f8c5 [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
8 * chip. When an event is recieved, it is mapped to an irq and sent
9 * 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
59/* IRQ <-> VIRQ mapping. */
Tejun Heo204fba42009-06-24 15:13:45 +090060static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070061
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070062/* IRQ <-> IPI mapping */
Tejun Heo204fba42009-06-24 15:13:45 +090063static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070064
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080065/* Interrupt types. */
66enum xen_irq_type {
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -080067 IRQT_UNBOUND = 0,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070068 IRQT_PIRQ,
69 IRQT_VIRQ,
70 IRQT_IPI,
71 IRQT_EVTCHN
72};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070073
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080074/*
75 * Packed IRQ information:
76 * type - enum xen_irq_type
77 * event channel - irq->event channel mapping
78 * cpu - cpu this event channel is bound to
79 * index - type-specific information:
Stefano Stabellini42a1de52010-06-24 16:42:04 +010080 * PIRQ - vector, with MSB being "needs EIO", or physical IRQ of the HVM
81 * guest, or GSI (real passthrough IRQ) of the device.
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080082 * VIRQ - virq number
83 * IPI - IPI vector
84 * EVTCHN -
85 */
86struct irq_info
87{
88 enum xen_irq_type type; /* type */
89 unsigned short evtchn; /* event channel */
90 unsigned short cpu; /* cpu bound */
91
92 union {
93 unsigned short virq;
94 enum ipi_vector ipi;
95 struct {
Stefano Stabellini7a043f12010-07-01 17:08:14 +010096 unsigned short pirq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080097 unsigned short gsi;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -040098 unsigned char vector;
99 unsigned char flags;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800100 } pirq;
101 } u;
102};
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400103#define PIRQ_NEEDS_EOI (1 << 0)
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400104#define PIRQ_SHAREABLE (1 << 1)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800105
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400106static struct irq_info *irq_info;
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100107static int *pirq_to_irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700108
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400109static int *evtchn_to_irq;
Jeremy Fitzhardinge3b32f572009-08-13 12:50:37 -0700110
Ian Campbellcb60d112011-03-10 16:08:08 +0000111static DEFINE_PER_CPU(unsigned long [NR_EVENT_CHANNELS/BITS_PER_LONG],
112 cpu_evtchn_mask);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700113
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700114/* Xen will never allocate port zero for any purpose. */
115#define VALID_EVTCHN(chn) ((chn) != 0)
116
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700117static struct irq_chip xen_dynamic_chip;
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700118static struct irq_chip xen_percpu_chip;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400119static struct irq_chip xen_pirq_chip;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700120
121/* Constructor for packed IRQ information. */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800122static struct irq_info mk_unbound_info(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700123{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800124 return (struct irq_info) { .type = IRQT_UNBOUND };
125}
126
127static struct irq_info mk_evtchn_info(unsigned short evtchn)
128{
Ian Campbell90af9512009-02-06 16:55:58 -0800129 return (struct irq_info) { .type = IRQT_EVTCHN, .evtchn = evtchn,
130 .cpu = 0 };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800131}
132
133static struct irq_info mk_ipi_info(unsigned short evtchn, enum ipi_vector ipi)
134{
135 return (struct irq_info) { .type = IRQT_IPI, .evtchn = evtchn,
Ian Campbell90af9512009-02-06 16:55:58 -0800136 .cpu = 0, .u.ipi = ipi };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800137}
138
139static struct irq_info mk_virq_info(unsigned short evtchn, unsigned short virq)
140{
141 return (struct irq_info) { .type = IRQT_VIRQ, .evtchn = evtchn,
Ian Campbell90af9512009-02-06 16:55:58 -0800142 .cpu = 0, .u.virq = virq };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800143}
144
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100145static struct irq_info mk_pirq_info(unsigned short evtchn, unsigned short pirq,
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800146 unsigned short gsi, unsigned short vector)
147{
148 return (struct irq_info) { .type = IRQT_PIRQ, .evtchn = evtchn,
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100149 .cpu = 0,
150 .u.pirq = { .pirq = pirq, .gsi = gsi, .vector = vector } };
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700151}
152
153/*
154 * Accessors for packed IRQ information.
155 */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800156static struct irq_info *info_for_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700157{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800158 return &irq_info[irq];
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700159}
160
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800161static unsigned int evtchn_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700162{
Joe Jin110e7c72011-01-07 14:50:12 +0800163 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
164 return 0;
165
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800166 return info_for_irq(irq)->evtchn;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700167}
168
Ian Campbelld4c04532009-02-06 19:20:31 -0800169unsigned irq_from_evtchn(unsigned int evtchn)
170{
171 return evtchn_to_irq[evtchn];
172}
173EXPORT_SYMBOL_GPL(irq_from_evtchn);
174
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800175static enum ipi_vector ipi_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700176{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800177 struct irq_info *info = info_for_irq(irq);
178
179 BUG_ON(info == NULL);
180 BUG_ON(info->type != IRQT_IPI);
181
182 return info->u.ipi;
183}
184
185static unsigned virq_from_irq(unsigned irq)
186{
187 struct irq_info *info = info_for_irq(irq);
188
189 BUG_ON(info == NULL);
190 BUG_ON(info->type != IRQT_VIRQ);
191
192 return info->u.virq;
193}
194
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100195static unsigned pirq_from_irq(unsigned irq)
196{
197 struct irq_info *info = info_for_irq(irq);
198
199 BUG_ON(info == NULL);
200 BUG_ON(info->type != IRQT_PIRQ);
201
202 return info->u.pirq.pirq;
203}
204
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800205static unsigned gsi_from_irq(unsigned irq)
206{
207 struct irq_info *info = info_for_irq(irq);
208
209 BUG_ON(info == NULL);
210 BUG_ON(info->type != IRQT_PIRQ);
211
212 return info->u.pirq.gsi;
213}
214
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800215static enum xen_irq_type type_from_irq(unsigned irq)
216{
217 return info_for_irq(irq)->type;
218}
219
220static unsigned cpu_from_irq(unsigned irq)
221{
222 return info_for_irq(irq)->cpu;
223}
224
225static unsigned int cpu_from_evtchn(unsigned int evtchn)
226{
227 int irq = evtchn_to_irq[evtchn];
228 unsigned ret = 0;
229
230 if (irq != -1)
231 ret = cpu_from_irq(irq);
232
233 return ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700234}
235
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400236static bool pirq_needs_eoi(unsigned irq)
237{
238 struct irq_info *info = info_for_irq(irq);
239
240 BUG_ON(info->type != IRQT_PIRQ);
241
242 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
243}
244
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700245static inline unsigned long active_evtchns(unsigned int cpu,
246 struct shared_info *sh,
247 unsigned int idx)
248{
249 return (sh->evtchn_pending[idx] &
Ian Campbellcb60d112011-03-10 16:08:08 +0000250 per_cpu(cpu_evtchn_mask, cpu)[idx] &
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700251 ~sh->evtchn_mask[idx]);
252}
253
254static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
255{
256 int irq = evtchn_to_irq[chn];
257
258 BUG_ON(irq == -1);
259#ifdef CONFIG_SMP
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000260 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700261#endif
262
Ian Campbellcb60d112011-03-10 16:08:08 +0000263 clear_bit(chn, per_cpu(cpu_evtchn_mask, cpu_from_irq(irq)));
264 set_bit(chn, per_cpu(cpu_evtchn_mask, cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700265
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800266 irq_info[irq].cpu = cpu;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700267}
268
269static void init_evtchn_cpu_bindings(void)
270{
Jan Beulich1c6969e2010-11-16 14:55:33 -0800271 int i;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700272#ifdef CONFIG_SMP
Thomas Gleixner10e58082008-10-16 14:19:04 +0200273 struct irq_desc *desc;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200274
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700275 /* By default all event channels notify CPU#0. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800276 for_each_irq_desc(i, desc) {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000277 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800278 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700279#endif
280
Jan Beulich1c6969e2010-11-16 14:55:33 -0800281 for_each_possible_cpu(i)
Ian Campbellcb60d112011-03-10 16:08:08 +0000282 memset(per_cpu(cpu_evtchn_mask, i),
283 (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
Jan Beulich1c6969e2010-11-16 14:55:33 -0800284
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700285}
286
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700287static inline void clear_evtchn(int port)
288{
289 struct shared_info *s = HYPERVISOR_shared_info;
290 sync_clear_bit(port, &s->evtchn_pending[0]);
291}
292
293static inline void set_evtchn(int port)
294{
295 struct shared_info *s = HYPERVISOR_shared_info;
296 sync_set_bit(port, &s->evtchn_pending[0]);
297}
298
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700299static inline int test_evtchn(int port)
300{
301 struct shared_info *s = HYPERVISOR_shared_info;
302 return sync_test_bit(port, &s->evtchn_pending[0]);
303}
304
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700305
306/**
307 * notify_remote_via_irq - send event to remote end of event channel via irq
308 * @irq: irq of event channel to send event to
309 *
310 * Unlike notify_remote_via_evtchn(), this is safe to use across
311 * save/restore. Notifications on a broken connection are silently
312 * dropped.
313 */
314void notify_remote_via_irq(int irq)
315{
316 int evtchn = evtchn_from_irq(irq);
317
318 if (VALID_EVTCHN(evtchn))
319 notify_remote_via_evtchn(evtchn);
320}
321EXPORT_SYMBOL_GPL(notify_remote_via_irq);
322
323static void mask_evtchn(int port)
324{
325 struct shared_info *s = HYPERVISOR_shared_info;
326 sync_set_bit(port, &s->evtchn_mask[0]);
327}
328
329static void unmask_evtchn(int port)
330{
331 struct shared_info *s = HYPERVISOR_shared_info;
332 unsigned int cpu = get_cpu();
333
334 BUG_ON(!irqs_disabled());
335
336 /* Slow path (hypercall) if this is a non-local port. */
337 if (unlikely(cpu != cpu_from_evtchn(port))) {
338 struct evtchn_unmask unmask = { .port = port };
339 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
340 } else {
Christoph Lameter780f36d2010-12-06 11:16:29 -0600341 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700342
343 sync_clear_bit(port, &s->evtchn_mask[0]);
344
345 /*
346 * The following is basically the equivalent of
347 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
348 * the interrupt edge' if the channel is masked.
349 */
350 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
351 !sync_test_and_set_bit(port / BITS_PER_LONG,
352 &vcpu_info->evtchn_pending_sel))
353 vcpu_info->evtchn_upcall_pending = 1;
354 }
355
356 put_cpu();
357}
358
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000359static int xen_allocate_irq_dynamic(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700360{
Ian Campbell89911502011-03-03 11:57:44 -0500361 int first = 0;
362 int irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700363
Ian Campbell89911502011-03-03 11:57:44 -0500364#ifdef CONFIG_X86_IO_APIC
365 /*
366 * For an HVM guest or domain 0 which see "real" (emulated or
367 * actual repectively) GSIs we allocate dynamic IRQs
368 * e.g. those corresponding to event channels or MSIs
369 * etc. from the range above those "real" GSIs to avoid
370 * collisions.
Konrad Rzeszutek Wilkd1b758e2010-12-09 14:53:29 -0500371 */
Ian Campbell89911502011-03-03 11:57:44 -0500372 if (xen_initial_domain() || xen_hvm_domain())
373 first = get_nr_irqs_gsi();
374#endif
375
376retry:
377 irq = irq_alloc_desc_from(first, -1);
378
379 if (irq == -ENOMEM && first > NR_IRQS_LEGACY) {
380 printk(KERN_ERR "Out of dynamic IRQ space and eating into GSI space. You should increase nr_irqs\n");
381 first = max(NR_IRQS_LEGACY, first - NR_IRQS_LEGACY);
382 goto retry;
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100383 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700384
Ian Campbell89911502011-03-03 11:57:44 -0500385 if (irq < 0)
386 panic("No available IRQ to bind to: increase nr_irqs!\n");
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800387
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700388 return irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400389}
390
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000391static int xen_allocate_irq_gsi(unsigned gsi)
392{
393 int irq;
394
Ian Campbell89911502011-03-03 11:57:44 -0500395 /*
396 * A PV guest has no concept of a GSI (since it has no ACPI
397 * nor access to/knowledge of the physical APICs). Therefore
398 * all IRQs are dynamically allocated from the entire IRQ
399 * space.
400 */
401 if (xen_pv_domain() && !xen_initial_domain())
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000402 return xen_allocate_irq_dynamic();
403
404 /* Legacy IRQ descriptors are already allocated by the arch. */
405 if (gsi < NR_IRQS_LEGACY)
406 return gsi;
407
408 irq = irq_alloc_desc_at(gsi, -1);
409 if (irq < 0)
410 panic("Unable to allocate to IRQ%d (%d)\n", gsi, irq);
411
412 return irq;
413}
414
415static void xen_free_irq(unsigned irq)
416{
Ian Campbell72146102011-02-03 09:49:35 +0000417 /* Legacy IRQ descriptors are managed by the arch. */
418 if (irq < NR_IRQS_LEGACY)
419 return;
420
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000421 irq_free_desc(irq);
422}
423
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400424static void pirq_unmask_notify(int irq)
425{
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100426 struct physdev_eoi eoi = { .irq = pirq_from_irq(irq) };
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400427
428 if (unlikely(pirq_needs_eoi(irq))) {
429 int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
430 WARN_ON(rc);
431 }
432}
433
434static void pirq_query_unmask(int irq)
435{
436 struct physdev_irq_status_query irq_status;
437 struct irq_info *info = info_for_irq(irq);
438
439 BUG_ON(info->type != IRQT_PIRQ);
440
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100441 irq_status.irq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400442 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
443 irq_status.flags = 0;
444
445 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
446 if (irq_status.flags & XENIRQSTAT_needs_eoi)
447 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
448}
449
450static bool probing_irq(int irq)
451{
452 struct irq_desc *desc = irq_to_desc(irq);
453
454 return desc && desc->action == NULL;
455}
456
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000457static unsigned int __startup_pirq(unsigned int irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400458{
459 struct evtchn_bind_pirq bind_pirq;
460 struct irq_info *info = info_for_irq(irq);
461 int evtchn = evtchn_from_irq(irq);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400462 int rc;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400463
464 BUG_ON(info->type != IRQT_PIRQ);
465
466 if (VALID_EVTCHN(evtchn))
467 goto out;
468
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100469 bind_pirq.pirq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400470 /* NB. We are happy to share unless we are probing. */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400471 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
472 BIND_PIRQ__WILL_SHARE : 0;
473 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
474 if (rc != 0) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400475 if (!probing_irq(irq))
476 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
477 irq);
478 return 0;
479 }
480 evtchn = bind_pirq.port;
481
482 pirq_query_unmask(irq);
483
484 evtchn_to_irq[evtchn] = irq;
485 bind_evtchn_to_cpu(evtchn, 0);
486 info->evtchn = evtchn;
487
488out:
489 unmask_evtchn(evtchn);
490 pirq_unmask_notify(irq);
491
492 return 0;
493}
494
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000495static unsigned int startup_pirq(struct irq_data *data)
496{
497 return __startup_pirq(data->irq);
498}
499
500static void shutdown_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400501{
502 struct evtchn_close close;
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000503 unsigned int irq = data->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400504 struct irq_info *info = info_for_irq(irq);
505 int evtchn = evtchn_from_irq(irq);
506
507 BUG_ON(info->type != IRQT_PIRQ);
508
509 if (!VALID_EVTCHN(evtchn))
510 return;
511
512 mask_evtchn(evtchn);
513
514 close.port = evtchn;
515 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
516 BUG();
517
518 bind_evtchn_to_cpu(evtchn, 0);
519 evtchn_to_irq[evtchn] = -1;
520 info->evtchn = 0;
521}
522
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000523static void enable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400524{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000525 startup_pirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400526}
527
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000528static void disable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400529{
530}
531
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000532static void ack_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400533{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000534 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400535
Ian Campbellaa673c12011-02-07 11:08:39 +0000536 move_native_irq(data->irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400537
538 if (VALID_EVTCHN(evtchn)) {
539 mask_evtchn(evtchn);
540 clear_evtchn(evtchn);
541 }
542}
543
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400544static int find_irq_by_gsi(unsigned gsi)
545{
546 int irq;
547
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400548 for (irq = 0; irq < nr_irqs; irq++) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400549 struct irq_info *info = info_for_irq(irq);
550
551 if (info == NULL || info->type != IRQT_PIRQ)
552 continue;
553
554 if (gsi_from_irq(irq) == gsi)
555 return irq;
556 }
557
558 return -1;
559}
560
Ian Campbellf4d06352011-03-10 16:08:07 +0000561int xen_allocate_pirq_gsi(unsigned gsi)
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100562{
Ian Campbellf4d06352011-03-10 16:08:07 +0000563 return gsi;
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100564}
565
Ian Campbell653378a2011-03-10 16:08:04 +0000566/*
567 * Do not make any assumptions regarding the relationship between the
568 * IRQ number returned here and the Xen pirq argument.
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100569 *
570 * Note: We don't assign an event channel until the irq actually started
571 * up. Return an existing irq if we've already got one for the gsi.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400572 */
Ian Campbellf4d06352011-03-10 16:08:07 +0000573int xen_bind_pirq_gsi_to_irq(unsigned gsi,
574 unsigned pirq, int shareable, char *name)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400575{
Ian Campbella0e18112011-03-10 16:08:03 +0000576 int irq = -1;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400577 struct physdev_irq irq_op;
578
579 spin_lock(&irq_mapping_update_lock);
580
Stefano Stabellinie5fc7342010-12-01 14:51:44 +0000581 if ((pirq > nr_irqs) || (gsi > nr_irqs)) {
Stefano Stabellini01557ba2010-08-20 14:46:52 +0100582 printk(KERN_WARNING "xen_map_pirq_gsi: %s %s is incorrect!\n",
Stefano Stabellinie5fc7342010-12-01 14:51:44 +0000583 pirq > nr_irqs ? "pirq" :"",
584 gsi > nr_irqs ? "gsi" : "");
Stefano Stabellini01557ba2010-08-20 14:46:52 +0100585 goto out;
586 }
587
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400588 irq = find_irq_by_gsi(gsi);
589 if (irq != -1) {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100590 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400591 irq, gsi);
592 goto out; /* XXX need refcount? */
593 }
594
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000595 irq = xen_allocate_irq_gsi(gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400596
597 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
Gerd Hoffmann1a60d052010-10-04 13:42:27 -0400598 handle_level_irq, name);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400599
600 irq_op.irq = irq;
Alex Nixonb5401a92010-03-18 16:31:34 -0400601 irq_op.vector = 0;
602
603 /* Only the privileged domain can do this. For non-priv, the pcifront
604 * driver provides a PCI bus that does the call to do exactly
605 * this in the priv domain. */
606 if (xen_initial_domain() &&
607 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000608 xen_free_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400609 irq = -ENOSPC;
610 goto out;
611 }
612
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100613 irq_info[irq] = mk_pirq_info(0, pirq, gsi, irq_op.vector);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400614 irq_info[irq].u.pirq.flags |= shareable ? PIRQ_SHAREABLE : 0;
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100615 pirq_to_irq[pirq] = irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400616
617out:
618 spin_unlock(&irq_mapping_update_lock);
619
620 return irq;
621}
622
Qing Hef731e3ef2010-10-11 15:30:09 +0100623#ifdef CONFIG_PCI_MSI
Ian Campbellbf480d92011-02-18 16:43:32 +0000624int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000625{
Ian Campbell5cad61a2011-02-18 16:43:31 +0000626 int rc;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000627 struct physdev_get_free_pirq op_get_free_pirq;
Ian Campbell5cad61a2011-02-18 16:43:31 +0000628
Ian Campbellbf480d92011-02-18 16:43:32 +0000629 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000630 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000631
Ian Campbell5cad61a2011-02-18 16:43:31 +0000632 WARN_ONCE(rc == -ENOSYS,
633 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
634
635 return rc ? -1 : op_get_free_pirq.pirq;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000636}
637
Ian Campbellbf480d92011-02-18 16:43:32 +0000638int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
Ian Campbellca1d8fe2011-02-18 16:43:36 +0000639 int pirq, int vector, const char *name)
Stefano Stabellini809f9262010-07-01 17:10:39 +0100640{
Ian Campbellbf480d92011-02-18 16:43:32 +0000641 int irq, ret;
Ian Campbell4b41df72011-02-18 16:43:29 +0000642
Stefano Stabellini809f9262010-07-01 17:10:39 +0100643 spin_lock(&irq_mapping_update_lock);
644
Ian Campbell4b41df72011-02-18 16:43:29 +0000645 irq = xen_allocate_irq_dynamic();
646 if (irq == -1)
Ian Campbellbb5d0792011-02-18 16:43:28 +0000647 goto out;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100648
Ian Campbell4b41df72011-02-18 16:43:29 +0000649 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
Stefano Stabellini809f9262010-07-01 17:10:39 +0100650 handle_level_irq, name);
651
Ian Campbellca1d8fe2011-02-18 16:43:36 +0000652 irq_info[irq] = mk_pirq_info(0, pirq, 0, vector);
Ian Campbellbf480d92011-02-18 16:43:32 +0000653 pirq_to_irq[pirq] = irq;
654 ret = set_irq_msi(irq, msidesc);
655 if (ret < 0)
656 goto error_irq;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100657out:
658 spin_unlock(&irq_mapping_update_lock);
Ian Campbell4b41df72011-02-18 16:43:29 +0000659 return irq;
Ian Campbellbf480d92011-02-18 16:43:32 +0000660error_irq:
661 spin_unlock(&irq_mapping_update_lock);
662 xen_free_irq(irq);
663 return -1;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100664}
Qing Hef731e3ef2010-10-11 15:30:09 +0100665#endif
666
Alex Nixonb5401a92010-03-18 16:31:34 -0400667int xen_destroy_irq(int irq)
668{
669 struct irq_desc *desc;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100670 struct physdev_unmap_pirq unmap_irq;
671 struct irq_info *info = info_for_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400672 int rc = -ENOENT;
673
674 spin_lock(&irq_mapping_update_lock);
675
676 desc = irq_to_desc(irq);
677 if (!desc)
678 goto out;
679
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100680 if (xen_initial_domain()) {
Konrad Rzeszutek Wilk12334712010-11-19 11:27:09 -0500681 unmap_irq.pirq = info->u.pirq.pirq;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100682 unmap_irq.domid = DOMID_SELF;
683 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
684 if (rc) {
685 printk(KERN_WARNING "unmap irq failed %d\n", rc);
686 goto out;
687 }
688 }
Konrad Rzeszutek Wilk1aa0b512011-02-17 11:23:58 -0500689 pirq_to_irq[info->u.pirq.pirq] = -1;
690
Alex Nixonb5401a92010-03-18 16:31:34 -0400691 irq_info[irq] = mk_unbound_info();
692
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000693 xen_free_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400694
695out:
696 spin_unlock(&irq_mapping_update_lock);
697 return rc;
698}
699
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000700int xen_irq_from_pirq(unsigned pirq)
701{
702 return pirq_to_irq[pirq];
703}
704
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700705int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700706{
707 int irq;
708
709 spin_lock(&irq_mapping_update_lock);
710
711 irq = evtchn_to_irq[evtchn];
712
713 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000714 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700715
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700716 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -0700717 handle_fasteoi_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700718
719 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800720 irq_info[irq] = mk_evtchn_info(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700721 }
722
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700723 spin_unlock(&irq_mapping_update_lock);
724
725 return irq;
726}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700727EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700728
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700729static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
730{
731 struct evtchn_bind_ipi bind_ipi;
732 int evtchn, irq;
733
734 spin_lock(&irq_mapping_update_lock);
735
736 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800737
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700738 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000739 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700740 if (irq < 0)
741 goto out;
742
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700743 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
744 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700745
746 bind_ipi.vcpu = cpu;
747 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
748 &bind_ipi) != 0)
749 BUG();
750 evtchn = bind_ipi.port;
751
752 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800753 irq_info[irq] = mk_ipi_info(evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700754 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
755
756 bind_evtchn_to_cpu(evtchn, cpu);
757 }
758
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700759 out:
760 spin_unlock(&irq_mapping_update_lock);
761 return irq;
762}
763
764
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100765int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700766{
767 struct evtchn_bind_virq bind_virq;
768 int evtchn, irq;
769
770 spin_lock(&irq_mapping_update_lock);
771
772 irq = per_cpu(virq_to_irq, cpu)[virq];
773
774 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000775 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700776
777 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
778 handle_percpu_irq, "virq");
779
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700780 bind_virq.virq = virq;
781 bind_virq.vcpu = cpu;
782 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
783 &bind_virq) != 0)
784 BUG();
785 evtchn = bind_virq.port;
786
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700787 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800788 irq_info[irq] = mk_virq_info(evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700789
790 per_cpu(virq_to_irq, cpu)[virq] = irq;
791
792 bind_evtchn_to_cpu(evtchn, cpu);
793 }
794
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700795 spin_unlock(&irq_mapping_update_lock);
796
797 return irq;
798}
799
800static void unbind_from_irq(unsigned int irq)
801{
802 struct evtchn_close close;
803 int evtchn = evtchn_from_irq(irq);
804
805 spin_lock(&irq_mapping_update_lock);
806
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800807 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700808 close.port = evtchn;
809 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
810 BUG();
811
812 switch (type_from_irq(irq)) {
813 case IRQT_VIRQ:
814 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800815 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700816 break;
Alex Nixond68d82a2008-08-22 11:52:15 +0100817 case IRQT_IPI:
818 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800819 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +0100820 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700821 default:
822 break;
823 }
824
825 /* Closed ports are implicitly re-bound to VCPU0. */
826 bind_evtchn_to_cpu(evtchn, 0);
827
828 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +0000829 }
830
831 if (irq_info[irq].type != IRQT_UNBOUND) {
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800832 irq_info[irq] = mk_unbound_info();
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700833
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000834 xen_free_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700835 }
836
837 spin_unlock(&irq_mapping_update_lock);
838}
839
840int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -0400841 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700842 unsigned long irqflags,
843 const char *devname, void *dev_id)
844{
845 unsigned int irq;
846 int retval;
847
848 irq = bind_evtchn_to_irq(evtchn);
849 retval = request_irq(irq, handler, irqflags, devname, dev_id);
850 if (retval != 0) {
851 unbind_from_irq(irq);
852 return retval;
853 }
854
855 return irq;
856}
857EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
858
859int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -0400860 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700861 unsigned long irqflags, const char *devname, void *dev_id)
862{
863 unsigned int irq;
864 int retval;
865
866 irq = bind_virq_to_irq(virq, cpu);
867 retval = request_irq(irq, handler, irqflags, devname, dev_id);
868 if (retval != 0) {
869 unbind_from_irq(irq);
870 return retval;
871 }
872
873 return irq;
874}
875EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
876
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700877int bind_ipi_to_irqhandler(enum ipi_vector ipi,
878 unsigned int cpu,
879 irq_handler_t handler,
880 unsigned long irqflags,
881 const char *devname,
882 void *dev_id)
883{
884 int irq, retval;
885
886 irq = bind_ipi_to_irq(ipi, cpu);
887 if (irq < 0)
888 return irq;
889
Thomas Gleixner676dc3c2011-02-05 20:08:59 +0000890 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700891 retval = request_irq(irq, handler, irqflags, devname, dev_id);
892 if (retval != 0) {
893 unbind_from_irq(irq);
894 return retval;
895 }
896
897 return irq;
898}
899
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700900void unbind_from_irqhandler(unsigned int irq, void *dev_id)
901{
902 free_irq(irq, dev_id);
903 unbind_from_irq(irq);
904}
905EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
906
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700907void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
908{
909 int irq = per_cpu(ipi_to_irq, cpu)[vector];
910 BUG_ON(irq < 0);
911 notify_remote_via_irq(irq);
912}
913
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700914irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
915{
916 struct shared_info *sh = HYPERVISOR_shared_info;
917 int cpu = smp_processor_id();
Ian Campbellcb60d112011-03-10 16:08:08 +0000918 unsigned long *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700919 int i;
920 unsigned long flags;
921 static DEFINE_SPINLOCK(debug_lock);
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100922 struct vcpu_info *v;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700923
924 spin_lock_irqsave(&debug_lock, flags);
925
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100926 printk("\nvcpu %d\n ", cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700927
928 for_each_online_cpu(i) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100929 int pending;
930 v = per_cpu(xen_vcpu, i);
931 pending = (get_irq_regs() && i == cpu)
932 ? xen_irqs_disabled(get_irq_regs())
933 : v->evtchn_upcall_mask;
934 printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i,
935 pending, v->evtchn_upcall_pending,
936 (int)(sizeof(v->evtchn_pending_sel)*2),
937 v->evtchn_pending_sel);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700938 }
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100939 v = per_cpu(xen_vcpu, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700940
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100941 printk("\npending:\n ");
942 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
943 printk("%0*lx%s", (int)sizeof(sh->evtchn_pending[0])*2,
944 sh->evtchn_pending[i],
945 i % 8 == 0 ? "\n " : " ");
946 printk("\nglobal mask:\n ");
947 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
948 printk("%0*lx%s",
949 (int)(sizeof(sh->evtchn_mask[0])*2),
950 sh->evtchn_mask[i],
951 i % 8 == 0 ? "\n " : " ");
952
953 printk("\nglobally unmasked:\n ");
954 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
955 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
956 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
957 i % 8 == 0 ? "\n " : " ");
958
959 printk("\nlocal cpu%d mask:\n ", cpu);
960 for (i = (NR_EVENT_CHANNELS/BITS_PER_LONG)-1; i >= 0; i--)
961 printk("%0*lx%s", (int)(sizeof(cpu_evtchn[0])*2),
962 cpu_evtchn[i],
963 i % 8 == 0 ? "\n " : " ");
964
965 printk("\nlocally unmasked:\n ");
966 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
967 unsigned long pending = sh->evtchn_pending[i]
968 & ~sh->evtchn_mask[i]
969 & cpu_evtchn[i];
970 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
971 pending, i % 8 == 0 ? "\n " : " ");
972 }
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700973
974 printk("\npending list:\n");
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100975 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700976 if (sync_test_bit(i, sh->evtchn_pending)) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100977 int word_idx = i / BITS_PER_LONG;
978 printk(" %d: event %d -> irq %d%s%s%s\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800979 cpu_from_evtchn(i), i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100980 evtchn_to_irq[i],
981 sync_test_bit(word_idx, &v->evtchn_pending_sel)
982 ? "" : " l2-clear",
983 !sync_test_bit(i, sh->evtchn_mask)
984 ? "" : " globally-masked",
985 sync_test_bit(i, cpu_evtchn)
986 ? "" : " locally-masked");
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700987 }
988 }
989
990 spin_unlock_irqrestore(&debug_lock, flags);
991
992 return IRQ_HANDLED;
993}
994
Tejun Heo245b2e72009-06-24 15:13:48 +0900995static DEFINE_PER_CPU(unsigned, xed_nesting_count);
996
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700997/*
998 * Search the CPUs pending events bitmasks. For each one found, map
999 * the event number to an irq, and feed it into do_IRQ() for
1000 * handling.
1001 *
1002 * Xen uses a two-level bitmap to speed searching. The first level is
1003 * a bitset of words which contain pending event bits. The second
1004 * level is a bitset of pending events themselves.
1005 */
Sheng Yang38e20b02010-05-14 12:40:51 +01001006static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001007{
1008 int cpu = get_cpu();
1009 struct shared_info *s = HYPERVISOR_shared_info;
Christoph Lameter780f36d2010-12-06 11:16:29 -06001010 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001011 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001012
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001013 do {
1014 unsigned long pending_words;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001015
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001016 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001017
Christoph Lameterb2e4ae62010-12-06 11:40:07 -06001018 if (__this_cpu_inc_return(xed_nesting_count) - 1)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001019 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001020
Isaku Yamahatae849c3e2008-04-02 10:53:56 -07001021#ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
1022 /* Clear master flag /before/ clearing selector flag. */
Isaku Yamahata6673cf62008-06-16 14:58:13 -07001023 wmb();
Isaku Yamahatae849c3e2008-04-02 10:53:56 -07001024#endif
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001025 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
1026 while (pending_words != 0) {
1027 unsigned long pending_bits;
1028 int word_idx = __ffs(pending_words);
1029 pending_words &= ~(1UL << word_idx);
1030
1031 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
1032 int bit_idx = __ffs(pending_bits);
1033 int port = (word_idx * BITS_PER_LONG) + bit_idx;
1034 int irq = evtchn_to_irq[port];
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001035 struct irq_desc *desc;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001036
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001037 mask_evtchn(port);
1038 clear_evtchn(port);
1039
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001040 if (irq != -1) {
1041 desc = irq_to_desc(irq);
1042 if (desc)
1043 generic_handle_irq_desc(irq, desc);
1044 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001045 }
1046 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001047
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001048 BUG_ON(!irqs_disabled());
1049
Christoph Lameter780f36d2010-12-06 11:16:29 -06001050 count = __this_cpu_read(xed_nesting_count);
1051 __this_cpu_write(xed_nesting_count, 0);
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001052 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001053
1054out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -08001055
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001056 put_cpu();
1057}
1058
Sheng Yang38e20b02010-05-14 12:40:51 +01001059void xen_evtchn_do_upcall(struct pt_regs *regs)
1060{
1061 struct pt_regs *old_regs = set_irq_regs(regs);
1062
1063 exit_idle();
1064 irq_enter();
1065
1066 __xen_evtchn_do_upcall();
1067
1068 irq_exit();
1069 set_irq_regs(old_regs);
1070}
1071
1072void xen_hvm_evtchn_do_upcall(void)
1073{
1074 __xen_evtchn_do_upcall();
1075}
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001076EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +01001077
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001078/* Rebind a new event channel to an existing irq. */
1079void rebind_evtchn_irq(int evtchn, int irq)
1080{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001081 struct irq_info *info = info_for_irq(irq);
1082
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001083 /* Make sure the irq is masked, since the new event channel
1084 will also be masked. */
1085 disable_irq(irq);
1086
1087 spin_lock(&irq_mapping_update_lock);
1088
1089 /* After resume the irq<->evtchn mappings are all cleared out */
1090 BUG_ON(evtchn_to_irq[evtchn] != -1);
1091 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001092 so there should be a proper type */
1093 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001094
1095 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001096 irq_info[irq] = mk_evtchn_info(evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001097
1098 spin_unlock(&irq_mapping_update_lock);
1099
1100 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +10301101 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001102
1103 /* Unmask the event channel. */
1104 enable_irq(irq);
1105}
1106
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001107/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -07001108static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001109{
1110 struct evtchn_bind_vcpu bind_vcpu;
1111 int evtchn = evtchn_from_irq(irq);
1112
Ian Campbellbe494722011-03-10 16:08:02 +00001113 if (!VALID_EVTCHN(evtchn))
1114 return -1;
1115
1116 /*
1117 * Events delivered via platform PCI interrupts are always
1118 * routed to vcpu 0 and hence cannot be rebound.
1119 */
1120 if (xen_hvm_domain() && !xen_have_vector_callback)
Yinghai Lud5dedd42009-04-27 17:59:21 -07001121 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001122
1123 /* Send future instances of this interrupt to other vcpu. */
1124 bind_vcpu.port = evtchn;
1125 bind_vcpu.vcpu = tcpu;
1126
1127 /*
1128 * If this fails, it usually just indicates that we're dealing with a
1129 * virq or IPI channel, which don't actually need to be rebound. Ignore
1130 * it, but don't do the xenlinux-level rebind in that case.
1131 */
1132 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1133 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001134
1135 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001136}
1137
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001138static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1139 bool force)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001140{
Rusty Russell0de26522008-12-13 21:20:26 +10301141 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001142
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001143 return rebind_irq_to_cpu(data->irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001144}
1145
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001146int resend_irq_on_evtchn(unsigned int irq)
1147{
1148 int masked, evtchn = evtchn_from_irq(irq);
1149 struct shared_info *s = HYPERVISOR_shared_info;
1150
1151 if (!VALID_EVTCHN(evtchn))
1152 return 1;
1153
1154 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
1155 sync_set_bit(evtchn, s->evtchn_pending);
1156 if (!masked)
1157 unmask_evtchn(evtchn);
1158
1159 return 1;
1160}
1161
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001162static void enable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001163{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001164 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001165
1166 if (VALID_EVTCHN(evtchn))
1167 unmask_evtchn(evtchn);
1168}
1169
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001170static void disable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001171{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001172 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001173
1174 if (VALID_EVTCHN(evtchn))
1175 mask_evtchn(evtchn);
1176}
1177
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001178static void ack_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001179{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001180 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001181
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001182 move_masked_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001183
1184 if (VALID_EVTCHN(evtchn))
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001185 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001186}
1187
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001188static int retrigger_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001189{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001190 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001191 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001192 int ret = 0;
1193
1194 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001195 int masked;
1196
1197 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1198 sync_set_bit(evtchn, sh->evtchn_pending);
1199 if (!masked)
1200 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001201 ret = 1;
1202 }
1203
1204 return ret;
1205}
1206
Ian Campbell0a852262011-03-10 16:08:06 +00001207static void restore_pirqs(void)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001208{
1209 int pirq, rc, irq, gsi;
1210 struct physdev_map_pirq map_irq;
1211
1212 for (pirq = 0; pirq < nr_irqs; pirq++) {
1213 irq = pirq_to_irq[pirq];
1214 if (irq == -1)
1215 continue;
1216
1217 /* save/restore of PT devices doesn't work, so at this point the
1218 * only devices present are GSI based emulated devices */
1219 gsi = gsi_from_irq(irq);
1220 if (!gsi)
1221 continue;
1222
1223 map_irq.domid = DOMID_SELF;
1224 map_irq.type = MAP_PIRQ_TYPE_GSI;
1225 map_irq.index = gsi;
1226 map_irq.pirq = pirq;
1227
1228 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1229 if (rc) {
1230 printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1231 gsi, irq, pirq, rc);
1232 irq_info[irq] = mk_unbound_info();
1233 pirq_to_irq[pirq] = -1;
1234 continue;
1235 }
1236
1237 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1238
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001239 __startup_pirq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001240 }
1241}
1242
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001243static void restore_cpu_virqs(unsigned int cpu)
1244{
1245 struct evtchn_bind_virq bind_virq;
1246 int virq, irq, evtchn;
1247
1248 for (virq = 0; virq < NR_VIRQS; virq++) {
1249 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1250 continue;
1251
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001252 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001253
1254 /* Get a new binding from Xen. */
1255 bind_virq.virq = virq;
1256 bind_virq.vcpu = cpu;
1257 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1258 &bind_virq) != 0)
1259 BUG();
1260 evtchn = bind_virq.port;
1261
1262 /* Record the new mapping. */
1263 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001264 irq_info[irq] = mk_virq_info(evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001265 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001266 }
1267}
1268
1269static void restore_cpu_ipis(unsigned int cpu)
1270{
1271 struct evtchn_bind_ipi bind_ipi;
1272 int ipi, irq, evtchn;
1273
1274 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1275 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1276 continue;
1277
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001278 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001279
1280 /* Get a new binding from Xen. */
1281 bind_ipi.vcpu = cpu;
1282 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1283 &bind_ipi) != 0)
1284 BUG();
1285 evtchn = bind_ipi.port;
1286
1287 /* Record the new mapping. */
1288 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001289 irq_info[irq] = mk_ipi_info(evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001290 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001291 }
1292}
1293
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001294/* Clear an irq's pending state, in preparation for polling on it */
1295void xen_clear_irq_pending(int irq)
1296{
1297 int evtchn = evtchn_from_irq(irq);
1298
1299 if (VALID_EVTCHN(evtchn))
1300 clear_evtchn(evtchn);
1301}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001302EXPORT_SYMBOL(xen_clear_irq_pending);
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001303void xen_set_irq_pending(int irq)
1304{
1305 int evtchn = evtchn_from_irq(irq);
1306
1307 if (VALID_EVTCHN(evtchn))
1308 set_evtchn(evtchn);
1309}
1310
1311bool xen_test_irq_pending(int irq)
1312{
1313 int evtchn = evtchn_from_irq(irq);
1314 bool ret = false;
1315
1316 if (VALID_EVTCHN(evtchn))
1317 ret = test_evtchn(evtchn);
1318
1319 return ret;
1320}
1321
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001322/* Poll waiting for an irq to become pending with timeout. In the usual case,
1323 * the irq will be disabled so it won't deliver an interrupt. */
1324void xen_poll_irq_timeout(int irq, u64 timeout)
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001325{
1326 evtchn_port_t evtchn = evtchn_from_irq(irq);
1327
1328 if (VALID_EVTCHN(evtchn)) {
1329 struct sched_poll poll;
1330
1331 poll.nr_ports = 1;
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001332 poll.timeout = timeout;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001333 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001334
1335 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1336 BUG();
1337 }
1338}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001339EXPORT_SYMBOL(xen_poll_irq_timeout);
1340/* Poll waiting for an irq to become pending. In the usual case, the
1341 * irq will be disabled so it won't deliver an interrupt. */
1342void xen_poll_irq(int irq)
1343{
1344 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1345}
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001346
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001347void xen_irq_resume(void)
1348{
1349 unsigned int cpu, irq, evtchn;
1350
1351 init_evtchn_cpu_bindings();
1352
1353 /* New event-channel space is not 'live' yet. */
1354 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1355 mask_evtchn(evtchn);
1356
1357 /* No IRQ <-> event-channel mappings. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001358 for (irq = 0; irq < nr_irqs; irq++)
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001359 irq_info[irq].evtchn = 0; /* zap event-channel binding */
1360
1361 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1362 evtchn_to_irq[evtchn] = -1;
1363
1364 for_each_possible_cpu(cpu) {
1365 restore_cpu_virqs(cpu);
1366 restore_cpu_ipis(cpu);
1367 }
Ian Campbell69035912010-11-01 16:30:09 +00001368
Ian Campbell0a852262011-03-10 16:08:06 +00001369 restore_pirqs();
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001370}
1371
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001372static struct irq_chip xen_dynamic_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001373 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001374
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001375 .irq_disable = disable_dynirq,
1376 .irq_mask = disable_dynirq,
1377 .irq_unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001378
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001379 .irq_eoi = ack_dynirq,
1380 .irq_set_affinity = set_affinity_irq,
1381 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001382};
1383
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001384static struct irq_chip xen_pirq_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001385 .name = "xen-pirq",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001386
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001387 .irq_startup = startup_pirq,
1388 .irq_shutdown = shutdown_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001389
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001390 .irq_enable = enable_pirq,
1391 .irq_unmask = enable_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001392
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001393 .irq_disable = disable_pirq,
1394 .irq_mask = disable_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001395
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001396 .irq_ack = ack_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001397
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001398 .irq_set_affinity = set_affinity_irq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001399
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001400 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001401};
1402
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001403static struct irq_chip xen_percpu_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001404 .name = "xen-percpu",
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001405
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001406 .irq_disable = disable_dynirq,
1407 .irq_mask = disable_dynirq,
1408 .irq_unmask = enable_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001409
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001410 .irq_ack = ack_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001411};
1412
Sheng Yang38e20b02010-05-14 12:40:51 +01001413int xen_set_callback_via(uint64_t via)
1414{
1415 struct xen_hvm_param a;
1416 a.domid = DOMID_SELF;
1417 a.index = HVM_PARAM_CALLBACK_IRQ;
1418 a.value = via;
1419 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1420}
1421EXPORT_SYMBOL_GPL(xen_set_callback_via);
1422
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001423#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001424/* Vector callbacks are better than PCI interrupts to receive event
1425 * channel notifications because we can receive vector callbacks on any
1426 * vcpu and we don't need PCI support or APIC interactions. */
1427void xen_callback_vector(void)
1428{
1429 int rc;
1430 uint64_t callback_via;
1431 if (xen_have_vector_callback) {
1432 callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
1433 rc = xen_set_callback_via(callback_via);
1434 if (rc) {
1435 printk(KERN_ERR "Request for Xen HVM callback vector"
1436 " failed.\n");
1437 xen_have_vector_callback = 0;
1438 return;
1439 }
1440 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1441 "enabled\n");
1442 /* in the restore case the vector has already been allocated */
1443 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
1444 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
1445 }
1446}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001447#else
1448void xen_callback_vector(void) {}
1449#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001450
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001451void __init xen_init_IRQ(void)
1452{
Stefano Stabellinie5fc7342010-12-01 14:51:44 +00001453 int i;
Mike Travisc7a35892009-01-10 21:58:11 -08001454
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001455 irq_info = kcalloc(nr_irqs, sizeof(*irq_info), GFP_KERNEL);
1456
Stefano Stabellinie5fc7342010-12-01 14:51:44 +00001457 /* We are using nr_irqs as the maximum number of pirq available but
1458 * that number is actually chosen by Xen and we don't know exactly
1459 * what it is. Be careful choosing high pirq numbers. */
1460 pirq_to_irq = kcalloc(nr_irqs, sizeof(*pirq_to_irq), GFP_KERNEL);
1461 for (i = 0; i < nr_irqs; i++)
Stefano Stabellini7a043f12010-07-01 17:08:14 +01001462 pirq_to_irq[i] = -1;
1463
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001464 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1465 GFP_KERNEL);
1466 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1467 evtchn_to_irq[i] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001468
1469 init_evtchn_cpu_bindings();
1470
1471 /* No event channels are 'live' right now. */
1472 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1473 mask_evtchn(i);
1474
Sheng Yang38e20b02010-05-14 12:40:51 +01001475 if (xen_hvm_domain()) {
1476 xen_callback_vector();
1477 native_init_IRQ();
Stefano Stabellini3942b742010-06-24 17:50:18 +01001478 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1479 * __acpi_register_gsi can point at the right function */
1480 pci_xen_hvm_init();
Sheng Yang38e20b02010-05-14 12:40:51 +01001481 } else {
1482 irq_ctx_init(smp_processor_id());
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +01001483 if (xen_initial_domain())
1484 xen_setup_pirqs();
Sheng Yang38e20b02010-05-14 12:40:51 +01001485 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001486}