10856cab1SChristian König /* 20856cab1SChristian König * Copyright 2015 Advanced Micro Devices, Inc. 30856cab1SChristian König * 40856cab1SChristian König * Permission is hereby granted, free of charge, to any person obtaining a 50856cab1SChristian König * copy of this software and associated documentation files (the "Software"), 60856cab1SChristian König * to deal in the Software without restriction, including without limitation 70856cab1SChristian König * the rights to use, copy, modify, merge, publish, distribute, sublicense, 80856cab1SChristian König * and/or sell copies of the Software, and to permit persons to whom the 90856cab1SChristian König * Software is furnished to do so, subject to the following conditions: 100856cab1SChristian König * 110856cab1SChristian König * The above copyright notice and this permission notice shall be included in 120856cab1SChristian König * all copies or substantial portions of the Software. 130856cab1SChristian König * 140856cab1SChristian König * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 150856cab1SChristian König * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 160856cab1SChristian König * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 170856cab1SChristian König * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 180856cab1SChristian König * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 190856cab1SChristian König * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 200856cab1SChristian König * OTHER DEALINGS IN THE SOFTWARE. 210856cab1SChristian König * 220856cab1SChristian König * 230856cab1SChristian König */ 240856cab1SChristian König #include <linux/kthread.h> 250856cab1SChristian König #include <linux/wait.h> 260856cab1SChristian König #include <linux/sched.h> 27fdf2f6c5SSam Ravnborg 28ca4e1724SAndrey Grodzovsky #include <drm/drm_drv.h> 29ca4e1724SAndrey Grodzovsky 300856cab1SChristian König #include "amdgpu.h" 310856cab1SChristian König #include "amdgpu_trace.h" 32f1549c09SLikun Gao #include "amdgpu_reset.h" 330856cab1SChristian König 34a6a1f036SLuben Tuikov static enum drm_gpu_sched_stat amdgpu_job_timedout(struct drm_sched_job *s_job) 350de2479cSMonk Liu { 363320b8d2SChristian König struct amdgpu_ring *ring = to_amdgpu_ring(s_job->sched); 373320b8d2SChristian König struct amdgpu_job *job = to_amdgpu_job(s_job); 38b8f67b9dSShashank Sharma struct amdgpu_task_info *ti; 3995a2f917SYintian Tao struct amdgpu_device *adev = ring->adev; 40ca4e1724SAndrey Grodzovsky int idx; 417258fa31SSurbhi Kakarya int r; 42ca4e1724SAndrey Grodzovsky 43c58a863bSGuchun Chen if (!drm_dev_enter(adev_to_drm(adev), &idx)) { 44ca4e1724SAndrey Grodzovsky DRM_INFO("%s - device unplugged skipping recovery on scheduler:%s", 45ca4e1724SAndrey Grodzovsky __func__, s_job->sched->name); 46ca4e1724SAndrey Grodzovsky 47ca4e1724SAndrey Grodzovsky /* Effectively the job is aborted as the device is gone */ 48ca4e1724SAndrey Grodzovsky return DRM_GPU_SCHED_STAT_ENODEV; 49ca4e1724SAndrey Grodzovsky } 500346bfd9STrigger Huang 51b8f67b9dSShashank Sharma 52194eb174SVictor Zhao adev->job_hang = true; 530e51a772SChristian König 54cc063ea2SMarek Olšák if (amdgpu_gpu_recovery && 55cc063ea2SMarek Olšák amdgpu_ring_soft_recovery(ring, job->vmid, s_job->s_fence->parent)) { 567876fa4fSChristian König DRM_ERROR("ring %s timeout, but soft recovered\n", 577876fa4fSChristian König s_job->sched->name); 58ca4e1724SAndrey Grodzovsky goto exit; 597876fa4fSChristian König } 607876fa4fSChristian König 61f024e883SChristian König DRM_ERROR("ring %s timeout, signaled seq=%u, emitted seq=%u\n", 623320b8d2SChristian König job->base.sched->name, atomic_read(&ring->fence_drv.last_seq), 633320b8d2SChristian König ring->fence_drv.sync_seq); 64b8f67b9dSShashank Sharma 65b8f67b9dSShashank Sharma ti = amdgpu_vm_get_task_info_pasid(ring->adev, job->pasid); 66b8f67b9dSShashank Sharma if (ti) { 670346bfd9STrigger Huang DRM_ERROR("Process information: process %s pid %d thread %s pid %d\n", 68b8f67b9dSShashank Sharma ti->process_name, ti->tgid, ti->task_name, ti->pid); 69b8f67b9dSShashank Sharma amdgpu_vm_put_task_info(ti); 70b8f67b9dSShashank Sharma } 714fbf87e2SMonk Liu 727a66ad6cSZhenGuo Yin dma_fence_set_error(&s_job->s_fence->finished, -ETIME); 737a66ad6cSZhenGuo Yin 7495a2f917SYintian Tao if (amdgpu_device_should_recover_gpu(ring->adev)) { 75f1549c09SLikun Gao struct amdgpu_reset_context reset_context; 76f1549c09SLikun Gao memset(&reset_context, 0, sizeof(reset_context)); 77f1549c09SLikun Gao 78f1549c09SLikun Gao reset_context.method = AMD_RESET_METHOD_NONE; 79f1549c09SLikun Gao reset_context.reset_req_dev = adev; 80f1549c09SLikun Gao clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags); 81f1549c09SLikun Gao 82f1549c09SLikun Gao r = amdgpu_device_gpu_recover(ring->adev, job, &reset_context); 837258fa31SSurbhi Kakarya if (r) 847258fa31SSurbhi Kakarya DRM_ERROR("GPU Recovery Failed: %d\n", r); 8595a2f917SYintian Tao } else { 86c3b6c607SMonk Liu drm_sched_suspend_timeout(&ring->sched); 8795a2f917SYintian Tao if (amdgpu_sriov_vf(adev)) 8895a2f917SYintian Tao adev->virt.tdr_debug = true; 8995a2f917SYintian Tao } 90ca4e1724SAndrey Grodzovsky 91ca4e1724SAndrey Grodzovsky exit: 92194eb174SVictor Zhao adev->job_hang = false; 93ca4e1724SAndrey Grodzovsky drm_dev_exit(idx); 94ca4e1724SAndrey Grodzovsky return DRM_GPU_SCHED_STAT_NOMINAL; 950de2479cSMonk Liu } 960de2479cSMonk Liu 97f7d66fb2SChristian König int amdgpu_job_alloc(struct amdgpu_device *adev, struct amdgpu_vm *vm, 98f7d66fb2SChristian König struct drm_sched_entity *entity, void *owner, 99f7d66fb2SChristian König unsigned int num_ibs, struct amdgpu_job **job) 1000856cab1SChristian König { 1010856cab1SChristian König if (num_ibs == 0) 1020856cab1SChristian König return -EINVAL; 1030856cab1SChristian König 1046103b2f2SChristian König *job = kzalloc(struct_size(*job, ibs, num_ibs), GFP_KERNEL); 1050856cab1SChristian König if (!*job) 1060856cab1SChristian König return -ENOMEM; 1070856cab1SChristian König 108a1917b73SChristian König /* 109a1917b73SChristian König * Initialize the scheduler to at least some ring so that we always 110a1917b73SChristian König * have a pointer to adev. 111a1917b73SChristian König */ 112a1917b73SChristian König (*job)->base.sched = &adev->rings[0]->sched; 113c5637837SMonk Liu (*job)->vm = vm; 1140856cab1SChristian König 1151b2d5edaSChristian König amdgpu_sync_create(&(*job)->explicit_sync); 116f88e295eSChristian König (*job)->generation = amdgpu_vm_generation(adev, vm); 117d8de8260SAndrey Grodzovsky (*job)->vm_pd_addr = AMDGPU_BO_INVALID_OFFSET; 118e86f9ceeSChristian König 119f7d66fb2SChristian König if (!entity) 1200856cab1SChristian König return 0; 121f7d66fb2SChristian König 122a78422e9SDanilo Krummrich return drm_sched_job_init(&(*job)->base, entity, 1, owner); 1230856cab1SChristian König } 1240856cab1SChristian König 125f7d66fb2SChristian König int amdgpu_job_alloc_with_ib(struct amdgpu_device *adev, 126f7d66fb2SChristian König struct drm_sched_entity *entity, void *owner, 127f7d66fb2SChristian König size_t size, enum amdgpu_ib_pool_type pool_type, 1280856cab1SChristian König struct amdgpu_job **job) 1290856cab1SChristian König { 1300856cab1SChristian König int r; 1310856cab1SChristian König 132f7d66fb2SChristian König r = amdgpu_job_alloc(adev, NULL, entity, owner, 1, job); 1330856cab1SChristian König if (r) 1340856cab1SChristian König return r; 1350856cab1SChristian König 1364624459cSChristian König (*job)->num_ibs = 1; 137c8e42d57Sxinhui pan r = amdgpu_ib_get(adev, NULL, size, pool_type, &(*job)->ibs[0]); 138f7d66fb2SChristian König if (r) { 139f7d66fb2SChristian König if (entity) 140f7d66fb2SChristian König drm_sched_job_cleanup(&(*job)->base); 1410856cab1SChristian König kfree(*job); 142f7d66fb2SChristian König } 1430856cab1SChristian König 1440856cab1SChristian König return r; 1450856cab1SChristian König } 1460856cab1SChristian König 147736ec9faSChristian König void amdgpu_job_set_resources(struct amdgpu_job *job, struct amdgpu_bo *gds, 148736ec9faSChristian König struct amdgpu_bo *gws, struct amdgpu_bo *oa) 149736ec9faSChristian König { 150736ec9faSChristian König if (gds) { 151736ec9faSChristian König job->gds_base = amdgpu_bo_gpu_offset(gds) >> PAGE_SHIFT; 152736ec9faSChristian König job->gds_size = amdgpu_bo_size(gds) >> PAGE_SHIFT; 153736ec9faSChristian König } 154736ec9faSChristian König if (gws) { 155736ec9faSChristian König job->gws_base = amdgpu_bo_gpu_offset(gws) >> PAGE_SHIFT; 156736ec9faSChristian König job->gws_size = amdgpu_bo_size(gws) >> PAGE_SHIFT; 157736ec9faSChristian König } 158736ec9faSChristian König if (oa) { 159736ec9faSChristian König job->oa_base = amdgpu_bo_gpu_offset(oa) >> PAGE_SHIFT; 160736ec9faSChristian König job->oa_size = amdgpu_bo_size(oa) >> PAGE_SHIFT; 161736ec9faSChristian König } 162736ec9faSChristian König } 163736ec9faSChristian König 164a5fb4ec2SChristian König void amdgpu_job_free_resources(struct amdgpu_job *job) 1650856cab1SChristian König { 166a1917b73SChristian König struct amdgpu_ring *ring = to_amdgpu_ring(job->base.sched); 167f54d1867SChris Wilson struct dma_fence *f; 1681ab0d211SChristian König unsigned i; 1691ab0d211SChristian König 17074ea8e78SChristian König /* Check if any fences where initialized */ 17174ea8e78SChristian König if (job->base.s_fence && job->base.s_fence->finished.ops) 17274ea8e78SChristian König f = &job->base.s_fence->finished; 17374ea8e78SChristian König else if (job->hw_fence.ops) 17474ea8e78SChristian König f = &job->hw_fence; 17574ea8e78SChristian König else 17674ea8e78SChristian König f = NULL; 17774ea8e78SChristian König 1780856cab1SChristian König for (i = 0; i < job->num_ibs; ++i) 179a1917b73SChristian König amdgpu_ib_free(ring->adev, &job->ibs[i], f); 1800856cab1SChristian König } 1810856cab1SChristian König 1821b1f42d8SLucas Stach static void amdgpu_job_free_cb(struct drm_sched_job *s_job) 183b6723c8dSMonk Liu { 1843320b8d2SChristian König struct amdgpu_job *job = to_amdgpu_job(s_job); 185c5f74f78SChristian König 18626efecf9SSharat Masetty drm_sched_job_cleanup(s_job); 18726efecf9SSharat Masetty 1881b2d5edaSChristian König amdgpu_sync_free(&job->explicit_sync); 189c530b02fSJack Zhang 1903cb93f39SStanley.Yang /* only put the hw fence if has embedded fence */ 1913cb93f39SStanley.Yang if (!job->hw_fence.ops) 1923cb93f39SStanley.Yang kfree(job); 1933cb93f39SStanley.Yang else 194c530b02fSJack Zhang dma_fence_put(&job->hw_fence); 195b6723c8dSMonk Liu } 196b6723c8dSMonk Liu 19768ce8b24SChristian König void amdgpu_job_set_gang_leader(struct amdgpu_job *job, 19868ce8b24SChristian König struct amdgpu_job *leader) 19968ce8b24SChristian König { 20068ce8b24SChristian König struct dma_fence *fence = &leader->base.s_fence->scheduled; 20168ce8b24SChristian König 20268ce8b24SChristian König WARN_ON(job->gang_submit); 20368ce8b24SChristian König 20468ce8b24SChristian König /* 20568ce8b24SChristian König * Don't add a reference when we are the gang leader to avoid circle 20668ce8b24SChristian König * dependency. 20768ce8b24SChristian König */ 20868ce8b24SChristian König if (job != leader) 20968ce8b24SChristian König dma_fence_get(fence); 21068ce8b24SChristian König job->gang_submit = fence; 21168ce8b24SChristian König } 21268ce8b24SChristian König 2131e24e31fSChristian König void amdgpu_job_free(struct amdgpu_job *job) 2141e24e31fSChristian König { 215f7d66fb2SChristian König if (job->base.entity) 216f7d66fb2SChristian König drm_sched_job_cleanup(&job->base); 217f7d66fb2SChristian König 2181e24e31fSChristian König amdgpu_job_free_resources(job); 2191b2d5edaSChristian König amdgpu_sync_free(&job->explicit_sync); 22068ce8b24SChristian König if (job->gang_submit != &job->base.s_fence->scheduled) 22168ce8b24SChristian König dma_fence_put(job->gang_submit); 222c530b02fSJack Zhang 2232581c5d8SYuBiao Wang if (!job->hw_fence.ops) 2242581c5d8SYuBiao Wang kfree(job); 2252581c5d8SYuBiao Wang else 226c530b02fSJack Zhang dma_fence_put(&job->hw_fence); 2271e24e31fSChristian König } 2281e24e31fSChristian König 229f7d66fb2SChristian König struct dma_fence *amdgpu_job_submit(struct amdgpu_job *job) 2300856cab1SChristian König { 231f7d66fb2SChristian König struct dma_fence *f; 2320856cab1SChristian König 233dbe48d03SDaniel Vetter drm_sched_job_arm(&job->base); 234f7d66fb2SChristian König f = dma_fence_get(&job->base.s_fence->finished); 235a5fb4ec2SChristian König amdgpu_job_free_resources(job); 2360e10e9a1SDaniel Vetter drm_sched_entity_push_job(&job->base); 2370856cab1SChristian König 238f7d66fb2SChristian König return f; 2390856cab1SChristian König } 2400856cab1SChristian König 241ee913fd9SChristian König int amdgpu_job_submit_direct(struct amdgpu_job *job, struct amdgpu_ring *ring, 242ee913fd9SChristian König struct dma_fence **fence) 243ee913fd9SChristian König { 244ee913fd9SChristian König int r; 245ee913fd9SChristian König 246ee913fd9SChristian König job->base.sched = &ring->sched; 247f6a3f660SAndrey Grodzovsky r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs, job, fence); 248f6a3f660SAndrey Grodzovsky 249ee913fd9SChristian König if (r) 250ee913fd9SChristian König return r; 251ee913fd9SChristian König 252ee913fd9SChristian König amdgpu_job_free(job); 253ee913fd9SChristian König return 0; 254ee913fd9SChristian König } 255ee913fd9SChristian König 256940ca22bSChristian König static struct dma_fence * 257a82f30b0SChristian König amdgpu_job_prepare_job(struct drm_sched_job *sched_job, 2581b1f42d8SLucas Stach struct drm_sched_entity *s_entity) 2590856cab1SChristian König { 260068c3304SNayan Deshmukh struct amdgpu_ring *ring = to_amdgpu_ring(s_entity->rq->sched); 2610856cab1SChristian König struct amdgpu_job *job = to_amdgpu_job(sched_job); 2621728baa7SChristian König struct dma_fence *fence = NULL; 263df83d1ebSChunming Zhou int r; 2640856cab1SChristian König 265e84e697dSChristian König /* Ignore soft recovered fences here */ 266e84e697dSChristian König r = drm_sched_entity_error(s_entity); 267e84e697dSChristian König if (r && r != -ENODATA) 268e84e697dSChristian König goto error; 269e84e697dSChristian König 270b09d6acbSChristian König if (!fence && job->gang_submit) 271b09d6acbSChristian König fence = amdgpu_device_switch_gang(ring->adev, job->gang_submit); 272b09d6acbSChristian König 273940ca22bSChristian König while (!fence && job->vm && !job->vmid) { 274940ca22bSChristian König r = amdgpu_vmid_grab(job->vm, ring, job, &fence); 275e84e697dSChristian König if (r) { 2760856cab1SChristian König DRM_ERROR("Error getting VM ID (%d)\n", r); 277e84e697dSChristian König goto error; 278e84e697dSChristian König } 2790856cab1SChristian König } 2800856cab1SChristian König 2810856cab1SChristian König return fence; 282e84e697dSChristian König 283e84e697dSChristian König error: 284e84e697dSChristian König dma_fence_set_error(&job->base.s_fence->finished, r); 285e84e697dSChristian König return NULL; 2860856cab1SChristian König } 2870856cab1SChristian König 2881b1f42d8SLucas Stach static struct dma_fence *amdgpu_job_run(struct drm_sched_job *sched_job) 2890856cab1SChristian König { 2903320b8d2SChristian König struct amdgpu_ring *ring = to_amdgpu_ring(sched_job->sched); 29168ce8b24SChristian König struct amdgpu_device *adev = ring->adev; 29248f05f29SMonk Liu struct dma_fence *fence = NULL, *finished; 2930856cab1SChristian König struct amdgpu_job *job; 294db5e65fcSAndrey Grodzovsky int r = 0; 2950856cab1SChristian König 2960856cab1SChristian König job = to_amdgpu_job(sched_job); 29748f05f29SMonk Liu finished = &job->base.s_fence->finished; 298e86f9ceeSChristian König 2990856cab1SChristian König trace_amdgpu_sched_run_job(job); 30048f05f29SMonk Liu 30168ce8b24SChristian König /* Skip job if VRAM is lost and never resubmit gangs */ 302f88e295eSChristian König if (job->generation != amdgpu_vm_generation(adev, job->vm) || 30368ce8b24SChristian König (job->job_run_counter && job->gang_submit)) 30468ce8b24SChristian König dma_fence_set_error(finished, -ECANCELED); 305f1403342SChristian König 306f1403342SChristian König if (finished->error < 0) { 307*cd409dbcSLijo Lazar dev_dbg(adev->dev, "Skip scheduling IBs in ring(%s)", 308*cd409dbcSLijo Lazar ring->name); 309f1403342SChristian König } else { 3103320b8d2SChristian König r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs, job, 31114e47f93SChristian König &fence); 31222a77cf6SChristian König if (r) 313*cd409dbcSLijo Lazar dev_err(adev->dev, 314*cd409dbcSLijo Lazar "Error scheduling IBs (%d) in ring(%s)", r, 315*cd409dbcSLijo Lazar ring->name); 31615d73ce6SChunming Zhou } 317b2ff0e8aSAndres Rodriguez 318c530b02fSJack Zhang job->job_run_counter++; 31922a77cf6SChristian König amdgpu_job_free_resources(job); 320db5e65fcSAndrey Grodzovsky 321db5e65fcSAndrey Grodzovsky fence = r ? ERR_PTR(r) : fence; 3220856cab1SChristian König return fence; 3230856cab1SChristian König } 3240856cab1SChristian König 3257c6e68c7SAndrey Grodzovsky #define to_drm_sched_job(sched_job) \ 3267c6e68c7SAndrey Grodzovsky container_of((sched_job), struct drm_sched_job, queue_node) 3277c6e68c7SAndrey Grodzovsky 3287c6e68c7SAndrey Grodzovsky void amdgpu_job_stop_all_jobs_on_sched(struct drm_gpu_scheduler *sched) 3297c6e68c7SAndrey Grodzovsky { 3307c6e68c7SAndrey Grodzovsky struct drm_sched_job *s_job; 3317c6e68c7SAndrey Grodzovsky struct drm_sched_entity *s_entity = NULL; 3327c6e68c7SAndrey Grodzovsky int i; 3337c6e68c7SAndrey Grodzovsky 3347c6e68c7SAndrey Grodzovsky /* Signal all jobs not yet scheduled */ 33538f922a5SLuben Tuikov for (i = DRM_SCHED_PRIORITY_KERNEL; i < sched->num_rqs; i++) { 33656e44960SLuben Tuikov struct drm_sched_rq *rq = sched->sched_rq[i]; 3377c6e68c7SAndrey Grodzovsky spin_lock(&rq->lock); 3387c6e68c7SAndrey Grodzovsky list_for_each_entry(s_entity, &rq->entities, list) { 3397c6e68c7SAndrey Grodzovsky while ((s_job = to_drm_sched_job(spsc_queue_pop(&s_entity->job_queue)))) { 3407c6e68c7SAndrey Grodzovsky struct drm_sched_fence *s_fence = s_job->s_fence; 3417c6e68c7SAndrey Grodzovsky 3427c6e68c7SAndrey Grodzovsky dma_fence_signal(&s_fence->scheduled); 3437c6e68c7SAndrey Grodzovsky dma_fence_set_error(&s_fence->finished, -EHWPOISON); 3447c6e68c7SAndrey Grodzovsky dma_fence_signal(&s_fence->finished); 3457c6e68c7SAndrey Grodzovsky } 3467c6e68c7SAndrey Grodzovsky } 3477c6e68c7SAndrey Grodzovsky spin_unlock(&rq->lock); 3487c6e68c7SAndrey Grodzovsky } 3497c6e68c7SAndrey Grodzovsky 3507c6e68c7SAndrey Grodzovsky /* Signal all jobs already scheduled to HW */ 3516efa4b46SLuben Tuikov list_for_each_entry(s_job, &sched->pending_list, list) { 3527c6e68c7SAndrey Grodzovsky struct drm_sched_fence *s_fence = s_job->s_fence; 3537c6e68c7SAndrey Grodzovsky 3547c6e68c7SAndrey Grodzovsky dma_fence_set_error(&s_fence->finished, -EHWPOISON); 3557c6e68c7SAndrey Grodzovsky dma_fence_signal(&s_fence->finished); 3567c6e68c7SAndrey Grodzovsky } 3577c6e68c7SAndrey Grodzovsky } 3587c6e68c7SAndrey Grodzovsky 3591b1f42d8SLucas Stach const struct drm_sched_backend_ops amdgpu_sched_ops = { 360a82f30b0SChristian König .prepare_job = amdgpu_job_prepare_job, 3610856cab1SChristian König .run_job = amdgpu_job_run, 3620e51a772SChristian König .timedout_job = amdgpu_job_timedout, 363c5f74f78SChristian König .free_job = amdgpu_job_free_cb 3640856cab1SChristian König }; 365