1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
5 #ifndef _ASM_POWERPC_HW_IRQ_H
6 #define _ASM_POWERPC_HW_IRQ_H
10 #include <linux/errno.h>
11 #include <linux/compiler.h>
12 #include <asm/ptrace.h>
13 #include <asm/processor.h>
18 * PACA flags in paca->irq_happened.
20 * This bits are set when interrupts occur while soft-disabled
21 * and allow a proper replay.
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).
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 #define PACA_IRQ_REPLAYING 0x40
42 * Some soft-masked interrupts must be hard masked until they are replayed
43 * (e.g., because the soft-masked handler does not clear the exception).
44 * Interrupt replay itself must remain hard masked too.
46 #ifdef CONFIG_PPC_BOOK3S
47 #define PACA_IRQ_MUST_HARD_MASK (PACA_IRQ_EE|PACA_IRQ_PMI|PACA_IRQ_REPLAYING)
49 #define PACA_IRQ_MUST_HARD_MASK (PACA_IRQ_EE|PACA_IRQ_REPLAYING)
52 #endif /* CONFIG_PPC64 */
55 * flags for paca->irq_soft_mask
57 #define IRQS_ENABLED 0
58 #define IRQS_DISABLED 1 /* local_irq_disable() interrupts */
59 #define IRQS_PMI_DISABLED 2
60 #define IRQS_ALL_DISABLED (IRQS_DISABLED | IRQS_PMI_DISABLED)
64 static inline void __hard_irq_enable(void)
66 if (IS_ENABLED(CONFIG_BOOKE
))
68 else if (IS_ENABLED(CONFIG_PPC_8xx
))
70 else if (IS_ENABLED(CONFIG_PPC_BOOK3S_64
))
71 __mtmsrd(MSR_EE
| MSR_RI
, 1);
73 mtmsr(mfmsr() | MSR_EE
);
76 static inline void __hard_irq_disable(void)
78 if (IS_ENABLED(CONFIG_BOOKE
))
80 else if (IS_ENABLED(CONFIG_PPC_8xx
))
82 else if (IS_ENABLED(CONFIG_PPC_BOOK3S_64
))
85 mtmsr(mfmsr() & ~MSR_EE
);
88 static inline void __hard_EE_RI_disable(void)
90 if (IS_ENABLED(CONFIG_BOOKE
))
92 else if (IS_ENABLED(CONFIG_PPC_8xx
))
94 else if (IS_ENABLED(CONFIG_PPC_BOOK3S_64
))
97 mtmsr(mfmsr() & ~(MSR_EE
| MSR_RI
));
100 static inline void __hard_RI_enable(void)
102 if (IS_ENABLED(CONFIG_BOOKE
))
105 if (IS_ENABLED(CONFIG_PPC_8xx
))
107 else if (IS_ENABLED(CONFIG_PPC_BOOK3S_64
))
110 mtmsr(mfmsr() | MSR_RI
);
114 #include <asm/paca.h>
116 static inline notrace
unsigned long irq_soft_mask_return(void)
123 : "i" (offsetof(struct paca_struct
, irq_soft_mask
)));
129 * The "memory" clobber acts as both a compiler barrier
130 * for the critical section and as a clobber because
131 * we changed paca->irq_soft_mask
133 static inline notrace
void irq_soft_mask_set(unsigned long mask
)
136 * The irq mask must always include the STD bit if any are set.
138 * and interrupts don't get replayed until the standard
139 * interrupt (local_irq_disable()) is unmasked.
141 * Other masks must only provide additional masking beyond
142 * the standard, and they are also not replayed until the
143 * standard interrupt becomes unmasked.
145 * This could be changed, but it will require partial
146 * unmasks to be replayed, among other things. For now, take
147 * the simple approach.
149 if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG
))
150 WARN_ON(mask
&& !(mask
& IRQS_DISABLED
));
156 "i" (offsetof(struct paca_struct
, irq_soft_mask
))
160 static inline notrace
unsigned long irq_soft_mask_set_return(unsigned long mask
)
162 unsigned long flags
= irq_soft_mask_return();
164 irq_soft_mask_set(mask
);
169 static inline notrace
unsigned long irq_soft_mask_or_return(unsigned long mask
)
171 unsigned long flags
= irq_soft_mask_return();
173 irq_soft_mask_set(flags
| mask
);
178 static inline notrace
unsigned long irq_soft_mask_andc_return(unsigned long mask
)
180 unsigned long flags
= irq_soft_mask_return();
182 irq_soft_mask_set(flags
& ~mask
);
187 static inline unsigned long arch_local_save_flags(void)
189 return irq_soft_mask_return();
192 static inline void arch_local_irq_disable(void)
194 irq_soft_mask_set(IRQS_DISABLED
);
197 extern void arch_local_irq_restore(unsigned long);
199 static inline void arch_local_irq_enable(void)
201 arch_local_irq_restore(IRQS_ENABLED
);
204 static inline unsigned long arch_local_irq_save(void)
206 return irq_soft_mask_or_return(IRQS_DISABLED
);
209 static inline bool arch_irqs_disabled_flags(unsigned long flags
)
211 return flags
& IRQS_DISABLED
;
214 static inline bool arch_irqs_disabled(void)
216 return arch_irqs_disabled_flags(arch_local_save_flags());
219 static inline void set_pmi_irq_pending(void)
222 * Invoked from PMU callback functions to set PMI bit in the paca.
223 * This has to be called with irq's disabled (via hard_irq_disable()).
225 if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG
))
226 WARN_ON_ONCE(mfmsr() & MSR_EE
);
228 get_paca()->irq_happened
|= PACA_IRQ_PMI
;
231 static inline void clear_pmi_irq_pending(void)
234 * Invoked from PMU callback functions to clear the pending PMI bit
237 if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG
))
238 WARN_ON_ONCE(mfmsr() & MSR_EE
);
240 get_paca()->irq_happened
&= ~PACA_IRQ_PMI
;
243 static inline bool pmi_irq_pending(void)
246 * Invoked from PMU callback functions to check if there is a pending
247 * PMI bit in the paca.
249 if (get_paca()->irq_happened
& PACA_IRQ_PMI
)
255 #ifdef CONFIG_PPC_BOOK3S
257 * To support disabling and enabling of irq with PMI, set of
258 * new powerpc_local_irq_pmu_save() and powerpc_local_irq_restore()
259 * functions are added. These macros are implemented using generic
260 * linux local_irq_* code from include/linux/irqflags.h.
262 #define raw_local_irq_pmu_save(flags) \
264 typecheck(unsigned long, flags); \
265 flags = irq_soft_mask_or_return(IRQS_DISABLED | \
266 IRQS_PMI_DISABLED); \
269 #define raw_local_irq_pmu_restore(flags) \
271 typecheck(unsigned long, flags); \
272 arch_local_irq_restore(flags); \
275 #ifdef CONFIG_TRACE_IRQFLAGS
276 #define powerpc_local_irq_pmu_save(flags) \
278 raw_local_irq_pmu_save(flags); \
279 if (!raw_irqs_disabled_flags(flags)) \
280 trace_hardirqs_off(); \
282 #define powerpc_local_irq_pmu_restore(flags) \
284 if (!raw_irqs_disabled_flags(flags)) \
285 trace_hardirqs_on(); \
286 raw_local_irq_pmu_restore(flags); \
289 #define powerpc_local_irq_pmu_save(flags) \
291 raw_local_irq_pmu_save(flags); \
293 #define powerpc_local_irq_pmu_restore(flags) \
295 raw_local_irq_pmu_restore(flags); \
297 #endif /* CONFIG_TRACE_IRQFLAGS */
299 #endif /* CONFIG_PPC_BOOK3S */
301 #define hard_irq_disable() do { \
302 unsigned long flags; \
303 __hard_irq_disable(); \
304 flags = irq_soft_mask_set_return(IRQS_ALL_DISABLED); \
305 local_paca->irq_happened |= PACA_IRQ_HARD_DIS; \
306 if (!arch_irqs_disabled_flags(flags)) { \
307 asm volatile("std%X0 %1,%0" : "=m" (local_paca->saved_r1) \
308 : "r" (current_stack_pointer)); \
309 trace_hardirqs_off(); \
313 static inline bool __lazy_irq_pending(u8 irq_happened
)
315 return !!(irq_happened
& ~PACA_IRQ_HARD_DIS
);
319 * Check if a lazy IRQ is pending. Should be called with IRQs hard disabled.
321 static inline bool lazy_irq_pending(void)
323 return __lazy_irq_pending(get_paca()->irq_happened
);
327 * Check if a lazy IRQ is pending, with no debugging checks.
328 * Should be called with IRQs hard disabled.
329 * For use in RI disabled code or other constrained situations.
331 static inline bool lazy_irq_pending_nocheck(void)
333 return __lazy_irq_pending(local_paca
->irq_happened
);
336 bool power_pmu_wants_prompt_pmi(void);
339 * This is called by asynchronous interrupts to check whether to
340 * conditionally re-enable hard interrupts after having cleared
341 * the source of the interrupt. They are kept disabled if there
342 * is a different soft-masked interrupt pending that requires hard
345 static inline bool should_hard_irq_enable(struct pt_regs
*regs
)
347 if (IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG
)) {
348 WARN_ON(irq_soft_mask_return() != IRQS_ALL_DISABLED
);
349 WARN_ON(!(get_paca()->irq_happened
& PACA_IRQ_HARD_DIS
));
350 WARN_ON(mfmsr() & MSR_EE
);
353 if (!IS_ENABLED(CONFIG_PERF_EVENTS
))
356 * If the PMU is not running, there is not much reason to enable
357 * MSR[EE] in irq handlers because any interrupts would just be
360 * TODO: Add test for 64e
362 if (IS_ENABLED(CONFIG_PPC_BOOK3S_64
)) {
363 if (!power_pmu_wants_prompt_pmi())
366 * If PMIs are disabled then IRQs should be disabled as well,
367 * so we shouldn't see this condition, check for it just in
368 * case because we are about to enable PMIs.
370 if (WARN_ON_ONCE(regs
->softe
& IRQS_PMI_DISABLED
))
374 if (get_paca()->irq_happened
& PACA_IRQ_MUST_HARD_MASK
)
381 * Do the hard enabling, only call this if should_hard_irq_enable is true.
382 * This allows PMI interrupts to profile irq handlers.
384 static inline void do_hard_irq_enable(void)
387 * Asynch interrupts come in with IRQS_ALL_DISABLED,
388 * PACA_IRQ_HARD_DIS, and MSR[EE]=0.
390 if (IS_ENABLED(CONFIG_PPC_BOOK3S_64
))
391 irq_soft_mask_andc_return(IRQS_PMI_DISABLED
);
392 get_paca()->irq_happened
&= ~PACA_IRQ_HARD_DIS
;
396 static inline bool arch_irq_disabled_regs(struct pt_regs
*regs
)
398 return (regs
->softe
& IRQS_DISABLED
);
401 extern bool prep_irq_for_idle(void);
402 extern bool prep_irq_for_idle_irqsoff(void);
403 extern void irq_set_pending_from_srr1(unsigned long srr1
);
405 #define fini_irq_for_idle_irqsoff() trace_hardirqs_off();
407 extern void force_external_irq_replay(void);
409 static inline void irq_soft_mask_regs_set_state(struct pt_regs
*regs
, unsigned long val
)
413 #else /* CONFIG_PPC64 */
415 static inline notrace
unsigned long irq_soft_mask_return(void)
420 static inline unsigned long arch_local_save_flags(void)
425 static inline void arch_local_irq_restore(unsigned long flags
)
427 if (IS_ENABLED(CONFIG_BOOKE
))
433 static inline unsigned long arch_local_irq_save(void)
435 unsigned long flags
= arch_local_save_flags();
437 if (IS_ENABLED(CONFIG_BOOKE
))
439 else if (IS_ENABLED(CONFIG_PPC_8xx
))
442 mtmsr(flags
& ~MSR_EE
);
447 static inline void arch_local_irq_disable(void)
449 __hard_irq_disable();
452 static inline void arch_local_irq_enable(void)
457 static inline bool arch_irqs_disabled_flags(unsigned long flags
)
459 return (flags
& MSR_EE
) == 0;
462 static inline bool arch_irqs_disabled(void)
464 return arch_irqs_disabled_flags(arch_local_save_flags());
467 #define hard_irq_disable() arch_local_irq_disable()
469 static inline bool arch_irq_disabled_regs(struct pt_regs
*regs
)
471 return !(regs
->msr
& MSR_EE
);
474 static __always_inline
bool should_hard_irq_enable(struct pt_regs
*regs
)
479 static inline void do_hard_irq_enable(void)
484 static inline void clear_pmi_irq_pending(void) { }
485 static inline void set_pmi_irq_pending(void) { }
486 static inline bool pmi_irq_pending(void) { return false; }
488 static inline void irq_soft_mask_regs_set_state(struct pt_regs
*regs
, unsigned long val
)
491 #endif /* CONFIG_PPC64 */
493 static inline unsigned long mtmsr_isync_irqsafe(unsigned long msr
)
496 if (arch_irqs_disabled()) {
498 * With soft-masking, MSR[EE] can change from 1 to 0
499 * asynchronously when irqs are disabled, and we don't want to
500 * set MSR[EE] back to 1 here if that has happened. A race-free
501 * way to do this is ensure EE is already 0. Another way it
502 * could be done is with a RESTART_TABLE handler, but that's
503 * probably overkill here.
507 irq_soft_mask_set(IRQS_ALL_DISABLED
);
508 local_paca
->irq_happened
|= PACA_IRQ_HARD_DIS
;
517 #define ARCH_IRQ_INIT_FLAGS IRQ_NOREQUEST
519 #endif /* __ASSEMBLY__ */
520 #endif /* __KERNEL__ */
521 #endif /* _ASM_POWERPC_HW_IRQ_H */