Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (C) 2013 Red Hat
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice (including the next
0012  * paragraph) shall be included in all copies or substantial portions of the
0013  * Software.
0014  *
0015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0018  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0020  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0021  * SOFTWARE.
0022  */
0023 
0024 #include <linux/slab.h>
0025 
0026 #include <drm/drm_flip_work.h>
0027 #include <drm/drm_print.h>
0028 #include <drm/drm_util.h>
0029 
0030 /**
0031  * drm_flip_work_allocate_task - allocate a flip-work task
0032  * @data: data associated to the task
0033  * @flags: allocator flags
0034  *
0035  * Allocate a drm_flip_task object and attach private data to it.
0036  */
0037 struct drm_flip_task *drm_flip_work_allocate_task(void *data, gfp_t flags)
0038 {
0039     struct drm_flip_task *task;
0040 
0041     task = kzalloc(sizeof(*task), flags);
0042     if (task)
0043         task->data = data;
0044 
0045     return task;
0046 }
0047 EXPORT_SYMBOL(drm_flip_work_allocate_task);
0048 
0049 /**
0050  * drm_flip_work_queue_task - queue a specific task
0051  * @work: the flip-work
0052  * @task: the task to handle
0053  *
0054  * Queues task, that will later be run (passed back to drm_flip_func_t
0055  * func) on a work queue after drm_flip_work_commit() is called.
0056  */
0057 void drm_flip_work_queue_task(struct drm_flip_work *work,
0058                   struct drm_flip_task *task)
0059 {
0060     unsigned long flags;
0061 
0062     spin_lock_irqsave(&work->lock, flags);
0063     list_add_tail(&task->node, &work->queued);
0064     spin_unlock_irqrestore(&work->lock, flags);
0065 }
0066 EXPORT_SYMBOL(drm_flip_work_queue_task);
0067 
0068 /**
0069  * drm_flip_work_queue - queue work
0070  * @work: the flip-work
0071  * @val: the value to queue
0072  *
0073  * Queues work, that will later be run (passed back to drm_flip_func_t
0074  * func) on a work queue after drm_flip_work_commit() is called.
0075  */
0076 void drm_flip_work_queue(struct drm_flip_work *work, void *val)
0077 {
0078     struct drm_flip_task *task;
0079 
0080     task = drm_flip_work_allocate_task(val,
0081                 drm_can_sleep() ? GFP_KERNEL : GFP_ATOMIC);
0082     if (task) {
0083         drm_flip_work_queue_task(work, task);
0084     } else {
0085         DRM_ERROR("%s could not allocate task!\n", work->name);
0086         work->func(work, val);
0087     }
0088 }
0089 EXPORT_SYMBOL(drm_flip_work_queue);
0090 
0091 /**
0092  * drm_flip_work_commit - commit queued work
0093  * @work: the flip-work
0094  * @wq: the work-queue to run the queued work on
0095  *
0096  * Trigger work previously queued by drm_flip_work_queue() to run
0097  * on a workqueue.  The typical usage would be to queue work (via
0098  * drm_flip_work_queue()) at any point (from vblank irq and/or
0099  * prior), and then from vblank irq commit the queued work.
0100  */
0101 void drm_flip_work_commit(struct drm_flip_work *work,
0102         struct workqueue_struct *wq)
0103 {
0104     unsigned long flags;
0105 
0106     spin_lock_irqsave(&work->lock, flags);
0107     list_splice_tail(&work->queued, &work->commited);
0108     INIT_LIST_HEAD(&work->queued);
0109     spin_unlock_irqrestore(&work->lock, flags);
0110     queue_work(wq, &work->worker);
0111 }
0112 EXPORT_SYMBOL(drm_flip_work_commit);
0113 
0114 static void flip_worker(struct work_struct *w)
0115 {
0116     struct drm_flip_work *work = container_of(w, struct drm_flip_work, worker);
0117     struct list_head tasks;
0118     unsigned long flags;
0119 
0120     while (1) {
0121         struct drm_flip_task *task, *tmp;
0122 
0123         INIT_LIST_HEAD(&tasks);
0124         spin_lock_irqsave(&work->lock, flags);
0125         list_splice_tail(&work->commited, &tasks);
0126         INIT_LIST_HEAD(&work->commited);
0127         spin_unlock_irqrestore(&work->lock, flags);
0128 
0129         if (list_empty(&tasks))
0130             break;
0131 
0132         list_for_each_entry_safe(task, tmp, &tasks, node) {
0133             work->func(work, task->data);
0134             kfree(task);
0135         }
0136     }
0137 }
0138 
0139 /**
0140  * drm_flip_work_init - initialize flip-work
0141  * @work: the flip-work to initialize
0142  * @name: debug name
0143  * @func: the callback work function
0144  *
0145  * Initializes/allocates resources for the flip-work
0146  */
0147 void drm_flip_work_init(struct drm_flip_work *work,
0148         const char *name, drm_flip_func_t func)
0149 {
0150     work->name = name;
0151     INIT_LIST_HEAD(&work->queued);
0152     INIT_LIST_HEAD(&work->commited);
0153     spin_lock_init(&work->lock);
0154     work->func = func;
0155 
0156     INIT_WORK(&work->worker, flip_worker);
0157 }
0158 EXPORT_SYMBOL(drm_flip_work_init);
0159 
0160 /**
0161  * drm_flip_work_cleanup - cleans up flip-work
0162  * @work: the flip-work to cleanup
0163  *
0164  * Destroy resources allocated for the flip-work
0165  */
0166 void drm_flip_work_cleanup(struct drm_flip_work *work)
0167 {
0168     WARN_ON(!list_empty(&work->queued) || !list_empty(&work->commited));
0169 }
0170 EXPORT_SYMBOL(drm_flip_work_cleanup);