drm_flip_work.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "drmP.h"
  24. #include "drm_flip_work.h"
  25. /**
  26. * drm_flip_work_queue - queue work
  27. * @work: the flip-work
  28. * @val: the value to queue
  29. *
  30. * Queues work, that will later be run (passed back to drm_flip_func_t
  31. * func) on a work queue after drm_flip_work_commit() is called.
  32. */
  33. void drm_flip_work_queue(struct drm_flip_work *work, void *val)
  34. {
  35. if (kfifo_put(&work->fifo, (const void **)&val)) {
  36. atomic_inc(&work->pending);
  37. } else {
  38. DRM_ERROR("%s fifo full!\n", work->name);
  39. work->func(work, val);
  40. }
  41. }
  42. EXPORT_SYMBOL(drm_flip_work_queue);
  43. /**
  44. * drm_flip_work_commit - commit queued work
  45. * @work: the flip-work
  46. * @wq: the work-queue to run the queued work on
  47. *
  48. * Trigger work previously queued by drm_flip_work_queue() to run
  49. * on a workqueue. The typical usage would be to queue work (via
  50. * drm_flip_work_queue()) at any point (from vblank irq and/or
  51. * prior), and then from vblank irq commit the queued work.
  52. */
  53. void drm_flip_work_commit(struct drm_flip_work *work,
  54. struct workqueue_struct *wq)
  55. {
  56. uint32_t pending = atomic_read(&work->pending);
  57. atomic_add(pending, &work->count);
  58. atomic_sub(pending, &work->pending);
  59. queue_work(wq, &work->worker);
  60. }
  61. EXPORT_SYMBOL(drm_flip_work_commit);
  62. static void flip_worker(struct work_struct *w)
  63. {
  64. struct drm_flip_work *work = container_of(w, struct drm_flip_work, worker);
  65. uint32_t count = atomic_read(&work->count);
  66. void *val = NULL;
  67. atomic_sub(count, &work->count);
  68. while(count--)
  69. if (!WARN_ON(!kfifo_get(&work->fifo, &val)))
  70. work->func(work, val);
  71. }
  72. /**
  73. * drm_flip_work_init - initialize flip-work
  74. * @work: the flip-work to initialize
  75. * @size: the max queue depth
  76. * @name: debug name
  77. * @func: the callback work function
  78. *
  79. * Initializes/allocates resources for the flip-work
  80. *
  81. * RETURNS:
  82. * Zero on success, error code on failure.
  83. */
  84. int drm_flip_work_init(struct drm_flip_work *work, int size,
  85. const char *name, drm_flip_func_t func)
  86. {
  87. int ret;
  88. work->name = name;
  89. atomic_set(&work->count, 0);
  90. atomic_set(&work->pending, 0);
  91. work->func = func;
  92. ret = kfifo_alloc(&work->fifo, size, GFP_KERNEL);
  93. if (ret) {
  94. DRM_ERROR("could not allocate %s fifo\n", name);
  95. return ret;
  96. }
  97. INIT_WORK(&work->worker, flip_worker);
  98. return 0;
  99. }
  100. EXPORT_SYMBOL(drm_flip_work_init);
  101. /**
  102. * drm_flip_work_cleanup - cleans up flip-work
  103. * @work: the flip-work to cleanup
  104. *
  105. * Destroy resources allocated for the flip-work
  106. */
  107. void drm_flip_work_cleanup(struct drm_flip_work *work)
  108. {
  109. WARN_ON(!kfifo_is_empty(&work->fifo));
  110. kfifo_free(&work->fifo);
  111. }
  112. EXPORT_SYMBOL(drm_flip_work_cleanup);