amdgpu_sched.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2015 Advanced Micro Devices, Inc.
  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 shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. *
  23. */
  24. #include <linux/kthread.h>
  25. #include <linux/wait.h>
  26. #include <linux/sched.h>
  27. #include <drm/drmP.h>
  28. #include "amdgpu.h"
  29. static struct fence *amdgpu_sched_dependency(struct amd_sched_job *job)
  30. {
  31. struct amdgpu_job *sched_job = (struct amdgpu_job *)job;
  32. return amdgpu_sync_get_fence(&sched_job->ibs->sync);
  33. }
  34. static struct fence *amdgpu_sched_run_job(struct amd_sched_job *job)
  35. {
  36. struct amdgpu_job *sched_job;
  37. struct amdgpu_fence *fence;
  38. int r;
  39. if (!job) {
  40. DRM_ERROR("job is null\n");
  41. return NULL;
  42. }
  43. sched_job = (struct amdgpu_job *)job;
  44. mutex_lock(&sched_job->job_lock);
  45. r = amdgpu_ib_schedule(sched_job->adev,
  46. sched_job->num_ibs,
  47. sched_job->ibs,
  48. sched_job->base.owner);
  49. if (r)
  50. goto err;
  51. fence = amdgpu_fence_ref(sched_job->ibs[sched_job->num_ibs - 1].fence);
  52. if (sched_job->free_job)
  53. sched_job->free_job(sched_job);
  54. mutex_unlock(&sched_job->job_lock);
  55. return &fence->base;
  56. err:
  57. DRM_ERROR("Run job error\n");
  58. mutex_unlock(&sched_job->job_lock);
  59. job->sched->ops->process_job(job);
  60. return NULL;
  61. }
  62. static void amdgpu_sched_process_job(struct amd_sched_job *job)
  63. {
  64. struct amdgpu_job *sched_job;
  65. if (!job) {
  66. DRM_ERROR("job is null\n");
  67. return;
  68. }
  69. sched_job = (struct amdgpu_job *)job;
  70. /* after processing job, free memory */
  71. fence_put(&sched_job->base.s_fence->base);
  72. kfree(sched_job);
  73. }
  74. struct amd_sched_backend_ops amdgpu_sched_ops = {
  75. .dependency = amdgpu_sched_dependency,
  76. .run_job = amdgpu_sched_run_job,
  77. .process_job = amdgpu_sched_process_job
  78. };
  79. int amdgpu_sched_ib_submit_kernel_helper(struct amdgpu_device *adev,
  80. struct amdgpu_ring *ring,
  81. struct amdgpu_ib *ibs,
  82. unsigned num_ibs,
  83. int (*free_job)(struct amdgpu_job *),
  84. void *owner,
  85. struct fence **f)
  86. {
  87. int r = 0;
  88. if (amdgpu_enable_scheduler) {
  89. struct amdgpu_job *job =
  90. kzalloc(sizeof(struct amdgpu_job), GFP_KERNEL);
  91. if (!job)
  92. return -ENOMEM;
  93. job->base.sched = ring->scheduler;
  94. job->base.s_entity = &adev->kernel_ctx.rings[ring->idx].entity;
  95. job->adev = adev;
  96. job->ibs = ibs;
  97. job->num_ibs = num_ibs;
  98. job->base.owner = owner;
  99. mutex_init(&job->job_lock);
  100. job->free_job = free_job;
  101. mutex_lock(&job->job_lock);
  102. r = amd_sched_entity_push_job((struct amd_sched_job *)job);
  103. if (r) {
  104. mutex_unlock(&job->job_lock);
  105. kfree(job);
  106. return r;
  107. }
  108. *f = fence_get(&job->base.s_fence->base);
  109. mutex_unlock(&job->job_lock);
  110. } else {
  111. r = amdgpu_ib_schedule(adev, num_ibs, ibs, owner);
  112. if (r)
  113. return r;
  114. *f = fence_get(&ibs[num_ibs - 1].fence->base);
  115. }
  116. return 0;
  117. }