1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __ASM_PREEMPT_H
3 #define __ASM_PREEMPT_H
5 #include <asm/current.h>
6 #include <linux/thread_info.h>
7 #include <asm/atomic_ops.h>
9 #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
11 #define PREEMPT_ENABLED (0 + PREEMPT_NEED_RESCHED)
13 static inline int preempt_count(void)
15 return READ_ONCE(S390_lowcore.preempt_count) & ~PREEMPT_NEED_RESCHED;
18 static inline void preempt_count_set(int pc)
23 old = READ_ONCE(S390_lowcore.preempt_count);
24 new = (old & PREEMPT_NEED_RESCHED) |
25 (pc & ~PREEMPT_NEED_RESCHED);
26 } while (__atomic_cmpxchg(&S390_lowcore.preempt_count,
30 #define init_task_preempt_count(p) do { } while (0)
32 #define init_idle_preempt_count(p, cpu) do { \
33 S390_lowcore.preempt_count = PREEMPT_ENABLED; \
36 static inline void set_preempt_need_resched(void)
38 __atomic_and(~PREEMPT_NEED_RESCHED, &S390_lowcore.preempt_count);
41 static inline void clear_preempt_need_resched(void)
43 __atomic_or(PREEMPT_NEED_RESCHED, &S390_lowcore.preempt_count);
46 static inline bool test_preempt_need_resched(void)
48 return !(READ_ONCE(S390_lowcore.preempt_count) & PREEMPT_NEED_RESCHED);
51 static inline void __preempt_count_add(int val)
54 * With some obscure config options and CONFIG_PROFILE_ALL_BRANCHES
55 * enabled, gcc 12 fails to handle __builtin_constant_p().
57 if (!IS_ENABLED(CONFIG_PROFILE_ALL_BRANCHES)) {
58 if (__builtin_constant_p(val) && (val >= -128) && (val <= 127)) {
59 __atomic_add_const(val, &S390_lowcore.preempt_count);
63 __atomic_add(val, &S390_lowcore.preempt_count);
66 static inline void __preempt_count_sub(int val)
68 __preempt_count_add(-val);
71 static inline bool __preempt_count_dec_and_test(void)
73 return __atomic_add(-1, &S390_lowcore.preempt_count) == 1;
76 static inline bool should_resched(int preempt_offset)
78 return unlikely(READ_ONCE(S390_lowcore.preempt_count) ==
82 #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
84 #define PREEMPT_ENABLED (0)
86 static inline int preempt_count(void)
88 return READ_ONCE(S390_lowcore.preempt_count);
91 static inline void preempt_count_set(int pc)
93 S390_lowcore.preempt_count = pc;
96 #define init_task_preempt_count(p) do { } while (0)
98 #define init_idle_preempt_count(p, cpu) do { \
99 S390_lowcore.preempt_count = PREEMPT_ENABLED; \
102 static inline void set_preempt_need_resched(void)
106 static inline void clear_preempt_need_resched(void)
110 static inline bool test_preempt_need_resched(void)
115 static inline void __preempt_count_add(int val)
117 S390_lowcore.preempt_count += val;
120 static inline void __preempt_count_sub(int val)
122 S390_lowcore.preempt_count -= val;
125 static inline bool __preempt_count_dec_and_test(void)
127 return !--S390_lowcore.preempt_count && tif_need_resched();
130 static inline bool should_resched(int preempt_offset)
132 return unlikely(preempt_count() == preempt_offset &&
136 #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
138 #ifdef CONFIG_PREEMPT
139 extern asmlinkage void preempt_schedule(void);
140 #define __preempt_schedule() preempt_schedule()
141 extern asmlinkage void preempt_schedule_notrace(void);
142 #define __preempt_schedule_notrace() preempt_schedule_notrace()
143 #endif /* CONFIG_PREEMPT */
145 #endif /* __ASM_PREEMPT_H */