drm_flip_work.c 4.4 KB

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