drm_flip_work.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #include <linux/slab.h>
  24. #include <drm/drm_flip_work.h>
  25. #include <drm/drm_print.h>
  26. #include <drm/drm_util.h>
  27. /**
  28. * drm_flip_work_allocate_task - allocate a flip-work task
  29. * @data: data associated to the task
  30. * @flags: allocator flags
  31. *
  32. * Allocate a drm_flip_task object and attach private data to it.
  33. */
  34. struct drm_flip_task *drm_flip_work_allocate_task(void *data, gfp_t flags)
  35. {
  36. struct drm_flip_task *task;
  37. task = kzalloc(sizeof(*task), flags);
  38. if (task)
  39. task->data = data;
  40. return task;
  41. }
  42. EXPORT_SYMBOL(drm_flip_work_allocate_task);
  43. /**
  44. * drm_flip_work_queue_task - queue a specific task
  45. * @work: the flip-work
  46. * @task: the task to handle
  47. *
  48. * Queues task, that will later be run (passed back to drm_flip_func_t
  49. * func) on a work queue after drm_flip_work_commit() is called.
  50. */
  51. void drm_flip_work_queue_task(struct drm_flip_work *work,
  52. struct drm_flip_task *task)
  53. {
  54. unsigned long flags;
  55. spin_lock_irqsave(&work->lock, flags);
  56. list_add_tail(&task->node, &work->queued);
  57. spin_unlock_irqrestore(&work->lock, flags);
  58. }
  59. EXPORT_SYMBOL(drm_flip_work_queue_task);
  60. /**
  61. * drm_flip_work_queue - queue work
  62. * @work: the flip-work
  63. * @val: the value to queue
  64. *
  65. * Queues work, that will later be run (passed back to drm_flip_func_t
  66. * func) on a work queue after drm_flip_work_commit() is called.
  67. */
  68. void drm_flip_work_queue(struct drm_flip_work *work, void *val)
  69. {
  70. struct drm_flip_task *task;
  71. task = drm_flip_work_allocate_task(val,
  72. drm_can_sleep() ? GFP_KERNEL : GFP_ATOMIC);
  73. if (task) {
  74. drm_flip_work_queue_task(work, task);
  75. } else {
  76. DRM_ERROR("%s could not allocate task!\n", work->name);
  77. work->func(work, val);
  78. }
  79. }
  80. EXPORT_SYMBOL(drm_flip_work_queue);
  81. /**
  82. * drm_flip_work_commit - commit queued work
  83. * @work: the flip-work
  84. * @wq: the work-queue to run the queued work on
  85. *
  86. * Trigger work previously queued by drm_flip_work_queue() to run
  87. * on a workqueue. The typical usage would be to queue work (via
  88. * drm_flip_work_queue()) at any point (from vblank irq and/or
  89. * prior), and then from vblank irq commit the queued work.
  90. */
  91. void drm_flip_work_commit(struct drm_flip_work *work,
  92. struct workqueue_struct *wq)
  93. {
  94. unsigned long flags;
  95. spin_lock_irqsave(&work->lock, flags);
  96. list_splice_tail(&work->queued, &work->commited);
  97. INIT_LIST_HEAD(&work->queued);
  98. spin_unlock_irqrestore(&work->lock, flags);
  99. queue_work(wq, &work->worker);
  100. }
  101. EXPORT_SYMBOL(drm_flip_work_commit);
  102. static void flip_worker(struct work_struct *w)
  103. {
  104. struct drm_flip_work *work = container_of(w, struct drm_flip_work, worker);
  105. struct list_head tasks;
  106. unsigned long flags;
  107. while (1) {
  108. struct drm_flip_task *task, *tmp;
  109. INIT_LIST_HEAD(&tasks);
  110. spin_lock_irqsave(&work->lock, flags);
  111. list_splice_tail(&work->commited, &tasks);
  112. INIT_LIST_HEAD(&work->commited);
  113. spin_unlock_irqrestore(&work->lock, flags);
  114. if (list_empty(&tasks))
  115. break;
  116. list_for_each_entry_safe(task, tmp, &tasks, node) {
  117. work->func(work, task->data);
  118. kfree(task);
  119. }
  120. }
  121. }
  122. /**
  123. * drm_flip_work_init - initialize flip-work
  124. * @work: the flip-work to initialize
  125. * @name: debug name
  126. * @func: the callback work function
  127. *
  128. * Initializes/allocates resources for the flip-work
  129. */
  130. void drm_flip_work_init(struct drm_flip_work *work,
  131. const char *name, drm_flip_func_t func)
  132. {
  133. work->name = name;
  134. INIT_LIST_HEAD(&work->queued);
  135. INIT_LIST_HEAD(&work->commited);
  136. spin_lock_init(&work->lock);
  137. work->func = func;
  138. INIT_WORK(&work->worker, flip_worker);
  139. }
  140. EXPORT_SYMBOL(drm_flip_work_init);
  141. /**
  142. * drm_flip_work_cleanup - cleans up flip-work
  143. * @work: the flip-work to cleanup
  144. *
  145. * Destroy resources allocated for the flip-work
  146. */
  147. void drm_flip_work_cleanup(struct drm_flip_work *work)
  148. {
  149. WARN_ON(!list_empty(&work->queued) || !list_empty(&work->commited));
  150. }
  151. EXPORT_SYMBOL(drm_flip_work_cleanup);