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