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 <linux/export.h> |
25 | #include "drmP.h" |
26 | #include "drm_flip_work.h" |
27 | |
28 | /** |
29 | * drm_flip_work_queue - queue work |
30 | * @work: the flip-work |
31 | * @val: the value to queue |
32 | * |
33 | * Queues work, that will later be run (passed back to drm_flip_func_t |
34 | * func) on a work queue after drm_flip_work_commit() is called. |
35 | */ |
36 | void drm_flip_work_queue(struct drm_flip_work *work, void *val) |
37 | { |
38 | if (kfifo_put(&work->fifo, val)) { |
39 | atomic_inc(&work->pending); |
40 | } else { |
41 | DRM_ERROR("%s fifo full!\n" , work->name); |
42 | work->func(work, val); |
43 | } |
44 | } |
45 | EXPORT_SYMBOL(drm_flip_work_queue); |
46 | |
47 | /** |
48 | * drm_flip_work_commit - commit queued work |
49 | * @work: the flip-work |
50 | * @wq: the work-queue to run the queued work on |
51 | * |
52 | * Trigger work previously queued by drm_flip_work_queue() to run |
53 | * on a workqueue. The typical usage would be to queue work (via |
54 | * drm_flip_work_queue()) at any point (from vblank irq and/or |
55 | * prior), and then from vblank irq commit the queued work. |
56 | */ |
57 | void drm_flip_work_commit(struct drm_flip_work *work, |
58 | struct workqueue_struct *wq) |
59 | { |
60 | uint32_t pending = atomic_read(&work->pending); |
61 | atomic_add(pending, &work->count); |
62 | atomic_sub(pending, &work->pending); |
63 | queue_work(wq, &work->worker); |
64 | } |
65 | EXPORT_SYMBOL(drm_flip_work_commit); |
66 | |
67 | static void flip_worker(struct work_struct *w) |
68 | { |
69 | struct drm_flip_work *work = container_of(w, struct drm_flip_work, worker); |
70 | uint32_t count = atomic_read(&work->count); |
71 | void *val = NULL; |
72 | |
73 | atomic_sub(count, &work->count); |
74 | |
75 | while(count--) |
76 | if (!WARN_ON(!kfifo_get(&work->fifo, &val))) |
77 | work->func(work, val); |
78 | } |
79 | |
80 | /** |
81 | * drm_flip_work_init - initialize flip-work |
82 | * @work: the flip-work to initialize |
83 | * @size: the max queue depth |
84 | * @name: debug name |
85 | * @func: the callback work function |
86 | * |
87 | * Initializes/allocates resources for the flip-work |
88 | * |
89 | * RETURNS: |
90 | * Zero on success, error code on failure. |
91 | */ |
92 | int drm_flip_work_init(struct drm_flip_work *work, int size, |
93 | const char *name, drm_flip_func_t func) |
94 | { |
95 | int ret; |
96 | |
97 | work->name = name; |
98 | atomic_set(&work->count, 0); |
99 | atomic_set(&work->pending, 0); |
100 | work->func = func; |
101 | |
102 | ret = kfifo_alloc(&work->fifo, size, GFP_KERNEL); |
103 | if (ret) { |
104 | DRM_ERROR("could not allocate %s fifo\n" , name); |
105 | return ret; |
106 | } |
107 | |
108 | INIT_WORK(&work->worker, flip_worker); |
109 | |
110 | return 0; |
111 | } |
112 | EXPORT_SYMBOL(drm_flip_work_init); |
113 | |
114 | /** |
115 | * drm_flip_work_cleanup - cleans up flip-work |
116 | * @work: the flip-work to cleanup |
117 | * |
118 | * Destroy resources allocated for the flip-work |
119 | */ |
120 | void drm_flip_work_cleanup(struct drm_flip_work *work) |
121 | { |
122 | WARN_ON(!kfifo_is_empty(&work->fifo)); |
123 | kfifo_free(&work->fifo); |
124 | } |
125 | EXPORT_SYMBOL(drm_flip_work_cleanup); |
126 | |