radeon_semaphore.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright 2011 Christian König.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  16. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  17. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  18. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  19. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * The above copyright notice and this permission notice (including the
  22. * next paragraph) shall be included in all copies or substantial portions
  23. * of the Software.
  24. *
  25. */
  26. /*
  27. * Authors:
  28. * Christian König <deathsimple@vodafone.de>
  29. */
  30. #include <drm/drmP.h>
  31. #include "radeon.h"
  32. #include "radeon_trace.h"
  33. int radeon_semaphore_create(struct radeon_device *rdev,
  34. struct radeon_semaphore **semaphore)
  35. {
  36. int i, r;
  37. *semaphore = kmalloc(sizeof(struct radeon_semaphore), GFP_KERNEL);
  38. if (*semaphore == NULL) {
  39. return -ENOMEM;
  40. }
  41. r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo,
  42. &(*semaphore)->sa_bo, 8, 8, true);
  43. if (r) {
  44. kfree(*semaphore);
  45. *semaphore = NULL;
  46. return r;
  47. }
  48. (*semaphore)->waiters = 0;
  49. (*semaphore)->gpu_addr = radeon_sa_bo_gpu_addr((*semaphore)->sa_bo);
  50. *((uint64_t*)radeon_sa_bo_cpu_addr((*semaphore)->sa_bo)) = 0;
  51. for (i = 0; i < RADEON_NUM_RINGS; ++i)
  52. (*semaphore)->sync_to[i] = NULL;
  53. return 0;
  54. }
  55. bool radeon_semaphore_emit_signal(struct radeon_device *rdev, int ridx,
  56. struct radeon_semaphore *semaphore)
  57. {
  58. struct radeon_ring *ring = &rdev->ring[ridx];
  59. trace_radeon_semaphore_signale(ridx, semaphore);
  60. if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, false)) {
  61. --semaphore->waiters;
  62. /* for debugging lockup only, used by sysfs debug files */
  63. ring->last_semaphore_signal_addr = semaphore->gpu_addr;
  64. return true;
  65. }
  66. return false;
  67. }
  68. bool radeon_semaphore_emit_wait(struct radeon_device *rdev, int ridx,
  69. struct radeon_semaphore *semaphore)
  70. {
  71. struct radeon_ring *ring = &rdev->ring[ridx];
  72. trace_radeon_semaphore_wait(ridx, semaphore);
  73. if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, true)) {
  74. ++semaphore->waiters;
  75. /* for debugging lockup only, used by sysfs debug files */
  76. ring->last_semaphore_wait_addr = semaphore->gpu_addr;
  77. return true;
  78. }
  79. return false;
  80. }
  81. /**
  82. * radeon_semaphore_sync_to - use the semaphore to sync to a fence
  83. *
  84. * @semaphore: semaphore object to add fence to
  85. * @fence: fence to sync to
  86. *
  87. * Sync to the fence using this semaphore object
  88. */
  89. void radeon_semaphore_sync_to(struct radeon_semaphore *semaphore,
  90. struct radeon_fence *fence)
  91. {
  92. struct radeon_fence *other;
  93. if (!fence)
  94. return;
  95. other = semaphore->sync_to[fence->ring];
  96. semaphore->sync_to[fence->ring] = radeon_fence_later(fence, other);
  97. }
  98. /**
  99. * radeon_semaphore_sync_rings - sync ring to all registered fences
  100. *
  101. * @rdev: radeon_device pointer
  102. * @semaphore: semaphore object to use for sync
  103. * @ring: ring that needs sync
  104. *
  105. * Ensure that all registered fences are signaled before letting
  106. * the ring continue. The caller must hold the ring lock.
  107. */
  108. int radeon_semaphore_sync_rings(struct radeon_device *rdev,
  109. struct radeon_semaphore *semaphore,
  110. int ring)
  111. {
  112. int i, r;
  113. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  114. struct radeon_fence *fence = semaphore->sync_to[i];
  115. /* check if we really need to sync */
  116. if (!radeon_fence_need_sync(fence, ring))
  117. continue;
  118. /* prevent GPU deadlocks */
  119. if (!rdev->ring[i].ready) {
  120. dev_err(rdev->dev, "Syncing to a disabled ring!");
  121. return -EINVAL;
  122. }
  123. /* allocate enough space for sync command */
  124. r = radeon_ring_alloc(rdev, &rdev->ring[i], 16);
  125. if (r) {
  126. return r;
  127. }
  128. /* emit the signal semaphore */
  129. if (!radeon_semaphore_emit_signal(rdev, i, semaphore)) {
  130. /* signaling wasn't successful wait manually */
  131. radeon_ring_undo(&rdev->ring[i]);
  132. radeon_fence_wait_locked(fence);
  133. continue;
  134. }
  135. /* we assume caller has already allocated space on waiters ring */
  136. if (!radeon_semaphore_emit_wait(rdev, ring, semaphore)) {
  137. /* waiting wasn't successful wait manually */
  138. radeon_ring_undo(&rdev->ring[i]);
  139. radeon_fence_wait_locked(fence);
  140. continue;
  141. }
  142. radeon_ring_commit(rdev, &rdev->ring[i]);
  143. radeon_fence_note_sync(fence, ring);
  144. }
  145. return 0;
  146. }
  147. void radeon_semaphore_free(struct radeon_device *rdev,
  148. struct radeon_semaphore **semaphore,
  149. struct radeon_fence *fence)
  150. {
  151. if (semaphore == NULL || *semaphore == NULL) {
  152. return;
  153. }
  154. if ((*semaphore)->waiters > 0) {
  155. dev_err(rdev->dev, "semaphore %p has more waiters than signalers,"
  156. " hardware lockup imminent!\n", *semaphore);
  157. }
  158. radeon_sa_bo_free(rdev, &(*semaphore)->sa_bo, fence);
  159. kfree(*semaphore);
  160. *semaphore = NULL;
  161. }