xref: /openbmc/linux/arch/s390/include/asm/preempt.h (revision ba61bb17)
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 #define PREEMPT_ENABLED	(0 + PREEMPT_NEED_RESCHED)
12 
13 static inline int preempt_count(void)
14 {
15 	return READ_ONCE(S390_lowcore.preempt_count) & ~PREEMPT_NEED_RESCHED;
16 }
17 
18 static inline void preempt_count_set(int pc)
19 {
20 	int old, new;
21 
22 	do {
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,
27 				  old, new) != old);
28 }
29 
30 #define init_task_preempt_count(p)	do { } while (0)
31 
32 #define init_idle_preempt_count(p, cpu)	do { \
33 	S390_lowcore.preempt_count = PREEMPT_ENABLED; \
34 } while (0)
35 
36 static inline void set_preempt_need_resched(void)
37 {
38 	__atomic_and(~PREEMPT_NEED_RESCHED, &S390_lowcore.preempt_count);
39 }
40 
41 static inline void clear_preempt_need_resched(void)
42 {
43 	__atomic_or(PREEMPT_NEED_RESCHED, &S390_lowcore.preempt_count);
44 }
45 
46 static inline bool test_preempt_need_resched(void)
47 {
48 	return !(READ_ONCE(S390_lowcore.preempt_count) & PREEMPT_NEED_RESCHED);
49 }
50 
51 static inline void __preempt_count_add(int val)
52 {
53 	if (__builtin_constant_p(val) && (val >= -128) && (val <= 127))
54 		__atomic_add_const(val, &S390_lowcore.preempt_count);
55 	else
56 		__atomic_add(val, &S390_lowcore.preempt_count);
57 }
58 
59 static inline void __preempt_count_sub(int val)
60 {
61 	__preempt_count_add(-val);
62 }
63 
64 static inline bool __preempt_count_dec_and_test(void)
65 {
66 	return __atomic_add(-1, &S390_lowcore.preempt_count) == 1;
67 }
68 
69 static inline bool should_resched(int preempt_offset)
70 {
71 	return unlikely(READ_ONCE(S390_lowcore.preempt_count) ==
72 			preempt_offset);
73 }
74 
75 #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
76 
77 #define PREEMPT_ENABLED	(0)
78 
79 static inline int preempt_count(void)
80 {
81 	return READ_ONCE(S390_lowcore.preempt_count);
82 }
83 
84 static inline void preempt_count_set(int pc)
85 {
86 	S390_lowcore.preempt_count = pc;
87 }
88 
89 #define init_task_preempt_count(p)	do { } while (0)
90 
91 #define init_idle_preempt_count(p, cpu)	do { \
92 	S390_lowcore.preempt_count = PREEMPT_ENABLED; \
93 } while (0)
94 
95 static inline void set_preempt_need_resched(void)
96 {
97 }
98 
99 static inline void clear_preempt_need_resched(void)
100 {
101 }
102 
103 static inline bool test_preempt_need_resched(void)
104 {
105 	return false;
106 }
107 
108 static inline void __preempt_count_add(int val)
109 {
110 	S390_lowcore.preempt_count += val;
111 }
112 
113 static inline void __preempt_count_sub(int val)
114 {
115 	S390_lowcore.preempt_count -= val;
116 }
117 
118 static inline bool __preempt_count_dec_and_test(void)
119 {
120 	return !--S390_lowcore.preempt_count && tif_need_resched();
121 }
122 
123 static inline bool should_resched(int preempt_offset)
124 {
125 	return unlikely(preempt_count() == preempt_offset &&
126 			tif_need_resched());
127 }
128 
129 #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
130 
131 #ifdef CONFIG_PREEMPT
132 extern asmlinkage void preempt_schedule(void);
133 #define __preempt_schedule() preempt_schedule()
134 extern asmlinkage void preempt_schedule_notrace(void);
135 #define __preempt_schedule_notrace() preempt_schedule_notrace()
136 #endif /* CONFIG_PREEMPT */
137 
138 #endif /* __ASM_PREEMPT_H */
139