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_flip_work.h"
28 * drm_flip_work_queue - queue work
29 * @work: the flip-work
30 * @val: the value to queue
32 * Queues work, that will later be run (passed back to drm_flip_func_t
33 * func) on a work queue after drm_flip_work_commit() is called.
35 void drm_flip_work_queue(struct drm_flip_work
*work
, void *val
)
37 if (kfifo_put(&work
->fifo
, val
)) {
38 atomic_inc(&work
->pending
);
40 DRM_ERROR("%s fifo full!\n", work
->name
);
41 work
->func(work
, val
);
44 EXPORT_SYMBOL(drm_flip_work_queue
);
47 * drm_flip_work_commit - commit queued work
48 * @work: the flip-work
49 * @wq: the work-queue to run the queued work on
51 * Trigger work previously queued by drm_flip_work_queue() to run
52 * on a workqueue. The typical usage would be to queue work (via
53 * drm_flip_work_queue()) at any point (from vblank irq and/or
54 * prior), and then from vblank irq commit the queued work.
56 void drm_flip_work_commit(struct drm_flip_work
*work
,
57 struct workqueue_struct
*wq
)
59 uint32_t pending
= atomic_read(&work
->pending
);
60 atomic_add(pending
, &work
->count
);
61 atomic_sub(pending
, &work
->pending
);
62 queue_work(wq
, &work
->worker
);
64 EXPORT_SYMBOL(drm_flip_work_commit
);
66 static void flip_worker(struct work_struct
*w
)
68 struct drm_flip_work
*work
= container_of(w
, struct drm_flip_work
, worker
);
69 uint32_t count
= atomic_read(&work
->count
);
72 atomic_sub(count
, &work
->count
);
75 if (!WARN_ON(!kfifo_get(&work
->fifo
, &val
)))
76 work
->func(work
, val
);
80 * drm_flip_work_init - initialize flip-work
81 * @work: the flip-work to initialize
82 * @size: the max queue depth
84 * @func: the callback work function
86 * Initializes/allocates resources for the flip-work
89 * Zero on success, error code on failure.
91 int drm_flip_work_init(struct drm_flip_work
*work
, int size
,
92 const char *name
, drm_flip_func_t func
)
97 atomic_set(&work
->count
, 0);
98 atomic_set(&work
->pending
, 0);
101 ret
= kfifo_alloc(&work
->fifo
, size
, GFP_KERNEL
);
103 DRM_ERROR("could not allocate %s fifo\n", name
);
107 INIT_WORK(&work
->worker
, flip_worker
);
111 EXPORT_SYMBOL(drm_flip_work_init
);
114 * drm_flip_work_cleanup - cleans up flip-work
115 * @work: the flip-work to cleanup
117 * Destroy resources allocated for the flip-work
119 void drm_flip_work_cleanup(struct drm_flip_work
*work
)
121 WARN_ON(!kfifo_is_empty(&work
->fifo
));
122 kfifo_free(&work
->fifo
);
124 EXPORT_SYMBOL(drm_flip_work_cleanup
);