blob: 99a7fa9ab0a32a1c24e4b699aa3ad10b4640b9f5 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +02002#ifndef __ASM_PREEMPT_H
3#define __ASM_PREEMPT_H
4
5#include <asm/rmwcc.h>
6#include <asm/percpu.h>
7#include <linux/thread_info.h>
8
9DECLARE_PER_CPU(int, __preempt_count);
10
Will Deacon08861d32018-09-19 13:39:26 +010011/* We use the MSB mostly because its available */
12#define PREEMPT_NEED_RESCHED 0x80000000
13
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +020014/*
Peter Zijlstraba1f14f2013-11-28 14:26:41 +010015 * We use the PREEMPT_NEED_RESCHED bit as an inverted NEED_RESCHED such
16 * that a decrement hitting 0 means we can and should reschedule.
17 */
18#define PREEMPT_ENABLED (0 + PREEMPT_NEED_RESCHED)
19
20/*
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +020021 * We mask the PREEMPT_NEED_RESCHED bit so as not to confuse all current users
22 * that think a non-zero value indicates we cannot preempt.
23 */
24static __always_inline int preempt_count(void)
25{
Christoph Lameterb3ca1c12014-04-07 15:39:34 -070026 return raw_cpu_read_4(__preempt_count) & ~PREEMPT_NEED_RESCHED;
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +020027}
28
29static __always_inline void preempt_count_set(int pc)
30{
Martin Schwidefskyf2851442016-11-07 14:01:00 +010031 int old, new;
32
33 do {
34 old = raw_cpu_read_4(__preempt_count);
35 new = (old & PREEMPT_NEED_RESCHED) |
36 (pc & ~PREEMPT_NEED_RESCHED);
37 } while (raw_cpu_cmpxchg_4(__preempt_count, old, new) != old);
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +020038}
39
40/*
41 * must be macros to avoid header recursion hell
42 */
Peter Zijlstrad87b7a32015-09-28 18:11:18 +020043#define init_task_preempt_count(p) do { } while (0)
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +020044
45#define init_idle_preempt_count(p, cpu) do { \
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +020046 per_cpu(__preempt_count, (cpu)) = PREEMPT_ENABLED; \
47} while (0)
48
49/*
50 * We fold the NEED_RESCHED bit into the preempt count such that
51 * preempt_enable() can decrement and test for needing to reschedule with a
52 * single instruction.
53 *
54 * We invert the actual bit, so that when the decrement hits 0 we know we both
55 * need to resched (the bit is cleared) and can resched (no preempt count).
56 */
57
58static __always_inline void set_preempt_need_resched(void)
59{
Christoph Lameterb3ca1c12014-04-07 15:39:34 -070060 raw_cpu_and_4(__preempt_count, ~PREEMPT_NEED_RESCHED);
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +020061}
62
63static __always_inline void clear_preempt_need_resched(void)
64{
Christoph Lameterb3ca1c12014-04-07 15:39:34 -070065 raw_cpu_or_4(__preempt_count, PREEMPT_NEED_RESCHED);
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +020066}
67
68static __always_inline bool test_preempt_need_resched(void)
69{
Christoph Lameterb3ca1c12014-04-07 15:39:34 -070070 return !(raw_cpu_read_4(__preempt_count) & PREEMPT_NEED_RESCHED);
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +020071}
72
73/*
74 * The various preempt_count add/sub methods
75 */
76
77static __always_inline void __preempt_count_add(int val)
78{
Christoph Lameterb3ca1c12014-04-07 15:39:34 -070079 raw_cpu_add_4(__preempt_count, val);
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +020080}
81
82static __always_inline void __preempt_count_sub(int val)
83{
Christoph Lameterb3ca1c12014-04-07 15:39:34 -070084 raw_cpu_add_4(__preempt_count, -val);
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +020085}
86
Peter Zijlstraba1f14f2013-11-28 14:26:41 +010087/*
88 * Because we keep PREEMPT_NEED_RESCHED set when we do _not_ need to reschedule
89 * a decrement which hits zero means we have no preempt_count and should
90 * reschedule.
91 */
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +020092static __always_inline bool __preempt_count_dec_and_test(void)
93{
Peter Zijlstra288e4522018-10-03 12:34:10 +020094 return GEN_UNARY_RMWcc("decl", __preempt_count, e, __percpu_arg([var]));
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +020095}
96
97/*
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +020098 * Returns true when we need to resched and can (barring IRQ state).
99 */
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +0300100static __always_inline bool should_resched(int preempt_offset)
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +0200101{
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +0300102 return unlikely(raw_cpu_read_4(__preempt_count) == preempt_offset);
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +0200103}
104
Peter Zijlstra1a338ac2013-08-14 14:51:00 +0200105#ifdef CONFIG_PREEMPT
106 extern asmlinkage void ___preempt_schedule(void);
Josh Poimboeuff5caf622017-09-20 16:24:33 -0500107# define __preempt_schedule() \
108 asm volatile ("call ___preempt_schedule" : ASM_CALL_CONSTRAINT)
Josh Poimboeuf821eae72016-02-18 11:41:58 -0600109
Peter Zijlstra1a338ac2013-08-14 14:51:00 +0200110 extern asmlinkage void preempt_schedule(void);
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +0200111 extern asmlinkage void ___preempt_schedule_notrace(void);
Josh Poimboeuff5caf622017-09-20 16:24:33 -0500112# define __preempt_schedule_notrace() \
113 asm volatile ("call ___preempt_schedule_notrace" : ASM_CALL_CONSTRAINT)
114
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +0200115 extern asmlinkage void preempt_schedule_notrace(void);
Peter Zijlstra1a338ac2013-08-14 14:51:00 +0200116#endif
117
Peter Zijlstrac2daa3b2013-08-14 14:51:00 +0200118#endif /* __ASM_PREEMPT_H */