drm_flip_work.c 4.4 KB

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