drm_flip_work.c 4.8 KB

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