2 * Copyright (C) 2013 Red Hat
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:
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
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
25 #include <drm/drm_util.h>
26 #include <drm/drm_flip_work.h>
29 * drm_flip_work_allocate_task - allocate a flip-work task
30 * @data: data associated to the task
31 * @flags: allocator flags
33 * Allocate a drm_flip_task object and attach private data to it.
35 struct drm_flip_task
*drm_flip_work_allocate_task(void *data
, gfp_t flags
)
37 struct drm_flip_task
*task
;
39 task
= kzalloc(sizeof(*task
), flags
);
45 EXPORT_SYMBOL(drm_flip_work_allocate_task
);
48 * drm_flip_work_queue_task - queue a specific task
49 * @work: the flip-work
50 * @task: the task to handle
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.
55 void drm_flip_work_queue_task(struct drm_flip_work
*work
,
56 struct drm_flip_task
*task
)
60 spin_lock_irqsave(&work
->lock
, flags
);
61 list_add_tail(&task
->node
, &work
->queued
);
62 spin_unlock_irqrestore(&work
->lock
, flags
);
64 EXPORT_SYMBOL(drm_flip_work_queue_task
);
67 * drm_flip_work_queue - queue work
68 * @work: the flip-work
69 * @val: the value to queue
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.
74 void drm_flip_work_queue(struct drm_flip_work
*work
, void *val
)
76 struct drm_flip_task
*task
;
78 task
= drm_flip_work_allocate_task(val
,
79 drm_can_sleep() ? GFP_KERNEL
: GFP_ATOMIC
);
81 drm_flip_work_queue_task(work
, task
);
83 DRM_ERROR("%s could not allocate task!\n", work
->name
);
84 work
->func(work
, val
);
87 EXPORT_SYMBOL(drm_flip_work_queue
);
90 * drm_flip_work_commit - commit queued work
91 * @work: the flip-work
92 * @wq: the work-queue to run the queued work on
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.
99 void drm_flip_work_commit(struct drm_flip_work
*work
,
100 struct workqueue_struct
*wq
)
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
);
110 EXPORT_SYMBOL(drm_flip_work_commit
);
112 static void flip_worker(struct work_struct
*w
)
114 struct drm_flip_work
*work
= container_of(w
, struct drm_flip_work
, worker
);
115 struct list_head tasks
;
119 struct drm_flip_task
*task
, *tmp
;
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
);
127 if (list_empty(&tasks
))
130 list_for_each_entry_safe(task
, tmp
, &tasks
, node
) {
131 work
->func(work
, task
->data
);
138 * drm_flip_work_init - initialize flip-work
139 * @work: the flip-work to initialize
141 * @func: the callback work function
143 * Initializes/allocates resources for the flip-work
145 void drm_flip_work_init(struct drm_flip_work
*work
,
146 const char *name
, drm_flip_func_t func
)
149 INIT_LIST_HEAD(&work
->queued
);
150 INIT_LIST_HEAD(&work
->commited
);
151 spin_lock_init(&work
->lock
);
154 INIT_WORK(&work
->worker
, flip_worker
);
156 EXPORT_SYMBOL(drm_flip_work_init
);
159 * drm_flip_work_cleanup - cleans up flip-work
160 * @work: the flip-work to cleanup
162 * Destroy resources allocated for the flip-work
164 void drm_flip_work_cleanup(struct drm_flip_work
*work
)
166 WARN_ON(!list_empty(&work
->queued
) || !list_empty(&work
->commited
));
168 EXPORT_SYMBOL(drm_flip_work_cleanup
);