xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/drm_flip_work.c (revision 816569f203573b784d78b52f8d2969e5b3dcb270)
1 /*	$NetBSD: drm_flip_work.c,v 1.6 2021/12/19 12:32:01 riastradh Exp $	*/
2 
3 /*
4  * Copyright (C) 2013 Red Hat
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: drm_flip_work.c,v 1.6 2021/12/19 12:32:01 riastradh Exp $");
28 
29 #include <linux/slab.h>
30 
31 #include <drm/drm_flip_work.h>
32 #include <drm/drm_print.h>
33 #include <drm/drm_util.h>
34 
35 /**
36  * drm_flip_work_allocate_task - allocate a flip-work task
37  * @data: data associated to the task
38  * @flags: allocator flags
39  *
40  * Allocate a drm_flip_task object and attach private data to it.
41  */
drm_flip_work_allocate_task(void * data,gfp_t flags)42 struct drm_flip_task *drm_flip_work_allocate_task(void *data, gfp_t flags)
43 {
44 	struct drm_flip_task *task;
45 
46 	task = kzalloc(sizeof(*task), flags);
47 	if (task)
48 		task->data = data;
49 
50 	return task;
51 }
52 EXPORT_SYMBOL(drm_flip_work_allocate_task);
53 
54 /**
55  * drm_flip_work_queue_task - queue a specific task
56  * @work: the flip-work
57  * @task: the task to handle
58  *
59  * Queues task, that will later be run (passed back to drm_flip_func_t
60  * func) on a work queue after drm_flip_work_commit() is called.
61  */
drm_flip_work_queue_task(struct drm_flip_work * work,struct drm_flip_task * task)62 void drm_flip_work_queue_task(struct drm_flip_work *work,
63 			      struct drm_flip_task *task)
64 {
65 	unsigned long flags;
66 
67 	spin_lock_irqsave(&work->lock, flags);
68 	list_add_tail(&task->node, &work->queued);
69 	spin_unlock_irqrestore(&work->lock, flags);
70 }
71 EXPORT_SYMBOL(drm_flip_work_queue_task);
72 
73 /**
74  * drm_flip_work_queue - queue work
75  * @work: the flip-work
76  * @val: the value to queue
77  *
78  * Queues work, that will later be run (passed back to drm_flip_func_t
79  * func) on a work queue after drm_flip_work_commit() is called.
80  */
drm_flip_work_queue(struct drm_flip_work * work,void * val)81 void drm_flip_work_queue(struct drm_flip_work *work, void *val)
82 {
83 	struct drm_flip_task *task;
84 
85 	task = drm_flip_work_allocate_task(val,
86 				drm_can_sleep() ? GFP_KERNEL : GFP_ATOMIC);
87 	if (task) {
88 		drm_flip_work_queue_task(work, task);
89 	} else {
90 		DRM_ERROR("%s could not allocate task!\n", work->name);
91 		work->func(work, val);
92 	}
93 }
94 EXPORT_SYMBOL(drm_flip_work_queue);
95 
96 /**
97  * drm_flip_work_commit - commit queued work
98  * @work: the flip-work
99  * @wq: the work-queue to run the queued work on
100  *
101  * Trigger work previously queued by drm_flip_work_queue() to run
102  * on a workqueue.  The typical usage would be to queue work (via
103  * drm_flip_work_queue()) at any point (from vblank irq and/or
104  * prior), and then from vblank irq commit the queued work.
105  */
drm_flip_work_commit(struct drm_flip_work * work,struct workqueue_struct * wq)106 void drm_flip_work_commit(struct drm_flip_work *work,
107 		struct workqueue_struct *wq)
108 {
109 	unsigned long flags;
110 
111 	spin_lock_irqsave(&work->lock, flags);
112 	list_splice_tail(&work->queued, &work->commited);
113 	INIT_LIST_HEAD(&work->queued);
114 	spin_unlock_irqrestore(&work->lock, flags);
115 	queue_work(wq, &work->worker);
116 }
117 EXPORT_SYMBOL(drm_flip_work_commit);
118 
flip_worker(struct work_struct * w)119 static void flip_worker(struct work_struct *w)
120 {
121 	struct drm_flip_work *work = container_of(w, struct drm_flip_work, worker);
122 	struct list_head tasks;
123 	unsigned long flags;
124 
125 	while (1) {
126 		struct drm_flip_task *task, *tmp;
127 
128 		INIT_LIST_HEAD(&tasks);
129 		spin_lock_irqsave(&work->lock, flags);
130 		list_splice_tail(&work->commited, &tasks);
131 		INIT_LIST_HEAD(&work->commited);
132 		spin_unlock_irqrestore(&work->lock, flags);
133 
134 		if (list_empty(&tasks))
135 			break;
136 
137 		list_for_each_entry_safe(task, tmp, &tasks, node) {
138 			work->func(work, task->data);
139 			kfree(task);
140 		}
141 	}
142 }
143 
144 /**
145  * drm_flip_work_init - initialize flip-work
146  * @work: the flip-work to initialize
147  * @name: debug name
148  * @func: the callback work function
149  *
150  * Initializes/allocates resources for the flip-work
151  */
drm_flip_work_init(struct drm_flip_work * work,const char * name,drm_flip_func_t func)152 void drm_flip_work_init(struct drm_flip_work *work,
153 		const char *name, drm_flip_func_t func)
154 {
155 	work->name = name;
156 	INIT_LIST_HEAD(&work->queued);
157 	INIT_LIST_HEAD(&work->commited);
158 	spin_lock_init(&work->lock);
159 	work->func = func;
160 
161 	INIT_WORK(&work->worker, flip_worker);
162 }
163 EXPORT_SYMBOL(drm_flip_work_init);
164 
165 /**
166  * drm_flip_work_cleanup - cleans up flip-work
167  * @work: the flip-work to cleanup
168  *
169  * Destroy resources allocated for the flip-work
170  */
drm_flip_work_cleanup(struct drm_flip_work * work)171 void drm_flip_work_cleanup(struct drm_flip_work *work)
172 {
173 	WARN_ON(!list_empty(&work->queued) || !list_empty(&work->commited));
174 	spin_lock_destroy(&work->lock);
175 }
176 EXPORT_SYMBOL(drm_flip_work_cleanup);
177