xref: /openbmc/linux/arch/arm64/kernel/irq.c (revision 3b619e22)
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/irq.h>
14 #include <linux/memory.h>
15 #include <linux/smp.h>
16 #include <linux/hardirq.h>
17 #include <linux/init.h>
18 #include <linux/irqchip.h>
19 #include <linux/kprobes.h>
20 #include <linux/scs.h>
21 #include <linux/seq_file.h>
22 #include <linux/vmalloc.h>
23 #include <asm/daifflags.h>
24 #include <asm/exception.h>
25 #include <asm/vmap_stack.h>
26 #include <asm/softirq_stack.h>
27 
28 /* Only access this in an NMI enter/exit */
29 DEFINE_PER_CPU(struct nmi_ctx, nmi_contexts);
30 
31 DEFINE_PER_CPU(unsigned long *, irq_stack_ptr);
32 
33 
34 DECLARE_PER_CPU(unsigned long *, irq_shadow_call_stack_ptr);
35 
36 #ifdef CONFIG_SHADOW_CALL_STACK
37 DEFINE_PER_CPU(unsigned long *, irq_shadow_call_stack_ptr);
38 #endif
39 
40 static void init_irq_scs(void)
41 {
42 	int cpu;
43 
44 	if (!scs_is_enabled())
45 		return;
46 
47 	for_each_possible_cpu(cpu)
48 		per_cpu(irq_shadow_call_stack_ptr, cpu) =
49 			scs_alloc(cpu_to_node(cpu));
50 }
51 
52 #ifdef CONFIG_VMAP_STACK
53 static void init_irq_stacks(void)
54 {
55 	int cpu;
56 	unsigned long *p;
57 
58 	for_each_possible_cpu(cpu) {
59 		p = arch_alloc_vmap_stack(IRQ_STACK_SIZE, cpu_to_node(cpu));
60 		per_cpu(irq_stack_ptr, cpu) = p;
61 	}
62 }
63 #else
64 /* irq stack only needs to be 16 byte aligned - not IRQ_STACK_SIZE aligned. */
65 DEFINE_PER_CPU_ALIGNED(unsigned long [IRQ_STACK_SIZE/sizeof(long)], irq_stack);
66 
67 static void init_irq_stacks(void)
68 {
69 	int cpu;
70 
71 	for_each_possible_cpu(cpu)
72 		per_cpu(irq_stack_ptr, cpu) = per_cpu(irq_stack, cpu);
73 }
74 #endif
75 
76 #ifndef CONFIG_PREEMPT_RT
77 static void ____do_softirq(struct pt_regs *regs)
78 {
79 	__do_softirq();
80 }
81 
82 void do_softirq_own_stack(void)
83 {
84 	call_on_irq_stack(NULL, ____do_softirq);
85 }
86 #endif
87 
88 static void default_handle_irq(struct pt_regs *regs)
89 {
90 	panic("IRQ taken without a root IRQ handler\n");
91 }
92 
93 static void default_handle_fiq(struct pt_regs *regs)
94 {
95 	panic("FIQ taken without a root FIQ handler\n");
96 }
97 
98 void (*handle_arch_irq)(struct pt_regs *) __ro_after_init = default_handle_irq;
99 void (*handle_arch_fiq)(struct pt_regs *) __ro_after_init = default_handle_fiq;
100 
101 int __init set_handle_irq(void (*handle_irq)(struct pt_regs *))
102 {
103 	if (handle_arch_irq != default_handle_irq)
104 		return -EBUSY;
105 
106 	handle_arch_irq = handle_irq;
107 	pr_info("Root IRQ handler: %ps\n", handle_irq);
108 	return 0;
109 }
110 
111 int __init set_handle_fiq(void (*handle_fiq)(struct pt_regs *))
112 {
113 	if (handle_arch_fiq != default_handle_fiq)
114 		return -EBUSY;
115 
116 	handle_arch_fiq = handle_fiq;
117 	pr_info("Root FIQ handler: %ps\n", handle_fiq);
118 	return 0;
119 }
120 
121 void __init init_IRQ(void)
122 {
123 	init_irq_stacks();
124 	init_irq_scs();
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