blob: 72725faa2e85e3999a0e1cfe7b01bfa3d64637d5 [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
Ian Campbell9158c352011-03-10 16:08:09 +0000121/* Get info for IRQ */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800122static struct irq_info *info_for_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700123{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800124 return &irq_info[irq];
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700125}
126
Ian Campbell9158c352011-03-10 16:08:09 +0000127/* Constructors for packed IRQ information. */
128static void xen_irq_info_common_init(struct irq_info *info,
129 enum xen_irq_type type,
130 unsigned short evtchn,
131 unsigned short cpu)
132{
133
134 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
135
136 info->type = type;
137 info->evtchn = evtchn;
138 info->cpu = cpu;
139}
140
141static void xen_irq_info_evtchn_init(unsigned irq,
142 unsigned short evtchn)
143{
144 struct irq_info *info = info_for_irq(irq);
145
146 xen_irq_info_common_init(info, IRQT_EVTCHN, evtchn, 0);
147}
148
149static void xen_irq_info_ipi_init(unsigned irq,
150 unsigned short evtchn,
151 enum ipi_vector ipi)
152{
153 struct irq_info *info = info_for_irq(irq);
154
155 xen_irq_info_common_init(info, IRQT_IPI, evtchn, 0);
156
157 info->u.ipi = ipi;
158}
159
160static void xen_irq_info_virq_init(unsigned irq,
161 unsigned short evtchn,
162 unsigned short virq)
163{
164 struct irq_info *info = info_for_irq(irq);
165
166 xen_irq_info_common_init(info, IRQT_VIRQ, evtchn, 0);
167
168 info->u.virq = virq;
169}
170
171static void xen_irq_info_pirq_init(unsigned irq,
172 unsigned short evtchn,
173 unsigned short pirq,
174 unsigned short gsi,
175 unsigned short vector,
176 unsigned char flags)
177{
178 struct irq_info *info = info_for_irq(irq);
179
180 xen_irq_info_common_init(info, IRQT_PIRQ, evtchn, 0);
181
182 info->u.pirq.pirq = pirq;
183 info->u.pirq.gsi = gsi;
184 info->u.pirq.vector = vector;
185 info->u.pirq.flags = flags;
186}
187
188/*
189 * Accessors for packed IRQ information.
190 */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800191static unsigned int evtchn_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700192{
Joe Jin110e7c72011-01-07 14:50:12 +0800193 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
194 return 0;
195
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800196 return info_for_irq(irq)->evtchn;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700197}
198
Ian Campbelld4c04532009-02-06 19:20:31 -0800199unsigned irq_from_evtchn(unsigned int evtchn)
200{
201 return evtchn_to_irq[evtchn];
202}
203EXPORT_SYMBOL_GPL(irq_from_evtchn);
204
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800205static enum ipi_vector ipi_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700206{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800207 struct irq_info *info = info_for_irq(irq);
208
209 BUG_ON(info == NULL);
210 BUG_ON(info->type != IRQT_IPI);
211
212 return info->u.ipi;
213}
214
215static unsigned virq_from_irq(unsigned irq)
216{
217 struct irq_info *info = info_for_irq(irq);
218
219 BUG_ON(info == NULL);
220 BUG_ON(info->type != IRQT_VIRQ);
221
222 return info->u.virq;
223}
224
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100225static unsigned pirq_from_irq(unsigned irq)
226{
227 struct irq_info *info = info_for_irq(irq);
228
229 BUG_ON(info == NULL);
230 BUG_ON(info->type != IRQT_PIRQ);
231
232 return info->u.pirq.pirq;
233}
234
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800235static unsigned gsi_from_irq(unsigned irq)
236{
237 struct irq_info *info = info_for_irq(irq);
238
239 BUG_ON(info == NULL);
240 BUG_ON(info->type != IRQT_PIRQ);
241
242 return info->u.pirq.gsi;
243}
244
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800245static enum xen_irq_type type_from_irq(unsigned irq)
246{
247 return info_for_irq(irq)->type;
248}
249
250static unsigned cpu_from_irq(unsigned irq)
251{
252 return info_for_irq(irq)->cpu;
253}
254
255static unsigned int cpu_from_evtchn(unsigned int evtchn)
256{
257 int irq = evtchn_to_irq[evtchn];
258 unsigned ret = 0;
259
260 if (irq != -1)
261 ret = cpu_from_irq(irq);
262
263 return ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700264}
265
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400266static bool pirq_needs_eoi(unsigned irq)
267{
268 struct irq_info *info = info_for_irq(irq);
269
270 BUG_ON(info->type != IRQT_PIRQ);
271
272 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
273}
274
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700275static inline unsigned long active_evtchns(unsigned int cpu,
276 struct shared_info *sh,
277 unsigned int idx)
278{
279 return (sh->evtchn_pending[idx] &
Ian Campbellcb60d112011-03-10 16:08:08 +0000280 per_cpu(cpu_evtchn_mask, cpu)[idx] &
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700281 ~sh->evtchn_mask[idx]);
282}
283
284static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
285{
286 int irq = evtchn_to_irq[chn];
287
288 BUG_ON(irq == -1);
289#ifdef CONFIG_SMP
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000290 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700291#endif
292
Ian Campbellcb60d112011-03-10 16:08:08 +0000293 clear_bit(chn, per_cpu(cpu_evtchn_mask, cpu_from_irq(irq)));
294 set_bit(chn, per_cpu(cpu_evtchn_mask, cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700295
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800296 irq_info[irq].cpu = cpu;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700297}
298
299static void init_evtchn_cpu_bindings(void)
300{
Jan Beulich1c6969e2010-11-16 14:55:33 -0800301 int i;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700302#ifdef CONFIG_SMP
Thomas Gleixner10e58082008-10-16 14:19:04 +0200303 struct irq_desc *desc;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200304
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700305 /* By default all event channels notify CPU#0. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800306 for_each_irq_desc(i, desc) {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000307 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800308 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700309#endif
310
Jan Beulich1c6969e2010-11-16 14:55:33 -0800311 for_each_possible_cpu(i)
Ian Campbellcb60d112011-03-10 16:08:08 +0000312 memset(per_cpu(cpu_evtchn_mask, i),
313 (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
Jan Beulich1c6969e2010-11-16 14:55:33 -0800314
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700315}
316
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700317static inline void clear_evtchn(int port)
318{
319 struct shared_info *s = HYPERVISOR_shared_info;
320 sync_clear_bit(port, &s->evtchn_pending[0]);
321}
322
323static inline void set_evtchn(int port)
324{
325 struct shared_info *s = HYPERVISOR_shared_info;
326 sync_set_bit(port, &s->evtchn_pending[0]);
327}
328
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700329static inline int test_evtchn(int port)
330{
331 struct shared_info *s = HYPERVISOR_shared_info;
332 return sync_test_bit(port, &s->evtchn_pending[0]);
333}
334
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700335
336/**
337 * notify_remote_via_irq - send event to remote end of event channel via irq
338 * @irq: irq of event channel to send event to
339 *
340 * Unlike notify_remote_via_evtchn(), this is safe to use across
341 * save/restore. Notifications on a broken connection are silently
342 * dropped.
343 */
344void notify_remote_via_irq(int irq)
345{
346 int evtchn = evtchn_from_irq(irq);
347
348 if (VALID_EVTCHN(evtchn))
349 notify_remote_via_evtchn(evtchn);
350}
351EXPORT_SYMBOL_GPL(notify_remote_via_irq);
352
353static void mask_evtchn(int port)
354{
355 struct shared_info *s = HYPERVISOR_shared_info;
356 sync_set_bit(port, &s->evtchn_mask[0]);
357}
358
359static void unmask_evtchn(int port)
360{
361 struct shared_info *s = HYPERVISOR_shared_info;
362 unsigned int cpu = get_cpu();
363
364 BUG_ON(!irqs_disabled());
365
366 /* Slow path (hypercall) if this is a non-local port. */
367 if (unlikely(cpu != cpu_from_evtchn(port))) {
368 struct evtchn_unmask unmask = { .port = port };
369 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
370 } else {
Christoph Lameter780f36d2010-12-06 11:16:29 -0600371 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700372
373 sync_clear_bit(port, &s->evtchn_mask[0]);
374
375 /*
376 * The following is basically the equivalent of
377 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
378 * the interrupt edge' if the channel is masked.
379 */
380 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
381 !sync_test_and_set_bit(port / BITS_PER_LONG,
382 &vcpu_info->evtchn_pending_sel))
383 vcpu_info->evtchn_upcall_pending = 1;
384 }
385
386 put_cpu();
387}
388
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000389static int xen_allocate_irq_dynamic(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700390{
Ian Campbell89911502011-03-03 11:57:44 -0500391 int first = 0;
392 int irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700393
Ian Campbell89911502011-03-03 11:57:44 -0500394#ifdef CONFIG_X86_IO_APIC
395 /*
396 * For an HVM guest or domain 0 which see "real" (emulated or
397 * actual repectively) GSIs we allocate dynamic IRQs
398 * e.g. those corresponding to event channels or MSIs
399 * etc. from the range above those "real" GSIs to avoid
400 * collisions.
Konrad Rzeszutek Wilkd1b758e2010-12-09 14:53:29 -0500401 */
Ian Campbell89911502011-03-03 11:57:44 -0500402 if (xen_initial_domain() || xen_hvm_domain())
403 first = get_nr_irqs_gsi();
404#endif
405
406retry:
407 irq = irq_alloc_desc_from(first, -1);
408
409 if (irq == -ENOMEM && first > NR_IRQS_LEGACY) {
410 printk(KERN_ERR "Out of dynamic IRQ space and eating into GSI space. You should increase nr_irqs\n");
411 first = max(NR_IRQS_LEGACY, first - NR_IRQS_LEGACY);
412 goto retry;
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100413 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700414
Ian Campbell89911502011-03-03 11:57:44 -0500415 if (irq < 0)
416 panic("No available IRQ to bind to: increase nr_irqs!\n");
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800417
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700418 return irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400419}
420
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000421static int xen_allocate_irq_gsi(unsigned gsi)
422{
423 int irq;
424
Ian Campbell89911502011-03-03 11:57:44 -0500425 /*
426 * A PV guest has no concept of a GSI (since it has no ACPI
427 * nor access to/knowledge of the physical APICs). Therefore
428 * all IRQs are dynamically allocated from the entire IRQ
429 * space.
430 */
431 if (xen_pv_domain() && !xen_initial_domain())
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000432 return xen_allocate_irq_dynamic();
433
434 /* Legacy IRQ descriptors are already allocated by the arch. */
435 if (gsi < NR_IRQS_LEGACY)
436 return gsi;
437
438 irq = irq_alloc_desc_at(gsi, -1);
439 if (irq < 0)
440 panic("Unable to allocate to IRQ%d (%d)\n", gsi, irq);
441
442 return irq;
443}
444
445static void xen_free_irq(unsigned irq)
446{
Ian Campbell9158c352011-03-10 16:08:09 +0000447 irq_info[irq].type = IRQT_UNBOUND;
448
Ian Campbell72146102011-02-03 09:49:35 +0000449 /* Legacy IRQ descriptors are managed by the arch. */
450 if (irq < NR_IRQS_LEGACY)
451 return;
452
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000453 irq_free_desc(irq);
454}
455
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400456static void pirq_unmask_notify(int irq)
457{
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100458 struct physdev_eoi eoi = { .irq = pirq_from_irq(irq) };
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400459
460 if (unlikely(pirq_needs_eoi(irq))) {
461 int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
462 WARN_ON(rc);
463 }
464}
465
466static void pirq_query_unmask(int irq)
467{
468 struct physdev_irq_status_query irq_status;
469 struct irq_info *info = info_for_irq(irq);
470
471 BUG_ON(info->type != IRQT_PIRQ);
472
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100473 irq_status.irq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400474 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
475 irq_status.flags = 0;
476
477 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
478 if (irq_status.flags & XENIRQSTAT_needs_eoi)
479 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
480}
481
482static bool probing_irq(int irq)
483{
484 struct irq_desc *desc = irq_to_desc(irq);
485
486 return desc && desc->action == NULL;
487}
488
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000489static unsigned int __startup_pirq(unsigned int irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400490{
491 struct evtchn_bind_pirq bind_pirq;
492 struct irq_info *info = info_for_irq(irq);
493 int evtchn = evtchn_from_irq(irq);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400494 int rc;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400495
496 BUG_ON(info->type != IRQT_PIRQ);
497
498 if (VALID_EVTCHN(evtchn))
499 goto out;
500
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100501 bind_pirq.pirq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400502 /* NB. We are happy to share unless we are probing. */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400503 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
504 BIND_PIRQ__WILL_SHARE : 0;
505 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
506 if (rc != 0) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400507 if (!probing_irq(irq))
508 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
509 irq);
510 return 0;
511 }
512 evtchn = bind_pirq.port;
513
514 pirq_query_unmask(irq);
515
516 evtchn_to_irq[evtchn] = irq;
517 bind_evtchn_to_cpu(evtchn, 0);
518 info->evtchn = evtchn;
519
520out:
521 unmask_evtchn(evtchn);
522 pirq_unmask_notify(irq);
523
524 return 0;
525}
526
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000527static unsigned int startup_pirq(struct irq_data *data)
528{
529 return __startup_pirq(data->irq);
530}
531
532static void shutdown_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400533{
534 struct evtchn_close close;
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000535 unsigned int irq = data->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400536 struct irq_info *info = info_for_irq(irq);
537 int evtchn = evtchn_from_irq(irq);
538
539 BUG_ON(info->type != IRQT_PIRQ);
540
541 if (!VALID_EVTCHN(evtchn))
542 return;
543
544 mask_evtchn(evtchn);
545
546 close.port = evtchn;
547 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
548 BUG();
549
550 bind_evtchn_to_cpu(evtchn, 0);
551 evtchn_to_irq[evtchn] = -1;
552 info->evtchn = 0;
553}
554
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000555static void enable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400556{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000557 startup_pirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400558}
559
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000560static void disable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400561{
562}
563
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000564static void ack_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400565{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000566 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400567
Ian Campbellaa673c12011-02-07 11:08:39 +0000568 move_native_irq(data->irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400569
570 if (VALID_EVTCHN(evtchn)) {
571 mask_evtchn(evtchn);
572 clear_evtchn(evtchn);
573 }
574}
575
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400576static int find_irq_by_gsi(unsigned gsi)
577{
578 int irq;
579
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400580 for (irq = 0; irq < nr_irqs; irq++) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400581 struct irq_info *info = info_for_irq(irq);
582
583 if (info == NULL || info->type != IRQT_PIRQ)
584 continue;
585
586 if (gsi_from_irq(irq) == gsi)
587 return irq;
588 }
589
590 return -1;
591}
592
Ian Campbellf4d06352011-03-10 16:08:07 +0000593int xen_allocate_pirq_gsi(unsigned gsi)
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100594{
Ian Campbellf4d06352011-03-10 16:08:07 +0000595 return gsi;
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100596}
597
Ian Campbell653378a2011-03-10 16:08:04 +0000598/*
599 * Do not make any assumptions regarding the relationship between the
600 * IRQ number returned here and the Xen pirq argument.
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100601 *
602 * Note: We don't assign an event channel until the irq actually started
603 * up. Return an existing irq if we've already got one for the gsi.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400604 */
Ian Campbellf4d06352011-03-10 16:08:07 +0000605int xen_bind_pirq_gsi_to_irq(unsigned gsi,
606 unsigned pirq, int shareable, char *name)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400607{
Ian Campbella0e18112011-03-10 16:08:03 +0000608 int irq = -1;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400609 struct physdev_irq irq_op;
610
611 spin_lock(&irq_mapping_update_lock);
612
Stefano Stabellinie5fc7342010-12-01 14:51:44 +0000613 if ((pirq > nr_irqs) || (gsi > nr_irqs)) {
Stefano Stabellini01557ba2010-08-20 14:46:52 +0100614 printk(KERN_WARNING "xen_map_pirq_gsi: %s %s is incorrect!\n",
Stefano Stabellinie5fc7342010-12-01 14:51:44 +0000615 pirq > nr_irqs ? "pirq" :"",
616 gsi > nr_irqs ? "gsi" : "");
Stefano Stabellini01557ba2010-08-20 14:46:52 +0100617 goto out;
618 }
619
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400620 irq = find_irq_by_gsi(gsi);
621 if (irq != -1) {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100622 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400623 irq, gsi);
624 goto out; /* XXX need refcount? */
625 }
626
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000627 irq = xen_allocate_irq_gsi(gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400628
629 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
Gerd Hoffmann1a60d052010-10-04 13:42:27 -0400630 handle_level_irq, name);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400631
632 irq_op.irq = irq;
Alex Nixonb5401a92010-03-18 16:31:34 -0400633 irq_op.vector = 0;
634
635 /* Only the privileged domain can do this. For non-priv, the pcifront
636 * driver provides a PCI bus that does the call to do exactly
637 * this in the priv domain. */
638 if (xen_initial_domain() &&
639 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000640 xen_free_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400641 irq = -ENOSPC;
642 goto out;
643 }
644
Ian Campbell9158c352011-03-10 16:08:09 +0000645 xen_irq_info_pirq_init(irq, 0, pirq, gsi, irq_op.vector,
646 shareable ? PIRQ_SHAREABLE : 0);
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100647 pirq_to_irq[pirq] = irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400648
649out:
650 spin_unlock(&irq_mapping_update_lock);
651
652 return irq;
653}
654
Qing Hef731e3ef2010-10-11 15:30:09 +0100655#ifdef CONFIG_PCI_MSI
Ian Campbellbf480d92011-02-18 16:43:32 +0000656int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000657{
Ian Campbell5cad61a2011-02-18 16:43:31 +0000658 int rc;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000659 struct physdev_get_free_pirq op_get_free_pirq;
Ian Campbell5cad61a2011-02-18 16:43:31 +0000660
Ian Campbellbf480d92011-02-18 16:43:32 +0000661 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000662 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000663
Ian Campbell5cad61a2011-02-18 16:43:31 +0000664 WARN_ONCE(rc == -ENOSYS,
665 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
666
667 return rc ? -1 : op_get_free_pirq.pirq;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000668}
669
Ian Campbellbf480d92011-02-18 16:43:32 +0000670int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
Ian Campbellca1d8fe2011-02-18 16:43:36 +0000671 int pirq, int vector, const char *name)
Stefano Stabellini809f9262010-07-01 17:10:39 +0100672{
Ian Campbellbf480d92011-02-18 16:43:32 +0000673 int irq, ret;
Ian Campbell4b41df72011-02-18 16:43:29 +0000674
Stefano Stabellini809f9262010-07-01 17:10:39 +0100675 spin_lock(&irq_mapping_update_lock);
676
Ian Campbell4b41df72011-02-18 16:43:29 +0000677 irq = xen_allocate_irq_dynamic();
678 if (irq == -1)
Ian Campbellbb5d0792011-02-18 16:43:28 +0000679 goto out;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100680
Ian Campbell4b41df72011-02-18 16:43:29 +0000681 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
Stefano Stabellini809f9262010-07-01 17:10:39 +0100682 handle_level_irq, name);
683
Ian Campbell9158c352011-03-10 16:08:09 +0000684 xen_irq_info_pirq_init(irq, 0, pirq, 0, vector, 0);
Ian Campbellbf480d92011-02-18 16:43:32 +0000685 pirq_to_irq[pirq] = irq;
686 ret = set_irq_msi(irq, msidesc);
687 if (ret < 0)
688 goto error_irq;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100689out:
690 spin_unlock(&irq_mapping_update_lock);
Ian Campbell4b41df72011-02-18 16:43:29 +0000691 return irq;
Ian Campbellbf480d92011-02-18 16:43:32 +0000692error_irq:
693 spin_unlock(&irq_mapping_update_lock);
694 xen_free_irq(irq);
695 return -1;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100696}
Qing Hef731e3ef2010-10-11 15:30:09 +0100697#endif
698
Alex Nixonb5401a92010-03-18 16:31:34 -0400699int xen_destroy_irq(int irq)
700{
701 struct irq_desc *desc;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100702 struct physdev_unmap_pirq unmap_irq;
703 struct irq_info *info = info_for_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400704 int rc = -ENOENT;
705
706 spin_lock(&irq_mapping_update_lock);
707
708 desc = irq_to_desc(irq);
709 if (!desc)
710 goto out;
711
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100712 if (xen_initial_domain()) {
Konrad Rzeszutek Wilk12334712010-11-19 11:27:09 -0500713 unmap_irq.pirq = info->u.pirq.pirq;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100714 unmap_irq.domid = DOMID_SELF;
715 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
716 if (rc) {
717 printk(KERN_WARNING "unmap irq failed %d\n", rc);
718 goto out;
719 }
720 }
Konrad Rzeszutek Wilk1aa0b512011-02-17 11:23:58 -0500721 pirq_to_irq[info->u.pirq.pirq] = -1;
722
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000723 xen_free_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400724
725out:
726 spin_unlock(&irq_mapping_update_lock);
727 return rc;
728}
729
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000730int xen_irq_from_pirq(unsigned pirq)
731{
732 return pirq_to_irq[pirq];
733}
734
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700735int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700736{
737 int irq;
738
739 spin_lock(&irq_mapping_update_lock);
740
741 irq = evtchn_to_irq[evtchn];
742
743 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000744 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700745
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700746 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -0700747 handle_fasteoi_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700748
749 evtchn_to_irq[evtchn] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000750 xen_irq_info_evtchn_init(irq, evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700751 }
752
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700753 spin_unlock(&irq_mapping_update_lock);
754
755 return irq;
756}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700757EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700758
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700759static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
760{
761 struct evtchn_bind_ipi bind_ipi;
762 int evtchn, irq;
763
764 spin_lock(&irq_mapping_update_lock);
765
766 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800767
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700768 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000769 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700770 if (irq < 0)
771 goto out;
772
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700773 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
774 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700775
776 bind_ipi.vcpu = cpu;
777 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
778 &bind_ipi) != 0)
779 BUG();
780 evtchn = bind_ipi.port;
781
782 evtchn_to_irq[evtchn] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000783 xen_irq_info_ipi_init(irq, evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700784 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
785
786 bind_evtchn_to_cpu(evtchn, cpu);
787 }
788
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700789 out:
790 spin_unlock(&irq_mapping_update_lock);
791 return irq;
792}
793
794
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100795int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700796{
797 struct evtchn_bind_virq bind_virq;
798 int evtchn, irq;
799
800 spin_lock(&irq_mapping_update_lock);
801
802 irq = per_cpu(virq_to_irq, cpu)[virq];
803
804 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000805 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700806
807 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
808 handle_percpu_irq, "virq");
809
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700810 bind_virq.virq = virq;
811 bind_virq.vcpu = cpu;
812 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
813 &bind_virq) != 0)
814 BUG();
815 evtchn = bind_virq.port;
816
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700817 evtchn_to_irq[evtchn] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000818 xen_irq_info_virq_init(irq, evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700819
820 per_cpu(virq_to_irq, cpu)[virq] = irq;
821
822 bind_evtchn_to_cpu(evtchn, cpu);
823 }
824
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700825 spin_unlock(&irq_mapping_update_lock);
826
827 return irq;
828}
829
830static void unbind_from_irq(unsigned int irq)
831{
832 struct evtchn_close close;
833 int evtchn = evtchn_from_irq(irq);
834
835 spin_lock(&irq_mapping_update_lock);
836
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800837 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700838 close.port = evtchn;
839 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
840 BUG();
841
842 switch (type_from_irq(irq)) {
843 case IRQT_VIRQ:
844 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800845 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700846 break;
Alex Nixond68d82a2008-08-22 11:52:15 +0100847 case IRQT_IPI:
848 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800849 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +0100850 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700851 default:
852 break;
853 }
854
855 /* Closed ports are implicitly re-bound to VCPU0. */
856 bind_evtchn_to_cpu(evtchn, 0);
857
858 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +0000859 }
860
Ian Campbell9158c352011-03-10 16:08:09 +0000861 BUG_ON(irq_info[irq].type == IRQT_UNBOUND);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700862
Ian Campbell9158c352011-03-10 16:08:09 +0000863 xen_free_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700864
865 spin_unlock(&irq_mapping_update_lock);
866}
867
868int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -0400869 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700870 unsigned long irqflags,
871 const char *devname, void *dev_id)
872{
873 unsigned int irq;
874 int retval;
875
876 irq = bind_evtchn_to_irq(evtchn);
877 retval = request_irq(irq, handler, irqflags, devname, dev_id);
878 if (retval != 0) {
879 unbind_from_irq(irq);
880 return retval;
881 }
882
883 return irq;
884}
885EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
886
887int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -0400888 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700889 unsigned long irqflags, const char *devname, void *dev_id)
890{
891 unsigned int irq;
892 int retval;
893
894 irq = bind_virq_to_irq(virq, cpu);
895 retval = request_irq(irq, handler, irqflags, devname, dev_id);
896 if (retval != 0) {
897 unbind_from_irq(irq);
898 return retval;
899 }
900
901 return irq;
902}
903EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
904
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700905int bind_ipi_to_irqhandler(enum ipi_vector ipi,
906 unsigned int cpu,
907 irq_handler_t handler,
908 unsigned long irqflags,
909 const char *devname,
910 void *dev_id)
911{
912 int irq, retval;
913
914 irq = bind_ipi_to_irq(ipi, cpu);
915 if (irq < 0)
916 return irq;
917
Thomas Gleixner676dc3c2011-02-05 20:08:59 +0000918 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700919 retval = request_irq(irq, handler, irqflags, devname, dev_id);
920 if (retval != 0) {
921 unbind_from_irq(irq);
922 return retval;
923 }
924
925 return irq;
926}
927
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700928void unbind_from_irqhandler(unsigned int irq, void *dev_id)
929{
930 free_irq(irq, dev_id);
931 unbind_from_irq(irq);
932}
933EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
934
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700935void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
936{
937 int irq = per_cpu(ipi_to_irq, cpu)[vector];
938 BUG_ON(irq < 0);
939 notify_remote_via_irq(irq);
940}
941
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700942irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
943{
944 struct shared_info *sh = HYPERVISOR_shared_info;
945 int cpu = smp_processor_id();
Ian Campbellcb60d112011-03-10 16:08:08 +0000946 unsigned long *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700947 int i;
948 unsigned long flags;
949 static DEFINE_SPINLOCK(debug_lock);
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100950 struct vcpu_info *v;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700951
952 spin_lock_irqsave(&debug_lock, flags);
953
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100954 printk("\nvcpu %d\n ", cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700955
956 for_each_online_cpu(i) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100957 int pending;
958 v = per_cpu(xen_vcpu, i);
959 pending = (get_irq_regs() && i == cpu)
960 ? xen_irqs_disabled(get_irq_regs())
961 : v->evtchn_upcall_mask;
962 printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i,
963 pending, v->evtchn_upcall_pending,
964 (int)(sizeof(v->evtchn_pending_sel)*2),
965 v->evtchn_pending_sel);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700966 }
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100967 v = per_cpu(xen_vcpu, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700968
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100969 printk("\npending:\n ");
970 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
971 printk("%0*lx%s", (int)sizeof(sh->evtchn_pending[0])*2,
972 sh->evtchn_pending[i],
973 i % 8 == 0 ? "\n " : " ");
974 printk("\nglobal mask:\n ");
975 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
976 printk("%0*lx%s",
977 (int)(sizeof(sh->evtchn_mask[0])*2),
978 sh->evtchn_mask[i],
979 i % 8 == 0 ? "\n " : " ");
980
981 printk("\nglobally unmasked:\n ");
982 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
983 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
984 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
985 i % 8 == 0 ? "\n " : " ");
986
987 printk("\nlocal cpu%d mask:\n ", cpu);
988 for (i = (NR_EVENT_CHANNELS/BITS_PER_LONG)-1; i >= 0; i--)
989 printk("%0*lx%s", (int)(sizeof(cpu_evtchn[0])*2),
990 cpu_evtchn[i],
991 i % 8 == 0 ? "\n " : " ");
992
993 printk("\nlocally unmasked:\n ");
994 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
995 unsigned long pending = sh->evtchn_pending[i]
996 & ~sh->evtchn_mask[i]
997 & cpu_evtchn[i];
998 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
999 pending, i % 8 == 0 ? "\n " : " ");
1000 }
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001001
1002 printk("\npending list:\n");
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001003 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001004 if (sync_test_bit(i, sh->evtchn_pending)) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001005 int word_idx = i / BITS_PER_LONG;
1006 printk(" %d: event %d -> irq %d%s%s%s\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001007 cpu_from_evtchn(i), i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001008 evtchn_to_irq[i],
1009 sync_test_bit(word_idx, &v->evtchn_pending_sel)
1010 ? "" : " l2-clear",
1011 !sync_test_bit(i, sh->evtchn_mask)
1012 ? "" : " globally-masked",
1013 sync_test_bit(i, cpu_evtchn)
1014 ? "" : " locally-masked");
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001015 }
1016 }
1017
1018 spin_unlock_irqrestore(&debug_lock, flags);
1019
1020 return IRQ_HANDLED;
1021}
1022
Tejun Heo245b2e72009-06-24 15:13:48 +09001023static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1024
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001025/*
1026 * Search the CPUs pending events bitmasks. For each one found, map
1027 * the event number to an irq, and feed it into do_IRQ() for
1028 * handling.
1029 *
1030 * Xen uses a two-level bitmap to speed searching. The first level is
1031 * a bitset of words which contain pending event bits. The second
1032 * level is a bitset of pending events themselves.
1033 */
Sheng Yang38e20b02010-05-14 12:40:51 +01001034static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001035{
1036 int cpu = get_cpu();
1037 struct shared_info *s = HYPERVISOR_shared_info;
Christoph Lameter780f36d2010-12-06 11:16:29 -06001038 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001039 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001040
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001041 do {
1042 unsigned long pending_words;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001043
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001044 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001045
Christoph Lameterb2e4ae62010-12-06 11:40:07 -06001046 if (__this_cpu_inc_return(xed_nesting_count) - 1)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001047 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001048
Isaku Yamahatae849c3e2008-04-02 10:53:56 -07001049#ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
1050 /* Clear master flag /before/ clearing selector flag. */
Isaku Yamahata6673cf62008-06-16 14:58:13 -07001051 wmb();
Isaku Yamahatae849c3e2008-04-02 10:53:56 -07001052#endif
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001053 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
1054 while (pending_words != 0) {
1055 unsigned long pending_bits;
1056 int word_idx = __ffs(pending_words);
1057 pending_words &= ~(1UL << word_idx);
1058
1059 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
1060 int bit_idx = __ffs(pending_bits);
1061 int port = (word_idx * BITS_PER_LONG) + bit_idx;
1062 int irq = evtchn_to_irq[port];
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001063 struct irq_desc *desc;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001064
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001065 mask_evtchn(port);
1066 clear_evtchn(port);
1067
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001068 if (irq != -1) {
1069 desc = irq_to_desc(irq);
1070 if (desc)
1071 generic_handle_irq_desc(irq, desc);
1072 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001073 }
1074 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001075
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001076 BUG_ON(!irqs_disabled());
1077
Christoph Lameter780f36d2010-12-06 11:16:29 -06001078 count = __this_cpu_read(xed_nesting_count);
1079 __this_cpu_write(xed_nesting_count, 0);
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001080 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001081
1082out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -08001083
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001084 put_cpu();
1085}
1086
Sheng Yang38e20b02010-05-14 12:40:51 +01001087void xen_evtchn_do_upcall(struct pt_regs *regs)
1088{
1089 struct pt_regs *old_regs = set_irq_regs(regs);
1090
1091 exit_idle();
1092 irq_enter();
1093
1094 __xen_evtchn_do_upcall();
1095
1096 irq_exit();
1097 set_irq_regs(old_regs);
1098}
1099
1100void xen_hvm_evtchn_do_upcall(void)
1101{
1102 __xen_evtchn_do_upcall();
1103}
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001104EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +01001105
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001106/* Rebind a new event channel to an existing irq. */
1107void rebind_evtchn_irq(int evtchn, int irq)
1108{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001109 struct irq_info *info = info_for_irq(irq);
1110
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001111 /* Make sure the irq is masked, since the new event channel
1112 will also be masked. */
1113 disable_irq(irq);
1114
1115 spin_lock(&irq_mapping_update_lock);
1116
1117 /* After resume the irq<->evtchn mappings are all cleared out */
1118 BUG_ON(evtchn_to_irq[evtchn] != -1);
1119 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001120 so there should be a proper type */
1121 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001122
1123 evtchn_to_irq[evtchn] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +00001124 xen_irq_info_evtchn_init(irq, evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001125
1126 spin_unlock(&irq_mapping_update_lock);
1127
1128 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +10301129 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001130
1131 /* Unmask the event channel. */
1132 enable_irq(irq);
1133}
1134
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001135/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -07001136static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001137{
1138 struct evtchn_bind_vcpu bind_vcpu;
1139 int evtchn = evtchn_from_irq(irq);
1140
Ian Campbellbe494722011-03-10 16:08:02 +00001141 if (!VALID_EVTCHN(evtchn))
1142 return -1;
1143
1144 /*
1145 * Events delivered via platform PCI interrupts are always
1146 * routed to vcpu 0 and hence cannot be rebound.
1147 */
1148 if (xen_hvm_domain() && !xen_have_vector_callback)
Yinghai Lud5dedd42009-04-27 17:59:21 -07001149 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001150
1151 /* Send future instances of this interrupt to other vcpu. */
1152 bind_vcpu.port = evtchn;
1153 bind_vcpu.vcpu = tcpu;
1154
1155 /*
1156 * If this fails, it usually just indicates that we're dealing with a
1157 * virq or IPI channel, which don't actually need to be rebound. Ignore
1158 * it, but don't do the xenlinux-level rebind in that case.
1159 */
1160 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1161 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001162
1163 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001164}
1165
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001166static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1167 bool force)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001168{
Rusty Russell0de26522008-12-13 21:20:26 +10301169 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001170
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001171 return rebind_irq_to_cpu(data->irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001172}
1173
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001174int resend_irq_on_evtchn(unsigned int irq)
1175{
1176 int masked, evtchn = evtchn_from_irq(irq);
1177 struct shared_info *s = HYPERVISOR_shared_info;
1178
1179 if (!VALID_EVTCHN(evtchn))
1180 return 1;
1181
1182 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
1183 sync_set_bit(evtchn, s->evtchn_pending);
1184 if (!masked)
1185 unmask_evtchn(evtchn);
1186
1187 return 1;
1188}
1189
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001190static void enable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001191{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001192 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001193
1194 if (VALID_EVTCHN(evtchn))
1195 unmask_evtchn(evtchn);
1196}
1197
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001198static void disable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001199{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001200 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001201
1202 if (VALID_EVTCHN(evtchn))
1203 mask_evtchn(evtchn);
1204}
1205
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001206static void ack_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001207{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001208 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001209
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001210 move_masked_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001211
1212 if (VALID_EVTCHN(evtchn))
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001213 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001214}
1215
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001216static int retrigger_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001217{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001218 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001219 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001220 int ret = 0;
1221
1222 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001223 int masked;
1224
1225 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1226 sync_set_bit(evtchn, sh->evtchn_pending);
1227 if (!masked)
1228 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001229 ret = 1;
1230 }
1231
1232 return ret;
1233}
1234
Ian Campbell0a852262011-03-10 16:08:06 +00001235static void restore_pirqs(void)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001236{
1237 int pirq, rc, irq, gsi;
1238 struct physdev_map_pirq map_irq;
1239
1240 for (pirq = 0; pirq < nr_irqs; pirq++) {
1241 irq = pirq_to_irq[pirq];
1242 if (irq == -1)
1243 continue;
1244
1245 /* save/restore of PT devices doesn't work, so at this point the
1246 * only devices present are GSI based emulated devices */
1247 gsi = gsi_from_irq(irq);
1248 if (!gsi)
1249 continue;
1250
1251 map_irq.domid = DOMID_SELF;
1252 map_irq.type = MAP_PIRQ_TYPE_GSI;
1253 map_irq.index = gsi;
1254 map_irq.pirq = pirq;
1255
1256 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1257 if (rc) {
1258 printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1259 gsi, irq, pirq, rc);
Ian Campbell9158c352011-03-10 16:08:09 +00001260 xen_free_irq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001261 pirq_to_irq[pirq] = -1;
1262 continue;
1263 }
1264
1265 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1266
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001267 __startup_pirq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001268 }
1269}
1270
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001271static void restore_cpu_virqs(unsigned int cpu)
1272{
1273 struct evtchn_bind_virq bind_virq;
1274 int virq, irq, evtchn;
1275
1276 for (virq = 0; virq < NR_VIRQS; virq++) {
1277 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1278 continue;
1279
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001280 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001281
1282 /* Get a new binding from Xen. */
1283 bind_virq.virq = virq;
1284 bind_virq.vcpu = cpu;
1285 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1286 &bind_virq) != 0)
1287 BUG();
1288 evtchn = bind_virq.port;
1289
1290 /* Record the new mapping. */
1291 evtchn_to_irq[evtchn] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +00001292 xen_irq_info_virq_init(irq, evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001293 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001294 }
1295}
1296
1297static void restore_cpu_ipis(unsigned int cpu)
1298{
1299 struct evtchn_bind_ipi bind_ipi;
1300 int ipi, irq, evtchn;
1301
1302 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1303 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1304 continue;
1305
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001306 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001307
1308 /* Get a new binding from Xen. */
1309 bind_ipi.vcpu = cpu;
1310 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1311 &bind_ipi) != 0)
1312 BUG();
1313 evtchn = bind_ipi.port;
1314
1315 /* Record the new mapping. */
1316 evtchn_to_irq[evtchn] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +00001317 xen_irq_info_ipi_init(irq, evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001318 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001319 }
1320}
1321
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001322/* Clear an irq's pending state, in preparation for polling on it */
1323void xen_clear_irq_pending(int irq)
1324{
1325 int evtchn = evtchn_from_irq(irq);
1326
1327 if (VALID_EVTCHN(evtchn))
1328 clear_evtchn(evtchn);
1329}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001330EXPORT_SYMBOL(xen_clear_irq_pending);
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001331void xen_set_irq_pending(int irq)
1332{
1333 int evtchn = evtchn_from_irq(irq);
1334
1335 if (VALID_EVTCHN(evtchn))
1336 set_evtchn(evtchn);
1337}
1338
1339bool xen_test_irq_pending(int irq)
1340{
1341 int evtchn = evtchn_from_irq(irq);
1342 bool ret = false;
1343
1344 if (VALID_EVTCHN(evtchn))
1345 ret = test_evtchn(evtchn);
1346
1347 return ret;
1348}
1349
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001350/* Poll waiting for an irq to become pending with timeout. In the usual case,
1351 * the irq will be disabled so it won't deliver an interrupt. */
1352void xen_poll_irq_timeout(int irq, u64 timeout)
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001353{
1354 evtchn_port_t evtchn = evtchn_from_irq(irq);
1355
1356 if (VALID_EVTCHN(evtchn)) {
1357 struct sched_poll poll;
1358
1359 poll.nr_ports = 1;
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001360 poll.timeout = timeout;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001361 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001362
1363 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1364 BUG();
1365 }
1366}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001367EXPORT_SYMBOL(xen_poll_irq_timeout);
1368/* Poll waiting for an irq to become pending. In the usual case, the
1369 * irq will be disabled so it won't deliver an interrupt. */
1370void xen_poll_irq(int irq)
1371{
1372 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1373}
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001374
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001375void xen_irq_resume(void)
1376{
1377 unsigned int cpu, irq, evtchn;
1378
1379 init_evtchn_cpu_bindings();
1380
1381 /* New event-channel space is not 'live' yet. */
1382 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1383 mask_evtchn(evtchn);
1384
1385 /* No IRQ <-> event-channel mappings. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001386 for (irq = 0; irq < nr_irqs; irq++)
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001387 irq_info[irq].evtchn = 0; /* zap event-channel binding */
1388
1389 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1390 evtchn_to_irq[evtchn] = -1;
1391
1392 for_each_possible_cpu(cpu) {
1393 restore_cpu_virqs(cpu);
1394 restore_cpu_ipis(cpu);
1395 }
Ian Campbell69035912010-11-01 16:30:09 +00001396
Ian Campbell0a852262011-03-10 16:08:06 +00001397 restore_pirqs();
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001398}
1399
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001400static struct irq_chip xen_dynamic_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001401 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001402
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001403 .irq_disable = disable_dynirq,
1404 .irq_mask = disable_dynirq,
1405 .irq_unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001406
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001407 .irq_eoi = ack_dynirq,
1408 .irq_set_affinity = set_affinity_irq,
1409 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001410};
1411
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001412static struct irq_chip xen_pirq_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001413 .name = "xen-pirq",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001414
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001415 .irq_startup = startup_pirq,
1416 .irq_shutdown = shutdown_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001417
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001418 .irq_enable = enable_pirq,
1419 .irq_unmask = enable_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001420
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001421 .irq_disable = disable_pirq,
1422 .irq_mask = disable_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001423
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001424 .irq_ack = ack_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001425
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001426 .irq_set_affinity = set_affinity_irq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001427
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001428 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001429};
1430
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001431static struct irq_chip xen_percpu_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001432 .name = "xen-percpu",
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001433
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001434 .irq_disable = disable_dynirq,
1435 .irq_mask = disable_dynirq,
1436 .irq_unmask = enable_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001437
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001438 .irq_ack = ack_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001439};
1440
Sheng Yang38e20b02010-05-14 12:40:51 +01001441int xen_set_callback_via(uint64_t via)
1442{
1443 struct xen_hvm_param a;
1444 a.domid = DOMID_SELF;
1445 a.index = HVM_PARAM_CALLBACK_IRQ;
1446 a.value = via;
1447 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1448}
1449EXPORT_SYMBOL_GPL(xen_set_callback_via);
1450
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001451#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001452/* Vector callbacks are better than PCI interrupts to receive event
1453 * channel notifications because we can receive vector callbacks on any
1454 * vcpu and we don't need PCI support or APIC interactions. */
1455void xen_callback_vector(void)
1456{
1457 int rc;
1458 uint64_t callback_via;
1459 if (xen_have_vector_callback) {
1460 callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
1461 rc = xen_set_callback_via(callback_via);
1462 if (rc) {
1463 printk(KERN_ERR "Request for Xen HVM callback vector"
1464 " failed.\n");
1465 xen_have_vector_callback = 0;
1466 return;
1467 }
1468 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1469 "enabled\n");
1470 /* in the restore case the vector has already been allocated */
1471 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
1472 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
1473 }
1474}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001475#else
1476void xen_callback_vector(void) {}
1477#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001478
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001479void __init xen_init_IRQ(void)
1480{
Stefano Stabellinie5fc7342010-12-01 14:51:44 +00001481 int i;
Mike Travisc7a35892009-01-10 21:58:11 -08001482
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001483 irq_info = kcalloc(nr_irqs, sizeof(*irq_info), GFP_KERNEL);
1484
Stefano Stabellinie5fc7342010-12-01 14:51:44 +00001485 /* We are using nr_irqs as the maximum number of pirq available but
1486 * that number is actually chosen by Xen and we don't know exactly
1487 * what it is. Be careful choosing high pirq numbers. */
1488 pirq_to_irq = kcalloc(nr_irqs, sizeof(*pirq_to_irq), GFP_KERNEL);
1489 for (i = 0; i < nr_irqs; i++)
Stefano Stabellini7a043f12010-07-01 17:08:14 +01001490 pirq_to_irq[i] = -1;
1491
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001492 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1493 GFP_KERNEL);
1494 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1495 evtchn_to_irq[i] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001496
1497 init_evtchn_cpu_bindings();
1498
1499 /* No event channels are 'live' right now. */
1500 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1501 mask_evtchn(i);
1502
Sheng Yang38e20b02010-05-14 12:40:51 +01001503 if (xen_hvm_domain()) {
1504 xen_callback_vector();
1505 native_init_IRQ();
Stefano Stabellini3942b742010-06-24 17:50:18 +01001506 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1507 * __acpi_register_gsi can point at the right function */
1508 pci_xen_hvm_init();
Sheng Yang38e20b02010-05-14 12:40:51 +01001509 } else {
1510 irq_ctx_init(smp_processor_id());
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +01001511 if (xen_initial_domain())
1512 xen_setup_pirqs();
Sheng Yang38e20b02010-05-14 12:40:51 +01001513 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001514}