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