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 <linux/thread_info.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#define PREEMPT_ENABLED	(0)
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_cistatic __always_inline int preempt_count(void)
108c2ecf20Sopenharmony_ci{
118c2ecf20Sopenharmony_ci	return READ_ONCE(current_thread_info()->preempt_count);
128c2ecf20Sopenharmony_ci}
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistatic __always_inline volatile int *preempt_count_ptr(void)
158c2ecf20Sopenharmony_ci{
168c2ecf20Sopenharmony_ci	return &current_thread_info()->preempt_count;
178c2ecf20Sopenharmony_ci}
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic __always_inline void preempt_count_set(int pc)
208c2ecf20Sopenharmony_ci{
218c2ecf20Sopenharmony_ci	*preempt_count_ptr() = pc;
228c2ecf20Sopenharmony_ci}
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/*
258c2ecf20Sopenharmony_ci * must be macros to avoid header recursion hell
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_ci#define init_task_preempt_count(p) do { \
288c2ecf20Sopenharmony_ci	task_thread_info(p)->preempt_count = FORK_PREEMPT_COUNT; \
298c2ecf20Sopenharmony_ci} while (0)
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define init_idle_preempt_count(p, cpu) do { \
328c2ecf20Sopenharmony_ci	task_thread_info(p)->preempt_count = PREEMPT_DISABLED; \
338c2ecf20Sopenharmony_ci} while (0)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic __always_inline void set_preempt_need_resched(void)
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci}
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic __always_inline void clear_preempt_need_resched(void)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic __always_inline bool test_preempt_need_resched(void)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	return false;
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/*
498c2ecf20Sopenharmony_ci * The various preempt_count add/sub methods
508c2ecf20Sopenharmony_ci */
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic __always_inline void __preempt_count_add(int val)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	*preempt_count_ptr() += val;
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic __always_inline void __preempt_count_sub(int val)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	*preempt_count_ptr() -= val;
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic __always_inline bool __preempt_count_dec_and_test(void)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	/*
658c2ecf20Sopenharmony_ci	 * Because of load-store architectures cannot do per-cpu atomic
668c2ecf20Sopenharmony_ci	 * operations; we cannot use PREEMPT_NEED_RESCHED because it might get
678c2ecf20Sopenharmony_ci	 * lost.
688c2ecf20Sopenharmony_ci	 */
698c2ecf20Sopenharmony_ci	return !--*preempt_count_ptr() && tif_need_resched();
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/*
738c2ecf20Sopenharmony_ci * Returns true when we need to resched and can (barring IRQ state).
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_cistatic __always_inline bool should_resched(int preempt_offset)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	return unlikely(preempt_count() == preempt_offset &&
788c2ecf20Sopenharmony_ci			tif_need_resched());
798c2ecf20Sopenharmony_ci}
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci#ifdef CONFIG_PREEMPTION
828c2ecf20Sopenharmony_ciextern asmlinkage void preempt_schedule(void);
838c2ecf20Sopenharmony_ci#define __preempt_schedule() preempt_schedule()
848c2ecf20Sopenharmony_ciextern asmlinkage void preempt_schedule_notrace(void);
858c2ecf20Sopenharmony_ci#define __preempt_schedule_notrace() preempt_schedule_notrace()
868c2ecf20Sopenharmony_ci#endif /* CONFIG_PREEMPTION */
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#endif /* __ASM_PREEMPT_H */
89