radeon_sync.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright 2014 Advanced Micro Devices, Inc.
  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 <[email protected]>
  29. */
  30. #include "radeon.h"
  31. #include "radeon_trace.h"
  32. /**
  33. * radeon_sync_create - zero init sync object
  34. *
  35. * @sync: sync object to initialize
  36. *
  37. * Just clear the sync object for now.
  38. */
  39. void radeon_sync_create(struct radeon_sync *sync)
  40. {
  41. unsigned i;
  42. for (i = 0; i < RADEON_NUM_SYNCS; ++i)
  43. sync->semaphores[i] = NULL;
  44. for (i = 0; i < RADEON_NUM_RINGS; ++i)
  45. sync->sync_to[i] = NULL;
  46. sync->last_vm_update = NULL;
  47. }
  48. /**
  49. * radeon_sync_fence - use the semaphore to sync to a fence
  50. *
  51. * @sync: sync object to add fence to
  52. * @fence: fence to sync to
  53. *
  54. * Sync to the fence using the semaphore objects
  55. */
  56. void radeon_sync_fence(struct radeon_sync *sync,
  57. struct radeon_fence *fence)
  58. {
  59. struct radeon_fence *other;
  60. if (!fence)
  61. return;
  62. other = sync->sync_to[fence->ring];
  63. sync->sync_to[fence->ring] = radeon_fence_later(fence, other);
  64. if (fence->is_vm_update) {
  65. other = sync->last_vm_update;
  66. sync->last_vm_update = radeon_fence_later(fence, other);
  67. }
  68. }
  69. /**
  70. * radeon_sync_resv - use the semaphores to sync to a reservation object
  71. *
  72. * @rdev: radeon_device pointer
  73. * @sync: sync object to add fences from reservation object to
  74. * @resv: reservation object with embedded fence
  75. * @shared: true if we should only sync to the exclusive fence
  76. *
  77. * Sync to the fence using the semaphore objects
  78. */
  79. int radeon_sync_resv(struct radeon_device *rdev,
  80. struct radeon_sync *sync,
  81. struct dma_resv *resv,
  82. bool shared)
  83. {
  84. struct dma_resv_iter cursor;
  85. struct radeon_fence *fence;
  86. struct dma_fence *f;
  87. int r = 0;
  88. dma_resv_for_each_fence(&cursor, resv, dma_resv_usage_rw(!shared), f) {
  89. fence = to_radeon_fence(f);
  90. if (fence && fence->rdev == rdev)
  91. radeon_sync_fence(sync, fence);
  92. else
  93. r = dma_fence_wait(f, true);
  94. if (r)
  95. break;
  96. }
  97. return r;
  98. }
  99. /**
  100. * radeon_sync_rings - sync ring to all registered fences
  101. *
  102. * @rdev: radeon_device pointer
  103. * @sync: sync object to use
  104. * @ring: ring that needs sync
  105. *
  106. * Ensure that all registered fences are signaled before letting
  107. * the ring continue. The caller must hold the ring lock.
  108. */
  109. int radeon_sync_rings(struct radeon_device *rdev,
  110. struct radeon_sync *sync,
  111. int ring)
  112. {
  113. unsigned count = 0;
  114. int i, r;
  115. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  116. struct radeon_fence *fence = sync->sync_to[i];
  117. struct radeon_semaphore *semaphore;
  118. /* check if we really need to sync */
  119. if (!radeon_fence_need_sync(fence, ring))
  120. continue;
  121. /* prevent GPU deadlocks */
  122. if (!rdev->ring[i].ready) {
  123. dev_err(rdev->dev, "Syncing to a disabled ring!");
  124. return -EINVAL;
  125. }
  126. if (count >= RADEON_NUM_SYNCS) {
  127. /* not enough room, wait manually */
  128. r = radeon_fence_wait(fence, false);
  129. if (r)
  130. return r;
  131. continue;
  132. }
  133. r = radeon_semaphore_create(rdev, &semaphore);
  134. if (r)
  135. return r;
  136. sync->semaphores[count++] = semaphore;
  137. /* allocate enough space for sync command */
  138. r = radeon_ring_alloc(rdev, &rdev->ring[i], 16);
  139. if (r)
  140. return r;
  141. /* emit the signal semaphore */
  142. if (!radeon_semaphore_emit_signal(rdev, i, semaphore)) {
  143. /* signaling wasn't successful wait manually */
  144. radeon_ring_undo(&rdev->ring[i]);
  145. r = radeon_fence_wait(fence, false);
  146. if (r)
  147. return r;
  148. continue;
  149. }
  150. /* we assume caller has already allocated space on waiters ring */
  151. if (!radeon_semaphore_emit_wait(rdev, ring, semaphore)) {
  152. /* waiting wasn't successful wait manually */
  153. radeon_ring_undo(&rdev->ring[i]);
  154. r = radeon_fence_wait(fence, false);
  155. if (r)
  156. return r;
  157. continue;
  158. }
  159. radeon_ring_commit(rdev, &rdev->ring[i], false);
  160. radeon_fence_note_sync(fence, ring);
  161. }
  162. return 0;
  163. }
  164. /**
  165. * radeon_sync_free - free the sync object
  166. *
  167. * @rdev: radeon_device pointer
  168. * @sync: sync object to use
  169. * @fence: fence to use for the free
  170. *
  171. * Free the sync object by freeing all semaphores in it.
  172. */
  173. void radeon_sync_free(struct radeon_device *rdev,
  174. struct radeon_sync *sync,
  175. struct radeon_fence *fence)
  176. {
  177. unsigned i;
  178. for (i = 0; i < RADEON_NUM_SYNCS; ++i)
  179. radeon_semaphore_free(rdev, &sync->semaphores[i], fence);
  180. }