18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef __ASM_PREEMPT_H
38c2ecf20Sopenharmony_ci#define __ASM_PREEMPT_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <asm/current.h>
68c2ecf20Sopenharmony_ci#include <linux/thread_info.h>
78c2ecf20Sopenharmony_ci#include <asm/atomic_ops.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/* We use the MSB mostly because its available */
128c2ecf20Sopenharmony_ci#define PREEMPT_NEED_RESCHED	0x80000000
138c2ecf20Sopenharmony_ci#define PREEMPT_ENABLED	(0 + PREEMPT_NEED_RESCHED)
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistatic inline int preempt_count(void)
168c2ecf20Sopenharmony_ci{
178c2ecf20Sopenharmony_ci	return READ_ONCE(S390_lowcore.preempt_count) & ~PREEMPT_NEED_RESCHED;
188c2ecf20Sopenharmony_ci}
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic inline void preempt_count_set(int pc)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	int old, new;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	do {
258c2ecf20Sopenharmony_ci		old = READ_ONCE(S390_lowcore.preempt_count);
268c2ecf20Sopenharmony_ci		new = (old & PREEMPT_NEED_RESCHED) |
278c2ecf20Sopenharmony_ci			(pc & ~PREEMPT_NEED_RESCHED);
288c2ecf20Sopenharmony_ci	} while (__atomic_cmpxchg(&S390_lowcore.preempt_count,
298c2ecf20Sopenharmony_ci				  old, new) != old);
308c2ecf20Sopenharmony_ci}
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic inline void set_preempt_need_resched(void)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	__atomic_and(~PREEMPT_NEED_RESCHED, &S390_lowcore.preempt_count);
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic inline void clear_preempt_need_resched(void)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	__atomic_or(PREEMPT_NEED_RESCHED, &S390_lowcore.preempt_count);
408c2ecf20Sopenharmony_ci}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic inline bool test_preempt_need_resched(void)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	return !(READ_ONCE(S390_lowcore.preempt_count) & PREEMPT_NEED_RESCHED);
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic inline void __preempt_count_add(int val)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	/*
508c2ecf20Sopenharmony_ci	 * With some obscure config options and CONFIG_PROFILE_ALL_BRANCHES
518c2ecf20Sopenharmony_ci	 * enabled, gcc 12 fails to handle __builtin_constant_p().
528c2ecf20Sopenharmony_ci	 */
538c2ecf20Sopenharmony_ci	if (!IS_ENABLED(CONFIG_PROFILE_ALL_BRANCHES)) {
548c2ecf20Sopenharmony_ci		if (__builtin_constant_p(val) && (val >= -128) && (val <= 127)) {
558c2ecf20Sopenharmony_ci			__atomic_add_const(val, &S390_lowcore.preempt_count);
568c2ecf20Sopenharmony_ci			return;
578c2ecf20Sopenharmony_ci		}
588c2ecf20Sopenharmony_ci	}
598c2ecf20Sopenharmony_ci	__atomic_add(val, &S390_lowcore.preempt_count);
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic inline void __preempt_count_sub(int val)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	__preempt_count_add(-val);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic inline bool __preempt_count_dec_and_test(void)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	return __atomic_add(-1, &S390_lowcore.preempt_count) == 1;
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic inline bool should_resched(int preempt_offset)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	return unlikely(READ_ONCE(S390_lowcore.preempt_count) ==
758c2ecf20Sopenharmony_ci			preempt_offset);
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci#else /* CONFIG_HAVE_MARCH_Z196_FEATURES */
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#define PREEMPT_ENABLED	(0)
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic inline int preempt_count(void)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	return READ_ONCE(S390_lowcore.preempt_count);
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic inline void preempt_count_set(int pc)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	S390_lowcore.preempt_count = pc;
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic inline void set_preempt_need_resched(void)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic inline void clear_preempt_need_resched(void)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic inline bool test_preempt_need_resched(void)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	return false;
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic inline void __preempt_count_add(int val)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	S390_lowcore.preempt_count += val;
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic inline void __preempt_count_sub(int val)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	S390_lowcore.preempt_count -= val;
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic inline bool __preempt_count_dec_and_test(void)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	return !--S390_lowcore.preempt_count && tif_need_resched();
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic inline bool should_resched(int preempt_offset)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	return unlikely(preempt_count() == preempt_offset &&
1238c2ecf20Sopenharmony_ci			tif_need_resched());
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci#endif /* CONFIG_HAVE_MARCH_Z196_FEATURES */
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci#define init_task_preempt_count(p)	do { } while (0)
1298c2ecf20Sopenharmony_ci/* Deferred to CPU bringup time */
1308c2ecf20Sopenharmony_ci#define init_idle_preempt_count(p, cpu)	do { } while (0)
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci#ifdef CONFIG_PREEMPTION
1338c2ecf20Sopenharmony_ciextern asmlinkage void preempt_schedule(void);
1348c2ecf20Sopenharmony_ci#define __preempt_schedule() preempt_schedule()
1358c2ecf20Sopenharmony_ciextern asmlinkage void preempt_schedule_notrace(void);
1368c2ecf20Sopenharmony_ci#define __preempt_schedule_notrace() preempt_schedule_notrace()
1378c2ecf20Sopenharmony_ci#endif /* CONFIG_PREEMPTION */
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci#endif /* __ASM_PREEMPT_H */
140