1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar 4 * Copyright (C) 2005-2006, Thomas Gleixner, Russell King 5 * 6 * This file contains the interrupt descriptor management code. Detailed 7 * information is available in Documentation/core-api/genericirq.rst 8 * 9 */ 10 #include <linux/irq.h> 11 #include <linux/slab.h> 12 #include <linux/export.h> 13 #include <linux/interrupt.h> 14 #include <linux/kernel_stat.h> 15 #include <linux/maple_tree.h> 16 #include <linux/irqdomain.h> 17 #include <linux/sysfs.h> 18 #include <linux/string_choices.h> 19 20 #include "internals.h" 21 22 /* 23 * lockdep: we want to handle all irq_desc locks as a single lock-class: 24 */ 25 static struct lock_class_key irq_desc_lock_class; 26 27 #if defined(CONFIG_SMP) 28 static int __init irq_affinity_setup(char *str) 29 { 30 alloc_bootmem_cpumask_var(&irq_default_affinity); 31 if (cpulist_parse(str, irq_default_affinity) < 0) { 32 /* 33 * Clear the mask so that init_irq_default_affinity() 34 * defaults it later. 35 */ 36 cpumask_clear(irq_default_affinity); 37 pr_warn("irqaffinity: incorrect CPU range, using default\n"); 38 return 1; 39 } 40 /* 41 * Set at least the boot cpu. We don't want to end up with 42 * bugreports caused by random commandline masks 43 */ 44 cpumask_set_cpu(smp_processor_id(), irq_default_affinity); 45 return 1; 46 } 47 __setup("irqaffinity=", irq_affinity_setup); 48 49 static void __init init_irq_default_affinity(void) 50 { 51 if (!cpumask_available(irq_default_affinity)) 52 zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT); 53 if (cpumask_empty(irq_default_affinity)) 54 cpumask_setall(irq_default_affinity); 55 } 56 #else 57 static void __init init_irq_default_affinity(void) 58 { 59 } 60 #endif 61 62 #ifdef CONFIG_SMP 63 static int alloc_masks(struct irq_desc *desc, int node) 64 { 65 if (!zalloc_cpumask_var_node(&desc->irq_common_data.affinity, 66 GFP_KERNEL, node)) 67 return -ENOMEM; 68 69 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK 70 if (!zalloc_cpumask_var_node(&desc->irq_common_data.effective_affinity, 71 GFP_KERNEL, node)) { 72 free_cpumask_var(desc->irq_common_data.affinity); 73 return -ENOMEM; 74 } 75 #endif 76 77 #ifdef CONFIG_GENERIC_PENDING_IRQ 78 if (!zalloc_cpumask_var_node(&desc->pending_mask, GFP_KERNEL, node)) { 79 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK 80 free_cpumask_var(desc->irq_common_data.effective_affinity); 81 #endif 82 free_cpumask_var(desc->irq_common_data.affinity); 83 return -ENOMEM; 84 } 85 #endif 86 return 0; 87 } 88 89 static void irq_redirect_work(struct irq_work *work) 90 { 91 handle_irq_desc(container_of(work, struct irq_desc, redirect.work)); 92 } 93 94 static void desc_smp_init(struct irq_desc *desc, int node, const struct cpumask *affinity) 95 { 96 if (!affinity) 97 affinity = irq_default_affinity; 98 cpumask_copy(desc->irq_common_data.affinity, affinity); 99 100 #ifdef CONFIG_GENERIC_PENDING_IRQ 101 cpumask_clear(desc->pending_mask); 102 #endif 103 #ifdef CONFIG_NUMA 104 desc->irq_common_data.node = node; 105 #endif 106 desc->redirect.work = IRQ_WORK_INIT_HARD(irq_redirect_work); 107 } 108 109 static void free_masks(struct irq_desc *desc) 110 { 111 #ifdef CONFIG_GENERIC_PENDING_IRQ 112 free_cpumask_var(desc->pending_mask); 113 #endif 114 free_cpumask_var(desc->irq_common_data.affinity); 115 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK 116 free_cpumask_var(desc->irq_common_data.effective_affinity); 117 #endif 118 } 119 120 #else 121 static inline int 122 alloc_masks(struct irq_desc *desc, int node) { return 0; } 123 static inline void 124 desc_smp_init(struct irq_desc *desc, int node, const struct cpumask *affinity) { } 125 static inline void free_masks(struct irq_desc *desc) { } 126 #endif 127 128 static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node, 129 const struct cpumask *affinity, struct module *owner) 130 { 131 desc->irq_common_data.handler_data = NULL; 132 desc->irq_common_data.msi_desc = NULL; 133 134 desc->irq_data.common = &desc->irq_common_data; 135 desc->irq_data.irq = irq; 136 desc->irq_data.chip = &no_irq_chip; 137 desc->irq_data.chip_data = NULL; 138 irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS); 139 irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED); 140 irqd_set(&desc->irq_data, IRQD_IRQ_MASKED); 141 desc->handle_irq = handle_bad_irq; 142 desc->depth = 1; 143 desc->irq_count = 0; 144 desc->irqs_unhandled = 0; 145 desc->tot_count = 0; 146 desc->name = NULL; 147 desc->owner = owner; 148 rcuref_init(&desc->refcnt, 1); 149 desc_smp_init(desc, node, affinity); 150 } 151 152 unsigned int total_nr_irqs __read_mostly = NR_IRQS; 153 154 /** 155 * irq_get_nr_irqs() - Number of interrupts supported by the system. 156 */ 157 unsigned int irq_get_nr_irqs(void) 158 { 159 return total_nr_irqs; 160 } 161 EXPORT_SYMBOL_GPL(irq_get_nr_irqs); 162 163 /** 164 * irq_set_nr_irqs() - Set the number of interrupts supported by the system. 165 * @nr: New number of interrupts. 166 * 167 * Return: @nr. 168 */ 169 unsigned int __init irq_set_nr_irqs(unsigned int nr) 170 { 171 total_nr_irqs = nr; 172 irq_proc_calc_prec(); 173 return nr; 174 } 175 176 static DEFINE_MUTEX(sparse_irq_lock); 177 static struct maple_tree sparse_irqs = MTREE_INIT_EXT(sparse_irqs, 178 MT_FLAGS_ALLOC_RANGE | 179 MT_FLAGS_LOCK_EXTERN | 180 MT_FLAGS_USE_RCU, 181 sparse_irq_lock); 182 183 static int irq_find_free_area(unsigned int from, unsigned int cnt) 184 { 185 MA_STATE(mas, &sparse_irqs, 0, 0); 186 187 if (mas_empty_area(&mas, from, MAX_SPARSE_IRQS, cnt)) 188 return -ENOSPC; 189 return mas.index; 190 } 191 192 struct irq_desc *irq_find_desc_at_or_after(unsigned int offset) 193 { 194 unsigned long index = offset; 195 196 lockdep_assert_in_rcu_read_lock(); 197 return mt_find(&sparse_irqs, &index, total_nr_irqs); 198 } 199 200 static void irq_insert_desc(unsigned int irq, struct irq_desc *desc) 201 { 202 MA_STATE(mas, &sparse_irqs, irq, irq); 203 WARN_ON(mas_store_gfp(&mas, desc, GFP_KERNEL) != 0); 204 } 205 206 static void delete_irq_desc(unsigned int irq) 207 { 208 MA_STATE(mas, &sparse_irqs, irq, irq); 209 mas_erase(&mas); 210 } 211 212 #ifdef CONFIG_SPARSE_IRQ 213 static const struct kobj_type irq_kobj_type; 214 #endif 215 216 static int init_desc(struct irq_desc *desc, int irq, int node, 217 unsigned int flags, 218 const struct cpumask *affinity, 219 struct module *owner) 220 { 221 desc->kstat_irqs = alloc_percpu(struct irqstat); 222 if (!desc->kstat_irqs) 223 return -ENOMEM; 224 225 if (alloc_masks(desc, node)) { 226 free_percpu(desc->kstat_irqs); 227 return -ENOMEM; 228 } 229 230 raw_spin_lock_init(&desc->lock); 231 lockdep_set_class(&desc->lock, &irq_desc_lock_class); 232 mutex_init(&desc->request_mutex); 233 init_waitqueue_head(&desc->wait_for_threads); 234 desc_set_defaults(irq, desc, node, affinity, owner); 235 irqd_set(&desc->irq_data, flags); 236 irq_resend_init(desc); 237 #ifdef CONFIG_SPARSE_IRQ 238 kobject_init(&desc->kobj, &irq_kobj_type); 239 init_rcu_head(&desc->rcu); 240 #endif 241 242 return 0; 243 } 244 245 #ifdef CONFIG_SPARSE_IRQ 246 247 static void irq_kobj_release(struct kobject *kobj); 248 249 #ifdef CONFIG_SYSFS 250 static struct kobject *irq_kobj_base; 251 252 #define IRQ_ATTR_RO(_name) \ 253 static struct kobj_attribute _name##_attr = __ATTR_RO(_name) 254 255 static ssize_t per_cpu_count_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 256 { 257 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj); 258 ssize_t ret = 0; 259 char *p = ""; 260 int cpu; 261 262 for_each_possible_cpu(cpu) { 263 unsigned int c = irq_desc_kstat_cpu(desc, cpu); 264 265 ret += sysfs_emit_at(buf, ret, "%s%u", p, c); 266 p = ","; 267 } 268 269 ret += sysfs_emit_at(buf, ret, "\n"); 270 return ret; 271 } 272 IRQ_ATTR_RO(per_cpu_count); 273 274 static ssize_t chip_name_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 275 { 276 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj); 277 278 guard(raw_spinlock_irq)(&desc->lock); 279 if (desc->irq_data.chip && desc->irq_data.chip->name) 280 return sysfs_emit(buf, "%s\n", desc->irq_data.chip->name); 281 return 0; 282 } 283 IRQ_ATTR_RO(chip_name); 284 285 static ssize_t hwirq_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 286 { 287 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj); 288 289 guard(raw_spinlock_irq)(&desc->lock); 290 if (desc->irq_data.domain) 291 return sysfs_emit(buf, "%lu\n", desc->irq_data.hwirq); 292 return 0; 293 } 294 IRQ_ATTR_RO(hwirq); 295 296 static ssize_t type_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 297 { 298 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj); 299 300 guard(raw_spinlock_irq)(&desc->lock); 301 return sysfs_emit(buf, "%s\n", irqd_is_level_type(&desc->irq_data) ? "level" : "edge"); 302 303 } 304 IRQ_ATTR_RO(type); 305 306 static ssize_t wakeup_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 307 { 308 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj); 309 310 guard(raw_spinlock_irq)(&desc->lock); 311 return sysfs_emit(buf, "%s\n", str_enabled_disabled(irqd_is_wakeup_set(&desc->irq_data))); 312 } 313 IRQ_ATTR_RO(wakeup); 314 315 static ssize_t name_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 316 { 317 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj); 318 319 guard(raw_spinlock_irq)(&desc->lock); 320 if (desc->name) 321 return sysfs_emit(buf, "%s\n", desc->name); 322 return 0; 323 } 324 IRQ_ATTR_RO(name); 325 326 static ssize_t actions_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) 327 { 328 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj); 329 struct irqaction *action; 330 ssize_t ret = 0; 331 char *p = ""; 332 333 scoped_guard(raw_spinlock_irq, &desc->lock) { 334 for_each_action_of_desc(desc, action) { 335 ret += sysfs_emit_at(buf, ret, "%s%s", p, action->name); 336 p = ","; 337 } 338 } 339 340 if (ret) 341 ret += sysfs_emit_at(buf, ret, "\n"); 342 return ret; 343 } 344 IRQ_ATTR_RO(actions); 345 346 static struct attribute *irq_attrs[] = { 347 &per_cpu_count_attr.attr, 348 &chip_name_attr.attr, 349 &hwirq_attr.attr, 350 &type_attr.attr, 351 &wakeup_attr.attr, 352 &name_attr.attr, 353 &actions_attr.attr, 354 NULL 355 }; 356 ATTRIBUTE_GROUPS(irq); 357 358 static const struct kobj_type irq_kobj_type = { 359 .release = irq_kobj_release, 360 .sysfs_ops = &kobj_sysfs_ops, 361 .default_groups = irq_groups, 362 }; 363 364 static void irq_sysfs_add(int irq, struct irq_desc *desc) 365 { 366 if (irq_kobj_base) { 367 /* 368 * Continue even in case of failure as this is nothing 369 * crucial and failures in the late irq_sysfs_init() 370 * cannot be rolled back. 371 */ 372 if (kobject_add(&desc->kobj, irq_kobj_base, "%d", irq)) 373 pr_warn("Failed to add kobject for irq %d\n", irq); 374 else 375 desc->istate |= IRQS_SYSFS; 376 } 377 } 378 379 static void irq_sysfs_del(struct irq_desc *desc) 380 { 381 /* 382 * Only invoke kobject_del() when kobject_add() was successfully 383 * invoked for the descriptor. This covers both early boot, where 384 * sysfs is not initialized yet, and the case of a failed 385 * kobject_add() invocation. 386 */ 387 if (desc->istate & IRQS_SYSFS) 388 kobject_del(&desc->kobj); 389 } 390 391 static int __init irq_sysfs_init(void) 392 { 393 struct irq_desc *desc; 394 int irq; 395 396 /* Prevent concurrent irq alloc/free */ 397 guard(mutex)(&sparse_irq_lock); 398 irq_kobj_base = kobject_create_and_add("irq", kernel_kobj); 399 if (!irq_kobj_base) 400 return -ENOMEM; 401 402 /* Add the already allocated interrupts */ 403 for_each_irq_desc(irq, desc) 404 irq_sysfs_add(irq, desc); 405 return 0; 406 } 407 postcore_initcall(irq_sysfs_init); 408 409 #else /* !CONFIG_SYSFS */ 410 411 static const struct kobj_type irq_kobj_type = { 412 .release = irq_kobj_release, 413 }; 414 415 static void irq_sysfs_add(int irq, struct irq_desc *desc) {} 416 static void irq_sysfs_del(struct irq_desc *desc) {} 417 418 #endif /* CONFIG_SYSFS */ 419 420 struct irq_desc *irq_to_desc(unsigned int irq) 421 { 422 return mtree_load(&sparse_irqs, irq); 423 } 424 #ifdef CONFIG_KVM_BOOK3S_64_HV_MODULE 425 EXPORT_SYMBOL_GPL(irq_to_desc); 426 #endif 427 428 void irq_lock_sparse(void) 429 { 430 mutex_lock(&sparse_irq_lock); 431 } 432 433 void irq_unlock_sparse(void) 434 { 435 mutex_unlock(&sparse_irq_lock); 436 } 437 438 static struct irq_desc *alloc_desc(int irq, int node, unsigned int flags, 439 const struct cpumask *affinity, 440 struct module *owner) 441 { 442 struct irq_desc *desc; 443 int ret; 444 445 desc = kzalloc_node(sizeof(*desc), GFP_KERNEL, node); 446 if (!desc) 447 return NULL; 448 449 ret = init_desc(desc, irq, node, flags, affinity, owner); 450 if (unlikely(ret)) { 451 kfree(desc); 452 return NULL; 453 } 454 455 return desc; 456 } 457 458 static void irq_kobj_release(struct kobject *kobj) 459 { 460 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj); 461 462 free_masks(desc); 463 free_percpu(desc->kstat_irqs); 464 kfree(desc); 465 } 466 467 static void delayed_free_desc(struct rcu_head *rhp) 468 { 469 struct irq_desc *desc = container_of(rhp, struct irq_desc, rcu); 470 471 kobject_put(&desc->kobj); 472 } 473 474 void irq_desc_free_rcu(struct irq_desc *desc) 475 { 476 /* 477 * We free the descriptor, masks and stat fields via RCU. That 478 * allows demultiplex interrupts to do rcu based management of 479 * the child interrupts. 480 * This also allows us to use rcu in kstat_irqs_usr(). 481 */ 482 call_rcu(&desc->rcu, delayed_free_desc); 483 } 484 485 static void free_desc(unsigned int irq) 486 { 487 struct irq_desc *desc = irq_to_desc(irq); 488 489 irq_remove_debugfs_entry(desc); 490 unregister_irq_proc(irq, desc); 491 492 /* 493 * sparse_irq_lock protects also show_interrupts() and 494 * kstat_irq_usr(). Once we deleted the descriptor from the 495 * sparse tree we can free it. Access in proc will fail to 496 * lookup the descriptor. 497 * 498 * The sysfs entry must be serialized against a concurrent 499 * irq_sysfs_init() as well. 500 */ 501 irq_sysfs_del(desc); 502 delete_irq_desc(irq); 503 irq_desc_put_ref(desc); 504 } 505 506 static int alloc_descs(unsigned int start, unsigned int cnt, int node, 507 const struct irq_affinity_desc *affinity, 508 struct module *owner) 509 { 510 struct irq_desc *desc; 511 int i; 512 513 /* Validate affinity mask(s) */ 514 if (affinity) { 515 for (i = 0; i < cnt; i++) { 516 if (cpumask_empty(&affinity[i].mask)) 517 return -EINVAL; 518 } 519 } 520 521 for (i = 0; i < cnt; i++) { 522 const struct cpumask *mask = NULL; 523 unsigned int flags = 0; 524 525 if (affinity) { 526 if (affinity->is_managed) { 527 flags = IRQD_AFFINITY_MANAGED | 528 IRQD_MANAGED_SHUTDOWN; 529 } 530 flags |= IRQD_AFFINITY_SET; 531 mask = &affinity->mask; 532 node = cpu_to_node(cpumask_first(mask)); 533 affinity++; 534 } 535 536 desc = alloc_desc(start + i, node, flags, mask, owner); 537 if (!desc) 538 goto err; 539 irq_insert_desc(start + i, desc); 540 irq_sysfs_add(start + i, desc); 541 irq_add_debugfs_entry(start + i, desc); 542 } 543 return start; 544 545 err: 546 for (i--; i >= 0; i--) 547 free_desc(start + i); 548 return -ENOMEM; 549 } 550 551 static bool irq_expand_nr_irqs(unsigned int nr) 552 { 553 if (nr > MAX_SPARSE_IRQS) 554 return false; 555 total_nr_irqs = nr; 556 irq_proc_calc_prec(); 557 return true; 558 } 559 560 int __init early_irq_init(void) 561 { 562 int i, initcnt, node = first_online_node; 563 struct irq_desc *desc; 564 565 init_irq_default_affinity(); 566 567 /* Let arch update nr_irqs and return the nr of preallocated irqs */ 568 initcnt = arch_probe_nr_irqs(); 569 printk(KERN_INFO "NR_IRQS: %d, nr_irqs: %d, preallocated irqs: %d\n", 570 NR_IRQS, total_nr_irqs, initcnt); 571 572 if (WARN_ON(total_nr_irqs > MAX_SPARSE_IRQS)) 573 total_nr_irqs = MAX_SPARSE_IRQS; 574 575 if (WARN_ON(initcnt > MAX_SPARSE_IRQS)) 576 initcnt = MAX_SPARSE_IRQS; 577 578 if (initcnt > total_nr_irqs) 579 total_nr_irqs = initcnt; 580 581 for (i = 0; i < initcnt; i++) { 582 desc = alloc_desc(i, node, 0, NULL, NULL); 583 irq_insert_desc(i, desc); 584 } 585 irq_proc_calc_prec(); 586 return arch_early_irq_init(); 587 } 588 589 #else /* !CONFIG_SPARSE_IRQ */ 590 591 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = { 592 [0 ... NR_IRQS-1] = { 593 .handle_irq = handle_bad_irq, 594 .depth = 1, 595 .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock), 596 } 597 }; 598 599 int __init early_irq_init(void) 600 { 601 int count, i, node = first_online_node; 602 int ret; 603 604 init_irq_default_affinity(); 605 606 pr_info("NR_IRQS: %d\n", NR_IRQS); 607 608 count = ARRAY_SIZE(irq_desc); 609 610 for (i = 0; i < count; i++) { 611 ret = init_desc(irq_desc + i, i, node, 0, NULL, NULL); 612 if (unlikely(ret)) 613 goto __free_desc_res; 614 } 615 616 irq_proc_calc_prec(); 617 return arch_early_irq_init(); 618 619 __free_desc_res: 620 while (--i >= 0) { 621 free_masks(irq_desc + i); 622 free_percpu(irq_desc[i].kstat_irqs); 623 } 624 625 return ret; 626 } 627 628 struct irq_desc *irq_to_desc(unsigned int irq) 629 { 630 return (irq < NR_IRQS) ? irq_desc + irq : NULL; 631 } 632 EXPORT_SYMBOL(irq_to_desc); 633 634 static void free_desc(unsigned int irq) 635 { 636 struct irq_desc *desc = irq_to_desc(irq); 637 int cpu; 638 639 scoped_guard(raw_spinlock_irqsave, &desc->lock) 640 desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL); 641 642 for_each_possible_cpu(cpu) 643 *per_cpu_ptr(desc->kstat_irqs, cpu) = (struct irqstat) { }; 644 645 delete_irq_desc(irq); 646 } 647 648 static inline int alloc_descs(unsigned int start, unsigned int cnt, int node, 649 const struct irq_affinity_desc *affinity, 650 struct module *owner) 651 { 652 u32 i; 653 654 for (i = 0; i < cnt; i++) { 655 struct irq_desc *desc = irq_to_desc(start + i); 656 657 desc->owner = owner; 658 irq_insert_desc(start + i, desc); 659 } 660 return start; 661 } 662 663 static inline bool irq_expand_nr_irqs(unsigned int nr) 664 { 665 return false; 666 } 667 668 void irq_mark_irq(unsigned int irq) 669 { 670 guard(mutex)(&sparse_irq_lock); 671 irq_insert_desc(irq, irq_desc + irq); 672 } 673 674 #endif /* !CONFIG_SPARSE_IRQ */ 675 676 int handle_irq_desc(struct irq_desc *desc) 677 { 678 struct irq_data *data; 679 680 if (!desc) 681 return -EINVAL; 682 683 data = irq_desc_get_irq_data(desc); 684 if (WARN_ON_ONCE(!in_hardirq() && irqd_is_handle_enforce_irqctx(data))) 685 return -EPERM; 686 687 generic_handle_irq_desc(desc); 688 return 0; 689 } 690 691 /** 692 * generic_handle_irq - Invoke the handler for a particular irq 693 * @irq: The irq number to handle 694 * 695 * Returns: 0 on success, or -EINVAL if conversion has failed 696 * 697 * This function must be called from an IRQ context with irq regs 698 * initialized. 699 */ 700 int generic_handle_irq(unsigned int irq) 701 { 702 return handle_irq_desc(irq_to_desc(irq)); 703 } 704 EXPORT_SYMBOL_GPL(generic_handle_irq); 705 706 /** 707 * generic_handle_irq_safe - Invoke the handler for a particular irq from any 708 * context. 709 * @irq: The irq number to handle 710 * 711 * Returns: 0 on success, a negative value on error. 712 * 713 * This function can be called from any context (IRQ or process context). It 714 * will report an error if not invoked from IRQ context and the irq has been 715 * marked to enforce IRQ-context only. 716 */ 717 int generic_handle_irq_safe(unsigned int irq) 718 { 719 unsigned long flags; 720 int ret; 721 722 local_irq_save(flags); 723 ret = handle_irq_desc(irq_to_desc(irq)); 724 local_irq_restore(flags); 725 return ret; 726 } 727 EXPORT_SYMBOL_GPL(generic_handle_irq_safe); 728 729 #ifdef CONFIG_IRQ_DOMAIN 730 /** 731 * generic_handle_domain_irq - Invoke the handler for a HW irq belonging 732 * to a domain. 733 * @domain: The domain where to perform the lookup 734 * @hwirq: The HW irq number to convert to a logical one 735 * 736 * Returns: 0 on success, or -EINVAL if conversion has failed 737 * 738 * This function must be called from an IRQ context with irq regs 739 * initialized. 740 */ 741 int generic_handle_domain_irq(struct irq_domain *domain, irq_hw_number_t hwirq) 742 { 743 return handle_irq_desc(irq_resolve_mapping(domain, hwirq)); 744 } 745 EXPORT_SYMBOL_GPL(generic_handle_domain_irq); 746 747 /** 748 * generic_handle_irq_safe - Invoke the handler for a HW irq belonging 749 * to a domain from any context. 750 * @domain: The domain where to perform the lookup 751 * @hwirq: The HW irq number to convert to a logical one 752 * 753 * Returns: 0 on success, a negative value on error. 754 * 755 * This function can be called from any context (IRQ or process 756 * context). If the interrupt is marked as 'enforce IRQ-context only' then 757 * the function must be invoked from hard interrupt context. 758 */ 759 int generic_handle_domain_irq_safe(struct irq_domain *domain, irq_hw_number_t hwirq) 760 { 761 unsigned long flags; 762 int ret; 763 764 local_irq_save(flags); 765 ret = handle_irq_desc(irq_resolve_mapping(domain, hwirq)); 766 local_irq_restore(flags); 767 return ret; 768 } 769 EXPORT_SYMBOL_GPL(generic_handle_domain_irq_safe); 770 771 /** 772 * generic_handle_domain_nmi - Invoke the handler for a HW nmi belonging 773 * to a domain. 774 * @domain: The domain where to perform the lookup 775 * @hwirq: The HW irq number to convert to a logical one 776 * 777 * Returns: 0 on success, or -EINVAL if conversion has failed 778 * 779 * This function must be called from an NMI context with irq regs 780 * initialized. 781 **/ 782 int generic_handle_domain_nmi(struct irq_domain *domain, irq_hw_number_t hwirq) 783 { 784 WARN_ON_ONCE(!in_nmi()); 785 return handle_irq_desc(irq_resolve_mapping(domain, hwirq)); 786 } 787 788 #ifdef CONFIG_SMP 789 static bool demux_redirect_remote(struct irq_desc *desc) 790 { 791 guard(raw_spinlock)(&desc->lock); 792 const struct cpumask *m = irq_data_get_effective_affinity_mask(&desc->irq_data); 793 unsigned int target_cpu = READ_ONCE(desc->redirect.target_cpu); 794 795 if (desc->irq_data.chip->irq_pre_redirect) 796 desc->irq_data.chip->irq_pre_redirect(&desc->irq_data); 797 798 /* 799 * If the interrupt handler is already running on a CPU that's included 800 * in the interrupt's affinity mask, redirection is not necessary. 801 */ 802 if (cpumask_test_cpu(smp_processor_id(), m)) 803 return false; 804 805 /* 806 * The desc->action check protects against IRQ shutdown: __free_irq() sets 807 * desc->action to NULL while holding desc->lock, which we also hold. 808 * 809 * Calling irq_work_queue_on() here is safe w.r.t. CPU unplugging: 810 * - takedown_cpu() schedules multi_cpu_stop() on all active CPUs, 811 * including the one that's taken down. 812 * - multi_cpu_stop() acts like a barrier, which means all active 813 * CPUs go through MULTI_STOP_DISABLE_IRQ and disable hard IRQs 814 * *before* the dying CPU runs take_cpu_down() in MULTI_STOP_RUN. 815 * - Hard IRQs are re-enabled at the end of multi_cpu_stop(), *after* 816 * the dying CPU has run take_cpu_down() in MULTI_STOP_RUN. 817 * - Since we run in hard IRQ context, we run either before or after 818 * take_cpu_down() but never concurrently. 819 * - If we run before take_cpu_down(), the dying CPU hasn't been marked 820 * offline yet (it's marked via take_cpu_down() -> __cpu_disable()), 821 * so the WARN in irq_work_queue_on() can't occur. 822 * - Furthermore, the work item we queue will be flushed later via 823 * take_cpu_down() -> cpuhp_invoke_callback_range_nofail() -> 824 * smpcfd_dying_cpu() -> irq_work_run(). 825 * - If we run after take_cpu_down(), target_cpu has been already 826 * updated via take_cpu_down() -> __cpu_disable(), which eventually 827 * calls irq_do_set_affinity() during IRQ migration. So, target_cpu 828 * no longer points to the dying CPU in this case. 829 */ 830 if (desc->action) 831 irq_work_queue_on(&desc->redirect.work, target_cpu); 832 833 return true; 834 } 835 #else /* CONFIG_SMP */ 836 static bool demux_redirect_remote(struct irq_desc *desc) 837 { 838 return false; 839 } 840 #endif 841 842 /** 843 * generic_handle_demux_domain_irq - Invoke the handler for a hardware interrupt 844 * of a demultiplexing domain. 845 * @domain: The domain where to perform the lookup 846 * @hwirq: The hardware interrupt number to convert to a logical one 847 * 848 * Returns: True on success, or false if lookup has failed 849 */ 850 bool generic_handle_demux_domain_irq(struct irq_domain *domain, irq_hw_number_t hwirq) 851 { 852 struct irq_desc *desc = irq_resolve_mapping(domain, hwirq); 853 854 if (unlikely(!desc)) 855 return false; 856 857 if (demux_redirect_remote(desc)) 858 return true; 859 860 return !handle_irq_desc(desc); 861 } 862 EXPORT_SYMBOL_GPL(generic_handle_demux_domain_irq); 863 864 #endif 865 866 /* Dynamic interrupt handling */ 867 868 /** 869 * irq_free_descs - free irq descriptors 870 * @from: Start of descriptor range 871 * @cnt: Number of consecutive irqs to free 872 */ 873 void irq_free_descs(unsigned int from, unsigned int cnt) 874 { 875 int i; 876 877 if (from >= total_nr_irqs || (from + cnt) > total_nr_irqs) 878 return; 879 880 guard(mutex)(&sparse_irq_lock); 881 for (i = 0; i < cnt; i++) 882 free_desc(from + i); 883 } 884 EXPORT_SYMBOL_GPL(irq_free_descs); 885 886 /** 887 * __irq_alloc_descs - allocate and initialize a range of irq descriptors 888 * @irq: Allocate for specific irq number if irq >= 0 889 * @from: Start the search from this irq number 890 * @cnt: Number of consecutive irqs to allocate. 891 * @node: Preferred node on which the irq descriptor should be allocated 892 * @owner: Owning module (can be NULL) 893 * @affinity: Optional pointer to an affinity mask array of size @cnt which 894 * hints where the irq descriptors should be allocated and which 895 * default affinities to use 896 * 897 * Returns the first irq number or error code 898 */ 899 int __ref __irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node, 900 struct module *owner, const struct irq_affinity_desc *affinity) 901 { 902 int start; 903 904 if (!cnt) 905 return -EINVAL; 906 907 if (irq >= 0) { 908 if (from > irq) 909 return -EINVAL; 910 from = irq; 911 } else { 912 /* 913 * For interrupts which are freely allocated the 914 * architecture can force a lower bound to the @from 915 * argument. x86 uses this to exclude the GSI space. 916 */ 917 from = arch_dynirq_lower_bound(from); 918 } 919 920 guard(mutex)(&sparse_irq_lock); 921 922 start = irq_find_free_area(from, cnt); 923 if (irq >=0 && start != irq) 924 return -EEXIST; 925 926 if (start + cnt > total_nr_irqs) { 927 if (!irq_expand_nr_irqs(start + cnt)) 928 return -ENOMEM; 929 } 930 return alloc_descs(start, cnt, node, affinity, owner); 931 } 932 EXPORT_SYMBOL_GPL(__irq_alloc_descs); 933 934 /** 935 * irq_get_next_irq - get next allocated irq number 936 * @offset: where to start the search 937 * 938 * Returns next irq number after offset or total_nr_irqs if none is found. 939 */ 940 unsigned int irq_get_next_irq(unsigned int offset) 941 { 942 struct irq_desc *desc; 943 944 guard(rcu)(); 945 desc = irq_find_desc_at_or_after(offset); 946 return desc ? irq_desc_get_irq(desc) : total_nr_irqs; 947 } 948 949 struct irq_desc *__irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus, 950 unsigned int check) 951 { 952 struct irq_desc *desc; 953 954 desc = irq_to_desc(irq); 955 if (!desc) 956 return NULL; 957 958 if (check & _IRQ_DESC_CHECK) { 959 if ((check & _IRQ_DESC_PERCPU) && !irq_settings_is_per_cpu_devid(desc)) 960 return NULL; 961 962 if (!(check & _IRQ_DESC_PERCPU) && irq_settings_is_per_cpu_devid(desc)) 963 return NULL; 964 } 965 966 if (bus) 967 chip_bus_lock(desc); 968 raw_spin_lock_irqsave(&desc->lock, *flags); 969 970 return desc; 971 } 972 973 void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus) 974 __releases(&desc->lock) 975 { 976 raw_spin_unlock_irqrestore(&desc->lock, flags); 977 if (bus) 978 chip_bus_sync_unlock(desc); 979 } 980 981 int irq_set_percpu_devid(unsigned int irq) 982 { 983 struct irq_desc *desc = irq_to_desc(irq); 984 985 if (!desc || desc->percpu_enabled) 986 return -EINVAL; 987 988 desc->percpu_enabled = kzalloc_obj(*desc->percpu_enabled); 989 990 if (!desc->percpu_enabled) 991 return -ENOMEM; 992 993 irq_set_percpu_devid_flags(irq); 994 return 0; 995 } 996 997 void kstat_incr_irq_this_cpu(unsigned int irq) 998 { 999 kstat_incr_irqs_this_cpu(irq_to_desc(irq)); 1000 } 1001 1002 /** 1003 * kstat_irqs_cpu - Get the statistics for an interrupt on a cpu 1004 * @irq: The interrupt number 1005 * @cpu: The cpu number 1006 * 1007 * Returns the sum of interrupt counts on @cpu since boot for 1008 * @irq. The caller must ensure that the interrupt is not removed 1009 * concurrently. 1010 */ 1011 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu) 1012 { 1013 struct irq_desc *desc = irq_to_desc(irq); 1014 1015 return desc ? irq_desc_kstat_cpu(desc, cpu) : 0; 1016 } 1017 1018 static unsigned int kstat_irqs_desc(struct irq_desc *desc, const struct cpumask *cpumask) 1019 { 1020 unsigned int sum = 0; 1021 int cpu; 1022 1023 if (!irq_settings_is_per_cpu_devid(desc) && 1024 !irq_settings_is_per_cpu(desc) && 1025 !irq_is_nmi(desc)) 1026 return data_race(desc->tot_count); 1027 1028 for_each_cpu(cpu, cpumask) 1029 sum += data_race(per_cpu(desc->kstat_irqs->cnt, cpu)); 1030 return sum; 1031 } 1032 1033 static unsigned int kstat_irqs(unsigned int irq) 1034 { 1035 struct irq_desc *desc = irq_to_desc(irq); 1036 1037 if (!desc) 1038 return 0; 1039 return kstat_irqs_desc(desc, cpu_possible_mask); 1040 } 1041 1042 #ifdef CONFIG_GENERIC_IRQ_STAT_SNAPSHOT 1043 1044 void kstat_snapshot_irqs(void) 1045 { 1046 struct irq_desc *desc; 1047 unsigned int irq; 1048 1049 for_each_irq_desc(irq, desc) 1050 this_cpu_write(desc->kstat_irqs->ref, this_cpu_read(desc->kstat_irqs->cnt)); 1051 } 1052 1053 unsigned int kstat_get_irq_since_snapshot(unsigned int irq) 1054 { 1055 struct irq_desc *desc = irq_to_desc(irq); 1056 1057 if (!desc) 1058 return 0; 1059 return this_cpu_read(desc->kstat_irqs->cnt) - this_cpu_read(desc->kstat_irqs->ref); 1060 } 1061 1062 #endif 1063 1064 /** 1065 * kstat_irqs_usr - Get the statistics for an interrupt from thread context 1066 * @irq: The interrupt number 1067 * 1068 * Returns the sum of interrupt counts on all cpus since boot for @irq. 1069 * 1070 * It uses rcu to protect the access since a concurrent removal of an 1071 * interrupt descriptor is observing an rcu grace period before 1072 * delayed_free_desc()/irq_kobj_release(). 1073 */ 1074 unsigned int kstat_irqs_usr(unsigned int irq) 1075 { 1076 unsigned int sum; 1077 1078 rcu_read_lock(); 1079 sum = kstat_irqs(irq); 1080 rcu_read_unlock(); 1081 return sum; 1082 } 1083 1084 #ifdef CONFIG_LOCKDEP 1085 void __irq_set_lockdep_class(unsigned int irq, struct lock_class_key *lock_class, 1086 struct lock_class_key *request_class) 1087 { 1088 struct irq_desc *desc = irq_to_desc(irq); 1089 1090 if (desc) { 1091 lockdep_set_class(&desc->lock, lock_class); 1092 lockdep_set_class(&desc->request_mutex, request_class); 1093 } 1094 } 1095 EXPORT_SYMBOL_GPL(__irq_set_lockdep_class); 1096 #endif 1097