blob: 1a59f0ed1ae392ac06fe26d2da2a9274239c0432 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-only */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Copyright (C) 2012 ARM Ltd.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004 */
5#ifndef __ASM_IRQFLAGS_H
6#define __ASM_IRQFLAGS_H
7
David Brazdil0f672f62019-12-10 10:32:29 +00008#include <asm/alternative.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009#include <asm/ptrace.h>
David Brazdil0f672f62019-12-10 10:32:29 +000010#include <asm/sysreg.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000011
12/*
13 * Aarch64 has flags for masking: Debug, Asynchronous (serror), Interrupts and
14 * FIQ exceptions, in the 'daif' register. We mask and unmask them in 'dai'
15 * order:
16 * Masking debug exceptions causes all other exceptions to be masked too/
17 * Masking SError masks irq, but not debug exceptions. Masking irqs has no
18 * side effects for other flags. Keeping to this order makes it easier for
19 * entry.S to know which exceptions should be unmasked.
20 *
21 * FIQ is never expected, but we mask it when we disable debug exceptions, and
22 * unmask it at all other times.
23 */
24
25/*
26 * CPU interrupt mask handling.
27 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000028static inline void arch_local_irq_enable(void)
29{
David Brazdil0f672f62019-12-10 10:32:29 +000030 if (system_has_prio_mask_debugging()) {
31 u32 pmr = read_sysreg_s(SYS_ICC_PMR_EL1);
32
33 WARN_ON_ONCE(pmr != GIC_PRIO_IRQON && pmr != GIC_PRIO_IRQOFF);
34 }
35
36 asm volatile(ALTERNATIVE(
37 "msr daifclr, #2 // arch_local_irq_enable\n"
38 "nop",
39 __msr_s(SYS_ICC_PMR_EL1, "%0")
40 "dsb sy",
41 ARM64_HAS_IRQ_PRIO_MASKING)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000042 :
David Brazdil0f672f62019-12-10 10:32:29 +000043 : "r" ((unsigned long) GIC_PRIO_IRQON)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000044 : "memory");
45}
46
47static inline void arch_local_irq_disable(void)
48{
David Brazdil0f672f62019-12-10 10:32:29 +000049 if (system_has_prio_mask_debugging()) {
50 u32 pmr = read_sysreg_s(SYS_ICC_PMR_EL1);
51
52 WARN_ON_ONCE(pmr != GIC_PRIO_IRQON && pmr != GIC_PRIO_IRQOFF);
53 }
54
55 asm volatile(ALTERNATIVE(
56 "msr daifset, #2 // arch_local_irq_disable",
57 __msr_s(SYS_ICC_PMR_EL1, "%0"),
58 ARM64_HAS_IRQ_PRIO_MASKING)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000059 :
David Brazdil0f672f62019-12-10 10:32:29 +000060 : "r" ((unsigned long) GIC_PRIO_IRQOFF)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000061 : "memory");
62}
63
64/*
65 * Save the current interrupt enable state.
66 */
67static inline unsigned long arch_local_save_flags(void)
68{
69 unsigned long flags;
David Brazdil0f672f62019-12-10 10:32:29 +000070
71 asm volatile(ALTERNATIVE(
72 "mrs %0, daif",
73 __mrs_s("%0", SYS_ICC_PMR_EL1),
74 ARM64_HAS_IRQ_PRIO_MASKING)
75 : "=&r" (flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076 :
77 : "memory");
David Brazdil0f672f62019-12-10 10:32:29 +000078
79 return flags;
80}
81
82static inline int arch_irqs_disabled_flags(unsigned long flags)
83{
84 int res;
85
86 asm volatile(ALTERNATIVE(
87 "and %w0, %w1, #" __stringify(PSR_I_BIT),
88 "eor %w0, %w1, #" __stringify(GIC_PRIO_IRQON),
89 ARM64_HAS_IRQ_PRIO_MASKING)
90 : "=&r" (res)
91 : "r" ((int) flags)
92 : "memory");
93
94 return res;
95}
96
97static inline unsigned long arch_local_irq_save(void)
98{
99 unsigned long flags;
100
101 flags = arch_local_save_flags();
102
103 /*
104 * There are too many states with IRQs disabled, just keep the current
105 * state if interrupts are already disabled/masked.
106 */
107 if (!arch_irqs_disabled_flags(flags))
108 arch_local_irq_disable();
109
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000110 return flags;
111}
112
113/*
114 * restore saved IRQ state
115 */
116static inline void arch_local_irq_restore(unsigned long flags)
117{
David Brazdil0f672f62019-12-10 10:32:29 +0000118 asm volatile(ALTERNATIVE(
119 "msr daif, %0\n"
120 "nop",
121 __msr_s(SYS_ICC_PMR_EL1, "%0")
122 "dsb sy",
123 ARM64_HAS_IRQ_PRIO_MASKING)
124 :
125 : "r" (flags)
126 : "memory");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000127}
128
David Brazdil0f672f62019-12-10 10:32:29 +0000129#endif /* __ASM_IRQFLAGS_H */