1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu> 4 */ 5 #ifndef _ASM_POWERPC_HW_IRQ_H 6 #define _ASM_POWERPC_HW_IRQ_H 7 8 #ifdef __KERNEL__ 9 10 #include <linux/errno.h> 11 #include <linux/compiler.h> 12 #include <asm/ptrace.h> 13 #include <asm/processor.h> 14 15 #ifdef CONFIG_PPC64 16 17 /* 18 * PACA flags in paca->irq_happened. 19 * 20 * This bits are set when interrupts occur while soft-disabled 21 * and allow a proper replay. 22 * 23 * The PACA_IRQ_HARD_DIS is set whenever we hard disable. It is almost 24 * always in synch with the MSR[EE] state, except: 25 * - A window in interrupt entry, where hardware disables MSR[EE] and that 26 * must be "reconciled" with the soft mask state. 27 * - NMI interrupts that hit in awkward places, until they fix the state. 28 * - When local irqs are being enabled and state is being fixed up. 29 * - When returning from an interrupt there are some windows where this 30 * can become out of synch, but gets fixed before the RFI or before 31 * executing the next user instruction (see arch/powerpc/kernel/interrupt.c). 32 */ 33 #define PACA_IRQ_HARD_DIS 0x01 34 #define PACA_IRQ_DBELL 0x02 35 #define PACA_IRQ_EE 0x04 36 #define PACA_IRQ_DEC 0x08 /* Or FIT */ 37 #define PACA_IRQ_HMI 0x10 38 #define PACA_IRQ_PMI 0x20 39 40 /* 41 * Some soft-masked interrupts must be hard masked until they are replayed 42 * (e.g., because the soft-masked handler does not clear the exception). 43 */ 44 #ifdef CONFIG_PPC_BOOK3S 45 #define PACA_IRQ_MUST_HARD_MASK (PACA_IRQ_EE|PACA_IRQ_PMI) 46 #else 47 #define PACA_IRQ_MUST_HARD_MASK (PACA_IRQ_EE) 48 #endif 49 50 #endif /* CONFIG_PPC64 */ 51 52 /* 53 * flags for paca->irq_soft_mask 54 */ 55 #define IRQS_ENABLED 0 56 #define IRQS_DISABLED 1 /* local_irq_disable() interrupts */ 57 #define IRQS_PMI_DISABLED 2 58 #define IRQS_ALL_DISABLED (IRQS_DISABLED | IRQS_PMI_DISABLED) 59 60 #ifndef __ASSEMBLY__ 61 62 static inline void __hard_irq_enable(void) 63 { 64 if (IS_ENABLED(CONFIG_BOOKE_OR_40x)) 65 wrtee(MSR_EE); 66 else if (IS_ENABLED(CONFIG_PPC_8xx)) 67 wrtspr(SPRN_EIE); 68 else if (IS_ENABLED(CONFIG_PPC_BOOK3S_64)) 69 __mtmsrd(MSR_EE | MSR_RI, 1); 70 else 71 mtmsr(mfmsr() | MSR_EE); 72 } 73 74 static inline void __hard_irq_disable(void) 75 { 76 if (IS_ENABLED(CONFIG_BOOKE_OR_40x)) 77 wrtee(0); 78 else if (IS_ENABLED(CONFIG_PPC_8xx)) 79 wrtspr(SPRN_EID); 80 else if (IS_ENABLED(CONFIG_PPC_BOOK3S_64)) 81 __mtmsrd(MSR_RI, 1); 82 else 83 mtmsr(mfmsr() & ~MSR_EE); 84 } 85 86 static inline void __hard_EE_RI_disable(void) 87 { 88 if (IS_ENABLED(CONFIG_BOOKE_OR_40x)) 89 wrtee(0); 90 else if (IS_ENABLED(CONFIG_PPC_8xx)) 91 wrtspr(SPRN_NRI); 92 else if (IS_ENABLED(CONFIG_PPC_BOOK3S_64)) 93 __mtmsrd(0, 1); 94 else 95 mtmsr(mfmsr() & ~(MSR_EE | MSR_RI)); 96 } 97 98 static inline void __hard_RI_enable(void) 99 { 100 if (IS_ENABLED(CONFIG_BOOKE_OR_40x)) 101 return; 102 103 if (IS_ENABLED(CONFIG_PPC_8xx)) 104 wrtspr(SPRN_EID); 105 else if (IS_ENABLED(CONFIG_PPC_BOOK3S_64)) 106 __mtmsrd(MSR_RI, 1); 107 else 108 mtmsr(mfmsr() | MSR_RI); 109 } 110 111 #ifdef CONFIG_PPC64 112 #include <asm/paca.h> 113 114 static inline notrace unsigned long irq_soft_mask_return(void) 115 { 116 unsigned long flags; 117 118 asm volatile( 119 "lbz %0,%1(13)" 120 : "=r" (flags) 121 : "i" (offsetof(struct paca_struct, irq_soft_mask))); 122 123 return flags; 124 } 125 126 /* 127 * The "memory" clobber acts as both a compiler barrier 128 * for the critical section and as a clobber because 129 * we changed paca->irq_soft_mask 130 */ 131 static inline notrace void irq_soft_mask_set(unsigned long mask) 132 { 133 /* 134 * The irq mask must always include the STD bit if any are set. 135 * 136 * and interrupts don't get replayed until the standard 137 * interrupt (local_irq_disable()) is unmasked. 138 * 139 * Other masks must only provide additional masking beyond 140 * the standard, and they are also not replayed until the 141 * standard interrupt becomes unmasked. 142 * 143 * This could be changed, but it will require partial 144 * unmasks to be replayed, among other things. For now, take 145 * the simple approach. 146 */ 147 if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG)) 148 WARN_ON(mask && !(mask & IRQS_DISABLED)); 149 150 asm volatile( 151 "stb %0,%1(13)" 152 : 153 : "r" (mask), 154 "i" (offsetof(struct paca_struct, irq_soft_mask)) 155 : "memory"); 156 } 157 158 static inline notrace unsigned long irq_soft_mask_set_return(unsigned long mask) 159 { 160 unsigned long flags; 161 162 #ifdef CONFIG_PPC_IRQ_SOFT_MASK_DEBUG 163 WARN_ON(mask && !(mask & IRQS_DISABLED)); 164 #endif 165 166 asm volatile( 167 "lbz %0,%1(13); stb %2,%1(13)" 168 : "=&r" (flags) 169 : "i" (offsetof(struct paca_struct, irq_soft_mask)), 170 "r" (mask) 171 : "memory"); 172 173 return flags; 174 } 175 176 static inline notrace unsigned long irq_soft_mask_or_return(unsigned long mask) 177 { 178 unsigned long flags, tmp; 179 180 asm volatile( 181 "lbz %0,%2(13); or %1,%0,%3; stb %1,%2(13)" 182 : "=&r" (flags), "=r" (tmp) 183 : "i" (offsetof(struct paca_struct, irq_soft_mask)), 184 "r" (mask) 185 : "memory"); 186 187 #ifdef CONFIG_PPC_IRQ_SOFT_MASK_DEBUG 188 WARN_ON((mask | flags) && !((mask | flags) & IRQS_DISABLED)); 189 #endif 190 191 return flags; 192 } 193 194 static inline unsigned long arch_local_save_flags(void) 195 { 196 return irq_soft_mask_return(); 197 } 198 199 static inline void arch_local_irq_disable(void) 200 { 201 irq_soft_mask_set(IRQS_DISABLED); 202 } 203 204 extern void arch_local_irq_restore(unsigned long); 205 206 static inline void arch_local_irq_enable(void) 207 { 208 arch_local_irq_restore(IRQS_ENABLED); 209 } 210 211 static inline unsigned long arch_local_irq_save(void) 212 { 213 return irq_soft_mask_set_return(IRQS_DISABLED); 214 } 215 216 static inline bool arch_irqs_disabled_flags(unsigned long flags) 217 { 218 return flags & IRQS_DISABLED; 219 } 220 221 static inline bool arch_irqs_disabled(void) 222 { 223 return arch_irqs_disabled_flags(arch_local_save_flags()); 224 } 225 226 static inline void set_pmi_irq_pending(void) 227 { 228 /* 229 * Invoked from PMU callback functions to set PMI bit in the paca. 230 * This has to be called with irq's disabled (via hard_irq_disable()). 231 */ 232 if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG)) 233 WARN_ON_ONCE(mfmsr() & MSR_EE); 234 235 get_paca()->irq_happened |= PACA_IRQ_PMI; 236 } 237 238 static inline void clear_pmi_irq_pending(void) 239 { 240 /* 241 * Invoked from PMU callback functions to clear the pending PMI bit 242 * in the paca. 243 */ 244 if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG)) 245 WARN_ON_ONCE(mfmsr() & MSR_EE); 246 247 get_paca()->irq_happened &= ~PACA_IRQ_PMI; 248 } 249 250 static inline bool pmi_irq_pending(void) 251 { 252 /* 253 * Invoked from PMU callback functions to check if there is a pending 254 * PMI bit in the paca. 255 */ 256 if (get_paca()->irq_happened & PACA_IRQ_PMI) 257 return true; 258 259 return false; 260 } 261 262 #ifdef CONFIG_PPC_BOOK3S 263 /* 264 * To support disabling and enabling of irq with PMI, set of 265 * new powerpc_local_irq_pmu_save() and powerpc_local_irq_restore() 266 * functions are added. These macros are implemented using generic 267 * linux local_irq_* code from include/linux/irqflags.h. 268 */ 269 #define raw_local_irq_pmu_save(flags) \ 270 do { \ 271 typecheck(unsigned long, flags); \ 272 flags = irq_soft_mask_or_return(IRQS_DISABLED | \ 273 IRQS_PMI_DISABLED); \ 274 } while(0) 275 276 #define raw_local_irq_pmu_restore(flags) \ 277 do { \ 278 typecheck(unsigned long, flags); \ 279 arch_local_irq_restore(flags); \ 280 } while(0) 281 282 #ifdef CONFIG_TRACE_IRQFLAGS 283 #define powerpc_local_irq_pmu_save(flags) \ 284 do { \ 285 raw_local_irq_pmu_save(flags); \ 286 if (!raw_irqs_disabled_flags(flags)) \ 287 trace_hardirqs_off(); \ 288 } while(0) 289 #define powerpc_local_irq_pmu_restore(flags) \ 290 do { \ 291 if (!raw_irqs_disabled_flags(flags)) \ 292 trace_hardirqs_on(); \ 293 raw_local_irq_pmu_restore(flags); \ 294 } while(0) 295 #else 296 #define powerpc_local_irq_pmu_save(flags) \ 297 do { \ 298 raw_local_irq_pmu_save(flags); \ 299 } while(0) 300 #define powerpc_local_irq_pmu_restore(flags) \ 301 do { \ 302 raw_local_irq_pmu_restore(flags); \ 303 } while (0) 304 #endif /* CONFIG_TRACE_IRQFLAGS */ 305 306 #endif /* CONFIG_PPC_BOOK3S */ 307 308 #define hard_irq_disable() do { \ 309 unsigned long flags; \ 310 __hard_irq_disable(); \ 311 flags = irq_soft_mask_set_return(IRQS_ALL_DISABLED); \ 312 local_paca->irq_happened |= PACA_IRQ_HARD_DIS; \ 313 if (!arch_irqs_disabled_flags(flags)) { \ 314 asm volatile("std%X0 %1,%0" : "=m" (local_paca->saved_r1) \ 315 : "r" (current_stack_pointer)); \ 316 trace_hardirqs_off(); \ 317 } \ 318 } while(0) 319 320 static inline bool __lazy_irq_pending(u8 irq_happened) 321 { 322 return !!(irq_happened & ~PACA_IRQ_HARD_DIS); 323 } 324 325 /* 326 * Check if a lazy IRQ is pending. Should be called with IRQs hard disabled. 327 */ 328 static inline bool lazy_irq_pending(void) 329 { 330 return __lazy_irq_pending(get_paca()->irq_happened); 331 } 332 333 /* 334 * Check if a lazy IRQ is pending, with no debugging checks. 335 * Should be called with IRQs hard disabled. 336 * For use in RI disabled code or other constrained situations. 337 */ 338 static inline bool lazy_irq_pending_nocheck(void) 339 { 340 return __lazy_irq_pending(local_paca->irq_happened); 341 } 342 343 bool power_pmu_wants_prompt_pmi(void); 344 345 /* 346 * This is called by asynchronous interrupts to check whether to 347 * conditionally re-enable hard interrupts after having cleared 348 * the source of the interrupt. They are kept disabled if there 349 * is a different soft-masked interrupt pending that requires hard 350 * masking. 351 */ 352 static inline bool should_hard_irq_enable(void) 353 { 354 if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG)) { 355 WARN_ON(irq_soft_mask_return() == IRQS_ENABLED); 356 WARN_ON(mfmsr() & MSR_EE); 357 } 358 359 if (!IS_ENABLED(CONFIG_PERF_EVENTS)) 360 return false; 361 /* 362 * If the PMU is not running, there is not much reason to enable 363 * MSR[EE] in irq handlers because any interrupts would just be 364 * soft-masked. 365 * 366 * TODO: Add test for 64e 367 */ 368 if (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && !power_pmu_wants_prompt_pmi()) 369 return false; 370 371 if (get_paca()->irq_happened & PACA_IRQ_MUST_HARD_MASK) 372 return false; 373 374 return true; 375 } 376 377 /* 378 * Do the hard enabling, only call this if should_hard_irq_enable is true. 379 */ 380 static inline void do_hard_irq_enable(void) 381 { 382 if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG)) { 383 WARN_ON(irq_soft_mask_return() == IRQS_ENABLED); 384 WARN_ON(get_paca()->irq_happened & PACA_IRQ_MUST_HARD_MASK); 385 WARN_ON(mfmsr() & MSR_EE); 386 } 387 /* 388 * This allows PMI interrupts (and watchdog soft-NMIs) through. 389 * There is no other reason to enable this way. 390 */ 391 get_paca()->irq_happened &= ~PACA_IRQ_HARD_DIS; 392 __hard_irq_enable(); 393 } 394 395 static inline bool arch_irq_disabled_regs(struct pt_regs *regs) 396 { 397 return (regs->softe & IRQS_DISABLED); 398 } 399 400 extern bool prep_irq_for_idle(void); 401 extern bool prep_irq_for_idle_irqsoff(void); 402 extern void irq_set_pending_from_srr1(unsigned long srr1); 403 404 #define fini_irq_for_idle_irqsoff() trace_hardirqs_off(); 405 406 extern void force_external_irq_replay(void); 407 408 static inline void irq_soft_mask_regs_set_state(struct pt_regs *regs, unsigned long val) 409 { 410 regs->softe = val; 411 } 412 #else /* CONFIG_PPC64 */ 413 414 static inline notrace unsigned long irq_soft_mask_return(void) 415 { 416 return 0; 417 } 418 419 static inline unsigned long arch_local_save_flags(void) 420 { 421 return mfmsr(); 422 } 423 424 static inline void arch_local_irq_restore(unsigned long flags) 425 { 426 if (IS_ENABLED(CONFIG_BOOKE)) 427 wrtee(flags); 428 else 429 mtmsr(flags); 430 } 431 432 static inline unsigned long arch_local_irq_save(void) 433 { 434 unsigned long flags = arch_local_save_flags(); 435 436 if (IS_ENABLED(CONFIG_BOOKE)) 437 wrtee(0); 438 else if (IS_ENABLED(CONFIG_PPC_8xx)) 439 wrtspr(SPRN_EID); 440 else 441 mtmsr(flags & ~MSR_EE); 442 443 return flags; 444 } 445 446 static inline void arch_local_irq_disable(void) 447 { 448 __hard_irq_disable(); 449 } 450 451 static inline void arch_local_irq_enable(void) 452 { 453 __hard_irq_enable(); 454 } 455 456 static inline bool arch_irqs_disabled_flags(unsigned long flags) 457 { 458 return (flags & MSR_EE) == 0; 459 } 460 461 static inline bool arch_irqs_disabled(void) 462 { 463 return arch_irqs_disabled_flags(arch_local_save_flags()); 464 } 465 466 #define hard_irq_disable() arch_local_irq_disable() 467 468 static inline bool arch_irq_disabled_regs(struct pt_regs *regs) 469 { 470 return !(regs->msr & MSR_EE); 471 } 472 473 static __always_inline bool should_hard_irq_enable(void) 474 { 475 return false; 476 } 477 478 static inline void do_hard_irq_enable(void) 479 { 480 BUILD_BUG(); 481 } 482 483 static inline void clear_pmi_irq_pending(void) { } 484 static inline void set_pmi_irq_pending(void) { } 485 static inline bool pmi_irq_pending(void) { return false; } 486 487 static inline void irq_soft_mask_regs_set_state(struct pt_regs *regs, unsigned long val) 488 { 489 } 490 #endif /* CONFIG_PPC64 */ 491 492 #define ARCH_IRQ_INIT_FLAGS IRQ_NOREQUEST 493 494 #endif /* __ASSEMBLY__ */ 495 #endif /* __KERNEL__ */ 496 #endif /* _ASM_POWERPC_HW_IRQ_H */ 497