File Index
Files and directories
Sign in
FILE

manage.c

File preview with syntax highlighting.

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