1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * IRQ subsystem internal functions and variables: 4 * 5 * Do not ever include this file from anything else than 6 * kernel/irq/. Do not even think about using any information outside 7 * of this file for your non core code. 8 */ 9 #include <linux/irqdesc.h> 10 #include <linux/kernel_stat.h> 11 #include <linux/pm_runtime.h> 12 #include <linux/rcuref.h> 13 #include <linux/sched/clock.h> 14 15 #include "debugfs.h" 16 #include "proc.h" 17 18 #ifdef CONFIG_SPARSE_IRQ 19 # define MAX_SPARSE_IRQS INT_MAX 20 #else 21 # define MAX_SPARSE_IRQS NR_IRQS 22 #endif 23 24 #define istate core_internal_state__do_not_mess_with_it 25 26 extern bool noirqdebug; 27 extern int irq_poll_cpu; 28 extern unsigned int total_nr_irqs; 29 30 extern struct irqaction chained_action; 31 32 /* 33 * Bits used by threaded handlers: 34 * IRQTF_RUNTHREAD - signals that the interrupt handler thread should run 35 * IRQTF_WARNED - warning "IRQ_WAKE_THREAD w/o thread_fn" has been printed 36 * IRQTF_AFFINITY - irq thread is requested to adjust affinity 37 * IRQTF_FORCED_THREAD - irq action is force threaded 38 * IRQTF_READY - signals that irq thread is ready 39 * IRQTF_EXIT_WORK - irq thread exit task_work is pending 40 */ 41 enum { 42 IRQTF_RUNTHREAD, 43 IRQTF_WARNED, 44 IRQTF_AFFINITY, 45 IRQTF_FORCED_THREAD, 46 IRQTF_READY, 47 IRQTF_EXIT_WORK, 48 }; 49 50 /* 51 * Bit masks for desc->core_internal_state__do_not_mess_with_it 52 * 53 * IRQS_AUTODETECT - autodetection in progress 54 * IRQS_SPURIOUS_DISABLED - was disabled due to spurious interrupt 55 * detection 56 * IRQS_POLL_INPROGRESS - polling in progress 57 * IRQS_ONESHOT - irq is not unmasked in primary handler 58 * IRQS_REPLAY - irq has been resent and will not be resent 59 * again until the handler has run and cleared 60 * this flag. 61 * IRQS_WAITING - irq is waiting 62 * IRQS_PENDING - irq needs to be resent and should be resent 63 * at the next available opportunity. 64 * IRQS_SUSPENDED - irq is suspended 65 * IRQS_NMI - irq line is used to deliver NMIs 66 * IRQS_SYSFS - descriptor has been added to sysfs 67 */ 68 enum { 69 IRQS_AUTODETECT = 0x00000001, 70 IRQS_SPURIOUS_DISABLED = 0x00000002, 71 IRQS_POLL_INPROGRESS = 0x00000008, 72 IRQS_ONESHOT = 0x00000020, 73 IRQS_REPLAY = 0x00000040, 74 IRQS_WAITING = 0x00000080, 75 IRQS_PENDING = 0x00000200, 76 IRQS_SUSPENDED = 0x00000800, 77 IRQS_TIMINGS = 0x00001000, 78 IRQS_NMI = 0x00002000, 79 IRQS_SYSFS = 0x00004000, 80 }; 81 82 #include "debug.h" 83 #include "settings.h" 84 85 extern int __irq_set_trigger(struct irq_desc *desc, unsigned long flags); 86 extern void __disable_irq(struct irq_desc *desc); 87 extern void __enable_irq(struct irq_desc *desc); 88 89 #define IRQ_RESEND true 90 #define IRQ_NORESEND false 91 92 #define IRQ_START_FORCE true 93 #define IRQ_START_COND false 94 95 extern int irq_activate(struct irq_desc *desc); 96 extern int irq_activate_and_startup(struct irq_desc *desc, bool resend); 97 extern int irq_startup(struct irq_desc *desc, bool resend, bool force); 98 extern void irq_startup_managed(struct irq_desc *desc); 99 100 extern void irq_shutdown(struct irq_desc *desc); 101 extern void irq_shutdown_and_deactivate(struct irq_desc *desc); 102 extern void irq_disable(struct irq_desc *desc); 103 extern void irq_percpu_enable(struct irq_desc *desc, unsigned int cpu); 104 extern void irq_percpu_disable(struct irq_desc *desc, unsigned int cpu); 105 extern void mask_irq(struct irq_desc *desc); 106 extern void unmask_irq(struct irq_desc *desc); 107 extern void unmask_threaded_irq(struct irq_desc *desc); 108 109 #ifdef CONFIG_SPARSE_IRQ 110 static __always_inline void irq_mark_irq(unsigned int irq) { } 111 void irq_desc_free_rcu(struct irq_desc *desc); 112 113 static __always_inline bool irq_desc_get_ref(struct irq_desc *desc) 114 { 115 return rcuref_get(&desc->refcnt); 116 } 117 118 static __always_inline void irq_desc_put_ref(struct irq_desc *desc) 119 { 120 if (rcuref_put(&desc->refcnt)) 121 irq_desc_free_rcu(desc); 122 } 123 #else 124 extern void irq_mark_irq(unsigned int irq); 125 static __always_inline bool irq_desc_get_ref(struct irq_desc *desc) { return true; } 126 static __always_inline void irq_desc_put_ref(struct irq_desc *desc) { } 127 #endif 128 129 irqreturn_t __handle_irq_event_percpu(struct irq_desc *desc); 130 irqreturn_t handle_irq_event_percpu(struct irq_desc *desc); 131 irqreturn_t handle_irq_event(struct irq_desc *desc); 132 133 /* Resending of interrupts :*/ 134 int check_irq_resend(struct irq_desc *desc, bool inject); 135 void clear_irq_resend(struct irq_desc *desc); 136 void irq_resend_init(struct irq_desc *desc); 137 void __irq_wake_thread(struct irq_desc *desc, struct irqaction *action); 138 139 void wake_threads_waitq(struct irq_desc *desc); 140 141 #ifdef CONFIG_PROC_FS 142 extern void register_irq_proc(unsigned int irq, struct irq_desc *desc); 143 extern void unregister_irq_proc(unsigned int irq, struct irq_desc *desc); 144 extern void register_handler_proc(unsigned int irq, struct irqaction *action); 145 extern void unregister_handler_proc(unsigned int irq, struct irqaction *action); 146 void irq_proc_update_valid(struct irq_desc *desc); 147 #else 148 static inline void register_irq_proc(unsigned int irq, struct irq_desc *desc) { } 149 static inline void unregister_irq_proc(unsigned int irq, struct irq_desc *desc) { } 150 static inline void register_handler_proc(unsigned int irq, 151 struct irqaction *action) { } 152 static inline void unregister_handler_proc(unsigned int irq, 153 struct irqaction *action) { } 154 static inline void irq_proc_update_valid(struct irq_desc *desc) { } 155 #endif 156 157 struct irq_desc *irq_find_desc_at_or_after(unsigned int offset); 158 159 extern bool irq_can_set_affinity_usr(unsigned int irq); 160 161 extern int irq_do_set_affinity(struct irq_data *data, 162 const struct cpumask *dest, bool force); 163 extern void irq_affinity_schedule_notify_work(struct irq_desc *desc); 164 165 #ifdef CONFIG_SMP 166 extern int irq_setup_affinity(struct irq_desc *desc); 167 #else 168 static inline int irq_setup_affinity(struct irq_desc *desc) { return 0; } 169 #endif 170 171 #define for_each_action_of_desc(desc, act) \ 172 for (act = desc->action; act; act = act->next) 173 174 /* Inline functions for support of irq chips on slow busses */ 175 static inline void chip_bus_lock(struct irq_desc *desc) 176 { 177 if (unlikely(desc->irq_data.chip->irq_bus_lock)) 178 desc->irq_data.chip->irq_bus_lock(&desc->irq_data); 179 } 180 181 static inline void chip_bus_sync_unlock(struct irq_desc *desc) 182 { 183 if (unlikely(desc->irq_data.chip->irq_bus_sync_unlock)) 184 desc->irq_data.chip->irq_bus_sync_unlock(&desc->irq_data); 185 } 186 187 #define _IRQ_DESC_CHECK (1 << 0) 188 #define _IRQ_DESC_PERCPU (1 << 1) 189 190 #define IRQ_GET_DESC_CHECK_GLOBAL (_IRQ_DESC_CHECK) 191 #define IRQ_GET_DESC_CHECK_PERCPU (_IRQ_DESC_CHECK | _IRQ_DESC_PERCPU) 192 193 struct irq_desc *__irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus, 194 unsigned int check); 195 void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus); 196 197 __DEFINE_CLASS_IS_CONDITIONAL(irqdesc_lock, true); 198 __DEFINE_UNLOCK_GUARD(irqdesc_lock, struct irq_desc, 199 if (_T->lock) __irq_put_desc_unlock(_T->lock, _T->flags, _T->bus), 200 unsigned long flags; bool bus); 201 202 static inline class_irqdesc_lock_t class_irqdesc_lock_constructor(unsigned int irq, bool bus, 203 unsigned int check) 204 { 205 class_irqdesc_lock_t _t = { .bus = bus, }; 206 207 _t.lock = __irq_get_desc_lock(irq, &_t.flags, bus, check); 208 209 return _t; 210 } 211 212 #define scoped_irqdesc_get_and_lock(_irq, _check) \ 213 scoped_guard(irqdesc_lock, _irq, false, _check) 214 215 #define scoped_irqdesc_get_and_buslock(_irq, _check) \ 216 scoped_guard(irqdesc_lock, _irq, true, _check) 217 218 #define scoped_irqdesc ((struct irq_desc *)(__guard_ptr(irqdesc_lock)(&scope))) 219 220 #define __irqd_to_state(d) ACCESS_PRIVATE((d)->common, state_use_accessors) 221 222 static inline unsigned int irqd_get(struct irq_data *d) 223 { 224 return __irqd_to_state(d); 225 } 226 227 /* 228 * Manipulation functions for irq_data.state 229 */ 230 static inline void irqd_set_move_pending(struct irq_data *d) 231 { 232 __irqd_to_state(d) |= IRQD_SETAFFINITY_PENDING; 233 } 234 235 static inline void irqd_clr_move_pending(struct irq_data *d) 236 { 237 __irqd_to_state(d) &= ~IRQD_SETAFFINITY_PENDING; 238 } 239 240 static inline void irqd_set_managed_shutdown(struct irq_data *d) 241 { 242 __irqd_to_state(d) |= IRQD_MANAGED_SHUTDOWN; 243 } 244 245 static inline void irqd_clr_managed_shutdown(struct irq_data *d) 246 { 247 __irqd_to_state(d) &= ~IRQD_MANAGED_SHUTDOWN; 248 } 249 250 static inline void irqd_clear(struct irq_data *d, unsigned int mask) 251 { 252 __irqd_to_state(d) &= ~mask; 253 } 254 255 static inline void irqd_set(struct irq_data *d, unsigned int mask) 256 { 257 __irqd_to_state(d) |= mask; 258 } 259 260 static inline bool irqd_has_set(struct irq_data *d, unsigned int mask) 261 { 262 return __irqd_to_state(d) & mask; 263 } 264 265 static inline void irq_state_set_disabled(struct irq_desc *desc) 266 { 267 irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED); 268 } 269 270 static inline void irq_state_set_masked(struct irq_desc *desc) 271 { 272 irqd_set(&desc->irq_data, IRQD_IRQ_MASKED); 273 } 274 275 #undef __irqd_to_state 276 277 static inline void __kstat_incr_irqs_this_cpu(struct irq_desc *desc) 278 { 279 __this_cpu_inc(desc->kstat_irqs->cnt); 280 __this_cpu_inc(kstat.irqs_sum); 281 } 282 283 static inline void kstat_incr_irqs_this_cpu(struct irq_desc *desc) 284 { 285 __kstat_incr_irqs_this_cpu(desc); 286 desc->tot_count++; 287 } 288 289 static inline int irq_desc_get_node(struct irq_desc *desc) 290 { 291 return irq_common_data_get_node(&desc->irq_common_data); 292 } 293 294 static inline int irq_desc_is_chained(struct irq_desc *desc) 295 { 296 return (desc->action && desc->action == &chained_action); 297 } 298 299 static inline bool irq_is_nmi(struct irq_desc *desc) 300 { 301 return desc->istate & IRQS_NMI; 302 } 303 304 #ifdef CONFIG_PM_SLEEP 305 void irq_pm_handle_wakeup(struct irq_desc *desc); 306 void irq_pm_install_action(struct irq_desc *desc, struct irqaction *action); 307 void irq_pm_remove_action(struct irq_desc *desc, struct irqaction *action); 308 #else 309 static inline void irq_pm_handle_wakeup(struct irq_desc *desc) { } 310 static inline void 311 irq_pm_install_action(struct irq_desc *desc, struct irqaction *action) { } 312 static inline void 313 irq_pm_remove_action(struct irq_desc *desc, struct irqaction *action) { } 314 #endif 315 316 #ifdef CONFIG_GENERIC_IRQ_CHIP 317 void irq_init_generic_chip(struct irq_chip_generic *gc, const char *name, 318 int num_ct, unsigned int irq_base, 319 void __iomem *reg_base, irq_flow_handler_t handler); 320 #else 321 static inline void 322 irq_init_generic_chip(struct irq_chip_generic *gc, const char *name, 323 int num_ct, unsigned int irq_base, 324 void __iomem *reg_base, irq_flow_handler_t handler) { } 325 #endif /* CONFIG_GENERIC_IRQ_CHIP */ 326 327 #ifdef CONFIG_GENERIC_PENDING_IRQ 328 static inline bool irq_can_move_pcntxt(struct irq_data *data) 329 { 330 return !(data->chip->flags & IRQCHIP_MOVE_DEFERRED); 331 } 332 static inline bool irq_move_pending(struct irq_data *data) 333 { 334 return irqd_is_setaffinity_pending(data); 335 } 336 static inline void 337 irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask) 338 { 339 cpumask_copy(desc->pending_mask, mask); 340 } 341 static inline void 342 irq_get_pending(struct cpumask *mask, struct irq_desc *desc) 343 { 344 cpumask_copy(mask, desc->pending_mask); 345 } 346 static inline struct cpumask *irq_desc_get_pending_mask(struct irq_desc *desc) 347 { 348 return desc->pending_mask; 349 } 350 bool irq_fixup_move_pending(struct irq_desc *desc, bool force_clear); 351 void irq_force_complete_move(struct irq_desc *desc); 352 #else /* CONFIG_GENERIC_PENDING_IRQ */ 353 static inline bool irq_can_move_pcntxt(struct irq_data *data) 354 { 355 return true; 356 } 357 static inline bool irq_move_pending(struct irq_data *data) 358 { 359 return false; 360 } 361 static inline void 362 irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask) 363 { 364 } 365 static inline void 366 irq_get_pending(struct cpumask *mask, struct irq_desc *desc) 367 { 368 } 369 static inline struct cpumask *irq_desc_get_pending_mask(struct irq_desc *desc) 370 { 371 return NULL; 372 } 373 static inline bool irq_fixup_move_pending(struct irq_desc *desc, bool fclear) 374 { 375 return false; 376 } 377 static inline void irq_force_complete_move(struct irq_desc *desc) { } 378 #endif /* !CONFIG_GENERIC_PENDING_IRQ */ 379 380 #if !defined(CONFIG_IRQ_DOMAIN) || !defined(CONFIG_IRQ_DOMAIN_HIERARCHY) 381 static inline int irq_domain_activate_irq(struct irq_data *data, bool reserve) 382 { 383 irqd_set_activated(data); 384 return 0; 385 } 386 static inline void irq_domain_deactivate_irq(struct irq_data *data) 387 { 388 irqd_clr_activated(data); 389 } 390 #endif 391 392 static inline struct irq_data *irqd_get_parent_data(struct irq_data *irqd) 393 { 394 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY 395 return irqd->parent_data; 396 #else 397 return NULL; 398 #endif 399 } 400