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(sizeof(struct irqaction), GFP_KERNEL); 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 raw_spin_unlock_irqrestore(&desc->lock, flags); 1806 chip_bus_sync_unlock(desc); 1807 mutex_unlock(&desc->request_mutex); 1808 1809 wake_up_and_wait_for_irq_thread_ready(desc, new); 1810 wake_up_and_wait_for_irq_thread_ready(desc, new->secondary); 1811 1812 register_irq_proc(irq, desc); 1813 new->dir = NULL; 1814 register_handler_proc(irq, new); 1815 return 0; 1816 1817 mismatch: 1818 if (!(new->flags & IRQF_PROBE_SHARED)) { 1819 pr_err("Flags mismatch irq %d. %08x (%s) vs. %08x (%s)\n", 1820 irq, new->flags, new->name, old->flags, old->name); 1821 #ifdef CONFIG_DEBUG_SHIRQ 1822 dump_stack(); 1823 #endif 1824 } 1825 ret = -EBUSY; 1826 1827 out_unlock: 1828 raw_spin_unlock_irqrestore(&desc->lock, flags); 1829 1830 if (!desc->action) 1831 irq_release_resources(desc); 1832 out_bus_unlock: 1833 chip_bus_sync_unlock(desc); 1834 mutex_unlock(&desc->request_mutex); 1835 1836 out_thread: 1837 if (new->thread) { 1838 struct task_struct *t = new->thread; 1839 1840 new->thread = NULL; 1841 kthread_stop_put(t); 1842 } 1843 if (new->secondary && new->secondary->thread) { 1844 struct task_struct *t = new->secondary->thread; 1845 1846 new->secondary->thread = NULL; 1847 kthread_stop_put(t); 1848 } 1849 out_mput: 1850 module_put(desc->owner); 1851 return ret; 1852 } 1853 1854 /* 1855 * Internal function to unregister an irqaction - used to free 1856 * regular and special interrupts that are part of the architecture. 1857 */ 1858 static struct irqaction *__free_irq(struct irq_desc *desc, void *dev_id) 1859 { 1860 unsigned irq = desc->irq_data.irq; 1861 struct irqaction *action, **action_ptr; 1862 unsigned long flags; 1863 1864 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq); 1865 1866 mutex_lock(&desc->request_mutex); 1867 chip_bus_lock(desc); 1868 raw_spin_lock_irqsave(&desc->lock, flags); 1869 1870 /* 1871 * There can be multiple actions per IRQ descriptor, find the right 1872 * one based on the dev_id: 1873 */ 1874 action_ptr = &desc->action; 1875 for (;;) { 1876 action = *action_ptr; 1877 1878 if (!action) { 1879 WARN(1, "Trying to free already-free IRQ %d\n", irq); 1880 raw_spin_unlock_irqrestore(&desc->lock, flags); 1881 chip_bus_sync_unlock(desc); 1882 mutex_unlock(&desc->request_mutex); 1883 return NULL; 1884 } 1885 1886 if (action->dev_id == dev_id) 1887 break; 1888 action_ptr = &action->next; 1889 } 1890 1891 /* Found it - now remove it from the list of entries: */ 1892 *action_ptr = action->next; 1893 1894 irq_pm_remove_action(desc, action); 1895 1896 /* If this was the last handler, shut down the IRQ line: */ 1897 if (!desc->action) { 1898 irq_settings_clr_disable_unlazy(desc); 1899 /* Only shutdown. Deactivate after synchronize_hardirq() */ 1900 irq_shutdown(desc); 1901 } 1902 1903 #ifdef CONFIG_SMP 1904 /* make sure affinity_hint is cleaned up */ 1905 if (WARN_ON_ONCE(desc->affinity_hint)) 1906 desc->affinity_hint = NULL; 1907 #endif 1908 1909 raw_spin_unlock_irqrestore(&desc->lock, flags); 1910 /* 1911 * Drop bus_lock here so the changes which were done in the chip 1912 * callbacks above are synced out to the irq chips which hang 1913 * behind a slow bus (I2C, SPI) before calling synchronize_hardirq(). 1914 * 1915 * Aside of that the bus_lock can also be taken from the threaded 1916 * handler in irq_finalize_oneshot() which results in a deadlock 1917 * because kthread_stop() would wait forever for the thread to 1918 * complete, which is blocked on the bus lock. 1919 * 1920 * The still held desc->request_mutex() protects against a 1921 * concurrent request_irq() of this irq so the release of resources 1922 * and timing data is properly serialized. 1923 */ 1924 chip_bus_sync_unlock(desc); 1925 1926 unregister_handler_proc(irq, action); 1927 1928 /* 1929 * Make sure it's not being used on another CPU and if the chip 1930 * supports it also make sure that there is no (not yet serviced) 1931 * interrupt in flight at the hardware level. 1932 */ 1933 __synchronize_irq(desc); 1934 1935 #ifdef CONFIG_DEBUG_SHIRQ 1936 /* 1937 * It's a shared IRQ -- the driver ought to be prepared for an IRQ 1938 * event to happen even now it's being freed, so let's make sure that 1939 * is so by doing an extra call to the handler .... 1940 * 1941 * ( We do this after actually deregistering it, to make sure that a 1942 * 'real' IRQ doesn't run in parallel with our fake. ) 1943 */ 1944 if (action->flags & IRQF_SHARED) { 1945 local_irq_save(flags); 1946 action->handler(irq, dev_id); 1947 local_irq_restore(flags); 1948 } 1949 #endif 1950 1951 /* 1952 * The action has already been removed above, but the thread writes 1953 * its oneshot mask bit when it completes. Though request_mutex is 1954 * held across this which prevents __setup_irq() from handing out 1955 * the same bit to a newly requested action. 1956 */ 1957 if (action->thread) { 1958 kthread_stop_put(action->thread); 1959 if (action->secondary && action->secondary->thread) 1960 kthread_stop_put(action->secondary->thread); 1961 } 1962 1963 /* Last action releases resources */ 1964 if (!desc->action) { 1965 /* 1966 * Reacquire bus lock as irq_release_resources() might 1967 * require it to deallocate resources over the slow bus. 1968 */ 1969 chip_bus_lock(desc); 1970 /* 1971 * There is no interrupt on the fly anymore. Deactivate it 1972 * completely. 1973 */ 1974 scoped_guard(raw_spinlock_irqsave, &desc->lock) 1975 irq_domain_deactivate_irq(&desc->irq_data); 1976 1977 irq_release_resources(desc); 1978 chip_bus_sync_unlock(desc); 1979 } 1980 1981 mutex_unlock(&desc->request_mutex); 1982 1983 irq_chip_pm_put(&desc->irq_data); 1984 module_put(desc->owner); 1985 kfree(action->secondary); 1986 return action; 1987 } 1988 1989 /** 1990 * free_irq - free an interrupt allocated with request_irq 1991 * @irq: Interrupt line to free 1992 * @dev_id: Device identity to free 1993 * 1994 * Remove an interrupt handler. The handler is removed and if the interrupt 1995 * line is no longer in use by any driver it is disabled. On a shared IRQ 1996 * the caller must ensure the interrupt is disabled on the card it drives 1997 * before calling this function. The function does not return until any 1998 * executing interrupts for this IRQ have completed. 1999 * 2000 * This function must not be called from interrupt context. 2001 * 2002 * Returns the devname argument passed to request_irq. 2003 */ 2004 const void *free_irq(unsigned int irq, void *dev_id) 2005 { 2006 struct irq_desc *desc = irq_to_desc(irq); 2007 struct irqaction *action; 2008 const char *devname; 2009 2010 if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc))) 2011 return NULL; 2012 2013 #ifdef CONFIG_SMP 2014 if (WARN_ON(desc->affinity_notify)) 2015 desc->affinity_notify = NULL; 2016 #endif 2017 2018 action = __free_irq(desc, dev_id); 2019 2020 if (!action) 2021 return NULL; 2022 2023 devname = action->name; 2024 kfree(action); 2025 return devname; 2026 } 2027 EXPORT_SYMBOL(free_irq); 2028 2029 /* This function must be called with desc->lock held */ 2030 static const void *__cleanup_nmi(unsigned int irq, struct irq_desc *desc) 2031 { 2032 const char *devname = NULL; 2033 2034 desc->istate &= ~IRQS_NMI; 2035 2036 if (!WARN_ON(desc->action == NULL)) { 2037 irq_pm_remove_action(desc, desc->action); 2038 devname = desc->action->name; 2039 unregister_handler_proc(irq, desc->action); 2040 2041 kfree(desc->action); 2042 desc->action = NULL; 2043 } 2044 2045 irq_settings_clr_disable_unlazy(desc); 2046 irq_shutdown_and_deactivate(desc); 2047 2048 irq_release_resources(desc); 2049 2050 irq_chip_pm_put(&desc->irq_data); 2051 module_put(desc->owner); 2052 2053 return devname; 2054 } 2055 2056 const void *free_nmi(unsigned int irq, void *dev_id) 2057 { 2058 struct irq_desc *desc = irq_to_desc(irq); 2059 2060 if (!desc || WARN_ON(!irq_is_nmi(desc))) 2061 return NULL; 2062 2063 if (WARN_ON(irq_settings_is_per_cpu_devid(desc))) 2064 return NULL; 2065 2066 /* NMI still enabled */ 2067 if (WARN_ON(desc->depth == 0)) 2068 disable_nmi_nosync(irq); 2069 2070 guard(raw_spinlock_irqsave)(&desc->lock); 2071 irq_nmi_teardown(desc); 2072 return __cleanup_nmi(irq, desc); 2073 } 2074 2075 /** 2076 * request_threaded_irq - allocate an interrupt line 2077 * @irq: Interrupt line to allocate 2078 * @handler: Function to be called when the IRQ occurs. 2079 * Primary handler for threaded interrupts. 2080 * If handler is NULL and thread_fn != NULL 2081 * the default primary handler is installed. 2082 * @thread_fn: Function called from the irq handler thread 2083 * If NULL, no irq thread is created 2084 * @irqflags: Interrupt type flags 2085 * @devname: An ascii name for the claiming device 2086 * @dev_id: A cookie passed back to the handler function 2087 * 2088 * This call allocates interrupt resources and enables the interrupt line 2089 * and IRQ handling. From the point this call is made your handler function 2090 * may be invoked. Since your handler function must clear any interrupt the 2091 * board raises, you must take care both to initialise your hardware and to 2092 * set up the interrupt handler in the right order. 2093 * 2094 * If you want to set up a threaded irq handler for your device then you 2095 * need to supply @handler and @thread_fn. @handler is still called in hard 2096 * interrupt context and has to check whether the interrupt originates from 2097 * the device. If yes it needs to disable the interrupt on the device and 2098 * return IRQ_WAKE_THREAD which will wake up the handler thread and run 2099 * @thread_fn. This split handler design is necessary to support shared 2100 * interrupts. 2101 * 2102 * @dev_id must be globally unique. Normally the address of the device data 2103 * structure is used as the cookie. Since the handler receives this value 2104 * it makes sense to use it. 2105 * 2106 * If your interrupt is shared you must pass a non NULL dev_id as this is 2107 * required when freeing the interrupt. 2108 * 2109 * Flags: 2110 * 2111 * IRQF_SHARED Interrupt is shared 2112 * IRQF_TRIGGER_* Specify active edge(s) or level 2113 * IRQF_ONESHOT Run thread_fn with interrupt line masked 2114 */ 2115 int request_threaded_irq(unsigned int irq, irq_handler_t handler, 2116 irq_handler_t thread_fn, unsigned long irqflags, 2117 const char *devname, void *dev_id) 2118 { 2119 struct irqaction *action; 2120 struct irq_desc *desc; 2121 int retval; 2122 2123 if (irq == IRQ_NOTCONNECTED) 2124 return -ENOTCONN; 2125 2126 /* 2127 * Sanity-check: shared interrupts must pass in a real dev-ID, 2128 * otherwise we'll have trouble later trying to figure out 2129 * which interrupt is which (messes up the interrupt freeing 2130 * logic etc). 2131 * 2132 * Also shared interrupts do not go well with disabling auto enable. 2133 * The sharing interrupt might request it while it's still disabled 2134 * and then wait for interrupts forever. 2135 * 2136 * Also IRQF_COND_SUSPEND only makes sense for shared interrupts and 2137 * it cannot be set along with IRQF_NO_SUSPEND. 2138 */ 2139 if (((irqflags & IRQF_SHARED) && !dev_id) || 2140 ((irqflags & IRQF_SHARED) && (irqflags & IRQF_NO_AUTOEN)) || 2141 (!(irqflags & IRQF_SHARED) && (irqflags & IRQF_COND_SUSPEND)) || 2142 ((irqflags & IRQF_NO_SUSPEND) && (irqflags & IRQF_COND_SUSPEND))) 2143 return -EINVAL; 2144 2145 desc = irq_to_desc(irq); 2146 if (!desc) 2147 return -EINVAL; 2148 2149 if (!irq_settings_can_request(desc) || 2150 WARN_ON(irq_settings_is_per_cpu_devid(desc))) 2151 return -EINVAL; 2152 2153 if (!handler) { 2154 if (!thread_fn) 2155 return -EINVAL; 2156 handler = irq_default_primary_handler; 2157 } 2158 2159 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL); 2160 if (!action) 2161 return -ENOMEM; 2162 2163 action->handler = handler; 2164 action->thread_fn = thread_fn; 2165 action->flags = irqflags; 2166 action->name = devname; 2167 action->dev_id = dev_id; 2168 2169 retval = irq_chip_pm_get(&desc->irq_data); 2170 if (retval < 0) { 2171 kfree(action); 2172 return retval; 2173 } 2174 2175 retval = __setup_irq(irq, desc, action); 2176 2177 if (retval) { 2178 irq_chip_pm_put(&desc->irq_data); 2179 kfree(action->secondary); 2180 kfree(action); 2181 } 2182 2183 #ifdef CONFIG_DEBUG_SHIRQ_FIXME 2184 if (!retval && (irqflags & IRQF_SHARED)) { 2185 /* 2186 * It's a shared IRQ -- the driver ought to be prepared for it 2187 * to happen immediately, so let's make sure.... 2188 * We disable the irq to make sure that a 'real' IRQ doesn't 2189 * run in parallel with our fake. 2190 */ 2191 unsigned long flags; 2192 2193 disable_irq(irq); 2194 local_irq_save(flags); 2195 2196 handler(irq, dev_id); 2197 2198 local_irq_restore(flags); 2199 enable_irq(irq); 2200 } 2201 #endif 2202 return retval; 2203 } 2204 EXPORT_SYMBOL(request_threaded_irq); 2205 2206 /** 2207 * request_any_context_irq - allocate an interrupt line 2208 * @irq: Interrupt line to allocate 2209 * @handler: Function to be called when the IRQ occurs. 2210 * Threaded handler for threaded interrupts. 2211 * @flags: Interrupt type flags 2212 * @name: An ascii name for the claiming device 2213 * @dev_id: A cookie passed back to the handler function 2214 * 2215 * This call allocates interrupt resources and enables the interrupt line 2216 * and IRQ handling. It selects either a hardirq or threaded handling 2217 * method depending on the context. 2218 * 2219 * Returns: On failure, it returns a negative value. On success, it returns either 2220 * IRQC_IS_HARDIRQ or IRQC_IS_NESTED. 2221 */ 2222 int request_any_context_irq(unsigned int irq, irq_handler_t handler, 2223 unsigned long flags, const char *name, void *dev_id) 2224 { 2225 struct irq_desc *desc; 2226 int ret; 2227 2228 if (irq == IRQ_NOTCONNECTED) 2229 return -ENOTCONN; 2230 2231 desc = irq_to_desc(irq); 2232 if (!desc) 2233 return -EINVAL; 2234 2235 if (irq_settings_is_nested_thread(desc)) { 2236 ret = request_threaded_irq(irq, NULL, handler, 2237 flags, name, dev_id); 2238 return !ret ? IRQC_IS_NESTED : ret; 2239 } 2240 2241 ret = request_irq(irq, handler, flags, name, dev_id); 2242 return !ret ? IRQC_IS_HARDIRQ : ret; 2243 } 2244 EXPORT_SYMBOL_GPL(request_any_context_irq); 2245 2246 /** 2247 * request_nmi - allocate an interrupt line for NMI delivery 2248 * @irq: Interrupt line to allocate 2249 * @handler: Function to be called when the IRQ occurs. 2250 * Threaded handler for threaded interrupts. 2251 * @irqflags: Interrupt type flags 2252 * @name: An ascii name for the claiming device 2253 * @dev_id: A cookie passed back to the handler function 2254 * 2255 * This call allocates interrupt resources and enables the interrupt line 2256 * and IRQ handling. It sets up the IRQ line to be handled as an NMI. 2257 * 2258 * An interrupt line delivering NMIs cannot be shared and IRQ handling 2259 * cannot be threaded. 2260 * 2261 * Interrupt lines requested for NMI delivering must produce per cpu 2262 * interrupts and have auto enabling setting disabled. 2263 * 2264 * @dev_id must be globally unique. Normally the address of the device data 2265 * structure is used as the cookie. Since the handler receives this value 2266 * it makes sense to use it. 2267 * 2268 * If the interrupt line cannot be used to deliver NMIs, function will fail 2269 * and return a negative value. 2270 */ 2271 int request_nmi(unsigned int irq, irq_handler_t handler, 2272 unsigned long irqflags, const char *name, void *dev_id) 2273 { 2274 struct irqaction *action; 2275 struct irq_desc *desc; 2276 int retval; 2277 2278 if (irq == IRQ_NOTCONNECTED) 2279 return -ENOTCONN; 2280 2281 /* NMI cannot be shared, used for Polling */ 2282 if (irqflags & (IRQF_SHARED | IRQF_COND_SUSPEND | IRQF_IRQPOLL)) 2283 return -EINVAL; 2284 2285 if (!(irqflags & IRQF_PERCPU)) 2286 return -EINVAL; 2287 2288 if (!handler) 2289 return -EINVAL; 2290 2291 desc = irq_to_desc(irq); 2292 2293 if (!desc || (irq_settings_can_autoenable(desc) && 2294 !(irqflags & IRQF_NO_AUTOEN)) || 2295 !irq_settings_can_request(desc) || 2296 WARN_ON(irq_settings_is_per_cpu_devid(desc)) || 2297 !irq_supports_nmi(desc)) 2298 return -EINVAL; 2299 2300 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL); 2301 if (!action) 2302 return -ENOMEM; 2303 2304 action->handler = handler; 2305 action->flags = irqflags | IRQF_NO_THREAD | IRQF_NOBALANCING; 2306 action->name = name; 2307 action->dev_id = dev_id; 2308 2309 retval = irq_chip_pm_get(&desc->irq_data); 2310 if (retval < 0) 2311 goto err_out; 2312 2313 retval = __setup_irq(irq, desc, action); 2314 if (retval) 2315 goto err_irq_setup; 2316 2317 scoped_guard(raw_spinlock_irqsave, &desc->lock) { 2318 /* Setup NMI state */ 2319 desc->istate |= IRQS_NMI; 2320 retval = irq_nmi_setup(desc); 2321 if (retval) { 2322 __cleanup_nmi(irq, desc); 2323 return -EINVAL; 2324 } 2325 return 0; 2326 } 2327 2328 err_irq_setup: 2329 irq_chip_pm_put(&desc->irq_data); 2330 err_out: 2331 kfree(action); 2332 2333 return retval; 2334 } 2335 2336 void enable_percpu_irq(unsigned int irq, unsigned int type) 2337 { 2338 scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_PERCPU) { 2339 struct irq_desc *desc = scoped_irqdesc; 2340 2341 /* 2342 * If the trigger type is not specified by the caller, then 2343 * use the default for this interrupt. 2344 */ 2345 type &= IRQ_TYPE_SENSE_MASK; 2346 if (type == IRQ_TYPE_NONE) 2347 type = irqd_get_trigger_type(&desc->irq_data); 2348 2349 if (type != IRQ_TYPE_NONE) { 2350 if (__irq_set_trigger(desc, type)) { 2351 WARN(1, "failed to set type for IRQ%d\n", irq); 2352 return; 2353 } 2354 } 2355 irq_percpu_enable(desc, smp_processor_id()); 2356 } 2357 } 2358 EXPORT_SYMBOL_GPL(enable_percpu_irq); 2359 2360 void enable_percpu_nmi(unsigned int irq, unsigned int type) 2361 { 2362 enable_percpu_irq(irq, type); 2363 } 2364 2365 /** 2366 * irq_percpu_is_enabled - Check whether the per cpu irq is enabled 2367 * @irq: Linux irq number to check for 2368 * 2369 * Must be called from a non migratable context. Returns the enable 2370 * state of a per cpu interrupt on the current cpu. 2371 */ 2372 bool irq_percpu_is_enabled(unsigned int irq) 2373 { 2374 scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_PERCPU) 2375 return cpumask_test_cpu(smp_processor_id(), scoped_irqdesc->percpu_enabled); 2376 return false; 2377 } 2378 EXPORT_SYMBOL_GPL(irq_percpu_is_enabled); 2379 2380 void disable_percpu_irq(unsigned int irq) 2381 { 2382 scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_PERCPU) 2383 irq_percpu_disable(scoped_irqdesc, smp_processor_id()); 2384 } 2385 EXPORT_SYMBOL_GPL(disable_percpu_irq); 2386 2387 void disable_percpu_nmi(unsigned int irq) 2388 { 2389 disable_percpu_irq(irq); 2390 } 2391 2392 /* 2393 * Internal function to unregister a percpu irqaction. 2394 */ 2395 static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id) 2396 { 2397 struct irq_desc *desc = irq_to_desc(irq); 2398 struct irqaction *action, **action_ptr; 2399 2400 WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq); 2401 2402 if (!desc) 2403 return NULL; 2404 2405 scoped_guard(raw_spinlock_irqsave, &desc->lock) { 2406 action_ptr = &desc->action; 2407 for (;;) { 2408 action = *action_ptr; 2409 2410 if (!action) { 2411 WARN(1, "Trying to free already-free IRQ %d\n", irq); 2412 return NULL; 2413 } 2414 2415 if (action->percpu_dev_id == dev_id) 2416 break; 2417 2418 action_ptr = &action->next; 2419 } 2420 2421 if (cpumask_intersects(desc->percpu_enabled, action->affinity)) { 2422 WARN(1, "percpu IRQ %d still enabled on CPU%d!\n", irq, 2423 cpumask_first_and(desc->percpu_enabled, action->affinity)); 2424 return NULL; 2425 } 2426 2427 /* Found it - now remove it from the list of entries: */ 2428 *action_ptr = action->next; 2429 2430 /* Demote from NMI if we killed the last action */ 2431 if (!desc->action) 2432 desc->istate &= ~IRQS_NMI; 2433 } 2434 2435 unregister_handler_proc(irq, action); 2436 irq_chip_pm_put(&desc->irq_data); 2437 module_put(desc->owner); 2438 return action; 2439 } 2440 2441 /** 2442 * free_percpu_irq - free an interrupt allocated with request_percpu_irq 2443 * @irq: Interrupt line to free 2444 * @dev_id: Device identity to free 2445 * 2446 * Remove a percpu interrupt handler. The handler is removed, but the 2447 * interrupt line is not disabled. This must be done on each CPU before 2448 * calling this function. The function does not return until any executing 2449 * interrupts for this IRQ have completed. 2450 * 2451 * This function must not be called from interrupt context. 2452 */ 2453 void free_percpu_irq(unsigned int irq, void __percpu *dev_id) 2454 { 2455 struct irq_desc *desc = irq_to_desc(irq); 2456 2457 if (!desc || !irq_settings_is_per_cpu_devid(desc)) 2458 return; 2459 2460 chip_bus_lock(desc); 2461 kfree(__free_percpu_irq(irq, dev_id)); 2462 chip_bus_sync_unlock(desc); 2463 } 2464 EXPORT_SYMBOL_GPL(free_percpu_irq); 2465 2466 void free_percpu_nmi(unsigned int irq, void __percpu *dev_id) 2467 { 2468 struct irq_desc *desc = irq_to_desc(irq); 2469 2470 if (!desc || !irq_settings_is_per_cpu_devid(desc)) 2471 return; 2472 2473 if (WARN_ON(!irq_is_nmi(desc))) 2474 return; 2475 2476 kfree(__free_percpu_irq(irq, dev_id)); 2477 } 2478 2479 static 2480 struct irqaction *create_percpu_irqaction(irq_handler_t handler, unsigned long flags, 2481 const char *devname, const cpumask_t *affinity, 2482 void __percpu *dev_id) 2483 { 2484 struct irqaction *action; 2485 2486 if (!affinity) 2487 affinity = cpu_possible_mask; 2488 2489 action = kzalloc(sizeof(struct irqaction), GFP_KERNEL); 2490 if (!action) 2491 return NULL; 2492 2493 action->handler = handler; 2494 action->flags = flags | IRQF_PERCPU | IRQF_NO_SUSPEND; 2495 action->name = devname; 2496 action->percpu_dev_id = dev_id; 2497 action->affinity = affinity; 2498 2499 /* 2500 * We allow some form of sharing for non-overlapping affinity 2501 * masks. Obviously, covering all CPUs prevents any sharing in 2502 * the first place. 2503 */ 2504 if (!cpumask_equal(affinity, cpu_possible_mask)) 2505 action->flags |= IRQF_SHARED; 2506 2507 return action; 2508 } 2509 2510 /** 2511 * request_percpu_irq_affinity - allocate a percpu interrupt line 2512 * @irq: Interrupt line to allocate 2513 * @handler: Function to be called when the IRQ occurs. 2514 * @devname: An ascii name for the claiming device 2515 * @affinity: A cpumask describing the target CPUs for this interrupt 2516 * @dev_id: A percpu cookie passed back to the handler function 2517 * 2518 * This call allocates interrupt resources, but doesn't enable the interrupt 2519 * on any CPU, as all percpu-devid interrupts are flagged with IRQ_NOAUTOEN. 2520 * It has to be done on each CPU using enable_percpu_irq(). 2521 * 2522 * @dev_id must be globally unique. It is a per-cpu variable, and 2523 * the handler gets called with the interrupted CPU's instance of 2524 * that variable. 2525 */ 2526 int request_percpu_irq_affinity(unsigned int irq, irq_handler_t handler, const char *devname, 2527 const cpumask_t *affinity, void __percpu *dev_id) 2528 { 2529 struct irqaction *action; 2530 struct irq_desc *desc; 2531 int retval; 2532 2533 if (!dev_id) 2534 return -EINVAL; 2535 2536 desc = irq_to_desc(irq); 2537 if (!desc || !irq_settings_can_request(desc) || 2538 !irq_settings_is_per_cpu_devid(desc)) 2539 return -EINVAL; 2540 2541 action = create_percpu_irqaction(handler, 0, devname, affinity, dev_id); 2542 if (!action) 2543 return -ENOMEM; 2544 2545 retval = irq_chip_pm_get(&desc->irq_data); 2546 if (retval < 0) { 2547 kfree(action); 2548 return retval; 2549 } 2550 2551 retval = __setup_irq(irq, desc, action); 2552 2553 if (retval) { 2554 irq_chip_pm_put(&desc->irq_data); 2555 kfree(action); 2556 } 2557 2558 return retval; 2559 } 2560 EXPORT_SYMBOL_GPL(request_percpu_irq_affinity); 2561 2562 /** 2563 * request_percpu_nmi - allocate a percpu interrupt line for NMI delivery 2564 * @irq: Interrupt line to allocate 2565 * @handler: Function to be called when the IRQ occurs. 2566 * @name: An ascii name for the claiming device 2567 * @affinity: A cpumask describing the target CPUs for this interrupt 2568 * @dev_id: A percpu cookie passed back to the handler function 2569 * 2570 * This call allocates interrupt resources for a per CPU NMI. Per CPU NMIs 2571 * have to be setup on each CPU by calling prepare_percpu_nmi() before 2572 * being enabled on the same CPU by using enable_percpu_nmi(). 2573 * 2574 * @dev_id must be globally unique. It is a per-cpu variable, and the 2575 * handler gets called with the interrupted CPU's instance of that 2576 * variable. 2577 * 2578 * Interrupt lines requested for NMI delivering should have auto enabling 2579 * setting disabled. 2580 * 2581 * If the interrupt line cannot be used to deliver NMIs, function 2582 * will fail returning a negative value. 2583 */ 2584 int request_percpu_nmi(unsigned int irq, irq_handler_t handler, const char *name, 2585 const struct cpumask *affinity, void __percpu *dev_id) 2586 { 2587 struct irqaction *action; 2588 struct irq_desc *desc; 2589 int retval; 2590 2591 if (!handler) 2592 return -EINVAL; 2593 2594 desc = irq_to_desc(irq); 2595 2596 if (!desc || !irq_settings_can_request(desc) || 2597 !irq_settings_is_per_cpu_devid(desc) || 2598 irq_settings_can_autoenable(desc) || 2599 !irq_supports_nmi(desc)) 2600 return -EINVAL; 2601 2602 /* The line cannot be NMI already if the new request covers all CPUs */ 2603 if (irq_is_nmi(desc) && 2604 (!affinity || cpumask_equal(affinity, cpu_possible_mask))) 2605 return -EINVAL; 2606 2607 action = create_percpu_irqaction(handler, IRQF_NO_THREAD | IRQF_NOBALANCING, 2608 name, affinity, dev_id); 2609 if (!action) 2610 return -ENOMEM; 2611 2612 retval = irq_chip_pm_get(&desc->irq_data); 2613 if (retval < 0) 2614 goto err_out; 2615 2616 retval = __setup_irq(irq, desc, action); 2617 if (retval) 2618 goto err_irq_setup; 2619 2620 scoped_guard(raw_spinlock_irqsave, &desc->lock) 2621 desc->istate |= IRQS_NMI; 2622 return 0; 2623 2624 err_irq_setup: 2625 irq_chip_pm_put(&desc->irq_data); 2626 err_out: 2627 kfree(action); 2628 2629 return retval; 2630 } 2631 2632 /** 2633 * prepare_percpu_nmi - performs CPU local setup for NMI delivery 2634 * @irq: Interrupt line to prepare for NMI delivery 2635 * 2636 * This call prepares an interrupt line to deliver NMI on the current CPU, 2637 * before that interrupt line gets enabled with enable_percpu_nmi(). 2638 * 2639 * As a CPU local operation, this should be called from non-preemptible 2640 * context. 2641 * 2642 * If the interrupt line cannot be used to deliver NMIs, function will fail 2643 * returning a negative value. 2644 */ 2645 int prepare_percpu_nmi(unsigned int irq) 2646 { 2647 int ret = -EINVAL; 2648 2649 WARN_ON(preemptible()); 2650 2651 scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_PERCPU) { 2652 if (WARN(!irq_is_nmi(scoped_irqdesc), 2653 "prepare_percpu_nmi called for a non-NMI interrupt: irq %u\n", irq)) 2654 return -EINVAL; 2655 2656 ret = irq_nmi_setup(scoped_irqdesc); 2657 if (ret) 2658 pr_err("Failed to setup NMI delivery: irq %u\n", irq); 2659 } 2660 return ret; 2661 } 2662 2663 /** 2664 * teardown_percpu_nmi - undoes NMI setup of IRQ line 2665 * @irq: Interrupt line from which CPU local NMI configuration should be removed 2666 * 2667 * This call undoes the setup done by prepare_percpu_nmi(). 2668 * 2669 * IRQ line should not be enabled for the current CPU. 2670 * As a CPU local operation, this should be called from non-preemptible 2671 * context. 2672 */ 2673 void teardown_percpu_nmi(unsigned int irq) 2674 { 2675 WARN_ON(preemptible()); 2676 2677 scoped_irqdesc_get_and_lock(irq, IRQ_GET_DESC_CHECK_PERCPU) { 2678 if (WARN_ON(!irq_is_nmi(scoped_irqdesc))) 2679 return; 2680 irq_nmi_teardown(scoped_irqdesc); 2681 } 2682 } 2683 2684 static int __irq_get_irqchip_state(struct irq_data *data, enum irqchip_irq_state which, bool *state) 2685 { 2686 struct irq_chip *chip; 2687 int err = -EINVAL; 2688 2689 do { 2690 chip = irq_data_get_irq_chip(data); 2691 if (WARN_ON_ONCE(!chip)) 2692 return -ENODEV; 2693 if (chip->irq_get_irqchip_state) 2694 break; 2695 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY 2696 data = data->parent_data; 2697 #else 2698 data = NULL; 2699 #endif 2700 } while (data); 2701 2702 if (data) 2703 err = chip->irq_get_irqchip_state(data, which, state); 2704 return err; 2705 } 2706 2707 /** 2708 * irq_get_irqchip_state - returns the irqchip state of a interrupt. 2709 * @irq: Interrupt line that is forwarded to a VM 2710 * @which: One of IRQCHIP_STATE_* the caller wants to know about 2711 * @state: a pointer to a boolean where the state is to be stored 2712 * 2713 * This call snapshots the internal irqchip state of an interrupt, 2714 * returning into @state the bit corresponding to stage @which 2715 * 2716 * This function should be called with preemption disabled if the interrupt 2717 * controller has per-cpu registers. 2718 */ 2719 int irq_get_irqchip_state(unsigned int irq, enum irqchip_irq_state which, bool *state) 2720 { 2721 scoped_irqdesc_get_and_buslock(irq, 0) { 2722 struct irq_data *data = irq_desc_get_irq_data(scoped_irqdesc); 2723 2724 return __irq_get_irqchip_state(data, which, state); 2725 } 2726 return -EINVAL; 2727 } 2728 EXPORT_SYMBOL_GPL(irq_get_irqchip_state); 2729 2730 /** 2731 * irq_set_irqchip_state - set the state of a forwarded interrupt. 2732 * @irq: Interrupt line that is forwarded to a VM 2733 * @which: State to be restored (one of IRQCHIP_STATE_*) 2734 * @val: Value corresponding to @which 2735 * 2736 * This call sets the internal irqchip state of an interrupt, depending on 2737 * the value of @which. 2738 * 2739 * This function should be called with migration disabled if the interrupt 2740 * controller has per-cpu registers. 2741 */ 2742 int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which, bool val) 2743 { 2744 scoped_irqdesc_get_and_buslock(irq, 0) { 2745 struct irq_data *data = irq_desc_get_irq_data(scoped_irqdesc); 2746 struct irq_chip *chip; 2747 2748 do { 2749 chip = irq_data_get_irq_chip(data); 2750 2751 if (WARN_ON_ONCE(!chip)) 2752 return -ENODEV; 2753 2754 if (chip->irq_set_irqchip_state) 2755 break; 2756 2757 data = irqd_get_parent_data(data); 2758 } while (data); 2759 2760 if (data) 2761 return chip->irq_set_irqchip_state(data, which, val); 2762 } 2763 return -EINVAL; 2764 } 2765 EXPORT_SYMBOL_GPL(irq_set_irqchip_state); 2766 2767 /** 2768 * irq_has_action - Check whether an interrupt is requested 2769 * @irq: The linux irq number 2770 * 2771 * Returns: A snapshot of the current state 2772 */ 2773 bool irq_has_action(unsigned int irq) 2774 { 2775 bool res; 2776 2777 rcu_read_lock(); 2778 res = irq_desc_has_action(irq_to_desc(irq)); 2779 rcu_read_unlock(); 2780 return res; 2781 } 2782 EXPORT_SYMBOL_GPL(irq_has_action); 2783 2784 /** 2785 * irq_check_status_bit - Check whether bits in the irq descriptor status are set 2786 * @irq: The linux irq number 2787 * @bitmask: The bitmask to evaluate 2788 * 2789 * Returns: True if one of the bits in @bitmask is set 2790 */ 2791 bool irq_check_status_bit(unsigned int irq, unsigned int bitmask) 2792 { 2793 struct irq_desc *desc; 2794 bool res = false; 2795 2796 rcu_read_lock(); 2797 desc = irq_to_desc(irq); 2798 if (desc) 2799 res = !!(desc->status_use_accessors & bitmask); 2800 rcu_read_unlock(); 2801 return res; 2802 } 2803 EXPORT_SYMBOL_GPL(irq_check_status_bit); 2804