summaryrefslogtreecommitdiff
path: root/sys/dev/pci/drm/include/linux/wait.h
blob: 5c8ef223f025355b803cc316022765d4beb3a346 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*	$OpenBSD: wait.h,v 1.1 2019/04/14 10:14:53 jsg Exp $	*/
/*
 * Copyright (c) 2013, 2014, 2015 Mark Kettenis
 * Copyright (c) 2017 Martin Pieuchot
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _LINUX_WAIT_H
#define _LINUX_WAIT_H

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mutex.h>

#include <linux/list.h>
#include <linux/errno.h>
#include <linux/spinlock.h>

struct wait_queue_entry {
	unsigned int flags;
	void *private;
	int (*func)(struct wait_queue_entry *, unsigned, int, void *);
	struct proc *proc;
	struct list_head entry;
};

typedef struct wait_queue_entry wait_queue_entry_t;

extern struct mutex sch_mtx;
extern volatile struct proc *sch_proc;
extern volatile void *sch_ident;
extern int sch_priority;

struct wait_queue_head {
	struct mutex lock;
	unsigned int count;
	struct list_head head;
};
typedef struct wait_queue_head wait_queue_head_t;

static inline void
init_waitqueue_head(wait_queue_head_t *wqh)
{
	mtx_init(&wqh->lock, IPL_TTY);
	wqh->count = 0;
	INIT_LIST_HEAD(&wqh->head);
}

#define __init_waitqueue_head(wq, name, key)	init_waitqueue_head(wq)

int default_wake_function(struct wait_queue_entry *, unsigned int, int, void *);
int autoremove_wake_function(struct wait_queue_entry *, unsigned int, int, void *);

static inline void
init_wait_entry(wait_queue_entry_t *wqe, int flags)
{
	wqe->flags = flags;
	wqe->private = NULL;
	wqe->func = autoremove_wake_function;
	wqe->proc = NULL;
	INIT_LIST_HEAD(&wqe->entry);
}

static inline void
__add_wait_queue(wait_queue_head_t *wqh, wait_queue_entry_t *wqe)
{
	list_add(&wqe->entry, &wqh->head);
}

static inline void
__add_wait_queue_entry_tail(wait_queue_head_t *wqh, wait_queue_entry_t *wqe)
{
	list_add_tail(&wqe->entry, &wqh->head);
}

static inline void
add_wait_queue(wait_queue_head_t *head, wait_queue_entry_t *new)
{
	mtx_enter(&head->lock);
	new->proc = curproc;
	__add_wait_queue(head, new);
	mtx_leave(&head->lock);
}

static inline void
__remove_wait_queue(wait_queue_head_t *wqh, wait_queue_entry_t *wqe)
{
	list_del(&wqe->entry);
}

static inline void
remove_wait_queue(wait_queue_head_t *head, wait_queue_entry_t *old)
{
	mtx_enter(&head->lock);
	__remove_wait_queue(head, old);
	old->proc = NULL;
	mtx_leave(&head->lock);
}

#define __wait_event_intr_timeout(wq, condition, timo, prio)		\
({									\
	long ret = timo;						\
	do {								\
		int deadline, __error;					\
									\
		KASSERT(!cold);						\
									\
		mtx_enter(&sch_mtx);					\
		atomic_inc_int(&(wq).count);				\
		deadline = ticks + ret;					\
		__error = msleep(&wq, &sch_mtx, prio, "drmweti", ret);	\
		ret = deadline - ticks;					\
		atomic_dec_int(&(wq).count);				\
		if (__error == ERESTART || __error == EINTR) {		\
			ret = -ERESTARTSYS;				\
			mtx_leave(&sch_mtx);				\
			break;						\
		}							\
		if (timo && (ret <= 0 || __error == EWOULDBLOCK)) { 	\
			mtx_leave(&sch_mtx);				\
			ret = ((condition)) ? 1 : 0;			\
			break;						\
 		}							\
		mtx_leave(&sch_mtx);					\
	} while (ret > 0 && !(condition));				\
	ret;								\
})

/*
 * Sleep until `condition' gets true.
 */
#define wait_event(wq, condition) 		\
do {						\
	if (!(condition))			\
		__wait_event_intr_timeout(wq, condition, 0, 0); \
} while (0)

#define wait_event_interruptible(wq, condition) 		\
({						\
	int __ret = 0;				\
	if (!(condition))			\
		__ret = __wait_event_intr_timeout(wq, condition, 0, PCATCH); \
	__ret;					\
})

#define wait_event_interruptible_locked(wq, condition) 		\
({						\
	int __ret = 0;				\
	if (!(condition))			\
		__ret = __wait_event_intr_timeout(wq, condition, 0, PCATCH); \
	__ret;					\
})

/*
 * Sleep until `condition' gets true or `timo' expires.
 *
 * Returns 0 if `condition' is still false when `timo' expires or
 * the remaining (>=1) ticks otherwise.
 */
#define wait_event_timeout(wq, condition, timo)	\
({						\
	long __ret = timo;			\
	if (!(condition))			\
		__ret = __wait_event_intr_timeout(wq, condition, timo, 0); \
	__ret;					\
})

/*
 * Sleep until `condition' gets true, `timo' expires or the process
 * receives a signal.
 *
 * Returns -ERESTARTSYS if interrupted by a signal.
 * Returns 0 if `condition' is still false when `timo' expires or
 * the remaining (>=1) ticks otherwise.
 */
#define wait_event_interruptible_timeout(wq, condition, timo) \
({						\
	long __ret = timo;			\
	if (!(condition))			\
		__ret = __wait_event_intr_timeout(wq, condition, timo, PCATCH);\
	__ret;					\
})

static inline void
_wake_up(wait_queue_head_t *wqh LOCK_FL_VARS)
{
	wait_queue_entry_t *wqe;
	wait_queue_entry_t *tmp;
	_mtx_enter(&wqh->lock LOCK_FL_ARGS);
	
	list_for_each_entry_safe(wqe, tmp, &wqh->head, entry) {
		if (wqe->func != NULL)
			wqe->func(wqe, 0, wqe->flags, NULL);
	}
	wakeup(wqh);
	_mtx_leave(&wqh->lock LOCK_FL_ARGS);
}

#define wake_up(wq)			_wake_up(wq LOCK_FILE_LINE)
#define wake_up_all(wq)			_wake_up(wq LOCK_FILE_LINE)

static inline void
wake_up_all_locked(wait_queue_head_t *wqh)
{
	wait_queue_entry_t *wqe;
	wait_queue_entry_t *tmp;

	list_for_each_entry_safe(wqe, tmp, &wqh->head, entry) {
		if (wqe->func != NULL)
			wqe->func(wqe, 0, wqe->flags, NULL);
	}
	wakeup(wqh);
}

#define wake_up_interruptible(wq)	_wake_up(wq LOCK_FILE_LINE)
#define waitqueue_active(wq)		((wq)->count > 0)

#define	DEFINE_WAIT(name)				\
	struct wait_queue_entry name = {		\
		.private = NULL,			\
		.func = autoremove_wake_function,	\
		.entry = LIST_HEAD_INIT((name).entry),	\
	}						
#define	DEFINE_WAIT_FUNC(name, cb)			\
	struct wait_queue_entry name = {		\
		.private = NULL,			\
		.func = cb,				\
		.entry = LIST_HEAD_INIT((name).entry),	\
	}

static inline void
prepare_to_wait(wait_queue_head_t *wqh, wait_queue_entry_t *wqe, int state)
{
	if (wqe->flags == 0) {
		mtx_enter(&sch_mtx);
		wqe->flags = 1;
	}
	MUTEX_ASSERT_LOCKED(&sch_mtx);
	if (list_empty(&wqe->entry))
		__add_wait_queue(wqh, wqe);
	sch_proc = curproc;
	sch_ident = wqe;
	sch_priority = state;
}

static inline void
finish_wait(wait_queue_head_t *wqh, wait_queue_entry_t *wqe)
{
	MUTEX_ASSERT_LOCKED(&sch_mtx);
	sch_ident = NULL;
	if (!list_empty(&wqe->entry))
		list_del_init(&wqe->entry);
	mtx_leave(&sch_mtx);
}

#endif