1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar 4 * Copyright (C) 2005-2006 Thomas Gleixner 5 * 6 * This file contains driver APIs to the irq subsystem. 7 */ 8 9 #define pr_fmt(fmt) "genirq: " fmt 10 11 #include <linux/irq.h> 12 #include <linux/kthread.h> 13 #include <linux/module.h> 14 #include <linux/random.h> 15 #include <linux/interrupt.h> 16 #include <linux/irqdomain.h> 17 #include <linux/slab.h> 18 #include <linux/sched.h> 19 #include <linux/sched/rt.h> 20 #include <linux/sched/task.h> 21 #include <linux/sched/isolation.h> 22 #include <uapi/linux/sched/types.h> 23 #include <linux/task_work.h> 24 25 #include "internals.h" 26 27 #if defined(CONFIG_IRQ_FORCED_THREADING) && !defined(CONFIG_PREEMPT_RT) 28 DEFINE_STATIC_KEY_FALSE(force_irqthreads_key); 29 30 static int __init setup_forced_irqthreads(char *arg) 31 { 32 static_branch_enable(&force_irqthreads_key); 33 return 0; 34 } 35 early_param("threadirqs", setup_forced_irqthreads); 36 #endif 37 38 #ifdef CONFIG_SMP 39 static inline void synchronize_irqwork(struct irq_desc *desc) 40 { 41 /* Synchronize pending or on the fly redirect work */ 42 irq_work_sync(&desc->redirect.work); 43 } 44 #else 45 static inline void synchronize_irqwork(struct irq_desc *desc) { } 46 #endif 47 48 static int __irq_get_irqchip_state(struct irq_data *d, enum irqchip_irq_state which, bool *state); 49 50 static void __synchronize_hardirq(struct irq_desc *desc, bool sync_chip) 51 { 52 struct irq_data *irqd = irq_desc_get_irq_data(desc); 53 bool inprogress; 54 55 do { 56 /* 57 * Wait until we're out of the critical section. This might 58 * give the wrong answer due to the lack of memory barriers. 59 */ 60 while (irqd_irq_inprogress(&desc->irq_data)) 61 cpu_relax(); 62 63 /* Ok, that indicated we're done: double-check carefully. */ 64 guard(raw_spinlock_irqsave)(&desc->lock); 65 inprogress = irqd_irq_inprogress(&desc->irq_data); 66 67 /* 68 * If requested and supported, check at the chip whether it 69 * is in flight at the hardware level, i.e. already pending 70 * in a CPU and waiting for service and acknowledge. 71 */ 72 if (!inprogress && sync_chip) { 73 /* 74 * Ignore the return code. inprogress is only updated 75 * when the chip supports it. 76 */ 77 __irq_get_irqchip_state(irqd, IRQCHIP_STATE_ACTIVE, 78 &inprogress); 79 } 80 /* Oops, that failed? */ 81 } while (inprogress); 82 } 83 84 /** 85 * synchronize_hardirq - wait for pending hard IRQ handlers (on other CPUs) 86 * @irq: interrupt number to wait for 87 * 88 * This function waits for any pending hard IRQ handlers for this interrupt 89 * to complete before returning. If you use this function while holding a 90 * resource the IRQ handler may need you will deadlock. It does not take 91 * associated threaded handlers into account. 92 * 93 * Do not use this for shutdown scenarios where you must be sure that all 94 * parts (hardirq and threaded handler) have completed. 95 * 96 * Returns: false if a threaded handler is active. 97 * 98 * This function may be called - with care - from IRQ context. 99 * 100 * It does not check whether there is an interrupt in flight at the 101 * hardware level, but not serviced yet, as this might deadlock when called 102 * with interrupts disabled and the target CPU of the interrupt is the 103 * current CPU. 104 */ 105 bool synchronize_hardirq(unsigned int irq) 106 { 107 struct irq_desc *desc = irq_to_desc(irq); 108 109 if (desc) { 110 __synchronize_hardirq(desc, false); 111 return !atomic_read(&desc->threads_active); 112 } 113 114 return true; 115 } 116 EXPORT_SYMBOL(synchronize_hardirq); 117 118 static void __synchronize_irq(struct irq_desc *desc) 119 { 120 synchronize_irqwork(desc); 121 __synchronize_hardirq(desc, true); 122 123 /* 124 * We made sure that no hardirq handler is running. Now verify that no 125 * threaded handlers are active. 126 */ 127 wait_event(desc->wait_for_threads, !atomic_read(&desc->threads_active)); 128 } 129 130 /** 131 * synchronize_irq - wait for pending IRQ handlers (on other CPUs) 132 * @irq: interrupt number to wait for 133 * 134 * This function waits for any pending IRQ handlers for this interrupt to 135 * complete before returning. If you use this function while holding a 136 * resource the IRQ handler may need you will deadlock. 137 * 138 * Can only be called from preemptible code as it might sleep when 139 * an interrupt thread is associated to @irq. 140 * 141 * It optionally makes sure (when the irq chip supports that method) 142 * that the interrupt is not pending in any CPU and waiting for 143 * service. 144 */ 145 void synchronize_irq(unsigned int irq) 146 { 147 struct irq_desc *desc = irq_to_desc(irq); 148 149 if (desc) 150 __synchronize_irq(desc); 151 } 152 EXPORT_SYMBOL(synchronize_irq); 153 154 #ifdef CONFIG_SMP 155 cpumask_var_t irq_default_affinity; 156 157 static bool __irq_can_set_affinity(struct irq_desc *desc) 158 { 159 if (!desc || !irqd_can_balance(&desc->irq_data) || 160 !desc->irq_data.chip || !desc->irq_data.chip->irq_set_affinity) 161 return false; 162 return true; 163 } 164 165 /** 166 * irq_can_set_affinity - Check if the affinity of a given irq can be set 167 * @irq: Interrupt to check 168 * 169 */ 170 int irq_can_set_affinity(unsigned int irq) 171 { 172 return __irq_can_set_affinity(irq_to_desc(irq)); 173 } 174 175 /** 176 * irq_can_set_affinity_usr - Check if affinity of a irq can be set from user space 177 * @irq: Interrupt to check 178 * 179 * Like irq_can_set_affinity() above, but additionally checks for the 180 * AFFINITY_MANAGED flag. 181 */ 182 bool irq_can_set_affinity_usr(unsigned int irq) 183 { 184 struct irq_desc *desc = irq_to_desc(irq); 185 186 return __irq_can_set_affinity(desc) && 187 !irqd_affinity_is_managed(&desc->irq_data); 188 } 189 190 /** 191 * irq_set_thread_affinity - Notify irq threads to adjust affinity 192 * @desc: irq descriptor which has affinity changed 193 * 194 * Just set IRQTF_AFFINITY and delegate the affinity setting to the 195 * interrupt thread itself. We can not call set_cpus_allowed_ptr() here as 196 * we hold desc->lock and this code can be called from hard interrupt 197 * context. 198 */ 199 static void irq_set_thread_affinity(struct irq_desc *desc) 200 { 201 struct irqaction *action; 202 203 for_each_action_of_desc(desc, action) { 204 if (action->thread) { 205 set_bit(IRQTF_AFFINITY, &action->thread_flags); 206 wake_up_process(action->thread); 207 } 208 if (action->secondary && action->secondary->thread) { 209 set_bit(IRQTF_AFFINITY, &action->secondary->thread_flags); 210 wake_up_process(action->secondary->thread); 211 } 212 } 213 } 214 215 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK 216 static void irq_validate_effective_affinity(struct irq_data *data) 217 { 218 const struct cpumask *m = irq_data_get_effective_affinity_mask(data); 219 struct irq_chip *chip = irq_data_get_irq_chip(data); 220 221 if (!cpumask_empty(m)) 222 return; 223 pr_warn_once("irq_chip %s did not update eff. affinity mask of irq %u\n", 224 chip->name, data->irq); 225 } 226 #else 227 static inline void irq_validate_effective_affinity(struct irq_data *data) { } 228 #endif 229 230 static DEFINE_PER_CPU(struct cpumask, __tmp_mask); 231 232 int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask, bool force) 233 { 234 struct cpumask *tmp_mask = this_cpu_ptr(&__tmp_mask); 235 struct irq_desc *desc = irq_data_to_desc(data); 236 struct irq_chip *chip = irq_data_get_irq_chip(data); 237 const struct cpumask *prog_mask; 238 int ret; 239 240 if (!chip || !chip->irq_set_affinity) 241 return -EINVAL; 242 243 /* 244 * If this is a managed interrupt and housekeeping is enabled on 245 * it check whether the requested affinity mask intersects with 246 * a housekeeping CPU. If so, then remove the isolated CPUs from 247 * the mask and just keep the housekeeping CPU(s). This prevents 248 * the affinity setter from routing the interrupt to an isolated 249 * CPU to avoid that I/O submitted from a housekeeping CPU causes 250 * interrupts on an isolated one. 251 * 252 * If the masks do not intersect or include online CPU(s) then 253 * keep the requested mask. The isolated target CPUs are only 254 * receiving interrupts when the I/O operation was submitted 255 * directly from them. 256 * 257 * If all housekeeping CPUs in the affinity mask are offline, the 258 * interrupt will be migrated by the CPU hotplug code once a 259 * housekeeping CPU which belongs to the affinity mask comes 260 * online. 261 */ 262 if (irqd_affinity_is_managed(data) && 263 housekeeping_enabled(HK_TYPE_MANAGED_IRQ)) { 264 const struct cpumask *hk_mask; 265 266 hk_mask = housekeeping_cpumask(HK_TYPE_MANAGED_IRQ); 267 268 cpumask_and(tmp_mask, mask, hk_mask); 269 if (!cpumask_intersects(tmp_mask, cpu_online_mask)) 270 prog_mask = mask; 271 else 272 prog_mask = tmp_mask; 273 } else { 274 prog_mask = mask; 275 } 276 277 /* 278 * Make sure we only provide online CPUs to the irqchip, 279 * unless we are being asked to force the affinity (in which 280 * case we do as we are told). 281 */ 282 cpumask_and(tmp_mask, prog_mask, cpu_online_mask); 283 if (!force && !cpumask_empty(tmp_mask)) 284 ret = chip->irq_set_affinity(data, tmp_mask, force); 285 else if (force) 286 ret = chip->irq_set_affinity(data, mask, force); 287 else 288 ret = -EINVAL; 289 290 switch (ret) { 291 case IRQ_SET_MASK_OK: 292 case IRQ_SET_MASK_OK_DONE: 293 cpumask_copy(desc->irq_common_data.affinity, mask); 294 fallthrough; 295 case IRQ_SET_MASK_OK_NOCOPY: 296 irq_validate_effective_affinity(data); 297 irq_set_thread_affinity(desc); 298 ret = 0; 299 } 300 301 return ret; 302 } 303 304 #ifdef CONFIG_GENERIC_PENDING_IRQ 305 static inline int irq_set_affinity_pending(struct irq_data *data, 306 const struct cpumask *dest) 307 { 308 struct irq_desc *desc = irq_data_to_desc(data); 309 310 irqd_set_move_pending(data); 311 irq_copy_pending(desc, dest); 312 return 0; 313 } 314 #else 315 static inline int irq_set_affinity_pending(struct irq_data *data, 316 const struct cpumask *dest) 317 { 318 return -EBUSY; 319 } 320 #endif 321 322 static int irq_try_set_affinity(struct irq_data *data, 323 const struct cpumask *dest, bool force) 324 { 325 int ret = irq_do_set_affinity(data, dest, force); 326 327 /* 328 * In case that the underlying vector management is busy and the 329 * architecture supports the generic pending mechanism then utilize 330 * this to avoid returning an error to user space. 331 */ 332 if (ret == -EBUSY && !force) 333 ret = irq_set_affinity_pending(data, dest); 334 return ret; 335 } 336 337 static bool irq_set_affinity_deactivated(struct irq_data *data, 338 const struct cpumask *mask) 339 { 340 struct irq_desc *desc = irq_data_to_desc(data); 341 342 /* 343 * Handle irq chips which can handle affinity only in activated 344 * state correctly 345 * 346 * If the interrupt is not yet activated, just store the affinity 347 * mask and do not call the chip driver at all. On activation the 348 * driver has to make sure anyway that the interrupt is in a 349 * usable state so startup works. 350 */ 351 if (!IS_ENABLED(CONFIG_IRQ_DOMAIN_HIERARCHY) || 352 irqd_is_activated(data) || !irqd_affinity_on_activate(data)) 353 return false; 354 355 cpumask_copy(desc->irq_common_data.affinity, mask); 356 irq_data_update_effective_affinity(data, mask); 357 irqd_set(data, IRQD_AFFINITY_SET); 358 return true; 359 } 360 361 /** 362 * irq_affinity_schedule_notify_work - Schedule work to notify about affinity change 363 * @desc: Interrupt descriptor whose affinity changed 364 */ 365 void irq_affinity_schedule_notify_work(struct irq_desc *desc) 366 { 367 lockdep_assert_held(&desc->lock); 368 369 kref_get(&desc->affinity_notify->kref); 370 if (!schedule_work(&desc->affinity_notify->work)) { 371 /* Work was already scheduled, drop our extra ref */ 372 kref_put(&desc->affinity_notify->kref, desc->affinity_notify->release); 373 } 374 } 375 376 int irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask, 377 bool force) 378 { 379 struct irq_chip *chip = irq_data_get_irq_chip(data); 380 struct irq_desc *desc = irq_data_to_desc(data); 381 int ret = 0; 382 383 if (!chip || !chip->irq_set_affinity) 384 return -EINVAL; 385 386 if (irq_set_affinity_deactivated(data, mask)) 387 return 0; 388 389 if (irq_can_move_pcntxt(data) && !irqd_is_setaffinity_pending(data)) { 390 ret = irq_try_set_affinity(data, mask, force); 391 } else { 392 irqd_set_move_pending(data); 393 irq_copy_pending(desc, mask); 394 } 395 396 if (desc->affinity_notify) 397 irq_affinity_schedule_notify_work(desc); 398 399 irqd_set(data, IRQD_AFFINITY_SET); 400 401 return ret; 402 } 403 404 /** 405 * irq_update_affinity_desc - Update affinity management for an interrupt 406 * @irq: The interrupt number to update 407 * @affinity: Pointer to the affinity descriptor 408 * 409 * This interface can be used to configure the affinity management of 410 * interrupts which have been allocated already. 411 * 412 * There are certain limitations on when it may be used - attempts to use it 413 * for when the kernel is configured for generic IRQ reservation mode (in 414 * config GENERIC_IRQ_RESERVATION_MODE) will fail, as it may conflict with 415 * managed/non-managed interrupt accounting. In addition, attempts to use it on 416 * an interrupt which is already started or which has already been configured 417 * as managed will also fail, as these mean invalid init state or double init. 418 */ 419 int irq_update_affinity_desc(unsigned int irq, struct irq_affinity_desc *affinity) 420 { 421 /* 422 * Supporting this with the reservation scheme used by x86 needs 423 * some more thought. Fail it for now. 424 */ 425 if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE)) 426 return -EOPNOTSUPP; 427 428 scoped_irqdesc_get_and_buslock(irq, 0) { 429 struct irq_desc *desc = scoped_irqdesc; 430 bool activated; 431 432 /* Requires the interrupt to be shut down */ 433 if (irqd_is_started(&desc->irq_data)) 434 return -EBUSY; 435 436 /* Interrupts which are already managed cannot be modified */ 437 if (irqd_affinity_is_managed(&desc->irq_data)) 438 return -EBUSY; 439 /* 440 * Deactivate the interrupt. That's required to undo 441 * anything an earlier activation has established. 442 */ 443 activated = irqd_is_activated(&desc->irq_data); 444 if (activated) 445 irq_domain_deactivate_irq(&desc->irq_data); 446 447 if (affinity->is_managed) { 448 irqd_set(&desc->irq_data, IRQD_AFFINITY_MANAGED); 449 irqd_set(&desc->irq_data, IRQD_MANAGED_SHUTDOWN); 450 } 451 452 cpumask_copy(desc->irq_common_data.affinity, &affinity->mask); 453 454 /* Restore the activation state */ 455 if (activated) 456 irq_domain_activate_irq(&desc->irq_data, false); 457 return 0; 458 } 459 return -EINVAL; 460 } 461 462 static int __irq_set_affinity(unsigned int irq, const struct cpumask *mask, 463 bool force) 464 { 465 struct irq_desc *desc = irq_to_desc(irq); 466 467 if (!desc) 468 return -EINVAL; 469 470 guard(raw_spinlock_irqsave)(&desc->lock); 471 return irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask, force); 472 } 473 474 /** 475 * irq_set_affinity - Set the irq affinity of a given irq 476 * @irq: Interrupt to set affinity 477 * @cpumask: cpumask 478 * 479 * Fails if cpumask does not contain an online CPU 480 */ 481 int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask) 482 { 483 return __irq_set_affinity(irq, cpumask, false); 484 } 485 EXPORT_SYMBOL_GPL(irq_set_affinity); 486 487 /** 488 * irq_force_affinity - Force the irq affinity of a given irq 489 * @irq: Interrupt to set affinity 490 * @cpumask: cpumask 491 * 492 * Same as irq_set_affinity, but without checking the mask against 493 * online cpus. 494 * 495 * Solely for low level cpu hotplug code, where we need to make per 496 * cpu interrupts affine before the cpu becomes online. 497 */ 498 int irq_force_affinity(unsigned int irq, const struct cpumask *cpumask) 499 { 500 return __irq_set_affinity(irq, cpumask, true); 501 } 502 EXPORT_SYMBOL_GPL(irq_force_affinity); 503 504 int __irq_apply_affinity_hint(unsigned int irq, const struct cpumask *m, bool setaffinity) 505 { 506 int ret = -EINVAL; 507 508 scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_GLOBAL) { 509 scoped_irqdesc->affinity_hint = m; 510 ret = 0; 511 } 512 513 if (!ret && m && setaffinity) 514 __irq_set_affinity(irq, m, false); 515 return ret; 516 } 517 EXPORT_SYMBOL_GPL(__irq_apply_affinity_hint); 518 519 static void irq_affinity_notify(struct work_struct *work) 520 { 521 struct irq_affinity_notify *notify = container_of(work, struct irq_affinity_notify, work); 522 struct irq_desc *desc = irq_to_desc(notify->irq); 523 cpumask_var_t cpumask; 524 525 if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL)) 526 goto out; 527 528 scoped_guard(raw_spinlock_irqsave, &desc->lock) { 529 if (irq_move_pending(&desc->irq_data)) 530 irq_get_pending(cpumask, desc); 531 else 532 cpumask_copy(cpumask, desc->irq_common_data.affinity); 533 } 534 535 notify->notify(notify, cpumask); 536 537 free_cpumask_var(cpumask); 538 out: 539 kref_put(¬ify->kref, notify->release); 540 } 541 542 /** 543 * irq_set_affinity_notifier - control notification of IRQ affinity changes 544 * @irq: Interrupt for which to enable/disable notification 545 * @notify: Context for notification, or %NULL to disable 546 * notification. Function pointers must be initialised; 547 * the other fields will be initialised by this function. 548 * 549 * Must be called in process context. Notification may only be enabled 550 * after the IRQ is allocated and must be disabled before the IRQ is freed 551 * using free_irq(). 552 */ 553 int irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify) 554 { 555 struct irq_desc *desc = irq_to_desc(irq); 556 struct irq_affinity_notify *old_notify; 557 558 /* The release function is promised process context */ 559 might_sleep(); 560 561 if (!desc || irq_is_nmi(desc)) 562 return -EINVAL; 563 564 /* Complete initialisation of *notify */ 565 if (notify) { 566 notify->irq = irq; 567 kref_init(¬ify->kref); 568 INIT_WORK(¬ify->work, irq_affinity_notify); 569 } 570 571 scoped_guard(raw_spinlock_irq, &desc->lock) { 572 old_notify = desc->affinity_notify; 573 desc->affinity_notify = notify; 574 } 575 576 if (old_notify) { 577 if (cancel_work_sync(&old_notify->work)) { 578 /* Pending work had a ref, put that one too */ 579 kref_put(&old_notify->kref, old_notify->release); 580 } 581 kref_put(&old_notify->kref, old_notify->release); 582 } 583 584 return 0; 585 } 586 EXPORT_SYMBOL_GPL(irq_set_affinity_notifier); 587 588 #ifndef CONFIG_AUTO_IRQ_AFFINITY 589 /* 590 * Generic version of the affinity autoselector. 591 */ 592 int irq_setup_affinity(struct irq_desc *desc) 593 { 594 struct cpumask *set = irq_default_affinity; 595 int node = irq_desc_get_node(desc); 596 597 static DEFINE_RAW_SPINLOCK(mask_lock); 598 static struct cpumask mask; 599 600 /* Excludes PER_CPU and NO_BALANCE interrupts */ 601 if (!__irq_can_set_affinity(desc)) 602 return 0; 603 604 guard(raw_spinlock)(&mask_lock); 605 /* 606 * Preserve the managed affinity setting and a userspace affinity 607 * setup, but make sure that one of the targets is online. 608 */ 609 if (irqd_affinity_is_managed(&desc->irq_data) || 610 irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) { 611 if (cpumask_intersects(desc->irq_common_data.affinity, 612 cpu_online_mask)) 613 set = desc->irq_common_data.affinity; 614 else 615 irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET); 616 } 617 618 cpumask_and(&mask, cpu_online_mask, set); 619 if (cpumask_empty(&mask)) 620 cpumask_copy(&mask, cpu_online_mask); 621 622 if (node != NUMA_NO_NODE) { 623 const struct cpumask *nodemask = cpumask_of_node(node); 624 625 /* make sure at least one of the cpus in nodemask is online */ 626 if (cpumask_intersects(&mask, nodemask)) 627 cpumask_and(&mask, &mask, nodemask); 628 } 629 return irq_do_set_affinity(&desc->irq_data, &mask, false); 630 } 631 #else 632 /* Wrapper for ALPHA specific affinity selector magic */ 633 int irq_setup_affinity(struct irq_desc *desc) 634 { 635 return irq_select_affinity(irq_desc_get_irq(desc)); 636 } 637 #endif /* CONFIG_AUTO_IRQ_AFFINITY */ 638 #endif /* CONFIG_SMP */ 639 640 641 /** 642 * irq_set_vcpu_affinity - Set vcpu affinity for the interrupt 643 * @irq: interrupt number to set affinity 644 * @vcpu_info: vCPU specific data or pointer to a percpu array of vCPU 645 * specific data for percpu_devid interrupts 646 * 647 * This function uses the vCPU specific data to set the vCPU affinity for 648 * an irq. The vCPU specific data is passed from outside, such as KVM. One 649 * example code path is as below: KVM -> IOMMU -> irq_set_vcpu_affinity(). 650 */ 651 int irq_set_vcpu_affinity(unsigned int irq, void *vcpu_info) 652 { 653 scoped_irqdesc_get_and_lock(irq, 0) { 654 struct irq_desc *desc = scoped_irqdesc; 655 struct irq_data *data; 656 struct irq_chip *chip; 657 658 data = irq_desc_get_irq_data(desc); 659 do { 660 chip = irq_data_get_irq_chip(data); 661 if (chip && chip->irq_set_vcpu_affinity) 662 break; 663 664 data = irqd_get_parent_data(data); 665 } while (data); 666 667 if (!data) 668 return -ENOSYS; 669 return chip->irq_set_vcpu_affinity(data, vcpu_info); 670 } 671 return -EINVAL; 672 } 673 EXPORT_SYMBOL_GPL(irq_set_vcpu_affinity); 674 675 void __disable_irq(struct irq_desc *desc) 676 { 677 if (!desc->depth++) 678 irq_disable(desc); 679 } 680 681 static int __disable_irq_nosync(unsigned int irq) 682 { 683 scoped_irqdesc_get_and_buslock(irq, IRQ_GET_DESC_CHECK_GLOBAL) { 684 __disable_irq(scoped_irqdesc); 685 return 0; 686 } 687 return -EINVAL; 688 } 689 690 /** 691 * disable_irq_nosync - disable an irq without waiting 692 * @irq: Interrupt to disable 693 * 694 * Disable the selected interrupt line. Disables and Enables are 695 * nested. 696 * Unlike disable_irq(), this function does not ensure existing 697 * instances of the IRQ handler have completed before returning. 698 * 699 * This function may be called from IRQ context. 700 */ 701 void disable_irq_nosync(unsigned int irq) 702 { 703 __disable_irq_nosync(irq); 704 } 705 EXPORT_SYMBOL(disable_irq_nosync); 706 707 /** 708 * disable_irq - disable an irq and wait for completion 709 * @irq: Interrupt to disable 710 * 711 * Disable the selected interrupt line. Enables and Disables are nested. 712 * 713 * This function waits for any pending IRQ handlers for this interrupt to 714 * complete before returning. If you use this function while holding a 715 * resource the IRQ handler may need you will deadlock. 716 * 717 * Can only be called from preemptible code as it might sleep when an 718 * interrupt thread is associated to @irq. 719 * 720 */ 721 void disable_irq(unsigned int irq) 722 { 723 might_sleep(); 724 if (!__disable_irq_nosync(irq)) 725 synchronize_irq(irq); 726 } 727 EXPORT_SYMBOL(disable_irq); 728 729 /** 730 * disable_hardirq - disables an irq and waits for hardirq completion 731 * @irq: Interrupt to disable 732 * 733 * Disable the selected interrupt line. Enables and Disables are nested. 734 * 735 * This function waits for any pending hard IRQ handlers for this interrupt 736 * to complete before returning. If you use this function while holding a 737 * resource the hard IRQ handler may need you will deadlock. 738 * 739 * When used to optimistically disable an interrupt from atomic context the 740 * return value must be checked. 741 * 742 * Returns: false if a threaded handler is active. 743 * 744 * This function may be called - with care - from IRQ context. 745 */ 746 bool disable_hardirq(unsigned int irq) 747 { 748 if (!__disable_irq_nosync(irq)) 749 return synchronize_hardirq(irq); 750 return false; 751 } 752 EXPORT_SYMBOL_GPL(disable_hardirq); 753 754 /** 755 * disable_nmi_nosync - disable an nmi without waiting 756 * @irq: Interrupt to disable 757 * 758 * Disable the selected interrupt line. Disables and enables are nested. 759 * 760 * The interrupt to disable must have been requested through request_nmi. 761 * Unlike disable_nmi(), this function does not ensure existing 762 * instances of the IRQ handler have completed before returning. 763 */ 764 void disable_nmi_nosync(unsigned int irq) 765 { 766 disable_irq_nosync(irq); 767 } 768 769 void __enable_irq(struct irq_desc *desc) 770 { 771 switch (desc->depth) { 772 case 0: 773 err_out: 774 WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n", 775 irq_desc_get_irq(desc)); 776 break; 777 case 1: { 778 if (desc->istate & IRQS_SUSPENDED) 779 goto err_out; 780 /* Prevent probing on this irq: */ 781 irq_settings_set_noprobe(desc); 782 /* 783 * Call irq_startup() not irq_enable() here because the 784 * interrupt might be marked NOAUTOEN so irq_startup() 785 * needs to be invoked when it gets enabled the first time. 786 * This is also required when __enable_irq() is invoked for 787 * a managed and shutdown interrupt from the S3 resume 788 * path. 789 * 790 * If it was already started up, then irq_startup() will 791 * invoke irq_enable() under the hood. 792 */ 793 irq_startup(desc, IRQ_RESEND, IRQ_START_FORCE); 794 break; 795 } 796 default: 797 desc->depth--; 798 } 799 } 800 801 /** 802 * enable_irq - enable handling of an irq 803 * @irq: Interrupt to enable 804 * 805 * Undoes the effect of one call to disable_irq(). If this matches the 806 * last disable, processing of interrupts on this IRQ line is re-enabled. 807 * 808 * This function may be called from IRQ context only when 809 * desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL ! 810 */ 811 void enable_irq(unsigned int irq) 812 { 813 scoped_irqdesc_get_and_buslock(irq, IRQ_GET_DESC_CHECK_GLOBAL) { 814 struct irq_desc *desc = scoped_irqdesc; 815 816 if (WARN(!desc->irq_data.chip, "enable_irq before setup/request_irq: irq %u\n", irq)) 817 return; 818 __enable_irq(desc); 819 } 820 } 821 EXPORT_SYMBOL(enable_irq); 822 823 /** 824 * enable_nmi - enable handling of an nmi 825 * @irq: Interrupt to enable 826 * 827 * The interrupt to enable must have been requested through request_nmi. 828 * Undoes the effect of one call to disable_nmi(). If this matches the last 829 * disable, processing of interrupts on this IRQ line is re-enabled. 830 */ 831 void enable_nmi(unsigned int irq) 832 { 833 enable_irq(irq); 834 } 835 836 static int set_irq_wake_real(unsigned int irq, unsigned int on) 837 { 838 struct irq_desc *desc = irq_to_desc(irq); 839 int ret = -ENXIO; 840 841 if (irq_desc_get_chip(desc)->flags & IRQCHIP_SKIP_SET_WAKE) 842 return 0; 843 844 if (desc->irq_data.chip->irq_set_wake) 845 ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on); 846 847 return ret; 848 } 849 850 /** 851 * irq_set_irq_wake - control irq power management wakeup 852 * @irq: interrupt to control 853 * @on: enable/disable power management wakeup 854 * 855 * Enable/disable power management wakeup mode, which is disabled by 856 * default. Enables and disables must match, just as they match for 857 * non-wakeup mode support. 858 * 859 * Wakeup mode lets this IRQ wake the system from sleep states like 860 * "suspend to RAM". 861 * 862 * Note: irq enable/disable state is completely orthogonal to the 863 * enable/disable state of irq wake. An irq can be disabled with 864 * disable_irq() and still wake the system as long as the irq has wake 865 * enabled. If this does not hold, then the underlying irq chip and the 866 * related driver need to be investigated. 867 */ 868 int irq_set_irq_wake(unsigned int irq, unsigned int on) 869 { 870 scoped_irqdesc_get_and_buslock(irq, IRQ_GET_DESC_CHECK_GLOBAL) { 871 struct irq_desc *desc = scoped_irqdesc; 872 int ret = 0; 873 874 /* Don't use NMIs as wake up interrupts please */ 875 if (irq_is_nmi(desc)) 876 return -EINVAL; 877 878 /* 879 * wakeup-capable irqs can be shared between drivers that 880 * don't need to have the same sleep mode behaviors. 881 */ 882 if (on) { 883 if (desc->wake_depth++ == 0) { 884 ret = set_irq_wake_real(irq, on); 885 if (ret) 886 desc->wake_depth = 0; 887 else 888 irqd_set(&desc->irq_data, IRQD_WAKEUP_STATE); 889 } 890 } else { 891 if (desc->wake_depth == 0) { 892 WARN(1, "Unbalanced IRQ %d wake disable\n", irq); 893 } else if (--desc->wake_depth == 0) { 894 ret = set_irq_wake_real(irq, on); 895 if (ret) 896 desc->wake_depth = 1; 897 else 898 irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE); 899 } 900 } 901 return ret; 902 } 903 return -EINVAL; 904 } 905 EXPORT_SYMBOL(irq_set_irq_wake); 906 907 /* 908 * Internal function that tells the architecture code whether a 909 * particular irq has been exclusively allocated or is available 910 * for driver use. 911 */ 912 bool can_request_irq(unsigned int irq, unsigned long irqflags) 913 { 914 scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_GLOBAL) { 915 struct irq_desc *desc = scoped_irqdesc; 916 917 if (irq_settings_can_request(desc)) { 918 if (!desc->action || irqflags & desc->action->flags & IRQF_SHARED) 919 return true; 920 } 921 } 922 return false; 923 } 924 925 int __irq_set_trigger(struct irq_desc *desc, unsigned long flags) 926 { 927 struct irq_chip *chip = desc->irq_data.chip; 928 int ret, unmask = 0; 929 930 if (!chip || !chip->irq_set_type) { 931 /* 932 * IRQF_TRIGGER_* but the PIC does not support multiple 933 * flow-types? 934 */ 935 pr_debug("No set_type function for IRQ %d (%s)\n", 936 irq_desc_get_irq(desc), 937 chip ? (chip->name ? : "unknown") : "unknown"); 938 return 0; 939 } 940 941 if (chip->flags & IRQCHIP_SET_TYPE_MASKED) { 942 if (!irqd_irq_masked(&desc->irq_data)) 943 mask_irq(desc); 944 if (!irqd_irq_disabled(&desc->irq_data)) 945 unmask = 1; 946 } 947 948 /* Mask all flags except trigger mode */ 949 flags &= IRQ_TYPE_SENSE_MASK; 950 ret = chip->irq_set_type(&desc->irq_data, flags); 951 952 switch (ret) { 953 case IRQ_SET_MASK_OK: 954 case IRQ_SET_MASK_OK_DONE: 955 irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK); 956 irqd_set(&desc->irq_data, flags); 957 fallthrough; 958 959 case IRQ_SET_MASK_OK_NOCOPY: 960 flags = irqd_get_trigger_type(&desc->irq_data); 961 irq_settings_set_trigger_mask(desc, flags); 962 irqd_clear(&desc->irq_data, IRQD_LEVEL); 963 irq_settings_clr_level(desc); 964 if (flags & IRQ_TYPE_LEVEL_MASK) { 965 irq_settings_set_level(desc); 966 irqd_set(&desc->irq_data, IRQD_LEVEL); 967 } 968 969 ret = 0; 970 break; 971 default: 972 pr_err("Setting trigger mode %lu for irq %u failed (%pS)\n", 973 flags, irq_desc_get_irq(desc), chip->irq_set_type); 974 } 975 if (unmask) 976 unmask_irq(desc); 977 return ret; 978 } 979 980 #ifdef CONFIG_HARDIRQS_SW_RESEND 981 int irq_set_parent(int irq, int parent_irq) 982 { 983 scoped_irqdesc_get_and_lock(irq, 0) { 984 scoped_irqdesc->parent_irq = parent_irq; 985 return 0; 986 } 987 return -EINVAL; 988 } 989 EXPORT_SYMBOL_GPL(irq_set_parent); 990 #endif 991 992 /* 993 * Default primary interrupt handler for threaded interrupts. Is 994 * assigned as primary handler when request_threaded_irq is called 995 * with handler == NULL. Useful for oneshot interrupts. 996 */ 997 static irqreturn_t irq_default_primary_handler(int irq, void *dev_id) 998 { 999 return IRQ_WAKE_THREAD; 1000 } 1001 1002 /* 1003 * Primary handler for nested threaded interrupts. Should never be 1004 * called. 1005 */ 1006 static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id) 1007 { 1008 WARN(1, "Primary handler called for nested irq %d\n", irq); 1009 return IRQ_NONE; 1010 } 1011 1012 static irqreturn_t irq_forced_secondary_handler(int irq, void *dev_id) 1013 { 1014 WARN(1, "Secondary action handler called for irq %d\n", irq); 1015 return IRQ_NONE; 1016 } 1017 1018 #ifdef CONFIG_SMP 1019 /* 1020 * Check whether we need to change the affinity of the interrupt thread. 1021 */ 1022 static void irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) 1023 { 1024 cpumask_var_t mask; 1025 1026 if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags)) 1027 return; 1028 1029 __set_current_state(TASK_RUNNING); 1030 1031 /* 1032 * In case we are out of memory we set IRQTF_AFFINITY again and 1033 * try again next time 1034 */ 1035 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) { 1036 set_bit(IRQTF_AFFINITY, &action->thread_flags); 1037 return; 1038 } 1039 1040 scoped_guard(raw_spinlock_irq, &desc->lock) { 1041 const struct cpumask *m; 1042 1043 m = irq_data_get_effective_affinity_mask(&desc->irq_data); 1044 cpumask_copy(mask, m); 1045 } 1046 1047 set_cpus_allowed_ptr(current, mask); 1048 free_cpumask_var(mask); 1049 } 1050 #else 1051 static inline void irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { } 1052 #endif 1053 1054 static int irq_wait_for_interrupt(struct irq_desc *desc, 1055 struct irqaction *action) 1056 { 1057 for (;;) { 1058 set_current_state(TASK_INTERRUPTIBLE); 1059 irq_thread_check_affinity(desc, action); 1060 1061 if (kthread_should_stop()) { 1062 /* may need to run one last time */ 1063 if (test_and_clear_bit(IRQTF_RUNTHREAD, 1064 &action->thread_flags)) { 1065 __set_current_state(TASK_RUNNING); 1066 return 0; 1067 } 1068 __set_current_state(TASK_RUNNING); 1069 return -1; 1070 } 1071 1072 if (test_and_clear_bit(IRQTF_RUNTHREAD, 1073 &action->thread_flags)) { 1074 __set_current_state(TASK_RUNNING); 1075 return 0; 1076 } 1077 schedule(); 1078 } 1079 } 1080 1081 /* 1082 * Oneshot interrupts keep the irq line masked until the threaded 1083 * handler finished. unmask if the interrupt has not been disabled and 1084 * is marked MASKED. 1085 */ 1086 static void irq_finalize_oneshot(struct irq_desc *desc, 1087 struct irqaction *action) 1088 { 1089 if (!(desc->istate & IRQS_ONESHOT) || 1090 action->handler == irq_forced_secondary_handler) 1091 return; 1092 again: 1093 chip_bus_lock(desc); 1094 raw_spin_lock_irq(&desc->lock); 1095 1096 /* 1097 * Implausible though it may be we need to protect us against 1098 * the following scenario: 1099 * 1100 * The thread is faster done than the hard interrupt handler 1101 * on the other CPU. If we unmask the irq line then the 1102 * interrupt can come in again and masks the line, leaves due 1103 * to IRQS_INPROGRESS and the irq line is masked forever. 1104 * 1105 * This also serializes the state of shared oneshot handlers 1106 * versus "desc->threads_oneshot |= action->thread_mask;" in 1107 * irq_wake_thread(). See the comment there which explains the 1108 * serialization. 1109 */ 1110 if (unlikely(irqd_irq_inprogress(&desc->irq_data))) { 1111 raw_spin_unlock_irq(&desc->lock); 1112 chip_bus_sync_unlock(desc); 1113 cpu_relax(); 1114 goto again; 1115 } 1116 1117 /* 1118 * Now check again, whether the thread should run. Otherwise 1119 * we would clear the threads_oneshot bit of this thread which 1120 * was just set. 1121 */ 1122 if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags)) 1123 goto out_unlock; 1124 1125 desc->threads_oneshot &= ~action->thread_mask; 1126 1127 if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) && 1128 irqd_irq_masked(&desc->irq_data)) 1129 unmask_threaded_irq(desc); 1130 1131 out_unlock: 1132 raw_spin_unlock_irq(&desc->lock); 1133 chip_bus_sync_unlock(desc); 1134 } 1135 1136 /* 1137 * Interrupts explicitly requested as threaded interrupts want to be 1138 * preemptible - many of them need to sleep and wait for slow busses to 1139 * complete. 1140 */ 1141 static irqreturn_t irq_thread_fn(struct irq_desc *desc, struct irqaction *action) 1142 { 1143 irqreturn_t ret = action->thread_fn(action->irq, action->dev_id); 1144 1145 if (ret == IRQ_HANDLED) 1146 atomic_inc(&desc->threads_handled); 1147 1148 irq_finalize_oneshot(desc, action); 1149 return ret; 1150 } 1151 1152 /* 1153 * Interrupts which are not explicitly requested as threaded 1154 * interrupts rely on the implicit bh/preempt disable of the hard irq 1155 * context. So we need to disable bh here to avoid deadlocks and other 1156 * side effects. 1157 */ 1158 static irqreturn_t irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action) 1159 { 1160 irqreturn_t ret; 1161 1162 local_bh_disable(); 1163 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) 1164 local_irq_disable(); 1165 ret = irq_thread_fn(desc, action); 1166 if (!IS_ENABLED(CONFIG_PREEMPT_RT)) 1167 local_irq_enable(); 1168 local_bh_enable(); 1169 return ret; 1170 } 1171 1172 void wake_threads_waitq(struct irq_desc *desc) 1173 { 1174 if (atomic_dec_and_test(&desc->threads_active)) 1175 wake_up(&desc->wait_for_threads); 1176 } 1177 1178 static void irq_thread_dtor(struct callback_head *unused) 1179 { 1180 struct task_struct *tsk = current; 1181 struct irq_desc *desc; 1182 struct irqaction *action; 1183 1184 if (WARN_ON_ONCE(!(current->flags & PF_EXITING))) 1185 return; 1186 1187 action = kthread_data(tsk); 1188 1189 pr_err("exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n", 1190 tsk->comm, tsk->pid, action->irq); 1191 1192 1193 desc = irq_to_desc(action->irq); 1194 /* 1195 * If IRQTF_RUNTHREAD is set, we need to decrement 1196 * desc->threads_active and wake possible waiters. 1197 */ 1198 if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags)) 1199 wake_threads_waitq(desc); 1200 1201 /* Prevent a stale desc->threads_oneshot */ 1202 irq_finalize_oneshot(desc, action); 1203 } 1204 1205 static void irq_wake_secondary(struct irq_desc *desc, struct irqaction *action) 1206 { 1207 struct irqaction *secondary = action->secondary; 1208 1209 if (WARN_ON_ONCE(!secondary)) 1210 return; 1211 1212 guard(raw_spinlock_irq)(&desc->lock); 1213 __irq_wake_thread(desc, secondary); 1214 } 1215 1216 /* 1217 * Internal function to notify that a interrupt thread is ready. 1218 */ 1219 static void irq_thread_set_ready(struct irq_desc *desc, 1220 struct irqaction *action) 1221 { 1222 set_bit(IRQTF_READY, &action->thread_flags); 1223 wake_up(&desc->wait_for_threads); 1224 } 1225 1226 /* 1227 * Internal function to wake up a interrupt thread and wait until it is 1228 * ready. 1229 */ 1230 static void wake_up_and_wait_for_irq_thread_ready(struct irq_desc *desc, 1231 struct irqaction *action) 1232 { 1233 if (!action || !action->thread) 1234 return; 1235 1236 wake_up_process(action->thread); 1237 wait_event(desc->wait_for_threads, 1238 test_bit(IRQTF_READY, &action->thread_flags)); 1239 } 1240 1241 /* 1242 * Interrupt handler thread 1243 */ 1244 static int irq_thread(void *data) 1245 { 1246 struct callback_head on_exit_work; 1247 struct irqaction *action = data; 1248 struct irq_desc *desc = irq_to_desc(action->irq); 1249 irqreturn_t (*handler_fn)(struct irq_desc *desc, 1250 struct irqaction *action); 1251 1252 irq_thread_set_ready(desc, action); 1253 1254 if (action->handler == irq_forced_secondary_handler) 1255 sched_set_fifo_secondary(current); 1256 else 1257 sched_set_fifo(current); 1258 1259 if (force_irqthreads() && test_bit(IRQTF_FORCED_THREAD, 1260 &action->thread_flags)) 1261 handler_fn = irq_forced_thread_fn; 1262 else 1263 handler_fn = irq_thread_fn; 1264 1265 init_task_work(&on_exit_work, irq_thread_dtor); 1266 task_work_add(current, &on_exit_work, TWA_NONE); 1267 1268 while (!irq_wait_for_interrupt(desc, action)) { 1269 irqreturn_t action_ret; 1270 1271 action_ret = handler_fn(desc, action); 1272 if (action_ret == IRQ_WAKE_THREAD) 1273 irq_wake_secondary(desc, action); 1274 1275 wake_threads_waitq(desc); 1276 } 1277 1278 /* 1279 * This is the regular exit path. __free_irq() is stopping the 1280 * thread via kthread_stop() after calling 1281 * synchronize_hardirq(). So neither IRQTF_RUNTHREAD nor the 1282 * oneshot mask bit can be set. 1283 */ 1284 task_work_cancel_func(current, irq_thread_dtor); 1285 return 0; 1286 } 1287 1288 /** 1289 * irq_wake_thread - wake the irq thread for the action identified by dev_id 1290 * @irq: Interrupt line 1291 * @dev_id: Device identity for which the thread should be woken 1292 */ 1293 void irq_wake_thread(unsigned int irq, void *dev_id) 1294 { 1295 struct irq_desc *desc = irq_to_desc(irq); 1296 struct irqaction *action; 1297 1298 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc))) 1299 return; 1300 1301 guard(raw_spinlock_irqsave)(&desc->lock); 1302 for_each_action_of_desc(desc, action) { 1303 if (action->dev_id == dev_id) { 1304 if (action->thread) 1305 __irq_wake_thread(desc, action); 1306 break; 1307 } 1308 } 1309 } 1310 EXPORT_SYMBOL_GPL(irq_wake_thread); 1311 1312 static int irq_setup_forced_threading(struct irqaction *new) 1313 { 1314 if (!force_irqthreads()) 1315 return 0; 1316 if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT)) 1317 return 0; 1318 1319 /* 1320 * No further action required for interrupts which are requested as 1321 * threaded interrupts already 1322 */ 1323 if (new->handler == irq_default_primary_handler) 1324 return 0; 1325 1326 new->flags |= IRQF_ONESHOT; 1327 1328 /* 1329 * Handle the case where we have a real primary handler and a 1330 * thread handler. We force thread them as well by creating a 1331 * secondary action. 1332 */ 1333 if (new->handler && new->thread_fn) { 1334 /* Allocate the secondary action */ 1335 new->secondary = kzalloc_obj(struct irqaction); 1336 if (!new->secondary) 1337 return -ENOMEM; 1338 new->secondary->handler = irq_forced_secondary_handler; 1339 new->secondary->thread_fn = new->thread_fn; 1340 new->secondary->dev_id = new->dev_id; 1341 new->secondary->irq = new->irq; 1342 new->secondary->name = new->name; 1343 } 1344 /* Deal with the primary handler */ 1345 set_bit(IRQTF_FORCED_THREAD, &new->thread_flags); 1346 new->thread_fn = new->handler; 1347 new->handler = irq_default_primary_handler; 1348 return 0; 1349 } 1350 1351 static int irq_request_resources(struct irq_desc *desc) 1352 { 1353 struct irq_data *d = &desc->irq_data; 1354 struct irq_chip *c = d->chip; 1355 1356 return c->irq_request_resources ? c->irq_request_resources(d) : 0; 1357 } 1358 1359 static void irq_release_resources(struct irq_desc *desc) 1360 { 1361 struct irq_data *d = &desc->irq_data; 1362 struct irq_chip *c = d->chip; 1363 1364 if (c->irq_release_resources) 1365 c->irq_release_resources(d); 1366 } 1367 1368 static bool irq_supports_nmi(struct irq_desc *desc) 1369 { 1370 struct irq_data *d = irq_desc_get_irq_data(desc); 1371 1372 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY 1373 /* Only IRQs directly managed by the root irqchip can be set as NMI */ 1374 if (d->parent_data) 1375 return false; 1376 #endif 1377 /* Don't support NMIs for chips behind a slow bus */ 1378 if (d->chip->irq_bus_lock || d->chip->irq_bus_sync_unlock) 1379 return false; 1380 1381 return d->chip->flags & IRQCHIP_SUPPORTS_NMI; 1382 } 1383 1384 static int irq_nmi_setup(struct irq_desc *desc) 1385 { 1386 struct irq_data *d = irq_desc_get_irq_data(desc); 1387 struct irq_chip *c = d->chip; 1388 1389 return c->irq_nmi_setup ? c->irq_nmi_setup(d) : -EINVAL; 1390 } 1391 1392 static void irq_nmi_teardown(struct irq_desc *desc) 1393 { 1394 struct irq_data *d = irq_desc_get_irq_data(desc); 1395 struct irq_chip *c = d->chip; 1396 1397 if (c->irq_nmi_teardown) 1398 c->irq_nmi_teardown(d); 1399 } 1400 1401 static int 1402 setup_irq_thread(struct irqaction *new, unsigned int irq, bool secondary) 1403 { 1404 struct task_struct *t; 1405 1406 if (!secondary) { 1407 t = kthread_create(irq_thread, new, "irq/%d-%s", irq, 1408 new->name); 1409 } else { 1410 t = kthread_create(irq_thread, new, "irq/%d-s-%s", irq, 1411 new->name); 1412 } 1413 1414 if (IS_ERR(t)) 1415 return PTR_ERR(t); 1416 1417 /* 1418 * We keep the reference to the task struct even if 1419 * the thread dies to avoid that the interrupt code 1420 * references an already freed task_struct. 1421 */ 1422 new->thread = get_task_struct(t); 1423 1424 /* 1425 * The affinity can not be established yet, but it will be once the 1426 * interrupt is enabled. Delay and defer the actual setting to the 1427 * thread itself once it is ready to run. In the meantime, prevent 1428 * it from ever being re-affined directly by cpuset or 1429 * housekeeping. The proper way to do it is to re-affine the whole 1430 * vector. 1431 */ 1432 kthread_bind_mask(t, cpu_possible_mask); 1433 1434 /* 1435 * Ensure the thread adjusts the affinity once it reaches the 1436 * thread function. 1437 */ 1438 set_bit(IRQTF_AFFINITY, &new->thread_flags); 1439 1440 return 0; 1441 } 1442 1443 static bool valid_percpu_irqaction(struct irqaction *old, struct irqaction *new) 1444 { 1445 do { 1446 if (cpumask_intersects(old->affinity, new->affinity) || 1447 old->percpu_dev_id == new->percpu_dev_id) 1448 return false; 1449 1450 old = old->next; 1451 } while (old); 1452 1453 return true; 1454 } 1455 1456 /* 1457 * Internal function to register an irqaction - typically used to 1458 * allocate special interrupts that are part of the architecture. 1459 * 1460 * Locking rules: 1461 * 1462 * desc->request_mutex Provides serialization against a concurrent free_irq() 1463 * chip_bus_lock Provides serialization for slow bus operations 1464 * desc->lock Provides serialization against hard interrupts 1465 * 1466 * chip_bus_lock and desc->lock are sufficient for all other management and 1467 * interrupt related functions. desc->request_mutex solely serializes 1468 * request/free_irq(). 1469 */ 1470 static int 1471 __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new) 1472 { 1473 struct irqaction *old, **old_ptr; 1474 unsigned long flags, thread_mask = 0; 1475 int ret, nested, shared = 0; 1476 bool per_cpu_devid; 1477 1478 if (!desc) 1479 return -EINVAL; 1480 1481 if (desc->irq_data.chip == &no_irq_chip) 1482 return -ENOSYS; 1483 if (!try_module_get(desc->owner)) 1484 return -ENODEV; 1485 1486 per_cpu_devid = irq_settings_is_per_cpu_devid(desc); 1487 1488 new->irq = irq; 1489 1490 /* 1491 * If the trigger type is not specified by the caller, 1492 * then use the default for this interrupt. 1493 */ 1494 if (!(new->flags & IRQF_TRIGGER_MASK)) 1495 new->flags |= irqd_get_trigger_type(&desc->irq_data); 1496 1497 /* 1498 * IRQF_ONESHOT means the interrupt source in the IRQ chip will be 1499 * masked until the threaded handled is done. If there is no thread 1500 * handler then it makes no sense to have IRQF_ONESHOT. 1501 */ 1502 WARN_ON_ONCE(new->flags & IRQF_ONESHOT && !new->thread_fn); 1503 1504 /* 1505 * Check whether the interrupt nests into another interrupt 1506 * thread. 1507 */ 1508 nested = irq_settings_is_nested_thread(desc); 1509 if (nested) { 1510 if (!new->thread_fn) { 1511 ret = -EINVAL; 1512 goto out_mput; 1513 } 1514 /* 1515 * Replace the primary handler which was provided from 1516 * the driver for non nested interrupt handling by the 1517 * dummy function which warns when called. 1518 */ 1519 new->handler = irq_nested_primary_handler; 1520 } else { 1521 if (irq_settings_can_thread(desc)) { 1522 ret = irq_setup_forced_threading(new); 1523 if (ret) 1524 goto out_mput; 1525 } 1526 } 1527 1528 /* 1529 * Create a handler thread when a thread function is supplied 1530 * and the interrupt does not nest into another interrupt 1531 * thread. 1532 */ 1533 if (new->thread_fn && !nested) { 1534 ret = setup_irq_thread(new, irq, false); 1535 if (ret) 1536 goto out_mput; 1537 if (new->secondary) { 1538 ret = setup_irq_thread(new->secondary, irq, true); 1539 if (ret) 1540 goto out_thread; 1541 } 1542 } 1543 1544 /* 1545 * Drivers are often written to work w/o knowledge about the 1546 * underlying irq chip implementation, so a request for a 1547 * threaded irq without a primary hard irq context handler 1548 * requires the ONESHOT flag to be set. Some irq chips like 1549 * MSI based interrupts are per se one shot safe. Check the 1550 * chip flags, so we can avoid the unmask dance at the end of 1551 * the threaded handler for those. 1552 */ 1553 if (desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE) 1554 new->flags &= ~IRQF_ONESHOT; 1555 1556 /* 1557 * Protects against a concurrent __free_irq() call which might wait 1558 * for synchronize_hardirq() to complete without holding the optional 1559 * chip bus lock and desc->lock. Also protects against handing out 1560 * a recycled oneshot thread_mask bit while it's still in use by 1561 * its previous owner. 1562 */ 1563 mutex_lock(&desc->request_mutex); 1564 1565 /* 1566 * Acquire bus lock as the irq_request_resources() callback below 1567 * might rely on the serialization or the magic power management 1568 * functions which are abusing the irq_bus_lock() callback, 1569 */ 1570 chip_bus_lock(desc); 1571 1572 /* First installed action requests resources. */ 1573 if (!desc->action) { 1574 ret = irq_request_resources(desc); 1575 if (ret) { 1576 pr_err("Failed to request resources for %s (irq %d) on irqchip %s\n", 1577 new->name, irq, desc->irq_data.chip->name); 1578 goto out_bus_unlock; 1579 } 1580 } 1581 1582 /* 1583 * The following block of code has to be executed atomically 1584 * protected against a concurrent interrupt and any of the other 1585 * management calls which are not serialized via 1586 * desc->request_mutex or the optional bus lock. 1587 */ 1588 raw_spin_lock_irqsave(&desc->lock, flags); 1589 old_ptr = &desc->action; 1590 old = *old_ptr; 1591 if (old) { 1592 /* 1593 * Can't share interrupts unless both agree to and are 1594 * the same type (level, edge, polarity). So both flag 1595 * fields must have IRQF_SHARED set and the bits which 1596 * set the trigger type must match. Also all must 1597 * agree on ONESHOT. 1598 * Interrupt lines used for NMIs cannot be shared. 1599 */ 1600 unsigned int oldtype; 1601 1602 if (irq_is_nmi(desc) && !per_cpu_devid) { 1603 pr_err("Invalid attempt to share NMI for %s (irq %d) on irqchip %s.\n", 1604 new->name, irq, desc->irq_data.chip->name); 1605 ret = -EINVAL; 1606 goto out_unlock; 1607 } 1608 1609 if (per_cpu_devid && !valid_percpu_irqaction(old, new)) { 1610 pr_err("Overlapping affinities for %s (irq %d) on irqchip %s.\n", 1611 new->name, irq, desc->irq_data.chip->name); 1612 ret = -EINVAL; 1613 goto out_unlock; 1614 } 1615 1616 /* 1617 * If nobody did set the configuration before, inherit 1618 * the one provided by the requester. 1619 */ 1620 if (irqd_trigger_type_was_set(&desc->irq_data)) { 1621 oldtype = irqd_get_trigger_type(&desc->irq_data); 1622 } else { 1623 oldtype = new->flags & IRQF_TRIGGER_MASK; 1624 irqd_set_trigger_type(&desc->irq_data, oldtype); 1625 } 1626 1627 if (!((old->flags & new->flags) & IRQF_SHARED) || 1628 (oldtype != (new->flags & IRQF_TRIGGER_MASK))) 1629 goto mismatch; 1630 1631 if ((old->flags & IRQF_ONESHOT) && 1632 (new->flags & IRQF_COND_ONESHOT)) 1633 new->flags |= IRQF_ONESHOT; 1634 else if ((old->flags ^ new->flags) & IRQF_ONESHOT) 1635 goto mismatch; 1636 1637 /* All handlers must agree on per-cpuness */ 1638 if ((old->flags & IRQF_PERCPU) != 1639 (new->flags & IRQF_PERCPU)) 1640 goto mismatch; 1641 1642 /* add new interrupt at end of irq queue */ 1643 do { 1644 /* 1645 * Or all existing action->thread_mask bits, 1646 * so we can find the next zero bit for this 1647 * new action. 1648 */ 1649 thread_mask |= old->thread_mask; 1650 old_ptr = &old->next; 1651 old = *old_ptr; 1652 } while (old); 1653 shared = 1; 1654 } 1655 1656 /* 1657 * Setup the thread mask for this irqaction for ONESHOT. For 1658 * !ONESHOT irqs the thread mask is 0 so we can avoid a 1659 * conditional in irq_wake_thread(). 1660 */ 1661 if (new->flags & IRQF_ONESHOT) { 1662 /* 1663 * Unlikely to have 32 resp 64 irqs sharing one line, 1664 * but who knows. 1665 */ 1666 if (thread_mask == ~0UL) { 1667 ret = -EBUSY; 1668 goto out_unlock; 1669 } 1670 /* 1671 * The thread_mask for the action is or'ed to 1672 * desc->thread_active to indicate that the 1673 * IRQF_ONESHOT thread handler has been woken, but not 1674 * yet finished. The bit is cleared when a thread 1675 * completes. When all threads of a shared interrupt 1676 * line have completed desc->threads_active becomes 1677 * zero and the interrupt line is unmasked. See 1678 * handle.c:irq_wake_thread() for further information. 1679 * 1680 * If no thread is woken by primary (hard irq context) 1681 * interrupt handlers, then desc->threads_active is 1682 * also checked for zero to unmask the irq line in the 1683 * affected hard irq flow handlers 1684 * (handle_[fasteoi|level]_irq). 1685 * 1686 * The new action gets the first zero bit of 1687 * thread_mask assigned. See the loop above which or's 1688 * all existing action->thread_mask bits. 1689 */ 1690 new->thread_mask = 1UL << ffz(thread_mask); 1691 1692 } else if (new->handler == irq_default_primary_handler && 1693 !(desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)) { 1694 /* 1695 * The interrupt was requested with handler = NULL, so 1696 * we use the default primary handler for it. But it 1697 * does not have the oneshot flag set. In combination 1698 * with level interrupts this is deadly, because the 1699 * default primary handler just wakes the thread, then 1700 * the irq lines is reenabled, but the device still 1701 * has the level irq asserted. Rinse and repeat.... 1702 * 1703 * While this works for edge type interrupts, we play 1704 * it safe and reject unconditionally because we can't 1705 * say for sure which type this interrupt really 1706 * has. The type flags are unreliable as the 1707 * underlying chip implementation can override them. 1708 */ 1709 pr_err("Threaded irq requested with handler=NULL and !ONESHOT for %s (irq %d)\n", 1710 new->name, irq); 1711 ret = -EINVAL; 1712 goto out_unlock; 1713 } 1714 1715 if (!shared) { 1716 /* Setup the type (level, edge polarity) if configured: */ 1717 if (new->flags & IRQF_TRIGGER_MASK) { 1718 ret = __irq_set_trigger(desc, 1719 new->flags & IRQF_TRIGGER_MASK); 1720 1721 if (ret) 1722 goto out_unlock; 1723 } 1724 1725 /* 1726 * Activate the interrupt. That activation must happen 1727 * independently of IRQ_NOAUTOEN. request_irq() can fail 1728 * and the callers are supposed to handle 1729 * that. enable_irq() of an interrupt requested with 1730 * IRQ_NOAUTOEN is not supposed to fail. The activation 1731 * keeps it in shutdown mode, it merily associates 1732 * resources if necessary and if that's not possible it 1733 * fails. Interrupts which are in managed shutdown mode 1734 * will simply ignore that activation request. 1735 */ 1736 ret = irq_activate(desc); 1737 if (ret) 1738 goto out_unlock; 1739 1740 desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \ 1741 IRQS_ONESHOT | IRQS_WAITING); 1742 irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS); 1743 1744 if (new->flags & IRQF_PERCPU) { 1745 irqd_set(&desc->irq_data, IRQD_PER_CPU); 1746 irq_settings_set_per_cpu(desc); 1747 if (new->flags & IRQF_NO_DEBUG) 1748 irq_settings_set_no_debug(desc); 1749 } 1750 1751 if (noirqdebug) 1752 irq_settings_set_no_debug(desc); 1753 1754 if (new->flags & IRQF_ONESHOT) 1755 desc->istate |= IRQS_ONESHOT; 1756 1757 /* Exclude IRQ from balancing if requested */ 1758 if (new->flags & IRQF_NOBALANCING) { 1759 irq_settings_set_no_balancing(desc); 1760 irqd_set(&desc->irq_data, IRQD_NO_BALANCING); 1761 } 1762 1763 if (!(new->flags & IRQF_NO_AUTOEN) && 1764 irq_settings_can_autoenable(desc)) { 1765 irq_startup(desc, IRQ_RESEND, IRQ_START_COND); 1766 } else if (!per_cpu_devid) { 1767 /* 1768 * Shared interrupts do not go well with disabling 1769 * auto enable. The sharing interrupt might request 1770 * it while it's still disabled and then wait for 1771 * interrupts forever. 1772 */ 1773 WARN_ON_ONCE(new->flags & IRQF_SHARED); 1774 /* Undo nested disables: */ 1775 desc->depth = 1; 1776 } 1777 1778 } else if (new->flags & IRQF_TRIGGER_MASK) { 1779 unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK; 1780 unsigned int omsk = irqd_get_trigger_type(&desc->irq_data); 1781 1782 if (nmsk != omsk) 1783 /* hope the handler works with current trigger mode */ 1784 pr_warn("irq %d uses trigger mode %u; requested %u\n", 1785 irq, omsk, nmsk); 1786 } 1787 1788 *old_ptr = new; 1789 1790 irq_pm_install_action(desc, new); 1791 1792 /* Reset broken irq detection when installing new handler */ 1793 desc->irq_count = 0; 1794 desc->irqs_unhandled = 0; 1795 1796 /* 1797 * Check whether we disabled the irq via the spurious handler 1798 * before. Reenable it and give it another chance. 1799 */ 1800 if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) { 1801 desc->istate &= ~IRQS_SPURIOUS_DISABLED; 1802 __enable_irq(desc); 1803 } 1804 1805 irq_proc_update_valid(desc); 1806 raw_spin_unlock_irqrestore(&desc->lock, flags); 1807 chip_bus_sync_unlock(desc); 1808 mutex_unlock(&desc->request_mutex); 1809 1810 wake_up_and_wait_for_irq_thread_ready(desc, new); 1811 wake_up_and_wait_for_irq_thread_ready(desc, new->secondary); 1812 1813 register_irq_proc(irq, desc); 1814 new->dir = NULL; 1815 register_handler_proc(irq, new); 1816 return 0; 1817 1818 mismatch: 1819 if (!(new->flags & IRQF_PROBE_SHARED)) { 1820 pr_err("Flags mismatch irq %d. %08x (%s) vs. %08x (%s)\n", 1821 irq, new->flags, new->name, old->flags, old->name); 1822 #ifdef CONFIG_DEBUG_SHIRQ 1823 dump_stack(); 1824 #endif 1825 } 1826 ret = -EBUSY; 1827 1828 out_unlock: 1829 raw_spin_unlock_irqrestore(&desc->lock, flags); 1830 1831 if (!desc->action) 1832 irq_release_resources(desc); 1833 out_bus_unlock: 1834 chip_bus_sync_unlock(desc); 1835 mutex_unlock(&desc->request_mutex); 1836 1837 out_thread: 1838 if (new->thread) { 1839 struct task_struct *t = new->thread; 1840 1841 new->thread = NULL; 1842 kthread_stop_put(t); 1843 } 1844 if (new->secondary && new->secondary->thread) { 1845 struct task_struct *t = new->secondary->thread; 1846 1847 new->secondary->thread = NULL; 1848 kthread_stop_put(t); 1849 } 1850 out_mput: 1851 module_put(desc->owner); 1852 return ret; 1853 } 1854 1855 /* 1856 * Internal function to unregister an irqaction - used to free 1857 * regular and special interrupts that are part of the architecture. 1858 */ 1859 static struct irqaction *__free_irq(struct irq_desc *desc, void *dev_id) 1860 { 1861 unsigned irq = desc->irq_data.irq; 1862 struct irqaction *action, **action_ptr; 1863 unsigned long flags; 1864 1865 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq); 1866 1867 mutex_lock(&desc->request_mutex); 1868 chip_bus_lock(desc); 1869 raw_spin_lock_irqsave(&desc->lock, flags); 1870 1871 /* 1872 * There can be multiple actions per IRQ descriptor, find the right 1873 * one based on the dev_id: 1874 */ 1875 action_ptr = &desc->action; 1876 for (;;) { 1877 action = *action_ptr; 1878 1879 if (!action) { 1880 WARN(1, "Trying to free already-free IRQ %d\n", irq); 1881 raw_spin_unlock_irqrestore(&desc->lock, flags); 1882 chip_bus_sync_unlock(desc); 1883 mutex_unlock(&desc->request_mutex); 1884 return NULL; 1885 } 1886 1887 if (action->dev_id == dev_id) 1888 break; 1889 action_ptr = &action->next; 1890 } 1891 1892 /* Found it - now remove it from the list of entries: */ 1893 *action_ptr = action->next; 1894 1895 irq_pm_remove_action(desc, action); 1896 1897 /* If this was the last handler, shut down the IRQ line: */ 1898 if (!desc->action) { 1899 irq_settings_clr_disable_unlazy(desc); 1900 /* Only shutdown. Deactivate after synchronize_hardirq() */ 1901 irq_shutdown(desc); 1902 } 1903 1904 #ifdef CONFIG_SMP 1905 /* make sure affinity_hint is cleaned up */ 1906 if (WARN_ON_ONCE(desc->affinity_hint)) 1907 desc->affinity_hint = NULL; 1908 #endif 1909 1910 irq_proc_update_valid(desc); 1911 raw_spin_unlock_irqrestore(&desc->lock, flags); 1912 /* 1913 * Drop bus_lock here so the changes which were done in the chip 1914 * callbacks above are synced out to the irq chips which hang 1915 * behind a slow bus (I2C, SPI) before calling synchronize_hardirq(). 1916 * 1917 * Aside of that the bus_lock can also be taken from the threaded 1918 * handler in irq_finalize_oneshot() which results in a deadlock 1919 * because kthread_stop() would wait forever for the thread to 1920 * complete, which is blocked on the bus lock. 1921 * 1922 * The still held desc->request_mutex() protects against a 1923 * concurrent request_irq() of this irq so the release of resources 1924 * and timing data is properly serialized. 1925 */ 1926 chip_bus_sync_unlock(desc); 1927 1928 unregister_handler_proc(irq, action); 1929 1930 /* 1931 * Make sure it's not being used on another CPU and if the chip 1932 * supports it also make sure that there is no (not yet serviced) 1933 * interrupt in flight at the hardware level. 1934 */ 1935 __synchronize_irq(desc); 1936 1937 #ifdef CONFIG_DEBUG_SHIRQ 1938 /* 1939 * It's a shared IRQ -- the driver ought to be prepared for an IRQ 1940 * event to happen even now it's being freed, so let's make sure that 1941 * is so by doing an extra call to the handler .... 1942 * 1943 * ( We do this after actually deregistering it, to make sure that a 1944 * 'real' IRQ doesn't run in parallel with our fake. ) 1945 */ 1946 if (action->flags & IRQF_SHARED) { 1947 local_irq_save(flags); 1948 action->handler(irq, dev_id); 1949 local_irq_restore(flags); 1950 } 1951 #endif 1952 1953 /* 1954 * The action has already been removed above, but the thread writes 1955 * its oneshot mask bit when it completes. Though request_mutex is 1956 * held across this which prevents __setup_irq() from handing out 1957 * the same bit to a newly requested action. 1958 */ 1959 if (action->thread) { 1960 kthread_stop_put(action->thread); 1961 if (action->secondary && action->secondary->thread) 1962 kthread_stop_put(action->secondary->thread); 1963 } 1964 1965 /* Last action releases resources */ 1966 if (!desc->action) { 1967 /* 1968 * Reacquire bus lock as irq_release_resources() might 1969 * require it to deallocate resources over the slow bus. 1970 */ 1971 chip_bus_lock(desc); 1972 /* 1973 * There is no interrupt on the fly anymore. Deactivate it 1974 * completely. 1975 */ 1976 scoped_guard(raw_spinlock_irqsave, &desc->lock) 1977 irq_domain_deactivate_irq(&desc->irq_data); 1978 1979 irq_release_resources(desc); 1980 chip_bus_sync_unlock(desc); 1981 } 1982 1983 mutex_unlock(&desc->request_mutex); 1984 1985 irq_chip_pm_put(&desc->irq_data); 1986 module_put(desc->owner); 1987 kfree(action->secondary); 1988 return action; 1989 } 1990 1991 /** 1992 * free_irq - free an interrupt allocated with request_irq 1993 * @irq: Interrupt line to free 1994 * @dev_id: Device identity to free 1995 * 1996 * Remove an interrupt handler. The handler is removed and if the interrupt 1997 * line is no longer in use by any driver it is disabled. On a shared IRQ 1998 * the caller must ensure the interrupt is disabled on the card it drives 1999 * before calling this function. The function does not return until any 2000 * executing interrupts for this IRQ have completed. 2001 * 2002 * This function must not be called from interrupt context. 2003 * 2004 * Returns the devname argument passed to request_irq. 2005 */ 2006 const void *free_irq(unsigned int irq, void *dev_id) 2007 { 2008 struct irq_desc *desc = irq_to_desc(irq); 2009 struct irqaction *action; 2010 const char *devname; 2011 2012 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc))) 2013 return NULL; 2014 2015 #ifdef CONFIG_SMP 2016 if (WARN_ON(desc->affinity_notify)) 2017 desc->affinity_notify = NULL; 2018 #endif 2019 2020 action = __free_irq(desc, dev_id); 2021 2022 if (!action) 2023 return NULL; 2024 2025 devname = action->name; 2026 kfree(action); 2027 return devname; 2028 } 2029 EXPORT_SYMBOL(free_irq); 2030 2031 static const void *__cleanup_nmi(unsigned int irq, struct irq_desc *desc) 2032 { 2033 struct irqaction *action = NULL; 2034 const char *devname = NULL; 2035 2036 scoped_guard(raw_spinlock_irqsave, &desc->lock) { 2037 irq_nmi_teardown(desc); 2038 2039 desc->istate &= ~IRQS_NMI; 2040 2041 if (!WARN_ON(desc->action == NULL)) { 2042 action = desc->action; 2043 irq_pm_remove_action(desc, action); 2044 devname = action->name; 2045 } 2046 desc->action = NULL; 2047 2048 irq_settings_clr_disable_unlazy(desc); 2049 irq_shutdown_and_deactivate(desc); 2050 } 2051 2052 irq_proc_update_valid(desc); 2053 2054 if (action) 2055 unregister_handler_proc(irq, action); 2056 kfree(action); 2057 2058 irq_release_resources(desc); 2059 2060 irq_chip_pm_put(&desc->irq_data); 2061 module_put(desc->owner); 2062 2063 return devname; 2064 } 2065 2066 const void *free_nmi(unsigned int irq, void *dev_id) 2067 { 2068 struct irq_desc *desc = irq_to_desc(irq); 2069 2070 if (!desc || WARN_ON(!irq_is_nmi(desc))) 2071 return NULL; 2072 2073 if (WARN_ON(irq_settings_is_per_cpu_devid(desc))) 2074 return NULL; 2075 2076 /* NMI still enabled */ 2077 if (WARN_ON(desc->depth == 0)) 2078 disable_nmi_nosync(irq); 2079 2080 return __cleanup_nmi(irq, desc); 2081 } 2082 2083 /** 2084 * request_threaded_irq - allocate an interrupt line 2085 * @irq: Interrupt line to allocate 2086 * @handler: Function to be called when the IRQ occurs. 2087 * Primary handler for threaded interrupts. 2088 * If handler is NULL and thread_fn != NULL 2089 * the default primary handler is installed. 2090 * @thread_fn: Function called from the irq handler thread 2091 * If NULL, no irq thread is created 2092 * @irqflags: Interrupt type flags 2093 * @devname: An ascii name for the claiming device 2094 * @dev_id: A cookie passed back to the handler function 2095 * 2096 * This call allocates interrupt resources and enables the interrupt line 2097 * and IRQ handling. From the point this call is made your handler function 2098 * may be invoked. Since your handler function must clear any interrupt the 2099 * board raises, you must take care both to initialise your hardware and to 2100 * set up the interrupt handler in the right order. 2101 * 2102 * If you want to set up a threaded irq handler for your device then you 2103 * need to supply @handler and @thread_fn. @handler is still called in hard 2104 * interrupt context and has to check whether the interrupt originates from 2105 * the device. If yes it needs to disable the interrupt on the device and 2106 * return IRQ_WAKE_THREAD which will wake up the handler thread and run 2107 * @thread_fn. This split handler design is necessary to support shared 2108 * interrupts. 2109 * 2110 * @dev_id must be globally unique. Normally the address of the device data 2111 * structure is used as the cookie. Since the handler receives this value 2112 * it makes sense to use it. 2113 * 2114 * If your interrupt is shared you must pass a non NULL dev_id as this is 2115 * required when freeing the interrupt. 2116 * 2117 * Flags: 2118 * 2119 * IRQF_SHARED Interrupt is shared 2120 * IRQF_TRIGGER_* Specify active edge(s) or level 2121 * IRQF_ONESHOT Run thread_fn with interrupt line masked 2122 */ 2123 int request_threaded_irq(unsigned int irq, irq_handler_t handler, 2124 irq_handler_t thread_fn, unsigned long irqflags, 2125 const char *devname, void *dev_id) 2126 { 2127 struct irqaction *action; 2128 struct irq_desc *desc; 2129 int retval; 2130 2131 if (irq == IRQ_NOTCONNECTED) 2132 return -ENOTCONN; 2133 2134 /* 2135 * Sanity-check: shared interrupts must pass in a real dev-ID, 2136 * otherwise we'll have trouble later trying to figure out 2137 * which interrupt is which (messes up the interrupt freeing 2138 * logic etc). 2139 * 2140 * Also shared interrupts do not go well with disabling auto enable. 2141 * The sharing interrupt might request it while it's still disabled 2142 * and then wait for interrupts forever. 2143 * 2144 * Also IRQF_COND_SUSPEND only makes sense for shared interrupts and 2145 * it cannot be set along with IRQF_NO_SUSPEND. 2146 */ 2147 if (((irqflags & IRQF_SHARED) && !dev_id) || 2148 ((irqflags & IRQF_SHARED) && (irqflags & IRQF_NO_AUTOEN)) || 2149 (!(irqflags & IRQF_SHARED) && (irqflags & IRQF_COND_SUSPEND)) || 2150 ((irqflags & IRQF_NO_SUSPEND) && (irqflags & IRQF_COND_SUSPEND))) 2151 return -EINVAL; 2152 2153 desc = irq_to_desc(irq); 2154 if (!desc) 2155 return -EINVAL; 2156 2157 if (!irq_settings_can_request(desc) || 2158 WARN_ON(irq_settings_is_per_cpu_devid(desc))) 2159 return -EINVAL; 2160 2161 if (!handler) { 2162 if (!thread_fn) 2163 return -EINVAL; 2164 handler = irq_default_primary_handler; 2165 } 2166 2167 action = kzalloc_obj(struct irqaction); 2168 if (!action) 2169 return -ENOMEM; 2170 2171 action->handler = handler; 2172 action->thread_fn = thread_fn; 2173 action->flags = irqflags; 2174 action->name = devname; 2175 action->dev_id = dev_id; 2176 2177 retval = irq_chip_pm_get(&desc->irq_data); 2178 if (retval < 0) { 2179 kfree(action); 2180 return retval; 2181 } 2182 2183 retval = __setup_irq(irq, desc, action); 2184 2185 if (retval) { 2186 irq_chip_pm_put(&desc->irq_data); 2187 kfree(action->secondary); 2188 kfree(action); 2189 } 2190 2191 #ifdef CONFIG_DEBUG_SHIRQ_FIXME 2192 if (!retval && (irqflags & IRQF_SHARED)) { 2193 /* 2194 * It's a shared IRQ -- the driver ought to be prepared for it 2195 * to happen immediately, so let's make sure.... 2196 * We disable the irq to make sure that a 'real' IRQ doesn't 2197 * run in parallel with our fake. 2198 */ 2199 unsigned long flags; 2200 2201 disable_irq(irq); 2202 local_irq_save(flags); 2203 2204 handler(irq, dev_id); 2205 2206 local_irq_restore(flags); 2207 enable_irq(irq); 2208 } 2209 #endif 2210 return retval; 2211 } 2212 EXPORT_SYMBOL(request_threaded_irq); 2213 2214 /** 2215 * request_any_context_irq - allocate an interrupt line 2216 * @irq: Interrupt line to allocate 2217 * @handler: Function to be called when the IRQ occurs. 2218 * Threaded handler for threaded interrupts. 2219 * @flags: Interrupt type flags 2220 * @name: An ascii name for the claiming device 2221 * @dev_id: A cookie passed back to the handler function 2222 * 2223 * This call allocates interrupt resources and enables the interrupt line 2224 * and IRQ handling. It selects either a hardirq or threaded handling 2225 * method depending on the context. 2226 * 2227 * Returns: On failure, it returns a negative value. On success, it returns either 2228 * IRQC_IS_HARDIRQ or IRQC_IS_NESTED. 2229 */ 2230 int request_any_context_irq(unsigned int irq, irq_handler_t handler, 2231 unsigned long flags, const char *name, void *dev_id) 2232 { 2233 struct irq_desc *desc; 2234 int ret; 2235 2236 if (irq == IRQ_NOTCONNECTED) 2237 return -ENOTCONN; 2238 2239 desc = irq_to_desc(irq); 2240 if (!desc) 2241 return -EINVAL; 2242 2243 if (irq_settings_is_nested_thread(desc)) { 2244 ret = request_threaded_irq(irq, NULL, handler, 2245 flags, name, dev_id); 2246 return !ret ? IRQC_IS_NESTED : ret; 2247 } 2248 2249 ret = request_irq(irq, handler, flags, name, dev_id); 2250 return !ret ? IRQC_IS_HARDIRQ : ret; 2251 } 2252 EXPORT_SYMBOL_GPL(request_any_context_irq); 2253 2254 /** 2255 * request_nmi - allocate an interrupt line for NMI delivery 2256 * @irq: Interrupt line to allocate 2257 * @handler: Function to be called when the IRQ occurs. 2258 * Threaded handler for threaded interrupts. 2259 * @irqflags: Interrupt type flags 2260 * @name: An ascii name for the claiming device 2261 * @dev_id: A cookie passed back to the handler function 2262 * 2263 * This call allocates interrupt resources and enables the interrupt line 2264 * and IRQ handling. It sets up the IRQ line to be handled as an NMI. 2265 * 2266 * An interrupt line delivering NMIs cannot be shared and IRQ handling 2267 * cannot be threaded. 2268 * 2269 * Interrupt lines requested for NMI delivering must produce per cpu 2270 * interrupts and have auto enabling setting disabled. 2271 * 2272 * @dev_id must be globally unique. Normally the address of the device data 2273 * structure is used as the cookie. Since the handler receives this value 2274 * it makes sense to use it. 2275 * 2276 * If the interrupt line cannot be used to deliver NMIs, function will fail 2277 * and return a negative value. 2278 */ 2279 int request_nmi(unsigned int irq, irq_handler_t handler, 2280 unsigned long irqflags, const char *name, void *dev_id) 2281 { 2282 struct irqaction *action; 2283 struct irq_desc *desc; 2284 int retval; 2285 2286 if (irq == IRQ_NOTCONNECTED) 2287 return -ENOTCONN; 2288 2289 /* NMI cannot be shared, used for Polling */ 2290 if (irqflags & (IRQF_SHARED | IRQF_COND_SUSPEND | IRQF_IRQPOLL)) 2291 return -EINVAL; 2292 2293 if (!(irqflags & IRQF_PERCPU)) 2294 return -EINVAL; 2295 2296 if (!handler) 2297 return -EINVAL; 2298 2299 desc = irq_to_desc(irq); 2300 2301 if (!desc || (irq_settings_can_autoenable(desc) && 2302 !(irqflags & IRQF_NO_AUTOEN)) || 2303 !irq_settings_can_request(desc) || 2304 WARN_ON(irq_settings_is_per_cpu_devid(desc)) || 2305 !irq_supports_nmi(desc)) 2306 return -EINVAL; 2307 2308 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL); 2309 if (!action) 2310 return -ENOMEM; 2311 2312 action->handler = handler; 2313 action->flags = irqflags | IRQF_NO_THREAD | IRQF_NOBALANCING; 2314 action->name = name; 2315 action->dev_id = dev_id; 2316 2317 retval = irq_chip_pm_get(&desc->irq_data); 2318 if (retval < 0) 2319 goto err_out; 2320 2321 retval = __setup_irq(irq, desc, action); 2322 if (retval) 2323 goto err_irq_setup; 2324 2325 scoped_guard(raw_spinlock_irqsave, &desc->lock) { 2326 /* Setup NMI state */ 2327 desc->istate |= IRQS_NMI; 2328 retval = irq_nmi_setup(desc); 2329 } 2330 2331 if (retval) { 2332 __cleanup_nmi(irq, desc); 2333 return -EINVAL; 2334 } 2335 return 0; 2336 2337 err_irq_setup: 2338 irq_chip_pm_put(&desc->irq_data); 2339 err_out: 2340 kfree(action); 2341 2342 return retval; 2343 } 2344 2345 void enable_percpu_irq(unsigned int irq, unsigned int type) 2346 { 2347 scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_PERCPU) { 2348 struct irq_desc *desc = scoped_irqdesc; 2349 2350 /* 2351 * If the trigger type is not specified by the caller, then 2352 * use the default for this interrupt. 2353 */ 2354 type &= IRQ_TYPE_SENSE_MASK; 2355 if (type == IRQ_TYPE_NONE) 2356 type = irqd_get_trigger_type(&desc->irq_data); 2357 2358 if (type != IRQ_TYPE_NONE) { 2359 if (__irq_set_trigger(desc, type)) { 2360 WARN(1, "failed to set type for IRQ%d\n", irq); 2361 return; 2362 } 2363 } 2364 irq_percpu_enable(desc, smp_processor_id()); 2365 } 2366 } 2367 EXPORT_SYMBOL_GPL(enable_percpu_irq); 2368 2369 void enable_percpu_nmi(unsigned int irq, unsigned int type) 2370 { 2371 enable_percpu_irq(irq, type); 2372 } 2373 2374 /** 2375 * irq_percpu_is_enabled - Check whether the per cpu irq is enabled 2376 * @irq: Linux irq number to check for 2377 * 2378 * Must be called from a non migratable context. Returns the enable 2379 * state of a per cpu interrupt on the current cpu. 2380 */ 2381 bool irq_percpu_is_enabled(unsigned int irq) 2382 { 2383 scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_PERCPU) 2384 return cpumask_test_cpu(smp_processor_id(), scoped_irqdesc->percpu_enabled); 2385 return false; 2386 } 2387 EXPORT_SYMBOL_GPL(irq_percpu_is_enabled); 2388 2389 void disable_percpu_irq(unsigned int irq) 2390 { 2391 scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_PERCPU) 2392 irq_percpu_disable(scoped_irqdesc, smp_processor_id()); 2393 } 2394 EXPORT_SYMBOL_GPL(disable_percpu_irq); 2395 2396 void disable_percpu_nmi(unsigned int irq) 2397 { 2398 disable_percpu_irq(irq); 2399 } 2400 2401 /* 2402 * Internal function to unregister a percpu irqaction. 2403 */ 2404 static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id) 2405 { 2406 struct irq_desc *desc = irq_to_desc(irq); 2407 struct irqaction *action, **action_ptr; 2408 2409 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq); 2410 2411 if (!desc) 2412 return NULL; 2413 2414 scoped_guard(raw_spinlock_irqsave, &desc->lock) { 2415 action_ptr = &desc->action; 2416 for (;;) { 2417 action = *action_ptr; 2418 2419 if (!action) { 2420 WARN(1, "Trying to free already-free IRQ %d\n", irq); 2421 return NULL; 2422 } 2423 2424 if (action->percpu_dev_id == dev_id) 2425 break; 2426 2427 action_ptr = &action->next; 2428 } 2429 2430 if (cpumask_intersects(desc->percpu_enabled, action->affinity)) { 2431 WARN(1, "percpu IRQ %d still enabled on CPU%d!\n", irq, 2432 cpumask_first_and(desc->percpu_enabled, action->affinity)); 2433 return NULL; 2434 } 2435 2436 /* Found it - now remove it from the list of entries: */ 2437 *action_ptr = action->next; 2438 2439 /* Demote from NMI if we killed the last action */ 2440 if (!desc->action) { 2441 desc->istate &= ~IRQS_NMI; 2442 irq_proc_update_valid(desc); 2443 } 2444 } 2445 2446 unregister_handler_proc(irq, action); 2447 irq_chip_pm_put(&desc->irq_data); 2448 module_put(desc->owner); 2449 return action; 2450 } 2451 2452 /** 2453 * free_percpu_irq - free an interrupt allocated with request_percpu_irq 2454 * @irq: Interrupt line to free 2455 * @dev_id: Device identity to free 2456 * 2457 * Remove a percpu interrupt handler. The handler is removed, but the 2458 * interrupt line is not disabled. This must be done on each CPU before 2459 * calling this function. The function does not return until any executing 2460 * interrupts for this IRQ have completed. 2461 * 2462 * This function must not be called from interrupt context. 2463 */ 2464 void free_percpu_irq(unsigned int irq, void __percpu *dev_id) 2465 { 2466 struct irq_desc *desc = irq_to_desc(irq); 2467 2468 if (!desc || !irq_settings_is_per_cpu_devid(desc)) 2469 return; 2470 2471 chip_bus_lock(desc); 2472 kfree(__free_percpu_irq(irq, dev_id)); 2473 chip_bus_sync_unlock(desc); 2474 } 2475 EXPORT_SYMBOL_GPL(free_percpu_irq); 2476 2477 void free_percpu_nmi(unsigned int irq, void __percpu *dev_id) 2478 { 2479 struct irq_desc *desc = irq_to_desc(irq); 2480 2481 if (!desc || !irq_settings_is_per_cpu_devid(desc)) 2482 return; 2483 2484 if (WARN_ON(!irq_is_nmi(desc))) 2485 return; 2486 2487 kfree(__free_percpu_irq(irq, dev_id)); 2488 } 2489 2490 static 2491 struct irqaction *create_percpu_irqaction(irq_handler_t handler, unsigned long flags, 2492 const char *devname, const cpumask_t *affinity, 2493 void __percpu *dev_id) 2494 { 2495 struct irqaction *action; 2496 2497 if (!affinity) 2498 affinity = cpu_possible_mask; 2499 2500 action = kzalloc_obj(struct irqaction); 2501 if (!action) 2502 return NULL; 2503 2504 action->handler = handler; 2505 action->flags = flags | IRQF_PERCPU | IRQF_NO_SUSPEND; 2506 action->name = devname; 2507 action->percpu_dev_id = dev_id; 2508 action->affinity = affinity; 2509 2510 /* 2511 * We allow some form of sharing for non-overlapping affinity 2512 * masks. Obviously, covering all CPUs prevents any sharing in 2513 * the first place. 2514 */ 2515 if (!cpumask_equal(affinity, cpu_possible_mask)) 2516 action->flags |= IRQF_SHARED; 2517 2518 return action; 2519 } 2520 2521 /** 2522 * request_percpu_irq_affinity - allocate a percpu interrupt line 2523 * @irq: Interrupt line to allocate 2524 * @handler: Function to be called when the IRQ occurs. 2525 * @devname: An ascii name for the claiming device 2526 * @affinity: A cpumask describing the target CPUs for this interrupt 2527 * @dev_id: A percpu cookie passed back to the handler function 2528 * 2529 * This call allocates interrupt resources, but doesn't enable the interrupt 2530 * on any CPU, as all percpu-devid interrupts are flagged with IRQ_NOAUTOEN. 2531 * It has to be done on each CPU using enable_percpu_irq(). 2532 * 2533 * @dev_id must be globally unique. It is a per-cpu variable, and 2534 * the handler gets called with the interrupted CPU's instance of 2535 * that variable. 2536 */ 2537 int request_percpu_irq_affinity(unsigned int irq, irq_handler_t handler, const char *devname, 2538 const cpumask_t *affinity, void __percpu *dev_id) 2539 { 2540 struct irqaction *action; 2541 struct irq_desc *desc; 2542 int retval; 2543 2544 if (!dev_id) 2545 return -EINVAL; 2546 2547 desc = irq_to_desc(irq); 2548 if (!desc || !irq_settings_can_request(desc) || 2549 !irq_settings_is_per_cpu_devid(desc)) 2550 return -EINVAL; 2551 2552 action = create_percpu_irqaction(handler, 0, devname, affinity, dev_id); 2553 if (!action) 2554 return -ENOMEM; 2555 2556 retval = irq_chip_pm_get(&desc->irq_data); 2557 if (retval < 0) { 2558 kfree(action); 2559 return retval; 2560 } 2561 2562 retval = __setup_irq(irq, desc, action); 2563 2564 if (retval) { 2565 irq_chip_pm_put(&desc->irq_data); 2566 kfree(action); 2567 } 2568 2569 return retval; 2570 } 2571 EXPORT_SYMBOL_GPL(request_percpu_irq_affinity); 2572 2573 /** 2574 * request_percpu_nmi - allocate a percpu interrupt line for NMI delivery 2575 * @irq: Interrupt line to allocate 2576 * @handler: Function to be called when the IRQ occurs. 2577 * @name: An ascii name for the claiming device 2578 * @affinity: A cpumask describing the target CPUs for this interrupt 2579 * @dev_id: A percpu cookie passed back to the handler function 2580 * 2581 * This call allocates interrupt resources for a per CPU NMI. Per CPU NMIs 2582 * have to be setup on each CPU by calling prepare_percpu_nmi() before 2583 * being enabled on the same CPU by using enable_percpu_nmi(). 2584 * 2585 * @dev_id must be globally unique. It is a per-cpu variable, and the 2586 * handler gets called with the interrupted CPU's instance of that 2587 * variable. 2588 * 2589 * Interrupt lines requested for NMI delivering should have auto enabling 2590 * setting disabled. 2591 * 2592 * If the interrupt line cannot be used to deliver NMIs, function 2593 * will fail returning a negative value. 2594 */ 2595 int request_percpu_nmi(unsigned int irq, irq_handler_t handler, const char *name, 2596 const struct cpumask *affinity, void __percpu *dev_id) 2597 { 2598 struct irqaction *action; 2599 struct irq_desc *desc; 2600 int retval; 2601 2602 if (!handler) 2603 return -EINVAL; 2604 2605 desc = irq_to_desc(irq); 2606 2607 if (!desc || !irq_settings_can_request(desc) || 2608 !irq_settings_is_per_cpu_devid(desc) || 2609 irq_settings_can_autoenable(desc) || 2610 !irq_supports_nmi(desc)) 2611 return -EINVAL; 2612 2613 /* The line cannot be NMI already if the new request covers all CPUs */ 2614 if (irq_is_nmi(desc) && 2615 (!affinity || cpumask_equal(affinity, cpu_possible_mask))) 2616 return -EINVAL; 2617 2618 action = create_percpu_irqaction(handler, IRQF_NO_THREAD | IRQF_NOBALANCING, 2619 name, affinity, dev_id); 2620 if (!action) 2621 return -ENOMEM; 2622 2623 retval = irq_chip_pm_get(&desc->irq_data); 2624 if (retval < 0) 2625 goto err_out; 2626 2627 retval = __setup_irq(irq, desc, action); 2628 if (retval) 2629 goto err_irq_setup; 2630 2631 scoped_guard(raw_spinlock_irqsave, &desc->lock) 2632 desc->istate |= IRQS_NMI; 2633 return 0; 2634 2635 err_irq_setup: 2636 irq_chip_pm_put(&desc->irq_data); 2637 err_out: 2638 kfree(action); 2639 2640 return retval; 2641 } 2642 2643 /** 2644 * prepare_percpu_nmi - performs CPU local setup for NMI delivery 2645 * @irq: Interrupt line to prepare for NMI delivery 2646 * 2647 * This call prepares an interrupt line to deliver NMI on the current CPU, 2648 * before that interrupt line gets enabled with enable_percpu_nmi(). 2649 * 2650 * As a CPU local operation, this should be called from non-preemptible 2651 * context. 2652 * 2653 * If the interrupt line cannot be used to deliver NMIs, function will fail 2654 * returning a negative value. 2655 */ 2656 int prepare_percpu_nmi(unsigned int irq) 2657 { 2658 int ret = -EINVAL; 2659 2660 WARN_ON(preemptible()); 2661 2662 scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_PERCPU) { 2663 if (WARN(!irq_is_nmi(scoped_irqdesc), 2664 "prepare_percpu_nmi called for a non-NMI interrupt: irq %u\n", irq)) 2665 return -EINVAL; 2666 2667 ret = irq_nmi_setup(scoped_irqdesc); 2668 if (ret) 2669 pr_err("Failed to setup NMI delivery: irq %u\n", irq); 2670 } 2671 return ret; 2672 } 2673 2674 /** 2675 * teardown_percpu_nmi - undoes NMI setup of IRQ line 2676 * @irq: Interrupt line from which CPU local NMI configuration should be removed 2677 * 2678 * This call undoes the setup done by prepare_percpu_nmi(). 2679 * 2680 * IRQ line should not be enabled for the current CPU. 2681 * As a CPU local operation, this should be called from non-preemptible 2682 * context. 2683 */ 2684 void teardown_percpu_nmi(unsigned int irq) 2685 { 2686 WARN_ON(preemptible()); 2687 2688 scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_PERCPU) { 2689 if (WARN_ON(!irq_is_nmi(scoped_irqdesc))) 2690 return; 2691 irq_nmi_teardown(scoped_irqdesc); 2692 } 2693 } 2694 2695 static int __irq_get_irqchip_state(struct irq_data *data, enum irqchip_irq_state which, bool *state) 2696 { 2697 struct irq_chip *chip; 2698 int err = -EINVAL; 2699 2700 do { 2701 chip = irq_data_get_irq_chip(data); 2702 if (WARN_ON_ONCE(!chip)) 2703 return -ENODEV; 2704 if (chip->irq_get_irqchip_state) 2705 break; 2706 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY 2707 data = data->parent_data; 2708 #else 2709 data = NULL; 2710 #endif 2711 } while (data); 2712 2713 if (data) 2714 err = chip->irq_get_irqchip_state(data, which, state); 2715 return err; 2716 } 2717 2718 /** 2719 * irq_get_irqchip_state - returns the irqchip state of a interrupt. 2720 * @irq: Interrupt line that is forwarded to a VM 2721 * @which: One of IRQCHIP_STATE_* the caller wants to know about 2722 * @state: a pointer to a boolean where the state is to be stored 2723 * 2724 * This call snapshots the internal irqchip state of an interrupt, 2725 * returning into @state the bit corresponding to stage @which 2726 * 2727 * This function should be called with preemption disabled if the interrupt 2728 * controller has per-cpu registers. 2729 */ 2730 int irq_get_irqchip_state(unsigned int irq, enum irqchip_irq_state which, bool *state) 2731 { 2732 scoped_irqdesc_get_and_buslock(irq, 0) { 2733 struct irq_data *data = irq_desc_get_irq_data(scoped_irqdesc); 2734 2735 return __irq_get_irqchip_state(data, which, state); 2736 } 2737 return -EINVAL; 2738 } 2739 EXPORT_SYMBOL_GPL(irq_get_irqchip_state); 2740 2741 /** 2742 * irq_set_irqchip_state - set the state of a forwarded interrupt. 2743 * @irq: Interrupt line that is forwarded to a VM 2744 * @which: State to be restored (one of IRQCHIP_STATE_*) 2745 * @val: Value corresponding to @which 2746 * 2747 * This call sets the internal irqchip state of an interrupt, depending on 2748 * the value of @which. 2749 * 2750 * This function should be called with migration disabled if the interrupt 2751 * controller has per-cpu registers. 2752 */ 2753 int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which, bool val) 2754 { 2755 scoped_irqdesc_get_and_buslock(irq, 0) { 2756 struct irq_data *data = irq_desc_get_irq_data(scoped_irqdesc); 2757 struct irq_chip *chip; 2758 2759 do { 2760 chip = irq_data_get_irq_chip(data); 2761 2762 if (WARN_ON_ONCE(!chip)) 2763 return -ENODEV; 2764 2765 if (chip->irq_set_irqchip_state) 2766 break; 2767 2768 data = irqd_get_parent_data(data); 2769 } while (data); 2770 2771 if (data) 2772 return chip->irq_set_irqchip_state(data, which, val); 2773 } 2774 return -EINVAL; 2775 } 2776 EXPORT_SYMBOL_GPL(irq_set_irqchip_state); 2777 2778 /** 2779 * irq_has_action - Check whether an interrupt is requested 2780 * @irq: The linux irq number 2781 * 2782 * Returns: A snapshot of the current state 2783 */ 2784 bool irq_has_action(unsigned int irq) 2785 { 2786 bool res; 2787 2788 rcu_read_lock(); 2789 res = irq_desc_has_action(irq_to_desc(irq)); 2790 rcu_read_unlock(); 2791 return res; 2792 } 2793 EXPORT_SYMBOL_GPL(irq_has_action); 2794 2795 /** 2796 * irq_check_status_bit - Check whether bits in the irq descriptor status are set 2797 * @irq: The linux irq number 2798 * @bitmask: The bitmask to evaluate 2799 * 2800 * Returns: True if one of the bits in @bitmask is set 2801 */ 2802 bool irq_check_status_bit(unsigned int irq, unsigned int bitmask) 2803 { 2804 struct irq_desc *desc; 2805 bool res = false; 2806 2807 rcu_read_lock(); 2808 desc = irq_to_desc(irq); 2809 if (desc) 2810 res = !!(desc->status_use_accessors & bitmask); 2811 rcu_read_unlock(); 2812 return res; 2813 } 2814 EXPORT_SYMBOL_GPL(irq_check_status_bit); 2815