blob: 43d8366c1e878ef2635d9c3b69733021c804e6f5 [file] [log] [blame]
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +00001/*
2 * Copyright (C) 2012 ARM Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16#ifndef __ASM_IRQFLAGS_H
17#define __ASM_IRQFLAGS_H
18
19#ifdef __KERNEL__
20
Julien Thierry4a503212019-01-31 14:58:50 +000021#include <asm/alternative.h>
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +000022#include <asm/ptrace.h>
Julien Thierry4a503212019-01-31 14:58:50 +000023#include <asm/sysreg.h>
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +000024
25/*
James Morse65be7a12017-11-02 12:12:35 +000026 * Aarch64 has flags for masking: Debug, Asynchronous (serror), Interrupts and
27 * FIQ exceptions, in the 'daif' register. We mask and unmask them in 'dai'
28 * order:
29 * Masking debug exceptions causes all other exceptions to be masked too/
30 * Masking SError masks irq, but not debug exceptions. Masking irqs has no
31 * side effects for other flags. Keeping to this order makes it easier for
32 * entry.S to know which exceptions should be unmasked.
33 *
34 * FIQ is never expected, but we mask it when we disable debug exceptions, and
35 * unmask it at all other times.
36 */
37
38/*
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +000039 * CPU interrupt mask handling.
40 */
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +000041static inline void arch_local_irq_enable(void)
42{
Julien Thierry4a503212019-01-31 14:58:50 +000043 asm volatile(ALTERNATIVE(
44 "msr daifclr, #2 // arch_local_irq_enable\n"
45 "nop",
46 "msr_s " __stringify(SYS_ICC_PMR_EL1) ",%0\n"
47 "dsb sy",
48 ARM64_HAS_IRQ_PRIO_MASKING)
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +000049 :
Julien Thierrya80554f2019-02-08 09:36:48 +000050 : "r" ((unsigned long) GIC_PRIO_IRQON)
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +000051 : "memory");
52}
53
54static inline void arch_local_irq_disable(void)
55{
Julien Thierry4a503212019-01-31 14:58:50 +000056 asm volatile(ALTERNATIVE(
57 "msr daifset, #2 // arch_local_irq_disable",
58 "msr_s " __stringify(SYS_ICC_PMR_EL1) ", %0",
59 ARM64_HAS_IRQ_PRIO_MASKING)
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +000060 :
Julien Thierrya80554f2019-02-08 09:36:48 +000061 : "r" ((unsigned long) GIC_PRIO_IRQOFF)
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +000062 : "memory");
63}
64
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +000065/*
66 * Save the current interrupt enable state.
67 */
68static inline unsigned long arch_local_save_flags(void)
69{
Julien Thierry4a503212019-01-31 14:58:50 +000070 unsigned long daif_bits;
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +000071 unsigned long flags;
Julien Thierry4a503212019-01-31 14:58:50 +000072
73 daif_bits = read_sysreg(daif);
74
75 /*
76 * The asm is logically equivalent to:
77 *
78 * if (system_uses_irq_prio_masking())
79 * flags = (daif_bits & PSR_I_BIT) ?
80 * GIC_PRIO_IRQOFF :
81 * read_sysreg_s(SYS_ICC_PMR_EL1);
82 * else
83 * flags = daif_bits;
84 */
85 asm volatile(ALTERNATIVE(
86 "mov %0, %1\n"
87 "nop\n"
88 "nop",
89 "mrs_s %0, " __stringify(SYS_ICC_PMR_EL1) "\n"
90 "ands %1, %1, " __stringify(PSR_I_BIT) "\n"
91 "csel %0, %0, %2, eq",
92 ARM64_HAS_IRQ_PRIO_MASKING)
93 : "=&r" (flags), "+r" (daif_bits)
Julien Thierrya80554f2019-02-08 09:36:48 +000094 : "r" ((unsigned long) GIC_PRIO_IRQOFF)
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +000095 : "memory");
Julien Thierry4a503212019-01-31 14:58:50 +000096
97 return flags;
98}
99
100static inline unsigned long arch_local_irq_save(void)
101{
102 unsigned long flags;
103
104 flags = arch_local_save_flags();
105
106 arch_local_irq_disable();
107
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +0000108 return flags;
109}
110
111/*
112 * restore saved IRQ state
113 */
114static inline void arch_local_irq_restore(unsigned long flags)
115{
Julien Thierry4a503212019-01-31 14:58:50 +0000116 asm volatile(ALTERNATIVE(
117 "msr daif, %0\n"
118 "nop",
119 "msr_s " __stringify(SYS_ICC_PMR_EL1) ", %0\n"
120 "dsb sy",
121 ARM64_HAS_IRQ_PRIO_MASKING)
122 : "+r" (flags)
123 :
124 : "memory");
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +0000125}
126
127static inline int arch_irqs_disabled_flags(unsigned long flags)
128{
Julien Thierry4a503212019-01-31 14:58:50 +0000129 int res;
130
131 asm volatile(ALTERNATIVE(
132 "and %w0, %w1, #" __stringify(PSR_I_BIT) "\n"
133 "nop",
134 "cmp %w1, #" __stringify(GIC_PRIO_IRQOFF) "\n"
135 "cset %w0, ls",
136 ARM64_HAS_IRQ_PRIO_MASKING)
137 : "=&r" (res)
138 : "r" ((int) flags)
139 : "memory");
140
141 return res;
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +0000142}
Marc Zyngierfb9bd7d62012-03-05 11:49:29 +0000143#endif
144#endif