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