1 1.1 riastrad /* $NetBSD: amdgpu_job.c,v 1.2 2021/12/18 23:44:58 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2015 Advanced Micro Devices, Inc. 5 1.1 riastrad * 6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 7 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 8 1.1 riastrad * to deal in the Software without restriction, including without limitation 9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 11 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 12 1.1 riastrad * 13 1.1 riastrad * The above copyright notice and this permission notice shall be included in 14 1.1 riastrad * all copies or substantial portions of the Software. 15 1.1 riastrad * 16 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 1.1 riastrad * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 23 1.1 riastrad * 24 1.1 riastrad * 25 1.1 riastrad */ 26 1.1 riastrad #include <sys/cdefs.h> 27 1.1 riastrad __KERNEL_RCSID(0, "$NetBSD: amdgpu_job.c,v 1.2 2021/12/18 23:44:58 riastradh Exp $"); 28 1.1 riastrad 29 1.1 riastrad #include <linux/kthread.h> 30 1.1 riastrad #include <linux/wait.h> 31 1.1 riastrad #include <linux/sched.h> 32 1.1 riastrad 33 1.1 riastrad #include "amdgpu.h" 34 1.1 riastrad #include "amdgpu_trace.h" 35 1.1 riastrad 36 1.1 riastrad static void amdgpu_job_timedout(struct drm_sched_job *s_job) 37 1.1 riastrad { 38 1.1 riastrad struct amdgpu_ring *ring = to_amdgpu_ring(s_job->sched); 39 1.1 riastrad struct amdgpu_job *job = to_amdgpu_job(s_job); 40 1.1 riastrad struct amdgpu_task_info ti; 41 1.1 riastrad 42 1.1 riastrad memset(&ti, 0, sizeof(struct amdgpu_task_info)); 43 1.1 riastrad 44 1.1 riastrad if (amdgpu_ring_soft_recovery(ring, job->vmid, s_job->s_fence->parent)) { 45 1.1 riastrad DRM_ERROR("ring %s timeout, but soft recovered\n", 46 1.1 riastrad s_job->sched->name); 47 1.1 riastrad return; 48 1.1 riastrad } 49 1.1 riastrad 50 1.1 riastrad amdgpu_vm_get_task_info(ring->adev, job->pasid, &ti); 51 1.1 riastrad DRM_ERROR("ring %s timeout, signaled seq=%u, emitted seq=%u\n", 52 1.1 riastrad job->base.sched->name, atomic_read(&ring->fence_drv.last_seq), 53 1.1 riastrad ring->fence_drv.sync_seq); 54 1.1 riastrad DRM_ERROR("Process information: process %s pid %d thread %s pid %d\n", 55 1.1 riastrad ti.process_name, ti.tgid, ti.task_name, ti.pid); 56 1.1 riastrad 57 1.1 riastrad if (amdgpu_device_should_recover_gpu(ring->adev)) 58 1.1 riastrad amdgpu_device_gpu_recover(ring->adev, job); 59 1.1 riastrad else 60 1.1 riastrad drm_sched_suspend_timeout(&ring->sched); 61 1.1 riastrad } 62 1.1 riastrad 63 1.1 riastrad int amdgpu_job_alloc(struct amdgpu_device *adev, unsigned num_ibs, 64 1.1 riastrad struct amdgpu_job **job, struct amdgpu_vm *vm) 65 1.1 riastrad { 66 1.1 riastrad size_t size = sizeof(struct amdgpu_job); 67 1.1 riastrad 68 1.1 riastrad if (num_ibs == 0) 69 1.1 riastrad return -EINVAL; 70 1.1 riastrad 71 1.1 riastrad size += sizeof(struct amdgpu_ib) * num_ibs; 72 1.1 riastrad 73 1.1 riastrad *job = kzalloc(size, GFP_KERNEL); 74 1.1 riastrad if (!*job) 75 1.1 riastrad return -ENOMEM; 76 1.1 riastrad 77 1.1 riastrad /* 78 1.1 riastrad * Initialize the scheduler to at least some ring so that we always 79 1.1 riastrad * have a pointer to adev. 80 1.1 riastrad */ 81 1.1 riastrad (*job)->base.sched = &adev->rings[0]->sched; 82 1.1 riastrad (*job)->vm = vm; 83 1.1 riastrad (*job)->ibs = (void *)&(*job)[1]; 84 1.1 riastrad (*job)->num_ibs = num_ibs; 85 1.1 riastrad 86 1.1 riastrad amdgpu_sync_create(&(*job)->sync); 87 1.1 riastrad amdgpu_sync_create(&(*job)->sched_sync); 88 1.1 riastrad (*job)->vram_lost_counter = atomic_read(&adev->vram_lost_counter); 89 1.1 riastrad (*job)->vm_pd_addr = AMDGPU_BO_INVALID_OFFSET; 90 1.1 riastrad 91 1.1 riastrad return 0; 92 1.1 riastrad } 93 1.1 riastrad 94 1.1 riastrad int amdgpu_job_alloc_with_ib(struct amdgpu_device *adev, unsigned size, 95 1.1 riastrad struct amdgpu_job **job) 96 1.1 riastrad { 97 1.1 riastrad int r; 98 1.1 riastrad 99 1.1 riastrad r = amdgpu_job_alloc(adev, 1, job, NULL); 100 1.1 riastrad if (r) 101 1.1 riastrad return r; 102 1.1 riastrad 103 1.1 riastrad r = amdgpu_ib_get(adev, NULL, size, &(*job)->ibs[0]); 104 1.1 riastrad if (r) 105 1.1 riastrad kfree(*job); 106 1.1 riastrad 107 1.1 riastrad return r; 108 1.1 riastrad } 109 1.1 riastrad 110 1.1 riastrad void amdgpu_job_free_resources(struct amdgpu_job *job) 111 1.1 riastrad { 112 1.1 riastrad struct amdgpu_ring *ring = to_amdgpu_ring(job->base.sched); 113 1.1 riastrad struct dma_fence *f; 114 1.1 riastrad unsigned i; 115 1.1 riastrad 116 1.1 riastrad /* use sched fence if available */ 117 1.1 riastrad f = job->base.s_fence ? &job->base.s_fence->finished : job->fence; 118 1.1 riastrad 119 1.1 riastrad for (i = 0; i < job->num_ibs; ++i) 120 1.1 riastrad amdgpu_ib_free(ring->adev, &job->ibs[i], f); 121 1.1 riastrad } 122 1.1 riastrad 123 1.1 riastrad static void amdgpu_job_free_cb(struct drm_sched_job *s_job) 124 1.1 riastrad { 125 1.1 riastrad struct amdgpu_ring *ring = to_amdgpu_ring(s_job->sched); 126 1.1 riastrad struct amdgpu_job *job = to_amdgpu_job(s_job); 127 1.1 riastrad 128 1.1 riastrad drm_sched_job_cleanup(s_job); 129 1.1 riastrad 130 1.1 riastrad amdgpu_ring_priority_put(ring, s_job->s_priority); 131 1.1 riastrad dma_fence_put(job->fence); 132 1.1 riastrad amdgpu_sync_free(&job->sync); 133 1.1 riastrad amdgpu_sync_free(&job->sched_sync); 134 1.1 riastrad kfree(job); 135 1.1 riastrad } 136 1.1 riastrad 137 1.1 riastrad void amdgpu_job_free(struct amdgpu_job *job) 138 1.1 riastrad { 139 1.1 riastrad amdgpu_job_free_resources(job); 140 1.1 riastrad 141 1.1 riastrad dma_fence_put(job->fence); 142 1.1 riastrad amdgpu_sync_free(&job->sync); 143 1.1 riastrad amdgpu_sync_free(&job->sched_sync); 144 1.1 riastrad kfree(job); 145 1.1 riastrad } 146 1.1 riastrad 147 1.1 riastrad int amdgpu_job_submit(struct amdgpu_job *job, struct drm_sched_entity *entity, 148 1.1 riastrad void *owner, struct dma_fence **f) 149 1.1 riastrad { 150 1.1 riastrad enum drm_sched_priority priority; 151 1.1 riastrad struct amdgpu_ring *ring; 152 1.1 riastrad int r; 153 1.1 riastrad 154 1.1 riastrad if (!f) 155 1.1 riastrad return -EINVAL; 156 1.1 riastrad 157 1.1 riastrad r = drm_sched_job_init(&job->base, entity, owner); 158 1.1 riastrad if (r) 159 1.1 riastrad return r; 160 1.1 riastrad 161 1.1 riastrad *f = dma_fence_get(&job->base.s_fence->finished); 162 1.1 riastrad amdgpu_job_free_resources(job); 163 1.1 riastrad priority = job->base.s_priority; 164 1.1 riastrad drm_sched_entity_push_job(&job->base, entity); 165 1.1 riastrad 166 1.1 riastrad ring = to_amdgpu_ring(entity->rq->sched); 167 1.1 riastrad amdgpu_ring_priority_get(ring, priority); 168 1.1 riastrad 169 1.1 riastrad return 0; 170 1.1 riastrad } 171 1.1 riastrad 172 1.1 riastrad int amdgpu_job_submit_direct(struct amdgpu_job *job, struct amdgpu_ring *ring, 173 1.1 riastrad struct dma_fence **fence) 174 1.1 riastrad { 175 1.1 riastrad int r; 176 1.1 riastrad 177 1.1 riastrad job->base.sched = &ring->sched; 178 1.1 riastrad r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs, NULL, fence); 179 1.1 riastrad job->fence = dma_fence_get(*fence); 180 1.1 riastrad if (r) 181 1.1 riastrad return r; 182 1.1 riastrad 183 1.1 riastrad amdgpu_job_free(job); 184 1.1 riastrad return 0; 185 1.1 riastrad } 186 1.1 riastrad 187 1.1 riastrad static struct dma_fence *amdgpu_job_dependency(struct drm_sched_job *sched_job, 188 1.1 riastrad struct drm_sched_entity *s_entity) 189 1.1 riastrad { 190 1.1 riastrad struct amdgpu_ring *ring = to_amdgpu_ring(s_entity->rq->sched); 191 1.1 riastrad struct amdgpu_job *job = to_amdgpu_job(sched_job); 192 1.1 riastrad struct amdgpu_vm *vm = job->vm; 193 1.1 riastrad struct dma_fence *fence; 194 1.1 riastrad bool explicit = false; 195 1.1 riastrad int r; 196 1.1 riastrad 197 1.1 riastrad fence = amdgpu_sync_get_fence(&job->sync, &explicit); 198 1.1 riastrad if (fence && explicit) { 199 1.1 riastrad if (drm_sched_dependency_optimized(fence, s_entity)) { 200 1.1 riastrad r = amdgpu_sync_fence(&job->sched_sync, fence, false); 201 1.1 riastrad if (r) 202 1.1 riastrad DRM_ERROR("Error adding fence (%d)\n", r); 203 1.1 riastrad } 204 1.1 riastrad } 205 1.1 riastrad 206 1.1 riastrad while (fence == NULL && vm && !job->vmid) { 207 1.1 riastrad r = amdgpu_vmid_grab(vm, ring, &job->sync, 208 1.1 riastrad &job->base.s_fence->finished, 209 1.1 riastrad job); 210 1.1 riastrad if (r) 211 1.1 riastrad DRM_ERROR("Error getting VM ID (%d)\n", r); 212 1.1 riastrad 213 1.1 riastrad fence = amdgpu_sync_get_fence(&job->sync, NULL); 214 1.1 riastrad } 215 1.1 riastrad 216 1.1 riastrad return fence; 217 1.1 riastrad } 218 1.1 riastrad 219 1.1 riastrad static struct dma_fence *amdgpu_job_run(struct drm_sched_job *sched_job) 220 1.1 riastrad { 221 1.1 riastrad struct amdgpu_ring *ring = to_amdgpu_ring(sched_job->sched); 222 1.1 riastrad struct dma_fence *fence = NULL, *finished; 223 1.1 riastrad struct amdgpu_job *job; 224 1.1 riastrad int r = 0; 225 1.1 riastrad 226 1.1 riastrad job = to_amdgpu_job(sched_job); 227 1.1 riastrad finished = &job->base.s_fence->finished; 228 1.1 riastrad 229 1.1 riastrad BUG_ON(amdgpu_sync_peek_fence(&job->sync, NULL)); 230 1.1 riastrad 231 1.1 riastrad trace_amdgpu_sched_run_job(job); 232 1.1 riastrad 233 1.1 riastrad if (job->vram_lost_counter != atomic_read(&ring->adev->vram_lost_counter)) 234 1.1 riastrad dma_fence_set_error(finished, -ECANCELED);/* skip IB as well if VRAM lost */ 235 1.1 riastrad 236 1.1 riastrad if (finished->error < 0) { 237 1.1 riastrad DRM_INFO("Skip scheduling IBs!\n"); 238 1.1 riastrad } else { 239 1.1 riastrad r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs, job, 240 1.1 riastrad &fence); 241 1.1 riastrad if (r) 242 1.1 riastrad DRM_ERROR("Error scheduling IBs (%d)\n", r); 243 1.1 riastrad } 244 1.1 riastrad /* if gpu reset, hw fence will be replaced here */ 245 1.1 riastrad dma_fence_put(job->fence); 246 1.1 riastrad job->fence = dma_fence_get(fence); 247 1.1 riastrad 248 1.1 riastrad amdgpu_job_free_resources(job); 249 1.1 riastrad 250 1.1 riastrad fence = r ? ERR_PTR(r) : fence; 251 1.1 riastrad return fence; 252 1.1 riastrad } 253 1.1 riastrad 254 1.1 riastrad #define to_drm_sched_job(sched_job) \ 255 1.1 riastrad container_of((sched_job), struct drm_sched_job, queue_node) 256 1.1 riastrad 257 1.1 riastrad void amdgpu_job_stop_all_jobs_on_sched(struct drm_gpu_scheduler *sched) 258 1.1 riastrad { 259 1.1 riastrad struct drm_sched_job *s_job; 260 1.1 riastrad struct drm_sched_entity *s_entity = NULL; 261 1.1 riastrad int i; 262 1.1 riastrad 263 1.1 riastrad /* Signal all jobs not yet scheduled */ 264 1.1 riastrad for (i = DRM_SCHED_PRIORITY_MAX - 1; i >= DRM_SCHED_PRIORITY_MIN; i--) { 265 1.1 riastrad struct drm_sched_rq *rq = &sched->sched_rq[i]; 266 1.1 riastrad 267 1.1 riastrad if (!rq) 268 1.1 riastrad continue; 269 1.1 riastrad 270 1.1 riastrad spin_lock(&rq->lock); 271 1.1 riastrad list_for_each_entry(s_entity, &rq->entities, list) { 272 1.1 riastrad while ((s_job = to_drm_sched_job(spsc_queue_pop(&s_entity->job_queue)))) { 273 1.1 riastrad struct drm_sched_fence *s_fence = s_job->s_fence; 274 1.1 riastrad 275 1.1 riastrad dma_fence_signal(&s_fence->scheduled); 276 1.1 riastrad dma_fence_set_error(&s_fence->finished, -EHWPOISON); 277 1.1 riastrad dma_fence_signal(&s_fence->finished); 278 1.1 riastrad } 279 1.1 riastrad } 280 1.1 riastrad spin_unlock(&rq->lock); 281 1.1 riastrad } 282 1.1 riastrad 283 1.1 riastrad /* Signal all jobs already scheduled to HW */ 284 1.1 riastrad list_for_each_entry(s_job, &sched->ring_mirror_list, node) { 285 1.1 riastrad struct drm_sched_fence *s_fence = s_job->s_fence; 286 1.1 riastrad 287 1.1 riastrad dma_fence_set_error(&s_fence->finished, -EHWPOISON); 288 1.1 riastrad dma_fence_signal(&s_fence->finished); 289 1.1 riastrad } 290 1.1 riastrad } 291 1.1 riastrad 292 1.1 riastrad const struct drm_sched_backend_ops amdgpu_sched_ops = { 293 1.1 riastrad .dependency = amdgpu_job_dependency, 294 1.1 riastrad .run_job = amdgpu_job_run, 295 1.1 riastrad .timedout_job = amdgpu_job_timedout, 296 1.1 riastrad .free_job = amdgpu_job_free_cb 297 1.1 riastrad }; 298