xref: /dragonfly/sys/sys/gtaskqueue.h (revision fab26bfaa6e498f885f4703c02c06f4d651dd445)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2014 Jeffrey Roberson <jeff@freebsd.org>
5  * Copyright (c) 2016 Matthew Macy <mmacy@mattmacy.io>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef _SYS_GTASKQUEUE_H_
33 #define _SYS_GTASKQUEUE_H_
34 
35 #ifndef _KERNEL
36 #error "no user-serviceable parts inside"
37 #endif
38 
39 #include <sys/bus.h>
40 #include <sys/types.h>
41 #include <sys/taskqueue.h>
42 
43 struct gtaskqueue;
44 struct taskqgroup;
45 
46 #define TASK_ENQUEUED                   0x1
47 #define TASK_NOENQUEUE                  0x2
48 #define TASK_NETWORK                    0x4
49 
50 #define TASK_IS_NET(ta)                 ((ta)->ta_flags & TASK_NETWORK)
51 
52 typedef void gtask_fn_t(void *context);
53 
54 struct gtask {
55           STAILQ_ENTRY(gtask) ta_link;  /* (q) link for queue */
56           uint16_t ta_flags;            /* (q) state flags */
57           u_short ta_priority;                    /* (c) Priority */
58           gtask_fn_t *ta_func;                    /* (c) task handler */
59           void    *ta_context;                    /* (c) argument for handler */
60 };
61 
62 #define   GROUPTASK_NAMELEN   32
63 
64 /*
65  * Taskqueue groups.  Manages dynamic thread groups and irq binding for
66  * device and other tasks.
67  */
68 struct grouptask {
69           struct gtask                  gt_task;
70           void                          *gt_taskqueue;
71           LIST_ENTRY(grouptask)         gt_list;
72           void                          *gt_uniq;
73           char                          gt_name[GROUPTASK_NAMELEN];
74           device_t            gt_dev;
75           struct resource               *gt_irq;
76           int                           gt_cpu;
77 };
78 
79 void      gtaskqueue_block(struct gtaskqueue *queue);
80 void      gtaskqueue_unblock(struct gtaskqueue *queue);
81 
82 int       gtaskqueue_cancel(struct gtaskqueue *queue, struct gtask *gtask);
83 void      gtaskqueue_drain(struct gtaskqueue *queue, struct gtask *task);
84 void      gtaskqueue_drain_all(struct gtaskqueue *queue);
85 
86 void      grouptask_block(struct grouptask *grouptask);
87 void      grouptask_unblock(struct grouptask *grouptask);
88 int       grouptaskqueue_enqueue(struct gtaskqueue *queue, struct gtask *task);
89 
90 void      taskqgroup_attach(struct taskqgroup *qgroup, struct grouptask *grptask,
91               void *uniq, device_t dev, struct resource *irq, const char *name);
92 int       taskqgroup_attach_cpu(struct taskqgroup *qgroup,
93               struct grouptask *grptask, void *uniq, int cpu, device_t dev,
94               struct resource *irq, const char *name);
95 void      taskqgroup_detach(struct taskqgroup *qgroup, struct grouptask *gtask);
96 struct taskqgroup *taskqgroup_create(const char *name, int cnt, int stride);
97 void      taskqgroup_destroy(struct taskqgroup *qgroup);
98 void      taskqgroup_bind(struct taskqgroup *qgroup);
99 void      taskqgroup_drain_all(struct taskqgroup *qgroup);
100 
101 #define   GTASK_INIT(gtask, flags, priority, func, context) do {      \
102           (gtask)->ta_flags = (flags);                                \
103           (gtask)->ta_priority = (priority);                          \
104           (gtask)->ta_func = (func);                                  \
105           (gtask)->ta_context = (context);                            \
106 } while (0)
107 
108 #define   GROUPTASK_INIT(gtask, priority, func, context)    \
109           GTASK_INIT(&(gtask)->gt_task, 0, priority, func, context)
110 
111 #define   GROUPTASK_ENQUEUE(gtask)                          \
112           grouptaskqueue_enqueue((gtask)->gt_taskqueue, &(gtask)->gt_task)
113 
114 #define TASKQGROUP_DECLARE(name)                            \
115 extern struct taskqgroup *qgroup_##name
116 
117 #define TASKQGROUP_DEFINE(name, cnt, stride)                                    \
118                                                                                           \
119 struct taskqgroup *qgroup_##name;                                               \
120                                                                                           \
121 static void                                                                               \
122 taskqgroup_define_##name(void *arg)                                             \
123 {                                                                                         \
124           qgroup_##name = taskqgroup_create(#name, (cnt), (stride));  \
125 }                                                                                         \
126 SYSINIT(taskqgroup_##name, SI_BOOT2_TASKQ, SI_ORDER_FIRST,            \
127     taskqgroup_define_##name, NULL);                                            \
128                                                                                           \
129 static void                                                                               \
130 taskqgroup_bind_##name(void *arg)                                               \
131 {                                                                                         \
132           taskqgroup_bind(qgroup_##name);                                                 \
133 }                                                                                         \
134 SYSINIT(taskqgroup_bind_##name, SI_BOOT2_TASKQ, SI_ORDER_ANY,                   \
135     taskqgroup_bind_##name, NULL)
136 
137 TASKQGROUP_DECLARE(softirq);
138 
139 #endif /* !_SYS_GTASKQUEUE_H_ */
140