radeon_semaphore.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "drmP.h"
  31. #include "drm.h"
  32. #include "radeon.h"
  33. int radeon_semaphore_create(struct radeon_device *rdev,
  34. struct radeon_semaphore **semaphore)
  35. {
  36. int 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. return 0;
  52. }
  53. void radeon_semaphore_emit_signal(struct radeon_device *rdev, int ring,
  54. struct radeon_semaphore *semaphore)
  55. {
  56. --semaphore->waiters;
  57. radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, false);
  58. }
  59. void radeon_semaphore_emit_wait(struct radeon_device *rdev, int ring,
  60. struct radeon_semaphore *semaphore)
  61. {
  62. ++semaphore->waiters;
  63. radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, true);
  64. }
  65. int radeon_semaphore_sync_rings(struct radeon_device *rdev,
  66. struct radeon_semaphore *semaphore,
  67. bool sync_to[RADEON_NUM_RINGS],
  68. int dst_ring)
  69. {
  70. int i = 0, r;
  71. mutex_lock(&rdev->ring_lock);
  72. r = radeon_ring_alloc(rdev, &rdev->ring[dst_ring], RADEON_NUM_RINGS * 8);
  73. if (r) {
  74. goto error;
  75. }
  76. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  77. /* no need to sync to our own or unused rings */
  78. if (!sync_to[i] || i == dst_ring)
  79. continue;
  80. /* prevent GPU deadlocks */
  81. if (!rdev->ring[i].ready) {
  82. dev_err(rdev->dev, "Trying to sync to a disabled ring!");
  83. r = -EINVAL;
  84. goto error;
  85. }
  86. r = radeon_ring_alloc(rdev, &rdev->ring[i], 8);
  87. if (r) {
  88. goto error;
  89. }
  90. radeon_semaphore_emit_signal(rdev, i, semaphore);
  91. radeon_semaphore_emit_wait(rdev, dst_ring, semaphore);
  92. radeon_ring_commit(rdev, &rdev->ring[i]);
  93. }
  94. radeon_ring_commit(rdev, &rdev->ring[dst_ring]);
  95. mutex_unlock(&rdev->ring_lock);
  96. return 0;
  97. error:
  98. /* unlock all locks taken so far */
  99. for (--i; i >= 0; --i) {
  100. if (sync_to[i] || i == dst_ring) {
  101. radeon_ring_undo(&rdev->ring[i]);
  102. }
  103. }
  104. radeon_ring_undo(&rdev->ring[dst_ring]);
  105. mutex_unlock(&rdev->ring_lock);
  106. return r;
  107. }
  108. void radeon_semaphore_free(struct radeon_device *rdev,
  109. struct radeon_semaphore *semaphore,
  110. struct radeon_fence *fence)
  111. {
  112. if (semaphore == NULL) {
  113. return;
  114. }
  115. if (semaphore->waiters > 0) {
  116. dev_err(rdev->dev, "semaphore %p has more waiters than signalers,"
  117. " hardware lockup imminent!\n", semaphore);
  118. }
  119. radeon_sa_bo_free(rdev, &semaphore->sa_bo, fence);
  120. kfree(semaphore);
  121. }