1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Based on arch/arm/kernel/irq.c 4 * 5 * Copyright (C) 1992 Linus Torvalds 6 * Modifications for ARM processor Copyright (C) 1995-2000 Russell King. 7 * Support for Dynamic Tick Timer Copyright (C) 2004-2005 Nokia Corporation. 8 * Dynamic Tick Timer written by Tony Lindgren <tony@atomide.com> and 9 * Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>. 10 * Copyright (C) 2012 ARM Ltd. 11 */ 12 13 #include <linux/errno.h> 14 #include <linux/hardirq.h> 15 #include <linux/init.h> 16 #include <linux/irq.h> 17 #include <linux/irqchip.h> 18 #include <linux/kprobes.h> 19 #include <linux/memory.h> 20 #include <linux/scs.h> 21 #include <linux/seq_file.h> 22 #include <linux/smp.h> 23 #include <linux/vmalloc.h> 24 #include <asm/daifflags.h> 25 #include <asm/exception.h> 26 #include <asm/numa.h> 27 #include <asm/softirq_stack.h> 28 #include <asm/stacktrace.h> 29 #include <asm/vmap_stack.h> 30 31 /* Only access this in an NMI enter/exit */ 32 DEFINE_PER_CPU(struct nmi_ctx, nmi_contexts); 33 34 DEFINE_PER_CPU(unsigned long *, irq_stack_ptr); 35 36 DECLARE_PER_CPU(unsigned long *, irq_shadow_call_stack_ptr); 37 38 #ifdef CONFIG_SHADOW_CALL_STACK 39 DEFINE_PER_CPU(unsigned long *, irq_shadow_call_stack_ptr); 40 #endif 41 42 static int __init init_irq_scs(void) 43 { 44 int cpu; 45 void *s; 46 47 if (!scs_is_enabled()) 48 return 0; 49 50 for_each_possible_cpu(cpu) { 51 s = scs_alloc(early_cpu_to_node(cpu)); 52 if (!s) 53 return -ENOMEM; 54 per_cpu(irq_shadow_call_stack_ptr, cpu) = s; 55 } 56 57 return 0; 58 } 59 60 static int __init init_irq_stacks(void) 61 { 62 int cpu; 63 unsigned long *p; 64 65 for_each_possible_cpu(cpu) { 66 p = arch_alloc_vmap_stack(IRQ_STACK_SIZE, early_cpu_to_node(cpu)); 67 if (!p) 68 return -ENOMEM; 69 per_cpu(irq_stack_ptr, cpu) = p; 70 } 71 72 return 0; 73 } 74 75 #ifdef CONFIG_SOFTIRQ_ON_OWN_STACK 76 static void ____do_softirq(struct pt_regs *regs) 77 { 78 __do_softirq(); 79 } 80 81 void do_softirq_own_stack(void) 82 { 83 call_on_irq_stack(NULL, ____do_softirq); 84 } 85 #endif 86 87 static void default_handle_irq(struct pt_regs *regs) 88 { 89 panic("IRQ taken without a root IRQ handler\n"); 90 } 91 92 static void default_handle_fiq(struct pt_regs *regs) 93 { 94 panic("FIQ taken without a root FIQ handler\n"); 95 } 96 97 void (*handle_arch_irq)(struct pt_regs *) __ro_after_init = default_handle_irq; 98 void (*handle_arch_fiq)(struct pt_regs *) __ro_after_init = default_handle_fiq; 99 100 int __init set_handle_irq(void (*handle_irq)(struct pt_regs *)) 101 { 102 if (handle_arch_irq != default_handle_irq) 103 return -EBUSY; 104 105 handle_arch_irq = handle_irq; 106 pr_info("Root IRQ handler: %ps\n", handle_irq); 107 return 0; 108 } 109 110 int __init set_handle_fiq(void (*handle_fiq)(struct pt_regs *)) 111 { 112 if (handle_arch_fiq != default_handle_fiq) 113 return -EBUSY; 114 115 handle_arch_fiq = handle_fiq; 116 pr_info("Root FIQ handler: %ps\n", handle_fiq); 117 return 0; 118 } 119 120 void __init init_IRQ(void) 121 { 122 if (init_irq_stacks() || init_irq_scs()) 123 panic("Failed to allocate IRQ stack resources\n"); 124 125 irqchip_init(); 126 127 if (system_uses_irq_prio_masking()) { 128 /* 129 * Now that we have a stack for our IRQ handler, set 130 * the PMR/PSR pair to a consistent state. 131 */ 132 WARN_ON(read_sysreg(daif) & PSR_A_BIT); 133 local_daif_restore(DAIF_PROCCTX_NOIRQ); 134 } 135 } 136