linux/drivers/xen/events.c
<<
>>
Prefs
   1/*
   2 * Xen event channels
   3 *
   4 * Xen models interrupts with abstract event channels.  Because each
   5 * domain gets 1024 event channels, but NR_IRQ is not that large, we
   6 * must dynamically map irqs<->event channels.  The event channels
   7 * interface with the rest of the kernel by defining a xen interrupt
   8 * chip.  When an event is received, it is mapped to an irq and sent
   9 * through the normal interrupt processing path.
  10 *
  11 * There are four kinds of events which can be mapped to an event
  12 * channel:
  13 *
  14 * 1. Inter-domain notifications.  This includes all the virtual
  15 *    device events, since they're driven by front-ends in another domain
  16 *    (typically dom0).
  17 * 2. VIRQs, typically used for timers.  These are per-cpu events.
  18 * 3. IPIs.
  19 * 4. PIRQs - Hardware interrupts.
  20 *
  21 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
  22 */
  23
  24#include <linux/linkage.h>
  25#include <linux/interrupt.h>
  26#include <linux/irq.h>
  27#include <linux/module.h>
  28#include <linux/string.h>
  29#include <linux/bootmem.h>
  30#include <linux/slab.h>
  31#include <linux/irqnr.h>
  32#include <linux/pci.h>
  33
  34#ifdef CONFIG_X86
  35#include <asm/desc.h>
  36#include <asm/ptrace.h>
  37#include <asm/irq.h>
  38#include <asm/idle.h>
  39#include <asm/io_apic.h>
  40#include <asm/xen/page.h>
  41#include <asm/xen/pci.h>
  42#endif
  43#include <asm/sync_bitops.h>
  44#include <asm/xen/hypercall.h>
  45#include <asm/xen/hypervisor.h>
  46
  47#include <xen/xen.h>
  48#include <xen/hvm.h>
  49#include <xen/xen-ops.h>
  50#include <xen/events.h>
  51#include <xen/interface/xen.h>
  52#include <xen/interface/event_channel.h>
  53#include <xen/interface/hvm/hvm_op.h>
  54#include <xen/interface/hvm/params.h>
  55#include <xen/interface/physdev.h>
  56#include <xen/interface/sched.h>
  57#include <asm/hw_irq.h>
  58
  59/*
  60 * This lock protects updates to the following mapping and reference-count
  61 * arrays. The lock does not need to be acquired to read the mapping tables.
  62 */
  63static DEFINE_MUTEX(irq_mapping_update_lock);
  64
  65static LIST_HEAD(xen_irq_list_head);
  66
  67/* IRQ <-> VIRQ mapping. */
  68static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
  69
  70/* IRQ <-> IPI mapping */
  71static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
  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        int refcnt;
  97        enum xen_irq_type type; /* type */
  98        unsigned irq;
  99        unsigned short evtchn;  /* event channel */
 100        unsigned short cpu;     /* cpu bound */
 101
 102        union {
 103                unsigned short virq;
 104                enum ipi_vector ipi;
 105                struct {
 106                        unsigned short pirq;
 107                        unsigned short gsi;
 108                        unsigned char vector;
 109                        unsigned char flags;
 110                        uint16_t domid;
 111                } pirq;
 112        } u;
 113};
 114#define PIRQ_NEEDS_EOI  (1 << 0)
 115#define PIRQ_SHAREABLE  (1 << 1)
 116
 117static int *evtchn_to_irq;
 118#ifdef CONFIG_X86
 119static unsigned long *pirq_eoi_map;
 120#endif
 121static bool (*pirq_needs_eoi)(unsigned irq);
 122
 123static DEFINE_PER_CPU(unsigned long [NR_EVENT_CHANNELS/BITS_PER_LONG],
 124                      cpu_evtchn_mask);
 125
 126/* Xen will never allocate port zero for any purpose. */
 127#define VALID_EVTCHN(chn)       ((chn) != 0)
 128
 129static struct irq_chip xen_dynamic_chip;
 130static struct irq_chip xen_percpu_chip;
 131static struct irq_chip xen_pirq_chip;
 132static void enable_dynirq(struct irq_data *data);
 133static void disable_dynirq(struct irq_data *data);
 134
 135/* Get info for IRQ */
 136static struct irq_info *info_for_irq(unsigned irq)
 137{
 138        return irq_get_handler_data(irq);
 139}
 140
 141/* Constructors for packed IRQ information. */
 142static void xen_irq_info_common_init(struct irq_info *info,
 143                                     unsigned irq,
 144                                     enum xen_irq_type type,
 145                                     unsigned short evtchn,
 146                                     unsigned short cpu)
 147{
 148
 149        BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
 150
 151        info->type = type;
 152        info->irq = irq;
 153        info->evtchn = evtchn;
 154        info->cpu = cpu;
 155
 156        evtchn_to_irq[evtchn] = irq;
 157}
 158
 159static void xen_irq_info_evtchn_init(unsigned irq,
 160                                     unsigned short evtchn)
 161{
 162        struct irq_info *info = info_for_irq(irq);
 163
 164        xen_irq_info_common_init(info, irq, IRQT_EVTCHN, evtchn, 0);
 165}
 166
 167static void xen_irq_info_ipi_init(unsigned cpu,
 168                                  unsigned irq,
 169                                  unsigned short evtchn,
 170                                  enum ipi_vector ipi)
 171{
 172        struct irq_info *info = info_for_irq(irq);
 173
 174        xen_irq_info_common_init(info, irq, IRQT_IPI, evtchn, 0);
 175
 176        info->u.ipi = ipi;
 177
 178        per_cpu(ipi_to_irq, cpu)[ipi] = irq;
 179}
 180
 181static void xen_irq_info_virq_init(unsigned cpu,
 182                                   unsigned irq,
 183                                   unsigned short evtchn,
 184                                   unsigned short virq)
 185{
 186        struct irq_info *info = info_for_irq(irq);
 187
 188        xen_irq_info_common_init(info, irq, IRQT_VIRQ, evtchn, 0);
 189
 190        info->u.virq = virq;
 191
 192        per_cpu(virq_to_irq, cpu)[virq] = irq;
 193}
 194
 195static void xen_irq_info_pirq_init(unsigned irq,
 196                                   unsigned short evtchn,
 197                                   unsigned short pirq,
 198                                   unsigned short gsi,
 199                                   unsigned short vector,
 200                                   uint16_t domid,
 201                                   unsigned char flags)
 202{
 203        struct irq_info *info = info_for_irq(irq);
 204
 205        xen_irq_info_common_init(info, irq, IRQT_PIRQ, evtchn, 0);
 206
 207        info->u.pirq.pirq = pirq;
 208        info->u.pirq.gsi = gsi;
 209        info->u.pirq.vector = vector;
 210        info->u.pirq.domid = domid;
 211        info->u.pirq.flags = flags;
 212}
 213
 214/*
 215 * Accessors for packed IRQ information.
 216 */
 217static unsigned int evtchn_from_irq(unsigned irq)
 218{
 219        if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
 220                return 0;
 221
 222        return info_for_irq(irq)->evtchn;
 223}
 224
 225unsigned irq_from_evtchn(unsigned int evtchn)
 226{
 227        return evtchn_to_irq[evtchn];
 228}
 229EXPORT_SYMBOL_GPL(irq_from_evtchn);
 230
 231static enum ipi_vector ipi_from_irq(unsigned irq)
 232{
 233        struct irq_info *info = info_for_irq(irq);
 234
 235        BUG_ON(info == NULL);
 236        BUG_ON(info->type != IRQT_IPI);
 237
 238        return info->u.ipi;
 239}
 240
 241static unsigned virq_from_irq(unsigned irq)
 242{
 243        struct irq_info *info = info_for_irq(irq);
 244
 245        BUG_ON(info == NULL);
 246        BUG_ON(info->type != IRQT_VIRQ);
 247
 248        return info->u.virq;
 249}
 250
 251static unsigned pirq_from_irq(unsigned irq)
 252{
 253        struct irq_info *info = info_for_irq(irq);
 254
 255        BUG_ON(info == NULL);
 256        BUG_ON(info->type != IRQT_PIRQ);
 257
 258        return info->u.pirq.pirq;
 259}
 260
 261static enum xen_irq_type type_from_irq(unsigned irq)
 262{
 263        return info_for_irq(irq)->type;
 264}
 265
 266static unsigned cpu_from_irq(unsigned irq)
 267{
 268        return info_for_irq(irq)->cpu;
 269}
 270
 271static unsigned int cpu_from_evtchn(unsigned int evtchn)
 272{
 273        int irq = evtchn_to_irq[evtchn];
 274        unsigned ret = 0;
 275
 276        if (irq != -1)
 277                ret = cpu_from_irq(irq);
 278
 279        return ret;
 280}
 281
 282#ifdef CONFIG_X86
 283static bool pirq_check_eoi_map(unsigned irq)
 284{
 285        return test_bit(pirq_from_irq(irq), pirq_eoi_map);
 286}
 287#endif
 288
 289static bool pirq_needs_eoi_flag(unsigned irq)
 290{
 291        struct irq_info *info = info_for_irq(irq);
 292        BUG_ON(info->type != IRQT_PIRQ);
 293
 294        return info->u.pirq.flags & PIRQ_NEEDS_EOI;
 295}
 296
 297static inline unsigned long active_evtchns(unsigned int cpu,
 298                                           struct shared_info *sh,
 299                                           unsigned int idx)
 300{
 301        return sh->evtchn_pending[idx] &
 302                per_cpu(cpu_evtchn_mask, cpu)[idx] &
 303                ~sh->evtchn_mask[idx];
 304}
 305
 306static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
 307{
 308        int irq = evtchn_to_irq[chn];
 309
 310        BUG_ON(irq == -1);
 311#ifdef CONFIG_SMP
 312        cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
 313#endif
 314
 315        clear_bit(chn, per_cpu(cpu_evtchn_mask, cpu_from_irq(irq)));
 316        set_bit(chn, per_cpu(cpu_evtchn_mask, cpu));
 317
 318        info_for_irq(irq)->cpu = cpu;
 319}
 320
 321static void init_evtchn_cpu_bindings(void)
 322{
 323        int i;
 324#ifdef CONFIG_SMP
 325        struct irq_info *info;
 326
 327        /* By default all event channels notify CPU#0. */
 328        list_for_each_entry(info, &xen_irq_list_head, list) {
 329                struct irq_desc *desc = irq_to_desc(info->irq);
 330                cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
 331        }
 332#endif
 333
 334        for_each_possible_cpu(i)
 335                memset(per_cpu(cpu_evtchn_mask, i),
 336                       (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
 337}
 338
 339static inline void clear_evtchn(int port)
 340{
 341        struct shared_info *s = HYPERVISOR_shared_info;
 342        sync_clear_bit(port, &s->evtchn_pending[0]);
 343}
 344
 345static inline void set_evtchn(int port)
 346{
 347        struct shared_info *s = HYPERVISOR_shared_info;
 348        sync_set_bit(port, &s->evtchn_pending[0]);
 349}
 350
 351static inline int test_evtchn(int port)
 352{
 353        struct shared_info *s = HYPERVISOR_shared_info;
 354        return sync_test_bit(port, &s->evtchn_pending[0]);
 355}
 356
 357
 358/**
 359 * notify_remote_via_irq - send event to remote end of event channel via irq
 360 * @irq: irq of event channel to send event to
 361 *
 362 * Unlike notify_remote_via_evtchn(), this is safe to use across
 363 * save/restore. Notifications on a broken connection are silently
 364 * dropped.
 365 */
 366void notify_remote_via_irq(int irq)
 367{
 368        int evtchn = evtchn_from_irq(irq);
 369
 370        if (VALID_EVTCHN(evtchn))
 371                notify_remote_via_evtchn(evtchn);
 372}
 373EXPORT_SYMBOL_GPL(notify_remote_via_irq);
 374
 375static void mask_evtchn(int port)
 376{
 377        struct shared_info *s = HYPERVISOR_shared_info;
 378        sync_set_bit(port, &s->evtchn_mask[0]);
 379}
 380
 381static void unmask_evtchn(int port)
 382{
 383        struct shared_info *s = HYPERVISOR_shared_info;
 384        unsigned int cpu = get_cpu();
 385        int do_hypercall = 0, evtchn_pending = 0;
 386
 387        BUG_ON(!irqs_disabled());
 388
 389        if (unlikely((cpu != cpu_from_evtchn(port))))
 390                do_hypercall = 1;
 391        else {
 392                /*
 393                 * Need to clear the mask before checking pending to
 394                 * avoid a race with an event becoming pending.
 395                 *
 396                 * EVTCHNOP_unmask will only trigger an upcall if the
 397                 * mask bit was set, so if a hypercall is needed
 398                 * remask the event.
 399                 */
 400                sync_clear_bit(port, &s->evtchn_mask[0]);
 401                evtchn_pending = sync_test_bit(port, &s->evtchn_pending[0]);
 402
 403                if (unlikely(evtchn_pending && xen_hvm_domain())) {
 404                        sync_set_bit(port, &s->evtchn_mask[0]);
 405                        do_hypercall = 1;
 406                }
 407        }
 408
 409        /* Slow path (hypercall) if this is a non-local port or if this is
 410         * an hvm domain and an event is pending (hvm domains don't have
 411         * their own implementation of irq_enable). */
 412        if (do_hypercall) {
 413                struct evtchn_unmask unmask = { .port = port };
 414                (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
 415        } else {
 416                struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
 417
 418                /*
 419                 * The following is basically the equivalent of
 420                 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
 421                 * the interrupt edge' if the channel is masked.
 422                 */
 423                if (evtchn_pending &&
 424                    !sync_test_and_set_bit(port / BITS_PER_LONG,
 425                                           &vcpu_info->evtchn_pending_sel))
 426                        vcpu_info->evtchn_upcall_pending = 1;
 427        }
 428
 429        put_cpu();
 430}
 431
 432static void xen_irq_init(unsigned irq)
 433{
 434        struct irq_info *info;
 435#ifdef CONFIG_SMP
 436        struct irq_desc *desc = irq_to_desc(irq);
 437
 438        /* By default all event channels notify CPU#0. */
 439        cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
 440#endif
 441
 442        info = kzalloc(sizeof(*info), GFP_KERNEL);
 443        if (info == NULL)
 444                panic("Unable to allocate metadata for IRQ%d\n", irq);
 445
 446        info->type = IRQT_UNBOUND;
 447        info->refcnt = -1;
 448
 449        irq_set_handler_data(irq, info);
 450
 451        list_add_tail(&info->list, &xen_irq_list_head);
 452}
 453
 454static int __must_check xen_allocate_irq_dynamic(void)
 455{
 456        int first = 0;
 457        int irq;
 458
 459#ifdef CONFIG_X86_IO_APIC
 460        /*
 461         * For an HVM guest or domain 0 which see "real" (emulated or
 462         * actual respectively) GSIs we allocate dynamic IRQs
 463         * e.g. those corresponding to event channels or MSIs
 464         * etc. from the range above those "real" GSIs to avoid
 465         * collisions.
 466         */
 467        if (xen_initial_domain() || xen_hvm_domain())
 468                first = get_nr_irqs_gsi();
 469#endif
 470
 471        irq = irq_alloc_desc_from(first, -1);
 472
 473        if (irq >= 0)
 474                xen_irq_init(irq);
 475
 476        return irq;
 477}
 478
 479static int __must_check xen_allocate_irq_gsi(unsigned gsi)
 480{
 481        int irq;
 482
 483        /*
 484         * A PV guest has no concept of a GSI (since it has no ACPI
 485         * nor access to/knowledge of the physical APICs). Therefore
 486         * all IRQs are dynamically allocated from the entire IRQ
 487         * space.
 488         */
 489        if (xen_pv_domain() && !xen_initial_domain())
 490                return xen_allocate_irq_dynamic();
 491
 492        /* Legacy IRQ descriptors are already allocated by the arch. */
 493        if (gsi < NR_IRQS_LEGACY)
 494                irq = gsi;
 495        else
 496                irq = irq_alloc_desc_at(gsi, -1);
 497
 498        xen_irq_init(irq);
 499
 500        return irq;
 501}
 502
 503static void xen_free_irq(unsigned irq)
 504{
 505        struct irq_info *info = irq_get_handler_data(irq);
 506
 507        list_del(&info->list);
 508
 509        irq_set_handler_data(irq, NULL);
 510
 511        WARN_ON(info->refcnt > 0);
 512
 513        kfree(info);
 514
 515        /* Legacy IRQ descriptors are managed by the arch. */
 516        if (irq < NR_IRQS_LEGACY)
 517                return;
 518
 519        irq_free_desc(irq);
 520}
 521
 522static void pirq_query_unmask(int irq)
 523{
 524        struct physdev_irq_status_query irq_status;
 525        struct irq_info *info = info_for_irq(irq);
 526
 527        BUG_ON(info->type != IRQT_PIRQ);
 528
 529        irq_status.irq = pirq_from_irq(irq);
 530        if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
 531                irq_status.flags = 0;
 532
 533        info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
 534        if (irq_status.flags & XENIRQSTAT_needs_eoi)
 535                info->u.pirq.flags |= PIRQ_NEEDS_EOI;
 536}
 537
 538static bool probing_irq(int irq)
 539{
 540        struct irq_desc *desc = irq_to_desc(irq);
 541
 542        return desc && desc->action == NULL;
 543}
 544
 545static void eoi_pirq(struct irq_data *data)
 546{
 547        int evtchn = evtchn_from_irq(data->irq);
 548        struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
 549        int rc = 0;
 550
 551        irq_move_irq(data);
 552
 553        if (VALID_EVTCHN(evtchn))
 554                clear_evtchn(evtchn);
 555
 556        if (pirq_needs_eoi(data->irq)) {
 557                rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
 558                WARN_ON(rc);
 559        }
 560}
 561
 562static void mask_ack_pirq(struct irq_data *data)
 563{
 564        disable_dynirq(data);
 565        eoi_pirq(data);
 566}
 567
 568static unsigned int __startup_pirq(unsigned int irq)
 569{
 570        struct evtchn_bind_pirq bind_pirq;
 571        struct irq_info *info = info_for_irq(irq);
 572        int evtchn = evtchn_from_irq(irq);
 573        int rc;
 574
 575        BUG_ON(info->type != IRQT_PIRQ);
 576
 577        if (VALID_EVTCHN(evtchn))
 578                goto out;
 579
 580        bind_pirq.pirq = pirq_from_irq(irq);
 581        /* NB. We are happy to share unless we are probing. */
 582        bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
 583                                        BIND_PIRQ__WILL_SHARE : 0;
 584        rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
 585        if (rc != 0) {
 586                if (!probing_irq(irq))
 587                        printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
 588                               irq);
 589                return 0;
 590        }
 591        evtchn = bind_pirq.port;
 592
 593        pirq_query_unmask(irq);
 594
 595        evtchn_to_irq[evtchn] = irq;
 596        bind_evtchn_to_cpu(evtchn, 0);
 597        info->evtchn = evtchn;
 598
 599out:
 600        unmask_evtchn(evtchn);
 601        eoi_pirq(irq_get_irq_data(irq));
 602
 603        return 0;
 604}
 605
 606static unsigned int startup_pirq(struct irq_data *data)
 607{
 608        return __startup_pirq(data->irq);
 609}
 610
 611static void shutdown_pirq(struct irq_data *data)
 612{
 613        struct evtchn_close close;
 614        unsigned int irq = data->irq;
 615        struct irq_info *info = info_for_irq(irq);
 616        int evtchn = evtchn_from_irq(irq);
 617
 618        BUG_ON(info->type != IRQT_PIRQ);
 619
 620        if (!VALID_EVTCHN(evtchn))
 621                return;
 622
 623        mask_evtchn(evtchn);
 624
 625        close.port = evtchn;
 626        if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
 627                BUG();
 628
 629        bind_evtchn_to_cpu(evtchn, 0);
 630        evtchn_to_irq[evtchn] = -1;
 631        info->evtchn = 0;
 632}
 633
 634static void enable_pirq(struct irq_data *data)
 635{
 636        startup_pirq(data);
 637}
 638
 639static void disable_pirq(struct irq_data *data)
 640{
 641        disable_dynirq(data);
 642}
 643
 644int xen_irq_from_gsi(unsigned gsi)
 645{
 646        struct irq_info *info;
 647
 648        list_for_each_entry(info, &xen_irq_list_head, list) {
 649                if (info->type != IRQT_PIRQ)
 650                        continue;
 651
 652                if (info->u.pirq.gsi == gsi)
 653                        return info->irq;
 654        }
 655
 656        return -1;
 657}
 658EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
 659
 660/*
 661 * Do not make any assumptions regarding the relationship between the
 662 * IRQ number returned here and the Xen pirq argument.
 663 *
 664 * Note: We don't assign an event channel until the irq actually started
 665 * up.  Return an existing irq if we've already got one for the gsi.
 666 *
 667 * Shareable implies level triggered, not shareable implies edge
 668 * triggered here.
 669 */
 670int xen_bind_pirq_gsi_to_irq(unsigned gsi,
 671                             unsigned pirq, int shareable, char *name)
 672{
 673        int irq = -1;
 674        struct physdev_irq irq_op;
 675
 676        mutex_lock(&irq_mapping_update_lock);
 677
 678        irq = xen_irq_from_gsi(gsi);
 679        if (irq != -1) {
 680                printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
 681                       irq, gsi);
 682                goto out;
 683        }
 684
 685        irq = xen_allocate_irq_gsi(gsi);
 686        if (irq < 0)
 687                goto out;
 688
 689        irq_op.irq = irq;
 690        irq_op.vector = 0;
 691
 692        /* Only the privileged domain can do this. For non-priv, the pcifront
 693         * driver provides a PCI bus that does the call to do exactly
 694         * this in the priv domain. */
 695        if (xen_initial_domain() &&
 696            HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
 697                xen_free_irq(irq);
 698                irq = -ENOSPC;
 699                goto out;
 700        }
 701
 702        xen_irq_info_pirq_init(irq, 0, pirq, gsi, irq_op.vector, DOMID_SELF,
 703                               shareable ? PIRQ_SHAREABLE : 0);
 704
 705        pirq_query_unmask(irq);
 706        /* We try to use the handler with the appropriate semantic for the
 707         * type of interrupt: if the interrupt is an edge triggered
 708         * interrupt we use handle_edge_irq.
 709         *
 710         * On the other hand if the interrupt is level triggered we use
 711         * handle_fasteoi_irq like the native code does for this kind of
 712         * interrupts.
 713         *
 714         * Depending on the Xen version, pirq_needs_eoi might return true
 715         * not only for level triggered interrupts but for edge triggered
 716         * interrupts too. In any case Xen always honors the eoi mechanism,
 717         * not injecting any more pirqs of the same kind if the first one
 718         * hasn't received an eoi yet. Therefore using the fasteoi handler
 719         * is the right choice either way.
 720         */
 721        if (shareable)
 722                irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
 723                                handle_fasteoi_irq, name);
 724        else
 725                irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
 726                                handle_edge_irq, name);
 727
 728out:
 729        mutex_unlock(&irq_mapping_update_lock);
 730
 731        return irq;
 732}
 733
 734#ifdef CONFIG_PCI_MSI
 735int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
 736{
 737        int rc;
 738        struct physdev_get_free_pirq op_get_free_pirq;
 739
 740        op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
 741        rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
 742
 743        WARN_ONCE(rc == -ENOSYS,
 744                  "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
 745
 746        return rc ? -1 : op_get_free_pirq.pirq;
 747}
 748
 749int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
 750                             int pirq, int vector, const char *name,
 751                             domid_t domid)
 752{
 753        int irq, ret;
 754
 755        mutex_lock(&irq_mapping_update_lock);
 756
 757        irq = xen_allocate_irq_dynamic();
 758        if (irq < 0)
 759                goto out;
 760
 761        irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_edge_irq,
 762                        name);
 763
 764        xen_irq_info_pirq_init(irq, 0, pirq, 0, vector, domid, 0);
 765        ret = irq_set_msi_desc(irq, msidesc);
 766        if (ret < 0)
 767                goto error_irq;
 768out:
 769        mutex_unlock(&irq_mapping_update_lock);
 770        return irq;
 771error_irq:
 772        mutex_unlock(&irq_mapping_update_lock);
 773        xen_free_irq(irq);
 774        return ret;
 775}
 776#endif
 777
 778int xen_destroy_irq(int irq)
 779{
 780        struct irq_desc *desc;
 781        struct physdev_unmap_pirq unmap_irq;
 782        struct irq_info *info = info_for_irq(irq);
 783        int rc = -ENOENT;
 784
 785        mutex_lock(&irq_mapping_update_lock);
 786
 787        desc = irq_to_desc(irq);
 788        if (!desc)
 789                goto out;
 790
 791        if (xen_initial_domain()) {
 792                unmap_irq.pirq = info->u.pirq.pirq;
 793                unmap_irq.domid = info->u.pirq.domid;
 794                rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
 795                /* If another domain quits without making the pci_disable_msix
 796                 * call, the Xen hypervisor takes care of freeing the PIRQs
 797                 * (free_domain_pirqs).
 798                 */
 799                if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
 800                        printk(KERN_INFO "domain %d does not have %d anymore\n",
 801                                info->u.pirq.domid, info->u.pirq.pirq);
 802                else if (rc) {
 803                        printk(KERN_WARNING "unmap irq failed %d\n", rc);
 804                        goto out;
 805                }
 806        }
 807
 808        xen_free_irq(irq);
 809
 810out:
 811        mutex_unlock(&irq_mapping_update_lock);
 812        return rc;
 813}
 814
 815int xen_irq_from_pirq(unsigned pirq)
 816{
 817        int irq;
 818
 819        struct irq_info *info;
 820
 821        mutex_lock(&irq_mapping_update_lock);
 822
 823        list_for_each_entry(info, &xen_irq_list_head, list) {
 824                if (info->type != IRQT_PIRQ)
 825                        continue;
 826                irq = info->irq;
 827                if (info->u.pirq.pirq == pirq)
 828                        goto out;
 829        }
 830        irq = -1;
 831out:
 832        mutex_unlock(&irq_mapping_update_lock);
 833
 834        return irq;
 835}
 836
 837
 838int xen_pirq_from_irq(unsigned irq)
 839{
 840        return pirq_from_irq(irq);
 841}
 842EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
 843int bind_evtchn_to_irq(unsigned int evtchn)
 844{
 845        int irq;
 846
 847        mutex_lock(&irq_mapping_update_lock);
 848
 849        irq = evtchn_to_irq[evtchn];
 850
 851        if (irq == -1) {
 852                irq = xen_allocate_irq_dynamic();
 853                if (irq < 0)
 854                        goto out;
 855
 856                irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
 857                                              handle_edge_irq, "event");
 858
 859                xen_irq_info_evtchn_init(irq, evtchn);
 860        } else {
 861                struct irq_info *info = info_for_irq(irq);
 862                WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
 863        }
 864        irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
 865
 866out:
 867        mutex_unlock(&irq_mapping_update_lock);
 868
 869        return irq;
 870}
 871EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
 872
 873static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
 874{
 875        struct evtchn_bind_ipi bind_ipi;
 876        int evtchn, irq;
 877
 878        mutex_lock(&irq_mapping_update_lock);
 879
 880        irq = per_cpu(ipi_to_irq, cpu)[ipi];
 881
 882        if (irq == -1) {
 883                irq = xen_allocate_irq_dynamic();
 884                if (irq < 0)
 885                        goto out;
 886
 887                irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
 888                                              handle_percpu_irq, "ipi");
 889
 890                bind_ipi.vcpu = cpu;
 891                if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
 892                                                &bind_ipi) != 0)
 893                        BUG();
 894                evtchn = bind_ipi.port;
 895
 896                xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
 897
 898                bind_evtchn_to_cpu(evtchn, cpu);
 899        } else {
 900                struct irq_info *info = info_for_irq(irq);
 901                WARN_ON(info == NULL || info->type != IRQT_IPI);
 902        }
 903
 904 out:
 905        mutex_unlock(&irq_mapping_update_lock);
 906        return irq;
 907}
 908
 909static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
 910                                          unsigned int remote_port)
 911{
 912        struct evtchn_bind_interdomain bind_interdomain;
 913        int err;
 914
 915        bind_interdomain.remote_dom  = remote_domain;
 916        bind_interdomain.remote_port = remote_port;
 917
 918        err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
 919                                          &bind_interdomain);
 920
 921        return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
 922}
 923
 924static int find_virq(unsigned int virq, unsigned int cpu)
 925{
 926        struct evtchn_status status;
 927        int port, rc = -ENOENT;
 928
 929        memset(&status, 0, sizeof(status));
 930        for (port = 0; port <= NR_EVENT_CHANNELS; port++) {
 931                status.dom = DOMID_SELF;
 932                status.port = port;
 933                rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
 934                if (rc < 0)
 935                        continue;
 936                if (status.status != EVTCHNSTAT_virq)
 937                        continue;
 938                if (status.u.virq == virq && status.vcpu == cpu) {
 939                        rc = port;
 940                        break;
 941                }
 942        }
 943        return rc;
 944}
 945
 946int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
 947{
 948        struct evtchn_bind_virq bind_virq;
 949        int evtchn, irq, ret;
 950
 951        mutex_lock(&irq_mapping_update_lock);
 952
 953        irq = per_cpu(virq_to_irq, cpu)[virq];
 954
 955        if (irq == -1) {
 956                irq = xen_allocate_irq_dynamic();
 957                if (irq < 0)
 958                        goto out;
 959
 960                irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
 961                                              handle_percpu_irq, "virq");
 962
 963                bind_virq.virq = virq;
 964                bind_virq.vcpu = cpu;
 965                ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
 966                                                &bind_virq);
 967                if (ret == 0)
 968                        evtchn = bind_virq.port;
 969                else {
 970                        if (ret == -EEXIST)
 971                                ret = find_virq(virq, cpu);
 972                        BUG_ON(ret < 0);
 973                        evtchn = ret;
 974                }
 975
 976                xen_irq_info_virq_init(cpu, irq, evtchn, virq);
 977
 978                bind_evtchn_to_cpu(evtchn, cpu);
 979        } else {
 980                struct irq_info *info = info_for_irq(irq);
 981                WARN_ON(info == NULL || info->type != IRQT_VIRQ);
 982        }
 983
 984out:
 985        mutex_unlock(&irq_mapping_update_lock);
 986
 987        return irq;
 988}
 989
 990static void unbind_from_irq(unsigned int irq)
 991{
 992        struct evtchn_close close;
 993        int evtchn = evtchn_from_irq(irq);
 994        struct irq_info *info = irq_get_handler_data(irq);
 995
 996        mutex_lock(&irq_mapping_update_lock);
 997
 998        if (info->refcnt > 0) {
 999                info->refcnt--;
1000                if (info->refcnt != 0)
1001                        goto done;
1002        }
1003
1004        if (VALID_EVTCHN(evtchn)) {
1005                close.port = evtchn;
1006                if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
1007                        BUG();
1008
1009                switch (type_from_irq(irq)) {
1010                case IRQT_VIRQ:
1011                        per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
1012                                [virq_from_irq(irq)] = -1;
1013                        break;
1014                case IRQT_IPI:
1015                        per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
1016                                [ipi_from_irq(irq)] = -1;
1017                        break;
1018                default:
1019                        break;
1020                }
1021
1022                /* Closed ports are implicitly re-bound to VCPU0. */
1023                bind_evtchn_to_cpu(evtchn, 0);
1024
1025                evtchn_to_irq[evtchn] = -1;
1026        }
1027
1028        BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
1029
1030        xen_free_irq(irq);
1031
1032 done:
1033        mutex_unlock(&irq_mapping_update_lock);
1034}
1035
1036int bind_evtchn_to_irqhandler(unsigned int evtchn,
1037                              irq_handler_t handler,
1038                              unsigned long irqflags,
1039                              const char *devname, void *dev_id)
1040{
1041        int irq, retval;
1042
1043        irq = bind_evtchn_to_irq(evtchn);
1044        if (irq < 0)
1045                return irq;
1046        retval = request_irq(irq, handler, irqflags, devname, dev_id);
1047        if (retval != 0) {
1048                unbind_from_irq(irq);
1049                return retval;
1050        }
1051
1052        return irq;
1053}
1054EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1055
1056int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1057                                          unsigned int remote_port,
1058                                          irq_handler_t handler,
1059                                          unsigned long irqflags,
1060                                          const char *devname,
1061                                          void *dev_id)
1062{
1063        int irq, retval;
1064
1065        irq = bind_interdomain_evtchn_to_irq(remote_domain, remote_port);
1066        if (irq < 0)
1067                return irq;
1068
1069        retval = request_irq(irq, handler, irqflags, devname, dev_id);
1070        if (retval != 0) {
1071                unbind_from_irq(irq);
1072                return retval;
1073        }
1074
1075        return irq;
1076}
1077EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
1078
1079int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
1080                            irq_handler_t handler,
1081                            unsigned long irqflags, const char *devname, void *dev_id)
1082{
1083        int irq, retval;
1084
1085        irq = bind_virq_to_irq(virq, cpu);
1086        if (irq < 0)
1087                return irq;
1088        retval = request_irq(irq, handler, irqflags, devname, dev_id);
1089        if (retval != 0) {
1090                unbind_from_irq(irq);
1091                return retval;
1092        }
1093
1094        return irq;
1095}
1096EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1097
1098int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1099                           unsigned int cpu,
1100                           irq_handler_t handler,
1101                           unsigned long irqflags,
1102                           const char *devname,
1103                           void *dev_id)
1104{
1105        int irq, retval;
1106
1107        irq = bind_ipi_to_irq(ipi, cpu);
1108        if (irq < 0)
1109                return irq;
1110
1111        irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
1112        retval = request_irq(irq, handler, irqflags, devname, dev_id);
1113        if (retval != 0) {
1114                unbind_from_irq(irq);
1115                return retval;
1116        }
1117
1118        return irq;
1119}
1120
1121void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1122{
1123        free_irq(irq, dev_id);
1124        unbind_from_irq(irq);
1125}
1126EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1127
1128int evtchn_make_refcounted(unsigned int evtchn)
1129{
1130        int irq = evtchn_to_irq[evtchn];
1131        struct irq_info *info;
1132
1133        if (irq == -1)
1134                return -ENOENT;
1135
1136        info = irq_get_handler_data(irq);
1137
1138        if (!info)
1139                return -ENOENT;
1140
1141        WARN_ON(info->refcnt != -1);
1142
1143        info->refcnt = 1;
1144
1145        return 0;
1146}
1147EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1148
1149int evtchn_get(unsigned int evtchn)
1150{
1151        int irq;
1152        struct irq_info *info;
1153        int err = -ENOENT;
1154
1155        if (evtchn >= NR_EVENT_CHANNELS)
1156                return -EINVAL;
1157
1158        mutex_lock(&irq_mapping_update_lock);
1159
1160        irq = evtchn_to_irq[evtchn];
1161        if (irq == -1)
1162                goto done;
1163
1164        info = irq_get_handler_data(irq);
1165
1166        if (!info)
1167                goto done;
1168
1169        err = -EINVAL;
1170        if (info->refcnt <= 0)
1171                goto done;
1172
1173        info->refcnt++;
1174        err = 0;
1175 done:
1176        mutex_unlock(&irq_mapping_update_lock);
1177
1178        return err;
1179}
1180EXPORT_SYMBOL_GPL(evtchn_get);
1181
1182void evtchn_put(unsigned int evtchn)
1183{
1184        int irq = evtchn_to_irq[evtchn];
1185        if (WARN_ON(irq == -1))
1186                return;
1187        unbind_from_irq(irq);
1188}
1189EXPORT_SYMBOL_GPL(evtchn_put);
1190
1191void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1192{
1193        int irq = per_cpu(ipi_to_irq, cpu)[vector];
1194        BUG_ON(irq < 0);
1195        notify_remote_via_irq(irq);
1196}
1197
1198irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
1199{
1200        struct shared_info *sh = HYPERVISOR_shared_info;
1201        int cpu = smp_processor_id();
1202        unsigned long *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
1203        int i;
1204        unsigned long flags;
1205        static DEFINE_SPINLOCK(debug_lock);
1206        struct vcpu_info *v;
1207
1208        spin_lock_irqsave(&debug_lock, flags);
1209
1210        printk("\nvcpu %d\n  ", cpu);
1211
1212        for_each_online_cpu(i) {
1213                int pending;
1214                v = per_cpu(xen_vcpu, i);
1215                pending = (get_irq_regs() && i == cpu)
1216                        ? xen_irqs_disabled(get_irq_regs())
1217                        : v->evtchn_upcall_mask;
1218                printk("%d: masked=%d pending=%d event_sel %0*lx\n  ", i,
1219                       pending, v->evtchn_upcall_pending,
1220                       (int)(sizeof(v->evtchn_pending_sel)*2),
1221                       v->evtchn_pending_sel);
1222        }
1223        v = per_cpu(xen_vcpu, cpu);
1224
1225        printk("\npending:\n   ");
1226        for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
1227                printk("%0*lx%s", (int)sizeof(sh->evtchn_pending[0])*2,
1228                       sh->evtchn_pending[i],
1229                       i % 8 == 0 ? "\n   " : " ");
1230        printk("\nglobal mask:\n   ");
1231        for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1232                printk("%0*lx%s",
1233                       (int)(sizeof(sh->evtchn_mask[0])*2),
1234                       sh->evtchn_mask[i],
1235                       i % 8 == 0 ? "\n   " : " ");
1236
1237        printk("\nglobally unmasked:\n   ");
1238        for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1239                printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1240                       sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1241                       i % 8 == 0 ? "\n   " : " ");
1242
1243        printk("\nlocal cpu%d mask:\n   ", cpu);
1244        for (i = (NR_EVENT_CHANNELS/BITS_PER_LONG)-1; i >= 0; i--)
1245                printk("%0*lx%s", (int)(sizeof(cpu_evtchn[0])*2),
1246                       cpu_evtchn[i],
1247                       i % 8 == 0 ? "\n   " : " ");
1248
1249        printk("\nlocally unmasked:\n   ");
1250        for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
1251                unsigned long pending = sh->evtchn_pending[i]
1252                        & ~sh->evtchn_mask[i]
1253                        & cpu_evtchn[i];
1254                printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1255                       pending, i % 8 == 0 ? "\n   " : " ");
1256        }
1257
1258        printk("\npending list:\n");
1259        for (i = 0; i < NR_EVENT_CHANNELS; i++) {
1260                if (sync_test_bit(i, sh->evtchn_pending)) {
1261                        int word_idx = i / BITS_PER_LONG;
1262                        printk("  %d: event %d -> irq %d%s%s%s\n",
1263                               cpu_from_evtchn(i), i,
1264                               evtchn_to_irq[i],
1265                               sync_test_bit(word_idx, &v->evtchn_pending_sel)
1266                                             ? "" : " l2-clear",
1267                               !sync_test_bit(i, sh->evtchn_mask)
1268                                             ? "" : " globally-masked",
1269                               sync_test_bit(i, cpu_evtchn)
1270                                             ? "" : " locally-masked");
1271                }
1272        }
1273
1274        spin_unlock_irqrestore(&debug_lock, flags);
1275
1276        return IRQ_HANDLED;
1277}
1278
1279static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1280static DEFINE_PER_CPU(unsigned int, current_word_idx);
1281static DEFINE_PER_CPU(unsigned int, current_bit_idx);
1282
1283/*
1284 * Mask out the i least significant bits of w
1285 */
1286#define MASK_LSBS(w, i) (w & ((~0UL) << i))
1287
1288/*
1289 * Search the CPUs pending events bitmasks.  For each one found, map
1290 * the event number to an irq, and feed it into do_IRQ() for
1291 * handling.
1292 *
1293 * Xen uses a two-level bitmap to speed searching.  The first level is
1294 * a bitset of words which contain pending event bits.  The second
1295 * level is a bitset of pending events themselves.
1296 */
1297static void __xen_evtchn_do_upcall(void)
1298{
1299        int start_word_idx, start_bit_idx;
1300        int word_idx, bit_idx;
1301        int i;
1302        int cpu = get_cpu();
1303        struct shared_info *s = HYPERVISOR_shared_info;
1304        struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
1305        unsigned count;
1306
1307        do {
1308                unsigned long pending_words;
1309
1310                vcpu_info->evtchn_upcall_pending = 0;
1311
1312                if (__this_cpu_inc_return(xed_nesting_count) - 1)
1313                        goto out;
1314
1315#ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
1316                /* Clear master flag /before/ clearing selector flag. */
1317                wmb();
1318#endif
1319                pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
1320
1321                start_word_idx = __this_cpu_read(current_word_idx);
1322                start_bit_idx = __this_cpu_read(current_bit_idx);
1323
1324                word_idx = start_word_idx;
1325
1326                for (i = 0; pending_words != 0; i++) {
1327                        unsigned long pending_bits;
1328                        unsigned long words;
1329
1330                        words = MASK_LSBS(pending_words, word_idx);
1331
1332                        /*
1333                         * If we masked out all events, wrap to beginning.
1334                         */
1335                        if (words == 0) {
1336                                word_idx = 0;
1337                                bit_idx = 0;
1338                                continue;
1339                        }
1340                        word_idx = __ffs(words);
1341
1342                        pending_bits = active_evtchns(cpu, s, word_idx);
1343                        bit_idx = 0; /* usually scan entire word from start */
1344                        if (word_idx == start_word_idx) {
1345                                /* We scan the starting word in two parts */
1346                                if (i == 0)
1347                                        /* 1st time: start in the middle */
1348                                        bit_idx = start_bit_idx;
1349                                else
1350                                        /* 2nd time: mask bits done already */
1351                                        bit_idx &= (1UL << start_bit_idx) - 1;
1352                        }
1353
1354                        do {
1355                                unsigned long bits;
1356                                int port, irq;
1357                                struct irq_desc *desc;
1358
1359                                bits = MASK_LSBS(pending_bits, bit_idx);
1360
1361                                /* If we masked out all events, move on. */
1362                                if (bits == 0)
1363                                        break;
1364
1365                                bit_idx = __ffs(bits);
1366
1367                                /* Process port. */
1368                                port = (word_idx * BITS_PER_LONG) + bit_idx;
1369                                irq = evtchn_to_irq[port];
1370
1371                                if (irq != -1) {
1372                                        desc = irq_to_desc(irq);
1373                                        if (desc)
1374                                                generic_handle_irq_desc(irq, desc);
1375                                }
1376
1377                                bit_idx = (bit_idx + 1) % BITS_PER_LONG;
1378
1379                                /* Next caller starts at last processed + 1 */
1380                                __this_cpu_write(current_word_idx,
1381                                                 bit_idx ? word_idx :
1382                                                 (word_idx+1) % BITS_PER_LONG);
1383                                __this_cpu_write(current_bit_idx, bit_idx);
1384                        } while (bit_idx != 0);
1385
1386                        /* Scan start_l1i twice; all others once. */
1387                        if ((word_idx != start_word_idx) || (i != 0))
1388                                pending_words &= ~(1UL << word_idx);
1389
1390                        word_idx = (word_idx + 1) % BITS_PER_LONG;
1391                }
1392
1393                BUG_ON(!irqs_disabled());
1394
1395                count = __this_cpu_read(xed_nesting_count);
1396                __this_cpu_write(xed_nesting_count, 0);
1397        } while (count != 1 || vcpu_info->evtchn_upcall_pending);
1398
1399out:
1400
1401        put_cpu();
1402}
1403
1404void xen_evtchn_do_upcall(struct pt_regs *regs)
1405{
1406        struct pt_regs *old_regs = set_irq_regs(regs);
1407
1408        irq_enter();
1409#ifdef CONFIG_X86
1410        exit_idle();
1411#endif
1412
1413        __xen_evtchn_do_upcall();
1414
1415        irq_exit();
1416        set_irq_regs(old_regs);
1417}
1418
1419void xen_hvm_evtchn_do_upcall(void)
1420{
1421        __xen_evtchn_do_upcall();
1422}
1423EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
1424
1425/* Rebind a new event channel to an existing irq. */
1426void rebind_evtchn_irq(int evtchn, int irq)
1427{
1428        struct irq_info *info = info_for_irq(irq);
1429
1430        /* Make sure the irq is masked, since the new event channel
1431           will also be masked. */
1432        disable_irq(irq);
1433
1434        mutex_lock(&irq_mapping_update_lock);
1435
1436        /* After resume the irq<->evtchn mappings are all cleared out */
1437        BUG_ON(evtchn_to_irq[evtchn] != -1);
1438        /* Expect irq to have been bound before,
1439           so there should be a proper type */
1440        BUG_ON(info->type == IRQT_UNBOUND);
1441
1442        xen_irq_info_evtchn_init(irq, evtchn);
1443
1444        mutex_unlock(&irq_mapping_update_lock);
1445
1446        /* new event channels are always bound to cpu 0 */
1447        irq_set_affinity(irq, cpumask_of(0));
1448
1449        /* Unmask the event channel. */
1450        enable_irq(irq);
1451}
1452
1453/* Rebind an evtchn so that it gets delivered to a specific cpu */
1454static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
1455{
1456        struct evtchn_bind_vcpu bind_vcpu;
1457        int evtchn = evtchn_from_irq(irq);
1458
1459        if (!VALID_EVTCHN(evtchn))
1460                return -1;
1461
1462        /*
1463         * Events delivered via platform PCI interrupts are always
1464         * routed to vcpu 0 and hence cannot be rebound.
1465         */
1466        if (xen_hvm_domain() && !xen_have_vector_callback)
1467                return -1;
1468
1469        /* Send future instances of this interrupt to other vcpu. */
1470        bind_vcpu.port = evtchn;
1471        bind_vcpu.vcpu = tcpu;
1472
1473        /*
1474         * If this fails, it usually just indicates that we're dealing with a
1475         * virq or IPI channel, which don't actually need to be rebound. Ignore
1476         * it, but don't do the xenlinux-level rebind in that case.
1477         */
1478        if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1479                bind_evtchn_to_cpu(evtchn, tcpu);
1480
1481        return 0;
1482}
1483
1484static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1485                            bool force)
1486{
1487        unsigned tcpu = cpumask_first(dest);
1488
1489        return rebind_irq_to_cpu(data->irq, tcpu);
1490}
1491
1492int resend_irq_on_evtchn(unsigned int irq)
1493{
1494        int masked, evtchn = evtchn_from_irq(irq);
1495        struct shared_info *s = HYPERVISOR_shared_info;
1496
1497        if (!VALID_EVTCHN(evtchn))
1498                return 1;
1499
1500        masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
1501        sync_set_bit(evtchn, s->evtchn_pending);
1502        if (!masked)
1503                unmask_evtchn(evtchn);
1504
1505        return 1;
1506}
1507
1508static void enable_dynirq(struct irq_data *data)
1509{
1510        int evtchn = evtchn_from_irq(data->irq);
1511
1512        if (VALID_EVTCHN(evtchn))
1513                unmask_evtchn(evtchn);
1514}
1515
1516static void disable_dynirq(struct irq_data *data)
1517{
1518        int evtchn = evtchn_from_irq(data->irq);
1519
1520        if (VALID_EVTCHN(evtchn))
1521                mask_evtchn(evtchn);
1522}
1523
1524static void ack_dynirq(struct irq_data *data)
1525{
1526        int evtchn = evtchn_from_irq(data->irq);
1527
1528        irq_move_irq(data);
1529
1530        if (VALID_EVTCHN(evtchn))
1531                clear_evtchn(evtchn);
1532}
1533
1534static void mask_ack_dynirq(struct irq_data *data)
1535{
1536        disable_dynirq(data);
1537        ack_dynirq(data);
1538}
1539
1540static int retrigger_dynirq(struct irq_data *data)
1541{
1542        int evtchn = evtchn_from_irq(data->irq);
1543        struct shared_info *sh = HYPERVISOR_shared_info;
1544        int ret = 0;
1545
1546        if (VALID_EVTCHN(evtchn)) {
1547                int masked;
1548
1549                masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1550                sync_set_bit(evtchn, sh->evtchn_pending);
1551                if (!masked)
1552                        unmask_evtchn(evtchn);
1553                ret = 1;
1554        }
1555
1556        return ret;
1557}
1558
1559static void restore_pirqs(void)
1560{
1561        int pirq, rc, irq, gsi;
1562        struct physdev_map_pirq map_irq;
1563        struct irq_info *info;
1564
1565        list_for_each_entry(info, &xen_irq_list_head, list) {
1566                if (info->type != IRQT_PIRQ)
1567                        continue;
1568
1569                pirq = info->u.pirq.pirq;
1570                gsi = info->u.pirq.gsi;
1571                irq = info->irq;
1572
1573                /* save/restore of PT devices doesn't work, so at this point the
1574                 * only devices present are GSI based emulated devices */
1575                if (!gsi)
1576                        continue;
1577
1578                map_irq.domid = DOMID_SELF;
1579                map_irq.type = MAP_PIRQ_TYPE_GSI;
1580                map_irq.index = gsi;
1581                map_irq.pirq = pirq;
1582
1583                rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1584                if (rc) {
1585                        printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1586                                        gsi, irq, pirq, rc);
1587                        xen_free_irq(irq);
1588                        continue;
1589                }
1590
1591                printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1592
1593                __startup_pirq(irq);
1594        }
1595}
1596
1597static void restore_cpu_virqs(unsigned int cpu)
1598{
1599        struct evtchn_bind_virq bind_virq;
1600        int virq, irq, evtchn;
1601
1602        for (virq = 0; virq < NR_VIRQS; virq++) {
1603                if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1604                        continue;
1605
1606                BUG_ON(virq_from_irq(irq) != virq);
1607
1608                /* Get a new binding from Xen. */
1609                bind_virq.virq = virq;
1610                bind_virq.vcpu = cpu;
1611                if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1612                                                &bind_virq) != 0)
1613                        BUG();
1614                evtchn = bind_virq.port;
1615
1616                /* Record the new mapping. */
1617                xen_irq_info_virq_init(cpu, irq, evtchn, virq);
1618                bind_evtchn_to_cpu(evtchn, cpu);
1619        }
1620}
1621
1622static void restore_cpu_ipis(unsigned int cpu)
1623{
1624        struct evtchn_bind_ipi bind_ipi;
1625        int ipi, irq, evtchn;
1626
1627        for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1628                if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1629                        continue;
1630
1631                BUG_ON(ipi_from_irq(irq) != ipi);
1632
1633                /* Get a new binding from Xen. */
1634                bind_ipi.vcpu = cpu;
1635                if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1636                                                &bind_ipi) != 0)
1637                        BUG();
1638                evtchn = bind_ipi.port;
1639
1640                /* Record the new mapping. */
1641                xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
1642                bind_evtchn_to_cpu(evtchn, cpu);
1643        }
1644}
1645
1646/* Clear an irq's pending state, in preparation for polling on it */
1647void xen_clear_irq_pending(int irq)
1648{
1649        int evtchn = evtchn_from_irq(irq);
1650
1651        if (VALID_EVTCHN(evtchn))
1652                clear_evtchn(evtchn);
1653}
1654EXPORT_SYMBOL(xen_clear_irq_pending);
1655void xen_set_irq_pending(int irq)
1656{
1657        int evtchn = evtchn_from_irq(irq);
1658
1659        if (VALID_EVTCHN(evtchn))
1660                set_evtchn(evtchn);
1661}
1662
1663bool xen_test_irq_pending(int irq)
1664{
1665        int evtchn = evtchn_from_irq(irq);
1666        bool ret = false;
1667
1668        if (VALID_EVTCHN(evtchn))
1669                ret = test_evtchn(evtchn);
1670
1671        return ret;
1672}
1673
1674/* Poll waiting for an irq to become pending with timeout.  In the usual case,
1675 * the irq will be disabled so it won't deliver an interrupt. */
1676void xen_poll_irq_timeout(int irq, u64 timeout)
1677{
1678        evtchn_port_t evtchn = evtchn_from_irq(irq);
1679
1680        if (VALID_EVTCHN(evtchn)) {
1681                struct sched_poll poll;
1682
1683                poll.nr_ports = 1;
1684                poll.timeout = timeout;
1685                set_xen_guest_handle(poll.ports, &evtchn);
1686
1687                if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1688                        BUG();
1689        }
1690}
1691EXPORT_SYMBOL(xen_poll_irq_timeout);
1692/* Poll waiting for an irq to become pending.  In the usual case, the
1693 * irq will be disabled so it won't deliver an interrupt. */
1694void xen_poll_irq(int irq)
1695{
1696        xen_poll_irq_timeout(irq, 0 /* no timeout */);
1697}
1698
1699/* Check whether the IRQ line is shared with other guests. */
1700int xen_test_irq_shared(int irq)
1701{
1702        struct irq_info *info = info_for_irq(irq);
1703        struct physdev_irq_status_query irq_status = { .irq = info->u.pirq.pirq };
1704
1705        if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
1706                return 0;
1707        return !(irq_status.flags & XENIRQSTAT_shared);
1708}
1709EXPORT_SYMBOL_GPL(xen_test_irq_shared);
1710
1711void xen_irq_resume(void)
1712{
1713        unsigned int cpu, evtchn;
1714        struct irq_info *info;
1715
1716        init_evtchn_cpu_bindings();
1717
1718        /* New event-channel space is not 'live' yet. */
1719        for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1720                mask_evtchn(evtchn);
1721
1722        /* No IRQ <-> event-channel mappings. */
1723        list_for_each_entry(info, &xen_irq_list_head, list)
1724                info->evtchn = 0; /* zap event-channel binding */
1725
1726        for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1727                evtchn_to_irq[evtchn] = -1;
1728
1729        for_each_possible_cpu(cpu) {
1730                restore_cpu_virqs(cpu);
1731                restore_cpu_ipis(cpu);
1732        }
1733
1734        restore_pirqs();
1735}
1736
1737static struct irq_chip xen_dynamic_chip __read_mostly = {
1738        .name                   = "xen-dyn",
1739
1740        .irq_disable            = disable_dynirq,
1741        .irq_mask               = disable_dynirq,
1742        .irq_unmask             = enable_dynirq,
1743
1744        .irq_ack                = ack_dynirq,
1745        .irq_mask_ack           = mask_ack_dynirq,
1746
1747        .irq_set_affinity       = set_affinity_irq,
1748        .irq_retrigger          = retrigger_dynirq,
1749};
1750
1751static struct irq_chip xen_pirq_chip __read_mostly = {
1752        .name                   = "xen-pirq",
1753
1754        .irq_startup            = startup_pirq,
1755        .irq_shutdown           = shutdown_pirq,
1756        .irq_enable             = enable_pirq,
1757        .irq_disable            = disable_pirq,
1758
1759        .irq_mask               = disable_dynirq,
1760        .irq_unmask             = enable_dynirq,
1761
1762        .irq_ack                = eoi_pirq,
1763        .irq_eoi                = eoi_pirq,
1764        .irq_mask_ack           = mask_ack_pirq,
1765
1766        .irq_set_affinity       = set_affinity_irq,
1767
1768        .irq_retrigger          = retrigger_dynirq,
1769};
1770
1771static struct irq_chip xen_percpu_chip __read_mostly = {
1772        .name                   = "xen-percpu",
1773
1774        .irq_disable            = disable_dynirq,
1775        .irq_mask               = disable_dynirq,
1776        .irq_unmask             = enable_dynirq,
1777
1778        .irq_ack                = ack_dynirq,
1779};
1780
1781int xen_set_callback_via(uint64_t via)
1782{
1783        struct xen_hvm_param a;
1784        a.domid = DOMID_SELF;
1785        a.index = HVM_PARAM_CALLBACK_IRQ;
1786        a.value = via;
1787        return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1788}
1789EXPORT_SYMBOL_GPL(xen_set_callback_via);
1790
1791#ifdef CONFIG_XEN_PVHVM
1792/* Vector callbacks are better than PCI interrupts to receive event
1793 * channel notifications because we can receive vector callbacks on any
1794 * vcpu and we don't need PCI support or APIC interactions. */
1795void xen_callback_vector(void)
1796{
1797        int rc;
1798        uint64_t callback_via;
1799        if (xen_have_vector_callback) {
1800                callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
1801                rc = xen_set_callback_via(callback_via);
1802                if (rc) {
1803                        printk(KERN_ERR "Request for Xen HVM callback vector"
1804                                        " failed.\n");
1805                        xen_have_vector_callback = 0;
1806                        return;
1807                }
1808                printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1809                                "enabled\n");
1810                /* in the restore case the vector has already been allocated */
1811                if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
1812                        alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
1813        }
1814}
1815#else
1816void xen_callback_vector(void) {}
1817#endif
1818
1819void __init xen_init_IRQ(void)
1820{
1821        int i;
1822
1823        evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1824                                    GFP_KERNEL);
1825        BUG_ON(!evtchn_to_irq);
1826        for (i = 0; i < NR_EVENT_CHANNELS; i++)
1827                evtchn_to_irq[i] = -1;
1828
1829        init_evtchn_cpu_bindings();
1830
1831        /* No event channels are 'live' right now. */
1832        for (i = 0; i < NR_EVENT_CHANNELS; i++)
1833                mask_evtchn(i);
1834
1835        pirq_needs_eoi = pirq_needs_eoi_flag;
1836
1837#ifdef CONFIG_X86
1838        if (xen_hvm_domain()) {
1839                xen_callback_vector();
1840                native_init_IRQ();
1841                /* pci_xen_hvm_init must be called after native_init_IRQ so that
1842                 * __acpi_register_gsi can point at the right function */
1843                pci_xen_hvm_init();
1844        } else {
1845                int rc;
1846                struct physdev_pirq_eoi_gmfn eoi_gmfn;
1847
1848                irq_ctx_init(smp_processor_id());
1849                if (xen_initial_domain())
1850                        pci_xen_initial_domain();
1851
1852                pirq_eoi_map = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
1853                eoi_gmfn.gmfn = virt_to_mfn(pirq_eoi_map);
1854                rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
1855                if (rc != 0) {
1856                        free_page((unsigned long) pirq_eoi_map);
1857                        pirq_eoi_map = NULL;
1858                } else
1859                        pirq_needs_eoi = pirq_check_eoi_map;
1860        }
1861#endif
1862}
1863
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.