drm/linux: Port kfifo.h to DragonFly BSD
[dragonfly.git] / sys / dev / drm / include / linux / sched.h
1 /*
2  * Copyright (c) 2015-2019 François Tigeot <ftigeot@wolfpond.org>
3  * Copyright (c) 2019 Matthew Dillon <dillon@backplane.com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #ifndef _LINUX_SCHED_H_
29 #define _LINUX_SCHED_H_
30
31 #include <linux/capability.h>
32 #include <linux/threads.h>
33 #include <linux/kernel.h>
34 #include <linux/types.h>
35 #include <linux/jiffies.h>
36 #include <linux/rbtree.h>
37 #include <linux/cpumask.h>
38 #include <linux/errno.h>
39 #include <linux/mm_types.h>
40 #include <linux/preempt.h>
41
42 #include <asm/page.h>
43
44 #include <linux/compiler.h>
45 #include <linux/completion.h>
46 #include <linux/pid.h>
47 #include <linux/rcupdate.h>
48 #include <linux/rculist.h>
49
50 #include <linux/time.h>
51 #include <linux/timer.h>
52 #include <linux/hrtimer.h>
53 #include <linux/gfp.h>
54
55 #include <asm/processor.h>
56
57 #include <linux/spinlock.h>
58
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/proc.h>
62 #include <sys/sched.h>
63 #include <sys/signal2.h>
64
65 #define TASK_RUNNING            0
66 #define TASK_INTERRUPTIBLE      1
67 #define TASK_UNINTERRUPTIBLE    2
68
69 #define TASK_NORMAL             (TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
70
71 #define MAX_SCHEDULE_TIMEOUT    LONG_MAX
72
73 /*
74  * schedule_timeout: puts the current thread to sleep until timeout
75  * if its state allows it to.
76  */
77 static inline long
78 schedule_timeout(signed long timeout)
79 {
80         unsigned long time_before, time_after;
81         long slept, ret = 0;
82         int timo;
83
84         if (timeout < 0) {
85                 kprintf("schedule_timeout(): timeout cannot be negative\n");
86                 goto done;
87         }
88
89         /*
90          * Indefinite wait if timeout is MAX_SCHEDULE_TIMEOUT, but we are
91          * also translating to an integer.  The first conditional will
92          * cover both but to code defensively test both.
93          */
94         if (timeout >= INT_MAX || timeout == MAX_SCHEDULE_TIMEOUT)
95                 timo = 0;
96         else
97                 timo = timeout;
98
99         switch (current->state) {
100         case TASK_INTERRUPTIBLE:
101                 time_before = ticks;
102                 tsleep(current, PCATCH, "lstim", timo);
103                 time_after = ticks;
104                 slept = time_after - time_before;
105                 ret = timeout - slept;
106                 if (ret < 0)
107                         ret = 0;
108                 break;
109         case TASK_UNINTERRUPTIBLE:
110                 tsleep(current, 0, "lstim", timo);
111                 break;
112         default:
113                 /* We are supposed to return immediately here */
114                 tsleep(current, 0, "lstim", 1);
115                 break;
116         }
117
118 done:
119         if (timeout == MAX_SCHEDULE_TIMEOUT)
120                 ret = MAX_SCHEDULE_TIMEOUT;
121
122         current->state = TASK_RUNNING;
123         return ret;
124 }
125
126 static inline long
127 io_schedule_timeout(signed long timeout)
128 {
129         return schedule_timeout(timeout);
130 }
131
132 #define TASK_COMM_LEN   MAXCOMLEN
133
134 /*
135  * local_clock: fast time source, monotonic on the same cpu
136  */
137 static inline uint64_t
138 local_clock(void)
139 {
140         struct timespec ts;
141
142         getnanouptime(&ts);
143         return (ts.tv_sec * NSEC_PER_SEC) + ts.tv_nsec;
144 }
145
146 static inline void
147 yield(void)
148 {
149         lwkt_yield();
150 }
151
152 #define __set_current_state(state_value)        current->state = (state_value);
153
154 #define set_current_state(state_value)          \
155 do {                                            \
156         __set_current_state(state_value);       \
157         mb();                                   \
158 } while (0)
159
160 static inline int
161 wake_up_process(struct task_struct *tsk)
162 {
163         /* Among other things, this function is supposed to act as
164          * a barrier */
165         smp_wmb();
166         wakeup(tsk);
167
168         return 1;       /* Always indicate the process was woken up */
169 }
170
171 static inline int
172 signal_pending(struct task_struct *p)
173 {
174         struct thread *t = container_of(p, struct thread, td_linux_task);
175
176         /* Some kernel threads do not have lwp, t->td_lwp can be NULL */
177         if (t->td_lwp == NULL)
178                 return 0;
179
180         return CURSIG(t->td_lwp);
181 }
182
183 static inline int
184 fatal_signal_pending(struct task_struct *p)
185 {
186         struct thread *t = container_of(p, struct thread, td_linux_task);
187         sigset_t pending_set;
188
189         /* Some kernel threads do not have lwp, t->td_lwp can be NULL */
190         if (t->td_lwp == NULL)
191                 return 0;
192
193         pending_set = lwp_sigpend(t->td_lwp);
194         return SIGISMEMBER(pending_set, SIGKILL);
195 }
196
197 static inline int
198 signal_pending_state(long state, struct task_struct *p)
199 {
200         if (state & TASK_INTERRUPTIBLE)
201                 return (signal_pending(p));
202         else
203                 return (fatal_signal_pending(p));
204 }
205
206 /* Explicit rescheduling in order to reduce latency */
207 static inline int
208 cond_resched(void)
209 {
210         lwkt_yield();
211         return 0;
212 }
213
214 static inline int
215 send_sig(int sig, struct proc *p, int priv)
216 {
217         ksignal(p, sig);
218         return 0;
219 }
220
221 static inline void
222 set_need_resched(void)
223 {
224         /* do nothing for now */
225         /* used on ttm_bo_reserve failures */
226 }
227
228 #endif  /* _LINUX_SCHED_H_ */