1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_PREEMPT_H
3 #define __ASM_PREEMPT_H
4
5 #include <asm/current.h>
6 #include <linux/thread_info.h>
7 #include <asm/atomic_ops.h>
8
9 #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
10
11 /* We use the MSB mostly because its available */
12 #define PREEMPT_NEED_RESCHED 0x80000000
13 #define PREEMPT_ENABLED (0 + PREEMPT_NEED_RESCHED)
14
preempt_count(void)15 static inline int preempt_count(void)
16 {
17 return READ_ONCE(S390_lowcore.preempt_count) & ~PREEMPT_NEED_RESCHED;
18 }
19
preempt_count_set(int pc)20 static inline void preempt_count_set(int pc)
21 {
22 int old, new;
23
24 do {
25 old = READ_ONCE(S390_lowcore.preempt_count);
26 new = (old & PREEMPT_NEED_RESCHED) |
27 (pc & ~PREEMPT_NEED_RESCHED);
28 } while (__atomic_cmpxchg(&S390_lowcore.preempt_count,
29 old, new) != old);
30 }
31
32 #define init_task_preempt_count(p) do { } while (0)
33
34 #define init_idle_preempt_count(p, cpu) do { \
35 S390_lowcore.preempt_count = PREEMPT_ENABLED; \
36 } while (0)
37
set_preempt_need_resched(void)38 static inline void set_preempt_need_resched(void)
39 {
40 __atomic_and(~PREEMPT_NEED_RESCHED, &S390_lowcore.preempt_count);
41 }
42
clear_preempt_need_resched(void)43 static inline void clear_preempt_need_resched(void)
44 {
45 __atomic_or(PREEMPT_NEED_RESCHED, &S390_lowcore.preempt_count);
46 }
47
test_preempt_need_resched(void)48 static inline bool test_preempt_need_resched(void)
49 {
50 return !(READ_ONCE(S390_lowcore.preempt_count) & PREEMPT_NEED_RESCHED);
51 }
52
__preempt_count_add(int val)53 static inline void __preempt_count_add(int val)
54 {
55 /*
56 * With some obscure config options and CONFIG_PROFILE_ALL_BRANCHES
57 * enabled, gcc 12 fails to handle __builtin_constant_p().
58 */
59 if (!IS_ENABLED(CONFIG_PROFILE_ALL_BRANCHES)) {
60 if (__builtin_constant_p(val) && (val >= -128) && (val <= 127)) {
61 __atomic_add_const(val, &S390_lowcore.preempt_count);
62 return;
63 }
64 }
65 __atomic_add(val, &S390_lowcore.preempt_count);
66 }
67
__preempt_count_sub(int val)68 static inline void __preempt_count_sub(int val)
69 {
70 __preempt_count_add(-val);
71 }
72
__preempt_count_dec_and_test(void)73 static inline bool __preempt_count_dec_and_test(void)
74 {
75 return __atomic_add(-1, &S390_lowcore.preempt_count) == 1;
76 }
77
should_resched(int preempt_offset)78 static inline bool should_resched(int preempt_offset)
79 {
80 return unlikely(READ_ONCE(S390_lowcore.preempt_count) ==
81 preempt_offset);
82 }
83
84 #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
85
86 #define PREEMPT_ENABLED (0)
87
preempt_count(void)88 static inline int preempt_count(void)
89 {
90 return READ_ONCE(S390_lowcore.preempt_count);
91 }
92
preempt_count_set(int pc)93 static inline void preempt_count_set(int pc)
94 {
95 S390_lowcore.preempt_count = pc;
96 }
97
98 #define init_task_preempt_count(p) do { } while (0)
99
100 #define init_idle_preempt_count(p, cpu) do { \
101 S390_lowcore.preempt_count = PREEMPT_ENABLED; \
102 } while (0)
103
set_preempt_need_resched(void)104 static inline void set_preempt_need_resched(void)
105 {
106 }
107
clear_preempt_need_resched(void)108 static inline void clear_preempt_need_resched(void)
109 {
110 }
111
test_preempt_need_resched(void)112 static inline bool test_preempt_need_resched(void)
113 {
114 return false;
115 }
116
__preempt_count_add(int val)117 static inline void __preempt_count_add(int val)
118 {
119 S390_lowcore.preempt_count += val;
120 }
121
__preempt_count_sub(int val)122 static inline void __preempt_count_sub(int val)
123 {
124 S390_lowcore.preempt_count -= val;
125 }
126
__preempt_count_dec_and_test(void)127 static inline bool __preempt_count_dec_and_test(void)
128 {
129 return !--S390_lowcore.preempt_count && tif_need_resched();
130 }
131
should_resched(int preempt_offset)132 static inline bool should_resched(int preempt_offset)
133 {
134 return unlikely(preempt_count() == preempt_offset &&
135 tif_need_resched());
136 }
137
138 #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
139
140 #ifdef CONFIG_PREEMPT
141 extern asmlinkage void preempt_schedule(void);
142 #define __preempt_schedule() preempt_schedule()
143 extern asmlinkage void preempt_schedule_notrace(void);
144 #define __preempt_schedule_notrace() preempt_schedule_notrace()
145 #endif /* CONFIG_PREEMPT */
146
147 #endif /* __ASM_PREEMPT_H */
148