GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / xen / events / events_base.c
1 /*
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 received, 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.
19  * 4. PIRQs - Hardware interrupts.
20  *
21  * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
22  */
23
24 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
25
26 #include <linux/linkage.h>
27 #include <linux/interrupt.h>
28 #include <linux/irq.h>
29 #include <linux/moduleparam.h>
30 #include <linux/string.h>
31 #include <linux/bootmem.h>
32 #include <linux/slab.h>
33 #include <linux/irqnr.h>
34 #include <linux/pci.h>
35 #include <linux/spinlock.h>
36 #include <linux/cpuhotplug.h>
37 #include <linux/atomic.h>
38 #include <linux/ktime.h>
39
40 #ifdef CONFIG_X86
41 #include <asm/desc.h>
42 #include <asm/ptrace.h>
43 #include <asm/irq.h>
44 #include <asm/io_apic.h>
45 #include <asm/i8259.h>
46 #include <asm/xen/pci.h>
47 #endif
48 #include <asm/sync_bitops.h>
49 #include <asm/xen/hypercall.h>
50 #include <asm/xen/hypervisor.h>
51 #include <xen/page.h>
52
53 #include <xen/xen.h>
54 #include <xen/hvm.h>
55 #include <xen/xen-ops.h>
56 #include <xen/events.h>
57 #include <xen/interface/xen.h>
58 #include <xen/interface/event_channel.h>
59 #include <xen/interface/hvm/hvm_op.h>
60 #include <xen/interface/hvm/params.h>
61 #include <xen/interface/physdev.h>
62 #include <xen/interface/sched.h>
63 #include <xen/interface/vcpu.h>
64 #include <asm/hw_irq.h>
65
66 #include "events_internal.h"
67
68 #undef MODULE_PARAM_PREFIX
69 #define MODULE_PARAM_PREFIX "xen."
70
71 static uint __read_mostly event_loop_timeout = 2;
72 module_param(event_loop_timeout, uint, 0644);
73
74 static uint __read_mostly event_eoi_delay = 10;
75 module_param(event_eoi_delay, uint, 0644);
76
77 const struct evtchn_ops *evtchn_ops;
78
79 /*
80  * This lock protects updates to the following mapping and reference-count
81  * arrays. The lock does not need to be acquired to read the mapping tables.
82  */
83 static DEFINE_MUTEX(irq_mapping_update_lock);
84
85 /*
86  * Lock hierarchy:
87  *
88  * irq_mapping_update_lock
89  *   IRQ-desc lock
90  *     percpu eoi_list_lock
91  *       irq_info->lock
92  */
93
94 static LIST_HEAD(xen_irq_list_head);
95
96 /* IRQ <-> VIRQ mapping. */
97 static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
98
99 /* IRQ <-> IPI mapping */
100 static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
101
102 int **evtchn_to_irq;
103 #ifdef CONFIG_X86
104 static unsigned long *pirq_eoi_map;
105 #endif
106 static bool (*pirq_needs_eoi)(unsigned irq);
107
108 #define EVTCHN_ROW(e)  (e / (PAGE_SIZE/sizeof(**evtchn_to_irq)))
109 #define EVTCHN_COL(e)  (e % (PAGE_SIZE/sizeof(**evtchn_to_irq)))
110 #define EVTCHN_PER_ROW (PAGE_SIZE / sizeof(**evtchn_to_irq))
111
112 /* Xen will never allocate port zero for any purpose. */
113 #define VALID_EVTCHN(chn)       ((chn) != 0)
114
115 static struct irq_info *legacy_info_ptrs[NR_IRQS_LEGACY];
116
117 static struct irq_chip xen_dynamic_chip;
118 static struct irq_chip xen_lateeoi_chip;
119 static struct irq_chip xen_percpu_chip;
120 static struct irq_chip xen_pirq_chip;
121 static void enable_dynirq(struct irq_data *data);
122 static void disable_dynirq(struct irq_data *data);
123
124 static DEFINE_PER_CPU(unsigned int, irq_epoch);
125
126 static void clear_evtchn_to_irq_row(int *evtchn_row)
127 {
128         unsigned col;
129
130         for (col = 0; col < EVTCHN_PER_ROW; col++)
131                 WRITE_ONCE(evtchn_row[col], -1);
132 }
133
134 static void clear_evtchn_to_irq_all(void)
135 {
136         unsigned row;
137
138         for (row = 0; row < EVTCHN_ROW(xen_evtchn_max_channels()); row++) {
139                 if (evtchn_to_irq[row] == NULL)
140                         continue;
141                 clear_evtchn_to_irq_row(evtchn_to_irq[row]);
142         }
143 }
144
145 static int set_evtchn_to_irq(unsigned evtchn, unsigned irq)
146 {
147         unsigned row;
148         unsigned col;
149         int *evtchn_row;
150
151         if (evtchn >= xen_evtchn_max_channels())
152                 return -EINVAL;
153
154         row = EVTCHN_ROW(evtchn);
155         col = EVTCHN_COL(evtchn);
156
157         if (evtchn_to_irq[row] == NULL) {
158                 /* Unallocated irq entries return -1 anyway */
159                 if (irq == -1)
160                         return 0;
161
162                 evtchn_row = (int *) __get_free_pages(GFP_KERNEL, 0);
163                 if (evtchn_row == NULL)
164                         return -ENOMEM;
165
166                 clear_evtchn_to_irq_row(evtchn_row);
167
168                 /*
169                  * We've prepared an empty row for the mapping. If a different
170                  * thread was faster inserting it, we can drop ours.
171                  */
172                 if (cmpxchg(&evtchn_to_irq[row], NULL, evtchn_row) != NULL)
173                         free_page((unsigned long) evtchn_row);
174         }
175
176         WRITE_ONCE(evtchn_to_irq[row][col], irq);
177         return 0;
178 }
179
180 int get_evtchn_to_irq(unsigned evtchn)
181 {
182         if (evtchn >= xen_evtchn_max_channels())
183                 return -1;
184         if (evtchn_to_irq[EVTCHN_ROW(evtchn)] == NULL)
185                 return -1;
186         return READ_ONCE(evtchn_to_irq[EVTCHN_ROW(evtchn)][EVTCHN_COL(evtchn)]);
187 }
188
189 /* Get info for IRQ */
190 struct irq_info *info_for_irq(unsigned irq)
191 {
192         if (irq < nr_legacy_irqs())
193                 return legacy_info_ptrs[irq];
194         else
195                 return irq_get_chip_data(irq);
196 }
197
198 static void set_info_for_irq(unsigned int irq, struct irq_info *info)
199 {
200         if (irq < nr_legacy_irqs())
201                 legacy_info_ptrs[irq] = info;
202         else
203                 irq_set_chip_data(irq, info);
204 }
205
206 static void delayed_free_irq(struct work_struct *work)
207 {
208         struct irq_info *info = container_of(work, struct irq_info,
209                                              work);
210         unsigned int irq = info->irq;
211
212         /* Remove the info pointer only now, with no potential users left. */
213         set_info_for_irq(irq, NULL);
214
215         kfree(info);
216
217         /* Legacy IRQ descriptors are managed by the arch. */
218         if (irq >= nr_legacy_irqs())
219                 irq_free_desc(irq);
220 }
221
222 static void rcu_free_irq(struct rcu_head *rcu_head)
223 {
224         struct irq_info *info = container_of(rcu_head, struct irq_info, rcu);
225
226         queue_work(system_wq, &info->work);
227 }
228
229 /* Constructors for packed IRQ information. */
230 static int xen_irq_info_common_setup(struct irq_info *info,
231                                      unsigned irq,
232                                      enum xen_irq_type type,
233                                      unsigned evtchn,
234                                      unsigned short cpu)
235 {
236         int ret;
237
238         BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
239
240         info->type = type;
241         info->irq = irq;
242         info->evtchn = evtchn;
243         info->cpu = cpu;
244         info->mask_reason = EVT_MASK_REASON_EXPLICIT;
245         raw_spin_lock_init(&info->lock);
246
247         ret = set_evtchn_to_irq(evtchn, irq);
248         if (ret < 0)
249                 return ret;
250
251         irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
252
253         return xen_evtchn_port_setup(info);
254 }
255
256 static int xen_irq_info_evtchn_setup(unsigned irq,
257                                      unsigned evtchn)
258 {
259         struct irq_info *info = info_for_irq(irq);
260
261         return xen_irq_info_common_setup(info, irq, IRQT_EVTCHN, evtchn, 0);
262 }
263
264 static int xen_irq_info_ipi_setup(unsigned cpu,
265                                   unsigned irq,
266                                   unsigned evtchn,
267                                   enum ipi_vector ipi)
268 {
269         struct irq_info *info = info_for_irq(irq);
270
271         info->u.ipi = ipi;
272
273         per_cpu(ipi_to_irq, cpu)[ipi] = irq;
274
275         return xen_irq_info_common_setup(info, irq, IRQT_IPI, evtchn, 0);
276 }
277
278 static int xen_irq_info_virq_setup(unsigned cpu,
279                                    unsigned irq,
280                                    unsigned evtchn,
281                                    unsigned virq)
282 {
283         struct irq_info *info = info_for_irq(irq);
284
285         info->u.virq = virq;
286
287         per_cpu(virq_to_irq, cpu)[virq] = irq;
288
289         return xen_irq_info_common_setup(info, irq, IRQT_VIRQ, evtchn, 0);
290 }
291
292 static int xen_irq_info_pirq_setup(unsigned irq,
293                                    unsigned evtchn,
294                                    unsigned pirq,
295                                    unsigned gsi,
296                                    uint16_t domid,
297                                    unsigned char flags)
298 {
299         struct irq_info *info = info_for_irq(irq);
300
301         info->u.pirq.pirq = pirq;
302         info->u.pirq.gsi = gsi;
303         info->u.pirq.domid = domid;
304         info->u.pirq.flags = flags;
305
306         return xen_irq_info_common_setup(info, irq, IRQT_PIRQ, evtchn, 0);
307 }
308
309 static void xen_irq_info_cleanup(struct irq_info *info)
310 {
311         set_evtchn_to_irq(info->evtchn, -1);
312         xen_evtchn_port_remove(info->evtchn, info->cpu);
313         info->evtchn = 0;
314 }
315
316 /*
317  * Accessors for packed IRQ information.
318  */
319 unsigned int evtchn_from_irq(unsigned irq)
320 {
321         const struct irq_info *info = NULL;
322
323         if (likely(irq < nr_irqs))
324                 info = info_for_irq(irq);
325         if (!info)
326                 return 0;
327
328         return info->evtchn;
329 }
330
331 unsigned irq_from_evtchn(unsigned int evtchn)
332 {
333         return get_evtchn_to_irq(evtchn);
334 }
335 EXPORT_SYMBOL_GPL(irq_from_evtchn);
336
337 int irq_from_virq(unsigned int cpu, unsigned int virq)
338 {
339         return per_cpu(virq_to_irq, cpu)[virq];
340 }
341
342 static enum ipi_vector ipi_from_irq(unsigned irq)
343 {
344         struct irq_info *info = info_for_irq(irq);
345
346         BUG_ON(info == NULL);
347         BUG_ON(info->type != IRQT_IPI);
348
349         return info->u.ipi;
350 }
351
352 static unsigned virq_from_irq(unsigned irq)
353 {
354         struct irq_info *info = info_for_irq(irq);
355
356         BUG_ON(info == NULL);
357         BUG_ON(info->type != IRQT_VIRQ);
358
359         return info->u.virq;
360 }
361
362 static unsigned pirq_from_irq(unsigned irq)
363 {
364         struct irq_info *info = info_for_irq(irq);
365
366         BUG_ON(info == NULL);
367         BUG_ON(info->type != IRQT_PIRQ);
368
369         return info->u.pirq.pirq;
370 }
371
372 static enum xen_irq_type type_from_irq(unsigned irq)
373 {
374         return info_for_irq(irq)->type;
375 }
376
377 unsigned cpu_from_irq(unsigned irq)
378 {
379         return info_for_irq(irq)->cpu;
380 }
381
382 unsigned int cpu_from_evtchn(unsigned int evtchn)
383 {
384         int irq = get_evtchn_to_irq(evtchn);
385         unsigned ret = 0;
386
387         if (irq != -1)
388                 ret = cpu_from_irq(irq);
389
390         return ret;
391 }
392
393 static void do_mask(struct irq_info *info, u8 reason)
394 {
395         unsigned long flags;
396
397         raw_spin_lock_irqsave(&info->lock, flags);
398
399         if (!info->mask_reason)
400                 mask_evtchn(info->evtchn);
401
402         info->mask_reason |= reason;
403
404         raw_spin_unlock_irqrestore(&info->lock, flags);
405 }
406
407 static void do_unmask(struct irq_info *info, u8 reason)
408 {
409         unsigned long flags;
410
411         raw_spin_lock_irqsave(&info->lock, flags);
412
413         info->mask_reason &= ~reason;
414
415         if (!info->mask_reason)
416                 unmask_evtchn(info->evtchn);
417
418         raw_spin_unlock_irqrestore(&info->lock, flags);
419 }
420
421 #ifdef CONFIG_X86
422 static bool pirq_check_eoi_map(unsigned irq)
423 {
424         return test_bit(pirq_from_irq(irq), pirq_eoi_map);
425 }
426 #endif
427
428 static bool pirq_needs_eoi_flag(unsigned irq)
429 {
430         struct irq_info *info = info_for_irq(irq);
431         BUG_ON(info->type != IRQT_PIRQ);
432
433         return info->u.pirq.flags & PIRQ_NEEDS_EOI;
434 }
435
436 static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
437 {
438         int irq = get_evtchn_to_irq(chn);
439         struct irq_info *info = info_for_irq(irq);
440
441         BUG_ON(irq == -1);
442 #ifdef CONFIG_SMP
443         cpumask_copy(irq_get_affinity_mask(irq), cpumask_of(cpu));
444 #endif
445         xen_evtchn_port_bind_to_cpu(info, cpu);
446
447         info->cpu = cpu;
448 }
449
450 /**
451  * notify_remote_via_irq - send event to remote end of event channel via irq
452  * @irq: irq of event channel to send event to
453  *
454  * Unlike notify_remote_via_evtchn(), this is safe to use across
455  * save/restore. Notifications on a broken connection are silently
456  * dropped.
457  */
458 void notify_remote_via_irq(int irq)
459 {
460         int evtchn = evtchn_from_irq(irq);
461
462         if (VALID_EVTCHN(evtchn))
463                 notify_remote_via_evtchn(evtchn);
464 }
465 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
466
467 struct lateeoi_work {
468         struct delayed_work delayed;
469         spinlock_t eoi_list_lock;
470         struct list_head eoi_list;
471 };
472
473 static DEFINE_PER_CPU(struct lateeoi_work, lateeoi);
474
475 static void lateeoi_list_del(struct irq_info *info)
476 {
477         struct lateeoi_work *eoi = &per_cpu(lateeoi, info->eoi_cpu);
478         unsigned long flags;
479
480         spin_lock_irqsave(&eoi->eoi_list_lock, flags);
481         list_del_init(&info->eoi_list);
482         spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
483 }
484
485 static void lateeoi_list_add(struct irq_info *info)
486 {
487         struct lateeoi_work *eoi = &per_cpu(lateeoi, info->eoi_cpu);
488         struct irq_info *elem;
489         u64 now = get_jiffies_64();
490         unsigned long delay;
491         unsigned long flags;
492
493         if (now < info->eoi_time)
494                 delay = info->eoi_time - now;
495         else
496                 delay = 1;
497
498         spin_lock_irqsave(&eoi->eoi_list_lock, flags);
499
500         if (list_empty(&eoi->eoi_list)) {
501                 list_add(&info->eoi_list, &eoi->eoi_list);
502                 mod_delayed_work_on(info->eoi_cpu, system_wq,
503                                     &eoi->delayed, delay);
504         } else {
505                 list_for_each_entry_reverse(elem, &eoi->eoi_list, eoi_list) {
506                         if (elem->eoi_time <= info->eoi_time)
507                                 break;
508                 }
509                 list_add(&info->eoi_list, &elem->eoi_list);
510         }
511
512         spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
513 }
514
515 static void xen_irq_lateeoi_locked(struct irq_info *info, bool spurious)
516 {
517         evtchn_port_t evtchn;
518         unsigned int cpu;
519         unsigned int delay = 0;
520
521         evtchn = info->evtchn;
522         if (!VALID_EVTCHN(evtchn) || !list_empty(&info->eoi_list))
523                 return;
524
525         if (spurious) {
526                 if ((1 << info->spurious_cnt) < (HZ << 2))
527                         info->spurious_cnt++;
528                 if (info->spurious_cnt > 1) {
529                         delay = 1 << (info->spurious_cnt - 2);
530                         if (delay > HZ)
531                                 delay = HZ;
532                         if (!info->eoi_time)
533                                 info->eoi_cpu = smp_processor_id();
534                         info->eoi_time = get_jiffies_64() + delay;
535                 }
536         } else {
537                 info->spurious_cnt = 0;
538         }
539
540         cpu = info->eoi_cpu;
541         if (info->eoi_time &&
542             (info->irq_epoch == per_cpu(irq_epoch, cpu) || delay)) {
543                 lateeoi_list_add(info);
544                 return;
545         }
546
547         info->eoi_time = 0;
548
549         /* is_active hasn't been reset yet, do it now. */
550         smp_store_release(&info->is_active, 0);
551         do_unmask(info, EVT_MASK_REASON_EOI_PENDING);
552 }
553
554 static void xen_irq_lateeoi_worker(struct work_struct *work)
555 {
556         struct lateeoi_work *eoi;
557         struct irq_info *info;
558         u64 now = get_jiffies_64();
559         unsigned long flags;
560
561         eoi = container_of(to_delayed_work(work), struct lateeoi_work, delayed);
562
563         rcu_read_lock();
564
565         while (true) {
566                 spin_lock_irqsave(&eoi->eoi_list_lock, flags);
567
568                 info = list_first_entry_or_null(&eoi->eoi_list, struct irq_info,
569                                                 eoi_list);
570
571                 if (info == NULL)
572                         break;
573
574                 if (now < info->eoi_time) {
575                         mod_delayed_work_on(info->eoi_cpu, system_wq,
576                                             &eoi->delayed,
577                                             info->eoi_time - now);
578                         break;
579                 }
580
581                 list_del_init(&info->eoi_list);
582
583                 spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
584
585                 info->eoi_time = 0;
586
587                 xen_irq_lateeoi_locked(info, false);
588         }
589
590         spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
591
592         rcu_read_unlock();
593 }
594
595 static void xen_cpu_init_eoi(unsigned int cpu)
596 {
597         struct lateeoi_work *eoi = &per_cpu(lateeoi, cpu);
598
599         INIT_DELAYED_WORK(&eoi->delayed, xen_irq_lateeoi_worker);
600         spin_lock_init(&eoi->eoi_list_lock);
601         INIT_LIST_HEAD(&eoi->eoi_list);
602 }
603
604 void xen_irq_lateeoi(unsigned int irq, unsigned int eoi_flags)
605 {
606         struct irq_info *info;
607
608         rcu_read_lock();
609
610         info = info_for_irq(irq);
611
612         if (info)
613                 xen_irq_lateeoi_locked(info, eoi_flags & XEN_EOI_FLAG_SPURIOUS);
614
615         rcu_read_unlock();
616 }
617 EXPORT_SYMBOL_GPL(xen_irq_lateeoi);
618
619 static void xen_irq_init(unsigned irq)
620 {
621         struct irq_info *info;
622
623 #ifdef CONFIG_SMP
624         /* By default all event channels notify CPU#0. */
625         cpumask_copy(irq_get_affinity_mask(irq), cpumask_of(0));
626 #endif
627
628         info = kzalloc(sizeof(*info), GFP_KERNEL);
629         if (info == NULL)
630                 panic("Unable to allocate metadata for IRQ%d\n", irq);
631
632         info->type = IRQT_UNBOUND;
633         info->refcnt = -1;
634         INIT_WORK(&info->work, delayed_free_irq);
635
636         set_info_for_irq(irq, info);
637
638         INIT_LIST_HEAD(&info->eoi_list);
639         list_add_tail(&info->list, &xen_irq_list_head);
640 }
641
642 static int __must_check xen_allocate_irqs_dynamic(int nvec)
643 {
644         int i, irq = irq_alloc_descs(-1, 0, nvec, -1);
645
646         if (irq >= 0) {
647                 for (i = 0; i < nvec; i++)
648                         xen_irq_init(irq + i);
649         }
650
651         return irq;
652 }
653
654 static inline int __must_check xen_allocate_irq_dynamic(void)
655 {
656
657         return xen_allocate_irqs_dynamic(1);
658 }
659
660 static int __must_check xen_allocate_irq_gsi(unsigned gsi)
661 {
662         int irq;
663
664         /*
665          * A PV guest has no concept of a GSI (since it has no ACPI
666          * nor access to/knowledge of the physical APICs). Therefore
667          * all IRQs are dynamically allocated from the entire IRQ
668          * space.
669          */
670         if (xen_pv_domain() && !xen_initial_domain())
671                 return xen_allocate_irq_dynamic();
672
673         /* Legacy IRQ descriptors are already allocated by the arch. */
674         if (gsi < nr_legacy_irqs())
675                 irq = gsi;
676         else
677                 irq = irq_alloc_desc_at(gsi, -1);
678
679         xen_irq_init(irq);
680
681         return irq;
682 }
683
684 static void xen_free_irq(unsigned irq)
685 {
686         struct irq_info *info = info_for_irq(irq);
687
688         if (WARN_ON(!info))
689                 return;
690
691         if (!list_empty(&info->eoi_list))
692                 lateeoi_list_del(info);
693
694         list_del(&info->list);
695
696         WARN_ON(info->refcnt > 0);
697
698         call_rcu(&info->rcu, rcu_free_irq);
699 }
700
701 static void xen_evtchn_close(unsigned int port)
702 {
703         struct evtchn_close close;
704
705         close.port = port;
706         if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
707                 BUG();
708 }
709
710 static void event_handler_exit(struct irq_info *info)
711 {
712         smp_store_release(&info->is_active, 0);
713         clear_evtchn(info->evtchn);
714 }
715
716 static void pirq_query_unmask(int irq)
717 {
718         struct physdev_irq_status_query irq_status;
719         struct irq_info *info = info_for_irq(irq);
720
721         BUG_ON(info->type != IRQT_PIRQ);
722
723         irq_status.irq = pirq_from_irq(irq);
724         if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
725                 irq_status.flags = 0;
726
727         info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
728         if (irq_status.flags & XENIRQSTAT_needs_eoi)
729                 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
730 }
731
732 static void eoi_pirq(struct irq_data *data)
733 {
734         struct irq_info *info = info_for_irq(data->irq);
735         int evtchn = info ? info->evtchn : 0;
736         struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
737         int rc = 0;
738
739         if (!VALID_EVTCHN(evtchn))
740                 return;
741
742         if (unlikely(irqd_is_setaffinity_pending(data)) &&
743             likely(!irqd_irq_disabled(data))) {
744                 do_mask(info, EVT_MASK_REASON_TEMPORARY);
745
746                 event_handler_exit(info);
747
748                 irq_move_masked_irq(data);
749
750                 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
751         } else
752                 event_handler_exit(info);
753
754         if (pirq_needs_eoi(data->irq)) {
755                 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
756                 WARN_ON(rc);
757         }
758 }
759
760 static void mask_ack_pirq(struct irq_data *data)
761 {
762         disable_dynirq(data);
763         eoi_pirq(data);
764 }
765
766 static unsigned int __startup_pirq(unsigned int irq)
767 {
768         struct evtchn_bind_pirq bind_pirq;
769         struct irq_info *info = info_for_irq(irq);
770         int evtchn = evtchn_from_irq(irq);
771         int rc;
772
773         BUG_ON(info->type != IRQT_PIRQ);
774
775         if (VALID_EVTCHN(evtchn))
776                 goto out;
777
778         bind_pirq.pirq = pirq_from_irq(irq);
779         /* NB. We are happy to share unless we are probing. */
780         bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
781                                         BIND_PIRQ__WILL_SHARE : 0;
782         rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
783         if (rc != 0) {
784                 pr_warn("Failed to obtain physical IRQ %d\n", irq);
785                 return 0;
786         }
787         evtchn = bind_pirq.port;
788
789         pirq_query_unmask(irq);
790
791         rc = set_evtchn_to_irq(evtchn, irq);
792         if (rc)
793                 goto err;
794
795         info->evtchn = evtchn;
796         bind_evtchn_to_cpu(evtchn, 0);
797
798         rc = xen_evtchn_port_setup(info);
799         if (rc)
800                 goto err;
801
802 out:
803         do_unmask(info, EVT_MASK_REASON_EXPLICIT);
804
805         eoi_pirq(irq_get_irq_data(irq));
806
807         return 0;
808
809 err:
810         pr_err("irq%d: Failed to set port to irq mapping (%d)\n", irq, rc);
811         xen_evtchn_close(evtchn);
812         return 0;
813 }
814
815 static unsigned int startup_pirq(struct irq_data *data)
816 {
817         return __startup_pirq(data->irq);
818 }
819
820 static void shutdown_pirq(struct irq_data *data)
821 {
822         unsigned int irq = data->irq;
823         struct irq_info *info = info_for_irq(irq);
824         unsigned evtchn = evtchn_from_irq(irq);
825
826         BUG_ON(info->type != IRQT_PIRQ);
827
828         if (!VALID_EVTCHN(evtchn))
829                 return;
830
831         do_mask(info, EVT_MASK_REASON_EXPLICIT);
832         xen_evtchn_close(evtchn);
833         xen_irq_info_cleanup(info);
834 }
835
836 static void enable_pirq(struct irq_data *data)
837 {
838         enable_dynirq(data);
839 }
840
841 static void disable_pirq(struct irq_data *data)
842 {
843         disable_dynirq(data);
844 }
845
846 int xen_irq_from_gsi(unsigned gsi)
847 {
848         struct irq_info *info;
849
850         list_for_each_entry(info, &xen_irq_list_head, list) {
851                 if (info->type != IRQT_PIRQ)
852                         continue;
853
854                 if (info->u.pirq.gsi == gsi)
855                         return info->irq;
856         }
857
858         return -1;
859 }
860 EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
861
862 static void __unbind_from_irq(unsigned int irq)
863 {
864         int evtchn = evtchn_from_irq(irq);
865         struct irq_info *info = info_for_irq(irq);
866
867         if (info->refcnt > 0) {
868                 info->refcnt--;
869                 if (info->refcnt != 0)
870                         return;
871         }
872
873         if (VALID_EVTCHN(evtchn)) {
874                 unsigned int cpu = cpu_from_irq(irq);
875
876                 xen_evtchn_close(evtchn);
877
878                 switch (type_from_irq(irq)) {
879                 case IRQT_VIRQ:
880                         per_cpu(virq_to_irq, cpu)[virq_from_irq(irq)] = -1;
881                         break;
882                 case IRQT_IPI:
883                         per_cpu(ipi_to_irq, cpu)[ipi_from_irq(irq)] = -1;
884                         break;
885                 default:
886                         break;
887                 }
888
889                 xen_irq_info_cleanup(info);
890         }
891
892         xen_free_irq(irq);
893 }
894
895 /*
896  * Do not make any assumptions regarding the relationship between the
897  * IRQ number returned here and the Xen pirq argument.
898  *
899  * Note: We don't assign an event channel until the irq actually started
900  * up.  Return an existing irq if we've already got one for the gsi.
901  *
902  * Shareable implies level triggered, not shareable implies edge
903  * triggered here.
904  */
905 int xen_bind_pirq_gsi_to_irq(unsigned gsi,
906                              unsigned pirq, int shareable, char *name)
907 {
908         int irq = -1;
909         struct physdev_irq irq_op;
910         int ret;
911
912         mutex_lock(&irq_mapping_update_lock);
913
914         irq = xen_irq_from_gsi(gsi);
915         if (irq != -1) {
916                 pr_info("%s: returning irq %d for gsi %u\n",
917                         __func__, irq, gsi);
918                 goto out;
919         }
920
921         irq = xen_allocate_irq_gsi(gsi);
922         if (irq < 0)
923                 goto out;
924
925         irq_op.irq = irq;
926         irq_op.vector = 0;
927
928         /* Only the privileged domain can do this. For non-priv, the pcifront
929          * driver provides a PCI bus that does the call to do exactly
930          * this in the priv domain. */
931         if (xen_initial_domain() &&
932             HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
933                 xen_free_irq(irq);
934                 irq = -ENOSPC;
935                 goto out;
936         }
937
938         ret = xen_irq_info_pirq_setup(irq, 0, pirq, gsi, DOMID_SELF,
939                                shareable ? PIRQ_SHAREABLE : 0);
940         if (ret < 0) {
941                 __unbind_from_irq(irq);
942                 irq = ret;
943                 goto out;
944         }
945
946         pirq_query_unmask(irq);
947         /* We try to use the handler with the appropriate semantic for the
948          * type of interrupt: if the interrupt is an edge triggered
949          * interrupt we use handle_edge_irq.
950          *
951          * On the other hand if the interrupt is level triggered we use
952          * handle_fasteoi_irq like the native code does for this kind of
953          * interrupts.
954          *
955          * Depending on the Xen version, pirq_needs_eoi might return true
956          * not only for level triggered interrupts but for edge triggered
957          * interrupts too. In any case Xen always honors the eoi mechanism,
958          * not injecting any more pirqs of the same kind if the first one
959          * hasn't received an eoi yet. Therefore using the fasteoi handler
960          * is the right choice either way.
961          */
962         if (shareable)
963                 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
964                                 handle_fasteoi_irq, name);
965         else
966                 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
967                                 handle_edge_irq, name);
968
969 out:
970         mutex_unlock(&irq_mapping_update_lock);
971
972         return irq;
973 }
974
975 #ifdef CONFIG_PCI_MSI
976 int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
977 {
978         int rc;
979         struct physdev_get_free_pirq op_get_free_pirq;
980
981         op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
982         rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
983
984         WARN_ONCE(rc == -ENOSYS,
985                   "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
986
987         return rc ? -1 : op_get_free_pirq.pirq;
988 }
989
990 int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
991                              int pirq, int nvec, const char *name, domid_t domid)
992 {
993         int i, irq, ret;
994
995         mutex_lock(&irq_mapping_update_lock);
996
997         irq = xen_allocate_irqs_dynamic(nvec);
998         if (irq < 0)
999                 goto out;
1000
1001         for (i = 0; i < nvec; i++) {
1002                 irq_set_chip_and_handler_name(irq + i, &xen_pirq_chip, handle_edge_irq, name);
1003
1004                 ret = xen_irq_info_pirq_setup(irq + i, 0, pirq + i, 0, domid,
1005                                               i == 0 ? 0 : PIRQ_MSI_GROUP);
1006                 if (ret < 0)
1007                         goto error_irq;
1008         }
1009
1010         ret = irq_set_msi_desc(irq, msidesc);
1011         if (ret < 0)
1012                 goto error_irq;
1013 out:
1014         mutex_unlock(&irq_mapping_update_lock);
1015         return irq;
1016 error_irq:
1017         while (nvec--)
1018                 __unbind_from_irq(irq + nvec);
1019         mutex_unlock(&irq_mapping_update_lock);
1020         return ret;
1021 }
1022 #endif
1023
1024 int xen_destroy_irq(int irq)
1025 {
1026         struct physdev_unmap_pirq unmap_irq;
1027         struct irq_info *info = info_for_irq(irq);
1028         int rc = -ENOENT;
1029
1030         mutex_lock(&irq_mapping_update_lock);
1031
1032         /*
1033          * If trying to remove a vector in a MSI group different
1034          * than the first one skip the PIRQ unmap unless this vector
1035          * is the first one in the group.
1036          */
1037         if (xen_initial_domain() && !(info->u.pirq.flags & PIRQ_MSI_GROUP)) {
1038                 unmap_irq.pirq = info->u.pirq.pirq;
1039                 unmap_irq.domid = info->u.pirq.domid;
1040                 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
1041                 /* If another domain quits without making the pci_disable_msix
1042                  * call, the Xen hypervisor takes care of freeing the PIRQs
1043                  * (free_domain_pirqs).
1044                  */
1045                 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
1046                         pr_info("domain %d does not have %d anymore\n",
1047                                 info->u.pirq.domid, info->u.pirq.pirq);
1048                 else if (rc) {
1049                         pr_warn("unmap irq failed %d\n", rc);
1050                         goto out;
1051                 }
1052         }
1053
1054         xen_free_irq(irq);
1055
1056 out:
1057         mutex_unlock(&irq_mapping_update_lock);
1058         return rc;
1059 }
1060
1061 int xen_irq_from_pirq(unsigned pirq)
1062 {
1063         int irq;
1064
1065         struct irq_info *info;
1066
1067         mutex_lock(&irq_mapping_update_lock);
1068
1069         list_for_each_entry(info, &xen_irq_list_head, list) {
1070                 if (info->type != IRQT_PIRQ)
1071                         continue;
1072                 irq = info->irq;
1073                 if (info->u.pirq.pirq == pirq)
1074                         goto out;
1075         }
1076         irq = -1;
1077 out:
1078         mutex_unlock(&irq_mapping_update_lock);
1079
1080         return irq;
1081 }
1082
1083
1084 int xen_pirq_from_irq(unsigned irq)
1085 {
1086         return pirq_from_irq(irq);
1087 }
1088 EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
1089
1090 static int bind_evtchn_to_irq_chip(evtchn_port_t evtchn, struct irq_chip *chip)
1091 {
1092         int irq;
1093         int ret;
1094
1095         if (evtchn >= xen_evtchn_max_channels())
1096                 return -ENOMEM;
1097
1098         mutex_lock(&irq_mapping_update_lock);
1099
1100         irq = get_evtchn_to_irq(evtchn);
1101
1102         if (irq == -1) {
1103                 irq = xen_allocate_irq_dynamic();
1104                 if (irq < 0)
1105                         goto out;
1106
1107                 irq_set_chip_and_handler_name(irq, chip,
1108                                               handle_edge_irq, "event");
1109
1110                 ret = xen_irq_info_evtchn_setup(irq, evtchn);
1111                 if (ret < 0) {
1112                         __unbind_from_irq(irq);
1113                         irq = ret;
1114                         goto out;
1115                 }
1116                 /* New interdomain events are bound to VCPU 0. */
1117                 bind_evtchn_to_cpu(evtchn, 0);
1118         } else {
1119                 struct irq_info *info = info_for_irq(irq);
1120                 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
1121         }
1122
1123 out:
1124         mutex_unlock(&irq_mapping_update_lock);
1125
1126         return irq;
1127 }
1128
1129 int bind_evtchn_to_irq(evtchn_port_t evtchn)
1130 {
1131         return bind_evtchn_to_irq_chip(evtchn, &xen_dynamic_chip);
1132 }
1133 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
1134
1135 int bind_evtchn_to_irq_lateeoi(evtchn_port_t evtchn)
1136 {
1137         return bind_evtchn_to_irq_chip(evtchn, &xen_lateeoi_chip);
1138 }
1139 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq_lateeoi);
1140
1141 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
1142 {
1143         struct evtchn_bind_ipi bind_ipi;
1144         int evtchn, irq;
1145         int ret;
1146
1147         mutex_lock(&irq_mapping_update_lock);
1148
1149         irq = per_cpu(ipi_to_irq, cpu)[ipi];
1150
1151         if (irq == -1) {
1152                 irq = xen_allocate_irq_dynamic();
1153                 if (irq < 0)
1154                         goto out;
1155
1156                 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
1157                                               handle_percpu_irq, "ipi");
1158
1159                 bind_ipi.vcpu = xen_vcpu_nr(cpu);
1160                 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1161                                                 &bind_ipi) != 0)
1162                         BUG();
1163                 evtchn = bind_ipi.port;
1164
1165                 ret = xen_irq_info_ipi_setup(cpu, irq, evtchn, ipi);
1166                 if (ret < 0) {
1167                         __unbind_from_irq(irq);
1168                         irq = ret;
1169                         goto out;
1170                 }
1171                 bind_evtchn_to_cpu(evtchn, cpu);
1172         } else {
1173                 struct irq_info *info = info_for_irq(irq);
1174                 WARN_ON(info == NULL || info->type != IRQT_IPI);
1175         }
1176
1177  out:
1178         mutex_unlock(&irq_mapping_update_lock);
1179         return irq;
1180 }
1181
1182 static int bind_interdomain_evtchn_to_irq_chip(unsigned int remote_domain,
1183                                                evtchn_port_t remote_port,
1184                                                struct irq_chip *chip)
1185 {
1186         struct evtchn_bind_interdomain bind_interdomain;
1187         int err;
1188
1189         bind_interdomain.remote_dom  = remote_domain;
1190         bind_interdomain.remote_port = remote_port;
1191
1192         err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
1193                                           &bind_interdomain);
1194
1195         return err ? : bind_evtchn_to_irq_chip(bind_interdomain.local_port,
1196                                                chip);
1197 }
1198
1199 int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
1200                                    evtchn_port_t remote_port)
1201 {
1202         return bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1203                                                    &xen_dynamic_chip);
1204 }
1205 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irq);
1206
1207 int bind_interdomain_evtchn_to_irq_lateeoi(unsigned int remote_domain,
1208                                            evtchn_port_t remote_port)
1209 {
1210         return bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1211                                                    &xen_lateeoi_chip);
1212 }
1213 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irq_lateeoi);
1214
1215 static int find_virq(unsigned int virq, unsigned int cpu)
1216 {
1217         struct evtchn_status status;
1218         int port, rc = -ENOENT;
1219
1220         memset(&status, 0, sizeof(status));
1221         for (port = 0; port < xen_evtchn_max_channels(); port++) {
1222                 status.dom = DOMID_SELF;
1223                 status.port = port;
1224                 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
1225                 if (rc < 0)
1226                         continue;
1227                 if (status.status != EVTCHNSTAT_virq)
1228                         continue;
1229                 if (status.u.virq == virq && status.vcpu == xen_vcpu_nr(cpu)) {
1230                         rc = port;
1231                         break;
1232                 }
1233         }
1234         return rc;
1235 }
1236
1237 /**
1238  * xen_evtchn_nr_channels - number of usable event channel ports
1239  *
1240  * This may be less than the maximum supported by the current
1241  * hypervisor ABI. Use xen_evtchn_max_channels() for the maximum
1242  * supported.
1243  */
1244 unsigned xen_evtchn_nr_channels(void)
1245 {
1246         return evtchn_ops->nr_channels();
1247 }
1248 EXPORT_SYMBOL_GPL(xen_evtchn_nr_channels);
1249
1250 int bind_virq_to_irq(unsigned int virq, unsigned int cpu, bool percpu)
1251 {
1252         struct evtchn_bind_virq bind_virq;
1253         int evtchn, irq, ret;
1254
1255         mutex_lock(&irq_mapping_update_lock);
1256
1257         irq = per_cpu(virq_to_irq, cpu)[virq];
1258
1259         if (irq == -1) {
1260                 irq = xen_allocate_irq_dynamic();
1261                 if (irq < 0)
1262                         goto out;
1263
1264                 if (percpu)
1265                         irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
1266                                                       handle_percpu_irq, "virq");
1267                 else
1268                         irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
1269                                                       handle_edge_irq, "virq");
1270
1271                 bind_virq.virq = virq;
1272                 bind_virq.vcpu = xen_vcpu_nr(cpu);
1273                 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1274                                                 &bind_virq);
1275                 if (ret == 0)
1276                         evtchn = bind_virq.port;
1277                 else {
1278                         if (ret == -EEXIST)
1279                                 ret = find_virq(virq, cpu);
1280                         BUG_ON(ret < 0);
1281                         evtchn = ret;
1282                 }
1283
1284                 ret = xen_irq_info_virq_setup(cpu, irq, evtchn, virq);
1285                 if (ret < 0) {
1286                         __unbind_from_irq(irq);
1287                         irq = ret;
1288                         goto out;
1289                 }
1290
1291                 bind_evtchn_to_cpu(evtchn, cpu);
1292         } else {
1293                 struct irq_info *info = info_for_irq(irq);
1294                 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
1295         }
1296
1297 out:
1298         mutex_unlock(&irq_mapping_update_lock);
1299
1300         return irq;
1301 }
1302
1303 static void unbind_from_irq(unsigned int irq)
1304 {
1305         mutex_lock(&irq_mapping_update_lock);
1306         __unbind_from_irq(irq);
1307         mutex_unlock(&irq_mapping_update_lock);
1308 }
1309
1310 static int bind_evtchn_to_irqhandler_chip(evtchn_port_t evtchn,
1311                                           irq_handler_t handler,
1312                                           unsigned long irqflags,
1313                                           const char *devname, void *dev_id,
1314                                           struct irq_chip *chip)
1315 {
1316         int irq, retval;
1317
1318         irq = bind_evtchn_to_irq_chip(evtchn, chip);
1319         if (irq < 0)
1320                 return irq;
1321         retval = request_irq(irq, handler, irqflags, devname, dev_id);
1322         if (retval != 0) {
1323                 unbind_from_irq(irq);
1324                 return retval;
1325         }
1326
1327         return irq;
1328 }
1329
1330 int bind_evtchn_to_irqhandler(evtchn_port_t evtchn,
1331                               irq_handler_t handler,
1332                               unsigned long irqflags,
1333                               const char *devname, void *dev_id)
1334 {
1335         return bind_evtchn_to_irqhandler_chip(evtchn, handler, irqflags,
1336                                               devname, dev_id,
1337                                               &xen_dynamic_chip);
1338 }
1339 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1340
1341 int bind_evtchn_to_irqhandler_lateeoi(evtchn_port_t evtchn,
1342                                       irq_handler_t handler,
1343                                       unsigned long irqflags,
1344                                       const char *devname, void *dev_id)
1345 {
1346         return bind_evtchn_to_irqhandler_chip(evtchn, handler, irqflags,
1347                                               devname, dev_id,
1348                                               &xen_lateeoi_chip);
1349 }
1350 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler_lateeoi);
1351
1352 static int bind_interdomain_evtchn_to_irqhandler_chip(
1353                 unsigned int remote_domain, evtchn_port_t remote_port,
1354                 irq_handler_t handler, unsigned long irqflags,
1355                 const char *devname, void *dev_id, struct irq_chip *chip)
1356 {
1357         int irq, retval;
1358
1359         irq = bind_interdomain_evtchn_to_irq_chip(remote_domain, remote_port,
1360                                                   chip);
1361         if (irq < 0)
1362                 return irq;
1363
1364         retval = request_irq(irq, handler, irqflags, devname, dev_id);
1365         if (retval != 0) {
1366                 unbind_from_irq(irq);
1367                 return retval;
1368         }
1369
1370         return irq;
1371 }
1372
1373 int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1374                                           evtchn_port_t remote_port,
1375                                           irq_handler_t handler,
1376                                           unsigned long irqflags,
1377                                           const char *devname,
1378                                           void *dev_id)
1379 {
1380         return bind_interdomain_evtchn_to_irqhandler_chip(remote_domain,
1381                                 remote_port, handler, irqflags, devname,
1382                                 dev_id, &xen_dynamic_chip);
1383 }
1384 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
1385
1386 int bind_interdomain_evtchn_to_irqhandler_lateeoi(unsigned int remote_domain,
1387                                                   evtchn_port_t remote_port,
1388                                                   irq_handler_t handler,
1389                                                   unsigned long irqflags,
1390                                                   const char *devname,
1391                                                   void *dev_id)
1392 {
1393         return bind_interdomain_evtchn_to_irqhandler_chip(remote_domain,
1394                                 remote_port, handler, irqflags, devname,
1395                                 dev_id, &xen_lateeoi_chip);
1396 }
1397 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler_lateeoi);
1398
1399 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
1400                             irq_handler_t handler,
1401                             unsigned long irqflags, const char *devname, void *dev_id)
1402 {
1403         int irq, retval;
1404
1405         irq = bind_virq_to_irq(virq, cpu, irqflags & IRQF_PERCPU);
1406         if (irq < 0)
1407                 return irq;
1408         retval = request_irq(irq, handler, irqflags, devname, dev_id);
1409         if (retval != 0) {
1410                 unbind_from_irq(irq);
1411                 return retval;
1412         }
1413
1414         return irq;
1415 }
1416 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1417
1418 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1419                            unsigned int cpu,
1420                            irq_handler_t handler,
1421                            unsigned long irqflags,
1422                            const char *devname,
1423                            void *dev_id)
1424 {
1425         int irq, retval;
1426
1427         irq = bind_ipi_to_irq(ipi, cpu);
1428         if (irq < 0)
1429                 return irq;
1430
1431         irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
1432         retval = request_irq(irq, handler, irqflags, devname, dev_id);
1433         if (retval != 0) {
1434                 unbind_from_irq(irq);
1435                 return retval;
1436         }
1437
1438         return irq;
1439 }
1440
1441 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1442 {
1443         struct irq_info *info = info_for_irq(irq);
1444
1445         if (WARN_ON(!info))
1446                 return;
1447         free_irq(irq, dev_id);
1448         unbind_from_irq(irq);
1449 }
1450 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1451
1452 /**
1453  * xen_set_irq_priority() - set an event channel priority.
1454  * @irq:irq bound to an event channel.
1455  * @priority: priority between XEN_IRQ_PRIORITY_MAX and XEN_IRQ_PRIORITY_MIN.
1456  */
1457 int xen_set_irq_priority(unsigned irq, unsigned priority)
1458 {
1459         struct evtchn_set_priority set_priority;
1460
1461         set_priority.port = evtchn_from_irq(irq);
1462         set_priority.priority = priority;
1463
1464         return HYPERVISOR_event_channel_op(EVTCHNOP_set_priority,
1465                                            &set_priority);
1466 }
1467 EXPORT_SYMBOL_GPL(xen_set_irq_priority);
1468
1469 int evtchn_make_refcounted(unsigned int evtchn)
1470 {
1471         int irq = get_evtchn_to_irq(evtchn);
1472         struct irq_info *info;
1473
1474         if (irq == -1)
1475                 return -ENOENT;
1476
1477         info = info_for_irq(irq);
1478
1479         if (!info)
1480                 return -ENOENT;
1481
1482         WARN_ON(info->refcnt != -1);
1483
1484         info->refcnt = 1;
1485
1486         return 0;
1487 }
1488 EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1489
1490 int evtchn_get(unsigned int evtchn)
1491 {
1492         int irq;
1493         struct irq_info *info;
1494         int err = -ENOENT;
1495
1496         if (evtchn >= xen_evtchn_max_channels())
1497                 return -EINVAL;
1498
1499         mutex_lock(&irq_mapping_update_lock);
1500
1501         irq = get_evtchn_to_irq(evtchn);
1502         if (irq == -1)
1503                 goto done;
1504
1505         info = info_for_irq(irq);
1506
1507         if (!info)
1508                 goto done;
1509
1510         err = -EINVAL;
1511         if (info->refcnt <= 0 || info->refcnt == SHRT_MAX)
1512                 goto done;
1513
1514         info->refcnt++;
1515         err = 0;
1516  done:
1517         mutex_unlock(&irq_mapping_update_lock);
1518
1519         return err;
1520 }
1521 EXPORT_SYMBOL_GPL(evtchn_get);
1522
1523 void evtchn_put(unsigned int evtchn)
1524 {
1525         int irq = get_evtchn_to_irq(evtchn);
1526         if (WARN_ON(irq == -1))
1527                 return;
1528         unbind_from_irq(irq);
1529 }
1530 EXPORT_SYMBOL_GPL(evtchn_put);
1531
1532 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1533 {
1534         int irq;
1535
1536 #ifdef CONFIG_X86
1537         if (unlikely(vector == XEN_NMI_VECTOR)) {
1538                 int rc =  HYPERVISOR_vcpu_op(VCPUOP_send_nmi, xen_vcpu_nr(cpu),
1539                                              NULL);
1540                 if (rc < 0)
1541                         printk(KERN_WARNING "Sending nmi to CPU%d failed (rc:%d)\n", cpu, rc);
1542                 return;
1543         }
1544 #endif
1545         irq = per_cpu(ipi_to_irq, cpu)[vector];
1546         BUG_ON(irq < 0);
1547         notify_remote_via_irq(irq);
1548 }
1549
1550 struct evtchn_loop_ctrl {
1551         ktime_t timeout;
1552         unsigned count;
1553         bool defer_eoi;
1554 };
1555
1556 void handle_irq_for_port(evtchn_port_t port, struct evtchn_loop_ctrl *ctrl)
1557 {
1558         int irq;
1559         struct irq_info *info;
1560
1561         irq = get_evtchn_to_irq(port);
1562         if (irq == -1)
1563                 return;
1564
1565         /*
1566          * Check for timeout every 256 events.
1567          * We are setting the timeout value only after the first 256
1568          * events in order to not hurt the common case of few loop
1569          * iterations. The 256 is basically an arbitrary value.
1570          *
1571          * In case we are hitting the timeout we need to defer all further
1572          * EOIs in order to ensure to leave the event handling loop rather
1573          * sooner than later.
1574          */
1575         if (!ctrl->defer_eoi && !(++ctrl->count & 0xff)) {
1576                 ktime_t kt = ktime_get();
1577
1578                 if (!ctrl->timeout) {
1579                         kt = ktime_add_ms(kt,
1580                                           jiffies_to_msecs(event_loop_timeout));
1581                         ctrl->timeout = kt;
1582                 } else if (kt > ctrl->timeout) {
1583                         ctrl->defer_eoi = true;
1584                 }
1585         }
1586
1587         info = info_for_irq(irq);
1588         if (xchg_acquire(&info->is_active, 1))
1589                 return;
1590
1591         if (ctrl->defer_eoi) {
1592                 info->eoi_cpu = smp_processor_id();
1593                 info->irq_epoch = __this_cpu_read(irq_epoch);
1594                 info->eoi_time = get_jiffies_64() + event_eoi_delay;
1595         }
1596
1597         generic_handle_irq(irq);
1598 }
1599
1600 static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1601
1602 static void __xen_evtchn_do_upcall(void)
1603 {
1604         struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
1605         int cpu = get_cpu();
1606         unsigned count;
1607         struct evtchn_loop_ctrl ctrl = { 0 };
1608
1609         /*
1610          * When closing an event channel the associated IRQ must not be freed
1611          * until all cpus have left the event handling loop. This is ensured
1612          * by taking the rcu_read_lock() while handling events, as freeing of
1613          * the IRQ is handled via queue_rcu_work() _after_ closing the event
1614          * channel.
1615          */
1616         rcu_read_lock();
1617
1618         do {
1619                 vcpu_info->evtchn_upcall_pending = 0;
1620
1621                 if (__this_cpu_inc_return(xed_nesting_count) - 1)
1622                         goto out;
1623
1624                 xen_evtchn_handle_events(cpu, &ctrl);
1625
1626                 BUG_ON(!irqs_disabled());
1627
1628                 count = __this_cpu_read(xed_nesting_count);
1629                 __this_cpu_write(xed_nesting_count, 0);
1630         } while (count != 1 || vcpu_info->evtchn_upcall_pending);
1631
1632 out:
1633         rcu_read_unlock();
1634
1635         /*
1636          * Increment irq_epoch only now to defer EOIs only for
1637          * xen_irq_lateeoi() invocations occurring from inside the loop
1638          * above.
1639          */
1640         __this_cpu_inc(irq_epoch);
1641
1642         put_cpu();
1643 }
1644
1645 void xen_evtchn_do_upcall(struct pt_regs *regs)
1646 {
1647         struct pt_regs *old_regs = set_irq_regs(regs);
1648
1649         irq_enter();
1650 #ifdef CONFIG_X86
1651         inc_irq_stat(irq_hv_callback_count);
1652 #endif
1653
1654         __xen_evtchn_do_upcall();
1655
1656         irq_exit();
1657         set_irq_regs(old_regs);
1658 }
1659
1660 void xen_hvm_evtchn_do_upcall(void)
1661 {
1662         __xen_evtchn_do_upcall();
1663 }
1664 EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
1665
1666 /* Rebind a new event channel to an existing irq. */
1667 void rebind_evtchn_irq(int evtchn, int irq)
1668 {
1669         struct irq_info *info = info_for_irq(irq);
1670
1671         if (WARN_ON(!info))
1672                 return;
1673
1674         /* Make sure the irq is masked, since the new event channel
1675            will also be masked. */
1676         disable_irq(irq);
1677
1678         mutex_lock(&irq_mapping_update_lock);
1679
1680         /* After resume the irq<->evtchn mappings are all cleared out */
1681         BUG_ON(get_evtchn_to_irq(evtchn) != -1);
1682         /* Expect irq to have been bound before,
1683            so there should be a proper type */
1684         BUG_ON(info->type == IRQT_UNBOUND);
1685
1686         (void)xen_irq_info_evtchn_setup(irq, evtchn);
1687
1688         mutex_unlock(&irq_mapping_update_lock);
1689
1690         bind_evtchn_to_cpu(evtchn, info->cpu);
1691         /* This will be deferred until interrupt is processed */
1692         irq_set_affinity(irq, cpumask_of(info->cpu));
1693
1694         /* Unmask the event channel. */
1695         enable_irq(irq);
1696 }
1697
1698 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1699 static int xen_rebind_evtchn_to_cpu(struct irq_info *info, unsigned int tcpu)
1700 {
1701         struct evtchn_bind_vcpu bind_vcpu;
1702         evtchn_port_t evtchn = info ? info->evtchn : 0;
1703
1704         if (!VALID_EVTCHN(evtchn))
1705                 return -1;
1706
1707         if (!xen_support_evtchn_rebind())
1708                 return -1;
1709
1710         /* Send future instances of this interrupt to other vcpu. */
1711         bind_vcpu.port = evtchn;
1712         bind_vcpu.vcpu = xen_vcpu_nr(tcpu);
1713
1714         /*
1715          * Mask the event while changing the VCPU binding to prevent
1716          * it being delivered on an unexpected VCPU.
1717          */
1718         do_mask(info, EVT_MASK_REASON_TEMPORARY);
1719
1720         /*
1721          * If this fails, it usually just indicates that we're dealing with a
1722          * virq or IPI channel, which don't actually need to be rebound. Ignore
1723          * it, but don't do the xenlinux-level rebind in that case.
1724          */
1725         if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1726                 bind_evtchn_to_cpu(evtchn, tcpu);
1727
1728         do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1729
1730         return 0;
1731 }
1732
1733 static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1734                             bool force)
1735 {
1736         unsigned tcpu = cpumask_first_and(dest, cpu_online_mask);
1737         int ret = xen_rebind_evtchn_to_cpu(info_for_irq(data->irq), tcpu);
1738
1739         if (!ret)
1740                 irq_data_update_effective_affinity(data, cpumask_of(tcpu));
1741
1742         return ret;
1743 }
1744
1745 /* To be called with desc->lock held. */
1746 int xen_set_affinity_evtchn(struct irq_desc *desc, unsigned int tcpu)
1747 {
1748         struct irq_data *d = irq_desc_get_irq_data(desc);
1749
1750         return set_affinity_irq(d, cpumask_of(tcpu), false);
1751 }
1752 EXPORT_SYMBOL_GPL(xen_set_affinity_evtchn);
1753
1754 static void enable_dynirq(struct irq_data *data)
1755 {
1756         struct irq_info *info = info_for_irq(data->irq);
1757         evtchn_port_t evtchn = info ? info->evtchn : 0;
1758
1759         if (VALID_EVTCHN(evtchn))
1760                 do_unmask(info, EVT_MASK_REASON_EXPLICIT);
1761 }
1762
1763 static void disable_dynirq(struct irq_data *data)
1764 {
1765         struct irq_info *info = info_for_irq(data->irq);
1766         evtchn_port_t evtchn = info ? info->evtchn : 0;
1767
1768         if (VALID_EVTCHN(evtchn))
1769                 do_mask(info, EVT_MASK_REASON_EXPLICIT);
1770 }
1771
1772 static void ack_dynirq(struct irq_data *data)
1773 {
1774         struct irq_info *info = info_for_irq(data->irq);
1775         evtchn_port_t evtchn = info ? info->evtchn : 0;
1776
1777         if (!VALID_EVTCHN(evtchn))
1778                 return;
1779
1780         if (unlikely(irqd_is_setaffinity_pending(data)) &&
1781             likely(!irqd_irq_disabled(data))) {
1782                 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1783
1784                 event_handler_exit(info);
1785
1786                 irq_move_masked_irq(data);
1787
1788                 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1789         } else
1790                 event_handler_exit(info);
1791 }
1792
1793 static void mask_ack_dynirq(struct irq_data *data)
1794 {
1795         disable_dynirq(data);
1796         ack_dynirq(data);
1797 }
1798
1799 static void lateeoi_ack_dynirq(struct irq_data *data)
1800 {
1801         struct irq_info *info = info_for_irq(data->irq);
1802         evtchn_port_t evtchn = info ? info->evtchn : 0;
1803
1804         if (!VALID_EVTCHN(evtchn))
1805                 return;
1806
1807         do_mask(info, EVT_MASK_REASON_EOI_PENDING);
1808
1809         if (unlikely(irqd_is_setaffinity_pending(data)) &&
1810             likely(!irqd_irq_disabled(data))) {
1811                 do_mask(info, EVT_MASK_REASON_TEMPORARY);
1812
1813                 clear_evtchn(evtchn);
1814
1815                 irq_move_masked_irq(data);
1816
1817                 do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1818         } else
1819                 clear_evtchn(evtchn);
1820 }
1821
1822 static void lateeoi_mask_ack_dynirq(struct irq_data *data)
1823 {
1824         struct irq_info *info = info_for_irq(data->irq);
1825         evtchn_port_t evtchn = info ? info->evtchn : 0;
1826
1827         if (VALID_EVTCHN(evtchn)) {
1828                 do_mask(info, EVT_MASK_REASON_EXPLICIT);
1829                 ack_dynirq(data);
1830         }
1831 }
1832
1833 static int retrigger_dynirq(struct irq_data *data)
1834 {
1835         struct irq_info *info = info_for_irq(data->irq);
1836         evtchn_port_t evtchn = info ? info->evtchn : 0;
1837
1838         if (!VALID_EVTCHN(evtchn))
1839                 return 0;
1840
1841         do_mask(info, EVT_MASK_REASON_TEMPORARY);
1842         set_evtchn(evtchn);
1843         do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1844
1845         return 1;
1846 }
1847
1848 static void restore_pirqs(void)
1849 {
1850         int pirq, rc, irq, gsi;
1851         struct physdev_map_pirq map_irq;
1852         struct irq_info *info;
1853
1854         list_for_each_entry(info, &xen_irq_list_head, list) {
1855                 if (info->type != IRQT_PIRQ)
1856                         continue;
1857
1858                 pirq = info->u.pirq.pirq;
1859                 gsi = info->u.pirq.gsi;
1860                 irq = info->irq;
1861
1862                 /* save/restore of PT devices doesn't work, so at this point the
1863                  * only devices present are GSI based emulated devices */
1864                 if (!gsi)
1865                         continue;
1866
1867                 map_irq.domid = DOMID_SELF;
1868                 map_irq.type = MAP_PIRQ_TYPE_GSI;
1869                 map_irq.index = gsi;
1870                 map_irq.pirq = pirq;
1871
1872                 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1873                 if (rc) {
1874                         pr_warn("xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1875                                 gsi, irq, pirq, rc);
1876                         xen_free_irq(irq);
1877                         continue;
1878                 }
1879
1880                 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1881
1882                 __startup_pirq(irq);
1883         }
1884 }
1885
1886 static void restore_cpu_virqs(unsigned int cpu)
1887 {
1888         struct evtchn_bind_virq bind_virq;
1889         int virq, irq, evtchn;
1890
1891         for (virq = 0; virq < NR_VIRQS; virq++) {
1892                 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1893                         continue;
1894
1895                 BUG_ON(virq_from_irq(irq) != virq);
1896
1897                 /* Get a new binding from Xen. */
1898                 bind_virq.virq = virq;
1899                 bind_virq.vcpu = xen_vcpu_nr(cpu);
1900                 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1901                                                 &bind_virq) != 0)
1902                         BUG();
1903                 evtchn = bind_virq.port;
1904
1905                 /* Record the new mapping. */
1906                 (void)xen_irq_info_virq_setup(cpu, irq, evtchn, virq);
1907                 bind_evtchn_to_cpu(evtchn, cpu);
1908         }
1909 }
1910
1911 static void restore_cpu_ipis(unsigned int cpu)
1912 {
1913         struct evtchn_bind_ipi bind_ipi;
1914         int ipi, irq, evtchn;
1915
1916         for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1917                 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1918                         continue;
1919
1920                 BUG_ON(ipi_from_irq(irq) != ipi);
1921
1922                 /* Get a new binding from Xen. */
1923                 bind_ipi.vcpu = xen_vcpu_nr(cpu);
1924                 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1925                                                 &bind_ipi) != 0)
1926                         BUG();
1927                 evtchn = bind_ipi.port;
1928
1929                 /* Record the new mapping. */
1930                 (void)xen_irq_info_ipi_setup(cpu, irq, evtchn, ipi);
1931                 bind_evtchn_to_cpu(evtchn, cpu);
1932         }
1933 }
1934
1935 /* Clear an irq's pending state, in preparation for polling on it */
1936 void xen_clear_irq_pending(int irq)
1937 {
1938         struct irq_info *info = info_for_irq(irq);
1939         evtchn_port_t evtchn = info ? info->evtchn : 0;
1940
1941         if (VALID_EVTCHN(evtchn))
1942                 event_handler_exit(info);
1943 }
1944 EXPORT_SYMBOL(xen_clear_irq_pending);
1945 void xen_set_irq_pending(int irq)
1946 {
1947         int evtchn = evtchn_from_irq(irq);
1948
1949         if (VALID_EVTCHN(evtchn))
1950                 set_evtchn(evtchn);
1951 }
1952
1953 bool xen_test_irq_pending(int irq)
1954 {
1955         int evtchn = evtchn_from_irq(irq);
1956         bool ret = false;
1957
1958         if (VALID_EVTCHN(evtchn))
1959                 ret = test_evtchn(evtchn);
1960
1961         return ret;
1962 }
1963
1964 /* Poll waiting for an irq to become pending with timeout.  In the usual case,
1965  * the irq will be disabled so it won't deliver an interrupt. */
1966 void xen_poll_irq_timeout(int irq, u64 timeout)
1967 {
1968         evtchn_port_t evtchn = evtchn_from_irq(irq);
1969
1970         if (VALID_EVTCHN(evtchn)) {
1971                 struct sched_poll poll;
1972
1973                 poll.nr_ports = 1;
1974                 poll.timeout = timeout;
1975                 set_xen_guest_handle(poll.ports, &evtchn);
1976
1977                 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1978                         BUG();
1979         }
1980 }
1981 EXPORT_SYMBOL(xen_poll_irq_timeout);
1982 /* Poll waiting for an irq to become pending.  In the usual case, the
1983  * irq will be disabled so it won't deliver an interrupt. */
1984 void xen_poll_irq(int irq)
1985 {
1986         xen_poll_irq_timeout(irq, 0 /* no timeout */);
1987 }
1988
1989 /* Check whether the IRQ line is shared with other guests. */
1990 int xen_test_irq_shared(int irq)
1991 {
1992         struct irq_info *info = info_for_irq(irq);
1993         struct physdev_irq_status_query irq_status;
1994
1995         if (WARN_ON(!info))
1996                 return -ENOENT;
1997
1998         irq_status.irq = info->u.pirq.pirq;
1999
2000         if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
2001                 return 0;
2002         return !(irq_status.flags & XENIRQSTAT_shared);
2003 }
2004 EXPORT_SYMBOL_GPL(xen_test_irq_shared);
2005
2006 void xen_irq_resume(void)
2007 {
2008         unsigned int cpu;
2009         struct irq_info *info;
2010
2011         /* New event-channel space is not 'live' yet. */
2012         xen_evtchn_resume();
2013
2014         /* No IRQ <-> event-channel mappings. */
2015         list_for_each_entry(info, &xen_irq_list_head, list)
2016                 info->evtchn = 0; /* zap event-channel binding */
2017
2018         clear_evtchn_to_irq_all();
2019
2020         for_each_possible_cpu(cpu) {
2021                 restore_cpu_virqs(cpu);
2022                 restore_cpu_ipis(cpu);
2023         }
2024
2025         restore_pirqs();
2026 }
2027
2028 static struct irq_chip xen_dynamic_chip __read_mostly = {
2029         .name                   = "xen-dyn",
2030
2031         .irq_disable            = disable_dynirq,
2032         .irq_mask               = disable_dynirq,
2033         .irq_unmask             = enable_dynirq,
2034
2035         .irq_ack                = ack_dynirq,
2036         .irq_mask_ack           = mask_ack_dynirq,
2037
2038         .irq_set_affinity       = set_affinity_irq,
2039         .irq_retrigger          = retrigger_dynirq,
2040 };
2041
2042 static struct irq_chip xen_lateeoi_chip __read_mostly = {
2043         /* The chip name needs to contain "xen-dyn" for irqbalance to work. */
2044         .name                   = "xen-dyn-lateeoi",
2045
2046         .irq_disable            = disable_dynirq,
2047         .irq_mask               = disable_dynirq,
2048         .irq_unmask             = enable_dynirq,
2049
2050         .irq_ack                = lateeoi_ack_dynirq,
2051         .irq_mask_ack           = lateeoi_mask_ack_dynirq,
2052
2053         .irq_set_affinity       = set_affinity_irq,
2054         .irq_retrigger          = retrigger_dynirq,
2055 };
2056
2057 static struct irq_chip xen_pirq_chip __read_mostly = {
2058         .name                   = "xen-pirq",
2059
2060         .irq_startup            = startup_pirq,
2061         .irq_shutdown           = shutdown_pirq,
2062         .irq_enable             = enable_pirq,
2063         .irq_disable            = disable_pirq,
2064
2065         .irq_mask               = disable_dynirq,
2066         .irq_unmask             = enable_dynirq,
2067
2068         .irq_ack                = eoi_pirq,
2069         .irq_eoi                = eoi_pirq,
2070         .irq_mask_ack           = mask_ack_pirq,
2071
2072         .irq_set_affinity       = set_affinity_irq,
2073
2074         .irq_retrigger          = retrigger_dynirq,
2075 };
2076
2077 static struct irq_chip xen_percpu_chip __read_mostly = {
2078         .name                   = "xen-percpu",
2079
2080         .irq_disable            = disable_dynirq,
2081         .irq_mask               = disable_dynirq,
2082         .irq_unmask             = enable_dynirq,
2083
2084         .irq_ack                = ack_dynirq,
2085 };
2086
2087 #ifdef CONFIG_XEN_PVHVM
2088 /* Vector callbacks are better than PCI interrupts to receive event
2089  * channel notifications because we can receive vector callbacks on any
2090  * vcpu and we don't need PCI support or APIC interactions. */
2091 void xen_callback_vector(void)
2092 {
2093         int rc;
2094         uint64_t callback_via;
2095
2096         if (xen_have_vector_callback) {
2097                 callback_via = HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR);
2098                 rc = xen_set_callback_via(callback_via);
2099                 if (rc) {
2100                         pr_err("Request for Xen HVM callback vector failed\n");
2101                         xen_have_vector_callback = 0;
2102                         return;
2103                 }
2104                 pr_info_once("Xen HVM callback vector for event delivery is enabled\n");
2105                 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR,
2106                                 xen_hvm_callback_vector);
2107         }
2108 }
2109 #else
2110 void xen_callback_vector(void) {}
2111 #endif
2112
2113 static bool fifo_events = true;
2114 module_param(fifo_events, bool, 0);
2115
2116 static int xen_evtchn_cpu_prepare(unsigned int cpu)
2117 {
2118         int ret = 0;
2119
2120         xen_cpu_init_eoi(cpu);
2121
2122         if (evtchn_ops->percpu_init)
2123                 ret = evtchn_ops->percpu_init(cpu);
2124
2125         return ret;
2126 }
2127
2128 static int xen_evtchn_cpu_dead(unsigned int cpu)
2129 {
2130         int ret = 0;
2131
2132         if (evtchn_ops->percpu_deinit)
2133                 ret = evtchn_ops->percpu_deinit(cpu);
2134
2135         return ret;
2136 }
2137
2138 void __init xen_init_IRQ(void)
2139 {
2140         int ret = -EINVAL;
2141         unsigned int evtchn;
2142
2143         if (fifo_events)
2144                 ret = xen_evtchn_fifo_init();
2145         if (ret < 0)
2146                 xen_evtchn_2l_init();
2147
2148         xen_cpu_init_eoi(smp_processor_id());
2149
2150         cpuhp_setup_state_nocalls(CPUHP_XEN_EVTCHN_PREPARE,
2151                                   "xen/evtchn:prepare",
2152                                   xen_evtchn_cpu_prepare, xen_evtchn_cpu_dead);
2153
2154         evtchn_to_irq = kcalloc(EVTCHN_ROW(xen_evtchn_max_channels()),
2155                                 sizeof(*evtchn_to_irq), GFP_KERNEL);
2156         BUG_ON(!evtchn_to_irq);
2157
2158         /* No event channels are 'live' right now. */
2159         for (evtchn = 0; evtchn < xen_evtchn_nr_channels(); evtchn++)
2160                 mask_evtchn(evtchn);
2161
2162         pirq_needs_eoi = pirq_needs_eoi_flag;
2163
2164 #ifdef CONFIG_X86
2165         if (xen_pv_domain()) {
2166                 irq_ctx_init(smp_processor_id());
2167                 if (xen_initial_domain())
2168                         pci_xen_initial_domain();
2169         }
2170         if (xen_feature(XENFEAT_hvm_callback_vector))
2171                 xen_callback_vector();
2172
2173         if (xen_hvm_domain()) {
2174                 native_init_IRQ();
2175                 /* pci_xen_hvm_init must be called after native_init_IRQ so that
2176                  * __acpi_register_gsi can point at the right function */
2177                 pci_xen_hvm_init();
2178         } else {
2179                 int rc;
2180                 struct physdev_pirq_eoi_gmfn eoi_gmfn;
2181
2182                 pirq_eoi_map = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
2183                 eoi_gmfn.gmfn = virt_to_gfn(pirq_eoi_map);
2184                 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
2185                 if (rc != 0) {
2186                         free_page((unsigned long) pirq_eoi_map);
2187                         pirq_eoi_map = NULL;
2188                 } else
2189                         pirq_needs_eoi = pirq_check_eoi_map;
2190         }
2191 #endif
2192 }