blob: bda49430c9ea3c9beb0669689e760bc6bd26c3ce [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +00002/*
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.
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +000011 */
12
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +000013#include <linux/irq.h>
Mark Rutlande3067862017-07-21 14:25:33 +010014#include <linux/memory.h>
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +000015#include <linux/smp.h>
Marc Zyngiera2638812020-06-20 17:19:00 +010016#include <linux/hardirq.h>
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +000017#include <linux/init.h>
Catalin Marinase851b582013-01-14 12:39:31 +000018#include <linux/irqchip.h>
Julien Thierry17ce3022019-06-11 10:38:09 +010019#include <linux/kprobes.h>
Sami Tolvanenac20ffb2020-11-30 15:34:42 -080020#include <linux/scs.h>
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +000021#include <linux/seq_file.h>
Mark Rutlande3067862017-07-21 14:25:33 +010022#include <linux/vmalloc.h>
Julien Thierry17ce3022019-06-11 10:38:09 +010023#include <asm/daifflags.h>
James Morseed8b20d2018-01-08 15:38:10 +000024#include <asm/vmap_stack.h>
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +000025
Julien Thierry58709702019-01-31 14:58:39 +000026/* Only access this in an NMI enter/exit */
27DEFINE_PER_CPU(struct nmi_ctx, nmi_contexts);
28
Mark Rutlandf60fe782017-07-31 21:17:03 +010029DEFINE_PER_CPU(unsigned long *, irq_stack_ptr);
AKASHI Takahiro132cd882015-12-04 11:02:26 +000030
Sami Tolvanenac20ffb2020-11-30 15:34:42 -080031
32DECLARE_PER_CPU(unsigned long *, irq_shadow_call_stack_ptr);
33
34#ifdef CONFIG_SHADOW_CALL_STACK
35DEFINE_PER_CPU(unsigned long *, irq_shadow_call_stack_ptr);
36#endif
37
38static void init_irq_scs(void)
39{
40 int cpu;
41
42 if (!IS_ENABLED(CONFIG_SHADOW_CALL_STACK))
43 return;
44
45 for_each_possible_cpu(cpu)
46 per_cpu(irq_shadow_call_stack_ptr, cpu) =
47 scs_alloc(cpu_to_node(cpu));
48}
49
Mark Rutlande3067862017-07-21 14:25:33 +010050#ifdef CONFIG_VMAP_STACK
51static void init_irq_stacks(void)
52{
53 int cpu;
54 unsigned long *p;
55
56 for_each_possible_cpu(cpu) {
James Morseed8b20d2018-01-08 15:38:10 +000057 p = arch_alloc_vmap_stack(IRQ_STACK_SIZE, cpu_to_node(cpu));
Mark Rutlande3067862017-07-21 14:25:33 +010058 per_cpu(irq_stack_ptr, cpu) = p;
59 }
60}
61#else
62/* irq stack only needs to be 16 byte aligned - not IRQ_STACK_SIZE aligned. */
63DEFINE_PER_CPU_ALIGNED(unsigned long [IRQ_STACK_SIZE/sizeof(long)], irq_stack);
64
Mark Rutlandf60fe782017-07-31 21:17:03 +010065static void init_irq_stacks(void)
66{
67 int cpu;
68
69 for_each_possible_cpu(cpu)
70 per_cpu(irq_stack_ptr, cpu) = per_cpu(irq_stack, cpu);
71}
Mark Rutlande3067862017-07-21 14:25:33 +010072#endif
Mark Rutlandf60fe782017-07-31 21:17:03 +010073
Mark Rutland8ff443c2021-03-15 11:56:26 +000074static void default_handle_irq(struct pt_regs *regs)
75{
76 panic("IRQ taken without a root IRQ handler\n");
77}
78
Mark Rutland3889ba72021-03-15 11:56:29 +000079static void default_handle_fiq(struct pt_regs *regs)
80{
81 panic("FIQ taken without a root FIQ handler\n");
82}
83
Mark Rutland8ff443c2021-03-15 11:56:26 +000084void (*handle_arch_irq)(struct pt_regs *) __ro_after_init = default_handle_irq;
Mark Rutland3889ba72021-03-15 11:56:29 +000085void (*handle_arch_fiq)(struct pt_regs *) __ro_after_init = default_handle_fiq;
Marc Zyngier338a7432021-03-15 11:56:25 +000086
87int __init set_handle_irq(void (*handle_irq)(struct pt_regs *))
88{
Mark Rutland8ff443c2021-03-15 11:56:26 +000089 if (handle_arch_irq != default_handle_irq)
Marc Zyngier338a7432021-03-15 11:56:25 +000090 return -EBUSY;
91
92 handle_arch_irq = handle_irq;
Mark Rutland8ff443c2021-03-15 11:56:26 +000093 pr_info("Root IRQ handler: %ps\n", handle_irq);
Marc Zyngier338a7432021-03-15 11:56:25 +000094 return 0;
95}
96
Mark Rutland3889ba72021-03-15 11:56:29 +000097int __init set_handle_fiq(void (*handle_fiq)(struct pt_regs *))
98{
99 if (handle_arch_fiq != default_handle_fiq)
100 return -EBUSY;
101
102 handle_arch_fiq = handle_fiq;
103 pr_info("Root FIQ handler: %ps\n", handle_fiq);
104 return 0;
105}
106
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +0000107void __init init_IRQ(void)
108{
Mark Rutlandf60fe782017-07-31 21:17:03 +0100109 init_irq_stacks();
Sami Tolvanenac20ffb2020-11-30 15:34:42 -0800110 init_irq_scs();
Catalin Marinase851b582013-01-14 12:39:31 +0000111 irqchip_init();
Wei Lie1d22382019-06-11 10:38:12 +0100112
113 if (system_uses_irq_prio_masking()) {
114 /*
115 * Now that we have a stack for our IRQ handler, set
116 * the PMR/PSR pair to a consistent state.
117 */
118 WARN_ON(read_sysreg(daif) & PSR_A_BIT);
119 local_daif_restore(DAIF_PROCCTX_NOIRQ);
120 }
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +0000121}