blob: b5f545db461a4ff4d427c5d51e8533ff9d3aeedd [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* 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
David Brazdil0f672f62019-12-10 10:32:29 +000011/* We use the MSB mostly because its available */
12#define PREEMPT_NEED_RESCHED 0x80000000
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000013#define PREEMPT_ENABLED (0 + PREEMPT_NEED_RESCHED)
14
15static inline int preempt_count(void)
16{
17 return READ_ONCE(S390_lowcore.preempt_count) & ~PREEMPT_NEED_RESCHED;
18}
19
20static 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000032static inline void set_preempt_need_resched(void)
33{
34 __atomic_and(~PREEMPT_NEED_RESCHED, &S390_lowcore.preempt_count);
35}
36
37static inline void clear_preempt_need_resched(void)
38{
39 __atomic_or(PREEMPT_NEED_RESCHED, &S390_lowcore.preempt_count);
40}
41
42static inline bool test_preempt_need_resched(void)
43{
44 return !(READ_ONCE(S390_lowcore.preempt_count) & PREEMPT_NEED_RESCHED);
45}
46
47static inline void __preempt_count_add(int val)
48{
49 if (__builtin_constant_p(val) && (val >= -128) && (val <= 127))
50 __atomic_add_const(val, &S390_lowcore.preempt_count);
51 else
52 __atomic_add(val, &S390_lowcore.preempt_count);
53}
54
55static inline void __preempt_count_sub(int val)
56{
57 __preempt_count_add(-val);
58}
59
60static inline bool __preempt_count_dec_and_test(void)
61{
62 return __atomic_add(-1, &S390_lowcore.preempt_count) == 1;
63}
64
65static inline bool should_resched(int preempt_offset)
66{
67 return unlikely(READ_ONCE(S390_lowcore.preempt_count) ==
68 preempt_offset);
69}
70
71#else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
72
73#define PREEMPT_ENABLED (0)
74
75static inline int preempt_count(void)
76{
77 return READ_ONCE(S390_lowcore.preempt_count);
78}
79
80static inline void preempt_count_set(int pc)
81{
82 S390_lowcore.preempt_count = pc;
83}
84
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000085static inline void set_preempt_need_resched(void)
86{
87}
88
89static inline void clear_preempt_need_resched(void)
90{
91}
92
93static inline bool test_preempt_need_resched(void)
94{
95 return false;
96}
97
98static inline void __preempt_count_add(int val)
99{
100 S390_lowcore.preempt_count += val;
101}
102
103static inline void __preempt_count_sub(int val)
104{
105 S390_lowcore.preempt_count -= val;
106}
107
108static inline bool __preempt_count_dec_and_test(void)
109{
110 return !--S390_lowcore.preempt_count && tif_need_resched();
111}
112
113static inline bool should_resched(int preempt_offset)
114{
115 return unlikely(preempt_count() == preempt_offset &&
116 tif_need_resched());
117}
118
119#endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
120
Olivier Deprez157378f2022-04-04 15:47:50 +0200121#define init_task_preempt_count(p) do { } while (0)
122/* Deferred to CPU bringup time */
123#define init_idle_preempt_count(p, cpu) do { } while (0)
124
125#ifdef CONFIG_PREEMPTION
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000126extern asmlinkage void preempt_schedule(void);
127#define __preempt_schedule() preempt_schedule()
128extern asmlinkage void preempt_schedule_notrace(void);
129#define __preempt_schedule_notrace() preempt_schedule_notrace()
Olivier Deprez157378f2022-04-04 15:47:50 +0200130#endif /* CONFIG_PREEMPTION */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000131
132#endif /* __ASM_PREEMPT_H */