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