xref: /dflybsd-src/sys/dev/drm/linux_tasklet.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
177a6b00eSFrançois Tigeot /*
277a6b00eSFrançois Tigeot  * Copyright (c) 2020 François Tigeot <ftigeot@wolfpond.org>
377a6b00eSFrançois Tigeot  * All rights reserved.
477a6b00eSFrançois Tigeot  *
577a6b00eSFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
677a6b00eSFrançois Tigeot  * modification, are permitted provided that the following conditions
777a6b00eSFrançois Tigeot  * are met:
877a6b00eSFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
977a6b00eSFrançois Tigeot  *    notice unmodified, this list of conditions, and the following
1077a6b00eSFrançois Tigeot  *    disclaimer.
1177a6b00eSFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
1277a6b00eSFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
1377a6b00eSFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
1477a6b00eSFrançois Tigeot  *
1577a6b00eSFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1677a6b00eSFrançois Tigeot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1777a6b00eSFrançois Tigeot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1877a6b00eSFrançois Tigeot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1977a6b00eSFrançois Tigeot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2077a6b00eSFrançois Tigeot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2177a6b00eSFrançois Tigeot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2277a6b00eSFrançois Tigeot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2377a6b00eSFrançois Tigeot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2477a6b00eSFrançois Tigeot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2577a6b00eSFrançois Tigeot  */
2677a6b00eSFrançois Tigeot 
2777a6b00eSFrançois Tigeot #include <linux/interrupt.h>
2877a6b00eSFrançois Tigeot #include <linux/slab.h>
2977a6b00eSFrançois Tigeot 
3077a6b00eSFrançois Tigeot #include <sys/kthread.h>
3177a6b00eSFrançois Tigeot 
3277a6b00eSFrançois Tigeot /*
3377a6b00eSFrançois Tigeot  * Linux tasklet constraints:
3477a6b00eSFrançois Tigeot  * - tasklets that have the same type cannot be run on multiple processors at
3577a6b00eSFrançois Tigeot  *   the same time
3677a6b00eSFrançois Tigeot  * - tasklets always run on the processor from which they were originally
3777a6b00eSFrançois Tigeot  *   submitted
3877a6b00eSFrançois Tigeot  * - when a tasklet is scheduled, its state is set to TASKLET_STATE_SCHED,
3977a6b00eSFrançois Tigeot  *   and the tasklet added to a queue
4077a6b00eSFrançois Tigeot  * - during the execution of its function, the tasklet state is set to
4177a6b00eSFrançois Tigeot  *   TASKLET_STATE_RUN and the TASKLET_STATE_SCHED state is removed
4277a6b00eSFrançois Tigeot  */
4377a6b00eSFrançois Tigeot 
4477a6b00eSFrançois Tigeot struct tasklet_entry {
4577a6b00eSFrançois Tigeot 	struct tasklet_struct *ts;
46e071d366SFrançois Tigeot 	STAILQ_ENTRY(tasklet_entry) tasklet_entries;
4777a6b00eSFrançois Tigeot };
4877a6b00eSFrançois Tigeot 
4977a6b00eSFrançois Tigeot static struct lock tasklet_lock = LOCK_INITIALIZER("dltll", 0, LK_CANRECURSE);
5077a6b00eSFrançois Tigeot 
5177a6b00eSFrançois Tigeot static struct thread *tasklet_td = NULL;
52e071d366SFrançois Tigeot STAILQ_HEAD(tasklet_list_head, tasklet_entry) tlist = STAILQ_HEAD_INITIALIZER(tlist);
53e071d366SFrançois Tigeot STAILQ_HEAD(tasklet_hi_list_head, tasklet_entry) tlist_hi = STAILQ_HEAD_INITIALIZER(tlist_hi);
5477a6b00eSFrançois Tigeot 
5577a6b00eSFrançois Tigeot static int tasklet_pending = 0;
5677a6b00eSFrançois Tigeot 
57*3f2dd94aSFrançois Tigeot /*
58*3f2dd94aSFrançois Tigeot  * Linux does:
59*3f2dd94aSFrançois Tigeot  * 1 - copy list locally
60*3f2dd94aSFrançois Tigeot  * 2 - empty global list
61*3f2dd94aSFrançois Tigeot  * 3 - process local list from head to tail
62*3f2dd94aSFrançois Tigeot  *****
63*3f2dd94aSFrançois Tigeot  * local list processing:
64*3f2dd94aSFrançois Tigeot  * - if element cannot be run, put it at the tail
65*3f2dd94aSFrançois Tigeot  * - last element == null
66*3f2dd94aSFrançois Tigeot  */
6777a6b00eSFrançois Tigeot #define PROCESS_TASKLET_LIST(which_list) do { \
68e071d366SFrançois Tigeot 	STAILQ_FOREACH_MUTABLE(te, &which_list, tasklet_entries, tmp_te) { \
6977a6b00eSFrançois Tigeot 		struct tasklet_struct *t = te->ts;			\
7077a6b00eSFrançois Tigeot 									\
7177a6b00eSFrançois Tigeot 		/*							\
7277a6b00eSFrançois Tigeot 		   This tasklet is dying, remove it from the list.	\
7377a6b00eSFrançois Tigeot 		   We allow to it to run one last time if it has	\
7477a6b00eSFrançois Tigeot 		   already been scheduled.				\
7577a6b00eSFrançois Tigeot 		*/							\
7677a6b00eSFrançois Tigeot 		if (test_bit(TASKLET_IS_DYING, &t->state)) {		\
77e071d366SFrançois Tigeot 			STAILQ_REMOVE(&which_list, te, tasklet_entry, tasklet_entries); \
7877a6b00eSFrançois Tigeot 			kfree(te);					\
7977a6b00eSFrançois Tigeot 		}							\
8077a6b00eSFrançois Tigeot 									\
81802fda02SFrançois Tigeot 		/* This tasklet is not enabled, try the next one */	\
82802fda02SFrançois Tigeot 		if (atomic_read(&t->count) != 0)			\
83802fda02SFrançois Tigeot 			continue;					\
84802fda02SFrançois Tigeot 									\
8577a6b00eSFrançois Tigeot 		/* This tasklet is not scheduled, try the next one */	\
8677a6b00eSFrançois Tigeot 		if (!test_bit(TASKLET_STATE_SCHED, &t->state))		\
8777a6b00eSFrançois Tigeot 			continue;					\
8877a6b00eSFrançois Tigeot 									\
8977a6b00eSFrançois Tigeot 		clear_bit(TASKLET_STATE_SCHED, &t->state);		\
9077a6b00eSFrançois Tigeot 		set_bit(TASKLET_STATE_RUN, &t->state);			\
9177a6b00eSFrançois Tigeot 									\
9277a6b00eSFrançois Tigeot 		lockmgr(&tasklet_lock, LK_RELEASE);			\
9377a6b00eSFrançois Tigeot 		if (t->func)						\
9477a6b00eSFrançois Tigeot 			t->func(t->data);				\
9577a6b00eSFrançois Tigeot 		lockmgr(&tasklet_lock, LK_EXCLUSIVE);			\
9677a6b00eSFrançois Tigeot 									\
9777a6b00eSFrançois Tigeot 		clear_bit(TASKLET_STATE_RUN, &t->state);		\
9877a6b00eSFrançois Tigeot 	}								\
9977a6b00eSFrançois Tigeot } while (0)
10077a6b00eSFrançois Tigeot 
10177a6b00eSFrançois Tigeot /* XXX runners should be CPU-specific */
10277a6b00eSFrançois Tigeot static void
tasklet_runner(void * arg)10377a6b00eSFrançois Tigeot tasklet_runner(void *arg)
10477a6b00eSFrançois Tigeot {
10577a6b00eSFrançois Tigeot 	struct tasklet_entry *te, *tmp_te;
10677a6b00eSFrançois Tigeot 
10777a6b00eSFrançois Tigeot 	lockmgr(&tasklet_lock, LK_EXCLUSIVE);
10877a6b00eSFrançois Tigeot 	while (1) {
10977a6b00eSFrançois Tigeot 		/*
11077a6b00eSFrançois Tigeot 		   Only sleep if we haven't been raced by a _schedule()
11177a6b00eSFrançois Tigeot 		   call during an unlock window
11277a6b00eSFrançois Tigeot 		*/
11377a6b00eSFrançois Tigeot 		if (tasklet_pending == 0) {
11477a6b00eSFrançois Tigeot 			lksleep(&tasklet_runner, &tasklet_lock, 0, "tkidle", 0);
11577a6b00eSFrançois Tigeot 		}
11677a6b00eSFrançois Tigeot 		tasklet_pending = 0;
11777a6b00eSFrançois Tigeot 
11877a6b00eSFrançois Tigeot 		/* Process hi tasklets first */
11977a6b00eSFrançois Tigeot 		PROCESS_TASKLET_LIST(tlist_hi);
12077a6b00eSFrançois Tigeot 		PROCESS_TASKLET_LIST(tlist);
12177a6b00eSFrançois Tigeot 	}
12277a6b00eSFrançois Tigeot 	lockmgr(&tasklet_lock, LK_RELEASE);
12377a6b00eSFrançois Tigeot }
12477a6b00eSFrançois Tigeot 
12577a6b00eSFrançois Tigeot void
tasklet_init(struct tasklet_struct * t,void (* func)(unsigned long),unsigned long data)12677a6b00eSFrançois Tigeot tasklet_init(struct tasklet_struct *t,
12777a6b00eSFrançois Tigeot 	     void (*func)(unsigned long), unsigned long data)
12877a6b00eSFrançois Tigeot {
12977a6b00eSFrançois Tigeot 	t->state = 0;
13077a6b00eSFrançois Tigeot 	t->func = func;
13177a6b00eSFrançois Tigeot 	t->data = data;
132802fda02SFrançois Tigeot 	atomic_set(&t->count, 0);
13377a6b00eSFrançois Tigeot }
13477a6b00eSFrançois Tigeot 
13565119432SFrançois Tigeot #define TASKLET_SCHEDULE_COMMON(t, list) do {			\
13665119432SFrançois Tigeot 	struct tasklet_entry *te;				\
13765119432SFrançois Tigeot 								\
13865119432SFrançois Tigeot 	lockmgr(&tasklet_lock, LK_EXCLUSIVE);			\
13965119432SFrançois Tigeot 	set_bit(TASKLET_STATE_SCHED, &t->state);		\
14065119432SFrançois Tigeot 								\
141e071d366SFrançois Tigeot 	STAILQ_FOREACH(te, &(list), tasklet_entries) {		\
14265119432SFrançois Tigeot 		if (te->ts == t)				\
14365119432SFrançois Tigeot 			goto found_and_done;			\
14465119432SFrançois Tigeot 	}							\
14565119432SFrançois Tigeot 								\
14665119432SFrançois Tigeot 	te = kzalloc(sizeof(struct tasklet_entry), M_WAITOK);	\
14765119432SFrançois Tigeot 	te->ts = t;						\
148e071d366SFrançois Tigeot 	STAILQ_INSERT_TAIL(&(list), te, tasklet_entries);	\
14965119432SFrançois Tigeot 								\
15065119432SFrançois Tigeot found_and_done:							\
15165119432SFrançois Tigeot 	tasklet_pending = 1;					\
15265119432SFrançois Tigeot 	wakeup(&tasklet_runner);				\
15365119432SFrançois Tigeot 	lockmgr(&tasklet_lock, LK_RELEASE);			\
15465119432SFrançois Tigeot } while (0)
15565119432SFrançois Tigeot 
15677a6b00eSFrançois Tigeot void
tasklet_schedule(struct tasklet_struct * t)15777a6b00eSFrançois Tigeot tasklet_schedule(struct tasklet_struct *t)
15877a6b00eSFrançois Tigeot {
15965119432SFrançois Tigeot 	TASKLET_SCHEDULE_COMMON(t, tlist);
16077a6b00eSFrançois Tigeot }
16177a6b00eSFrançois Tigeot 
16277a6b00eSFrançois Tigeot void
tasklet_hi_schedule(struct tasklet_struct * t)16377a6b00eSFrançois Tigeot tasklet_hi_schedule(struct tasklet_struct *t)
16477a6b00eSFrançois Tigeot {
16565119432SFrançois Tigeot 	TASKLET_SCHEDULE_COMMON(t, tlist_hi);
16677a6b00eSFrançois Tigeot }
16777a6b00eSFrançois Tigeot 
16877a6b00eSFrançois Tigeot void
tasklet_kill(struct tasklet_struct * t)16977a6b00eSFrançois Tigeot tasklet_kill(struct tasklet_struct *t)
17077a6b00eSFrançois Tigeot {
17177a6b00eSFrançois Tigeot 	set_bit(TASKLET_IS_DYING, &t->state);
17277a6b00eSFrançois Tigeot 	wakeup(&tasklet_runner);
17377a6b00eSFrançois Tigeot }
17477a6b00eSFrançois Tigeot 
init_tasklets(void * arg)17577a6b00eSFrançois Tigeot static int init_tasklets(void *arg)
17677a6b00eSFrançois Tigeot {
17777a6b00eSFrançois Tigeot 	kthread_create(tasklet_runner, NULL, &tasklet_td, "tasklet_runner");
17877a6b00eSFrançois Tigeot 
17977a6b00eSFrançois Tigeot 	return 0;
18077a6b00eSFrançois Tigeot }
18177a6b00eSFrançois Tigeot 
18277a6b00eSFrançois Tigeot SYSINIT(linux_tasklet_init, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, init_tasklets, NULL);
183