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