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