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