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