xref: /linux/drivers/gpu/drm/drm_flip_work.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
1cabaafc7SRob Clark /*
2cabaafc7SRob Clark  * Copyright (C) 2013 Red Hat
3cabaafc7SRob Clark  *
4cabaafc7SRob Clark  * Permission is hereby granted, free of charge, to any person obtaining a
5cabaafc7SRob Clark  * copy of this software and associated documentation files (the "Software"),
6cabaafc7SRob Clark  * to deal in the Software without restriction, including without limitation
7cabaafc7SRob Clark  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8cabaafc7SRob Clark  * and/or sell copies of the Software, and to permit persons to whom the
9cabaafc7SRob Clark  * Software is furnished to do so, subject to the following conditions:
10cabaafc7SRob Clark  *
11cabaafc7SRob Clark  * The above copyright notice and this permission notice (including the next
12cabaafc7SRob Clark  * paragraph) shall be included in all copies or substantial portions of the
13cabaafc7SRob Clark  * Software.
14cabaafc7SRob Clark  *
15cabaafc7SRob Clark  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16cabaafc7SRob Clark  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17cabaafc7SRob Clark  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18cabaafc7SRob Clark  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19cabaafc7SRob Clark  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20cabaafc7SRob Clark  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21cabaafc7SRob Clark  * SOFTWARE.
22cabaafc7SRob Clark  */
23cabaafc7SRob Clark 
240500c04eSSam Ravnborg #include <linux/slab.h>
250500c04eSSam Ravnborg 
26f9be5f20SMaarten Lankhorst #include <drm/drm_flip_work.h>
270500c04eSSam Ravnborg #include <drm/drm_print.h>
280500c04eSSam Ravnborg #include <drm/drm_util.h>
29cabaafc7SRob Clark 
30*78dfe8a0SThomas Zimmermann struct drm_flip_task {
31*78dfe8a0SThomas Zimmermann 	struct list_head node;
32*78dfe8a0SThomas Zimmermann 	void *data;
33*78dfe8a0SThomas Zimmermann };
34*78dfe8a0SThomas Zimmermann 
drm_flip_work_allocate_task(void * data,gfp_t flags)35*78dfe8a0SThomas Zimmermann static struct drm_flip_task *drm_flip_work_allocate_task(void *data, gfp_t flags)
368bd4ae20SBoris BREZILLON {
378bd4ae20SBoris BREZILLON 	struct drm_flip_task *task;
388bd4ae20SBoris BREZILLON 
398bd4ae20SBoris BREZILLON 	task = kzalloc(sizeof(*task), flags);
408bd4ae20SBoris BREZILLON 	if (task)
418bd4ae20SBoris BREZILLON 		task->data = data;
428bd4ae20SBoris BREZILLON 
438bd4ae20SBoris BREZILLON 	return task;
448bd4ae20SBoris BREZILLON }
458bd4ae20SBoris BREZILLON 
drm_flip_work_queue_task(struct drm_flip_work * work,struct drm_flip_task * task)46*78dfe8a0SThomas Zimmermann static void drm_flip_work_queue_task(struct drm_flip_work *work, struct drm_flip_task *task)
478bd4ae20SBoris BREZILLON {
488bd4ae20SBoris BREZILLON 	unsigned long flags;
498bd4ae20SBoris BREZILLON 
508bd4ae20SBoris BREZILLON 	spin_lock_irqsave(&work->lock, flags);
518bd4ae20SBoris BREZILLON 	list_add_tail(&task->node, &work->queued);
528bd4ae20SBoris BREZILLON 	spin_unlock_irqrestore(&work->lock, flags);
538bd4ae20SBoris BREZILLON }
548bd4ae20SBoris BREZILLON 
558bd4ae20SBoris BREZILLON /**
56cabaafc7SRob Clark  * drm_flip_work_queue - queue work
57cabaafc7SRob Clark  * @work: the flip-work
58cabaafc7SRob Clark  * @val: the value to queue
59cabaafc7SRob Clark  *
60cabaafc7SRob Clark  * Queues work, that will later be run (passed back to drm_flip_func_t
61cabaafc7SRob Clark  * func) on a work queue after drm_flip_work_commit() is called.
62cabaafc7SRob Clark  */
drm_flip_work_queue(struct drm_flip_work * work,void * val)63cabaafc7SRob Clark void drm_flip_work_queue(struct drm_flip_work *work, void *val)
64cabaafc7SRob Clark {
658bd4ae20SBoris BREZILLON 	struct drm_flip_task *task;
668bd4ae20SBoris BREZILLON 
678bd4ae20SBoris BREZILLON 	task = drm_flip_work_allocate_task(val,
688bd4ae20SBoris BREZILLON 				drm_can_sleep() ? GFP_KERNEL : GFP_ATOMIC);
698bd4ae20SBoris BREZILLON 	if (task) {
708bd4ae20SBoris BREZILLON 		drm_flip_work_queue_task(work, task);
71cabaafc7SRob Clark 	} else {
728bd4ae20SBoris BREZILLON 		DRM_ERROR("%s could not allocate task!\n", work->name);
73cabaafc7SRob Clark 		work->func(work, val);
74cabaafc7SRob Clark 	}
75cabaafc7SRob Clark }
76cabaafc7SRob Clark EXPORT_SYMBOL(drm_flip_work_queue);
77cabaafc7SRob Clark 
78cabaafc7SRob Clark /**
79cabaafc7SRob Clark  * drm_flip_work_commit - commit queued work
80cabaafc7SRob Clark  * @work: the flip-work
81cabaafc7SRob Clark  * @wq: the work-queue to run the queued work on
82cabaafc7SRob Clark  *
83cabaafc7SRob Clark  * Trigger work previously queued by drm_flip_work_queue() to run
84cabaafc7SRob Clark  * on a workqueue.  The typical usage would be to queue work (via
85cabaafc7SRob Clark  * drm_flip_work_queue()) at any point (from vblank irq and/or
86cabaafc7SRob Clark  * prior), and then from vblank irq commit the queued work.
87cabaafc7SRob Clark  */
drm_flip_work_commit(struct drm_flip_work * work,struct workqueue_struct * wq)88cabaafc7SRob Clark void drm_flip_work_commit(struct drm_flip_work *work,
89cabaafc7SRob Clark 		struct workqueue_struct *wq)
90cabaafc7SRob Clark {
918bd4ae20SBoris BREZILLON 	unsigned long flags;
928bd4ae20SBoris BREZILLON 
938bd4ae20SBoris BREZILLON 	spin_lock_irqsave(&work->lock, flags);
948bd4ae20SBoris BREZILLON 	list_splice_tail(&work->queued, &work->commited);
958bd4ae20SBoris BREZILLON 	INIT_LIST_HEAD(&work->queued);
968bd4ae20SBoris BREZILLON 	spin_unlock_irqrestore(&work->lock, flags);
97cabaafc7SRob Clark 	queue_work(wq, &work->worker);
98cabaafc7SRob Clark }
99cabaafc7SRob Clark EXPORT_SYMBOL(drm_flip_work_commit);
100cabaafc7SRob Clark 
flip_worker(struct work_struct * w)101cabaafc7SRob Clark static void flip_worker(struct work_struct *w)
102cabaafc7SRob Clark {
103cabaafc7SRob Clark 	struct drm_flip_work *work = container_of(w, struct drm_flip_work, worker);
1048bd4ae20SBoris BREZILLON 	struct list_head tasks;
1058bd4ae20SBoris BREZILLON 	unsigned long flags;
106cabaafc7SRob Clark 
1078bd4ae20SBoris BREZILLON 	while (1) {
1088bd4ae20SBoris BREZILLON 		struct drm_flip_task *task, *tmp;
109cabaafc7SRob Clark 
1108bd4ae20SBoris BREZILLON 		INIT_LIST_HEAD(&tasks);
1118bd4ae20SBoris BREZILLON 		spin_lock_irqsave(&work->lock, flags);
1128bd4ae20SBoris BREZILLON 		list_splice_tail(&work->commited, &tasks);
1138bd4ae20SBoris BREZILLON 		INIT_LIST_HEAD(&work->commited);
1148bd4ae20SBoris BREZILLON 		spin_unlock_irqrestore(&work->lock, flags);
1158bd4ae20SBoris BREZILLON 
1168bd4ae20SBoris BREZILLON 		if (list_empty(&tasks))
1178bd4ae20SBoris BREZILLON 			break;
1188bd4ae20SBoris BREZILLON 
1198bd4ae20SBoris BREZILLON 		list_for_each_entry_safe(task, tmp, &tasks, node) {
1208bd4ae20SBoris BREZILLON 			work->func(work, task->data);
1218bd4ae20SBoris BREZILLON 			kfree(task);
1228bd4ae20SBoris BREZILLON 		}
1238bd4ae20SBoris BREZILLON 	}
124cabaafc7SRob Clark }
125cabaafc7SRob Clark 
126cabaafc7SRob Clark /**
127cabaafc7SRob Clark  * drm_flip_work_init - initialize flip-work
128cabaafc7SRob Clark  * @work: the flip-work to initialize
129cabaafc7SRob Clark  * @name: debug name
130cabaafc7SRob Clark  * @func: the callback work function
131cabaafc7SRob Clark  *
132cabaafc7SRob Clark  * Initializes/allocates resources for the flip-work
133cabaafc7SRob Clark  */
drm_flip_work_init(struct drm_flip_work * work,const char * name,drm_flip_func_t func)134d7f8db53SBoris BREZILLON void drm_flip_work_init(struct drm_flip_work *work,
135cabaafc7SRob Clark 		const char *name, drm_flip_func_t func)
136cabaafc7SRob Clark {
137cabaafc7SRob Clark 	work->name = name;
1388bd4ae20SBoris BREZILLON 	INIT_LIST_HEAD(&work->queued);
1398bd4ae20SBoris BREZILLON 	INIT_LIST_HEAD(&work->commited);
1408bd4ae20SBoris BREZILLON 	spin_lock_init(&work->lock);
141cabaafc7SRob Clark 	work->func = func;
142cabaafc7SRob Clark 
143cabaafc7SRob Clark 	INIT_WORK(&work->worker, flip_worker);
144cabaafc7SRob Clark }
145cabaafc7SRob Clark EXPORT_SYMBOL(drm_flip_work_init);
146cabaafc7SRob Clark 
147cabaafc7SRob Clark /**
148cabaafc7SRob Clark  * drm_flip_work_cleanup - cleans up flip-work
149cabaafc7SRob Clark  * @work: the flip-work to cleanup
150cabaafc7SRob Clark  *
151cabaafc7SRob Clark  * Destroy resources allocated for the flip-work
152cabaafc7SRob Clark  */
drm_flip_work_cleanup(struct drm_flip_work * work)153cabaafc7SRob Clark void drm_flip_work_cleanup(struct drm_flip_work *work)
154cabaafc7SRob Clark {
1558bd4ae20SBoris BREZILLON 	WARN_ON(!list_empty(&work->queued) || !list_empty(&work->commited));
156cabaafc7SRob Clark }
157cabaafc7SRob Clark EXPORT_SYMBOL(drm_flip_work_cleanup);
158