xref: /dflybsd-src/sys/dev/drm/linux_tasklet.c (revision 802fda022d020e8bc9ede6244f135ee3984e9592)
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;
4677a6b00eSFrançois Tigeot 	SLIST_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;
5277a6b00eSFrançois Tigeot SLIST_HEAD(tasklet_list_head, tasklet_entry) tlist = SLIST_HEAD_INITIALIZER(tlist);
5377a6b00eSFrançois Tigeot SLIST_HEAD(tasklet_hi_list_head, tasklet_entry) tlist_hi = SLIST_HEAD_INITIALIZER(tlist_hi);
5477a6b00eSFrançois Tigeot 
5577a6b00eSFrançois Tigeot static int tasklet_pending = 0;
5677a6b00eSFrançois Tigeot 
5777a6b00eSFrançois Tigeot #define PROCESS_TASKLET_LIST(which_list) do { \
5877a6b00eSFrançois Tigeot 	SLIST_FOREACH_MUTABLE(te, &which_list, tasklet_entries, tmp_te) { \
5977a6b00eSFrançois Tigeot 		struct tasklet_struct *t = te->ts;			\
6077a6b00eSFrançois Tigeot 									\
6177a6b00eSFrançois Tigeot 		/*							\
6277a6b00eSFrançois Tigeot 		   This tasklet is dying, remove it from the list.	\
6377a6b00eSFrançois Tigeot 		   We allow to it to run one last time if it has	\
6477a6b00eSFrançois Tigeot 		   already been scheduled.				\
6577a6b00eSFrançois Tigeot 		*/							\
6677a6b00eSFrançois Tigeot 		if (test_bit(TASKLET_IS_DYING, &t->state)) {		\
6777a6b00eSFrançois Tigeot 			SLIST_REMOVE(&which_list, te, tasklet_entry, tasklet_entries); \
6877a6b00eSFrançois Tigeot 			kfree(te);					\
6977a6b00eSFrançois Tigeot 		}							\
7077a6b00eSFrançois Tigeot 									\
71*802fda02SFrançois Tigeot 		/* This tasklet is not enabled, try the next one */	\
72*802fda02SFrançois Tigeot 		if (atomic_read(&t->count) != 0)			\
73*802fda02SFrançois Tigeot 			continue;					\
74*802fda02SFrançois Tigeot 									\
7577a6b00eSFrançois Tigeot 		/* This tasklet is not scheduled, try the next one */	\
7677a6b00eSFrançois Tigeot 		if (!test_bit(TASKLET_STATE_SCHED, &t->state))		\
7777a6b00eSFrançois Tigeot 			continue;					\
7877a6b00eSFrançois Tigeot 									\
7977a6b00eSFrançois Tigeot 		clear_bit(TASKLET_STATE_SCHED, &t->state);		\
8077a6b00eSFrançois Tigeot 		set_bit(TASKLET_STATE_RUN, &t->state);			\
8177a6b00eSFrançois Tigeot 									\
8277a6b00eSFrançois Tigeot 		lockmgr(&tasklet_lock, LK_RELEASE);			\
8377a6b00eSFrançois Tigeot 		if (t->func)						\
8477a6b00eSFrançois Tigeot 			t->func(t->data);				\
8577a6b00eSFrançois Tigeot 		lockmgr(&tasklet_lock, LK_EXCLUSIVE);			\
8677a6b00eSFrançois Tigeot 									\
8777a6b00eSFrançois Tigeot 		clear_bit(TASKLET_STATE_RUN, &t->state);		\
8877a6b00eSFrançois Tigeot 	}								\
8977a6b00eSFrançois Tigeot } while (0)
9077a6b00eSFrançois Tigeot 
9177a6b00eSFrançois Tigeot /* XXX runners should be CPU-specific */
9277a6b00eSFrançois Tigeot static void
9377a6b00eSFrançois Tigeot tasklet_runner(void *arg)
9477a6b00eSFrançois Tigeot {
9577a6b00eSFrançois Tigeot 	struct tasklet_entry *te, *tmp_te;
9677a6b00eSFrançois Tigeot 
9777a6b00eSFrançois Tigeot 	lockmgr(&tasklet_lock, LK_EXCLUSIVE);
9877a6b00eSFrançois Tigeot 	while (1) {
9977a6b00eSFrançois Tigeot 		/*
10077a6b00eSFrançois Tigeot 		   Only sleep if we haven't been raced by a _schedule()
10177a6b00eSFrançois Tigeot 		   call during an unlock window
10277a6b00eSFrançois Tigeot 		*/
10377a6b00eSFrançois Tigeot 		if (tasklet_pending == 0) {
10477a6b00eSFrançois Tigeot 			lksleep(&tasklet_runner, &tasklet_lock, 0, "tkidle", 0);
10577a6b00eSFrançois Tigeot 		}
10677a6b00eSFrançois Tigeot 		tasklet_pending = 0;
10777a6b00eSFrançois Tigeot 
10877a6b00eSFrançois Tigeot 		/* Process hi tasklets first */
10977a6b00eSFrançois Tigeot 		PROCESS_TASKLET_LIST(tlist_hi);
11077a6b00eSFrançois Tigeot 		PROCESS_TASKLET_LIST(tlist);
11177a6b00eSFrançois Tigeot 	}
11277a6b00eSFrançois Tigeot 	lockmgr(&tasklet_lock, LK_RELEASE);
11377a6b00eSFrançois Tigeot }
11477a6b00eSFrançois Tigeot 
11577a6b00eSFrançois Tigeot void
11677a6b00eSFrançois Tigeot tasklet_init(struct tasklet_struct *t,
11777a6b00eSFrançois Tigeot 	     void (*func)(unsigned long), unsigned long data)
11877a6b00eSFrançois Tigeot {
11977a6b00eSFrançois Tigeot 	t->state = 0;
12077a6b00eSFrançois Tigeot 	t->func = func;
12177a6b00eSFrançois Tigeot 	t->data = data;
122*802fda02SFrançois Tigeot 	atomic_set(&t->count, 0);
12377a6b00eSFrançois Tigeot }
12477a6b00eSFrançois Tigeot 
12577a6b00eSFrançois Tigeot void
12677a6b00eSFrançois Tigeot tasklet_schedule(struct tasklet_struct *t)
12777a6b00eSFrançois Tigeot {
12877a6b00eSFrançois Tigeot 	struct tasklet_entry *te;
12977a6b00eSFrançois Tigeot 
13077a6b00eSFrançois Tigeot 	lockmgr(&tasklet_lock, LK_EXCLUSIVE);
13177a6b00eSFrançois Tigeot 	set_bit(TASKLET_STATE_SCHED, &t->state);
13277a6b00eSFrançois Tigeot 
13377a6b00eSFrançois Tigeot 	SLIST_FOREACH(te, &tlist, tasklet_entries) {
13477a6b00eSFrançois Tigeot 		if (te->ts == t)
13577a6b00eSFrançois Tigeot 			goto found_and_done;
13677a6b00eSFrançois Tigeot 	}
13777a6b00eSFrançois Tigeot 
13877a6b00eSFrançois Tigeot 	te = kzalloc(sizeof(struct tasklet_entry), M_WAITOK);
13977a6b00eSFrançois Tigeot 	te->ts = t;
14077a6b00eSFrançois Tigeot 	SLIST_INSERT_HEAD(&tlist, te, tasklet_entries);
14177a6b00eSFrançois Tigeot 
14277a6b00eSFrançois Tigeot found_and_done:
14377a6b00eSFrançois Tigeot 	/* schedule the runner thread on the local cpu core */
14477a6b00eSFrançois Tigeot 	tasklet_pending = 1;
14577a6b00eSFrançois Tigeot 	wakeup(&tasklet_runner);
14677a6b00eSFrançois Tigeot 	lockmgr(&tasklet_lock, LK_RELEASE);
14777a6b00eSFrançois Tigeot }
14877a6b00eSFrançois Tigeot 
14977a6b00eSFrançois Tigeot void
15077a6b00eSFrançois Tigeot tasklet_hi_schedule(struct tasklet_struct *t)
15177a6b00eSFrançois Tigeot {
15277a6b00eSFrançois Tigeot 	struct tasklet_entry *te;
15377a6b00eSFrançois Tigeot 
15477a6b00eSFrançois Tigeot 	lockmgr(&tasklet_lock, LK_EXCLUSIVE);
15577a6b00eSFrançois Tigeot 	set_bit(TASKLET_STATE_SCHED, &t->state);
15677a6b00eSFrançois Tigeot 
15777a6b00eSFrançois Tigeot 	SLIST_FOREACH(te, &tlist_hi, tasklet_entries) {
15877a6b00eSFrançois Tigeot 		if (te->ts == t)
15977a6b00eSFrançois Tigeot 			goto found_and_done;
16077a6b00eSFrançois Tigeot 	}
16177a6b00eSFrançois Tigeot 
16277a6b00eSFrançois Tigeot 	te = kzalloc(sizeof(struct tasklet_entry), M_WAITOK);
16377a6b00eSFrançois Tigeot 	te->ts = t;
16477a6b00eSFrançois Tigeot 	SLIST_INSERT_HEAD(&tlist_hi, te, tasklet_entries);
16577a6b00eSFrançois Tigeot 
16677a6b00eSFrançois Tigeot found_and_done:
16777a6b00eSFrançois Tigeot 	/* schedule the runner thread on the local cpu core */
16877a6b00eSFrançois Tigeot 	tasklet_pending = 1;
16977a6b00eSFrançois Tigeot 	wakeup(&tasklet_runner);
17077a6b00eSFrançois Tigeot 	lockmgr(&tasklet_lock, LK_RELEASE);
17177a6b00eSFrançois Tigeot }
17277a6b00eSFrançois Tigeot 
17377a6b00eSFrançois Tigeot void
17477a6b00eSFrançois Tigeot tasklet_kill(struct tasklet_struct *t)
17577a6b00eSFrançois Tigeot {
17677a6b00eSFrançois Tigeot 	set_bit(TASKLET_IS_DYING, &t->state);
17777a6b00eSFrançois Tigeot 	wakeup(&tasklet_runner);
17877a6b00eSFrançois Tigeot }
17977a6b00eSFrançois Tigeot 
18077a6b00eSFrançois Tigeot static int init_tasklets(void *arg)
18177a6b00eSFrançois Tigeot {
18277a6b00eSFrançois Tigeot 	kthread_create(tasklet_runner, NULL, &tasklet_td, "tasklet_runner");
18377a6b00eSFrançois Tigeot 
18477a6b00eSFrançois Tigeot 	return 0;
18577a6b00eSFrançois Tigeot }
18677a6b00eSFrançois Tigeot 
18777a6b00eSFrançois Tigeot SYSINIT(linux_tasklet_init, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, init_tasklets, NULL);
188