GNU Linux-libre 4.14.290-gnu1
[releases.git] / arch / s390 / include / asm / preempt.h
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         /*
54          * With some obscure config options and CONFIG_PROFILE_ALL_BRANCHES
55          * enabled, gcc 12 fails to handle __builtin_constant_p().
56          */
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);
60                         return;
61                 }
62         }
63         __atomic_add(val, &S390_lowcore.preempt_count);
64 }
65
66 static inline void __preempt_count_sub(int val)
67 {
68         __preempt_count_add(-val);
69 }
70
71 static inline bool __preempt_count_dec_and_test(void)
72 {
73         return __atomic_add(-1, &S390_lowcore.preempt_count) == 1;
74 }
75
76 static inline bool should_resched(int preempt_offset)
77 {
78         return unlikely(READ_ONCE(S390_lowcore.preempt_count) ==
79                         preempt_offset);
80 }
81
82 #else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
83
84 #define PREEMPT_ENABLED (0)
85
86 static inline int preempt_count(void)
87 {
88         return READ_ONCE(S390_lowcore.preempt_count);
89 }
90
91 static inline void preempt_count_set(int pc)
92 {
93         S390_lowcore.preempt_count = pc;
94 }
95
96 #define init_task_preempt_count(p)      do { } while (0)
97
98 #define init_idle_preempt_count(p, cpu) do { \
99         S390_lowcore.preempt_count = PREEMPT_ENABLED; \
100 } while (0)
101
102 static inline void set_preempt_need_resched(void)
103 {
104 }
105
106 static inline void clear_preempt_need_resched(void)
107 {
108 }
109
110 static inline bool test_preempt_need_resched(void)
111 {
112         return false;
113 }
114
115 static inline void __preempt_count_add(int val)
116 {
117         S390_lowcore.preempt_count += val;
118 }
119
120 static inline void __preempt_count_sub(int val)
121 {
122         S390_lowcore.preempt_count -= val;
123 }
124
125 static inline bool __preempt_count_dec_and_test(void)
126 {
127         return !--S390_lowcore.preempt_count && tif_need_resched();
128 }
129
130 static inline bool should_resched(int preempt_offset)
131 {
132         return unlikely(preempt_count() == preempt_offset &&
133                         tif_need_resched());
134 }
135
136 #endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
137
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 */
144
145 #endif /* __ASM_PREEMPT_H */