xref: /dflybsd-src/sys/dev/drm/include/linux/sched.h (revision 6994034513cc2f8ff3d79e00d28295f4dcceaf02)
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 struct seq_file;
66 
67 #define	TASK_RUNNING		0
68 #define	TASK_INTERRUPTIBLE	1
69 #define	TASK_UNINTERRUPTIBLE	2
70 
71 #define TASK_NORMAL		(TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE)
72 
73 #define MAX_SCHEDULE_TIMEOUT    LONG_MAX
74 
75 /*
76  * schedule_timeout: puts the current thread to sleep until timeout
77  * if its state allows it to.
78  */
79 static inline long
80 schedule_timeout(signed long timeout)
81 {
82 	unsigned long time_before, time_after;
83 	long slept, ret = 0;
84 	int timo;
85 
86 	if (timeout < 0) {
87 		kprintf("schedule_timeout(): timeout cannot be negative\n");
88 		goto done;
89 	}
90 
91 	/*
92 	 * Indefinite wait if timeout is MAX_SCHEDULE_TIMEOUT, but we are
93 	 * also translating to an integer.  The first conditional will
94 	 * cover both but to code defensively test both.
95 	 */
96 	if (timeout >= INT_MAX || timeout == MAX_SCHEDULE_TIMEOUT)
97 		timo = 0;
98 	else
99 		timo = timeout;
100 
101 	switch (current->state) {
102 	case TASK_INTERRUPTIBLE:
103 		time_before = ticks;
104 		tsleep(current, PCATCH, "lstim", timo);
105 		time_after = ticks;
106 		slept = time_after - time_before;
107 		ret = timeout - slept;
108 		if (ret < 0)
109 			ret = 0;
110 		break;
111 	case TASK_UNINTERRUPTIBLE:
112 		tsleep(current, 0, "lstim", timo);
113 		break;
114 	default:
115 		/* We are supposed to return immediately here */
116 		tsleep(current, 0, "lstim", 1);
117 		break;
118 	}
119 
120 done:
121 	if (timeout == MAX_SCHEDULE_TIMEOUT)
122 		ret = MAX_SCHEDULE_TIMEOUT;
123 
124 	current->state = TASK_RUNNING;
125 	return ret;
126 }
127 
128 static inline long
129 io_schedule_timeout(signed long timeout)
130 {
131 	return schedule_timeout(timeout);
132 }
133 
134 #define TASK_COMM_LEN	MAXCOMLEN
135 
136 /*
137  * local_clock: fast time source, monotonic on the same cpu
138  */
139 static inline uint64_t
140 local_clock(void)
141 {
142 	struct timespec ts;
143 
144 	getnanouptime(&ts);
145 	return (ts.tv_sec * NSEC_PER_SEC) + ts.tv_nsec;
146 }
147 
148 static inline void
149 yield(void)
150 {
151 	lwkt_yield();
152 }
153 
154 #define __set_current_state(state_value)	current->state = (state_value);
155 
156 #define set_current_state(state_value)		\
157 do {						\
158 	__set_current_state(state_value);	\
159 	mb();					\
160 } while (0)
161 
162 static inline int
163 wake_up_process(struct task_struct *tsk)
164 {
165 	/* Among other things, this function is supposed to act as
166 	 * a barrier */
167 	smp_wmb();
168 	wakeup(tsk);
169 
170 	return 1;	/* Always indicate the process was woken up */
171 }
172 
173 static inline int
174 signal_pending(struct task_struct *p)
175 {
176 	struct thread *t = p->dfly_td;
177 
178 	/* Some kernel threads do not have lwp, t->td_lwp can be NULL */
179 	if (t->td_lwp == NULL)
180 		return 0;
181 
182 	return CURSIG(t->td_lwp);
183 }
184 
185 static inline int
186 fatal_signal_pending(struct task_struct *p)
187 {
188 	struct thread *t = p->dfly_td;
189 	sigset_t pending_set;
190 
191 	/* Some kernel threads do not have lwp, t->td_lwp can be NULL */
192 	if (t->td_lwp == NULL)
193 		return 0;
194 
195 	pending_set = lwp_sigpend(t->td_lwp);
196 	return SIGISMEMBER(pending_set, SIGKILL);
197 }
198 
199 static inline int
200 signal_pending_state(long state, struct task_struct *p)
201 {
202 	if (state & TASK_INTERRUPTIBLE)
203 		return (signal_pending(p));
204 	else
205 		return (fatal_signal_pending(p));
206 }
207 
208 /* Explicit rescheduling in order to reduce latency */
209 static inline int
210 cond_resched(void)
211 {
212 	lwkt_yield();
213 	return 0;
214 }
215 
216 static inline int
217 send_sig(int sig, struct proc *p, int priv)
218 {
219 	ksignal(p, sig);
220 	return 0;
221 }
222 
223 static inline void
224 set_need_resched(void)
225 {
226 	/* do nothing for now */
227 	/* used on ttm_bo_reserve failures */
228 }
229 
230 #endif	/* _LINUX_SCHED_H_ */
231