radeon_semaphore.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. uint32_t *cpu_addr;
  37. int i, r;
  38. *semaphore = kmalloc(sizeof(struct radeon_semaphore), GFP_KERNEL);
  39. if (*semaphore == NULL) {
  40. return -ENOMEM;
  41. }
  42. r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, &(*semaphore)->sa_bo,
  43. 8 * RADEON_NUM_SYNCS, 8);
  44. if (r) {
  45. kfree(*semaphore);
  46. *semaphore = NULL;
  47. return r;
  48. }
  49. (*semaphore)->waiters = 0;
  50. (*semaphore)->gpu_addr = radeon_sa_bo_gpu_addr((*semaphore)->sa_bo);
  51. cpu_addr = radeon_sa_bo_cpu_addr((*semaphore)->sa_bo);
  52. for (i = 0; i < RADEON_NUM_SYNCS; ++i)
  53. cpu_addr[i] = 0;
  54. for (i = 0; i < RADEON_NUM_RINGS; ++i)
  55. (*semaphore)->sync_to[i] = NULL;
  56. return 0;
  57. }
  58. bool radeon_semaphore_emit_signal(struct radeon_device *rdev, int ridx,
  59. struct radeon_semaphore *semaphore)
  60. {
  61. struct radeon_ring *ring = &rdev->ring[ridx];
  62. trace_radeon_semaphore_signale(ridx, semaphore);
  63. if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, false)) {
  64. --semaphore->waiters;
  65. /* for debugging lockup only, used by sysfs debug files */
  66. ring->last_semaphore_signal_addr = semaphore->gpu_addr;
  67. return true;
  68. }
  69. return false;
  70. }
  71. bool radeon_semaphore_emit_wait(struct radeon_device *rdev, int ridx,
  72. struct radeon_semaphore *semaphore)
  73. {
  74. struct radeon_ring *ring = &rdev->ring[ridx];
  75. trace_radeon_semaphore_wait(ridx, semaphore);
  76. if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, true)) {
  77. ++semaphore->waiters;
  78. /* for debugging lockup only, used by sysfs debug files */
  79. ring->last_semaphore_wait_addr = semaphore->gpu_addr;
  80. return true;
  81. }
  82. return false;
  83. }
  84. /**
  85. * radeon_semaphore_sync_to - use the semaphore to sync to a fence
  86. *
  87. * @semaphore: semaphore object to add fence to
  88. * @fence: fence to sync to
  89. *
  90. * Sync to the fence using this semaphore object
  91. */
  92. void radeon_semaphore_sync_to(struct radeon_semaphore *semaphore,
  93. struct radeon_fence *fence)
  94. {
  95. struct radeon_fence *other;
  96. if (!fence)
  97. return;
  98. other = semaphore->sync_to[fence->ring];
  99. semaphore->sync_to[fence->ring] = radeon_fence_later(fence, other);
  100. }
  101. /**
  102. * radeon_semaphore_sync_rings - sync ring to all registered fences
  103. *
  104. * @rdev: radeon_device pointer
  105. * @semaphore: semaphore object to use for sync
  106. * @ring: ring that needs sync
  107. *
  108. * Ensure that all registered fences are signaled before letting
  109. * the ring continue. The caller must hold the ring lock.
  110. */
  111. int radeon_semaphore_sync_rings(struct radeon_device *rdev,
  112. struct radeon_semaphore *semaphore,
  113. int ring)
  114. {
  115. unsigned count = 0;
  116. int i, r;
  117. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  118. struct radeon_fence *fence = semaphore->sync_to[i];
  119. /* check if we really need to sync */
  120. if (!radeon_fence_need_sync(fence, ring))
  121. continue;
  122. /* prevent GPU deadlocks */
  123. if (!rdev->ring[i].ready) {
  124. dev_err(rdev->dev, "Syncing to a disabled ring!");
  125. return -EINVAL;
  126. }
  127. if (++count > RADEON_NUM_SYNCS) {
  128. /* not enough room, wait manually */
  129. r = radeon_fence_wait(fence, false);
  130. if (r)
  131. return r;
  132. continue;
  133. }
  134. /* allocate enough space for sync command */
  135. r = radeon_ring_alloc(rdev, &rdev->ring[i], 16);
  136. if (r) {
  137. return r;
  138. }
  139. /* emit the signal semaphore */
  140. if (!radeon_semaphore_emit_signal(rdev, i, semaphore)) {
  141. /* signaling wasn't successful wait manually */
  142. radeon_ring_undo(&rdev->ring[i]);
  143. r = radeon_fence_wait(fence, false);
  144. if (r)
  145. return r;
  146. continue;
  147. }
  148. /* we assume caller has already allocated space on waiters ring */
  149. if (!radeon_semaphore_emit_wait(rdev, ring, semaphore)) {
  150. /* waiting wasn't successful wait manually */
  151. radeon_ring_undo(&rdev->ring[i]);
  152. r = radeon_fence_wait(fence, false);
  153. if (r)
  154. return r;
  155. continue;
  156. }
  157. radeon_ring_commit(rdev, &rdev->ring[i]);
  158. radeon_fence_note_sync(fence, ring);
  159. semaphore->gpu_addr += 8;
  160. }
  161. return 0;
  162. }
  163. void radeon_semaphore_free(struct radeon_device *rdev,
  164. struct radeon_semaphore **semaphore,
  165. struct radeon_fence *fence)
  166. {
  167. if (semaphore == NULL || *semaphore == NULL) {
  168. return;
  169. }
  170. if ((*semaphore)->waiters > 0) {
  171. dev_err(rdev->dev, "semaphore %p has more waiters than signalers,"
  172. " hardware lockup imminent!\n", *semaphore);
  173. }
  174. radeon_sa_bo_free(rdev, &(*semaphore)->sa_bo, fence);
  175. kfree(*semaphore);
  176. *semaphore = NULL;
  177. }