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 DEFINE_PER_CPU(unsigned int, __softirq_pending); 77 78 #ifdef CONFIG_PPC32 79 atomic_t ppc_n_lost_interrupts; 80 81 #ifdef CONFIG_TAU_INT 82 extern int tau_initialized; 83 u32 tau_interrupts(unsigned long cpu); 84 #endif 85 #endif /* CONFIG_PPC32 */ 86 87 struct irq_stat_info { 88 const char *symbol; 89 const char *text; 90 int optional; 91 }; 92 93 /* ISE - IRQ STAT ENABLED, ISC - IRQ STAT CONDITIONAL */ 94 #define ISE(idx, sym, txt)[IRQ_COUNT_##idx] = { .symbol = sym, .text = txt, .optional = 0} 95 #define ISC(idx, sym, txt)[IRQ_COUNT_##idx] = { .symbol = sym, .text = txt, .optional = 1} 96 97 98 static struct irq_stat_info irq_stat_info[IRQ_COUNT_MAX] __ro_after_init = { 99 ISE(LOC_TIMER, "LOC", " Local timer interrupts for timer event device\n"), 100 ISE(BCT_TIMER, "BCT", " Broadcast timer interrupts for timer event device\n"), 101 ISE(OTHER_TIMER, "LOC", " Local timer interrupts for others\n"), 102 ISE(SPURIOUS, "SPU", " Spurious interrupts\n"), 103 ISE(PMI, "PMI", " Performance monitoring interrupts\n"), 104 ISC(MCE, "MCE", " Machine check exceptions\n"), 105 ISC(NMI_SRESET, "NMI", " System Reset interrupts\n"), 106 #ifdef CONFIG_PPC_WATCHDOG 107 ISE(WATCHDOG, "WDG", " Watchdog soft-NMI interrupts\n"), 108 #endif 109 #ifdef CONFIG_PPC_DOORBELL 110 ISE(DOORBELL, "DBL", " Doorbell interrupts\n"), 111 #endif 112 }; 113 114 /* 115 * Used for default disabled counters to increment the stats and to enable the 116 * entry for /proc/interrupts output. 117 */ 118 static DECLARE_BITMAP(irq_stat_count_show, IRQ_COUNT_MAX) __read_mostly; 119 void inc_irq_stat_and_enable(enum irq_stat_counts which) 120 { 121 __this_cpu_inc(irq_stat.counts[which]); 122 set_bit(which, irq_stat_count_show); 123 } 124 125 #ifdef CONFIG_PROC_FS 126 int arch_show_interrupts(struct seq_file *p, int prec) 127 { 128 const struct irq_stat_info *info = irq_stat_info; 129 130 for (unsigned int i = 0; i < ARRAY_SIZE(irq_stat_info); i++, info++) { 131 if (info->optional && !test_bit(i, irq_stat_count_show)) 132 continue; 133 134 seq_printf(p, "%*s:", prec, info->symbol); 135 irq_proc_emit_counts(p, &irq_stat.counts[i]); 136 seq_puts(p, info->text); 137 } 138 139 #if defined(CONFIG_PPC32) && defined(CONFIG_TAU_INT) 140 if (tau_initialized) { 141 int j; 142 seq_printf(p, "%*s:", prec, "TAU"); 143 for_each_online_cpu(j) 144 seq_put_decimal_ull_width(p, " ", tau_interrupts(j), 10); 145 seq_puts(p, " PowerPC Thermal Assist (cpu temp)\n"); 146 } 147 #endif /* CONFIG_PPC32 && CONFIG_TAU_INT */ 148 #ifdef CONFIG_PPC_BOOK3S_64 149 if (cpu_has_feature(CPU_FTR_HVMODE)) { 150 int j; 151 seq_printf(p, "%*s:", prec, "HMI"); 152 for_each_online_cpu(j) 153 seq_put_decimal_ull_width(p, " ", paca_ptrs[j]->hmi_irqs, 10); 154 seq_printf(p, " Hypervisor Maintenance Interrupts\n"); 155 } 156 #endif 157 return 0; 158 } 159 #endif /* CONFIG_PROC_FS */ 160 161 static int __init irq_init_stats(void) 162 { 163 struct irq_stat_info *info = irq_stat_info; 164 165 for (unsigned int i = 0; i < ARRAY_SIZE(irq_stat_info); i++, info++) { 166 if (info->optional == 0) 167 set_bit(i, irq_stat_count_show); 168 } 169 170 return 0; 171 } 172 late_initcall(irq_init_stats); 173 174 /* 175 * /proc/stat helpers 176 */ 177 u64 arch_irq_stat_cpu(unsigned int cpu) 178 { 179 irq_cpustat_t *p = per_cpu_ptr(&irq_stat, cpu); 180 u64 sum = 0; 181 182 for (unsigned int i = 0; i < ARRAY_SIZE(irq_stat_info); i++) 183 sum += p->counts[i]; 184 185 #ifdef CONFIG_PPC_BOOK3S_64 186 sum += paca_ptrs[cpu]->hmi_irqs; 187 #endif 188 return sum; 189 } 190 191 static inline void check_stack_overflow(unsigned long sp) 192 { 193 if (!IS_ENABLED(CONFIG_DEBUG_STACKOVERFLOW)) 194 return; 195 196 sp &= THREAD_SIZE - 1; 197 198 /* check for stack overflow: is there less than 1/4th free? */ 199 if (unlikely(sp < THREAD_SIZE / 4)) { 200 pr_err("do_IRQ: stack overflow: %ld\n", sp); 201 dump_stack(); 202 } 203 } 204 205 #ifdef CONFIG_SOFTIRQ_ON_OWN_STACK 206 static __always_inline void call_do_softirq(const void *sp) 207 { 208 /* Temporarily switch r1 to sp, call __do_softirq() then restore r1. */ 209 asm volatile ( 210 PPC_STLU " %%r1, %[offset](%[sp]) ;" 211 "mr %%r1, %[sp] ;" 212 #ifdef CONFIG_PPC_KERNEL_PCREL 213 "bl %[callee]@notoc ;" 214 #else 215 "bl %[callee] ;" 216 #endif 217 PPC_LL " %%r1, 0(%%r1) ;" 218 : // Outputs 219 : // Inputs 220 [sp] "b" (sp), [offset] "i" (THREAD_SIZE - STACK_FRAME_MIN_SIZE), 221 [callee] "i" (__do_softirq) 222 : // Clobbers 223 "lr", "xer", "ctr", "memory", "cr0", "cr1", "cr5", "cr6", "cr7", "r0", 224 /* r2 may be clobbered by the callee when using PCREL mode in the ELFv2 ABI. */ 225 #ifdef CONFIG_PPC_KERNEL_PCREL 226 "r2", 227 #endif 228 "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", 229 "r11", "r12" 230 ); 231 } 232 #endif 233 234 DEFINE_STATIC_CALL_RET0(ppc_get_irq, *ppc_md.get_irq); 235 236 static void __do_irq(struct pt_regs *regs, unsigned long oldsp) 237 { 238 unsigned int irq; 239 240 trace_irq_entry(regs); 241 242 check_stack_overflow(oldsp); 243 244 /* 245 * Query the platform PIC for the interrupt & ack it. 246 * 247 * This will typically lower the interrupt line to the CPU 248 */ 249 irq = static_call(ppc_get_irq)(); 250 251 /* We can hard enable interrupts now to allow perf interrupts */ 252 if (should_hard_irq_enable(regs)) 253 do_hard_irq_enable(); 254 255 /* And finally process it */ 256 if (unlikely(!irq)) 257 inc_irq_stat(SPURIOUS); 258 else 259 generic_handle_irq(irq); 260 261 trace_irq_exit(regs); 262 } 263 264 static __always_inline void call_do_irq(struct pt_regs *regs, void *sp) 265 { 266 register unsigned long r3 asm("r3") = (unsigned long)regs; 267 268 /* Temporarily switch r1 to sp, call __do_irq() then restore r1. */ 269 asm volatile ( 270 PPC_STLU " %%r1, %[offset](%[sp]) ;" 271 "mr %%r4, %%r1 ;" 272 "mr %%r1, %[sp] ;" 273 #ifdef CONFIG_PPC_KERNEL_PCREL 274 "bl %[callee]@notoc ;" 275 #else 276 "bl %[callee] ;" 277 #endif 278 PPC_LL " %%r1, 0(%%r1) ;" 279 : // Outputs 280 "+r" (r3) 281 : // Inputs 282 [sp] "b" (sp), [offset] "i" (THREAD_SIZE - STACK_FRAME_MIN_SIZE), 283 [callee] "i" (__do_irq) 284 : // Clobbers 285 "lr", "xer", "ctr", "memory", "cr0", "cr1", "cr5", "cr6", "cr7", "r0", 286 /* r2 may be clobbered by the callee when using PCREL mode in the ELFv2 ABI. */ 287 #ifdef CONFIG_PPC_KERNEL_PCREL 288 "r2", 289 #endif 290 "r4", "r5", "r6", "r7", "r8", "r9", "r10", 291 "r11", "r12" 292 ); 293 } 294 295 void __do_IRQ(struct pt_regs *regs) 296 { 297 struct pt_regs *old_regs = set_irq_regs(regs); 298 void *cursp, *irqsp; 299 300 /* Switch to the irq stack to handle this */ 301 cursp = (void *)(current_stack_pointer & ~(THREAD_SIZE - 1)); 302 irqsp = hardirq_ctx[raw_smp_processor_id()]; 303 304 /* Already there ? If not switch stack and call */ 305 if (unlikely(cursp == irqsp)) 306 __do_irq(regs, current_stack_pointer); 307 else 308 call_do_irq(regs, irqsp); 309 310 set_irq_regs(old_regs); 311 } 312 313 DEFINE_INTERRUPT_HANDLER_ASYNC(do_IRQ) 314 { 315 __do_IRQ(regs); 316 } 317 318 static void *__init alloc_vm_stack(void) 319 { 320 return __vmalloc_node(THREAD_SIZE, THREAD_ALIGN, THREADINFO_GFP, 321 NUMA_NO_NODE, (void *)_RET_IP_); 322 } 323 324 static void __init vmap_irqstack_init(void) 325 { 326 int i; 327 328 for_each_possible_cpu(i) { 329 softirq_ctx[i] = alloc_vm_stack(); 330 hardirq_ctx[i] = alloc_vm_stack(); 331 } 332 } 333 334 335 void __init init_IRQ(void) 336 { 337 if (IS_ENABLED(CONFIG_VMAP_STACK)) 338 vmap_irqstack_init(); 339 340 if (ppc_md.init_IRQ) 341 ppc_md.init_IRQ(); 342 343 if (!WARN_ON(!ppc_md.get_irq)) 344 static_call_update(ppc_get_irq, ppc_md.get_irq); 345 } 346 347 #ifdef CONFIG_BOOKE 348 void *critirq_ctx[NR_CPUS] __read_mostly; 349 void *dbgirq_ctx[NR_CPUS] __read_mostly; 350 void *mcheckirq_ctx[NR_CPUS] __read_mostly; 351 #endif 352 353 void *softirq_ctx[NR_CPUS] __read_mostly; 354 void *hardirq_ctx[NR_CPUS] __read_mostly; 355 356 #ifdef CONFIG_SOFTIRQ_ON_OWN_STACK 357 void do_softirq_own_stack(void) 358 { 359 call_do_softirq(softirq_ctx[smp_processor_id()]); 360 } 361 #endif 362 363 irq_hw_number_t virq_to_hw(unsigned int virq) 364 { 365 struct irq_data *irq_data = irq_get_irq_data(virq); 366 return WARN_ON(!irq_data) ? 0 : irq_data->hwirq; 367 } 368 EXPORT_SYMBOL_GPL(virq_to_hw); 369 370 #ifdef CONFIG_SMP 371 int irq_choose_cpu(const struct cpumask *mask) 372 { 373 int cpuid; 374 375 if (cpumask_equal(mask, cpu_online_mask)) { 376 static int irq_rover; 377 static DEFINE_RAW_SPINLOCK(irq_rover_lock); 378 unsigned long flags; 379 380 /* Round-robin distribution... */ 381 do_round_robin: 382 raw_spin_lock_irqsave(&irq_rover_lock, flags); 383 384 irq_rover = cpumask_next_wrap(irq_rover, cpu_online_mask); 385 cpuid = irq_rover; 386 387 raw_spin_unlock_irqrestore(&irq_rover_lock, flags); 388 } else { 389 cpuid = cpumask_first_and(mask, cpu_online_mask); 390 if (cpuid >= nr_cpu_ids) 391 goto do_round_robin; 392 } 393 394 return get_hard_smp_processor_id(cpuid); 395 } 396 #else 397 int irq_choose_cpu(const struct cpumask *mask) 398 { 399 return hard_smp_processor_id(); 400 } 401 #endif 402