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
24*0500c04eSSam Ravnborg #include <linux/slab.h>
25*0500c04eSSam Ravnborg
26f9be5f20SMaarten Lankhorst #include <drm/drm_flip_work.h>
27*0500c04eSSam Ravnborg #include <drm/drm_print.h>
28*0500c04eSSam Ravnborg #include <drm/drm_util.h>
29cabaafc7SRob Clark
30cabaafc7SRob Clark /**
318bd4ae20SBoris BREZILLON * drm_flip_work_allocate_task - allocate a flip-work task
328bd4ae20SBoris BREZILLON * @data: data associated to the task
338bd4ae20SBoris BREZILLON * @flags: allocator flags
348bd4ae20SBoris BREZILLON *
358bd4ae20SBoris BREZILLON * Allocate a drm_flip_task object and attach private data to it.
368bd4ae20SBoris BREZILLON */
drm_flip_work_allocate_task(void * data,gfp_t flags)378bd4ae20SBoris BREZILLON struct drm_flip_task *drm_flip_work_allocate_task(void *data, gfp_t flags)
388bd4ae20SBoris BREZILLON {
398bd4ae20SBoris BREZILLON struct drm_flip_task *task;
408bd4ae20SBoris BREZILLON
418bd4ae20SBoris BREZILLON task = kzalloc(sizeof(*task), flags);
428bd4ae20SBoris BREZILLON if (task)
438bd4ae20SBoris BREZILLON task->data = data;
448bd4ae20SBoris BREZILLON
458bd4ae20SBoris BREZILLON return task;
468bd4ae20SBoris BREZILLON }
478bd4ae20SBoris BREZILLON EXPORT_SYMBOL(drm_flip_work_allocate_task);
488bd4ae20SBoris BREZILLON
498bd4ae20SBoris BREZILLON /**
508bd4ae20SBoris BREZILLON * drm_flip_work_queue_task - queue a specific task
518bd4ae20SBoris BREZILLON * @work: the flip-work
528bd4ae20SBoris BREZILLON * @task: the task to handle
538bd4ae20SBoris BREZILLON *
548bd4ae20SBoris BREZILLON * Queues task, that will later be run (passed back to drm_flip_func_t
558bd4ae20SBoris BREZILLON * func) on a work queue after drm_flip_work_commit() is called.
568bd4ae20SBoris BREZILLON */
drm_flip_work_queue_task(struct drm_flip_work * work,struct drm_flip_task * task)578bd4ae20SBoris BREZILLON void drm_flip_work_queue_task(struct drm_flip_work *work,
588bd4ae20SBoris BREZILLON struct drm_flip_task *task)
598bd4ae20SBoris BREZILLON {
608bd4ae20SBoris BREZILLON unsigned long flags;
618bd4ae20SBoris BREZILLON
628bd4ae20SBoris BREZILLON spin_lock_irqsave(&work->lock, flags);
638bd4ae20SBoris BREZILLON list_add_tail(&task->node, &work->queued);
648bd4ae20SBoris BREZILLON spin_unlock_irqrestore(&work->lock, flags);
658bd4ae20SBoris BREZILLON }
668bd4ae20SBoris BREZILLON EXPORT_SYMBOL(drm_flip_work_queue_task);
678bd4ae20SBoris BREZILLON
688bd4ae20SBoris BREZILLON /**
69cabaafc7SRob Clark * drm_flip_work_queue - queue work
70cabaafc7SRob Clark * @work: the flip-work
71cabaafc7SRob Clark * @val: the value to queue
72cabaafc7SRob Clark *
73cabaafc7SRob Clark * Queues work, that will later be run (passed back to drm_flip_func_t
74cabaafc7SRob Clark * func) on a work queue after drm_flip_work_commit() is called.
75cabaafc7SRob Clark */
drm_flip_work_queue(struct drm_flip_work * work,void * val)76cabaafc7SRob Clark void drm_flip_work_queue(struct drm_flip_work *work, void *val)
77cabaafc7SRob Clark {
788bd4ae20SBoris BREZILLON struct drm_flip_task *task;
798bd4ae20SBoris BREZILLON
808bd4ae20SBoris BREZILLON task = drm_flip_work_allocate_task(val,
818bd4ae20SBoris BREZILLON drm_can_sleep() ? GFP_KERNEL : GFP_ATOMIC);
828bd4ae20SBoris BREZILLON if (task) {
838bd4ae20SBoris BREZILLON drm_flip_work_queue_task(work, task);
84cabaafc7SRob Clark } else {
858bd4ae20SBoris BREZILLON DRM_ERROR("%s could not allocate task!\n", work->name);
86cabaafc7SRob Clark work->func(work, val);
87cabaafc7SRob Clark }
88cabaafc7SRob Clark }
89cabaafc7SRob Clark EXPORT_SYMBOL(drm_flip_work_queue);
90cabaafc7SRob Clark
91cabaafc7SRob Clark /**
92cabaafc7SRob Clark * drm_flip_work_commit - commit queued work
93cabaafc7SRob Clark * @work: the flip-work
94cabaafc7SRob Clark * @wq: the work-queue to run the queued work on
95cabaafc7SRob Clark *
96cabaafc7SRob Clark * Trigger work previously queued by drm_flip_work_queue() to run
97cabaafc7SRob Clark * on a workqueue. The typical usage would be to queue work (via
98cabaafc7SRob Clark * drm_flip_work_queue()) at any point (from vblank irq and/or
99cabaafc7SRob Clark * prior), and then from vblank irq commit the queued work.
100cabaafc7SRob Clark */
drm_flip_work_commit(struct drm_flip_work * work,struct workqueue_struct * wq)101cabaafc7SRob Clark void drm_flip_work_commit(struct drm_flip_work *work,
102cabaafc7SRob Clark struct workqueue_struct *wq)
103cabaafc7SRob Clark {
1048bd4ae20SBoris BREZILLON unsigned long flags;
1058bd4ae20SBoris BREZILLON
1068bd4ae20SBoris BREZILLON spin_lock_irqsave(&work->lock, flags);
1078bd4ae20SBoris BREZILLON list_splice_tail(&work->queued, &work->commited);
1088bd4ae20SBoris BREZILLON INIT_LIST_HEAD(&work->queued);
1098bd4ae20SBoris BREZILLON spin_unlock_irqrestore(&work->lock, flags);
110cabaafc7SRob Clark queue_work(wq, &work->worker);
111cabaafc7SRob Clark }
112cabaafc7SRob Clark EXPORT_SYMBOL(drm_flip_work_commit);
113cabaafc7SRob Clark
flip_worker(struct work_struct * w)114cabaafc7SRob Clark static void flip_worker(struct work_struct *w)
115cabaafc7SRob Clark {
116cabaafc7SRob Clark struct drm_flip_work *work = container_of(w, struct drm_flip_work, worker);
1178bd4ae20SBoris BREZILLON struct list_head tasks;
1188bd4ae20SBoris BREZILLON unsigned long flags;
119cabaafc7SRob Clark
1208bd4ae20SBoris BREZILLON while (1) {
1218bd4ae20SBoris BREZILLON struct drm_flip_task *task, *tmp;
122cabaafc7SRob Clark
1238bd4ae20SBoris BREZILLON INIT_LIST_HEAD(&tasks);
1248bd4ae20SBoris BREZILLON spin_lock_irqsave(&work->lock, flags);
1258bd4ae20SBoris BREZILLON list_splice_tail(&work->commited, &tasks);
1268bd4ae20SBoris BREZILLON INIT_LIST_HEAD(&work->commited);
1278bd4ae20SBoris BREZILLON spin_unlock_irqrestore(&work->lock, flags);
1288bd4ae20SBoris BREZILLON
1298bd4ae20SBoris BREZILLON if (list_empty(&tasks))
1308bd4ae20SBoris BREZILLON break;
1318bd4ae20SBoris BREZILLON
1328bd4ae20SBoris BREZILLON list_for_each_entry_safe(task, tmp, &tasks, node) {
1338bd4ae20SBoris BREZILLON work->func(work, task->data);
1348bd4ae20SBoris BREZILLON kfree(task);
1358bd4ae20SBoris BREZILLON }
1368bd4ae20SBoris BREZILLON }
137cabaafc7SRob Clark }
138cabaafc7SRob Clark
139cabaafc7SRob Clark /**
140cabaafc7SRob Clark * drm_flip_work_init - initialize flip-work
141cabaafc7SRob Clark * @work: the flip-work to initialize
142cabaafc7SRob Clark * @name: debug name
143cabaafc7SRob Clark * @func: the callback work function
144cabaafc7SRob Clark *
145cabaafc7SRob Clark * Initializes/allocates resources for the flip-work
146cabaafc7SRob Clark */
drm_flip_work_init(struct drm_flip_work * work,const char * name,drm_flip_func_t func)147d7f8db53SBoris BREZILLON void drm_flip_work_init(struct drm_flip_work *work,
148cabaafc7SRob Clark const char *name, drm_flip_func_t func)
149cabaafc7SRob Clark {
150cabaafc7SRob Clark work->name = name;
1518bd4ae20SBoris BREZILLON INIT_LIST_HEAD(&work->queued);
1528bd4ae20SBoris BREZILLON INIT_LIST_HEAD(&work->commited);
1538bd4ae20SBoris BREZILLON spin_lock_init(&work->lock);
154cabaafc7SRob Clark work->func = func;
155cabaafc7SRob Clark
156cabaafc7SRob Clark INIT_WORK(&work->worker, flip_worker);
157cabaafc7SRob Clark }
158cabaafc7SRob Clark EXPORT_SYMBOL(drm_flip_work_init);
159cabaafc7SRob Clark
160cabaafc7SRob Clark /**
161cabaafc7SRob Clark * drm_flip_work_cleanup - cleans up flip-work
162cabaafc7SRob Clark * @work: the flip-work to cleanup
163cabaafc7SRob Clark *
164cabaafc7SRob Clark * Destroy resources allocated for the flip-work
165cabaafc7SRob Clark */
drm_flip_work_cleanup(struct drm_flip_work * work)166cabaafc7SRob Clark void drm_flip_work_cleanup(struct drm_flip_work *work)
167cabaafc7SRob Clark {
1688bd4ae20SBoris BREZILLON WARN_ON(!list_empty(&work->queued) || !list_empty(&work->commited));
169cabaafc7SRob Clark }
170cabaafc7SRob Clark EXPORT_SYMBOL(drm_flip_work_cleanup);
171