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