powerpc/irq: Simplify __do_irq()
[linux-2.6-microblaze.git] / arch / powerpc / kernel / irq.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Derived from arch/i386/kernel/irq.c
4  *    Copyright (C) 1992 Linus Torvalds
5  *  Adapted from arch/i386 by Gary Thomas
6  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
7  *  Updated and modified by Cort Dougan <cort@fsmlabs.com>
8  *    Copyright (C) 1996-2001 Cort Dougan
9  *  Adapted for Power Macintosh by Paul Mackerras
10  *    Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
11  *
12  * This file contains the code used by various IRQ handling routines:
13  * asking for different IRQ's should be done through these routines
14  * instead of just grabbing them. Thus setups with different IRQ numbers
15  * shouldn't result in any weird surprises, and installing new handlers
16  * should be easier.
17  *
18  * The MPC8xx has an interrupt mask in the SIU.  If a bit is set, the
19  * interrupt is _enabled_.  As expected, IRQ0 is bit 0 in the 32-bit
20  * mask register (of which only 16 are defined), hence the weird shifting
21  * and complement of the cached_irq_mask.  I want to be able to stuff
22  * this right into the SIU SMASK register.
23  * Many of the prep/chrp functions are conditional compiled on CONFIG_PPC_8xx
24  * to reduce code space and undefined function references.
25  */
26
27 #undef DEBUG
28
29 #include <linux/export.h>
30 #include <linux/threads.h>
31 #include <linux/kernel_stat.h>
32 #include <linux/signal.h>
33 #include <linux/sched.h>
34 #include <linux/ptrace.h>
35 #include <linux/ioport.h>
36 #include <linux/interrupt.h>
37 #include <linux/timex.h>
38 #include <linux/init.h>
39 #include <linux/slab.h>
40 #include <linux/delay.h>
41 #include <linux/irq.h>
42 #include <linux/seq_file.h>
43 #include <linux/cpumask.h>
44 #include <linux/profile.h>
45 #include <linux/bitops.h>
46 #include <linux/list.h>
47 #include <linux/radix-tree.h>
48 #include <linux/mutex.h>
49 #include <linux/pci.h>
50 #include <linux/debugfs.h>
51 #include <linux/of.h>
52 #include <linux/of_irq.h>
53 #include <linux/vmalloc.h>
54 #include <linux/pgtable.h>
55 #include <linux/static_call.h>
56
57 #include <linux/uaccess.h>
58 #include <asm/interrupt.h>
59 #include <asm/io.h>
60 #include <asm/irq.h>
61 #include <asm/cache.h>
62 #include <asm/ptrace.h>
63 #include <asm/machdep.h>
64 #include <asm/udbg.h>
65 #include <asm/smp.h>
66 #include <asm/hw_irq.h>
67 #include <asm/softirq_stack.h>
68 #include <asm/ppc_asm.h>
69
70 #define CREATE_TRACE_POINTS
71 #include <asm/trace.h>
72 #include <asm/cpu_has_feature.h>
73
74 DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
75 EXPORT_PER_CPU_SYMBOL(irq_stat);
76
77 #ifdef CONFIG_PPC32
78 atomic_t ppc_n_lost_interrupts;
79
80 #ifdef CONFIG_TAU_INT
81 extern int tau_initialized;
82 u32 tau_interrupts(unsigned long cpu);
83 #endif
84 #endif /* CONFIG_PPC32 */
85
86 int arch_show_interrupts(struct seq_file *p, int prec)
87 {
88         int j;
89
90 #if defined(CONFIG_PPC32) && defined(CONFIG_TAU_INT)
91         if (tau_initialized) {
92                 seq_printf(p, "%*s: ", prec, "TAU");
93                 for_each_online_cpu(j)
94                         seq_printf(p, "%10u ", tau_interrupts(j));
95                 seq_puts(p, "  PowerPC             Thermal Assist (cpu temp)\n");
96         }
97 #endif /* CONFIG_PPC32 && CONFIG_TAU_INT */
98
99         seq_printf(p, "%*s: ", prec, "LOC");
100         for_each_online_cpu(j)
101                 seq_printf(p, "%10u ", per_cpu(irq_stat, j).timer_irqs_event);
102         seq_printf(p, "  Local timer interrupts for timer event device\n");
103
104         seq_printf(p, "%*s: ", prec, "BCT");
105         for_each_online_cpu(j)
106                 seq_printf(p, "%10u ", per_cpu(irq_stat, j).broadcast_irqs_event);
107         seq_printf(p, "  Broadcast timer interrupts for timer event device\n");
108
109         seq_printf(p, "%*s: ", prec, "LOC");
110         for_each_online_cpu(j)
111                 seq_printf(p, "%10u ", per_cpu(irq_stat, j).timer_irqs_others);
112         seq_printf(p, "  Local timer interrupts for others\n");
113
114         seq_printf(p, "%*s: ", prec, "SPU");
115         for_each_online_cpu(j)
116                 seq_printf(p, "%10u ", per_cpu(irq_stat, j).spurious_irqs);
117         seq_printf(p, "  Spurious interrupts\n");
118
119         seq_printf(p, "%*s: ", prec, "PMI");
120         for_each_online_cpu(j)
121                 seq_printf(p, "%10u ", per_cpu(irq_stat, j).pmu_irqs);
122         seq_printf(p, "  Performance monitoring interrupts\n");
123
124         seq_printf(p, "%*s: ", prec, "MCE");
125         for_each_online_cpu(j)
126                 seq_printf(p, "%10u ", per_cpu(irq_stat, j).mce_exceptions);
127         seq_printf(p, "  Machine check exceptions\n");
128
129 #ifdef CONFIG_PPC_BOOK3S_64
130         if (cpu_has_feature(CPU_FTR_HVMODE)) {
131                 seq_printf(p, "%*s: ", prec, "HMI");
132                 for_each_online_cpu(j)
133                         seq_printf(p, "%10u ", paca_ptrs[j]->hmi_irqs);
134                 seq_printf(p, "  Hypervisor Maintenance Interrupts\n");
135         }
136 #endif
137
138         seq_printf(p, "%*s: ", prec, "NMI");
139         for_each_online_cpu(j)
140                 seq_printf(p, "%10u ", per_cpu(irq_stat, j).sreset_irqs);
141         seq_printf(p, "  System Reset interrupts\n");
142
143 #ifdef CONFIG_PPC_WATCHDOG
144         seq_printf(p, "%*s: ", prec, "WDG");
145         for_each_online_cpu(j)
146                 seq_printf(p, "%10u ", per_cpu(irq_stat, j).soft_nmi_irqs);
147         seq_printf(p, "  Watchdog soft-NMI interrupts\n");
148 #endif
149
150 #ifdef CONFIG_PPC_DOORBELL
151         if (cpu_has_feature(CPU_FTR_DBELL)) {
152                 seq_printf(p, "%*s: ", prec, "DBL");
153                 for_each_online_cpu(j)
154                         seq_printf(p, "%10u ", per_cpu(irq_stat, j).doorbell_irqs);
155                 seq_printf(p, "  Doorbell interrupts\n");
156         }
157 #endif
158
159         return 0;
160 }
161
162 /*
163  * /proc/stat helpers
164  */
165 u64 arch_irq_stat_cpu(unsigned int cpu)
166 {
167         u64 sum = per_cpu(irq_stat, cpu).timer_irqs_event;
168
169         sum += per_cpu(irq_stat, cpu).broadcast_irqs_event;
170         sum += per_cpu(irq_stat, cpu).pmu_irqs;
171         sum += per_cpu(irq_stat, cpu).mce_exceptions;
172         sum += per_cpu(irq_stat, cpu).spurious_irqs;
173         sum += per_cpu(irq_stat, cpu).timer_irqs_others;
174 #ifdef CONFIG_PPC_BOOK3S_64
175         sum += paca_ptrs[cpu]->hmi_irqs;
176 #endif
177         sum += per_cpu(irq_stat, cpu).sreset_irqs;
178 #ifdef CONFIG_PPC_WATCHDOG
179         sum += per_cpu(irq_stat, cpu).soft_nmi_irqs;
180 #endif
181 #ifdef CONFIG_PPC_DOORBELL
182         sum += per_cpu(irq_stat, cpu).doorbell_irqs;
183 #endif
184
185         return sum;
186 }
187
188 static inline void check_stack_overflow(unsigned long sp)
189 {
190         if (!IS_ENABLED(CONFIG_DEBUG_STACKOVERFLOW))
191                 return;
192
193         sp &= THREAD_SIZE - 1;
194
195         /* check for stack overflow: is there less than 1/4th free? */
196         if (unlikely(sp < THREAD_SIZE / 4)) {
197                 pr_err("do_IRQ: stack overflow: %ld\n", sp);
198                 dump_stack();
199         }
200 }
201
202 static __always_inline void call_do_softirq(const void *sp)
203 {
204         /* Temporarily switch r1 to sp, call __do_softirq() then restore r1. */
205         asm volatile (
206                  PPC_STLU "     %%r1, %[offset](%[sp])  ;"
207                 "mr             %%r1, %[sp]             ;"
208                 "bl             %[callee]               ;"
209                  PPC_LL "       %%r1, 0(%%r1)           ;"
210                  : // Outputs
211                  : // Inputs
212                    [sp] "b" (sp), [offset] "i" (THREAD_SIZE - STACK_FRAME_OVERHEAD),
213                    [callee] "i" (__do_softirq)
214                  : // Clobbers
215                    "lr", "xer", "ctr", "memory", "cr0", "cr1", "cr5", "cr6",
216                    "cr7", "r0", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
217                    "r11", "r12"
218         );
219 }
220
221 DEFINE_STATIC_CALL_RET0(ppc_get_irq, *ppc_md.get_irq);
222
223 static void __do_irq(struct pt_regs *regs, unsigned long oldsp)
224 {
225         unsigned int irq;
226
227         trace_irq_entry(regs);
228
229         check_stack_overflow(oldsp);
230
231         /*
232          * Query the platform PIC for the interrupt & ack it.
233          *
234          * This will typically lower the interrupt line to the CPU
235          */
236         irq = static_call(ppc_get_irq)();
237
238         /* We can hard enable interrupts now to allow perf interrupts */
239         if (should_hard_irq_enable())
240                 do_hard_irq_enable();
241
242         /* And finally process it */
243         if (unlikely(!irq))
244                 __this_cpu_inc(irq_stat.spurious_irqs);
245         else
246                 generic_handle_irq(irq);
247
248         trace_irq_exit(regs);
249 }
250
251 static __always_inline void call_do_irq(struct pt_regs *regs, void *sp)
252 {
253         register unsigned long r3 asm("r3") = (unsigned long)regs;
254
255         /* Temporarily switch r1 to sp, call __do_irq() then restore r1. */
256         asm volatile (
257                  PPC_STLU "     %%r1, %[offset](%[sp])  ;"
258                 "mr             %%r4, %%r1              ;"
259                 "mr             %%r1, %[sp]             ;"
260                 "bl             %[callee]               ;"
261                  PPC_LL "       %%r1, 0(%%r1)           ;"
262                  : // Outputs
263                    "+r" (r3)
264                  : // Inputs
265                    [sp] "b" (sp), [offset] "i" (THREAD_SIZE - STACK_FRAME_OVERHEAD),
266                    [callee] "i" (__do_irq)
267                  : // Clobbers
268                    "lr", "xer", "ctr", "memory", "cr0", "cr1", "cr5", "cr6",
269                    "cr7", "r0", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
270                    "r11", "r12"
271         );
272 }
273
274 void __do_IRQ(struct pt_regs *regs)
275 {
276         struct pt_regs *old_regs = set_irq_regs(regs);
277         void *cursp, *irqsp, *sirqsp;
278
279         /* Switch to the irq stack to handle this */
280         cursp = (void *)(current_stack_pointer & ~(THREAD_SIZE - 1));
281         irqsp = hardirq_ctx[raw_smp_processor_id()];
282         sirqsp = softirq_ctx[raw_smp_processor_id()];
283
284         /* Already there ? If not switch stack and call */
285         if (unlikely(cursp == irqsp || cursp == sirqsp))
286                 __do_irq(regs, current_stack_pointer);
287         else
288                 call_do_irq(regs, irqsp);
289
290         set_irq_regs(old_regs);
291 }
292
293 DEFINE_INTERRUPT_HANDLER_ASYNC(do_IRQ)
294 {
295         __do_IRQ(regs);
296 }
297
298 static void *__init alloc_vm_stack(void)
299 {
300         return __vmalloc_node(THREAD_SIZE, THREAD_ALIGN, THREADINFO_GFP,
301                               NUMA_NO_NODE, (void *)_RET_IP_);
302 }
303
304 static void __init vmap_irqstack_init(void)
305 {
306         int i;
307
308         for_each_possible_cpu(i) {
309                 softirq_ctx[i] = alloc_vm_stack();
310                 hardirq_ctx[i] = alloc_vm_stack();
311         }
312 }
313
314
315 void __init init_IRQ(void)
316 {
317         if (IS_ENABLED(CONFIG_VMAP_STACK))
318                 vmap_irqstack_init();
319
320         if (ppc_md.init_IRQ)
321                 ppc_md.init_IRQ();
322
323         if (!WARN_ON(!ppc_md.get_irq))
324                 static_call_update(ppc_get_irq, ppc_md.get_irq);
325 }
326
327 #ifdef CONFIG_BOOKE_OR_40x
328 void   *critirq_ctx[NR_CPUS] __read_mostly;
329 void    *dbgirq_ctx[NR_CPUS] __read_mostly;
330 void *mcheckirq_ctx[NR_CPUS] __read_mostly;
331 #endif
332
333 void *softirq_ctx[NR_CPUS] __read_mostly;
334 void *hardirq_ctx[NR_CPUS] __read_mostly;
335
336 void do_softirq_own_stack(void)
337 {
338         call_do_softirq(softirq_ctx[smp_processor_id()]);
339 }
340
341 irq_hw_number_t virq_to_hw(unsigned int virq)
342 {
343         struct irq_data *irq_data = irq_get_irq_data(virq);
344         return WARN_ON(!irq_data) ? 0 : irq_data->hwirq;
345 }
346 EXPORT_SYMBOL_GPL(virq_to_hw);
347
348 #ifdef CONFIG_SMP
349 int irq_choose_cpu(const struct cpumask *mask)
350 {
351         int cpuid;
352
353         if (cpumask_equal(mask, cpu_online_mask)) {
354                 static int irq_rover;
355                 static DEFINE_RAW_SPINLOCK(irq_rover_lock);
356                 unsigned long flags;
357
358                 /* Round-robin distribution... */
359 do_round_robin:
360                 raw_spin_lock_irqsave(&irq_rover_lock, flags);
361
362                 irq_rover = cpumask_next(irq_rover, cpu_online_mask);
363                 if (irq_rover >= nr_cpu_ids)
364                         irq_rover = cpumask_first(cpu_online_mask);
365
366                 cpuid = irq_rover;
367
368                 raw_spin_unlock_irqrestore(&irq_rover_lock, flags);
369         } else {
370                 cpuid = cpumask_first_and(mask, cpu_online_mask);
371                 if (cpuid >= nr_cpu_ids)
372                         goto do_round_robin;
373         }
374
375         return get_hard_smp_processor_id(cpuid);
376 }
377 #else
378 int irq_choose_cpu(const struct cpumask *mask)
379 {
380         return hard_smp_processor_id();
381 }
382 #endif