blob: bc69a9d92abc4c77783e65fa868fc77e3de89e4b [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 Fitzhardingee46cdb62007-07-17 18:37:05 -070031
Sheng Yang38e20b02010-05-14 12:40:51 +010032#include <asm/desc.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070033#include <asm/ptrace.h>
34#include <asm/irq.h>
Jeremy Fitzhardinge792dc4f2009-02-06 14:09:43 -080035#include <asm/idle.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070036#include <asm/sync_bitops.h>
37#include <asm/xen/hypercall.h>
Adrian Bunk8d1b8752007-07-20 00:31:44 -070038#include <asm/xen/hypervisor.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070039
Sheng Yang38e20b02010-05-14 12:40:51 +010040#include <xen/xen.h>
41#include <xen/hvm.h>
Isaku Yamahatae04d0d02008-04-02 10:53:55 -070042#include <xen/xen-ops.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070043#include <xen/events.h>
44#include <xen/interface/xen.h>
45#include <xen/interface/event_channel.h>
Sheng Yang38e20b02010-05-14 12:40:51 +010046#include <xen/interface/hvm/hvm_op.h>
47#include <xen/interface/hvm/params.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070048
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -040049/* Leave low irqs free for identity mapping */
50#define LEGACY_IRQS 16
51
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070052/*
53 * This lock protects updates to the following mapping and reference-count
54 * arrays. The lock does not need to be acquired to read the mapping tables.
55 */
56static DEFINE_SPINLOCK(irq_mapping_update_lock);
57
58/* IRQ <-> VIRQ mapping. */
Tejun Heo204fba42009-06-24 15:13:45 +090059static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070060
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070061/* IRQ <-> IPI mapping */
Tejun Heo204fba42009-06-24 15:13:45 +090062static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070063
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080064/* Interrupt types. */
65enum xen_irq_type {
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -080066 IRQT_UNBOUND = 0,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070067 IRQT_PIRQ,
68 IRQT_VIRQ,
69 IRQT_IPI,
70 IRQT_EVTCHN
71};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070072
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080073/*
74 * Packed IRQ information:
75 * type - enum xen_irq_type
76 * event channel - irq->event channel mapping
77 * cpu - cpu this event channel is bound to
78 * index - type-specific information:
79 * PIRQ - vector, with MSB being "needs EIO"
80 * VIRQ - virq number
81 * IPI - IPI vector
82 * EVTCHN -
83 */
84struct irq_info
85{
86 enum xen_irq_type type; /* type */
87 unsigned short evtchn; /* event channel */
88 unsigned short cpu; /* cpu bound */
89
90 union {
91 unsigned short virq;
92 enum ipi_vector ipi;
93 struct {
94 unsigned short gsi;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -040095 unsigned char vector;
96 unsigned char flags;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080097 } pirq;
98 } u;
99};
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400100#define PIRQ_NEEDS_EOI (1 << 0)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800101
102static struct irq_info irq_info[NR_IRQS];
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700103
104static int evtchn_to_irq[NR_EVENT_CHANNELS] = {
105 [0 ... NR_EVENT_CHANNELS-1] = -1
106};
Mike Travisc7a35892009-01-10 21:58:11 -0800107struct cpu_evtchn_s {
108 unsigned long bits[NR_EVENT_CHANNELS/BITS_PER_LONG];
109};
110static struct cpu_evtchn_s *cpu_evtchn_mask_p;
111static inline unsigned long *cpu_evtchn_mask(int cpu)
112{
113 return cpu_evtchn_mask_p[cpu].bits;
114}
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700115
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700116/* Xen will never allocate port zero for any purpose. */
117#define VALID_EVTCHN(chn) ((chn) != 0)
118
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700119static struct irq_chip xen_dynamic_chip;
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700120static struct irq_chip xen_percpu_chip;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400121static struct irq_chip xen_pirq_chip;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700122
123/* Constructor for packed IRQ information. */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800124static struct irq_info mk_unbound_info(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700125{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800126 return (struct irq_info) { .type = IRQT_UNBOUND };
127}
128
129static struct irq_info mk_evtchn_info(unsigned short evtchn)
130{
Ian Campbell90af9512009-02-06 16:55:58 -0800131 return (struct irq_info) { .type = IRQT_EVTCHN, .evtchn = evtchn,
132 .cpu = 0 };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800133}
134
135static struct irq_info mk_ipi_info(unsigned short evtchn, enum ipi_vector ipi)
136{
137 return (struct irq_info) { .type = IRQT_IPI, .evtchn = evtchn,
Ian Campbell90af9512009-02-06 16:55:58 -0800138 .cpu = 0, .u.ipi = ipi };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800139}
140
141static struct irq_info mk_virq_info(unsigned short evtchn, unsigned short virq)
142{
143 return (struct irq_info) { .type = IRQT_VIRQ, .evtchn = evtchn,
Ian Campbell90af9512009-02-06 16:55:58 -0800144 .cpu = 0, .u.virq = virq };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800145}
146
147static struct irq_info mk_pirq_info(unsigned short evtchn,
148 unsigned short gsi, unsigned short vector)
149{
150 return (struct irq_info) { .type = IRQT_PIRQ, .evtchn = evtchn,
Ian Campbell90af9512009-02-06 16:55:58 -0800151 .cpu = 0, .u.pirq = { .gsi = gsi, .vector = vector } };
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700152}
153
154/*
155 * Accessors for packed IRQ information.
156 */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800157static struct irq_info *info_for_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700158{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800159 return &irq_info[irq];
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700160}
161
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800162static unsigned int evtchn_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700163{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800164 return info_for_irq(irq)->evtchn;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700165}
166
Ian Campbelld4c04532009-02-06 19:20:31 -0800167unsigned irq_from_evtchn(unsigned int evtchn)
168{
169 return evtchn_to_irq[evtchn];
170}
171EXPORT_SYMBOL_GPL(irq_from_evtchn);
172
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800173static enum ipi_vector ipi_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700174{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800175 struct irq_info *info = info_for_irq(irq);
176
177 BUG_ON(info == NULL);
178 BUG_ON(info->type != IRQT_IPI);
179
180 return info->u.ipi;
181}
182
183static unsigned virq_from_irq(unsigned irq)
184{
185 struct irq_info *info = info_for_irq(irq);
186
187 BUG_ON(info == NULL);
188 BUG_ON(info->type != IRQT_VIRQ);
189
190 return info->u.virq;
191}
192
193static unsigned gsi_from_irq(unsigned irq)
194{
195 struct irq_info *info = info_for_irq(irq);
196
197 BUG_ON(info == NULL);
198 BUG_ON(info->type != IRQT_PIRQ);
199
200 return info->u.pirq.gsi;
201}
202
203static unsigned vector_from_irq(unsigned irq)
204{
205 struct irq_info *info = info_for_irq(irq);
206
207 BUG_ON(info == NULL);
208 BUG_ON(info->type != IRQT_PIRQ);
209
210 return info->u.pirq.vector;
211}
212
213static enum xen_irq_type type_from_irq(unsigned irq)
214{
215 return info_for_irq(irq)->type;
216}
217
218static unsigned cpu_from_irq(unsigned irq)
219{
220 return info_for_irq(irq)->cpu;
221}
222
223static unsigned int cpu_from_evtchn(unsigned int evtchn)
224{
225 int irq = evtchn_to_irq[evtchn];
226 unsigned ret = 0;
227
228 if (irq != -1)
229 ret = cpu_from_irq(irq);
230
231 return ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700232}
233
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400234static bool pirq_needs_eoi(unsigned irq)
235{
236 struct irq_info *info = info_for_irq(irq);
237
238 BUG_ON(info->type != IRQT_PIRQ);
239
240 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
241}
242
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700243static inline unsigned long active_evtchns(unsigned int cpu,
244 struct shared_info *sh,
245 unsigned int idx)
246{
247 return (sh->evtchn_pending[idx] &
Mike Travisc7a35892009-01-10 21:58:11 -0800248 cpu_evtchn_mask(cpu)[idx] &
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700249 ~sh->evtchn_mask[idx]);
250}
251
252static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
253{
254 int irq = evtchn_to_irq[chn];
255
256 BUG_ON(irq == -1);
257#ifdef CONFIG_SMP
Mike Travis7f7ace02009-01-10 21:58:08 -0800258 cpumask_copy(irq_to_desc(irq)->affinity, cpumask_of(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700259#endif
260
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800261 __clear_bit(chn, cpu_evtchn_mask(cpu_from_irq(irq)));
Mike Travisc7a35892009-01-10 21:58:11 -0800262 __set_bit(chn, cpu_evtchn_mask(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700263
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800264 irq_info[irq].cpu = cpu;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700265}
266
267static void init_evtchn_cpu_bindings(void)
268{
269#ifdef CONFIG_SMP
Thomas Gleixner10e58082008-10-16 14:19:04 +0200270 struct irq_desc *desc;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700271 int i;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200272
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700273 /* By default all event channels notify CPU#0. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800274 for_each_irq_desc(i, desc) {
Mike Travis7f7ace02009-01-10 21:58:08 -0800275 cpumask_copy(desc->affinity, cpumask_of(0));
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800276 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700277#endif
278
Mike Travisc7a35892009-01-10 21:58:11 -0800279 memset(cpu_evtchn_mask(0), ~0, sizeof(cpu_evtchn_mask(0)));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700280}
281
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700282static inline void clear_evtchn(int port)
283{
284 struct shared_info *s = HYPERVISOR_shared_info;
285 sync_clear_bit(port, &s->evtchn_pending[0]);
286}
287
288static inline void set_evtchn(int port)
289{
290 struct shared_info *s = HYPERVISOR_shared_info;
291 sync_set_bit(port, &s->evtchn_pending[0]);
292}
293
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700294static inline int test_evtchn(int port)
295{
296 struct shared_info *s = HYPERVISOR_shared_info;
297 return sync_test_bit(port, &s->evtchn_pending[0]);
298}
299
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700300
301/**
302 * notify_remote_via_irq - send event to remote end of event channel via irq
303 * @irq: irq of event channel to send event to
304 *
305 * Unlike notify_remote_via_evtchn(), this is safe to use across
306 * save/restore. Notifications on a broken connection are silently
307 * dropped.
308 */
309void notify_remote_via_irq(int irq)
310{
311 int evtchn = evtchn_from_irq(irq);
312
313 if (VALID_EVTCHN(evtchn))
314 notify_remote_via_evtchn(evtchn);
315}
316EXPORT_SYMBOL_GPL(notify_remote_via_irq);
317
318static void mask_evtchn(int port)
319{
320 struct shared_info *s = HYPERVISOR_shared_info;
321 sync_set_bit(port, &s->evtchn_mask[0]);
322}
323
324static void unmask_evtchn(int port)
325{
326 struct shared_info *s = HYPERVISOR_shared_info;
327 unsigned int cpu = get_cpu();
328
329 BUG_ON(!irqs_disabled());
330
331 /* Slow path (hypercall) if this is a non-local port. */
332 if (unlikely(cpu != cpu_from_evtchn(port))) {
333 struct evtchn_unmask unmask = { .port = port };
334 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
335 } else {
336 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
337
338 sync_clear_bit(port, &s->evtchn_mask[0]);
339
340 /*
341 * The following is basically the equivalent of
342 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
343 * the interrupt edge' if the channel is masked.
344 */
345 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
346 !sync_test_and_set_bit(port / BITS_PER_LONG,
347 &vcpu_info->evtchn_pending_sel))
348 vcpu_info->evtchn_upcall_pending = 1;
349 }
350
351 put_cpu();
352}
353
354static int find_unbound_irq(void)
355{
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200356 struct irq_data *data;
357 int irq, res;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700358
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100359 for (irq = 0; irq < nr_irqs; irq++) {
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200360 data = irq_get_irq_data(irq);
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100361 /* only 0->15 have init'd desc; handle irq > 16 */
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200362 if (!data)
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100363 break;
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200364 if (data->chip == &no_irq_chip)
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100365 break;
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200366 if (data->chip != &xen_dynamic_chip)
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100367 continue;
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800368 if (irq_info[irq].type == IRQT_UNBOUND)
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200369 return irq;
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100370 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700371
Yinghai Lu5a15d7e2008-08-19 20:49:57 -0700372 if (irq == nr_irqs)
373 panic("No available IRQ to bind to: increase nr_irqs!\n");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700374
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200375 res = irq_alloc_desc_at(irq, 0);
Jeremy Fitzhardinge6f8a0ed2008-12-17 13:42:29 -0800376
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200377 if (WARN_ON(res != irq))
378 return -1;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800379
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700380 return irq;
381}
382
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400383static bool identity_mapped_irq(unsigned irq)
384{
385 /* only identity map legacy irqs */
386 return irq < LEGACY_IRQS;
387}
388
389static void pirq_unmask_notify(int irq)
390{
391 struct physdev_eoi eoi = { .irq = irq };
392
393 if (unlikely(pirq_needs_eoi(irq))) {
394 int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
395 WARN_ON(rc);
396 }
397}
398
399static void pirq_query_unmask(int irq)
400{
401 struct physdev_irq_status_query irq_status;
402 struct irq_info *info = info_for_irq(irq);
403
404 BUG_ON(info->type != IRQT_PIRQ);
405
406 irq_status.irq = irq;
407 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
408 irq_status.flags = 0;
409
410 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
411 if (irq_status.flags & XENIRQSTAT_needs_eoi)
412 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
413}
414
415static bool probing_irq(int irq)
416{
417 struct irq_desc *desc = irq_to_desc(irq);
418
419 return desc && desc->action == NULL;
420}
421
422static unsigned int startup_pirq(unsigned int irq)
423{
424 struct evtchn_bind_pirq bind_pirq;
425 struct irq_info *info = info_for_irq(irq);
426 int evtchn = evtchn_from_irq(irq);
427
428 BUG_ON(info->type != IRQT_PIRQ);
429
430 if (VALID_EVTCHN(evtchn))
431 goto out;
432
433 bind_pirq.pirq = irq;
434 /* NB. We are happy to share unless we are probing. */
435 bind_pirq.flags = probing_irq(irq) ? 0 : BIND_PIRQ__WILL_SHARE;
436 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq) != 0) {
437 if (!probing_irq(irq))
438 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
439 irq);
440 return 0;
441 }
442 evtchn = bind_pirq.port;
443
444 pirq_query_unmask(irq);
445
446 evtchn_to_irq[evtchn] = irq;
447 bind_evtchn_to_cpu(evtchn, 0);
448 info->evtchn = evtchn;
449
450out:
451 unmask_evtchn(evtchn);
452 pirq_unmask_notify(irq);
453
454 return 0;
455}
456
457static void shutdown_pirq(unsigned int irq)
458{
459 struct evtchn_close close;
460 struct irq_info *info = info_for_irq(irq);
461 int evtchn = evtchn_from_irq(irq);
462
463 BUG_ON(info->type != IRQT_PIRQ);
464
465 if (!VALID_EVTCHN(evtchn))
466 return;
467
468 mask_evtchn(evtchn);
469
470 close.port = evtchn;
471 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
472 BUG();
473
474 bind_evtchn_to_cpu(evtchn, 0);
475 evtchn_to_irq[evtchn] = -1;
476 info->evtchn = 0;
477}
478
479static void enable_pirq(unsigned int irq)
480{
481 startup_pirq(irq);
482}
483
484static void disable_pirq(unsigned int irq)
485{
486}
487
488static void ack_pirq(unsigned int irq)
489{
490 int evtchn = evtchn_from_irq(irq);
491
492 move_native_irq(irq);
493
494 if (VALID_EVTCHN(evtchn)) {
495 mask_evtchn(evtchn);
496 clear_evtchn(evtchn);
497 }
498}
499
500static void end_pirq(unsigned int irq)
501{
502 int evtchn = evtchn_from_irq(irq);
503 struct irq_desc *desc = irq_to_desc(irq);
504
505 if (WARN_ON(!desc))
506 return;
507
508 if ((desc->status & (IRQ_DISABLED|IRQ_PENDING)) ==
509 (IRQ_DISABLED|IRQ_PENDING)) {
510 shutdown_pirq(irq);
511 } else if (VALID_EVTCHN(evtchn)) {
512 unmask_evtchn(evtchn);
513 pirq_unmask_notify(irq);
514 }
515}
516
517static int find_irq_by_gsi(unsigned gsi)
518{
519 int irq;
520
521 for (irq = 0; irq < NR_IRQS; irq++) {
522 struct irq_info *info = info_for_irq(irq);
523
524 if (info == NULL || info->type != IRQT_PIRQ)
525 continue;
526
527 if (gsi_from_irq(irq) == gsi)
528 return irq;
529 }
530
531 return -1;
532}
533
534/*
535 * Allocate a physical irq, along with a vector. We don't assign an
536 * event channel until the irq actually started up. Return an
537 * existing irq if we've already got one for the gsi.
538 */
539int xen_allocate_pirq(unsigned gsi)
540{
541 int irq;
542 struct physdev_irq irq_op;
543
544 spin_lock(&irq_mapping_update_lock);
545
546 irq = find_irq_by_gsi(gsi);
547 if (irq != -1) {
548 printk(KERN_INFO "xen_allocate_pirq: returning irq %d for gsi %u\n",
549 irq, gsi);
550 goto out; /* XXX need refcount? */
551 }
552
553 if (identity_mapped_irq(gsi)) {
554 irq = gsi;
555 dynamic_irq_init(irq);
556 } else
557 irq = find_unbound_irq();
558
559 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
560 handle_level_irq, "pirq");
561
562 irq_op.irq = irq;
563 if (HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
564 dynamic_irq_cleanup(irq);
565 irq = -ENOSPC;
566 goto out;
567 }
568
569 irq_info[irq] = mk_pirq_info(0, gsi, irq_op.vector);
570
571out:
572 spin_unlock(&irq_mapping_update_lock);
573
574 return irq;
575}
576
577int xen_vector_from_irq(unsigned irq)
578{
579 return vector_from_irq(irq);
580}
581
582int xen_gsi_from_irq(unsigned irq)
583{
584 return gsi_from_irq(irq);
585}
586
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700587int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700588{
589 int irq;
590
591 spin_lock(&irq_mapping_update_lock);
592
593 irq = evtchn_to_irq[evtchn];
594
595 if (irq == -1) {
596 irq = find_unbound_irq();
597
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700598 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
Jeremy Fitzhardingedffe2e12010-08-20 19:10:01 -0700599 handle_edge_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700600
601 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800602 irq_info[irq] = mk_evtchn_info(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700603 }
604
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700605 spin_unlock(&irq_mapping_update_lock);
606
607 return irq;
608}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700609EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700610
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700611static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
612{
613 struct evtchn_bind_ipi bind_ipi;
614 int evtchn, irq;
615
616 spin_lock(&irq_mapping_update_lock);
617
618 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800619
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700620 if (irq == -1) {
621 irq = find_unbound_irq();
622 if (irq < 0)
623 goto out;
624
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700625 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
626 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700627
628 bind_ipi.vcpu = cpu;
629 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
630 &bind_ipi) != 0)
631 BUG();
632 evtchn = bind_ipi.port;
633
634 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800635 irq_info[irq] = mk_ipi_info(evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700636 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
637
638 bind_evtchn_to_cpu(evtchn, cpu);
639 }
640
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700641 out:
642 spin_unlock(&irq_mapping_update_lock);
643 return irq;
644}
645
646
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700647static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
648{
649 struct evtchn_bind_virq bind_virq;
650 int evtchn, irq;
651
652 spin_lock(&irq_mapping_update_lock);
653
654 irq = per_cpu(virq_to_irq, cpu)[virq];
655
656 if (irq == -1) {
657 bind_virq.virq = virq;
658 bind_virq.vcpu = cpu;
659 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
660 &bind_virq) != 0)
661 BUG();
662 evtchn = bind_virq.port;
663
664 irq = find_unbound_irq();
665
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700666 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
667 handle_percpu_irq, "virq");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700668
669 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800670 irq_info[irq] = mk_virq_info(evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700671
672 per_cpu(virq_to_irq, cpu)[virq] = irq;
673
674 bind_evtchn_to_cpu(evtchn, cpu);
675 }
676
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700677 spin_unlock(&irq_mapping_update_lock);
678
679 return irq;
680}
681
682static void unbind_from_irq(unsigned int irq)
683{
684 struct evtchn_close close;
685 int evtchn = evtchn_from_irq(irq);
686
687 spin_lock(&irq_mapping_update_lock);
688
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800689 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700690 close.port = evtchn;
691 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
692 BUG();
693
694 switch (type_from_irq(irq)) {
695 case IRQT_VIRQ:
696 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800697 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700698 break;
Alex Nixond68d82a2008-08-22 11:52:15 +0100699 case IRQT_IPI:
700 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800701 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +0100702 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700703 default:
704 break;
705 }
706
707 /* Closed ports are implicitly re-bound to VCPU0. */
708 bind_evtchn_to_cpu(evtchn, 0);
709
710 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +0000711 }
712
713 if (irq_info[irq].type != IRQT_UNBOUND) {
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800714 irq_info[irq] = mk_unbound_info();
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700715
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200716 irq_free_desc(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700717 }
718
719 spin_unlock(&irq_mapping_update_lock);
720}
721
722int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -0400723 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700724 unsigned long irqflags,
725 const char *devname, void *dev_id)
726{
727 unsigned int irq;
728 int retval;
729
730 irq = bind_evtchn_to_irq(evtchn);
731 retval = request_irq(irq, handler, irqflags, devname, dev_id);
732 if (retval != 0) {
733 unbind_from_irq(irq);
734 return retval;
735 }
736
737 return irq;
738}
739EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
740
741int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -0400742 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700743 unsigned long irqflags, const char *devname, void *dev_id)
744{
745 unsigned int irq;
746 int retval;
747
748 irq = bind_virq_to_irq(virq, cpu);
749 retval = request_irq(irq, handler, irqflags, devname, dev_id);
750 if (retval != 0) {
751 unbind_from_irq(irq);
752 return retval;
753 }
754
755 return irq;
756}
757EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
758
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700759int bind_ipi_to_irqhandler(enum ipi_vector ipi,
760 unsigned int cpu,
761 irq_handler_t handler,
762 unsigned long irqflags,
763 const char *devname,
764 void *dev_id)
765{
766 int irq, retval;
767
768 irq = bind_ipi_to_irq(ipi, cpu);
769 if (irq < 0)
770 return irq;
771
Ian Campbell4877c732010-07-29 11:16:35 +0100772 irqflags |= IRQF_NO_SUSPEND;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700773 retval = request_irq(irq, handler, irqflags, devname, dev_id);
774 if (retval != 0) {
775 unbind_from_irq(irq);
776 return retval;
777 }
778
779 return irq;
780}
781
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700782void unbind_from_irqhandler(unsigned int irq, void *dev_id)
783{
784 free_irq(irq, dev_id);
785 unbind_from_irq(irq);
786}
787EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
788
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700789void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
790{
791 int irq = per_cpu(ipi_to_irq, cpu)[vector];
792 BUG_ON(irq < 0);
793 notify_remote_via_irq(irq);
794}
795
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700796irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
797{
798 struct shared_info *sh = HYPERVISOR_shared_info;
799 int cpu = smp_processor_id();
800 int i;
801 unsigned long flags;
802 static DEFINE_SPINLOCK(debug_lock);
803
804 spin_lock_irqsave(&debug_lock, flags);
805
806 printk("vcpu %d\n ", cpu);
807
808 for_each_online_cpu(i) {
809 struct vcpu_info *v = per_cpu(xen_vcpu, i);
810 printk("%d: masked=%d pending=%d event_sel %08lx\n ", i,
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700811 (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask,
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700812 v->evtchn_upcall_pending,
813 v->evtchn_pending_sel);
814 }
815 printk("pending:\n ");
816 for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
817 printk("%08lx%s", sh->evtchn_pending[i],
818 i % 8 == 0 ? "\n " : " ");
819 printk("\nmasks:\n ");
820 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
821 printk("%08lx%s", sh->evtchn_mask[i],
822 i % 8 == 0 ? "\n " : " ");
823
824 printk("\nunmasked:\n ");
825 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
826 printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
827 i % 8 == 0 ? "\n " : " ");
828
829 printk("\npending list:\n");
830 for(i = 0; i < NR_EVENT_CHANNELS; i++) {
831 if (sync_test_bit(i, sh->evtchn_pending)) {
832 printk(" %d: event %d -> irq %d\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800833 cpu_from_evtchn(i), i,
834 evtchn_to_irq[i]);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700835 }
836 }
837
838 spin_unlock_irqrestore(&debug_lock, flags);
839
840 return IRQ_HANDLED;
841}
842
Tejun Heo245b2e72009-06-24 15:13:48 +0900843static DEFINE_PER_CPU(unsigned, xed_nesting_count);
844
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700845/*
846 * Search the CPUs pending events bitmasks. For each one found, map
847 * the event number to an irq, and feed it into do_IRQ() for
848 * handling.
849 *
850 * Xen uses a two-level bitmap to speed searching. The first level is
851 * a bitset of words which contain pending event bits. The second
852 * level is a bitset of pending events themselves.
853 */
Sheng Yang38e20b02010-05-14 12:40:51 +0100854static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700855{
856 int cpu = get_cpu();
857 struct shared_info *s = HYPERVISOR_shared_info;
858 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700859 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700860
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700861 do {
862 unsigned long pending_words;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700863
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700864 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700865
Tejun Heo245b2e72009-06-24 15:13:48 +0900866 if (__get_cpu_var(xed_nesting_count)++)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700867 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700868
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700869#ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
870 /* Clear master flag /before/ clearing selector flag. */
Isaku Yamahata6673cf62008-06-16 14:58:13 -0700871 wmb();
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700872#endif
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700873 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
874 while (pending_words != 0) {
875 unsigned long pending_bits;
876 int word_idx = __ffs(pending_words);
877 pending_words &= ~(1UL << word_idx);
878
879 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
880 int bit_idx = __ffs(pending_bits);
881 int port = (word_idx * BITS_PER_LONG) + bit_idx;
882 int irq = evtchn_to_irq[port];
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -0800883 struct irq_desc *desc;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700884
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -0800885 if (irq != -1) {
886 desc = irq_to_desc(irq);
887 if (desc)
888 generic_handle_irq_desc(irq, desc);
889 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700890 }
891 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700892
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700893 BUG_ON(!irqs_disabled());
894
Tejun Heo245b2e72009-06-24 15:13:48 +0900895 count = __get_cpu_var(xed_nesting_count);
896 __get_cpu_var(xed_nesting_count) = 0;
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100897 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700898
899out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -0800900
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700901 put_cpu();
902}
903
Sheng Yang38e20b02010-05-14 12:40:51 +0100904void xen_evtchn_do_upcall(struct pt_regs *regs)
905{
906 struct pt_regs *old_regs = set_irq_regs(regs);
907
908 exit_idle();
909 irq_enter();
910
911 __xen_evtchn_do_upcall();
912
913 irq_exit();
914 set_irq_regs(old_regs);
915}
916
917void xen_hvm_evtchn_do_upcall(void)
918{
919 __xen_evtchn_do_upcall();
920}
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100921EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +0100922
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100923/* Rebind a new event channel to an existing irq. */
924void rebind_evtchn_irq(int evtchn, int irq)
925{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800926 struct irq_info *info = info_for_irq(irq);
927
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100928 /* Make sure the irq is masked, since the new event channel
929 will also be masked. */
930 disable_irq(irq);
931
932 spin_lock(&irq_mapping_update_lock);
933
934 /* After resume the irq<->evtchn mappings are all cleared out */
935 BUG_ON(evtchn_to_irq[evtchn] != -1);
936 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800937 so there should be a proper type */
938 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100939
940 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800941 irq_info[irq] = mk_evtchn_info(evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100942
943 spin_unlock(&irq_mapping_update_lock);
944
945 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +1030946 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100947
948 /* Unmask the event channel. */
949 enable_irq(irq);
950}
951
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700952/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -0700953static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700954{
955 struct evtchn_bind_vcpu bind_vcpu;
956 int evtchn = evtchn_from_irq(irq);
957
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100958 /* events delivered via platform PCI interrupts are always
959 * routed to vcpu 0 */
960 if (!VALID_EVTCHN(evtchn) ||
961 (xen_hvm_domain() && !xen_have_vector_callback))
Yinghai Lud5dedd42009-04-27 17:59:21 -0700962 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700963
964 /* Send future instances of this interrupt to other vcpu. */
965 bind_vcpu.port = evtchn;
966 bind_vcpu.vcpu = tcpu;
967
968 /*
969 * If this fails, it usually just indicates that we're dealing with a
970 * virq or IPI channel, which don't actually need to be rebound. Ignore
971 * it, but don't do the xenlinux-level rebind in that case.
972 */
973 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
974 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700975
976 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700977}
978
Yinghai Lud5dedd42009-04-27 17:59:21 -0700979static int set_affinity_irq(unsigned irq, const struct cpumask *dest)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700980{
Rusty Russell0de26522008-12-13 21:20:26 +1030981 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700982
983 return rebind_irq_to_cpu(irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700984}
985
Isaku Yamahata642e0c82008-04-02 10:53:57 -0700986int resend_irq_on_evtchn(unsigned int irq)
987{
988 int masked, evtchn = evtchn_from_irq(irq);
989 struct shared_info *s = HYPERVISOR_shared_info;
990
991 if (!VALID_EVTCHN(evtchn))
992 return 1;
993
994 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
995 sync_set_bit(evtchn, s->evtchn_pending);
996 if (!masked)
997 unmask_evtchn(evtchn);
998
999 return 1;
1000}
1001
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001002static void enable_dynirq(unsigned int irq)
1003{
1004 int evtchn = evtchn_from_irq(irq);
1005
1006 if (VALID_EVTCHN(evtchn))
1007 unmask_evtchn(evtchn);
1008}
1009
1010static void disable_dynirq(unsigned int irq)
1011{
1012 int evtchn = evtchn_from_irq(irq);
1013
1014 if (VALID_EVTCHN(evtchn))
1015 mask_evtchn(evtchn);
1016}
1017
1018static void ack_dynirq(unsigned int irq)
1019{
1020 int evtchn = evtchn_from_irq(irq);
1021
1022 move_native_irq(irq);
1023
1024 if (VALID_EVTCHN(evtchn))
1025 clear_evtchn(evtchn);
1026}
1027
1028static int retrigger_dynirq(unsigned int irq)
1029{
1030 int evtchn = evtchn_from_irq(irq);
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001031 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001032 int ret = 0;
1033
1034 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001035 int masked;
1036
1037 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1038 sync_set_bit(evtchn, sh->evtchn_pending);
1039 if (!masked)
1040 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001041 ret = 1;
1042 }
1043
1044 return ret;
1045}
1046
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001047static void restore_cpu_virqs(unsigned int cpu)
1048{
1049 struct evtchn_bind_virq bind_virq;
1050 int virq, irq, evtchn;
1051
1052 for (virq = 0; virq < NR_VIRQS; virq++) {
1053 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1054 continue;
1055
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001056 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001057
1058 /* Get a new binding from Xen. */
1059 bind_virq.virq = virq;
1060 bind_virq.vcpu = cpu;
1061 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1062 &bind_virq) != 0)
1063 BUG();
1064 evtchn = bind_virq.port;
1065
1066 /* Record the new mapping. */
1067 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001068 irq_info[irq] = mk_virq_info(evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001069 bind_evtchn_to_cpu(evtchn, cpu);
1070
1071 /* Ready for use. */
1072 unmask_evtchn(evtchn);
1073 }
1074}
1075
1076static void restore_cpu_ipis(unsigned int cpu)
1077{
1078 struct evtchn_bind_ipi bind_ipi;
1079 int ipi, irq, evtchn;
1080
1081 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1082 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1083 continue;
1084
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001085 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001086
1087 /* Get a new binding from Xen. */
1088 bind_ipi.vcpu = cpu;
1089 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1090 &bind_ipi) != 0)
1091 BUG();
1092 evtchn = bind_ipi.port;
1093
1094 /* Record the new mapping. */
1095 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001096 irq_info[irq] = mk_ipi_info(evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001097 bind_evtchn_to_cpu(evtchn, cpu);
1098
1099 /* Ready for use. */
1100 unmask_evtchn(evtchn);
1101
1102 }
1103}
1104
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001105/* Clear an irq's pending state, in preparation for polling on it */
1106void xen_clear_irq_pending(int irq)
1107{
1108 int evtchn = evtchn_from_irq(irq);
1109
1110 if (VALID_EVTCHN(evtchn))
1111 clear_evtchn(evtchn);
1112}
1113
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001114void xen_set_irq_pending(int irq)
1115{
1116 int evtchn = evtchn_from_irq(irq);
1117
1118 if (VALID_EVTCHN(evtchn))
1119 set_evtchn(evtchn);
1120}
1121
1122bool xen_test_irq_pending(int irq)
1123{
1124 int evtchn = evtchn_from_irq(irq);
1125 bool ret = false;
1126
1127 if (VALID_EVTCHN(evtchn))
1128 ret = test_evtchn(evtchn);
1129
1130 return ret;
1131}
1132
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001133/* Poll waiting for an irq to become pending. In the usual case, the
1134 irq will be disabled so it won't deliver an interrupt. */
1135void xen_poll_irq(int irq)
1136{
1137 evtchn_port_t evtchn = evtchn_from_irq(irq);
1138
1139 if (VALID_EVTCHN(evtchn)) {
1140 struct sched_poll poll;
1141
1142 poll.nr_ports = 1;
1143 poll.timeout = 0;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001144 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001145
1146 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1147 BUG();
1148 }
1149}
1150
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001151void xen_irq_resume(void)
1152{
1153 unsigned int cpu, irq, evtchn;
1154
1155 init_evtchn_cpu_bindings();
1156
1157 /* New event-channel space is not 'live' yet. */
1158 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1159 mask_evtchn(evtchn);
1160
1161 /* No IRQ <-> event-channel mappings. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001162 for (irq = 0; irq < nr_irqs; irq++)
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001163 irq_info[irq].evtchn = 0; /* zap event-channel binding */
1164
1165 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1166 evtchn_to_irq[evtchn] = -1;
1167
1168 for_each_possible_cpu(cpu) {
1169 restore_cpu_virqs(cpu);
1170 restore_cpu_ipis(cpu);
1171 }
1172}
1173
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001174static struct irq_chip xen_dynamic_chip __read_mostly = {
1175 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001176
1177 .disable = disable_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001178 .mask = disable_dynirq,
1179 .unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001180
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001181 .ack = ack_dynirq,
1182 .set_affinity = set_affinity_irq,
1183 .retrigger = retrigger_dynirq,
1184};
1185
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001186static struct irq_chip xen_pirq_chip __read_mostly = {
1187 .name = "xen-pirq",
1188
1189 .startup = startup_pirq,
1190 .shutdown = shutdown_pirq,
1191
1192 .enable = enable_pirq,
1193 .unmask = enable_pirq,
1194
1195 .disable = disable_pirq,
1196 .mask = disable_pirq,
1197
1198 .ack = ack_pirq,
1199 .end = end_pirq,
1200
1201 .set_affinity = set_affinity_irq,
1202
1203 .retrigger = retrigger_dynirq,
1204};
1205
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001206static struct irq_chip xen_percpu_chip __read_mostly = {
1207 .name = "xen-percpu",
1208
1209 .disable = disable_dynirq,
1210 .mask = disable_dynirq,
1211 .unmask = enable_dynirq,
1212
1213 .ack = ack_dynirq,
1214};
1215
Sheng Yang38e20b02010-05-14 12:40:51 +01001216int xen_set_callback_via(uint64_t via)
1217{
1218 struct xen_hvm_param a;
1219 a.domid = DOMID_SELF;
1220 a.index = HVM_PARAM_CALLBACK_IRQ;
1221 a.value = via;
1222 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1223}
1224EXPORT_SYMBOL_GPL(xen_set_callback_via);
1225
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001226#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001227/* Vector callbacks are better than PCI interrupts to receive event
1228 * channel notifications because we can receive vector callbacks on any
1229 * vcpu and we don't need PCI support or APIC interactions. */
1230void xen_callback_vector(void)
1231{
1232 int rc;
1233 uint64_t callback_via;
1234 if (xen_have_vector_callback) {
1235 callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
1236 rc = xen_set_callback_via(callback_via);
1237 if (rc) {
1238 printk(KERN_ERR "Request for Xen HVM callback vector"
1239 " failed.\n");
1240 xen_have_vector_callback = 0;
1241 return;
1242 }
1243 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1244 "enabled\n");
1245 /* in the restore case the vector has already been allocated */
1246 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
1247 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
1248 }
1249}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001250#else
1251void xen_callback_vector(void) {}
1252#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001253
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001254void __init xen_init_IRQ(void)
1255{
1256 int i;
Mike Travisc7a35892009-01-10 21:58:11 -08001257
Pekka Enberga70c3522009-07-01 11:51:18 +03001258 cpu_evtchn_mask_p = kcalloc(nr_cpu_ids, sizeof(struct cpu_evtchn_s),
1259 GFP_KERNEL);
Christophe Saout28e08862009-01-11 11:46:23 -08001260 BUG_ON(cpu_evtchn_mask_p == NULL);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001261
1262 init_evtchn_cpu_bindings();
1263
1264 /* No event channels are 'live' right now. */
1265 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1266 mask_evtchn(i);
1267
Sheng Yang38e20b02010-05-14 12:40:51 +01001268 if (xen_hvm_domain()) {
1269 xen_callback_vector();
1270 native_init_IRQ();
1271 } else {
1272 irq_ctx_init(smp_processor_id());
1273 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001274}