xref: /freebsd/sys/compat/linuxkpi/common/include/linux/irq_work.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1eda697d2SEmmanuel Vadot /*-
2eda697d2SEmmanuel Vadot  * Copyright (c) 2020 The FreeBSD Foundation
3eda697d2SEmmanuel Vadot  *
4eda697d2SEmmanuel Vadot  * This software was developed by Emmanuel Vadot under sponsorship
5eda697d2SEmmanuel Vadot  * from the FreeBSD Foundation.
6eda697d2SEmmanuel Vadot  *
7eda697d2SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
8eda697d2SEmmanuel Vadot  * modification, are permitted provided that the following conditions
9eda697d2SEmmanuel Vadot  * are met:
10eda697d2SEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
11eda697d2SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
12eda697d2SEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
13eda697d2SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
14eda697d2SEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
15eda697d2SEmmanuel Vadot  *
16eda697d2SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17eda697d2SEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18eda697d2SEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19eda697d2SEmmanuel Vadot  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20eda697d2SEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21eda697d2SEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22eda697d2SEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23eda697d2SEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24eda697d2SEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25eda697d2SEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26eda697d2SEmmanuel Vadot  * SUCH DAMAGE.
27eda697d2SEmmanuel Vadot  */
28eda697d2SEmmanuel Vadot 
29307f78f3SVladimir Kondratyev #ifndef _LINUXKPI_LINUX_IRQ_WORK_H_
30307f78f3SVladimir Kondratyev #define	_LINUXKPI_LINUX_IRQ_WORK_H_
31eda697d2SEmmanuel Vadot 
32ec25b6faSVladimir Kondratyev #include <sys/param.h>
33ec25b6faSVladimir Kondratyev #include <sys/taskqueue.h>
34ec25b6faSVladimir Kondratyev 
35bec4576eSVladimir Kondratyev #include <linux/llist.h>
3671fe907dSVladimir Kondratyev #include <linux/workqueue.h>
3771fe907dSVladimir Kondratyev 
3871fe907dSVladimir Kondratyev #define	LKPI_IRQ_WORK_STD_TQ	system_wq->taskqueue
3971fe907dSVladimir Kondratyev #define	LKPI_IRQ_WORK_FAST_TQ	linux_irq_work_tq
4071fe907dSVladimir Kondratyev 
4171fe907dSVladimir Kondratyev #ifdef LKPI_IRQ_WORK_USE_FAST_TQ
4271fe907dSVladimir Kondratyev #define	LKPI_IRQ_WORK_TQ	LKPI_IRQ_WORK_FAST_TQ
4371fe907dSVladimir Kondratyev #else
4471fe907dSVladimir Kondratyev #define	LKPI_IRQ_WORK_TQ	LKPI_IRQ_WORK_STD_TQ
4571fe907dSVladimir Kondratyev #endif
46bec4576eSVladimir Kondratyev 
47ec25b6faSVladimir Kondratyev struct irq_work;
48ec25b6faSVladimir Kondratyev typedef void (*irq_work_func_t)(struct irq_work *);
49eda697d2SEmmanuel Vadot 
50eda697d2SEmmanuel Vadot struct irq_work {
51ec25b6faSVladimir Kondratyev 	struct task irq_task;
52*f021c5c4SJean-Sébastien Pédron 	union {
5317ee6acaSEmmanuel Vadot 		struct llist_node llnode;
54*f021c5c4SJean-Sébastien Pédron 		struct {
55*f021c5c4SJean-Sébastien Pédron 			struct llist_node llist;
56*f021c5c4SJean-Sébastien Pédron 		} node;
57*f021c5c4SJean-Sébastien Pédron 	};
58ec25b6faSVladimir Kondratyev 	irq_work_func_t func;
59eda697d2SEmmanuel Vadot };
60eda697d2SEmmanuel Vadot 
61ec25b6faSVladimir Kondratyev extern struct taskqueue *linux_irq_work_tq;
62ec25b6faSVladimir Kondratyev 
63ec25b6faSVladimir Kondratyev #define	DEFINE_IRQ_WORK(name, _func)	struct irq_work name = {	\
64ec25b6faSVladimir Kondratyev 	.irq_task = TASK_INITIALIZER(0, linux_irq_work_fn, &(name)),	\
65ec25b6faSVladimir Kondratyev 	.func  = (_func),						\
66ec25b6faSVladimir Kondratyev }
67ec25b6faSVladimir Kondratyev 
68ec25b6faSVladimir Kondratyev void	linux_irq_work_fn(void *, int);
69ec25b6faSVladimir Kondratyev 
70eda697d2SEmmanuel Vadot static inline void
init_irq_work(struct irq_work * irqw,irq_work_func_t func)71ec25b6faSVladimir Kondratyev init_irq_work(struct irq_work *irqw, irq_work_func_t func)
72eda697d2SEmmanuel Vadot {
73ec25b6faSVladimir Kondratyev 	TASK_INIT(&irqw->irq_task, 0, linux_irq_work_fn, irqw);
74ec25b6faSVladimir Kondratyev 	irqw->func = func;
75eda697d2SEmmanuel Vadot }
76eda697d2SEmmanuel Vadot 
772192bc32SEmmanuel Vadot static inline bool
irq_work_queue(struct irq_work * irqw)78eda697d2SEmmanuel Vadot irq_work_queue(struct irq_work *irqw)
79eda697d2SEmmanuel Vadot {
8071fe907dSVladimir Kondratyev 	return (taskqueue_enqueue_flags(LKPI_IRQ_WORK_TQ, &irqw->irq_task,
81fa30bff5SVladimir Kondratyev 	    TASKQUEUE_FAIL_IF_PENDING) == 0);
82eda697d2SEmmanuel Vadot }
83eda697d2SEmmanuel Vadot 
84864b1100SVladimir Kondratyev static inline void
irq_work_sync(struct irq_work * irqw)85864b1100SVladimir Kondratyev irq_work_sync(struct irq_work *irqw)
86864b1100SVladimir Kondratyev {
8771fe907dSVladimir Kondratyev 	taskqueue_drain(LKPI_IRQ_WORK_TQ, &irqw->irq_task);
88864b1100SVladimir Kondratyev }
89864b1100SVladimir Kondratyev 
90307f78f3SVladimir Kondratyev #endif /* _LINUXKPI_LINUX_IRQ_WORK_H_ */
91