xref: /dragonfly/sys/dev/drm/include/linux/workqueue.h (revision b29f78b5)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6  * Copyright (c) 2014 François Tigeot
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #ifndef	_LINUX_WORKQUEUE_H_
31 #define	_LINUX_WORKQUEUE_H_
32 
33 #include <sys/types.h>
34 #include <sys/malloc.h>
35 #include <linux/types.h>
36 #include <linux/kernel.h>
37 #include <linux/timer.h>
38 
39 #include <sys/taskqueue.h>
40 
41 struct workqueue_struct {
42 	struct taskqueue	*taskqueue;
43 };
44 
45 struct work_struct {
46 	struct	task 		work_task;
47 	struct	taskqueue	*taskqueue;
48 	void			(*fn)(struct work_struct *);
49 };
50 
51 struct delayed_work {
52 	struct work_struct	work;
53 	struct callout		timer;
54 	struct lwkt_token	token;
55 };
56 
57 static inline struct delayed_work *
58 to_delayed_work(struct work_struct *work)
59 {
60 
61 	return container_of(work, struct delayed_work, work);
62 }
63 
64 
65 static inline void
66 _work_fn(void *context, int pending)
67 {
68 	struct work_struct *work;
69 
70 	work = context;
71 	work->fn(work);
72 }
73 
74 #define	INIT_WORK(work, func) 	 					\
75 do {									\
76 	(work)->fn = (func);						\
77 	(work)->taskqueue = NULL;					\
78 	TASK_INIT(&(work)->work_task, 0, _work_fn, (work));		\
79 } while (0)
80 
81 #define	INIT_DELAYED_WORK(_work, func)					\
82 do {									\
83 	INIT_WORK(&(_work)->work, func);				\
84 	lwkt_token_init(&(_work)->token, "workqueue token");		\
85 	callout_init_mp(&(_work)->timer);				\
86 } while (0)
87 
88 #define	INIT_DEFERRABLE_WORK	INIT_DELAYED_WORK
89 
90 #define	schedule_work(work)						\
91 do {									\
92 	(work)->taskqueue = taskqueue_thread[mycpuid];				\
93 	taskqueue_enqueue(taskqueue_thread[mycpuid], &(work)->work_task);	\
94 } while (0)
95 
96 #define	flush_scheduled_work()	flush_taskqueue(taskqueue_thread[mycpuid])
97 
98 static inline int queue_work(struct workqueue_struct *q, struct work_struct *work)
99 {
100 	(work)->taskqueue = (q)->taskqueue;
101 	/* Return opposite val to align with Linux logic */
102 	return !taskqueue_enqueue((q)->taskqueue, &(work)->work_task);
103 }
104 
105 static inline void
106 _delayed_work_fn(void *arg)
107 {
108 	struct delayed_work *work;
109 
110 	work = arg;
111 	taskqueue_enqueue(work->work.taskqueue, &work->work.work_task);
112 }
113 
114 static inline int
115 queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *work,
116     unsigned long delay)
117 {
118 	int pending;
119 
120 	pending = work->work.work_task.ta_pending;
121 	work->work.taskqueue = wq->taskqueue;
122 	if (delay != 0) {
123 		lwkt_gettoken(&work->token);
124 		callout_reset(&work->timer, delay, _delayed_work_fn, work);
125 		lwkt_reltoken(&work->token);
126 	} else {
127 		_delayed_work_fn((void *)work);
128 	}
129 
130 	return (!pending);
131 }
132 
133 static inline bool schedule_delayed_work(struct delayed_work *dwork,
134                                          unsigned long delay)
135 {
136         struct workqueue_struct wq;
137         wq.taskqueue = taskqueue_thread[mycpuid];
138         return queue_delayed_work(&wq, dwork, delay);
139 }
140 
141 static inline struct workqueue_struct *
142 _create_workqueue_common(char *name, int cpus)
143 {
144 	struct workqueue_struct *wq;
145 
146 	wq = kmalloc(sizeof(*wq), M_DRM, M_WAITOK);
147 	wq->taskqueue = taskqueue_create((name), M_WAITOK,
148 	    taskqueue_thread_enqueue,  &wq->taskqueue);
149 	taskqueue_start_threads(&wq->taskqueue, cpus, 0, -1, "%s", name);
150 
151 	return (wq);
152 }
153 
154 
155 #define	create_singlethread_workqueue(name)				\
156 	_create_workqueue_common(name, 1)
157 
158 #define	create_workqueue(name)						\
159 	_create_workqueue_common(name, MAXCPU)
160 
161 #define alloc_ordered_workqueue(name, flags)				\
162 	_create_workqueue_common(name, 1)
163 
164 static inline void
165 destroy_workqueue(struct workqueue_struct *wq)
166 {
167 	taskqueue_free(wq->taskqueue);
168 	kfree(wq);
169 }
170 
171 #define	flush_workqueue(wq)	flush_taskqueue((wq)->taskqueue)
172 
173 static inline void
174 _flush_fn(void *context, int pending)
175 {
176 }
177 
178 static inline void
179 flush_taskqueue(struct taskqueue *tq)
180 {
181 	struct task flushtask;
182 
183 	PHOLD(curproc);
184 	TASK_INIT(&flushtask, 0, _flush_fn, NULL);
185 	taskqueue_enqueue(tq, &flushtask);
186 	taskqueue_drain(tq, &flushtask);
187 	PRELE(curproc);
188 }
189 
190 static inline int
191 cancel_work_sync(struct work_struct *work)
192 {
193 	if (work->taskqueue &&
194 	    taskqueue_cancel(work->taskqueue, &work->work_task, NULL))
195 		taskqueue_drain(work->taskqueue, &work->work_task);
196 	return 0;
197 }
198 
199 /*
200  * This may leave work running on another CPU as it does on Linux.
201  */
202 static inline int
203 cancel_delayed_work(struct delayed_work *work)
204 {
205 
206 	lwkt_gettoken(&work->token);
207 	callout_stop(&work->timer);
208 	lwkt_reltoken(&work->token);
209 	if (work->work.taskqueue)
210 		return (taskqueue_cancel(work->work.taskqueue,
211 		    &work->work.work_task, NULL) == 0);
212 	return 0;
213 }
214 
215 static inline int
216 cancel_delayed_work_sync(struct delayed_work *work)
217 {
218 
219 	lwkt_gettoken(&work->token);
220 	callout_drain(&work->timer);
221 	lwkt_reltoken(&work->token);
222 	if (work->work.taskqueue &&
223 	    taskqueue_cancel(work->work.taskqueue, &work->work.work_task, NULL))
224 		taskqueue_drain(work->work.taskqueue, &work->work.work_task);
225 	return 0;
226 }
227 
228 static inline bool
229 mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
230 		                      unsigned long delay)
231 {
232 	cancel_delayed_work(dwork);
233 	queue_delayed_work(wq, dwork, delay);
234 	return false;
235 }
236 
237 #endif	/* _LINUX_WORKQUEUE_H_ */
238