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>
8 #include <asm/cmpxchg.h>
11 #ifdef MARCH_HAS_Z196_FEATURES
13 /* We use the MSB mostly because its available */
14 #define PREEMPT_NEED_RESCHED 0x80000000
15 #define PREEMPT_ENABLED (0 + PREEMPT_NEED_RESCHED)
17 static __always_inline int preempt_count(void)
19 return READ_ONCE(get_lowcore()->preempt_count) & ~PREEMPT_NEED_RESCHED;
22 static __always_inline void preempt_count_set(int pc)
26 old = READ_ONCE(get_lowcore()->preempt_count);
28 new = (old & PREEMPT_NEED_RESCHED) | (pc & ~PREEMPT_NEED_RESCHED);
29 } while (!arch_try_cmpxchg(&get_lowcore()->preempt_count, &old, new));
32 static __always_inline void set_preempt_need_resched(void)
34 __atomic_and(~PREEMPT_NEED_RESCHED, &get_lowcore()->preempt_count);
37 static __always_inline void clear_preempt_need_resched(void)
39 __atomic_or(PREEMPT_NEED_RESCHED, &get_lowcore()->preempt_count);
42 static __always_inline bool test_preempt_need_resched(void)
44 return !(READ_ONCE(get_lowcore()->preempt_count) & PREEMPT_NEED_RESCHED);
47 static __always_inline void __preempt_count_add(int val)
50 * With some obscure config options and CONFIG_PROFILE_ALL_BRANCHES
51 * enabled, gcc 12 fails to handle __builtin_constant_p().
53 if (!IS_ENABLED(CONFIG_PROFILE_ALL_BRANCHES)) {
54 if (__builtin_constant_p(val) && (val >= -128) && (val <= 127)) {
55 __atomic_add_const(val, &get_lowcore()->preempt_count);
59 __atomic_add(val, &get_lowcore()->preempt_count);
62 static __always_inline void __preempt_count_sub(int val)
64 __preempt_count_add(-val);
67 static __always_inline bool __preempt_count_dec_and_test(void)
69 return __atomic_add(-1, &get_lowcore()->preempt_count) == 1;
72 static __always_inline bool should_resched(int preempt_offset)
74 return unlikely(READ_ONCE(get_lowcore()->preempt_count) ==
78 #else /* MARCH_HAS_Z196_FEATURES */
80 #define PREEMPT_ENABLED (0)
82 static __always_inline int preempt_count(void)
84 return READ_ONCE(get_lowcore()->preempt_count);
87 static __always_inline void preempt_count_set(int pc)
89 get_lowcore()->preempt_count = pc;
92 static __always_inline void set_preempt_need_resched(void)
96 static __always_inline void clear_preempt_need_resched(void)
100 static __always_inline bool test_preempt_need_resched(void)
105 static __always_inline void __preempt_count_add(int val)
107 get_lowcore()->preempt_count += val;
110 static __always_inline void __preempt_count_sub(int val)
112 get_lowcore()->preempt_count -= val;
115 static __always_inline bool __preempt_count_dec_and_test(void)
117 return !--get_lowcore()->preempt_count && tif_need_resched();
120 static __always_inline bool should_resched(int preempt_offset)
122 return unlikely(preempt_count() == preempt_offset &&
126 #endif /* MARCH_HAS_Z196_FEATURES */
128 #define init_task_preempt_count(p) do { } while (0)
129 /* Deferred to CPU bringup time */
130 #define init_idle_preempt_count(p, cpu) do { } while (0)
132 #ifdef CONFIG_PREEMPTION
134 void preempt_schedule(void);
135 void preempt_schedule_notrace(void);
137 #ifdef CONFIG_PREEMPT_DYNAMIC
139 void dynamic_preempt_schedule(void);
140 void dynamic_preempt_schedule_notrace(void);
141 #define __preempt_schedule() dynamic_preempt_schedule()
142 #define __preempt_schedule_notrace() dynamic_preempt_schedule_notrace()
144 #else /* CONFIG_PREEMPT_DYNAMIC */
146 #define __preempt_schedule() preempt_schedule()
147 #define __preempt_schedule_notrace() preempt_schedule_notrace()
149 #endif /* CONFIG_PREEMPT_DYNAMIC */
151 #endif /* CONFIG_PREEMPTION */
153 #endif /* __ASM_PREEMPT_H */