xref: /dragonfly/sys/dev/drm/include/linux/workqueue.h (revision e0a1e7ab)
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-2017 François Tigeot <ftigeot@wolfpond.org>
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 <sys/proc.h>
36 #include <linux/types.h>
37 #include <linux/kernel.h>
38 #include <linux/timer.h>
39 #include <linux/lockdep.h>
40 
41 #include <sys/taskqueue.h>
42 
43 struct workqueue_struct {
44 	struct taskqueue	*taskqueue;
45 };
46 
47 struct work_struct {
48 	struct	task 		work_task;
49 	struct	taskqueue	*taskqueue;
50 	void			(*fn)(struct work_struct *);
51 };
52 
53 struct delayed_work {
54 	struct work_struct	work;
55 	struct callout		timer;
56 	struct lwkt_token	token;
57 };
58 
59 static inline struct delayed_work *
60 to_delayed_work(struct work_struct *work)
61 {
62 
63 	return container_of(work, struct delayed_work, work);
64 }
65 
66 
67 static inline void
68 _work_fn(void *context, int pending)
69 {
70 	struct work_struct *work;
71 
72 	work = context;
73 	work->fn(work);
74 }
75 
76 #define	INIT_WORK(work, func) 	 					\
77 do {									\
78 	(work)->fn = (func);						\
79 	(work)->taskqueue = NULL;					\
80 	TASK_INIT(&(work)->work_task, 0, _work_fn, (work));		\
81 } while (0)
82 
83 #define INIT_WORK_ONSTACK(work, func)	INIT_WORK(work, func)
84 
85 #define INIT_DELAYED_WORK(_work, func)					\
86 do {									\
87 	INIT_WORK(&(_work)->work, func);				\
88 	lwkt_token_init(&(_work)->token, "workqueue token");		\
89 	callout_init_mp(&(_work)->timer);				\
90 } while (0)
91 
92 #define	INIT_DEFERRABLE_WORK	INIT_DELAYED_WORK
93 
94 #define	schedule_work(work)						\
95 do {									\
96 	(work)->taskqueue = taskqueue_thread[mycpuid];				\
97 	taskqueue_enqueue(taskqueue_thread[mycpuid], &(work)->work_task);	\
98 } while (0)
99 
100 #define	flush_scheduled_work()	flush_taskqueue(taskqueue_thread[mycpuid])
101 
102 static inline int queue_work(struct workqueue_struct *q, struct work_struct *work)
103 {
104 	(work)->taskqueue = (q)->taskqueue;
105 	/* Return opposite val to align with Linux logic */
106 	return !taskqueue_enqueue((q)->taskqueue, &(work)->work_task);
107 }
108 
109 static inline void
110 _delayed_work_fn(void *arg)
111 {
112 	struct delayed_work *work;
113 
114 	work = arg;
115 	taskqueue_enqueue(work->work.taskqueue, &work->work.work_task);
116 }
117 
118 static inline int
119 queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *work,
120     unsigned long delay)
121 {
122 	int pending;
123 
124 	pending = work->work.work_task.ta_pending;
125 	work->work.taskqueue = wq->taskqueue;
126 	if (delay != 0) {
127 		lwkt_gettoken(&work->token);
128 		callout_reset(&work->timer, delay, _delayed_work_fn, work);
129 		lwkt_reltoken(&work->token);
130 	} else {
131 		_delayed_work_fn((void *)work);
132 	}
133 
134 	return (!pending);
135 }
136 
137 static inline bool schedule_delayed_work(struct delayed_work *dwork,
138                                          unsigned long delay)
139 {
140         struct workqueue_struct wq;
141         wq.taskqueue = taskqueue_thread[mycpuid];
142         return queue_delayed_work(&wq, dwork, delay);
143 }
144 
145 static inline struct workqueue_struct *
146 _create_workqueue_common(char *name, int cpus)
147 {
148 	struct workqueue_struct *wq;
149 
150 	wq = kmalloc(sizeof(*wq), M_DRM, M_WAITOK);
151 	wq->taskqueue = taskqueue_create((name), M_WAITOK,
152 	    taskqueue_thread_enqueue,  &wq->taskqueue);
153 	taskqueue_start_threads(&wq->taskqueue, cpus,
154 				TDPRI_KERN_DAEMON, -1, "%s", name);
155 
156 	return (wq);
157 }
158 
159 
160 #define	create_singlethread_workqueue(name)				\
161 	_create_workqueue_common(name, 1)
162 
163 #define	create_workqueue(name)						\
164 	_create_workqueue_common(name, MAXCPU)
165 
166 #define alloc_ordered_workqueue(name, flags)				\
167 	_create_workqueue_common(name, 1)
168 
169 #define alloc_workqueue(name, flags, max_active)			\
170 	_create_workqueue_common(name, max_active)
171 
172 static inline void
173 destroy_workqueue(struct workqueue_struct *wq)
174 {
175 	taskqueue_free(wq->taskqueue);
176 	kfree(wq);
177 }
178 
179 #define	flush_workqueue(wq)	flush_taskqueue((wq)->taskqueue)
180 
181 static inline void
182 _flush_fn(void *context, int pending)
183 {
184 }
185 
186 static inline void
187 flush_taskqueue(struct taskqueue *tq)
188 {
189 	struct task flushtask;
190 
191 	PHOLD(curproc);
192 	TASK_INIT(&flushtask, 0, _flush_fn, NULL);
193 	taskqueue_enqueue(tq, &flushtask);
194 	taskqueue_drain(tq, &flushtask);
195 	PRELE(curproc);
196 }
197 
198 static inline int
199 cancel_work_sync(struct work_struct *work)
200 {
201 	if (work->taskqueue &&
202 	    taskqueue_cancel(work->taskqueue, &work->work_task, NULL))
203 		taskqueue_drain(work->taskqueue, &work->work_task);
204 	return 0;
205 }
206 
207 /*
208  * This may leave work running on another CPU as it does on Linux.
209  */
210 static inline int
211 cancel_delayed_work(struct delayed_work *work)
212 {
213 
214 	lwkt_gettoken(&work->token);
215 	callout_stop(&work->timer);
216 	lwkt_reltoken(&work->token);
217 	if (work->work.taskqueue)
218 		return (taskqueue_cancel(work->work.taskqueue,
219 		    &work->work.work_task, NULL) == 0);
220 	return 0;
221 }
222 
223 static inline int
224 cancel_delayed_work_sync(struct delayed_work *work)
225 {
226 
227 	lwkt_gettoken(&work->token);
228 	callout_drain(&work->timer);
229 	lwkt_reltoken(&work->token);
230 	if (work->work.taskqueue &&
231 	    taskqueue_cancel(work->work.taskqueue, &work->work.work_task, NULL))
232 		taskqueue_drain(work->work.taskqueue, &work->work.work_task);
233 	return 0;
234 }
235 
236 static inline bool
237 mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork,
238 		                      unsigned long delay)
239 {
240 	cancel_delayed_work(dwork);
241 	queue_delayed_work(wq, dwork, delay);
242 	return false;
243 }
244 
245 static inline bool
246 flush_work(struct work_struct *work)
247 {
248 	if (work->taskqueue != NULL)
249 		taskqueue_drain(work->taskqueue, &work->work_task);
250 	return true;
251 }
252 
253 static inline void
254 destroy_work_on_stack(struct work_struct *work)
255 {
256 }
257 
258 /* System-wide workqueues */
259 extern struct workqueue_struct *system_wq;
260 extern struct workqueue_struct *system_long_wq;
261 extern struct workqueue_struct *system_power_efficient_wq;
262 extern struct workqueue_struct *system_unbound_wq;
263 
264 static inline unsigned int
265 work_busy(struct work_struct *work)
266 {
267 	/* Just pretend nothing is busy, this function is unreliable anyway */
268 	return 0;
269 }
270 
271 #endif	/* _LINUX_WORKQUEUE_H_ */
272